Changeset f80e0218 for src/SymTab
- Timestamp:
- Jun 30, 2016, 4:32:56 PM (10 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, stuck-waitfor-destruct, with_gc
- Children:
- ea29e73
- Parents:
- 1b5c81ed (diff), 84d4d6f (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:
-
- 2 added
- 5 edited
-
AddVisit.h (modified) (2 diffs)
-
Autogen.cc (added)
-
Autogen.h (added)
-
Indexer.cc (modified) (25 diffs)
-
Indexer.h (modified) (7 diffs)
-
Validate.cc (modified) (16 diffs)
-
module.mk (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/SymTab/AddVisit.h
r1b5c81ed rf80e0218 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // AddVisit.h -- 7 // AddVisit.h -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Sun May 17 16:14:32 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Thu Apr 7 14:42:21201611 // Last Modified By : Rob Schluntz 12 // Last Modified On : Thu Apr 14 15:52:42 2016 13 13 // Update Count : 5 14 14 // … … 48 48 // maybeAccept( caseStmt->get_condition(), visitor ); 49 49 // } 50 51 template< typename Visitor > 52 void acceptAndAdd( std::list< Declaration * > &translationUnit, Visitor &visitor, bool addBefore ) { 53 std::list< Declaration * >::iterator i = translationUnit.begin(); 54 while ( i != translationUnit.end() ) { 55 (*i)->accept( visitor ); 56 std::list< Declaration * >::iterator next = i; 57 next++; 58 if ( ! visitor.get_declsToAdd().empty() ) { 59 translationUnit.splice( addBefore ? i : next, visitor.get_declsToAdd() ); 60 } // if 61 i = next; 62 } // while 63 } 64 50 65 } // namespace SymTab 51 66 -
src/SymTab/Indexer.cc
r1b5c81ed rf80e0218 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // Indexer.cc -- 7 // Indexer.cc -- 8 8 // 9 9 // Author : Richard C. Bilson 10 10 // Created On : Sun May 17 21:37:33 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Wed Mar 2 17:31:29201611 // Last Modified By : Rob Schluntz 12 // Last Modified On : Fri Apr 22 15:25:43 2016 13 13 // Update Count : 11 14 14 // … … 59 59 } 60 60 } 61 61 62 62 template< typename Decl > 63 63 void dump( const std::unordered_map< std::string, Decl* > &table, std::ostream &os ) { … … 66 66 } // for 67 67 } 68 68 69 69 struct Indexer::Impl { 70 70 Impl( unsigned long _scope ) : refCount(1), scope( _scope ), size( 0 ), base(), … … 76 76 unsigned long size; ///< Number of elements stored in this table 77 77 const Indexer base; ///< Base indexer this extends 78 78 79 79 IdTable idTable; ///< Identifier namespace 80 80 TypeTable typeTable; ///< Type namespace … … 213 213 void Indexer::visit( StructDecl *aggregateDecl ) { 214 214 // make up a forward declaration and add it before processing the members 215 StructDecl fwdDecl( aggregateDecl->get_name() ); 215 // needs to be on the heap because addStruct saves the pointer 216 StructDecl &fwdDecl = *new StructDecl( aggregateDecl->get_name() ); 216 217 cloneAll( aggregateDecl->get_parameters(), fwdDecl.get_parameters() ); 217 218 debugPrint( "Adding fwd decl for struct " << fwdDecl.get_name() << std::endl ); 218 219 addStruct( &fwdDecl ); 219 220 220 221 enterScope(); 221 222 acceptAll( aggregateDecl->get_parameters(), *this ); 222 223 acceptAll( aggregateDecl->get_members(), *this ); 223 224 leaveScope(); 224 225 225 226 debugPrint( "Adding struct " << aggregateDecl->get_name() << std::endl ); 226 227 // this addition replaces the forward declaration … … 234 235 debugPrint( "Adding fwd decl for union " << fwdDecl.get_name() << std::endl ); 235 236 addUnion( &fwdDecl ); 236 237 237 238 enterScope(); 238 239 acceptAll( aggregateDecl->get_parameters(), *this ); 239 240 acceptAll( aggregateDecl->get_members(), *this ); 240 241 leaveScope(); 241 242 242 243 debugPrint( "Adding union " << aggregateDecl->get_name() << std::endl ); 243 244 addUnion( aggregateDecl ); … … 256 257 acceptAll( aggregateDecl->get_members(), *this ); 257 258 leaveScope(); 258 259 259 260 debugPrint( "Adding context " << aggregateDecl->get_name() << std::endl ); 260 261 addTrait( aggregateDecl ); … … 438 439 } 439 440 440 441 441 442 442 443 void Indexer::lookupId( const std::string &id, std::list< DeclarationWithType* > &out ) const { 443 444 std::unordered_set< std::string > foundMangleNames; 444 445 445 446 Indexer::Impl *searchTables = tables; 446 447 while ( searchTables ) { … … 452 453 // mark the mangled name as found, skipping this insertion if a declaration for that name has already been found 453 454 if ( foundMangleNames.insert( decl->first ).second == false ) continue; 454 455 455 456 out.push_back( decl->second ); 456 457 } 457 458 } 458 459 459 460 // get declarations from base indexers 460 461 searchTables = searchTables->base.tables; … … 511 512 } 512 513 513 bool Indexer::hasIncompatibleCDecl( const std::string &id, const std::string &mangleName ) const {514 bool Indexer::hasIncompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const { 514 515 if ( ! tables ) return false; 516 if ( tables->scope < scope ) return false; 515 517 516 518 IdTable::const_iterator decls = tables->idTable.find( id ); … … 518 520 const MangleTable &mangleTable = decls->second; 519 521 for ( MangleTable::const_iterator decl = mangleTable.begin(); decl != mangleTable.end(); ++decl ) { 520 // check for C decls with the same name, skipping 522 // check for C decls with the same name, skipping 521 523 // those with a compatible type (by mangleName) 522 524 if ( decl->second->get_linkage() == LinkageSpec::C && decl->first != mangleName ) return true; … … 524 526 } 525 527 526 return tables->base.hasIncompatibleCDecl( id, mangleName );527 } 528 528 return tables->base.hasIncompatibleCDecl( id, mangleName, scope ); 529 } 530 529 531 NamedTypeDecl *Indexer::lookupTypeAtScope( const std::string &id, unsigned long scope ) const { 530 532 if ( ! tables ) return 0; … … 534 536 return ret != tables->typeTable.end() ? ret->second : tables->base.lookupTypeAtScope( id, scope ); 535 537 } 536 538 537 539 StructDecl *Indexer::lookupStructAtScope( const std::string &id, unsigned long scope ) const { 538 540 if ( ! tables ) return 0; … … 542 544 return ret != tables->structTable.end() ? ret->second : tables->base.lookupStructAtScope( id, scope ); 543 545 } 544 546 545 547 EnumDecl *Indexer::lookupEnumAtScope( const std::string &id, unsigned long scope ) const { 546 548 if ( ! tables ) return 0; … … 550 552 return ret != tables->enumTable.end() ? ret->second : tables->base.lookupEnumAtScope( id, scope ); 551 553 } 552 554 553 555 UnionDecl *Indexer::lookupUnionAtScope( const std::string &id, unsigned long scope ) const { 554 556 if ( ! tables ) return 0; … … 558 560 return ret != tables->unionTable.end() ? ret->second : tables->base.lookupUnionAtScope( id, scope ); 559 561 } 560 562 561 563 TraitDecl *Indexer::lookupTraitAtScope( const std::string &id, unsigned long scope ) const { 562 564 if ( ! tables ) return 0; … … 601 603 return true; 602 604 } 603 605 604 606 void Indexer::addId( DeclarationWithType *decl ) { 605 607 makeWritable(); … … 617 619 DeclarationWithType *existing = lookupIdAtScope( name, mangleName, scope ); 618 620 if ( ! existing || ! addedIdConflicts( existing, decl ) ) { 619 // this ensures that no two declarations with the same unmangled name both have C linkage620 if ( decl->get_linkage() == LinkageSpec::C && hasIncompatibleCDecl( name, mangleName ) ) {621 // this ensures that no two declarations with the same unmangled name at the same scope both have C linkage 622 if ( decl->get_linkage() == LinkageSpec::C && hasIncompatibleCDecl( name, mangleName, scope ) ) { 621 623 throw SemanticError( "invalid overload of C function ", decl ); 622 } // NOTE this is broken in Richard's original code in such a way that it never triggers (it 623 // doesn't check decls that have the same manglename, and all C-linkage decls are defined to 624 } // NOTE this is broken in Richard's original code in such a way that it never triggers (it 625 // doesn't check decls that have the same manglename, and all C-linkage decls are defined to 624 626 // have their name as their manglename, hence the error can never trigger). 625 // The code here is closer to correct, but name mangling would have to be completely 627 // The code here is closer to correct, but name mangling would have to be completely 626 628 // isomorphic to C type-compatibility, which it may not be. 627 629 628 630 tables->idTable[ name ][ mangleName ] = decl; 629 631 ++tables->size; … … 640 642 } 641 643 } 642 644 643 645 void Indexer::addType( NamedTypeDecl *decl ) { 644 646 makeWritable(); … … 671 673 addStruct( new StructDecl( id ) ); 672 674 } 673 675 674 676 void Indexer::addStruct( StructDecl *decl ) { 675 677 makeWritable(); … … 689 691 } 690 692 } 691 693 692 694 void Indexer::addEnum( EnumDecl *decl ) { 693 695 makeWritable(); … … 711 713 addUnion( new UnionDecl( id ) ); 712 714 } 713 715 714 716 void Indexer::addUnion( UnionDecl *decl ) { 715 717 makeWritable(); … … 729 731 } 730 732 } 731 733 732 734 void Indexer::addTrait( TraitDecl *decl ) { 733 735 makeWritable(); … … 750 752 void Indexer::enterScope() { 751 753 ++scope; 752 754 753 755 if ( doDebug ) { 754 756 std::cout << "--- Entering scope " << scope << std::endl; … … 783 785 using std::cerr; 784 786 785 cerr << "===idTable===" << std::endl; 786 if ( tables ) dump( tables->idTable, os ); 787 cerr << "===typeTable===" << std::endl; 788 if ( tables ) dump( tables->typeTable, os ); 789 cerr << "===structTable===" << std::endl; 790 if ( tables ) dump( tables->structTable, os ); 791 cerr << "===enumTable===" << std::endl; 792 if ( tables ) dump( tables->enumTable, os ); 793 cerr << "===unionTable===" << std::endl; 794 if ( tables ) dump( tables->unionTable, os ); 795 cerr << "===contextTable===" << std::endl; 796 if ( tables ) dump( tables->traitTable, os ); 787 if ( tables ) { 788 os << "--- scope " << tables->scope << " ---" << std::endl; 789 790 os << "===idTable===" << std::endl; 791 dump( tables->idTable, os ); 792 os << "===typeTable===" << std::endl; 793 dump( tables->typeTable, os ); 794 os << "===structTable===" << std::endl; 795 dump( tables->structTable, os ); 796 os << "===enumTable===" << std::endl; 797 dump( tables->enumTable, os ); 798 os << "===unionTable===" << std::endl; 799 dump( tables->unionTable, os ); 800 os << "===contextTable===" << std::endl; 801 dump( tables->traitTable, os ); 802 803 tables->base.print( os, indent ); 804 } else { 805 os << "--- end ---" << std::endl; 806 } 807 797 808 } 798 809 } // namespace SymTab -
src/SymTab/Indexer.h
r1b5c81ed rf80e0218 5 5 // file "LICENCE" distributed with Cforall. 6 6 // 7 // Indexer.h -- 7 // Indexer.h -- 8 8 // 9 9 // Author : Richard C. Bilson … … 33 33 Indexer& operator= ( Indexer &&that ); 34 34 35 //using Visitor::visit;35 using Visitor::visit; 36 36 virtual void visit( ObjectDecl *objectDecl ); 37 37 virtual void visit( FunctionDecl *functionDecl ); … … 54 54 virtual void visit( MemberExpr *memberExpr ); 55 55 virtual void visit( VariableExpr *variableExpr ); 56 virtual void visit( ConstantExpr *constantExpr ); 56 virtual void visit( ConstantExpr *constantExpr ); 57 57 virtual void visit( SizeofExpr *sizeofExpr ); 58 58 virtual void visit( AlignofExpr *alignofExpr ); … … 93 93 /// Gets the top-most trait declaration with the given ID 94 94 TraitDecl *lookupTrait( const std::string &id ) const; 95 95 96 96 void print( std::ostream &os, int indent = 0 ) const; 97 97 private: … … 99 99 DeclarationWithType *lookupIdAtScope( const std::string &id, const std::string &mangleName, unsigned long scope ) const; 100 100 /// returns true if there exists a declaration with C linkage and the given name with a different mangled name 101 bool hasIncompatibleCDecl( const std::string &id, const std::string &mangleName ) const;101 bool hasIncompatibleCDecl( const std::string &id, const std::string &mangleName, unsigned long scope ) const; 102 102 // equivalents to lookup functions that only look at tables at scope `scope` (which should be >= tables->scope) 103 103 NamedTypeDecl *lookupTypeAtScope( const std::string &id, unsigned long scope ) const; … … 106 106 UnionDecl *lookupUnionAtScope( const std::string &id, unsigned long scope ) const; 107 107 TraitDecl *lookupTraitAtScope( const std::string &id, unsigned long scope ) const; 108 108 109 109 void addId( DeclarationWithType *decl ); 110 110 void addType( NamedTypeDecl *decl ); … … 115 115 void addUnion( UnionDecl *decl ); 116 116 void addTrait( TraitDecl *decl ); 117 117 118 118 struct Impl; 119 119 Impl *tables; ///< Copy-on-write instance of table data structure -
src/SymTab/Validate.cc
r1b5c81ed rf80e0218 9 9 // Author : Richard C. Bilson 10 10 // Created On : Sun May 17 21:50:04 2015 11 // Last Modified By : Peter A. Buhr12 // Last Modified On : Wed Apr 13 16:39:30201613 // Update Count : 2 5111 // Last Modified By : Rob Schluntz 12 // Last Modified On : Wed May 11 13:17:52 2016 13 // Update Count : 297 14 14 // 15 15 … … 56 56 #include "MakeLibCfa.h" 57 57 #include "TypeEquality.h" 58 #include "Autogen.h" 58 59 #include "ResolvExpr/typeops.h" 59 60 … … 122 123 123 124 const Indexer *indexer; 124 };125 126 class AutogenerateRoutines : public Visitor {127 public:128 /// Generates assignment operators for aggregate types as required129 static void autogenerateRoutines( std::list< Declaration * > &translationUnit );130 131 std::list< Declaration * > &get_declsToAdd() { return declsToAdd; }132 133 virtual void visit( EnumDecl *enumDecl );134 virtual void visit( StructDecl *structDecl );135 virtual void visit( UnionDecl *structDecl );136 virtual void visit( TypeDecl *typeDecl );137 virtual void visit( TraitDecl *ctxDecl );138 virtual void visit( FunctionDecl *functionDecl );139 140 virtual void visit( FunctionType *ftype );141 virtual void visit( PointerType *ftype );142 143 virtual void visit( CompoundStmt *compoundStmt );144 virtual void visit( SwitchStmt *switchStmt );145 virtual void visit( ChooseStmt *chooseStmt );146 // virtual void visit( CaseStmt *caseStmt );147 148 AutogenerateRoutines() : functionNesting( 0 ) {}149 private:150 template< typename StmtClass > void visitStatement( StmtClass *stmt );151 152 std::list< Declaration * > declsToAdd;153 std::set< std::string > structsDone;154 unsigned int functionNesting; // current level of nested functions155 125 }; 156 126 … … 192 162 template<typename AggDecl> 193 163 void addImplicitTypedef( AggDecl * aggDecl ); 194 164 195 165 typedef std::map< std::string, std::pair< TypedefDecl *, int > > TypedefMap; 196 166 TypedefMap typedefNames; 197 167 int scopeLevel; 198 168 }; 169 170 class VerifyCtorDtor : public Visitor { 171 public: 172 /// ensure that constructors and destructors have at least one 173 /// parameter, the first of which must be a pointer, and no 174 /// return values. 175 static void verify( std::list< Declaration * > &translationUnit ); 176 177 virtual void visit( FunctionDecl *funcDecl ); 178 }; 199 179 200 180 class CompoundLiteral : public GenPoly::DeclMutator { … … 217 197 ReturnChecker::checkFunctionReturns( translationUnit ); 218 198 mutateAll( translationUnit, compoundliteral ); 219 AutogenerateRoutines::autogenerateRoutines( translationUnit );199 autogenerateRoutines( translationUnit ); 220 200 acceptAll( translationUnit, pass3 ); 201 VerifyCtorDtor::verify( translationUnit ); 221 202 } 222 203 … … 228 209 type->accept( pass2 ); 229 210 type->accept( pass3 ); 230 }231 232 template< typename Visitor >233 void acceptAndAdd( std::list< Declaration * > &translationUnit, Visitor &visitor, bool addBefore ) {234 std::list< Declaration * >::iterator i = translationUnit.begin();235 while ( i != translationUnit.end() ) {236 (*i)->accept( visitor );237 std::list< Declaration * >::iterator next = i;238 next++;239 if ( ! visitor.get_declsToAdd().empty() ) {240 translationUnit.splice( addBefore ? i : next, visitor.get_declsToAdd() );241 } // if242 i = next;243 } // while244 211 } 245 212 … … 312 279 void Pass1::visit( EnumDecl *enumDecl ) { 313 280 // Set the type of each member of the enumeration to be EnumConstant 314 315 281 for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) { 316 282 ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i ); 317 283 assert( obj ); 318 // obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false, false, false ), enumDecl->get_name() ) ); 319 BasicType * enumType = new BasicType( Type::Qualifiers(), BasicType::SignedInt ); 320 obj->set_type( enumType ) ; 284 obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false, false, false ), enumDecl->get_name() ) ); 321 285 } // for 322 286 Parent::visit( enumDecl ); … … 324 288 325 289 namespace { 326 template< typename DWT Iterator>327 void fixFunctionList( DWT Iterator begin, DWTIterator end, FunctionType *func ) {290 template< typename DWTList > 291 void fixFunctionList( DWTList & dwts, FunctionType * func ) { 328 292 // the only case in which "void" is valid is where it is the only one in the list; then it should be removed 329 293 // entirely other fix ups are handled by the FixFunction class 294 typedef typename DWTList::iterator DWTIterator; 295 DWTIterator begin( dwts.begin() ), end( dwts.end() ); 330 296 if ( begin == end ) return; 331 297 FixFunction fixer; 332 298 DWTIterator i = begin; 333 *i = (*i )->acceptMutator( fixer );299 *i = (*i)->acceptMutator( fixer ); 334 300 if ( fixer.get_isVoid() ) { 335 301 DWTIterator j = i; 336 302 ++i; 337 func->get_parameters().erase( j );303 dwts.erase( j ); 338 304 if ( i != end ) { 339 305 throw SemanticError( "invalid type void in function type ", func ); … … 354 320 void Pass1::visit( FunctionType *func ) { 355 321 // Fix up parameters and return types 356 fixFunctionList( func->get_parameters() .begin(), func->get_parameters().end(), func );357 fixFunctionList( func->get_returnVals() .begin(), func->get_returnVals().end(), func );322 fixFunctionList( func->get_parameters(), func ); 323 fixFunctionList( func->get_returnVals(), func ); 358 324 Visitor::visit( func ); 359 325 } … … 418 384 419 385 void Pass2::visit( StructDecl *structDecl ) { 386 // visit struct members first so that the types of self-referencing members are updated properly 387 Parent::visit( structDecl ); 420 388 if ( ! structDecl->get_members().empty() ) { 421 389 ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() ); … … 427 395 } // if 428 396 } // if 429 Indexer::visit( structDecl );430 397 } 431 398 432 399 void Pass2::visit( UnionDecl *unionDecl ) { 400 Parent::visit( unionDecl ); 433 401 if ( ! unionDecl->get_members().empty() ) { 434 402 ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() ); … … 440 408 } // if 441 409 } // if 442 Indexer::visit( unionDecl );443 410 } 444 411 … … 503 470 } 504 471 505 static const std::list< std::string > noLabels;506 507 void AutogenerateRoutines::autogenerateRoutines( std::list< Declaration * > &translationUnit ) {508 AutogenerateRoutines visitor;509 acceptAndAdd( translationUnit, visitor, false );510 }511 512 template< typename OutputIterator >513 void makeScalarAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, DeclarationWithType *member, OutputIterator out ) {514 ObjectDecl *obj = dynamic_cast<ObjectDecl *>( member );515 // unnamed bit fields are not copied as they cannot be accessed516 if ( obj != NULL && obj->get_name() == "" && obj->get_bitfieldWidth() != NULL ) return;517 518 UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) );519 520 UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );521 derefExpr->get_args().push_back( new VariableExpr( dstParam ) );522 523 // do something special for unnamed members524 Expression *dstselect = new AddressExpr( new MemberExpr( member, derefExpr ) );525 assignExpr->get_args().push_back( dstselect );526 527 Expression *srcselect = new MemberExpr( member, new VariableExpr( srcParam ) );528 assignExpr->get_args().push_back( srcselect );529 530 *out++ = new ExprStmt( noLabels, assignExpr );531 }532 533 template< typename OutputIterator >534 void makeArrayAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, DeclarationWithType *member, ArrayType *array, OutputIterator out ) {535 static UniqueName indexName( "_index" );536 537 // for a flexible array member nothing is done -- user must define own assignment538 if ( ! array->get_dimension() ) return;539 540 ObjectDecl *index = new ObjectDecl( indexName.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), 0 );541 *out++ = new DeclStmt( noLabels, index );542 543 UntypedExpr *init = new UntypedExpr( new NameExpr( "?=?" ) );544 init->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );545 init->get_args().push_back( new NameExpr( "0" ) );546 Statement *initStmt = new ExprStmt( noLabels, init );547 std::list<Statement *> initList;548 initList.push_back( initStmt );549 550 UntypedExpr *cond = new UntypedExpr( new NameExpr( "?<?" ) );551 cond->get_args().push_back( new VariableExpr( index ) );552 cond->get_args().push_back( array->get_dimension()->clone() );553 554 UntypedExpr *inc = new UntypedExpr( new NameExpr( "++?" ) );555 inc->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );556 557 UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) );558 559 UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );560 derefExpr->get_args().push_back( new VariableExpr( dstParam ) );561 562 Expression *dstselect = new MemberExpr( member, derefExpr );563 UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?+?" ) );564 dstIndex->get_args().push_back( dstselect );565 dstIndex->get_args().push_back( new VariableExpr( index ) );566 assignExpr->get_args().push_back( dstIndex );567 568 Expression *srcselect = new MemberExpr( member, new VariableExpr( srcParam ) );569 UntypedExpr *srcIndex = new UntypedExpr( new NameExpr( "?[?]" ) );570 srcIndex->get_args().push_back( srcselect );571 srcIndex->get_args().push_back( new VariableExpr( index ) );572 assignExpr->get_args().push_back( srcIndex );573 574 *out++ = new ForStmt( noLabels, initList, cond, inc, new ExprStmt( noLabels, assignExpr ) );575 }576 577 template< typename OutputIterator >578 void makeUnionFieldsAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, UnionInstType *unionType, OutputIterator out ) {579 UntypedExpr *copy = new UntypedExpr( new NameExpr( "__builtin_memcpy" ) );580 copy->get_args().push_back( new VariableExpr( dstParam ) );581 copy->get_args().push_back( new AddressExpr( new VariableExpr( srcParam ) ) );582 copy->get_args().push_back( new SizeofExpr( unionType ) );583 584 *out++ = new ExprStmt( noLabels, copy );585 }586 587 //E ?=?(E volatile*, int),588 // ?=?(E _Atomic volatile*, int);589 void makeEnumAssignment( EnumDecl *enumDecl, EnumInstType *refType, unsigned int functionNesting, std::list< Declaration * > &declsToAdd ) {590 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );591 592 ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );593 assignType->get_returnVals().push_back( returnVal );594 595 // need two assignment operators with different types596 FunctionType * assignType2 = assignType->clone();597 598 // E ?=?(E volatile *, E)599 Type *etype = refType->clone();600 // etype->get_qualifiers() += Type::Qualifiers(false, true, false, false, false, false);601 602 ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), etype ), 0 );603 assignType->get_parameters().push_back( dstParam );604 605 ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, etype->clone(), 0 );606 assignType->get_parameters().push_back( srcParam );607 608 // E ?=?(E volatile *, int)609 assignType2->get_parameters().push_back( dstParam->clone() );610 BasicType * paramType = new BasicType(Type::Qualifiers(), BasicType::SignedInt);611 ObjectDecl *srcParam2 = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, paramType, 0 );612 assignType2->get_parameters().push_back( srcParam2 );613 614 // Routines at global scope marked "static" to prevent multiple definitions is separate translation units615 // because each unit generates copies of the default routines for each aggregate.616 617 // since there is no definition, these should not be inline618 // make these intrinsic so that the code generator does not make use of them619 FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, assignType, 0, false, false );620 assignDecl->fixUniqueId();621 FunctionDecl *assignDecl2 = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, assignType2, 0, false, false );622 assignDecl2->fixUniqueId();623 624 // these should be built in the same way that the prelude625 // functions are, so build a list containing the prototypes626 // and allow MakeLibCfa to autogenerate the bodies.627 std::list< Declaration * > assigns;628 assigns.push_back( assignDecl );629 assigns.push_back( assignDecl2 );630 631 LibCfa::makeLibCfa( assigns );632 633 // need to remove the prototypes, since this may be nested in a routine634 for (int start = 0, end = assigns.size()/2; start < end; start++) {635 delete assigns.front();636 assigns.pop_front();637 } // for638 639 declsToAdd.insert( declsToAdd.begin(), assigns.begin(), assigns.end() );640 }641 642 /// Clones a reference type, replacing any parameters it may have with a clone of the provided list643 template< typename GenericInstType >644 GenericInstType *cloneWithParams( GenericInstType *refType, const std::list< Expression* >& params ) {645 GenericInstType *clone = refType->clone();646 clone->get_parameters().clear();647 cloneAll( params, clone->get_parameters() );648 return clone;649 }650 651 /// Creates a new type decl that's the same as src, but renamed and with only the ?=? assertion (for complete types only)652 TypeDecl *cloneAndRename( TypeDecl *src, const std::string &name ) {653 TypeDecl *dst = new TypeDecl( name, src->get_storageClass(), 0, src->get_kind() );654 655 if ( src->get_kind() == TypeDecl::Any ) {656 // just include assignment operator assertion657 TypeInstType *assignParamType = new TypeInstType( Type::Qualifiers(), name, dst );658 FunctionType *assignFunctionType = new FunctionType( Type::Qualifiers(), false );659 assignFunctionType->get_returnVals().push_back(660 new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, assignParamType->clone(), 0 ) );661 assignFunctionType->get_parameters().push_back(662 new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), assignParamType->clone() ), 0 ) );663 assignFunctionType->get_parameters().push_back(664 new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, assignParamType, 0 ) );665 FunctionDecl *assignAssert = new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, assignFunctionType, 0, false, false );666 dst->get_assertions().push_back( assignAssert );667 }668 669 return dst;670 }671 672 Declaration *makeStructAssignment( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting ) {673 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );674 675 // Make function polymorphic in same parameters as generic struct, if applicable676 bool isGeneric = false; // NOTE this flag is an incredibly ugly kludge; we should fix the assignment signature instead (ditto for union)677 std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters();678 std::list< Expression* > structParams; // List of matching parameters to put on types679 TypeSubstitution genericSubs; // Substitutions to make to member types of struct680 for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) {681 isGeneric = true;682 TypeDecl *typeParam = cloneAndRename( *param, "_autoassign_" + aggregateDecl->get_name() + "_" + (*param)->get_name() );683 assignType->get_forall().push_back( typeParam );684 TypeInstType *newParamType = new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam );685 genericSubs.add( (*param)->get_name(), newParamType );686 structParams.push_back( new TypeExpr( newParamType ) );687 }688 689 ObjectDecl *returnVal = new ObjectDecl( "_ret", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, structParams ), 0 );690 assignType->get_returnVals().push_back( returnVal );691 692 ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), cloneWithParams( refType, structParams ) ), 0 );693 assignType->get_parameters().push_back( dstParam );694 695 ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, structParams ), 0 );696 assignType->get_parameters().push_back( srcParam );697 698 // Routines at global scope marked "static" to prevent multiple definitions is separate translation units699 // because each unit generates copies of the default routines for each aggregate.700 FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false );701 assignDecl->fixUniqueId();702 703 for ( std::list< Declaration * >::const_iterator member = aggregateDecl->get_members().begin(); member != aggregateDecl->get_members().end(); ++member ) {704 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member ) ) {705 // query the type qualifiers of this field and skip assigning it if it is marked const.706 // If it is an array type, we need to strip off the array layers to find its qualifiers.707 Type * type = dwt->get_type();708 while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {709 type = at->get_base();710 }711 712 if ( type->get_qualifiers().isConst ) {713 // don't assign const members714 continue;715 }716 717 if ( isGeneric ) {718 // rewrite member type in terms of the type variables on this operator719 DeclarationWithType *fixedMember = dwt->clone();720 genericSubs.apply( fixedMember );721 722 // assign to both destination and return value723 if ( ArrayType *array = dynamic_cast< ArrayType * >( fixedMember->get_type() ) ) {724 makeArrayAssignment( srcParam, dstParam, fixedMember, array, back_inserter( assignDecl->get_statements()->get_kids() ) );725 makeArrayAssignment( srcParam, returnVal, fixedMember, array, back_inserter( assignDecl->get_statements()->get_kids() ) );726 } else {727 makeScalarAssignment( srcParam, dstParam, fixedMember, back_inserter( assignDecl->get_statements()->get_kids() ) );728 makeScalarAssignment( srcParam, returnVal, fixedMember, back_inserter( assignDecl->get_statements()->get_kids() ) );729 } // if730 } else {731 // assign to destination732 if ( ArrayType *array = dynamic_cast< ArrayType * >( dwt->get_type() ) ) {733 makeArrayAssignment( srcParam, dstParam, dwt, array, back_inserter( assignDecl->get_statements()->get_kids() ) );734 } else {735 makeScalarAssignment( srcParam, dstParam, dwt, back_inserter( assignDecl->get_statements()->get_kids() ) );736 } // if737 } // if738 } // if739 } // for740 if ( ! isGeneric ) assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );741 742 return assignDecl;743 }744 745 Declaration *makeUnionAssignment( UnionDecl *aggregateDecl, UnionInstType *refType, unsigned int functionNesting ) {746 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );747 748 // Make function polymorphic in same parameters as generic union, if applicable749 bool isGeneric = false; // NOTE this flag is an incredibly ugly kludge; we should fix the assignment signature instead (ditto for struct)750 std::list< TypeDecl* >& genericParams = aggregateDecl->get_parameters();751 std::list< Expression* > unionParams; // List of matching parameters to put on types752 for ( std::list< TypeDecl* >::const_iterator param = genericParams.begin(); param != genericParams.end(); ++param ) {753 isGeneric = true;754 TypeDecl *typeParam = cloneAndRename( *param, "_autoassign_" + aggregateDecl->get_name() + "_" + (*param)->get_name() );755 assignType->get_forall().push_back( typeParam );756 unionParams.push_back( new TypeExpr( new TypeInstType( Type::Qualifiers(), typeParam->get_name(), typeParam ) ) );757 }758 759 ObjectDecl *returnVal = new ObjectDecl( "_ret", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, unionParams ), 0 );760 assignType->get_returnVals().push_back( returnVal );761 762 ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), cloneWithParams( refType, unionParams ) ), 0 );763 assignType->get_parameters().push_back( dstParam );764 765 ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, cloneWithParams( refType, unionParams ), 0 );766 assignType->get_parameters().push_back( srcParam );767 768 // Routines at global scope marked "static" to prevent multiple definitions is separate translation units769 // because each unit generates copies of the default routines for each aggregate.770 FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false );771 assignDecl->fixUniqueId();772 773 makeUnionFieldsAssignment( srcParam, dstParam, cloneWithParams( refType, unionParams ), back_inserter( assignDecl->get_statements()->get_kids() ) );774 if ( isGeneric ) makeUnionFieldsAssignment( srcParam, returnVal, cloneWithParams( refType, unionParams ), back_inserter( assignDecl->get_statements()->get_kids() ) );775 776 if ( ! isGeneric ) assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );777 778 return assignDecl;779 }780 781 void AutogenerateRoutines::visit( EnumDecl *enumDecl ) {782 if ( ! enumDecl->get_members().empty() ) {783 EnumInstType *enumInst = new EnumInstType( Type::Qualifiers(), enumDecl->get_name() );784 // enumInst->set_baseEnum( enumDecl );785 // declsToAdd.push_back(786 makeEnumAssignment( enumDecl, enumInst, functionNesting, declsToAdd );787 }788 }789 790 void AutogenerateRoutines::visit( StructDecl *structDecl ) {791 if ( ! structDecl->get_members().empty() && structsDone.find( structDecl->get_name() ) == structsDone.end() ) {792 StructInstType structInst( Type::Qualifiers(), structDecl->get_name() );793 structInst.set_baseStruct( structDecl );794 declsToAdd.push_back( makeStructAssignment( structDecl, &structInst, functionNesting ) );795 structsDone.insert( structDecl->get_name() );796 } // if797 }798 799 void AutogenerateRoutines::visit( UnionDecl *unionDecl ) {800 if ( ! unionDecl->get_members().empty() ) {801 UnionInstType unionInst( Type::Qualifiers(), unionDecl->get_name() );802 unionInst.set_baseUnion( unionDecl );803 declsToAdd.push_back( makeUnionAssignment( unionDecl, &unionInst, functionNesting ) );804 } // if805 }806 807 void AutogenerateRoutines::visit( TypeDecl *typeDecl ) {808 CompoundStmt *stmts = 0;809 TypeInstType *typeInst = new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), false );810 typeInst->set_baseType( typeDecl );811 ObjectDecl *src = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst->clone(), 0 );812 ObjectDecl *dst = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), typeInst->clone() ), 0 );813 if ( typeDecl->get_base() ) {814 stmts = new CompoundStmt( std::list< Label >() );815 UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );816 assign->get_args().push_back( new CastExpr( new VariableExpr( dst ), new PointerType( Type::Qualifiers(), typeDecl->get_base()->clone() ) ) );817 assign->get_args().push_back( new CastExpr( new VariableExpr( src ), typeDecl->get_base()->clone() ) );818 stmts->get_kids().push_back( new ReturnStmt( std::list< Label >(), assign ) );819 } // if820 FunctionType *type = new FunctionType( Type::Qualifiers(), false );821 type->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst, 0 ) );822 type->get_parameters().push_back( dst );823 type->get_parameters().push_back( src );824 FunctionDecl *func = new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::AutoGen, type, stmts, false, false );825 declsToAdd.push_back( func );826 }827 828 void addDecls( std::list< Declaration * > &declsToAdd, std::list< Statement * > &statements, std::list< Statement * >::iterator i ) {829 for ( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) {830 statements.insert( i, new DeclStmt( noLabels, *decl ) );831 } // for832 declsToAdd.clear();833 }834 835 void AutogenerateRoutines::visit( FunctionType *) {836 // ensure that we don't add assignment ops for types defined as part of the function837 }838 839 void AutogenerateRoutines::visit( PointerType *) {840 // ensure that we don't add assignment ops for types defined as part of the pointer841 }842 843 void AutogenerateRoutines::visit( TraitDecl *) {844 // ensure that we don't add assignment ops for types defined as part of the context845 }846 847 template< typename StmtClass >848 inline void AutogenerateRoutines::visitStatement( StmtClass *stmt ) {849 std::set< std::string > oldStructs = structsDone;850 addVisit( stmt, *this );851 structsDone = oldStructs;852 }853 854 void AutogenerateRoutines::visit( FunctionDecl *functionDecl ) {855 maybeAccept( functionDecl->get_functionType(), *this );856 acceptAll( functionDecl->get_oldDecls(), *this );857 functionNesting += 1;858 maybeAccept( functionDecl->get_statements(), *this );859 functionNesting -= 1;860 }861 862 void AutogenerateRoutines::visit( CompoundStmt *compoundStmt ) {863 visitStatement( compoundStmt );864 }865 866 void AutogenerateRoutines::visit( SwitchStmt *switchStmt ) {867 visitStatement( switchStmt );868 }869 870 void AutogenerateRoutines::visit( ChooseStmt *switchStmt ) {871 visitStatement( switchStmt );872 }873 874 // void AutogenerateRoutines::visit( CaseStmt *caseStmt ) {875 // visitStatement( caseStmt );876 // }877 878 472 void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) { 879 473 ReturnChecker checker; … … 889 483 890 484 void ReturnChecker::visit( ReturnStmt * returnStmt ) { 485 // Previously this also checked for the existence of an expr paired with no return values on 486 // the function return type. This is incorrect, since you can have an expression attached to 487 // a return statement in a void-returning function in C. The expression is treated as if it 488 // were cast to void. 891 489 if ( returnStmt->get_expr() == NULL && returnVals.size() != 0 ) { 892 490 throw SemanticError( "Non-void function returns no values: " , returnStmt ); 893 } else if ( returnStmt->get_expr() != NULL && returnVals.size() == 0 ) {894 throw SemanticError( "void function returns values: " , returnStmt );895 491 } 896 492 } … … 1033 629 return aggDecl; 1034 630 } 1035 631 1036 632 template<typename AggDecl> 1037 633 void EliminateTypedef::addImplicitTypedef( AggDecl * aggDecl ) { … … 1072 668 } 1073 669 670 void VerifyCtorDtor::verify( std::list< Declaration * > & translationUnit ) { 671 VerifyCtorDtor verifier; 672 acceptAll( translationUnit, verifier ); 673 } 674 675 void VerifyCtorDtor::visit( FunctionDecl * funcDecl ) { 676 FunctionType * funcType = funcDecl->get_functionType(); 677 std::list< DeclarationWithType * > &returnVals = funcType->get_returnVals(); 678 std::list< DeclarationWithType * > ¶ms = funcType->get_parameters(); 679 680 if ( funcDecl->get_name() == "?{}" || funcDecl->get_name() == "^?{}" ) { 681 if ( params.size() == 0 ) { 682 throw SemanticError( "Constructors and destructors require at least one parameter ", funcDecl ); 683 } 684 if ( ! dynamic_cast< PointerType * >( params.front()->get_type() ) ) { 685 throw SemanticError( "First parameter of a constructor or destructor must be a pointer ", funcDecl ); 686 } 687 if ( returnVals.size() != 0 ) { 688 throw SemanticError( "Constructors and destructors cannot have explicit return values ", funcDecl ); 689 } 690 } 691 692 Visitor::visit( funcDecl ); 693 // original idea: modify signature of ctor/dtors and insert appropriate return statements 694 // to cause desired behaviour 695 // new idea: add comma exprs to every ctor call to produce first parameter. 696 // this requires some memoization of the first parameter, because it can be a 697 // complicated expression with side effects (see: malloc). idea: add temporary variable 698 // that is assigned address of constructed object in ctor argument position and 699 // return the temporary. It should also be done after all implicit ctors are 700 // added, so not in this pass! 701 } 702 1074 703 DeclarationWithType * CompoundLiteral::mutate( ObjectDecl *objectDecl ) { 1075 704 storageclass = objectDecl->get_storageClass(); -
src/SymTab/module.mk
r1b5c81ed rf80e0218 6 6 ## file "LICENCE" distributed with Cforall. 7 7 ## 8 ## module.mk -- 8 ## module.mk -- 9 9 ## 10 10 ## Author : Richard C. Bilson … … 20 20 SymTab/FixFunction.cc \ 21 21 SymTab/ImplementationType.cc \ 22 SymTab/TypeEquality.cc 22 SymTab/TypeEquality.cc \ 23 SymTab/Autogen.cc
Note:
See TracChangeset
for help on using the changeset viewer.