@@ -30,8 +30,7 @@ internal static BlobBuilder GetFieldSignature(Type fieldType, Type[] requiredCus
3030 {
3131 BlobBuilder fieldSignature = new ( ) ;
3232 FieldTypeEncoder encoder = new BlobEncoder ( fieldSignature ) . Field ( ) ;
33- WriteReturnTypeCustomModifiers ( encoder . CustomModifiers ( ) , requiredCustomModifiers , optionalCustomModifiers , module ) ;
34- WriteSignatureForType ( encoder . Type ( ) , fieldType , module ) ;
33+ WriteSignatureForType ( encoder . Type ( ) , fieldType , module , requiredCustomModifiers , optionalCustomModifiers ) ;
3534
3635 return fieldSignature ;
3736 }
@@ -48,7 +47,7 @@ internal static BlobBuilder GetConstructorSignature(ParameterInfo[]? parameters,
4847
4948 retType . Void ( ) ;
5049
51- WriteParametersSignature ( module , Array . ConvertAll ( parameters , p => p . ParameterType ) , parameterEncoder ) ;
50+ WriteParametersSignature ( module , GetParameterTypes ( parameters ) , parameterEncoder ) ;
5251
5352 return constructorSignature ;
5453 }
@@ -85,16 +84,8 @@ internal static BlobBuilder GetMethodSignature(ModuleBuilderImpl module, Type[]?
8584 new BlobEncoder ( methodSignature ) . MethodSignature ( convention , genParamCount , isInstance ) .
8685 Parameters ( paramsLength , out ReturnTypeEncoder retEncoder , out ParametersEncoder parEncoder ) ;
8786
88- WriteReturnTypeCustomModifiers ( retEncoder . CustomModifiers ( ) , returnTypeRequiredModifiers , returnTypeOptionalModifiers , module ) ;
89-
90- if ( returnType != null && returnType != module . GetTypeFromCoreAssembly ( CoreTypeId . Void ) )
91- {
92- WriteSignatureForType ( retEncoder . Type ( ) , returnType , module ) ;
93- }
94- else
95- {
96- retEncoder . Void ( ) ;
97- }
87+ returnType ??= module . GetTypeFromCoreAssembly ( CoreTypeId . Void ) ;
88+ WriteSignatureForType ( retEncoder . Type ( ) , returnType , module , returnTypeRequiredModifiers , returnTypeOptionalModifiers ) ;
9889
9990 WriteParametersSignature ( module , parameters , parEncoder , parameterRequiredModifiers , parameterOptionalModifiers ) ;
10091
@@ -106,24 +97,29 @@ internal static BlobBuilder GetMethodSignature(ModuleBuilderImpl module, Type[]?
10697 return methodSignature ;
10798 }
10899
109- private static void WriteReturnTypeCustomModifiers ( CustomModifiersEncoder encoder ,
110- Type [ ] ? requiredModifiers , Type [ ] ? optionalModifiers , ModuleBuilderImpl module )
100+ internal static Type [ ] GetParameterTypes ( ParameterInfo [ ] parameterInfos )
111101 {
112- if ( requiredModifiers != null )
102+ if ( parameterInfos . Length == 0 )
113103 {
114- WriteCustomModifiers ( encoder , requiredModifiers , isOptional : false , module ) ;
104+ return Type . EmptyTypes ;
115105 }
116106
117- if ( optionalModifiers != null )
107+ Type [ ] parameterTypes = new Type [ parameterInfos . Length ] ;
108+
109+ for ( int i = 0 ; i < parameterInfos . Length ; i ++ )
118110 {
119- WriteCustomModifiers ( encoder , optionalModifiers , isOptional : true , module ) ;
111+ parameterTypes [ i ] = parameterInfos [ i ] . GetModifiedParameterType ( ) ;
120112 }
113+
114+ return parameterTypes ;
121115 }
122116
123117 private static void WriteCustomModifiers ( CustomModifiersEncoder encoder , Type [ ] customModifiers , bool isOptional , ModuleBuilderImpl module )
124118 {
125- foreach ( Type modifier in customModifiers )
119+ // GetOptionalCustomModifiers and GetRequiredCustomModifiers return modifiers in reverse order
120+ for ( int i = customModifiers . Length - 1 ; i >= 0 ; i -- )
126121 {
122+ Type modifier = customModifiers [ i ] ;
127123 encoder . AddModifier ( module . GetTypeHandle ( modifier ) , isOptional ) ;
128124 }
129125 }
@@ -137,17 +133,10 @@ private static void WriteParametersSignature(ModuleBuilderImpl module, Type[]? p
137133 {
138134 ParameterTypeEncoder encoder = parameterEncoder . AddParameter ( ) ;
139135
140- if ( requiredModifiers != null && requiredModifiers . Length > i && requiredModifiers [ i ] != null )
141- {
142- WriteCustomModifiers ( encoder . CustomModifiers ( ) , requiredModifiers [ i ] , isOptional : false , module ) ;
143- }
144-
145- if ( optionalModifiers != null && optionalModifiers . Length > i && optionalModifiers [ i ] != null )
146- {
147- WriteCustomModifiers ( encoder . CustomModifiers ( ) , optionalModifiers [ i ] , isOptional : true , module ) ;
148- }
136+ Type [ ] ? modreqs = ( requiredModifiers != null && requiredModifiers . Length > i ) ? requiredModifiers [ i ] : null ;
137+ Type [ ] ? modopts = ( optionalModifiers != null && optionalModifiers . Length > i ) ? optionalModifiers [ i ] : null ;
149138
150- WriteSignatureForType ( encoder . Type ( ) , parameters [ i ] , module ) ;
139+ WriteSignatureForType ( encoder . Type ( ) , parameters [ i ] , module , modreqs , modopts ) ;
151140 }
152141 }
153142 }
@@ -160,15 +149,16 @@ internal static BlobBuilder GetPropertySignature(PropertyBuilderImpl property, M
160149 PropertySignature ( isInstanceProperty : property . CallingConventions . HasFlag ( CallingConventions . HasThis ) ) .
161150 Parameters ( property . ParameterTypes == null ? 0 : property . ParameterTypes . Length , out ReturnTypeEncoder retType , out ParametersEncoder paramEncoder ) ;
162151
163- WriteReturnTypeCustomModifiers ( retType . CustomModifiers ( ) , property . _returnTypeRequiredCustomModifiers , property . _returnTypeOptionalCustomModifiers , module ) ;
164- WriteSignatureForType ( retType . Type ( ) , property . PropertyType , module ) ;
152+ WriteSignatureForType ( retType . Type ( ) , property . PropertyType , module , property . _returnTypeRequiredCustomModifiers , property . _returnTypeOptionalCustomModifiers ) ;
165153 WriteParametersSignature ( module , property . ParameterTypes , paramEncoder , property . _parameterTypeRequiredCustomModifiers , property . _parameterTypeOptionalCustomModifiers ) ;
166154
167155 return propertySignature ;
168156 }
169157
170- private static void WriteSignatureForType ( SignatureTypeEncoder signature , Type type , ModuleBuilderImpl module )
158+ private static void WriteSignatureForType ( SignatureTypeEncoder signature , Type type , ModuleBuilderImpl module , Type [ ] ? requiredModifiers = null , Type [ ] ? optionalModifiers = null )
171159 {
160+ WriteCustomModifiers ( signature . CustomModifiers ( ) , requiredModifiers ?? type . GetRequiredCustomModifiers ( ) , isOptional : false , module ) ;
161+ WriteCustomModifiers ( signature . CustomModifiers ( ) , optionalModifiers ?? type . GetOptionalCustomModifiers ( ) , isOptional : true , module ) ;
172162 if ( type . IsArray )
173163 {
174164 Type elementType = type . GetElementType ( ) ! ;
@@ -180,8 +170,8 @@ private static void WriteSignatureForType(SignatureTypeEncoder signature, Type t
180170 else
181171 {
182172 signature . Array ( out SignatureTypeEncoder elTypeSignature , out ArrayShapeEncoder arrayEncoder ) ;
183- WriteSimpleSignature ( elTypeSignature , elementType , module ) ;
184- arrayEncoder . Shape ( type . GetArrayRank ( ) , ImmutableArray . Create < int > ( ) , ImmutableArray . Create < int > ( new int [ rank ] ) ) ;
173+ WriteSignatureForType ( elTypeSignature , elementType , module ) ;
174+ arrayEncoder . Shape ( type . GetArrayRank ( ) , [ ] , default ) ;
185175 }
186176 }
187177 else if ( type . IsPointer )
@@ -223,14 +213,64 @@ private static void WriteSignatureForType(SignatureTypeEncoder signature, Type t
223213 {
224214 signature . GenericTypeParameter ( type . GenericParameterPosition ) ;
225215 }
216+ else if ( type . IsFunctionPointer )
217+ {
218+ WriteSignatureForFunctionPointerType ( signature , type , module ) ;
219+ }
226220 else
227221 {
228222 WriteSimpleSignature ( signature , type , module ) ;
229223 }
230224 }
231225
226+ private static void WriteSignatureForFunctionPointerType ( SignatureTypeEncoder signature , Type type , ModuleBuilderImpl module )
227+ {
228+ SignatureCallingConvention callConv = SignatureCallingConvention . Default ;
229+ FunctionPointerAttributes attribs = FunctionPointerAttributes . None ;
230+
231+ Type returnType = type . GetFunctionPointerReturnType ( ) ;
232+ Type [ ] paramTypes = type . GetFunctionPointerParameterTypes ( ) ;
233+
234+ if ( type . IsUnmanagedFunctionPointer )
235+ {
236+ callConv = SignatureCallingConvention . Unmanaged ;
237+
238+ if ( type . GetFunctionPointerCallingConventions ( ) is Type [ ] conventions && conventions . Length == 1 )
239+ {
240+ switch ( conventions [ 0 ] . FullName )
241+ {
242+ case "System.Runtime.CompilerServices.CallConvCdecl" :
243+ callConv = SignatureCallingConvention . CDecl ;
244+ break ;
245+ case "System.Runtime.CompilerServices.CallConvStdcall" :
246+ callConv = SignatureCallingConvention . StdCall ;
247+ break ;
248+ case "System.Runtime.CompilerServices.CallConvThiscall" :
249+ callConv = SignatureCallingConvention . ThisCall ;
250+ break ;
251+ case "System.Runtime.CompilerServices.CallConvFastcall" :
252+ callConv = SignatureCallingConvention . FastCall ;
253+ break ;
254+ }
255+ }
256+ }
257+
258+ MethodSignatureEncoder sigEncoder = signature . FunctionPointer ( callConv , attribs ) ;
259+ sigEncoder . Parameters ( paramTypes . Length , out ReturnTypeEncoder retTypeEncoder , out ParametersEncoder paramsEncoder ) ;
260+
261+ WriteSignatureForType ( retTypeEncoder . Type ( ) , returnType , module ) ;
262+
263+ foreach ( Type paramType in paramTypes )
264+ {
265+ ParameterTypeEncoder paramEncoder = paramsEncoder . AddParameter ( ) ;
266+
267+ WriteSignatureForType ( paramEncoder . Type ( ) , paramType , module ) ;
268+ }
269+ }
270+
232271 private static void WriteSimpleSignature ( SignatureTypeEncoder signature , Type type , ModuleBuilderImpl module )
233272 {
273+ type = type . UnderlyingSystemType ;
234274 CoreTypeId ? typeId = module . GetTypeIdFromCoreTypes ( type ) ;
235275
236276 switch ( typeId )
0 commit comments