@@ -752,11 +752,14 @@ private void GenerateVisitorInterface()
752
752
753
753
internal abstract class ASTConverterCodeGenerator : ManagedParserCodeGenerator
754
754
{
755
- readonly Enumeration StmtClassEnum ;
756
- protected ASTConverterCodeGenerator ( BindingContext context , IEnumerable < Declaration > declarations , Enumeration stmtClassEnum )
755
+ public abstract string BaseTypeName { get ; }
756
+ public string ParamName => BaseTypeName . ToLowerInvariant ( ) ;
757
+
758
+ public abstract bool IsAbstractASTNode ( Class kind ) ;
759
+
760
+ protected ASTConverterCodeGenerator ( BindingContext context , IEnumerable < Declaration > declarations )
757
761
: base ( context , declarations )
758
762
{
759
- StmtClassEnum = stmtClassEnum ;
760
763
}
761
764
762
765
public override void Process ( )
@@ -765,6 +768,7 @@ public override void Process()
765
768
NewLine ( ) ;
766
769
767
770
WriteLine ( "using CppSharp.Parser.AST;" ) ;
771
+ NewLine ( ) ;
768
772
WriteLine ( "using static CppSharp.ConversionUtils;" ) ;
769
773
NewLine ( ) ;
770
774
@@ -779,11 +783,9 @@ public override void Process()
779
783
UnindentAndWriteCloseBrace ( ) ;
780
784
}
781
785
782
- public virtual string BaseTypeName { get ; }
786
+ protected abstract void GenerateVisitorSwitch ( IEnumerable < string > classes ) ;
783
787
784
- public string ParamName => BaseTypeName . ToLowerInvariant ( ) ;
785
-
786
- private void GenerateVisitor ( )
788
+ protected virtual void GenerateVisitor ( )
787
789
{
788
790
var comment = new RawComment
789
791
{
@@ -792,12 +794,13 @@ private void GenerateVisitor()
792
794
793
795
GenerateComment ( comment ) ;
794
796
795
- WriteLine ( $ "public abstract class { BaseTypeName } Visitor<TRet> where TRet : class") ;
797
+ WriteLine ( $ "public abstract class { BaseTypeName } Visitor<TRet>") ;
798
+ WriteLineIndent ( "where TRet : class" ) ;
796
799
WriteOpenBraceAndIndent ( ) ;
797
800
798
801
var classes = Declarations
799
802
. OfType < Class > ( )
800
- . Where ( @class => ! IsAbstractStmt ( @class ) )
803
+ . Where ( @class => ! IsAbstractASTNode ( @class ) )
801
804
. Select ( @class => @class . Name )
802
805
. ToArray ( ) ;
803
806
@@ -812,62 +815,24 @@ private void GenerateVisitor()
812
815
WriteLineIndent ( "return default(TRet);" ) ;
813
816
NewLine ( ) ;
814
817
815
- WriteLine ( $ "switch({ ParamName } .StmtClass)") ;
816
- WriteOpenBraceAndIndent ( ) ;
817
-
818
- var enumItems = StmtClassEnum != null ?
819
- StmtClassEnum . Items . Where ( item => item . IsGenerated )
820
- . Select ( item => RemoveFromEnd ( item . Name , "Class" ) )
821
- . Where ( @class => ! IsAbstractStmt ( @class ) )
822
- : new List < string > ( ) ;
823
-
824
- GenerateSwitchCases ( StmtClassEnum != null ? enumItems : classes ) ;
818
+ GenerateVisitorSwitch ( classes ) ;
825
819
826
820
UnindentAndWriteCloseBrace ( ) ;
827
821
UnindentAndWriteCloseBrace ( ) ;
828
- UnindentAndWriteCloseBrace ( ) ;
829
- }
830
-
831
- public virtual void GenerateSwitchCases ( IEnumerable < string > classes )
832
- {
833
- foreach ( var className in classes )
834
- {
835
- WriteLine ( $ "case StmtClass.{ className } :") ;
836
- WriteOpenBraceAndIndent ( ) ;
837
-
838
- WriteLine ( $ "var _{ ParamName } = { className } .__CreateInstance({ ParamName } .__Instance);") ;
839
-
840
- var isExpression = Declarations
841
- . OfType < Class > ( )
842
- . All ( c => c . Name != className ) ;
843
-
844
- if ( isExpression )
845
- WriteLine ( $ "return VisitExpression(_{ ParamName } as Expr) as TRet;") ;
846
- else
847
- WriteLine ( $ "return Visit{ className } (_{ ParamName } );") ;
848
-
849
- UnindentAndWriteCloseBrace ( ) ;
850
- }
851
-
852
- WriteLine ( "default:" ) ;
853
- WriteLineIndent ( $ "throw new System.NotImplementedException(" +
854
- $ "{ ParamName } .StmtClass.ToString());") ;
855
822
}
856
823
857
824
private void GenerateConverter ( )
858
825
{
859
- WriteLine ( "public unsafe class {0}Converter : {0}Visitor<AST.{0}>" ,
860
- BaseTypeName ) ;
826
+ WriteLine ( "public unsafe class {0}Converter : {0}Visitor<AST.{0}>" , BaseTypeName ) ;
861
827
WriteOpenBraceAndIndent ( ) ;
862
828
863
829
foreach ( var @class in Declarations . OfType < Class > ( ) )
864
830
{
865
- if ( IsAbstractStmt ( @class ) )
831
+ if ( IsAbstractASTNode ( @class ) )
866
832
continue ;
867
833
868
834
PushBlock ( ) ;
869
- WriteLine ( "public override AST.{0} Visit{1}({1} {2})" ,
870
- BaseTypeName , @class . Name , ParamName ) ;
835
+ WriteLine ( "public override AST.{0} Visit{1}({1} {2})" , BaseTypeName , @class . Name , ParamName ) ;
871
836
WriteOpenBraceAndIndent ( ) ;
872
837
873
838
var qualifiedName = $ "{ GetQualifiedName ( @class ) } ";
@@ -923,8 +888,7 @@ public override bool VisitProperty(Property property)
923
888
public override bool VisitMethodDecl ( Method method )
924
889
{
925
890
var managedName = GetDeclName ( method , GeneratorKind . CSharp ) ;
926
- var nativeName = CaseRenamePass . ConvertCaseString ( method ,
927
- RenameCasePattern . LowerCamelCase ) ;
891
+ var nativeName = CaseRenamePass . ConvertCaseString ( method , RenameCasePattern . LowerCamelCase ) ;
928
892
929
893
WriteLine ( $ "for (uint i = 0; i < { ParamName } .Get{ nativeName } Count; i++)") ;
930
894
WriteOpenBraceAndIndent ( ) ;
@@ -942,7 +906,7 @@ public override bool VisitMethodDecl(Method method)
942
906
return true ;
943
907
}
944
908
945
- private void MarshalDecl ( AST . Type type , string declTypeName , string bindingsName )
909
+ protected virtual void MarshalDecl ( AST . Type type , string declTypeName , string bindingsName )
946
910
{
947
911
var typeName = $ "AST.{ declTypeName } ";
948
912
if ( type . TryGetEnum ( out Enumeration @enum ) )
@@ -975,8 +939,7 @@ internal class NativeParserCodeGenerator : Generators.C.CCodeGenerator
975
939
{
976
940
internal readonly IEnumerable < Declaration > Declarations ;
977
941
978
- public NativeParserCodeGenerator ( BindingContext context ,
979
- IEnumerable < Declaration > declarations )
942
+ public NativeParserCodeGenerator ( BindingContext context , IEnumerable < Declaration > declarations )
980
943
: base ( context )
981
944
{
982
945
Declarations = declarations ;
0 commit comments