Changeset 90152a4 for src/Common
- Timestamp:
- Aug 27, 2018, 4:40:34 PM (8 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum, stuck-waitfor-destruct
- Children:
- b7c89aa
- Parents:
- f9feab8 (diff), 305581d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)links above to see all the changes relative to each parent. - Location:
- src/Common
- Files:
-
- 4 added
- 9 edited
-
Debug.h (modified) (1 diff)
-
ErrorObjects.h (added)
-
Eval.cc (added)
-
Heap.cc (added)
-
Heap.h (added)
-
PassVisitor.h (modified) (14 diffs)
-
PassVisitor.impl.h (modified) (38 diffs)
-
PassVisitor.proto.h (modified) (6 diffs)
-
ScopedMap.h (modified) (19 diffs)
-
SemanticError.cc (modified) (1 diff)
-
SemanticError.h (modified) (1 diff)
-
module.mk (modified) (2 diffs)
-
utility.h (modified) (5 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/Common/Debug.h
rf9feab8 r90152a4 28 28 namespace Debug { 29 29 /// debug codegen a translation unit 30 static inline void codeGen( __attribute__((unused)) const std::list< Declaration * > & translationUnit, __attribute__((unused)) const std::string & label ) {30 static inline void codeGen( __attribute__((unused)) const std::list< Declaration * > & translationUnit, __attribute__((unused)) const std::string & label, __attribute__((unused)) LinkageSpec::Spec linkageFilter = LinkageSpec::Builtin ) { 31 31 #ifdef DEBUG 32 32 std::list< Declaration * > decls; 33 33 34 filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), [ ]( Declaration * decl ) {35 return ! LinkageSpec::isBuiltin( decl->get_linkage());34 filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), [linkageFilter]( Declaration * decl ) { 35 return ! (decl->linkage & linkageFilter); 36 36 }); 37 37 38 38 std::cerr << "======" << label << "======" << std::endl; 39 CodeGen::generate( decls, std::cerr, false, true ); 39 CodeGen::generate( 40 decls, 41 std::cerr, 42 true /* doIntrinsics */, 43 true /* pretty */, 44 false /* generateC */, 45 false /* lineMarks */, 46 true /* printTypeExpr */ 47 ); 40 48 #endif 41 49 } // dump 42 50 43 static inline void treeDump( __attribute__((unused)) const std::list< Declaration * > & translationUnit, __attribute__((unused)) const std::string & label ) {51 static inline void treeDump( __attribute__((unused)) const std::list< Declaration * > & translationUnit, __attribute__((unused)) const std::string & label, __attribute__((unused)) LinkageSpec::Spec linkageFilter = LinkageSpec::Compiler ) { 44 52 #ifdef DEBUG 45 53 std::list< Declaration * > decls; 46 54 47 filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), [ ]( Declaration * decl ) {48 return ! LinkageSpec::isBuiltin( decl->get_linkage());55 filter( translationUnit.begin(), translationUnit.end(), back_inserter( decls ), [linkageFilter]( Declaration * decl ) { 56 return ! (decl->linkage & linkageFilter); 49 57 }); 50 58 -
src/Common/PassVisitor.h
rf9feab8 r90152a4 19 19 #include "SynTree/Expression.h" 20 20 #include "SynTree/Constant.h" 21 #include "SynTree/TypeSubstitution.h" 21 22 class TypeSubstitution; 22 23 23 24 #include "PassVisitor.proto.h" … … 65 66 virtual void visit( TypedefDecl * typeDecl ) override final; 66 67 virtual void visit( AsmDecl * asmDecl ) override final; 68 virtual void visit( StaticAssertDecl * assertDecl ) override final; 67 69 68 70 virtual void visit( CompoundStmt * compoundStmt ) override final; 69 71 virtual void visit( ExprStmt * exprStmt ) override final; 70 72 virtual void visit( AsmStmt * asmStmt ) override final; 73 virtual void visit( DirectiveStmt * dirStmt ) override final; 71 74 virtual void visit( IfStmt * ifStmt ) override final; 72 75 virtual void visit( WhileStmt * whileStmt ) override final; … … 90 93 virtual void visit( NameExpr * nameExpr ) override final; 91 94 virtual void visit( CastExpr * castExpr ) override final; 95 virtual void visit( KeywordCastExpr * castExpr ) override final; 92 96 virtual void visit( VirtualCastExpr * castExpr ) override final; 93 97 virtual void visit( AddressExpr * addressExpr ) override final; … … 118 122 virtual void visit( StmtExpr * stmtExpr ) override final; 119 123 virtual void visit( UniqueExpr * uniqueExpr ) override final; 124 virtual void visit( UntypedInitExpr * initExpr ) override final; 125 virtual void visit( InitExpr * initExpr ) override final; 126 virtual void visit( DeletedExpr * delExpr ) override final; 127 virtual void visit( DefaultArgExpr * argExpr ) override final; 128 virtual void visit( GenericExpr * genExpr ) override final; 120 129 121 130 virtual void visit( VoidType * basicType ) override final; … … 124 133 virtual void visit( ArrayType * arrayType ) override final; 125 134 virtual void visit( ReferenceType * referenceType ) override final; 135 virtual void visit( QualifiedType * qualType ) override final; 126 136 virtual void visit( FunctionType * functionType ) override final; 127 137 virtual void visit( StructInstType * aggregateUseType ) override final; … … 136 146 virtual void visit( ZeroType * zeroType ) override final; 137 147 virtual void visit( OneType * oneType ) override final; 148 virtual void visit( GlobalScopeType * globalType ) override final; 138 149 139 150 virtual void visit( Designation * designation ) override final; … … 157 168 virtual Declaration * mutate( TypedefDecl * typeDecl ) override final; 158 169 virtual AsmDecl * mutate( AsmDecl * asmDecl ) override final; 170 virtual StaticAssertDecl * mutate( StaticAssertDecl * assertDecl ) override final; 159 171 160 172 virtual CompoundStmt * mutate( CompoundStmt * compoundStmt ) override final; 161 173 virtual Statement * mutate( ExprStmt * exprStmt ) override final; 162 174 virtual Statement * mutate( AsmStmt * asmStmt ) override final; 175 virtual Statement * mutate( DirectiveStmt * dirStmt ) override final; 163 176 virtual Statement * mutate( IfStmt * ifStmt ) override final; 164 177 virtual Statement * mutate( WhileStmt * whileStmt ) override final; … … 181 194 virtual Expression * mutate( UntypedExpr * untypedExpr ) override final; 182 195 virtual Expression * mutate( NameExpr * nameExpr ) override final; 183 virtual Expression * mutate( AddressExpr * castExpr ) override final;196 virtual Expression * mutate( AddressExpr * addrExpr ) override final; 184 197 virtual Expression * mutate( LabelAddressExpr * labAddressExpr ) override final; 185 198 virtual Expression * mutate( CastExpr * castExpr ) override final; 199 virtual Expression * mutate( KeywordCastExpr * castExpr ) override final; 186 200 virtual Expression * mutate( VirtualCastExpr * castExpr ) override final; 187 201 virtual Expression * mutate( UntypedMemberExpr * memberExpr ) override final; … … 210 224 virtual Expression * mutate( StmtExpr * stmtExpr ) override final; 211 225 virtual Expression * mutate( UniqueExpr * uniqueExpr ) override final; 226 virtual Expression * mutate( UntypedInitExpr * initExpr ) override final; 227 virtual Expression * mutate( InitExpr * initExpr ) override final; 228 virtual Expression * mutate( DeletedExpr * delExpr ) override final; 229 virtual Expression * mutate( DefaultArgExpr * argExpr ) override final; 230 virtual Expression * mutate( GenericExpr * genExpr ) override final; 212 231 213 232 virtual Type * mutate( VoidType * basicType ) override final; … … 216 235 virtual Type * mutate( ArrayType * arrayType ) override final; 217 236 virtual Type * mutate( ReferenceType * referenceType ) override final; 237 virtual Type * mutate( QualifiedType * qualType ) override final; 218 238 virtual Type * mutate( FunctionType * functionType ) override final; 219 239 virtual Type * mutate( StructInstType * aggregateUseType ) override final; … … 228 248 virtual Type * mutate( ZeroType * zeroType ) override final; 229 249 virtual Type * mutate( OneType * oneType ) override final; 250 virtual Type * mutate( GlobalScopeType * globalType ) override final; 230 251 231 252 virtual Designation * mutate( Designation * designation ) override final; … … 243 264 244 265 private: 266 bool inFunction = false; 267 245 268 template<typename pass_t> friend void acceptAll( std::list< Declaration* > &decls, PassVisitor< pass_t >& visitor ); 246 269 template<typename pass_t> friend void mutateAll( std::list< Declaration* > &decls, PassVisitor< pass_t >& visitor ); … … 286 309 bool_ref * get_visit_children_ptr() { return visit_children_impl(pass, 0); } 287 310 288 void indexerScopeEnter () { indexer_impl_enterScope ( pass, 0 ); }289 void indexerScopeLeave () { indexer_impl_leaveScope ( pass, 0 ); }290 void indexerAddId ( DeclarationWithType * node) { indexer_impl_addId ( pass, 0, node ); }291 void indexerAddType ( NamedTypeDecl * node) { indexer_impl_addType ( pass, 0, node ); }292 void indexerAddStruct ( const std::string & id) { indexer_impl_addStruct ( pass, 0, id ); }293 void indexerAddStruct ( StructDecl * node) { indexer_impl_addStruct ( pass, 0, node ); }294 void indexerAddStructFwd( StructDecl * node) { indexer_impl_addStructFwd( pass, 0, node ); }295 void indexerAddEnum ( EnumDecl * node) { indexer_impl_addEnum ( pass, 0, node ); }296 void indexerAddUnion ( const std::string & id) { indexer_impl_addUnion ( pass, 0, id ); }297 void indexerAddUnion ( UnionDecl * node) { indexer_impl_addUnion ( pass, 0, node ); }298 void indexerAddUnionFwd ( UnionDecl * node) { indexer_impl_addUnionFwd ( pass, 0, node ); }299 void indexerAddTrait ( TraitDecl * node) { indexer_impl_addTrait ( pass, 0, node ); }300 void indexerAddWith ( WithStmt * node ) { indexer_impl_addWith ( pass, 0, node); }311 void indexerScopeEnter () { indexer_impl_enterScope ( pass, 0 ); } 312 void indexerScopeLeave () { indexer_impl_leaveScope ( pass, 0 ); } 313 void indexerAddId ( DeclarationWithType * node ) { indexer_impl_addId ( pass, 0, node ); } 314 void indexerAddType ( NamedTypeDecl * node ) { indexer_impl_addType ( pass, 0, node ); } 315 void indexerAddStruct ( const std::string & id ) { indexer_impl_addStruct ( pass, 0, id ); } 316 void indexerAddStruct ( StructDecl * node ) { indexer_impl_addStruct ( pass, 0, node ); } 317 void indexerAddStructFwd( StructDecl * node ) { indexer_impl_addStructFwd( pass, 0, node ); } 318 void indexerAddEnum ( EnumDecl * node ) { indexer_impl_addEnum ( pass, 0, node ); } 319 void indexerAddUnion ( const std::string & id ) { indexer_impl_addUnion ( pass, 0, id ); } 320 void indexerAddUnion ( UnionDecl * node ) { indexer_impl_addUnion ( pass, 0, node ); } 321 void indexerAddUnionFwd ( UnionDecl * node ) { indexer_impl_addUnionFwd ( pass, 0, node ); } 322 void indexerAddTrait ( TraitDecl * node ) { indexer_impl_addTrait ( pass, 0, node ); } 323 void indexerAddWith ( std::list< Expression * > & exprs, BaseSyntaxNode * withStmt ) { indexer_impl_addWith( pass, 0, exprs, withStmt ); } 301 324 302 325 … … 403 426 }; 404 427 428 #include "SynTree/TypeSubstitution.h" 405 429 #include "PassVisitor.impl.h" -
src/Common/PassVisitor.impl.h
rf9feab8 r90152a4 62 62 63 63 template< typename pass_type > 64 staticinline void acceptAll( std::list< Declaration* > &decls, PassVisitor< pass_type >& visitor ) {64 inline void acceptAll( std::list< Declaration* > &decls, PassVisitor< pass_type >& visitor ) { 65 65 DeclList_t* beforeDecls = visitor.get_beforeDecls(); 66 66 DeclList_t* afterDecls = visitor.get_afterDecls(); 67 SemanticError errors;67 SemanticErrorException errors; 68 68 69 69 for ( std::list< Declaration* >::iterator i = decls.begin(); ; ++i ) { … … 76 76 // run visitor on declaration 77 77 maybeAccept_impl( *i, visitor ); 78 } catch( SemanticError &e ) { 79 e.set_location( (*i)->location ); 78 } catch( SemanticErrorException &e ) { 80 79 errors.append( e ); 81 80 } … … 90 89 91 90 template< typename pass_type > 92 staticinline void mutateAll( std::list< Declaration* > &decls, PassVisitor< pass_type >& mutator ) {91 inline void mutateAll( std::list< Declaration* > &decls, PassVisitor< pass_type >& mutator ) { 93 92 DeclList_t* beforeDecls = mutator.get_beforeDecls(); 94 93 DeclList_t* afterDecls = mutator.get_afterDecls(); 95 SemanticError errors;94 SemanticErrorException errors; 96 95 97 96 for ( std::list< Declaration* >::iterator i = decls.begin(); ; ++i ) { … … 103 102 // run mutator on declaration 104 103 maybeMutate_impl( *i, mutator ); 105 } catch( SemanticError &e ) { 106 e.set_location( (*i)->location ); 104 } catch( SemanticErrorException &e ) { 107 105 errors.append( e ); 108 106 } … … 127 125 inline void maybeAccept_impl( Container & container, PassVisitor< pass_type > & visitor ) { 128 126 if ( ! visitor.get_visit_children() ) return; 129 SemanticError errors;127 SemanticErrorException errors; 130 128 for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) { 131 129 try { … … 133 131 (*i)->accept( visitor ); 134 132 } 135 } catch( SemanticError &e ) { 136 e.set_location( (*i)->location ); 133 } catch( SemanticErrorException &e ) { 137 134 errors.append( e ); 138 135 } … … 155 152 inline void maybeMutate_impl( Container & container, PassVisitor< pass_type > & mutator ) { 156 153 if ( ! mutator.get_visit_children() ) return; 157 SemanticError errors;154 SemanticErrorException errors; 158 155 for ( typename Container::iterator i = container.begin(); i != container.end(); ++i ) { 159 156 try { … … 162 159 assert( *i ); 163 160 } // if 164 } catch( SemanticError &e ) { 165 e.set_location( (*i)->location ); 161 } catch( SemanticErrorException &e ) { 166 162 errors.append( e ); 167 163 } // try … … 176 172 void PassVisitor< pass_type >::handleStatementList( std::list< Statement * > & statements, func_t func ) { 177 173 if ( ! get_visit_children() ) return; 178 SemanticError errors;174 SemanticErrorException errors; 179 175 180 176 // don't want statements from outer CompoundStmts to be added to this CompoundStmt … … 199 195 || ( empty( beforeDecls ) && empty( afterDecls )) ); 200 196 201 } catch ( SemanticError &e ) { 202 e.set_location( (*i)->location ); 197 } catch ( SemanticErrorException &e ) { 203 198 errors.append( e ); 204 199 } … … 365 360 maybeAccept_impl ( node->attributes , *this ); 366 361 367 if ( node->name != "" ) { 368 indexerAddId( node ); 369 } 362 indexerAddId( node ); 370 363 371 364 VISIT_END( node ); … … 381 374 maybeMutate_impl ( node->attributes , *this ); 382 375 383 if ( node->name != "" ) { 384 indexerAddId( node ); 385 } 376 indexerAddId( node ); 386 377 387 378 MUTATE_END( DeclarationWithType, node ); … … 394 385 VISIT_START( node ); 395 386 396 if ( node->name != "" ) { 397 indexerAddId( node ); 398 } 399 387 indexerAddId( node ); 388 389 maybeAccept_impl( node->withExprs, *this ); 400 390 { 391 // with clause introduces a level of scope (for the with expression members). 392 // with clause exprs are added to the indexer before parameters so that parameters 393 // shadow with exprs and not the other way around. 401 394 auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } ); 402 // implicit add __func__ identifier as specified in the C manual 6.4.2.2 403 static ObjectDecl func( 404 "__func__", noStorageClasses, LinkageSpec::C, nullptr, 405 new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ), nullptr, true, false ), 406 nullptr 407 ); 408 indexerAddId( &func ); 409 maybeAccept_impl( node->type, *this ); 410 maybeAccept_impl( node->statements, *this ); 411 maybeAccept_impl( node->attributes, *this ); 395 indexerAddWith( node->withExprs, node ); 396 { 397 auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } ); 398 // implicit add __func__ identifier as specified in the C manual 6.4.2.2 399 static ObjectDecl func( 400 "__func__", noStorageClasses, LinkageSpec::C, nullptr, 401 new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ), nullptr, true, false ), 402 nullptr 403 ); 404 indexerAddId( &func ); 405 maybeAccept_impl( node->type, *this ); 406 // function body needs to have the same scope as parameters - CompoundStmt will not enter 407 // a new scope if inFunction is true 408 ValueGuard< bool > oldInFunction( inFunction ); 409 inFunction = true; 410 maybeAccept_impl( node->statements, *this ); 411 maybeAccept_impl( node->attributes, *this ); 412 } 412 413 } 413 414 … … 419 420 MUTATE_START( node ); 420 421 421 if ( node->name != "" ) { 422 indexerAddId( node ); 423 } 422 indexerAddId( node ); 424 423 425 424 { 425 // with clause introduces a level of scope (for the with expression members). 426 // with clause exprs are added to the indexer before parameters so that parameters 427 // shadow with exprs and not the other way around. 426 428 auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } ); 427 // implicit add __func__ identifier as specified in the C manual 6.4.2.2 428 static ObjectDecl func( 429 "__func__", noStorageClasses, LinkageSpec::C, nullptr, 430 new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ), nullptr, true, false ), 431 nullptr 432 ); 433 indexerAddId( &func ); 434 maybeMutate_impl( node->type, *this ); 435 maybeMutate_impl( node->statements, *this ); 436 maybeMutate_impl( node->attributes, *this ); 429 indexerAddWith( node->withExprs, node ); 430 { 431 auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } ); 432 // implicit add __func__ identifier as specified in the C manual 6.4.2.2 433 static ObjectDecl func( 434 "__func__", noStorageClasses, LinkageSpec::C, nullptr, 435 new ArrayType( Type::Qualifiers(), new BasicType( Type::Qualifiers( Type::Const ), BasicType::Char ), nullptr, true, false ), 436 nullptr 437 ); 438 indexerAddId( &func ); 439 maybeMutate_impl( node->type, *this ); 440 // function body needs to have the same scope as parameters - CompoundStmt will not enter 441 // a new scope if inFunction is true 442 ValueGuard< bool > oldInFunction( inFunction ); 443 inFunction = true; 444 maybeMutate_impl( node->statements, *this ); 445 maybeMutate_impl( node->attributes, *this ); 446 } 437 447 } 438 448 … … 683 693 684 694 //-------------------------------------------------------------------------- 695 // StaticAssertDecl 696 template< typename pass_type > 697 void PassVisitor< pass_type >::visit( StaticAssertDecl * node ) { 698 VISIT_START( node ); 699 700 node->condition = visitExpression( node->condition ); 701 maybeAccept_impl( node->message, *this ); 702 703 VISIT_END( node ); 704 } 705 706 template< typename pass_type > 707 StaticAssertDecl * PassVisitor< pass_type >::mutate( StaticAssertDecl * node ) { 708 MUTATE_START( node ); 709 710 node->condition = mutateExpression( node->condition ); 711 maybeMutate_impl( node->message, *this ); 712 713 MUTATE_END( StaticAssertDecl, node ); 714 } 715 716 //-------------------------------------------------------------------------- 685 717 // CompoundStmt 686 718 template< typename pass_type > … … 688 720 VISIT_START( node ); 689 721 { 690 auto guard1 = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } ); 722 // do not enter a new scope if inFunction is true - needs to check old state before the assignment 723 ValueGuard< bool > oldInFunction( inFunction ); 724 auto guard1 = makeFuncGuard( [this, &oldInFunction]() { if ( ! oldInFunction.old ) indexerScopeEnter(); }, [this, &oldInFunction]() { if ( ! oldInFunction.old ) indexerScopeLeave(); } ); 691 725 auto guard2 = makeFuncGuard( [this]() { call_beginScope(); }, [this]() { call_endScope(); } ); 726 inFunction = false; 692 727 visitStatementList( node->kids ); 693 728 } … … 699 734 MUTATE_START( node ); 700 735 { 701 auto guard1 = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } ); 736 // do not enter a new scope if inFunction is true - needs to check old state before the assignment 737 ValueGuard< bool > oldInFunction( inFunction ); 738 auto guard1 = makeFuncGuard( [this, &oldInFunction]() { if ( ! oldInFunction.old ) indexerScopeEnter(); }, [this, &oldInFunction]() { if ( ! oldInFunction.old ) indexerScopeLeave(); } ); 702 739 auto guard2 = makeFuncGuard( [this]() { call_beginScope(); }, [this]() { call_endScope(); } ); 740 inFunction = false; 703 741 mutateStatementList( node->kids ); 704 742 } … … 730 768 template< typename pass_type > 731 769 void PassVisitor< pass_type >::visit( AsmStmt * node ) { 732 VISIT_BODY( node ); 770 VISIT_START( node ) 771 772 maybeAccept_impl( node->instruction, *this ); 773 maybeAccept_impl( node->output, *this ); 774 maybeAccept_impl( node->input, *this ); 775 maybeAccept_impl( node->clobber, *this ); 776 777 VISIT_END( node ); 733 778 } 734 779 735 780 template< typename pass_type > 736 781 Statement * PassVisitor< pass_type >::mutate( AsmStmt * node ) { 737 MUTATE_BODY( Statement, node ); 782 MUTATE_START( node ); 783 784 maybeMutate_impl( node->instruction, *this ); 785 maybeMutate_impl( node->output, *this ); 786 maybeMutate_impl( node->input, *this ); 787 maybeMutate_impl( node->clobber, *this ); 788 789 MUTATE_END( Statement, node ); 790 } 791 792 //-------------------------------------------------------------------------- 793 // AsmStmt 794 template< typename pass_type > 795 void PassVisitor< pass_type >::visit( DirectiveStmt * node ) { 796 VISIT_START( node ) 797 798 VISIT_END( node ); 799 } 800 801 template< typename pass_type > 802 Statement * PassVisitor< pass_type >::mutate( DirectiveStmt * node ) { 803 MUTATE_START( node ); 804 805 MUTATE_END( Statement, node ); 738 806 } 739 807 … … 774 842 VISIT_START( node ); 775 843 776 visitExpression( node->condition ); 777 node->body = visitStatement( node->body ); 844 { 845 // while statements introduce a level of scope (for the initialization) 846 auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } ); 847 maybeAccept_impl( node->initialization, *this ); 848 visitExpression ( node->condition ); 849 node->body = visitStatement( node->body ); 850 } 778 851 779 852 VISIT_END( node ); … … 784 857 MUTATE_START( node ); 785 858 786 node->condition = mutateExpression( node->condition ); 787 node->body = mutateStatement ( node->body ); 859 { 860 // while statements introduce a level of scope (for the initialization) 861 auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } ); 862 maybeMutate_impl( node->initialization, *this ); 863 node->condition = mutateExpression( node->condition ); 864 node->body = mutateStatement ( node->body ); 865 } 866 788 867 789 868 MUTATE_END( Statement, node ); … … 868 947 template< typename pass_type > 869 948 void PassVisitor< pass_type >::visit( BranchStmt * node ) { 870 VISIT_BODY( node ); 949 VISIT_START( node ); 950 VISIT_END( node ); 871 951 } 872 952 873 953 template< typename pass_type > 874 954 Statement * PassVisitor< pass_type >::mutate( BranchStmt * node ) { 875 MUTATE_BODY( Statement, node ); 955 MUTATE_START( node ); 956 MUTATE_END( Statement, node ); 876 957 } 877 958 … … 901 982 template< typename pass_type > 902 983 void PassVisitor< pass_type >::visit( ThrowStmt * node ) { 903 VISIT_BODY( node ); 984 VISIT_START( node ); 985 986 maybeAccept_impl( node->expr, *this ); 987 maybeAccept_impl( node->target, *this ); 988 989 VISIT_END( node ); 904 990 } 905 991 906 992 template< typename pass_type > 907 993 Statement * PassVisitor< pass_type >::mutate( ThrowStmt * node ) { 908 MUTATE_BODY( Statement, node ); 994 MUTATE_START( node ); 995 996 maybeMutate_impl( node->expr, *this ); 997 maybeMutate_impl( node->target, *this ); 998 999 MUTATE_END( Statement, node ); 909 1000 } 910 1001 … … 965 1056 template< typename pass_type > 966 1057 void PassVisitor< pass_type >::visit( FinallyStmt * node ) { 967 VISIT_BODY( node ); 1058 VISIT_START( node ); 1059 1060 maybeAccept_impl( node->block, *this ); 1061 1062 VISIT_END( node ); 968 1063 } 969 1064 970 1065 template< typename pass_type > 971 1066 Statement * PassVisitor< pass_type >::mutate( FinallyStmt * node ) { 972 MUTATE_BODY( Statement, node ); 1067 MUTATE_START( node ); 1068 1069 maybeMutate_impl( node->block, *this ); 1070 1071 MUTATE_END( Statement, node ); 973 1072 } 974 1073 … … 977 1076 template< typename pass_type > 978 1077 void PassVisitor< pass_type >::visit( WaitForStmt * node ) { 979 VISIT_BODY( node ); 1078 VISIT_START( node ); 1079 1080 for( auto & clause : node->clauses ) { 1081 maybeAccept_impl( clause.target.function, *this ); 1082 maybeAccept_impl( clause.target.arguments, *this ); 1083 1084 maybeAccept_impl( clause.statement, *this ); 1085 maybeAccept_impl( clause.condition, *this ); 1086 } 1087 1088 maybeAccept_impl( node->timeout.time, *this ); 1089 maybeAccept_impl( node->timeout.statement, *this ); 1090 maybeAccept_impl( node->timeout.condition, *this ); 1091 maybeAccept_impl( node->orelse.statement, *this ); 1092 maybeAccept_impl( node->orelse.condition, *this ); 1093 1094 VISIT_END( node ); 980 1095 } 981 1096 982 1097 template< typename pass_type > 983 1098 Statement * PassVisitor< pass_type >::mutate( WaitForStmt * node ) { 984 MUTATE_BODY( Statement, node ); 1099 MUTATE_START( node ); 1100 1101 for( auto & clause : node->clauses ) { 1102 maybeMutate_impl( clause.target.function, *this ); 1103 maybeMutate_impl( clause.target.arguments, *this ); 1104 1105 maybeMutate_impl( clause.statement, *this ); 1106 maybeMutate_impl( clause.condition, *this ); 1107 } 1108 1109 maybeMutate_impl( node->timeout.time, *this ); 1110 maybeMutate_impl( node->timeout.statement, *this ); 1111 maybeMutate_impl( node->timeout.condition, *this ); 1112 maybeMutate_impl( node->orelse.statement, *this ); 1113 maybeMutate_impl( node->orelse.condition, *this ); 1114 1115 MUTATE_END( Statement, node ); 985 1116 } 986 1117 … … 996 1127 // catch statements introduce a level of scope (for the caught exception) 997 1128 auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } ); 998 indexerAddWith( node );1129 indexerAddWith( node->exprs, node ); 999 1130 maybeAccept_impl( node->stmt, *this ); 1000 1131 } … … 1009 1140 // catch statements introduce a level of scope (for the caught exception) 1010 1141 auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } ); 1011 indexerAddWith( node );1142 indexerAddWith( node->exprs, node ); 1012 1143 maybeMutate_impl( node->stmt, *this ); 1013 1144 } … … 1019 1150 template< typename pass_type > 1020 1151 void PassVisitor< pass_type >::visit( NullStmt * node ) { 1021 VISIT_BODY( node ); 1152 VISIT_START( node ); 1153 VISIT_END( node ); 1022 1154 } 1023 1155 1024 1156 template< typename pass_type > 1025 1157 NullStmt * PassVisitor< pass_type >::mutate( NullStmt * node ) { 1026 MUTATE_BODY( NullStmt, node ); 1158 MUTATE_START( node ); 1159 MUTATE_END( NullStmt, node ); 1027 1160 } 1028 1161 … … 1031 1164 template< typename pass_type > 1032 1165 void PassVisitor< pass_type >::visit( DeclStmt * node ) { 1033 VISIT_BODY( node ); 1166 VISIT_START( node ); 1167 1168 maybeAccept_impl( node->decl, *this ); 1169 1170 VISIT_END( node ); 1034 1171 } 1035 1172 1036 1173 template< typename pass_type > 1037 1174 Statement * PassVisitor< pass_type >::mutate( DeclStmt * node ) { 1038 MUTATE_BODY( Statement, node ); 1175 MUTATE_START( node ); 1176 1177 maybeMutate_impl( node->decl, *this ); 1178 1179 MUTATE_END( Statement, node ); 1039 1180 } 1040 1181 … … 1043 1184 template< typename pass_type > 1044 1185 void PassVisitor< pass_type >::visit( ImplicitCtorDtorStmt * node ) { 1045 VISIT_BODY( node ); 1186 VISIT_START( node ); 1187 1188 maybeAccept_impl( node->callStmt, *this ); 1189 1190 VISIT_END( node ); 1046 1191 } 1047 1192 1048 1193 template< typename pass_type > 1049 1194 Statement * PassVisitor< pass_type >::mutate( ImplicitCtorDtorStmt * node ) { 1050 MUTATE_BODY( Statement, node ); 1195 MUTATE_START( node ); 1196 1197 maybeMutate_impl( node->callStmt, *this ); 1198 1199 MUTATE_END( Statement, node ); 1051 1200 } 1052 1201 … … 1141 1290 template< typename pass_type > 1142 1291 Expression * PassVisitor< pass_type >::mutate( CastExpr * node ) { 1292 MUTATE_START( node ); 1293 1294 indexerScopedMutate( node->env , *this ); 1295 indexerScopedMutate( node->result, *this ); 1296 maybeMutate_impl ( node->arg , *this ); 1297 1298 MUTATE_END( Expression, node ); 1299 } 1300 1301 //-------------------------------------------------------------------------- 1302 // KeywordCastExpr 1303 template< typename pass_type > 1304 void PassVisitor< pass_type >::visit( KeywordCastExpr * node ) { 1305 VISIT_START( node ); 1306 1307 indexerScopedAccept( node->result, *this ); 1308 maybeAccept_impl ( node->arg , *this ); 1309 1310 VISIT_END( node ); 1311 } 1312 1313 template< typename pass_type > 1314 Expression * PassVisitor< pass_type >::mutate( KeywordCastExpr * node ) { 1143 1315 MUTATE_START( node ); 1144 1316 … … 1404 1576 indexerScopedAccept( node->result, *this ); 1405 1577 maybeAccept_impl ( node->type , *this ); 1406 maybeAccept_impl ( node->member, *this );1407 1578 1408 1579 VISIT_END( node ); … … 1416 1587 indexerScopedMutate( node->result, *this ); 1417 1588 maybeMutate_impl ( node->type , *this ); 1418 maybeMutate_impl ( node->member, *this );1419 1589 1420 1590 MUTATE_END( Expression, node ); … … 1853 2023 } 1854 2024 2025 //-------------------------------------------------------------------------- 2026 // UntypedInitExpr 2027 template< typename pass_type > 2028 void PassVisitor< pass_type >::visit( UntypedInitExpr * node ) { 2029 VISIT_START( node ); 2030 2031 indexerScopedAccept( node->result, *this ); 2032 maybeAccept_impl ( node->expr , *this ); 2033 // not currently visiting initAlts, but this doesn't matter since this node is only used in the resolver. 2034 2035 VISIT_END( node ); 2036 } 2037 2038 template< typename pass_type > 2039 Expression * PassVisitor< pass_type >::mutate( UntypedInitExpr * node ) { 2040 MUTATE_START( node ); 2041 2042 indexerScopedMutate( node->env , *this ); 2043 indexerScopedMutate( node->result, *this ); 2044 maybeMutate_impl ( node->expr , *this ); 2045 // not currently visiting initAlts, but this doesn't matter since this node is only used in the resolver. 2046 2047 MUTATE_END( Expression, node ); 2048 } 2049 2050 //-------------------------------------------------------------------------- 2051 // InitExpr 2052 template< typename pass_type > 2053 void PassVisitor< pass_type >::visit( InitExpr * node ) { 2054 VISIT_START( node ); 2055 2056 indexerScopedAccept( node->result, *this ); 2057 maybeAccept_impl ( node->expr , *this ); 2058 maybeAccept_impl ( node->designation, *this ); 2059 2060 VISIT_END( node ); 2061 } 2062 2063 template< typename pass_type > 2064 Expression * PassVisitor< pass_type >::mutate( InitExpr * node ) { 2065 MUTATE_START( node ); 2066 2067 indexerScopedMutate( node->env , *this ); 2068 indexerScopedMutate( node->result, *this ); 2069 maybeMutate_impl ( node->expr , *this ); 2070 maybeMutate_impl ( node->designation, *this ); 2071 2072 MUTATE_END( Expression, node ); 2073 } 2074 2075 //-------------------------------------------------------------------------- 2076 // DeletedExpr 2077 template< typename pass_type > 2078 void PassVisitor< pass_type >::visit( DeletedExpr * node ) { 2079 VISIT_START( node ); 2080 2081 indexerScopedAccept( node->result, *this ); 2082 maybeAccept_impl( node->expr, *this ); 2083 // don't visit deleteStmt, because it is a pointer to somewhere else in the tree. 2084 2085 VISIT_END( node ); 2086 } 2087 2088 template< typename pass_type > 2089 Expression * PassVisitor< pass_type >::mutate( DeletedExpr * node ) { 2090 MUTATE_START( node ); 2091 2092 indexerScopedMutate( node->env, *this ); 2093 indexerScopedMutate( node->result, *this ); 2094 maybeMutate_impl( node->expr, *this ); 2095 2096 MUTATE_END( Expression, node ); 2097 } 2098 2099 //-------------------------------------------------------------------------- 2100 // DefaultArgExpr 2101 template< typename pass_type > 2102 void PassVisitor< pass_type >::visit( DefaultArgExpr * node ) { 2103 VISIT_START( node ); 2104 2105 indexerScopedAccept( node->result, *this ); 2106 maybeAccept_impl( node->expr, *this ); 2107 2108 VISIT_END( node ); 2109 } 2110 2111 template< typename pass_type > 2112 Expression * PassVisitor< pass_type >::mutate( DefaultArgExpr * node ) { 2113 MUTATE_START( node ); 2114 2115 indexerScopedMutate( node->env, *this ); 2116 indexerScopedMutate( node->result, *this ); 2117 maybeMutate_impl( node->expr, *this ); 2118 2119 MUTATE_END( Expression, node ); 2120 } 2121 2122 //-------------------------------------------------------------------------- 2123 // GenericExpr 2124 template< typename pass_type > 2125 void PassVisitor< pass_type >::visit( GenericExpr * node ) { 2126 VISIT_START( node ); 2127 2128 indexerScopedAccept( node->result, *this ); 2129 maybeAccept_impl( node->control, *this ); 2130 for ( GenericExpr::Association & assoc : node->associations ) { 2131 indexerScopedAccept( assoc.type, *this ); 2132 maybeAccept_impl( assoc.expr, *this ); 2133 } 2134 2135 VISIT_END( node ); 2136 } 2137 2138 template< typename pass_type > 2139 Expression * PassVisitor< pass_type >::mutate( GenericExpr * node ) { 2140 MUTATE_START( node ); 2141 2142 indexerScopedMutate( node->env, *this ); 2143 indexerScopedMutate( node->result, *this ); 2144 maybeMutate_impl( node->control, *this ); 2145 for ( GenericExpr::Association & assoc : node->associations ) { 2146 indexerScopedMutate( assoc.type, *this ); 2147 maybeMutate_impl( assoc.expr, *this ); 2148 } 2149 2150 MUTATE_END( Expression, node ); 2151 } 2152 2153 //-------------------------------------------------------------------------- 2154 // VoidType 1855 2155 template< typename pass_type > 1856 2156 void PassVisitor< pass_type >::visit( VoidType * node ) { 1857 VISIT_BODY( node ); 1858 } 1859 2157 VISIT_START( node ); 2158 2159 maybeAccept_impl( node->forall, *this ); 2160 2161 VISIT_END( node ); 2162 } 2163 2164 template< typename pass_type > 2165 Type * PassVisitor< pass_type >::mutate( VoidType * node ) { 2166 MUTATE_START( node ); 2167 2168 maybeMutate_impl( node->forall, *this ); 2169 2170 MUTATE_END( Type, node ); 2171 } 2172 2173 //-------------------------------------------------------------------------- 2174 // BasicType 1860 2175 template< typename pass_type > 1861 2176 void PassVisitor< pass_type >::visit( BasicType * node ) { 1862 VISIT_BODY( node ); 1863 } 1864 2177 VISIT_START( node ); 2178 2179 maybeAccept_impl( node->forall, *this ); 2180 2181 VISIT_END( node ); 2182 } 2183 2184 template< typename pass_type > 2185 Type * PassVisitor< pass_type >::mutate( BasicType * node ) { 2186 MUTATE_START( node ); 2187 2188 maybeMutate_impl( node->forall, *this ); 2189 2190 MUTATE_END( Type, node ); 2191 } 2192 2193 //-------------------------------------------------------------------------- 2194 // PointerType 1865 2195 template< typename pass_type > 1866 2196 void PassVisitor< pass_type >::visit( PointerType * node ) { 1867 VISIT_BODY( node ); 1868 } 1869 2197 VISIT_START( node ); 2198 2199 maybeAccept_impl( node->forall, *this ); 2200 // xxx - should PointerType visit/mutate dimension? 2201 maybeAccept_impl( node->base, *this ); 2202 2203 VISIT_END( node ); 2204 } 2205 2206 template< typename pass_type > 2207 Type * PassVisitor< pass_type >::mutate( PointerType * node ) { 2208 MUTATE_START( node ); 2209 2210 maybeMutate_impl( node->forall, *this ); 2211 // xxx - should PointerType visit/mutate dimension? 2212 maybeMutate_impl( node->base, *this ); 2213 2214 MUTATE_END( Type, node ); 2215 } 2216 2217 //-------------------------------------------------------------------------- 2218 // ArrayType 1870 2219 template< typename pass_type > 1871 2220 void PassVisitor< pass_type >::visit( ArrayType * node ) { 1872 VISIT_BODY( node ); 1873 } 1874 2221 VISIT_START( node ); 2222 2223 maybeAccept_impl( node->forall, *this ); 2224 maybeAccept_impl( node->dimension, *this ); 2225 maybeAccept_impl( node->base, *this ); 2226 2227 VISIT_END( node ); 2228 } 2229 2230 template< typename pass_type > 2231 Type * PassVisitor< pass_type >::mutate( ArrayType * node ) { 2232 MUTATE_START( node ); 2233 2234 maybeMutate_impl( node->forall, *this ); 2235 maybeMutate_impl( node->dimension, *this ); 2236 maybeMutate_impl( node->base, *this ); 2237 2238 MUTATE_END( Type, node ); 2239 } 2240 2241 //-------------------------------------------------------------------------- 2242 // ReferenceType 1875 2243 template< typename pass_type > 1876 2244 void PassVisitor< pass_type >::visit( ReferenceType * node ) { 1877 VISIT_BODY( node ); 1878 } 1879 2245 VISIT_START( node ); 2246 2247 maybeAccept_impl( node->forall, *this ); 2248 maybeAccept_impl( node->base, *this ); 2249 2250 VISIT_END( node ); 2251 } 2252 2253 template< typename pass_type > 2254 Type * PassVisitor< pass_type >::mutate( ReferenceType * node ) { 2255 MUTATE_START( node ); 2256 2257 maybeMutate_impl( node->forall, *this ); 2258 maybeMutate_impl( node->base, *this ); 2259 2260 MUTATE_END( Type, node ); 2261 } 2262 2263 //-------------------------------------------------------------------------- 2264 // QualifiedType 2265 template< typename pass_type > 2266 void PassVisitor< pass_type >::visit( QualifiedType * node ) { 2267 VISIT_START( node ); 2268 2269 maybeAccept_impl( node->forall, *this ); 2270 maybeAccept_impl( node->parent, *this ); 2271 maybeAccept_impl( node->child, *this ); 2272 2273 VISIT_END( node ); 2274 } 2275 2276 template< typename pass_type > 2277 Type * PassVisitor< pass_type >::mutate( QualifiedType * node ) { 2278 MUTATE_START( node ); 2279 2280 maybeMutate_impl( node->forall, *this ); 2281 maybeMutate_impl( node->parent, *this ); 2282 maybeMutate_impl( node->child, *this ); 2283 2284 MUTATE_END( Type, node ); 2285 } 2286 2287 //-------------------------------------------------------------------------- 2288 // FunctionType 1880 2289 template< typename pass_type > 1881 2290 void PassVisitor< pass_type >::visit( FunctionType * node ) { 1882 VISIT_BODY( node ); 2291 VISIT_START( node ); 2292 2293 maybeAccept_impl( node->forall, *this ); 2294 maybeAccept_impl( node->returnVals, *this ); 2295 maybeAccept_impl( node->parameters, *this ); 2296 2297 VISIT_END( node ); 2298 } 2299 2300 template< typename pass_type > 2301 Type * PassVisitor< pass_type >::mutate( FunctionType * node ) { 2302 MUTATE_START( node ); 2303 2304 maybeMutate_impl( node->forall, *this ); 2305 maybeMutate_impl( node->returnVals, *this ); 2306 maybeMutate_impl( node->parameters, *this ); 2307 2308 MUTATE_END( Type, node ); 1883 2309 } 1884 2310 … … 1951 2377 template< typename pass_type > 1952 2378 void PassVisitor< pass_type >::visit( EnumInstType * node ) { 1953 VISIT_BODY( node ); 2379 VISIT_START( node ); 2380 2381 maybeAccept_impl( node->forall, *this ); 2382 maybeAccept_impl( node->parameters, *this ); 2383 2384 VISIT_END( node ); 1954 2385 } 1955 2386 1956 2387 template< typename pass_type > 1957 2388 Type * PassVisitor< pass_type >::mutate( EnumInstType * node ) { 1958 MUTATE_BODY( Type, node ); 2389 MUTATE_START( node ); 2390 2391 maybeMutate_impl( node->forall, *this ); 2392 maybeMutate_impl( node->parameters, *this ); 2393 2394 MUTATE_END( Type, node ); 1959 2395 } 1960 2396 … … 1985 2421 template< typename pass_type > 1986 2422 void PassVisitor< pass_type >::visit( TypeInstType * node ) { 1987 VISIT_BODY( node ); 1988 } 1989 2423 VISIT_START( node ); 2424 2425 maybeAccept_impl( node->forall , *this ); 2426 maybeAccept_impl( node->parameters, *this ); 2427 2428 VISIT_END( node ); 2429 } 2430 2431 template< typename pass_type > 2432 Type * PassVisitor< pass_type >::mutate( TypeInstType * node ) { 2433 MUTATE_START( node ); 2434 2435 maybeMutate_impl( node->forall , *this ); 2436 maybeMutate_impl( node->parameters, *this ); 2437 2438 MUTATE_END( Type, node ); 2439 } 2440 2441 //-------------------------------------------------------------------------- 2442 // TupleType 1990 2443 template< typename pass_type > 1991 2444 void PassVisitor< pass_type >::visit( TupleType * node ) { 1992 VISIT_BODY( node ); 1993 } 1994 2445 VISIT_START( node ); 2446 2447 maybeAccept_impl( node->forall, *this ); 2448 maybeAccept_impl( node->types, *this ); 2449 maybeAccept_impl( node->members, *this ); 2450 2451 VISIT_END( node ); 2452 } 2453 2454 template< typename pass_type > 2455 Type * PassVisitor< pass_type >::mutate( TupleType * node ) { 2456 MUTATE_START( node ); 2457 2458 maybeMutate_impl( node->forall, *this ); 2459 maybeMutate_impl( node->types, *this ); 2460 maybeMutate_impl( node->members, *this ); 2461 2462 MUTATE_END( Type, node ); 2463 } 2464 2465 //-------------------------------------------------------------------------- 2466 // TypeofType 1995 2467 template< typename pass_type > 1996 2468 void PassVisitor< pass_type >::visit( TypeofType * node ) { 1997 VISIT_BODY( node ); 1998 } 1999 2469 VISIT_START( node ); 2470 2471 assert( node->expr ); 2472 maybeAccept_impl( node->expr, *this ); 2473 2474 VISIT_END( node ); 2475 } 2476 2477 template< typename pass_type > 2478 Type * PassVisitor< pass_type >::mutate( TypeofType * node ) { 2479 MUTATE_START( node ); 2480 2481 assert( node->expr ); 2482 maybeMutate_impl( node->expr, *this ); 2483 2484 MUTATE_END( Type, node ); 2485 } 2486 2487 //-------------------------------------------------------------------------- 2488 // AttrType 2000 2489 template< typename pass_type > 2001 2490 void PassVisitor< pass_type >::visit( AttrType * node ) { 2002 VISIT_BODY( node ); 2003 } 2004 2491 VISIT_START( node ); 2492 2493 if ( node->isType ) { 2494 assert( node->type ); 2495 maybeAccept_impl( node->type, *this ); 2496 } else { 2497 assert( node->expr ); 2498 maybeAccept_impl( node->expr, *this ); 2499 } // if 2500 2501 VISIT_END( node ); 2502 } 2503 2504 template< typename pass_type > 2505 Type * PassVisitor< pass_type >::mutate( AttrType * node ) { 2506 MUTATE_START( node ); 2507 2508 if ( node->isType ) { 2509 assert( node->type ); 2510 maybeMutate_impl( node->type, *this ); 2511 } else { 2512 assert( node->expr ); 2513 maybeMutate_impl( node->expr, *this ); 2514 } // if 2515 2516 MUTATE_END( Type, node ); 2517 } 2518 2519 //-------------------------------------------------------------------------- 2520 // VarArgsType 2005 2521 template< typename pass_type > 2006 2522 void PassVisitor< pass_type >::visit( VarArgsType * node ) { 2007 VISIT_BODY( node ); 2008 } 2009 2523 VISIT_START( node ); 2524 2525 maybeAccept_impl( node->forall, *this ); 2526 2527 VISIT_END( node ); 2528 } 2529 2530 template< typename pass_type > 2531 Type * PassVisitor< pass_type >::mutate( VarArgsType * node ) { 2532 MUTATE_START( node ); 2533 2534 maybeMutate_impl( node->forall, *this ); 2535 2536 MUTATE_END( Type, node ); 2537 } 2538 2539 //-------------------------------------------------------------------------- 2540 // ZeroType 2010 2541 template< typename pass_type > 2011 2542 void PassVisitor< pass_type >::visit( ZeroType * node ) { 2012 VISIT_BODY( node ); 2013 } 2014 2543 VISIT_START( node ); 2544 2545 maybeAccept_impl( node->forall, *this ); 2546 2547 VISIT_END( node ); 2548 } 2549 2550 template< typename pass_type > 2551 Type * PassVisitor< pass_type >::mutate( ZeroType * node ) { 2552 MUTATE_START( node ); 2553 2554 maybeMutate_impl( node->forall, *this ); 2555 2556 MUTATE_END( Type, node ); 2557 } 2558 2559 //-------------------------------------------------------------------------- 2560 // OneType 2015 2561 template< typename pass_type > 2016 2562 void PassVisitor< pass_type >::visit( OneType * node ) { 2017 VISIT_BODY( node ); 2018 } 2019 2563 VISIT_START( node ); 2564 2565 maybeAccept_impl( node->forall, *this ); 2566 2567 VISIT_END( node ); 2568 } 2569 2570 template< typename pass_type > 2571 Type * PassVisitor< pass_type >::mutate( OneType * node ) { 2572 MUTATE_START( node ); 2573 2574 maybeMutate_impl( node->forall, *this ); 2575 2576 MUTATE_END( Type, node ); 2577 } 2578 2579 //-------------------------------------------------------------------------- 2580 // GlobalScopeType 2581 template< typename pass_type > 2582 void PassVisitor< pass_type >::visit( GlobalScopeType * node ) { 2583 VISIT_START( node ); 2584 2585 maybeAccept_impl( node->forall, *this ); 2586 2587 VISIT_END( node ); 2588 } 2589 2590 template< typename pass_type > 2591 Type * PassVisitor< pass_type >::mutate( GlobalScopeType * node ) { 2592 MUTATE_START( node ); 2593 2594 maybeMutate_impl( node->forall, *this ); 2595 2596 MUTATE_END( Type, node ); 2597 } 2598 2599 //-------------------------------------------------------------------------- 2600 // Designation 2020 2601 template< typename pass_type > 2021 2602 void PassVisitor< pass_type >::visit( Designation * node ) { 2022 2603 VISIT_START( node ); 2023 2604 2024 maybeAccept_impl( node-> get_designators(), *this );2605 maybeAccept_impl( node->designators, *this ); 2025 2606 2026 2607 VISIT_END( node ); … … 2031 2612 MUTATE_START( node ); 2032 2613 2033 maybeMutate_impl( node-> get_designators(), *this );2614 maybeMutate_impl( node->designators, *this ); 2034 2615 2035 2616 MUTATE_END( Designation, node ); … … 2042 2623 VISIT_START( node ); 2043 2624 2044 visitExpression( node-> get_value());2625 visitExpression( node->value ); 2045 2626 2046 2627 VISIT_END( node ); … … 2051 2632 MUTATE_START( node ); 2052 2633 2053 node-> set_value( mutateExpression( node->get_value() ));2634 node->value = mutateExpression( node->value ); 2054 2635 2055 2636 MUTATE_END( Initializer, node ); 2056 2637 } 2057 2638 2639 //-------------------------------------------------------------------------- 2640 // ListInit 2058 2641 template< typename pass_type > 2059 2642 void PassVisitor< pass_type >::visit( ListInit * node ) { 2060 VISIT_BODY( node ); 2061 } 2062 2643 VISIT_START( node ); 2644 2645 maybeAccept_impl( node->designations, *this ); 2646 maybeAccept_impl( node->initializers, *this ); 2647 2648 VISIT_END( node ); 2649 } 2650 2651 template< typename pass_type > 2652 Initializer * PassVisitor< pass_type >::mutate( ListInit * node ) { 2653 MUTATE_START( node ); 2654 2655 maybeMutate_impl( node->designations, *this ); 2656 maybeMutate_impl( node->initializers, *this ); 2657 2658 MUTATE_END( Initializer, node ); 2659 } 2660 2661 //-------------------------------------------------------------------------- 2662 // ConstructorInit 2063 2663 template< typename pass_type > 2064 2664 void PassVisitor< pass_type >::visit( ConstructorInit * node ) { 2065 VISIT_BODY( node ); 2066 } 2067 2665 VISIT_START( node ); 2666 2667 maybeAccept_impl( node->ctor, *this ); 2668 maybeAccept_impl( node->dtor, *this ); 2669 maybeAccept_impl( node->init, *this ); 2670 2671 VISIT_END( node ); 2672 } 2673 2674 template< typename pass_type > 2675 Initializer * PassVisitor< pass_type >::mutate( ConstructorInit * node ) { 2676 MUTATE_START( node ); 2677 2678 maybeMutate_impl( node->ctor, *this ); 2679 maybeMutate_impl( node->dtor, *this ); 2680 maybeMutate_impl( node->init, *this ); 2681 2682 MUTATE_END( Initializer, node ); 2683 } 2684 2685 //-------------------------------------------------------------------------- 2686 // Subrange 2068 2687 template< typename pass_type > 2069 2688 void PassVisitor< pass_type >::visit( Subrange * node ) { 2070 VISIT_BODY( node ); 2071 } 2072 2689 VISIT_START( node ); 2690 2691 VISIT_END( node ); 2692 } 2693 2694 template< typename pass_type > 2695 Subrange * PassVisitor< pass_type >::mutate( Subrange * node ) { 2696 MUTATE_START( node ); 2697 2698 MUTATE_END( Subrange, node ); 2699 } 2700 2701 //-------------------------------------------------------------------------- 2702 // Attribute 2073 2703 template< typename pass_type > 2074 2704 void PassVisitor< pass_type >::visit( Constant * node ) { 2075 VISIT_BODY( node ); 2076 } 2077 2705 VISIT_START( node ); 2706 2707 VISIT_END( node ); 2708 } 2709 2710 template< typename pass_type > 2711 Constant * PassVisitor< pass_type >::mutate( Constant * node ) { 2712 MUTATE_START( node ); 2713 2714 MUTATE_END( Constant, node ); 2715 } 2716 2717 //-------------------------------------------------------------------------- 2718 // Attribute 2078 2719 template< typename pass_type > 2079 2720 void PassVisitor< pass_type >::visit( Attribute * node ) { 2080 VISIT_BODY( node ); 2081 } 2082 2083 //--------------------------------------------------------------------------------------------------------------- 2084 template< typename pass_type > 2085 Type * PassVisitor< pass_type >::mutate( VoidType * node ) { 2086 MUTATE_BODY( Type, node ); 2087 } 2088 2089 template< typename pass_type > 2090 Type * PassVisitor< pass_type >::mutate( BasicType * node ) { 2091 MUTATE_BODY( Type, node ); 2092 } 2093 2094 template< typename pass_type > 2095 Type * PassVisitor< pass_type >::mutate( PointerType * node ) { 2096 MUTATE_BODY( Type, node ); 2097 } 2098 2099 template< typename pass_type > 2100 Type * PassVisitor< pass_type >::mutate( ArrayType * node ) { 2101 MUTATE_BODY( Type, node ); 2102 } 2103 2104 template< typename pass_type > 2105 Type * PassVisitor< pass_type >::mutate( ReferenceType * node ) { 2106 MUTATE_BODY( Type, node ); 2107 } 2108 2109 template< typename pass_type > 2110 Type * PassVisitor< pass_type >::mutate( FunctionType * node ) { 2111 MUTATE_BODY( Type, node ); 2112 } 2113 2114 template< typename pass_type > 2115 Type * PassVisitor< pass_type >::mutate( TypeInstType * node ) { 2116 MUTATE_BODY( Type, node ); 2117 } 2118 2119 template< typename pass_type > 2120 Type * PassVisitor< pass_type >::mutate( TupleType * node ) { 2121 MUTATE_BODY( Type, node ); 2122 } 2123 2124 template< typename pass_type > 2125 Type * PassVisitor< pass_type >::mutate( TypeofType * node ) { 2126 MUTATE_BODY( Type, node ); 2127 } 2128 2129 template< typename pass_type > 2130 Type * PassVisitor< pass_type >::mutate( AttrType * node ) { 2131 MUTATE_BODY( Type, node ); 2132 } 2133 2134 template< typename pass_type > 2135 Type * PassVisitor< pass_type >::mutate( VarArgsType * node ) { 2136 MUTATE_BODY( Type, node ); 2137 } 2138 2139 template< typename pass_type > 2140 Type * PassVisitor< pass_type >::mutate( ZeroType * node ) { 2141 MUTATE_BODY( Type, node ); 2142 } 2143 2144 template< typename pass_type > 2145 Type * PassVisitor< pass_type >::mutate( OneType * node ) { 2146 MUTATE_BODY( Type, node ); 2147 } 2148 2149 template< typename pass_type > 2150 Initializer * PassVisitor< pass_type >::mutate( ListInit * node ) { 2151 MUTATE_BODY( Initializer, node ); 2152 } 2153 2154 template< typename pass_type > 2155 Initializer * PassVisitor< pass_type >::mutate( ConstructorInit * node ) { 2156 MUTATE_BODY( Initializer, node ); 2157 } 2158 2159 template< typename pass_type > 2160 Subrange * PassVisitor< pass_type >::mutate( Subrange * node ) { 2161 MUTATE_BODY( Subrange, node ); 2162 } 2163 2164 template< typename pass_type > 2165 Constant * PassVisitor< pass_type >::mutate( Constant * node ) { 2166 MUTATE_BODY( Constant, node ); 2721 VISIT_START( node ); 2722 2723 maybeAccept_impl( node->parameters, *this ); 2724 2725 VISIT_END( node ); 2167 2726 } 2168 2727 2169 2728 template< typename pass_type > 2170 2729 Attribute * PassVisitor< pass_type >::mutate( Attribute * node ) { 2171 MUTATE_BODY( Attribute, node ); 2172 } 2173 2730 MUTATE_START( node ); 2731 2732 maybeMutate_impl( node->parameters, *this ); 2733 2734 MUTATE_END( Attribute, node ); 2735 } 2736 2737 //-------------------------------------------------------------------------- 2738 // TypeSubstitution 2174 2739 template< typename pass_type > 2175 2740 TypeSubstitution * PassVisitor< pass_type >::mutate( TypeSubstitution * node ) { -
src/Common/PassVisitor.proto.h
rf9feab8 r90152a4 47 47 48 48 operator bool() { return m_ref ? *m_ref : true; } 49 bool operator=( bool val ) { return *m_ref = val; }49 bool operator=( bool val ) { assert(m_ref); return *m_ref = val; } 50 50 51 51 private: … … 53 53 friend class ChildrenGuard; 54 54 55 bool * set( bool &val ) {55 bool * set( bool * val ) { 56 56 bool * prev = m_ref; 57 m_ref = &val;57 m_ref = val; 58 58 return prev; 59 59 } … … 67 67 ChildrenGuard( bool_ref * ref ) 68 68 : m_val ( true ) 69 , m_prev( ref ? ref->set( m_val ) : nullptr )69 , m_prev( ref ? ref->set( &m_val ) : nullptr ) 70 70 , m_ref ( ref ) 71 71 {} … … 73 73 ~ChildrenGuard() { 74 74 if( m_ref ) { 75 m_ref->set( *m_prev );75 m_ref->set( m_prev ); 76 76 } 77 77 } … … 193 193 194 194 195 #define INDEXER_FUNC ( func, type ) \195 #define INDEXER_FUNC1( func, type ) \ 196 196 template<typename pass_type> \ 197 197 static inline auto indexer_impl_##func ( pass_type & pass, int, type arg ) -> decltype( pass.indexer.func( arg ), void() ) { \ … … 202 202 static inline void indexer_impl_##func ( pass_type &, long, type ) { } \ 203 203 204 INDEXER_FUNC( addId , DeclarationWithType * ); 205 INDEXER_FUNC( addType , NamedTypeDecl * ); 206 INDEXER_FUNC( addStruct , StructDecl * ); 207 INDEXER_FUNC( addEnum , EnumDecl * ); 208 INDEXER_FUNC( addUnion , UnionDecl * ); 209 INDEXER_FUNC( addTrait , TraitDecl * ); 210 INDEXER_FUNC( addWith , WithStmt * ); 204 #define INDEXER_FUNC2( func, type1, type2 ) \ 205 template<typename pass_type> \ 206 static inline auto indexer_impl_##func ( pass_type & pass, int, type1 arg1, type2 arg2 ) -> decltype( pass.indexer.func( arg1, arg2 ), void() ) { \ 207 pass.indexer.func( arg1, arg2 ); \ 208 } \ 209 \ 210 template<typename pass_type> \ 211 static inline void indexer_impl_##func ( pass_type &, long, type1, type2 ) { } 212 213 214 INDEXER_FUNC1( addId , DeclarationWithType * ); 215 INDEXER_FUNC1( addType , NamedTypeDecl * ); 216 INDEXER_FUNC1( addStruct , StructDecl * ); 217 INDEXER_FUNC1( addEnum , EnumDecl * ); 218 INDEXER_FUNC1( addUnion , UnionDecl * ); 219 INDEXER_FUNC1( addTrait , TraitDecl * ); 220 INDEXER_FUNC2( addWith , std::list< Expression * > &, BaseSyntaxNode * ); 211 221 212 222 -
src/Common/ScopedMap.h
rf9feab8 r90152a4 10 10 // Created On : Wed Dec 2 11:37:00 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Fri Jul 21 22:18:24 201713 // Update Count : 212 // Last Modified On : Mon May 21 15:22:40 2018 13 // Update Count : 3 14 14 // 15 15 … … 22 22 #include <vector> 23 23 24 /// Default (empty) ScopedMap note type 25 struct EmptyNote {}; 26 24 27 /// A map where the items are placed into nested scopes; 25 /// inserted items are placed into the innermost scope, lookup looks from the innermost scope outward 26 template<typename Key, typename Value> 28 /// inserted items are placed into the innermost scope, lookup looks from the innermost scope outward. 29 /// Scopes may be annotated with a value; the annotation defaults to empty 30 template<typename Key, typename Value, typename Note = EmptyNote> 27 31 class ScopedMap { 28 typedef std::map< Key, Value > Scope; 32 typedef std::map< Key, Value > MapType; 33 struct Scope { 34 MapType map; 35 Note note; 36 37 template<typename N> 38 Scope(N&& n) : map(), note(std::forward<N>(n)) {} 39 40 Scope() = default; 41 Scope(const Scope&) = default; 42 Scope(Scope&&) = default; 43 Scope& operator= (const Scope&) = default; 44 Scope& operator= (Scope&&) = default; 45 }; 29 46 typedef std::vector< Scope > ScopeList; 30 47 31 48 ScopeList scopes; ///< scoped list of maps 32 49 public: 33 typedef typename Scope::key_type key_type;34 typedef typename Scope::mapped_type mapped_type;35 typedef typename Scope::value_type value_type;50 typedef typename MapType::key_type key_type; 51 typedef typename MapType::mapped_type mapped_type; 52 typedef typename MapType::value_type value_type; 36 53 typedef typename ScopeList::size_type size_type; 37 54 typedef typename ScopeList::difference_type difference_type; 38 typedef typename Scope::reference reference;39 typedef typename Scope::const_reference const_reference;40 typedef typename Scope::pointer pointer;41 typedef typename Scope::const_pointer const_pointer;55 typedef typename MapType::reference reference; 56 typedef typename MapType::const_reference const_reference; 57 typedef typename MapType::pointer pointer; 58 typedef typename MapType::const_pointer const_pointer; 42 59 43 60 class iterator : public std::iterator< std::bidirectional_iterator_tag, … … 45 62 friend class ScopedMap; 46 63 friend class const_iterator; 47 typedef typename std::map< Key, Value >::iterator wrapped_iterator;48 typedef typename std::vector< std::map< Key, Value > >scope_list;64 typedef typename ScopedMap::MapType::iterator wrapped_iterator; 65 typedef typename ScopedMap::ScopeList scope_list; 49 66 typedef typename scope_list::size_type size_type; 50 67 51 68 /// Checks if this iterator points to a valid item 52 69 bool is_valid() const { 53 return it != (*scopes)[level]. end();70 return it != (*scopes)[level].map.end(); 54 71 } 55 72 … … 79 96 80 97 iterator& operator++ () { 81 if ( it == (*scopes)[level]. end() ) {98 if ( it == (*scopes)[level].map.end() ) { 82 99 if ( level == 0 ) return *this; 83 100 --level; 84 it = (*scopes)[level]. begin();101 it = (*scopes)[level].map.begin(); 85 102 } else { 86 103 ++it; … … 92 109 iterator& operator-- () { 93 110 // may fail if this is the begin iterator; allowed by STL spec 94 if ( it == (*scopes)[level]. begin() ) {111 if ( it == (*scopes)[level].map.begin() ) { 95 112 ++level; 96 it = (*scopes)[level]. end();113 it = (*scopes)[level].map.end(); 97 114 } 98 115 --it; … … 107 124 108 125 size_type get_level() const { return level; } 126 127 Note& get_note() { return (*scopes)[level].note; } 128 const Note& get_note() const { return (*scopes)[level].note; } 109 129 110 130 private: … … 117 137 value_type > { 118 138 friend class ScopedMap; 119 typedef typename std::map< Key, Value >::iterator wrapped_iterator;120 typedef typename std::map< Key, Value >::const_iterator wrapped_const_iterator;121 typedef typename std::vector< std::map< Key, Value > >scope_list;139 typedef typename ScopedMap::MapType::iterator wrapped_iterator; 140 typedef typename ScopedMap::MapType::const_iterator wrapped_const_iterator; 141 typedef typename ScopedMap::ScopeList scope_list; 122 142 typedef typename scope_list::size_type size_type; 123 143 124 144 /// Checks if this iterator points to a valid item 125 145 bool is_valid() const { 126 return it != (*scopes)[level]. end();146 return it != (*scopes)[level].map.end(); 127 147 } 128 148 … … 157 177 158 178 const_iterator& operator++ () { 159 if ( it == (*scopes)[level]. end() ) {179 if ( it == (*scopes)[level].map.end() ) { 160 180 if ( level == 0 ) return *this; 161 181 --level; 162 it = (*scopes)[level]. begin();182 it = (*scopes)[level].map.begin(); 163 183 } else { 164 184 ++it; … … 170 190 const_iterator& operator-- () { 171 191 // may fail if this is the begin iterator; allowed by STL spec 172 if ( it == (*scopes)[level]. begin() ) {192 if ( it == (*scopes)[level].map.begin() ) { 173 193 ++level; 174 it = (*scopes)[level]. end();194 it = (*scopes)[level].map.end(); 175 195 } 176 196 --it; … … 185 205 186 206 size_type get_level() const { return level; } 207 208 const Note& get_note() const { return (*scopes)[level].note; } 187 209 188 210 private: … … 197 219 } 198 220 221 // Starts a new scope with the given note 222 template<typename N> 223 void beginScope( N&& n ) { 224 scopes.emplace_back( std::forward<N>(n) ); 225 } 226 199 227 /// Ends a scope; invalidates any iterators pointing to elements of that scope 200 228 void endScope() { … … 204 232 205 233 /// Default constructor initializes with one scope 206 ScopedMap() { beginScope(); } 207 208 iterator begin() { return iterator(scopes, scopes.back().begin(), currentScope()).next_valid(); } 209 const_iterator begin() const { return const_iterator(scopes, scopes.back().begin(), currentScope()).next_valid(); } 210 const_iterator cbegin() const { return const_iterator(scopes, scopes.back().begin(), currentScope()).next_valid(); } 211 iterator end() { return iterator(scopes, scopes[0].end(), 0); } 212 const_iterator end() const { return const_iterator(scopes, scopes[0].end(), 0); } 213 const_iterator cend() const { return const_iterator(scopes, scopes[0].end(), 0); } 234 ScopedMap() : scopes() { beginScope(); } 235 236 /// Constructs with a given note on the outermost scope 237 template<typename N> 238 ScopedMap( N&& n ) : scopes() { beginScope(std::forward<N>(n)); } 239 240 iterator begin() { return iterator(scopes, scopes.back().map.begin(), currentScope()).next_valid(); } 241 const_iterator begin() const { return const_iterator(scopes, scopes.back().map.begin(), currentScope()).next_valid(); } 242 const_iterator cbegin() const { return const_iterator(scopes, scopes.back().map.begin(), currentScope()).next_valid(); } 243 iterator end() { return iterator(scopes, scopes[0].map.end(), 0); } 244 const_iterator end() const { return const_iterator(scopes, scopes[0].map.end(), 0); } 245 const_iterator cend() const { return const_iterator(scopes, scopes[0].map.end(), 0); } 214 246 215 247 /// Gets the index of the current scope (counted from 1) 216 248 size_type currentScope() const { return scopes.size() - 1; } 249 250 /// Gets the note at the given scope 251 Note& getNote( size_type i ) { return scopes[i].note; } 252 const Note& getNote( size_type i ) const { return scopes[i].note; } 217 253 218 254 /// Finds the given key in the outermost scope it occurs; returns end() for none such 219 255 iterator find( const Key &key ) { 220 256 for ( size_type i = scopes.size() - 1; ; --i ) { 221 typename Scope::iterator val = scopes[i].find( key );222 if ( val != scopes[i]. end() ) return iterator( scopes, val, i );257 typename MapType::iterator val = scopes[i].map.find( key ); 258 if ( val != scopes[i].map.end() ) return iterator( scopes, val, i ); 223 259 if ( i == 0 ) break; 224 260 } … … 226 262 } 227 263 const_iterator find( const Key &key ) const { 228 return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->find( key ) );264 return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->find( key ) ); 229 265 } 230 266 231 267 /// Finds the given key in the provided scope; returns end() for none such 232 268 iterator findAt( size_type scope, const Key& key ) { 233 typename Scope::iterator val = scopes[scope].find( key );234 if ( val != scopes[scope]. end() ) return iterator( scopes, val, scope );269 typename MapType::iterator val = scopes[scope].map.find( key ); 270 if ( val != scopes[scope].map.end() ) return iterator( scopes, val, scope ); 235 271 return end(); 236 272 } 237 273 const_iterator findAt( size_type scope, const Key& key ) const { 238 return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findAt( scope, key ) );274 return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findAt( scope, key ) ); 239 275 } 240 276 … … 243 279 if ( it.level == 0 ) return end(); 244 280 for ( size_type i = it.level - 1; ; --i ) { 245 typename Scope::iterator val = scopes[i].find( key );246 if ( val != scopes[i]. end() ) return iterator( scopes, val, i );281 typename MapType::iterator val = scopes[i].map.find( key ); 282 if ( val != scopes[i].map.end() ) return iterator( scopes, val, i ); 247 283 if ( i == 0 ) break; 248 284 } … … 250 286 } 251 287 const_iterator findNext( const_iterator &it, const Key &key ) const { 252 return const_iterator( const_cast< ScopedMap< Key, Value >* >(this)->findNext( it, key ) );288 return const_iterator( const_cast< ScopedMap< Key, Value, Note >* >(this)->findNext( it, key ) ); 253 289 } 254 290 … … 256 292 template< typename value_type_t > 257 293 std::pair< iterator, bool > insert( value_type_t&& value ) { 258 std::pair< typename Scope::iterator, bool > res = scopes.back().insert( std::forward<value_type_t>( value ) );294 std::pair< typename MapType::iterator, bool > res = scopes.back().map.insert( std::forward<value_type_t>( value ) ); 259 295 return std::make_pair( iterator(scopes, std::move( res.first ), scopes.size()-1), std::move( res.second ) ); 260 296 } … … 262 298 template< typename value_type_t > 263 299 std::pair< iterator, bool > insert( iterator at, value_type_t&& value ) { 264 Scope& scope = (*at.scopes) [ at.level ];265 std::pair< typename Scope::iterator, bool > res = scope.insert( std::forward<value_type_t>( value ) );300 MapType& scope = (*at.scopes)[ at.level ].map; 301 std::pair< typename MapType::iterator, bool > res = scope.insert( std::forward<value_type_t>( value ) ); 266 302 return std::make_pair( iterator(scopes, std::move( res.first ), at.level), std::move( res.second ) ); 267 303 } … … 272 308 template< typename value_type_t > 273 309 std::pair< iterator, bool > insertAt( size_type scope, value_type_t&& value ) { 274 std::pair< typename Scope::iterator, bool > res = scopes.at(scope).insert( std::forward<value_type_t>( value ) );310 std::pair< typename MapType::iterator, bool > res = scopes.at(scope).map.insert( std::forward<value_type_t>( value ) ); 275 311 return std::make_pair( iterator(scopes, std::move( res.first ), scope), std::move( res.second ) ); 312 } 313 314 template< typename value_t > 315 std::pair< iterator, bool > insertAt( size_type scope, const Key& key, value_t&& value ) { 316 return insertAt( scope, std::make_pair( key, std::forward<value_t>( value ) ) ); 276 317 } 277 318 … … 283 324 284 325 iterator erase( iterator pos ) { 285 Scope& scope = (*pos.scopes) [ pos.level ];326 MapType& scope = (*pos.scopes)[ pos.level ].map; 286 327 const typename iterator::wrapped_iterator& new_it = scope.erase( pos.it ); 287 328 iterator it( *pos.scopes, new_it, pos.level ); -
src/Common/SemanticError.cc
rf9feab8 r90152a4 7 7 // SemanticError.cc -- 8 8 // 9 // Author : Richard C. Bilson9 // Author : Thierry Delisle 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T ue Aug 29 18:17:35 201713 // Update Count : 312 // Last Modified On : Thu Jun 7 08:05:26 2018 13 // Update Count : 10 14 14 // 15 15 16 #include <cstdarg> 16 17 #include <cstdio> // for fileno, stderr 18 #include <cstring> 17 19 #include <unistd.h> // for isatty 18 20 #include <iostream> // for basic_ostream, operator<<, ostream 19 21 #include <list> // for list, _List_iterator 20 22 #include <string> // for string, operator<<, operator+, to_string 23 #include <vector> 21 24 22 25 #include "Common/utility.h" // for to_string, CodeLocation (ptr only) 23 26 #include "SemanticError.h" 24 27 25 SemanticError::SemanticError() { 28 //----------------------------------------------------------------------------- 29 // Severity Handling 30 std::vector<Severity> & get_severities() { 31 static std::vector<Severity> severities; 32 if(severities.empty()) { 33 severities.reserve((size_t)Warning::NUMBER_OF_WARNINGS); 34 for ( const auto w : WarningFormats ) { 35 severities.push_back( w.default_severity ); 36 } // for 37 } 38 return severities; 26 39 } 27 40 28 SemanticError::SemanticError( std::string error ) { 29 append( error ); 41 void SemanticWarning_SuppressAll() { 42 for( auto & s : get_severities() ) { 43 s = Severity::Suppress; 44 } 30 45 } 31 46 32 void SemanticError::append( SemanticError &other ) { 47 void SemanticWarning_EnableAll() { 48 for( auto & s : get_severities() ) { 49 s = Severity::Warn; 50 } 51 } 52 53 void SemanticWarning_WarningAsError() { 54 for( auto & s : get_severities() ) { 55 if(s == Severity::Warn) s = Severity::Error; 56 } 57 } 58 59 void SemanticWarning_Set(const char * const name, Severity s) { 60 size_t idx = 0; 61 for ( const auto & w : WarningFormats ) { 62 if ( std::strcmp( name, w.name ) == 0 ) { 63 get_severities()[idx] = s; 64 break; 65 } 66 idx++; 67 } 68 } 69 70 //----------------------------------------------------------------------------- 71 // Semantic Error 72 bool SemanticErrorThrow = false; 73 74 SemanticErrorException::SemanticErrorException( CodeLocation location, std::string error ) { 75 append( location, error ); 76 } 77 78 void SemanticErrorException::append( SemanticErrorException &other ) { 33 79 errors.splice( errors.end(), other.errors ); 34 80 } 35 81 36 void SemanticError ::append(const std::string & msg ) {37 errors.emplace_back( error_str() +msg );82 void SemanticErrorException::append( CodeLocation location, const std::string & msg ) { 83 errors.emplace_back( location, msg ); 38 84 } 39 85 40 bool SemanticError ::isEmpty() const {86 bool SemanticErrorException::isEmpty() const { 41 87 return errors.empty(); 42 88 } 43 89 44 void SemanticError ::print( std::ostream &os) {90 void SemanticErrorException::print() { 45 91 using std::to_string; 46 92 for( auto err : errors ) { 47 os << err.location<< err.description << std::endl;93 std::cerr << ErrorHelpers::bold() << err.location << ErrorHelpers::error_str() << ErrorHelpers::reset_font() << err.description << std::endl; 48 94 } 49 95 } 50 96 51 void SemanticError::set_location( const CodeLocation& location ) { 52 errors.begin()->maybeSet( location ); 97 void SemanticError( CodeLocation location, std::string error ) { 98 SemanticErrorThrow = true; 99 throw SemanticErrorException( location, error ); 100 } 101 102 namespace { 103 // convert format string and arguments into a single string 104 std::string fmtToString(const char * fmt, va_list ap) { 105 int size = 128; 106 while ( true ) { 107 char buf[size]; 108 va_list args; 109 va_copy( args, ap ); 110 int n = vsnprintf(&buf[0], size, fmt, args); 111 va_end( args ); 112 if ( n < size && n >= 0 ) return buf; 113 size *= 2; 114 } 115 assert( false ); 116 } 117 } 118 119 void SemanticWarningImpl( CodeLocation location, Warning warning, const char * const fmt, ... ) { 120 Severity severity = get_severities()[(int)warning]; 121 switch(severity) { 122 case Severity::Suppress : 123 break; 124 case Severity::Warn : 125 { 126 va_list args; 127 va_start(args, fmt); 128 std::string msg = fmtToString( fmt, args ); 129 va_end(args); 130 std::cerr << ErrorHelpers::bold() << location << ErrorHelpers::warning_str() << ErrorHelpers::reset_font() << msg << std::endl; 131 } 132 break; 133 case Severity::Error : 134 { 135 va_list args; 136 va_start(args, fmt); 137 std::string msg = fmtToString( fmt, args ); 138 va_end(args); 139 SemanticError(location, msg); 140 } 141 break; 142 case Severity::Critical : 143 assertf(false, "Critical errors not implemented yet"); 144 break; 145 } 146 } 147 148 //----------------------------------------------------------------------------- 149 // Helpers 150 namespace ErrorHelpers { 151 const std::string & error_str() { 152 static std::string str = isatty( STDERR_FILENO ) ? "\e[31merror:\e[39m " : "error: "; 153 return str; 154 } 155 156 const std::string & warning_str() { 157 static std::string str = isatty( STDERR_FILENO ) ? "\e[95mwarning:\e[39m " : "warning: "; 158 return str; 159 } 160 161 const std::string & bold_ttycode() { 162 static std::string str = isatty( STDERR_FILENO ) ? "\e[1m" : ""; 163 return str; 164 } 165 166 const std::string & reset_font_ttycode() { 167 static std::string str = isatty( STDERR_FILENO ) ? "\e[0m" : ""; 168 return str; 169 } 170 171 std::string make_bold( const std::string & str ) { 172 return bold_ttycode() + str + reset_font_ttycode(); 173 } 174 175 std::ostream & operator<<(std::ostream & os, bold) { 176 os << bold_ttycode(); 177 return os; 178 } 179 180 std::ostream & operator<<(std::ostream & os, reset_font) { 181 os << reset_font_ttycode(); 182 return os; 183 } 53 184 } 54 185 -
src/Common/SemanticError.h
rf9feab8 r90152a4 7 7 // SemanticError.h -- 8 8 // 9 // Author : Richard C. Bilson9 // Author : Thierry Delisle 10 10 // Created On : Mon May 18 07:44:20 2015 11 11 // Last Modified By : Peter A. Buhr 12 // Last Modified On : T ue Aug 29 22:03:36 201713 // Update Count : 1712 // Last Modified On : Thu Jul 19 10:09:17 2018 13 // Update Count : 31 14 14 // 15 15 16 16 #pragma once 17 17 18 #include <exception> // for exception 19 #include <iostream> // for ostream 20 #include <list> // for list 21 #include <string> // for string 22 #include <unistd.h> // for isatty 18 #include "ErrorObjects.h" 19 #include <cstring> 23 20 24 #include "CodeLocation.h" // for CodeLocation, toString 21 //----------------------------------------------------------------------------- 22 // Errors 25 23 26 struct error { 27 std::string description; 28 CodeLocation location; 24 extern bool SemanticErrorThrow; 29 25 30 error() = default; 31 error( const std::string & str ) : description( str ) {} 26 __attribute__((noreturn)) void SemanticError( CodeLocation location, std::string error ); 32 27 33 void maybeSet( const CodeLocation & location ) { 34 if( this->location.isUnset() ) { 35 this->location = location; 36 } 37 } 28 template< typename T > 29 __attribute__((noreturn)) static inline void SemanticError( const T * obj, const std::string & error ) { 30 SemanticError( obj->location, toString( error, obj ) ); 31 } 32 33 template< typename T > 34 __attribute__((noreturn)) static inline void SemanticError( CodeLocation location, const T * obj, const std::string & error ) { 35 SemanticError( location, toString( error, obj ) ); 36 } 37 38 //----------------------------------------------------------------------------- 39 // Warnings 40 41 enum class Severity { 42 Suppress, 43 Warn, 44 Error, 45 Critical 38 46 }; 39 47 40 class SemanticError : public std::exception { 41 public: 42 SemanticError(); 43 SemanticError( std::string error ); 44 template< typename T > SemanticError( const std::string & error, const T * obj ); 45 ~SemanticError() throw() {} 46 47 static inline const std::string & error_str() { 48 static std::string str = isatty( STDERR_FILENO ) ? "\e[31merror:\e[39m " : "error: "; 49 return str; 50 } 51 52 void append( SemanticError & other ); 53 void append( const std::string & ); 54 bool isEmpty() const; 55 void print( std::ostream & os ); 56 57 void set_location( const CodeLocation & location ); 58 // constructs an exception using the given message and the printed representation of the obj (T must have a print 59 // method) 60 private: 61 std::list< error > errors; 48 struct WarningData { 49 const char * const name; 50 const char * const message; 51 const Severity default_severity; 62 52 }; 63 53 64 template< typename T > 65 SemanticError::SemanticError( const std::string & error, const T * obj ) { 66 append( toString( error, obj ) ); 54 constexpr WarningData WarningFormats[] = { 55 {"self-assign" , "self assignment of expression: %s" , Severity::Warn}, 56 {"reference-conversion" , "rvalue to reference conversion of rvalue: %s" , Severity::Warn}, 57 {"qualifiers-zero_t-one_t", "questionable use of type qualifier %s with %s", Severity::Warn}, 58 {"aggregate-forward-decl" , "forward declaration of nested aggregate: %s" , Severity::Warn}, 59 {"superfluous-decl" , "declaration does not allocate storage: %s" , Severity::Warn}, 60 {"gcc-attributes" , "invalid attribute: %s" , Severity::Warn}, 61 }; 62 63 enum class Warning { 64 SelfAssignment, 65 RvalueToReferenceConversion, 66 BadQualifiersZeroOne, 67 AggrForwardDecl, 68 SuperfluousDecl, 69 GccAttributes, 70 NUMBER_OF_WARNINGS, // This MUST be the last warning 71 }; 72 73 static_assert( 74 (sizeof(WarningFormats) / sizeof(WarningFormats[0])) == ((unsigned long)Warning::NUMBER_OF_WARNINGS), 75 "Each warning format should have a corresponding warning enum value" 76 ); 77 78 #define SemanticWarning(loc, id, ...) SemanticWarningImpl(loc, id, WarningFormats[(int)id].message, __VA_ARGS__) 79 80 void SemanticWarningImpl (CodeLocation loc, Warning warn, const char * const fmt, ...) __attribute__((format(printf, 3, 4))); 81 82 void SemanticWarning_SuppressAll (); 83 void SemanticWarning_EnableAll (); 84 void SemanticWarning_WarningAsError(); 85 void SemanticWarning_Set (const char * const name, Severity s); 86 87 // SKULLDUGGERY: cfa.cc is built before SemanticError.cc but needs this routine. 88 static inline bool SemanticWarning_Exist(const char * const name) { 89 for ( const auto & w : WarningFormats ) { 90 if ( std::strcmp( name, w.name ) == 0 ) return true; 91 } 92 return false; 93 } 94 95 //----------------------------------------------------------------------------- 96 // Helpers 97 namespace ErrorHelpers { 98 const std::string & error_str(); 99 const std::string & warning_str(); 100 const std::string & bold_ttycode(); 101 const std::string & reset_font_ttycode(); 102 103 std::string make_bold( const std::string & str ); 104 105 struct bold {}; 106 std::ostream & operator<<(std::ostream & os, bold); 107 108 struct reset_font {}; 109 std::ostream & operator<<(std::ostream & os, reset_font); 67 110 } 68 111 -
src/Common/module.mk
rf9feab8 r90152a4 6 6 ## file "LICENCE" distributed with Cforall. 7 7 ## 8 ## module.mk -- 8 ## module.mk -- 9 9 ## 10 10 ## Author : Richard C. Bilson … … 18 18 Common/UniqueName.cc \ 19 19 Common/DebugMalloc.cc \ 20 Common/Assert.cc 20 Common/Assert.cc \ 21 Common/Heap.cc \ 22 Common/Eval.cc -
src/Common/utility.h
rf9feab8 r90152a4 9 9 // Author : Richard C. Bilson 10 10 // Created On : Mon May 18 07:44:20 2015 11 // Last Modified By : Andrew Beach12 // Last Modified On : Thr Aug 17 11:38:00 201713 // Update Count : 3411 // Last Modified By : Peter A. Buhr 12 // Last Modified On : Sun May 6 22:24:16 2018 13 // Update Count : 40 14 14 // 15 15 … … 31 31 #include "Common/Indenter.h" 32 32 33 class Expression; 34 33 35 template< typename T > 34 36 static inline T * maybeClone( const T *orig ) { … … 98 100 } 99 101 102 template< typename SrcContainer, typename DestContainer, typename Predicate > 103 void cloneAll_if( const SrcContainer &src, DestContainer &dest, Predicate pred ) { 104 std::back_insert_iterator< DestContainer > out( dest ); 105 for ( auto x : src ) { 106 if ( pred(x) ) { 107 *out++ = x->clone(); 108 } 109 } // while 110 } 111 100 112 template< typename Container > 101 113 void assertAll( const Container &container ) { … … 151 163 return os.str(); 152 164 } 165 166 #define toCString( ... ) toString( __VA_ARGS__ ).c_str() 153 167 154 168 // replace element of list with all elements of another list … … 424 438 } 425 439 426 440 // ----------------------------------------------------------------------------- 441 // O(1) polymorphic integer ilog2, using clz, which returns the number of leading 0-bits, starting at the most 442 // significant bit (single instruction on x86) 443 444 template<typename T> 445 inline 446 #if defined(__GNUC__) && __GNUC__ > 4 447 constexpr 448 #endif 449 T ilog2(const T & t) { 450 if(std::is_integral<T>::value) { 451 const constexpr int r = sizeof(t) * __CHAR_BIT__ - 1; 452 if( sizeof(T) == sizeof(unsigned int) ) return r - __builtin_clz ( t ); 453 if( sizeof(T) == sizeof(unsigned long) ) return r - __builtin_clzl ( t ); 454 if( sizeof(T) == sizeof(unsigned long long) ) return r - __builtin_clzll( t ); 455 } 456 assert(false); 457 return -1; 458 } // ilog2 459 460 // ----------------------------------------------------------------------------- 461 /// evaluates expr as a long long int. If second is false, expr could not be evaluated 462 std::pair<long long int, bool> eval(Expression * expr); 427 463 428 464 // Local Variables: //
Note:
See TracChangeset
for help on using the changeset viewer.