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