| [0dd3a2f] | 1 | // | 
|---|
|  | 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo | 
|---|
|  | 3 | // | 
|---|
|  | 4 | // The contents of this file are covered under the licence agreement in the | 
|---|
|  | 5 | // file "LICENCE" distributed with Cforall. | 
|---|
|  | 6 | // | 
|---|
| [9cb8e88d] | 7 | // Validate.cc -- | 
|---|
| [0dd3a2f] | 8 | // | 
|---|
|  | 9 | // Author           : Richard C. Bilson | 
|---|
|  | 10 | // Created On       : Sun May 17 21:50:04 2015 | 
|---|
| [f066321] | 11 | // Last Modified By : Rob Schluntz | 
|---|
| [fb24492] | 12 | // Last Modified On : Wed May 11 13:17:52 2016 | 
|---|
| [7528ba1] | 13 | // Update Count     : 297 | 
|---|
| [0dd3a2f] | 14 | // | 
|---|
|  | 15 |  | 
|---|
|  | 16 | // The "validate" phase of translation is used to take a syntax tree and convert it into a standard form that aims to be | 
|---|
|  | 17 | // as regular in structure as possible.  Some assumptions can be made regarding the state of the tree after this pass is | 
|---|
|  | 18 | // complete, including: | 
|---|
|  | 19 | // | 
|---|
|  | 20 | // - No nested structure or union definitions; any in the input are "hoisted" to the level of the containing struct or | 
|---|
|  | 21 | //   union. | 
|---|
|  | 22 | // | 
|---|
|  | 23 | // - All enumeration constants have type EnumInstType. | 
|---|
|  | 24 | // | 
|---|
|  | 25 | // - The type "void" never occurs in lists of function parameter or return types; neither do tuple types.  A function | 
|---|
|  | 26 | //   taking no arguments has no argument types, and tuples are flattened. | 
|---|
|  | 27 | // | 
|---|
|  | 28 | // - No context instances exist; they are all replaced by the set of declarations signified by the context, instantiated | 
|---|
|  | 29 | //   by the particular set of type arguments. | 
|---|
|  | 30 | // | 
|---|
|  | 31 | // - Every declaration is assigned a unique id. | 
|---|
|  | 32 | // | 
|---|
|  | 33 | // - No typedef declarations or instances exist; the actual type is substituted for each instance. | 
|---|
|  | 34 | // | 
|---|
|  | 35 | // - Each type, struct, and union definition is followed by an appropriate assignment operator. | 
|---|
|  | 36 | // | 
|---|
|  | 37 | // - Each use of a struct or union is connected to a complete definition of that struct or union, even if that | 
|---|
|  | 38 | //   definition occurs later in the input. | 
|---|
| [51b73452] | 39 |  | 
|---|
|  | 40 | #include <list> | 
|---|
|  | 41 | #include <iterator> | 
|---|
| [630a82a] | 42 | #include "Common/utility.h" | 
|---|
|  | 43 | #include "Common/UniqueName.h" | 
|---|
| [51b73452] | 44 | #include "Validate.h" | 
|---|
|  | 45 | #include "SynTree/Visitor.h" | 
|---|
|  | 46 | #include "SynTree/Mutator.h" | 
|---|
|  | 47 | #include "SynTree/Type.h" | 
|---|
| [630a82a] | 48 | #include "SynTree/Expression.h" | 
|---|
| [51b73452] | 49 | #include "SynTree/Statement.h" | 
|---|
|  | 50 | #include "SynTree/TypeSubstitution.h" | 
|---|
| [68cd1ce] | 51 | #include "Indexer.h" | 
|---|
| [51b73452] | 52 | #include "FixFunction.h" | 
|---|
| [cc79d97] | 53 | // #include "ImplementationType.h" | 
|---|
| [630a82a] | 54 | #include "GenPoly/DeclMutator.h" | 
|---|
| [51b73452] | 55 | #include "AddVisit.h" | 
|---|
| [f6d7e0f] | 56 | #include "MakeLibCfa.h" | 
|---|
| [cc79d97] | 57 | #include "TypeEquality.h" | 
|---|
| [620cb95] | 58 | #include "Autogen.h" | 
|---|
| [1cbca6e] | 59 | #include "ResolvExpr/typeops.h" | 
|---|
| [51b73452] | 60 |  | 
|---|
| [c8ffe20b] | 61 | #define debugPrint( x ) if ( doDebug ) { std::cout << x; } | 
|---|
| [51b73452] | 62 |  | 
|---|
|  | 63 | namespace SymTab { | 
|---|
| [a08ba92] | 64 | class HoistStruct : public Visitor { | 
|---|
|  | 65 | public: | 
|---|
| [82dd287] | 66 | /// Flattens nested struct types | 
|---|
| [0dd3a2f] | 67 | static void hoistStruct( std::list< Declaration * > &translationUnit ); | 
|---|
| [9cb8e88d] | 68 |  | 
|---|
| [0dd3a2f] | 69 | std::list< Declaration * > &get_declsToAdd() { return declsToAdd; } | 
|---|
| [9cb8e88d] | 70 |  | 
|---|
| [0dd3a2f] | 71 | virtual void visit( StructDecl *aggregateDecl ); | 
|---|
|  | 72 | virtual void visit( UnionDecl *aggregateDecl ); | 
|---|
| [c8ffe20b] | 73 |  | 
|---|
| [0dd3a2f] | 74 | virtual void visit( CompoundStmt *compoundStmt ); | 
|---|
|  | 75 | virtual void visit( SwitchStmt *switchStmt ); | 
|---|
|  | 76 | virtual void visit( ChooseStmt *chooseStmt ); | 
|---|
| [630a82a] | 77 | // virtual void visit( CaseStmt *caseStmt ); | 
|---|
| [a08ba92] | 78 | private: | 
|---|
| [0dd3a2f] | 79 | HoistStruct(); | 
|---|
| [c8ffe20b] | 80 |  | 
|---|
| [0dd3a2f] | 81 | template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl ); | 
|---|
| [c8ffe20b] | 82 |  | 
|---|
| [0dd3a2f] | 83 | std::list< Declaration * > declsToAdd; | 
|---|
|  | 84 | bool inStruct; | 
|---|
| [a08ba92] | 85 | }; | 
|---|
| [c8ffe20b] | 86 |  | 
|---|
| [de91427b] | 87 | /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers. | 
|---|
| [a08ba92] | 88 | class Pass1 : public Visitor { | 
|---|
| [0dd3a2f] | 89 | typedef Visitor Parent; | 
|---|
|  | 90 | virtual void visit( EnumDecl *aggregateDecl ); | 
|---|
|  | 91 | virtual void visit( FunctionType *func ); | 
|---|
| [a08ba92] | 92 | }; | 
|---|
| [82dd287] | 93 |  | 
|---|
|  | 94 | /// Associates forward declarations of aggregates with their definitions | 
|---|
| [a08ba92] | 95 | class Pass2 : public Indexer { | 
|---|
| [0dd3a2f] | 96 | typedef Indexer Parent; | 
|---|
| [a08ba92] | 97 | public: | 
|---|
| [0dd3a2f] | 98 | Pass2( bool doDebug, const Indexer *indexer ); | 
|---|
| [a08ba92] | 99 | private: | 
|---|
| [0dd3a2f] | 100 | virtual void visit( StructInstType *structInst ); | 
|---|
|  | 101 | virtual void visit( UnionInstType *unionInst ); | 
|---|
| [4040425] | 102 | virtual void visit( TraitInstType *contextInst ); | 
|---|
| [0dd3a2f] | 103 | virtual void visit( StructDecl *structDecl ); | 
|---|
|  | 104 | virtual void visit( UnionDecl *unionDecl ); | 
|---|
|  | 105 | virtual void visit( TypeInstType *typeInst ); | 
|---|
|  | 106 |  | 
|---|
|  | 107 | const Indexer *indexer; | 
|---|
| [9cb8e88d] | 108 |  | 
|---|
| [0dd3a2f] | 109 | typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType; | 
|---|
|  | 110 | typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType; | 
|---|
|  | 111 | ForwardStructsType forwardStructs; | 
|---|
|  | 112 | ForwardUnionsType forwardUnions; | 
|---|
| [a08ba92] | 113 | }; | 
|---|
| [c8ffe20b] | 114 |  | 
|---|
| [82dd287] | 115 | /// Replaces array and function types in forall lists by appropriate pointer type | 
|---|
| [a08ba92] | 116 | class Pass3 : public Indexer { | 
|---|
| [0dd3a2f] | 117 | typedef Indexer Parent; | 
|---|
| [a08ba92] | 118 | public: | 
|---|
| [0dd3a2f] | 119 | Pass3( const Indexer *indexer ); | 
|---|
| [a08ba92] | 120 | private: | 
|---|
| [0dd3a2f] | 121 | virtual void visit( ObjectDecl *object ); | 
|---|
|  | 122 | virtual void visit( FunctionDecl *func ); | 
|---|
| [c8ffe20b] | 123 |  | 
|---|
| [0dd3a2f] | 124 | const Indexer *indexer; | 
|---|
| [a08ba92] | 125 | }; | 
|---|
| [c8ffe20b] | 126 |  | 
|---|
| [de91427b] | 127 | class ReturnChecker : public Visitor { | 
|---|
|  | 128 | public: | 
|---|
|  | 129 | /// Checks that return statements return nothing if their return type is void | 
|---|
|  | 130 | /// and return something if the return type is non-void. | 
|---|
|  | 131 | static void checkFunctionReturns( std::list< Declaration * > & translationUnit ); | 
|---|
|  | 132 | private: | 
|---|
|  | 133 | virtual void visit( FunctionDecl * functionDecl ); | 
|---|
|  | 134 |  | 
|---|
|  | 135 | virtual void visit( ReturnStmt * returnStmt ); | 
|---|
|  | 136 |  | 
|---|
|  | 137 | std::list< DeclarationWithType * > returnVals; | 
|---|
|  | 138 | }; | 
|---|
|  | 139 |  | 
|---|
| [a08ba92] | 140 | class EliminateTypedef : public Mutator { | 
|---|
|  | 141 | public: | 
|---|
| [de91427b] | 142 | EliminateTypedef() : scopeLevel( 0 ) {} | 
|---|
|  | 143 | /// Replaces typedefs by forward declarations | 
|---|
| [0dd3a2f] | 144 | static void eliminateTypedef( std::list< Declaration * > &translationUnit ); | 
|---|
| [a08ba92] | 145 | private: | 
|---|
| [0dd3a2f] | 146 | virtual Declaration *mutate( TypedefDecl *typeDecl ); | 
|---|
|  | 147 | virtual TypeDecl *mutate( TypeDecl *typeDecl ); | 
|---|
|  | 148 | virtual DeclarationWithType *mutate( FunctionDecl *funcDecl ); | 
|---|
| [1db21619] | 149 | virtual DeclarationWithType *mutate( ObjectDecl *objDecl ); | 
|---|
| [0dd3a2f] | 150 | virtual CompoundStmt *mutate( CompoundStmt *compoundStmt ); | 
|---|
|  | 151 | virtual Type *mutate( TypeInstType *aggregateUseType ); | 
|---|
|  | 152 | virtual Expression *mutate( CastExpr *castExpr ); | 
|---|
| [cc79d97] | 153 |  | 
|---|
| [85c4ef0] | 154 | virtual Declaration *mutate( StructDecl * structDecl ); | 
|---|
|  | 155 | virtual Declaration *mutate( UnionDecl * unionDecl ); | 
|---|
|  | 156 | virtual Declaration *mutate( EnumDecl * enumDecl ); | 
|---|
| [4040425] | 157 | virtual Declaration *mutate( TraitDecl * contextDecl ); | 
|---|
| [85c4ef0] | 158 |  | 
|---|
|  | 159 | template<typename AggDecl> | 
|---|
|  | 160 | AggDecl *handleAggregate( AggDecl * aggDecl ); | 
|---|
|  | 161 |  | 
|---|
| [45161b4d] | 162 | template<typename AggDecl> | 
|---|
|  | 163 | void addImplicitTypedef( AggDecl * aggDecl ); | 
|---|
| [70a06f6] | 164 |  | 
|---|
| [cc79d97] | 165 | typedef std::map< std::string, std::pair< TypedefDecl *, int > > TypedefMap; | 
|---|
|  | 166 | TypedefMap typedefNames; | 
|---|
|  | 167 | int scopeLevel; | 
|---|
| [a08ba92] | 168 | }; | 
|---|
| [c8ffe20b] | 169 |  | 
|---|
| [9cb8e88d] | 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 ); | 
|---|
| [70a06f6] | 178 | }; | 
|---|
|  | 179 |  | 
|---|
| [630a82a] | 180 | class CompoundLiteral : public GenPoly::DeclMutator { | 
|---|
|  | 181 | DeclarationNode::StorageClass storageclass = DeclarationNode::NoStorageClass; | 
|---|
|  | 182 |  | 
|---|
|  | 183 | virtual DeclarationWithType * mutate( ObjectDecl *objectDecl ); | 
|---|
|  | 184 | virtual Expression *mutate( CompoundLiteralExpr *compLitExpr ); | 
|---|
| [9cb8e88d] | 185 | }; | 
|---|
|  | 186 |  | 
|---|
| [a08ba92] | 187 | void validate( std::list< Declaration * > &translationUnit, bool doDebug ) { | 
|---|
| [0dd3a2f] | 188 | Pass1 pass1; | 
|---|
|  | 189 | Pass2 pass2( doDebug, 0 ); | 
|---|
|  | 190 | Pass3 pass3( 0 ); | 
|---|
| [630a82a] | 191 | CompoundLiteral compoundliteral; | 
|---|
|  | 192 |  | 
|---|
| [0dd3a2f] | 193 | EliminateTypedef::eliminateTypedef( translationUnit ); | 
|---|
|  | 194 | HoistStruct::hoistStruct( translationUnit ); | 
|---|
|  | 195 | acceptAll( translationUnit, pass1 ); | 
|---|
|  | 196 | acceptAll( translationUnit, pass2 ); | 
|---|
| [de91427b] | 197 | ReturnChecker::checkFunctionReturns( translationUnit ); | 
|---|
| [630a82a] | 198 | mutateAll( translationUnit, compoundliteral ); | 
|---|
| [620cb95] | 199 | autogenerateRoutines( translationUnit ); | 
|---|
| [0dd3a2f] | 200 | acceptAll( translationUnit, pass3 ); | 
|---|
| [9cb8e88d] | 201 | VerifyCtorDtor::verify( translationUnit ); | 
|---|
| [a08ba92] | 202 | } | 
|---|
| [9cb8e88d] | 203 |  | 
|---|
| [a08ba92] | 204 | void validateType( Type *type, const Indexer *indexer ) { | 
|---|
| [0dd3a2f] | 205 | Pass1 pass1; | 
|---|
|  | 206 | Pass2 pass2( false, indexer ); | 
|---|
|  | 207 | Pass3 pass3( indexer ); | 
|---|
|  | 208 | type->accept( pass1 ); | 
|---|
|  | 209 | type->accept( pass2 ); | 
|---|
|  | 210 | type->accept( pass3 ); | 
|---|
| [a08ba92] | 211 | } | 
|---|
| [c8ffe20b] | 212 |  | 
|---|
| [a08ba92] | 213 | void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) { | 
|---|
| [0dd3a2f] | 214 | HoistStruct hoister; | 
|---|
|  | 215 | acceptAndAdd( translationUnit, hoister, true ); | 
|---|
| [a08ba92] | 216 | } | 
|---|
| [c8ffe20b] | 217 |  | 
|---|
| [a08ba92] | 218 | HoistStruct::HoistStruct() : inStruct( false ) { | 
|---|
|  | 219 | } | 
|---|
| [c8ffe20b] | 220 |  | 
|---|
| [a08ba92] | 221 | void filter( std::list< Declaration * > &declList, bool (*pred)( Declaration * ), bool doDelete ) { | 
|---|
| [0dd3a2f] | 222 | std::list< Declaration * >::iterator i = declList.begin(); | 
|---|
|  | 223 | while ( i != declList.end() ) { | 
|---|
|  | 224 | std::list< Declaration * >::iterator next = i; | 
|---|
|  | 225 | ++next; | 
|---|
|  | 226 | if ( pred( *i ) ) { | 
|---|
|  | 227 | if ( doDelete ) { | 
|---|
|  | 228 | delete *i; | 
|---|
|  | 229 | } // if | 
|---|
|  | 230 | declList.erase( i ); | 
|---|
|  | 231 | } // if | 
|---|
|  | 232 | i = next; | 
|---|
|  | 233 | } // while | 
|---|
| [a08ba92] | 234 | } | 
|---|
| [c8ffe20b] | 235 |  | 
|---|
| [a08ba92] | 236 | bool isStructOrUnion( Declaration *decl ) { | 
|---|
| [0dd3a2f] | 237 | return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl ); | 
|---|
| [a08ba92] | 238 | } | 
|---|
| [51b73452] | 239 |  | 
|---|
| [a08ba92] | 240 | template< typename AggDecl > | 
|---|
|  | 241 | void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) { | 
|---|
| [0dd3a2f] | 242 | if ( inStruct ) { | 
|---|
|  | 243 | // Add elements in stack order corresponding to nesting structure. | 
|---|
|  | 244 | declsToAdd.push_front( aggregateDecl ); | 
|---|
|  | 245 | Visitor::visit( aggregateDecl ); | 
|---|
|  | 246 | } else { | 
|---|
|  | 247 | inStruct = true; | 
|---|
|  | 248 | Visitor::visit( aggregateDecl ); | 
|---|
|  | 249 | inStruct = false; | 
|---|
|  | 250 | } // if | 
|---|
|  | 251 | // Always remove the hoisted aggregate from the inner structure. | 
|---|
|  | 252 | filter( aggregateDecl->get_members(), isStructOrUnion, false ); | 
|---|
| [a08ba92] | 253 | } | 
|---|
| [c8ffe20b] | 254 |  | 
|---|
| [a08ba92] | 255 | void HoistStruct::visit( StructDecl *aggregateDecl ) { | 
|---|
| [0dd3a2f] | 256 | handleAggregate( aggregateDecl ); | 
|---|
| [a08ba92] | 257 | } | 
|---|
| [c8ffe20b] | 258 |  | 
|---|
| [a08ba92] | 259 | void HoistStruct::visit( UnionDecl *aggregateDecl ) { | 
|---|
| [0dd3a2f] | 260 | handleAggregate( aggregateDecl ); | 
|---|
| [a08ba92] | 261 | } | 
|---|
| [c8ffe20b] | 262 |  | 
|---|
| [a08ba92] | 263 | void HoistStruct::visit( CompoundStmt *compoundStmt ) { | 
|---|
| [0dd3a2f] | 264 | addVisit( compoundStmt, *this ); | 
|---|
| [a08ba92] | 265 | } | 
|---|
| [c8ffe20b] | 266 |  | 
|---|
| [a08ba92] | 267 | void HoistStruct::visit( SwitchStmt *switchStmt ) { | 
|---|
| [0dd3a2f] | 268 | addVisit( switchStmt, *this ); | 
|---|
| [a08ba92] | 269 | } | 
|---|
| [c8ffe20b] | 270 |  | 
|---|
| [a08ba92] | 271 | void HoistStruct::visit( ChooseStmt *switchStmt ) { | 
|---|
| [0dd3a2f] | 272 | addVisit( switchStmt, *this ); | 
|---|
| [a08ba92] | 273 | } | 
|---|
| [c8ffe20b] | 274 |  | 
|---|
| [630a82a] | 275 | // void HoistStruct::visit( CaseStmt *caseStmt ) { | 
|---|
|  | 276 | //      addVisit( caseStmt, *this ); | 
|---|
|  | 277 | // } | 
|---|
| [c8ffe20b] | 278 |  | 
|---|
| [a08ba92] | 279 | void Pass1::visit( EnumDecl *enumDecl ) { | 
|---|
| [0dd3a2f] | 280 | // Set the type of each member of the enumeration to be EnumConstant | 
|---|
| [9cb8e88d] | 281 |  | 
|---|
| [0dd3a2f] | 282 | for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) { | 
|---|
| [f6d7e0f] | 283 | ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i ); | 
|---|
| [0dd3a2f] | 284 | assert( obj ); | 
|---|
| [f6d7e0f] | 285 | // obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false, false, false ), enumDecl->get_name() ) ); | 
|---|
|  | 286 | BasicType * enumType = new BasicType( Type::Qualifiers(), BasicType::SignedInt ); | 
|---|
|  | 287 | obj->set_type( enumType ) ; | 
|---|
| [0dd3a2f] | 288 | } // for | 
|---|
|  | 289 | Parent::visit( enumDecl ); | 
|---|
| [a08ba92] | 290 | } | 
|---|
| [51b73452] | 291 |  | 
|---|
| [a08ba92] | 292 | namespace { | 
|---|
| [83de11e] | 293 | template< typename DWTList > | 
|---|
|  | 294 | void fixFunctionList( DWTList & dwts, FunctionType * func ) { | 
|---|
| [0dd3a2f] | 295 | // the only case in which "void" is valid is where it is the only one in the list; then it should be removed | 
|---|
|  | 296 | // entirely other fix ups are handled by the FixFunction class | 
|---|
| [83de11e] | 297 | typedef typename DWTList::iterator DWTIterator; | 
|---|
|  | 298 | DWTIterator begin( dwts.begin() ), end( dwts.end() ); | 
|---|
| [0dd3a2f] | 299 | if ( begin == end ) return; | 
|---|
|  | 300 | FixFunction fixer; | 
|---|
|  | 301 | DWTIterator i = begin; | 
|---|
| [83de11e] | 302 | *i = (*i)->acceptMutator( fixer ); | 
|---|
| [0dd3a2f] | 303 | if ( fixer.get_isVoid() ) { | 
|---|
|  | 304 | DWTIterator j = i; | 
|---|
|  | 305 | ++i; | 
|---|
| [83de11e] | 306 | dwts.erase( j ); | 
|---|
| [9cb8e88d] | 307 | if ( i != end ) { | 
|---|
| [0dd3a2f] | 308 | throw SemanticError( "invalid type void in function type ", func ); | 
|---|
|  | 309 | } // if | 
|---|
|  | 310 | } else { | 
|---|
|  | 311 | ++i; | 
|---|
|  | 312 | for ( ; i != end; ++i ) { | 
|---|
|  | 313 | FixFunction fixer; | 
|---|
|  | 314 | *i = (*i )->acceptMutator( fixer ); | 
|---|
|  | 315 | if ( fixer.get_isVoid() ) { | 
|---|
|  | 316 | throw SemanticError( "invalid type void in function type ", func ); | 
|---|
|  | 317 | } // if | 
|---|
|  | 318 | } // for | 
|---|
|  | 319 | } // if | 
|---|
|  | 320 | } | 
|---|
| [a08ba92] | 321 | } | 
|---|
| [c8ffe20b] | 322 |  | 
|---|
| [a08ba92] | 323 | void Pass1::visit( FunctionType *func ) { | 
|---|
| [0dd3a2f] | 324 | // Fix up parameters and return types | 
|---|
| [83de11e] | 325 | fixFunctionList( func->get_parameters(), func ); | 
|---|
|  | 326 | fixFunctionList( func->get_returnVals(), func ); | 
|---|
| [0dd3a2f] | 327 | Visitor::visit( func ); | 
|---|
| [a08ba92] | 328 | } | 
|---|
| [c8ffe20b] | 329 |  | 
|---|
| [a08ba92] | 330 | Pass2::Pass2( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug ) { | 
|---|
| [0dd3a2f] | 331 | if ( other_indexer ) { | 
|---|
|  | 332 | indexer = other_indexer; | 
|---|
|  | 333 | } else { | 
|---|
|  | 334 | indexer = this; | 
|---|
|  | 335 | } // if | 
|---|
| [a08ba92] | 336 | } | 
|---|
| [c8ffe20b] | 337 |  | 
|---|
| [a08ba92] | 338 | void Pass2::visit( StructInstType *structInst ) { | 
|---|
| [0dd3a2f] | 339 | Parent::visit( structInst ); | 
|---|
|  | 340 | StructDecl *st = indexer->lookupStruct( structInst->get_name() ); | 
|---|
|  | 341 | // it's not a semantic error if the struct is not found, just an implicit forward declaration | 
|---|
|  | 342 | if ( st ) { | 
|---|
| [98735ef] | 343 | //assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() ); | 
|---|
| [0dd3a2f] | 344 | structInst->set_baseStruct( st ); | 
|---|
|  | 345 | } // if | 
|---|
|  | 346 | if ( ! st || st->get_members().empty() ) { | 
|---|
|  | 347 | // use of forward declaration | 
|---|
|  | 348 | forwardStructs[ structInst->get_name() ].push_back( structInst ); | 
|---|
|  | 349 | } // if | 
|---|
| [a08ba92] | 350 | } | 
|---|
| [c8ffe20b] | 351 |  | 
|---|
| [a08ba92] | 352 | void Pass2::visit( UnionInstType *unionInst ) { | 
|---|
| [0dd3a2f] | 353 | Parent::visit( unionInst ); | 
|---|
|  | 354 | UnionDecl *un = indexer->lookupUnion( unionInst->get_name() ); | 
|---|
|  | 355 | // it's not a semantic error if the union is not found, just an implicit forward declaration | 
|---|
|  | 356 | if ( un ) { | 
|---|
|  | 357 | unionInst->set_baseUnion( un ); | 
|---|
|  | 358 | } // if | 
|---|
|  | 359 | if ( ! un || un->get_members().empty() ) { | 
|---|
|  | 360 | // use of forward declaration | 
|---|
|  | 361 | forwardUnions[ unionInst->get_name() ].push_back( unionInst ); | 
|---|
|  | 362 | } // if | 
|---|
| [a08ba92] | 363 | } | 
|---|
| [c8ffe20b] | 364 |  | 
|---|
| [4040425] | 365 | void Pass2::visit( TraitInstType *contextInst ) { | 
|---|
| [0dd3a2f] | 366 | Parent::visit( contextInst ); | 
|---|
| [4040425] | 367 | TraitDecl *ctx = indexer->lookupTrait( contextInst->get_name() ); | 
|---|
| [0dd3a2f] | 368 | if ( ! ctx ) { | 
|---|
|  | 369 | throw SemanticError( "use of undeclared context " + contextInst->get_name() ); | 
|---|
| [17cd4eb] | 370 | } // if | 
|---|
| [0dd3a2f] | 371 | for ( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) { | 
|---|
|  | 372 | for ( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) { | 
|---|
| [4040425] | 373 | if ( TraitInstType *otherCtx = dynamic_cast< TraitInstType * >(*assert ) ) { | 
|---|
| [0dd3a2f] | 374 | cloneAll( otherCtx->get_members(), contextInst->get_members() ); | 
|---|
|  | 375 | } else { | 
|---|
|  | 376 | contextInst->get_members().push_back( (*assert )->clone() ); | 
|---|
|  | 377 | } // if | 
|---|
|  | 378 | } // for | 
|---|
|  | 379 | } // for | 
|---|
| [51b986f] | 380 |  | 
|---|
|  | 381 | if ( ctx->get_parameters().size() != contextInst->get_parameters().size() ) { | 
|---|
|  | 382 | throw SemanticError( "incorrect number of context parameters: ", contextInst ); | 
|---|
|  | 383 | } // if | 
|---|
|  | 384 |  | 
|---|
| [0dd3a2f] | 385 | applySubstitution( ctx->get_parameters().begin(), ctx->get_parameters().end(), contextInst->get_parameters().begin(), ctx->get_members().begin(), ctx->get_members().end(), back_inserter( contextInst->get_members() ) ); | 
|---|
| [a08ba92] | 386 | } | 
|---|
| [c8ffe20b] | 387 |  | 
|---|
| [a08ba92] | 388 | void Pass2::visit( StructDecl *structDecl ) { | 
|---|
| [677c1be] | 389 | // visit struct members first so that the types of self-referencing members are updated properly | 
|---|
|  | 390 | Parent::visit( structDecl ); | 
|---|
| [0dd3a2f] | 391 | if ( ! structDecl->get_members().empty() ) { | 
|---|
|  | 392 | ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() ); | 
|---|
|  | 393 | if ( fwds != forwardStructs.end() ) { | 
|---|
|  | 394 | for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { | 
|---|
|  | 395 | (*inst )->set_baseStruct( structDecl ); | 
|---|
|  | 396 | } // for | 
|---|
|  | 397 | forwardStructs.erase( fwds ); | 
|---|
|  | 398 | } // if | 
|---|
|  | 399 | } // if | 
|---|
| [a08ba92] | 400 | } | 
|---|
| [c8ffe20b] | 401 |  | 
|---|
| [a08ba92] | 402 | void Pass2::visit( UnionDecl *unionDecl ) { | 
|---|
| [677c1be] | 403 | Parent::visit( unionDecl ); | 
|---|
| [0dd3a2f] | 404 | if ( ! unionDecl->get_members().empty() ) { | 
|---|
|  | 405 | ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() ); | 
|---|
|  | 406 | if ( fwds != forwardUnions.end() ) { | 
|---|
|  | 407 | for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { | 
|---|
|  | 408 | (*inst )->set_baseUnion( unionDecl ); | 
|---|
|  | 409 | } // for | 
|---|
|  | 410 | forwardUnions.erase( fwds ); | 
|---|
|  | 411 | } // if | 
|---|
|  | 412 | } // if | 
|---|
| [a08ba92] | 413 | } | 
|---|
| [c8ffe20b] | 414 |  | 
|---|
| [a08ba92] | 415 | void Pass2::visit( TypeInstType *typeInst ) { | 
|---|
| [0dd3a2f] | 416 | if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) { | 
|---|
|  | 417 | if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) { | 
|---|
|  | 418 | typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype ); | 
|---|
|  | 419 | } // if | 
|---|
|  | 420 | } // if | 
|---|
| [a08ba92] | 421 | } | 
|---|
| [c8ffe20b] | 422 |  | 
|---|
| [a08ba92] | 423 | Pass3::Pass3( const Indexer *other_indexer ) :  Indexer( false ) { | 
|---|
| [0dd3a2f] | 424 | if ( other_indexer ) { | 
|---|
|  | 425 | indexer = other_indexer; | 
|---|
|  | 426 | } else { | 
|---|
|  | 427 | indexer = this; | 
|---|
|  | 428 | } // if | 
|---|
| [a08ba92] | 429 | } | 
|---|
| [c8ffe20b] | 430 |  | 
|---|
| [82dd287] | 431 | /// Fix up assertions | 
|---|
| [a08ba92] | 432 | void forallFixer( Type *func ) { | 
|---|
| [0dd3a2f] | 433 | for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) { | 
|---|
|  | 434 | std::list< DeclarationWithType * > toBeDone, nextRound; | 
|---|
|  | 435 | toBeDone.splice( toBeDone.end(), (*type )->get_assertions() ); | 
|---|
|  | 436 | while ( ! toBeDone.empty() ) { | 
|---|
|  | 437 | for ( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) { | 
|---|
| [4040425] | 438 | if ( TraitInstType *ctx = dynamic_cast< TraitInstType * >( (*assertion )->get_type() ) ) { | 
|---|
| [0dd3a2f] | 439 | for ( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) { | 
|---|
|  | 440 | DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i ); | 
|---|
|  | 441 | assert( dwt ); | 
|---|
|  | 442 | nextRound.push_back( dwt->clone() ); | 
|---|
|  | 443 | } | 
|---|
|  | 444 | delete ctx; | 
|---|
|  | 445 | } else { | 
|---|
|  | 446 | FixFunction fixer; | 
|---|
|  | 447 | *assertion = (*assertion )->acceptMutator( fixer ); | 
|---|
|  | 448 | if ( fixer.get_isVoid() ) { | 
|---|
|  | 449 | throw SemanticError( "invalid type void in assertion of function ", func ); | 
|---|
|  | 450 | } | 
|---|
|  | 451 | (*type )->get_assertions().push_back( *assertion ); | 
|---|
|  | 452 | } // if | 
|---|
|  | 453 | } // for | 
|---|
|  | 454 | toBeDone.clear(); | 
|---|
|  | 455 | toBeDone.splice( toBeDone.end(), nextRound ); | 
|---|
|  | 456 | } // while | 
|---|
|  | 457 | } // for | 
|---|
| [a08ba92] | 458 | } | 
|---|
| [c8ffe20b] | 459 |  | 
|---|
| [a08ba92] | 460 | void Pass3::visit( ObjectDecl *object ) { | 
|---|
| [0dd3a2f] | 461 | forallFixer( object->get_type() ); | 
|---|
|  | 462 | if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) { | 
|---|
|  | 463 | forallFixer( pointer->get_base() ); | 
|---|
|  | 464 | } // if | 
|---|
|  | 465 | Parent::visit( object ); | 
|---|
|  | 466 | object->fixUniqueId(); | 
|---|
| [a08ba92] | 467 | } | 
|---|
| [c8ffe20b] | 468 |  | 
|---|
| [a08ba92] | 469 | void Pass3::visit( FunctionDecl *func ) { | 
|---|
| [0dd3a2f] | 470 | forallFixer( func->get_type() ); | 
|---|
|  | 471 | Parent::visit( func ); | 
|---|
|  | 472 | func->fixUniqueId(); | 
|---|
| [a08ba92] | 473 | } | 
|---|
| [c8ffe20b] | 474 |  | 
|---|
| [de91427b] | 475 | void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) { | 
|---|
|  | 476 | ReturnChecker checker; | 
|---|
|  | 477 | acceptAll( translationUnit, checker ); | 
|---|
|  | 478 | } | 
|---|
|  | 479 |  | 
|---|
|  | 480 | void ReturnChecker::visit( FunctionDecl * functionDecl ) { | 
|---|
|  | 481 | std::list< DeclarationWithType * > oldReturnVals = returnVals; | 
|---|
|  | 482 | returnVals = functionDecl->get_functionType()->get_returnVals(); | 
|---|
|  | 483 | Visitor::visit( functionDecl ); | 
|---|
|  | 484 | returnVals = oldReturnVals; | 
|---|
|  | 485 | } | 
|---|
|  | 486 |  | 
|---|
|  | 487 | void ReturnChecker::visit( ReturnStmt * returnStmt ) { | 
|---|
|  | 488 | if ( returnStmt->get_expr() == NULL && returnVals.size() != 0 ) { | 
|---|
|  | 489 | throw SemanticError( "Non-void function returns no values: " , returnStmt ); | 
|---|
|  | 490 | } else if ( returnStmt->get_expr() != NULL && returnVals.size() == 0 ) { | 
|---|
|  | 491 | throw SemanticError( "void function returns values: " , returnStmt ); | 
|---|
|  | 492 | } | 
|---|
|  | 493 | } | 
|---|
|  | 494 |  | 
|---|
|  | 495 |  | 
|---|
| [a08ba92] | 496 | bool isTypedef( Declaration *decl ) { | 
|---|
| [0dd3a2f] | 497 | return dynamic_cast< TypedefDecl * >( decl ); | 
|---|
| [a08ba92] | 498 | } | 
|---|
| [c8ffe20b] | 499 |  | 
|---|
| [a08ba92] | 500 | void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) { | 
|---|
| [0dd3a2f] | 501 | EliminateTypedef eliminator; | 
|---|
|  | 502 | mutateAll( translationUnit, eliminator ); | 
|---|
|  | 503 | filter( translationUnit, isTypedef, true ); | 
|---|
| [a08ba92] | 504 | } | 
|---|
| [c8ffe20b] | 505 |  | 
|---|
| [85c4ef0] | 506 | Type *EliminateTypedef::mutate( TypeInstType * typeInst ) { | 
|---|
| [9cb8e88d] | 507 | // instances of typedef types will come here. If it is an instance | 
|---|
| [cc79d97] | 508 | // of a typdef type, link the instance to its actual type. | 
|---|
|  | 509 | TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() ); | 
|---|
| [0dd3a2f] | 510 | if ( def != typedefNames.end() ) { | 
|---|
| [cc79d97] | 511 | Type *ret = def->second.first->get_base()->clone(); | 
|---|
| [0dd3a2f] | 512 | ret->get_qualifiers() += typeInst->get_qualifiers(); | 
|---|
| [0215a76f] | 513 | // place instance parameters on the typedef'd type | 
|---|
|  | 514 | if ( ! typeInst->get_parameters().empty() ) { | 
|---|
|  | 515 | ReferenceToType *rtt = dynamic_cast<ReferenceToType*>(ret); | 
|---|
|  | 516 | if ( ! rtt ) { | 
|---|
|  | 517 | throw SemanticError("cannot apply type parameters to base type of " + typeInst->get_name()); | 
|---|
|  | 518 | } | 
|---|
|  | 519 | rtt->get_parameters().clear(); | 
|---|
| [b644d6f] | 520 | cloneAll( typeInst->get_parameters(), rtt->get_parameters() ); | 
|---|
|  | 521 | mutateAll( rtt->get_parameters(), *this );  // recursively fix typedefs on parameters | 
|---|
| [1db21619] | 522 | } // if | 
|---|
| [0dd3a2f] | 523 | delete typeInst; | 
|---|
|  | 524 | return ret; | 
|---|
|  | 525 | } // if | 
|---|
|  | 526 | return typeInst; | 
|---|
| [a08ba92] | 527 | } | 
|---|
| [c8ffe20b] | 528 |  | 
|---|
| [85c4ef0] | 529 | Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) { | 
|---|
| [0dd3a2f] | 530 | Declaration *ret = Mutator::mutate( tyDecl ); | 
|---|
| [cc79d97] | 531 | if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) { | 
|---|
| [9cb8e88d] | 532 | // typedef to the same name from the same scope | 
|---|
| [cc79d97] | 533 | // must be from the same type | 
|---|
|  | 534 |  | 
|---|
|  | 535 | Type * t1 = tyDecl->get_base(); | 
|---|
|  | 536 | Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base(); | 
|---|
| [1cbca6e] | 537 | if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) { | 
|---|
| [cc79d97] | 538 | throw SemanticError( "cannot redefine typedef: " + tyDecl->get_name() ); | 
|---|
| [85c4ef0] | 539 | } | 
|---|
| [cc79d97] | 540 | } else { | 
|---|
|  | 541 | typedefNames[ tyDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel ); | 
|---|
|  | 542 | } // if | 
|---|
|  | 543 |  | 
|---|
| [0dd3a2f] | 544 | // When a typedef is a forward declaration: | 
|---|
|  | 545 | //    typedef struct screen SCREEN; | 
|---|
|  | 546 | // the declaration portion must be retained: | 
|---|
|  | 547 | //    struct screen; | 
|---|
|  | 548 | // because the expansion of the typedef is: | 
|---|
|  | 549 | //    void rtn( SCREEN *p ) => void rtn( struct screen *p ) | 
|---|
|  | 550 | // hence the type-name "screen" must be defined. | 
|---|
|  | 551 | // Note, qualifiers on the typedef are superfluous for the forward declaration. | 
|---|
|  | 552 | if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( tyDecl->get_base() ) ) { | 
|---|
|  | 553 | return new StructDecl( aggDecl->get_name() ); | 
|---|
|  | 554 | } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( tyDecl->get_base() ) ) { | 
|---|
|  | 555 | return new UnionDecl( aggDecl->get_name() ); | 
|---|
|  | 556 | } else { | 
|---|
|  | 557 | return ret; | 
|---|
|  | 558 | } // if | 
|---|
| [a08ba92] | 559 | } | 
|---|
| [c8ffe20b] | 560 |  | 
|---|
| [85c4ef0] | 561 | TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) { | 
|---|
| [cc79d97] | 562 | TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() ); | 
|---|
| [0dd3a2f] | 563 | if ( i != typedefNames.end() ) { | 
|---|
|  | 564 | typedefNames.erase( i ) ; | 
|---|
|  | 565 | } // if | 
|---|
|  | 566 | return typeDecl; | 
|---|
| [a08ba92] | 567 | } | 
|---|
| [c8ffe20b] | 568 |  | 
|---|
| [85c4ef0] | 569 | DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) { | 
|---|
| [cc79d97] | 570 | TypedefMap oldNames = typedefNames; | 
|---|
| [0dd3a2f] | 571 | DeclarationWithType *ret = Mutator::mutate( funcDecl ); | 
|---|
|  | 572 | typedefNames = oldNames; | 
|---|
|  | 573 | return ret; | 
|---|
| [a08ba92] | 574 | } | 
|---|
| [c8ffe20b] | 575 |  | 
|---|
| [1db21619] | 576 | DeclarationWithType *EliminateTypedef::mutate( ObjectDecl * objDecl ) { | 
|---|
| [cc79d97] | 577 | TypedefMap oldNames = typedefNames; | 
|---|
| [1db21619] | 578 | DeclarationWithType *ret = Mutator::mutate( objDecl ); | 
|---|
| [ae4c85a] | 579 | typedefNames = oldNames; | 
|---|
| [02e5ab6] | 580 | // is the type a function? | 
|---|
| [1db21619] | 581 | if ( FunctionType *funtype = dynamic_cast<FunctionType *>( ret->get_type() ) ) { | 
|---|
| [02e5ab6] | 582 | // replace the current object declaration with a function declaration | 
|---|
| [1db21619] | 583 | return new FunctionDecl( ret->get_name(), ret->get_storageClass(), ret->get_linkage(), funtype, 0, ret->get_isInline(), ret->get_isNoreturn() ); | 
|---|
|  | 584 | } else if ( objDecl->get_isInline() || objDecl->get_isNoreturn() ) { | 
|---|
|  | 585 | throw SemanticError( "invalid inline or _Noreturn specification in declaration of ", objDecl ); | 
|---|
|  | 586 | } // if | 
|---|
| [0dd3a2f] | 587 | return ret; | 
|---|
| [a08ba92] | 588 | } | 
|---|
| [c8ffe20b] | 589 |  | 
|---|
| [85c4ef0] | 590 | Expression *EliminateTypedef::mutate( CastExpr * castExpr ) { | 
|---|
| [cc79d97] | 591 | TypedefMap oldNames = typedefNames; | 
|---|
| [0dd3a2f] | 592 | Expression *ret = Mutator::mutate( castExpr ); | 
|---|
|  | 593 | typedefNames = oldNames; | 
|---|
|  | 594 | return ret; | 
|---|
| [a08ba92] | 595 | } | 
|---|
| [c8ffe20b] | 596 |  | 
|---|
| [85c4ef0] | 597 | CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) { | 
|---|
| [cc79d97] | 598 | TypedefMap oldNames = typedefNames; | 
|---|
|  | 599 | scopeLevel += 1; | 
|---|
| [0dd3a2f] | 600 | CompoundStmt *ret = Mutator::mutate( compoundStmt ); | 
|---|
| [cc79d97] | 601 | scopeLevel -= 1; | 
|---|
| [0dd3a2f] | 602 | std::list< Statement * >::iterator i = compoundStmt->get_kids().begin(); | 
|---|
|  | 603 | while ( i != compoundStmt->get_kids().end() ) { | 
|---|
| [85c4ef0] | 604 | std::list< Statement * >::iterator next = i+1; | 
|---|
| [0dd3a2f] | 605 | if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) { | 
|---|
|  | 606 | if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) { | 
|---|
|  | 607 | delete *i; | 
|---|
|  | 608 | compoundStmt->get_kids().erase( i ); | 
|---|
|  | 609 | } // if | 
|---|
|  | 610 | } // if | 
|---|
|  | 611 | i = next; | 
|---|
|  | 612 | } // while | 
|---|
|  | 613 | typedefNames = oldNames; | 
|---|
|  | 614 | return ret; | 
|---|
| [a08ba92] | 615 | } | 
|---|
| [85c4ef0] | 616 |  | 
|---|
| [45161b4d] | 617 | // there may be typedefs nested within aggregates in order for everything to work properly, these should be removed | 
|---|
|  | 618 | // as well | 
|---|
| [85c4ef0] | 619 | template<typename AggDecl> | 
|---|
|  | 620 | AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) { | 
|---|
|  | 621 | std::list<Declaration *>::iterator it = aggDecl->get_members().begin(); | 
|---|
|  | 622 | for ( ; it != aggDecl->get_members().end(); ) { | 
|---|
|  | 623 | std::list< Declaration * >::iterator next = it+1; | 
|---|
|  | 624 | if ( dynamic_cast< TypedefDecl * >( *it ) ) { | 
|---|
|  | 625 | delete *it; | 
|---|
|  | 626 | aggDecl->get_members().erase( it ); | 
|---|
|  | 627 | } // if | 
|---|
|  | 628 | it = next; | 
|---|
|  | 629 | } | 
|---|
|  | 630 | return aggDecl; | 
|---|
|  | 631 | } | 
|---|
|  | 632 |  | 
|---|
| [45161b4d] | 633 | template<typename AggDecl> | 
|---|
|  | 634 | void EliminateTypedef::addImplicitTypedef( AggDecl * aggDecl ) { | 
|---|
|  | 635 | if ( typedefNames.count( aggDecl->get_name() ) == 0 ) { | 
|---|
|  | 636 | Type *type; | 
|---|
|  | 637 | if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( aggDecl ) ) { | 
|---|
|  | 638 | type = new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ); | 
|---|
|  | 639 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( aggDecl ) ) { | 
|---|
|  | 640 | type = new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ); | 
|---|
|  | 641 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( aggDecl )  ) { | 
|---|
|  | 642 | type = new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ); | 
|---|
|  | 643 | } // if | 
|---|
|  | 644 | TypedefDecl * tyDecl = new TypedefDecl( aggDecl->get_name(), DeclarationNode::NoStorageClass, type ); | 
|---|
|  | 645 | typedefNames[ aggDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel ); | 
|---|
|  | 646 | } // if | 
|---|
|  | 647 | } | 
|---|
| [85c4ef0] | 648 | Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) { | 
|---|
| [45161b4d] | 649 | addImplicitTypedef( structDecl ); | 
|---|
| [85c4ef0] | 650 | Mutator::mutate( structDecl ); | 
|---|
|  | 651 | return handleAggregate( structDecl ); | 
|---|
|  | 652 | } | 
|---|
|  | 653 |  | 
|---|
|  | 654 | Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) { | 
|---|
| [45161b4d] | 655 | addImplicitTypedef( unionDecl ); | 
|---|
| [85c4ef0] | 656 | Mutator::mutate( unionDecl ); | 
|---|
|  | 657 | return handleAggregate( unionDecl ); | 
|---|
|  | 658 | } | 
|---|
|  | 659 |  | 
|---|
|  | 660 | Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) { | 
|---|
| [45161b4d] | 661 | addImplicitTypedef( enumDecl ); | 
|---|
| [85c4ef0] | 662 | Mutator::mutate( enumDecl ); | 
|---|
|  | 663 | return handleAggregate( enumDecl ); | 
|---|
|  | 664 | } | 
|---|
|  | 665 |  | 
|---|
| [45161b4d] | 666 | Declaration *EliminateTypedef::mutate( TraitDecl * contextDecl ) { | 
|---|
| [85c4ef0] | 667 | Mutator::mutate( contextDecl ); | 
|---|
|  | 668 | return handleAggregate( contextDecl ); | 
|---|
|  | 669 | } | 
|---|
|  | 670 |  | 
|---|
| [9cb8e88d] | 671 | void VerifyCtorDtor::verify( std::list< Declaration * > & translationUnit ) { | 
|---|
|  | 672 | VerifyCtorDtor verifier; | 
|---|
|  | 673 | acceptAll( translationUnit, verifier ); | 
|---|
|  | 674 | } | 
|---|
|  | 675 |  | 
|---|
|  | 676 | void VerifyCtorDtor::visit( FunctionDecl * funcDecl ) { | 
|---|
|  | 677 | FunctionType * funcType = funcDecl->get_functionType(); | 
|---|
|  | 678 | std::list< DeclarationWithType * > &returnVals = funcType->get_returnVals(); | 
|---|
|  | 679 | std::list< DeclarationWithType * > ¶ms = funcType->get_parameters(); | 
|---|
|  | 680 |  | 
|---|
|  | 681 | if ( funcDecl->get_name() == "?{}" || funcDecl->get_name() == "^?{}" ) { | 
|---|
|  | 682 | if ( params.size() == 0 ) { | 
|---|
|  | 683 | throw SemanticError( "Constructors and destructors require at least one parameter ", funcDecl ); | 
|---|
|  | 684 | } | 
|---|
|  | 685 | if ( ! dynamic_cast< PointerType * >( params.front()->get_type() ) ) { | 
|---|
|  | 686 | throw SemanticError( "First parameter of a constructor or destructor must be a pointer ", funcDecl ); | 
|---|
|  | 687 | } | 
|---|
|  | 688 | if ( returnVals.size() != 0 ) { | 
|---|
|  | 689 | throw SemanticError( "Constructors and destructors cannot have explicit return values ", funcDecl ); | 
|---|
|  | 690 | } | 
|---|
|  | 691 | } | 
|---|
|  | 692 |  | 
|---|
|  | 693 | Visitor::visit( funcDecl ); | 
|---|
|  | 694 | // original idea: modify signature of ctor/dtors and insert appropriate return statements | 
|---|
|  | 695 | // to cause desired behaviour | 
|---|
|  | 696 | // new idea: add comma exprs to every ctor call to produce first parameter. | 
|---|
|  | 697 | // this requires some memoization of the first parameter, because it can be a | 
|---|
|  | 698 | // complicated expression with side effects (see: malloc). idea: add temporary variable | 
|---|
|  | 699 | // that is assigned address of constructed object in ctor argument position and | 
|---|
|  | 700 | // return the temporary. It should also be done after all implicit ctors are | 
|---|
|  | 701 | // added, so not in this pass! | 
|---|
|  | 702 | } | 
|---|
| [70a06f6] | 703 |  | 
|---|
| [630a82a] | 704 | DeclarationWithType * CompoundLiteral::mutate( ObjectDecl *objectDecl ) { | 
|---|
|  | 705 | storageclass = objectDecl->get_storageClass(); | 
|---|
|  | 706 | DeclarationWithType * temp = Mutator::mutate( objectDecl ); | 
|---|
|  | 707 | storageclass = DeclarationNode::NoStorageClass; | 
|---|
|  | 708 | return temp; | 
|---|
|  | 709 | } | 
|---|
|  | 710 |  | 
|---|
|  | 711 | Expression *CompoundLiteral::mutate( CompoundLiteralExpr *compLitExpr ) { | 
|---|
|  | 712 | // transform [storage_class] ... (struct S){ 3, ... }; | 
|---|
|  | 713 | // into [storage_class] struct S temp =  { 3, ... }; | 
|---|
|  | 714 | static UniqueName indexName( "_compLit" ); | 
|---|
|  | 715 |  | 
|---|
|  | 716 | ObjectDecl *tempvar = new ObjectDecl( indexName.newName(), storageclass, LinkageSpec::C, 0, compLitExpr->get_type(), compLitExpr->get_initializer() ); | 
|---|
|  | 717 | compLitExpr->set_type( 0 ); | 
|---|
|  | 718 | compLitExpr->set_initializer( 0 ); | 
|---|
|  | 719 | delete compLitExpr; | 
|---|
|  | 720 | DeclarationWithType * newtempvar = mutate( tempvar ); | 
|---|
|  | 721 | addDeclaration( newtempvar );                                   // add modified temporary to current block | 
|---|
|  | 722 | return new VariableExpr( newtempvar ); | 
|---|
|  | 723 | } | 
|---|
| [51b73452] | 724 | } // namespace SymTab | 
|---|
| [0dd3a2f] | 725 |  | 
|---|
|  | 726 | // Local Variables: // | 
|---|
|  | 727 | // tab-width: 4 // | 
|---|
|  | 728 | // mode: c++ // | 
|---|
|  | 729 | // compile-command: "make install" // | 
|---|
|  | 730 | // End: // | 
|---|