Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/Common/PassVisitor.impl.h

    r9dcb653 r33a25f9  
    6666        DeclList_t* beforeDecls = visitor.get_beforeDecls();
    6767        DeclList_t* afterDecls  = visitor.get_afterDecls();
     68        SemanticError errors;
    6869
    6970        for ( std::list< Declaration* >::iterator i = decls.begin(); ; ++i ) {
     
    7374                if ( i == decls.end() ) break;
    7475
    75                 // run mutator on declaration
    76                 maybeAccept( *i, visitor );
     76                try {
     77                        // run visitor on declaration
     78                        maybeAccept( *i, visitor );
     79                } catch( SemanticError &e ) {
     80                        e.set_location( (*i)->location );
     81                        errors.append( e );
     82                }
    7783
    7884                // splice in new declarations before current decl
    7985                if ( !empty( beforeDecls ) ) { decls.splice( i, *beforeDecls ); }
    8086        }
     87        if ( ! errors.isEmpty() ) {
     88                throw errors;
     89        }
    8190}
    8291
     
    8695        DeclList_t* beforeDecls = mutator.get_beforeDecls();
    8796        DeclList_t* afterDecls  = mutator.get_afterDecls();
     97        SemanticError errors;
    8898
    8999        for ( std::list< Declaration* >::iterator i = decls.begin(); ; ++i ) {
     
    92102
    93103                if ( i == decls.end() ) break;
    94 
    95                 // run mutator on declaration
    96                 *i = maybeMutate( *i, mutator );
     104                try {
     105                        // run mutator on declaration
     106                        *i = maybeMutate( *i, mutator );
     107                } catch( SemanticError &e ) {
     108                        e.set_location( (*i)->location );
     109                        errors.append( e );
     110                }
    97111
    98112                // splice in new declarations before current decl
    99113                if ( !empty( beforeDecls ) ) { decls.splice( i, *beforeDecls ); }
     114        }
     115        if ( ! errors.isEmpty() ) {
     116                throw errors;
    100117        }
    101118}
     
    166183
    167184                } catch ( SemanticError &e ) {
     185                        e.set_location( (*i)->location );
    168186                        errors.append( e );
    169187                }
     
    275293//------------------------------------------------------------------------------------------------------------------------------------------------------------------------
    276294
     295// A NOTE ON THE ORDER OF TRAVERSAL
     296//
     297// Types and typedefs have their base types visited before they are added to the type table.  This is ok, since there is
     298// no such thing as a recursive type or typedef.
     299//
     300//             typedef struct { T *x; } T; // never allowed
     301//
     302// for structs/unions, it is possible to have recursion, so the decl should be added as if it's incomplete to begin, the
     303// members are traversed, and then the complete type should be added (assuming the type is completed by this particular
     304// declaration).
     305//
     306//             struct T { struct T *x; }; // allowed
     307//
     308// It is important to add the complete type to the symbol table *after* the members/base has been traversed, since that
     309// traversal may modify the definition of the type and these modifications should be visible when the symbol table is
     310// queried later in this pass.
     311//
     312// TODO: figure out whether recursive contexts are sensible/possible/reasonable.
    277313
    278314//--------------------------------------------------------------------------
     
    432468        indexerAddEnum( node );
    433469
    434         // unlike structs, contexts, and unions, enums inject their members into the global scope
     470        // unlike structs, traits, and unions, enums inject their members into the global scope
    435471        maybeAccept( node->parameters, *this );
    436472        maybeAccept( node->members   , *this );
     
    445481        indexerAddEnum( node );
    446482
    447         // unlike structs, contexts, and unions, enums inject their members into the global scope
     483        // unlike structs, traits, and unions, enums inject their members into the global scope
    448484        maybeMutateRef( node->parameters, *this );
    449485        maybeMutateRef( node->members   , *this );
     
    496532        }
    497533
     534        // see A NOTE ON THE ORDER OF TRAVERSAL, above
     535        // note that assertions come after the type is added to the symtab, since they are not part of the type proper
     536        // and may depend on the type itself
    498537        indexerAddType( node );
    499538
     
    506545
    507546template< typename pass_type >
    508 TypeDecl * PassVisitor< pass_type >::mutate( TypeDecl * node ) {
     547Declaration * PassVisitor< pass_type >::mutate( TypeDecl * node ) {
    509548        MUTATE_START( node );
    510549
     
    515554        }
    516555
     556        // see A NOTE ON THE ORDER OF TRAVERSAL, above
     557        // note that assertions come after the type is added to the symtab, since they are not part of the type proper
     558        // and may depend on the type itself
    517559        indexerAddType( node );
    518560
     
    521563        indexerScopedMutate( node->init, *this );
    522564
    523         MUTATE_END( TypeDecl, node );
     565        MUTATE_END( Declaration, node );
    524566}
    525567
     
    641683void PassVisitor< pass_type >::visit( IfStmt * node ) {
    642684        VISIT_START( node );
    643 
    644         visitExpression( node->condition );
    645         node->thenPart = visitStatement( node->thenPart );
    646         node->elsePart = visitStatement( node->elsePart );
    647 
     685        {
     686                // if statements introduce a level of scope (for the initialization)
     687                auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } );
     688                acceptAll( node->get_initialization(), *this );
     689                visitExpression( node->condition );
     690                node->thenPart = visitStatement( node->thenPart );
     691                node->elsePart = visitStatement( node->elsePart );
     692        }
    648693        VISIT_END( node );
    649694}
     
    653698        MUTATE_START( node );
    654699        {
    655                 auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } );
     700                // if statements introduce a level of scope (for the initialization)
     701                auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } );
     702                maybeMutateRef( node->get_initialization(), *this );
    656703                node->condition = mutateExpression( node->condition );
    657704                node->thenPart  = mutateStatement ( node->thenPart  );
     
    689736        VISIT_START( node );
    690737        {
     738                // for statements introduce a level of scope (for the initialization)
    691739                auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } );
    692740                maybeAccept( node->initialization, *this );
     
    702750        MUTATE_START( node );
    703751        {
     752                // for statements introduce a level of scope (for the initialization)
    704753                auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } );
    705754                maybeMutateRef( node->initialization, *this );
     
    830879        VISIT_START( node );
    831880        {
     881                // catch statements introduce a level of scope (for the caught exception)
    832882                auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } );
    833883                maybeAccept( node->decl, *this );
     
    842892        MUTATE_START( node );
    843893        {
     894                // catch statements introduce a level of scope (for the caught exception)
    844895                auto guard = makeFuncGuard( [this]() { indexerScopeEnter(); }, [this]() { indexerScopeLeave(); } );
    845896                maybeMutateRef( node->decl, *this );
Note: See TracChangeset for help on using the changeset viewer.