[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 |
---|
[25fcb84] | 50 | #include "ControlStruct/Mutate.h" // for ForExprMutator |
---|
[30f9072] | 51 | #include "Common/PassVisitor.h" // for PassVisitor, WithDeclsToAdd |
---|
[d180746] | 52 | #include "Common/ScopedMap.h" // for ScopedMap |
---|
[30f9072] | 53 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 54 | #include "Common/UniqueName.h" // for UniqueName |
---|
| 55 | #include "Common/utility.h" // for operator+, cloneAll, deleteAll |
---|
[be9288a] | 56 | #include "Concurrency/Keywords.h" // for applyKeywords |
---|
[30f9072] | 57 | #include "FixFunction.h" // for FixFunction |
---|
| 58 | #include "Indexer.h" // for Indexer |
---|
[8b11840] | 59 | #include "InitTweak/GenInit.h" // for fixReturnStatements |
---|
[d180746] | 60 | #include "InitTweak/InitTweak.h" // for isCtorDtorAssign |
---|
| 61 | #include "Parser/LinkageSpec.h" // for C |
---|
| 62 | #include "ResolvExpr/typeops.h" // for typesCompatible |
---|
[be9288a] | 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 | |
---|
[b16923d] | 79 | #define debugPrint( x ) if ( doDebug ) x |
---|
[51b7345] | 80 | |
---|
| 81 | namespace SymTab { |
---|
[15f5c5e] | 82 | /// hoists declarations that are difficult to hoist while parsing |
---|
| 83 | struct HoistTypeDecls final : public WithDeclsToAdd { |
---|
[29f9e20] | 84 | void previsit( SizeofExpr * ); |
---|
| 85 | void previsit( AlignofExpr * ); |
---|
| 86 | void previsit( UntypedOffsetofExpr * ); |
---|
[95d09bdb] | 87 | void previsit( CompoundLiteralExpr * ); |
---|
[29f9e20] | 88 | void handleType( Type * ); |
---|
| 89 | }; |
---|
| 90 | |
---|
[a12c81f3] | 91 | struct FixQualifiedTypes final : public WithIndexer { |
---|
| 92 | Type * postmutate( QualifiedType * ); |
---|
| 93 | }; |
---|
| 94 | |
---|
[a09e45b] | 95 | struct HoistStruct final : public WithDeclsToAdd, public WithGuards { |
---|
[82dd287] | 96 | /// Flattens nested struct types |
---|
[0dd3a2f] | 97 | static void hoistStruct( std::list< Declaration * > &translationUnit ); |
---|
[9cb8e88d] | 98 | |
---|
[a09e45b] | 99 | void previsit( StructDecl * aggregateDecl ); |
---|
| 100 | void previsit( UnionDecl * aggregateDecl ); |
---|
[0f40912] | 101 | void previsit( StaticAssertDecl * assertDecl ); |
---|
[d419d8e] | 102 | void previsit( StructInstType * type ); |
---|
| 103 | void previsit( UnionInstType * type ); |
---|
| 104 | void previsit( EnumInstType * type ); |
---|
[9cb8e88d] | 105 | |
---|
[a08ba92] | 106 | private: |
---|
[0dd3a2f] | 107 | template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl ); |
---|
[c8ffe20b] | 108 | |
---|
[bdad6eb7] | 109 | AggregateDecl * parentAggr = nullptr; |
---|
[a08ba92] | 110 | }; |
---|
[c8ffe20b] | 111 | |
---|
[cce9429] | 112 | /// Fix return types so that every function returns exactly one value |
---|
[d24d4e1] | 113 | struct ReturnTypeFixer { |
---|
[cce9429] | 114 | static void fix( std::list< Declaration * > &translationUnit ); |
---|
| 115 | |
---|
[0db6fc0] | 116 | void postvisit( FunctionDecl * functionDecl ); |
---|
| 117 | void postvisit( FunctionType * ftype ); |
---|
[cce9429] | 118 | }; |
---|
| 119 | |
---|
[de91427b] | 120 | /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers. |
---|
[d24d4e1] | 121 | struct EnumAndPointerDecay { |
---|
[06edda0] | 122 | void previsit( EnumDecl *aggregateDecl ); |
---|
| 123 | void previsit( FunctionType *func ); |
---|
[a08ba92] | 124 | }; |
---|
[82dd287] | 125 | |
---|
| 126 | /// Associates forward declarations of aggregates with their definitions |
---|
[afcb0a3] | 127 | struct LinkReferenceToTypes final : public WithIndexer, public WithGuards, public WithVisitorRef<LinkReferenceToTypes>, public WithShortCircuiting { |
---|
[522363e] | 128 | LinkReferenceToTypes( const Indexer *indexer ); |
---|
| 129 | void postvisit( TypeInstType *typeInst ); |
---|
[be9036d] | 130 | |
---|
[522363e] | 131 | void postvisit( EnumInstType *enumInst ); |
---|
| 132 | void postvisit( StructInstType *structInst ); |
---|
| 133 | void postvisit( UnionInstType *unionInst ); |
---|
| 134 | void postvisit( TraitInstType *traitInst ); |
---|
[afcb0a3] | 135 | void previsit( QualifiedType * qualType ); |
---|
| 136 | void postvisit( QualifiedType * qualType ); |
---|
[be9036d] | 137 | |
---|
[522363e] | 138 | void postvisit( EnumDecl *enumDecl ); |
---|
| 139 | void postvisit( StructDecl *structDecl ); |
---|
| 140 | void postvisit( UnionDecl *unionDecl ); |
---|
| 141 | void postvisit( TraitDecl * traitDecl ); |
---|
[be9036d] | 142 | |
---|
[b95fe40] | 143 | void previsit( StructDecl *structDecl ); |
---|
| 144 | void previsit( UnionDecl *unionDecl ); |
---|
| 145 | |
---|
| 146 | void renameGenericParams( std::list< TypeDecl * > & params ); |
---|
| 147 | |
---|
[06edda0] | 148 | private: |
---|
[522363e] | 149 | const Indexer *local_indexer; |
---|
[9cb8e88d] | 150 | |
---|
[c0aa336] | 151 | typedef std::map< std::string, std::list< EnumInstType * > > ForwardEnumsType; |
---|
[0dd3a2f] | 152 | typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType; |
---|
| 153 | typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType; |
---|
[c0aa336] | 154 | ForwardEnumsType forwardEnums; |
---|
[0dd3a2f] | 155 | ForwardStructsType forwardStructs; |
---|
| 156 | ForwardUnionsType forwardUnions; |
---|
[b95fe40] | 157 | /// true if currently in a generic type body, so that type parameter instances can be renamed appropriately |
---|
| 158 | bool inGeneric = false; |
---|
[a08ba92] | 159 | }; |
---|
[c8ffe20b] | 160 | |
---|
[06edda0] | 161 | /// Replaces array and function types in forall lists by appropriate pointer type and assigns each Object and Function declaration a unique ID. |
---|
[522363e] | 162 | struct ForallPointerDecay final { |
---|
[8b11840] | 163 | void previsit( ObjectDecl * object ); |
---|
| 164 | void previsit( FunctionDecl * func ); |
---|
[bbf3fda] | 165 | void previsit( FunctionType * ftype ); |
---|
[bd7e609] | 166 | void previsit( StructDecl * aggrDecl ); |
---|
| 167 | void previsit( UnionDecl * aggrDecl ); |
---|
[a08ba92] | 168 | }; |
---|
[c8ffe20b] | 169 | |
---|
[d24d4e1] | 170 | struct ReturnChecker : public WithGuards { |
---|
[de91427b] | 171 | /// Checks that return statements return nothing if their return type is void |
---|
| 172 | /// and return something if the return type is non-void. |
---|
| 173 | static void checkFunctionReturns( std::list< Declaration * > & translationUnit ); |
---|
| 174 | |
---|
[0db6fc0] | 175 | void previsit( FunctionDecl * functionDecl ); |
---|
| 176 | void previsit( ReturnStmt * returnStmt ); |
---|
[de91427b] | 177 | |
---|
[0db6fc0] | 178 | typedef std::list< DeclarationWithType * > ReturnVals; |
---|
| 179 | ReturnVals returnVals; |
---|
[de91427b] | 180 | }; |
---|
| 181 | |
---|
[48ed81c] | 182 | struct ReplaceTypedef final : public WithVisitorRef<ReplaceTypedef>, public WithGuards, public WithShortCircuiting, public WithDeclsToAdd { |
---|
| 183 | ReplaceTypedef() : scopeLevel( 0 ) {} |
---|
[de91427b] | 184 | /// Replaces typedefs by forward declarations |
---|
[48ed81c] | 185 | static void replaceTypedef( std::list< Declaration * > &translationUnit ); |
---|
[85c4ef0] | 186 | |
---|
[48ed81c] | 187 | void premutate( QualifiedType * ); |
---|
| 188 | Type * postmutate( QualifiedType * qualType ); |
---|
[a506df4] | 189 | Type * postmutate( TypeInstType * aggregateUseType ); |
---|
| 190 | Declaration * postmutate( TypedefDecl * typeDecl ); |
---|
| 191 | void premutate( TypeDecl * typeDecl ); |
---|
| 192 | void premutate( FunctionDecl * funcDecl ); |
---|
| 193 | void premutate( ObjectDecl * objDecl ); |
---|
| 194 | DeclarationWithType * postmutate( ObjectDecl * objDecl ); |
---|
| 195 | |
---|
| 196 | void premutate( CastExpr * castExpr ); |
---|
| 197 | |
---|
| 198 | void premutate( CompoundStmt * compoundStmt ); |
---|
| 199 | |
---|
| 200 | void premutate( StructDecl * structDecl ); |
---|
| 201 | void premutate( UnionDecl * unionDecl ); |
---|
| 202 | void premutate( EnumDecl * enumDecl ); |
---|
[0bcc2b7] | 203 | void premutate( TraitDecl * ); |
---|
[a506df4] | 204 | |
---|
[1f370451] | 205 | void premutate( FunctionType * ftype ); |
---|
| 206 | |
---|
[a506df4] | 207 | private: |
---|
[45161b4d] | 208 | template<typename AggDecl> |
---|
| 209 | void addImplicitTypedef( AggDecl * aggDecl ); |
---|
[48ed81c] | 210 | template< typename AggDecl > |
---|
| 211 | void handleAggregate( AggDecl * aggr ); |
---|
[70a06f6] | 212 | |
---|
[46f6134] | 213 | typedef std::unique_ptr<TypedefDecl> TypedefDeclPtr; |
---|
[e491159] | 214 | typedef ScopedMap< std::string, std::pair< TypedefDeclPtr, int > > TypedefMap; |
---|
[0bcc2b7] | 215 | typedef ScopedMap< std::string, TypeDecl * > TypeDeclMap; |
---|
[cc79d97] | 216 | TypedefMap typedefNames; |
---|
[679864e1] | 217 | TypeDeclMap typedeclNames; |
---|
[cc79d97] | 218 | int scopeLevel; |
---|
[1f370451] | 219 | bool inFunctionType = false; |
---|
[a08ba92] | 220 | }; |
---|
[c8ffe20b] | 221 | |
---|
[69918cea] | 222 | struct EliminateTypedef { |
---|
| 223 | /// removes TypedefDecls from the AST |
---|
| 224 | static void eliminateTypedef( std::list< Declaration * > &translationUnit ); |
---|
| 225 | |
---|
| 226 | template<typename AggDecl> |
---|
| 227 | void handleAggregate( AggDecl *aggregateDecl ); |
---|
| 228 | |
---|
| 229 | void previsit( StructDecl * aggregateDecl ); |
---|
| 230 | void previsit( UnionDecl * aggregateDecl ); |
---|
| 231 | void previsit( CompoundStmt * compoundStmt ); |
---|
| 232 | }; |
---|
| 233 | |
---|
[d24d4e1] | 234 | struct VerifyCtorDtorAssign { |
---|
[d1969a6] | 235 | /// ensure that constructors, destructors, and assignment have at least one |
---|
| 236 | /// parameter, the first of which must be a pointer, and that ctor/dtors have no |
---|
[9cb8e88d] | 237 | /// return values. |
---|
| 238 | static void verify( std::list< Declaration * > &translationUnit ); |
---|
| 239 | |
---|
[0db6fc0] | 240 | void previsit( FunctionDecl *funcDecl ); |
---|
[5f98ce5] | 241 | }; |
---|
[70a06f6] | 242 | |
---|
[11ab8ea8] | 243 | /// ensure that generic types have the correct number of type arguments |
---|
[d24d4e1] | 244 | struct ValidateGenericParameters { |
---|
[0db6fc0] | 245 | void previsit( StructInstType * inst ); |
---|
| 246 | void previsit( UnionInstType * inst ); |
---|
[5f98ce5] | 247 | }; |
---|
[70a06f6] | 248 | |
---|
[d24d4e1] | 249 | struct ArrayLength { |
---|
[fbd7ad6] | 250 | /// for array types without an explicit length, compute the length and store it so that it |
---|
| 251 | /// is known to the rest of the phases. For example, |
---|
| 252 | /// int x[] = { 1, 2, 3 }; |
---|
| 253 | /// int y[][2] = { { 1, 2, 3 }, { 1, 2, 3 } }; |
---|
| 254 | /// here x and y are known at compile-time to have length 3, so change this into |
---|
| 255 | /// int x[3] = { 1, 2, 3 }; |
---|
| 256 | /// int y[3][2] = { { 1, 2, 3 }, { 1, 2, 3 } }; |
---|
| 257 | static void computeLength( std::list< Declaration * > & translationUnit ); |
---|
| 258 | |
---|
[0db6fc0] | 259 | void previsit( ObjectDecl * objDecl ); |
---|
[fbd7ad6] | 260 | }; |
---|
| 261 | |
---|
[d24d4e1] | 262 | struct CompoundLiteral final : public WithDeclsToAdd, public WithVisitorRef<CompoundLiteral> { |
---|
[68fe077a] | 263 | Type::StorageClasses storageClasses; |
---|
[630a82a] | 264 | |
---|
[d24d4e1] | 265 | void premutate( ObjectDecl *objectDecl ); |
---|
| 266 | Expression * postmutate( CompoundLiteralExpr *compLitExpr ); |
---|
[9cb8e88d] | 267 | }; |
---|
| 268 | |
---|
[5809461] | 269 | struct LabelAddressFixer final : public WithGuards { |
---|
| 270 | std::set< Label > labels; |
---|
| 271 | |
---|
| 272 | void premutate( FunctionDecl * funcDecl ); |
---|
| 273 | Expression * postmutate( AddressExpr * addrExpr ); |
---|
| 274 | }; |
---|
[4fbdfae0] | 275 | |
---|
| 276 | FunctionDecl * dereferenceOperator = nullptr; |
---|
| 277 | struct FindSpecialDeclarations final { |
---|
| 278 | void previsit( FunctionDecl * funcDecl ); |
---|
| 279 | }; |
---|
| 280 | |
---|
[522363e] | 281 | void validate( std::list< Declaration * > &translationUnit, __attribute__((unused)) bool doDebug ) { |
---|
[06edda0] | 282 | PassVisitor<EnumAndPointerDecay> epc; |
---|
[522363e] | 283 | PassVisitor<LinkReferenceToTypes> lrt( nullptr ); |
---|
| 284 | PassVisitor<ForallPointerDecay> fpd; |
---|
[d24d4e1] | 285 | PassVisitor<CompoundLiteral> compoundliteral; |
---|
[0db6fc0] | 286 | PassVisitor<ValidateGenericParameters> genericParams; |
---|
[4fbdfae0] | 287 | PassVisitor<FindSpecialDeclarations> finder; |
---|
[5809461] | 288 | PassVisitor<LabelAddressFixer> labelAddrFixer; |
---|
[15f5c5e] | 289 | PassVisitor<HoistTypeDecls> hoistDecls; |
---|
[a12c81f3] | 290 | PassVisitor<FixQualifiedTypes> fixQual; |
---|
[630a82a] | 291 | |
---|
[15f5c5e] | 292 | acceptAll( translationUnit, hoistDecls ); |
---|
[48ed81c] | 293 | ReplaceTypedef::replaceTypedef( translationUnit ); |
---|
[cce9429] | 294 | ReturnTypeFixer::fix( translationUnit ); // must happen before autogen |
---|
[8e0147a] | 295 | acceptAll( translationUnit, epc ); // must happen before VerifyCtorDtorAssign, because void return objects should not exist; before LinkReferenceToTypes because it is an indexer and needs correct types for mangling |
---|
[861799c] | 296 | acceptAll( translationUnit, lrt ); // must happen before autogen, because sized flag needs to propagate to generated functions |
---|
[a12c81f3] | 297 | mutateAll( translationUnit, fixQual ); // must happen after LinkReferenceToTypes, because aggregate members are accessed |
---|
[29f9e20] | 298 | HoistStruct::hoistStruct( translationUnit ); // must happen after EliminateTypedef, so that aggregate typedefs occur in the correct order |
---|
[69918cea] | 299 | EliminateTypedef::eliminateTypedef( translationUnit ); // |
---|
[11ab8ea8] | 300 | acceptAll( translationUnit, genericParams ); // check as early as possible - can't happen before LinkReferenceToTypes |
---|
[ed8a0d2] | 301 | VerifyCtorDtorAssign::verify( translationUnit ); // must happen before autogen, because autogen examines existing ctor/dtors |
---|
[bd7e609] | 302 | ReturnChecker::checkFunctionReturns( translationUnit ); |
---|
| 303 | InitTweak::fixReturnStatements( translationUnit ); // must happen before autogen |
---|
[bcda04c] | 304 | Concurrency::applyKeywords( translationUnit ); |
---|
[bd7e609] | 305 | acceptAll( translationUnit, fpd ); // must happen before autogenerateRoutines, after Concurrency::applyKeywords because uniqueIds must be set on declaration before resolution |
---|
[25fcb84] | 306 | ControlStruct::hoistControlDecls( translationUnit ); // hoist initialization out of for statements; must happen before autogenerateRoutines |
---|
[06edda0] | 307 | autogenerateRoutines( translationUnit ); // moved up, used to be below compoundLiteral - currently needs EnumAndPointerDecay |
---|
[bcda04c] | 308 | Concurrency::implementMutexFuncs( translationUnit ); |
---|
| 309 | Concurrency::implementThreadStarter( translationUnit ); |
---|
[d24d4e1] | 310 | mutateAll( translationUnit, compoundliteral ); |
---|
[fbd7ad6] | 311 | ArrayLength::computeLength( translationUnit ); |
---|
[bd7e609] | 312 | acceptAll( translationUnit, finder ); // xxx - remove this pass soon |
---|
[5809461] | 313 | mutateAll( translationUnit, labelAddrFixer ); |
---|
[a08ba92] | 314 | } |
---|
[9cb8e88d] | 315 | |
---|
[a08ba92] | 316 | void validateType( Type *type, const Indexer *indexer ) { |
---|
[06edda0] | 317 | PassVisitor<EnumAndPointerDecay> epc; |
---|
[522363e] | 318 | PassVisitor<LinkReferenceToTypes> lrt( indexer ); |
---|
| 319 | PassVisitor<ForallPointerDecay> fpd; |
---|
[bda58ad] | 320 | type->accept( epc ); |
---|
[cce9429] | 321 | type->accept( lrt ); |
---|
[06edda0] | 322 | type->accept( fpd ); |
---|
[a08ba92] | 323 | } |
---|
[c8ffe20b] | 324 | |
---|
[29f9e20] | 325 | |
---|
[15f5c5e] | 326 | void HoistTypeDecls::handleType( Type * type ) { |
---|
[29f9e20] | 327 | // some type declarations are buried in expressions and not easy to hoist during parsing; hoist them here |
---|
| 328 | AggregateDecl * aggr = nullptr; |
---|
| 329 | if ( StructInstType * inst = dynamic_cast< StructInstType * >( type ) ) { |
---|
| 330 | aggr = inst->baseStruct; |
---|
| 331 | } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( type ) ) { |
---|
| 332 | aggr = inst->baseUnion; |
---|
| 333 | } else if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( type ) ) { |
---|
| 334 | aggr = inst->baseEnum; |
---|
| 335 | } |
---|
| 336 | if ( aggr && aggr->body ) { |
---|
| 337 | declsToAddBefore.push_front( aggr ); |
---|
| 338 | } |
---|
| 339 | } |
---|
| 340 | |
---|
[15f5c5e] | 341 | void HoistTypeDecls::previsit( SizeofExpr * expr ) { |
---|
[29f9e20] | 342 | handleType( expr->type ); |
---|
| 343 | } |
---|
| 344 | |
---|
[15f5c5e] | 345 | void HoistTypeDecls::previsit( AlignofExpr * expr ) { |
---|
[29f9e20] | 346 | handleType( expr->type ); |
---|
| 347 | } |
---|
| 348 | |
---|
[15f5c5e] | 349 | void HoistTypeDecls::previsit( UntypedOffsetofExpr * expr ) { |
---|
[29f9e20] | 350 | handleType( expr->type ); |
---|
| 351 | } |
---|
| 352 | |
---|
[95d09bdb] | 353 | void HoistTypeDecls::previsit( CompoundLiteralExpr * expr ) { |
---|
| 354 | handleType( expr->result ); |
---|
| 355 | } |
---|
| 356 | |
---|
[29f9e20] | 357 | |
---|
[a12c81f3] | 358 | Type * FixQualifiedTypes::postmutate( QualifiedType * qualType ) { |
---|
| 359 | Type * parent = qualType->parent; |
---|
| 360 | Type * child = qualType->child; |
---|
| 361 | if ( dynamic_cast< GlobalScopeType * >( qualType->parent ) ) { |
---|
| 362 | // .T => lookup T at global scope |
---|
[062e8df] | 363 | if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( child ) ) { |
---|
[a12c81f3] | 364 | auto td = indexer.globalLookupType( inst->name ); |
---|
[062e8df] | 365 | if ( ! td ) { |
---|
| 366 | SemanticError( qualType->location, toString("Use of undefined global type ", inst->name) ); |
---|
| 367 | } |
---|
[a12c81f3] | 368 | auto base = td->base; |
---|
[062e8df] | 369 | assert( base ); |
---|
[8a3ecb9] | 370 | Type * ret = base->clone(); |
---|
| 371 | ret->get_qualifiers() = qualType->get_qualifiers(); |
---|
| 372 | return ret; |
---|
[a12c81f3] | 373 | } else { |
---|
[062e8df] | 374 | // .T => T is not a type name |
---|
| 375 | assertf( false, "unhandled global qualified child type: %s", toCString(child) ); |
---|
[a12c81f3] | 376 | } |
---|
| 377 | } else { |
---|
| 378 | // S.T => S must be an aggregate type, find the declaration for T in S. |
---|
| 379 | AggregateDecl * aggr = nullptr; |
---|
| 380 | if ( StructInstType * inst = dynamic_cast< StructInstType * >( parent ) ) { |
---|
| 381 | aggr = inst->baseStruct; |
---|
| 382 | } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * > ( parent ) ) { |
---|
| 383 | aggr = inst->baseUnion; |
---|
| 384 | } else { |
---|
[062e8df] | 385 | SemanticError( qualType->location, toString("Qualified type requires an aggregate on the left, but has: ", parent) ); |
---|
[a12c81f3] | 386 | } |
---|
| 387 | assert( aggr ); // TODO: need to handle forward declarations |
---|
| 388 | for ( Declaration * member : aggr->members ) { |
---|
| 389 | if ( StructInstType * inst = dynamic_cast< StructInstType * >( child ) ) { |
---|
| 390 | if ( StructDecl * aggr = dynamic_cast< StructDecl * >( member ) ) { |
---|
| 391 | if ( aggr->name == inst->name ) { |
---|
[8a3ecb9] | 392 | // TODO: is this case, and other non-TypeInstType cases, necessary? |
---|
[a12c81f3] | 393 | return new StructInstType( qualType->get_qualifiers(), aggr ); |
---|
| 394 | } |
---|
| 395 | } |
---|
| 396 | } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( child ) ) { |
---|
| 397 | if ( UnionDecl * aggr = dynamic_cast< UnionDecl * > ( member ) ) { |
---|
| 398 | if ( aggr->name == inst->name ) { |
---|
| 399 | return new UnionInstType( qualType->get_qualifiers(), aggr ); |
---|
| 400 | } |
---|
| 401 | } |
---|
| 402 | } else if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( child ) ) { |
---|
| 403 | if ( EnumDecl * aggr = dynamic_cast< EnumDecl * > ( member ) ) { |
---|
| 404 | if ( aggr->name == inst->name ) { |
---|
| 405 | return new EnumInstType( qualType->get_qualifiers(), aggr ); |
---|
| 406 | } |
---|
| 407 | } |
---|
| 408 | } else if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( child ) ) { |
---|
[8a3ecb9] | 409 | // name on the right is a typedef |
---|
[a12c81f3] | 410 | if ( NamedTypeDecl * aggr = dynamic_cast< NamedTypeDecl * > ( member ) ) { |
---|
| 411 | if ( aggr->name == inst->name ) { |
---|
[062e8df] | 412 | assert( aggr->base ); |
---|
[8a3ecb9] | 413 | Type * ret = aggr->base->clone(); |
---|
| 414 | ret->get_qualifiers() = qualType->get_qualifiers(); |
---|
| 415 | return ret; |
---|
[a12c81f3] | 416 | } |
---|
| 417 | } |
---|
| 418 | } else { |
---|
| 419 | // S.T - S is not an aggregate => error |
---|
| 420 | assertf( false, "unhandled qualified child type: %s", toCString(qualType) ); |
---|
| 421 | } |
---|
| 422 | } |
---|
| 423 | // failed to find a satisfying definition of type |
---|
[062e8df] | 424 | SemanticError( qualType->location, toString("Undefined type in qualified type: ", qualType) ); |
---|
[a12c81f3] | 425 | } |
---|
| 426 | |
---|
| 427 | // ... may want to link canonical SUE definition to each forward decl so that it becomes easier to lookup? |
---|
| 428 | } |
---|
| 429 | |
---|
| 430 | |
---|
[a08ba92] | 431 | void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) { |
---|
[a09e45b] | 432 | PassVisitor<HoistStruct> hoister; |
---|
| 433 | acceptAll( translationUnit, hoister ); |
---|
[a08ba92] | 434 | } |
---|
[c8ffe20b] | 435 | |
---|
[0f40912] | 436 | bool shouldHoist( Declaration *decl ) { |
---|
| 437 | return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl ) || dynamic_cast< StaticAssertDecl * >( decl ); |
---|
[a08ba92] | 438 | } |
---|
[c0aa336] | 439 | |
---|
[d419d8e] | 440 | namespace { |
---|
| 441 | void qualifiedName( AggregateDecl * aggr, std::ostringstream & ss ) { |
---|
| 442 | if ( aggr->parent ) qualifiedName( aggr->parent, ss ); |
---|
| 443 | ss << "__" << aggr->name; |
---|
| 444 | } |
---|
| 445 | |
---|
| 446 | // mangle nested type names using entire parent chain |
---|
| 447 | std::string qualifiedName( AggregateDecl * aggr ) { |
---|
| 448 | std::ostringstream ss; |
---|
| 449 | qualifiedName( aggr, ss ); |
---|
| 450 | return ss.str(); |
---|
| 451 | } |
---|
| 452 | } |
---|
| 453 | |
---|
[a08ba92] | 454 | template< typename AggDecl > |
---|
| 455 | void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) { |
---|
[bdad6eb7] | 456 | if ( parentAggr ) { |
---|
[d419d8e] | 457 | aggregateDecl->parent = parentAggr; |
---|
| 458 | aggregateDecl->name = qualifiedName( aggregateDecl ); |
---|
[0dd3a2f] | 459 | // Add elements in stack order corresponding to nesting structure. |
---|
[a09e45b] | 460 | declsToAddBefore.push_front( aggregateDecl ); |
---|
[0dd3a2f] | 461 | } else { |
---|
[bdad6eb7] | 462 | GuardValue( parentAggr ); |
---|
| 463 | parentAggr = aggregateDecl; |
---|
[0dd3a2f] | 464 | } // if |
---|
| 465 | // Always remove the hoisted aggregate from the inner structure. |
---|
[0f40912] | 466 | GuardAction( [aggregateDecl]() { filter( aggregateDecl->members, shouldHoist, false ); } ); |
---|
[a08ba92] | 467 | } |
---|
[c8ffe20b] | 468 | |
---|
[0f40912] | 469 | void HoistStruct::previsit( StaticAssertDecl * assertDecl ) { |
---|
| 470 | if ( parentAggr ) { |
---|
| 471 | declsToAddBefore.push_back( assertDecl ); |
---|
| 472 | } |
---|
| 473 | } |
---|
| 474 | |
---|
[a09e45b] | 475 | void HoistStruct::previsit( StructDecl * aggregateDecl ) { |
---|
[0dd3a2f] | 476 | handleAggregate( aggregateDecl ); |
---|
[a08ba92] | 477 | } |
---|
[c8ffe20b] | 478 | |
---|
[a09e45b] | 479 | void HoistStruct::previsit( UnionDecl * aggregateDecl ) { |
---|
[0dd3a2f] | 480 | handleAggregate( aggregateDecl ); |
---|
[a08ba92] | 481 | } |
---|
[c8ffe20b] | 482 | |
---|
[d419d8e] | 483 | void HoistStruct::previsit( StructInstType * type ) { |
---|
| 484 | // need to reset type name after expanding to qualified name |
---|
| 485 | assert( type->baseStruct ); |
---|
| 486 | type->name = type->baseStruct->name; |
---|
| 487 | } |
---|
| 488 | |
---|
| 489 | void HoistStruct::previsit( UnionInstType * type ) { |
---|
| 490 | assert( type->baseUnion ); |
---|
| 491 | type->name = type->baseUnion->name; |
---|
| 492 | } |
---|
| 493 | |
---|
| 494 | void HoistStruct::previsit( EnumInstType * type ) { |
---|
| 495 | assert( type->baseEnum ); |
---|
| 496 | type->name = type->baseEnum->name; |
---|
| 497 | } |
---|
| 498 | |
---|
| 499 | |
---|
[69918cea] | 500 | bool isTypedef( Declaration *decl ) { |
---|
| 501 | return dynamic_cast< TypedefDecl * >( decl ); |
---|
| 502 | } |
---|
| 503 | |
---|
| 504 | void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) { |
---|
| 505 | PassVisitor<EliminateTypedef> eliminator; |
---|
| 506 | acceptAll( translationUnit, eliminator ); |
---|
| 507 | filter( translationUnit, isTypedef, true ); |
---|
| 508 | } |
---|
| 509 | |
---|
| 510 | template< typename AggDecl > |
---|
| 511 | void EliminateTypedef::handleAggregate( AggDecl *aggregateDecl ) { |
---|
| 512 | filter( aggregateDecl->members, isTypedef, true ); |
---|
| 513 | } |
---|
| 514 | |
---|
| 515 | void EliminateTypedef::previsit( StructDecl * aggregateDecl ) { |
---|
| 516 | handleAggregate( aggregateDecl ); |
---|
| 517 | } |
---|
| 518 | |
---|
| 519 | void EliminateTypedef::previsit( UnionDecl * aggregateDecl ) { |
---|
| 520 | handleAggregate( aggregateDecl ); |
---|
| 521 | } |
---|
| 522 | |
---|
| 523 | void EliminateTypedef::previsit( CompoundStmt * compoundStmt ) { |
---|
| 524 | // remove and delete decl stmts |
---|
| 525 | filter( compoundStmt->kids, [](Statement * stmt) { |
---|
| 526 | if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( stmt ) ) { |
---|
| 527 | if ( dynamic_cast< TypedefDecl * >( declStmt->decl ) ) { |
---|
| 528 | return true; |
---|
| 529 | } // if |
---|
| 530 | } // if |
---|
| 531 | return false; |
---|
| 532 | }, true); |
---|
| 533 | } |
---|
| 534 | |
---|
[06edda0] | 535 | void EnumAndPointerDecay::previsit( EnumDecl *enumDecl ) { |
---|
[0dd3a2f] | 536 | // Set the type of each member of the enumeration to be EnumConstant |
---|
[0b3b2ae] | 537 | for ( std::list< Declaration * >::iterator i = enumDecl->members.begin(); i != enumDecl->members.end(); ++i ) { |
---|
[f6d7e0f] | 538 | ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i ); |
---|
[0dd3a2f] | 539 | assert( obj ); |
---|
[0b3b2ae] | 540 | obj->set_type( new EnumInstType( Type::Qualifiers( Type::Const ), enumDecl->name ) ); |
---|
[0dd3a2f] | 541 | } // for |
---|
[a08ba92] | 542 | } |
---|
[51b7345] | 543 | |
---|
[a08ba92] | 544 | namespace { |
---|
[83de11e] | 545 | template< typename DWTList > |
---|
[4bda2cf] | 546 | void fixFunctionList( DWTList & dwts, bool isVarArgs, FunctionType * func ) { |
---|
| 547 | auto nvals = dwts.size(); |
---|
| 548 | bool containsVoid = false; |
---|
| 549 | for ( auto & dwt : dwts ) { |
---|
| 550 | // fix each DWT and record whether a void was found |
---|
| 551 | containsVoid |= fixFunction( dwt ); |
---|
| 552 | } |
---|
| 553 | |
---|
| 554 | // the only case in which "void" is valid is where it is the only one in the list |
---|
| 555 | if ( containsVoid && ( nvals > 1 || isVarArgs ) ) { |
---|
[a16764a6] | 556 | SemanticError( func, "invalid type void in function type " ); |
---|
[4bda2cf] | 557 | } |
---|
| 558 | |
---|
| 559 | // one void is the only thing in the list; remove it. |
---|
| 560 | if ( containsVoid ) { |
---|
| 561 | delete dwts.front(); |
---|
| 562 | dwts.clear(); |
---|
| 563 | } |
---|
[0dd3a2f] | 564 | } |
---|
[a08ba92] | 565 | } |
---|
[c8ffe20b] | 566 | |
---|
[06edda0] | 567 | void EnumAndPointerDecay::previsit( FunctionType *func ) { |
---|
[0dd3a2f] | 568 | // Fix up parameters and return types |
---|
[4bda2cf] | 569 | fixFunctionList( func->parameters, func->isVarArgs, func ); |
---|
| 570 | fixFunctionList( func->returnVals, false, func ); |
---|
[a08ba92] | 571 | } |
---|
[c8ffe20b] | 572 | |
---|
[522363e] | 573 | LinkReferenceToTypes::LinkReferenceToTypes( const Indexer *other_indexer ) { |
---|
[0dd3a2f] | 574 | if ( other_indexer ) { |
---|
[522363e] | 575 | local_indexer = other_indexer; |
---|
[0dd3a2f] | 576 | } else { |
---|
[522363e] | 577 | local_indexer = &indexer; |
---|
[0dd3a2f] | 578 | } // if |
---|
[a08ba92] | 579 | } |
---|
[c8ffe20b] | 580 | |
---|
[522363e] | 581 | void LinkReferenceToTypes::postvisit( EnumInstType *enumInst ) { |
---|
[eaa6430] | 582 | EnumDecl *st = local_indexer->lookupEnum( enumInst->name ); |
---|
[c0aa336] | 583 | // it's not a semantic error if the enum is not found, just an implicit forward declaration |
---|
| 584 | if ( st ) { |
---|
[eaa6430] | 585 | enumInst->baseEnum = st; |
---|
[c0aa336] | 586 | } // if |
---|
[29f9e20] | 587 | if ( ! st || ! st->body ) { |
---|
[c0aa336] | 588 | // use of forward declaration |
---|
[eaa6430] | 589 | forwardEnums[ enumInst->name ].push_back( enumInst ); |
---|
[c0aa336] | 590 | } // if |
---|
| 591 | } |
---|
| 592 | |
---|
[48fa824] | 593 | void checkGenericParameters( ReferenceToType * inst ) { |
---|
| 594 | for ( Expression * param : inst->parameters ) { |
---|
| 595 | if ( ! dynamic_cast< TypeExpr * >( param ) ) { |
---|
[a16764a6] | 596 | SemanticError( inst, "Expression parameters for generic types are currently unsupported: " ); |
---|
[48fa824] | 597 | } |
---|
| 598 | } |
---|
| 599 | } |
---|
| 600 | |
---|
[522363e] | 601 | void LinkReferenceToTypes::postvisit( StructInstType *structInst ) { |
---|
[eaa6430] | 602 | StructDecl *st = local_indexer->lookupStruct( structInst->name ); |
---|
[0dd3a2f] | 603 | // it's not a semantic error if the struct is not found, just an implicit forward declaration |
---|
| 604 | if ( st ) { |
---|
[eaa6430] | 605 | structInst->baseStruct = st; |
---|
[0dd3a2f] | 606 | } // if |
---|
[29f9e20] | 607 | if ( ! st || ! st->body ) { |
---|
[0dd3a2f] | 608 | // use of forward declaration |
---|
[eaa6430] | 609 | forwardStructs[ structInst->name ].push_back( structInst ); |
---|
[0dd3a2f] | 610 | } // if |
---|
[48fa824] | 611 | checkGenericParameters( structInst ); |
---|
[a08ba92] | 612 | } |
---|
[c8ffe20b] | 613 | |
---|
[522363e] | 614 | void LinkReferenceToTypes::postvisit( UnionInstType *unionInst ) { |
---|
[eaa6430] | 615 | UnionDecl *un = local_indexer->lookupUnion( unionInst->name ); |
---|
[0dd3a2f] | 616 | // it's not a semantic error if the union is not found, just an implicit forward declaration |
---|
| 617 | if ( un ) { |
---|
[eaa6430] | 618 | unionInst->baseUnion = un; |
---|
[0dd3a2f] | 619 | } // if |
---|
[29f9e20] | 620 | if ( ! un || ! un->body ) { |
---|
[0dd3a2f] | 621 | // use of forward declaration |
---|
[eaa6430] | 622 | forwardUnions[ unionInst->name ].push_back( unionInst ); |
---|
[0dd3a2f] | 623 | } // if |
---|
[48fa824] | 624 | checkGenericParameters( unionInst ); |
---|
[a08ba92] | 625 | } |
---|
[c8ffe20b] | 626 | |
---|
[afcb0a3] | 627 | void LinkReferenceToTypes::previsit( QualifiedType * ) { |
---|
| 628 | visit_children = false; |
---|
| 629 | } |
---|
| 630 | |
---|
| 631 | void LinkReferenceToTypes::postvisit( QualifiedType * qualType ) { |
---|
| 632 | // linking only makes sense for the 'oldest ancestor' of the qualified type |
---|
| 633 | qualType->parent->accept( *visitor ); |
---|
| 634 | } |
---|
| 635 | |
---|
[be9036d] | 636 | template< typename Decl > |
---|
| 637 | void normalizeAssertions( std::list< Decl * > & assertions ) { |
---|
| 638 | // ensure no duplicate trait members after the clone |
---|
| 639 | auto pred = [](Decl * d1, Decl * d2) { |
---|
| 640 | // only care if they're equal |
---|
| 641 | DeclarationWithType * dwt1 = dynamic_cast<DeclarationWithType *>( d1 ); |
---|
| 642 | DeclarationWithType * dwt2 = dynamic_cast<DeclarationWithType *>( d2 ); |
---|
| 643 | if ( dwt1 && dwt2 ) { |
---|
[eaa6430] | 644 | if ( dwt1->name == dwt2->name && ResolvExpr::typesCompatible( dwt1->get_type(), dwt2->get_type(), SymTab::Indexer() ) ) { |
---|
[be9036d] | 645 | // std::cerr << "=========== equal:" << std::endl; |
---|
| 646 | // std::cerr << "d1: " << d1 << std::endl; |
---|
| 647 | // std::cerr << "d2: " << d2 << std::endl; |
---|
| 648 | return false; |
---|
| 649 | } |
---|
[2c57025] | 650 | } |
---|
[be9036d] | 651 | return d1 < d2; |
---|
| 652 | }; |
---|
| 653 | std::set<Decl *, decltype(pred)> unique_members( assertions.begin(), assertions.end(), pred ); |
---|
| 654 | // if ( unique_members.size() != assertions.size() ) { |
---|
| 655 | // std::cerr << "============different" << std::endl; |
---|
| 656 | // std::cerr << unique_members.size() << " " << assertions.size() << std::endl; |
---|
| 657 | // } |
---|
| 658 | |
---|
| 659 | std::list< Decl * > order; |
---|
| 660 | order.splice( order.end(), assertions ); |
---|
| 661 | std::copy_if( order.begin(), order.end(), back_inserter( assertions ), [&]( Decl * decl ) { |
---|
| 662 | return unique_members.count( decl ); |
---|
| 663 | }); |
---|
| 664 | } |
---|
| 665 | |
---|
| 666 | // expand assertions from trait instance, performing the appropriate type variable substitutions |
---|
| 667 | template< typename Iterator > |
---|
| 668 | void expandAssertions( TraitInstType * inst, Iterator out ) { |
---|
[eaa6430] | 669 | assertf( inst->baseTrait, "Trait instance not linked to base trait: %s", toCString( inst ) ); |
---|
[be9036d] | 670 | std::list< DeclarationWithType * > asserts; |
---|
| 671 | for ( Declaration * decl : inst->baseTrait->members ) { |
---|
[e3e16bc] | 672 | asserts.push_back( strict_dynamic_cast<DeclarationWithType *>( decl->clone() ) ); |
---|
[2c57025] | 673 | } |
---|
[be9036d] | 674 | // substitute trait decl parameters for instance parameters |
---|
| 675 | applySubstitution( inst->baseTrait->parameters.begin(), inst->baseTrait->parameters.end(), inst->parameters.begin(), asserts.begin(), asserts.end(), out ); |
---|
| 676 | } |
---|
| 677 | |
---|
[522363e] | 678 | void LinkReferenceToTypes::postvisit( TraitDecl * traitDecl ) { |
---|
[be9036d] | 679 | if ( traitDecl->name == "sized" ) { |
---|
| 680 | // "sized" is a special trait - flick the sized status on for the type variable |
---|
| 681 | assertf( traitDecl->parameters.size() == 1, "Built-in trait 'sized' has incorrect number of parameters: %zd", traitDecl->parameters.size() ); |
---|
| 682 | TypeDecl * td = traitDecl->parameters.front(); |
---|
| 683 | td->set_sized( true ); |
---|
| 684 | } |
---|
| 685 | |
---|
| 686 | // move assertions from type parameters into the body of the trait |
---|
| 687 | for ( TypeDecl * td : traitDecl->parameters ) { |
---|
| 688 | for ( DeclarationWithType * assert : td->assertions ) { |
---|
| 689 | if ( TraitInstType * inst = dynamic_cast< TraitInstType * >( assert->get_type() ) ) { |
---|
| 690 | expandAssertions( inst, back_inserter( traitDecl->members ) ); |
---|
| 691 | } else { |
---|
| 692 | traitDecl->members.push_back( assert->clone() ); |
---|
| 693 | } |
---|
| 694 | } |
---|
| 695 | deleteAll( td->assertions ); |
---|
| 696 | td->assertions.clear(); |
---|
| 697 | } // for |
---|
| 698 | } |
---|
[2ae171d8] | 699 | |
---|
[522363e] | 700 | void LinkReferenceToTypes::postvisit( TraitInstType * traitInst ) { |
---|
[2ae171d8] | 701 | // handle other traits |
---|
[522363e] | 702 | TraitDecl *traitDecl = local_indexer->lookupTrait( traitInst->name ); |
---|
[4a9ccc3] | 703 | if ( ! traitDecl ) { |
---|
[a16764a6] | 704 | SemanticError( traitInst->location, "use of undeclared trait " + traitInst->name ); |
---|
[17cd4eb] | 705 | } // if |
---|
[0b3b2ae] | 706 | if ( traitDecl->parameters.size() != traitInst->parameters.size() ) { |
---|
[a16764a6] | 707 | SemanticError( traitInst, "incorrect number of trait parameters: " ); |
---|
[4a9ccc3] | 708 | } // if |
---|
[be9036d] | 709 | traitInst->baseTrait = traitDecl; |
---|
[79970ed] | 710 | |
---|
[4a9ccc3] | 711 | // need to carry over the 'sized' status of each decl in the instance |
---|
[eaa6430] | 712 | for ( auto p : group_iterate( traitDecl->parameters, traitInst->parameters ) ) { |
---|
[5c4d27f] | 713 | TypeExpr * expr = dynamic_cast< TypeExpr * >( std::get<1>(p) ); |
---|
| 714 | if ( ! expr ) { |
---|
[a16764a6] | 715 | SemanticError( std::get<1>(p), "Expression parameters for trait instances are currently unsupported: " ); |
---|
[5c4d27f] | 716 | } |
---|
[4a9ccc3] | 717 | if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( expr->get_type() ) ) { |
---|
| 718 | TypeDecl * formalDecl = std::get<0>(p); |
---|
[eaa6430] | 719 | TypeDecl * instDecl = inst->baseType; |
---|
[4a9ccc3] | 720 | if ( formalDecl->get_sized() ) instDecl->set_sized( true ); |
---|
| 721 | } |
---|
| 722 | } |
---|
[be9036d] | 723 | // normalizeAssertions( traitInst->members ); |
---|
[a08ba92] | 724 | } |
---|
[c8ffe20b] | 725 | |
---|
[522363e] | 726 | void LinkReferenceToTypes::postvisit( EnumDecl *enumDecl ) { |
---|
[c0aa336] | 727 | // visit enum members first so that the types of self-referencing members are updated properly |
---|
[b16923d] | 728 | if ( enumDecl->body ) { |
---|
[eaa6430] | 729 | ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl->name ); |
---|
[c0aa336] | 730 | if ( fwds != forwardEnums.end() ) { |
---|
| 731 | for ( std::list< EnumInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
[eaa6430] | 732 | (*inst)->baseEnum = enumDecl; |
---|
[c0aa336] | 733 | } // for |
---|
| 734 | forwardEnums.erase( fwds ); |
---|
| 735 | } // if |
---|
| 736 | } // if |
---|
| 737 | } |
---|
| 738 | |
---|
[b95fe40] | 739 | void LinkReferenceToTypes::renameGenericParams( std::list< TypeDecl * > & params ) { |
---|
| 740 | // rename generic type parameters uniquely so that they do not conflict with user-defined function forall parameters, e.g. |
---|
| 741 | // forall(otype T) |
---|
| 742 | // struct Box { |
---|
| 743 | // T x; |
---|
| 744 | // }; |
---|
| 745 | // forall(otype T) |
---|
| 746 | // void f(Box(T) b) { |
---|
| 747 | // ... |
---|
| 748 | // } |
---|
| 749 | // The T in Box and the T in f are different, so internally the naming must reflect that. |
---|
| 750 | GuardValue( inGeneric ); |
---|
| 751 | inGeneric = ! params.empty(); |
---|
| 752 | for ( TypeDecl * td : params ) { |
---|
| 753 | td->name = "__" + td->name + "_generic_"; |
---|
| 754 | } |
---|
| 755 | } |
---|
| 756 | |
---|
| 757 | void LinkReferenceToTypes::previsit( StructDecl * structDecl ) { |
---|
| 758 | renameGenericParams( structDecl->parameters ); |
---|
| 759 | } |
---|
| 760 | |
---|
| 761 | void LinkReferenceToTypes::previsit( UnionDecl * unionDecl ) { |
---|
| 762 | renameGenericParams( unionDecl->parameters ); |
---|
| 763 | } |
---|
| 764 | |
---|
[522363e] | 765 | void LinkReferenceToTypes::postvisit( StructDecl *structDecl ) { |
---|
[677c1be] | 766 | // visit struct members first so that the types of self-referencing members are updated properly |
---|
[522363e] | 767 | // xxx - need to ensure that type parameters match up between forward declarations and definition (most importantly, number of type parameters and their defaults) |
---|
[b16923d] | 768 | if ( structDecl->body ) { |
---|
[eaa6430] | 769 | ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->name ); |
---|
[0dd3a2f] | 770 | if ( fwds != forwardStructs.end() ) { |
---|
| 771 | for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
[eaa6430] | 772 | (*inst)->baseStruct = structDecl; |
---|
[0dd3a2f] | 773 | } // for |
---|
| 774 | forwardStructs.erase( fwds ); |
---|
| 775 | } // if |
---|
| 776 | } // if |
---|
[a08ba92] | 777 | } |
---|
[c8ffe20b] | 778 | |
---|
[522363e] | 779 | void LinkReferenceToTypes::postvisit( UnionDecl *unionDecl ) { |
---|
[b16923d] | 780 | if ( unionDecl->body ) { |
---|
[eaa6430] | 781 | ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->name ); |
---|
[0dd3a2f] | 782 | if ( fwds != forwardUnions.end() ) { |
---|
| 783 | for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
[eaa6430] | 784 | (*inst)->baseUnion = unionDecl; |
---|
[0dd3a2f] | 785 | } // for |
---|
| 786 | forwardUnions.erase( fwds ); |
---|
| 787 | } // if |
---|
| 788 | } // if |
---|
[a08ba92] | 789 | } |
---|
[c8ffe20b] | 790 | |
---|
[522363e] | 791 | void LinkReferenceToTypes::postvisit( TypeInstType *typeInst ) { |
---|
[b95fe40] | 792 | // ensure generic parameter instances are renamed like the base type |
---|
| 793 | if ( inGeneric && typeInst->baseType ) typeInst->name = typeInst->baseType->name; |
---|
[eaa6430] | 794 | if ( NamedTypeDecl *namedTypeDecl = local_indexer->lookupType( typeInst->name ) ) { |
---|
[0dd3a2f] | 795 | if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) { |
---|
| 796 | typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype ); |
---|
| 797 | } // if |
---|
| 798 | } // if |
---|
[a08ba92] | 799 | } |
---|
[c8ffe20b] | 800 | |
---|
[4a9ccc3] | 801 | /// Fix up assertions - flattens assertion lists, removing all trait instances |
---|
[8b11840] | 802 | void forallFixer( std::list< TypeDecl * > & forall, BaseSyntaxNode * node ) { |
---|
| 803 | for ( TypeDecl * type : forall ) { |
---|
[be9036d] | 804 | std::list< DeclarationWithType * > asserts; |
---|
| 805 | asserts.splice( asserts.end(), type->assertions ); |
---|
| 806 | // expand trait instances into their members |
---|
| 807 | for ( DeclarationWithType * assertion : asserts ) { |
---|
| 808 | if ( TraitInstType *traitInst = dynamic_cast< TraitInstType * >( assertion->get_type() ) ) { |
---|
| 809 | // expand trait instance into all of its members |
---|
| 810 | expandAssertions( traitInst, back_inserter( type->assertions ) ); |
---|
| 811 | delete traitInst; |
---|
| 812 | } else { |
---|
| 813 | // pass other assertions through |
---|
| 814 | type->assertions.push_back( assertion ); |
---|
| 815 | } // if |
---|
| 816 | } // for |
---|
| 817 | // apply FixFunction to every assertion to check for invalid void type |
---|
| 818 | for ( DeclarationWithType *& assertion : type->assertions ) { |
---|
[4bda2cf] | 819 | bool isVoid = fixFunction( assertion ); |
---|
| 820 | if ( isVoid ) { |
---|
[a16764a6] | 821 | SemanticError( node, "invalid type void in assertion of function " ); |
---|
[be9036d] | 822 | } // if |
---|
| 823 | } // for |
---|
| 824 | // normalizeAssertions( type->assertions ); |
---|
[0dd3a2f] | 825 | } // for |
---|
[a08ba92] | 826 | } |
---|
[c8ffe20b] | 827 | |
---|
[522363e] | 828 | void ForallPointerDecay::previsit( ObjectDecl *object ) { |
---|
[3d2b7bc] | 829 | // ensure that operator names only apply to functions or function pointers |
---|
| 830 | if ( CodeGen::isOperator( object->name ) && ! dynamic_cast< FunctionType * >( object->type->stripDeclarator() ) ) { |
---|
| 831 | SemanticError( object->location, toCString( "operator ", object->name.c_str(), " is not a function or function pointer." ) ); |
---|
| 832 | } |
---|
[0dd3a2f] | 833 | object->fixUniqueId(); |
---|
[a08ba92] | 834 | } |
---|
[c8ffe20b] | 835 | |
---|
[522363e] | 836 | void ForallPointerDecay::previsit( FunctionDecl *func ) { |
---|
[0dd3a2f] | 837 | func->fixUniqueId(); |
---|
[a08ba92] | 838 | } |
---|
[c8ffe20b] | 839 | |
---|
[bbf3fda] | 840 | void ForallPointerDecay::previsit( FunctionType * ftype ) { |
---|
| 841 | forallFixer( ftype->forall, ftype ); |
---|
| 842 | } |
---|
| 843 | |
---|
[bd7e609] | 844 | void ForallPointerDecay::previsit( StructDecl * aggrDecl ) { |
---|
| 845 | forallFixer( aggrDecl->parameters, aggrDecl ); |
---|
| 846 | } |
---|
| 847 | |
---|
| 848 | void ForallPointerDecay::previsit( UnionDecl * aggrDecl ) { |
---|
| 849 | forallFixer( aggrDecl->parameters, aggrDecl ); |
---|
| 850 | } |
---|
| 851 | |
---|
[de91427b] | 852 | void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) { |
---|
[0db6fc0] | 853 | PassVisitor<ReturnChecker> checker; |
---|
[de91427b] | 854 | acceptAll( translationUnit, checker ); |
---|
| 855 | } |
---|
| 856 | |
---|
[0db6fc0] | 857 | void ReturnChecker::previsit( FunctionDecl * functionDecl ) { |
---|
[0508ab3] | 858 | GuardValue( returnVals ); |
---|
[de91427b] | 859 | returnVals = functionDecl->get_functionType()->get_returnVals(); |
---|
| 860 | } |
---|
| 861 | |
---|
[0db6fc0] | 862 | void ReturnChecker::previsit( ReturnStmt * returnStmt ) { |
---|
[74d1804] | 863 | // Previously this also checked for the existence of an expr paired with no return values on |
---|
| 864 | // the function return type. This is incorrect, since you can have an expression attached to |
---|
| 865 | // a return statement in a void-returning function in C. The expression is treated as if it |
---|
| 866 | // were cast to void. |
---|
[30f9072] | 867 | if ( ! returnStmt->get_expr() && returnVals.size() != 0 ) { |
---|
[a16764a6] | 868 | SemanticError( returnStmt, "Non-void function returns no values: " ); |
---|
[de91427b] | 869 | } |
---|
| 870 | } |
---|
| 871 | |
---|
| 872 | |
---|
[48ed81c] | 873 | void ReplaceTypedef::replaceTypedef( std::list< Declaration * > &translationUnit ) { |
---|
| 874 | PassVisitor<ReplaceTypedef> eliminator; |
---|
[0dd3a2f] | 875 | mutateAll( translationUnit, eliminator ); |
---|
[a506df4] | 876 | if ( eliminator.pass.typedefNames.count( "size_t" ) ) { |
---|
[5f98ce5] | 877 | // grab and remember declaration of size_t |
---|
[0b3b2ae] | 878 | SizeType = eliminator.pass.typedefNames["size_t"].first->base->clone(); |
---|
[5f98ce5] | 879 | } else { |
---|
[40e636a] | 880 | // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong |
---|
| 881 | // eventually should have a warning for this case. |
---|
| 882 | SizeType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
[5f98ce5] | 883 | } |
---|
[a08ba92] | 884 | } |
---|
[c8ffe20b] | 885 | |
---|
[48ed81c] | 886 | void ReplaceTypedef::premutate( QualifiedType * ) { |
---|
| 887 | visit_children = false; |
---|
| 888 | } |
---|
| 889 | |
---|
| 890 | Type * ReplaceTypedef::postmutate( QualifiedType * qualType ) { |
---|
| 891 | // replacing typedefs only makes sense for the 'oldest ancestor' of the qualified type |
---|
| 892 | qualType->parent = qualType->parent->acceptMutator( *visitor ); |
---|
| 893 | return qualType; |
---|
| 894 | } |
---|
| 895 | |
---|
| 896 | Type * ReplaceTypedef::postmutate( TypeInstType * typeInst ) { |
---|
[9cb8e88d] | 897 | // instances of typedef types will come here. If it is an instance |
---|
[cc79d97] | 898 | // of a typdef type, link the instance to its actual type. |
---|
[0b3b2ae] | 899 | TypedefMap::const_iterator def = typedefNames.find( typeInst->name ); |
---|
[0dd3a2f] | 900 | if ( def != typedefNames.end() ) { |
---|
[f53836b] | 901 | Type *ret = def->second.first->base->clone(); |
---|
[e82ef13] | 902 | ret->location = typeInst->location; |
---|
[6f95000] | 903 | ret->get_qualifiers() |= typeInst->get_qualifiers(); |
---|
[1f370451] | 904 | // attributes are not carried over from typedef to function parameters/return values |
---|
| 905 | if ( ! inFunctionType ) { |
---|
| 906 | ret->attributes.splice( ret->attributes.end(), typeInst->attributes ); |
---|
| 907 | } else { |
---|
| 908 | deleteAll( ret->attributes ); |
---|
| 909 | ret->attributes.clear(); |
---|
| 910 | } |
---|
[0215a76f] | 911 | // place instance parameters on the typedef'd type |
---|
[f53836b] | 912 | if ( ! typeInst->parameters.empty() ) { |
---|
[0215a76f] | 913 | ReferenceToType *rtt = dynamic_cast<ReferenceToType*>(ret); |
---|
| 914 | if ( ! rtt ) { |
---|
[a16764a6] | 915 | SemanticError( typeInst->location, "Cannot apply type parameters to base type of " + typeInst->name ); |
---|
[0215a76f] | 916 | } |
---|
[0b3b2ae] | 917 | rtt->parameters.clear(); |
---|
[f53836b] | 918 | cloneAll( typeInst->parameters, rtt->parameters ); |
---|
| 919 | mutateAll( rtt->parameters, *visitor ); // recursively fix typedefs on parameters |
---|
[1db21619] | 920 | } // if |
---|
[0dd3a2f] | 921 | delete typeInst; |
---|
| 922 | return ret; |
---|
[679864e1] | 923 | } else { |
---|
[0b3b2ae] | 924 | TypeDeclMap::const_iterator base = typedeclNames.find( typeInst->name ); |
---|
[062e8df] | 925 | if ( base == typedeclNames.end() ) { |
---|
| 926 | SemanticError( typeInst->location, toString("Use of undefined type ", typeInst->name) ); |
---|
| 927 | } |
---|
[1e8b02f5] | 928 | typeInst->set_baseType( base->second ); |
---|
[062e8df] | 929 | return typeInst; |
---|
[0dd3a2f] | 930 | } // if |
---|
[062e8df] | 931 | assert( false ); |
---|
[a08ba92] | 932 | } |
---|
[c8ffe20b] | 933 | |
---|
[f53836b] | 934 | struct VarLenChecker : WithShortCircuiting { |
---|
| 935 | void previsit( FunctionType * ) { visit_children = false; } |
---|
| 936 | void previsit( ArrayType * at ) { |
---|
| 937 | isVarLen |= at->isVarLen; |
---|
| 938 | } |
---|
| 939 | bool isVarLen = false; |
---|
| 940 | }; |
---|
| 941 | |
---|
| 942 | bool isVariableLength( Type * t ) { |
---|
| 943 | PassVisitor<VarLenChecker> varLenChecker; |
---|
| 944 | maybeAccept( t, varLenChecker ); |
---|
| 945 | return varLenChecker.pass.isVarLen; |
---|
| 946 | } |
---|
| 947 | |
---|
[48ed81c] | 948 | Declaration * ReplaceTypedef::postmutate( TypedefDecl * tyDecl ) { |
---|
[0b3b2ae] | 949 | if ( typedefNames.count( tyDecl->name ) == 1 && typedefNames[ tyDecl->name ].second == scopeLevel ) { |
---|
[9cb8e88d] | 950 | // typedef to the same name from the same scope |
---|
[cc79d97] | 951 | // must be from the same type |
---|
| 952 | |
---|
[0b3b2ae] | 953 | Type * t1 = tyDecl->base; |
---|
| 954 | Type * t2 = typedefNames[ tyDecl->name ].first->base; |
---|
[1cbca6e] | 955 | if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) { |
---|
[a16764a6] | 956 | SemanticError( tyDecl->location, "Cannot redefine typedef: " + tyDecl->name ); |
---|
[f53836b] | 957 | } |
---|
[4b97770] | 958 | // Cannot redefine VLA typedefs. Note: this is slightly incorrect, because our notion of VLAs |
---|
| 959 | // at this point in the translator is imprecise. In particular, this will disallow redefining typedefs |
---|
| 960 | // with arrays whose dimension is an enumerator or a cast of a constant/enumerator. The effort required |
---|
| 961 | // to fix this corner case likely outweighs the utility of allowing it. |
---|
[f53836b] | 962 | if ( isVariableLength( t1 ) || isVariableLength( t2 ) ) { |
---|
[a16764a6] | 963 | SemanticError( tyDecl->location, "Cannot redefine typedef: " + tyDecl->name ); |
---|
[85c4ef0] | 964 | } |
---|
[cc79d97] | 965 | } else { |
---|
[0b3b2ae] | 966 | typedefNames[ tyDecl->name ] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel ); |
---|
[cc79d97] | 967 | } // if |
---|
| 968 | |
---|
[0dd3a2f] | 969 | // When a typedef is a forward declaration: |
---|
| 970 | // typedef struct screen SCREEN; |
---|
| 971 | // the declaration portion must be retained: |
---|
| 972 | // struct screen; |
---|
| 973 | // because the expansion of the typedef is: |
---|
| 974 | // void rtn( SCREEN *p ) => void rtn( struct screen *p ) |
---|
| 975 | // hence the type-name "screen" must be defined. |
---|
| 976 | // Note, qualifiers on the typedef are superfluous for the forward declaration. |
---|
[6f95000] | 977 | |
---|
[0b3b2ae] | 978 | Type *designatorType = tyDecl->base->stripDeclarator(); |
---|
[6f95000] | 979 | if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( designatorType ) ) { |
---|
[48ed81c] | 980 | declsToAddBefore.push_back( new StructDecl( aggDecl->name, DeclarationNode::Struct, noAttributes, tyDecl->linkage ) ); |
---|
[6f95000] | 981 | } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( designatorType ) ) { |
---|
[48ed81c] | 982 | declsToAddBefore.push_back( new UnionDecl( aggDecl->name, noAttributes, tyDecl->linkage ) ); |
---|
[6f95000] | 983 | } else if ( EnumInstType *enumDecl = dynamic_cast< EnumInstType * >( designatorType ) ) { |
---|
[48ed81c] | 984 | declsToAddBefore.push_back( new EnumDecl( enumDecl->name, noAttributes, tyDecl->linkage ) ); |
---|
[0dd3a2f] | 985 | } // if |
---|
[48ed81c] | 986 | return tyDecl->clone(); |
---|
[a08ba92] | 987 | } |
---|
[c8ffe20b] | 988 | |
---|
[48ed81c] | 989 | void ReplaceTypedef::premutate( TypeDecl * typeDecl ) { |
---|
[0b3b2ae] | 990 | TypedefMap::iterator i = typedefNames.find( typeDecl->name ); |
---|
[0dd3a2f] | 991 | if ( i != typedefNames.end() ) { |
---|
| 992 | typedefNames.erase( i ) ; |
---|
| 993 | } // if |
---|
[679864e1] | 994 | |
---|
[0bcc2b7] | 995 | typedeclNames.insert( typeDecl->name, typeDecl ); |
---|
[a08ba92] | 996 | } |
---|
[c8ffe20b] | 997 | |
---|
[48ed81c] | 998 | void ReplaceTypedef::premutate( FunctionDecl * ) { |
---|
[a506df4] | 999 | GuardScope( typedefNames ); |
---|
[0bcc2b7] | 1000 | GuardScope( typedeclNames ); |
---|
[a08ba92] | 1001 | } |
---|
[c8ffe20b] | 1002 | |
---|
[48ed81c] | 1003 | void ReplaceTypedef::premutate( ObjectDecl * ) { |
---|
[a506df4] | 1004 | GuardScope( typedefNames ); |
---|
[0bcc2b7] | 1005 | GuardScope( typedeclNames ); |
---|
[a506df4] | 1006 | } |
---|
[dd020c0] | 1007 | |
---|
[48ed81c] | 1008 | DeclarationWithType * ReplaceTypedef::postmutate( ObjectDecl * objDecl ) { |
---|
[0b3b2ae] | 1009 | if ( FunctionType *funtype = dynamic_cast<FunctionType *>( objDecl->type ) ) { // function type? |
---|
[02e5ab6] | 1010 | // replace the current object declaration with a function declaration |
---|
[0b3b2ae] | 1011 | FunctionDecl * newDecl = new FunctionDecl( objDecl->name, objDecl->get_storageClasses(), objDecl->linkage, funtype, 0, objDecl->attributes, objDecl->get_funcSpec() ); |
---|
| 1012 | objDecl->attributes.clear(); |
---|
[dbe8f244] | 1013 | objDecl->set_type( nullptr ); |
---|
[0a86a30] | 1014 | delete objDecl; |
---|
| 1015 | return newDecl; |
---|
[1db21619] | 1016 | } // if |
---|
[a506df4] | 1017 | return objDecl; |
---|
[a08ba92] | 1018 | } |
---|
[c8ffe20b] | 1019 | |
---|
[48ed81c] | 1020 | void ReplaceTypedef::premutate( CastExpr * ) { |
---|
[a506df4] | 1021 | GuardScope( typedefNames ); |
---|
[0bcc2b7] | 1022 | GuardScope( typedeclNames ); |
---|
[a08ba92] | 1023 | } |
---|
[c8ffe20b] | 1024 | |
---|
[48ed81c] | 1025 | void ReplaceTypedef::premutate( CompoundStmt * ) { |
---|
[a506df4] | 1026 | GuardScope( typedefNames ); |
---|
[0bcc2b7] | 1027 | GuardScope( typedeclNames ); |
---|
[cc79d97] | 1028 | scopeLevel += 1; |
---|
[a506df4] | 1029 | GuardAction( [this](){ scopeLevel -= 1; } ); |
---|
| 1030 | } |
---|
| 1031 | |
---|
[45161b4d] | 1032 | template<typename AggDecl> |
---|
[48ed81c] | 1033 | void ReplaceTypedef::addImplicitTypedef( AggDecl * aggDecl ) { |
---|
[45161b4d] | 1034 | if ( typedefNames.count( aggDecl->get_name() ) == 0 ) { |
---|
[62e5546] | 1035 | Type *type = nullptr; |
---|
[45161b4d] | 1036 | if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( aggDecl ) ) { |
---|
| 1037 | type = new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ); |
---|
| 1038 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( aggDecl ) ) { |
---|
| 1039 | type = new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ); |
---|
| 1040 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( aggDecl ) ) { |
---|
| 1041 | type = new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ); |
---|
| 1042 | } // if |
---|
[0b0f1dd] | 1043 | TypedefDeclPtr tyDecl( new TypedefDecl( aggDecl->get_name(), aggDecl->location, Type::StorageClasses(), type, aggDecl->get_linkage() ) ); |
---|
[46f6134] | 1044 | typedefNames[ aggDecl->get_name() ] = std::make_pair( std::move( tyDecl ), scopeLevel ); |
---|
[48ed81c] | 1045 | // add the implicit typedef to the AST |
---|
| 1046 | declsToAddBefore.push_back( new TypedefDecl( aggDecl->get_name(), aggDecl->location, Type::StorageClasses(), type->clone(), aggDecl->get_linkage() ) ); |
---|
[45161b4d] | 1047 | } // if |
---|
| 1048 | } |
---|
[4e06c1e] | 1049 | |
---|
[48ed81c] | 1050 | template< typename AggDecl > |
---|
| 1051 | void ReplaceTypedef::handleAggregate( AggDecl * aggr ) { |
---|
| 1052 | SemanticErrorException errors; |
---|
[a506df4] | 1053 | |
---|
[48ed81c] | 1054 | ValueGuard< std::list<Declaration * > > oldBeforeDecls( declsToAddBefore ); |
---|
| 1055 | ValueGuard< std::list<Declaration * > > oldAfterDecls ( declsToAddAfter ); |
---|
| 1056 | declsToAddBefore.clear(); |
---|
| 1057 | declsToAddAfter.clear(); |
---|
[a506df4] | 1058 | |
---|
[48ed81c] | 1059 | GuardScope( typedefNames ); |
---|
[0bcc2b7] | 1060 | GuardScope( typedeclNames ); |
---|
[48ed81c] | 1061 | mutateAll( aggr->parameters, *visitor ); |
---|
[85c4ef0] | 1062 | |
---|
[48ed81c] | 1063 | // unroll mutateAll for aggr->members so that implicit typedefs for nested types are added to the aggregate body. |
---|
| 1064 | for ( std::list< Declaration * >::iterator i = aggr->members.begin(); i != aggr->members.end(); ++i ) { |
---|
| 1065 | if ( !declsToAddAfter.empty() ) { aggr->members.splice( i, declsToAddAfter ); } |
---|
[a506df4] | 1066 | |
---|
[48ed81c] | 1067 | try { |
---|
| 1068 | *i = maybeMutate( *i, *visitor ); |
---|
| 1069 | } catch ( SemanticErrorException &e ) { |
---|
| 1070 | errors.append( e ); |
---|
| 1071 | } |
---|
| 1072 | |
---|
| 1073 | if ( !declsToAddBefore.empty() ) { aggr->members.splice( i, declsToAddBefore ); } |
---|
| 1074 | } |
---|
| 1075 | |
---|
| 1076 | if ( !declsToAddAfter.empty() ) { aggr->members.splice( aggr->members.end(), declsToAddAfter ); } |
---|
| 1077 | if ( !errors.isEmpty() ) { throw errors; } |
---|
[85c4ef0] | 1078 | } |
---|
| 1079 | |
---|
[48ed81c] | 1080 | void ReplaceTypedef::premutate( StructDecl * structDecl ) { |
---|
| 1081 | visit_children = false; |
---|
| 1082 | addImplicitTypedef( structDecl ); |
---|
| 1083 | handleAggregate( structDecl ); |
---|
[a506df4] | 1084 | } |
---|
| 1085 | |
---|
[48ed81c] | 1086 | void ReplaceTypedef::premutate( UnionDecl * unionDecl ) { |
---|
| 1087 | visit_children = false; |
---|
| 1088 | addImplicitTypedef( unionDecl ); |
---|
| 1089 | handleAggregate( unionDecl ); |
---|
[85c4ef0] | 1090 | } |
---|
| 1091 | |
---|
[48ed81c] | 1092 | void ReplaceTypedef::premutate( EnumDecl * enumDecl ) { |
---|
| 1093 | addImplicitTypedef( enumDecl ); |
---|
[85c4ef0] | 1094 | } |
---|
| 1095 | |
---|
[48ed81c] | 1096 | void ReplaceTypedef::premutate( FunctionType * ) { |
---|
[1f370451] | 1097 | GuardValue( inFunctionType ); |
---|
| 1098 | inFunctionType = true; |
---|
| 1099 | } |
---|
| 1100 | |
---|
[0bcc2b7] | 1101 | void ReplaceTypedef::premutate( TraitDecl * ) { |
---|
| 1102 | GuardScope( typedefNames ); |
---|
| 1103 | GuardScope( typedeclNames); |
---|
| 1104 | } |
---|
| 1105 | |
---|
[d1969a6] | 1106 | void VerifyCtorDtorAssign::verify( std::list< Declaration * > & translationUnit ) { |
---|
[0db6fc0] | 1107 | PassVisitor<VerifyCtorDtorAssign> verifier; |
---|
[9cb8e88d] | 1108 | acceptAll( translationUnit, verifier ); |
---|
| 1109 | } |
---|
| 1110 | |
---|
[0db6fc0] | 1111 | void VerifyCtorDtorAssign::previsit( FunctionDecl * funcDecl ) { |
---|
[9cb8e88d] | 1112 | FunctionType * funcType = funcDecl->get_functionType(); |
---|
| 1113 | std::list< DeclarationWithType * > &returnVals = funcType->get_returnVals(); |
---|
| 1114 | std::list< DeclarationWithType * > ¶ms = funcType->get_parameters(); |
---|
| 1115 | |
---|
[bff227f] | 1116 | if ( CodeGen::isCtorDtorAssign( funcDecl->get_name() ) ) { // TODO: also check /=, etc. |
---|
[9cb8e88d] | 1117 | if ( params.size() == 0 ) { |
---|
[a16764a6] | 1118 | SemanticError( funcDecl, "Constructors, destructors, and assignment functions require at least one parameter " ); |
---|
[9cb8e88d] | 1119 | } |
---|
[ce8c12f] | 1120 | ReferenceType * refType = dynamic_cast< ReferenceType * >( params.front()->get_type() ); |
---|
[084fecc] | 1121 | if ( ! refType ) { |
---|
[a16764a6] | 1122 | SemanticError( funcDecl, "First parameter of a constructor, destructor, or assignment function must be a reference " ); |
---|
[9cb8e88d] | 1123 | } |
---|
[bff227f] | 1124 | if ( CodeGen::isCtorDtor( funcDecl->get_name() ) && returnVals.size() != 0 ) { |
---|
[a16764a6] | 1125 | SemanticError( funcDecl, "Constructors and destructors cannot have explicit return values " ); |
---|
[9cb8e88d] | 1126 | } |
---|
| 1127 | } |
---|
| 1128 | } |
---|
[70a06f6] | 1129 | |
---|
[11ab8ea8] | 1130 | template< typename Aggr > |
---|
| 1131 | void validateGeneric( Aggr * inst ) { |
---|
| 1132 | std::list< TypeDecl * > * params = inst->get_baseParameters(); |
---|
[30f9072] | 1133 | if ( params ) { |
---|
[11ab8ea8] | 1134 | std::list< Expression * > & args = inst->get_parameters(); |
---|
[67cf18c] | 1135 | |
---|
| 1136 | // insert defaults arguments when a type argument is missing (currently only supports missing arguments at the end of the list). |
---|
| 1137 | // A substitution is used to ensure that defaults are replaced correctly, e.g., |
---|
| 1138 | // forall(otype T, otype alloc = heap_allocator(T)) struct vector; |
---|
| 1139 | // vector(int) v; |
---|
| 1140 | // after insertion of default values becomes |
---|
| 1141 | // vector(int, heap_allocator(T)) |
---|
| 1142 | // and the substitution is built with T=int so that after substitution, the result is |
---|
| 1143 | // vector(int, heap_allocator(int)) |
---|
| 1144 | TypeSubstitution sub; |
---|
| 1145 | auto paramIter = params->begin(); |
---|
| 1146 | for ( size_t i = 0; paramIter != params->end(); ++paramIter, ++i ) { |
---|
| 1147 | if ( i < args.size() ) { |
---|
[e3e16bc] | 1148 | TypeExpr * expr = strict_dynamic_cast< TypeExpr * >( *std::next( args.begin(), i ) ); |
---|
[67cf18c] | 1149 | sub.add( (*paramIter)->get_name(), expr->get_type()->clone() ); |
---|
| 1150 | } else if ( i == args.size() ) { |
---|
| 1151 | Type * defaultType = (*paramIter)->get_init(); |
---|
| 1152 | if ( defaultType ) { |
---|
| 1153 | args.push_back( new TypeExpr( defaultType->clone() ) ); |
---|
| 1154 | sub.add( (*paramIter)->get_name(), defaultType->clone() ); |
---|
| 1155 | } |
---|
| 1156 | } |
---|
| 1157 | } |
---|
| 1158 | |
---|
| 1159 | sub.apply( inst ); |
---|
[a16764a6] | 1160 | if ( args.size() < params->size() ) SemanticError( inst, "Too few type arguments in generic type " ); |
---|
| 1161 | if ( args.size() > params->size() ) SemanticError( inst, "Too many type arguments in generic type " ); |
---|
[11ab8ea8] | 1162 | } |
---|
| 1163 | } |
---|
| 1164 | |
---|
[0db6fc0] | 1165 | void ValidateGenericParameters::previsit( StructInstType * inst ) { |
---|
[11ab8ea8] | 1166 | validateGeneric( inst ); |
---|
| 1167 | } |
---|
[9cb8e88d] | 1168 | |
---|
[0db6fc0] | 1169 | void ValidateGenericParameters::previsit( UnionInstType * inst ) { |
---|
[11ab8ea8] | 1170 | validateGeneric( inst ); |
---|
[9cb8e88d] | 1171 | } |
---|
[70a06f6] | 1172 | |
---|
[d24d4e1] | 1173 | void CompoundLiteral::premutate( ObjectDecl *objectDecl ) { |
---|
[a7c90d4] | 1174 | storageClasses = objectDecl->get_storageClasses(); |
---|
[630a82a] | 1175 | } |
---|
| 1176 | |
---|
[d24d4e1] | 1177 | Expression *CompoundLiteral::postmutate( CompoundLiteralExpr *compLitExpr ) { |
---|
[630a82a] | 1178 | // transform [storage_class] ... (struct S){ 3, ... }; |
---|
| 1179 | // into [storage_class] struct S temp = { 3, ... }; |
---|
| 1180 | static UniqueName indexName( "_compLit" ); |
---|
| 1181 | |
---|
[d24d4e1] | 1182 | ObjectDecl *tempvar = new ObjectDecl( indexName.newName(), storageClasses, LinkageSpec::C, nullptr, compLitExpr->get_result(), compLitExpr->get_initializer() ); |
---|
| 1183 | compLitExpr->set_result( nullptr ); |
---|
| 1184 | compLitExpr->set_initializer( nullptr ); |
---|
[630a82a] | 1185 | delete compLitExpr; |
---|
[d24d4e1] | 1186 | declsToAddBefore.push_back( tempvar ); // add modified temporary to current block |
---|
| 1187 | return new VariableExpr( tempvar ); |
---|
[630a82a] | 1188 | } |
---|
[cce9429] | 1189 | |
---|
| 1190 | void ReturnTypeFixer::fix( std::list< Declaration * > &translationUnit ) { |
---|
[0db6fc0] | 1191 | PassVisitor<ReturnTypeFixer> fixer; |
---|
[cce9429] | 1192 | acceptAll( translationUnit, fixer ); |
---|
| 1193 | } |
---|
| 1194 | |
---|
[0db6fc0] | 1195 | void ReturnTypeFixer::postvisit( FunctionDecl * functionDecl ) { |
---|
[9facf3b] | 1196 | FunctionType * ftype = functionDecl->get_functionType(); |
---|
| 1197 | std::list< DeclarationWithType * > & retVals = ftype->get_returnVals(); |
---|
[56e49b0] | 1198 | assertf( retVals.size() == 0 || retVals.size() == 1, "Function %s has too many return values: %zu", functionDecl->get_name().c_str(), retVals.size() ); |
---|
[9facf3b] | 1199 | if ( retVals.size() == 1 ) { |
---|
[861799c] | 1200 | // 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). |
---|
| 1201 | // ensure other return values have a name. |
---|
[9facf3b] | 1202 | DeclarationWithType * ret = retVals.front(); |
---|
| 1203 | if ( ret->get_name() == "" ) { |
---|
| 1204 | ret->set_name( toString( "_retval_", CodeGen::genName( functionDecl ) ) ); |
---|
| 1205 | } |
---|
[c6d2e93] | 1206 | ret->get_attributes().push_back( new Attribute( "unused" ) ); |
---|
[9facf3b] | 1207 | } |
---|
| 1208 | } |
---|
[cce9429] | 1209 | |
---|
[0db6fc0] | 1210 | void ReturnTypeFixer::postvisit( FunctionType * ftype ) { |
---|
[cce9429] | 1211 | // xxx - need to handle named return values - this information needs to be saved somehow |
---|
| 1212 | // so that resolution has access to the names. |
---|
| 1213 | // Note that this pass needs to happen early so that other passes which look for tuple types |
---|
| 1214 | // find them in all of the right places, including function return types. |
---|
| 1215 | std::list< DeclarationWithType * > & retVals = ftype->get_returnVals(); |
---|
| 1216 | if ( retVals.size() > 1 ) { |
---|
| 1217 | // generate a single return parameter which is the tuple of all of the return values |
---|
[e3e16bc] | 1218 | TupleType * tupleType = strict_dynamic_cast< TupleType * >( ResolvExpr::extractResultType( ftype ) ); |
---|
[cce9429] | 1219 | // ensure return value is not destructed by explicitly creating an empty ListInit node wherein maybeConstruct is false. |
---|
[68fe077a] | 1220 | ObjectDecl * newRet = new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, tupleType, new ListInit( std::list<Initializer*>(), noDesignators, false ) ); |
---|
[cce9429] | 1221 | deleteAll( retVals ); |
---|
| 1222 | retVals.clear(); |
---|
| 1223 | retVals.push_back( newRet ); |
---|
| 1224 | } |
---|
| 1225 | } |
---|
[fbd7ad6] | 1226 | |
---|
| 1227 | void ArrayLength::computeLength( std::list< Declaration * > & translationUnit ) { |
---|
[0db6fc0] | 1228 | PassVisitor<ArrayLength> len; |
---|
[fbd7ad6] | 1229 | acceptAll( translationUnit, len ); |
---|
| 1230 | } |
---|
| 1231 | |
---|
[0db6fc0] | 1232 | void ArrayLength::previsit( ObjectDecl * objDecl ) { |
---|
[0b3b2ae] | 1233 | if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->type ) ) { |
---|
[30f9072] | 1234 | if ( at->get_dimension() ) return; |
---|
[0b3b2ae] | 1235 | if ( ListInit * init = dynamic_cast< ListInit * >( objDecl->init ) ) { |
---|
| 1236 | at->set_dimension( new ConstantExpr( Constant::from_ulong( init->initializers.size() ) ) ); |
---|
[fbd7ad6] | 1237 | } |
---|
| 1238 | } |
---|
| 1239 | } |
---|
[4fbdfae0] | 1240 | |
---|
[5809461] | 1241 | struct LabelFinder { |
---|
| 1242 | std::set< Label > & labels; |
---|
| 1243 | LabelFinder( std::set< Label > & labels ) : labels( labels ) {} |
---|
| 1244 | void previsit( Statement * stmt ) { |
---|
| 1245 | for ( Label & l : stmt->labels ) { |
---|
| 1246 | labels.insert( l ); |
---|
| 1247 | } |
---|
| 1248 | } |
---|
| 1249 | }; |
---|
| 1250 | |
---|
| 1251 | void LabelAddressFixer::premutate( FunctionDecl * funcDecl ) { |
---|
| 1252 | GuardValue( labels ); |
---|
| 1253 | PassVisitor<LabelFinder> finder( labels ); |
---|
| 1254 | funcDecl->accept( finder ); |
---|
| 1255 | } |
---|
| 1256 | |
---|
| 1257 | Expression * LabelAddressFixer::postmutate( AddressExpr * addrExpr ) { |
---|
| 1258 | // convert &&label into label address |
---|
| 1259 | if ( AddressExpr * inner = dynamic_cast< AddressExpr * >( addrExpr->arg ) ) { |
---|
| 1260 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( inner->arg ) ) { |
---|
| 1261 | if ( labels.count( nameExpr->name ) ) { |
---|
| 1262 | Label name = nameExpr->name; |
---|
| 1263 | delete addrExpr; |
---|
| 1264 | return new LabelAddressExpr( name ); |
---|
| 1265 | } |
---|
| 1266 | } |
---|
| 1267 | } |
---|
| 1268 | return addrExpr; |
---|
| 1269 | } |
---|
| 1270 | |
---|
[4fbdfae0] | 1271 | void FindSpecialDeclarations::previsit( FunctionDecl * funcDecl ) { |
---|
| 1272 | if ( ! dereferenceOperator ) { |
---|
| 1273 | if ( funcDecl->get_name() == "*?" && funcDecl->get_linkage() == LinkageSpec::Intrinsic ) { |
---|
| 1274 | FunctionType * ftype = funcDecl->get_functionType(); |
---|
| 1275 | if ( ftype->get_parameters().size() == 1 && ftype->get_parameters().front()->get_type()->get_qualifiers() == Type::Qualifiers() ) { |
---|
| 1276 | dereferenceOperator = funcDecl; |
---|
| 1277 | } |
---|
| 1278 | } |
---|
| 1279 | } |
---|
| 1280 | } |
---|
[51b7345] | 1281 | } // namespace SymTab |
---|
[0dd3a2f] | 1282 | |
---|
| 1283 | // Local Variables: // |
---|
| 1284 | // tab-width: 4 // |
---|
| 1285 | // mode: c++ // |
---|
| 1286 | // compile-command: "make install" // |
---|
| 1287 | // End: // |
---|