@@ -752,11 +752,14 @@ private void GenerateVisitorInterface()
752752
753753 internal abstract class ASTConverterCodeGenerator : ManagedParserCodeGenerator
754754 {
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 )
757761 : base ( context , declarations )
758762 {
759- StmtClassEnum = stmtClassEnum ;
760763 }
761764
762765 public override void Process ( )
@@ -765,6 +768,7 @@ public override void Process()
765768 NewLine ( ) ;
766769
767770 WriteLine ( "using CppSharp.Parser.AST;" ) ;
771+ NewLine ( ) ;
768772 WriteLine ( "using static CppSharp.ConversionUtils;" ) ;
769773 NewLine ( ) ;
770774
@@ -779,11 +783,9 @@ public override void Process()
779783 UnindentAndWriteCloseBrace ( ) ;
780784 }
781785
782- public virtual string BaseTypeName { get ; }
786+ protected abstract void GenerateVisitorSwitch ( IEnumerable < string > classes ) ;
783787
784- public string ParamName => BaseTypeName . ToLowerInvariant ( ) ;
785-
786- private void GenerateVisitor ( )
788+ protected virtual void GenerateVisitor ( )
787789 {
788790 var comment = new RawComment
789791 {
@@ -792,12 +794,13 @@ private void GenerateVisitor()
792794
793795 GenerateComment ( comment ) ;
794796
795- WriteLine ( $ "public abstract class { BaseTypeName } Visitor<TRet> where TRet : class") ;
797+ WriteLine ( $ "public abstract class { BaseTypeName } Visitor<TRet>") ;
798+ WriteLineIndent ( "where TRet : class" ) ;
796799 WriteOpenBraceAndIndent ( ) ;
797800
798801 var classes = Declarations
799802 . OfType < Class > ( )
800- . Where ( @class => ! IsAbstractStmt ( @class ) )
803+ . Where ( @class => ! IsAbstractASTNode ( @class ) )
801804 . Select ( @class => @class . Name )
802805 . ToArray ( ) ;
803806
@@ -812,62 +815,24 @@ private void GenerateVisitor()
812815 WriteLineIndent ( "return default(TRet);" ) ;
813816 NewLine ( ) ;
814817
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 ) ;
825819
826820 UnindentAndWriteCloseBrace ( ) ;
827821 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());") ;
855822 }
856823
857824 private void GenerateConverter ( )
858825 {
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 ) ;
861827 WriteOpenBraceAndIndent ( ) ;
862828
863829 foreach ( var @class in Declarations . OfType < Class > ( ) )
864830 {
865- if ( IsAbstractStmt ( @class ) )
831+ if ( IsAbstractASTNode ( @class ) )
866832 continue ;
867833
868834 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 ) ;
871836 WriteOpenBraceAndIndent ( ) ;
872837
873838 var qualifiedName = $ "{ GetQualifiedName ( @class ) } ";
@@ -923,8 +888,7 @@ public override bool VisitProperty(Property property)
923888 public override bool VisitMethodDecl ( Method method )
924889 {
925890 var managedName = GetDeclName ( method , GeneratorKind . CSharp ) ;
926- var nativeName = CaseRenamePass . ConvertCaseString ( method ,
927- RenameCasePattern . LowerCamelCase ) ;
891+ var nativeName = CaseRenamePass . ConvertCaseString ( method , RenameCasePattern . LowerCamelCase ) ;
928892
929893 WriteLine ( $ "for (uint i = 0; i < { ParamName } .Get{ nativeName } Count; i++)") ;
930894 WriteOpenBraceAndIndent ( ) ;
@@ -942,7 +906,7 @@ public override bool VisitMethodDecl(Method method)
942906 return true ;
943907 }
944908
945- private void MarshalDecl ( AST . Type type , string declTypeName , string bindingsName )
909+ protected virtual void MarshalDecl ( AST . Type type , string declTypeName , string bindingsName )
946910 {
947911 var typeName = $ "AST.{ declTypeName } ";
948912 if ( type . TryGetEnum ( out Enumeration @enum ) )
@@ -975,8 +939,7 @@ internal class NativeParserCodeGenerator : Generators.C.CCodeGenerator
975939 {
976940 internal readonly IEnumerable < Declaration > Declarations ;
977941
978- public NativeParserCodeGenerator ( BindingContext context ,
979- IEnumerable < Declaration > declarations )
942+ public NativeParserCodeGenerator ( BindingContext context , IEnumerable < Declaration > declarations )
980943 : base ( context )
981944 {
982945 Declarations = declarations ;
0 commit comments