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