13
13
using Microsoft . CodeAnalysis . Operations ;
14
14
using REFrameworkNET ;
15
15
using System ;
16
- using System . ComponentModel . DataAnnotations ;
17
-
18
- /*public interface CrappyTest {
19
- public string Concat(object arg0);
20
-
21
- public string Concat(object arg0, object arg1);
22
-
23
- public string Concat(object arg0, object arg1, object arg2);
24
-
25
- public string Concat(global::System.Object[] args);
26
-
27
- public string Concat(string str0, string str1);
28
-
29
- public string Concat(string str0, string str1, string str2);
30
-
31
- public string Concat(string str0, string str1, string str2, string str3);
32
-
33
- public string Concat(object str0, object str1);
34
-
35
- public string Concat(object str0, object str1, object str2);
36
-
37
- public string Concat(object str0, object str1, object str2, object str3);
38
-
39
- public string Concat(global::System.String[] values);
40
-
41
- public string Format(string format, object arg0);
42
- }*/
43
16
44
17
public class ClassGenerator {
45
18
private string className ;
@@ -49,6 +22,8 @@ public class ClassGenerator {
49
22
private TypeDeclarationSyntax ? typeDeclaration ;
50
23
private bool addedNewKeyword = false ;
51
24
25
+ private List < FieldDeclarationSyntax > internalFieldDeclarations = [ ] ;
26
+
52
27
public TypeDeclarationSyntax ? TypeDeclaration {
53
28
get {
54
29
return typeDeclaration ;
@@ -91,37 +66,6 @@ public ClassGenerator(string className_, REFrameworkNET.TypeDefinition t_) {
91
66
}
92
67
93
68
private static TypeSyntax MakeProperType ( REFrameworkNET . TypeDefinition ? targetType , REFrameworkNET . TypeDefinition ? containingType ) {
94
- if ( containingType != null && targetType != null ) {
95
- if ( containingType . FullName . StartsWith ( "System." ) ) {
96
- /*var containingRtType = containingType.GetRuntimeType();
97
- var targetRtType = targetType.GetRuntimeType();
98
-
99
- if (containingRtType != null && targetRtType != null) {
100
- var containingRtAssembly = (ManagedObject)containingRtType.Call("get_Assembly");
101
- var targetRtAssembly = (ManagedObject)targetRtType.Call("get_Assembly");
102
-
103
- if ((string)containingRtAssembly.Call("get_FullName") != (string)targetRtAssembly.Call("get_FullName")) {
104
- System.Console.WriteLine("Method " + containingType.FullName + " is referencing type " + targetType.FullName + " in a different assembly");
105
- return SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.ObjectKeyword));
106
- }
107
- }*/
108
-
109
- // Check if any of the generic arguments are not in the same assembly
110
- /*if (containingRtType != null && targetType.IsGenericType()) {
111
- foreach (REFrameworkNET.TypeDefinition td in targetType.GenericArguments) {
112
- if (td != null) {
113
- var rtType = td.GetRuntimeType();
114
- if (rtType != null) {
115
- if ((ManagedObject)containingRtType.Call("get_Assembly") != (ManagedObject)rtType.Call("get_Assembly")) {
116
- return SyntaxFactory.PredefinedType(SyntaxFactory.Token(SyntaxKind.ObjectKeyword));
117
- }
118
- }
119
- }
120
- }
121
- }*/
122
- }
123
- }
124
-
125
69
TypeSyntax outSyntax = SyntaxFactory . PredefinedType ( SyntaxFactory . Token ( SyntaxKind . VoidKeyword ) ) ;
126
70
127
71
string ogTargetTypename = targetType != null ? targetType . GetFullName ( ) : "" ;
@@ -294,12 +238,6 @@ private static TypeSyntax MakeProperType(REFrameworkNET.TypeDefinition? targetTy
294
238
295
239
typeDeclaration = GenerateMethods ( baseTypes ) ;
296
240
297
- List < FieldDeclarationSyntax > internalFieldDeclarations = [ ] ;
298
-
299
- if ( internalFieldDeclarations . Count > 0 ) {
300
- typeDeclaration = typeDeclaration . AddMembers ( internalFieldDeclarations . ToArray ( ) ) ;
301
- }
302
-
303
241
if ( baseTypes . Count > 0 && typeDeclaration != null ) {
304
242
refTypeFieldDecl = refTypeFieldDecl . AddModifiers ( SyntaxFactory . Token ( SyntaxKind . NewKeyword ) ) ;
305
243
//fieldDeclaration2 = fieldDeclaration2.AddModifiers(SyntaxFactory.Token(SyntaxKind.NewKeyword));
@@ -312,6 +250,11 @@ private static TypeSyntax MakeProperType(REFrameworkNET.TypeDefinition? targetTy
312
250
//typeDeclaration = typeDeclaration.AddMembers(refProxyFieldDecl);
313
251
}
314
252
253
+ // Logically needs to come after the REFType field is added as they reference it
254
+ if ( internalFieldDeclarations . Count > 0 && typeDeclaration != null ) {
255
+ typeDeclaration = typeDeclaration . AddMembers ( internalFieldDeclarations . ToArray ( ) ) ;
256
+ }
257
+
315
258
return GenerateNestedTypes ( ) ;
316
259
}
317
260
@@ -326,7 +269,7 @@ private TypeDeclarationSyntax GenerateMethods(List<SimpleBaseTypeSyntax> baseTyp
326
269
327
270
HashSet < string > seenMethodSignatures = [ ] ;
328
271
329
- var validMethods = new List < REFrameworkNET . Method > ( ) ;
272
+ List < REFrameworkNET . Method > validMethods = [ ] ;
330
273
331
274
try {
332
275
foreach ( REFrameworkNET . Method m in methods ) {
@@ -376,6 +319,8 @@ private TypeDeclarationSyntax GenerateMethods(List<SimpleBaseTypeSyntax> baseTyp
376
319
SyntaxFactory . ParseAttributeArgumentList ( "(" + method . GetIndex ( ) . ToString ( ) + ")" ) ) )
377
320
) ;
378
321
322
+ bool anyOutParams = false ;
323
+
379
324
if ( method . Parameters . Count > 0 ) {
380
325
// If any of the params have ! in them, skip this method
381
326
if ( method . Parameters . Any ( param => param != null && ( param . Type == null || ( param . Type != null && param . Type . FullName . Contains ( '!' ) ) ) ) ) {
@@ -395,6 +340,7 @@ private TypeDeclarationSyntax GenerateMethods(List<SimpleBaseTypeSyntax> baseTyp
395
340
396
341
bool anyUnsafeParams = false ;
397
342
343
+
398
344
if ( runtimeParams != null ) {
399
345
foreach ( dynamic param in runtimeParams ) {
400
346
if ( param . get_IsRetval ( ) == true ) {
@@ -430,6 +376,7 @@ private TypeDeclarationSyntax GenerateMethods(List<SimpleBaseTypeSyntax> baseTyp
430
376
if ( isOut == true ) {
431
377
simpleMethodSignature += "out" ;
432
378
modifiers . Add ( SyntaxFactory . Token ( SyntaxKind . OutKeyword ) ) ;
379
+ anyOutParams = true ;
433
380
}
434
381
435
382
if ( isByRef == true ) {
@@ -461,36 +408,42 @@ private TypeDeclarationSyntax GenerateMethods(List<SimpleBaseTypeSyntax> baseTyp
461
408
}
462
409
463
410
if ( method . IsStatic ( ) ) {
464
- // Add System.ComponentModel.Description("static") attribute
465
- //methodDeclaration = methodDeclaration.AddAttributeLists(SyntaxFactory.AttributeList().AddAttributes(SyntaxFactory.Attribute(SyntaxFactory.ParseName("global::System.ComponentModel.DescriptionAttribute"), SyntaxFactory.ParseAttributeArgumentList("(\"static\")"))));
466
-
467
411
// lets see what happens if we just make it static
468
- /* methodDeclaration = methodDeclaration.AddModifiers(SyntaxFactory.Token(SyntaxKind.StaticKeyword));
412
+ methodDeclaration = methodDeclaration . AddModifiers ( SyntaxFactory . Token ( SyntaxKind . StaticKeyword ) ) ;
469
413
470
414
// Now we must add a body to it that actually calls the method
471
415
// We have our REFType field, so we can lookup the method and call it
472
416
// Make a private static field to hold the REFrameworkNET.Method
473
- var internalFieldName = "INTERNAL_" + method.GetIndex().ToString();
417
+ var internalFieldName = "INTERNAL_" + method . Name + method . GetIndex ( ) . ToString ( ) ;
474
418
var methodVariableDeclaration = SyntaxFactory . VariableDeclaration ( SyntaxFactory . ParseTypeName ( "global::REFrameworkNET.Method" ) )
475
- .AddVariables(SyntaxFactory.VariableDeclarator(internalFieldName).WithInitializer(SyntaxFactory.EqualsValueClause(SyntaxFactory.ParseExpression("REFType.GetMethod(\"" + method.Name + "\")"))));
476
-
419
+ . AddVariables ( SyntaxFactory . VariableDeclarator ( internalFieldName ) . WithInitializer ( SyntaxFactory . EqualsValueClause ( SyntaxFactory . ParseExpression ( "REFType.GetMethod(\" " + method . GetMethodSignature ( ) + "\" )" ) ) ) ) ;
420
+
477
421
var methodFieldDeclaration = SyntaxFactory . FieldDeclaration ( methodVariableDeclaration ) . AddModifiers ( SyntaxFactory . Token ( SyntaxKind . PrivateKeyword ) , SyntaxFactory . Token ( SyntaxKind . StaticKeyword ) , SyntaxFactory . Token ( SyntaxKind . ReadOnlyKeyword ) ) ;
478
422
internalFieldDeclarations . Add ( methodFieldDeclaration ) ;
479
423
480
- // Now we can add the body
481
- // bool HandleInvokeMember_Internal(System::Object^ obj, array<System::Object^>^ args, System::Object^% result);
424
+ List < StatementSyntax > bodyStatements = [ ] ;
425
+
482
426
if ( method . ReturnType . FullName == "System.Void" ) {
483
- var body = internalFieldName + ".Invoke(null, null)";
484
- methodDeclaration = methodDeclaration.AddBodyStatements(SyntaxFactory.ParseStatement(body));
427
+ if ( method . Parameters . Count == 0 ) {
428
+ bodyStatements . Add ( SyntaxFactory . ParseStatement ( internalFieldName + ".Invoke(null, null);" ) ) ;
429
+ } else if ( ! anyOutParams ) {
430
+ bodyStatements . Add ( SyntaxFactory . ParseStatement ( internalFieldName + ".Invoke(null, new object[] {" + string . Join ( ", " , method . Parameters . Select ( param => param . Name ) ) + "});" ) ) ;
431
+ } else {
432
+ bodyStatements . Add ( SyntaxFactory . ParseStatement ( "throw new System.NotImplementedException();" ) ) ; // TODO: Implement this
433
+ }
485
434
} else {
486
- var body1 = "object INTERNAL_result = null;";
487
- var body2 = internalFieldName + ".HandleInvokeMember_Internal(null, " + (method.Parameters.Count > 0 ? "new object[] {" + string.Join(", ", method.Parameters.Select(param => param.Name)) + "}" : "null") + ", ref INTERNAL_result);";
488
- string body3 = "return (" + returnType.GetText() + ")INTERNAL_result;";
435
+ if ( method . Parameters . Count == 0 ) {
436
+ bodyStatements . Add ( SyntaxFactory . ParseStatement ( "return (" + returnType . GetText ( ) . ToString ( ) + ")" + internalFieldName + ".InvokeBoxed(typeof(" + returnType . GetText ( ) . ToString ( ) + "), null, null);" ) ) ;
437
+ } else if ( ! anyOutParams ) {
438
+ bodyStatements . Add ( SyntaxFactory . ParseStatement ( "return (" + returnType . GetText ( ) . ToString ( ) + ")" + internalFieldName + ".InvokeBoxed(typeof(" + returnType . GetText ( ) . ToString ( ) + "), null, new object[] {" + string . Join ( ", " , method . Parameters . Select ( param => param . Name ) ) + "});" ) ) ;
439
+ } else {
440
+ bodyStatements . Add ( SyntaxFactory . ParseStatement ( "throw new System.NotImplementedException();" ) ) ; // TODO: Implement this
441
+ }
442
+ }
489
443
490
- methodDeclaration = methodDeclaration.AddBodyStatements(
491
- [SyntaxFactory.ParseStatement(body1), SyntaxFactory.ParseStatement(body2), SyntaxFactory.ParseStatement(body3)]
492
- );
493
- }*/
444
+ methodDeclaration = methodDeclaration . AddBodyStatements (
445
+ [ .. bodyStatements ]
446
+ ) ;
494
447
}
495
448
496
449
if ( seenMethodSignatures . Contains ( simpleMethodSignature ) ) {
0 commit comments