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