[51b7345] | 1 | #include <cassert> |
---|
| 2 | |
---|
| 3 | #include "SynTree.h" |
---|
| 4 | #include "SemanticError.h" |
---|
| 5 | |
---|
| 6 | #ifndef SYNTREE_MUTATOR_H |
---|
| 7 | #define SYNTREE_MUTATOR_H |
---|
| 8 | |
---|
[d9a0e76] | 9 | class Mutator { |
---|
| 10 | protected: |
---|
[51b7345] | 11 | Mutator(); |
---|
| 12 | virtual ~Mutator(); |
---|
[d9a0e76] | 13 | public: |
---|
| 14 | virtual ObjectDecl* mutate( ObjectDecl *objectDecl ); |
---|
| 15 | virtual DeclarationWithType* mutate( FunctionDecl *functionDecl ); |
---|
| 16 | virtual Declaration* mutate( StructDecl *aggregateDecl ); |
---|
| 17 | virtual Declaration* mutate( UnionDecl *aggregateDecl ); |
---|
| 18 | virtual Declaration* mutate( EnumDecl *aggregateDecl ); |
---|
| 19 | virtual Declaration* mutate( ContextDecl *aggregateDecl ); |
---|
| 20 | virtual TypeDecl* mutate( TypeDecl *typeDecl ); |
---|
| 21 | virtual Declaration* mutate( TypedefDecl *typeDecl ); |
---|
| 22 | |
---|
| 23 | virtual CompoundStmt* mutate( CompoundStmt *compoundStmt ); |
---|
| 24 | virtual Statement* mutate( ExprStmt *exprStmt ); |
---|
| 25 | virtual Statement* mutate( IfStmt *ifStmt ); |
---|
| 26 | virtual Statement* mutate( WhileStmt *whileStmt ); |
---|
| 27 | virtual Statement* mutate( ForStmt *forStmt ); |
---|
| 28 | virtual Statement* mutate( SwitchStmt *switchStmt ); |
---|
| 29 | virtual Statement* mutate( ChooseStmt *chooseStmt ); |
---|
| 30 | virtual Statement* mutate( FallthruStmt *fallthruStmt ); |
---|
| 31 | virtual Statement* mutate( CaseStmt *caseStmt ); |
---|
| 32 | virtual Statement* mutate( BranchStmt *branchStmt ); |
---|
| 33 | virtual Statement* mutate( ReturnStmt *returnStmt ); |
---|
| 34 | virtual Statement* mutate( TryStmt *returnStmt ); |
---|
| 35 | virtual Statement* mutate( CatchStmt *catchStmt ); |
---|
| 36 | virtual Statement* mutate( FinallyStmt *catchStmt ); |
---|
| 37 | virtual NullStmt* mutate( NullStmt *nullStmt ); |
---|
| 38 | virtual Statement* mutate( DeclStmt *declStmt ); |
---|
| 39 | |
---|
| 40 | virtual Expression* mutate( ApplicationExpr *applicationExpr ); |
---|
| 41 | virtual Expression* mutate( UntypedExpr *untypedExpr ); |
---|
| 42 | virtual Expression* mutate( NameExpr *nameExpr ); |
---|
| 43 | virtual Expression* mutate( AddressExpr *castExpr ); |
---|
| 44 | virtual Expression* mutate( LabelAddressExpr *labAddressExpr ); |
---|
| 45 | virtual Expression* mutate( CastExpr *castExpr ); |
---|
| 46 | virtual Expression* mutate( UntypedMemberExpr *memberExpr ); |
---|
| 47 | virtual Expression* mutate( MemberExpr *memberExpr ); |
---|
| 48 | virtual Expression* mutate( VariableExpr *variableExpr ); |
---|
| 49 | virtual Expression* mutate( ConstantExpr *constantExpr ); |
---|
| 50 | virtual Expression* mutate( SizeofExpr *sizeofExpr ); |
---|
| 51 | virtual Expression* mutate( AttrExpr *attrExpr ); |
---|
| 52 | virtual Expression* mutate( LogicalExpr *logicalExpr ); |
---|
| 53 | virtual Expression* mutate( ConditionalExpr *conditionalExpr ); |
---|
| 54 | virtual Expression* mutate( CommaExpr *commaExpr ); |
---|
| 55 | virtual Expression* mutate( TupleExpr *tupleExpr ); |
---|
| 56 | virtual Expression* mutate( SolvedTupleExpr *tupleExpr ); |
---|
| 57 | virtual Expression* mutate( TypeExpr *typeExpr ); |
---|
| 58 | virtual Expression* mutate( UntypedValofExpr *valofExpr ); |
---|
| 59 | |
---|
| 60 | virtual Type* mutate( VoidType *basicType ); |
---|
| 61 | virtual Type* mutate( BasicType *basicType ); |
---|
| 62 | virtual Type* mutate( PointerType *pointerType ); |
---|
| 63 | virtual Type* mutate( ArrayType *arrayType ); |
---|
| 64 | virtual Type* mutate( FunctionType *functionType ); |
---|
| 65 | virtual Type* mutate( StructInstType *aggregateUseType ); |
---|
| 66 | virtual Type* mutate( UnionInstType *aggregateUseType ); |
---|
| 67 | virtual Type* mutate( EnumInstType *aggregateUseType ); |
---|
| 68 | virtual Type* mutate( ContextInstType *aggregateUseType ); |
---|
| 69 | virtual Type* mutate( TypeInstType *aggregateUseType ); |
---|
| 70 | virtual Type* mutate( TupleType *tupleType ); |
---|
| 71 | virtual Type* mutate( TypeofType *typeofType ); |
---|
| 72 | virtual Type* mutate( AttrType *attrType ); |
---|
| 73 | |
---|
| 74 | virtual Initializer* mutate( SingleInit *singleInit ); |
---|
| 75 | virtual Initializer* mutate( ListInit *listInit ); |
---|
| 76 | |
---|
| 77 | virtual Subrange *mutate( Subrange *subrange ); |
---|
| 78 | |
---|
| 79 | virtual Constant *mutate( Constant *constant ); |
---|
| 80 | private: |
---|
| 81 | virtual Declaration* handleAggregateDecl(AggregateDecl *aggregateDecl ); |
---|
| 82 | virtual Declaration* handleNamedTypeDecl(NamedTypeDecl *typeDecl ); |
---|
| 83 | virtual Type* handleReferenceToType(ReferenceToType *aggregateUseType ); |
---|
[51b7345] | 84 | }; |
---|
| 85 | |
---|
| 86 | template< typename TreeType, typename MutatorType > |
---|
[d9a0e76] | 87 | inline TreeType *maybeMutate( TreeType *tree, MutatorType &mutator ) { |
---|
| 88 | if ( tree ) { |
---|
[51b7345] | 89 | TreeType *newnode = dynamic_cast< TreeType* >( tree->acceptMutator( mutator ) ); |
---|
| 90 | assert( newnode ); |
---|
| 91 | return newnode; |
---|
| 92 | /// return tree->acceptMutator( mutator ); |
---|
| 93 | } else { |
---|
| 94 | return 0; |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | |
---|
| 98 | template< typename Container, typename MutatorType > |
---|
[d9a0e76] | 99 | inline void mutateAll( Container &container, MutatorType &mutator ) { |
---|
[51b7345] | 100 | SemanticError errors; |
---|
[d9a0e76] | 101 | for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) { |
---|
[51b7345] | 102 | try { |
---|
[d9a0e76] | 103 | if ( *i ) { |
---|
[51b7345] | 104 | /// *i = (*i)->acceptMutator( mutator ); |
---|
| 105 | *i = dynamic_cast< typename Container::value_type >( (*i)->acceptMutator( mutator ) ); |
---|
| 106 | assert( *i ); |
---|
| 107 | } |
---|
| 108 | } catch( SemanticError &e ) { |
---|
| 109 | errors.append( e ); |
---|
| 110 | } |
---|
| 111 | } |
---|
[d9a0e76] | 112 | if ( !errors.isEmpty() ) { |
---|
[51b7345] | 113 | throw errors; |
---|
| 114 | } |
---|
| 115 | } |
---|
| 116 | |
---|
[d9a0e76] | 117 | #endif // SYNTREE_MUTATOR_H |
---|
[51b7345] | 118 | |
---|
| 119 | // Local Variables: // |
---|
| 120 | // mode: c++ // |
---|
| 121 | // End: // |
---|