[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 |
---|
[4e06c1e] | 11 | // Last Modified By : Peter A. Buhr |
---|
[fbcde64] | 12 | // Last Modified On : Thu Mar 30 16:50:13 2017 |
---|
| 13 | // Update Count : 357 |
---|
[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 | // |
---|
[3c13c03] | 25 | // - The type "void" never occurs in lists of function parameter or return types. A function |
---|
| 26 | // taking no arguments has no argument types. |
---|
[0dd3a2f] | 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. |
---|
[51b7345] | 39 | |
---|
[0db6fc0] | 40 | #include <algorithm> |
---|
[51b7345] | 41 | #include <iterator> |
---|
[0db6fc0] | 42 | #include <list> |
---|
| 43 | |
---|
| 44 | #include "CodeGen/CodeGenerator.h" |
---|
| 45 | |
---|
| 46 | #include "Common/PassVisitor.h" |
---|
[e491159] | 47 | #include "Common/ScopedMap.h" |
---|
[630a82a] | 48 | #include "Common/UniqueName.h" |
---|
[0db6fc0] | 49 | #include "Common/utility.h" |
---|
| 50 | |
---|
[bcda04c] | 51 | #include "Concurrency/Keywords.h" |
---|
[0db6fc0] | 52 | |
---|
[630a82a] | 53 | #include "GenPoly/DeclMutator.h" |
---|
[0db6fc0] | 54 | |
---|
| 55 | #include "InitTweak/InitTweak.h" |
---|
| 56 | |
---|
[51b7345] | 57 | #include "AddVisit.h" |
---|
[0db6fc0] | 58 | #include "Autogen.h" |
---|
| 59 | #include "FixFunction.h" |
---|
| 60 | // #include "ImplementationType.h" |
---|
| 61 | #include "Indexer.h" |
---|
[f6d7e0f] | 62 | #include "MakeLibCfa.h" |
---|
[cc79d97] | 63 | #include "TypeEquality.h" |
---|
[0db6fc0] | 64 | #include "Validate.h" |
---|
| 65 | |
---|
[1cbca6e] | 66 | #include "ResolvExpr/typeops.h" |
---|
[0db6fc0] | 67 | |
---|
| 68 | #include "SynTree/Expression.h" |
---|
| 69 | #include "SynTree/Mutator.h" |
---|
| 70 | #include "SynTree/Statement.h" |
---|
| 71 | #include "SynTree/Type.h" |
---|
| 72 | #include "SynTree/TypeSubstitution.h" |
---|
| 73 | #include "SynTree/Visitor.h" |
---|
[51b7345] | 74 | |
---|
[c8ffe20b] | 75 | #define debugPrint( x ) if ( doDebug ) { std::cout << x; } |
---|
[51b7345] | 76 | |
---|
| 77 | namespace SymTab { |
---|
[9facf3b] | 78 | class HoistStruct final : public Visitor { |
---|
[c0aa336] | 79 | template< typename Visitor > |
---|
| 80 | friend void acceptAndAdd( std::list< Declaration * > &translationUnit, Visitor &visitor ); |
---|
| 81 | template< typename Visitor > |
---|
| 82 | friend void addVisitStatementList( std::list< Statement* > &stmts, Visitor &visitor ); |
---|
[a08ba92] | 83 | public: |
---|
[82dd287] | 84 | /// Flattens nested struct types |
---|
[0dd3a2f] | 85 | static void hoistStruct( std::list< Declaration * > &translationUnit ); |
---|
[9cb8e88d] | 86 | |
---|
[0dd3a2f] | 87 | std::list< Declaration * > &get_declsToAdd() { return declsToAdd; } |
---|
[9cb8e88d] | 88 | |
---|
[c0aa336] | 89 | virtual void visit( EnumInstType *enumInstType ); |
---|
| 90 | virtual void visit( StructInstType *structInstType ); |
---|
| 91 | virtual void visit( UnionInstType *unionInstType ); |
---|
[0dd3a2f] | 92 | virtual void visit( StructDecl *aggregateDecl ); |
---|
| 93 | virtual void visit( UnionDecl *aggregateDecl ); |
---|
[c8ffe20b] | 94 | |
---|
[0dd3a2f] | 95 | virtual void visit( CompoundStmt *compoundStmt ); |
---|
| 96 | virtual void visit( SwitchStmt *switchStmt ); |
---|
[a08ba92] | 97 | private: |
---|
[0dd3a2f] | 98 | HoistStruct(); |
---|
[c8ffe20b] | 99 | |
---|
[0dd3a2f] | 100 | template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl ); |
---|
[c8ffe20b] | 101 | |
---|
[c0aa336] | 102 | std::list< Declaration * > declsToAdd, declsToAddAfter; |
---|
[0dd3a2f] | 103 | bool inStruct; |
---|
[a08ba92] | 104 | }; |
---|
[c8ffe20b] | 105 | |
---|
[cce9429] | 106 | /// Fix return types so that every function returns exactly one value |
---|
[0db6fc0] | 107 | class ReturnTypeFixer { |
---|
[cce9429] | 108 | public: |
---|
| 109 | static void fix( std::list< Declaration * > &translationUnit ); |
---|
| 110 | |
---|
[0db6fc0] | 111 | void postvisit( FunctionDecl * functionDecl ); |
---|
| 112 | void postvisit( FunctionType * ftype ); |
---|
[cce9429] | 113 | }; |
---|
| 114 | |
---|
[de91427b] | 115 | /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers. |
---|
[9facf3b] | 116 | class EnumAndPointerDecayPass final : public Visitor { |
---|
[0dd3a2f] | 117 | typedef Visitor Parent; |
---|
| 118 | virtual void visit( EnumDecl *aggregateDecl ); |
---|
| 119 | virtual void visit( FunctionType *func ); |
---|
[a08ba92] | 120 | }; |
---|
[82dd287] | 121 | |
---|
| 122 | /// Associates forward declarations of aggregates with their definitions |
---|
[cce9429] | 123 | class LinkReferenceToTypes final : public Indexer { |
---|
[0dd3a2f] | 124 | typedef Indexer Parent; |
---|
[a08ba92] | 125 | public: |
---|
[cce9429] | 126 | LinkReferenceToTypes( bool doDebug, const Indexer *indexer ); |
---|
[a08ba92] | 127 | private: |
---|
[4a9ccc3] | 128 | using Parent::visit; |
---|
[c0aa336] | 129 | void visit( EnumInstType *enumInst ) final; |
---|
[62e5546] | 130 | void visit( StructInstType *structInst ) final; |
---|
| 131 | void visit( UnionInstType *unionInst ) final; |
---|
| 132 | void visit( TraitInstType *contextInst ) final; |
---|
[c0aa336] | 133 | void visit( EnumDecl *enumDecl ) final; |
---|
[62e5546] | 134 | void visit( StructDecl *structDecl ) final; |
---|
| 135 | void visit( UnionDecl *unionDecl ) final; |
---|
| 136 | void visit( TypeInstType *typeInst ) final; |
---|
[0dd3a2f] | 137 | |
---|
| 138 | const Indexer *indexer; |
---|
[9cb8e88d] | 139 | |
---|
[c0aa336] | 140 | typedef std::map< std::string, std::list< EnumInstType * > > ForwardEnumsType; |
---|
[0dd3a2f] | 141 | typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType; |
---|
| 142 | typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType; |
---|
[c0aa336] | 143 | ForwardEnumsType forwardEnums; |
---|
[0dd3a2f] | 144 | ForwardStructsType forwardStructs; |
---|
| 145 | ForwardUnionsType forwardUnions; |
---|
[a08ba92] | 146 | }; |
---|
[c8ffe20b] | 147 | |
---|
[82dd287] | 148 | /// Replaces array and function types in forall lists by appropriate pointer type |
---|
[4a9ccc3] | 149 | class Pass3 final : public Indexer { |
---|
[0dd3a2f] | 150 | typedef Indexer Parent; |
---|
[a08ba92] | 151 | public: |
---|
[4a9ccc3] | 152 | using Parent::visit; |
---|
[0dd3a2f] | 153 | Pass3( const Indexer *indexer ); |
---|
[a08ba92] | 154 | private: |
---|
[4a9ccc3] | 155 | virtual void visit( ObjectDecl *object ) override; |
---|
| 156 | virtual void visit( FunctionDecl *func ) override; |
---|
[c8ffe20b] | 157 | |
---|
[0dd3a2f] | 158 | const Indexer *indexer; |
---|
[a08ba92] | 159 | }; |
---|
[c8ffe20b] | 160 | |
---|
[0db6fc0] | 161 | class ReturnChecker { |
---|
[de91427b] | 162 | public: |
---|
| 163 | /// Checks that return statements return nothing if their return type is void |
---|
| 164 | /// and return something if the return type is non-void. |
---|
| 165 | static void checkFunctionReturns( std::list< Declaration * > & translationUnit ); |
---|
| 166 | private: |
---|
[0db6fc0] | 167 | void previsit( FunctionDecl * functionDecl ); |
---|
| 168 | void postvisit( FunctionDecl * functionDecl ); |
---|
| 169 | void previsit( ReturnStmt * returnStmt ); |
---|
[de91427b] | 170 | |
---|
[0db6fc0] | 171 | typedef std::list< DeclarationWithType * > ReturnVals; |
---|
| 172 | ReturnVals returnVals; |
---|
| 173 | std::stack< ReturnVals > returnValsStack; |
---|
[de91427b] | 174 | }; |
---|
| 175 | |
---|
[a08ba92] | 176 | class EliminateTypedef : public Mutator { |
---|
| 177 | public: |
---|
[de91427b] | 178 | EliminateTypedef() : scopeLevel( 0 ) {} |
---|
| 179 | /// Replaces typedefs by forward declarations |
---|
[0dd3a2f] | 180 | static void eliminateTypedef( std::list< Declaration * > &translationUnit ); |
---|
[a08ba92] | 181 | private: |
---|
[0dd3a2f] | 182 | virtual Declaration *mutate( TypedefDecl *typeDecl ); |
---|
| 183 | virtual TypeDecl *mutate( TypeDecl *typeDecl ); |
---|
| 184 | virtual DeclarationWithType *mutate( FunctionDecl *funcDecl ); |
---|
[1db21619] | 185 | virtual DeclarationWithType *mutate( ObjectDecl *objDecl ); |
---|
[0dd3a2f] | 186 | virtual CompoundStmt *mutate( CompoundStmt *compoundStmt ); |
---|
| 187 | virtual Type *mutate( TypeInstType *aggregateUseType ); |
---|
| 188 | virtual Expression *mutate( CastExpr *castExpr ); |
---|
[cc79d97] | 189 | |
---|
[85c4ef0] | 190 | virtual Declaration *mutate( StructDecl * structDecl ); |
---|
| 191 | virtual Declaration *mutate( UnionDecl * unionDecl ); |
---|
| 192 | virtual Declaration *mutate( EnumDecl * enumDecl ); |
---|
[4040425] | 193 | virtual Declaration *mutate( TraitDecl * contextDecl ); |
---|
[85c4ef0] | 194 | |
---|
| 195 | template<typename AggDecl> |
---|
| 196 | AggDecl *handleAggregate( AggDecl * aggDecl ); |
---|
| 197 | |
---|
[45161b4d] | 198 | template<typename AggDecl> |
---|
| 199 | void addImplicitTypedef( AggDecl * aggDecl ); |
---|
[70a06f6] | 200 | |
---|
[46f6134] | 201 | typedef std::unique_ptr<TypedefDecl> TypedefDeclPtr; |
---|
[e491159] | 202 | typedef ScopedMap< std::string, std::pair< TypedefDeclPtr, int > > TypedefMap; |
---|
[679864e1] | 203 | typedef std::map< std::string, TypeDecl * > TypeDeclMap; |
---|
[cc79d97] | 204 | TypedefMap typedefNames; |
---|
[679864e1] | 205 | TypeDeclMap typedeclNames; |
---|
[cc79d97] | 206 | int scopeLevel; |
---|
[a08ba92] | 207 | }; |
---|
[c8ffe20b] | 208 | |
---|
[0db6fc0] | 209 | class VerifyCtorDtorAssign { |
---|
[9cb8e88d] | 210 | public: |
---|
[d1969a6] | 211 | /// ensure that constructors, destructors, and assignment have at least one |
---|
| 212 | /// parameter, the first of which must be a pointer, and that ctor/dtors have no |
---|
[9cb8e88d] | 213 | /// return values. |
---|
| 214 | static void verify( std::list< Declaration * > &translationUnit ); |
---|
| 215 | |
---|
[0db6fc0] | 216 | void previsit( FunctionDecl *funcDecl ); |
---|
[5f98ce5] | 217 | }; |
---|
[70a06f6] | 218 | |
---|
[11ab8ea8] | 219 | /// ensure that generic types have the correct number of type arguments |
---|
[0db6fc0] | 220 | class ValidateGenericParameters { |
---|
[11ab8ea8] | 221 | public: |
---|
[0db6fc0] | 222 | void previsit( StructInstType * inst ); |
---|
| 223 | void previsit( UnionInstType * inst ); |
---|
[11ab8ea8] | 224 | }; |
---|
| 225 | |
---|
[0db6fc0] | 226 | class ArrayLength { |
---|
[fbd7ad6] | 227 | public: |
---|
| 228 | /// for array types without an explicit length, compute the length and store it so that it |
---|
| 229 | /// is known to the rest of the phases. For example, |
---|
| 230 | /// int x[] = { 1, 2, 3 }; |
---|
| 231 | /// int y[][2] = { { 1, 2, 3 }, { 1, 2, 3 } }; |
---|
| 232 | /// here x and y are known at compile-time to have length 3, so change this into |
---|
| 233 | /// int x[3] = { 1, 2, 3 }; |
---|
| 234 | /// int y[3][2] = { { 1, 2, 3 }, { 1, 2, 3 } }; |
---|
| 235 | static void computeLength( std::list< Declaration * > & translationUnit ); |
---|
| 236 | |
---|
[0db6fc0] | 237 | void previsit( ObjectDecl * objDecl ); |
---|
[fbd7ad6] | 238 | }; |
---|
| 239 | |
---|
[62e5546] | 240 | class CompoundLiteral final : public GenPoly::DeclMutator { |
---|
[68fe077a] | 241 | Type::StorageClasses storageClasses; |
---|
[630a82a] | 242 | |
---|
[62e5546] | 243 | using GenPoly::DeclMutator::mutate; |
---|
| 244 | DeclarationWithType * mutate( ObjectDecl *objectDecl ) final; |
---|
| 245 | Expression *mutate( CompoundLiteralExpr *compLitExpr ) final; |
---|
[9cb8e88d] | 246 | }; |
---|
| 247 | |
---|
[a08ba92] | 248 | void validate( std::list< Declaration * > &translationUnit, bool doDebug ) { |
---|
[bda58ad] | 249 | EnumAndPointerDecayPass epc; |
---|
[cce9429] | 250 | LinkReferenceToTypes lrt( doDebug, 0 ); |
---|
[0dd3a2f] | 251 | Pass3 pass3( 0 ); |
---|
[630a82a] | 252 | CompoundLiteral compoundliteral; |
---|
[0db6fc0] | 253 | PassVisitor<ValidateGenericParameters> genericParams; |
---|
[630a82a] | 254 | |
---|
[fbcde64] | 255 | EliminateTypedef::eliminateTypedef( translationUnit ); |
---|
[11ab8ea8] | 256 | HoistStruct::hoistStruct( translationUnit ); // must happen after EliminateTypedef, so that aggregate typedefs occur in the correct order |
---|
[cce9429] | 257 | ReturnTypeFixer::fix( translationUnit ); // must happen before autogen |
---|
[861799c] | 258 | acceptAll( translationUnit, lrt ); // must happen before autogen, because sized flag needs to propagate to generated functions |
---|
[11ab8ea8] | 259 | acceptAll( translationUnit, genericParams ); // check as early as possible - can't happen before LinkReferenceToTypes |
---|
[ed8a0d2] | 260 | acceptAll( translationUnit, epc ); // must happen before VerifyCtorDtorAssign, because void return objects should not exist |
---|
| 261 | VerifyCtorDtorAssign::verify( translationUnit ); // must happen before autogen, because autogen examines existing ctor/dtors |
---|
[bcda04c] | 262 | Concurrency::applyKeywords( translationUnit ); |
---|
[bda58ad] | 263 | autogenerateRoutines( translationUnit ); // moved up, used to be below compoundLiteral - currently needs EnumAndPointerDecayPass |
---|
[bcda04c] | 264 | Concurrency::implementMutexFuncs( translationUnit ); |
---|
| 265 | Concurrency::implementThreadStarter( translationUnit ); |
---|
[de91427b] | 266 | ReturnChecker::checkFunctionReturns( translationUnit ); |
---|
[40e636a] | 267 | compoundliteral.mutateDeclarationList( translationUnit ); |
---|
[0dd3a2f] | 268 | acceptAll( translationUnit, pass3 ); |
---|
[fbd7ad6] | 269 | ArrayLength::computeLength( translationUnit ); |
---|
[a08ba92] | 270 | } |
---|
[9cb8e88d] | 271 | |
---|
[a08ba92] | 272 | void validateType( Type *type, const Indexer *indexer ) { |
---|
[bda58ad] | 273 | EnumAndPointerDecayPass epc; |
---|
[cce9429] | 274 | LinkReferenceToTypes lrt( false, indexer ); |
---|
[0dd3a2f] | 275 | Pass3 pass3( indexer ); |
---|
[bda58ad] | 276 | type->accept( epc ); |
---|
[cce9429] | 277 | type->accept( lrt ); |
---|
[0dd3a2f] | 278 | type->accept( pass3 ); |
---|
[a08ba92] | 279 | } |
---|
[c8ffe20b] | 280 | |
---|
[a08ba92] | 281 | void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) { |
---|
[0dd3a2f] | 282 | HoistStruct hoister; |
---|
[c0aa336] | 283 | acceptAndAdd( translationUnit, hoister ); |
---|
[a08ba92] | 284 | } |
---|
[c8ffe20b] | 285 | |
---|
[a08ba92] | 286 | HoistStruct::HoistStruct() : inStruct( false ) { |
---|
| 287 | } |
---|
[c8ffe20b] | 288 | |
---|
[a08ba92] | 289 | void filter( std::list< Declaration * > &declList, bool (*pred)( Declaration * ), bool doDelete ) { |
---|
[0dd3a2f] | 290 | std::list< Declaration * >::iterator i = declList.begin(); |
---|
| 291 | while ( i != declList.end() ) { |
---|
| 292 | std::list< Declaration * >::iterator next = i; |
---|
| 293 | ++next; |
---|
| 294 | if ( pred( *i ) ) { |
---|
| 295 | if ( doDelete ) { |
---|
| 296 | delete *i; |
---|
| 297 | } // if |
---|
| 298 | declList.erase( i ); |
---|
| 299 | } // if |
---|
| 300 | i = next; |
---|
| 301 | } // while |
---|
[a08ba92] | 302 | } |
---|
[c8ffe20b] | 303 | |
---|
[a08ba92] | 304 | bool isStructOrUnion( Declaration *decl ) { |
---|
[0dd3a2f] | 305 | return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl ); |
---|
[a08ba92] | 306 | } |
---|
[c0aa336] | 307 | |
---|
[a08ba92] | 308 | template< typename AggDecl > |
---|
| 309 | void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 310 | if ( inStruct ) { |
---|
| 311 | // Add elements in stack order corresponding to nesting structure. |
---|
| 312 | declsToAdd.push_front( aggregateDecl ); |
---|
| 313 | Visitor::visit( aggregateDecl ); |
---|
| 314 | } else { |
---|
| 315 | inStruct = true; |
---|
| 316 | Visitor::visit( aggregateDecl ); |
---|
| 317 | inStruct = false; |
---|
| 318 | } // if |
---|
| 319 | // Always remove the hoisted aggregate from the inner structure. |
---|
| 320 | filter( aggregateDecl->get_members(), isStructOrUnion, false ); |
---|
[a08ba92] | 321 | } |
---|
[c8ffe20b] | 322 | |
---|
[c0aa336] | 323 | void HoistStruct::visit( EnumInstType *structInstType ) { |
---|
| 324 | if ( structInstType->get_baseEnum() ) { |
---|
| 325 | declsToAdd.push_front( structInstType->get_baseEnum() ); |
---|
| 326 | } |
---|
| 327 | } |
---|
| 328 | |
---|
| 329 | void HoistStruct::visit( StructInstType *structInstType ) { |
---|
| 330 | if ( structInstType->get_baseStruct() ) { |
---|
| 331 | declsToAdd.push_front( structInstType->get_baseStruct() ); |
---|
| 332 | } |
---|
| 333 | } |
---|
| 334 | |
---|
| 335 | void HoistStruct::visit( UnionInstType *structInstType ) { |
---|
| 336 | if ( structInstType->get_baseUnion() ) { |
---|
| 337 | declsToAdd.push_front( structInstType->get_baseUnion() ); |
---|
| 338 | } |
---|
| 339 | } |
---|
| 340 | |
---|
[a08ba92] | 341 | void HoistStruct::visit( StructDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 342 | handleAggregate( aggregateDecl ); |
---|
[a08ba92] | 343 | } |
---|
[c8ffe20b] | 344 | |
---|
[a08ba92] | 345 | void HoistStruct::visit( UnionDecl *aggregateDecl ) { |
---|
[0dd3a2f] | 346 | handleAggregate( aggregateDecl ); |
---|
[a08ba92] | 347 | } |
---|
[c8ffe20b] | 348 | |
---|
[a08ba92] | 349 | void HoistStruct::visit( CompoundStmt *compoundStmt ) { |
---|
[0dd3a2f] | 350 | addVisit( compoundStmt, *this ); |
---|
[a08ba92] | 351 | } |
---|
[c8ffe20b] | 352 | |
---|
[a08ba92] | 353 | void HoistStruct::visit( SwitchStmt *switchStmt ) { |
---|
[0dd3a2f] | 354 | addVisit( switchStmt, *this ); |
---|
[a08ba92] | 355 | } |
---|
[c8ffe20b] | 356 | |
---|
[bda58ad] | 357 | void EnumAndPointerDecayPass::visit( EnumDecl *enumDecl ) { |
---|
[0dd3a2f] | 358 | // Set the type of each member of the enumeration to be EnumConstant |
---|
| 359 | for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) { |
---|
[f6d7e0f] | 360 | ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i ); |
---|
[0dd3a2f] | 361 | assert( obj ); |
---|
[f2e40a9f] | 362 | obj->set_type( new EnumInstType( Type::Qualifiers( Type::Const ), enumDecl->get_name() ) ); |
---|
[0dd3a2f] | 363 | } // for |
---|
| 364 | Parent::visit( enumDecl ); |
---|
[a08ba92] | 365 | } |
---|
[51b7345] | 366 | |
---|
[a08ba92] | 367 | namespace { |
---|
[83de11e] | 368 | template< typename DWTList > |
---|
| 369 | void fixFunctionList( DWTList & dwts, FunctionType * func ) { |
---|
[0dd3a2f] | 370 | // the only case in which "void" is valid is where it is the only one in the list; then it should be removed |
---|
| 371 | // entirely other fix ups are handled by the FixFunction class |
---|
[83de11e] | 372 | typedef typename DWTList::iterator DWTIterator; |
---|
| 373 | DWTIterator begin( dwts.begin() ), end( dwts.end() ); |
---|
[0dd3a2f] | 374 | if ( begin == end ) return; |
---|
| 375 | FixFunction fixer; |
---|
| 376 | DWTIterator i = begin; |
---|
[83de11e] | 377 | *i = (*i)->acceptMutator( fixer ); |
---|
[0dd3a2f] | 378 | if ( fixer.get_isVoid() ) { |
---|
| 379 | DWTIterator j = i; |
---|
| 380 | ++i; |
---|
[bda58ad] | 381 | delete *j; |
---|
[83de11e] | 382 | dwts.erase( j ); |
---|
[9cb8e88d] | 383 | if ( i != end ) { |
---|
[0dd3a2f] | 384 | throw SemanticError( "invalid type void in function type ", func ); |
---|
| 385 | } // if |
---|
| 386 | } else { |
---|
| 387 | ++i; |
---|
| 388 | for ( ; i != end; ++i ) { |
---|
| 389 | FixFunction fixer; |
---|
| 390 | *i = (*i )->acceptMutator( fixer ); |
---|
| 391 | if ( fixer.get_isVoid() ) { |
---|
| 392 | throw SemanticError( "invalid type void in function type ", func ); |
---|
| 393 | } // if |
---|
| 394 | } // for |
---|
| 395 | } // if |
---|
| 396 | } |
---|
[a08ba92] | 397 | } |
---|
[c8ffe20b] | 398 | |
---|
[bda58ad] | 399 | void EnumAndPointerDecayPass::visit( FunctionType *func ) { |
---|
[0dd3a2f] | 400 | // Fix up parameters and return types |
---|
[83de11e] | 401 | fixFunctionList( func->get_parameters(), func ); |
---|
| 402 | fixFunctionList( func->get_returnVals(), func ); |
---|
[0dd3a2f] | 403 | Visitor::visit( func ); |
---|
[a08ba92] | 404 | } |
---|
[c8ffe20b] | 405 | |
---|
[cce9429] | 406 | LinkReferenceToTypes::LinkReferenceToTypes( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug ) { |
---|
[0dd3a2f] | 407 | if ( other_indexer ) { |
---|
| 408 | indexer = other_indexer; |
---|
| 409 | } else { |
---|
| 410 | indexer = this; |
---|
| 411 | } // if |
---|
[a08ba92] | 412 | } |
---|
[c8ffe20b] | 413 | |
---|
[c0aa336] | 414 | void LinkReferenceToTypes::visit( EnumInstType *enumInst ) { |
---|
| 415 | Parent::visit( enumInst ); |
---|
| 416 | EnumDecl *st = indexer->lookupEnum( enumInst->get_name() ); |
---|
| 417 | // it's not a semantic error if the enum is not found, just an implicit forward declaration |
---|
| 418 | if ( st ) { |
---|
| 419 | //assert( ! enumInst->get_baseEnum() || enumInst->get_baseEnum()->get_members().empty() || ! st->get_members().empty() ); |
---|
| 420 | enumInst->set_baseEnum( st ); |
---|
| 421 | } // if |
---|
| 422 | if ( ! st || st->get_members().empty() ) { |
---|
| 423 | // use of forward declaration |
---|
| 424 | forwardEnums[ enumInst->get_name() ].push_back( enumInst ); |
---|
| 425 | } // if |
---|
| 426 | } |
---|
| 427 | |
---|
[cce9429] | 428 | void LinkReferenceToTypes::visit( StructInstType *structInst ) { |
---|
[0dd3a2f] | 429 | Parent::visit( structInst ); |
---|
| 430 | StructDecl *st = indexer->lookupStruct( structInst->get_name() ); |
---|
| 431 | // it's not a semantic error if the struct is not found, just an implicit forward declaration |
---|
| 432 | if ( st ) { |
---|
[98735ef] | 433 | //assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() ); |
---|
[0dd3a2f] | 434 | structInst->set_baseStruct( st ); |
---|
| 435 | } // if |
---|
| 436 | if ( ! st || st->get_members().empty() ) { |
---|
| 437 | // use of forward declaration |
---|
| 438 | forwardStructs[ structInst->get_name() ].push_back( structInst ); |
---|
| 439 | } // if |
---|
[a08ba92] | 440 | } |
---|
[c8ffe20b] | 441 | |
---|
[cce9429] | 442 | void LinkReferenceToTypes::visit( UnionInstType *unionInst ) { |
---|
[0dd3a2f] | 443 | Parent::visit( unionInst ); |
---|
| 444 | UnionDecl *un = indexer->lookupUnion( unionInst->get_name() ); |
---|
| 445 | // it's not a semantic error if the union is not found, just an implicit forward declaration |
---|
| 446 | if ( un ) { |
---|
| 447 | unionInst->set_baseUnion( un ); |
---|
| 448 | } // if |
---|
| 449 | if ( ! un || un->get_members().empty() ) { |
---|
| 450 | // use of forward declaration |
---|
| 451 | forwardUnions[ unionInst->get_name() ].push_back( unionInst ); |
---|
| 452 | } // if |
---|
[a08ba92] | 453 | } |
---|
[c8ffe20b] | 454 | |
---|
[4a9ccc3] | 455 | void LinkReferenceToTypes::visit( TraitInstType *traitInst ) { |
---|
| 456 | Parent::visit( traitInst ); |
---|
| 457 | if ( traitInst->get_name() == "sized" ) { |
---|
[2c57025] | 458 | // "sized" is a special trait with no members - just flick the sized status on for the type variable |
---|
[4a9ccc3] | 459 | if ( traitInst->get_parameters().size() != 1 ) { |
---|
| 460 | throw SemanticError( "incorrect number of trait parameters: ", traitInst ); |
---|
[2c57025] | 461 | } |
---|
[4a9ccc3] | 462 | TypeExpr * param = safe_dynamic_cast< TypeExpr * > ( traitInst->get_parameters().front() ); |
---|
[2c57025] | 463 | TypeInstType * inst = safe_dynamic_cast< TypeInstType * > ( param->get_type() ); |
---|
| 464 | TypeDecl * decl = inst->get_baseType(); |
---|
| 465 | decl->set_sized( true ); |
---|
| 466 | // since "sized" is special, the next few steps don't apply |
---|
| 467 | return; |
---|
| 468 | } |
---|
[4a9ccc3] | 469 | TraitDecl *traitDecl = indexer->lookupTrait( traitInst->get_name() ); |
---|
| 470 | if ( ! traitDecl ) { |
---|
| 471 | throw SemanticError( "use of undeclared trait " + traitInst->get_name() ); |
---|
[17cd4eb] | 472 | } // if |
---|
[4a9ccc3] | 473 | if ( traitDecl->get_parameters().size() != traitInst->get_parameters().size() ) { |
---|
| 474 | throw SemanticError( "incorrect number of trait parameters: ", traitInst ); |
---|
| 475 | } // if |
---|
| 476 | |
---|
| 477 | for ( TypeDecl * td : traitDecl->get_parameters() ) { |
---|
| 478 | for ( DeclarationWithType * assert : td->get_assertions() ) { |
---|
| 479 | traitInst->get_members().push_back( assert->clone() ); |
---|
[0dd3a2f] | 480 | } // for |
---|
| 481 | } // for |
---|
[51b986f] | 482 | |
---|
[4a9ccc3] | 483 | // need to clone members of the trait for ownership purposes |
---|
[79970ed] | 484 | std::list< Declaration * > members; |
---|
[4a9ccc3] | 485 | std::transform( traitDecl->get_members().begin(), traitDecl->get_members().end(), back_inserter( members ), [](Declaration * dwt) { return dwt->clone(); } ); |
---|
| 486 | |
---|
| 487 | applySubstitution( traitDecl->get_parameters().begin(), traitDecl->get_parameters().end(), traitInst->get_parameters().begin(), members.begin(), members.end(), back_inserter( traitInst->get_members() ) ); |
---|
[79970ed] | 488 | |
---|
[4a9ccc3] | 489 | // need to carry over the 'sized' status of each decl in the instance |
---|
| 490 | for ( auto p : group_iterate( traitDecl->get_parameters(), traitInst->get_parameters() ) ) { |
---|
| 491 | TypeExpr * expr = safe_dynamic_cast< TypeExpr * >( std::get<1>(p) ); |
---|
| 492 | if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( expr->get_type() ) ) { |
---|
| 493 | TypeDecl * formalDecl = std::get<0>(p); |
---|
| 494 | TypeDecl * instDecl = inst->get_baseType(); |
---|
| 495 | if ( formalDecl->get_sized() ) instDecl->set_sized( true ); |
---|
| 496 | } |
---|
| 497 | } |
---|
[a08ba92] | 498 | } |
---|
[c8ffe20b] | 499 | |
---|
[c0aa336] | 500 | void LinkReferenceToTypes::visit( EnumDecl *enumDecl ) { |
---|
| 501 | // visit enum members first so that the types of self-referencing members are updated properly |
---|
| 502 | Parent::visit( enumDecl ); |
---|
| 503 | if ( ! enumDecl->get_members().empty() ) { |
---|
| 504 | ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl->get_name() ); |
---|
| 505 | if ( fwds != forwardEnums.end() ) { |
---|
| 506 | for ( std::list< EnumInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
| 507 | (*inst )->set_baseEnum( enumDecl ); |
---|
| 508 | } // for |
---|
| 509 | forwardEnums.erase( fwds ); |
---|
| 510 | } // if |
---|
| 511 | } // if |
---|
| 512 | } |
---|
| 513 | |
---|
[cce9429] | 514 | void LinkReferenceToTypes::visit( StructDecl *structDecl ) { |
---|
[677c1be] | 515 | // visit struct members first so that the types of self-referencing members are updated properly |
---|
[67cf18c] | 516 | // xxx - need to ensure that type parameters match up between forward declarations and definition (most importantly, number of type parameters and and their defaults) |
---|
[677c1be] | 517 | Parent::visit( structDecl ); |
---|
[0dd3a2f] | 518 | if ( ! structDecl->get_members().empty() ) { |
---|
| 519 | ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() ); |
---|
| 520 | if ( fwds != forwardStructs.end() ) { |
---|
| 521 | for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
| 522 | (*inst )->set_baseStruct( structDecl ); |
---|
| 523 | } // for |
---|
| 524 | forwardStructs.erase( fwds ); |
---|
| 525 | } // if |
---|
| 526 | } // if |
---|
[a08ba92] | 527 | } |
---|
[c8ffe20b] | 528 | |
---|
[cce9429] | 529 | void LinkReferenceToTypes::visit( UnionDecl *unionDecl ) { |
---|
[677c1be] | 530 | Parent::visit( unionDecl ); |
---|
[0dd3a2f] | 531 | if ( ! unionDecl->get_members().empty() ) { |
---|
| 532 | ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() ); |
---|
| 533 | if ( fwds != forwardUnions.end() ) { |
---|
| 534 | for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
| 535 | (*inst )->set_baseUnion( unionDecl ); |
---|
| 536 | } // for |
---|
| 537 | forwardUnions.erase( fwds ); |
---|
| 538 | } // if |
---|
| 539 | } // if |
---|
[a08ba92] | 540 | } |
---|
[c8ffe20b] | 541 | |
---|
[cce9429] | 542 | void LinkReferenceToTypes::visit( TypeInstType *typeInst ) { |
---|
[0dd3a2f] | 543 | if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) { |
---|
| 544 | if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) { |
---|
| 545 | typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype ); |
---|
| 546 | } // if |
---|
| 547 | } // if |
---|
[a08ba92] | 548 | } |
---|
[c8ffe20b] | 549 | |
---|
[a08ba92] | 550 | Pass3::Pass3( const Indexer *other_indexer ) : Indexer( false ) { |
---|
[0dd3a2f] | 551 | if ( other_indexer ) { |
---|
| 552 | indexer = other_indexer; |
---|
| 553 | } else { |
---|
| 554 | indexer = this; |
---|
| 555 | } // if |
---|
[a08ba92] | 556 | } |
---|
[c8ffe20b] | 557 | |
---|
[4a9ccc3] | 558 | /// Fix up assertions - flattens assertion lists, removing all trait instances |
---|
| 559 | void forallFixer( Type * func ) { |
---|
| 560 | for ( TypeDecl * type : func->get_forall() ) { |
---|
[0dd3a2f] | 561 | std::list< DeclarationWithType * > toBeDone, nextRound; |
---|
[4a9ccc3] | 562 | toBeDone.splice( toBeDone.end(), type->get_assertions() ); |
---|
[0dd3a2f] | 563 | while ( ! toBeDone.empty() ) { |
---|
[4a9ccc3] | 564 | for ( DeclarationWithType * assertion : toBeDone ) { |
---|
| 565 | if ( TraitInstType *traitInst = dynamic_cast< TraitInstType * >( assertion->get_type() ) ) { |
---|
| 566 | // expand trait instance into all of its members |
---|
| 567 | for ( Declaration * member : traitInst->get_members() ) { |
---|
| 568 | DeclarationWithType *dwt = safe_dynamic_cast< DeclarationWithType * >( member ); |
---|
[0dd3a2f] | 569 | nextRound.push_back( dwt->clone() ); |
---|
| 570 | } |
---|
[4a9ccc3] | 571 | delete traitInst; |
---|
[0dd3a2f] | 572 | } else { |
---|
[4a9ccc3] | 573 | // pass assertion through |
---|
[0dd3a2f] | 574 | FixFunction fixer; |
---|
[4a9ccc3] | 575 | assertion = assertion->acceptMutator( fixer ); |
---|
[0dd3a2f] | 576 | if ( fixer.get_isVoid() ) { |
---|
| 577 | throw SemanticError( "invalid type void in assertion of function ", func ); |
---|
| 578 | } |
---|
[4a9ccc3] | 579 | type->get_assertions().push_back( assertion ); |
---|
[0dd3a2f] | 580 | } // if |
---|
| 581 | } // for |
---|
| 582 | toBeDone.clear(); |
---|
| 583 | toBeDone.splice( toBeDone.end(), nextRound ); |
---|
| 584 | } // while |
---|
| 585 | } // for |
---|
[a08ba92] | 586 | } |
---|
[c8ffe20b] | 587 | |
---|
[a08ba92] | 588 | void Pass3::visit( ObjectDecl *object ) { |
---|
[0dd3a2f] | 589 | forallFixer( object->get_type() ); |
---|
| 590 | if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) { |
---|
| 591 | forallFixer( pointer->get_base() ); |
---|
| 592 | } // if |
---|
| 593 | Parent::visit( object ); |
---|
| 594 | object->fixUniqueId(); |
---|
[a08ba92] | 595 | } |
---|
[c8ffe20b] | 596 | |
---|
[a08ba92] | 597 | void Pass3::visit( FunctionDecl *func ) { |
---|
[0dd3a2f] | 598 | forallFixer( func->get_type() ); |
---|
| 599 | Parent::visit( func ); |
---|
| 600 | func->fixUniqueId(); |
---|
[a08ba92] | 601 | } |
---|
[c8ffe20b] | 602 | |
---|
[de91427b] | 603 | void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) { |
---|
[0db6fc0] | 604 | PassVisitor<ReturnChecker> checker; |
---|
[de91427b] | 605 | acceptAll( translationUnit, checker ); |
---|
| 606 | } |
---|
| 607 | |
---|
[0db6fc0] | 608 | void ReturnChecker::previsit( FunctionDecl * functionDecl ) { |
---|
| 609 | returnValsStack.push( returnVals ); |
---|
[de91427b] | 610 | returnVals = functionDecl->get_functionType()->get_returnVals(); |
---|
[0db6fc0] | 611 | } |
---|
[af397ef8] | 612 | void ReturnChecker::postvisit( __attribute__((unused)) FunctionDecl * functionDecl ) { |
---|
[0db6fc0] | 613 | returnVals = returnValsStack.top(); |
---|
| 614 | returnValsStack.pop(); |
---|
[de91427b] | 615 | } |
---|
| 616 | |
---|
[0db6fc0] | 617 | void ReturnChecker::previsit( ReturnStmt * returnStmt ) { |
---|
[74d1804] | 618 | // Previously this also checked for the existence of an expr paired with no return values on |
---|
| 619 | // the function return type. This is incorrect, since you can have an expression attached to |
---|
| 620 | // a return statement in a void-returning function in C. The expression is treated as if it |
---|
| 621 | // were cast to void. |
---|
[de91427b] | 622 | if ( returnStmt->get_expr() == NULL && returnVals.size() != 0 ) { |
---|
| 623 | throw SemanticError( "Non-void function returns no values: " , returnStmt ); |
---|
| 624 | } |
---|
| 625 | } |
---|
| 626 | |
---|
| 627 | |
---|
[a08ba92] | 628 | bool isTypedef( Declaration *decl ) { |
---|
[0dd3a2f] | 629 | return dynamic_cast< TypedefDecl * >( decl ); |
---|
[a08ba92] | 630 | } |
---|
[c8ffe20b] | 631 | |
---|
[a08ba92] | 632 | void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) { |
---|
[0dd3a2f] | 633 | EliminateTypedef eliminator; |
---|
| 634 | mutateAll( translationUnit, eliminator ); |
---|
[5f98ce5] | 635 | if ( eliminator.typedefNames.count( "size_t" ) ) { |
---|
| 636 | // grab and remember declaration of size_t |
---|
| 637 | SizeType = eliminator.typedefNames["size_t"].first->get_base()->clone(); |
---|
| 638 | } else { |
---|
[40e636a] | 639 | // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong |
---|
| 640 | // eventually should have a warning for this case. |
---|
| 641 | SizeType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
[5f98ce5] | 642 | } |
---|
[0dd3a2f] | 643 | filter( translationUnit, isTypedef, true ); |
---|
[5f98ce5] | 644 | |
---|
[a08ba92] | 645 | } |
---|
[c8ffe20b] | 646 | |
---|
[85c4ef0] | 647 | Type *EliminateTypedef::mutate( TypeInstType * typeInst ) { |
---|
[9cb8e88d] | 648 | // instances of typedef types will come here. If it is an instance |
---|
[cc79d97] | 649 | // of a typdef type, link the instance to its actual type. |
---|
| 650 | TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() ); |
---|
[0dd3a2f] | 651 | if ( def != typedefNames.end() ) { |
---|
[cc79d97] | 652 | Type *ret = def->second.first->get_base()->clone(); |
---|
[6f95000] | 653 | ret->get_qualifiers() |= typeInst->get_qualifiers(); |
---|
[0215a76f] | 654 | // place instance parameters on the typedef'd type |
---|
| 655 | if ( ! typeInst->get_parameters().empty() ) { |
---|
| 656 | ReferenceToType *rtt = dynamic_cast<ReferenceToType*>(ret); |
---|
| 657 | if ( ! rtt ) { |
---|
| 658 | throw SemanticError("cannot apply type parameters to base type of " + typeInst->get_name()); |
---|
| 659 | } |
---|
| 660 | rtt->get_parameters().clear(); |
---|
[b644d6f] | 661 | cloneAll( typeInst->get_parameters(), rtt->get_parameters() ); |
---|
| 662 | mutateAll( rtt->get_parameters(), *this ); // recursively fix typedefs on parameters |
---|
[1db21619] | 663 | } // if |
---|
[0dd3a2f] | 664 | delete typeInst; |
---|
| 665 | return ret; |
---|
[679864e1] | 666 | } else { |
---|
| 667 | TypeDeclMap::const_iterator base = typedeclNames.find( typeInst->get_name() ); |
---|
[43c89a7] | 668 | assertf( base != typedeclNames.end(), "Can't find typedecl name %s", typeInst->get_name().c_str() ); |
---|
[1e8b02f5] | 669 | typeInst->set_baseType( base->second ); |
---|
[0dd3a2f] | 670 | } // if |
---|
| 671 | return typeInst; |
---|
[a08ba92] | 672 | } |
---|
[c8ffe20b] | 673 | |
---|
[85c4ef0] | 674 | Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) { |
---|
[0dd3a2f] | 675 | Declaration *ret = Mutator::mutate( tyDecl ); |
---|
[5f98ce5] | 676 | |
---|
[cc79d97] | 677 | if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) { |
---|
[9cb8e88d] | 678 | // typedef to the same name from the same scope |
---|
[cc79d97] | 679 | // must be from the same type |
---|
| 680 | |
---|
| 681 | Type * t1 = tyDecl->get_base(); |
---|
| 682 | Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base(); |
---|
[1cbca6e] | 683 | if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) { |
---|
[cc79d97] | 684 | throw SemanticError( "cannot redefine typedef: " + tyDecl->get_name() ); |
---|
[85c4ef0] | 685 | } |
---|
[cc79d97] | 686 | } else { |
---|
[46f6134] | 687 | typedefNames[ tyDecl->get_name() ] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel ); |
---|
[cc79d97] | 688 | } // if |
---|
| 689 | |
---|
[0dd3a2f] | 690 | // When a typedef is a forward declaration: |
---|
| 691 | // typedef struct screen SCREEN; |
---|
| 692 | // the declaration portion must be retained: |
---|
| 693 | // struct screen; |
---|
| 694 | // because the expansion of the typedef is: |
---|
| 695 | // void rtn( SCREEN *p ) => void rtn( struct screen *p ) |
---|
| 696 | // hence the type-name "screen" must be defined. |
---|
| 697 | // Note, qualifiers on the typedef are superfluous for the forward declaration. |
---|
[6f95000] | 698 | |
---|
| 699 | Type *designatorType = tyDecl->get_base()->stripDeclarator(); |
---|
| 700 | if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( designatorType ) ) { |
---|
[dd020c0] | 701 | return new StructDecl( aggDecl->get_name() ); |
---|
[6f95000] | 702 | } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( designatorType ) ) { |
---|
[dd020c0] | 703 | return new UnionDecl( aggDecl->get_name() ); |
---|
[6f95000] | 704 | } else if ( EnumInstType *enumDecl = dynamic_cast< EnumInstType * >( designatorType ) ) { |
---|
[956a9c7] | 705 | return new EnumDecl( enumDecl->get_name() ); |
---|
[0dd3a2f] | 706 | } else { |
---|
[46f6134] | 707 | return ret->clone(); |
---|
[0dd3a2f] | 708 | } // if |
---|
[a08ba92] | 709 | } |
---|
[c8ffe20b] | 710 | |
---|
[85c4ef0] | 711 | TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) { |
---|
[cc79d97] | 712 | TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() ); |
---|
[0dd3a2f] | 713 | if ( i != typedefNames.end() ) { |
---|
| 714 | typedefNames.erase( i ) ; |
---|
| 715 | } // if |
---|
[679864e1] | 716 | |
---|
| 717 | typedeclNames[ typeDecl->get_name() ] = typeDecl; |
---|
[2c57025] | 718 | return Mutator::mutate( typeDecl ); |
---|
[a08ba92] | 719 | } |
---|
[c8ffe20b] | 720 | |
---|
[85c4ef0] | 721 | DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) { |
---|
[46f6134] | 722 | typedefNames.beginScope(); |
---|
[0dd3a2f] | 723 | DeclarationWithType *ret = Mutator::mutate( funcDecl ); |
---|
[46f6134] | 724 | typedefNames.endScope(); |
---|
[0dd3a2f] | 725 | return ret; |
---|
[a08ba92] | 726 | } |
---|
[c8ffe20b] | 727 | |
---|
[1db21619] | 728 | DeclarationWithType *EliminateTypedef::mutate( ObjectDecl * objDecl ) { |
---|
[46f6134] | 729 | typedefNames.beginScope(); |
---|
[1db21619] | 730 | DeclarationWithType *ret = Mutator::mutate( objDecl ); |
---|
[46f6134] | 731 | typedefNames.endScope(); |
---|
[dd020c0] | 732 | |
---|
| 733 | if ( FunctionType *funtype = dynamic_cast<FunctionType *>( ret->get_type() ) ) { // function type? |
---|
[02e5ab6] | 734 | // replace the current object declaration with a function declaration |
---|
[a7c90d4] | 735 | FunctionDecl * newDecl = new FunctionDecl( ret->get_name(), ret->get_storageClasses(), ret->get_linkage(), funtype, 0, objDecl->get_attributes(), ret->get_funcSpec() ); |
---|
[0a86a30] | 736 | objDecl->get_attributes().clear(); |
---|
[dbe8f244] | 737 | objDecl->set_type( nullptr ); |
---|
[0a86a30] | 738 | delete objDecl; |
---|
| 739 | return newDecl; |
---|
[1db21619] | 740 | } // if |
---|
[0dd3a2f] | 741 | return ret; |
---|
[a08ba92] | 742 | } |
---|
[c8ffe20b] | 743 | |
---|
[85c4ef0] | 744 | Expression *EliminateTypedef::mutate( CastExpr * castExpr ) { |
---|
[46f6134] | 745 | typedefNames.beginScope(); |
---|
[0dd3a2f] | 746 | Expression *ret = Mutator::mutate( castExpr ); |
---|
[46f6134] | 747 | typedefNames.endScope(); |
---|
[0dd3a2f] | 748 | return ret; |
---|
[a08ba92] | 749 | } |
---|
[c8ffe20b] | 750 | |
---|
[85c4ef0] | 751 | CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) { |
---|
[46f6134] | 752 | typedefNames.beginScope(); |
---|
[cc79d97] | 753 | scopeLevel += 1; |
---|
[0dd3a2f] | 754 | CompoundStmt *ret = Mutator::mutate( compoundStmt ); |
---|
[cc79d97] | 755 | scopeLevel -= 1; |
---|
[0dd3a2f] | 756 | std::list< Statement * >::iterator i = compoundStmt->get_kids().begin(); |
---|
| 757 | while ( i != compoundStmt->get_kids().end() ) { |
---|
[85c4ef0] | 758 | std::list< Statement * >::iterator next = i+1; |
---|
[0dd3a2f] | 759 | if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) { |
---|
| 760 | if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) { |
---|
| 761 | delete *i; |
---|
| 762 | compoundStmt->get_kids().erase( i ); |
---|
| 763 | } // if |
---|
| 764 | } // if |
---|
| 765 | i = next; |
---|
| 766 | } // while |
---|
[46f6134] | 767 | typedefNames.endScope(); |
---|
[0dd3a2f] | 768 | return ret; |
---|
[a08ba92] | 769 | } |
---|
[85c4ef0] | 770 | |
---|
[43c89a7] | 771 | // there may be typedefs nested within aggregates. in order for everything to work properly, these should be removed |
---|
[45161b4d] | 772 | // as well |
---|
[85c4ef0] | 773 | template<typename AggDecl> |
---|
| 774 | AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) { |
---|
| 775 | std::list<Declaration *>::iterator it = aggDecl->get_members().begin(); |
---|
| 776 | for ( ; it != aggDecl->get_members().end(); ) { |
---|
| 777 | std::list< Declaration * >::iterator next = it+1; |
---|
| 778 | if ( dynamic_cast< TypedefDecl * >( *it ) ) { |
---|
| 779 | delete *it; |
---|
| 780 | aggDecl->get_members().erase( it ); |
---|
| 781 | } // if |
---|
| 782 | it = next; |
---|
| 783 | } |
---|
| 784 | return aggDecl; |
---|
| 785 | } |
---|
| 786 | |
---|
[45161b4d] | 787 | template<typename AggDecl> |
---|
| 788 | void EliminateTypedef::addImplicitTypedef( AggDecl * aggDecl ) { |
---|
| 789 | if ( typedefNames.count( aggDecl->get_name() ) == 0 ) { |
---|
[62e5546] | 790 | Type *type = nullptr; |
---|
[45161b4d] | 791 | if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( aggDecl ) ) { |
---|
| 792 | type = new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ); |
---|
| 793 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( aggDecl ) ) { |
---|
| 794 | type = new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ); |
---|
| 795 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( aggDecl ) ) { |
---|
| 796 | type = new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ); |
---|
| 797 | } // if |
---|
[68fe077a] | 798 | TypedefDeclPtr tyDecl( new TypedefDecl( aggDecl->get_name(), Type::StorageClasses(), type ) ); |
---|
[46f6134] | 799 | typedefNames[ aggDecl->get_name() ] = std::make_pair( std::move( tyDecl ), scopeLevel ); |
---|
[45161b4d] | 800 | } // if |
---|
| 801 | } |
---|
[4e06c1e] | 802 | |
---|
[85c4ef0] | 803 | Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) { |
---|
[45161b4d] | 804 | addImplicitTypedef( structDecl ); |
---|
[85c4ef0] | 805 | Mutator::mutate( structDecl ); |
---|
| 806 | return handleAggregate( structDecl ); |
---|
| 807 | } |
---|
| 808 | |
---|
| 809 | Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) { |
---|
[45161b4d] | 810 | addImplicitTypedef( unionDecl ); |
---|
[85c4ef0] | 811 | Mutator::mutate( unionDecl ); |
---|
| 812 | return handleAggregate( unionDecl ); |
---|
| 813 | } |
---|
| 814 | |
---|
| 815 | Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) { |
---|
[45161b4d] | 816 | addImplicitTypedef( enumDecl ); |
---|
[85c4ef0] | 817 | Mutator::mutate( enumDecl ); |
---|
| 818 | return handleAggregate( enumDecl ); |
---|
| 819 | } |
---|
| 820 | |
---|
[45161b4d] | 821 | Declaration *EliminateTypedef::mutate( TraitDecl * contextDecl ) { |
---|
[85c4ef0] | 822 | Mutator::mutate( contextDecl ); |
---|
| 823 | return handleAggregate( contextDecl ); |
---|
| 824 | } |
---|
| 825 | |
---|
[d1969a6] | 826 | void VerifyCtorDtorAssign::verify( std::list< Declaration * > & translationUnit ) { |
---|
[0db6fc0] | 827 | PassVisitor<VerifyCtorDtorAssign> verifier; |
---|
[9cb8e88d] | 828 | acceptAll( translationUnit, verifier ); |
---|
| 829 | } |
---|
| 830 | |
---|
[0db6fc0] | 831 | void VerifyCtorDtorAssign::previsit( FunctionDecl * funcDecl ) { |
---|
[9cb8e88d] | 832 | FunctionType * funcType = funcDecl->get_functionType(); |
---|
| 833 | std::list< DeclarationWithType * > &returnVals = funcType->get_returnVals(); |
---|
| 834 | std::list< DeclarationWithType * > ¶ms = funcType->get_parameters(); |
---|
| 835 | |
---|
[d1969a6] | 836 | if ( InitTweak::isCtorDtorAssign( funcDecl->get_name() ) ) { |
---|
[9cb8e88d] | 837 | if ( params.size() == 0 ) { |
---|
[d1969a6] | 838 | throw SemanticError( "Constructors, destructors, and assignment functions require at least one parameter ", funcDecl ); |
---|
[9cb8e88d] | 839 | } |
---|
[ed8a0d2] | 840 | PointerType * ptrType = dynamic_cast< PointerType * >( params.front()->get_type() ); |
---|
| 841 | if ( ! ptrType || ptrType->is_array() ) { |
---|
[d1969a6] | 842 | throw SemanticError( "First parameter of a constructor, destructor, or assignment function must be a pointer ", funcDecl ); |
---|
[9cb8e88d] | 843 | } |
---|
[d1969a6] | 844 | if ( InitTweak::isCtorDtor( funcDecl->get_name() ) && returnVals.size() != 0 ) { |
---|
[9cb8e88d] | 845 | throw SemanticError( "Constructors and destructors cannot have explicit return values ", funcDecl ); |
---|
| 846 | } |
---|
| 847 | } |
---|
| 848 | } |
---|
[70a06f6] | 849 | |
---|
[11ab8ea8] | 850 | template< typename Aggr > |
---|
| 851 | void validateGeneric( Aggr * inst ) { |
---|
| 852 | std::list< TypeDecl * > * params = inst->get_baseParameters(); |
---|
| 853 | if ( params != NULL ) { |
---|
| 854 | std::list< Expression * > & args = inst->get_parameters(); |
---|
[67cf18c] | 855 | |
---|
| 856 | // insert defaults arguments when a type argument is missing (currently only supports missing arguments at the end of the list). |
---|
| 857 | // A substitution is used to ensure that defaults are replaced correctly, e.g., |
---|
| 858 | // forall(otype T, otype alloc = heap_allocator(T)) struct vector; |
---|
| 859 | // vector(int) v; |
---|
| 860 | // after insertion of default values becomes |
---|
| 861 | // vector(int, heap_allocator(T)) |
---|
| 862 | // and the substitution is built with T=int so that after substitution, the result is |
---|
| 863 | // vector(int, heap_allocator(int)) |
---|
| 864 | TypeSubstitution sub; |
---|
| 865 | auto paramIter = params->begin(); |
---|
| 866 | for ( size_t i = 0; paramIter != params->end(); ++paramIter, ++i ) { |
---|
| 867 | if ( i < args.size() ) { |
---|
| 868 | TypeExpr * expr = safe_dynamic_cast< TypeExpr * >( *std::next( args.begin(), i ) ); |
---|
| 869 | sub.add( (*paramIter)->get_name(), expr->get_type()->clone() ); |
---|
| 870 | } else if ( i == args.size() ) { |
---|
| 871 | Type * defaultType = (*paramIter)->get_init(); |
---|
| 872 | if ( defaultType ) { |
---|
| 873 | args.push_back( new TypeExpr( defaultType->clone() ) ); |
---|
| 874 | sub.add( (*paramIter)->get_name(), defaultType->clone() ); |
---|
| 875 | } |
---|
| 876 | } |
---|
| 877 | } |
---|
| 878 | |
---|
| 879 | sub.apply( inst ); |
---|
[11ab8ea8] | 880 | if ( args.size() < params->size() ) throw SemanticError( "Too few type arguments in generic type ", inst ); |
---|
| 881 | if ( args.size() > params->size() ) throw SemanticError( "Too many type arguments in generic type ", inst ); |
---|
| 882 | } |
---|
| 883 | } |
---|
| 884 | |
---|
[0db6fc0] | 885 | void ValidateGenericParameters::previsit( StructInstType * inst ) { |
---|
[11ab8ea8] | 886 | validateGeneric( inst ); |
---|
| 887 | } |
---|
| 888 | |
---|
[0db6fc0] | 889 | void ValidateGenericParameters::previsit( UnionInstType * inst ) { |
---|
[11ab8ea8] | 890 | validateGeneric( inst ); |
---|
| 891 | } |
---|
| 892 | |
---|
[630a82a] | 893 | DeclarationWithType * CompoundLiteral::mutate( ObjectDecl *objectDecl ) { |
---|
[a7c90d4] | 894 | storageClasses = objectDecl->get_storageClasses(); |
---|
[630a82a] | 895 | DeclarationWithType * temp = Mutator::mutate( objectDecl ); |
---|
| 896 | return temp; |
---|
| 897 | } |
---|
| 898 | |
---|
| 899 | Expression *CompoundLiteral::mutate( CompoundLiteralExpr *compLitExpr ) { |
---|
| 900 | // transform [storage_class] ... (struct S){ 3, ... }; |
---|
| 901 | // into [storage_class] struct S temp = { 3, ... }; |
---|
| 902 | static UniqueName indexName( "_compLit" ); |
---|
| 903 | |
---|
[fbcde64] | 904 | ObjectDecl *tempvar = new ObjectDecl( indexName.newName(), storageClasses, LinkageSpec::C, 0, compLitExpr->get_result(), compLitExpr->get_initializer() ); |
---|
| 905 | compLitExpr->set_result( 0 ); |
---|
[630a82a] | 906 | compLitExpr->set_initializer( 0 ); |
---|
| 907 | delete compLitExpr; |
---|
| 908 | DeclarationWithType * newtempvar = mutate( tempvar ); |
---|
| 909 | addDeclaration( newtempvar ); // add modified temporary to current block |
---|
| 910 | return new VariableExpr( newtempvar ); |
---|
| 911 | } |
---|
[cce9429] | 912 | |
---|
| 913 | void ReturnTypeFixer::fix( std::list< Declaration * > &translationUnit ) { |
---|
[0db6fc0] | 914 | PassVisitor<ReturnTypeFixer> fixer; |
---|
[cce9429] | 915 | acceptAll( translationUnit, fixer ); |
---|
| 916 | } |
---|
| 917 | |
---|
[0db6fc0] | 918 | void ReturnTypeFixer::postvisit( FunctionDecl * functionDecl ) { |
---|
[9facf3b] | 919 | FunctionType * ftype = functionDecl->get_functionType(); |
---|
| 920 | std::list< DeclarationWithType * > & retVals = ftype->get_returnVals(); |
---|
| 921 | assertf( retVals.size() == 0 || retVals.size() == 1, "Function %s has too many return values: %d", functionDecl->get_name().c_str(), retVals.size() ); |
---|
| 922 | if ( retVals.size() == 1 ) { |
---|
[861799c] | 923 | // ensure all function return values have a name - use the name of the function to disambiguate (this also provides a nice bit of help for debugging). |
---|
| 924 | // ensure other return values have a name. |
---|
[9facf3b] | 925 | DeclarationWithType * ret = retVals.front(); |
---|
| 926 | if ( ret->get_name() == "" ) { |
---|
| 927 | ret->set_name( toString( "_retval_", CodeGen::genName( functionDecl ) ) ); |
---|
| 928 | } |
---|
| 929 | } |
---|
| 930 | } |
---|
[cce9429] | 931 | |
---|
[0db6fc0] | 932 | void ReturnTypeFixer::postvisit( FunctionType * ftype ) { |
---|
[cce9429] | 933 | // xxx - need to handle named return values - this information needs to be saved somehow |
---|
| 934 | // so that resolution has access to the names. |
---|
| 935 | // Note that this pass needs to happen early so that other passes which look for tuple types |
---|
| 936 | // find them in all of the right places, including function return types. |
---|
| 937 | std::list< DeclarationWithType * > & retVals = ftype->get_returnVals(); |
---|
| 938 | if ( retVals.size() > 1 ) { |
---|
| 939 | // generate a single return parameter which is the tuple of all of the return values |
---|
| 940 | TupleType * tupleType = safe_dynamic_cast< TupleType * >( ResolvExpr::extractResultType( ftype ) ); |
---|
| 941 | // ensure return value is not destructed by explicitly creating an empty ListInit node wherein maybeConstruct is false. |
---|
[68fe077a] | 942 | ObjectDecl * newRet = new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, tupleType, new ListInit( std::list<Initializer*>(), noDesignators, false ) ); |
---|
[cce9429] | 943 | deleteAll( retVals ); |
---|
| 944 | retVals.clear(); |
---|
| 945 | retVals.push_back( newRet ); |
---|
| 946 | } |
---|
| 947 | } |
---|
[fbd7ad6] | 948 | |
---|
| 949 | void ArrayLength::computeLength( std::list< Declaration * > & translationUnit ) { |
---|
[0db6fc0] | 950 | PassVisitor<ArrayLength> len; |
---|
[fbd7ad6] | 951 | acceptAll( translationUnit, len ); |
---|
| 952 | } |
---|
| 953 | |
---|
[0db6fc0] | 954 | void ArrayLength::previsit( ObjectDecl * objDecl ) { |
---|
[fbd7ad6] | 955 | if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->get_type() ) ) { |
---|
| 956 | if ( at->get_dimension() != nullptr ) return; |
---|
| 957 | if ( ListInit * init = dynamic_cast< ListInit * >( objDecl->get_init() ) ) { |
---|
| 958 | at->set_dimension( new ConstantExpr( Constant::from_ulong( init->get_initializers().size() ) ) ); |
---|
| 959 | } |
---|
| 960 | } |
---|
| 961 | } |
---|
[51b7345] | 962 | } // namespace SymTab |
---|
[0dd3a2f] | 963 | |
---|
| 964 | // Local Variables: // |
---|
| 965 | // tab-width: 4 // |
---|
| 966 | // mode: c++ // |
---|
| 967 | // compile-command: "make install" // |
---|
| 968 | // End: // |
---|