Changeset e8032b0 for src/SymTab
- Timestamp:
- Mar 8, 2016, 1:51:25 PM (9 years ago)
- Branches:
- ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, string, with_gc
- Children:
- 52c2a72
- Parents:
- 4e284ea6
- Location:
- src/SymTab
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
src/SymTab/Indexer.cc
r4e284ea6 re8032b0 14 14 // 15 15 16 #include "Indexer.h" 17 18 #include "IdTable.h" 19 #include "AggregateTable.h" 20 #include "TypeTable.h" 21 16 22 #include "SynTree/Declaration.h" 17 23 #include "SynTree/Type.h" … … 19 25 #include "SynTree/Initializer.h" 20 26 #include "SynTree/Statement.h" 21 #include "Indexer.h" 27 22 28 #include <typeinfo> 29 #include <utility> 23 30 #include "Common/utility.h" 24 31 … … 33 40 } 34 41 35 Indexer::Indexer( bool useDebug ) : doDebug( useDebug ) {} 36 37 Indexer::~Indexer() {} 42 struct Indexer::Impl { 43 Impl() : refCount(1), size(0), base(), 44 idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {} 45 Impl( const Indexer &_base ) : refCount(1), size(0), base( _base ), 46 idTable(), typeTable(), structTable(), enumTable(), unionTable(), traitTable() {} 47 unsigned long refCount; ///< Number of references to these tables 48 unsigned long size; ///< Number of elements stored in this table 49 const Indexer base; ///< Base indexer this extends 50 51 IdTable idTable; ///< Identifier namespace 52 TypeTable typeTable; ///< Type namespace 53 StructTable structTable; ///< Struct namespace 54 EnumTable enumTable; ///< Enum namespace 55 UnionTable unionTable; ///< Union namespace 56 TraitTable traitTable; ///< Trait namespace 57 }; 58 59 Indexer::Impl *Indexer::newRef( Indexer::Impl *toClone ) { 60 if ( ! toClone ) return 0; 61 62 // shorten the search chain by skipping empty links 63 Indexer::Impl *ret = toClone->size == 0 ? toClone->base.tables : toClone; 64 if ( ret ) { ++ret->refCount; } 65 66 return ret; 67 } 68 69 void Indexer::deleteRef( Indexer::Impl *toFree ) { 70 if ( ! toFree ) return; 71 72 if ( --toFree->refCount == 0 ) delete toFree; 73 } 74 75 void Indexer::makeWritable() { 76 if ( ! tables ) { 77 tables = new Indexer::Impl; 78 } else if ( tables->refCount > 1 ) { 79 // tables->base inherits the ref that used to belong to this indexer 80 // this is basically equivalent to std::move( *this ) as the argument 81 tables = new Indexer::Impl( Indexer( tables, doDebug ) ); 82 } 83 } 84 85 Indexer::Indexer( bool _doDebug ) : tables(0), doDebug( _doDebug ) {} 86 87 Indexer::Indexer( const Indexer &that ) : tables( newRef( that.tables ) ), doDebug( that.doDebug ) {} 88 89 Indexer::Indexer( Indexer &&that ) : tables( that.tables ), doDebug( that.doDebug ) { 90 that.tables = 0; 91 } 92 93 Indexer::~Indexer() { 94 deleteRef( tables ); 95 } 96 97 Indexer& Indexer::operator= ( const Indexer &that ) { 98 deleteRef( tables ); 99 100 tables = newRef( that.tables ); 101 doDebug = that.doDebug; 102 103 return *this; 104 } 105 106 Indexer& Indexer::operator= ( Indexer &&that ) { 107 deleteRef( tables ); 108 109 tables = that.tables; 110 doDebug = that.doDebug; 111 112 that.tables = 0; 113 114 return *this; 115 } 38 116 39 117 void Indexer::visit( ObjectDecl *objectDecl ) { … … 45 123 if ( objectDecl->get_name() != "" ) { 46 124 debugPrint( "Adding object " << objectDecl->get_name() << std::endl ); 47 idTable.addDecl( objectDecl );125 addId( objectDecl ); 48 126 } // if 49 127 } … … 52 130 if ( functionDecl->get_name() == "" ) return; 53 131 debugPrint( "Adding function " << functionDecl->get_name() << std::endl ); 54 idTable.addDecl( functionDecl );132 addId( functionDecl ); 55 133 enterScope(); 56 134 maybeAccept( functionDecl->get_functionType(), *this ); … … 90 168 leaveScope(); 91 169 debugPrint( "Adding type " << typeDecl->get_name() << std::endl ); 92 typeTable.add( typeDecl );170 addType( typeDecl ); 93 171 acceptAll( typeDecl->get_assertions(), *this ); 94 172 } … … 100 178 leaveScope(); 101 179 debugPrint( "Adding typedef " << typeDecl->get_name() << std::endl ); 102 typeTable.add( typeDecl );180 addType( typeDecl ); 103 181 } 104 182 … … 108 186 cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() ); 109 187 debugPrint( "Adding fwd decl for struct " << fwdDecl.get_name() << std::endl ); 110 structTable.add( &fwdDecl );188 addStruct( &fwdDecl ); 111 189 112 190 enterScope(); … … 117 195 debugPrint( "Adding struct " << aggregateDecl->get_name() << std::endl ); 118 196 // this addition replaces the forward declaration 119 structTable.add( aggregateDecl );197 addStruct( aggregateDecl ); 120 198 } 121 199 … … 125 203 cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() ); 126 204 debugPrint( "Adding fwd decl for union " << fwdDecl.get_name() << std::endl ); 127 unionTable.add( &fwdDecl );205 addUnion( &fwdDecl ); 128 206 129 207 enterScope(); … … 133 211 134 212 debugPrint( "Adding union " << aggregateDecl->get_name() << std::endl ); 135 unionTable.add( aggregateDecl );213 addUnion( aggregateDecl ); 136 214 } 137 215 138 216 void Indexer::visit( EnumDecl *aggregateDecl ) { 139 217 debugPrint( "Adding enum " << aggregateDecl->get_name() << std::endl ); 140 enumTable.add( aggregateDecl );218 addEnum( aggregateDecl ); 141 219 // unlike structs, contexts, and unions, enums inject their members into the global scope 142 220 acceptAll( aggregateDecl->get_members(), *this ); … … 150 228 151 229 debugPrint( "Adding context " << aggregateDecl->get_name() << std::endl ); 152 contextTable.add( aggregateDecl );230 addTrait( aggregateDecl ); 153 231 } 154 232 … … 299 377 300 378 void Indexer::visit( StructInstType *structInst ) { 301 if ( ! structTable.lookup( structInst->get_name() ) ) {379 if ( ! lookupStruct( structInst->get_name() ) ) { 302 380 debugPrint( "Adding struct " << structInst->get_name() << " from implicit forward declaration" << std::endl ); 303 structTable.add( structInst->get_name() );381 addStruct( structInst->get_name() ); 304 382 } 305 383 enterScope(); … … 309 387 310 388 void Indexer::visit( UnionInstType *unionInst ) { 311 if ( ! unionTable.lookup( unionInst->get_name() ) ) {389 if ( ! lookupUnion( unionInst->get_name() ) ) { 312 390 debugPrint( "Adding union " << unionInst->get_name() << " from implicit forward declaration" << std::endl ); 313 unionTable.add( unionInst->get_name() );391 addUnion( unionInst->get_name() ); 314 392 } 315 393 enterScope(); … … 327 405 328 406 void Indexer::lookupId( const std::string &id, std::list< DeclarationWithType* > &list ) const { 329 idTable.lookupId( id, list ); 407 if ( ! tables ) return; 408 409 tables->idTable.lookupId( id, list ); 410 tables->base.lookupId( id, list ); 330 411 } 331 412 332 413 DeclarationWithType* Indexer::lookupId( const std::string &id) const { 333 return idTable.lookupId(id); 414 if ( ! tables ) return 0; 415 416 DeclarationWithType *ret = tables->idTable.lookupId(id); 417 return ret ? ret : tables->base.lookupId(id); 334 418 } 335 419 336 420 NamedTypeDecl *Indexer::lookupType( const std::string &id ) const { 337 return typeTable.lookup( id ); 421 if ( ! tables ) return 0; 422 423 NamedTypeDecl *ret = tables->typeTable.lookup( id ); 424 return ret ? ret : tables->base.lookupType( id ); 338 425 } 339 426 340 427 StructDecl *Indexer::lookupStruct( const std::string &id ) const { 341 return structTable.lookup( id ); 428 if ( ! tables ) return 0; 429 430 StructDecl *ret = tables->structTable.lookup( id ); 431 return ret ? ret : tables->base.lookupStruct( id ); 342 432 } 343 433 344 434 EnumDecl *Indexer::lookupEnum( const std::string &id ) const { 345 return enumTable.lookup( id ); 435 if ( ! tables ) return 0; 436 437 EnumDecl *ret = tables->enumTable.lookup( id ); 438 return ret ? ret : tables->base.lookupEnum( id ); 346 439 } 347 440 348 441 UnionDecl *Indexer::lookupUnion( const std::string &id ) const { 349 return unionTable.lookup( id ); 350 } 351 352 TraitDecl * Indexer::lookupTrait( const std::string &id ) const { 353 return contextTable.lookup( id ); 442 if ( ! tables ) return 0; 443 444 UnionDecl *ret = tables->unionTable.lookup( id ); 445 return ret ? ret : tables->base.lookupUnion( id ); 446 } 447 448 TraitDecl *Indexer::lookupTrait( const std::string &id ) const { 449 if ( ! tables ) return 0; 450 451 TraitDecl *ret = tables->traitTable.lookup( id ); 452 return ret ? ret : tables->base.lookupTrait( id ); 453 } 454 455 void Indexer::addId( DeclarationWithType *decl ) { 456 makeWritable(); 457 tables->idTable.addDecl( decl ); 458 ++tables->size; 459 } 460 461 void Indexer::addType( NamedTypeDecl *decl ) { 462 makeWritable(); 463 tables->typeTable.add( decl ); 464 ++tables->size; 465 } 466 467 void Indexer::addStruct( const std::string &id ) { 468 makeWritable(); 469 tables->structTable.add( id ); 470 ++tables->size; 471 } 472 473 void Indexer::addStruct( StructDecl *decl ) { 474 makeWritable(); 475 tables->structTable.add( decl ); 476 ++tables->size; 477 } 478 479 void Indexer::addEnum( EnumDecl *decl ) { 480 makeWritable(); 481 tables->enumTable.add( decl ); 482 ++tables->size; 483 } 484 485 void Indexer::addUnion( const std::string &id ) { 486 makeWritable(); 487 tables->unionTable.add( id ); 488 ++tables->size; 489 } 490 491 void Indexer::addUnion( UnionDecl *decl ) { 492 makeWritable(); 493 tables->unionTable.add( decl ); 494 ++tables->size; 495 } 496 497 void Indexer::addTrait( TraitDecl *decl ) { 498 makeWritable(); 499 tables->traitTable.add( decl ); 500 ++tables->size; 354 501 } 355 502 356 503 void Indexer::enterScope() { 504 makeWritable(); 505 357 506 if ( doDebug ) { 358 507 std::cout << "--- Entering scope" << std::endl; 359 508 } 360 idTable.enterScope(); 361 typeTable.enterScope(); 362 structTable.enterScope(); 363 enumTable.enterScope(); 364 unionTable.enterScope(); 365 contextTable.enterScope(); 509 510 tables->idTable.enterScope(); 511 tables->typeTable.enterScope(); 512 tables->structTable.enterScope(); 513 tables->enumTable.enterScope(); 514 tables->unionTable.enterScope(); 515 tables->traitTable.enterScope(); 366 516 } 367 517 368 518 void Indexer::leaveScope() { 369 519 using std::cout; 370 using std::endl; 371 520 521 makeWritable(); 522 372 523 if ( doDebug ) { 373 cout << "--- Leaving scope containing" << endl;374 idTable.dump( cout );375 t ypeTable.dump( cout );376 structTable.dump( cout );377 enumTable.dump( cout );378 unionTable.dump( cout );379 contextTable.dump( cout );380 } 381 idTable.leaveScope();382 t ypeTable.leaveScope();383 structTable.leaveScope();384 enumTable.leaveScope();385 unionTable.leaveScope();386 contextTable.leaveScope();524 cout << "--- Leaving scope containing" << std::endl; 525 tables->idTable.dump( cout ); 526 tables->typeTable.dump( cout ); 527 tables->structTable.dump( cout ); 528 tables->enumTable.dump( cout ); 529 tables->unionTable.dump( cout ); 530 tables->traitTable.dump( cout ); 531 } 532 tables->idTable.leaveScope(); 533 tables->typeTable.leaveScope(); 534 tables->structTable.leaveScope(); 535 tables->enumTable.leaveScope(); 536 tables->unionTable.leaveScope(); 537 tables->traitTable.leaveScope(); 387 538 } 388 539 389 540 void Indexer::print( std::ostream &os, int indent ) const { 390 541 using std::cerr; 391 using std::endl; 392 393 cerr << "===idTable===" << endl; 394 idTable.dump( os ); 395 cerr << "===typeTable===" << endl; 396 typeTable.dump( os ); 397 cerr << "===structTable===" << endl; 398 structTable.dump( os ); 399 cerr << "===enumTable===" << endl; 400 enumTable.dump( os ); 401 cerr << "===unionTable===" << endl; 402 unionTable.dump( os ); 403 cerr << "===contextTable===" << endl; 404 contextTable.dump( os ); 405 #if 0 406 idTable.dump( os ); 407 typeTable.dump( os ); 408 structTable.dump( os ); 409 enumTable.dump( os ); 410 unionTable.dump( os ); 411 contextTable.dump( os ); 412 #endif 542 543 cerr << "===idTable===" << std::endl; 544 if ( tables ) tables->idTable.dump( os ); 545 cerr << "===typeTable===" << std::endl; 546 if ( tables ) tables->typeTable.dump( os ); 547 cerr << "===structTable===" << std::endl; 548 if ( tables ) tables->structTable.dump( os ); 549 cerr << "===enumTable===" << std::endl; 550 if ( tables ) tables->enumTable.dump( os ); 551 cerr << "===unionTable===" << std::endl; 552 if ( tables ) tables->unionTable.dump( os ); 553 cerr << "===contextTable===" << std::endl; 554 if ( tables ) tables->traitTable.dump( os ); 413 555 } 414 556 } // namespace SymTab -
src/SymTab/Indexer.h
r4e284ea6 re8032b0 21 21 22 22 #include "SynTree/Visitor.h" 23 #include "IdTable.h"24 #include "AggregateTable.h"25 #include "TypeTable.h"26 23 27 24 namespace SymTab { … … 29 26 public: 30 27 Indexer( bool useDebug = false ); 28 29 Indexer( const Indexer &that ); 30 Indexer( Indexer &&that ); 31 31 virtual ~Indexer(); 32 Indexer& operator= ( const Indexer &that ); 33 Indexer& operator= ( Indexer &&that ); 32 34 33 35 //using Visitor::visit; … … 78 80 void leaveScope(); 79 81 80 void lookupId( const std::string &id, std::list< DeclarationWithType* > &) const;81 DeclarationWithType* lookupId( const std::string &id ) const;82 void lookupId( const std::string &id, std::list< DeclarationWithType* > &out ) const; 83 DeclarationWithType* lookupId( const std::string &id ) const; 82 84 NamedTypeDecl *lookupType( const std::string &id ) const; 83 85 StructDecl *lookupStruct( const std::string &id ) const; … … 88 90 void print( std::ostream &os, int indent = 0 ) const; 89 91 private: 90 IdTable idTable; 91 TypeTable typeTable; 92 StructTable structTable; 93 EnumTable enumTable; 94 UnionTable unionTable; 95 TraitTable contextTable; 96 97 bool doDebug; // display debugging trace 92 void addId( DeclarationWithType *decl ); 93 void addType( NamedTypeDecl *decl ); 94 void addStruct( const std::string &id ); 95 void addStruct( StructDecl *decl ); 96 void addEnum( EnumDecl *decl ); 97 void addUnion( const std::string &id ); 98 void addUnion( UnionDecl *decl ); 99 void addTrait( TraitDecl *decl ); 100 101 struct Impl; 102 Impl *tables; ///< Copy-on-write instance of table data structure 103 bool doDebug; ///< Display debugging trace? 104 105 Indexer( Impl *_tables, bool _doDebug ) : tables( _tables ), doDebug( _doDebug ) {} 106 107 /// Takes a new ref to a table (returns null if null) 108 static Impl *newRef( Impl *toClone ); 109 /// Clears a ref to a table (does nothing if null) 110 static void deleteRef( Impl *toFree ); 111 112 /// Ensures that tables variable is writable (i.e. allocated and uniquely owned by this Indexer) 113 void makeWritable(); 98 114 }; 99 115 } // namespace SymTab
Note: See TracChangeset
for help on using the changeset viewer.