Skip to content

Commit 8394812

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 69170d7 commit 8394812

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
@@ -139,10 +139,6 @@ public static void Marshaller(
139139
// Track the type (it may be needed to marshal parameters or return values)
140140
emitState.TrackTypeDefinition(marshallerType, keyValuePairType, "Marshaller");
141141

142-
// Prepare the external types we need in the implemented methods
143-
TypeSignature typeSignature2 = keyValuePairType;
144-
TypeSignature windowsRuntimeObjectReferenceValueType = interopReferences.WindowsRuntimeObjectReferenceValue.ToValueTypeSignature();
145-
146142
// Determine which 'CreateComInterfaceFlags' flags we use for the marshalled CCW
147143
CreateComInterfaceFlags flags = keyValuePairType.IsTrackerSupportRequired(interopReferences)
148144
? CreateComInterfaceFlags.TrackerSupport
@@ -155,8 +151,8 @@ public static void Marshaller(
155151
name: "ConvertToUnmanaged"u8,
156152
attributes: MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig,
157153
signature: MethodSignature.CreateStatic(
158-
returnType: windowsRuntimeObjectReferenceValueType,
159-
parameterTypes: [typeSignature2]))
154+
returnType: interopReferences.WindowsRuntimeObjectReferenceValue.ToValueTypeSignature(),
155+
parameterTypes: [keyValuePairType]))
160156
{
161157
CilInstructions =
162158
{
@@ -185,7 +181,7 @@ public static void Marshaller(
185181
name: "ConvertToManaged"u8,
186182
attributes: MethodAttributes.Public | MethodAttributes.Static | MethodAttributes.HideBySig,
187183
signature: MethodSignature.CreateStatic(
188-
returnType: typeSignature2,
184+
returnType: keyValuePairType,
189185
parameterTypes: [module.CorLibTypeFactory.Void.MakePointerType()]))
190186
{
191187
CilLocalVariables = { loc_0_default },

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)