[0dd3a2f] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
---|
| 3 | //
|
---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
---|
| 5 | // file "LICENCE" distributed with Cforall.
|
---|
| 6 | //
|
---|
[9cb8e88d] | 7 | // Validate.cc --
|
---|
[0dd3a2f] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Sun May 17 21:50:04 2015
|
---|
[5dcb881] | 11 | // Last Modified By : Andrew Beach
|
---|
[b9f8274] | 12 | // Last Modified On : Tue Jul 12 15:00:00 2022
|
---|
| 13 | // Update Count : 367
|
---|
[0dd3a2f] | 14 | //
|
---|
| 15 |
|
---|
| 16 | // The "validate" phase of translation is used to take a syntax tree and convert it into a standard form that aims to be
|
---|
| 17 | // as regular in structure as possible. Some assumptions can be made regarding the state of the tree after this pass is
|
---|
| 18 | // complete, including:
|
---|
| 19 | //
|
---|
| 20 | // - No nested structure or union definitions; any in the input are "hoisted" to the level of the containing struct or
|
---|
| 21 | // union.
|
---|
| 22 | //
|
---|
| 23 | // - All enumeration constants have type EnumInstType.
|
---|
| 24 | //
|
---|
[3c13c03] | 25 | // - The type "void" never occurs in lists of function parameter or return types. A function
|
---|
| 26 | // taking no arguments has no argument types.
|
---|
[0dd3a2f] | 27 | //
|
---|
| 28 | // - No context instances exist; they are all replaced by the set of declarations signified by the context, instantiated
|
---|
| 29 | // by the particular set of type arguments.
|
---|
| 30 | //
|
---|
| 31 | // - Every declaration is assigned a unique id.
|
---|
| 32 | //
|
---|
| 33 | // - No typedef declarations or instances exist; the actual type is substituted for each instance.
|
---|
| 34 | //
|
---|
| 35 | // - Each type, struct, and union definition is followed by an appropriate assignment operator.
|
---|
| 36 | //
|
---|
| 37 | // - Each use of a struct or union is connected to a complete definition of that struct or union, even if that
|
---|
| 38 | // definition occurs later in the input.
|
---|
[51b73452] | 39 |
|
---|
[0db6fc0] | 40 | #include "Validate.h"
|
---|
| 41 |
|
---|
[d180746] | 42 | #include <cassert> // for assertf, assert
|
---|
[30f9072] | 43 | #include <cstddef> // for size_t
|
---|
[d180746] | 44 | #include <list> // for list
|
---|
| 45 | #include <string> // for string
|
---|
[18e683b] | 46 | #include <unordered_map> // for unordered_map
|
---|
[d180746] | 47 | #include <utility> // for pair
|
---|
[30f9072] | 48 |
|
---|
| 49 | #include "CodeGen/CodeGenerator.h" // for genName
|
---|
[9236060] | 50 | #include "CodeGen/OperatorTable.h" // for isCtorDtor, isCtorDtorAssign
|
---|
[25fcb84] | 51 | #include "ControlStruct/Mutate.h" // for ForExprMutator
|
---|
[18e683b] | 52 | #include "Common/CodeLocation.h" // for CodeLocation
|
---|
[7abee38] | 53 | #include "Common/Stats.h" // for Stats::Heap
|
---|
[30f9072] | 54 | #include "Common/PassVisitor.h" // for PassVisitor, WithDeclsToAdd
|
---|
[d180746] | 55 | #include "Common/ScopedMap.h" // for ScopedMap
|
---|
[30f9072] | 56 | #include "Common/SemanticError.h" // for SemanticError
|
---|
[9feb34b] | 57 | #include "Common/ToString.hpp" // for toCString
|
---|
[30f9072] | 58 | #include "Common/UniqueName.h" // for UniqueName
|
---|
[9feb34b] | 59 | #include "Common/utility.h" // for cloneAll, deleteAll
|
---|
[16ba4a6f] | 60 | #include "CompilationState.h" // skip some passes in new-ast build
|
---|
[be9288a] | 61 | #include "Concurrency/Keywords.h" // for applyKeywords
|
---|
[30f9072] | 62 | #include "FixFunction.h" // for FixFunction
|
---|
| 63 | #include "Indexer.h" // for Indexer
|
---|
[8b11840] | 64 | #include "InitTweak/GenInit.h" // for fixReturnStatements
|
---|
[d180746] | 65 | #include "InitTweak/InitTweak.h" // for isCtorDtorAssign
|
---|
[e563edf] | 66 | #include "ResolvExpr/typeops.h" // for extractResultType
|
---|
| 67 | #include "ResolvExpr/Unify.h" // for typesCompatible
|
---|
[4934ea3] | 68 | #include "ResolvExpr/Resolver.h" // for findSingleExpression
|
---|
[2b79a70] | 69 | #include "ResolvExpr/ResolveTypeof.h" // for resolveTypeof
|
---|
[be9288a] | 70 | #include "SymTab/Autogen.h" // for SizeType
|
---|
[9939dc3] | 71 | #include "SymTab/ValidateType.h" // for decayEnumsAndPointers, decayFo...
|
---|
[07de76b] | 72 | #include "SynTree/LinkageSpec.h" // for C
|
---|
[be9288a] | 73 | #include "SynTree/Attribute.h" // for noAttributes, Attribute
|
---|
[30f9072] | 74 | #include "SynTree/Constant.h" // for Constant
|
---|
[d180746] | 75 | #include "SynTree/Declaration.h" // for ObjectDecl, DeclarationWithType
|
---|
| 76 | #include "SynTree/Expression.h" // for CompoundLiteralExpr, Expressio...
|
---|
| 77 | #include "SynTree/Initializer.h" // for ListInit, Initializer
|
---|
| 78 | #include "SynTree/Label.h" // for operator==, Label
|
---|
| 79 | #include "SynTree/Mutator.h" // for Mutator
|
---|
| 80 | #include "SynTree/Type.h" // for Type, TypeInstType, EnumInstType
|
---|
| 81 | #include "SynTree/TypeSubstitution.h" // for TypeSubstitution
|
---|
| 82 | #include "SynTree/Visitor.h" // for Visitor
|
---|
[fd2debf] | 83 | #include "Validate/HandleAttributes.h" // for handleAttributes
|
---|
[2bfc6b2] | 84 | #include "Validate/FindSpecialDecls.h" // for FindSpecialDecls
|
---|
[d180746] | 85 |
|
---|
| 86 | class CompoundStmt;
|
---|
| 87 | class ReturnStmt;
|
---|
| 88 | class SwitchStmt;
|
---|
[51b73452] | 89 |
|
---|
[b16923d] | 90 | #define debugPrint( x ) if ( doDebug ) x
|
---|
[51b73452] | 91 |
|
---|
| 92 | namespace SymTab {
|
---|
[15f5c5e] | 93 | /// hoists declarations that are difficult to hoist while parsing
|
---|
| 94 | struct HoistTypeDecls final : public WithDeclsToAdd {
|
---|
[29f9e20] | 95 | void previsit( SizeofExpr * );
|
---|
| 96 | void previsit( AlignofExpr * );
|
---|
| 97 | void previsit( UntypedOffsetofExpr * );
|
---|
[95d09bdb] | 98 | void previsit( CompoundLiteralExpr * );
|
---|
[29f9e20] | 99 | void handleType( Type * );
|
---|
| 100 | };
|
---|
| 101 |
|
---|
[a12c81f3] | 102 | struct FixQualifiedTypes final : public WithIndexer {
|
---|
[6e50a6b] | 103 | FixQualifiedTypes() : WithIndexer(false) {}
|
---|
[a12c81f3] | 104 | Type * postmutate( QualifiedType * );
|
---|
| 105 | };
|
---|
| 106 |
|
---|
[a09e45b] | 107 | struct HoistStruct final : public WithDeclsToAdd, public WithGuards {
|
---|
[82dd287] | 108 | /// Flattens nested struct types
|
---|
[0dd3a2f] | 109 | static void hoistStruct( std::list< Declaration * > &translationUnit );
|
---|
[9cb8e88d] | 110 |
|
---|
[a09e45b] | 111 | void previsit( StructDecl * aggregateDecl );
|
---|
| 112 | void previsit( UnionDecl * aggregateDecl );
|
---|
[0f40912] | 113 | void previsit( StaticAssertDecl * assertDecl );
|
---|
[d419d8e] | 114 | void previsit( StructInstType * type );
|
---|
| 115 | void previsit( UnionInstType * type );
|
---|
| 116 | void previsit( EnumInstType * type );
|
---|
[9cb8e88d] | 117 |
|
---|
[a08ba92] | 118 | private:
|
---|
[ef5b828] | 119 | template< typename AggDecl > void handleAggregate( AggDecl * aggregateDecl );
|
---|
[c8ffe20b] | 120 |
|
---|
[bdad6eb7] | 121 | AggregateDecl * parentAggr = nullptr;
|
---|
[a08ba92] | 122 | };
|
---|
[c8ffe20b] | 123 |
|
---|
[cce9429] | 124 | /// Fix return types so that every function returns exactly one value
|
---|
[d24d4e1] | 125 | struct ReturnTypeFixer {
|
---|
[cce9429] | 126 | static void fix( std::list< Declaration * > &translationUnit );
|
---|
| 127 |
|
---|
[0db6fc0] | 128 | void postvisit( FunctionDecl * functionDecl );
|
---|
| 129 | void postvisit( FunctionType * ftype );
|
---|
[cce9429] | 130 | };
|
---|
| 131 |
|
---|
[6e50a6b] | 132 | /// Does early resolution on the expressions that give enumeration constants their values
|
---|
| 133 | struct ResolveEnumInitializers final : public WithIndexer, public WithGuards, public WithVisitorRef<ResolveEnumInitializers>, public WithShortCircuiting {
|
---|
| 134 | ResolveEnumInitializers( const Indexer * indexer );
|
---|
| 135 | void postvisit( EnumDecl * enumDecl );
|
---|
| 136 |
|
---|
| 137 | private:
|
---|
| 138 | const Indexer * local_indexer;
|
---|
| 139 |
|
---|
| 140 | };
|
---|
| 141 |
|
---|
[06edda0] | 142 | /// Replaces array and function types in forall lists by appropriate pointer type and assigns each Object and Function declaration a unique ID.
|
---|
[c1ed2ee] | 143 | struct ForallPointerDecay_old final {
|
---|
[8b11840] | 144 | void previsit( ObjectDecl * object );
|
---|
| 145 | void previsit( FunctionDecl * func );
|
---|
[bbf3fda] | 146 | void previsit( FunctionType * ftype );
|
---|
[bd7e609] | 147 | void previsit( StructDecl * aggrDecl );
|
---|
| 148 | void previsit( UnionDecl * aggrDecl );
|
---|
[a08ba92] | 149 | };
|
---|
[c8ffe20b] | 150 |
|
---|
[d24d4e1] | 151 | struct ReturnChecker : public WithGuards {
|
---|
[de91427b] | 152 | /// Checks that return statements return nothing if their return type is void
|
---|
| 153 | /// and return something if the return type is non-void.
|
---|
| 154 | static void checkFunctionReturns( std::list< Declaration * > & translationUnit );
|
---|
| 155 |
|
---|
[0db6fc0] | 156 | void previsit( FunctionDecl * functionDecl );
|
---|
| 157 | void previsit( ReturnStmt * returnStmt );
|
---|
[de91427b] | 158 |
|
---|
[0db6fc0] | 159 | typedef std::list< DeclarationWithType * > ReturnVals;
|
---|
| 160 | ReturnVals returnVals;
|
---|
[de91427b] | 161 | };
|
---|
| 162 |
|
---|
[48ed81c] | 163 | struct ReplaceTypedef final : public WithVisitorRef<ReplaceTypedef>, public WithGuards, public WithShortCircuiting, public WithDeclsToAdd {
|
---|
| 164 | ReplaceTypedef() : scopeLevel( 0 ) {}
|
---|
[de91427b] | 165 | /// Replaces typedefs by forward declarations
|
---|
[48ed81c] | 166 | static void replaceTypedef( std::list< Declaration * > &translationUnit );
|
---|
[85c4ef0] | 167 |
|
---|
[48ed81c] | 168 | void premutate( QualifiedType * );
|
---|
| 169 | Type * postmutate( QualifiedType * qualType );
|
---|
[a506df4] | 170 | Type * postmutate( TypeInstType * aggregateUseType );
|
---|
| 171 | Declaration * postmutate( TypedefDecl * typeDecl );
|
---|
| 172 | void premutate( TypeDecl * typeDecl );
|
---|
| 173 | void premutate( FunctionDecl * funcDecl );
|
---|
| 174 | void premutate( ObjectDecl * objDecl );
|
---|
| 175 | DeclarationWithType * postmutate( ObjectDecl * objDecl );
|
---|
| 176 |
|
---|
| 177 | void premutate( CastExpr * castExpr );
|
---|
| 178 |
|
---|
| 179 | void premutate( CompoundStmt * compoundStmt );
|
---|
| 180 |
|
---|
| 181 | void premutate( StructDecl * structDecl );
|
---|
| 182 | void premutate( UnionDecl * unionDecl );
|
---|
| 183 | void premutate( EnumDecl * enumDecl );
|
---|
[0bcc2b7] | 184 | void premutate( TraitDecl * );
|
---|
[a506df4] | 185 |
|
---|
[1f370451] | 186 | void premutate( FunctionType * ftype );
|
---|
| 187 |
|
---|
[a506df4] | 188 | private:
|
---|
[45161b4d] | 189 | template<typename AggDecl>
|
---|
| 190 | void addImplicitTypedef( AggDecl * aggDecl );
|
---|
[48ed81c] | 191 | template< typename AggDecl >
|
---|
| 192 | void handleAggregate( AggDecl * aggr );
|
---|
[70a06f6] | 193 |
|
---|
[46f6134] | 194 | typedef std::unique_ptr<TypedefDecl> TypedefDeclPtr;
|
---|
[e491159] | 195 | typedef ScopedMap< std::string, std::pair< TypedefDeclPtr, int > > TypedefMap;
|
---|
[0bcc2b7] | 196 | typedef ScopedMap< std::string, TypeDecl * > TypeDeclMap;
|
---|
[cc79d97] | 197 | TypedefMap typedefNames;
|
---|
[679864e1] | 198 | TypeDeclMap typedeclNames;
|
---|
[cc79d97] | 199 | int scopeLevel;
|
---|
[1f370451] | 200 | bool inFunctionType = false;
|
---|
[a08ba92] | 201 | };
|
---|
[c8ffe20b] | 202 |
|
---|
[69918cea] | 203 | struct EliminateTypedef {
|
---|
| 204 | /// removes TypedefDecls from the AST
|
---|
| 205 | static void eliminateTypedef( std::list< Declaration * > &translationUnit );
|
---|
| 206 |
|
---|
| 207 | template<typename AggDecl>
|
---|
[ef5b828] | 208 | void handleAggregate( AggDecl * aggregateDecl );
|
---|
[69918cea] | 209 |
|
---|
| 210 | void previsit( StructDecl * aggregateDecl );
|
---|
| 211 | void previsit( UnionDecl * aggregateDecl );
|
---|
| 212 | void previsit( CompoundStmt * compoundStmt );
|
---|
| 213 | };
|
---|
| 214 |
|
---|
[d24d4e1] | 215 | struct VerifyCtorDtorAssign {
|
---|
[d1969a6] | 216 | /// ensure that constructors, destructors, and assignment have at least one
|
---|
| 217 | /// parameter, the first of which must be a pointer, and that ctor/dtors have no
|
---|
[9cb8e88d] | 218 | /// return values.
|
---|
| 219 | static void verify( std::list< Declaration * > &translationUnit );
|
---|
| 220 |
|
---|
[ef5b828] | 221 | void previsit( FunctionDecl * funcDecl );
|
---|
[5f98ce5] | 222 | };
|
---|
[70a06f6] | 223 |
|
---|
[11ab8ea8] | 224 | /// ensure that generic types have the correct number of type arguments
|
---|
[d24d4e1] | 225 | struct ValidateGenericParameters {
|
---|
[0db6fc0] | 226 | void previsit( StructInstType * inst );
|
---|
| 227 | void previsit( UnionInstType * inst );
|
---|
[5f98ce5] | 228 | };
|
---|
[70a06f6] | 229 |
|
---|
[6e50a6b] | 230 | /// desugar declarations and uses of dimension paramaters like [N],
|
---|
| 231 | /// from type-system managed values, to tunnneling via ordinary types,
|
---|
| 232 | /// as char[-] in and sizeof(-) out
|
---|
| 233 | struct TranslateDimensionGenericParameters : public WithIndexer, public WithGuards {
|
---|
| 234 | static void translateDimensions( std::list< Declaration * > &translationUnit );
|
---|
| 235 | TranslateDimensionGenericParameters();
|
---|
| 236 |
|
---|
| 237 | bool nextVisitedNodeIsChildOfSUIT = false; // SUIT = Struct or Union -Inst Type
|
---|
| 238 | bool visitingChildOfSUIT = false;
|
---|
| 239 | void changeState_ChildOfSUIT( bool newVal );
|
---|
| 240 | void premutate( StructInstType * sit );
|
---|
| 241 | void premutate( UnionInstType * uit );
|
---|
| 242 | void premutate( BaseSyntaxNode * node );
|
---|
| 243 |
|
---|
| 244 | TypeDecl * postmutate( TypeDecl * td );
|
---|
| 245 | Expression * postmutate( DimensionExpr * de );
|
---|
| 246 | Expression * postmutate( Expression * e );
|
---|
| 247 | };
|
---|
| 248 |
|
---|
[2b79a70] | 249 | struct FixObjectType : public WithIndexer {
|
---|
| 250 | /// resolves typeof type in object, function, and type declarations
|
---|
| 251 | static void fix( std::list< Declaration * > & translationUnit );
|
---|
| 252 |
|
---|
| 253 | void previsit( ObjectDecl * );
|
---|
| 254 | void previsit( FunctionDecl * );
|
---|
| 255 | void previsit( TypeDecl * );
|
---|
| 256 | };
|
---|
| 257 |
|
---|
[09867ec] | 258 | struct InitializerLength {
|
---|
[fbd7ad6] | 259 | /// for array types without an explicit length, compute the length and store it so that it
|
---|
| 260 | /// is known to the rest of the phases. For example,
|
---|
| 261 | /// int x[] = { 1, 2, 3 };
|
---|
| 262 | /// int y[][2] = { { 1, 2, 3 }, { 1, 2, 3 } };
|
---|
| 263 | /// here x and y are known at compile-time to have length 3, so change this into
|
---|
| 264 | /// int x[3] = { 1, 2, 3 };
|
---|
| 265 | /// int y[3][2] = { { 1, 2, 3 }, { 1, 2, 3 } };
|
---|
| 266 | static void computeLength( std::list< Declaration * > & translationUnit );
|
---|
| 267 |
|
---|
[0db6fc0] | 268 | void previsit( ObjectDecl * objDecl );
|
---|
[09867ec] | 269 | };
|
---|
| 270 |
|
---|
| 271 | struct ArrayLength : public WithIndexer {
|
---|
| 272 | static void computeLength( std::list< Declaration * > & translationUnit );
|
---|
| 273 |
|
---|
[3ff4c1e] | 274 | void previsit( ArrayType * arrayType );
|
---|
[fbd7ad6] | 275 | };
|
---|
| 276 |
|
---|
[d24d4e1] | 277 | struct CompoundLiteral final : public WithDeclsToAdd, public WithVisitorRef<CompoundLiteral> {
|
---|
[68fe077a] | 278 | Type::StorageClasses storageClasses;
|
---|
[630a82a] | 279 |
|
---|
[ef5b828] | 280 | void premutate( ObjectDecl * objectDecl );
|
---|
| 281 | Expression * postmutate( CompoundLiteralExpr * compLitExpr );
|
---|
[9cb8e88d] | 282 | };
|
---|
| 283 |
|
---|
[5809461] | 284 | struct LabelAddressFixer final : public WithGuards {
|
---|
| 285 | std::set< Label > labels;
|
---|
| 286 |
|
---|
| 287 | void premutate( FunctionDecl * funcDecl );
|
---|
| 288 | Expression * postmutate( AddressExpr * addrExpr );
|
---|
| 289 | };
|
---|
[4fbdfae0] | 290 |
|
---|
[b9f8274] | 291 | void validate( std::list< Declaration * > &translationUnit, __attribute__((unused)) bool doDebug ) {
|
---|
[15f5c5e] | 292 | PassVisitor<HoistTypeDecls> hoistDecls;
|
---|
[3c0d4cd] | 293 | {
|
---|
| 294 | Stats::Heap::newPass("validate-A");
|
---|
| 295 | Stats::Time::BlockGuard guard("validate-A");
|
---|
[98538288] | 296 | VerifyCtorDtorAssign::verify( translationUnit ); // must happen before autogen, because autogen examines existing ctor/dtors
|
---|
[3c0d4cd] | 297 | acceptAll( translationUnit, hoistDecls );
|
---|
| 298 | ReplaceTypedef::replaceTypedef( translationUnit );
|
---|
| 299 | ReturnTypeFixer::fix( translationUnit ); // must happen before autogen
|
---|
[9939dc3] | 300 | decayEnumsAndPointers( translationUnit ); // must happen before VerifyCtorDtorAssign, because void return objects should not exist; before LinkReferenceToTypes_old because it is an indexer and needs correct types for mangling
|
---|
[3c0d4cd] | 301 | }
|
---|
[5dcb881] | 302 | PassVisitor<FixQualifiedTypes> fixQual;
|
---|
[3c0d4cd] | 303 | {
|
---|
| 304 | Stats::Heap::newPass("validate-B");
|
---|
| 305 | Stats::Time::BlockGuard guard("validate-B");
|
---|
[72e76fd] | 306 | linkReferenceToTypes( translationUnit ); // Must happen before auto-gen, because it uses the sized flag.
|
---|
[6e50a6b] | 307 | mutateAll( translationUnit, fixQual ); // must happen after LinkReferenceToTypes_old, because aggregate members are accessed
|
---|
| 308 | HoistStruct::hoistStruct( translationUnit );
|
---|
| 309 | EliminateTypedef::eliminateTypedef( translationUnit );
|
---|
[3c0d4cd] | 310 | }
|
---|
[5dcb881] | 311 | PassVisitor<ValidateGenericParameters> genericParams;
|
---|
| 312 | PassVisitor<ResolveEnumInitializers> rei( nullptr );
|
---|
[3c0d4cd] | 313 | {
|
---|
| 314 | Stats::Heap::newPass("validate-C");
|
---|
| 315 | Stats::Time::BlockGuard guard("validate-C");
|
---|
[6e50a6b] | 316 | Stats::Time::TimeBlock("Validate Generic Parameters", [&]() {
|
---|
| 317 | acceptAll( translationUnit, genericParams ); // check as early as possible - can't happen before LinkReferenceToTypes_old; observed failing when attempted before eliminateTypedef
|
---|
| 318 | });
|
---|
| 319 | Stats::Time::TimeBlock("Translate Dimensions", [&]() {
|
---|
| 320 | TranslateDimensionGenericParameters::translateDimensions( translationUnit );
|
---|
| 321 | });
|
---|
[7c919559] | 322 | if (!useNewAST) {
|
---|
[6e50a6b] | 323 | Stats::Time::TimeBlock("Resolve Enum Initializers", [&]() {
|
---|
| 324 | acceptAll( translationUnit, rei ); // must happen after translateDimensions because rei needs identifier lookup, which needs name mangling
|
---|
| 325 | });
|
---|
[7c919559] | 326 | }
|
---|
[6e50a6b] | 327 | Stats::Time::TimeBlock("Check Function Returns", [&]() {
|
---|
| 328 | ReturnChecker::checkFunctionReturns( translationUnit );
|
---|
| 329 | });
|
---|
| 330 | Stats::Time::TimeBlock("Fix Return Statements", [&]() {
|
---|
| 331 | InitTweak::fixReturnStatements( translationUnit ); // must happen before autogen
|
---|
| 332 | });
|
---|
[3c0d4cd] | 333 | }
|
---|
| 334 | {
|
---|
| 335 | Stats::Heap::newPass("validate-D");
|
---|
| 336 | Stats::Time::BlockGuard guard("validate-D");
|
---|
[c884f2d] | 337 | Stats::Time::TimeBlock("Apply Concurrent Keywords", [&]() {
|
---|
| 338 | Concurrency::applyKeywords( translationUnit );
|
---|
| 339 | });
|
---|
| 340 | Stats::Time::TimeBlock("Forall Pointer Decay", [&]() {
|
---|
[9490621] | 341 | decayForallPointers( translationUnit ); // must happen before autogenerateRoutines, after Concurrency::applyKeywords because uniqueIds must be set on declaration before resolution
|
---|
[c884f2d] | 342 | });
|
---|
| 343 | Stats::Time::TimeBlock("Hoist Control Declarations", [&]() {
|
---|
| 344 | ControlStruct::hoistControlDecls( translationUnit ); // hoist initialization out of for statements; must happen before autogenerateRoutines
|
---|
| 345 | });
|
---|
| 346 | Stats::Time::TimeBlock("Generate Autogen routines", [&]() {
|
---|
[c1ed2ee] | 347 | autogenerateRoutines( translationUnit ); // moved up, used to be below compoundLiteral - currently needs EnumAndPointerDecay_old
|
---|
[c884f2d] | 348 | });
|
---|
[3c0d4cd] | 349 | }
|
---|
[5dcb881] | 350 | PassVisitor<CompoundLiteral> compoundliteral;
|
---|
[3c0d4cd] | 351 | {
|
---|
| 352 | Stats::Heap::newPass("validate-E");
|
---|
| 353 | Stats::Time::BlockGuard guard("validate-E");
|
---|
[c884f2d] | 354 | Stats::Time::TimeBlock("Implement Mutex Func", [&]() {
|
---|
| 355 | Concurrency::implementMutexFuncs( translationUnit );
|
---|
| 356 | });
|
---|
| 357 | Stats::Time::TimeBlock("Implement Thread Start", [&]() {
|
---|
| 358 | Concurrency::implementThreadStarter( translationUnit );
|
---|
| 359 | });
|
---|
| 360 | Stats::Time::TimeBlock("Compound Literal", [&]() {
|
---|
| 361 | mutateAll( translationUnit, compoundliteral );
|
---|
| 362 | });
|
---|
[16ba4a6f] | 363 | if (!useNewAST) {
|
---|
| 364 | Stats::Time::TimeBlock("Resolve With Expressions", [&]() {
|
---|
| 365 | ResolvExpr::resolveWithExprs( translationUnit ); // must happen before FixObjectType because user-code is resolved and may contain with variables
|
---|
| 366 | });
|
---|
| 367 | }
|
---|
[3c0d4cd] | 368 | }
|
---|
[5dcb881] | 369 | PassVisitor<LabelAddressFixer> labelAddrFixer;
|
---|
[3c0d4cd] | 370 | {
|
---|
| 371 | Stats::Heap::newPass("validate-F");
|
---|
| 372 | Stats::Time::BlockGuard guard("validate-F");
|
---|
[16ba4a6f] | 373 | if (!useNewAST) {
|
---|
| 374 | Stats::Time::TimeCall("Fix Object Type",
|
---|
| 375 | FixObjectType::fix, translationUnit);
|
---|
| 376 | }
|
---|
[09867ec] | 377 | Stats::Time::TimeCall("Initializer Length",
|
---|
| 378 | InitializerLength::computeLength, translationUnit);
|
---|
| 379 | if (!useNewAST) {
|
---|
| 380 | Stats::Time::TimeCall("Array Length",
|
---|
| 381 | ArrayLength::computeLength, translationUnit);
|
---|
| 382 | }
|
---|
[095b99a] | 383 | Stats::Time::TimeCall("Find Special Declarations",
|
---|
| 384 | Validate::findSpecialDecls, translationUnit);
|
---|
| 385 | Stats::Time::TimeCall("Fix Label Address",
|
---|
| 386 | mutateAll<LabelAddressFixer>, translationUnit, labelAddrFixer);
|
---|
[16ba4a6f] | 387 | if (!useNewAST) {
|
---|
| 388 | Stats::Time::TimeCall("Handle Attributes",
|
---|
| 389 | Validate::handleAttributes, translationUnit);
|
---|
| 390 | }
|
---|
[3c0d4cd] | 391 | }
|
---|
[a08ba92] | 392 | }
|
---|
[9cb8e88d] | 393 |
|
---|
[15f5c5e] | 394 | void HoistTypeDecls::handleType( Type * type ) {
|
---|
[29f9e20] | 395 | // some type declarations are buried in expressions and not easy to hoist during parsing; hoist them here
|
---|
| 396 | AggregateDecl * aggr = nullptr;
|
---|
| 397 | if ( StructInstType * inst = dynamic_cast< StructInstType * >( type ) ) {
|
---|
| 398 | aggr = inst->baseStruct;
|
---|
| 399 | } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * >( type ) ) {
|
---|
| 400 | aggr = inst->baseUnion;
|
---|
| 401 | } else if ( EnumInstType * inst = dynamic_cast< EnumInstType * >( type ) ) {
|
---|
| 402 | aggr = inst->baseEnum;
|
---|
| 403 | }
|
---|
| 404 | if ( aggr && aggr->body ) {
|
---|
| 405 | declsToAddBefore.push_front( aggr );
|
---|
| 406 | }
|
---|
| 407 | }
|
---|
| 408 |
|
---|
[15f5c5e] | 409 | void HoistTypeDecls::previsit( SizeofExpr * expr ) {
|
---|
[29f9e20] | 410 | handleType( expr->type );
|
---|
| 411 | }
|
---|
| 412 |
|
---|
[15f5c5e] | 413 | void HoistTypeDecls::previsit( AlignofExpr * expr ) {
|
---|
[29f9e20] | 414 | handleType( expr->type );
|
---|
| 415 | }
|
---|
| 416 |
|
---|
[15f5c5e] | 417 | void HoistTypeDecls::previsit( UntypedOffsetofExpr * expr ) {
|
---|
[29f9e20] | 418 | handleType( expr->type );
|
---|
| 419 | }
|
---|
| 420 |
|
---|
[95d09bdb] | 421 | void HoistTypeDecls::previsit( CompoundLiteralExpr * expr ) {
|
---|
| 422 | handleType( expr->result );
|
---|
| 423 | }
|
---|
| 424 |
|
---|
[29f9e20] | 425 |
|
---|
[a12c81f3] | 426 | Type * FixQualifiedTypes::postmutate( QualifiedType * qualType ) {
|
---|
| 427 | Type * parent = qualType->parent;
|
---|
| 428 | Type * child = qualType->child;
|
---|
| 429 | if ( dynamic_cast< GlobalScopeType * >( qualType->parent ) ) {
|
---|
| 430 | // .T => lookup T at global scope
|
---|
[062e8df] | 431 | if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( child ) ) {
|
---|
[a12c81f3] | 432 | auto td = indexer.globalLookupType( inst->name );
|
---|
[062e8df] | 433 | if ( ! td ) {
|
---|
| 434 | SemanticError( qualType->location, toString("Use of undefined global type ", inst->name) );
|
---|
| 435 | }
|
---|
[a12c81f3] | 436 | auto base = td->base;
|
---|
[062e8df] | 437 | assert( base );
|
---|
[8a3ecb9] | 438 | Type * ret = base->clone();
|
---|
| 439 | ret->get_qualifiers() = qualType->get_qualifiers();
|
---|
| 440 | return ret;
|
---|
[a12c81f3] | 441 | } else {
|
---|
[062e8df] | 442 | // .T => T is not a type name
|
---|
| 443 | assertf( false, "unhandled global qualified child type: %s", toCString(child) );
|
---|
[a12c81f3] | 444 | }
|
---|
| 445 | } else {
|
---|
| 446 | // S.T => S must be an aggregate type, find the declaration for T in S.
|
---|
| 447 | AggregateDecl * aggr = nullptr;
|
---|
| 448 | if ( StructInstType * inst = dynamic_cast< StructInstType * >( parent ) ) {
|
---|
| 449 | aggr = inst->baseStruct;
|
---|
| 450 | } else if ( UnionInstType * inst = dynamic_cast< UnionInstType * > ( parent ) ) {
|
---|
| 451 | aggr = inst->baseUnion;
|
---|
| 452 | } else {
|
---|
[062e8df] | 453 | SemanticError( qualType->location, toString("Qualified type requires an aggregate on the left, but has: ", parent) );
|
---|
[a12c81f3] | 454 | }
|
---|
| 455 | assert( aggr ); // TODO: need to handle forward declarations
|
---|
| 456 | for ( Declaration * member : aggr->members ) {
|
---|
[7e08acf] | 457 | if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( child ) ) {
|
---|
[8a3ecb9] | 458 | // name on the right is a typedef
|
---|
[a12c81f3] | 459 | if ( NamedTypeDecl * aggr = dynamic_cast< NamedTypeDecl * > ( member ) ) {
|
---|
| 460 | if ( aggr->name == inst->name ) {
|
---|
[062e8df] | 461 | assert( aggr->base );
|
---|
[8a3ecb9] | 462 | Type * ret = aggr->base->clone();
|
---|
| 463 | ret->get_qualifiers() = qualType->get_qualifiers();
|
---|
[7e08acf] | 464 | TypeSubstitution sub = parent->genericSubstitution();
|
---|
| 465 | sub.apply(ret);
|
---|
[8a3ecb9] | 466 | return ret;
|
---|
[a12c81f3] | 467 | }
|
---|
| 468 | }
|
---|
| 469 | } else {
|
---|
| 470 | // S.T - S is not an aggregate => error
|
---|
| 471 | assertf( false, "unhandled qualified child type: %s", toCString(qualType) );
|
---|
| 472 | }
|
---|
| 473 | }
|
---|
| 474 | // failed to find a satisfying definition of type
|
---|
[062e8df] | 475 | SemanticError( qualType->location, toString("Undefined type in qualified type: ", qualType) );
|
---|
[a12c81f3] | 476 | }
|
---|
| 477 |
|
---|
| 478 | // ... may want to link canonical SUE definition to each forward decl so that it becomes easier to lookup?
|
---|
| 479 | }
|
---|
| 480 |
|
---|
| 481 |
|
---|
[a08ba92] | 482 | void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) {
|
---|
[a09e45b] | 483 | PassVisitor<HoistStruct> hoister;
|
---|
| 484 | acceptAll( translationUnit, hoister );
|
---|
[a08ba92] | 485 | }
|
---|
[c8ffe20b] | 486 |
|
---|
[ef5b828] | 487 | bool shouldHoist( Declaration * decl ) {
|
---|
[0f40912] | 488 | return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl ) || dynamic_cast< StaticAssertDecl * >( decl );
|
---|
[a08ba92] | 489 | }
|
---|
[c0aa336] | 490 |
|
---|
[d419d8e] | 491 | namespace {
|
---|
| 492 | void qualifiedName( AggregateDecl * aggr, std::ostringstream & ss ) {
|
---|
| 493 | if ( aggr->parent ) qualifiedName( aggr->parent, ss );
|
---|
| 494 | ss << "__" << aggr->name;
|
---|
| 495 | }
|
---|
| 496 |
|
---|
| 497 | // mangle nested type names using entire parent chain
|
---|
| 498 | std::string qualifiedName( AggregateDecl * aggr ) {
|
---|
| 499 | std::ostringstream ss;
|
---|
| 500 | qualifiedName( aggr, ss );
|
---|
| 501 | return ss.str();
|
---|
| 502 | }
|
---|
| 503 | }
|
---|
| 504 |
|
---|
[a08ba92] | 505 | template< typename AggDecl >
|
---|
[ef5b828] | 506 | void HoistStruct::handleAggregate( AggDecl * aggregateDecl ) {
|
---|
[bdad6eb7] | 507 | if ( parentAggr ) {
|
---|
[d419d8e] | 508 | aggregateDecl->parent = parentAggr;
|
---|
| 509 | aggregateDecl->name = qualifiedName( aggregateDecl );
|
---|
[0dd3a2f] | 510 | // Add elements in stack order corresponding to nesting structure.
|
---|
[a09e45b] | 511 | declsToAddBefore.push_front( aggregateDecl );
|
---|
[0dd3a2f] | 512 | } else {
|
---|
[bdad6eb7] | 513 | GuardValue( parentAggr );
|
---|
| 514 | parentAggr = aggregateDecl;
|
---|
[0dd3a2f] | 515 | } // if
|
---|
| 516 | // Always remove the hoisted aggregate from the inner structure.
|
---|
[0f40912] | 517 | GuardAction( [aggregateDecl]() { filter( aggregateDecl->members, shouldHoist, false ); } );
|
---|
[a08ba92] | 518 | }
|
---|
[c8ffe20b] | 519 |
|
---|
[0f40912] | 520 | void HoistStruct::previsit( StaticAssertDecl * assertDecl ) {
|
---|
| 521 | if ( parentAggr ) {
|
---|
| 522 | declsToAddBefore.push_back( assertDecl );
|
---|
| 523 | }
|
---|
| 524 | }
|
---|
| 525 |
|
---|
[a09e45b] | 526 | void HoistStruct::previsit( StructDecl * aggregateDecl ) {
|
---|
[0dd3a2f] | 527 | handleAggregate( aggregateDecl );
|
---|
[a08ba92] | 528 | }
|
---|
[c8ffe20b] | 529 |
|
---|
[a09e45b] | 530 | void HoistStruct::previsit( UnionDecl * aggregateDecl ) {
|
---|
[0dd3a2f] | 531 | handleAggregate( aggregateDecl );
|
---|
[a08ba92] | 532 | }
|
---|
[c8ffe20b] | 533 |
|
---|
[d419d8e] | 534 | void HoistStruct::previsit( StructInstType * type ) {
|
---|
| 535 | // need to reset type name after expanding to qualified name
|
---|
| 536 | assert( type->baseStruct );
|
---|
| 537 | type->name = type->baseStruct->name;
|
---|
| 538 | }
|
---|
| 539 |
|
---|
| 540 | void HoistStruct::previsit( UnionInstType * type ) {
|
---|
| 541 | assert( type->baseUnion );
|
---|
| 542 | type->name = type->baseUnion->name;
|
---|
| 543 | }
|
---|
| 544 |
|
---|
| 545 | void HoistStruct::previsit( EnumInstType * type ) {
|
---|
| 546 | assert( type->baseEnum );
|
---|
| 547 | type->name = type->baseEnum->name;
|
---|
| 548 | }
|
---|
| 549 |
|
---|
| 550 |
|
---|
[ef5b828] | 551 | bool isTypedef( Declaration * decl ) {
|
---|
[69918cea] | 552 | return dynamic_cast< TypedefDecl * >( decl );
|
---|
| 553 | }
|
---|
| 554 |
|
---|
| 555 | void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
|
---|
| 556 | PassVisitor<EliminateTypedef> eliminator;
|
---|
| 557 | acceptAll( translationUnit, eliminator );
|
---|
| 558 | filter( translationUnit, isTypedef, true );
|
---|
| 559 | }
|
---|
| 560 |
|
---|
| 561 | template< typename AggDecl >
|
---|
[ef5b828] | 562 | void EliminateTypedef::handleAggregate( AggDecl * aggregateDecl ) {
|
---|
[69918cea] | 563 | filter( aggregateDecl->members, isTypedef, true );
|
---|
| 564 | }
|
---|
| 565 |
|
---|
| 566 | void EliminateTypedef::previsit( StructDecl * aggregateDecl ) {
|
---|
| 567 | handleAggregate( aggregateDecl );
|
---|
| 568 | }
|
---|
| 569 |
|
---|
| 570 | void EliminateTypedef::previsit( UnionDecl * aggregateDecl ) {
|
---|
| 571 | handleAggregate( aggregateDecl );
|
---|
| 572 | }
|
---|
| 573 |
|
---|
| 574 | void EliminateTypedef::previsit( CompoundStmt * compoundStmt ) {
|
---|
| 575 | // remove and delete decl stmts
|
---|
| 576 | filter( compoundStmt->kids, [](Statement * stmt) {
|
---|
[ef5b828] | 577 | if ( DeclStmt * declStmt = dynamic_cast< DeclStmt * >( stmt ) ) {
|
---|
[69918cea] | 578 | if ( dynamic_cast< TypedefDecl * >( declStmt->decl ) ) {
|
---|
| 579 | return true;
|
---|
| 580 | } // if
|
---|
| 581 | } // if
|
---|
| 582 | return false;
|
---|
| 583 | }, true);
|
---|
| 584 | }
|
---|
| 585 |
|
---|
[be9036d] | 586 | // expand assertions from trait instance, performing the appropriate type variable substitutions
|
---|
| 587 | template< typename Iterator >
|
---|
| 588 | void expandAssertions( TraitInstType * inst, Iterator out ) {
|
---|
[eaa6430] | 589 | assertf( inst->baseTrait, "Trait instance not linked to base trait: %s", toCString( inst ) );
|
---|
[be9036d] | 590 | std::list< DeclarationWithType * > asserts;
|
---|
| 591 | for ( Declaration * decl : inst->baseTrait->members ) {
|
---|
[e3e16bc] | 592 | asserts.push_back( strict_dynamic_cast<DeclarationWithType *>( decl->clone() ) );
|
---|
[2c57025] | 593 | }
|
---|
[be9036d] | 594 | // substitute trait decl parameters for instance parameters
|
---|
| 595 | applySubstitution( inst->baseTrait->parameters.begin(), inst->baseTrait->parameters.end(), inst->parameters.begin(), asserts.begin(), asserts.end(), out );
|
---|
| 596 | }
|
---|
| 597 |
|
---|
[6e50a6b] | 598 | ResolveEnumInitializers::ResolveEnumInitializers( const Indexer * other_indexer ) : WithIndexer( true ) {
|
---|
| 599 | if ( other_indexer ) {
|
---|
| 600 | local_indexer = other_indexer;
|
---|
| 601 | } else {
|
---|
| 602 | local_indexer = &indexer;
|
---|
| 603 | } // if
|
---|
| 604 | }
|
---|
| 605 |
|
---|
| 606 | void ResolveEnumInitializers::postvisit( EnumDecl * enumDecl ) {
|
---|
| 607 | if ( enumDecl->body ) {
|
---|
| 608 | for ( Declaration * member : enumDecl->members ) {
|
---|
| 609 | ObjectDecl * field = strict_dynamic_cast<ObjectDecl *>( member );
|
---|
| 610 | if ( field->init ) {
|
---|
| 611 | // need to resolve enumerator initializers early so that other passes that determine if an expression is constexpr have the appropriate information.
|
---|
| 612 | SingleInit * init = strict_dynamic_cast<SingleInit *>( field->init );
|
---|
[4559b34] | 613 | if ( !enumDecl->base || dynamic_cast<BasicType *>(enumDecl->base))
|
---|
| 614 | ResolvExpr::findSingleExpression( init->value, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), indexer );
|
---|
| 615 | else {
|
---|
| 616 | if (dynamic_cast<PointerType *>(enumDecl->base)) {
|
---|
| 617 | auto typePtr = dynamic_cast<PointerType *>(enumDecl->base);
|
---|
| 618 | ResolvExpr::findSingleExpression( init->value,
|
---|
| 619 | new PointerType( Type::Qualifiers(), typePtr->base ), indexer );
|
---|
| 620 | } else {
|
---|
| 621 | ResolvExpr::findSingleExpression( init->value, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), indexer );
|
---|
| 622 | }
|
---|
| 623 | }
|
---|
[6e50a6b] | 624 | }
|
---|
| 625 | }
|
---|
[4559b34] | 626 |
|
---|
[6e50a6b] | 627 | } // if
|
---|
| 628 | }
|
---|
| 629 |
|
---|
[4a9ccc3] | 630 | /// Fix up assertions - flattens assertion lists, removing all trait instances
|
---|
[8b11840] | 631 | void forallFixer( std::list< TypeDecl * > & forall, BaseSyntaxNode * node ) {
|
---|
| 632 | for ( TypeDecl * type : forall ) {
|
---|
[be9036d] | 633 | std::list< DeclarationWithType * > asserts;
|
---|
| 634 | asserts.splice( asserts.end(), type->assertions );
|
---|
| 635 | // expand trait instances into their members
|
---|
| 636 | for ( DeclarationWithType * assertion : asserts ) {
|
---|
[ef5b828] | 637 | if ( TraitInstType * traitInst = dynamic_cast< TraitInstType * >( assertion->get_type() ) ) {
|
---|
[be9036d] | 638 | // expand trait instance into all of its members
|
---|
| 639 | expandAssertions( traitInst, back_inserter( type->assertions ) );
|
---|
| 640 | delete traitInst;
|
---|
| 641 | } else {
|
---|
| 642 | // pass other assertions through
|
---|
| 643 | type->assertions.push_back( assertion );
|
---|
| 644 | } // if
|
---|
| 645 | } // for
|
---|
| 646 | // apply FixFunction to every assertion to check for invalid void type
|
---|
| 647 | for ( DeclarationWithType *& assertion : type->assertions ) {
|
---|
[4bda2cf] | 648 | bool isVoid = fixFunction( assertion );
|
---|
| 649 | if ( isVoid ) {
|
---|
[a16764a6] | 650 | SemanticError( node, "invalid type void in assertion of function " );
|
---|
[be9036d] | 651 | } // if
|
---|
| 652 | } // for
|
---|
| 653 | // normalizeAssertions( type->assertions );
|
---|
[0dd3a2f] | 654 | } // for
|
---|
[a08ba92] | 655 | }
|
---|
[c8ffe20b] | 656 |
|
---|
[9490621] | 657 | /// Replace all traits in assertion lists with their assertions.
|
---|
| 658 | void expandTraits( std::list< TypeDecl * > & forall ) {
|
---|
| 659 | for ( TypeDecl * type : forall ) {
|
---|
| 660 | std::list< DeclarationWithType * > asserts;
|
---|
| 661 | asserts.splice( asserts.end(), type->assertions );
|
---|
| 662 | // expand trait instances into their members
|
---|
| 663 | for ( DeclarationWithType * assertion : asserts ) {
|
---|
| 664 | if ( TraitInstType * traitInst = dynamic_cast< TraitInstType * >( assertion->get_type() ) ) {
|
---|
| 665 | // expand trait instance into all of its members
|
---|
| 666 | expandAssertions( traitInst, back_inserter( type->assertions ) );
|
---|
| 667 | delete traitInst;
|
---|
| 668 | } else {
|
---|
| 669 | // pass other assertions through
|
---|
| 670 | type->assertions.push_back( assertion );
|
---|
| 671 | } // if
|
---|
| 672 | } // for
|
---|
| 673 | }
|
---|
| 674 | }
|
---|
| 675 |
|
---|
| 676 | /// Fix each function in the assertion list and check for invalid void type.
|
---|
| 677 | void fixAssertions(
|
---|
| 678 | std::list< TypeDecl * > & forall, BaseSyntaxNode * node ) {
|
---|
| 679 | for ( TypeDecl * type : forall ) {
|
---|
| 680 | for ( DeclarationWithType *& assertion : type->assertions ) {
|
---|
| 681 | bool isVoid = fixFunction( assertion );
|
---|
| 682 | if ( isVoid ) {
|
---|
| 683 | SemanticError( node, "invalid type void in assertion of function " );
|
---|
| 684 | } // if
|
---|
| 685 | } // for
|
---|
| 686 | }
|
---|
| 687 | }
|
---|
| 688 |
|
---|
[ef5b828] | 689 | void ForallPointerDecay_old::previsit( ObjectDecl * object ) {
|
---|
[3d2b7bc] | 690 | // ensure that operator names only apply to functions or function pointers
|
---|
| 691 | if ( CodeGen::isOperator( object->name ) && ! dynamic_cast< FunctionType * >( object->type->stripDeclarator() ) ) {
|
---|
| 692 | SemanticError( object->location, toCString( "operator ", object->name.c_str(), " is not a function or function pointer." ) );
|
---|
| 693 | }
|
---|
[0dd3a2f] | 694 | object->fixUniqueId();
|
---|
[a08ba92] | 695 | }
|
---|
[c8ffe20b] | 696 |
|
---|
[ef5b828] | 697 | void ForallPointerDecay_old::previsit( FunctionDecl * func ) {
|
---|
[0dd3a2f] | 698 | func->fixUniqueId();
|
---|
[a08ba92] | 699 | }
|
---|
[c8ffe20b] | 700 |
|
---|
[c1ed2ee] | 701 | void ForallPointerDecay_old::previsit( FunctionType * ftype ) {
|
---|
[bbf3fda] | 702 | forallFixer( ftype->forall, ftype );
|
---|
| 703 | }
|
---|
| 704 |
|
---|
[c1ed2ee] | 705 | void ForallPointerDecay_old::previsit( StructDecl * aggrDecl ) {
|
---|
[bd7e609] | 706 | forallFixer( aggrDecl->parameters, aggrDecl );
|
---|
| 707 | }
|
---|
| 708 |
|
---|
[c1ed2ee] | 709 | void ForallPointerDecay_old::previsit( UnionDecl * aggrDecl ) {
|
---|
[bd7e609] | 710 | forallFixer( aggrDecl->parameters, aggrDecl );
|
---|
| 711 | }
|
---|
| 712 |
|
---|
[de91427b] | 713 | void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) {
|
---|
[0db6fc0] | 714 | PassVisitor<ReturnChecker> checker;
|
---|
[de91427b] | 715 | acceptAll( translationUnit, checker );
|
---|
| 716 | }
|
---|
| 717 |
|
---|
[0db6fc0] | 718 | void ReturnChecker::previsit( FunctionDecl * functionDecl ) {
|
---|
[0508ab3] | 719 | GuardValue( returnVals );
|
---|
[de91427b] | 720 | returnVals = functionDecl->get_functionType()->get_returnVals();
|
---|
| 721 | }
|
---|
| 722 |
|
---|
[0db6fc0] | 723 | void ReturnChecker::previsit( ReturnStmt * returnStmt ) {
|
---|
[74d1804] | 724 | // Previously this also checked for the existence of an expr paired with no return values on
|
---|
| 725 | // the function return type. This is incorrect, since you can have an expression attached to
|
---|
| 726 | // a return statement in a void-returning function in C. The expression is treated as if it
|
---|
| 727 | // were cast to void.
|
---|
[30f9072] | 728 | if ( ! returnStmt->get_expr() && returnVals.size() != 0 ) {
|
---|
[a16764a6] | 729 | SemanticError( returnStmt, "Non-void function returns no values: " );
|
---|
[de91427b] | 730 | }
|
---|
| 731 | }
|
---|
| 732 |
|
---|
| 733 |
|
---|
[48ed81c] | 734 | void ReplaceTypedef::replaceTypedef( std::list< Declaration * > &translationUnit ) {
|
---|
| 735 | PassVisitor<ReplaceTypedef> eliminator;
|
---|
[0dd3a2f] | 736 | mutateAll( translationUnit, eliminator );
|
---|
[a506df4] | 737 | if ( eliminator.pass.typedefNames.count( "size_t" ) ) {
|
---|
[5f98ce5] | 738 | // grab and remember declaration of size_t
|
---|
[2bfc6b2] | 739 | Validate::SizeType = eliminator.pass.typedefNames["size_t"].first->base->clone();
|
---|
[5f98ce5] | 740 | } else {
|
---|
[40e636a] | 741 | // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong
|
---|
| 742 | // eventually should have a warning for this case.
|
---|
[2bfc6b2] | 743 | Validate::SizeType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
|
---|
[5f98ce5] | 744 | }
|
---|
[a08ba92] | 745 | }
|
---|
[c8ffe20b] | 746 |
|
---|
[48ed81c] | 747 | void ReplaceTypedef::premutate( QualifiedType * ) {
|
---|
| 748 | visit_children = false;
|
---|
| 749 | }
|
---|
| 750 |
|
---|
| 751 | Type * ReplaceTypedef::postmutate( QualifiedType * qualType ) {
|
---|
| 752 | // replacing typedefs only makes sense for the 'oldest ancestor' of the qualified type
|
---|
[ef5b828] | 753 | qualType->parent = qualType->parent->acceptMutator( * visitor );
|
---|
[48ed81c] | 754 | return qualType;
|
---|
| 755 | }
|
---|
| 756 |
|
---|
[a7c31e0] | 757 | static bool isNonParameterAttribute( Attribute * attr ) {
|
---|
| 758 | static const std::vector<std::string> bad_names = {
|
---|
| 759 | "aligned", "__aligned__",
|
---|
| 760 | };
|
---|
| 761 | for ( auto name : bad_names ) {
|
---|
| 762 | if ( name == attr->name ) {
|
---|
| 763 | return true;
|
---|
| 764 | }
|
---|
| 765 | }
|
---|
| 766 | return false;
|
---|
| 767 | }
|
---|
| 768 |
|
---|
[48ed81c] | 769 | Type * ReplaceTypedef::postmutate( TypeInstType * typeInst ) {
|
---|
[9cb8e88d] | 770 | // instances of typedef types will come here. If it is an instance
|
---|
[cc79d97] | 771 | // of a typdef type, link the instance to its actual type.
|
---|
[0b3b2ae] | 772 | TypedefMap::const_iterator def = typedefNames.find( typeInst->name );
|
---|
[0dd3a2f] | 773 | if ( def != typedefNames.end() ) {
|
---|
[ef5b828] | 774 | Type * ret = def->second.first->base->clone();
|
---|
[e82ef13] | 775 | ret->location = typeInst->location;
|
---|
[6f95000] | 776 | ret->get_qualifiers() |= typeInst->get_qualifiers();
|
---|
[a7c31e0] | 777 | // GCC ignores certain attributes if they arrive by typedef, this mimics that.
|
---|
| 778 | if ( inFunctionType ) {
|
---|
| 779 | ret->attributes.remove_if( isNonParameterAttribute );
|
---|
[1f370451] | 780 | }
|
---|
[a7c31e0] | 781 | ret->attributes.splice( ret->attributes.end(), typeInst->attributes );
|
---|
[0215a76f] | 782 | // place instance parameters on the typedef'd type
|
---|
[f53836b] | 783 | if ( ! typeInst->parameters.empty() ) {
|
---|
[ef5b828] | 784 | ReferenceToType * rtt = dynamic_cast<ReferenceToType *>(ret);
|
---|
[0215a76f] | 785 | if ( ! rtt ) {
|
---|
[a16764a6] | 786 | SemanticError( typeInst->location, "Cannot apply type parameters to base type of " + typeInst->name );
|
---|
[0215a76f] | 787 | }
|
---|
[0b3b2ae] | 788 | rtt->parameters.clear();
|
---|
[f53836b] | 789 | cloneAll( typeInst->parameters, rtt->parameters );
|
---|
[ef5b828] | 790 | mutateAll( rtt->parameters, * visitor ); // recursively fix typedefs on parameters
|
---|
[1db21619] | 791 | } // if
|
---|
[0dd3a2f] | 792 | delete typeInst;
|
---|
| 793 | return ret;
|
---|
[679864e1] | 794 | } else {
|
---|
[0b3b2ae] | 795 | TypeDeclMap::const_iterator base = typedeclNames.find( typeInst->name );
|
---|
[062e8df] | 796 | if ( base == typedeclNames.end() ) {
|
---|
| 797 | SemanticError( typeInst->location, toString("Use of undefined type ", typeInst->name) );
|
---|
| 798 | }
|
---|
[1e8b02f5] | 799 | typeInst->set_baseType( base->second );
|
---|
[062e8df] | 800 | return typeInst;
|
---|
[0dd3a2f] | 801 | } // if
|
---|
[062e8df] | 802 | assert( false );
|
---|
[a08ba92] | 803 | }
|
---|
[c8ffe20b] | 804 |
|
---|
[f53836b] | 805 | struct VarLenChecker : WithShortCircuiting {
|
---|
| 806 | void previsit( FunctionType * ) { visit_children = false; }
|
---|
| 807 | void previsit( ArrayType * at ) {
|
---|
| 808 | isVarLen |= at->isVarLen;
|
---|
| 809 | }
|
---|
| 810 | bool isVarLen = false;
|
---|
| 811 | };
|
---|
| 812 |
|
---|
| 813 | bool isVariableLength( Type * t ) {
|
---|
| 814 | PassVisitor<VarLenChecker> varLenChecker;
|
---|
| 815 | maybeAccept( t, varLenChecker );
|
---|
| 816 | return varLenChecker.pass.isVarLen;
|
---|
| 817 | }
|
---|
| 818 |
|
---|
[48ed81c] | 819 | Declaration * ReplaceTypedef::postmutate( TypedefDecl * tyDecl ) {
|
---|
[0b3b2ae] | 820 | if ( typedefNames.count( tyDecl->name ) == 1 && typedefNames[ tyDecl->name ].second == scopeLevel ) {
|
---|
[9cb8e88d] | 821 | // typedef to the same name from the same scope
|
---|
[cc79d97] | 822 | // must be from the same type
|
---|
| 823 |
|
---|
[0b3b2ae] | 824 | Type * t1 = tyDecl->base;
|
---|
| 825 | Type * t2 = typedefNames[ tyDecl->name ].first->base;
|
---|
[1cbca6e] | 826 | if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) {
|
---|
[a16764a6] | 827 | SemanticError( tyDecl->location, "Cannot redefine typedef: " + tyDecl->name );
|
---|
[f53836b] | 828 | }
|
---|
[4b97770] | 829 | // Cannot redefine VLA typedefs. Note: this is slightly incorrect, because our notion of VLAs
|
---|
| 830 | // at this point in the translator is imprecise. In particular, this will disallow redefining typedefs
|
---|
| 831 | // with arrays whose dimension is an enumerator or a cast of a constant/enumerator. The effort required
|
---|
| 832 | // to fix this corner case likely outweighs the utility of allowing it.
|
---|
[f53836b] | 833 | if ( isVariableLength( t1 ) || isVariableLength( t2 ) ) {
|
---|
[a16764a6] | 834 | SemanticError( tyDecl->location, "Cannot redefine typedef: " + tyDecl->name );
|
---|
[85c4ef0] | 835 | }
|
---|
[cc79d97] | 836 | } else {
|
---|
[0b3b2ae] | 837 | typedefNames[ tyDecl->name ] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel );
|
---|
[cc79d97] | 838 | } // if
|
---|
| 839 |
|
---|
[0dd3a2f] | 840 | // When a typedef is a forward declaration:
|
---|
| 841 | // typedef struct screen SCREEN;
|
---|
| 842 | // the declaration portion must be retained:
|
---|
| 843 | // struct screen;
|
---|
| 844 | // because the expansion of the typedef is:
|
---|
[ef5b828] | 845 | // void rtn( SCREEN * p ) => void rtn( struct screen * p )
|
---|
[0dd3a2f] | 846 | // hence the type-name "screen" must be defined.
|
---|
| 847 | // Note, qualifiers on the typedef are superfluous for the forward declaration.
|
---|
[6f95000] | 848 |
|
---|
[ef5b828] | 849 | Type * designatorType = tyDecl->base->stripDeclarator();
|
---|
| 850 | if ( StructInstType * aggDecl = dynamic_cast< StructInstType * >( designatorType ) ) {
|
---|
[312029a] | 851 | declsToAddBefore.push_back( new StructDecl( aggDecl->name, AggregateDecl::Struct, noAttributes, tyDecl->linkage ) );
|
---|
[ef5b828] | 852 | } else if ( UnionInstType * aggDecl = dynamic_cast< UnionInstType * >( designatorType ) ) {
|
---|
[48ed81c] | 853 | declsToAddBefore.push_back( new UnionDecl( aggDecl->name, noAttributes, tyDecl->linkage ) );
|
---|
[b0d9ff7] | 854 | } else if ( EnumInstType * enumInst = dynamic_cast< EnumInstType * >( designatorType ) ) {
|
---|
| 855 | if ( enumInst->baseEnum ) {
|
---|
| 856 | const EnumDecl * enumDecl = enumInst->baseEnum;
|
---|
| 857 | declsToAddBefore.push_back( new EnumDecl( enumDecl->name, noAttributes, enumDecl->isTyped, tyDecl->linkage, enumDecl->base ) );
|
---|
[3e54399] | 858 | } else {
|
---|
[b0d9ff7] | 859 | declsToAddBefore.push_back( new EnumDecl( enumInst->name, noAttributes, tyDecl->linkage ) );
|
---|
[3e54399] | 860 | }
|
---|
[0dd3a2f] | 861 | } // if
|
---|
[48ed81c] | 862 | return tyDecl->clone();
|
---|
[a08ba92] | 863 | }
|
---|
[c8ffe20b] | 864 |
|
---|
[48ed81c] | 865 | void ReplaceTypedef::premutate( TypeDecl * typeDecl ) {
|
---|
[21a2a7d] | 866 | typedefNames.erase( typeDecl->name );
|
---|
[0bcc2b7] | 867 | typedeclNames.insert( typeDecl->name, typeDecl );
|
---|
[a08ba92] | 868 | }
|
---|
[c8ffe20b] | 869 |
|
---|
[48ed81c] | 870 | void ReplaceTypedef::premutate( FunctionDecl * ) {
|
---|
[a506df4] | 871 | GuardScope( typedefNames );
|
---|
[0bcc2b7] | 872 | GuardScope( typedeclNames );
|
---|
[a08ba92] | 873 | }
|
---|
[c8ffe20b] | 874 |
|
---|
[48ed81c] | 875 | void ReplaceTypedef::premutate( ObjectDecl * ) {
|
---|
[a506df4] | 876 | GuardScope( typedefNames );
|
---|
[0bcc2b7] | 877 | GuardScope( typedeclNames );
|
---|
[a506df4] | 878 | }
|
---|
[dd020c0] | 879 |
|
---|
[48ed81c] | 880 | DeclarationWithType * ReplaceTypedef::postmutate( ObjectDecl * objDecl ) {
|
---|
[ef5b828] | 881 | if ( FunctionType * funtype = dynamic_cast<FunctionType *>( objDecl->type ) ) { // function type?
|
---|
[02e5ab6] | 882 | // replace the current object declaration with a function declaration
|
---|
[0b3b2ae] | 883 | FunctionDecl * newDecl = new FunctionDecl( objDecl->name, objDecl->get_storageClasses(), objDecl->linkage, funtype, 0, objDecl->attributes, objDecl->get_funcSpec() );
|
---|
| 884 | objDecl->attributes.clear();
|
---|
[dbe8f244] | 885 | objDecl->set_type( nullptr );
|
---|
[0a86a30] | 886 | delete objDecl;
|
---|
| 887 | return newDecl;
|
---|
[1db21619] | 888 | } // if
|
---|
[a506df4] | 889 | return objDecl;
|
---|
[a08ba92] | 890 | }
|
---|
[c8ffe20b] | 891 |
|
---|
[48ed81c] | 892 | void ReplaceTypedef::premutate( CastExpr * ) {
|
---|
[a506df4] | 893 | GuardScope( typedefNames );
|
---|
[0bcc2b7] | 894 | GuardScope( typedeclNames );
|
---|
[a08ba92] | 895 | }
|
---|
[c8ffe20b] | 896 |
|
---|
[48ed81c] | 897 | void ReplaceTypedef::premutate( CompoundStmt * ) {
|
---|
[a506df4] | 898 | GuardScope( typedefNames );
|
---|
[0bcc2b7] | 899 | GuardScope( typedeclNames );
|
---|
[cc79d97] | 900 | scopeLevel += 1;
|
---|
[a506df4] | 901 | GuardAction( [this](){ scopeLevel -= 1; } );
|
---|
| 902 | }
|
---|
| 903 |
|
---|
[45161b4d] | 904 | template<typename AggDecl>
|
---|
[48ed81c] | 905 | void ReplaceTypedef::addImplicitTypedef( AggDecl * aggDecl ) {
|
---|
[45161b4d] | 906 | if ( typedefNames.count( aggDecl->get_name() ) == 0 ) {
|
---|
[ef5b828] | 907 | Type * type = nullptr;
|
---|
[45161b4d] | 908 | if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( aggDecl ) ) {
|
---|
| 909 | type = new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() );
|
---|
| 910 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( aggDecl ) ) {
|
---|
| 911 | type = new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() );
|
---|
| 912 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( aggDecl ) ) {
|
---|
| 913 | type = new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() );
|
---|
| 914 | } // if
|
---|
[0b0f1dd] | 915 | TypedefDeclPtr tyDecl( new TypedefDecl( aggDecl->get_name(), aggDecl->location, Type::StorageClasses(), type, aggDecl->get_linkage() ) );
|
---|
[46f6134] | 916 | typedefNames[ aggDecl->get_name() ] = std::make_pair( std::move( tyDecl ), scopeLevel );
|
---|
[48ed81c] | 917 | // add the implicit typedef to the AST
|
---|
| 918 | declsToAddBefore.push_back( new TypedefDecl( aggDecl->get_name(), aggDecl->location, Type::StorageClasses(), type->clone(), aggDecl->get_linkage() ) );
|
---|
[45161b4d] | 919 | } // if
|
---|
| 920 | }
|
---|
[4e06c1e] | 921 |
|
---|
[48ed81c] | 922 | template< typename AggDecl >
|
---|
| 923 | void ReplaceTypedef::handleAggregate( AggDecl * aggr ) {
|
---|
| 924 | SemanticErrorException errors;
|
---|
[a506df4] | 925 |
|
---|
[48ed81c] | 926 | ValueGuard< std::list<Declaration * > > oldBeforeDecls( declsToAddBefore );
|
---|
| 927 | ValueGuard< std::list<Declaration * > > oldAfterDecls ( declsToAddAfter );
|
---|
| 928 | declsToAddBefore.clear();
|
---|
| 929 | declsToAddAfter.clear();
|
---|
[a506df4] | 930 |
|
---|
[48ed81c] | 931 | GuardScope( typedefNames );
|
---|
[0bcc2b7] | 932 | GuardScope( typedeclNames );
|
---|
[ef5b828] | 933 | mutateAll( aggr->parameters, * visitor );
|
---|
[798a8b3] | 934 | mutateAll( aggr->attributes, * visitor );
|
---|
[85c4ef0] | 935 |
|
---|
[48ed81c] | 936 | // unroll mutateAll for aggr->members so that implicit typedefs for nested types are added to the aggregate body.
|
---|
| 937 | for ( std::list< Declaration * >::iterator i = aggr->members.begin(); i != aggr->members.end(); ++i ) {
|
---|
| 938 | if ( !declsToAddAfter.empty() ) { aggr->members.splice( i, declsToAddAfter ); }
|
---|
[a506df4] | 939 |
|
---|
[48ed81c] | 940 | try {
|
---|
[ef5b828] | 941 | * i = maybeMutate( * i, * visitor );
|
---|
[48ed81c] | 942 | } catch ( SemanticErrorException &e ) {
|
---|
| 943 | errors.append( e );
|
---|
| 944 | }
|
---|
| 945 |
|
---|
| 946 | if ( !declsToAddBefore.empty() ) { aggr->members.splice( i, declsToAddBefore ); }
|
---|
| 947 | }
|
---|
| 948 |
|
---|
| 949 | if ( !declsToAddAfter.empty() ) { aggr->members.splice( aggr->members.end(), declsToAddAfter ); }
|
---|
| 950 | if ( !errors.isEmpty() ) { throw errors; }
|
---|
[85c4ef0] | 951 | }
|
---|
| 952 |
|
---|
[48ed81c] | 953 | void ReplaceTypedef::premutate( StructDecl * structDecl ) {
|
---|
| 954 | visit_children = false;
|
---|
| 955 | addImplicitTypedef( structDecl );
|
---|
| 956 | handleAggregate( structDecl );
|
---|
[a506df4] | 957 | }
|
---|
| 958 |
|
---|
[48ed81c] | 959 | void ReplaceTypedef::premutate( UnionDecl * unionDecl ) {
|
---|
| 960 | visit_children = false;
|
---|
| 961 | addImplicitTypedef( unionDecl );
|
---|
| 962 | handleAggregate( unionDecl );
|
---|
[85c4ef0] | 963 | }
|
---|
| 964 |
|
---|
[48ed81c] | 965 | void ReplaceTypedef::premutate( EnumDecl * enumDecl ) {
|
---|
| 966 | addImplicitTypedef( enumDecl );
|
---|
[85c4ef0] | 967 | }
|
---|
| 968 |
|
---|
[48ed81c] | 969 | void ReplaceTypedef::premutate( FunctionType * ) {
|
---|
[1f370451] | 970 | GuardValue( inFunctionType );
|
---|
| 971 | inFunctionType = true;
|
---|
| 972 | }
|
---|
| 973 |
|
---|
[0bcc2b7] | 974 | void ReplaceTypedef::premutate( TraitDecl * ) {
|
---|
| 975 | GuardScope( typedefNames );
|
---|
| 976 | GuardScope( typedeclNames);
|
---|
| 977 | }
|
---|
| 978 |
|
---|
[d1969a6] | 979 | void VerifyCtorDtorAssign::verify( std::list< Declaration * > & translationUnit ) {
|
---|
[0db6fc0] | 980 | PassVisitor<VerifyCtorDtorAssign> verifier;
|
---|
[9cb8e88d] | 981 | acceptAll( translationUnit, verifier );
|
---|
| 982 | }
|
---|
| 983 |
|
---|
[0db6fc0] | 984 | void VerifyCtorDtorAssign::previsit( FunctionDecl * funcDecl ) {
|
---|
[9cb8e88d] | 985 | FunctionType * funcType = funcDecl->get_functionType();
|
---|
| 986 | std::list< DeclarationWithType * > &returnVals = funcType->get_returnVals();
|
---|
| 987 | std::list< DeclarationWithType * > ¶ms = funcType->get_parameters();
|
---|
| 988 |
|
---|
[bff227f] | 989 | if ( CodeGen::isCtorDtorAssign( funcDecl->get_name() ) ) { // TODO: also check /=, etc.
|
---|
[9cb8e88d] | 990 | if ( params.size() == 0 ) {
|
---|
[98538288] | 991 | SemanticError( funcDecl->location, "Constructors, destructors, and assignment functions require at least one parameter." );
|
---|
[9cb8e88d] | 992 | }
|
---|
[ce8c12f] | 993 | ReferenceType * refType = dynamic_cast< ReferenceType * >( params.front()->get_type() );
|
---|
[084fecc] | 994 | if ( ! refType ) {
|
---|
[98538288] | 995 | SemanticError( funcDecl->location, "First parameter of a constructor, destructor, or assignment function must be a reference." );
|
---|
[9cb8e88d] | 996 | }
|
---|
[bff227f] | 997 | if ( CodeGen::isCtorDtor( funcDecl->get_name() ) && returnVals.size() != 0 ) {
|
---|
[98538288] | 998 | if(!returnVals.front()->get_type()->isVoid()) {
|
---|
| 999 | SemanticError( funcDecl->location, "Constructors and destructors cannot have explicit return values." );
|
---|
| 1000 | }
|
---|
[9cb8e88d] | 1001 | }
|
---|
| 1002 | }
|
---|
| 1003 | }
|
---|
[70a06f6] | 1004 |
|
---|
[6e50a6b] | 1005 | // Test for special name on a generic parameter. Special treatment for the
|
---|
| 1006 | // special name is a bootstrapping hack. In most cases, the worlds of T's
|
---|
| 1007 | // and of N's don't overlap (normal treamtemt). The foundations in
|
---|
| 1008 | // array.hfa use tagging for both types and dimensions. Tagging treats
|
---|
| 1009 | // its subject parameter even more opaquely than T&, which assumes it is
|
---|
| 1010 | // possible to have a pointer/reference to such an object. Tagging only
|
---|
| 1011 | // seeks to identify the type-system resident at compile time. Both N's
|
---|
| 1012 | // and T's can make tags. The tag definition uses the special name, which
|
---|
| 1013 | // is treated as "an N or a T." This feature is not inteded to be used
|
---|
| 1014 | // outside of the definition and immediate uses of a tag.
|
---|
| 1015 | static inline bool isReservedTysysIdOnlyName( const std::string & name ) {
|
---|
| 1016 | // name's prefix was __CFA_tysys_id_only, before it got wrapped in __..._generic
|
---|
| 1017 | int foundAt = name.find("__CFA_tysys_id_only");
|
---|
| 1018 | if (foundAt == 0) return true;
|
---|
| 1019 | if (foundAt == 2 && name[0] == '_' && name[1] == '_') return true;
|
---|
| 1020 | return false;
|
---|
| 1021 | }
|
---|
| 1022 |
|
---|
[11ab8ea8] | 1023 | template< typename Aggr >
|
---|
| 1024 | void validateGeneric( Aggr * inst ) {
|
---|
| 1025 | std::list< TypeDecl * > * params = inst->get_baseParameters();
|
---|
[30f9072] | 1026 | if ( params ) {
|
---|
[11ab8ea8] | 1027 | std::list< Expression * > & args = inst->get_parameters();
|
---|
[67cf18c] | 1028 |
|
---|
| 1029 | // insert defaults arguments when a type argument is missing (currently only supports missing arguments at the end of the list).
|
---|
| 1030 | // A substitution is used to ensure that defaults are replaced correctly, e.g.,
|
---|
| 1031 | // forall(otype T, otype alloc = heap_allocator(T)) struct vector;
|
---|
| 1032 | // vector(int) v;
|
---|
| 1033 | // after insertion of default values becomes
|
---|
| 1034 | // vector(int, heap_allocator(T))
|
---|
| 1035 | // and the substitution is built with T=int so that after substitution, the result is
|
---|
| 1036 | // vector(int, heap_allocator(int))
|
---|
| 1037 | TypeSubstitution sub;
|
---|
| 1038 | auto paramIter = params->begin();
|
---|
[6e50a6b] | 1039 | auto argIter = args.begin();
|
---|
| 1040 | for ( ; paramIter != params->end(); ++paramIter, ++argIter ) {
|
---|
| 1041 | if ( argIter != args.end() ) {
|
---|
| 1042 | TypeExpr * expr = dynamic_cast< TypeExpr * >( * argIter );
|
---|
| 1043 | if ( expr ) {
|
---|
| 1044 | sub.add( (* paramIter)->get_name(), expr->get_type()->clone() );
|
---|
| 1045 | }
|
---|
| 1046 | } else {
|
---|
[ef5b828] | 1047 | Type * defaultType = (* paramIter)->get_init();
|
---|
[67cf18c] | 1048 | if ( defaultType ) {
|
---|
| 1049 | args.push_back( new TypeExpr( defaultType->clone() ) );
|
---|
[ef5b828] | 1050 | sub.add( (* paramIter)->get_name(), defaultType->clone() );
|
---|
[6e50a6b] | 1051 | argIter = std::prev(args.end());
|
---|
| 1052 | } else {
|
---|
| 1053 | SemanticError( inst, "Too few type arguments in generic type " );
|
---|
[67cf18c] | 1054 | }
|
---|
| 1055 | }
|
---|
[6e50a6b] | 1056 | assert( argIter != args.end() );
|
---|
| 1057 | bool typeParamDeclared = (*paramIter)->kind != TypeDecl::Kind::Dimension;
|
---|
| 1058 | bool typeArgGiven;
|
---|
| 1059 | if ( isReservedTysysIdOnlyName( (*paramIter)->name ) ) {
|
---|
| 1060 | // coerce a match when declaration is reserved name, which means "either"
|
---|
| 1061 | typeArgGiven = typeParamDeclared;
|
---|
| 1062 | } else {
|
---|
| 1063 | typeArgGiven = dynamic_cast< TypeExpr * >( * argIter );
|
---|
| 1064 | }
|
---|
| 1065 | if ( ! typeParamDeclared && typeArgGiven ) SemanticError( inst, "Type argument given for value parameter: " );
|
---|
| 1066 | if ( typeParamDeclared && ! typeArgGiven ) SemanticError( inst, "Expression argument given for type parameter: " );
|
---|
[67cf18c] | 1067 | }
|
---|
| 1068 |
|
---|
| 1069 | sub.apply( inst );
|
---|
[a16764a6] | 1070 | if ( args.size() > params->size() ) SemanticError( inst, "Too many type arguments in generic type " );
|
---|
[11ab8ea8] | 1071 | }
|
---|
| 1072 | }
|
---|
| 1073 |
|
---|
[0db6fc0] | 1074 | void ValidateGenericParameters::previsit( StructInstType * inst ) {
|
---|
[11ab8ea8] | 1075 | validateGeneric( inst );
|
---|
| 1076 | }
|
---|
[9cb8e88d] | 1077 |
|
---|
[0db6fc0] | 1078 | void ValidateGenericParameters::previsit( UnionInstType * inst ) {
|
---|
[11ab8ea8] | 1079 | validateGeneric( inst );
|
---|
[9cb8e88d] | 1080 | }
|
---|
[70a06f6] | 1081 |
|
---|
[6e50a6b] | 1082 | void TranslateDimensionGenericParameters::translateDimensions( std::list< Declaration * > &translationUnit ) {
|
---|
| 1083 | PassVisitor<TranslateDimensionGenericParameters> translator;
|
---|
| 1084 | mutateAll( translationUnit, translator );
|
---|
| 1085 | }
|
---|
| 1086 |
|
---|
| 1087 | TranslateDimensionGenericParameters::TranslateDimensionGenericParameters() : WithIndexer( false ) {}
|
---|
| 1088 |
|
---|
| 1089 | // Declaration of type variable: forall( [N] ) -> forall( N & | sized( N ) )
|
---|
| 1090 | TypeDecl * TranslateDimensionGenericParameters::postmutate( TypeDecl * td ) {
|
---|
| 1091 | if ( td->kind == TypeDecl::Dimension ) {
|
---|
| 1092 | td->kind = TypeDecl::Dtype;
|
---|
| 1093 | if ( ! isReservedTysysIdOnlyName( td->name ) ) {
|
---|
| 1094 | td->sized = true;
|
---|
| 1095 | }
|
---|
| 1096 | }
|
---|
| 1097 | return td;
|
---|
| 1098 | }
|
---|
| 1099 |
|
---|
| 1100 | // Situational awareness:
|
---|
| 1101 | // array( float, [[currentExpr]] ) has visitingChildOfSUIT == true
|
---|
| 1102 | // array( float, [[currentExpr]] - 1 ) has visitingChildOfSUIT == false
|
---|
| 1103 | // size_t x = [[currentExpr]] has visitingChildOfSUIT == false
|
---|
| 1104 | void TranslateDimensionGenericParameters::changeState_ChildOfSUIT( bool newVal ) {
|
---|
| 1105 | GuardValue( nextVisitedNodeIsChildOfSUIT );
|
---|
| 1106 | GuardValue( visitingChildOfSUIT );
|
---|
| 1107 | visitingChildOfSUIT = nextVisitedNodeIsChildOfSUIT;
|
---|
| 1108 | nextVisitedNodeIsChildOfSUIT = newVal;
|
---|
| 1109 | }
|
---|
| 1110 | void TranslateDimensionGenericParameters::premutate( StructInstType * sit ) {
|
---|
| 1111 | (void) sit;
|
---|
| 1112 | changeState_ChildOfSUIT(true);
|
---|
| 1113 | }
|
---|
| 1114 | void TranslateDimensionGenericParameters::premutate( UnionInstType * uit ) {
|
---|
| 1115 | (void) uit;
|
---|
| 1116 | changeState_ChildOfSUIT(true);
|
---|
| 1117 | }
|
---|
| 1118 | void TranslateDimensionGenericParameters::premutate( BaseSyntaxNode * node ) {
|
---|
| 1119 | (void) node;
|
---|
| 1120 | changeState_ChildOfSUIT(false);
|
---|
| 1121 | }
|
---|
| 1122 |
|
---|
| 1123 | // Passing values as dimension arguments: array( float, 7 ) -> array( float, char[ 7 ] )
|
---|
| 1124 | // Consuming dimension parameters: size_t x = N - 1 ; -> size_t x = sizeof(N) - 1 ;
|
---|
| 1125 | // Intertwined reality: array( float, N ) -> array( float, N )
|
---|
| 1126 | // array( float, N - 1 ) -> array( float, char[ sizeof(N) - 1 ] )
|
---|
| 1127 | // Intertwined case 1 is not just an optimization.
|
---|
| 1128 | // Avoiding char[sizeof(-)] is necessary to enable the call of f to bind the value of N, in:
|
---|
| 1129 | // forall([N]) void f( array(float, N) & );
|
---|
| 1130 | // array(float, 7) a;
|
---|
| 1131 | // f(a);
|
---|
| 1132 |
|
---|
| 1133 | Expression * TranslateDimensionGenericParameters::postmutate( DimensionExpr * de ) {
|
---|
| 1134 | // Expression de is an occurrence of N in LHS of above examples.
|
---|
| 1135 | // Look up the name that de references.
|
---|
| 1136 | // If we are in a struct body, then this reference can be to an entry of the stuct's forall list.
|
---|
| 1137 | // Whether or not we are in a struct body, this reference can be to an entry of a containing function's forall list.
|
---|
| 1138 | // If we are in a struct body, then the stuct's forall declarations are innermost (functions don't occur in structs).
|
---|
| 1139 | // Thus, a potential struct's declaration is highest priority.
|
---|
| 1140 | // A struct's forall declarations are already renamed with _generic_ suffix. Try that name variant first.
|
---|
| 1141 |
|
---|
| 1142 | std::string useName = "__" + de->name + "_generic_";
|
---|
| 1143 | TypeDecl * namedParamDecl = const_cast<TypeDecl *>( strict_dynamic_cast<const TypeDecl *, nullptr >( indexer.lookupType( useName ) ) );
|
---|
| 1144 |
|
---|
| 1145 | if ( ! namedParamDecl ) {
|
---|
| 1146 | useName = de->name;
|
---|
| 1147 | namedParamDecl = const_cast<TypeDecl *>( strict_dynamic_cast<const TypeDecl *, nullptr >( indexer.lookupType( useName ) ) );
|
---|
| 1148 | }
|
---|
| 1149 |
|
---|
| 1150 | // Expect to find it always. A misspelled name would have been parsed as an identifier.
|
---|
| 1151 | assert( namedParamDecl && "Type-system-managed value name not found in symbol table" );
|
---|
| 1152 |
|
---|
| 1153 | delete de;
|
---|
| 1154 |
|
---|
| 1155 | TypeInstType * refToDecl = new TypeInstType( 0, useName, namedParamDecl );
|
---|
| 1156 |
|
---|
| 1157 | if ( visitingChildOfSUIT ) {
|
---|
| 1158 | // As in postmutate( Expression * ), topmost expression needs a TypeExpr wrapper
|
---|
| 1159 | // But avoid ArrayType-Sizeof
|
---|
| 1160 | return new TypeExpr( refToDecl );
|
---|
| 1161 | } else {
|
---|
| 1162 | // the N occurrence is being used directly as a runtime value,
|
---|
| 1163 | // if we are in a type instantiation, then the N is within a bigger value computation
|
---|
| 1164 | return new SizeofExpr( refToDecl );
|
---|
| 1165 | }
|
---|
| 1166 | }
|
---|
| 1167 |
|
---|
| 1168 | Expression * TranslateDimensionGenericParameters::postmutate( Expression * e ) {
|
---|
| 1169 | if ( visitingChildOfSUIT ) {
|
---|
| 1170 | // e is an expression used as an argument to instantiate a type
|
---|
| 1171 | if (! dynamic_cast< TypeExpr * >( e ) ) {
|
---|
| 1172 | // e is a value expression
|
---|
| 1173 | // but not a DimensionExpr, which has a distinct postmutate
|
---|
| 1174 | Type * typeExprContent = new ArrayType( 0, new BasicType( 0, BasicType::Char ), e, true, false );
|
---|
| 1175 | TypeExpr * result = new TypeExpr( typeExprContent );
|
---|
| 1176 | return result;
|
---|
| 1177 | }
|
---|
| 1178 | }
|
---|
| 1179 | return e;
|
---|
| 1180 | }
|
---|
| 1181 |
|
---|
[ef5b828] | 1182 | void CompoundLiteral::premutate( ObjectDecl * objectDecl ) {
|
---|
[a7c90d4] | 1183 | storageClasses = objectDecl->get_storageClasses();
|
---|
[630a82a] | 1184 | }
|
---|
| 1185 |
|
---|
[ef5b828] | 1186 | Expression * CompoundLiteral::postmutate( CompoundLiteralExpr * compLitExpr ) {
|
---|
[630a82a] | 1187 | // transform [storage_class] ... (struct S){ 3, ... };
|
---|
| 1188 | // into [storage_class] struct S temp = { 3, ... };
|
---|
| 1189 | static UniqueName indexName( "_compLit" );
|
---|
| 1190 |
|
---|
[ef5b828] | 1191 | ObjectDecl * tempvar = new ObjectDecl( indexName.newName(), storageClasses, LinkageSpec::C, nullptr, compLitExpr->get_result(), compLitExpr->get_initializer() );
|
---|
[d24d4e1] | 1192 | compLitExpr->set_result( nullptr );
|
---|
| 1193 | compLitExpr->set_initializer( nullptr );
|
---|
[630a82a] | 1194 | delete compLitExpr;
|
---|
[d24d4e1] | 1195 | declsToAddBefore.push_back( tempvar ); // add modified temporary to current block
|
---|
| 1196 | return new VariableExpr( tempvar );
|
---|
[630a82a] | 1197 | }
|
---|
[cce9429] | 1198 |
|
---|
| 1199 | void ReturnTypeFixer::fix( std::list< Declaration * > &translationUnit ) {
|
---|
[0db6fc0] | 1200 | PassVisitor<ReturnTypeFixer> fixer;
|
---|
[cce9429] | 1201 | acceptAll( translationUnit, fixer );
|
---|
| 1202 | }
|
---|
| 1203 |
|
---|
[0db6fc0] | 1204 | void ReturnTypeFixer::postvisit( FunctionDecl * functionDecl ) {
|
---|
[9facf3b] | 1205 | FunctionType * ftype = functionDecl->get_functionType();
|
---|
| 1206 | std::list< DeclarationWithType * > & retVals = ftype->get_returnVals();
|
---|
[56e49b0] | 1207 | assertf( retVals.size() == 0 || retVals.size() == 1, "Function %s has too many return values: %zu", functionDecl->get_name().c_str(), retVals.size() );
|
---|
[9facf3b] | 1208 | if ( retVals.size() == 1 ) {
|
---|
[861799c7] | 1209 | // 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).
|
---|
| 1210 | // ensure other return values have a name.
|
---|
[9facf3b] | 1211 | DeclarationWithType * ret = retVals.front();
|
---|
| 1212 | if ( ret->get_name() == "" ) {
|
---|
| 1213 | ret->set_name( toString( "_retval_", CodeGen::genName( functionDecl ) ) );
|
---|
| 1214 | }
|
---|
[c6d2e93] | 1215 | ret->get_attributes().push_back( new Attribute( "unused" ) );
|
---|
[9facf3b] | 1216 | }
|
---|
| 1217 | }
|
---|
[cce9429] | 1218 |
|
---|
[0db6fc0] | 1219 | void ReturnTypeFixer::postvisit( FunctionType * ftype ) {
|
---|
[cce9429] | 1220 | // xxx - need to handle named return values - this information needs to be saved somehow
|
---|
| 1221 | // so that resolution has access to the names.
|
---|
| 1222 | // Note that this pass needs to happen early so that other passes which look for tuple types
|
---|
| 1223 | // find them in all of the right places, including function return types.
|
---|
| 1224 | std::list< DeclarationWithType * > & retVals = ftype->get_returnVals();
|
---|
| 1225 | if ( retVals.size() > 1 ) {
|
---|
| 1226 | // generate a single return parameter which is the tuple of all of the return values
|
---|
[e3e16bc] | 1227 | TupleType * tupleType = strict_dynamic_cast< TupleType * >( ResolvExpr::extractResultType( ftype ) );
|
---|
[cce9429] | 1228 | // ensure return value is not destructed by explicitly creating an empty ListInit node wherein maybeConstruct is false.
|
---|
[ef5b828] | 1229 | ObjectDecl * newRet = new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, tupleType, new ListInit( std::list<Initializer *>(), noDesignators, false ) );
|
---|
[cce9429] | 1230 | deleteAll( retVals );
|
---|
| 1231 | retVals.clear();
|
---|
| 1232 | retVals.push_back( newRet );
|
---|
| 1233 | }
|
---|
| 1234 | }
|
---|
[fbd7ad6] | 1235 |
|
---|
[2b79a70] | 1236 | void FixObjectType::fix( std::list< Declaration * > & translationUnit ) {
|
---|
| 1237 | PassVisitor<FixObjectType> fixer;
|
---|
| 1238 | acceptAll( translationUnit, fixer );
|
---|
| 1239 | }
|
---|
| 1240 |
|
---|
| 1241 | void FixObjectType::previsit( ObjectDecl * objDecl ) {
|
---|
[ef5b828] | 1242 | Type * new_type = ResolvExpr::resolveTypeof( objDecl->get_type(), indexer );
|
---|
[2b79a70] | 1243 | objDecl->set_type( new_type );
|
---|
| 1244 | }
|
---|
| 1245 |
|
---|
| 1246 | void FixObjectType::previsit( FunctionDecl * funcDecl ) {
|
---|
[ef5b828] | 1247 | Type * new_type = ResolvExpr::resolveTypeof( funcDecl->type, indexer );
|
---|
[2b79a70] | 1248 | funcDecl->set_type( new_type );
|
---|
| 1249 | }
|
---|
| 1250 |
|
---|
[ef5b828] | 1251 | void FixObjectType::previsit( TypeDecl * typeDecl ) {
|
---|
[2b79a70] | 1252 | if ( typeDecl->get_base() ) {
|
---|
[ef5b828] | 1253 | Type * new_type = ResolvExpr::resolveTypeof( typeDecl->get_base(), indexer );
|
---|
[2b79a70] | 1254 | typeDecl->set_base( new_type );
|
---|
| 1255 | } // if
|
---|
| 1256 | }
|
---|
| 1257 |
|
---|
[09867ec] | 1258 | void InitializerLength::computeLength( std::list< Declaration * > & translationUnit ) {
|
---|
| 1259 | PassVisitor<InitializerLength> len;
|
---|
| 1260 | acceptAll( translationUnit, len );
|
---|
| 1261 | }
|
---|
| 1262 |
|
---|
[fbd7ad6] | 1263 | void ArrayLength::computeLength( std::list< Declaration * > & translationUnit ) {
|
---|
[0db6fc0] | 1264 | PassVisitor<ArrayLength> len;
|
---|
[fbd7ad6] | 1265 | acceptAll( translationUnit, len );
|
---|
| 1266 | }
|
---|
| 1267 |
|
---|
[09867ec] | 1268 | void InitializerLength::previsit( ObjectDecl * objDecl ) {
|
---|
[0b3b2ae] | 1269 | if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->type ) ) {
|
---|
[f072892] | 1270 | if ( at->dimension ) return;
|
---|
[0b3b2ae] | 1271 | if ( ListInit * init = dynamic_cast< ListInit * >( objDecl->init ) ) {
|
---|
[f072892] | 1272 | at->dimension = new ConstantExpr( Constant::from_ulong( init->initializers.size() ) );
|
---|
[fbd7ad6] | 1273 | }
|
---|
| 1274 | }
|
---|
| 1275 | }
|
---|
[4fbdfae0] | 1276 |
|
---|
[4934ea3] | 1277 | void ArrayLength::previsit( ArrayType * type ) {
|
---|
[09867ec] | 1278 | if ( type->dimension ) {
|
---|
| 1279 | // need to resolve array dimensions early so that constructor code can correctly determine
|
---|
| 1280 | // if a type is a VLA (and hence whether its elements need to be constructed)
|
---|
| 1281 | ResolvExpr::findSingleExpression( type->dimension, Validate::SizeType->clone(), indexer );
|
---|
| 1282 |
|
---|
| 1283 | // must re-evaluate whether a type is a VLA, now that more information is available
|
---|
| 1284 | // (e.g. the dimension may have been an enumerator, which was unknown prior to this step)
|
---|
| 1285 | type->isVarLen = ! InitTweak::isConstExpr( type->dimension );
|
---|
[4934ea3] | 1286 | }
|
---|
| 1287 | }
|
---|
| 1288 |
|
---|
[5809461] | 1289 | struct LabelFinder {
|
---|
| 1290 | std::set< Label > & labels;
|
---|
| 1291 | LabelFinder( std::set< Label > & labels ) : labels( labels ) {}
|
---|
| 1292 | void previsit( Statement * stmt ) {
|
---|
| 1293 | for ( Label & l : stmt->labels ) {
|
---|
| 1294 | labels.insert( l );
|
---|
| 1295 | }
|
---|
| 1296 | }
|
---|
| 1297 | };
|
---|
| 1298 |
|
---|
| 1299 | void LabelAddressFixer::premutate( FunctionDecl * funcDecl ) {
|
---|
| 1300 | GuardValue( labels );
|
---|
| 1301 | PassVisitor<LabelFinder> finder( labels );
|
---|
| 1302 | funcDecl->accept( finder );
|
---|
| 1303 | }
|
---|
| 1304 |
|
---|
| 1305 | Expression * LabelAddressFixer::postmutate( AddressExpr * addrExpr ) {
|
---|
| 1306 | // convert &&label into label address
|
---|
| 1307 | if ( AddressExpr * inner = dynamic_cast< AddressExpr * >( addrExpr->arg ) ) {
|
---|
| 1308 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( inner->arg ) ) {
|
---|
| 1309 | if ( labels.count( nameExpr->name ) ) {
|
---|
| 1310 | Label name = nameExpr->name;
|
---|
| 1311 | delete addrExpr;
|
---|
| 1312 | return new LabelAddressExpr( name );
|
---|
| 1313 | }
|
---|
| 1314 | }
|
---|
| 1315 | }
|
---|
| 1316 | return addrExpr;
|
---|
| 1317 | }
|
---|
[c8e4d2f8] | 1318 |
|
---|
[51b73452] | 1319 | } // namespace SymTab
|
---|
[0dd3a2f] | 1320 |
|
---|
| 1321 | // Local Variables: //
|
---|
| 1322 | // tab-width: 4 //
|
---|
| 1323 | // mode: c++ //
|
---|
| 1324 | // compile-command: "make install" //
|
---|
| 1325 | // End: //
|
---|