Changes in src/SymTab/Validate.cc [82dd287:85c4ef0]
- File:
-
- 1 edited
-
src/SymTab/Validate.cc (modified) (21 diffs)
Legend:
- Unmodified
- Added
- Removed
-
src/SymTab/Validate.cc
r82dd287 r85c4ef0 10 10 // Created On : Sun May 17 21:50:04 2015 11 11 // Last Modified By : Rob Schluntz 12 // Last Modified On : Tue Jul 07 10:41:23201513 // Update Count : 1 3612 // Last Modified On : Mon Jul 13 14:38:19 2015 13 // Update Count : 184 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 55 #include "TypeEquality.h" 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 types63 62 static void hoistStruct( std::list< Declaration * > &translationUnit ); 64 63 … … 85 84 }; 86 85 87 /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers88 86 class Pass1 : public Visitor { 89 87 typedef Visitor Parent; … … 91 89 virtual void visit( FunctionType *func ); 92 90 }; 93 94 /// Associates forward declarations of aggregates with their definitions 91 95 92 class Pass2 : public Indexer { 96 93 typedef Indexer Parent; … … 113 110 }; 114 111 115 /// Replaces array and function types in forall lists by appropriate pointer type116 112 class Pass3 : public Indexer { 117 113 typedef Indexer Parent; … … 127 123 class AddStructAssignment : public Visitor { 128 124 public: 129 /// Generates assignment operators for aggregate types as required130 125 static void addStructAssignment( std::list< Declaration * > &translationUnit ); 131 126 … … 162 157 class EliminateTypedef : public Mutator { 163 158 public: 159 EliminateTypedef() : scopeLevel( 0 ) {} 164 160 static void eliminateTypedef( std::list< Declaration * > &translationUnit ); 165 161 private: … … 171 167 virtual Type *mutate( TypeInstType *aggregateUseType ); 172 168 virtual Expression *mutate( CastExpr *castExpr ); 173 174 std::map< std::string, TypedefDecl * > typedefNames; 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; 175 181 }; 176 182 … … 438 444 } 439 445 440 /// Fix up assertions441 446 void forallFixer( Type *func ) { 447 // Fix up assertions 442 448 for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) { 443 449 std::list< DeclarationWithType * > toBeDone, nextRound; … … 589 595 assignDecl2->fixUniqueId(); 590 596 597 // these should be built in the same way that the prelude 598 // functions are, so build a list containing the prototypes 599 // and allow MakeLibCfa to autogenerate the bodies. 591 600 std::list< Declaration * > assigns; 592 601 assigns.push_back( assignDecl ); … … 595 604 LibCfa::makeLibCfa( assigns ); 596 605 597 // need to remove the prototypes, since th ese can appearnested in a routine606 // need to remove the prototypes, since this may be nested in a routine 598 607 for (int start = 0, end = assigns.size()/2; start < end; start++) { 599 608 delete assigns.front(); … … 602 611 603 612 declsToAdd.insert( declsToAdd.begin(), assigns.begin(), assigns.end() ); 604 605 // return assignDecl;606 613 } 607 614 … … 801 808 } 802 809 803 Type *EliminateTypedef::mutate( TypeInstType *typeInst ) { 804 std::map< std::string, TypedefDecl * >::const_iterator def = typedefNames.find( typeInst->get_name() ); 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() ); 805 814 if ( def != typedefNames.end() ) { 806 Type *ret = def->second ->get_base()->clone();815 Type *ret = def->second.first->get_base()->clone(); 807 816 ret->get_qualifiers() += typeInst->get_qualifiers(); 808 817 delete typeInst; … … 812 821 } 813 822 814 Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) {823 Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) { 815 824 Declaration *ret = Mutator::mutate( tyDecl ); 816 typedefNames[ tyDecl->get_name() ] = 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 817 838 // When a typedef is a forward declaration: 818 839 // typedef struct screen SCREEN; … … 832 853 } 833 854 834 TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) {835 std::map< std::string, TypedefDecl * >::iterator i = typedefNames.find( typeDecl->get_name() );855 TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) { 856 TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() ); 836 857 if ( i != typedefNames.end() ) { 837 858 typedefNames.erase( i ) ; … … 840 861 } 841 862 842 DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) {843 std::map< std::string, TypedefDecl * >oldNames = typedefNames;863 DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) { 864 TypedefMap oldNames = typedefNames; 844 865 DeclarationWithType *ret = Mutator::mutate( funcDecl ); 845 866 typedefNames = oldNames; … … 847 868 } 848 869 849 ObjectDecl *EliminateTypedef::mutate( ObjectDecl * objDecl ) {850 std::map< std::string, TypedefDecl * >oldNames = typedefNames;870 ObjectDecl *EliminateTypedef::mutate( ObjectDecl * objDecl ) { 871 TypedefMap oldNames = typedefNames; 851 872 ObjectDecl *ret = Mutator::mutate( objDecl ); 852 873 typedefNames = oldNames; … … 854 875 } 855 876 856 Expression *EliminateTypedef::mutate( CastExpr * castExpr ) {857 std::map< std::string, TypedefDecl * >oldNames = typedefNames;877 Expression *EliminateTypedef::mutate( CastExpr * castExpr ) { 878 TypedefMap oldNames = typedefNames; 858 879 Expression *ret = Mutator::mutate( castExpr ); 859 880 typedefNames = oldNames; … … 861 882 } 862 883 863 CompoundStmt *EliminateTypedef::mutate( CompoundStmt *compoundStmt ) { 864 std::map< std::string, TypedefDecl * > oldNames = typedefNames; 884 CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) { 885 TypedefMap oldNames = typedefNames; 886 scopeLevel += 1; 865 887 CompoundStmt *ret = Mutator::mutate( compoundStmt ); 888 scopeLevel -= 1; 866 889 std::list< Statement * >::iterator i = compoundStmt->get_kids().begin(); 867 890 while ( i != compoundStmt->get_kids().end() ) { 868 std::list< Statement * >::iterator next = i; 869 ++next; 891 std::list< Statement * >::iterator next = i+1; 870 892 if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) { 871 893 if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) { … … 879 901 return ret; 880 902 } 903 904 // there may be typedefs nested within aggregates 905 // in order for everything to work properly, these 906 // should be removed as well 907 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 } // if 916 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 881 941 } // namespace SymTab 882 942
Note:
See TracChangeset
for help on using the changeset viewer.