Changes in src/SymTab/Indexer.cc [0ac366b:d55d7a6]
- File:
-
- 1 edited
-
src/SymTab/Indexer.cc (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/SymTab/Indexer.cc
r0ac366b rd55d7a6 286 286 } 287 287 288 const Indexer::IdData *Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) const {289 if ( ! tables ) return nullptr;290 if ( tables->scope < scope ) return nullptr;288 DeclarationWithType *Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) const { 289 if ( ! tables ) return 0; 290 if ( tables->scope < scope ) return 0; 291 291 292 292 IdTable::const_iterator decls = tables->idTable.find( id ); … … 294 294 const MangleTable &mangleTable = decls->second; 295 295 MangleTable::const_iterator decl = mangleTable.find( mangleName ); 296 if ( decl != mangleTable.end() ) return &decl->second;296 if ( decl != mangleTable.end() ) return decl->second.id; 297 297 } 298 298 299 299 return tables->base.lookupIdAtScope( id, mangleName, scope ); 300 }301 302 Indexer::IdData * Indexer::lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) {303 return const_cast<IdData *>(const_cast<const Indexer *>(this)->lookupIdAtScope( id, mangleName, scope ));304 300 } 305 301 … … 377 373 } 378 374 379 bool addedIdConflicts( Indexer::IdData & existing, DeclarationWithType *added, BaseSyntaxNode * deleteStmt, Indexer::ConflictFunction handleConflicts) {375 bool addedIdConflicts( DeclarationWithType *existing, DeclarationWithType *added ) { 380 376 // if we're giving the same name mangling to things of different types then there is something wrong 381 assert( (dynamic_cast<ObjectDecl*>( added ) && dynamic_cast<ObjectDecl*>( existing .id) )382 || (dynamic_cast<FunctionDecl*>( added ) && dynamic_cast<FunctionDecl*>( existing .id) ) );383 384 if ( LinkageSpec::isOverridable( existing .id->get_linkage() ) ) {377 assert( (dynamic_cast<ObjectDecl*>( added ) && dynamic_cast<ObjectDecl*>( existing ) ) 378 || (dynamic_cast<FunctionDecl*>( added ) && dynamic_cast<FunctionDecl*>( existing ) ) ); 379 380 if ( LinkageSpec::isOverridable( existing->get_linkage() ) ) { 385 381 // new definition shadows the autogenerated one, even at the same scope 386 382 return false; 387 } else if ( LinkageSpec::isMangled( added->get_linkage() ) || ResolvExpr::typesCompatible( added->get_type(), existing.id->get_type(), Indexer() ) ) { 388 389 // it is a conflict if one declaration is deleted and the other is not 390 if ( deleteStmt && ! existing.deleteStmt ) { 391 return handleConflicts( existing, "deletion of defined identifier " ); 392 } else if ( ! deleteStmt && existing.deleteStmt ) { 393 return handleConflicts( existing, "definition of deleted identifier " ); 394 } 395 383 } else if ( LinkageSpec::isMangled( added->get_linkage() ) || ResolvExpr::typesCompatible( added->get_type(), existing->get_type(), Indexer() ) ) { 396 384 // typesCompatible doesn't really do the right thing here. When checking compatibility of function types, 397 385 // we should ignore outermost pointer qualifiers, except _Atomic? 398 FunctionDecl * newentry = dynamic_cast< FunctionDecl* >( added );399 FunctionDecl * oldentry = dynamic_cast< FunctionDecl * >( existing.id);386 FunctionDecl *newentry = dynamic_cast< FunctionDecl* >( added ); 387 FunctionDecl *oldentry = dynamic_cast< FunctionDecl* >( existing ); 400 388 if ( newentry && oldentry ) { 401 389 if ( newentry->get_statements() && oldentry->get_statements() ) { 402 return handleConflicts( existing, "duplicate function definition for " );390 throw SemanticError( added, "duplicate function definition for " ); 403 391 } // if 404 392 } else { … … 407 395 // xxx - perhaps it's actually if either is intrinsic then this is okay? 408 396 // might also need to be same storage class? 409 ObjectDecl * newobj = dynamic_cast< ObjectDecl* >( added );410 ObjectDecl * oldobj = dynamic_cast< ObjectDecl * >( existing.id);397 ObjectDecl *newobj = dynamic_cast< ObjectDecl* >( added ); 398 ObjectDecl *oldobj = dynamic_cast< ObjectDecl* >( existing ); 411 399 if ( ! newobj->get_storageClasses().is_extern && ! oldobj->get_storageClasses().is_extern ) { 412 return handleConflicts( existing, "duplicate object definition for " );400 throw SemanticError( added, "duplicate object definition for " ); 413 401 } // if 414 402 } // if 415 403 } else { 416 return handleConflicts( existing, "duplicate definition for " );404 throw SemanticError( added, "duplicate definition for " ); 417 405 } // if 418 406 … … 420 408 } 421 409 422 void Indexer::addId( DeclarationWithType *decl, ConflictFunction handleConflicts, Expression * baseExpr, BaseSyntaxNode * deleteStmt) {410 void Indexer::addId( DeclarationWithType *decl, Expression * baseExpr ) { 423 411 if ( decl->name == "" ) return; 424 412 debugPrint( "Adding Id " << decl->name << std::endl ); … … 443 431 // isomorphic to C type-compatibility, which it may not be. 444 432 if ( hasIncompatibleCDecl( name, mangleName, scope ) ) { 445 throw SemanticError( "conflicting overload of C function ", decl);446 } 447 } else { 448 // Check that a Cforall declaration doesn't over rideany C declaration433 throw SemanticError( decl, "conflicting overload of C function " ); 434 } 435 } else { 436 // Check that a Cforall declaration doesn't overload any C declaration 449 437 if ( hasCompatibleCDecl( name, mangleName, scope ) ) { 450 throw SemanticError( "Cforall declaration hides C function ", decl);438 throw SemanticError( decl, "Cforall declaration hides C function " ); 451 439 } 452 440 } 453 441 454 442 // Skip repeat declarations of the same identifier 455 IdData *existing = lookupIdAtScope( name, mangleName, scope );456 if ( existing && existing->id && addedIdConflicts( *existing, decl, deleteStmt, handleConflicts) ) return;443 DeclarationWithType *existing = lookupIdAtScope( name, mangleName, scope ); 444 if ( existing && addedIdConflicts( existing, decl ) ) return; 457 445 458 446 // add to indexer 459 tables->idTable[ name ][ mangleName ] = { decl, baseExpr , deleteStmt};447 tables->idTable[ name ][ mangleName ] = { decl, baseExpr }; 460 448 ++tables->size; 461 }462 463 void Indexer::addId( DeclarationWithType * decl, Expression * baseExpr ) {464 // default handling of conflicts is to raise an error465 addId( decl, [decl](IdData &, const std::string & msg) { throw SemanticError( msg, decl ); return true; }, baseExpr );466 }467 468 void Indexer::addDeletedId( DeclarationWithType * decl, BaseSyntaxNode * deleteStmt ) {469 // default handling of conflicts is to raise an error470 addId( decl, [decl](IdData &, const std::string & msg) { throw SemanticError( msg, decl ); return true; }, nullptr, deleteStmt );471 449 } 472 450 … … 477 455 return true; 478 456 } else { 479 throw SemanticError( "redeclaration of ", added);457 throw SemanticError( added, "redeclaration of " ); 480 458 } 481 459 } … … 504 482 return false; 505 483 } else if ( ! added->get_members().empty() ) { 506 throw SemanticError( "redeclaration of ", added);484 throw SemanticError( added, "redeclaration of " ); 507 485 } // if 508 486 return true; … … 595 573 } 596 574 597 void Indexer::addMembers( AggregateDecl * aggr, Expression * expr , ConflictFunction handleConflicts) {575 void Indexer::addMembers( AggregateDecl * aggr, Expression * expr ) { 598 576 for ( Declaration * decl : aggr->members ) { 599 577 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { 600 addId( dwt, handleConflicts,expr );578 addId( dwt, expr ); 601 579 if ( dwt->name == "" ) { 602 580 Type * t = dwt->get_type()->stripReferences(); … … 604 582 Expression * base = expr->clone(); 605 583 ResolvExpr::referenceToRvalueConversion( base ); 606 addMembers( t->getAggr(), new MemberExpr( dwt, base ) , handleConflicts);584 addMembers( t->getAggr(), new MemberExpr( dwt, base ) ); 607 585 } 608 586 } … … 611 589 } 612 590 613 void Indexer::addWith( std::list< Expression * > & withExprs , BaseSyntaxNode * withStmt) {591 void Indexer::addWith( std::list< Expression * > & withExprs ) { 614 592 for ( Expression * expr : withExprs ) { 615 593 if ( expr->result ) { … … 617 595 assertf( aggr, "WithStmt expr has non-aggregate type: %s", toString( expr->result ).c_str() ); 618 596 619 addMembers( aggr, expr, [withStmt](IdData & existing, const std::string &) { 620 // on conflict, delete the identifier 621 existing.deleteStmt = withStmt; 622 return true; 623 }); 597 addMembers( aggr, expr ); 624 598 } 625 599 } … … 680 654 681 655 void Indexer::print( std::ostream &os, int indent ) const { 682 using std::cerr;656 using std::cerr; 683 657 684 658 if ( tables ) { … … 706 680 707 681 Expression * Indexer::IdData::combine() const { 708 Expression * ret = nullptr;709 682 if ( baseExpr ) { 710 683 Expression * base = baseExpr->clone(); 711 684 ResolvExpr::referenceToRvalueConversion( base ); 712 ret = new MemberExpr( id, base );685 Expression * ret = new MemberExpr( id, base ); 713 686 // xxx - this introduces hidden environments, for now remove them. 714 687 // std::swap( base->env, ret->env ); 715 688 delete base->env; 716 689 base->env = nullptr; 717 } else { 718 ret = new VariableExpr( id ); 719 } 720 if ( deleteStmt ) ret = new DeletedExpr( ret, deleteStmt ); 721 return ret; 690 return ret; 691 } else { 692 return new VariableExpr( id ); 693 } 722 694 } 723 695 } // namespace SymTab
Note:
See TracChangeset
for help on using the changeset viewer.