9
9
10
10
namespace CppSharp
11
11
{
12
+ class TemplateArgumentHandler
13
+ {
14
+ public static string GetTemplateArgumentTypeName ( TemplateArgument arg , CppTypePrinter printer )
15
+ {
16
+ switch ( arg . Kind )
17
+ {
18
+ case TemplateArgument . ArgumentKind . Type :
19
+ return GetTemplateTypeArgName ( arg . Type , printer ) ;
20
+ case TemplateArgument . ArgumentKind . Declaration :
21
+ return GetTemplateDeclArgName ( arg . Declaration , printer ) ;
22
+ case TemplateArgument . ArgumentKind . NullPtr :
23
+ return "nullptr_t" ;
24
+ case TemplateArgument . ArgumentKind . Integral :
25
+ return GetTemplateIntegralArgName ( arg . Integral , printer ) ;
26
+ default :
27
+ throw new NotImplementedException ( $ "Unhandled template argument kind: { arg . Kind } ") ;
28
+ }
29
+ }
30
+
31
+ private static string GetTemplateTypeArgName ( QualifiedType type , CppTypePrinter printer )
32
+ {
33
+ var typeStr = type . Type . Visit ( printer ) . Type ;
34
+ if ( type . Type . IsPointer ( ) )
35
+ return $ "{ typeStr } *";
36
+ return typeStr ;
37
+ }
38
+
39
+ private static string GetTemplateDeclArgName ( Declaration decl , CppTypePrinter printer )
40
+ {
41
+ return decl ? . Visit ( printer ) . Type ?? "nullptr" ;
42
+ }
43
+
44
+ private static string GetTemplateIntegralArgName ( long value , CppTypePrinter printer )
45
+ {
46
+ return value . ToString ( ) ;
47
+ }
48
+ }
49
+
12
50
internal static class CodeGeneratorHelpers
13
51
{
14
52
internal static CppTypePrinter CppTypePrinter ;
@@ -31,6 +69,17 @@ @class is "Stmt"
31
69
or "OverloadExpr"
32
70
or "CoroutineSuspendExpr" ;
33
71
72
+ public static bool SkipClass ( Class @class )
73
+ {
74
+ if ( ! @class . IsGenerated )
75
+ return true ;
76
+
77
+ if ( @class . Access != AccessSpecifier . Public )
78
+ return true ;
79
+
80
+ return false ;
81
+ }
82
+
34
83
public static bool SkipProperty ( Property property , bool skipBaseCheck = false )
35
84
{
36
85
if ( ! property . IsGenerated )
@@ -61,11 +110,22 @@ public static bool SkipProperty(Property property, bool skipBaseCheck = false)
61
110
62
111
var typeName = property . Type . Visit ( CppTypePrinter ) . Type ;
63
112
113
+ // General properties.
114
+ if ( typeName . Contains ( "_iterator" ) ||
115
+ typeName . Contains ( "_range" ) ||
116
+ property . Name . Contains ( "_begin" ) ||
117
+ property . Name . Contains ( "_end" ) ||
118
+ property . Name . Contains ( "_empty" ) ||
119
+ property . Name . Contains ( "_size" ) )
120
+ return true ;
121
+
122
+
123
+ return false ;
64
124
//
65
125
// Statement properties.
66
126
//
67
127
68
- if ( typeName . Contains ( "LabelDecl" ) ||
128
+ /* if (typeName.Contains("LabelDecl") ||
69
129
typeName.Contains("VarDecl") ||
70
130
typeName.Contains("Token") ||
71
131
typeName.Contains("CapturedDecl") ||
@@ -77,7 +137,7 @@ public static bool SkipProperty(Property property, bool skipBaseCheck = false)
77
137
typeName.Contains("NestedNameSpecifierLoc") ||
78
138
typeName.Contains("DeclarationNameInfo") ||
79
139
typeName.Contains("DeclGroupRef"))
80
- return true ;
140
+ return true;*/
81
141
82
142
//
83
143
// Expressions
@@ -209,11 +269,6 @@ public static bool SkipProperty(Property property, bool skipBaseCheck = false)
209
269
if ( typeName . Contains ( "StorageDuration" ) )
210
270
return true ;
211
271
212
- // General properties.
213
- if ( typeName . Contains ( "_iterator" ) ||
214
- typeName . Contains ( "_range" ) )
215
- return true ;
216
-
217
272
if ( typeName . Contains ( "ArrayRef" ) )
218
273
return true ;
219
274
@@ -246,7 +301,7 @@ public static bool SkipProperty(Property property, bool skipBaseCheck = false)
246
301
247
302
public static bool SkipMethod ( Method method )
248
303
{
249
- if ( method . Ignore )
304
+ if ( method . IsGenerated )
250
305
return true ;
251
306
252
307
var @class = method . Namespace as Class ;
@@ -335,8 +390,14 @@ public static string GetDeclName(Declaration decl, GeneratorKind kind)
335
390
336
391
if ( kind == GeneratorKind . CPlusPlus )
337
392
{
338
- if ( Generators . C . CCodeGenerator . IsReservedKeyword ( name ) )
393
+ if ( name == "inline" )
394
+ {
395
+ name = "isInline" ;
396
+ }
397
+ else if ( Generators . C . CCodeGenerator . IsReservedKeyword ( name ) )
398
+ {
339
399
name = $ "_{ name } ";
400
+ }
340
401
}
341
402
else if ( kind == GeneratorKind . CSharp )
342
403
{
@@ -372,7 +433,7 @@ public static AST.Type GetDeclType(AST.Type type,
372
433
TypePrinter typePrinter )
373
434
{
374
435
var qualifiedType = new QualifiedType ( type ) ;
375
- if ( qualifiedType . Type . IsPointerTo ( out TagType tagType ) )
436
+ if ( qualifiedType . Type . IsPointerTo ( out TagType _ ) )
376
437
qualifiedType = qualifiedType . StripConst ( ) ;
377
438
378
439
var typeName = qualifiedType . Type . Visit ( typePrinter ) . Type ;
@@ -423,61 +484,23 @@ public static string GetDeclTypeName(AST.Type type,
423
484
}
424
485
}
425
486
426
- string className = null ;
427
- if ( typeName . Contains ( "FieldDecl" ) )
428
- className = "Field" ;
429
- else if ( typeName . Contains ( "NamedDecl" ) )
430
- className = "Declaration" ;
431
- else if ( typeName . Contains ( "CXXMethodDecl" ) )
432
- className = "Method" ;
433
- else if ( typeName . Contains ( "FunctionDecl" ) )
434
- className = "Function" ;
435
- else if ( typeName . Contains ( "FunctionTemplateDecl" ) )
436
- className = "FunctionTemplate" ;
437
- else if ( typeName is "Decl" or "Decl*" )
438
- className = "Declaration" ;
439
-
440
- if ( className != null )
441
- return ( typePrinter is CppTypePrinter ) ? $ "{ className } *" : className ;
442
-
443
487
return typeName ;
444
488
}
445
489
446
490
public static AST . Type GetIteratorType ( Method method )
447
491
{
448
- var retType = method . ReturnType . Type ;
449
-
450
- TemplateSpecializationType templateSpecType ;
451
- TypedefType typedefType ;
452
- TypedefNameDecl typedefNameDecl ;
492
+ var retType = method . ReturnType . Type . Desugar ( ) ;
453
493
454
- if ( retType is TemplateSpecializationType )
455
- {
456
- templateSpecType = retType as TemplateSpecializationType ;
457
- typedefType = templateSpecType . Arguments [ 0 ] . Type . Type as TypedefType ;
458
- typedefNameDecl = typedefType . Declaration as TypedefNameDecl ;
459
- }
460
- else
461
- {
462
- typedefType = retType as TypedefType ;
463
- typedefNameDecl = typedefType . Declaration as TypedefNameDecl ;
464
- templateSpecType = typedefNameDecl . Type as TemplateSpecializationType ;
465
- typedefType = templateSpecType . Arguments [ 0 ] . Type . Type as TypedefType ;
466
- typedefNameDecl = typedefType . Declaration as TypedefNameDecl ;
467
- typedefType = typedefNameDecl . Type as TypedefType ;
468
- if ( typedefType != null )
469
- typedefNameDecl = typedefType . Declaration as TypedefNameDecl ;
470
- }
494
+ if ( retType is TemplateSpecializationType templateSpecType )
495
+ retType = templateSpecType . Arguments [ 0 ] . Type . Type . Desugar ( ) ;
471
496
472
- var iteratorType = typedefNameDecl . Type ;
473
- if ( iteratorType . IsPointerTo ( out PointerType pointee ) )
474
- iteratorType = iteratorType . GetPointee ( ) ;
497
+ if ( retType . IsPointerTo ( out PointerType pointee ) )
498
+ retType = pointee ;
475
499
476
- return iteratorType ;
500
+ return retType ;
477
501
}
478
502
479
- public static string GetIteratorTypeName ( AST . Type iteratorType ,
480
- TypePrinter typePrinter )
503
+ public static string GetIteratorTypeName ( AST . Type iteratorType , TypePrinter typePrinter )
481
504
{
482
505
if ( iteratorType . IsPointer ( ) )
483
506
iteratorType = iteratorType . GetFinalPointee ( ) ;
@@ -494,7 +517,8 @@ public static string GetIteratorTypeName(AST.Type iteratorType,
494
517
else if ( iteratorTypeName . Contains ( "StmtIterator" ) )
495
518
iteratorTypeName = "Stmt" ;
496
519
497
- else if ( iteratorTypeName . Contains ( "CastIterator" ) )
520
+ else if ( iteratorTypeName . Contains ( "CastIterator" ) ||
521
+ iteratorTypeName . Contains ( "DeclContext::" ) )
498
522
{
499
523
if ( iteratorType is TypedefType typedefType )
500
524
iteratorType = typedefType . Declaration . Type ;
@@ -509,9 +533,6 @@ public static string GetIteratorTypeName(AST.Type iteratorType,
509
533
iteratorTypeName = CleanClangNamespaceFromName ( iteratorTypeName ) ;
510
534
}
511
535
512
- if ( iteratorTypeName == "Decl" )
513
- iteratorTypeName = "Declaration" ;
514
-
515
536
if ( typePrinter is CppTypePrinter )
516
537
return $ "{ iteratorTypeName } *";
517
538
@@ -547,5 +568,109 @@ public static string FirstLetterToUpperCase(string s)
547
568
a [ 0 ] = char . ToUpper ( a [ 0 ] ) ;
548
569
return new string ( a ) ;
549
570
}
571
+
572
+ public static bool SkipDeclProperty ( Property property )
573
+ {
574
+ if ( ! property . IsGenerated || property . Access != AccessSpecifier . Public )
575
+ return true ;
576
+
577
+ var @class = property . Namespace as Class ;
578
+
579
+ if ( @class . GetBaseProperty ( property ) != null )
580
+ return true ;
581
+
582
+ var typeName = property . Type . Visit ( CppTypePrinter ) . Type ;
583
+
584
+ // Skip properties that deal with source locations/ranges
585
+ if ( typeName . Contains ( "SourceLocation" ) || typeName . Contains ( "SourceRange" ) )
586
+ return true ;
587
+
588
+ // Skip Clang-specific internal properties
589
+ if ( typeName . Contains ( "ASTContext" ) || typeName . Contains ( "DeclContext" ) )
590
+ return true ;
591
+
592
+ // Skip template-specific properties that are handled separately
593
+ if ( typeName . Contains ( "TemplateParameterList" ) ||
594
+ typeName . Contains ( "TemplateArgument" ) )
595
+ return true ;
596
+
597
+ return false ;
598
+ }
599
+
600
+ public static bool SkipTypeProperty ( Property property )
601
+ {
602
+ if ( ! property . IsGenerated || property . Access != AccessSpecifier . Public )
603
+ return true ;
604
+
605
+ var @class = property . Namespace as Class ;
606
+
607
+ if ( @class . GetBaseProperty ( property ) != null )
608
+ return true ;
609
+
610
+ var typeName = property . Type . Visit ( CppTypePrinter ) . Type ;
611
+
612
+ // Skip source location properties
613
+ if ( typeName . Contains ( "SourceLocation" ) )
614
+ return true ;
615
+
616
+ // Skip internal Clang type properties
617
+ if ( typeName . Contains ( "TypeLoc" ) || typeName . Contains ( "ASTContext" ) )
618
+ return true ;
619
+
620
+ return false ;
621
+ }
622
+
623
+ public static bool IsAbstractType ( Class @class ) =>
624
+ @class . Name is "Type"
625
+ or "ArrayType"
626
+ or "TagType"
627
+ or "FunctionType" ;
628
+
629
+ public static bool IsAbstractDecl ( Class @class ) =>
630
+ @class . Name is "Decl"
631
+ or "NamedDecl"
632
+ or "ValueDecl"
633
+ or "TypeDecl"
634
+ or "DeclContext" ;
635
+
636
+ public static string GetPropertyAccessorName ( Property property , bool isGetter )
637
+ {
638
+ var baseName = FirstLetterToUpperCase ( GetDeclName ( property ) ) ;
639
+ return isGetter ? $ "Get{ baseName } " : $ "Set{ baseName } ";
640
+ }
641
+
642
+ public static string GetPropertyTypeName ( Property property , bool includeNamespace = true )
643
+ {
644
+ var typeName = GetDeclTypeName ( property ) ;
645
+
646
+ // Handle special cases
647
+ if ( typeName . Contains ( "QualType" ) ||
648
+ ( property . Type . IsPointerTo ( out TagType tagType ) &&
649
+ tagType . Declaration ? . Name . Contains ( "Type" ) == true ) )
650
+ {
651
+ return includeNamespace ? $ "AST::{ typeName } " : typeName ;
652
+ }
653
+
654
+ // Handle template types
655
+ if ( property . Type is TemplateSpecializationType templateType )
656
+ {
657
+ var templateName = templateType . Template . TemplatedDecl . Name ;
658
+ return includeNamespace ? $ "AST::{ templateName } " : templateName ;
659
+ }
660
+
661
+ return typeName ;
662
+ }
663
+
664
+ public static bool IsTypeProperty ( Property property )
665
+ {
666
+ if ( property . Type . IsPointerTo ( out TagType tagType ) )
667
+ return tagType . Declaration ? . Name . Contains ( "Type" ) == true ;
668
+
669
+ var typeName = GetDeclTypeName ( property ) ;
670
+ return typeName . Contains ( "QualType" ) ||
671
+ typeName . Contains ( "Type" ) ||
672
+ ( property . Type is TemplateSpecializationType templateType &&
673
+ templateType . Template . TemplatedDecl . Name . Contains ( "Type" ) ) ;
674
+ }
550
675
}
551
676
}
0 commit comments