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