| 1 | //
 | 
|---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
 | 
|---|
| 3 | //
 | 
|---|
| 4 | // The contents of this file are covered under the licence agreement in the
 | 
|---|
| 5 | // file "LICENCE" distributed with Cforall.
 | 
|---|
| 6 | //
 | 
|---|
| 7 | // Mutator.h --
 | 
|---|
| 8 | //
 | 
|---|
| 9 | // Author           : Richard C. Bilson
 | 
|---|
| 10 | // Created On       : Mon May 18 07:44:20 2015
 | 
|---|
| 11 | // Last Modified By : Peter A. Buhr
 | 
|---|
| 12 | // Last Modified On : Tue Feb  1 09:26:49 2022
 | 
|---|
| 13 | // Update Count     : 20
 | 
|---|
| 14 | //
 | 
|---|
| 15 | #pragma once
 | 
|---|
| 16 | 
 | 
|---|
| 17 | #include <cassert>                 // for assert
 | 
|---|
| 18 | 
 | 
|---|
| 19 | #include "Common/SemanticError.h"  // for SemanticError
 | 
|---|
| 20 | #include "SynTree/SynTree.h"       // for AST nodes
 | 
|---|
| 21 | 
 | 
|---|
| 22 | class Mutator {
 | 
|---|
| 23 |   protected:
 | 
|---|
| 24 |         Mutator() = default;
 | 
|---|
| 25 |         virtual ~Mutator() = default;
 | 
|---|
| 26 |   public:
 | 
|---|
| 27 |         virtual DeclarationWithType * mutate( ObjectDecl * objectDecl ) = 0;
 | 
|---|
| 28 |         virtual DeclarationWithType * mutate( FunctionDecl * functionDecl ) = 0;
 | 
|---|
| 29 |         virtual Declaration * mutate( StructDecl * aggregateDecl ) = 0;
 | 
|---|
| 30 |         virtual Declaration * mutate( UnionDecl * aggregateDecl ) = 0;
 | 
|---|
| 31 |         virtual Declaration * mutate( EnumDecl * aggregateDecl ) = 0;
 | 
|---|
| 32 |         virtual Declaration * mutate( TraitDecl * aggregateDecl ) = 0;
 | 
|---|
| 33 |         virtual Declaration * mutate( TypeDecl * typeDecl ) = 0;
 | 
|---|
| 34 |         virtual Declaration * mutate( TypedefDecl * typeDecl ) = 0;
 | 
|---|
| 35 |         virtual AsmDecl * mutate( AsmDecl * asmDecl ) = 0;
 | 
|---|
| 36 |         virtual DirectiveDecl * mutate( DirectiveDecl * directiveDecl ) = 0;
 | 
|---|
| 37 |         virtual StaticAssertDecl * mutate( StaticAssertDecl * assertDecl ) = 0;
 | 
|---|
| 38 |         virtual DeclarationWithType * mutate( InlineMemberDecl * InlineMemberDecl ) = 0;
 | 
|---|
| 39 | 
 | 
|---|
| 40 |         virtual CompoundStmt * mutate( CompoundStmt * compoundStmt ) = 0;
 | 
|---|
| 41 |         virtual Statement * mutate( ExprStmt * exprStmt ) = 0;
 | 
|---|
| 42 |         virtual Statement * mutate( AsmStmt * asmStmt ) = 0;
 | 
|---|
| 43 |         virtual Statement * mutate( DirectiveStmt * dirStmt ) = 0;
 | 
|---|
| 44 |         virtual Statement * mutate( IfStmt * ifStmt ) = 0;
 | 
|---|
| 45 |         virtual Statement * mutate( WhileDoStmt * whileDoStmt ) = 0;
 | 
|---|
| 46 |         virtual Statement * mutate( ForStmt * forStmt ) = 0;
 | 
|---|
| 47 |         virtual Statement * mutate( SwitchStmt * switchStmt ) = 0;
 | 
|---|
| 48 |         virtual Statement * mutate( CaseStmt * caseStmt ) = 0;
 | 
|---|
| 49 |         virtual Statement * mutate( BranchStmt * branchStmt ) = 0;
 | 
|---|
| 50 |         virtual Statement * mutate( ReturnStmt * returnStmt ) = 0;
 | 
|---|
| 51 |         virtual Statement * mutate( ThrowStmt * throwStmt ) = 0;
 | 
|---|
| 52 |         virtual Statement * mutate( TryStmt * tryStmt ) = 0;
 | 
|---|
| 53 |         virtual Statement * mutate( CatchStmt * catchStmt ) = 0;
 | 
|---|
| 54 |         virtual Statement * mutate( FinallyStmt * catchStmt ) = 0;
 | 
|---|
| 55 |         virtual Statement * mutate( SuspendStmt * suspendStmt ) = 0;
 | 
|---|
| 56 |         virtual Statement * mutate( WaitForStmt * waitforStmt ) = 0;
 | 
|---|
| 57 |         virtual Declaration * mutate( WithStmt * withStmt ) = 0;
 | 
|---|
| 58 |         virtual NullStmt * mutate( NullStmt * nullStmt ) = 0;
 | 
|---|
| 59 |         virtual Statement * mutate( DeclStmt * declStmt ) = 0;
 | 
|---|
| 60 |         virtual Statement * mutate( ImplicitCtorDtorStmt * impCtorDtorStmt ) = 0;
 | 
|---|
| 61 |         virtual Statement * mutate( MutexStmt * mutexStmt ) = 0;
 | 
|---|
| 62 | 
 | 
|---|
| 63 |         virtual Expression * mutate( ApplicationExpr * applicationExpr ) = 0;
 | 
|---|
| 64 |         virtual Expression * mutate( UntypedExpr * untypedExpr ) = 0;
 | 
|---|
| 65 |         virtual Expression * mutate( NameExpr * nameExpr ) = 0;
 | 
|---|
| 66 |         virtual Expression * mutate( AddressExpr * addrExpr ) = 0;
 | 
|---|
| 67 |         virtual Expression * mutate( LabelAddressExpr * labAddressExpr ) = 0;
 | 
|---|
| 68 |         virtual Expression * mutate( CastExpr * castExpr ) = 0;
 | 
|---|
| 69 |         virtual Expression * mutate( KeywordCastExpr * castExpr ) = 0;
 | 
|---|
| 70 |         virtual Expression * mutate( VirtualCastExpr * castExpr ) = 0;
 | 
|---|
| 71 |         virtual Expression * mutate( UntypedMemberExpr * memberExpr ) = 0;
 | 
|---|
| 72 |         virtual Expression * mutate( MemberExpr * memberExpr ) = 0;
 | 
|---|
| 73 |         virtual Expression * mutate( VariableExpr * variableExpr ) = 0;
 | 
|---|
| 74 |         virtual Expression * mutate( ConstantExpr * constantExpr ) = 0;
 | 
|---|
| 75 |         virtual Expression * mutate( SizeofExpr * sizeofExpr ) = 0;
 | 
|---|
| 76 |         virtual Expression * mutate( AlignofExpr * alignofExpr ) = 0;
 | 
|---|
| 77 |         virtual Expression * mutate( UntypedOffsetofExpr * offsetofExpr ) = 0;
 | 
|---|
| 78 |         virtual Expression * mutate( OffsetofExpr * offsetofExpr ) = 0;
 | 
|---|
| 79 |         virtual Expression * mutate( OffsetPackExpr * offsetPackExpr ) = 0;
 | 
|---|
| 80 |         virtual Expression * mutate( LogicalExpr * logicalExpr ) = 0;
 | 
|---|
| 81 |         virtual Expression * mutate( ConditionalExpr * conditionalExpr ) = 0;
 | 
|---|
| 82 |         virtual Expression * mutate( CommaExpr * commaExpr ) = 0;
 | 
|---|
| 83 |         virtual Expression * mutate( TypeExpr * typeExpr ) = 0;
 | 
|---|
| 84 |         virtual Expression * mutate( DimensionExpr * dimensionExpr ) = 0;
 | 
|---|
| 85 |         virtual Expression * mutate( AsmExpr * asmExpr ) = 0;
 | 
|---|
| 86 |         virtual Expression * mutate( ImplicitCopyCtorExpr * impCpCtorExpr ) = 0;
 | 
|---|
| 87 |         virtual Expression * mutate( ConstructorExpr * ctorExpr ) = 0;
 | 
|---|
| 88 |         virtual Expression * mutate( CompoundLiteralExpr * compLitExpr ) = 0;
 | 
|---|
| 89 |         virtual Expression * mutate( RangeExpr * rangeExpr ) = 0;
 | 
|---|
| 90 |         virtual Expression * mutate( UntypedTupleExpr * tupleExpr ) = 0;
 | 
|---|
| 91 |         virtual Expression * mutate( TupleExpr * tupleExpr ) = 0;
 | 
|---|
| 92 |         virtual Expression * mutate( TupleIndexExpr * tupleExpr ) = 0;
 | 
|---|
| 93 |         virtual Expression * mutate( TupleAssignExpr * assignExpr ) = 0;
 | 
|---|
| 94 |         virtual Expression * mutate( StmtExpr  * stmtExpr ) = 0;
 | 
|---|
| 95 |         virtual Expression * mutate( UniqueExpr  * uniqueExpr ) = 0;
 | 
|---|
| 96 |         virtual Expression * mutate( UntypedInitExpr  * initExpr ) = 0;
 | 
|---|
| 97 |         virtual Expression * mutate( InitExpr  * initExpr ) = 0;
 | 
|---|
| 98 |         virtual Expression * mutate( DeletedExpr * delExpr ) = 0;
 | 
|---|
| 99 |         virtual Expression * mutate( DefaultArgExpr * argExpr ) = 0;
 | 
|---|
| 100 |         virtual Expression * mutate( GenericExpr * genExpr ) = 0;
 | 
|---|
| 101 |         virtual Expression * mutate( QualifiedNameExpr * qualifiedNameExpr ) = 0;
 | 
|---|
| 102 | 
 | 
|---|
| 103 |         virtual Type * mutate( VoidType * basicType ) = 0;
 | 
|---|
| 104 |         virtual Type * mutate( BasicType * basicType ) = 0;
 | 
|---|
| 105 |         virtual Type * mutate( PointerType * pointerType ) = 0;
 | 
|---|
| 106 |         virtual Type * mutate( ArrayType * arrayType ) = 0;
 | 
|---|
| 107 |         virtual Type * mutate( ReferenceType * refType ) = 0;
 | 
|---|
| 108 |         virtual Type * mutate( QualifiedType * qualType ) = 0;
 | 
|---|
| 109 |         virtual Type * mutate( FunctionType * functionType ) = 0;
 | 
|---|
| 110 |         virtual Type * mutate( StructInstType * aggregateUseType ) = 0;
 | 
|---|
| 111 |         virtual Type * mutate( UnionInstType * aggregateUseType ) = 0;
 | 
|---|
| 112 |         virtual Type * mutate( EnumInstType * aggregateUseType ) = 0;
 | 
|---|
| 113 |         virtual Type * mutate( TraitInstType * aggregateUseType ) = 0;
 | 
|---|
| 114 |         virtual Type * mutate( TypeInstType * aggregateUseType ) = 0;
 | 
|---|
| 115 |         virtual Type * mutate( TupleType * tupleType ) = 0;
 | 
|---|
| 116 |         virtual Type * mutate( TypeofType * typeofType ) = 0;
 | 
|---|
| 117 |         virtual Type * mutate( VTableType * vtableType ) = 0;
 | 
|---|
| 118 |         virtual Type * mutate( AttrType * attrType ) = 0;
 | 
|---|
| 119 |         virtual Type * mutate( VarArgsType * varArgsType ) = 0;
 | 
|---|
| 120 |         virtual Type * mutate( ZeroType * zeroType ) = 0;
 | 
|---|
| 121 |         virtual Type * mutate( OneType * oneType ) = 0;
 | 
|---|
| 122 |         virtual Type * mutate( GlobalScopeType * globalType ) = 0;
 | 
|---|
| 123 | 
 | 
|---|
| 124 |         virtual Designation * mutate( Designation * designation ) = 0 ;
 | 
|---|
| 125 |         virtual Initializer * mutate( SingleInit * singleInit ) = 0 ;
 | 
|---|
| 126 |         virtual Initializer * mutate( ListInit * listInit ) = 0 ;
 | 
|---|
| 127 |         virtual Initializer * mutate( ConstructorInit * ctorInit ) = 0 ;
 | 
|---|
| 128 | 
 | 
|---|
| 129 |         virtual Constant * mutate( Constant * constant ) = 0;
 | 
|---|
| 130 | 
 | 
|---|
| 131 |         virtual Attribute * mutate( Attribute * attribute ) = 0;
 | 
|---|
| 132 | 
 | 
|---|
| 133 |         virtual TypeSubstitution * mutate( TypeSubstitution * sub ) = 0;
 | 
|---|
| 134 | };
 | 
