Skip to content

Commit 3499d99

Browse files
committed
Refactor marshaller method signatures and field usage
Removed redundant local variables and replaced them with direct usage of existing type and reference properties in marshaller method definitions. Updated method signatures and field assignments for clarity and consistency across Delegate, KeyValuePair, and Interface marshaller builders.
1 parent 4f7368b commit 3499d99

File tree

3 files changed

+15
-33
lines changed

3 files changed

+15
-33
lines changed

src/WinRT.Interop.Generator/Builders/InteropTypeDefinitionBuilder.Delegate.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -640,7 +640,6 @@ public static void Marshaller(
640640
module.TopLevelTypes.Add(marshallerType);
641641

642642
// Prepare the external types we need in the implemented methods
643-
TypeSignature delegateType2 = delegateType;
644643
TypeSignature windowsRuntimeObjectReferenceValueType = interopReferences.WindowsRuntimeObjectReferenceValue.ToValueTypeSignature();
645644

646645
// Define the 'ConvertToUnmanaged' method as follows:
@@ -651,7 +650,7 @@ public static void Marshaller(
651650
attributes: MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig,
652651
signature: MethodSignature.CreateStatic(
653652
returnType: windowsRuntimeObjectReferenceValueType,
654-
parameterTypes: [delegateType2]))
653+
parameterTypes: [delegateType]))
655654
{
656655
CilInstructions =
657656
{
@@ -676,14 +675,14 @@ public static void Marshaller(
676675
name: "ConvertToManaged"u8,
677676
attributes: MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig,
678677
signature: MethodSignature.CreateStatic(
679-
returnType: delegateType2,
678+
returnType: delegateType,
680679
parameterTypes: [module.CorLibTypeFactory.Void.MakePointerType()]))
681680
{
682681
CilInstructions =
683682
{
684683
{ Ldarg_0 },
685684
{ Call, windowsRuntimeDelegateMarshallerConvertToManaged },
686-
{ Castclass, delegateType2.ToTypeDefOrRef() },
685+
{ Castclass, delegateType.ToTypeDefOrRef() },
687686
{ Ret }
688687
}
689688
};
@@ -698,7 +697,7 @@ public static void Marshaller(
698697
attributes: MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig,
699698
signature: MethodSignature.CreateStatic(
700699
returnType: windowsRuntimeObjectReferenceValueType,
701-
parameterTypes: [delegateType2]))
700+
parameterTypes: [delegateType]))
702701
{
703702
CilInstructions =
704703
{
@@ -723,7 +722,7 @@ public static void Marshaller(
723722
name: "UnboxToManaged"u8,
724723
attributes: MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig,
725724
signature: MethodSignature.CreateStatic(
726-
returnType: delegateType2,
725+
returnType: delegateType,
727726
parameterTypes: [module.CorLibTypeFactory.Void.MakePointerType()]))
728727
{
729728
CilInstructions =

src/WinRT.Interop.Generator/Builders/InteropTypeDefinitionBuilder.KeyValuePair.cs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,19 +47,15 @@ public static void Marshaller(
4747
// Track the type (it may be needed to marshal parameters or return values)
4848
emitState.TrackTypeDefinition(marshallerType, keyValuePairType, "Marshaller");
4949

50-
// Prepare the external types we need in the implemented methods
51-
TypeSignature typeSignature2 = keyValuePairType;
52-
TypeSignature windowsRuntimeObjectReferenceValueType = interopReferences.WindowsRuntimeObjectReferenceValue.ToValueTypeSignature();
53-
5450
// Define the 'ConvertToUnmanaged' method as follows:
5551
//
5652
// public static WindowsRuntimeObjectReferenceValue ConvertToUnmanaged(<KEY_VALUE_PAIR_TYPE> value)
5753
MethodDefinition convertToUnmanagedMethod = new(
5854
name: "ConvertToUnmanaged"u8,
5955
attributes: MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig,
6056
signature: MethodSignature.CreateStatic(
61-
returnType: windowsRuntimeObjectReferenceValueType,
62-
parameterTypes: [typeSignature2]))
57+
returnType: interopReferences.WindowsRuntimeObjectReferenceValue.ToValueTypeSignature(),
58+
parameterTypes: [keyValuePairType]))
6359
{
6460
CilInstructions =
6561
{
@@ -77,7 +73,7 @@ public static void Marshaller(
7773
name: "ConvertToManaged"u8,
7874
attributes: MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig,
7975
signature: MethodSignature.CreateStatic(
80-
returnType: typeSignature2,
76+
returnType: keyValuePairType,
8177
parameterTypes: [module.CorLibTypeFactory.Void.MakePointerType()]))
8278
{
8379
CilInstructions =

src/WinRT.Interop.Generator/Builders/InteropTypeDefinitionBuilder.cs

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -303,28 +303,21 @@ private static void Marshaller(
303303

304304
module.TopLevelTypes.Add(marshallerType);
305305

306-
// Prepare the external types we need in the implemented methods
307-
TypeSignature typeSignature2 = typeSignature;
308-
TypeSignature windowsRuntimeObjectReferenceValueType = interopReferences.WindowsRuntimeObjectReferenceValue.ToValueTypeSignature();
309-
310-
// Reference the instantiated 'ConvertToUnmanaged' method for the marshaller
311-
MemberReference windowsRuntimeInterfaceMarshallerConvertToUnmanaged = interopReferences.WindowsRuntimeInterfaceMarshallerConvertToUnmanaged(typeSignature);
312-
313306
// Define the 'ConvertToUnmanaged' method as follows:
314307
//
315308
// public static WindowsRuntimeObjectReferenceValue ConvertToUnmanaged(<INTERFACE_TYPE> value)
316309
MethodDefinition convertToUnmanagedMethod = new(
317310
name: "ConvertToUnmanaged"u8,
318311
attributes: MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig,
319312
signature: MethodSignature.CreateStatic(
320-
returnType: windowsRuntimeObjectReferenceValueType,
321-
parameterTypes: [typeSignature2]))
313+
returnType: interopReferences.WindowsRuntimeObjectReferenceValue.ToValueTypeSignature(),
314+
parameterTypes: [typeSignature]))
322315
{
323316
CilInstructions =
324317
{
325318
{ Ldarg_0 },
326319
{ Call, get_IidMethod },
327-
{ Call, windowsRuntimeInterfaceMarshallerConvertToUnmanaged },
320+
{ Call, interopReferences.WindowsRuntimeInterfaceMarshallerConvertToUnmanaged(typeSignature) },
328321
{ Ret }
329322
}
330323
};
@@ -343,14 +336,14 @@ private static void Marshaller(
343336
name: "ConvertToManaged"u8,
344337
attributes: MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig,
345338
signature: MethodSignature.CreateStatic(
346-
returnType: typeSignature2,
339+
returnType: typeSignature,
347340
parameterTypes: [module.CorLibTypeFactory.Void.MakePointerType()]))
348341
{
349342
CilInstructions =
350343
{
351344
{ Ldarg_0 },
352345
{ Call, windowsRuntimeUnsealedObjectMarshallerConvertToManaged },
353-
{ Castclass, typeSignature2.ToTypeDefOrRef() },
346+
{ Castclass, typeSignature.ToTypeDefOrRef() },
354347
{ Ret }
355348
}
356349
};
@@ -596,12 +589,6 @@ private static void InterfaceEntriesImpl<TArg>(
596589
// Create the static constructor to initialize the interface entries
597590
MethodDefinition cctor = implType.GetOrCreateStaticConstructor(module);
598591

599-
// Import the target fields (they have to be in the module, or the resulting assembly won't be valid):
600-
// - [0]: Guid IID
601-
// - [1]: nint Vtable
602-
IFieldDescriptor comInterfaceEntryIIDField = interopReferences.ComInterfaceEntryIID;
603-
IFieldDescriptor comInterfaceEntryVtableField = interopReferences.ComInterfaceEntryVtable;
604-
605592
// We need to create a new method body bound to this constructor
606593
CilInstructionCollection cctorInstructions = cctor.CilMethodBody!.Instructions;
607594

@@ -622,14 +609,14 @@ private static void InterfaceEntriesImpl<TArg>(
622609
// Invoke the callback to emit code to load 'IID' on the evaluation stack
623610
get_IID(implTypes[i], cctorInstructions, interopReferences, module);
624611

625-
_ = cctorInstructions.Add(Stfld, comInterfaceEntryIIDField);
612+
_ = cctorInstructions.Add(Stfld, interopReferences.ComInterfaceEntryIID);
626613
_ = cctorInstructions.Add(Ldsflda, entriesField);
627614
_ = cctorInstructions.Add(Ldflda, entriesFieldType.Fields[i]);
628615

629616
// Same as above, but to get the vtable pointer on the stack
630617
get_Vtable(implTypes[i], cctorInstructions, interopReferences, module);
631618

632-
_ = cctorInstructions.Add(Stfld, comInterfaceEntryVtableField);
619+
_ = cctorInstructions.Add(Stfld, interopReferences.ComInterfaceEntryVtable);
633620
}
634621

635622
_ = cctorInstructions.Add(Ret);

0 commit comments

Comments
 (0)