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