12
12
13
13
namespace CppSharp
14
14
{
15
- internal class StmtDeclarationsCodeGenerator : NativeParserCodeGenerator
15
+ internal class StmtDeclarationsCodeGenerator : DeclarationsCodeGenerator
16
16
{
17
- public StmtDeclarationsCodeGenerator ( BindingContext context ,
18
- IEnumerable < Declaration > declarations )
17
+ public StmtDeclarationsCodeGenerator ( BindingContext context , IEnumerable < Declaration > declarations )
19
18
: base ( context , declarations )
20
19
{
21
20
}
22
21
23
- public void GenerateDeclarations ( )
24
- {
25
- Process ( ) ;
26
- GenerateIncludes ( ) ;
27
- NewLine ( ) ;
28
-
29
- WriteLine ( "namespace CppSharp::CppParser::AST {" ) ;
30
- NewLine ( ) ;
22
+ public override string BaseTypeName => "Stmt" ;
31
23
32
- GenerateForwardDecls ( ) ;
33
- NewLine ( ) ;
24
+ public override bool VisitDeclaration ( Declaration decl )
25
+ {
26
+ if ( ! base . VisitDeclaration ( decl ) )
27
+ return false ;
34
28
35
- foreach ( var decl in Declarations )
29
+ if ( decl . Name == "GCCAsmStmt" )
36
30
{
37
- if ( decl . Name == "GCCAsmStmt" )
38
- {
39
- WriteLine ( "class StringLiteral;" ) ;
40
- WriteLine ( "class AddrLabelExpr;" ) ;
41
- NewLine ( ) ;
42
- }
43
-
44
- decl . Visit ( this ) ;
31
+ WriteLine ( "class StringLiteral;" ) ;
32
+ WriteLine ( "class AddrLabelExpr;" ) ;
33
+ NewLine ( ) ;
45
34
}
46
35
47
- NewLine ( ) ;
48
- WriteLine ( "}" ) ;
49
- }
50
-
51
- public virtual void GenerateIncludes ( )
52
- {
53
- WriteInclude ( "Sources.h" , CInclude . IncludeKind . Quoted ) ;
54
- WriteInclude ( "Types.h" , CInclude . IncludeKind . Quoted ) ;
36
+ return true ;
55
37
}
56
38
57
- public virtual void GenerateForwardDecls ( )
39
+ public override void GenerateForwardDecls ( )
58
40
{
59
41
WriteLine ( "class Expr;" ) ;
60
42
WriteLine ( "class Declaration;" ) ;
@@ -103,33 +85,16 @@ public override bool GenerateClassBody(Class @class)
103
85
}
104
86
}
105
87
106
- internal class StmtDefinitionsCodeGenerator : NativeParserCodeGenerator
88
+ internal class StmtDefinitionsCodeGenerator : DefinitionsCodeGenerator
107
89
{
108
- public StmtDefinitionsCodeGenerator ( BindingContext context ,
109
- IEnumerable < Declaration > declarations )
90
+ public StmtDefinitionsCodeGenerator ( BindingContext context , IEnumerable < Declaration > declarations )
110
91
: base ( context , declarations )
111
92
{
112
93
}
113
94
114
- public override bool GeneratePragmaOnce => false ;
115
-
116
- public void GenerateDefinitions ( )
117
- {
118
- Process ( ) ;
119
-
120
- GenerateIncludes ( ) ;
121
- NewLine ( ) ;
122
-
123
- WriteLine ( "namespace CppSharp::CppParser::AST {" ) ;
124
- NewLine ( ) ;
125
-
126
- foreach ( var decl in Declarations . OfType < Class > ( ) )
127
- decl . Visit ( this ) ;
128
-
129
- WriteLine ( "}" ) ;
130
- }
95
+ public override string BaseTypeName => "Stmt" ;
131
96
132
- public virtual void GenerateIncludes ( )
97
+ public override void GenerateIncludes ( )
133
98
{
134
99
GenerateCommonIncludes ( ) ;
135
100
WriteInclude ( "Stmt.h" , CInclude . IncludeKind . Quoted ) ;
@@ -185,44 +150,6 @@ public override bool VisitClassDecl(Class @class)
185
150
186
151
return true ;
187
152
}
188
-
189
- internal void GenerateMemberInits ( Class @class )
190
- {
191
- foreach ( var property in @class . Properties )
192
- {
193
- if ( SkipProperty ( property ) )
194
- continue ;
195
-
196
- var typeName = GetDeclTypeName ( property ) ;
197
- if ( typeName == "std::string" )
198
- continue ;
199
-
200
- WriteLineIndent ( $ ", { GetDeclName ( property ) } ({ GenerateInit ( property ) } )") ;
201
- }
202
- }
203
-
204
- private string GenerateInit ( Property property )
205
- {
206
- if ( property . Type . IsPointer ( ) )
207
- return "nullptr" ;
208
-
209
- if ( property . Type . IsPrimitiveType ( PrimitiveType . Bool ) )
210
- return "false" ;
211
-
212
- var typeName = GetDeclTypeName ( property ) ;
213
- if ( property . Type . TryGetClass ( out Class _ ) )
214
- return $ "{ typeName } ()";
215
-
216
- if ( property . Type . TryGetEnum ( out Enumeration @enum ) )
217
- return $ "{ GetQualifiedName ( @enum ) } ::{ @enum . Items . First ( ) . Name } ";
218
-
219
- return "0" ;
220
- }
221
-
222
- public override bool VisitEnumDecl ( Enumeration @enum )
223
- {
224
- return true ;
225
- }
226
153
}
227
154
228
155
internal class StmtParserCodeGenerator : NativeParserCodeGenerator
@@ -469,9 +396,9 @@ protected override void GenerateVisitorSwitch(IEnumerable<string> classes)
469
396
StmtClassEnum . Items . Where ( item => item . IsGenerated )
470
397
. Select ( item => RemoveFromEnd ( item . Name , "Class" ) )
471
398
. Where ( @class => ! IsAbstractStmt ( @class ) )
472
- : new List < string > ( ) ;
399
+ : classes ;
473
400
474
- GenerateSwitchCases ( StmtClassEnum != null ? enumItems : classes ) ;
401
+ GenerateSwitchCases ( enumItems ) ;
475
402
476
403
UnindentAndWriteCloseBrace ( ) ;
477
404
}
@@ -510,6 +437,8 @@ public ExprDeclarationsCodeGenerator(BindingContext context,
510
437
{
511
438
}
512
439
440
+ public override string BaseTypeName => "Expr" ;
441
+
513
442
public override void GenerateIncludes ( )
514
443
{
515
444
WriteInclude ( "Stmt.h" , CInclude . IncludeKind . Quoted ) ;
@@ -532,7 +461,7 @@ public ExprDefinitionsCodeGenerator(BindingContext context,
532
461
{
533
462
}
534
463
535
- public override bool GeneratePragmaOnce => false ;
464
+ public override string BaseTypeName => "Expr" ;
536
465
537
466
public override void GenerateIncludes ( )
538
467
{
@@ -563,8 +492,7 @@ public override void GenerateIncludes()
563
492
564
493
internal class ExprASTConverterCodeGenerator : StmtASTConverterCodeGenerator
565
494
{
566
- public ExprASTConverterCodeGenerator ( BindingContext context ,
567
- IEnumerable < Declaration > declarations )
495
+ public ExprASTConverterCodeGenerator ( BindingContext context , IEnumerable < Declaration > declarations )
568
496
: base ( context , declarations , null )
569
497
{
570
498
}
@@ -595,14 +523,7 @@ private void GenerateSwitchCases(IEnumerable<string> classes)
595
523
596
524
WriteLine ( $ "var _{ ParamName } = { className } .__CreateInstance({ ParamName } .__Instance);") ;
597
525
598
- var isExpression = Declarations
599
- . OfType < Class > ( )
600
- . All ( c => c . Name != className ) ;
601
-
602
- if ( isExpression )
603
- WriteLine ( $ "return VisitExpression(_{ ParamName } as Expr) as TRet;") ;
604
- else
605
- WriteLine ( $ "return Visit{ className } (_{ ParamName } );") ;
526
+ WriteLine ( $ "return Visit{ className } (_{ ParamName } );") ;
606
527
607
528
UnindentAndWriteCloseBrace ( ) ;
608
529
}
0 commit comments