|---|
| 135 | 
 | 
|---|
| 136 | template< typename TreeType, typename MutatorType >
 | 
|---|
| 137 | inline TreeType *maybeMutate( TreeType *tree, MutatorType &mutator ) {
 | 
|---|
| 138 |         if ( tree ) {
 | 
|---|
| 139 |                 TreeType *newnode = dynamic_cast< TreeType * >( tree->acceptMutator( mutator ) );
 | 
|---|
| 140 |                 assert( newnode );
 | 
|---|
| 141 |                 return newnode;
 | 
|---|
| 142 |         } else {
 | 
|---|
| 143 |                 return 0;
 | 
|---|
| 144 |         } // if
 | 
|---|
| 145 | }
 | 
|---|
| 146 | 
 | 
|---|
| 147 | template< typename Container, typename MutatorType >
 | 
|---|
| 148 | inline void mutateAll( Container &container, MutatorType &mutator ) {
 | 
|---|
| 149 |         SemanticErrorException errors;
 | 
|---|
| 150 |         for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) {
 | 
|---|
| 151 |                 try {
 | 
|---|
| 152 |                         if ( *i ) {
 | 
|---|
| 153 | ///                 *i = (*i)->acceptMutator( mutator );
 | 
|---|
| 154 |                                 *i = dynamic_cast< typename Container::value_type >( (*i)->acceptMutator( mutator ) );
 | 
|---|
| 155 |                                 assert( *i );
 | 
|---|
| 156 |                         } // if
 | 
|---|
| 157 |                 } catch( SemanticErrorException &e ) {
 | 
|---|
| 158 |                         errors.append( e );
 | 
|---|
| 159 |                 } // try
 | 
|---|
| 160 |         } // for
 | 
|---|
| 161 |         if ( ! errors.isEmpty() ) {
 | 
|---|
| 162 |                 throw errors;
 | 
|---|
| 163 |         } // if
 | 
|---|
| 164 | }
 | 
|---|
| 165 | 
 | 
|---|
| 166 | // Local Variables: //
 | 
|---|
| 167 | // tab-width: 4 //
 | 
|---|
| 168 | // mode: c++ //
 | 
|---|
| 169 | // compile-command: "make install" //
 | 
|---|
| 170 | // End: //
 | 
|---|