Changeset 695e00d for src/ResolvExpr/Resolver.cc
- Timestamp:
- Sep 19, 2017, 2:14:39 PM (7 years ago)
- 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. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/ResolvExpr/Resolver.cc
re149f77 r695e00d 21 21 #include "Alternative.h" // for Alternative, AltList 22 22 #include "AlternativeFinder.h" // for AlternativeFinder, resolveIn... 23 #include "Common/PassVisitor.h" // for PassVisitor 23 24 #include "Common/SemanticError.h" // for SemanticError 24 25 #include "Common/utility.h" // for ValueGuard, group_iterate … … 44 45 45 46 namespace 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 ); 84 79 private: 85 80 typedef std::list< Initializer * >::iterator InitIterator; … … 98 93 99 94 void resolve( std::list< Declaration * > translationUnit ) { 100 Resolverresolver;95 PassVisitor<Resolver> resolver; 101 96 acceptAll( translationUnit, resolver ); 102 97 } 103 98 99 // used in resolveTypeof 104 100 Expression *resolveInVoidContext( Expression *expr, const SymTab::Indexer &indexer ) { 105 101 TypeEnvironment env; 106 102 return resolveInVoidContext( expr, indexer, env ); 107 103 } 108 109 104 110 105 namespace { … … 192 187 } 193 188 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 ); 196 191 objectDecl->set_type( new_type ); 197 192 // To handle initialization of routine pointers, e.g., int (*fp)(int) = foo(), means that class-variable … … 200 195 // each value of initContext is retained, so the type on the first analysis is preserved and used for selecting 201 196 // the RHS. 202 ValueGuard<CurrentObject> temp( currentObject );197 GuardValue( currentObject ); 203 198 currentObject = CurrentObject( objectDecl->get_type() ); 204 199 if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) { … … 207 202 currentObject = CurrentObject( new BasicType( Type::Qualifiers(), BasicType::SignedInt ) ); 208 203 } 209 Parent::visit( objectDecl );210 if ( inEnumDecl && dynamic_cast< EnumInstType * >( objectDecl->get_type() ) ) {211 // delete newly created signed int type212 // delete currentObject.getType();213 }214 204 } 215 205 … … 218 208 if ( type->get_dimension() ) { 219 209 CastExpr *castExpr = new CastExpr( type->get_dimension(), SymTab::SizeType->clone() ); 220 Expression *newExpr = findSingleExpression( castExpr, *this);210 Expression *newExpr = findSingleExpression( castExpr, indexer ); 221 211 delete type->get_dimension(); 222 212 type->set_dimension( newExpr ); … … 224 214 } 225 215 226 void Resolver:: visit( ArrayType * at ) {216 void Resolver::previsit( ArrayType * at ) { 227 217 handlePtrType( at ); 228 Parent::visit( at ); 229 } 230 231 void Resolver::visit( PointerType * pt ) { 218 } 219 220 void Resolver::previsit( PointerType * pt ) { 232 221 handlePtrType( pt ); 233 Parent::visit( pt ); 234 } 235 236 void Resolver::visit( TypeDecl *typeDecl ) { 222 } 223 224 void Resolver::previsit( TypeDecl *typeDecl ) { 237 225 if ( typeDecl->get_base() ) { 238 Type *new_type = resolveTypeof( typeDecl->get_base(), *this);226 Type *new_type = resolveTypeof( typeDecl->get_base(), indexer ); 239 227 typeDecl->set_base( new_type ); 240 228 } // if 241 Parent::visit( typeDecl ); 242 } 243 244 void Resolver::visit( FunctionDecl *functionDecl ) { 229 } 230 231 void Resolver::previsit( FunctionDecl *functionDecl ) { 245 232 #if 0 246 std::c out<< "resolver visiting functiondecl ";247 functionDecl->print( std::c out);248 std::c out<< std::endl;233 std::cerr << "resolver visiting functiondecl "; 234 functionDecl->print( std::cerr ); 235 std::cerr << std::endl; 249 236 #endif 250 Type *new_type = resolveTypeof( functionDecl->get_type(), *this);237 Type *new_type = resolveTypeof( functionDecl->get_type(), indexer ); 251 238 functionDecl->set_type( new_type ); 252 ValueGuard< Type * > oldFunctionReturn( functionReturn );239 GuardValue( functionReturn ); 253 240 functionReturn = ResolvExpr::extractResultType( functionDecl->get_functionType() ); 254 Parent::visit( functionDecl ); 255 241 } 242 243 244 void Resolver::postvisit( FunctionDecl *functionDecl ) { 256 245 // default value expressions have an environment which shouldn't be there and trips up later passes. 257 246 // xxx - it might be necessary to somehow keep the information from this environment, but I can't currently … … 267 256 } 268 257 269 void Resolver:: visit( EnumDecl * enumDecl) {258 void Resolver::previsit( EnumDecl * ) { 270 259 // in case we decide to allow nested enums 271 ValueGuard< bool > oldInEnumDecl( inEnumDecl );260 GuardValue( inEnumDecl ); 272 261 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; 277 266 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 ); 279 268 delete exprStmt->get_expr(); 280 269 exprStmt->set_expr( newExpr ); 281 270 } 282 271 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 ); 285 275 delete asmExpr->get_operand(); 286 276 asmExpr->set_operand( newExpr ); 287 277 if ( asmExpr->get_inout() ) { 288 newExpr = findVoidExpression( asmExpr->get_inout(), *this);278 newExpr = findVoidExpression( asmExpr->get_inout(), indexer ); 289 279 delete asmExpr->get_inout(); 290 280 asmExpr->set_inout( newExpr ); … … 292 282 } 293 283 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 ); 301 292 delete ifStmt->get_condition(); 302 293 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 ); 308 298 delete whileStmt->get_condition(); 309 299 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 ) { 316 303 if ( forStmt->get_condition() ) { 317 Expression * newExpr = findSingleExpression( forStmt->get_condition(), *this);304 Expression * newExpr = findSingleExpression( forStmt->get_condition(), indexer ); 318 305 delete forStmt->get_condition(); 319 306 forStmt->set_condition( newExpr ); … … 321 308 322 309 if ( forStmt->get_increment() ) { 323 Expression * newExpr = findVoidExpression( forStmt->get_increment(), *this);310 Expression * newExpr = findVoidExpression( forStmt->get_increment(), indexer ); 324 311 delete forStmt->get_increment(); 325 312 forStmt->set_increment( newExpr ); … … 327 314 } 328 315 329 void Resolver:: visit( SwitchStmt *switchStmt ) {330 ValueGuard< CurrentObject > oldCurrentObject( currentObject );316 void Resolver::previsit( SwitchStmt *switchStmt ) { 317 GuardValue( currentObject ); 331 318 Expression *newExpr; 332 newExpr = findIntegralExpression( switchStmt->get_condition(), *this);319 newExpr = findIntegralExpression( switchStmt->get_condition(), indexer ); 333 320 delete switchStmt->get_condition(); 334 321 switchStmt->set_condition( newExpr ); 335 322 336 323 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 ) { 341 327 if ( caseStmt->get_condition() ) { 342 328 std::list< InitAlternative > initAlts = currentObject.getOptions(); 343 329 assertf( initAlts.size() == 1, "SwitchStmt did not correctly resolve an integral expression." ); 344 330 CastExpr * castExpr = new CastExpr( caseStmt->get_condition(), initAlts.front().type->clone() ); 345 Expression * newExpr = findSingleExpression( castExpr, *this);331 Expression * newExpr = findSingleExpression( castExpr, indexer ); 346 332 castExpr = strict_dynamic_cast< CastExpr * >( newExpr ); 347 333 caseStmt->set_condition( castExpr->get_arg() ); … … 349 335 delete castExpr; 350 336 } 351 Parent::visit( caseStmt );352 } 353 354 void Resolver::visit( BranchStmt *branchStmt ) {337 } 338 339 void Resolver::previsit( BranchStmt *branchStmt ) { 340 visit_children = false; 355 341 // must resolve the argument for a computed goto 356 342 if ( branchStmt->get_type() == BranchStmt::Goto ) { // check for computed goto statement … … 359 345 PointerType pt( Type::Qualifiers(), v.clone() ); 360 346 CastExpr * castExpr = new CastExpr( arg, pt.clone() ); 361 Expression * newExpr = findSingleExpression( castExpr, *this); // find best expression347 Expression * newExpr = findSingleExpression( castExpr, indexer ); // find best expression 362 348 branchStmt->set_target( newExpr ); 363 349 } // if … … 365 351 } 366 352 367 void Resolver::visit( ReturnStmt *returnStmt ) { 353 void Resolver::previsit( ReturnStmt *returnStmt ) { 354 visit_children = false; 368 355 if ( returnStmt->get_expr() ) { 369 356 CastExpr *castExpr = new CastExpr( returnStmt->get_expr(), functionReturn->clone() ); 370 Expression *newExpr = findSingleExpression( castExpr, *this);357 Expression *newExpr = findSingleExpression( castExpr, indexer ); 371 358 delete castExpr; 372 359 returnStmt->set_expr( newExpr ); … … 374 361 } 375 362 376 void Resolver::visit( ThrowStmt *throwStmt ) { 363 void Resolver::previsit( ThrowStmt *throwStmt ) { 364 visit_children = false; 377 365 // TODO: Replace *exception type with &exception type. 378 366 if ( throwStmt->get_expr() ) { 379 367 StructDecl * exception_decl = 380 lookupStruct( "__cfaehm__base_exception_t" );368 indexer.lookupStruct( "__cfaehm__base_exception_t" ); 381 369 assert( exception_decl ); 382 370 Expression * wrapped = new CastExpr( … … 390 378 ) 391 379 ); 392 Expression * newExpr = findSingleExpression( wrapped, *this);380 Expression * newExpr = findSingleExpression( wrapped, indexer ); 393 381 throwStmt->set_expr( newExpr ); 394 382 } 395 383 } 396 384 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 ) { 403 386 if ( catchStmt->get_cond() ) { 404 387 Expression * wrapped = new CastExpr( … … 406 389 new BasicType( noQualifiers, BasicType::Bool ) 407 390 ); 408 catchStmt->set_cond( findSingleExpression( wrapped, *this ) ); 409 } 410 411 Parent::leaveScope(); 391 catchStmt->set_cond( findSingleExpression( wrapped, indexer ) ); 392 } 412 393 } 413 394 … … 435 416 } 436 417 437 void Resolver:: visit( WaitForStmt * stmt ) {418 void Resolver::previsit( WaitForStmt * stmt ) { 438 419 439 420 // Resolve all clauses first … … 623 604 } 624 605 625 void Resolver::visit( SingleInit *singleInit ) { 606 void Resolver::previsit( SingleInit *singleInit ) { 607 visit_children = false; 626 608 // resolve initialization using the possibilities as determined by the currentObject cursor 627 609 UntypedInitExpr * untyped = new UntypedInitExpr( singleInit->get_value(), currentObject.getOptions() ); 628 Expression * newExpr = findSingleExpression( untyped, *this);610 Expression * newExpr = findSingleExpression( untyped, indexer ); 629 611 InitExpr * initExpr = strict_dynamic_cast< InitExpr * >( newExpr ); 630 612 … … 665 647 } 666 648 667 void Resolver::visit( ListInit * listInit ) { 649 void Resolver::previsit( ListInit * listInit ) { 650 visit_children = false; 668 651 // move cursor into brace-enclosed initializer-list 669 652 currentObject.enterListInit(); … … 676 659 Initializer * init = std::get<1>(p); 677 660 newDesignations.push_back( currentObject.findNext( des ) ); 678 init->accept( * this);661 init->accept( *visitor ); 679 662 } 680 663 // set the set of 'resolved' designations and leave the brace-enclosed initializer-list … … 705 688 delete ctorInit->get_dtor(); 706 689 ctorInit->set_dtor( NULL ); 707 maybeAccept( ctorInit->get_init(), * this);690 maybeAccept( ctorInit->get_init(), *visitor ); 708 691 } 709 692 … … 711 694 void resolveCtorInit( ConstructorInit * ctorInit, const SymTab::Indexer & indexer ) { 712 695 assert( ctorInit ); 713 Resolverresolver( indexer );696 PassVisitor<Resolver> resolver( indexer ); 714 697 ctorInit->accept( resolver ); 715 698 } … … 717 700 void resolveStmtExpr( StmtExpr * stmtExpr, const SymTab::Indexer & indexer ) { 718 701 assert( stmtExpr ); 719 Resolverresolver( indexer );702 PassVisitor<Resolver> resolver( indexer ); 720 703 stmtExpr->accept( resolver ); 721 704 } 722 705 723 void Resolver::visit( ConstructorInit *ctorInit ) { 706 void Resolver::previsit( ConstructorInit *ctorInit ) { 707 visit_children = false; 724 708 // 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 ); 727 711 728 712 // found a constructor - can get rid of C-style initializer
Note: See TracChangeset
for help on using the changeset viewer.