[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 |
---|
[5dcb881] | 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Fri Nov 12 11:00:00 2021 |
---|
| 13 | // Update Count : 364 |
---|
[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 |
---|
[18e683b] | 46 | #include <unordered_map> // for unordered_map |
---|
[d180746] | 47 | #include <utility> // for pair |
---|
[30f9072] | 48 | |
---|
[18e683b] | 49 | #include "AST/Chain.hpp" |
---|
[c1ed2ee] | 50 | #include "AST/Decl.hpp" |
---|
| 51 | #include "AST/Node.hpp" |
---|
| 52 | #include "AST/Pass.hpp" |
---|
| 53 | #include "AST/SymbolTable.hpp" |
---|
| 54 | #include "AST/Type.hpp" |
---|
[c1398e4] | 55 | #include "AST/TypeSubstitution.hpp" |
---|
[30f9072] | 56 | #include "CodeGen/CodeGenerator.h" // for genName |
---|
[9236060] | 57 | #include "CodeGen/OperatorTable.h" // for isCtorDtor, isCtorDtorAssign |
---|
[25fcb84] | 58 | #include "ControlStruct/Mutate.h" // for ForExprMutator |
---|
[18e683b] | 59 | #include "Common/CodeLocation.h" // for CodeLocation |
---|
[7abee38] | 60 | #include "Common/Stats.h" // for Stats::Heap |
---|
[30f9072] | 61 | #include "Common/PassVisitor.h" // for PassVisitor, WithDeclsToAdd |
---|
[d180746] | 62 | #include "Common/ScopedMap.h" // for ScopedMap |
---|
[30f9072] | 63 | #include "Common/SemanticError.h" // for SemanticError |
---|
| 64 | #include "Common/UniqueName.h" // for UniqueName |
---|
| 65 | #include "Common/utility.h" // for operator+, cloneAll, deleteAll |
---|
[16ba4a6f] | 66 | #include "CompilationState.h" // skip some passes in new-ast build |
---|
[be9288a] | 67 | #include "Concurrency/Keywords.h" // for applyKeywords |
---|
[30f9072] | 68 | #include "FixFunction.h" // for FixFunction |
---|
| 69 | #include "Indexer.h" // for Indexer |
---|
[8b11840] | 70 | #include "InitTweak/GenInit.h" // for fixReturnStatements |
---|
[d180746] | 71 | #include "InitTweak/InitTweak.h" // for isCtorDtorAssign |
---|
| 72 | #include "ResolvExpr/typeops.h" // for typesCompatible |
---|
[4934ea3] | 73 | #include "ResolvExpr/Resolver.h" // for findSingleExpression |
---|
[2b79a70] | 74 | #include "ResolvExpr/ResolveTypeof.h" // for resolveTypeof |
---|
[be9288a] | 75 | #include "SymTab/Autogen.h" // for SizeType |
---|
[07de76b] | 76 | #include "SynTree/LinkageSpec.h" // for C |
---|
[be9288a] | 77 | #include "SynTree/Attribute.h" // for noAttributes, Attribute |
---|
[30f9072] | 78 | #include "SynTree/Constant.h" // for Constant |
---|
[d180746] | 79 | #include "SynTree/Declaration.h" // for ObjectDecl, DeclarationWithType |
---|
| 80 | #include "SynTree/Expression.h" // for CompoundLiteralExpr, Expressio... |
---|
| 81 | #include "SynTree/Initializer.h" // for ListInit, Initializer |
---|
| 82 | #include "SynTree/Label.h" // for operator==, Label |
---|
| 83 | #include "SynTree/Mutator.h" // for Mutator |
---|
| 84 | #include "SynTree/Type.h" // for Type, TypeInstType, EnumInstType |
---|
| 85 | #include "SynTree/TypeSubstitution.h" // for TypeSubstitution |
---|
| 86 | #include "SynTree/Visitor.h" // for Visitor |
---|
[fd2debf] | 87 | #include "Validate/HandleAttributes.h" // for handleAttributes |
---|
[2bfc6b2] | 88 | #include "Validate/FindSpecialDecls.h" // for FindSpecialDecls |
---|
[d180746] | 89 | |
---|
| 90 | class CompoundStmt; |
---|
| 91 | class ReturnStmt; |
---|
| 92 | class SwitchStmt; |
---|
[51b7345] | 93 | |
---|
[b16923d] | 94 | #define debugPrint( x ) if ( doDebug ) x |
---|
[51b7345] | 95 | |
---|
| 96 | namespace SymTab { |
---|
[15f5c5e] | 97 | /// hoists declarations that are difficult to hoist while parsing |
---|
| 98 | struct HoistTypeDecls final : public WithDeclsToAdd { |
---|
[29f9e20] | 99 | void previsit( SizeofExpr * ); |
---|
| 100 | void previsit( AlignofExpr * ); |
---|
| 101 | void previsit( UntypedOffsetofExpr * ); |
---|
[95d09bdb] | 102 | void previsit( CompoundLiteralExpr * ); |
---|
[29f9e20] | 103 | void handleType( Type * ); |
---|
| 104 | }; |
---|
| 105 | |
---|
[a12c81f3] | 106 | struct FixQualifiedTypes final : public WithIndexer { |
---|
[6e50a6b] | 107 | FixQualifiedTypes() : WithIndexer(false) {} |
---|
[a12c81f3] | 108 | Type * postmutate( QualifiedType * ); |
---|
| 109 | }; |
---|
| 110 | |
---|
[a09e45b] | 111 | struct HoistStruct final : public WithDeclsToAdd, public WithGuards { |
---|
[82dd287] | 112 | /// Flattens nested struct types |
---|
[0dd3a2f] | 113 | static void hoistStruct( std::list< Declaration * > &translationUnit ); |
---|
[9cb8e88d] | 114 | |
---|
[a09e45b] | 115 | void previsit( StructDecl * aggregateDecl ); |
---|
| 116 | void previsit( UnionDecl * aggregateDecl ); |
---|
[0f40912] | 117 | void previsit( StaticAssertDecl * assertDecl ); |
---|
[d419d8e] | 118 | void previsit( StructInstType * type ); |
---|
| 119 | void previsit( UnionInstType * type ); |
---|
| 120 | void previsit( EnumInstType * type ); |
---|
[9cb8e88d] | 121 | |
---|
[a08ba92] | 122 | private: |
---|
[ef5b828] | 123 | template< typename AggDecl > void handleAggregate( AggDecl * aggregateDecl ); |
---|
[c8ffe20b] | 124 | |
---|
[bdad6eb7] | 125 | AggregateDecl * parentAggr = nullptr; |
---|
[a08ba92] | 126 | }; |
---|
[c8ffe20b] | 127 | |
---|
[cce9429] | 128 | /// Fix return types so that every function returns exactly one value |
---|
[d24d4e1] | 129 | struct ReturnTypeFixer { |
---|
[cce9429] | 130 | static void fix( std::list< Declaration * > &translationUnit ); |
---|
| 131 | |
---|
[0db6fc0] | 132 | void postvisit( FunctionDecl * functionDecl ); |
---|
| 133 | void postvisit( FunctionType * ftype ); |
---|
[cce9429] | 134 | }; |
---|
| 135 | |
---|
[de91427b] | 136 | /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers. |
---|
[c1ed2ee] | 137 | struct EnumAndPointerDecay_old { |
---|
[ef5b828] | 138 | void previsit( EnumDecl * aggregateDecl ); |
---|
| 139 | void previsit( FunctionType * func ); |
---|
[a08ba92] | 140 | }; |
---|
[82dd287] | 141 | |
---|
| 142 | /// Associates forward declarations of aggregates with their definitions |
---|
[c1ed2ee] | 143 | struct LinkReferenceToTypes_old final : public WithIndexer, public WithGuards, public WithVisitorRef<LinkReferenceToTypes_old>, public WithShortCircuiting { |
---|
[ef5b828] | 144 | LinkReferenceToTypes_old( const Indexer * indexer ); |
---|
| 145 | void postvisit( TypeInstType * typeInst ); |
---|
[be9036d] | 146 | |
---|
[ef5b828] | 147 | void postvisit( EnumInstType * enumInst ); |
---|
| 148 | void postvisit( StructInstType * structInst ); |
---|
| 149 | void postvisit( UnionInstType * unionInst ); |
---|
| 150 | void postvisit( TraitInstType * traitInst ); |
---|
[afcb0a3] | 151 | void previsit( QualifiedType * qualType ); |
---|
| 152 | void postvisit( QualifiedType * qualType ); |
---|
[be9036d] | 153 | |
---|
[ef5b828] | 154 | void postvisit( EnumDecl * enumDecl ); |
---|
| 155 | void postvisit( StructDecl * structDecl ); |
---|
| 156 | void postvisit( UnionDecl * unionDecl ); |
---|
[522363e] | 157 | void postvisit( TraitDecl * traitDecl ); |
---|
[be9036d] | 158 | |
---|
[ef5b828] | 159 | void previsit( StructDecl * structDecl ); |
---|
| 160 | void previsit( UnionDecl * unionDecl ); |
---|
[b95fe40] | 161 | |
---|
| 162 | void renameGenericParams( std::list< TypeDecl * > & params ); |
---|
| 163 | |
---|
[06edda0] | 164 | private: |
---|
[ef5b828] | 165 | const Indexer * local_indexer; |
---|
[9cb8e88d] | 166 | |
---|
[c0aa336] | 167 | typedef std::map< std::string, std::list< EnumInstType * > > ForwardEnumsType; |
---|
[0dd3a2f] | 168 | typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType; |
---|
| 169 | typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType; |
---|
[c0aa336] | 170 | ForwardEnumsType forwardEnums; |
---|
[0dd3a2f] | 171 | ForwardStructsType forwardStructs; |
---|
| 172 | ForwardUnionsType forwardUnions; |
---|
[b95fe40] | 173 | /// true if currently in a generic type body, so that type parameter instances can be renamed appropriately |
---|
| 174 | bool inGeneric = false; |
---|
[a08ba92] | 175 | }; |
---|
[c8ffe20b] | 176 | |
---|
[6e50a6b] | 177 | /// Does early resolution on the expressions that give enumeration constants their values |
---|
| 178 | struct ResolveEnumInitializers final : public WithIndexer, public WithGuards, public WithVisitorRef<ResolveEnumInitializers>, public WithShortCircuiting { |
---|
| 179 | ResolveEnumInitializers( const Indexer * indexer ); |
---|
| 180 | void postvisit( EnumDecl * enumDecl ); |
---|
| 181 | |
---|
| 182 | private: |
---|
| 183 | const Indexer * local_indexer; |
---|
| 184 | |
---|
| 185 | }; |
---|
| 186 | |
---|
[06edda0] | 187 | /// Replaces array and function types in forall lists by appropriate pointer type and assigns each Object and Function declaration a unique ID. |
---|
[c1ed2ee] | 188 | struct ForallPointerDecay_old final { |
---|
[8b11840] | 189 | void previsit( ObjectDecl * object ); |
---|
| 190 | void previsit( FunctionDecl * func ); |
---|
[bbf3fda] | 191 | void previsit( FunctionType * ftype ); |
---|
[bd7e609] | 192 | void previsit( StructDecl * aggrDecl ); |
---|
| 193 | void previsit( UnionDecl * aggrDecl ); |
---|
[a08ba92] | 194 | }; |
---|
[c8ffe20b] | 195 | |
---|
[9490621] | 196 | // These structs are the sub-sub-passes of ForallPointerDecay_old. |
---|
| 197 | |
---|
| 198 | struct TraitExpander_old final { |
---|
| 199 | void previsit( FunctionType * ); |
---|
| 200 | void previsit( StructDecl * ); |
---|
| 201 | void previsit( UnionDecl * ); |
---|
| 202 | }; |
---|
| 203 | |
---|
| 204 | struct AssertionFixer_old final { |
---|
| 205 | void previsit( FunctionType * ); |
---|
| 206 | void previsit( StructDecl * ); |
---|
| 207 | void previsit( UnionDecl * ); |
---|
| 208 | }; |
---|
| 209 | |
---|
| 210 | struct CheckOperatorTypes_old final { |
---|
| 211 | void previsit( ObjectDecl * ); |
---|
| 212 | }; |
---|
| 213 | |
---|
| 214 | struct FixUniqueIds_old final { |
---|
| 215 | void previsit( DeclarationWithType * ); |
---|
| 216 | }; |
---|
| 217 | |
---|
[d24d4e1] | 218 | struct ReturnChecker : public WithGuards { |
---|
[de91427b] | 219 | /// Checks that return statements return nothing if their return type is void |
---|
| 220 | /// and return something if the return type is non-void. |
---|
| 221 | static void checkFunctionReturns( std::list< Declaration * > & translationUnit ); |
---|
| 222 | |
---|
[0db6fc0] | 223 | void previsit( FunctionDecl * functionDecl ); |
---|
| 224 | void previsit( ReturnStmt * returnStmt ); |
---|
[de91427b] | 225 | |
---|
[0db6fc0] | 226 | typedef std::list< DeclarationWithType * > ReturnVals; |
---|
| 227 | ReturnVals returnVals; |
---|
[de91427b] | 228 | }; |
---|
| 229 | |
---|
[48ed81c] | 230 | struct ReplaceTypedef final : public WithVisitorRef<ReplaceTypedef>, public WithGuards, public WithShortCircuiting, public WithDeclsToAdd { |
---|
| 231 | ReplaceTypedef() : scopeLevel( 0 ) {} |
---|
[de91427b] | 232 | /// Replaces typedefs by forward declarations |
---|
[48ed81c] | 233 | static void replaceTypedef( std::list< Declaration * > &translationUnit ); |
---|
[85c4ef0] | 234 | |
---|
[48ed81c] | 235 | void premutate( QualifiedType * ); |
---|
| 236 | Type * postmutate( QualifiedType * qualType ); |
---|
[a506df4] | 237 | Type * postmutate( TypeInstType * aggregateUseType ); |
---|
| 238 | Declaration * postmutate( TypedefDecl * typeDecl ); |
---|
| 239 | void premutate( TypeDecl * typeDecl ); |
---|
| 240 | void premutate( FunctionDecl * funcDecl ); |
---|
| 241 | void premutate( ObjectDecl * objDecl ); |
---|
| 242 | DeclarationWithType * postmutate( ObjectDecl * objDecl ); |
---|
| 243 | |
---|
| 244 | void premutate( CastExpr * castExpr ); |
---|
| 245 | |
---|
| 246 | void premutate( CompoundStmt * compoundStmt ); |
---|
| 247 | |
---|
| 248 | void premutate( StructDecl * structDecl ); |
---|
| 249 | void premutate( UnionDecl * unionDecl ); |
---|
| 250 | void premutate( EnumDecl * enumDecl ); |
---|
[0bcc2b7] | 251 | void premutate( TraitDecl * ); |
---|
[a506df4] | 252 | |
---|
[1f370451] | 253 | void premutate( FunctionType * ftype ); |
---|
| 254 | |
---|
[a506df4] | 255 | private: |
---|
[45161b4d] | 256 | template<typename AggDecl> |
---|
| 257 | void addImplicitTypedef( AggDecl * aggDecl ); |
---|
[48ed81c] | 258 | template< typename AggDecl > |
---|
| 259 | void handleAggregate( AggDecl * aggr ); |
---|
[70a06f6] | 260 | |
---|
[46f6134] | 261 | typedef std::unique_ptr<TypedefDecl> TypedefDeclPtr; |
---|
[e491159] | 262 | typedef ScopedMap< std::string, std::pair< TypedefDeclPtr, int > > TypedefMap; |
---|
[0bcc2b7] | 263 | typedef ScopedMap< std::string, TypeDecl * > TypeDeclMap; |
---|
[cc79d97] | 264 | TypedefMap typedefNames; |
---|
[679864e1] | 265 | TypeDeclMap typedeclNames; |
---|
[cc79d97] | 266 | int scopeLevel; |
---|
[1f370451] | 267 | bool inFunctionType = false; |
---|
[a08ba92] | 268 | }; |
---|
[c8ffe20b] | 269 | |
---|
[69918cea] | 270 | struct EliminateTypedef { |
---|
| 271 | /// removes TypedefDecls from the AST |
---|
| 272 | static void eliminateTypedef( std::list< Declaration * > &translationUnit ); |
---|
| 273 | |
---|
| 274 | template<typename AggDecl> |
---|
[ef5b828] | 275 | void handleAggregate( AggDecl * aggregateDecl ); |
---|
[69918cea] | 276 | |
---|
| 277 | void previsit( StructDecl * aggregateDecl ); |
---|
| 278 | void previsit( UnionDecl * aggregateDecl ); |
---|
| 279 | void previsit( CompoundStmt * compoundStmt ); |
---|
| 280 | }; |
---|
| 281 | |
---|
[d24d4e1] | 282 | struct VerifyCtorDtorAssign { |
---|
[d1969a6] | 283 | /// ensure that constructors, destructors, and assignment have at least one |
---|
| 284 | /// parameter, the first of which must be a pointer, and that ctor/dtors have no |
---|
[9cb8e88d] | 285 | /// return values. |
---|
| 286 | static void verify( std::list< Declaration * > &translationUnit ); |
---|
| 287 | |
---|
[ef5b828] | 288 | void previsit( FunctionDecl * funcDecl ); |
---|
[5f98ce5] | 289 | }; |
---|
[70a06f6] | 290 | |
---|
[11ab8ea8] | 291 | /// ensure that generic types have the correct number of type arguments |
---|
[d24d4e1] | 292 | struct ValidateGenericParameters { |
---|
[0db6fc0] | 293 | void previsit( StructInstType * inst ); |
---|
| 294 | void previsit( UnionInstType * inst ); |
---|
[5f98ce5] | 295 | }; |
---|
[70a06f6] | 296 | |
---|
[6e50a6b] | 297 | /// desugar declarations and uses of dimension paramaters like [N], |
---|
| 298 | /// from type-system managed values, to tunnneling via ordinary types, |
---|
| 299 | /// as char[-] in and sizeof(-) out |
---|
| 300 | struct TranslateDimensionGenericParameters : public WithIndexer, public WithGuards { |
---|
| 301 | static void translateDimensions( std::list< Declaration * > &translationUnit ); |
---|
| 302 | TranslateDimensionGenericParameters(); |
---|
| 303 | |
---|
| 304 | bool nextVisitedNodeIsChildOfSUIT = false; // SUIT = Struct or Union -Inst Type |
---|
| 305 | bool visitingChildOfSUIT = false; |
---|
| 306 | void changeState_ChildOfSUIT( bool newVal ); |
---|
| 307 | void premutate( StructInstType * sit ); |
---|
| 308 | void premutate( UnionInstType * uit ); |
---|
| 309 | void premutate( BaseSyntaxNode * node ); |
---|
| 310 | |
---|
| 311 | TypeDecl * postmutate( TypeDecl * td ); |
---|
| 312 | Expression * postmutate( DimensionExpr * de ); |
---|
| 313 | Expression * postmutate( Expression * e ); |
---|
| 314 | }; |
---|
| 315 | |
---|
[2b79a70] | 316 | struct FixObjectType : public WithIndexer { |
---|
| 317 | /// resolves typeof type in object, function, and type declarations |
---|
| 318 | static void fix( std::list< Declaration * > & translationUnit ); |
---|
| 319 | |
---|
| 320 | void previsit( ObjectDecl * ); |
---|
| 321 | void previsit( FunctionDecl * ); |
---|
| 322 | void previsit( TypeDecl * ); |
---|
| 323 | }; |
---|
| 324 | |
---|
[09867ec] | 325 | struct InitializerLength { |
---|
[fbd7ad6] | 326 | /// for array types without an explicit length, compute the length and store it so that it |
---|
| 327 | /// is known to the rest of the phases. For example, |
---|
| 328 | /// int x[] = { 1, 2, 3 }; |
---|
| 329 | /// int y[][2] = { { 1, 2, 3 }, { 1, 2, 3 } }; |
---|
| 330 | /// here x and y are known at compile-time to have length 3, so change this into |
---|
| 331 | /// int x[3] = { 1, 2, 3 }; |
---|
| 332 | /// int y[3][2] = { { 1, 2, 3 }, { 1, 2, 3 } }; |
---|
| 333 | static void computeLength( std::list< Declaration * > & translationUnit ); |
---|
| 334 | |
---|
[0db6fc0] | 335 | void previsit( ObjectDecl * objDecl ); |
---|
[09867ec] | 336 | }; |
---|
| 337 | |
---|
| 338 | struct ArrayLength : public WithIndexer { |
---|
| 339 | static void computeLength( std::list< Declaration * > & translationUnit ); |
---|
| 340 | |
---|
[3ff4c1e] | 341 | void previsit( ArrayType * arrayType ); |
---|
[fbd7ad6] | 342 | }; |
---|
| 343 | |
---|
[d24d4e1] | 344 | struct CompoundLiteral final : public WithDeclsToAdd, public WithVisitorRef<CompoundLiteral> { |
---|
[68fe077a] | 345 | Type::StorageClasses storageClasses; |
---|
[630a82a] | 346 | |
---|
[ef5b828] | 347 | void premutate( ObjectDecl * objectDecl ); |
---|
| 348 | Expression * postmutate( CompoundLiteralExpr * compLitExpr ); |
---|
[9cb8e88d] | 349 | }; |
---|
| 350 | |
---|
[5809461] | 351 | struct LabelAddressFixer final : public WithGuards { |
---|
| 352 | std::set< Label > labels; |
---|
| 353 | |
---|
| 354 | void premutate( FunctionDecl * funcDecl ); |
---|
| 355 | Expression * postmutate( AddressExpr * addrExpr ); |
---|
| 356 | }; |
---|
[4fbdfae0] | 357 | |
---|
[5dcb881] | 358 | void validate_A( std::list< Declaration * > & translationUnit ) { |
---|
[c1ed2ee] | 359 | PassVisitor<EnumAndPointerDecay_old> epc; |
---|
[15f5c5e] | 360 | PassVisitor<HoistTypeDecls> hoistDecls; |
---|
[3c0d4cd] | 361 | { |
---|
| 362 | Stats::Heap::newPass("validate-A"); |
---|
| 363 | Stats::Time::BlockGuard guard("validate-A"); |
---|
[98538288] | 364 | VerifyCtorDtorAssign::verify( translationUnit ); // must happen before autogen, because autogen examines existing ctor/dtors |
---|
[3c0d4cd] | 365 | acceptAll( translationUnit, hoistDecls ); |
---|
| 366 | ReplaceTypedef::replaceTypedef( translationUnit ); |
---|
| 367 | ReturnTypeFixer::fix( translationUnit ); // must happen before autogen |
---|
[c1ed2ee] | 368 | acceptAll( translationUnit, epc ); // must happen before VerifyCtorDtorAssign, because void return objects should not exist; before LinkReferenceToTypes_old because it is an indexer and needs correct types for mangling |
---|
[3c0d4cd] | 369 | } |
---|
[5dcb881] | 370 | } |
---|
| 371 | |
---|
| 372 | void validate_B( std::list< Declaration * > & translationUnit ) { |
---|
| 373 | PassVisitor<LinkReferenceToTypes_old> lrt( nullptr ); |
---|
| 374 | PassVisitor<FixQualifiedTypes> fixQual; |
---|
[3c0d4cd] | 375 | { |
---|
| 376 | Stats::Heap::newPass("validate-B"); |
---|
| 377 | Stats::Time::BlockGuard guard("validate-B"); |
---|
[6e50a6b] | 378 | acceptAll( translationUnit, lrt ); // must happen before autogen, because sized flag needs to propagate to generated functions |
---|
| 379 | mutateAll( translationUnit, fixQual ); // must happen after LinkReferenceToTypes_old, because aggregate members are accessed |
---|
| 380 | HoistStruct::hoistStruct( translationUnit ); |
---|
| 381 | EliminateTypedef::eliminateTypedef( translationUnit ); |
---|
[3c0d4cd] | 382 | } |
---|
[5dcb881] | 383 | } |
---|
| 384 | |
---|
| 385 | void validate_C( std::list< Declaration * > & translationUnit ) { |
---|
| 386 | PassVisitor<ValidateGenericParameters> genericParams; |
---|
| 387 | PassVisitor<ResolveEnumInitializers> rei( nullptr ); |
---|
[3c0d4cd] | 388 | { |
---|
| 389 | Stats::Heap::newPass("validate-C"); |
---|
| 390 | Stats::Time::BlockGuard guard("validate-C"); |
---|
[6e50a6b] | 391 | Stats::Time::TimeBlock("Validate Generic Parameters", [&]() { |
---|
| 392 | acceptAll( translationUnit, genericParams ); // check as early as possible - can't happen before LinkReferenceToTypes_old; observed failing when attempted before eliminateTypedef |
---|
| 393 | }); |
---|
| 394 | Stats::Time::TimeBlock("Translate Dimensions", [&]() { |
---|
| 395 | TranslateDimensionGenericParameters::translateDimensions( translationUnit ); |
---|
| 396 | }); |
---|
| 397 | Stats::Time::TimeBlock("Resolve Enum Initializers", [&]() { |
---|
| 398 | acceptAll( translationUnit, rei ); // must happen after translateDimensions because rei needs identifier lookup, which needs name mangling |
---|
| 399 | }); |
---|
| 400 | Stats::Time::TimeBlock("Check Function Returns", [&]() { |
---|
| 401 | ReturnChecker::checkFunctionReturns( translationUnit ); |
---|
| 402 | }); |
---|
| 403 | Stats::Time::TimeBlock("Fix Return Statements", [&]() { |
---|
| 404 | InitTweak::fixReturnStatements( translationUnit ); // must happen before autogen |
---|
| 405 | }); |
---|
[3c0d4cd] | 406 | } |
---|
[5dcb881] | 407 | } |
---|
| 408 | |
---|
| 409 | void validate_D( std::list< Declaration * > & translationUnit ) { |
---|
[3c0d4cd] | 410 | { |
---|
| 411 | Stats::Heap::newPass("validate-D"); |
---|
| 412 | Stats::Time::BlockGuard guard("validate-D"); |
---|
[c884f2d] | 413 | Stats::Time::TimeBlock("Apply Concurrent Keywords", [&]() { |
---|
| 414 | Concurrency::applyKeywords( translationUnit ); |
---|
| 415 | }); |
---|
| 416 | Stats::Time::TimeBlock("Forall Pointer Decay", [&]() { |
---|
[9490621] | 417 | decayForallPointers( translationUnit ); // must happen before autogenerateRoutines, after Concurrency::applyKeywords because uniqueIds must be set on declaration before resolution |
---|
[c884f2d] | 418 | }); |
---|
| 419 | Stats::Time::TimeBlock("Hoist Control Declarations", [&]() { |
---|
| 420 | ControlStruct::hoistControlDecls( translationUnit ); // hoist initialization out of for statements; must happen before autogenerateRoutines |
---|
| 421 | }); |
---|
| 422 | Stats::Time::TimeBlock("Generate Autogen routines", [&]() { |
---|
[c1ed2ee] | 423 | autogenerateRoutines( translationUnit ); // moved up, used to be below compoundLiteral - currently needs EnumAndPointerDecay_old |
---|
[c884f2d] | 424 | }); |
---|
[3c0d4cd] | 425 | } |
---|
[5dcb881] | 426 | } |
---|
| 427 | |
---|
| 428 | void validate_E( std::list< Declaration * > & translationUnit ) { |
---|
| 429 | PassVisitor<CompoundLiteral> compoundliteral; |
---|
[3c0d4cd] | 430 | { |
---|
| 431 | Stats::Heap::newPass("validate-E"); |
---|
| 432 | Stats::Time::BlockGuard guard("validate-E"); |
---|
[c884f2d] | 433 | Stats::Time::TimeBlock("Implement Mutex Func", [&]() { |
---|
| 434 | Concurrency::implementMutexFuncs( translationUnit ); |
---|
| 435 | }); |
---|
| 436 | Stats::Time::TimeBlock("Implement Thread Start", [&]() { |
---|
| 437 | Concurrency::implementThreadStarter( translationUnit ); |
---|
| 438 | }); |
---|
| 439 | Stats::Time::TimeBlock("Compound Literal", [&]() { |
---|
| 440 | mutateAll( translationUnit, compoundliteral ); |
---|
| 441 | }); |
---|
[16ba4a6f] | 442 | if (!useNewAST) { |
---|
| 443 | Stats::Time::TimeBlock("Resolve With Expressions", [&]() { |
---|
| 444 | ResolvExpr::resolveWithExprs( translationUnit ); // must happen before FixObjectType because user-code is resolved and may contain with variables |
---|
| 445 | }); |
---|
| 446 | } |
---|
[3c0d4cd] | 447 | } |
---|
[5dcb881] | 448 | } |
---|
| 449 | |
---|
| 450 | void validate_F( std::list< Declaration * > & translationUnit ) { |
---|
| 451 | PassVisitor<LabelAddressFixer> labelAddrFixer; |
---|
[3c0d4cd] | 452 | { |
---|
| 453 | Stats::Heap::newPass("validate-F"); |
---|
| 454 | Stats::Time::BlockGuard guard("validate-F"); |
---|
[16ba4a6f] | 455 | if (!useNewAST) { |
---|
| 456 | Stats::Time::TimeCall("Fix Object Type", |
---|
| 457 | FixObjectType::fix, translationUnit); |
---|
| 458 | } |
---|
[09867ec] | 459 | Stats::Time::TimeCall("Initializer Length", |
---|
| 460 | InitializerLength::computeLength, translationUnit); |
---|
| 461 | if (!useNewAST) { |
---|
| 462 | Stats::Time::TimeCall("Array Length", |
---|
| 463 | ArrayLength::computeLength, translationUnit); |
---|
| 464 | } |
---|
[095b99a] | 465 | Stats::Time::TimeCall("Find Special Declarations", |
---|
| 466 | Validate::findSpecialDecls, translationUnit); |
---|
| 467 | Stats::Time::TimeCall("Fix Label Address", |
---|
| 468 | mutateAll<LabelAddressFixer>, translationUnit, labelAddrFixer); |
---|
[16ba4a6f] | 469 | if (!useNewAST) { |
---|
| 470 | Stats::Time::TimeCall("Handle Attributes", |
---|
| 471 | Validate::handleAttributes, translationUnit); |
---|
| 472 | } |
---|
[3c0d4cd] | 473 | } |
---|
[a08ba92] | 474 | } |
---|
[9cb8e88d] | 475 | |
---|
[a488783] | 476 | void decayForallPointers( std::list< Declaration * > & translationUnit ) { |
---|
[9490621] | 477 | PassVisitor<TraitExpander_old> te; |
---|
| 478 | acceptAll( translationUnit, te ); |
---|
| 479 | PassVisitor<AssertionFixer_old> af; |
---|
| 480 | acceptAll( translationUnit, af ); |
---|
| 481 | PassVisitor<CheckOperatorTypes_old> cot; |
---|
| 482 | acceptAll( translationUnit, cot ); |
---|
| 483 | PassVisitor<FixUniqueIds_old> fui; |
---|
| 484 | acceptAll( translationUnit, fui ); |
---|
| 485 | } |
---|
| 486 | |
---|
| 487 | void decayForallPointersA( std::list< Declaration * > & translationUnit ) { |
---|
| 488 | PassVisitor<TraitExpander_old> te; |
---|
| 489 | acceptAll( translationUnit, te ); |
---|
| 490 | } |
---|
| 491 | void decayForallPointersB( std::list< Declaration * > & translationUnit ) { |
---|
| 492 | PassVisitor<AssertionFixer_old> af; |
---|
| 493 | acceptAll( translationUnit, af ); |
---|
| 494 | } |
---|
| 495 | void decayForallPointersC( std::list< Declaration * > & translationUnit ) { |
---|
| 496 | PassVisitor<CheckOperatorTypes_old> cot; |
---|
| 497 | acceptAll( translationUnit, cot ); |
---|
| 498 | } |
---|
| 499 | void decayForallPointersD( std::list< Declaration * > & translationUnit ) { |
---|
| 500 | PassVisitor<FixUniqueIds_old> fui; |
---|
| 501 | acceptAll( translationUnit, fui ); |
---|
[a488783] | 502 | } |
---|
| 503 | |
---|
[5dcb881] | 504 | void validate( std::list< Declaration * > &translationUnit, __attribute__((unused)) bool doDebug ) { |
---|
| 505 | validate_A( translationUnit ); |
---|
| 506 | validate_B( translationUnit ); |
---|
| 507 | validate_C( translationUnit ); |
---|
| 508 | validate_D( translationUnit ); |
---|
| 509 | validate_E( translationUnit ); |
---|
| 510 | validate_F( translationUnit ); |
---|
| 511 | } |
---|
| 512 | |
---|
[ef5b828] | 513 | void validateType( Type * type, const Indexer * indexer ) { |
---|
[c1ed2ee] | 514 | PassVisitor<EnumAndPointerDecay_old> epc; |
---|
| 515 | PassVisitor<LinkReferenceToTypes_old> lrt( indexer ); |
---|
[9490621] | 516 | PassVisitor<TraitExpander_old> te; |
---|
| 517 | PassVisitor<AssertionFixer_old> af; |
---|
| 518 | PassVisitor<CheckOperatorTypes_old> cot; |
---|
| 519 | PassVisitor<FixUniqueIds_old> fui; |
---|
[bda58ad] | 520 | type->accept( epc ); |
---|
[cce9429] | 521 | type->accept( lrt ); |
---|
[9490621] | 522 | type->accept( te ); |
---|
| 523 | type->accept( af ); |
---|
| 524 | type->accept( cot ); |
---|
| 525 | type->accept( fui ); |
---|
[a08ba92] | 526 | } |
---|
[c8ffe20b] | 527 | |
---|
[15f5c5e] | 528 | void HoistTypeDecls::handleType( Type * type ) { |
---|
[29f9e20] | 529 | // some type declarations are buried in expressions and not easy to hoist during parsing; hoist them here |
---|
| 530 | AggregateDecl * aggr = nullptr; |
---|
| 531 | if ( StructInstType * inst = dynamic_cast< StructInstType * >( type ) ) { |
---|
| 532 | aggr = inst->baseStruct; |
---|
| 533 | } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( type ) ) { |
---|
| 534 | aggr = inst->baseUnion; |
---|
| 535 | } else if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( type ) ) { |
---|
| 536 | aggr = inst->baseEnum; |
---|
| 537 | } |
---|
| 538 | if ( aggr && aggr->body ) { |
---|
| 539 | declsToAddBefore.push_front( aggr ); |
---|
| 540 | } |
---|
| 541 | } |
---|
| 542 | |
---|
[15f5c5e] | 543 | void HoistTypeDecls::previsit( SizeofExpr * expr ) { |
---|
[29f9e20] | 544 | handleType( expr->type ); |
---|
| 545 | } |
---|
| 546 | |
---|
[15f5c5e] | 547 | void HoistTypeDecls::previsit( AlignofExpr * expr ) { |
---|
[29f9e20] | 548 | handleType( expr->type ); |
---|
| 549 | } |
---|
| 550 | |
---|
[15f5c5e] | 551 | void HoistTypeDecls::previsit( UntypedOffsetofExpr * expr ) { |
---|
[29f9e20] | 552 | handleType( expr->type ); |
---|
| 553 | } |
---|
| 554 | |
---|
[95d09bdb] | 555 | void HoistTypeDecls::previsit( CompoundLiteralExpr * expr ) { |
---|
| 556 | handleType( expr->result ); |
---|
| 557 | } |
---|
| 558 | |
---|
[29f9e20] | 559 | |
---|
[a12c81f3] | 560 | Type * FixQualifiedTypes::postmutate( QualifiedType * qualType ) { |
---|
| 561 | Type * parent = qualType->parent; |
---|
| 562 | Type * child = qualType->child; |
---|
| 563 | if ( dynamic_cast< GlobalScopeType * >( qualType->parent ) ) { |
---|
| 564 | // .T => lookup T at global scope |
---|
[062e8df] | 565 | if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( child ) ) { |
---|
[a12c81f3] | 566 | auto td = indexer.globalLookupType( inst->name ); |
---|
[062e8df] | 567 | if ( ! td ) { |
---|
| 568 | SemanticError( qualType->location, toString("Use of undefined global type ", inst->name) ); |
---|
| 569 | } |
---|
[a12c81f3] | 570 | auto base = td->base; |
---|
[062e8df] | 571 | assert( base ); |
---|
[8a3ecb9] | 572 | Type * ret = base->clone(); |
---|
| 573 | ret->get_qualifiers() = qualType->get_qualifiers(); |
---|
| 574 | return ret; |
---|
[a12c81f3] | 575 | } else { |
---|
[062e8df] | 576 | // .T => T is not a type name |
---|
| 577 | assertf( false, "unhandled global qualified child type: %s", toCString(child) ); |
---|
[a12c81f3] | 578 | } |
---|
| 579 | } else { |
---|
| 580 | // S.T => S must be an aggregate type, find the declaration for T in S. |
---|
| 581 | AggregateDecl * aggr = nullptr; |
---|
| 582 | if ( StructInstType * inst = dynamic_cast< StructInstType * >( parent ) ) { |
---|
| 583 | aggr = inst->baseStruct; |
---|
| 584 | } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * > ( parent ) ) { |
---|
| 585 | aggr = inst->baseUnion; |
---|
| 586 | } else { |
---|
[062e8df] | 587 | SemanticError( qualType->location, toString("Qualified type requires an aggregate on the left, but has: ", parent) ); |
---|
[a12c81f3] | 588 | } |
---|
| 589 | assert( aggr ); // TODO: need to handle forward declarations |
---|
| 590 | for ( Declaration * member : aggr->members ) { |
---|
[7e08acf] | 591 | if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( child ) ) { |
---|
[8a3ecb9] | 592 | // name on the right is a typedef |
---|
[a12c81f3] | 593 | if ( NamedTypeDecl * aggr = dynamic_cast< NamedTypeDecl * > ( member ) ) { |
---|
| 594 | if ( aggr->name == inst->name ) { |
---|
[062e8df] | 595 | assert( aggr->base ); |
---|
[8a3ecb9] | 596 | Type * ret = aggr->base->clone(); |
---|
| 597 | ret->get_qualifiers() = qualType->get_qualifiers(); |
---|
[7e08acf] | 598 | TypeSubstitution sub = parent->genericSubstitution(); |
---|
| 599 | sub.apply(ret); |
---|
[8a3ecb9] | 600 | return ret; |
---|
[a12c81f3] | 601 | } |
---|
| 602 | } |
---|
| 603 | } else { |
---|
| 604 | // S.T - S is not an aggregate => error |
---|
| 605 | assertf( false, "unhandled qualified child type: %s", toCString(qualType) ); |
---|
| 606 | } |
---|
| 607 | } |
---|
| 608 | // failed to find a satisfying definition of type |
---|
[062e8df] | 609 | SemanticError( qualType->location, toString("Undefined type in qualified type: ", qualType) ); |
---|
[a12c81f3] | 610 | } |
---|
| 611 | |
---|
| 612 | // ... may want to link canonical SUE definition to each forward decl so that it becomes easier to lookup? |
---|
| 613 | } |
---|
| 614 | |
---|
| 615 | |
---|
[a08ba92] | 616 | void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) { |
---|
[a09e45b] | 617 | PassVisitor<HoistStruct> hoister; |
---|
| 618 | acceptAll( translationUnit, hoister ); |
---|
[a08ba92] | 619 | } |
---|
[c8ffe20b] | 620 | |
---|
[ef5b828] | 621 | bool shouldHoist( Declaration * decl ) { |
---|
[0f40912] | 622 | return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl ) || dynamic_cast< StaticAssertDecl * >( decl ); |
---|
[a08ba92] | 623 | } |
---|
[c0aa336] | 624 | |
---|
[d419d8e] | 625 | namespace { |
---|
| 626 | void qualifiedName( AggregateDecl * aggr, std::ostringstream & ss ) { |
---|
| 627 | if ( aggr->parent ) qualifiedName( aggr->parent, ss ); |
---|
| 628 | ss << "__" << aggr->name; |
---|
| 629 | } |
---|
| 630 | |
---|
| 631 | // mangle nested type names using entire parent chain |
---|
| 632 | std::string qualifiedName( AggregateDecl * aggr ) { |
---|
| 633 | std::ostringstream ss; |
---|
| 634 | qualifiedName( aggr, ss ); |
---|
| 635 | return ss.str(); |
---|
| 636 | } |
---|
| 637 | } |
---|
| 638 | |
---|
[a08ba92] | 639 | template< typename AggDecl > |
---|
[ef5b828] | 640 | void HoistStruct::handleAggregate( AggDecl * aggregateDecl ) { |
---|
[bdad6eb7] | 641 | if ( parentAggr ) { |
---|
[d419d8e] | 642 | aggregateDecl->parent = parentAggr; |
---|
| 643 | aggregateDecl->name = qualifiedName( aggregateDecl ); |
---|
[0dd3a2f] | 644 | // Add elements in stack order corresponding to nesting structure. |
---|
[a09e45b] | 645 | declsToAddBefore.push_front( aggregateDecl ); |
---|
[0dd3a2f] | 646 | } else { |
---|
[bdad6eb7] | 647 | GuardValue( parentAggr ); |
---|
| 648 | parentAggr = aggregateDecl; |
---|
[0dd3a2f] | 649 | } // if |
---|
| 650 | // Always remove the hoisted aggregate from the inner structure. |
---|
[0f40912] | 651 | GuardAction( [aggregateDecl]() { filter( aggregateDecl->members, shouldHoist, false ); } ); |
---|
[a08ba92] | 652 | } |
---|
[c8ffe20b] | 653 | |
---|
[0f40912] | 654 | void HoistStruct::previsit( StaticAssertDecl * assertDecl ) { |
---|
| 655 | if ( parentAggr ) { |
---|
| 656 | declsToAddBefore.push_back( assertDecl ); |
---|
| 657 | } |
---|
| 658 | } |
---|
| 659 | |
---|
[a09e45b] | 660 | void HoistStruct::previsit( StructDecl * aggregateDecl ) { |
---|
[0dd3a2f] | 661 | handleAggregate( aggregateDecl ); |
---|
[a08ba92] | 662 | } |
---|
[c8ffe20b] | 663 | |
---|
[a09e45b] | 664 | void HoistStruct::previsit( UnionDecl * aggregateDecl ) { |
---|
[0dd3a2f] | 665 | handleAggregate( aggregateDecl ); |
---|
[a08ba92] | 666 | } |
---|
[c8ffe20b] | 667 | |
---|
[d419d8e] | 668 | void HoistStruct::previsit( StructInstType * type ) { |
---|
| 669 | // need to reset type name after expanding to qualified name |
---|
| 670 | assert( type->baseStruct ); |
---|
| 671 | type->name = type->baseStruct->name; |
---|
| 672 | } |
---|
| 673 | |
---|
| 674 | void HoistStruct::previsit( UnionInstType * type ) { |
---|
| 675 | assert( type->baseUnion ); |
---|
| 676 | type->name = type->baseUnion->name; |
---|
| 677 | } |
---|
| 678 | |
---|
| 679 | void HoistStruct::previsit( EnumInstType * type ) { |
---|
| 680 | assert( type->baseEnum ); |
---|
| 681 | type->name = type->baseEnum->name; |
---|
| 682 | } |
---|
| 683 | |
---|
| 684 | |
---|
[ef5b828] | 685 | bool isTypedef( Declaration * decl ) { |
---|
[69918cea] | 686 | return dynamic_cast< TypedefDecl * >( decl ); |
---|
| 687 | } |
---|
| 688 | |
---|
| 689 | void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) { |
---|
| 690 | PassVisitor<EliminateTypedef> eliminator; |
---|
| 691 | acceptAll( translationUnit, eliminator ); |
---|
| 692 | filter( translationUnit, isTypedef, true ); |
---|
| 693 | } |
---|
| 694 | |
---|
| 695 | template< typename AggDecl > |
---|
[ef5b828] | 696 | void EliminateTypedef::handleAggregate( AggDecl * aggregateDecl ) { |
---|
[69918cea] | 697 | filter( aggregateDecl->members, isTypedef, true ); |
---|
| 698 | } |
---|
| 699 | |
---|
| 700 | void EliminateTypedef::previsit( StructDecl * aggregateDecl ) { |
---|
| 701 | handleAggregate( aggregateDecl ); |
---|
| 702 | } |
---|
| 703 | |
---|
| 704 | void EliminateTypedef::previsit( UnionDecl * aggregateDecl ) { |
---|
| 705 | handleAggregate( aggregateDecl ); |
---|
| 706 | } |
---|
| 707 | |
---|
| 708 | void EliminateTypedef::previsit( CompoundStmt * compoundStmt ) { |
---|
| 709 | // remove and delete decl stmts |
---|
| 710 | filter( compoundStmt->kids, [](Statement * stmt) { |
---|
[ef5b828] | 711 | if ( DeclStmt * declStmt = dynamic_cast< DeclStmt * >( stmt ) ) { |
---|
[69918cea] | 712 | if ( dynamic_cast< TypedefDecl * >( declStmt->decl ) ) { |
---|
| 713 | return true; |
---|
| 714 | } // if |
---|
| 715 | } // if |
---|
| 716 | return false; |
---|
| 717 | }, true); |
---|
| 718 | } |
---|
| 719 | |
---|
[ef5b828] | 720 | void EnumAndPointerDecay_old::previsit( EnumDecl * enumDecl ) { |
---|
[0dd3a2f] | 721 | // Set the type of each member of the enumeration to be EnumConstant |
---|
[0b3b2ae] | 722 | for ( std::list< Declaration * >::iterator i = enumDecl->members.begin(); i != enumDecl->members.end(); ++i ) { |
---|
[ef5b828] | 723 | ObjectDecl * obj = dynamic_cast< ObjectDecl * >( * i ); |
---|
[0dd3a2f] | 724 | assert( obj ); |
---|
[0b3b2ae] | 725 | obj->set_type( new EnumInstType( Type::Qualifiers( Type::Const ), enumDecl->name ) ); |
---|
[0dd3a2f] | 726 | } // for |
---|
[a08ba92] | 727 | } |
---|
[51b7345] | 728 | |
---|
[a08ba92] | 729 | namespace { |
---|
[83de11e] | 730 | template< typename DWTList > |
---|
[4bda2cf] | 731 | void fixFunctionList( DWTList & dwts, bool isVarArgs, FunctionType * func ) { |
---|
| 732 | auto nvals = dwts.size(); |
---|
| 733 | bool containsVoid = false; |
---|
| 734 | for ( auto & dwt : dwts ) { |
---|
| 735 | // fix each DWT and record whether a void was found |
---|
| 736 | containsVoid |= fixFunction( dwt ); |
---|
| 737 | } |
---|
| 738 | |
---|
| 739 | // the only case in which "void" is valid is where it is the only one in the list |
---|
| 740 | if ( containsVoid && ( nvals > 1 || isVarArgs ) ) { |
---|
[a16764a6] | 741 | SemanticError( func, "invalid type void in function type " ); |
---|
[4bda2cf] | 742 | } |
---|
| 743 | |
---|
| 744 | // one void is the only thing in the list; remove it. |
---|
| 745 | if ( containsVoid ) { |
---|
| 746 | delete dwts.front(); |
---|
| 747 | dwts.clear(); |
---|
| 748 | } |
---|
[0dd3a2f] | 749 | } |
---|
[a08ba92] | 750 | } |
---|
[c8ffe20b] | 751 | |
---|
[ef5b828] | 752 | void EnumAndPointerDecay_old::previsit( FunctionType * func ) { |
---|
[0dd3a2f] | 753 | // Fix up parameters and return types |
---|
[4bda2cf] | 754 | fixFunctionList( func->parameters, func->isVarArgs, func ); |
---|
| 755 | fixFunctionList( func->returnVals, false, func ); |
---|
[a08ba92] | 756 | } |
---|
[c8ffe20b] | 757 | |
---|
[6e50a6b] | 758 | LinkReferenceToTypes_old::LinkReferenceToTypes_old( const Indexer * other_indexer ) : WithIndexer( false ) { |
---|
[0dd3a2f] | 759 | if ( other_indexer ) { |
---|
[522363e] | 760 | local_indexer = other_indexer; |
---|
[0dd3a2f] | 761 | } else { |
---|
[522363e] | 762 | local_indexer = &indexer; |
---|
[0dd3a2f] | 763 | } // if |
---|
[a08ba92] | 764 | } |
---|
[c8ffe20b] | 765 | |
---|
[ef5b828] | 766 | void LinkReferenceToTypes_old::postvisit( EnumInstType * enumInst ) { |
---|
[8fd52e90] | 767 | const EnumDecl * st = local_indexer->lookupEnum( enumInst->name ); |
---|
[c0aa336] | 768 | // it's not a semantic error if the enum is not found, just an implicit forward declaration |
---|
| 769 | if ( st ) { |
---|
[8fd52e90] | 770 | enumInst->baseEnum = const_cast<EnumDecl *>(st); // Just linking in the node |
---|
[c0aa336] | 771 | } // if |
---|
[29f9e20] | 772 | if ( ! st || ! st->body ) { |
---|
[c0aa336] | 773 | // use of forward declaration |
---|
[eaa6430] | 774 | forwardEnums[ enumInst->name ].push_back( enumInst ); |
---|
[c0aa336] | 775 | } // if |
---|
| 776 | } |
---|
| 777 | |
---|
[ef5b828] | 778 | void LinkReferenceToTypes_old::postvisit( StructInstType * structInst ) { |
---|
[8fd52e90] | 779 | const StructDecl * st = local_indexer->lookupStruct( structInst->name ); |
---|
[0dd3a2f] | 780 | // it's not a semantic error if the struct is not found, just an implicit forward declaration |
---|
| 781 | if ( st ) { |
---|
[8fd52e90] | 782 | structInst->baseStruct = const_cast<StructDecl *>(st); // Just linking in the node |
---|
[0dd3a2f] | 783 | } // if |
---|
[29f9e20] | 784 | if ( ! st || ! st->body ) { |
---|
[0dd3a2f] | 785 | // use of forward declaration |
---|
[eaa6430] | 786 | forwardStructs[ structInst->name ].push_back( structInst ); |
---|
[0dd3a2f] | 787 | } // if |
---|
[a08ba92] | 788 | } |
---|
[c8ffe20b] | 789 | |
---|
[ef5b828] | 790 | void LinkReferenceToTypes_old::postvisit( UnionInstType * unionInst ) { |
---|
[8fd52e90] | 791 | const UnionDecl * un = local_indexer->lookupUnion( unionInst->name ); |
---|
[0dd3a2f] | 792 | // it's not a semantic error if the union is not found, just an implicit forward declaration |
---|
| 793 | if ( un ) { |
---|
[8fd52e90] | 794 | unionInst->baseUnion = const_cast<UnionDecl *>(un); // Just linking in the node |
---|
[0dd3a2f] | 795 | } // if |
---|
[29f9e20] | 796 | if ( ! un || ! un->body ) { |
---|
[0dd3a2f] | 797 | // use of forward declaration |
---|
[eaa6430] | 798 | forwardUnions[ unionInst->name ].push_back( unionInst ); |
---|
[0dd3a2f] | 799 | } // if |
---|
[a08ba92] | 800 | } |
---|
[c8ffe20b] | 801 | |
---|
[c1ed2ee] | 802 | void LinkReferenceToTypes_old::previsit( QualifiedType * ) { |
---|
[afcb0a3] | 803 | visit_children = false; |
---|
| 804 | } |
---|
| 805 | |
---|
[c1ed2ee] | 806 | void LinkReferenceToTypes_old::postvisit( QualifiedType * qualType ) { |
---|
[afcb0a3] | 807 | // linking only makes sense for the 'oldest ancestor' of the qualified type |
---|
[ef5b828] | 808 | qualType->parent->accept( * visitor ); |
---|
[afcb0a3] | 809 | } |
---|
| 810 | |
---|
[be9036d] | 811 | template< typename Decl > |
---|
| 812 | void normalizeAssertions( std::list< Decl * > & assertions ) { |
---|
| 813 | // ensure no duplicate trait members after the clone |
---|
| 814 | auto pred = [](Decl * d1, Decl * d2) { |
---|
| 815 | // only care if they're equal |
---|
| 816 | DeclarationWithType * dwt1 = dynamic_cast<DeclarationWithType *>( d1 ); |
---|
| 817 | DeclarationWithType * dwt2 = dynamic_cast<DeclarationWithType *>( d2 ); |
---|
| 818 | if ( dwt1 && dwt2 ) { |
---|
[eaa6430] | 819 | if ( dwt1->name == dwt2->name && ResolvExpr::typesCompatible( dwt1->get_type(), dwt2->get_type(), SymTab::Indexer() ) ) { |
---|
[be9036d] | 820 | // std::cerr << "=========== equal:" << std::endl; |
---|
| 821 | // std::cerr << "d1: " << d1 << std::endl; |
---|
| 822 | // std::cerr << "d2: " << d2 << std::endl; |
---|
| 823 | return false; |
---|
| 824 | } |
---|
[2c57025] | 825 | } |
---|
[be9036d] | 826 | return d1 < d2; |
---|
| 827 | }; |
---|
| 828 | std::set<Decl *, decltype(pred)> unique_members( assertions.begin(), assertions.end(), pred ); |
---|
| 829 | // if ( unique_members.size() != assertions.size() ) { |
---|
| 830 | // std::cerr << "============different" << std::endl; |
---|
| 831 | // std::cerr << unique_members.size() << " " << assertions.size() << std::endl; |
---|
| 832 | // } |
---|
| 833 | |
---|
| 834 | std::list< Decl * > order; |
---|
| 835 | order.splice( order.end(), assertions ); |
---|
| 836 | std::copy_if( order.begin(), order.end(), back_inserter( assertions ), [&]( Decl * decl ) { |
---|
| 837 | return unique_members.count( decl ); |
---|
| 838 | }); |
---|
| 839 | } |
---|
| 840 | |
---|
| 841 | // expand assertions from trait instance, performing the appropriate type variable substitutions |
---|
| 842 | template< typename Iterator > |
---|
| 843 | void expandAssertions( TraitInstType * inst, Iterator out ) { |
---|
[eaa6430] | 844 | assertf( inst->baseTrait, "Trait instance not linked to base trait: %s", toCString( inst ) ); |
---|
[be9036d] | 845 | std::list< DeclarationWithType * > asserts; |
---|
| 846 | for ( Declaration * decl : inst->baseTrait->members ) { |
---|
[e3e16bc] | 847 | asserts.push_back( strict_dynamic_cast<DeclarationWithType *>( decl->clone() ) ); |
---|
[2c57025] | 848 | } |
---|
[be9036d] | 849 | // substitute trait decl parameters for instance parameters |
---|
| 850 | applySubstitution( inst->baseTrait->parameters.begin(), inst->baseTrait->parameters.end(), inst->parameters.begin(), asserts.begin(), asserts.end(), out ); |
---|
| 851 | } |
---|
| 852 | |
---|
[c1ed2ee] | 853 | void LinkReferenceToTypes_old::postvisit( TraitDecl * traitDecl ) { |
---|
[be9036d] | 854 | if ( traitDecl->name == "sized" ) { |
---|
| 855 | // "sized" is a special trait - flick the sized status on for the type variable |
---|
| 856 | assertf( traitDecl->parameters.size() == 1, "Built-in trait 'sized' has incorrect number of parameters: %zd", traitDecl->parameters.size() ); |
---|
| 857 | TypeDecl * td = traitDecl->parameters.front(); |
---|
| 858 | td->set_sized( true ); |
---|
| 859 | } |
---|
| 860 | |
---|
| 861 | // move assertions from type parameters into the body of the trait |
---|
| 862 | for ( TypeDecl * td : traitDecl->parameters ) { |
---|
| 863 | for ( DeclarationWithType * assert : td->assertions ) { |
---|
| 864 | if ( TraitInstType * inst = dynamic_cast< TraitInstType * >( assert->get_type() ) ) { |
---|
| 865 | expandAssertions( inst, back_inserter( traitDecl->members ) ); |
---|
| 866 | } else { |
---|
| 867 | traitDecl->members.push_back( assert->clone() ); |
---|
| 868 | } |
---|
| 869 | } |
---|
| 870 | deleteAll( td->assertions ); |
---|
| 871 | td->assertions.clear(); |
---|
| 872 | } // for |
---|
| 873 | } |
---|
[2ae171d8] | 874 | |
---|
[c1ed2ee] | 875 | void LinkReferenceToTypes_old::postvisit( TraitInstType * traitInst ) { |
---|
[2ae171d8] | 876 | // handle other traits |
---|
[8fd52e90] | 877 | const TraitDecl * traitDecl = local_indexer->lookupTrait( traitInst->name ); |
---|
[4a9ccc3] | 878 | if ( ! traitDecl ) { |
---|
[a16764a6] | 879 | SemanticError( traitInst->location, "use of undeclared trait " + traitInst->name ); |
---|
[17cd4eb] | 880 | } // if |
---|
[0b3b2ae] | 881 | if ( traitDecl->parameters.size() != traitInst->parameters.size() ) { |
---|
[a16764a6] | 882 | SemanticError( traitInst, "incorrect number of trait parameters: " ); |
---|
[4a9ccc3] | 883 | } // if |
---|
[8fd52e90] | 884 | traitInst->baseTrait = const_cast<TraitDecl *>(traitDecl); // Just linking in the node |
---|
[79970ed] | 885 | |
---|
[4a9ccc3] | 886 | // need to carry over the 'sized' status of each decl in the instance |
---|
[eaa6430] | 887 | for ( auto p : group_iterate( traitDecl->parameters, traitInst->parameters ) ) { |
---|
[5c4d27f] | 888 | TypeExpr * expr = dynamic_cast< TypeExpr * >( std::get<1>(p) ); |
---|
| 889 | if ( ! expr ) { |
---|
[a16764a6] | 890 | SemanticError( std::get<1>(p), "Expression parameters for trait instances are currently unsupported: " ); |
---|
[5c4d27f] | 891 | } |
---|
[4a9ccc3] | 892 | if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( expr->get_type() ) ) { |
---|
| 893 | TypeDecl * formalDecl = std::get<0>(p); |
---|
[eaa6430] | 894 | TypeDecl * instDecl = inst->baseType; |
---|
[4a9ccc3] | 895 | if ( formalDecl->get_sized() ) instDecl->set_sized( true ); |
---|
| 896 | } |
---|
| 897 | } |
---|
[be9036d] | 898 | // normalizeAssertions( traitInst->members ); |
---|
[a08ba92] | 899 | } |
---|
[c8ffe20b] | 900 | |
---|
[ef5b828] | 901 | void LinkReferenceToTypes_old::postvisit( EnumDecl * enumDecl ) { |
---|
[c0aa336] | 902 | // visit enum members first so that the types of self-referencing members are updated properly |
---|
[b16923d] | 903 | if ( enumDecl->body ) { |
---|
[eaa6430] | 904 | ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl->name ); |
---|
[c0aa336] | 905 | if ( fwds != forwardEnums.end() ) { |
---|
| 906 | for ( std::list< EnumInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
[ef5b828] | 907 | (* inst)->baseEnum = enumDecl; |
---|
[c0aa336] | 908 | } // for |
---|
| 909 | forwardEnums.erase( fwds ); |
---|
| 910 | } // if |
---|
| 911 | } // if |
---|
| 912 | } |
---|
| 913 | |
---|
[c1ed2ee] | 914 | void LinkReferenceToTypes_old::renameGenericParams( std::list< TypeDecl * > & params ) { |
---|
[b95fe40] | 915 | // rename generic type parameters uniquely so that they do not conflict with user-defined function forall parameters, e.g. |
---|
| 916 | // forall(otype T) |
---|
| 917 | // struct Box { |
---|
| 918 | // T x; |
---|
| 919 | // }; |
---|
| 920 | // forall(otype T) |
---|
| 921 | // void f(Box(T) b) { |
---|
| 922 | // ... |
---|
| 923 | // } |
---|
| 924 | // The T in Box and the T in f are different, so internally the naming must reflect that. |
---|
| 925 | GuardValue( inGeneric ); |
---|
| 926 | inGeneric = ! params.empty(); |
---|
| 927 | for ( TypeDecl * td : params ) { |
---|
| 928 | td->name = "__" + td->name + "_generic_"; |
---|
| 929 | } |
---|
| 930 | } |
---|
| 931 | |
---|
[c1ed2ee] | 932 | void LinkReferenceToTypes_old::previsit( StructDecl * structDecl ) { |
---|
[b95fe40] | 933 | renameGenericParams( structDecl->parameters ); |
---|
| 934 | } |
---|
| 935 | |
---|
[c1ed2ee] | 936 | void LinkReferenceToTypes_old::previsit( UnionDecl * unionDecl ) { |
---|
[b95fe40] | 937 | renameGenericParams( unionDecl->parameters ); |
---|
| 938 | } |
---|
| 939 | |
---|
[ef5b828] | 940 | void LinkReferenceToTypes_old::postvisit( StructDecl * structDecl ) { |
---|
[677c1be] | 941 | // visit struct members first so that the types of self-referencing members are updated properly |
---|
[522363e] | 942 | // xxx - need to ensure that type parameters match up between forward declarations and definition (most importantly, number of type parameters and their defaults) |
---|
[b16923d] | 943 | if ( structDecl->body ) { |
---|
[eaa6430] | 944 | ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->name ); |
---|
[0dd3a2f] | 945 | if ( fwds != forwardStructs.end() ) { |
---|
| 946 | for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
[ef5b828] | 947 | (* inst)->baseStruct = structDecl; |
---|
[0dd3a2f] | 948 | } // for |
---|
| 949 | forwardStructs.erase( fwds ); |
---|
| 950 | } // if |
---|
| 951 | } // if |
---|
[a08ba92] | 952 | } |
---|
[c8ffe20b] | 953 | |
---|
[ef5b828] | 954 | void LinkReferenceToTypes_old::postvisit( UnionDecl * unionDecl ) { |
---|
[b16923d] | 955 | if ( unionDecl->body ) { |
---|
[eaa6430] | 956 | ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->name ); |
---|
[0dd3a2f] | 957 | if ( fwds != forwardUnions.end() ) { |
---|
| 958 | for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) { |
---|
[ef5b828] | 959 | (* inst)->baseUnion = unionDecl; |
---|
[0dd3a2f] | 960 | } // for |
---|
| 961 | forwardUnions.erase( fwds ); |
---|
| 962 | } // if |
---|
| 963 | } // if |
---|
[a08ba92] | 964 | } |
---|
[c8ffe20b] | 965 | |
---|
[ef5b828] | 966 | void LinkReferenceToTypes_old::postvisit( TypeInstType * typeInst ) { |
---|
[b95fe40] | 967 | // ensure generic parameter instances are renamed like the base type |
---|
| 968 | if ( inGeneric && typeInst->baseType ) typeInst->name = typeInst->baseType->name; |
---|
[ef5b828] | 969 | if ( const NamedTypeDecl * namedTypeDecl = local_indexer->lookupType( typeInst->name ) ) { |
---|
| 970 | if ( const TypeDecl * typeDecl = dynamic_cast< const TypeDecl * >( namedTypeDecl ) ) { |
---|
| 971 | typeInst->set_isFtype( typeDecl->kind == TypeDecl::Ftype ); |
---|
[0dd3a2f] | 972 | } // if |
---|
| 973 | } // if |
---|
[a08ba92] | 974 | } |
---|
[c8ffe20b] | 975 | |
---|
[6e50a6b] | 976 | ResolveEnumInitializers::ResolveEnumInitializers( const Indexer * other_indexer ) : WithIndexer( true ) { |
---|
| 977 | if ( other_indexer ) { |
---|
| 978 | local_indexer = other_indexer; |
---|
| 979 | } else { |
---|
| 980 | local_indexer = &indexer; |
---|
| 981 | } // if |
---|
| 982 | } |
---|
| 983 | |
---|
| 984 | void ResolveEnumInitializers::postvisit( EnumDecl * enumDecl ) { |
---|
| 985 | if ( enumDecl->body ) { |
---|
| 986 | for ( Declaration * member : enumDecl->members ) { |
---|
| 987 | ObjectDecl * field = strict_dynamic_cast<ObjectDecl *>( member ); |
---|
| 988 | if ( field->init ) { |
---|
| 989 | // need to resolve enumerator initializers early so that other passes that determine if an expression is constexpr have the appropriate information. |
---|
| 990 | SingleInit * init = strict_dynamic_cast<SingleInit *>( field->init ); |
---|
| 991 | ResolvExpr::findSingleExpression( init->value, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), indexer ); |
---|
| 992 | } |
---|
| 993 | } |
---|
| 994 | } // if |
---|
| 995 | } |
---|
| 996 | |
---|
[4a9ccc3] | 997 | /// Fix up assertions - flattens assertion lists, removing all trait instances |
---|
[8b11840] | 998 | void forallFixer( std::list< TypeDecl * > & forall, BaseSyntaxNode * node ) { |
---|
| 999 | for ( TypeDecl * type : forall ) { |
---|
[be9036d] | 1000 | std::list< DeclarationWithType * > asserts; |
---|
| 1001 | asserts.splice( asserts.end(), type->assertions ); |
---|
| 1002 | // expand trait instances into their members |
---|
| 1003 | for ( DeclarationWithType * assertion : asserts ) { |
---|
[ef5b828] | 1004 | if ( TraitInstType * traitInst = dynamic_cast< TraitInstType * >( assertion->get_type() ) ) { |
---|
[be9036d] | 1005 | // expand trait instance into all of its members |
---|
| 1006 | expandAssertions( traitInst, back_inserter( type->assertions ) ); |
---|
| 1007 | delete traitInst; |
---|
| 1008 | } else { |
---|
| 1009 | // pass other assertions through |
---|
| 1010 | type->assertions.push_back( assertion ); |
---|
| 1011 | } // if |
---|
| 1012 | } // for |
---|
| 1013 | // apply FixFunction to every assertion to check for invalid void type |
---|
| 1014 | for ( DeclarationWithType *& assertion : type->assertions ) { |
---|
[4bda2cf] | 1015 | bool isVoid = fixFunction( assertion ); |
---|
| 1016 | if ( isVoid ) { |
---|
[a16764a6] | 1017 | SemanticError( node, "invalid type void in assertion of function " ); |
---|
[be9036d] | 1018 | } // if |
---|
| 1019 | } // for |
---|
| 1020 | // normalizeAssertions( type->assertions ); |
---|
[0dd3a2f] | 1021 | } // for |
---|
[a08ba92] | 1022 | } |
---|
[c8ffe20b] | 1023 | |
---|
[9490621] | 1024 | /// Replace all traits in assertion lists with their assertions. |
---|
| 1025 | void expandTraits( std::list< TypeDecl * > & forall ) { |
---|
| 1026 | for ( TypeDecl * type : forall ) { |
---|
| 1027 | std::list< DeclarationWithType * > asserts; |
---|
| 1028 | asserts.splice( asserts.end(), type->assertions ); |
---|
| 1029 | // expand trait instances into their members |
---|
| 1030 | for ( DeclarationWithType * assertion : asserts ) { |
---|
| 1031 | if ( TraitInstType * traitInst = dynamic_cast< TraitInstType * >( assertion->get_type() ) ) { |
---|
| 1032 | // expand trait instance into all of its members |
---|
| 1033 | expandAssertions( traitInst, back_inserter( type->assertions ) ); |
---|
| 1034 | delete traitInst; |
---|
| 1035 | } else { |
---|
| 1036 | // pass other assertions through |
---|
| 1037 | type->assertions.push_back( assertion ); |
---|
| 1038 | } // if |
---|
| 1039 | } // for |
---|
| 1040 | } |
---|
| 1041 | } |
---|
| 1042 | |
---|
| 1043 | /// Fix each function in the assertion list and check for invalid void type. |
---|
| 1044 | void fixAssertions( |
---|
| 1045 | std::list< TypeDecl * > & forall, BaseSyntaxNode * node ) { |
---|
| 1046 | for ( TypeDecl * type : forall ) { |
---|
| 1047 | for ( DeclarationWithType *& assertion : type->assertions ) { |
---|
| 1048 | bool isVoid = fixFunction( assertion ); |
---|
| 1049 | if ( isVoid ) { |
---|
| 1050 | SemanticError( node, "invalid type void in assertion of function " ); |
---|
| 1051 | } // if |
---|
| 1052 | } // for |
---|
| 1053 | } |
---|
| 1054 | } |
---|
| 1055 | |
---|
[ef5b828] | 1056 | void ForallPointerDecay_old::previsit( ObjectDecl * object ) { |
---|
[3d2b7bc] | 1057 | // ensure that operator names only apply to functions or function pointers |
---|
| 1058 | if ( CodeGen::isOperator( object->name ) && ! dynamic_cast< FunctionType * >( object->type->stripDeclarator() ) ) { |
---|
| 1059 | SemanticError( object->location, toCString( "operator ", object->name.c_str(), " is not a function or function pointer." ) ); |
---|
| 1060 | } |
---|
[0dd3a2f] | 1061 | object->fixUniqueId(); |
---|
[a08ba92] | 1062 | } |
---|
[c8ffe20b] | 1063 | |
---|
[ef5b828] | 1064 | void ForallPointerDecay_old::previsit( FunctionDecl * func ) { |
---|
[0dd3a2f] | 1065 | func->fixUniqueId(); |
---|
[a08ba92] | 1066 | } |
---|
[c8ffe20b] | 1067 | |
---|
[c1ed2ee] | 1068 | void ForallPointerDecay_old::previsit( FunctionType * ftype ) { |
---|
[bbf3fda] | 1069 | forallFixer( ftype->forall, ftype ); |
---|
| 1070 | } |
---|
| 1071 | |
---|
[c1ed2ee] | 1072 | void ForallPointerDecay_old::previsit( StructDecl * aggrDecl ) { |
---|
[bd7e609] | 1073 | forallFixer( aggrDecl->parameters, aggrDecl ); |
---|
| 1074 | } |
---|
| 1075 | |
---|
[c1ed2ee] | 1076 | void ForallPointerDecay_old::previsit( UnionDecl * aggrDecl ) { |
---|
[bd7e609] | 1077 | forallFixer( aggrDecl->parameters, aggrDecl ); |
---|
| 1078 | } |
---|
| 1079 | |
---|
[9490621] | 1080 | void TraitExpander_old::previsit( FunctionType * ftype ) { |
---|
| 1081 | expandTraits( ftype->forall ); |
---|
| 1082 | } |
---|
| 1083 | |
---|
| 1084 | void TraitExpander_old::previsit( StructDecl * aggrDecl ) { |
---|
| 1085 | expandTraits( aggrDecl->parameters ); |
---|
| 1086 | } |
---|
| 1087 | |
---|
| 1088 | void TraitExpander_old::previsit( UnionDecl * aggrDecl ) { |
---|
| 1089 | expandTraits( aggrDecl->parameters ); |
---|
| 1090 | } |
---|
| 1091 | |
---|
| 1092 | void AssertionFixer_old::previsit( FunctionType * ftype ) { |
---|
| 1093 | fixAssertions( ftype->forall, ftype ); |
---|
| 1094 | } |
---|
| 1095 | |
---|
| 1096 | void AssertionFixer_old::previsit( StructDecl * aggrDecl ) { |
---|
| 1097 | fixAssertions( aggrDecl->parameters, aggrDecl ); |
---|
| 1098 | } |
---|
| 1099 | |
---|
| 1100 | void AssertionFixer_old::previsit( UnionDecl * aggrDecl ) { |
---|
| 1101 | fixAssertions( aggrDecl->parameters, aggrDecl ); |
---|
| 1102 | } |
---|
| 1103 | |
---|
| 1104 | void CheckOperatorTypes_old::previsit( ObjectDecl * object ) { |
---|
| 1105 | // ensure that operator names only apply to functions or function pointers |
---|
| 1106 | if ( CodeGen::isOperator( object->name ) && ! dynamic_cast< FunctionType * >( object->type->stripDeclarator() ) ) { |
---|
| 1107 | SemanticError( object->location, toCString( "operator ", object->name.c_str(), " is not a function or function pointer." ) ); |
---|
| 1108 | } |
---|
| 1109 | } |
---|
| 1110 | |
---|
| 1111 | void FixUniqueIds_old::previsit( DeclarationWithType * decl ) { |
---|
| 1112 | decl->fixUniqueId(); |
---|
| 1113 | } |
---|
| 1114 | |
---|
[de91427b] | 1115 | void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) { |
---|
[0db6fc0] | 1116 | PassVisitor<ReturnChecker> checker; |
---|
[de91427b] | 1117 | acceptAll( translationUnit, checker ); |
---|
| 1118 | } |
---|
| 1119 | |
---|
[0db6fc0] | 1120 | void ReturnChecker::previsit( FunctionDecl * functionDecl ) { |
---|
[0508ab3] | 1121 | GuardValue( returnVals ); |
---|
[de91427b] | 1122 | returnVals = functionDecl->get_functionType()->get_returnVals(); |
---|
| 1123 | } |
---|
| 1124 | |
---|
[0db6fc0] | 1125 | void ReturnChecker::previsit( ReturnStmt * returnStmt ) { |
---|
[74d1804] | 1126 | // Previously this also checked for the existence of an expr paired with no return values on |
---|
| 1127 | // the function return type. This is incorrect, since you can have an expression attached to |
---|
| 1128 | // a return statement in a void-returning function in C. The expression is treated as if it |
---|
| 1129 | // were cast to void. |
---|
[30f9072] | 1130 | if ( ! returnStmt->get_expr() && returnVals.size() != 0 ) { |
---|
[a16764a6] | 1131 | SemanticError( returnStmt, "Non-void function returns no values: " ); |
---|
[de91427b] | 1132 | } |
---|
| 1133 | } |
---|
| 1134 | |
---|
| 1135 | |
---|
[48ed81c] | 1136 | void ReplaceTypedef::replaceTypedef( std::list< Declaration * > &translationUnit ) { |
---|
| 1137 | PassVisitor<ReplaceTypedef> eliminator; |
---|
[0dd3a2f] | 1138 | mutateAll( translationUnit, eliminator ); |
---|
[a506df4] | 1139 | if ( eliminator.pass.typedefNames.count( "size_t" ) ) { |
---|
[5f98ce5] | 1140 | // grab and remember declaration of size_t |
---|
[2bfc6b2] | 1141 | Validate::SizeType = eliminator.pass.typedefNames["size_t"].first->base->clone(); |
---|
[5f98ce5] | 1142 | } else { |
---|
[40e636a] | 1143 | // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong |
---|
| 1144 | // eventually should have a warning for this case. |
---|
[2bfc6b2] | 1145 | Validate::SizeType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt ); |
---|
[5f98ce5] | 1146 | } |
---|
[a08ba92] | 1147 | } |
---|
[c8ffe20b] | 1148 | |
---|
[48ed81c] | 1149 | void ReplaceTypedef::premutate( QualifiedType * ) { |
---|
| 1150 | visit_children = false; |
---|
| 1151 | } |
---|
| 1152 | |
---|
| 1153 | Type * ReplaceTypedef::postmutate( QualifiedType * qualType ) { |
---|
| 1154 | // replacing typedefs only makes sense for the 'oldest ancestor' of the qualified type |
---|
[ef5b828] | 1155 | qualType->parent = qualType->parent->acceptMutator( * visitor ); |
---|
[48ed81c] | 1156 | return qualType; |
---|
| 1157 | } |
---|
| 1158 | |
---|
[a7c31e0] | 1159 | static bool isNonParameterAttribute( Attribute * attr ) { |
---|
| 1160 | static const std::vector<std::string> bad_names = { |
---|
| 1161 | "aligned", "__aligned__", |
---|
| 1162 | }; |
---|
| 1163 | for ( auto name : bad_names ) { |
---|
| 1164 | if ( name == attr->name ) { |
---|
| 1165 | return true; |
---|
| 1166 | } |
---|
| 1167 | } |
---|
| 1168 | return false; |
---|
| 1169 | } |
---|
| 1170 | |
---|
[48ed81c] | 1171 | Type * ReplaceTypedef::postmutate( TypeInstType * typeInst ) { |
---|
[9cb8e88d] | 1172 | // instances of typedef types will come here. If it is an instance |
---|
[cc79d97] | 1173 | // of a typdef type, link the instance to its actual type. |
---|
[0b3b2ae] | 1174 | TypedefMap::const_iterator def = typedefNames.find( typeInst->name ); |
---|
[0dd3a2f] | 1175 | if ( def != typedefNames.end() ) { |
---|
[ef5b828] | 1176 | Type * ret = def->second.first->base->clone(); |
---|
[e82ef13] | 1177 | ret->location = typeInst->location; |
---|
[6f95000] | 1178 | ret->get_qualifiers() |= typeInst->get_qualifiers(); |
---|
[a7c31e0] | 1179 | // GCC ignores certain attributes if they arrive by typedef, this mimics that. |
---|
| 1180 | if ( inFunctionType ) { |
---|
| 1181 | ret->attributes.remove_if( isNonParameterAttribute ); |
---|
[1f370451] | 1182 | } |
---|
[a7c31e0] | 1183 | ret->attributes.splice( ret->attributes.end(), typeInst->attributes ); |
---|
[0215a76f] | 1184 | // place instance parameters on the typedef'd type |
---|
[f53836b] | 1185 | if ( ! typeInst->parameters.empty() ) { |
---|
[ef5b828] | 1186 | ReferenceToType * rtt = dynamic_cast<ReferenceToType *>(ret); |
---|
[0215a76f] | 1187 | if ( ! rtt ) { |
---|
[a16764a6] | 1188 | SemanticError( typeInst->location, "Cannot apply type parameters to base type of " + typeInst->name ); |
---|
[0215a76f] | 1189 | } |
---|
[0b3b2ae] | 1190 | rtt->parameters.clear(); |
---|
[f53836b] | 1191 | cloneAll( typeInst->parameters, rtt->parameters ); |
---|
[ef5b828] | 1192 | mutateAll( rtt->parameters, * visitor ); // recursively fix typedefs on parameters |
---|
[1db21619] | 1193 | } // if |
---|
[0dd3a2f] | 1194 | delete typeInst; |
---|
| 1195 | return ret; |
---|
[679864e1] | 1196 | } else { |
---|
[0b3b2ae] | 1197 | TypeDeclMap::const_iterator base = typedeclNames.find( typeInst->name ); |
---|
[062e8df] | 1198 | if ( base == typedeclNames.end() ) { |
---|
| 1199 | SemanticError( typeInst->location, toString("Use of undefined type ", typeInst->name) ); |
---|
| 1200 | } |
---|
[1e8b02f5] | 1201 | typeInst->set_baseType( base->second ); |
---|
[062e8df] | 1202 | return typeInst; |
---|
[0dd3a2f] | 1203 | } // if |
---|
[062e8df] | 1204 | assert( false ); |
---|
[a08ba92] | 1205 | } |
---|
[c8ffe20b] | 1206 | |
---|
[f53836b] | 1207 | struct VarLenChecker : WithShortCircuiting { |
---|
| 1208 | void previsit( FunctionType * ) { visit_children = false; } |
---|
| 1209 | void previsit( ArrayType * at ) { |
---|
| 1210 | isVarLen |= at->isVarLen; |
---|
| 1211 | } |
---|
| 1212 | bool isVarLen = false; |
---|
| 1213 | }; |
---|
| 1214 | |
---|
| 1215 | bool isVariableLength( Type * t ) { |
---|
| 1216 | PassVisitor<VarLenChecker> varLenChecker; |
---|
| 1217 | maybeAccept( t, varLenChecker ); |
---|
| 1218 | return varLenChecker.pass.isVarLen; |
---|
| 1219 | } |
---|
| 1220 | |
---|
[48ed81c] | 1221 | Declaration * ReplaceTypedef::postmutate( TypedefDecl * tyDecl ) { |
---|
[0b3b2ae] | 1222 | if ( typedefNames.count( tyDecl->name ) == 1 && typedefNames[ tyDecl->name ].second == scopeLevel ) { |
---|
[9cb8e88d] | 1223 | // typedef to the same name from the same scope |
---|
[cc79d97] | 1224 | // must be from the same type |
---|
| 1225 | |
---|
[0b3b2ae] | 1226 | Type * t1 = tyDecl->base; |
---|
| 1227 | Type * t2 = typedefNames[ tyDecl->name ].first->base; |
---|
[1cbca6e] | 1228 | if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) { |
---|
[a16764a6] | 1229 | SemanticError( tyDecl->location, "Cannot redefine typedef: " + tyDecl->name ); |
---|
[f53836b] | 1230 | } |
---|
[4b97770] | 1231 | // Cannot redefine VLA typedefs. Note: this is slightly incorrect, because our notion of VLAs |
---|
| 1232 | // at this point in the translator is imprecise. In particular, this will disallow redefining typedefs |
---|
| 1233 | // with arrays whose dimension is an enumerator or a cast of a constant/enumerator. The effort required |
---|
| 1234 | // to fix this corner case likely outweighs the utility of allowing it. |
---|
[f53836b] | 1235 | if ( isVariableLength( t1 ) || isVariableLength( t2 ) ) { |
---|
[a16764a6] | 1236 | SemanticError( tyDecl->location, "Cannot redefine typedef: " + tyDecl->name ); |
---|
[85c4ef0] | 1237 | } |
---|
[cc79d97] | 1238 | } else { |
---|
[0b3b2ae] | 1239 | typedefNames[ tyDecl->name ] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel ); |
---|
[cc79d97] | 1240 | } // if |
---|
| 1241 | |
---|
[0dd3a2f] | 1242 | // When a typedef is a forward declaration: |
---|
| 1243 | // typedef struct screen SCREEN; |
---|
| 1244 | // the declaration portion must be retained: |
---|
| 1245 | // struct screen; |
---|
| 1246 | // because the expansion of the typedef is: |
---|
[ef5b828] | 1247 | // void rtn( SCREEN * p ) => void rtn( struct screen * p ) |
---|
[0dd3a2f] | 1248 | // hence the type-name "screen" must be defined. |
---|
| 1249 | // Note, qualifiers on the typedef are superfluous for the forward declaration. |
---|
[6f95000] | 1250 | |
---|
[ef5b828] | 1251 | Type * designatorType = tyDecl->base->stripDeclarator(); |
---|
| 1252 | if ( StructInstType * aggDecl = dynamic_cast< StructInstType * >( designatorType ) ) { |
---|
[312029a] | 1253 | declsToAddBefore.push_back( new StructDecl( aggDecl->name, AggregateDecl::Struct, noAttributes, tyDecl->linkage ) ); |
---|
[ef5b828] | 1254 | } else if ( UnionInstType * aggDecl = dynamic_cast< UnionInstType * >( designatorType ) ) { |
---|
[48ed81c] | 1255 | declsToAddBefore.push_back( new UnionDecl( aggDecl->name, noAttributes, tyDecl->linkage ) ); |
---|
[ef5b828] | 1256 | } else if ( EnumInstType * enumDecl = dynamic_cast< EnumInstType * >( designatorType ) ) { |
---|
[48ed81c] | 1257 | declsToAddBefore.push_back( new EnumDecl( enumDecl->name, noAttributes, tyDecl->linkage ) ); |
---|
[0dd3a2f] | 1258 | } // if |
---|
[48ed81c] | 1259 | return tyDecl->clone(); |
---|
[a08ba92] | 1260 | } |
---|
[c8ffe20b] | 1261 | |
---|
[48ed81c] | 1262 | void ReplaceTypedef::premutate( TypeDecl * typeDecl ) { |
---|
[0b3b2ae] | 1263 | TypedefMap::iterator i = typedefNames.find( typeDecl->name ); |
---|
[0dd3a2f] | 1264 | if ( i != typedefNames.end() ) { |
---|
| 1265 | typedefNames.erase( i ) ; |
---|
| 1266 | } // if |
---|
[679864e1] | 1267 | |
---|
[0bcc2b7] | 1268 | typedeclNames.insert( typeDecl->name, typeDecl ); |
---|
[a08ba92] | 1269 | } |
---|
[c8ffe20b] | 1270 | |
---|
[48ed81c] | 1271 | void ReplaceTypedef::premutate( FunctionDecl * ) { |
---|
[a506df4] | 1272 | GuardScope( typedefNames ); |
---|
[0bcc2b7] | 1273 | GuardScope( typedeclNames ); |
---|
[a08ba92] | 1274 | } |
---|
[c8ffe20b] | 1275 | |
---|
[48ed81c] | 1276 | void ReplaceTypedef::premutate( ObjectDecl * ) { |
---|
[a506df4] | 1277 | GuardScope( typedefNames ); |
---|
[0bcc2b7] | 1278 | GuardScope( typedeclNames ); |
---|
[a506df4] | 1279 | } |
---|
[dd020c0] | 1280 | |
---|
[48ed81c] | 1281 | DeclarationWithType * ReplaceTypedef::postmutate( ObjectDecl * objDecl ) { |
---|
[ef5b828] | 1282 | if ( FunctionType * funtype = dynamic_cast<FunctionType *>( objDecl->type ) ) { // function type? |
---|
[02e5ab6] | 1283 | // replace the current object declaration with a function declaration |
---|
[0b3b2ae] | 1284 | FunctionDecl * newDecl = new FunctionDecl( objDecl->name, objDecl->get_storageClasses(), objDecl->linkage, funtype, 0, objDecl->attributes, objDecl->get_funcSpec() ); |
---|
| 1285 | objDecl->attributes.clear(); |
---|
[dbe8f244] | 1286 | objDecl->set_type( nullptr ); |
---|
[0a86a30] | 1287 | delete objDecl; |
---|
| 1288 | return newDecl; |
---|
[1db21619] | 1289 | } // if |
---|
[a506df4] | 1290 | return objDecl; |
---|
[a08ba92] | 1291 | } |
---|
[c8ffe20b] | 1292 | |
---|
[48ed81c] | 1293 | void ReplaceTypedef::premutate( CastExpr * ) { |
---|
[a506df4] | 1294 | GuardScope( typedefNames ); |
---|
[0bcc2b7] | 1295 | GuardScope( typedeclNames ); |
---|
[a08ba92] | 1296 | } |
---|
[c8ffe20b] | 1297 | |
---|
[48ed81c] | 1298 | void ReplaceTypedef::premutate( CompoundStmt * ) { |
---|
[a506df4] | 1299 | GuardScope( typedefNames ); |
---|
[0bcc2b7] | 1300 | GuardScope( typedeclNames ); |
---|
[cc79d97] | 1301 | scopeLevel += 1; |
---|
[a506df4] | 1302 | GuardAction( [this](){ scopeLevel -= 1; } ); |
---|
| 1303 | } |
---|
| 1304 | |
---|
[45161b4d] | 1305 | template<typename AggDecl> |
---|
[48ed81c] | 1306 | void ReplaceTypedef::addImplicitTypedef( AggDecl * aggDecl ) { |
---|
[45161b4d] | 1307 | if ( typedefNames.count( aggDecl->get_name() ) == 0 ) { |
---|
[ef5b828] | 1308 | Type * type = nullptr; |
---|
[45161b4d] | 1309 | if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( aggDecl ) ) { |
---|
| 1310 | type = new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() ); |
---|
| 1311 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( aggDecl ) ) { |
---|
| 1312 | type = new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() ); |
---|
| 1313 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( aggDecl ) ) { |
---|
| 1314 | type = new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() ); |
---|
| 1315 | } // if |
---|
[0b0f1dd] | 1316 | TypedefDeclPtr tyDecl( new TypedefDecl( aggDecl->get_name(), aggDecl->location, Type::StorageClasses(), type, aggDecl->get_linkage() ) ); |
---|
[46f6134] | 1317 | typedefNames[ aggDecl->get_name() ] = std::make_pair( std::move( tyDecl ), scopeLevel ); |
---|
[48ed81c] | 1318 | // add the implicit typedef to the AST |
---|
| 1319 | declsToAddBefore.push_back( new TypedefDecl( aggDecl->get_name(), aggDecl->location, Type::StorageClasses(), type->clone(), aggDecl->get_linkage() ) ); |
---|
[45161b4d] | 1320 | } // if |
---|
| 1321 | } |
---|
[4e06c1e] | 1322 | |
---|
[48ed81c] | 1323 | template< typename AggDecl > |
---|
| 1324 | void ReplaceTypedef::handleAggregate( AggDecl * aggr ) { |
---|
| 1325 | SemanticErrorException errors; |
---|
[a506df4] | 1326 | |
---|
[48ed81c] | 1327 | ValueGuard< std::list<Declaration * > > oldBeforeDecls( declsToAddBefore ); |
---|
| 1328 | ValueGuard< std::list<Declaration * > > oldAfterDecls ( declsToAddAfter ); |
---|
| 1329 | declsToAddBefore.clear(); |
---|
| 1330 | declsToAddAfter.clear(); |
---|
[a506df4] | 1331 | |
---|
[48ed81c] | 1332 | GuardScope( typedefNames ); |
---|
[0bcc2b7] | 1333 | GuardScope( typedeclNames ); |
---|
[ef5b828] | 1334 | mutateAll( aggr->parameters, * visitor ); |
---|
[798a8b3] | 1335 | mutateAll( aggr->attributes, * visitor ); |
---|
[85c4ef0] | 1336 | |
---|
[48ed81c] | 1337 | // unroll mutateAll for aggr->members so that implicit typedefs for nested types are added to the aggregate body. |
---|
| 1338 | for ( std::list< Declaration * >::iterator i = aggr->members.begin(); i != aggr->members.end(); ++i ) { |
---|
| 1339 | if ( !declsToAddAfter.empty() ) { aggr->members.splice( i, declsToAddAfter ); } |
---|
[a506df4] | 1340 | |
---|
[48ed81c] | 1341 | try { |
---|
[ef5b828] | 1342 | * i = maybeMutate( * i, * visitor ); |
---|
[48ed81c] | 1343 | } catch ( SemanticErrorException &e ) { |
---|
| 1344 | errors.append( e ); |
---|
| 1345 | } |
---|
| 1346 | |
---|
| 1347 | if ( !declsToAddBefore.empty() ) { aggr->members.splice( i, declsToAddBefore ); } |
---|
| 1348 | } |
---|
| 1349 | |
---|
| 1350 | if ( !declsToAddAfter.empty() ) { aggr->members.splice( aggr->members.end(), declsToAddAfter ); } |
---|
| 1351 | if ( !errors.isEmpty() ) { throw errors; } |
---|
[85c4ef0] | 1352 | } |
---|
| 1353 | |
---|
[48ed81c] | 1354 | void ReplaceTypedef::premutate( StructDecl * structDecl ) { |
---|
| 1355 | visit_children = false; |
---|
| 1356 | addImplicitTypedef( structDecl ); |
---|
| 1357 | handleAggregate( structDecl ); |
---|
[a506df4] | 1358 | } |
---|
| 1359 | |
---|
[48ed81c] | 1360 | void ReplaceTypedef::premutate( UnionDecl * unionDecl ) { |
---|
| 1361 | visit_children = false; |
---|
| 1362 | addImplicitTypedef( unionDecl ); |
---|
| 1363 | handleAggregate( unionDecl ); |
---|
[85c4ef0] | 1364 | } |
---|
| 1365 | |
---|
[48ed81c] | 1366 | void ReplaceTypedef::premutate( EnumDecl * enumDecl ) { |
---|
| 1367 | addImplicitTypedef( enumDecl ); |
---|
[85c4ef0] | 1368 | } |
---|
| 1369 | |
---|
[48ed81c] | 1370 | void ReplaceTypedef::premutate( FunctionType * ) { |
---|
[1f370451] | 1371 | GuardValue( inFunctionType ); |
---|
| 1372 | inFunctionType = true; |
---|
| 1373 | } |
---|
| 1374 | |
---|
[0bcc2b7] | 1375 | void ReplaceTypedef::premutate( TraitDecl * ) { |
---|
| 1376 | GuardScope( typedefNames ); |
---|
| 1377 | GuardScope( typedeclNames); |
---|
| 1378 | } |
---|
| 1379 | |
---|
[d1969a6] | 1380 | void VerifyCtorDtorAssign::verify( std::list< Declaration * > & translationUnit ) { |
---|
[0db6fc0] | 1381 | PassVisitor<VerifyCtorDtorAssign> verifier; |
---|
[9cb8e88d] | 1382 | acceptAll( translationUnit, verifier ); |
---|
| 1383 | } |
---|
| 1384 | |
---|
[0db6fc0] | 1385 | void VerifyCtorDtorAssign::previsit( FunctionDecl * funcDecl ) { |
---|
[9cb8e88d] | 1386 | FunctionType * funcType = funcDecl->get_functionType(); |
---|
| 1387 | std::list< DeclarationWithType * > &returnVals = funcType->get_returnVals(); |
---|
| 1388 | std::list< DeclarationWithType * > ¶ms = funcType->get_parameters(); |
---|
| 1389 | |
---|
[bff227f] | 1390 | if ( CodeGen::isCtorDtorAssign( funcDecl->get_name() ) ) { // TODO: also check /=, etc. |
---|
[9cb8e88d] | 1391 | if ( params.size() == 0 ) { |
---|
[98538288] | 1392 | SemanticError( funcDecl->location, "Constructors, destructors, and assignment functions require at least one parameter." ); |
---|
[9cb8e88d] | 1393 | } |
---|
[ce8c12f] | 1394 | ReferenceType * refType = dynamic_cast< ReferenceType * >( params.front()->get_type() ); |
---|
[084fecc] | 1395 | if ( ! refType ) { |
---|
[98538288] | 1396 | SemanticError( funcDecl->location, "First parameter of a constructor, destructor, or assignment function must be a reference." ); |
---|
[9cb8e88d] | 1397 | } |
---|
[bff227f] | 1398 | if ( CodeGen::isCtorDtor( funcDecl->get_name() ) && returnVals.size() != 0 ) { |
---|
[98538288] | 1399 | if(!returnVals.front()->get_type()->isVoid()) { |
---|
| 1400 | SemanticError( funcDecl->location, "Constructors and destructors cannot have explicit return values." ); |
---|
| 1401 | } |
---|
[9cb8e88d] | 1402 | } |
---|
| 1403 | } |
---|
| 1404 | } |
---|
[70a06f6] | 1405 | |
---|
[6e50a6b] | 1406 | // Test for special name on a generic parameter. Special treatment for the |
---|
| 1407 | // special name is a bootstrapping hack. In most cases, the worlds of T's |
---|
| 1408 | // and of N's don't overlap (normal treamtemt). The foundations in |
---|
| 1409 | // array.hfa use tagging for both types and dimensions. Tagging treats |
---|
| 1410 | // its subject parameter even more opaquely than T&, which assumes it is |
---|
| 1411 | // possible to have a pointer/reference to such an object. Tagging only |
---|
| 1412 | // seeks to identify the type-system resident at compile time. Both N's |
---|
| 1413 | // and T's can make tags. The tag definition uses the special name, which |
---|
| 1414 | // is treated as "an N or a T." This feature is not inteded to be used |
---|
| 1415 | // outside of the definition and immediate uses of a tag. |
---|
| 1416 | static inline bool isReservedTysysIdOnlyName( const std::string & name ) { |
---|
| 1417 | // name's prefix was __CFA_tysys_id_only, before it got wrapped in __..._generic |
---|
| 1418 | int foundAt = name.find("__CFA_tysys_id_only"); |
---|
| 1419 | if (foundAt == 0) return true; |
---|
| 1420 | if (foundAt == 2 && name[0] == '_' && name[1] == '_') return true; |
---|
| 1421 | return false; |
---|
| 1422 | } |
---|
| 1423 | |
---|
[11ab8ea8] | 1424 | template< typename Aggr > |
---|
| 1425 | void validateGeneric( Aggr * inst ) { |
---|
| 1426 | std::list< TypeDecl * > * params = inst->get_baseParameters(); |
---|
[30f9072] | 1427 | if ( params ) { |
---|
[11ab8ea8] | 1428 | std::list< Expression * > & args = inst->get_parameters(); |
---|
[67cf18c] | 1429 | |
---|
| 1430 | // insert defaults arguments when a type argument is missing (currently only supports missing arguments at the end of the list). |
---|
| 1431 | // A substitution is used to ensure that defaults are replaced correctly, e.g., |
---|
| 1432 | // forall(otype T, otype alloc = heap_allocator(T)) struct vector; |
---|
| 1433 | // vector(int) v; |
---|
| 1434 | // after insertion of default values becomes |
---|
| 1435 | // vector(int, heap_allocator(T)) |
---|
| 1436 | // and the substitution is built with T=int so that after substitution, the result is |
---|
| 1437 | // vector(int, heap_allocator(int)) |
---|
| 1438 | TypeSubstitution sub; |
---|
| 1439 | auto paramIter = params->begin(); |
---|
[6e50a6b] | 1440 | auto argIter = args.begin(); |
---|
| 1441 | for ( ; paramIter != params->end(); ++paramIter, ++argIter ) { |
---|
| 1442 | if ( argIter != args.end() ) { |
---|
| 1443 | TypeExpr * expr = dynamic_cast< TypeExpr * >( * argIter ); |
---|
| 1444 | if ( expr ) { |
---|
| 1445 | sub.add( (* paramIter)->get_name(), expr->get_type()->clone() ); |
---|
| 1446 | } |
---|
| 1447 | } else { |
---|
[ef5b828] | 1448 | Type * defaultType = (* paramIter)->get_init(); |
---|
[67cf18c] | 1449 | if ( defaultType ) { |
---|
| 1450 | args.push_back( new TypeExpr( defaultType->clone() ) ); |
---|
[ef5b828] | 1451 | sub.add( (* paramIter)->get_name(), defaultType->clone() ); |
---|
[6e50a6b] | 1452 | argIter = std::prev(args.end()); |
---|
| 1453 | } else { |
---|
| 1454 | SemanticError( inst, "Too few type arguments in generic type " ); |
---|
[67cf18c] | 1455 | } |
---|
| 1456 | } |
---|
[6e50a6b] | 1457 | assert( argIter != args.end() ); |
---|
| 1458 | bool typeParamDeclared = (*paramIter)->kind != TypeDecl::Kind::Dimension; |
---|
| 1459 | bool typeArgGiven; |
---|
| 1460 | if ( isReservedTysysIdOnlyName( (*paramIter)->name ) ) { |
---|
| 1461 | // coerce a match when declaration is reserved name, which means "either" |
---|
| 1462 | typeArgGiven = typeParamDeclared; |
---|
| 1463 | } else { |
---|
| 1464 | typeArgGiven = dynamic_cast< TypeExpr * >( * argIter ); |
---|
| 1465 | } |
---|
| 1466 | if ( ! typeParamDeclared && typeArgGiven ) SemanticError( inst, "Type argument given for value parameter: " ); |
---|
| 1467 | if ( typeParamDeclared && ! typeArgGiven ) SemanticError( inst, "Expression argument given for type parameter: " ); |
---|
[67cf18c] | 1468 | } |
---|
| 1469 | |
---|
| 1470 | sub.apply( inst ); |
---|
[a16764a6] | 1471 | if ( args.size() > params->size() ) SemanticError( inst, "Too many type arguments in generic type " ); |
---|
[11ab8ea8] | 1472 | } |
---|
| 1473 | } |
---|
| 1474 | |
---|
[0db6fc0] | 1475 | void ValidateGenericParameters::previsit( StructInstType * inst ) { |
---|
[11ab8ea8] | 1476 | validateGeneric( inst ); |
---|
| 1477 | } |
---|
[9cb8e88d] | 1478 | |
---|
[0db6fc0] | 1479 | void ValidateGenericParameters::previsit( UnionInstType * inst ) { |
---|
[11ab8ea8] | 1480 | validateGeneric( inst ); |
---|
[9cb8e88d] | 1481 | } |
---|
[70a06f6] | 1482 | |
---|
[6e50a6b] | 1483 | void TranslateDimensionGenericParameters::translateDimensions( std::list< Declaration * > &translationUnit ) { |
---|
| 1484 | PassVisitor<TranslateDimensionGenericParameters> translator; |
---|
| 1485 | mutateAll( translationUnit, translator ); |
---|
| 1486 | } |
---|
| 1487 | |
---|
| 1488 | TranslateDimensionGenericParameters::TranslateDimensionGenericParameters() : WithIndexer( false ) {} |
---|
| 1489 | |
---|
| 1490 | // Declaration of type variable: forall( [N] ) -> forall( N & | sized( N ) ) |
---|
| 1491 | TypeDecl * TranslateDimensionGenericParameters::postmutate( TypeDecl * td ) { |
---|
| 1492 | if ( td->kind == TypeDecl::Dimension ) { |
---|
| 1493 | td->kind = TypeDecl::Dtype; |
---|
| 1494 | if ( ! isReservedTysysIdOnlyName( td->name ) ) { |
---|
| 1495 | td->sized = true; |
---|
| 1496 | } |
---|
| 1497 | } |
---|
| 1498 | return td; |
---|
| 1499 | } |
---|
| 1500 | |
---|
| 1501 | // Situational awareness: |
---|
| 1502 | // array( float, [[currentExpr]] ) has visitingChildOfSUIT == true |
---|
| 1503 | // array( float, [[currentExpr]] - 1 ) has visitingChildOfSUIT == false |
---|
| 1504 | // size_t x = [[currentExpr]] has visitingChildOfSUIT == false |
---|
| 1505 | void TranslateDimensionGenericParameters::changeState_ChildOfSUIT( bool newVal ) { |
---|
| 1506 | GuardValue( nextVisitedNodeIsChildOfSUIT ); |
---|
| 1507 | GuardValue( visitingChildOfSUIT ); |
---|
| 1508 | visitingChildOfSUIT = nextVisitedNodeIsChildOfSUIT; |
---|
| 1509 | nextVisitedNodeIsChildOfSUIT = newVal; |
---|
| 1510 | } |
---|
| 1511 | void TranslateDimensionGenericParameters::premutate( StructInstType * sit ) { |
---|
| 1512 | (void) sit; |
---|
| 1513 | changeState_ChildOfSUIT(true); |
---|
| 1514 | } |
---|
| 1515 | void TranslateDimensionGenericParameters::premutate( UnionInstType * uit ) { |
---|
| 1516 | (void) uit; |
---|
| 1517 | changeState_ChildOfSUIT(true); |
---|
| 1518 | } |
---|
| 1519 | void TranslateDimensionGenericParameters::premutate( BaseSyntaxNode * node ) { |
---|
| 1520 | (void) node; |
---|
| 1521 | changeState_ChildOfSUIT(false); |
---|
| 1522 | } |
---|
| 1523 | |
---|
| 1524 | // Passing values as dimension arguments: array( float, 7 ) -> array( float, char[ 7 ] ) |
---|
| 1525 | // Consuming dimension parameters: size_t x = N - 1 ; -> size_t x = sizeof(N) - 1 ; |
---|
| 1526 | // Intertwined reality: array( float, N ) -> array( float, N ) |
---|
| 1527 | // array( float, N - 1 ) -> array( float, char[ sizeof(N) - 1 ] ) |
---|
| 1528 | // Intertwined case 1 is not just an optimization. |
---|
| 1529 | // Avoiding char[sizeof(-)] is necessary to enable the call of f to bind the value of N, in: |
---|
| 1530 | // forall([N]) void f( array(float, N) & ); |
---|
| 1531 | // array(float, 7) a; |
---|
| 1532 | // f(a); |
---|
| 1533 | |
---|
| 1534 | Expression * TranslateDimensionGenericParameters::postmutate( DimensionExpr * de ) { |
---|
| 1535 | // Expression de is an occurrence of N in LHS of above examples. |
---|
| 1536 | // Look up the name that de references. |
---|
| 1537 | // If we are in a struct body, then this reference can be to an entry of the stuct's forall list. |
---|
| 1538 | // Whether or not we are in a struct body, this reference can be to an entry of a containing function's forall list. |
---|
| 1539 | // If we are in a struct body, then the stuct's forall declarations are innermost (functions don't occur in structs). |
---|
| 1540 | // Thus, a potential struct's declaration is highest priority. |
---|
| 1541 | // A struct's forall declarations are already renamed with _generic_ suffix. Try that name variant first. |
---|
| 1542 | |
---|
| 1543 | std::string useName = "__" + de->name + "_generic_"; |
---|
| 1544 | TypeDecl * namedParamDecl = const_cast<TypeDecl *>( strict_dynamic_cast<const TypeDecl *, nullptr >( indexer.lookupType( useName ) ) ); |
---|
| 1545 | |
---|
| 1546 | if ( ! namedParamDecl ) { |
---|
| 1547 | useName = de->name; |
---|
| 1548 | namedParamDecl = const_cast<TypeDecl *>( strict_dynamic_cast<const TypeDecl *, nullptr >( indexer.lookupType( useName ) ) ); |
---|
| 1549 | } |
---|
| 1550 | |
---|
| 1551 | // Expect to find it always. A misspelled name would have been parsed as an identifier. |
---|
| 1552 | assert( namedParamDecl && "Type-system-managed value name not found in symbol table" ); |
---|
| 1553 | |
---|
| 1554 | delete de; |
---|
| 1555 | |
---|
| 1556 | TypeInstType * refToDecl = new TypeInstType( 0, useName, namedParamDecl ); |
---|
| 1557 | |
---|
| 1558 | if ( visitingChildOfSUIT ) { |
---|
| 1559 | // As in postmutate( Expression * ), topmost expression needs a TypeExpr wrapper |
---|
| 1560 | // But avoid ArrayType-Sizeof |
---|
| 1561 | return new TypeExpr( refToDecl ); |
---|
| 1562 | } else { |
---|
| 1563 | // the N occurrence is being used directly as a runtime value, |
---|
| 1564 | // if we are in a type instantiation, then the N is within a bigger value computation |
---|
| 1565 | return new SizeofExpr( refToDecl ); |
---|
| 1566 | } |
---|
| 1567 | } |
---|
| 1568 | |
---|
| 1569 | Expression * TranslateDimensionGenericParameters::postmutate( Expression * e ) { |
---|
| 1570 | if ( visitingChildOfSUIT ) { |
---|
| 1571 | // e is an expression used as an argument to instantiate a type |
---|
| 1572 | if (! dynamic_cast< TypeExpr * >( e ) ) { |
---|
| 1573 | // e is a value expression |
---|
| 1574 | // but not a DimensionExpr, which has a distinct postmutate |
---|
| 1575 | Type * typeExprContent = new ArrayType( 0, new BasicType( 0, BasicType::Char ), e, true, false ); |
---|
| 1576 | TypeExpr * result = new TypeExpr( typeExprContent ); |
---|
| 1577 | return result; |
---|
| 1578 | } |
---|
| 1579 | } |
---|
| 1580 | return e; |
---|
| 1581 | } |
---|
| 1582 | |
---|
[ef5b828] | 1583 | void CompoundLiteral::premutate( ObjectDecl * objectDecl ) { |
---|
[a7c90d4] | 1584 | storageClasses = objectDecl->get_storageClasses(); |
---|
[630a82a] | 1585 | } |
---|
| 1586 | |
---|
[ef5b828] | 1587 | Expression * CompoundLiteral::postmutate( CompoundLiteralExpr * compLitExpr ) { |
---|
[630a82a] | 1588 | // transform [storage_class] ... (struct S){ 3, ... }; |
---|
| 1589 | // into [storage_class] struct S temp = { 3, ... }; |
---|
| 1590 | static UniqueName indexName( "_compLit" ); |
---|
| 1591 | |
---|
[ef5b828] | 1592 | ObjectDecl * tempvar = new ObjectDecl( indexName.newName(), storageClasses, LinkageSpec::C, nullptr, compLitExpr->get_result(), compLitExpr->get_initializer() ); |
---|
[d24d4e1] | 1593 | compLitExpr->set_result( nullptr ); |
---|
| 1594 | compLitExpr->set_initializer( nullptr ); |
---|
[630a82a] | 1595 | delete compLitExpr; |
---|
[d24d4e1] | 1596 | declsToAddBefore.push_back( tempvar ); // add modified temporary to current block |
---|
| 1597 | return new VariableExpr( tempvar ); |
---|
[630a82a] | 1598 | } |
---|
[cce9429] | 1599 | |
---|
| 1600 | void ReturnTypeFixer::fix( std::list< Declaration * > &translationUnit ) { |
---|
[0db6fc0] | 1601 | PassVisitor<ReturnTypeFixer> fixer; |
---|
[cce9429] | 1602 | acceptAll( translationUnit, fixer ); |
---|
| 1603 | } |
---|
| 1604 | |
---|
[0db6fc0] | 1605 | void ReturnTypeFixer::postvisit( FunctionDecl * functionDecl ) { |
---|
[9facf3b] | 1606 | FunctionType * ftype = functionDecl->get_functionType(); |
---|
| 1607 | std::list< DeclarationWithType * > & retVals = ftype->get_returnVals(); |
---|
[56e49b0] | 1608 | assertf( retVals.size() == 0 || retVals.size() == 1, "Function %s has too many return values: %zu", functionDecl->get_name().c_str(), retVals.size() ); |
---|
[9facf3b] | 1609 | if ( retVals.size() == 1 ) { |
---|
[861799c] | 1610 | // 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). |
---|
| 1611 | // ensure other return values have a name. |
---|
[9facf3b] | 1612 | DeclarationWithType * ret = retVals.front(); |
---|
| 1613 | if ( ret->get_name() == "" ) { |
---|
| 1614 | ret->set_name( toString( "_retval_", CodeGen::genName( functionDecl ) ) ); |
---|
| 1615 | } |
---|
[c6d2e93] | 1616 | ret->get_attributes().push_back( new Attribute( "unused" ) ); |
---|
[9facf3b] | 1617 | } |
---|
| 1618 | } |
---|
[cce9429] | 1619 | |
---|
[0db6fc0] | 1620 | void ReturnTypeFixer::postvisit( FunctionType * ftype ) { |
---|
[cce9429] | 1621 | // xxx - need to handle named return values - this information needs to be saved somehow |
---|
| 1622 | // so that resolution has access to the names. |
---|
| 1623 | // Note that this pass needs to happen early so that other passes which look for tuple types |
---|
| 1624 | // find them in all of the right places, including function return types. |
---|
| 1625 | std::list< DeclarationWithType * > & retVals = ftype->get_returnVals(); |
---|
| 1626 | if ( retVals.size() > 1 ) { |
---|
| 1627 | // generate a single return parameter which is the tuple of all of the return values |
---|
[e3e16bc] | 1628 | TupleType * tupleType = strict_dynamic_cast< TupleType * >( ResolvExpr::extractResultType( ftype ) ); |
---|
[cce9429] | 1629 | // ensure return value is not destructed by explicitly creating an empty ListInit node wherein maybeConstruct is false. |
---|
[ef5b828] | 1630 | ObjectDecl * newRet = new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, tupleType, new ListInit( std::list<Initializer *>(), noDesignators, false ) ); |
---|
[cce9429] | 1631 | deleteAll( retVals ); |
---|
| 1632 | retVals.clear(); |
---|
| 1633 | retVals.push_back( newRet ); |
---|
| 1634 | } |
---|
| 1635 | } |
---|
[fbd7ad6] | 1636 | |
---|
[2b79a70] | 1637 | void FixObjectType::fix( std::list< Declaration * > & translationUnit ) { |
---|
| 1638 | PassVisitor<FixObjectType> fixer; |
---|
| 1639 | acceptAll( translationUnit, fixer ); |
---|
| 1640 | } |
---|
| 1641 | |
---|
| 1642 | void FixObjectType::previsit( ObjectDecl * objDecl ) { |
---|
[ef5b828] | 1643 | Type * new_type = ResolvExpr::resolveTypeof( objDecl->get_type(), indexer ); |
---|
[2b79a70] | 1644 | objDecl->set_type( new_type ); |
---|
| 1645 | } |
---|
| 1646 | |
---|
| 1647 | void FixObjectType::previsit( FunctionDecl * funcDecl ) { |
---|
[ef5b828] | 1648 | Type * new_type = ResolvExpr::resolveTypeof( funcDecl->type, indexer ); |
---|
[2b79a70] | 1649 | funcDecl->set_type( new_type ); |
---|
| 1650 | } |
---|
| 1651 | |
---|
[ef5b828] | 1652 | void FixObjectType::previsit( TypeDecl * typeDecl ) { |
---|
[2b79a70] | 1653 | if ( typeDecl->get_base() ) { |
---|
[ef5b828] | 1654 | Type * new_type = ResolvExpr::resolveTypeof( typeDecl->get_base(), indexer ); |
---|
[2b79a70] | 1655 | typeDecl->set_base( new_type ); |
---|
| 1656 | } // if |
---|
| 1657 | } |
---|
| 1658 | |
---|
[09867ec] | 1659 | void InitializerLength::computeLength( std::list< Declaration * > & translationUnit ) { |
---|
| 1660 | PassVisitor<InitializerLength> len; |
---|
| 1661 | acceptAll( translationUnit, len ); |
---|
| 1662 | } |
---|
| 1663 | |
---|
[fbd7ad6] | 1664 | void ArrayLength::computeLength( std::list< Declaration * > & translationUnit ) { |
---|
[0db6fc0] | 1665 | PassVisitor<ArrayLength> len; |
---|
[fbd7ad6] | 1666 | acceptAll( translationUnit, len ); |
---|
| 1667 | } |
---|
| 1668 | |
---|
[09867ec] | 1669 | void InitializerLength::previsit( ObjectDecl * objDecl ) { |
---|
[0b3b2ae] | 1670 | if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->type ) ) { |
---|
[f072892] | 1671 | if ( at->dimension ) return; |
---|
[0b3b2ae] | 1672 | if ( ListInit * init = dynamic_cast< ListInit * >( objDecl->init ) ) { |
---|
[f072892] | 1673 | at->dimension = new ConstantExpr( Constant::from_ulong( init->initializers.size() ) ); |
---|
[fbd7ad6] | 1674 | } |
---|
| 1675 | } |
---|
| 1676 | } |
---|
[4fbdfae0] | 1677 | |
---|
[4934ea3] | 1678 | void ArrayLength::previsit( ArrayType * type ) { |
---|
[09867ec] | 1679 | if ( type->dimension ) { |
---|
| 1680 | // need to resolve array dimensions early so that constructor code can correctly determine |
---|
| 1681 | // if a type is a VLA (and hence whether its elements need to be constructed) |
---|
| 1682 | ResolvExpr::findSingleExpression( type->dimension, Validate::SizeType->clone(), indexer ); |
---|
| 1683 | |
---|
| 1684 | // must re-evaluate whether a type is a VLA, now that more information is available |
---|
| 1685 | // (e.g. the dimension may have been an enumerator, which was unknown prior to this step) |
---|
| 1686 | type->isVarLen = ! InitTweak::isConstExpr( type->dimension ); |
---|
[4934ea3] | 1687 | } |
---|
| 1688 | } |
---|
| 1689 | |
---|
[5809461] | 1690 | struct LabelFinder { |
---|
| 1691 | std::set< Label > & labels; |
---|
| 1692 | LabelFinder( std::set< Label > & labels ) : labels( labels ) {} |
---|
| 1693 | void previsit( Statement * stmt ) { |
---|
| 1694 | for ( Label & l : stmt->labels ) { |
---|
| 1695 | labels.insert( l ); |
---|
| 1696 | } |
---|
| 1697 | } |
---|
| 1698 | }; |
---|
| 1699 | |
---|
| 1700 | void LabelAddressFixer::premutate( FunctionDecl * funcDecl ) { |
---|
| 1701 | GuardValue( labels ); |
---|
| 1702 | PassVisitor<LabelFinder> finder( labels ); |
---|
| 1703 | funcDecl->accept( finder ); |
---|
| 1704 | } |
---|
| 1705 | |
---|
| 1706 | Expression * LabelAddressFixer::postmutate( AddressExpr * addrExpr ) { |
---|
| 1707 | // convert &&label into label address |
---|
| 1708 | if ( AddressExpr * inner = dynamic_cast< AddressExpr * >( addrExpr->arg ) ) { |
---|
| 1709 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( inner->arg ) ) { |
---|
| 1710 | if ( labels.count( nameExpr->name ) ) { |
---|
| 1711 | Label name = nameExpr->name; |
---|
| 1712 | delete addrExpr; |
---|
| 1713 | return new LabelAddressExpr( name ); |
---|
| 1714 | } |
---|
| 1715 | } |
---|
| 1716 | } |
---|
| 1717 | return addrExpr; |
---|
| 1718 | } |
---|
[c8e4d2f8] | 1719 | |
---|
[c1ed2ee] | 1720 | namespace { |
---|
[ef5b828] | 1721 | /// Replaces enum types by int, and function/array types in function parameter and return |
---|
[c1ed2ee] | 1722 | /// lists by appropriate pointers |
---|
[954c954] | 1723 | /* |
---|
[c1ed2ee] | 1724 | struct EnumAndPointerDecay_new { |
---|
| 1725 | const ast::EnumDecl * previsit( const ast::EnumDecl * enumDecl ) { |
---|
| 1726 | // set the type of each member of the enumeration to be EnumConstant |
---|
| 1727 | for ( unsigned i = 0; i < enumDecl->members.size(); ++i ) { |
---|
| 1728 | // build new version of object with EnumConstant |
---|
[ef5b828] | 1729 | ast::ptr< ast::ObjectDecl > obj = |
---|
[c1ed2ee] | 1730 | enumDecl->members[i].strict_as< ast::ObjectDecl >(); |
---|
[ef5b828] | 1731 | obj.get_and_mutate()->type = |
---|
[c1ed2ee] | 1732 | new ast::EnumInstType{ enumDecl->name, ast::CV::Const }; |
---|
[ef5b828] | 1733 | |
---|
[c1ed2ee] | 1734 | // set into decl |
---|
| 1735 | ast::EnumDecl * mut = mutate( enumDecl ); |
---|
| 1736 | mut->members[i] = obj.get(); |
---|
| 1737 | enumDecl = mut; |
---|
| 1738 | } |
---|
| 1739 | return enumDecl; |
---|
| 1740 | } |
---|
| 1741 | |
---|
| 1742 | static const ast::FunctionType * fixFunctionList( |
---|
[ef5b828] | 1743 | const ast::FunctionType * func, |
---|
[c1ed2ee] | 1744 | std::vector< ast::ptr< ast::DeclWithType > > ast::FunctionType::* field, |
---|
| 1745 | ast::ArgumentFlag isVarArgs = ast::FixedArgs |
---|
| 1746 | ) { |
---|
[ef5b828] | 1747 | const auto & dwts = func->* field; |
---|
[c1ed2ee] | 1748 | unsigned nvals = dwts.size(); |
---|
| 1749 | bool hasVoid = false; |
---|
| 1750 | for ( unsigned i = 0; i < nvals; ++i ) { |
---|
| 1751 | func = ast::mutate_field_index( func, field, i, fixFunction( dwts[i], hasVoid ) ); |
---|
| 1752 | } |
---|
[ef5b828] | 1753 | |
---|
[c1ed2ee] | 1754 | // the only case in which "void" is valid is where it is the only one in the list |
---|
| 1755 | if ( hasVoid && ( nvals > 1 || isVarArgs ) ) { |
---|
[ef5b828] | 1756 | SemanticError( |
---|
[c1ed2ee] | 1757 | dwts.front()->location, func, "invalid type void in function type" ); |
---|
| 1758 | } |
---|
| 1759 | |
---|
| 1760 | // one void is the only thing in the list, remove it |
---|
| 1761 | if ( hasVoid ) { |
---|
[ef5b828] | 1762 | func = ast::mutate_field( |
---|
[c1ed2ee] | 1763 | func, field, std::vector< ast::ptr< ast::DeclWithType > >{} ); |
---|
| 1764 | } |
---|
| 1765 | |
---|
| 1766 | return func; |
---|
| 1767 | } |
---|
| 1768 | |
---|
| 1769 | const ast::FunctionType * previsit( const ast::FunctionType * func ) { |
---|
| 1770 | func = fixFunctionList( func, &ast::FunctionType::params, func->isVarArgs ); |
---|
| 1771 | return fixFunctionList( func, &ast::FunctionType::returns ); |
---|
| 1772 | } |
---|
| 1773 | }; |
---|
| 1774 | |
---|
[c1398e4] | 1775 | /// expand assertions from a trait instance, performing appropriate type variable substitutions |
---|
[ef5b828] | 1776 | void expandAssertions( |
---|
| 1777 | const ast::TraitInstType * inst, std::vector< ast::ptr< ast::DeclWithType > > & out |
---|
[c1398e4] | 1778 | ) { |
---|
| 1779 | assertf( inst->base, "Trait instance not linked to base trait: %s", toCString( inst ) ); |
---|
| 1780 | |
---|
| 1781 | // build list of trait members, substituting trait decl parameters for instance parameters |
---|
[ef5b828] | 1782 | ast::TypeSubstitution sub{ |
---|
[c1398e4] | 1783 | inst->base->params.begin(), inst->base->params.end(), inst->params.begin() }; |
---|
| 1784 | // deliberately take ast::ptr by-value to ensure this does not mutate inst->base |
---|
| 1785 | for ( ast::ptr< ast::Decl > decl : inst->base->members ) { |
---|
| 1786 | auto member = decl.strict_as< ast::DeclWithType >(); |
---|
| 1787 | sub.apply( member ); |
---|
| 1788 | out.emplace_back( member ); |
---|
| 1789 | } |
---|
| 1790 | } |
---|
| 1791 | |
---|
[c1ed2ee] | 1792 | /// Associates forward declarations of aggregates with their definitions |
---|
[ef5b828] | 1793 | class LinkReferenceToTypes_new final |
---|
| 1794 | : public ast::WithSymbolTable, public ast::WithGuards, public |
---|
[c1ed2ee] | 1795 | ast::WithVisitorRef<LinkReferenceToTypes_new>, public ast::WithShortCircuiting { |
---|
[ef5b828] | 1796 | |
---|
| 1797 | // these maps of uses of forward declarations of types need to have the actual type |
---|
| 1798 | // declaration switched in * after * they have been traversed. To enable this in the |
---|
| 1799 | // ast::Pass framework, any node that needs to be so mutated has mutate() called on it |
---|
| 1800 | // before it is placed in the map, properly updating its parents in the usual traversal, |
---|
[18e683b] | 1801 | // then can have the actual mutation applied later |
---|
| 1802 | using ForwardEnumsType = std::unordered_multimap< std::string, ast::EnumInstType * >; |
---|
| 1803 | using ForwardStructsType = std::unordered_multimap< std::string, ast::StructInstType * >; |
---|
| 1804 | using ForwardUnionsType = std::unordered_multimap< std::string, ast::UnionInstType * >; |
---|
[ef5b828] | 1805 | |
---|
[18e683b] | 1806 | const CodeLocation & location; |
---|
| 1807 | const ast::SymbolTable * localSymtab; |
---|
[ef5b828] | 1808 | |
---|
[18e683b] | 1809 | ForwardEnumsType forwardEnums; |
---|
| 1810 | ForwardStructsType forwardStructs; |
---|
| 1811 | ForwardUnionsType forwardUnions; |
---|
[c1ed2ee] | 1812 | |
---|
[ef5b828] | 1813 | /// true if currently in a generic type body, so that type parameter instances can be |
---|
[18e683b] | 1814 | /// renamed appropriately |
---|
| 1815 | bool inGeneric = false; |
---|
[c1ed2ee] | 1816 | |
---|
[18e683b] | 1817 | public: |
---|
| 1818 | /// contstruct using running symbol table |
---|
[ef5b828] | 1819 | LinkReferenceToTypes_new( const CodeLocation & loc ) |
---|
[18e683b] | 1820 | : location( loc ), localSymtab( &symtab ) {} |
---|
[ef5b828] | 1821 | |
---|
[18e683b] | 1822 | /// construct using provided symbol table |
---|
[ef5b828] | 1823 | LinkReferenceToTypes_new( const CodeLocation & loc, const ast::SymbolTable & syms ) |
---|
[18e683b] | 1824 | : location( loc ), localSymtab( &syms ) {} |
---|
| 1825 | |
---|
| 1826 | const ast::Type * postvisit( const ast::TypeInstType * typeInst ) { |
---|
| 1827 | // ensure generic parameter instances are renamed like the base type |
---|
| 1828 | if ( inGeneric && typeInst->base ) { |
---|
[ef5b828] | 1829 | typeInst = ast::mutate_field( |
---|
[18e683b] | 1830 | typeInst, &ast::TypeInstType::name, typeInst->base->name ); |
---|
| 1831 | } |
---|
| 1832 | |
---|
[ef5b828] | 1833 | if ( |
---|
| 1834 | auto typeDecl = dynamic_cast< const ast::TypeDecl * >( |
---|
| 1835 | localSymtab->lookupType( typeInst->name ) ) |
---|
[18e683b] | 1836 | ) { |
---|
| 1837 | typeInst = ast::mutate_field( typeInst, &ast::TypeInstType::kind, typeDecl->kind ); |
---|
| 1838 | } |
---|
| 1839 | |
---|
| 1840 | return typeInst; |
---|
| 1841 | } |
---|
| 1842 | |
---|
| 1843 | const ast::Type * postvisit( const ast::EnumInstType * inst ) { |
---|
| 1844 | const ast::EnumDecl * decl = localSymtab->lookupEnum( inst->name ); |
---|
| 1845 | // not a semantic error if the enum is not found, just an implicit forward declaration |
---|
| 1846 | if ( decl ) { |
---|
| 1847 | inst = ast::mutate_field( inst, &ast::EnumInstType::base, decl ); |
---|
| 1848 | } |
---|
| 1849 | if ( ! decl || ! decl->body ) { |
---|
| 1850 | // forward declaration |
---|
| 1851 | auto mut = mutate( inst ); |
---|
| 1852 | forwardEnums.emplace( inst->name, mut ); |
---|
| 1853 | inst = mut; |
---|
| 1854 | } |
---|
| 1855 | return inst; |
---|
| 1856 | } |
---|
| 1857 | |
---|
[98e8b3b] | 1858 | void checkGenericParameters( const ast::BaseInstType * inst ) { |
---|
[18e683b] | 1859 | for ( const ast::Expr * param : inst->params ) { |
---|
| 1860 | if ( ! dynamic_cast< const ast::TypeExpr * >( param ) ) { |
---|
[ef5b828] | 1861 | SemanticError( |
---|
[18e683b] | 1862 | location, inst, "Expression parameters for generic types are currently " |
---|
| 1863 | "unsupported: " ); |
---|
| 1864 | } |
---|
| 1865 | } |
---|
| 1866 | } |
---|
| 1867 | |
---|
| 1868 | const ast::StructInstType * postvisit( const ast::StructInstType * inst ) { |
---|
| 1869 | const ast::StructDecl * decl = localSymtab->lookupStruct( inst->name ); |
---|
| 1870 | // not a semantic error if the struct is not found, just an implicit forward declaration |
---|
| 1871 | if ( decl ) { |
---|
| 1872 | inst = ast::mutate_field( inst, &ast::StructInstType::base, decl ); |
---|
| 1873 | } |
---|
| 1874 | if ( ! decl || ! decl->body ) { |
---|
| 1875 | // forward declaration |
---|
| 1876 | auto mut = mutate( inst ); |
---|
| 1877 | forwardStructs.emplace( inst->name, mut ); |
---|
| 1878 | inst = mut; |
---|
| 1879 | } |
---|
| 1880 | checkGenericParameters( inst ); |
---|
| 1881 | return inst; |
---|
| 1882 | } |
---|
| 1883 | |
---|
| 1884 | const ast::UnionInstType * postvisit( const ast::UnionInstType * inst ) { |
---|
| 1885 | const ast::UnionDecl * decl = localSymtab->lookupUnion( inst->name ); |
---|
| 1886 | // not a semantic error if the struct is not found, just an implicit forward declaration |
---|
| 1887 | if ( decl ) { |
---|
| 1888 | inst = ast::mutate_field( inst, &ast::UnionInstType::base, decl ); |
---|
| 1889 | } |
---|
| 1890 | if ( ! decl || ! decl->body ) { |
---|
| 1891 | // forward declaration |
---|
| 1892 | auto mut = mutate( inst ); |
---|
| 1893 | forwardUnions.emplace( inst->name, mut ); |
---|
| 1894 | inst = mut; |
---|
| 1895 | } |
---|
| 1896 | checkGenericParameters( inst ); |
---|
| 1897 | return inst; |
---|
| 1898 | } |
---|
| 1899 | |
---|
| 1900 | const ast::Type * postvisit( const ast::TraitInstType * traitInst ) { |
---|
| 1901 | // handle other traits |
---|
| 1902 | const ast::TraitDecl * traitDecl = localSymtab->lookupTrait( traitInst->name ); |
---|
| 1903 | if ( ! traitDecl ) { |
---|
| 1904 | SemanticError( location, "use of undeclared trait " + traitInst->name ); |
---|
| 1905 | } |
---|
| 1906 | if ( traitDecl->params.size() != traitInst->params.size() ) { |
---|
| 1907 | SemanticError( location, traitInst, "incorrect number of trait parameters: " ); |
---|
| 1908 | } |
---|
| 1909 | traitInst = ast::mutate_field( traitInst, &ast::TraitInstType::base, traitDecl ); |
---|
| 1910 | |
---|
| 1911 | // need to carry over the "sized" status of each decl in the instance |
---|
| 1912 | for ( unsigned i = 0; i < traitDecl->params.size(); ++i ) { |
---|
| 1913 | auto expr = traitInst->params[i].as< ast::TypeExpr >(); |
---|
| 1914 | if ( ! expr ) { |
---|
[ef5b828] | 1915 | SemanticError( |
---|
[18e683b] | 1916 | traitInst->params[i].get(), "Expression parameters for trait instances " |
---|
| 1917 | "are currently unsupported: " ); |
---|
| 1918 | } |
---|
| 1919 | |
---|
| 1920 | if ( auto inst = expr->type.as< ast::TypeInstType >() ) { |
---|
| 1921 | if ( traitDecl->params[i]->sized && ! inst->base->sized ) { |
---|
| 1922 | // traitInst = ast::mutate_field_index( |
---|
| 1923 | // traitInst, &ast::TraitInstType::params, i, |
---|
| 1924 | // ... |
---|
| 1925 | // ); |
---|
| 1926 | ast::TraitInstType * mut = ast::mutate( traitInst ); |
---|
| 1927 | ast::chain_mutate( mut->params[i] ) |
---|
| 1928 | ( &ast::TypeExpr::type ) |
---|
| 1929 | ( &ast::TypeInstType::base )->sized = true; |
---|
| 1930 | traitInst = mut; |
---|
| 1931 | } |
---|
| 1932 | } |
---|
| 1933 | } |
---|
| 1934 | |
---|
| 1935 | return traitInst; |
---|
| 1936 | } |
---|
[ef5b828] | 1937 | |
---|
[18e683b] | 1938 | void previsit( const ast::QualifiedType * ) { visit_children = false; } |
---|
[ef5b828] | 1939 | |
---|
[18e683b] | 1940 | const ast::Type * postvisit( const ast::QualifiedType * qualType ) { |
---|
| 1941 | // linking only makes sense for the "oldest ancestor" of the qualified type |
---|
[ef5b828] | 1942 | return ast::mutate_field( |
---|
| 1943 | qualType, &ast::QualifiedType::parent, qualType->parent->accept( * visitor ) ); |
---|
[18e683b] | 1944 | } |
---|
| 1945 | |
---|
| 1946 | const ast::Decl * postvisit( const ast::EnumDecl * enumDecl ) { |
---|
[ef5b828] | 1947 | // visit enum members first so that the types of self-referencing members are updated |
---|
[18e683b] | 1948 | // properly |
---|
| 1949 | if ( ! enumDecl->body ) return enumDecl; |
---|
| 1950 | |
---|
| 1951 | // update forward declarations to point here |
---|
| 1952 | auto fwds = forwardEnums.equal_range( enumDecl->name ); |
---|
| 1953 | if ( fwds.first != fwds.second ) { |
---|
| 1954 | auto inst = fwds.first; |
---|
| 1955 | do { |
---|
[ef5b828] | 1956 | // forward decl is stored * mutably * in map, can thus be updated |
---|
[18e683b] | 1957 | inst->second->base = enumDecl; |
---|
| 1958 | } while ( ++inst != fwds.second ); |
---|
| 1959 | forwardEnums.erase( fwds.first, fwds.second ); |
---|
| 1960 | } |
---|
[ef5b828] | 1961 | |
---|
[18e683b] | 1962 | // ensure that enumerator initializers are properly set |
---|
| 1963 | for ( unsigned i = 0; i < enumDecl->members.size(); ++i ) { |
---|
| 1964 | auto field = enumDecl->members[i].strict_as< ast::ObjectDecl >(); |
---|
| 1965 | if ( field->init ) { |
---|
[ef5b828] | 1966 | // need to resolve enumerator initializers early so that other passes that |
---|
[18e683b] | 1967 | // determine if an expression is constexpr have appropriate information |
---|
| 1968 | auto init = field->init.strict_as< ast::SingleInit >(); |
---|
[ef5b828] | 1969 | |
---|
| 1970 | enumDecl = ast::mutate_field_index( |
---|
| 1971 | enumDecl, &ast::EnumDecl::members, i, |
---|
| 1972 | ast::mutate_field( field, &ast::ObjectDecl::init, |
---|
[18e683b] | 1973 | ast::mutate_field( init, &ast::SingleInit::value, |
---|
[ef5b828] | 1974 | ResolvExpr::findSingleExpression( |
---|
[18e683b] | 1975 | init->value, new ast::BasicType{ ast::BasicType::SignedInt }, |
---|
| 1976 | symtab ) ) ) ); |
---|
| 1977 | } |
---|
| 1978 | } |
---|
| 1979 | |
---|
| 1980 | return enumDecl; |
---|
| 1981 | } |
---|
| 1982 | |
---|
[ef5b828] | 1983 | /// rename generic type parameters uniquely so that they do not conflict with user defined |
---|
[18e683b] | 1984 | /// function forall parameters, e.g. the T in Box and the T in f, below |
---|
| 1985 | /// forall(otype T) |
---|
| 1986 | /// struct Box { |
---|
| 1987 | /// T x; |
---|
| 1988 | /// }; |
---|
| 1989 | /// forall(otype T) |
---|
| 1990 | /// void f(Box(T) b) { |
---|
| 1991 | /// ... |
---|
| 1992 | /// } |
---|
| 1993 | template< typename AggrDecl > |
---|
| 1994 | const AggrDecl * renameGenericParams( const AggrDecl * aggr ) { |
---|
| 1995 | GuardValue( inGeneric ); |
---|
| 1996 | inGeneric = ! aggr->params.empty(); |
---|
| 1997 | |
---|
| 1998 | for ( unsigned i = 0; i < aggr->params.size(); ++i ) { |
---|
| 1999 | const ast::TypeDecl * td = aggr->params[i]; |
---|
| 2000 | |
---|
[ef5b828] | 2001 | aggr = ast::mutate_field_index( |
---|
| 2002 | aggr, &AggrDecl::params, i, |
---|
[18e683b] | 2003 | ast::mutate_field( td, &ast::TypeDecl::name, "__" + td->name + "_generic_" ) ); |
---|
| 2004 | } |
---|
| 2005 | return aggr; |
---|
| 2006 | } |
---|
| 2007 | |
---|
| 2008 | const ast::StructDecl * previsit( const ast::StructDecl * structDecl ) { |
---|
| 2009 | return renameGenericParams( structDecl ); |
---|
| 2010 | } |
---|
| 2011 | |
---|
| 2012 | void postvisit( const ast::StructDecl * structDecl ) { |
---|
[ef5b828] | 2013 | // visit struct members first so that the types of self-referencing members are |
---|
[18e683b] | 2014 | // updated properly |
---|
| 2015 | if ( ! structDecl->body ) return; |
---|
| 2016 | |
---|
| 2017 | // update forward declarations to point here |
---|
| 2018 | auto fwds = forwardStructs.equal_range( structDecl->name ); |
---|
| 2019 | if ( fwds.first != fwds.second ) { |
---|
| 2020 | auto inst = fwds.first; |
---|
| 2021 | do { |
---|
[ef5b828] | 2022 | // forward decl is stored * mutably * in map, can thus be updated |
---|
[18e683b] | 2023 | inst->second->base = structDecl; |
---|
| 2024 | } while ( ++inst != fwds.second ); |
---|
| 2025 | forwardStructs.erase( fwds.first, fwds.second ); |
---|
| 2026 | } |
---|
| 2027 | } |
---|
| 2028 | |
---|
| 2029 | const ast::UnionDecl * previsit( const ast::UnionDecl * unionDecl ) { |
---|
| 2030 | return renameGenericParams( unionDecl ); |
---|
| 2031 | } |
---|
| 2032 | |
---|
| 2033 | void postvisit( const ast::UnionDecl * unionDecl ) { |
---|
[ef5b828] | 2034 | // visit union members first so that the types of self-referencing members are updated |
---|
[18e683b] | 2035 | // properly |
---|
| 2036 | if ( ! unionDecl->body ) return; |
---|
| 2037 | |
---|
| 2038 | // update forward declarations to point here |
---|
| 2039 | auto fwds = forwardUnions.equal_range( unionDecl->name ); |
---|
| 2040 | if ( fwds.first != fwds.second ) { |
---|
| 2041 | auto inst = fwds.first; |
---|
| 2042 | do { |
---|
[ef5b828] | 2043 | // forward decl is stored * mutably * in map, can thus be updated |
---|
[18e683b] | 2044 | inst->second->base = unionDecl; |
---|
| 2045 | } while ( ++inst != fwds.second ); |
---|
| 2046 | forwardUnions.erase( fwds.first, fwds.second ); |
---|
| 2047 | } |
---|
| 2048 | } |
---|
| 2049 | |
---|
| 2050 | const ast::Decl * postvisit( const ast::TraitDecl * traitDecl ) { |
---|
| 2051 | // set the "sized" status for the special "sized" trait |
---|
[c1398e4] | 2052 | if ( traitDecl->name == "sized" ) { |
---|
| 2053 | assertf( traitDecl->params.size() == 1, "Built-in trait 'sized' has incorrect " |
---|
| 2054 | "number of parameters: %zd", traitDecl->params.size() ); |
---|
| 2055 | |
---|
[ef5b828] | 2056 | traitDecl = ast::mutate_field_index( |
---|
| 2057 | traitDecl, &ast::TraitDecl::params, 0, |
---|
| 2058 | ast::mutate_field( |
---|
[c1398e4] | 2059 | traitDecl->params.front().get(), &ast::TypeDecl::sized, true ) ); |
---|
| 2060 | } |
---|
[18e683b] | 2061 | |
---|
[c1398e4] | 2062 | // move assertions from type parameters into the body of the trait |
---|
| 2063 | std::vector< ast::ptr< ast::DeclWithType > > added; |
---|
| 2064 | for ( const ast::TypeDecl * td : traitDecl->params ) { |
---|
| 2065 | for ( const ast::DeclWithType * assn : td->assertions ) { |
---|
| 2066 | auto inst = dynamic_cast< const ast::TraitInstType * >( assn->get_type() ); |
---|
| 2067 | if ( inst ) { |
---|
| 2068 | expandAssertions( inst, added ); |
---|
| 2069 | } else { |
---|
| 2070 | added.emplace_back( assn ); |
---|
| 2071 | } |
---|
| 2072 | } |
---|
| 2073 | } |
---|
| 2074 | if ( ! added.empty() ) { |
---|
| 2075 | auto mut = mutate( traitDecl ); |
---|
| 2076 | for ( const ast::DeclWithType * decl : added ) { |
---|
| 2077 | mut->members.emplace_back( decl ); |
---|
| 2078 | } |
---|
| 2079 | traitDecl = mut; |
---|
| 2080 | } |
---|
[ef5b828] | 2081 | |
---|
[c1398e4] | 2082 | return traitDecl; |
---|
[18e683b] | 2083 | } |
---|
[c1ed2ee] | 2084 | }; |
---|
| 2085 | |
---|
[ef5b828] | 2086 | /// Replaces array and function types in forall lists by appropriate pointer type and assigns |
---|
[c1ed2ee] | 2087 | /// each object and function declaration a unique ID |
---|
[c1398e4] | 2088 | class ForallPointerDecay_new { |
---|
| 2089 | const CodeLocation & location; |
---|
| 2090 | public: |
---|
| 2091 | ForallPointerDecay_new( const CodeLocation & loc ) : location( loc ) {} |
---|
| 2092 | |
---|
| 2093 | const ast::ObjectDecl * previsit( const ast::ObjectDecl * obj ) { |
---|
| 2094 | // ensure that operator names only apply to functions or function pointers |
---|
[ef5b828] | 2095 | if ( |
---|
| 2096 | CodeGen::isOperator( obj->name ) |
---|
[c1398e4] | 2097 | && ! dynamic_cast< const ast::FunctionType * >( obj->type->stripDeclarator() ) |
---|
| 2098 | ) { |
---|
| 2099 | SemanticError( obj->location, toCString( "operator ", obj->name.c_str(), " is not " |
---|
| 2100 | "a function or function pointer." ) ); |
---|
| 2101 | } |
---|
| 2102 | |
---|
| 2103 | // ensure object has unique ID |
---|
| 2104 | if ( obj->uniqueId ) return obj; |
---|
| 2105 | auto mut = mutate( obj ); |
---|
| 2106 | mut->fixUniqueId(); |
---|
| 2107 | return mut; |
---|
| 2108 | } |
---|
| 2109 | |
---|
| 2110 | const ast::FunctionDecl * previsit( const ast::FunctionDecl * func ) { |
---|
| 2111 | // ensure function has unique ID |
---|
| 2112 | if ( func->uniqueId ) return func; |
---|
| 2113 | auto mut = mutate( func ); |
---|
| 2114 | mut->fixUniqueId(); |
---|
| 2115 | return mut; |
---|
| 2116 | } |
---|
| 2117 | |
---|
| 2118 | /// Fix up assertions -- flattens assertion lists, removing all trait instances |
---|
| 2119 | template< typename node_t, typename parent_t > |
---|
[ef5b828] | 2120 | static const node_t * forallFixer( |
---|
| 2121 | const CodeLocation & loc, const node_t * node, |
---|
[361bf01] | 2122 | ast::FunctionType::ForallList parent_t::* forallField |
---|
[c1398e4] | 2123 | ) { |
---|
[ef5b828] | 2124 | for ( unsigned i = 0; i < (node->* forallField).size(); ++i ) { |
---|
| 2125 | const ast::TypeDecl * type = (node->* forallField)[i]; |
---|
[c1398e4] | 2126 | if ( type->assertions.empty() ) continue; |
---|
| 2127 | |
---|
| 2128 | std::vector< ast::ptr< ast::DeclWithType > > asserts; |
---|
| 2129 | asserts.reserve( type->assertions.size() ); |
---|
| 2130 | |
---|
| 2131 | // expand trait instances into their members |
---|
| 2132 | for ( const ast::DeclWithType * assn : type->assertions ) { |
---|
[ef5b828] | 2133 | auto traitInst = |
---|
| 2134 | dynamic_cast< const ast::TraitInstType * >( assn->get_type() ); |
---|
[c1398e4] | 2135 | if ( traitInst ) { |
---|
| 2136 | // expand trait instance to all its members |
---|
| 2137 | expandAssertions( traitInst, asserts ); |
---|
| 2138 | } else { |
---|
| 2139 | // pass other assertions through |
---|
| 2140 | asserts.emplace_back( assn ); |
---|
| 2141 | } |
---|
| 2142 | } |
---|
| 2143 | |
---|
| 2144 | // apply FixFunction to every assertion to check for invalid void type |
---|
| 2145 | for ( ast::ptr< ast::DeclWithType > & assn : asserts ) { |
---|
| 2146 | bool isVoid = false; |
---|
| 2147 | assn = fixFunction( assn, isVoid ); |
---|
| 2148 | if ( isVoid ) { |
---|
| 2149 | SemanticError( loc, node, "invalid type void in assertion of function " ); |
---|
| 2150 | } |
---|
| 2151 | } |
---|
| 2152 | |
---|
| 2153 | // place mutated assertion list in node |
---|
| 2154 | auto mut = mutate( type ); |
---|
| 2155 | mut->assertions = move( asserts ); |
---|
| 2156 | node = ast::mutate_field_index( node, forallField, i, mut ); |
---|
| 2157 | } |
---|
| 2158 | return node; |
---|
| 2159 | } |
---|
| 2160 | |
---|
| 2161 | const ast::FunctionType * previsit( const ast::FunctionType * ftype ) { |
---|
| 2162 | return forallFixer( location, ftype, &ast::FunctionType::forall ); |
---|
| 2163 | } |
---|
| 2164 | |
---|
| 2165 | const ast::StructDecl * previsit( const ast::StructDecl * aggrDecl ) { |
---|
| 2166 | return forallFixer( aggrDecl->location, aggrDecl, &ast::StructDecl::params ); |
---|
| 2167 | } |
---|
| 2168 | |
---|
| 2169 | const ast::UnionDecl * previsit( const ast::UnionDecl * aggrDecl ) { |
---|
| 2170 | return forallFixer( aggrDecl->location, aggrDecl, &ast::UnionDecl::params ); |
---|
| 2171 | } |
---|
[c1ed2ee] | 2172 | }; |
---|
[3e5dd913] | 2173 | */ |
---|
[c1ed2ee] | 2174 | } // anonymous namespace |
---|
| 2175 | |
---|
[3e5dd913] | 2176 | /* |
---|
[ef5b828] | 2177 | const ast::Type * validateType( |
---|
[18e683b] | 2178 | const CodeLocation & loc, const ast::Type * type, const ast::SymbolTable & symtab ) { |
---|
[954c954] | 2179 | // ast::Pass< EnumAndPointerDecay_new > epc; |
---|
[18e683b] | 2180 | ast::Pass< LinkReferenceToTypes_new > lrt{ loc, symtab }; |
---|
[c1398e4] | 2181 | ast::Pass< ForallPointerDecay_new > fpd{ loc }; |
---|
[c1ed2ee] | 2182 | |
---|
[954c954] | 2183 | return type->accept( lrt )->accept( fpd ); |
---|
[c1ed2ee] | 2184 | } |
---|
[3e5dd913] | 2185 | */ |
---|
[c1ed2ee] | 2186 | |
---|
[51b7345] | 2187 | } // namespace SymTab |
---|
[0dd3a2f] | 2188 | |
---|
| 2189 | // Local Variables: // |
---|
| 2190 | // tab-width: 4 // |
---|
| 2191 | // mode: c++ // |
---|
| 2192 | // compile-command: "make install" // |
---|
| 2193 | // End: // |
---|