Changes in src/SymTab/Validate.cc [85c4ef0:82dd287]
- File:
-
- 1 edited
-
src/SymTab/Validate.cc (modified) (21 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/SymTab/Validate.cc
r85c4ef0 r82dd287 10 10 // Created On : Sun May 17 21:50:04 2015 11 11 // Last Modified By : Rob Schluntz 12 // Last Modified On : Mon Jul 13 14:38:19201513 // Update Count : 1 8412 // Last Modified On : Tue Jul 07 10:41:23 2015 13 // Update Count : 136 14 14 // 15 15 … … 48 48 #include "Indexer.h" 49 49 #include "FixFunction.h" 50 //#include "ImplementationType.h"50 #include "ImplementationType.h" 51 51 #include "utility.h" 52 52 #include "UniqueName.h" 53 53 #include "AddVisit.h" 54 54 #include "MakeLibCfa.h" 55 #include "TypeEquality.h" 55 56 56 57 57 #define debugPrint( x ) if ( doDebug ) { std::cout << x; } … … 60 60 class HoistStruct : public Visitor { 61 61 public: 62 /// Flattens nested struct types 62 63 static void hoistStruct( std::list< Declaration * > &translationUnit ); 63 64 … … 84 85 }; 85 86 87 /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers 86 88 class Pass1 : public Visitor { 87 89 typedef Visitor Parent; … … 89 91 virtual void visit( FunctionType *func ); 90 92 }; 91 93 94 /// Associates forward declarations of aggregates with their definitions 92 95 class Pass2 : public Indexer { 93 96 typedef Indexer Parent; … … 110 113 }; 111 114 115 /// Replaces array and function types in forall lists by appropriate pointer type 112 116 class Pass3 : public Indexer { 113 117 typedef Indexer Parent; … … 123 127 class AddStructAssignment : public Visitor { 124 128 public: 129 /// Generates assignment operators for aggregate types as required 125 130 static void addStructAssignment( std::list< Declaration * > &translationUnit ); 126 131 … … 157 162 class EliminateTypedef : public Mutator { 158 163 public: 159 EliminateTypedef() : scopeLevel( 0 ) {}160 164 static void eliminateTypedef( std::list< Declaration * > &translationUnit ); 161 165 private: … … 167 171 virtual Type *mutate( TypeInstType *aggregateUseType ); 168 172 virtual Expression *mutate( CastExpr *castExpr ); 169 170 virtual Declaration *mutate( StructDecl * structDecl ); 171 virtual Declaration *mutate( UnionDecl * unionDecl ); 172 virtual Declaration *mutate( EnumDecl * enumDecl ); 173 virtual Declaration *mutate( ContextDecl * contextDecl ); 174 175 template<typename AggDecl> 176 AggDecl *handleAggregate( AggDecl * aggDecl ); 177 178 typedef std::map< std::string, std::pair< TypedefDecl *, int > > TypedefMap; 179 TypedefMap typedefNames; 180 int scopeLevel; 173 174 std::map< std::string, TypedefDecl * > typedefNames; 181 175 }; 182 176 … … 444 438 } 445 439 440 /// Fix up assertions 446 441 void forallFixer( Type *func ) { 447 // Fix up assertions448 442 for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) { 449 443 std::list< DeclarationWithType * > toBeDone, nextRound; … … 595 589 assignDecl2->fixUniqueId(); 596 590 597 // these should be built in the same way that the prelude598 // functions are, so build a list containing the prototypes599 // and allow MakeLibCfa to autogenerate the bodies.600 591 std::list< Declaration * > assigns; 601 592 assigns.push_back( assignDecl ); … … 604 595 LibCfa::makeLibCfa( assigns ); 605 596 606 // need to remove the prototypes, since th is may benested in a routine597 // need to remove the prototypes, since these can appear nested in a routine 607 598 for (int start = 0, end = assigns.size()/2; start < end; start++) { 608 599 delete assigns.front(); … … 611 602 612 603 declsToAdd.insert( declsToAdd.begin(), assigns.begin(), assigns.end() ); 604 605 // return assignDecl; 613 606 } 614 607 … … 808 801 } 809 802 810 Type *EliminateTypedef::mutate( TypeInstType * typeInst ) { 811 // instances of typedef types will come here. If it is an instance 812 // of a typdef type, link the instance to its actual type. 813 TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() ); 803 Type *EliminateTypedef::mutate( TypeInstType *typeInst ) { 804 std::map< std::string, TypedefDecl * >::const_iterator def = typedefNames.find( typeInst->get_name() ); 814 805 if ( def != typedefNames.end() ) { 815 Type *ret = def->second .first->get_base()->clone();806 Type *ret = def->second->get_base()->clone(); 816 807 ret->get_qualifiers() += typeInst->get_qualifiers(); 817 808 delete typeInst; … … 821 812 } 822 813 823 Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) {814 Declaration *EliminateTypedef::mutate( TypedefDecl *tyDecl ) { 824 815 Declaration *ret = Mutator::mutate( tyDecl ); 825 if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) { 826 // typedef to the same name from the same scope 827 // must be from the same type 828 829 Type * t1 = tyDecl->get_base(); 830 Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base(); 831 if ( ! typeEquals( t1, t2, true ) ) { 832 throw SemanticError( "cannot redefine typedef: " + tyDecl->get_name() ); 833 } 834 } else { 835 typedefNames[ tyDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel ); 836 } // if 837 816 typedefNames[ tyDecl->get_name() ] = tyDecl; 838 817 // When a typedef is a forward declaration: 839 818 // typedef struct screen SCREEN; … … 853 832 } 854 833 855 TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) {856 TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() );834 TypeDecl *EliminateTypedef::mutate( TypeDecl *typeDecl ) { 835 std::map< std::string, TypedefDecl * >::iterator i = typedefNames.find( typeDecl->get_name() ); 857 836 if ( i != typedefNames.end() ) { 858 837 typedefNames.erase( i ) ; … … 861 840 } 862 841 863 DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) {864 TypedefMapoldNames = typedefNames;842 DeclarationWithType *EliminateTypedef::mutate( FunctionDecl *funcDecl ) { 843 std::map< std::string, TypedefDecl * > oldNames = typedefNames; 865 844 DeclarationWithType *ret = Mutator::mutate( funcDecl ); 866 845 typedefNames = oldNames; … … 868 847 } 869 848 870 ObjectDecl *EliminateTypedef::mutate( ObjectDecl * objDecl ) {871 TypedefMapoldNames = typedefNames;849 ObjectDecl *EliminateTypedef::mutate( ObjectDecl *objDecl ) { 850 std::map< std::string, TypedefDecl * > oldNames = typedefNames; 872 851 ObjectDecl *ret = Mutator::mutate( objDecl ); 873 852 typedefNames = oldNames; … … 875 854 } 876 855 877 Expression *EliminateTypedef::mutate( CastExpr * castExpr ) {878 TypedefMapoldNames = typedefNames;856 Expression *EliminateTypedef::mutate( CastExpr *castExpr ) { 857 std::map< std::string, TypedefDecl * > oldNames = typedefNames; 879 858 Expression *ret = Mutator::mutate( castExpr ); 880 859 typedefNames = oldNames; … … 882 861 } 883 862 884 CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) { 885 TypedefMap oldNames = typedefNames; 886 scopeLevel += 1; 863 CompoundStmt *EliminateTypedef::mutate( CompoundStmt *compoundStmt ) { 864 std::map< std::string, TypedefDecl * > oldNames = typedefNames; 887 865 CompoundStmt *ret = Mutator::mutate( compoundStmt ); 888 scopeLevel -= 1;889 866 std::list< Statement * >::iterator i = compoundStmt->get_kids().begin(); 890 867 while ( i != compoundStmt->get_kids().end() ) { 891 std::list< Statement * >::iterator next = i+1; 868 std::list< Statement * >::iterator next = i; 869 ++next; 892 870 if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) { 893 871 if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) { … … 901 879 return ret; 902 880 } 903 904 // there may be typedefs nested within aggregates905 // in order for everything to work properly, these906 // should be removed as well907 template<typename AggDecl>908 AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) {909 std::list<Declaration *>::iterator it = aggDecl->get_members().begin();910 for ( ; it != aggDecl->get_members().end(); ) {911 std::list< Declaration * >::iterator next = it+1;912 if ( dynamic_cast< TypedefDecl * >( *it ) ) {913 delete *it;914 aggDecl->get_members().erase( it );915 } // if916 it = next;917 }918 return aggDecl;919 }920 921 Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) {922 Mutator::mutate( structDecl );923 return handleAggregate( structDecl );924 }925 926 Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) {927 Mutator::mutate( unionDecl );928 return handleAggregate( unionDecl );929 }930 931 Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) {932 Mutator::mutate( enumDecl );933 return handleAggregate( enumDecl );934 }935 936 Declaration *EliminateTypedef::mutate( ContextDecl * contextDecl ) {937 Mutator::mutate( contextDecl );938 return handleAggregate( contextDecl );939 }940 941 881 } // namespace SymTab 942 882
Note:
See TracChangeset
for help on using the changeset viewer.