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