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