[13932f14] | 1 | #pragma once |
---|
| 2 | |
---|
[08fc48f] | 3 | // IWYU pragma: private, include "Common/PassVisitor.h" |
---|
| 4 | |
---|
[296b2be] | 5 | #include <stack> |
---|
[02fdb8e] | 6 | #include <type_traits> |
---|
[296b2be] | 7 | |
---|
[675716e] | 8 | #include "Common/Stats.h" |
---|
[3c398b6] | 9 | #include "Common/utility.h" |
---|
| 10 | |
---|
[ab904dc] | 11 | #include "SynTree/Mutator.h" |
---|
[13932f14] | 12 | #include "SynTree/Visitor.h" |
---|
| 13 | |
---|
[e0886db] | 14 | #include "SymTab/Indexer.h" |
---|
| 15 | |
---|
[5ea7a22] | 16 | #include "SynTree/Attribute.h" |
---|
[ab904dc] | 17 | #include "SynTree/Initializer.h" |
---|
| 18 | #include "SynTree/Statement.h" |
---|
| 19 | #include "SynTree/Type.h" |
---|
| 20 | #include "SynTree/Declaration.h" |
---|
| 21 | #include "SynTree/Expression.h" |
---|
| 22 | #include "SynTree/Constant.h" |
---|
[07c178f0] | 23 | |
---|
| 24 | class TypeSubstitution; |
---|
[ab904dc] | 25 | |
---|
[296b2be] | 26 | #include "PassVisitor.proto.h" |
---|
[ab904dc] | 27 | |
---|
[13932f14] | 28 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
[5d88a0a] | 29 | // Templated visitor type |
---|
| 30 | // To use declare a PassVisitor< YOUR VISITOR TYPE > |
---|
[5e298d7] | 31 | // The visitor type should specify the previsit/postvisit/premutate/postmutate for types that are desired. |
---|
[4551a6e] | 32 | // Note: previsit/postvisit/premutate/postmutate must be **public** members |
---|
[5e298d7] | 33 | // |
---|
| 34 | // Several additional features are available through inheritance |
---|
| 35 | // | WithTypeSubstitution - provides polymorphic TypeSubstitution * env for the current expression |
---|
| 36 | // | WithStmtsToAdd - provides the ability to insert statements before or after the current statement by adding new statements into |
---|
| 37 | // stmtsToAddBefore or stmtsToAddAfter respectively. |
---|
[6ca154b] | 38 | // | WithShortCircuiting - provides the ability to skip visiting child nodes; set visit_children to false in pre{visit,mutate} to skip visiting children |
---|
[d24d4e1] | 39 | // | WithGuards - provides the ability to save/restore data like a LIFO stack; to save, call GuardValue with the variable to save, the variable |
---|
[5e298d7] | 40 | // will automatically be restored to its previous value after the corresponding postvisit/postmutate teminates. |
---|
[13932f14] | 41 | //------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
---|
| 42 | template< typename pass_type > |
---|
[ab904dc] | 43 | class PassVisitor final : public Visitor, public Mutator { |
---|
[13932f14] | 44 | public: |
---|
| 45 | |
---|
| 46 | template< typename... Args > |
---|
[aff3af4] | 47 | PassVisitor(Args &&... args) |
---|
[13932f14] | 48 | : pass( std::forward<Args>( args )... ) |
---|
[b73bd70] | 49 | { |
---|
| 50 | typedef PassVisitor<pass_type> this_t; |
---|
| 51 | this_t * const * visitor = visitor_impl(pass, 0); |
---|
| 52 | if(visitor) { |
---|
| 53 | *const_cast<this_t **>( visitor ) = this; |
---|
| 54 | } |
---|
| 55 | } |
---|
[13932f14] | 56 | |
---|
| 57 | virtual ~PassVisitor() = default; |
---|
| 58 | |
---|
[aff3af4] | 59 | pass_type pass; |
---|
[13932f14] | 60 | |
---|
[5ea7a22] | 61 | virtual void visit( ObjectDecl * objectDecl ) override final; |
---|
| 62 | virtual void visit( FunctionDecl * functionDecl ) override final; |
---|
| 63 | virtual void visit( StructDecl * aggregateDecl ) override final; |
---|
| 64 | virtual void visit( UnionDecl * aggregateDecl ) override final; |
---|
| 65 | virtual void visit( EnumDecl * aggregateDecl ) override final; |
---|
| 66 | virtual void visit( TraitDecl * aggregateDecl ) override final; |
---|
| 67 | virtual void visit( TypeDecl * typeDecl ) override final; |
---|
| 68 | virtual void visit( TypedefDecl * typeDecl ) override final; |
---|
| 69 | virtual void visit( AsmDecl * asmDecl ) override final; |
---|
[f6e3e34] | 70 | virtual void visit( StaticAssertDecl * assertDecl ) override final; |
---|
[5ea7a22] | 71 | |
---|
| 72 | virtual void visit( CompoundStmt * compoundStmt ) override final; |
---|
| 73 | virtual void visit( ExprStmt * exprStmt ) override final; |
---|
| 74 | virtual void visit( AsmStmt * asmStmt ) override final; |
---|
[cc32d83] | 75 | virtual void visit( DirectiveStmt * dirStmt ) override final; |
---|
[5ea7a22] | 76 | virtual void visit( IfStmt * ifStmt ) override final; |
---|
| 77 | virtual void visit( WhileStmt * whileStmt ) override final; |
---|
| 78 | virtual void visit( ForStmt * forStmt ) override final; |
---|
| 79 | virtual void visit( SwitchStmt * switchStmt ) override final; |
---|
| 80 | virtual void visit( CaseStmt * caseStmt ) override final; |
---|
| 81 | virtual void visit( BranchStmt * branchStmt ) override final; |
---|
| 82 | virtual void visit( ReturnStmt * returnStmt ) override final; |
---|
| 83 | virtual void visit( ThrowStmt * throwStmt ) override final; |
---|
| 84 | virtual void visit( TryStmt * tryStmt ) override final; |
---|
| 85 | virtual void visit( CatchStmt * catchStmt ) override final; |
---|
| 86 | virtual void visit( FinallyStmt * finallyStmt ) override final; |
---|
| 87 | virtual void visit( WaitForStmt * waitforStmt ) override final; |
---|
[61255ad] | 88 | virtual void visit( WithStmt * withStmt ) override final; |
---|
[5ea7a22] | 89 | virtual void visit( NullStmt * nullStmt ) override final; |
---|
| 90 | virtual void visit( DeclStmt * declStmt ) override final; |
---|
| 91 | virtual void visit( ImplicitCtorDtorStmt * impCtorDtorStmt ) override final; |
---|
| 92 | |
---|
| 93 | virtual void visit( ApplicationExpr * applicationExpr ) override final; |
---|
| 94 | virtual void visit( UntypedExpr * untypedExpr ) override final; |
---|
| 95 | virtual void visit( NameExpr * nameExpr ) override final; |
---|
| 96 | virtual void visit( CastExpr * castExpr ) override final; |
---|
[9a705dc8] | 97 | virtual void visit( KeywordCastExpr * castExpr ) override final; |
---|
[5ea7a22] | 98 | virtual void visit( VirtualCastExpr * castExpr ) override final; |
---|
| 99 | virtual void visit( AddressExpr * addressExpr ) override final; |
---|
| 100 | virtual void visit( LabelAddressExpr * labAddressExpr ) override final; |
---|
| 101 | virtual void visit( UntypedMemberExpr * memberExpr ) override final; |
---|
| 102 | virtual void visit( MemberExpr * memberExpr ) override final; |
---|
| 103 | virtual void visit( VariableExpr * variableExpr ) override final; |
---|
| 104 | virtual void visit( ConstantExpr * constantExpr ) override final; |
---|
| 105 | virtual void visit( SizeofExpr * sizeofExpr ) override final; |
---|
| 106 | virtual void visit( AlignofExpr * alignofExpr ) override final; |
---|
| 107 | virtual void visit( UntypedOffsetofExpr * offsetofExpr ) override final; |
---|
| 108 | virtual void visit( OffsetofExpr * offsetofExpr ) override final; |
---|
| 109 | virtual void visit( OffsetPackExpr * offsetPackExpr ) override final; |
---|
| 110 | virtual void visit( AttrExpr * attrExpr ) override final; |
---|
| 111 | virtual void visit( LogicalExpr * logicalExpr ) override final; |
---|
| 112 | virtual void visit( ConditionalExpr * conditionalExpr ) override final; |
---|
| 113 | virtual void visit( CommaExpr * commaExpr ) override final; |
---|
| 114 | virtual void visit( TypeExpr * typeExpr ) override final; |
---|
| 115 | virtual void visit( AsmExpr * asmExpr ) override final; |
---|
| 116 | virtual void visit( ImplicitCopyCtorExpr * impCpCtorExpr ) override final; |
---|
| 117 | virtual void visit( ConstructorExpr * ctorExpr ) override final; |
---|
| 118 | virtual void visit( CompoundLiteralExpr * compLitExpr ) override final; |
---|
| 119 | virtual void visit( RangeExpr * rangeExpr ) override final; |
---|
| 120 | virtual void visit( UntypedTupleExpr * tupleExpr ) override final; |
---|
| 121 | virtual void visit( TupleExpr * tupleExpr ) override final; |
---|
| 122 | virtual void visit( TupleIndexExpr * tupleExpr ) override final; |
---|
| 123 | virtual void visit( TupleAssignExpr * assignExpr ) override final; |
---|
| 124 | virtual void visit( StmtExpr * stmtExpr ) override final; |
---|
| 125 | virtual void visit( UniqueExpr * uniqueExpr ) override final; |
---|
[73367a8] | 126 | virtual void visit( UntypedInitExpr * initExpr ) override final; |
---|
| 127 | virtual void visit( InitExpr * initExpr ) override final; |
---|
[44b4114] | 128 | virtual void visit( DeletedExpr * delExpr ) override final; |
---|
[0f79853] | 129 | virtual void visit( DefaultArgExpr * argExpr ) override final; |
---|
[d807ca28] | 130 | virtual void visit( GenericExpr * genExpr ) override final; |
---|
[5ea7a22] | 131 | |
---|
| 132 | virtual void visit( VoidType * basicType ) override final; |
---|
| 133 | virtual void visit( BasicType * basicType ) override final; |
---|
| 134 | virtual void visit( PointerType * pointerType ) override final; |
---|
| 135 | virtual void visit( ArrayType * arrayType ) override final; |
---|
| 136 | virtual void visit( ReferenceType * referenceType ) override final; |
---|
[c5d7701] | 137 | virtual void visit( QualifiedType * qualType ) override final; |
---|
[5ea7a22] | 138 | virtual void visit( FunctionType * functionType ) override final; |
---|
| 139 | virtual void visit( StructInstType * aggregateUseType ) override final; |
---|
| 140 | virtual void visit( UnionInstType * aggregateUseType ) override final; |
---|
| 141 | virtual void visit( EnumInstType * aggregateUseType ) override final; |
---|
| 142 | virtual void visit( TraitInstType * aggregateUseType ) override final; |
---|
| 143 | virtual void visit( TypeInstType * aggregateUseType ) override final; |
---|
| 144 | virtual void visit( TupleType * tupleType ) override final; |
---|
| 145 | virtual void visit( TypeofType * typeofType ) override final; |
---|
| 146 | virtual void visit( AttrType * attrType ) override final; |
---|
| 147 | virtual void visit( VarArgsType * varArgsType ) override final; |
---|
| 148 | virtual void visit( ZeroType * zeroType ) override final; |
---|
| 149 | virtual void visit( OneType * oneType ) override final; |
---|
[47498bd] | 150 | virtual void visit( GlobalScopeType * globalType ) override final; |
---|
[5ea7a22] | 151 | |
---|
| 152 | virtual void visit( Designation * designation ) override final; |
---|
| 153 | virtual void visit( SingleInit * singleInit ) override final; |
---|
| 154 | virtual void visit( ListInit * listInit ) override final; |
---|
| 155 | virtual void visit( ConstructorInit * ctorInit ) override final; |
---|
| 156 | |
---|
| 157 | virtual void visit( Constant * constant ) override final; |
---|
| 158 | |
---|
| 159 | virtual void visit( Attribute * attribute ) override final; |
---|
| 160 | |
---|
| 161 | virtual DeclarationWithType * mutate( ObjectDecl * objectDecl ) override final; |
---|
| 162 | virtual DeclarationWithType * mutate( FunctionDecl * functionDecl ) override final; |
---|
| 163 | virtual Declaration * mutate( StructDecl * aggregateDecl ) override final; |
---|
| 164 | virtual Declaration * mutate( UnionDecl * aggregateDecl ) override final; |
---|
| 165 | virtual Declaration * mutate( EnumDecl * aggregateDecl ) override final; |
---|
| 166 | virtual Declaration * mutate( TraitDecl * aggregateDecl ) override final; |
---|
| 167 | virtual Declaration * mutate( TypeDecl * typeDecl ) override final; |
---|
| 168 | virtual Declaration * mutate( TypedefDecl * typeDecl ) override final; |
---|
| 169 | virtual AsmDecl * mutate( AsmDecl * asmDecl ) override final; |
---|
[f6e3e34] | 170 | virtual StaticAssertDecl * mutate( StaticAssertDecl * assertDecl ) override final; |
---|
[5ea7a22] | 171 | |
---|
| 172 | virtual CompoundStmt * mutate( CompoundStmt * compoundStmt ) override final; |
---|
| 173 | virtual Statement * mutate( ExprStmt * exprStmt ) override final; |
---|
| 174 | virtual Statement * mutate( AsmStmt * asmStmt ) override final; |
---|
[cc32d83] | 175 | virtual Statement * mutate( DirectiveStmt * dirStmt ) override final; |
---|
[5ea7a22] | 176 | virtual Statement * mutate( IfStmt * ifStmt ) override final; |
---|
| 177 | virtual Statement * mutate( WhileStmt * whileStmt ) override final; |
---|
| 178 | virtual Statement * mutate( ForStmt * forStmt ) override final; |
---|
| 179 | virtual Statement * mutate( SwitchStmt * switchStmt ) override final; |
---|
| 180 | virtual Statement * mutate( CaseStmt * caseStmt ) override final; |
---|
| 181 | virtual Statement * mutate( BranchStmt * branchStmt ) override final; |
---|
| 182 | virtual Statement * mutate( ReturnStmt * returnStmt ) override final; |
---|
| 183 | virtual Statement * mutate( ThrowStmt * throwStmt ) override final; |
---|
| 184 | virtual Statement * mutate( TryStmt * tryStmt ) override final; |
---|
| 185 | virtual Statement * mutate( CatchStmt * catchStmt ) override final; |
---|
| 186 | virtual Statement * mutate( FinallyStmt * finallyStmt ) override final; |
---|
| 187 | virtual Statement * mutate( WaitForStmt * waitforStmt ) override final; |
---|
[61255ad] | 188 | virtual Statement * mutate( WithStmt * withStmt ) override final; |
---|
[5ea7a22] | 189 | virtual NullStmt * mutate( NullStmt * nullStmt ) override final; |
---|
| 190 | virtual Statement * mutate( DeclStmt * declStmt ) override final; |
---|
| 191 | virtual Statement * mutate( ImplicitCtorDtorStmt * impCtorDtorStmt ) override final; |
---|
| 192 | |
---|
| 193 | virtual Expression * mutate( ApplicationExpr * applicationExpr ) override final; |
---|
| 194 | virtual Expression * mutate( UntypedExpr * untypedExpr ) override final; |
---|
| 195 | virtual Expression * mutate( NameExpr * nameExpr ) override final; |
---|
[9a705dc8] | 196 | virtual Expression * mutate( AddressExpr * addrExpr ) override final; |
---|
[5ea7a22] | 197 | virtual Expression * mutate( LabelAddressExpr * labAddressExpr ) override final; |
---|
| 198 | virtual Expression * mutate( CastExpr * castExpr ) override final; |
---|
[9a705dc8] | 199 | virtual Expression * mutate( KeywordCastExpr * castExpr ) override final; |
---|
[5ea7a22] | 200 | virtual Expression * mutate( VirtualCastExpr * castExpr ) override final; |
---|
| 201 | virtual Expression * mutate( UntypedMemberExpr * memberExpr ) override final; |
---|
| 202 | virtual Expression * mutate( MemberExpr * memberExpr ) override final; |
---|
| 203 | virtual Expression * mutate( VariableExpr * variableExpr ) override final; |
---|
| 204 | virtual Expression * mutate( ConstantExpr * constantExpr ) override final; |
---|
| 205 | virtual Expression * mutate( SizeofExpr * sizeofExpr ) override final; |
---|
| 206 | virtual Expression * mutate( AlignofExpr * alignofExpr ) override final; |
---|
| 207 | virtual Expression * mutate( UntypedOffsetofExpr * offsetofExpr ) override final; |
---|
| 208 | virtual Expression * mutate( OffsetofExpr * offsetofExpr ) override final; |
---|
| 209 | virtual Expression * mutate( OffsetPackExpr * offsetPackExpr ) override final; |
---|
| 210 | virtual Expression * mutate( AttrExpr * attrExpr ) override final; |
---|
| 211 | virtual Expression * mutate( LogicalExpr * logicalExpr ) override final; |
---|
| 212 | virtual Expression * mutate( ConditionalExpr * conditionalExpr ) override final; |
---|
| 213 | virtual Expression * mutate( CommaExpr * commaExpr ) override final; |
---|
| 214 | virtual Expression * mutate( TypeExpr * typeExpr ) override final; |
---|
| 215 | virtual Expression * mutate( AsmExpr * asmExpr ) override final; |
---|
| 216 | virtual Expression * mutate( ImplicitCopyCtorExpr * impCpCtorExpr ) override final; |
---|
| 217 | virtual Expression * mutate( ConstructorExpr * ctorExpr ) override final; |
---|
| 218 | virtual Expression * mutate( CompoundLiteralExpr * compLitExpr ) override final; |
---|
| 219 | virtual Expression * mutate( RangeExpr * rangeExpr ) override final; |
---|
| 220 | virtual Expression * mutate( UntypedTupleExpr * tupleExpr ) override final; |
---|
| 221 | virtual Expression * mutate( TupleExpr * tupleExpr ) override final; |
---|
| 222 | virtual Expression * mutate( TupleIndexExpr * tupleExpr ) override final; |
---|
| 223 | virtual Expression * mutate( TupleAssignExpr * assignExpr ) override final; |
---|
| 224 | virtual Expression * mutate( StmtExpr * stmtExpr ) override final; |
---|
| 225 | virtual Expression * mutate( UniqueExpr * uniqueExpr ) override final; |
---|
[73367a8] | 226 | virtual Expression * mutate( UntypedInitExpr * initExpr ) override final; |
---|
| 227 | virtual Expression * mutate( InitExpr * initExpr ) override final; |
---|
[44b4114] | 228 | virtual Expression * mutate( DeletedExpr * delExpr ) override final; |
---|
[0f79853] | 229 | virtual Expression * mutate( DefaultArgExpr * argExpr ) override final; |
---|
[d807ca28] | 230 | virtual Expression * mutate( GenericExpr * genExpr ) override final; |
---|
[5ea7a22] | 231 | |
---|
| 232 | virtual Type * mutate( VoidType * basicType ) override final; |
---|
| 233 | virtual Type * mutate( BasicType * basicType ) override final; |
---|
| 234 | virtual Type * mutate( PointerType * pointerType ) override final; |
---|
| 235 | virtual Type * mutate( ArrayType * arrayType ) override final; |
---|
| 236 | virtual Type * mutate( ReferenceType * referenceType ) override final; |
---|
[c5d7701] | 237 | virtual Type * mutate( QualifiedType * qualType ) override final; |
---|
[5ea7a22] | 238 | virtual Type * mutate( FunctionType * functionType ) override final; |
---|
| 239 | virtual Type * mutate( StructInstType * aggregateUseType ) override final; |
---|
| 240 | virtual Type * mutate( UnionInstType * aggregateUseType ) override final; |
---|
| 241 | virtual Type * mutate( EnumInstType * aggregateUseType ) override final; |
---|
| 242 | virtual Type * mutate( TraitInstType * aggregateUseType ) override final; |
---|
| 243 | virtual Type * mutate( TypeInstType * aggregateUseType ) override final; |
---|
| 244 | virtual Type * mutate( TupleType * tupleType ) override final; |
---|
| 245 | virtual Type * mutate( TypeofType * typeofType ) override final; |
---|
| 246 | virtual Type * mutate( AttrType * attrType ) override final; |
---|
| 247 | virtual Type * mutate( VarArgsType * varArgsType ) override final; |
---|
| 248 | virtual Type * mutate( ZeroType * zeroType ) override final; |
---|
| 249 | virtual Type * mutate( OneType * oneType ) override final; |
---|
[47498bd] | 250 | virtual Type * mutate( GlobalScopeType * globalType ) override final; |
---|
[5ea7a22] | 251 | |
---|
| 252 | virtual Designation * mutate( Designation * designation ) override final; |
---|
| 253 | virtual Initializer * mutate( SingleInit * singleInit ) override final; |
---|
| 254 | virtual Initializer * mutate( ListInit * listInit ) override final; |
---|
| 255 | virtual Initializer * mutate( ConstructorInit * ctorInit ) override final; |
---|
| 256 | |
---|
| 257 | virtual Constant * mutate( Constant * constant ) override final; |
---|
| 258 | |
---|
| 259 | virtual Attribute * mutate( Attribute * attribute ) override final; |
---|
[447c356] | 260 | |
---|
| 261 | virtual TypeSubstitution * mutate( TypeSubstitution * sub ) final; |
---|
[ab904dc] | 262 | |
---|
[13932f14] | 263 | private: |
---|
[61d9b4b] | 264 | bool inFunction = false; |
---|
| 265 | |
---|
[6ca154b] | 266 | template<typename pass_t> friend void acceptAll( std::list< Declaration* > &decls, PassVisitor< pass_t >& visitor ); |
---|
| 267 | template<typename pass_t> friend void mutateAll( std::list< Declaration* > &decls, PassVisitor< pass_t >& visitor ); |
---|
[3c398b6] | 268 | template< typename TreeType, typename pass_t > friend void maybeAccept_impl( TreeType * tree, PassVisitor< pass_t > & visitor ); |
---|
| 269 | template< typename TreeType, typename pass_t > friend void maybeMutate_impl( TreeType *& tree, PassVisitor< pass_t > & mutator ); |
---|
| 270 | template< typename Container, typename pass_t > friend void maybeAccept_impl( Container & container, PassVisitor< pass_t > & visitor ); |
---|
| 271 | template< typename Container, typename pass_t > friend void maybeMutate_impl( Container & container, PassVisitor< pass_t > & mutator ); |
---|
[6ca154b] | 272 | |
---|
[296b2be] | 273 | template<typename node_type> void call_previsit ( node_type * node ) { previsit_impl ( pass, node, 0 ); } |
---|
| 274 | template<typename node_type> void call_postvisit( node_type * node ) { postvisit_impl( pass, node, 0 ); } |
---|
[13932f14] | 275 | |
---|
[296b2be] | 276 | template<typename node_type> void call_premutate ( node_type * node ) { premutate_impl( pass, node, 0 ); } |
---|
| 277 | template<typename return_type, typename node_type> return_type call_postmutate ( node_type * node ) { return postmutate_impl<return_type>( pass, node, 0 ); } |
---|
[ab904dc] | 278 | |
---|
[296b2be] | 279 | void call_beginScope() { begin_scope_impl( pass, 0 ); } |
---|
| 280 | void call_endScope () { end_scope_impl ( pass, 0 ); } |
---|
| 281 | |
---|
[3fb9a83] | 282 | void set_env( TypeSubstitution * env ) { set_env_impl( pass, env, 0); } |
---|
| 283 | |
---|
[6ca154b] | 284 | template< typename func_t > |
---|
| 285 | void handleStatementList( std::list< Statement * > & statements, func_t func ); |
---|
| 286 | void visitStatementList ( std::list< Statement* > &statements ); |
---|
[296b2be] | 287 | void mutateStatementList( std::list< Statement* > &statements ); |
---|
[134322e] | 288 | |
---|
[6ca154b] | 289 | template< typename func_t > |
---|
| 290 | Statement * handleStatement( Statement * stmt, func_t func ); |
---|
| 291 | Statement * visitStatement ( Statement * stmt ); |
---|
[296b2be] | 292 | Statement * mutateStatement( Statement * stmt ); |
---|
[134322e] | 293 | |
---|
[6ca154b] | 294 | template< typename func_t > |
---|
| 295 | Expression * handleExpression( Expression * expr, func_t func ); |
---|
| 296 | Expression * visitExpression ( Expression * expr ); |
---|
[296b2be] | 297 | Expression * mutateExpression( Expression * expr ); |
---|
| 298 | |
---|
[ab904dc] | 299 | |
---|
[02fdb8e] | 300 | auto get_env_ptr () -> decltype(env_impl( pass, 0)) { return env_impl( pass, 0); } |
---|
[134322e] | 301 | std::list< Statement* > * get_beforeStmts() { return stmtsToAddBefore_impl( pass, 0); } |
---|
| 302 | std::list< Statement* > * get_afterStmts () { return stmtsToAddAfter_impl ( pass, 0); } |
---|
[35df560] | 303 | std::list< Declaration* > * get_beforeDecls() { return declsToAddBefore_impl( pass, 0); } |
---|
| 304 | std::list< Declaration* > * get_afterDecls () { return declsToAddAfter_impl ( pass, 0); } |
---|
[b73bd70] | 305 | |
---|
[3c398b6] | 306 | bool get_visit_children () { bool_ref * ptr = visit_children_impl(pass, 0); return ptr ? *ptr : true; } |
---|
| 307 | bool_ref * get_visit_children_ptr() { return visit_children_impl(pass, 0); } |
---|
[e0886db] | 308 | |
---|
[4670c79] | 309 | void indexerScopeEnter () { indexer_impl_enterScope ( pass, 0 ); } |
---|
| 310 | void indexerScopeLeave () { indexer_impl_leaveScope ( pass, 0 ); } |
---|
| 311 | void indexerAddId ( DeclarationWithType * node ) { indexer_impl_addId ( pass, 0, node ); } |
---|
| 312 | void indexerAddType ( NamedTypeDecl * node ) { indexer_impl_addType ( pass, 0, node ); } |
---|
| 313 | void indexerAddStruct ( const std::string & id ) { indexer_impl_addStruct ( pass, 0, id ); } |
---|
| 314 | void indexerAddStruct ( StructDecl * node ) { indexer_impl_addStruct ( pass, 0, node ); } |
---|
| 315 | void indexerAddStructFwd( StructDecl * node ) { indexer_impl_addStructFwd( pass, 0, node ); } |
---|
| 316 | void indexerAddEnum ( EnumDecl * node ) { indexer_impl_addEnum ( pass, 0, node ); } |
---|
| 317 | void indexerAddUnion ( const std::string & id ) { indexer_impl_addUnion ( pass, 0, id ); } |
---|
| 318 | void indexerAddUnion ( UnionDecl * node ) { indexer_impl_addUnion ( pass, 0, node ); } |
---|
| 319 | void indexerAddUnionFwd ( UnionDecl * node ) { indexer_impl_addUnionFwd ( pass, 0, node ); } |
---|
| 320 | void indexerAddTrait ( TraitDecl * node ) { indexer_impl_addTrait ( pass, 0, node ); } |
---|
[3ed994e] | 321 | void indexerAddWith ( std::list< Expression * > & exprs, BaseSyntaxNode * withStmt ) { indexer_impl_addWith( pass, 0, exprs, withStmt ); } |
---|
[d8893ca] | 322 | |
---|
[e0886db] | 323 | |
---|
| 324 | template< typename TreeType, typename VisitorType > |
---|
| 325 | friend inline void indexerScopedAccept( TreeType * tree, VisitorType &visitor ); |
---|
| 326 | |
---|
| 327 | template< typename TreeType, typename VisitorType > |
---|
| 328 | friend inline void indexerScopedMutate( TreeType *& tree, VisitorType &visitor ); |
---|
[13932f14] | 329 | }; |
---|
| 330 | |
---|
[6e09f211] | 331 | template<typename pass_type, typename T> |
---|
| 332 | void GuardValue( pass_type * pass, T& val ) { |
---|
| 333 | pass->at_cleanup( [ val ]( void * newVal ) { |
---|
| 334 | * static_cast< T * >( newVal ) = val; |
---|
| 335 | }, static_cast< void * >( & val ) ); |
---|
| 336 | } |
---|
| 337 | |
---|
[637568b] | 338 | class WithTypeSubstitution { |
---|
| 339 | protected: |
---|
| 340 | WithTypeSubstitution() = default; |
---|
| 341 | ~WithTypeSubstitution() = default; |
---|
| 342 | |
---|
[4eb31f2b] | 343 | public: |
---|
[d24d4e1] | 344 | TypeSubstitution * env = nullptr; |
---|
[637568b] | 345 | }; |
---|
| 346 | |
---|
[02fdb8e] | 347 | class WithConstTypeSubstitution { |
---|
| 348 | protected: |
---|
| 349 | WithConstTypeSubstitution() = default; |
---|
| 350 | ~WithConstTypeSubstitution() = default; |
---|
| 351 | |
---|
| 352 | public: |
---|
| 353 | const TypeSubstitution * env = nullptr; |
---|
| 354 | }; |
---|
| 355 | |
---|
[637568b] | 356 | class WithStmtsToAdd { |
---|
| 357 | protected: |
---|
| 358 | WithStmtsToAdd() = default; |
---|
| 359 | ~WithStmtsToAdd() = default; |
---|
| 360 | |
---|
[4eb31f2b] | 361 | public: |
---|
[637568b] | 362 | std::list< Statement* > stmtsToAddBefore; |
---|
| 363 | std::list< Statement* > stmtsToAddAfter; |
---|
| 364 | }; |
---|
[d24d4e1] | 365 | |
---|
| 366 | class WithDeclsToAdd { |
---|
| 367 | protected: |
---|
| 368 | WithDeclsToAdd() = default; |
---|
[e0886db] | 369 | ~WithDeclsToAdd() { |
---|
| 370 | assert( declsToAddBefore.empty() ); |
---|
| 371 | } |
---|
[d24d4e1] | 372 | |
---|
| 373 | public: |
---|
| 374 | std::list< Declaration* > declsToAddBefore; |
---|
| 375 | std::list< Declaration* > declsToAddAfter; |
---|
| 376 | }; |
---|
| 377 | |
---|
[637568b] | 378 | class WithShortCircuiting { |
---|
| 379 | protected: |
---|
| 380 | WithShortCircuiting() = default; |
---|
| 381 | ~WithShortCircuiting() = default; |
---|
| 382 | |
---|
[4eb31f2b] | 383 | public: |
---|
[b73bd70] | 384 | bool_ref visit_children; |
---|
[637568b] | 385 | }; |
---|
| 386 | |
---|
[d24d4e1] | 387 | class WithGuards { |
---|
[637568b] | 388 | protected: |
---|
[d24d4e1] | 389 | WithGuards() = default; |
---|
| 390 | ~WithGuards() = default; |
---|
[637568b] | 391 | |
---|
[4eb31f2b] | 392 | public: |
---|
[637568b] | 393 | at_cleanup_t at_cleanup; |
---|
| 394 | |
---|
[4eb31f2b] | 395 | template< typename T > |
---|
[637568b] | 396 | void GuardValue( T& val ) { |
---|
| 397 | at_cleanup( [ val ]( void * newVal ) { |
---|
| 398 | * static_cast< T * >( newVal ) = val; |
---|
| 399 | }, static_cast< void * >( & val ) ); |
---|
| 400 | } |
---|
[5dd0704] | 401 | |
---|
[d24d4e1] | 402 | template< typename T > |
---|
| 403 | void GuardScope( T& val ) { |
---|
| 404 | val.beginScope(); |
---|
| 405 | at_cleanup( []( void * val ) { |
---|
| 406 | static_cast< T * >( val )->endScope(); |
---|
| 407 | }, static_cast< void * >( & val ) ); |
---|
| 408 | } |
---|
| 409 | |
---|
[5dd0704] | 410 | template< typename Func > |
---|
[d24d4e1] | 411 | void GuardAction( Func func ) { |
---|
| 412 | at_cleanup( [func](__attribute__((unused)) void *) { func(); }, nullptr ); |
---|
[5dd0704] | 413 | } |
---|
[637568b] | 414 | }; |
---|
| 415 | |
---|
[b73bd70] | 416 | template<typename pass_type> |
---|
| 417 | class WithVisitorRef { |
---|
| 418 | protected: |
---|
[d24d4e1] | 419 | WithVisitorRef() {} |
---|
| 420 | ~WithVisitorRef() {} |
---|
[b73bd70] | 421 | |
---|
| 422 | public: |
---|
[d24d4e1] | 423 | PassVisitor<pass_type> * const visitor = nullptr; |
---|
[b73bd70] | 424 | }; |
---|
[637568b] | 425 | |
---|
[e0886db] | 426 | class WithIndexer { |
---|
| 427 | protected: |
---|
| 428 | WithIndexer() {} |
---|
| 429 | ~WithIndexer() {} |
---|
| 430 | |
---|
| 431 | public: |
---|
| 432 | SymTab::Indexer indexer; |
---|
| 433 | }; |
---|
| 434 | |
---|
[675716e] | 435 | #include "Common/Stats.h" |
---|
| 436 | |
---|
| 437 | extern struct PassVisitorStats { |
---|
| 438 | size_t depth = 0; |
---|
| 439 | Stats::Counters::MaxCounter<double> * max = nullptr; |
---|
| 440 | Stats::Counters::AverageCounter<double> * avg = nullptr; |
---|
| 441 | } pass_visitor_stats; |
---|
| 442 | |
---|
[07c178f0] | 443 | #include "SynTree/TypeSubstitution.h" |
---|
[aff3af4] | 444 | #include "PassVisitor.impl.h" |
---|