1212
1313namespace CppSharp
1414{
15- internal class StmtDeclarationsCodeGenerator : NativeParserCodeGenerator
15+ internal class StmtDeclarationsCodeGenerator : DeclarationsCodeGenerator
1616 {
17- public StmtDeclarationsCodeGenerator ( BindingContext context ,
18- IEnumerable < Declaration > declarations )
17+ public StmtDeclarationsCodeGenerator ( BindingContext context , IEnumerable < Declaration > declarations )
1918 : base ( context , declarations )
2019 {
2120 }
2221
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" ;
3123
32- GenerateForwardDecls ( ) ;
33- NewLine ( ) ;
24+ public override bool VisitDeclaration ( Declaration decl )
25+ {
26+ if ( ! base . VisitDeclaration ( decl ) )
27+ return false ;
3428
35- foreach ( var decl in Declarations )
29+ if ( decl . Name == "GCCAsmStmt" )
3630 {
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 ( ) ;
4534 }
4635
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 ;
5537 }
5638
57- public virtual void GenerateForwardDecls ( )
39+ public override void GenerateForwardDecls ( )
5840 {
5941 WriteLine ( "class Expr;" ) ;
6042 WriteLine ( "class Declaration;" ) ;
@@ -103,33 +85,16 @@ public override bool GenerateClassBody(Class @class)
10385 }
10486 }
10587
106- internal class StmtDefinitionsCodeGenerator : NativeParserCodeGenerator
88+ internal class StmtDefinitionsCodeGenerator : DefinitionsCodeGenerator
10789 {
108- public StmtDefinitionsCodeGenerator ( BindingContext context ,
109- IEnumerable < Declaration > declarations )
90+ public StmtDefinitionsCodeGenerator ( BindingContext context , IEnumerable < Declaration > declarations )
11091 : base ( context , declarations )
11192 {
11293 }
11394
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" ;
13196
132- public virtual void GenerateIncludes ( )
97+ public override void GenerateIncludes ( )
13398 {
13499 GenerateCommonIncludes ( ) ;
135100 WriteInclude ( "Stmt.h" , CInclude . IncludeKind . Quoted ) ;
@@ -185,44 +150,6 @@ public override bool VisitClassDecl(Class @class)
185150
186151 return true ;
187152 }
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- }
226153 }
227154
228155 internal class StmtParserCodeGenerator : NativeParserCodeGenerator
@@ -469,9 +396,9 @@ protected override void GenerateVisitorSwitch(IEnumerable<string> classes)
469396 StmtClassEnum . Items . Where ( item => item . IsGenerated )
470397 . Select ( item => RemoveFromEnd ( item . Name , "Class" ) )
471398 . Where ( @class => ! IsAbstractStmt ( @class ) )
472- : new List < string > ( ) ;
399+ : classes ;
473400
474- GenerateSwitchCases ( StmtClassEnum != null ? enumItems : classes ) ;
401+ GenerateSwitchCases ( enumItems ) ;
475402
476403 UnindentAndWriteCloseBrace ( ) ;
477404 }
@@ -510,6 +437,8 @@ public ExprDeclarationsCodeGenerator(BindingContext context,
510437 {
511438 }
512439
440+ public override string BaseTypeName => "Expr" ;
441+
513442 public override void GenerateIncludes ( )
514443 {
515444 WriteInclude ( "Stmt.h" , CInclude . IncludeKind . Quoted ) ;
@@ -532,7 +461,7 @@ public ExprDefinitionsCodeGenerator(BindingContext context,
532461 {
533462 }
534463
535- public override bool GeneratePragmaOnce => false ;
464+ public override string BaseTypeName => "Expr" ;
536465
537466 public override void GenerateIncludes ( )
538467 {
@@ -563,8 +492,7 @@ public override void GenerateIncludes()
563492
564493 internal class ExprASTConverterCodeGenerator : StmtASTConverterCodeGenerator
565494 {
566- public ExprASTConverterCodeGenerator ( BindingContext context ,
567- IEnumerable < Declaration > declarations )
495+ public ExprASTConverterCodeGenerator ( BindingContext context , IEnumerable < Declaration > declarations )
568496 : base ( context , declarations , null )
569497 {
570498 }
@@ -595,14 +523,7 @@ private void GenerateSwitchCases(IEnumerable<string> classes)
595523
596524 WriteLine ( $ "var _{ ParamName } = { className } .__CreateInstance({ ParamName } .__Instance);") ;
597525
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 } );") ;
606527
607528 UnindentAndWriteCloseBrace ( ) ;
608529 }
0 commit comments