Ignore:
Timestamp:
Sep 19, 2017, 2:14:39 PM (7 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
8f98b78
Parents:
e149f77 (diff), e06be49 (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.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/ResolvExpr/Resolver.cc

    re149f77 r695e00d  
    2121#include "Alternative.h"                 // for Alternative, AltList
    2222#include "AlternativeFinder.h"           // for AlternativeFinder, resolveIn...
     23#include "Common/PassVisitor.h"          // for PassVisitor
    2324#include "Common/SemanticError.h"        // for SemanticError
    2425#include "Common/utility.h"              // for ValueGuard, group_iterate
     
    4445
    4546namespace ResolvExpr {
    46         class Resolver final : public SymTab::Indexer {
    47           public:
    48                 Resolver() : SymTab::Indexer( false ) {}
    49                 Resolver( const SymTab:: Indexer & other ) : SymTab::Indexer( other ) {
    50                         if ( const Resolver * res = dynamic_cast< const Resolver * >( &other ) ) {
    51                                 functionReturn = res->functionReturn;
    52                                 currentObject = res->currentObject;
    53                                 inEnumDecl = res->inEnumDecl;
    54                         }
    55                 }
    56 
    57                 typedef SymTab::Indexer Parent;
    58                 using Parent::visit;
    59                 virtual void visit( FunctionDecl *functionDecl ) override;
    60                 virtual void visit( ObjectDecl *functionDecl ) override;
    61                 virtual void visit( TypeDecl *typeDecl ) override;
    62                 virtual void visit( EnumDecl * enumDecl ) override;
    63 
    64                 virtual void visit( ArrayType * at ) override;
    65                 virtual void visit( PointerType * at ) override;
    66 
    67                 virtual void visit( ExprStmt *exprStmt ) override;
    68                 virtual void visit( AsmExpr *asmExpr ) override;
    69                 virtual void visit( AsmStmt *asmStmt ) override;
    70                 virtual void visit( IfStmt *ifStmt ) override;
    71                 virtual void visit( WhileStmt *whileStmt ) override;
    72                 virtual void visit( ForStmt *forStmt ) override;
    73                 virtual void visit( SwitchStmt *switchStmt ) override;
    74                 virtual void visit( CaseStmt *caseStmt ) override;
    75                 virtual void visit( BranchStmt *branchStmt ) override;
    76                 virtual void visit( ReturnStmt *returnStmt ) override;
    77                 virtual void visit( ThrowStmt *throwStmt ) override;
    78                 virtual void visit( CatchStmt *catchStmt ) override;
    79                 virtual void visit( WaitForStmt *waitforStmt ) override;
    80 
    81                 virtual void visit( SingleInit *singleInit ) override;
    82                 virtual void visit( ListInit *listInit ) override;
    83                 virtual void visit( ConstructorInit *ctorInit ) override;
     47        struct Resolver final : public WithIndexer, public WithGuards, public WithVisitorRef<Resolver>, public WithShortCircuiting {
     48                Resolver() {}
     49                Resolver( const SymTab::Indexer & other ) {
     50                        indexer = other;
     51                }
     52
     53                void previsit( FunctionDecl *functionDecl );
     54                void postvisit( FunctionDecl *functionDecl );
     55                void previsit( ObjectDecl *functionDecl );
     56                void previsit( TypeDecl *typeDecl );
     57                void previsit( EnumDecl * enumDecl );
     58
     59                void previsit( ArrayType * at );
     60                void previsit( PointerType * at );
     61
     62                void previsit( ExprStmt *exprStmt );
     63                void previsit( AsmExpr *asmExpr );
     64                void previsit( AsmStmt *asmStmt );
     65                void previsit( IfStmt *ifStmt );
     66                void previsit( WhileStmt *whileStmt );
     67                void previsit( ForStmt *forStmt );
     68                void previsit( SwitchStmt *switchStmt );
     69                void previsit( CaseStmt *caseStmt );
     70                void previsit( BranchStmt *branchStmt );
     71                void previsit( ReturnStmt *returnStmt );
     72                void previsit( ThrowStmt *throwStmt );
     73                void previsit( CatchStmt *catchStmt );
     74                void previsit( WaitForStmt * stmt );
     75
     76                void previsit( SingleInit *singleInit );
     77                void previsit( ListInit *listInit );
     78                void previsit( ConstructorInit *ctorInit );
    8479          private:
    8580        typedef std::list< Initializer * >::iterator InitIterator;
     
    9893
    9994        void resolve( std::list< Declaration * > translationUnit ) {
    100                 Resolver resolver;
     95                PassVisitor<Resolver> resolver;
    10196                acceptAll( translationUnit, resolver );
    10297        }
    10398
     99        // used in resolveTypeof
    104100        Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) {
    105101                TypeEnvironment env;
    106102                return resolveInVoidContext( expr, indexer, env );
    107103        }
    108 
    109104
    110105        namespace {
     
    192187        }
    193188
    194         void Resolver::visit( ObjectDecl *objectDecl ) {
    195                 Type *new_type = resolveTypeof( objectDecl->get_type(), *this );
     189        void Resolver::previsit( ObjectDecl *objectDecl ) {
     190                Type *new_type = resolveTypeof( objectDecl->get_type(), indexer );
    196191                objectDecl->set_type( new_type );
    197192                // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable
     
    200195                // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting
    201196                // the RHS.
    202                 ValueGuard<CurrentObject> temp( currentObject );
     197                GuardValue( currentObject );
    203198                currentObject = CurrentObject( objectDecl->get_type() );
    204199                if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) {
     
    207202                        currentObject = CurrentObject( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) );
    208203                }
    209                 Parent::visit( objectDecl );
    210                 if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) {
    211                         // delete newly created signed int type
    212                         // delete currentObject.getType();
    213                 }
    214204        }
    215205
     
    218208                if ( type->get_dimension() ) {
    219209                        CastExpr *castExpr = new CastExpr( type->get_dimension(), SymTab::SizeType->clone() );
    220                         Expression *newExpr = findSingleExpression( castExpr, *this );
     210                        Expression *newExpr = findSingleExpression( castExpr, indexer );
    221211                        delete type->get_dimension();
    222212                        type->set_dimension( newExpr );
     
    224214        }
    225215
    226         void Resolver::visit( ArrayType * at ) {
     216        void Resolver::previsit( ArrayType * at ) {
    227217                handlePtrType( at );
    228                 Parent::visit( at );
    229         }
    230 
    231         void Resolver::visit( PointerType * pt ) {
     218        }
     219
     220        void Resolver::previsit( PointerType * pt ) {
    232221                handlePtrType( pt );
    233                 Parent::visit( pt );
    234         }
    235 
    236         void Resolver::visit( TypeDecl *typeDecl ) {
     222        }
     223
     224        void Resolver::previsit( TypeDecl *typeDecl ) {
    237225                if ( typeDecl->get_base() ) {
    238                         Type *new_type = resolveTypeof( typeDecl->get_base(), *this );
     226                        Type *new_type = resolveTypeof( typeDecl->get_base(), indexer );
    239227                        typeDecl->set_base( new_type );
    240228                } // if
    241                 Parent::visit( typeDecl );
    242         }
    243 
    244         void Resolver::visit( FunctionDecl *functionDecl ) {
     229        }
     230
     231        void Resolver::previsit( FunctionDecl *functionDecl ) {
    245232#if 0
    246                 std::cout << "resolver visiting functiondecl ";
    247                 functionDecl->print( std::cout );
    248                 std::cout << std::endl;
     233                std::cerr << "resolver visiting functiondecl ";
     234                functionDecl->print( std::cerr );
     235                std::cerr << std::endl;
    249236#endif
    250                 Type *new_type = resolveTypeof( functionDecl->get_type(), *this );
     237                Type *new_type = resolveTypeof( functionDecl->get_type(), indexer );
    251238                functionDecl->set_type( new_type );
    252                 ValueGuard< Type * > oldFunctionReturn( functionReturn );
     239                GuardValue( functionReturn );
    253240                functionReturn = ResolvExpr::extractResultType( functionDecl->get_functionType() );
    254                 Parent::visit( functionDecl );
    255 
     241        }
     242
     243
     244        void Resolver::postvisit( FunctionDecl *functionDecl ) {
    256245                // default value expressions have an environment which shouldn't be there and trips up later passes.
    257246                // xxx - it might be necessary to somehow keep the information from this environment, but I can't currently
     
    267256        }
    268257
    269         void Resolver::visit( EnumDecl * enumDecl ) {
     258        void Resolver::previsit( EnumDecl * ) {
    270259                // in case we decide to allow nested enums
    271                 ValueGuard< bool > oldInEnumDecl( inEnumDecl );
     260                GuardValue( inEnumDecl );
    272261                inEnumDecl = true;
    273                 Parent::visit( enumDecl );
    274         }
    275 
    276         void Resolver::visit( ExprStmt *exprStmt ) {
     262        }
     263
     264        void Resolver::previsit( ExprStmt *exprStmt ) {
     265                visit_children = false;
    277266                assertf( exprStmt->get_expr(), "ExprStmt has null Expression in resolver" );
    278                 Expression *newExpr = findVoidExpression( exprStmt->get_expr(), *this );
     267                Expression *newExpr = findVoidExpression( exprStmt->get_expr(), indexer );
    279268                delete exprStmt->get_expr();
    280269                exprStmt->set_expr( newExpr );
    281270        }
    282271
    283         void Resolver::visit( AsmExpr *asmExpr ) {
    284                 Expression *newExpr = findVoidExpression( asmExpr->get_operand(), *this );
     272        void Resolver::previsit( AsmExpr *asmExpr ) {
     273                visit_children = false;
     274                Expression *newExpr = findVoidExpression( asmExpr->get_operand(), indexer );
    285275                delete asmExpr->get_operand();
    286276                asmExpr->set_operand( newExpr );
    287277                if ( asmExpr->get_inout() ) {
    288                         newExpr = findVoidExpression( asmExpr->get_inout(), *this );
     278                        newExpr = findVoidExpression( asmExpr->get_inout(), indexer );
    289279                        delete asmExpr->get_inout();
    290280                        asmExpr->set_inout( newExpr );
     
    292282        }
    293283
    294         void Resolver::visit( AsmStmt *asmStmt ) {
    295                 acceptAll( asmStmt->get_input(), *this);
    296                 acceptAll( asmStmt->get_output(), *this);
    297         }
    298 
    299         void Resolver::visit( IfStmt *ifStmt ) {
    300                 Expression *newExpr = findSingleExpression( ifStmt->get_condition(), *this );
     284        void Resolver::previsit( AsmStmt *asmStmt ) {
     285                visit_children = false;
     286                acceptAll( asmStmt->get_input(), *visitor );
     287                acceptAll( asmStmt->get_output(), *visitor );
     288        }
     289
     290        void Resolver::previsit( IfStmt *ifStmt ) {
     291                Expression *newExpr = findSingleExpression( ifStmt->get_condition(), indexer );
    301292                delete ifStmt->get_condition();
    302293                ifStmt->set_condition( newExpr );
    303                 Parent::visit( ifStmt );
    304         }
    305 
    306         void Resolver::visit( WhileStmt *whileStmt ) {
    307                 Expression *newExpr = findSingleExpression( whileStmt->get_condition(), *this );
     294        }
     295
     296        void Resolver::previsit( WhileStmt *whileStmt ) {
     297                Expression *newExpr = findSingleExpression( whileStmt->get_condition(), indexer );
    308298                delete whileStmt->get_condition();
    309299                whileStmt->set_condition( newExpr );
    310                 Parent::visit( whileStmt );
    311         }
    312 
    313         void Resolver::visit( ForStmt *forStmt ) {
    314                 Parent::visit( forStmt );
    315 
     300        }
     301
     302        void Resolver::previsit( ForStmt *forStmt ) {
    316303                if ( forStmt->get_condition() ) {
    317                         Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this );
     304                        Expression * newExpr = findSingleExpression( forStmt->get_condition(), indexer );
    318305                        delete forStmt->get_condition();
    319306                        forStmt->set_condition( newExpr );
     
    321308
    322309                if ( forStmt->get_increment() ) {
    323                         Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this );
     310                        Expression * newExpr = findVoidExpression( forStmt->get_increment(), indexer );
    324311                        delete forStmt->get_increment();
    325312                        forStmt->set_increment( newExpr );
     
    327314        }
    328315
    329         void Resolver::visit( SwitchStmt *switchStmt ) {
    330                 ValueGuard< CurrentObject > oldCurrentObject( currentObject );
     316        void Resolver::previsit( SwitchStmt *switchStmt ) {
     317                GuardValue( currentObject );
    331318                Expression *newExpr;
    332                 newExpr = findIntegralExpression( switchStmt->get_condition(), *this );
     319                newExpr = findIntegralExpression( switchStmt->get_condition(), indexer );
    333320                delete switchStmt->get_condition();
    334321                switchStmt->set_condition( newExpr );
    335322
    336323                currentObject = CurrentObject( newExpr->get_result() );
    337                 Parent::visit( switchStmt );
    338         }
    339 
    340         void Resolver::visit( CaseStmt *caseStmt ) {
     324        }
     325
     326        void Resolver::previsit( CaseStmt *caseStmt ) {
    341327                if ( caseStmt->get_condition() ) {
    342328                        std::list< InitAlternative > initAlts = currentObject.getOptions();
    343329                        assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral expression." );
    344330                        CastExpr * castExpr = new CastExpr( caseStmt->get_condition(), initAlts.front().type->clone() );
    345                         Expression * newExpr = findSingleExpression( castExpr, *this );
     331                        Expression * newExpr = findSingleExpression( castExpr, indexer );
    346332                        castExpr = strict_dynamic_cast< CastExpr * >( newExpr );
    347333                        caseStmt->set_condition( castExpr->get_arg() );
     
    349335                        delete castExpr;
    350336                }
    351                 Parent::visit( caseStmt );
    352         }
    353 
    354         void Resolver::visit( BranchStmt *branchStmt ) {
     337        }
     338
     339        void Resolver::previsit( BranchStmt *branchStmt ) {
     340                visit_children = false;
    355341                // must resolve the argument for a computed goto
    356342                if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement
     
    359345                                PointerType pt( Type::Qualifiers(), v.clone() );
    360346                                CastExpr * castExpr = new CastExpr( arg, pt.clone() );
    361                                 Expression * newExpr = findSingleExpression( castExpr, *this ); // find best expression
     347                                Expression * newExpr = findSingleExpression( castExpr, indexer ); // find best expression
    362348                                branchStmt->set_target( newExpr );
    363349                        } // if
     
    365351        }
    366352
    367         void Resolver::visit( ReturnStmt *returnStmt ) {
     353        void Resolver::previsit( ReturnStmt *returnStmt ) {
     354                visit_children = false;
    368355                if ( returnStmt->get_expr() ) {
    369356                        CastExpr *castExpr = new CastExpr( returnStmt->get_expr(), functionReturn->clone() );
    370                         Expression *newExpr = findSingleExpression( castExpr, *this );
     357                        Expression *newExpr = findSingleExpression( castExpr, indexer );
    371358                        delete castExpr;
    372359                        returnStmt->set_expr( newExpr );
     
    374361        }
    375362
    376         void Resolver::visit( ThrowStmt *throwStmt ) {
     363        void Resolver::previsit( ThrowStmt *throwStmt ) {
     364                visit_children = false;
    377365                // TODO: Replace *exception type with &exception type.
    378366                if ( throwStmt->get_expr() ) {
    379367                        StructDecl * exception_decl =
    380                                 lookupStruct( "__cfaehm__base_exception_t" );
     368                                indexer.lookupStruct( "__cfaehm__base_exception_t" );
    381369                        assert( exception_decl );
    382370                        Expression * wrapped = new CastExpr(
     
    390378                                        )
    391379                                );
    392                         Expression * newExpr = findSingleExpression( wrapped, *this );
     380                        Expression * newExpr = findSingleExpression( wrapped, indexer );
    393381                        throwStmt->set_expr( newExpr );
    394382                }
    395383        }
    396384
    397         void Resolver::visit( CatchStmt *catchStmt ) {
    398                 // inline Indexer::visit so that the exception variable is still in-scope for
    399                 // findSingleExpression() below
    400                 Parent::enterScope();
    401                 Visitor::visit( catchStmt );
    402 
     385        void Resolver::previsit( CatchStmt *catchStmt ) {
    403386                if ( catchStmt->get_cond() ) {
    404387                        Expression * wrapped = new CastExpr(
     
    406389                                new BasicType( noQualifiers, BasicType::Bool )
    407390                                );
    408                         catchStmt->set_cond( findSingleExpression( wrapped, *this ) );
    409                 }
    410 
    411                 Parent::leaveScope();
     391                        catchStmt->set_cond( findSingleExpression( wrapped, indexer ) );
     392                }
    412393        }
    413394
     
    435416        }
    436417
    437         void Resolver::visit( WaitForStmt * stmt ) {
     418        void Resolver::previsit( WaitForStmt * stmt ) {
    438419
    439420                // Resolve all clauses first
     
    623604        }
    624605
    625         void Resolver::visit( SingleInit *singleInit ) {
     606        void Resolver::previsit( SingleInit *singleInit ) {
     607                visit_children = false;
    626608                // resolve initialization using the possibilities as determined by the currentObject cursor
    627609                UntypedInitExpr * untyped = new UntypedInitExpr( singleInit->get_value(), currentObject.getOptions() );
    628                 Expression * newExpr = findSingleExpression( untyped, *this );
     610                Expression * newExpr = findSingleExpression( untyped, indexer );
    629611                InitExpr * initExpr = strict_dynamic_cast< InitExpr * >( newExpr );
    630612
     
    665647        }
    666648
    667         void Resolver::visit( ListInit * listInit ) {
     649        void Resolver::previsit( ListInit * listInit ) {
     650                visit_children = false;
    668651                // move cursor into brace-enclosed initializer-list
    669652                currentObject.enterListInit();
     
    676659                        Initializer * init = std::get<1>(p);
    677660                        newDesignations.push_back( currentObject.findNext( des ) );
    678                         init->accept( *this );
     661                        init->accept( *visitor );
    679662                }
    680663                // set the set of 'resolved' designations and leave the brace-enclosed initializer-list
     
    705688                delete ctorInit->get_dtor();
    706689                ctorInit->set_dtor( NULL );
    707                 maybeAccept( ctorInit->get_init(), *this );
     690                maybeAccept( ctorInit->get_init(), *visitor );
    708691        }
    709692
     
    711694        void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ) {
    712695                assert( ctorInit );
    713                 Resolver resolver( indexer );
     696                PassVisitor<Resolver> resolver( indexer );
    714697                ctorInit->accept( resolver );
    715698        }
     
    717700        void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) {
    718701                assert( stmtExpr );
    719                 Resolver resolver( indexer );
     702                PassVisitor<Resolver> resolver( indexer );
    720703                stmtExpr->accept( resolver );
    721704        }
    722705
    723         void Resolver::visit( ConstructorInit *ctorInit ) {
     706        void Resolver::previsit( ConstructorInit *ctorInit ) {
     707                visit_children = false;
    724708                // xxx - fallback init has been removed => remove fallbackInit function and remove complexity from FixInit and remove C-init from ConstructorInit
    725                 maybeAccept( ctorInit->get_ctor(), *this );
    726                 maybeAccept( ctorInit->get_dtor(), *this );
     709                maybeAccept( ctorInit->get_ctor(), *visitor );
     710                maybeAccept( ctorInit->get_dtor(), *visitor );
    727711
    728712                // found a constructor - can get rid of C-style initializer
Note: See TracChangeset for help on using the changeset viewer.