Changeset c13e8dc8 for src/SymTab
- Timestamp:
- Dec 5, 2017, 2:35:03 PM (7 years ago)
- Branches:
- ADT, arm-eh, ast-experimental, cleanup-dtors, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, pthread-emulation, qualifiedEnum
- Children:
- f9feab8
- Parents:
- 9c35431 (diff), 65197c2 (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. - Location:
- src/SymTab
- Files:
-
- 4 deleted
- 7 edited
Legend:
- Unmodified
- Added
- Removed
-
src/SymTab/AddVisit.h
r9c35431 rc13e8dc8 24 24 // add any new declarations after the previous statement 25 25 for ( std::list< Declaration* >::iterator decl = visitor.declsToAddAfter.begin(); decl != visitor.declsToAddAfter.end(); ++decl ) { 26 DeclStmt *declStmt = new DeclStmt( noLabels,*decl );26 DeclStmt *declStmt = new DeclStmt( *decl ); 27 27 stmts.insert( stmt, declStmt ); 28 28 } … … 36 36 // add any new declarations before the statement 37 37 for ( std::list< Declaration* >::iterator decl = visitor.declsToAdd.begin(); decl != visitor.declsToAdd.end(); ++decl ) { 38 DeclStmt *declStmt = new DeclStmt( noLabels,*decl );38 DeclStmt *declStmt = new DeclStmt( *decl ); 39 39 stmts.insert( stmt, declStmt ); 40 40 } -
src/SymTab/Autogen.cc
r9c35431 rc13e8dc8 267 267 Type::StorageClasses scs = functionNesting > 0 ? Type::StorageClasses() : Type::StorageClasses( Type::Static ); 268 268 LinkageSpec::Spec spec = isIntrinsic ? LinkageSpec::Intrinsic : LinkageSpec::AutoGen; 269 FunctionDecl * decl = new FunctionDecl( fname, scs, spec, ftype, new CompoundStmt( noLabels),269 FunctionDecl * decl = new FunctionDecl( fname, scs, spec, ftype, new CompoundStmt(), 270 270 std::list< Attribute * >(), Type::FuncSpecifiers( Type::Inline ) ); 271 271 decl->fixUniqueId(); … … 302 302 assert( assignType->returnVals.size() == 1 ); 303 303 ObjectDecl * dstParam = strict_dynamic_cast< ObjectDecl * >( assignType->parameters.front() ); 304 dcl->statements->push_back( new ReturnStmt( n oLabels, new VariableExpr( dstParam ) ) );304 dcl->statements->push_back( new ReturnStmt( new VariableExpr( dstParam ) ) ); 305 305 } 306 306 resolve( dcl ); … … 471 471 copy->args.push_back( new AddressExpr( new VariableExpr( srcParam ) ) ); 472 472 copy->args.push_back( new SizeofExpr( srcParam->get_type()->clone() ) ); 473 *out++ = new ExprStmt( noLabels,copy );473 *out++ = new ExprStmt( copy ); 474 474 } 475 475 … … 547 547 callExpr->get_args().push_back( new VariableExpr( dstParam ) ); 548 548 callExpr->get_args().push_back( new VariableExpr( srcParam ) ); 549 funcDecl->statements->push_back( new ExprStmt( noLabels,callExpr ) );549 funcDecl->statements->push_back( new ExprStmt( callExpr ) ); 550 550 } else { 551 551 // default ctor/dtor body is empty - add unused attribute to parameter to silence warnings … … 572 572 expr->args.push_back( new CastExpr( new VariableExpr( dst ), new ReferenceType( Type::Qualifiers(), typeDecl->base->clone() ) ) ); 573 573 if ( src ) expr->args.push_back( new CastExpr( new VariableExpr( src ), typeDecl->base->clone() ) ); 574 dcl->statements->kids.push_back( new ExprStmt( noLabels,expr ) );574 dcl->statements->kids.push_back( new ExprStmt( expr ) ); 575 575 }; 576 576 … … 667 667 untyped->get_args().push_back( new VariableExpr( ftype->get_parameters().back() ) ); 668 668 } 669 function->get_statements()->get_kids().push_back( new ExprStmt( noLabels,untyped ) );670 function->get_statements()->get_kids().push_back( new ReturnStmt( noLabels,UntypedExpr::createDeref( new VariableExpr( ftype->get_parameters().front() ) ) ) );669 function->get_statements()->get_kids().push_back( new ExprStmt( untyped ) ); 670 function->get_statements()->get_kids().push_back( new ReturnStmt( UntypedExpr::createDeref( new VariableExpr( ftype->get_parameters().front() ) ) ) ); 671 671 } 672 672 -
src/SymTab/Autogen.h
r9c35431 rc13e8dc8 107 107 fExpr->args.splice( fExpr->args.end(), args ); 108 108 109 *out++ = new ExprStmt( noLabels,fExpr );109 *out++ = new ExprStmt( fExpr ); 110 110 111 111 srcParam.clearArrayIndices(); … … 165 165 166 166 // for stmt's body, eventually containing call 167 CompoundStmt * body = new CompoundStmt( noLabels);167 CompoundStmt * body = new CompoundStmt(); 168 168 Statement * listInit = genCall( srcParam, dstParam, fname, back_inserter( body->kids ), array->base, addCast, forward ); 169 169 170 170 // block containing for stmt and index variable 171 171 std::list<Statement *> initList; 172 CompoundStmt * block = new CompoundStmt( noLabels);173 block->push_back( new DeclStmt( noLabels,index ) );172 CompoundStmt * block = new CompoundStmt(); 173 block->push_back( new DeclStmt( index ) ); 174 174 if ( listInit ) block->get_kids().push_back( listInit ); 175 block->push_back( new ForStmt( noLabels,initList, cond, inc, body ) );175 block->push_back( new ForStmt( initList, cond, inc, body ) ); 176 176 177 177 *out++ = block; -
src/SymTab/Indexer.cc
r9c35431 rc13e8dc8 40 40 41 41 namespace SymTab { 42 typedef std::unordered_map< std::string, DeclarationWithType* > MangleTable; 42 std::ostream & operator<<( std::ostream & out, const Indexer::IdData & data ) { 43 return out << "(" << data.id << "," << data.baseExpr << ")"; 44 } 45 46 typedef std::unordered_map< std::string, Indexer::IdData > MangleTable; 43 47 typedef std::unordered_map< std::string, MangleTable > IdTable; 44 48 typedef std::unordered_map< std::string, NamedTypeDecl* > TypeTable; … … 97 101 } 98 102 99 void Indexer::removeSpecialOverrides( const std::string &id, std::list< DeclarationWithType *> & out ) const {103 void Indexer::removeSpecialOverrides( const std::string &id, std::list< IdData > & out ) const { 100 104 // only need to perform this step for constructors, destructors, and assignment functions 101 105 if ( ! CodeGen::isCtorDtorAssign( id ) ) return; … … 104 108 struct ValueType { 105 109 struct DeclBall { 106 FunctionDecl *decl;110 IdData decl; 107 111 bool isUserDefinedFunc; // properties for this particular decl 108 112 bool isDefaultCtor; … … 120 124 // another FunctionDecl for the current type was found - determine 121 125 // if it has special properties and update data structure accordingly 122 ValueType & operator+=( FunctionDecl * function ) { 126 ValueType & operator+=( IdData data ) { 127 DeclarationWithType * function = data.id; 123 128 bool isUserDefinedFunc = ! LinkageSpec::isOverridable( function->get_linkage() ); 124 129 bool isDefaultCtor = InitTweak::isDefaultConstructor( function ); 125 130 bool isDtor = InitTweak::isDestructor( function ); 126 131 bool isCopyFunc = InitTweak::isCopyFunction( function, function->get_name() ); 127 decls.push_back( DeclBall{ function, isUserDefinedFunc, isDefaultCtor, isDtor, isCopyFunc } );132 decls.push_back( DeclBall{ data, isUserDefinedFunc, isDefaultCtor, isDtor, isCopyFunc } ); 128 133 existsUserDefinedFunc = existsUserDefinedFunc || isUserDefinedFunc; 129 134 existsUserDefinedCtor = existsUserDefinedCtor || (isUserDefinedFunc && CodeGen::isConstructor( function->get_name() ) ); … … 135 140 }; // ValueType 136 141 137 std::list< DeclarationWithType *> copy;142 std::list< IdData > copy; 138 143 copy.splice( copy.end(), out ); 139 144 140 145 // organize discovered declarations by type 141 146 std::unordered_map< std::string, ValueType > funcMap; 142 for ( DeclarationWithType *decl : copy ) {143 if ( FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl ) ) {147 for ( auto decl : copy ) { 148 if ( FunctionDecl * function = dynamic_cast< FunctionDecl * >( decl.id ) ) { 144 149 std::list< DeclarationWithType * > & params = function->get_functionType()->get_parameters(); 145 150 assert( ! params.empty() ); … … 147 152 Type * base = InitTweak::getPointerBase( params.front()->get_type() ); 148 153 assert( base ); 149 funcMap[ Mangler::mangle( base ) ] += function;154 funcMap[ Mangler::mangle( base ) ] += decl; 150 155 } else { 151 156 out.push_back( decl ); … … 164 169 bool noUserDefinedFunc = ! val.existsUserDefinedFunc; 165 170 bool isUserDefinedFunc = ball.isUserDefinedFunc; 166 bool isAcceptableDefaultCtor = (! val.existsUserDefinedCtor || (! val.existsUserDefinedDefaultCtor && ball.decl ->get_linkage() == LinkageSpec::Intrinsic)) && ball.isDefaultCtor; // allow default constructors only when no user-defined constructors exist, except in the case of intrinsics, which require exact overrides171 bool isAcceptableDefaultCtor = (! val.existsUserDefinedCtor || (! val.existsUserDefinedDefaultCtor && ball.decl.id->get_linkage() == LinkageSpec::Intrinsic)) && ball.isDefaultCtor; // allow default constructors only when no user-defined constructors exist, except in the case of intrinsics, which require exact overrides 167 172 bool isAcceptableCopyFunc = ! val.existsUserDefinedCopyFunc && ball.isCopyFunc; // handles copy ctor and assignment operator 168 173 bool isAcceptableDtor = ! val.existsUserDefinedDtor && ball.isDtor; … … 219 224 } 220 225 221 void Indexer::lookupId( const std::string &id, std::list< DeclarationWithType*> &out ) const {226 void Indexer::lookupId( const std::string &id, std::list< IdData > &out ) const { 222 227 std::unordered_set< std::string > foundMangleNames; 223 228 … … 289 294 const MangleTable &mangleTable = decls->second; 290 295 MangleTable::const_iterator decl = mangleTable.find( mangleName ); 291 if ( decl != mangleTable.end() ) return decl->second ;296 if ( decl != mangleTable.end() ) return decl->second.id; 292 297 } 293 298 … … 304 309 for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { 305 310 // check for C decls with the same name, skipping those with a compatible type (by mangleName) 306 if ( ! LinkageSpec::isMangled( decl->second ->get_linkage() ) && decl->first != mangleName ) return true;311 if ( ! LinkageSpec::isMangled( decl->second.id->get_linkage() ) && decl->first != mangleName ) return true; 307 312 } 308 313 } … … 321 326 // check for C decls with the same name, skipping 322 327 // those with an incompatible type (by mangleName) 323 if ( ! LinkageSpec::isMangled( decl->second ->get_linkage() ) && decl->first == mangleName ) return true;328 if ( ! LinkageSpec::isMangled( decl->second.id->get_linkage() ) && decl->first == mangleName ) return true; 324 329 } 325 330 } … … 403 408 } 404 409 405 void Indexer::addId( DeclarationWithType *decl ) {410 void Indexer::addId( DeclarationWithType *decl, Expression * baseExpr ) { 406 411 debugPrint( "Adding Id " << decl->name << std::endl ); 407 412 makeWritable(); … … 439 444 440 445 // add to indexer 441 tables->idTable[ name ][ mangleName ] = decl;446 tables->idTable[ name ][ mangleName ] = { decl, baseExpr }; 442 447 ++tables->size; 443 448 } … … 563 568 if ( ! addedDeclConflicts( existing->second, decl ) ) { 564 569 existing->second = decl; 570 } 571 } 572 } 573 574 void Indexer::addWith( WithStmt * stmt ) { 575 for ( Expression * expr : stmt->exprs ) { 576 if ( expr->result ) { 577 AggregateDecl * aggr = expr->result->stripReferences()->getAggr(); 578 assertf( aggr, "WithStmt expr has non-aggregate type: %s", toString( expr->result ).c_str() ); 579 580 for ( Declaration * decl : aggr->members ) { 581 if ( DeclarationWithType * dwt = dynamic_cast< DeclarationWithType * >( decl ) ) { 582 addId( dwt, expr ); 583 } 584 } 565 585 } 566 586 } … … 645 665 646 666 } 667 668 Expression * Indexer::IdData::combine() const { 669 if ( baseExpr ) { 670 Expression * base = baseExpr->clone(); 671 ResolvExpr::referenceToRvalueConversion( base ); 672 Expression * ret = new MemberExpr( id, base ); 673 // xxx - this introduces hidden environments, for now remove them. 674 // std::swap( base->env, ret->env ); 675 delete base->env; 676 base->env = nullptr; 677 return ret; 678 } else { 679 return new VariableExpr( id ); 680 } 681 } 647 682 } // namespace SymTab 648 683 -
src/SymTab/Indexer.h
r9c35431 rc13e8dc8 39 39 void leaveScope(); 40 40 41 struct IdData { 42 DeclarationWithType * id; 43 Expression * baseExpr; // WithExpr 44 45 Expression * combine() const; 46 }; 47 41 48 /// Gets all declarations with the given ID 42 void lookupId( const std::string &id, std::list< DeclarationWithType*> &out ) const;49 void lookupId( const std::string &id, std::list< IdData > &out ) const; 43 50 /// Gets the top-most type declaration with the given ID 44 51 NamedTypeDecl *lookupType( const std::string &id ) const; … … 67 74 TraitDecl *lookupTraitAtScope( const std::string &id, unsigned long scope ) const; 68 75 69 void addId( DeclarationWithType *decl );76 void addId( DeclarationWithType *decl, Expression * baseExpr = nullptr ); 70 77 void addType( NamedTypeDecl *decl ); 71 78 void addStruct( const std::string &id ); … … 75 82 void addUnion( UnionDecl *decl ); 76 83 void addTrait( TraitDecl *decl ); 84 85 /// adds all of the IDs from WithStmt exprs 86 void addWith( WithStmt * ); 77 87 78 88 /// convenience function for adding a list of Ids to the indexer … … 100 110 // so that they will not be selected 101 111 // void removeSpecialOverrides( FunctionDecl *decl ); 102 void removeSpecialOverrides( const std::string &id, std::list< DeclarationWithType *> & out ) const;112 void removeSpecialOverrides( const std::string &id, std::list< IdData > & out ) const; 103 113 104 114 /// Ensures that tables variable is writable (i.e. allocated, uniquely owned by this Indexer, and at the current scope) -
src/SymTab/Validate.cc
r9c35431 rc13e8dc8 81 81 82 82 namespace SymTab { 83 class HoistStruct final : public Visitor { 84 template< typename Visitor > 85 friend void acceptAndAdd( std::list< Declaration * > &translationUnit, Visitor &visitor ); 86 template< typename Visitor > 87 friend void addVisitStatementList( std::list< Statement* > &stmts, Visitor &visitor ); 88 public: 83 struct HoistStruct final : public WithDeclsToAdd, public WithGuards { 89 84 /// Flattens nested struct types 90 85 static void hoistStruct( std::list< Declaration * > &translationUnit ); 91 86 92 std::list< Declaration * > &get_declsToAdd() { return declsToAdd; } 93 94 virtual void visit( EnumInstType *enumInstType ); 95 virtual void visit( StructInstType *structInstType ); 96 virtual void visit( UnionInstType *unionInstType ); 97 virtual void visit( StructDecl *aggregateDecl ); 98 virtual void visit( UnionDecl *aggregateDecl ); 99 100 virtual void visit( CompoundStmt *compoundStmt ); 101 virtual void visit( SwitchStmt *switchStmt ); 87 void previsit( EnumInstType * enumInstType ); 88 void previsit( StructInstType * structInstType ); 89 void previsit( UnionInstType * unionInstType ); 90 void previsit( StructDecl * aggregateDecl ); 91 void previsit( UnionDecl * aggregateDecl ); 92 102 93 private: 103 HoistStruct();104 105 94 template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl ); 106 95 107 std::list< Declaration * > declsToAdd, declsToAddAfter; 108 bool inStruct; 96 bool inStruct = false; 109 97 }; 110 98 … … 305 293 306 294 void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) { 307 HoistStruct hoister; 308 acceptAndAdd( translationUnit, hoister ); 309 } 310 311 HoistStruct::HoistStruct() : inStruct( false ) { 295 PassVisitor<HoistStruct> hoister; 296 acceptAll( translationUnit, hoister ); 312 297 } 313 298 … … 320 305 if ( inStruct ) { 321 306 // Add elements in stack order corresponding to nesting structure. 322 declsToAdd.push_front( aggregateDecl ); 323 Visitor::visit( aggregateDecl ); 307 declsToAddBefore.push_front( aggregateDecl ); 324 308 } else { 309 GuardValue( inStruct ); 325 310 inStruct = true; 326 Visitor::visit( aggregateDecl );327 inStruct = false;328 311 } // if 329 312 // Always remove the hoisted aggregate from the inner structure. 330 filter( aggregateDecl->get_members(), isStructOrUnion, false);331 } 332 333 void HoistStruct:: visit( EnumInstType *structInstType) {334 if ( structInstType->get_baseEnum()) {335 declsToAdd .push_front( structInstType->get_baseEnum());336 } 337 } 338 339 void HoistStruct:: visit( StructInstType *structInstType) {340 if ( structInstType->get_baseStruct()) {341 declsToAdd .push_front( structInstType->get_baseStruct());342 } 343 } 344 345 void HoistStruct:: visit( UnionInstType *structInstType) {346 if ( structInstType->get_baseUnion()) {347 declsToAdd .push_front( structInstType->get_baseUnion());348 } 349 } 350 351 void HoistStruct:: visit( StructDecl *aggregateDecl ) {313 GuardAction( [aggregateDecl]() { filter( aggregateDecl->members, isStructOrUnion, false ); } ); 314 } 315 316 void HoistStruct::previsit( EnumInstType * inst ) { 317 if ( inst->baseEnum ) { 318 declsToAddBefore.push_front( inst->baseEnum ); 319 } 320 } 321 322 void HoistStruct::previsit( StructInstType * inst ) { 323 if ( inst->baseStruct ) { 324 declsToAddBefore.push_front( inst->baseStruct ); 325 } 326 } 327 328 void HoistStruct::previsit( UnionInstType * inst ) { 329 if ( inst->baseUnion ) { 330 declsToAddBefore.push_front( inst->baseUnion ); 331 } 332 } 333 334 void HoistStruct::previsit( StructDecl * aggregateDecl ) { 352 335 handleAggregate( aggregateDecl ); 353 336 } 354 337 355 void HoistStruct:: visit( UnionDecl *aggregateDecl ) {338 void HoistStruct::previsit( UnionDecl * aggregateDecl ) { 356 339 handleAggregate( aggregateDecl ); 357 }358 359 void HoistStruct::visit( CompoundStmt *compoundStmt ) {360 addVisit( compoundStmt, *this );361 }362 363 void HoistStruct::visit( SwitchStmt *switchStmt ) {364 addVisit( switchStmt, *this );365 340 } 366 341 -
src/SymTab/module.mk
r9c35431 rc13e8dc8 19 19 SymTab/Validate.cc \ 20 20 SymTab/FixFunction.cc \ 21 SymTab/ImplementationType.cc \22 SymTab/TypeEquality.cc \23 21 SymTab/Autogen.cc
Note: See TracChangeset
for help on using the changeset viewer.