[0dd3a2f] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
---|
| 3 | //
|
---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
---|
| 5 | // file "LICENCE" distributed with Cforall.
|
---|
| 6 | //
|
---|
[9cb8e88d] | 7 | // Validate.cc --
|
---|
[0dd3a2f] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Sun May 17 21:50:04 2015
|
---|
[b128d3e] | 11 | // Last Modified By : Peter A. Buhr
|
---|
| 12 | // Last Modified On : Mon Aug 28 13:47:23 2017
|
---|
| 13 | // Update Count : 359
|
---|
[0dd3a2f] | 14 | //
|
---|
| 15 |
|
---|
| 16 | // The "validate" phase of translation is used to take a syntax tree and convert it into a standard form that aims to be
|
---|
| 17 | // as regular in structure as possible. Some assumptions can be made regarding the state of the tree after this pass is
|
---|
| 18 | // complete, including:
|
---|
| 19 | //
|
---|
| 20 | // - No nested structure or union definitions; any in the input are "hoisted" to the level of the containing struct or
|
---|
| 21 | // union.
|
---|
| 22 | //
|
---|
| 23 | // - All enumeration constants have type EnumInstType.
|
---|
| 24 | //
|
---|
[3c13c03] | 25 | // - The type "void" never occurs in lists of function parameter or return types. A function
|
---|
| 26 | // taking no arguments has no argument types.
|
---|
[0dd3a2f] | 27 | //
|
---|
| 28 | // - No context instances exist; they are all replaced by the set of declarations signified by the context, instantiated
|
---|
| 29 | // by the particular set of type arguments.
|
---|
| 30 | //
|
---|
| 31 | // - Every declaration is assigned a unique id.
|
---|
| 32 | //
|
---|
| 33 | // - No typedef declarations or instances exist; the actual type is substituted for each instance.
|
---|
| 34 | //
|
---|
| 35 | // - Each type, struct, and union definition is followed by an appropriate assignment operator.
|
---|
| 36 | //
|
---|
| 37 | // - Each use of a struct or union is connected to a complete definition of that struct or union, even if that
|
---|
| 38 | // definition occurs later in the input.
|
---|
[51b73452] | 39 |
|
---|
[0db6fc0] | 40 | #include "Validate.h"
|
---|
| 41 |
|
---|
[d180746] | 42 | #include <cassert> // for assertf, assert
|
---|
[30f9072] | 43 | #include <cstddef> // for size_t
|
---|
[d180746] | 44 | #include <list> // for list
|
---|
| 45 | #include <string> // for string
|
---|
| 46 | #include <utility> // for pair
|
---|
[30f9072] | 47 |
|
---|
| 48 | #include "CodeGen/CodeGenerator.h" // for genName
|
---|
[9236060] | 49 | #include "CodeGen/OperatorTable.h" // for isCtorDtor, isCtorDtorAssign
|
---|
[30f9072] | 50 | #include "Common/PassVisitor.h" // for PassVisitor, WithDeclsToAdd
|
---|
[d180746] | 51 | #include "Common/ScopedMap.h" // for ScopedMap
|
---|
[30f9072] | 52 | #include "Common/SemanticError.h" // for SemanticError
|
---|
| 53 | #include "Common/UniqueName.h" // for UniqueName
|
---|
| 54 | #include "Common/utility.h" // for operator+, cloneAll, deleteAll
|
---|
[be9288a] | 55 | #include "Concurrency/Keywords.h" // for applyKeywords
|
---|
[30f9072] | 56 | #include "FixFunction.h" // for FixFunction
|
---|
| 57 | #include "Indexer.h" // for Indexer
|
---|
[8b11840] | 58 | #include "InitTweak/GenInit.h" // for fixReturnStatements
|
---|
[d180746] | 59 | #include "InitTweak/InitTweak.h" // for isCtorDtorAssign
|
---|
| 60 | #include "Parser/LinkageSpec.h" // for C
|
---|
| 61 | #include "ResolvExpr/typeops.h" // for typesCompatible
|
---|
[be9288a] | 62 | #include "SymTab/Autogen.h" // for SizeType
|
---|
| 63 | #include "SynTree/Attribute.h" // for noAttributes, Attribute
|
---|
[30f9072] | 64 | #include "SynTree/Constant.h" // for Constant
|
---|
[d180746] | 65 | #include "SynTree/Declaration.h" // for ObjectDecl, DeclarationWithType
|
---|
| 66 | #include "SynTree/Expression.h" // for CompoundLiteralExpr, Expressio...
|
---|
| 67 | #include "SynTree/Initializer.h" // for ListInit, Initializer
|
---|
| 68 | #include "SynTree/Label.h" // for operator==, Label
|
---|
| 69 | #include "SynTree/Mutator.h" // for Mutator
|
---|
| 70 | #include "SynTree/Type.h" // for Type, TypeInstType, EnumInstType
|
---|
| 71 | #include "SynTree/TypeSubstitution.h" // for TypeSubstitution
|
---|
| 72 | #include "SynTree/Visitor.h" // for Visitor
|
---|
| 73 |
|
---|
| 74 | class CompoundStmt;
|
---|
| 75 | class ReturnStmt;
|
---|
| 76 | class SwitchStmt;
|
---|
[51b73452] | 77 |
|
---|
| 78 |
|
---|
[c8ffe20b] | 79 | #define debugPrint( x ) if ( doDebug ) { std::cout << x; }
|
---|
[51b73452] | 80 |
|
---|
| 81 | namespace SymTab {
|
---|
[a09e45b] | 82 | struct HoistStruct final : public WithDeclsToAdd, public WithGuards {
|
---|
[82dd287] | 83 | /// Flattens nested struct types
|
---|
[0dd3a2f] | 84 | static void hoistStruct( std::list< Declaration * > &translationUnit );
|
---|
[9cb8e88d] | 85 |
|
---|
[a09e45b] | 86 | void previsit( EnumInstType * enumInstType );
|
---|
| 87 | void previsit( StructInstType * structInstType );
|
---|
| 88 | void previsit( UnionInstType * unionInstType );
|
---|
| 89 | void previsit( StructDecl * aggregateDecl );
|
---|
| 90 | void previsit( UnionDecl * aggregateDecl );
|
---|
[0f40912] | 91 | void previsit( StaticAssertDecl * assertDecl );
|
---|
[9cb8e88d] | 92 |
|
---|
[a08ba92] | 93 | private:
|
---|
[0dd3a2f] | 94 | template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl );
|
---|
[c8ffe20b] | 95 |
|
---|
[bdad6eb7] | 96 | AggregateDecl * parentAggr = nullptr;
|
---|
[a08ba92] | 97 | };
|
---|
[c8ffe20b] | 98 |
|
---|
[cce9429] | 99 | /// Fix return types so that every function returns exactly one value
|
---|
[d24d4e1] | 100 | struct ReturnTypeFixer {
|
---|
[cce9429] | 101 | static void fix( std::list< Declaration * > &translationUnit );
|
---|
| 102 |
|
---|
[0db6fc0] | 103 | void postvisit( FunctionDecl * functionDecl );
|
---|
| 104 | void postvisit( FunctionType * ftype );
|
---|
[cce9429] | 105 | };
|
---|
| 106 |
|
---|
[de91427b] | 107 | /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers.
|
---|
[d24d4e1] | 108 | struct EnumAndPointerDecay {
|
---|
[06edda0] | 109 | void previsit( EnumDecl *aggregateDecl );
|
---|
| 110 | void previsit( FunctionType *func );
|
---|
[a08ba92] | 111 | };
|
---|
[82dd287] | 112 |
|
---|
| 113 | /// Associates forward declarations of aggregates with their definitions
|
---|
[b95fe40] | 114 | struct LinkReferenceToTypes final : public WithIndexer, public WithGuards {
|
---|
[522363e] | 115 | LinkReferenceToTypes( const Indexer *indexer );
|
---|
| 116 | void postvisit( TypeInstType *typeInst );
|
---|
[be9036d] | 117 |
|
---|
[522363e] | 118 | void postvisit( EnumInstType *enumInst );
|
---|
| 119 | void postvisit( StructInstType *structInst );
|
---|
| 120 | void postvisit( UnionInstType *unionInst );
|
---|
| 121 | void postvisit( TraitInstType *traitInst );
|
---|
[be9036d] | 122 |
|
---|
[522363e] | 123 | void postvisit( EnumDecl *enumDecl );
|
---|
| 124 | void postvisit( StructDecl *structDecl );
|
---|
| 125 | void postvisit( UnionDecl *unionDecl );
|
---|
| 126 | void postvisit( TraitDecl * traitDecl );
|
---|
[be9036d] | 127 |
|
---|
[b95fe40] | 128 | void previsit( StructDecl *structDecl );
|
---|
| 129 | void previsit( UnionDecl *unionDecl );
|
---|
| 130 |
|
---|
| 131 | void renameGenericParams( std::list< TypeDecl * > & params );
|
---|
| 132 |
|
---|
[06edda0] | 133 | private:
|
---|
[522363e] | 134 | const Indexer *local_indexer;
|
---|
[9cb8e88d] | 135 |
|
---|
[c0aa336] | 136 | typedef std::map< std::string, std::list< EnumInstType * > > ForwardEnumsType;
|
---|
[0dd3a2f] | 137 | typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType;
|
---|
| 138 | typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType;
|
---|
[c0aa336] | 139 | ForwardEnumsType forwardEnums;
|
---|
[0dd3a2f] | 140 | ForwardStructsType forwardStructs;
|
---|
| 141 | ForwardUnionsType forwardUnions;
|
---|
[b95fe40] | 142 | /// true if currently in a generic type body, so that type parameter instances can be renamed appropriately
|
---|
| 143 | bool inGeneric = false;
|
---|
[a08ba92] | 144 | };
|
---|
[c8ffe20b] | 145 |
|
---|
[06edda0] | 146 | /// Replaces array and function types in forall lists by appropriate pointer type and assigns each Object and Function declaration a unique ID.
|
---|
[522363e] | 147 | struct ForallPointerDecay final {
|
---|
[8b11840] | 148 | void previsit( ObjectDecl * object );
|
---|
| 149 | void previsit( FunctionDecl * func );
|
---|
[bbf3fda] | 150 | void previsit( FunctionType * ftype );
|
---|
[bd7e609] | 151 | void previsit( StructDecl * aggrDecl );
|
---|
| 152 | void previsit( UnionDecl * aggrDecl );
|
---|
[a08ba92] | 153 | };
|
---|
[c8ffe20b] | 154 |
|
---|
[d24d4e1] | 155 | struct ReturnChecker : public WithGuards {
|
---|
[de91427b] | 156 | /// Checks that return statements return nothing if their return type is void
|
---|
| 157 | /// and return something if the return type is non-void.
|
---|
| 158 | static void checkFunctionReturns( std::list< Declaration * > & translationUnit );
|
---|
| 159 |
|
---|
[0db6fc0] | 160 | void previsit( FunctionDecl * functionDecl );
|
---|
| 161 | void previsit( ReturnStmt * returnStmt );
|
---|
[de91427b] | 162 |
|
---|
[0db6fc0] | 163 | typedef std::list< DeclarationWithType * > ReturnVals;
|
---|
| 164 | ReturnVals returnVals;
|
---|
[de91427b] | 165 | };
|
---|
| 166 |
|
---|
[a506df4] | 167 | struct EliminateTypedef final : public WithVisitorRef<EliminateTypedef>, public WithGuards {
|
---|
[de91427b] | 168 | EliminateTypedef() : scopeLevel( 0 ) {}
|
---|
| 169 | /// Replaces typedefs by forward declarations
|
---|
[0dd3a2f] | 170 | static void eliminateTypedef( std::list< Declaration * > &translationUnit );
|
---|
[85c4ef0] | 171 |
|
---|
[a506df4] | 172 | Type * postmutate( TypeInstType * aggregateUseType );
|
---|
| 173 | Declaration * postmutate( TypedefDecl * typeDecl );
|
---|
| 174 | void premutate( TypeDecl * typeDecl );
|
---|
| 175 | void premutate( FunctionDecl * funcDecl );
|
---|
| 176 | void premutate( ObjectDecl * objDecl );
|
---|
| 177 | DeclarationWithType * postmutate( ObjectDecl * objDecl );
|
---|
| 178 |
|
---|
| 179 | void premutate( CastExpr * castExpr );
|
---|
| 180 |
|
---|
| 181 | void premutate( CompoundStmt * compoundStmt );
|
---|
| 182 | CompoundStmt * postmutate( CompoundStmt * compoundStmt );
|
---|
| 183 |
|
---|
| 184 | void premutate( StructDecl * structDecl );
|
---|
| 185 | Declaration * postmutate( StructDecl * structDecl );
|
---|
| 186 | void premutate( UnionDecl * unionDecl );
|
---|
| 187 | Declaration * postmutate( UnionDecl * unionDecl );
|
---|
| 188 | void premutate( EnumDecl * enumDecl );
|
---|
| 189 | Declaration * postmutate( EnumDecl * enumDecl );
|
---|
| 190 | Declaration * postmutate( TraitDecl * contextDecl );
|
---|
| 191 |
|
---|
[1f370451] | 192 | void premutate( FunctionType * ftype );
|
---|
| 193 |
|
---|
[a506df4] | 194 | private:
|
---|
[85c4ef0] | 195 | template<typename AggDecl>
|
---|
| 196 | AggDecl *handleAggregate( AggDecl * aggDecl );
|
---|
| 197 |
|
---|
[45161b4d] | 198 | template<typename AggDecl>
|
---|
| 199 | void addImplicitTypedef( AggDecl * aggDecl );
|
---|
[70a06f6] | 200 |
|
---|
[46f6134] | 201 | typedef std::unique_ptr<TypedefDecl> TypedefDeclPtr;
|
---|
[e491159] | 202 | typedef ScopedMap< std::string, std::pair< TypedefDeclPtr, int > > TypedefMap;
|
---|
[679864e1] | 203 | typedef std::map< std::string, TypeDecl * > TypeDeclMap;
|
---|
[cc79d97] | 204 | TypedefMap typedefNames;
|
---|
[679864e1] | 205 | TypeDeclMap typedeclNames;
|
---|
[cc79d97] | 206 | int scopeLevel;
|
---|
[1f370451] | 207 | bool inFunctionType = false;
|
---|
[a08ba92] | 208 | };
|
---|
[c8ffe20b] | 209 |
|
---|
[d24d4e1] | 210 | struct VerifyCtorDtorAssign {
|
---|
[d1969a6] | 211 | /// ensure that constructors, destructors, and assignment have at least one
|
---|
| 212 | /// parameter, the first of which must be a pointer, and that ctor/dtors have no
|
---|
[9cb8e88d] | 213 | /// return values.
|
---|
| 214 | static void verify( std::list< Declaration * > &translationUnit );
|
---|
| 215 |
|
---|
[0db6fc0] | 216 | void previsit( FunctionDecl *funcDecl );
|
---|
[5f98ce5] | 217 | };
|
---|
[70a06f6] | 218 |
|
---|
[11ab8ea8] | 219 | /// ensure that generic types have the correct number of type arguments
|
---|
[d24d4e1] | 220 | struct ValidateGenericParameters {
|
---|
[0db6fc0] | 221 | void previsit( StructInstType * inst );
|
---|
| 222 | void previsit( UnionInstType * inst );
|
---|
[5f98ce5] | 223 | };
|
---|
[70a06f6] | 224 |
|
---|
[d24d4e1] | 225 | struct ArrayLength {
|
---|
[fbd7ad6] | 226 | /// for array types without an explicit length, compute the length and store it so that it
|
---|
| 227 | /// is known to the rest of the phases. For example,
|
---|
| 228 | /// int x[] = { 1, 2, 3 };
|
---|
| 229 | /// int y[][2] = { { 1, 2, 3 }, { 1, 2, 3 } };
|
---|
| 230 | /// here x and y are known at compile-time to have length 3, so change this into
|
---|
| 231 | /// int x[3] = { 1, 2, 3 };
|
---|
| 232 | /// int y[3][2] = { { 1, 2, 3 }, { 1, 2, 3 } };
|
---|
| 233 | static void computeLength( std::list< Declaration * > & translationUnit );
|
---|
| 234 |
|
---|
[0db6fc0] | 235 | void previsit( ObjectDecl * objDecl );
|
---|
[fbd7ad6] | 236 | };
|
---|
| 237 |
|
---|
[d24d4e1] | 238 | struct CompoundLiteral final : public WithDeclsToAdd, public WithVisitorRef<CompoundLiteral> {
|
---|
[68fe077a] | 239 | Type::StorageClasses storageClasses;
|
---|
[630a82a] | 240 |
|
---|
[d24d4e1] | 241 | void premutate( ObjectDecl *objectDecl );
|
---|
| 242 | Expression * postmutate( CompoundLiteralExpr *compLitExpr );
|
---|
[9cb8e88d] | 243 | };
|
---|
| 244 |
|
---|
[5809461] | 245 | struct LabelAddressFixer final : public WithGuards {
|
---|
| 246 | std::set< Label > labels;
|
---|
| 247 |
|
---|
| 248 | void premutate( FunctionDecl * funcDecl );
|
---|
| 249 | Expression * postmutate( AddressExpr * addrExpr );
|
---|
| 250 | };
|
---|
[4fbdfae0] | 251 |
|
---|
| 252 | FunctionDecl * dereferenceOperator = nullptr;
|
---|
| 253 | struct FindSpecialDeclarations final {
|
---|
| 254 | void previsit( FunctionDecl * funcDecl );
|
---|
| 255 | };
|
---|
| 256 |
|
---|
[522363e] | 257 | void validate( std::list< Declaration * > &translationUnit, __attribute__((unused)) bool doDebug ) {
|
---|
[06edda0] | 258 | PassVisitor<EnumAndPointerDecay> epc;
|
---|
[522363e] | 259 | PassVisitor<LinkReferenceToTypes> lrt( nullptr );
|
---|
| 260 | PassVisitor<ForallPointerDecay> fpd;
|
---|
[d24d4e1] | 261 | PassVisitor<CompoundLiteral> compoundliteral;
|
---|
[0db6fc0] | 262 | PassVisitor<ValidateGenericParameters> genericParams;
|
---|
[4fbdfae0] | 263 | PassVisitor<FindSpecialDeclarations> finder;
|
---|
[5809461] | 264 | PassVisitor<LabelAddressFixer> labelAddrFixer;
|
---|
[630a82a] | 265 |
|
---|
[fbcde64] | 266 | EliminateTypedef::eliminateTypedef( translationUnit );
|
---|
[11ab8ea8] | 267 | HoistStruct::hoistStruct( translationUnit ); // must happen after EliminateTypedef, so that aggregate typedefs occur in the correct order
|
---|
[cce9429] | 268 | ReturnTypeFixer::fix( translationUnit ); // must happen before autogen
|
---|
[8e0147a] | 269 | acceptAll( translationUnit, epc ); // must happen before VerifyCtorDtorAssign, because void return objects should not exist; before LinkReferenceToTypes because it is an indexer and needs correct types for mangling
|
---|
[861799c7] | 270 | acceptAll( translationUnit, lrt ); // must happen before autogen, because sized flag needs to propagate to generated functions
|
---|
[11ab8ea8] | 271 | acceptAll( translationUnit, genericParams ); // check as early as possible - can't happen before LinkReferenceToTypes
|
---|
[ed8a0d2] | 272 | VerifyCtorDtorAssign::verify( translationUnit ); // must happen before autogen, because autogen examines existing ctor/dtors
|
---|
[bd7e609] | 273 | ReturnChecker::checkFunctionReturns( translationUnit );
|
---|
| 274 | InitTweak::fixReturnStatements( translationUnit ); // must happen before autogen
|
---|
[bcda04c] | 275 | Concurrency::applyKeywords( translationUnit );
|
---|
[bd7e609] | 276 | acceptAll( translationUnit, fpd ); // must happen before autogenerateRoutines, after Concurrency::applyKeywords because uniqueIds must be set on declaration before resolution
|
---|
[06edda0] | 277 | autogenerateRoutines( translationUnit ); // moved up, used to be below compoundLiteral - currently needs EnumAndPointerDecay
|
---|
[bcda04c] | 278 | Concurrency::implementMutexFuncs( translationUnit );
|
---|
| 279 | Concurrency::implementThreadStarter( translationUnit );
|
---|
[d24d4e1] | 280 | mutateAll( translationUnit, compoundliteral );
|
---|
[fbd7ad6] | 281 | ArrayLength::computeLength( translationUnit );
|
---|
[bd7e609] | 282 | acceptAll( translationUnit, finder ); // xxx - remove this pass soon
|
---|
[5809461] | 283 | mutateAll( translationUnit, labelAddrFixer );
|
---|
[a08ba92] | 284 | }
|
---|
[9cb8e88d] | 285 |
|
---|
[a08ba92] | 286 | void validateType( Type *type, const Indexer *indexer ) {
|
---|
[06edda0] | 287 | PassVisitor<EnumAndPointerDecay> epc;
|
---|
[522363e] | 288 | PassVisitor<LinkReferenceToTypes> lrt( indexer );
|
---|
| 289 | PassVisitor<ForallPointerDecay> fpd;
|
---|
[bda58ad] | 290 | type->accept( epc );
|
---|
[cce9429] | 291 | type->accept( lrt );
|
---|
[06edda0] | 292 | type->accept( fpd );
|
---|
[a08ba92] | 293 | }
|
---|
[c8ffe20b] | 294 |
|
---|
[a08ba92] | 295 | void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) {
|
---|
[a09e45b] | 296 | PassVisitor<HoistStruct> hoister;
|
---|
| 297 | acceptAll( translationUnit, hoister );
|
---|
[a08ba92] | 298 | }
|
---|
[c8ffe20b] | 299 |
|
---|
[0f40912] | 300 | bool shouldHoist( Declaration *decl ) {
|
---|
| 301 | return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl ) || dynamic_cast< StaticAssertDecl * >( decl );
|
---|
[a08ba92] | 302 | }
|
---|
[c0aa336] | 303 |
|
---|
[a08ba92] | 304 | template< typename AggDecl >
|
---|
| 305 | void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) {
|
---|
[bdad6eb7] | 306 | if ( parentAggr ) {
|
---|
[0dd3a2f] | 307 | // Add elements in stack order corresponding to nesting structure.
|
---|
[a09e45b] | 308 | declsToAddBefore.push_front( aggregateDecl );
|
---|
[0dd3a2f] | 309 | } else {
|
---|
[bdad6eb7] | 310 | GuardValue( parentAggr );
|
---|
| 311 | parentAggr = aggregateDecl;
|
---|
[0dd3a2f] | 312 | } // if
|
---|
| 313 | // Always remove the hoisted aggregate from the inner structure.
|
---|
[0f40912] | 314 | GuardAction( [aggregateDecl]() { filter( aggregateDecl->members, shouldHoist, false ); } );
|
---|
[a08ba92] | 315 | }
|
---|
[c8ffe20b] | 316 |
|
---|
[a09e45b] | 317 | void HoistStruct::previsit( EnumInstType * inst ) {
|
---|
[b2da0574] | 318 | if ( inst->baseEnum && inst->baseEnum->body ) {
|
---|
[a09e45b] | 319 | declsToAddBefore.push_front( inst->baseEnum );
|
---|
[c0aa336] | 320 | }
|
---|
| 321 | }
|
---|
| 322 |
|
---|
[a09e45b] | 323 | void HoistStruct::previsit( StructInstType * inst ) {
|
---|
[b2da0574] | 324 | if ( inst->baseStruct && inst->baseStruct->body ) {
|
---|
[a09e45b] | 325 | declsToAddBefore.push_front( inst->baseStruct );
|
---|
[c0aa336] | 326 | }
|
---|
| 327 | }
|
---|
| 328 |
|
---|
[a09e45b] | 329 | void HoistStruct::previsit( UnionInstType * inst ) {
|
---|
[b2da0574] | 330 | if ( inst->baseUnion && inst->baseUnion->body ) {
|
---|
[a09e45b] | 331 | declsToAddBefore.push_front( inst->baseUnion );
|
---|
[c0aa336] | 332 | }
|
---|
| 333 | }
|
---|
| 334 |
|
---|
[0f40912] | 335 | void HoistStruct::previsit( StaticAssertDecl * assertDecl ) {
|
---|
| 336 | if ( parentAggr ) {
|
---|
| 337 | declsToAddBefore.push_back( assertDecl );
|
---|
| 338 | }
|
---|
| 339 | }
|
---|
| 340 |
|
---|
[a09e45b] | 341 | void HoistStruct::previsit( StructDecl * aggregateDecl ) {
|
---|
[0dd3a2f] | 342 | handleAggregate( aggregateDecl );
|
---|
[a08ba92] | 343 | }
|
---|
[c8ffe20b] | 344 |
|
---|
[a09e45b] | 345 | void HoistStruct::previsit( UnionDecl * aggregateDecl ) {
|
---|
[0dd3a2f] | 346 | handleAggregate( aggregateDecl );
|
---|
[a08ba92] | 347 | }
|
---|
[c8ffe20b] | 348 |
|
---|
[06edda0] | 349 | void EnumAndPointerDecay::previsit( EnumDecl *enumDecl ) {
|
---|
[0dd3a2f] | 350 | // Set the type of each member of the enumeration to be EnumConstant
|
---|
| 351 | for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) {
|
---|
[f6d7e0f] | 352 | ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i );
|
---|
[0dd3a2f] | 353 | assert( obj );
|
---|
[f2e40a9f] | 354 | obj->set_type( new EnumInstType( Type::Qualifiers( Type::Const ), enumDecl->get_name() ) );
|
---|
[0dd3a2f] | 355 | } // for
|
---|
[a08ba92] | 356 | }
|
---|
[51b73452] | 357 |
|
---|
[a08ba92] | 358 | namespace {
|
---|
[83de11e] | 359 | template< typename DWTList >
|
---|
[4bda2cf] | 360 | void fixFunctionList( DWTList & dwts, bool isVarArgs, FunctionType * func ) {
|
---|
| 361 | auto nvals = dwts.size();
|
---|
| 362 | bool containsVoid = false;
|
---|
| 363 | for ( auto & dwt : dwts ) {
|
---|
| 364 | // fix each DWT and record whether a void was found
|
---|
| 365 | containsVoid |= fixFunction( dwt );
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | // the only case in which "void" is valid is where it is the only one in the list
|
---|
| 369 | if ( containsVoid && ( nvals > 1 || isVarArgs ) ) {
|
---|
[a16764a6] | 370 | SemanticError( func, "invalid type void in function type " );
|
---|
[4bda2cf] | 371 | }
|
---|
| 372 |
|
---|
| 373 | // one void is the only thing in the list; remove it.
|
---|
| 374 | if ( containsVoid ) {
|
---|
| 375 | delete dwts.front();
|
---|
| 376 | dwts.clear();
|
---|
| 377 | }
|
---|
[0dd3a2f] | 378 | }
|
---|
[a08ba92] | 379 | }
|
---|
[c8ffe20b] | 380 |
|
---|
[06edda0] | 381 | void EnumAndPointerDecay::previsit( FunctionType *func ) {
|
---|
[0dd3a2f] | 382 | // Fix up parameters and return types
|
---|
[4bda2cf] | 383 | fixFunctionList( func->parameters, func->isVarArgs, func );
|
---|
| 384 | fixFunctionList( func->returnVals, false, func );
|
---|
[a08ba92] | 385 | }
|
---|
[c8ffe20b] | 386 |
|
---|
[522363e] | 387 | LinkReferenceToTypes::LinkReferenceToTypes( const Indexer *other_indexer ) {
|
---|
[0dd3a2f] | 388 | if ( other_indexer ) {
|
---|
[522363e] | 389 | local_indexer = other_indexer;
|
---|
[0dd3a2f] | 390 | } else {
|
---|
[522363e] | 391 | local_indexer = &indexer;
|
---|
[0dd3a2f] | 392 | } // if
|
---|
[a08ba92] | 393 | }
|
---|
[c8ffe20b] | 394 |
|
---|
[522363e] | 395 | void LinkReferenceToTypes::postvisit( EnumInstType *enumInst ) {
|
---|
| 396 | EnumDecl *st = local_indexer->lookupEnum( enumInst->get_name() );
|
---|
[c0aa336] | 397 | // it's not a semantic error if the enum is not found, just an implicit forward declaration
|
---|
| 398 | if ( st ) {
|
---|
| 399 | //assert( ! enumInst->get_baseEnum() || enumInst->get_baseEnum()->get_members().empty() || ! st->get_members().empty() );
|
---|
| 400 | enumInst->set_baseEnum( st );
|
---|
| 401 | } // if
|
---|
| 402 | if ( ! st || st->get_members().empty() ) {
|
---|
| 403 | // use of forward declaration
|
---|
| 404 | forwardEnums[ enumInst->get_name() ].push_back( enumInst );
|
---|
| 405 | } // if
|
---|
| 406 | }
|
---|
| 407 |
|
---|
[48fa824] | 408 | void checkGenericParameters( ReferenceToType * inst ) {
|
---|
| 409 | for ( Expression * param : inst->parameters ) {
|
---|
| 410 | if ( ! dynamic_cast< TypeExpr * >( param ) ) {
|
---|
[a16764a6] | 411 | SemanticError( inst, "Expression parameters for generic types are currently unsupported: " );
|
---|
[48fa824] | 412 | }
|
---|
| 413 | }
|
---|
| 414 | }
|
---|
| 415 |
|
---|
[522363e] | 416 | void LinkReferenceToTypes::postvisit( StructInstType *structInst ) {
|
---|
| 417 | StructDecl *st = local_indexer->lookupStruct( structInst->get_name() );
|
---|
[0dd3a2f] | 418 | // it's not a semantic error if the struct is not found, just an implicit forward declaration
|
---|
| 419 | if ( st ) {
|
---|
[98735ef] | 420 | //assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() );
|
---|
[0dd3a2f] | 421 | structInst->set_baseStruct( st );
|
---|
| 422 | } // if
|
---|
| 423 | if ( ! st || st->get_members().empty() ) {
|
---|
| 424 | // use of forward declaration
|
---|
| 425 | forwardStructs[ structInst->get_name() ].push_back( structInst );
|
---|
| 426 | } // if
|
---|
[48fa824] | 427 | checkGenericParameters( structInst );
|
---|
[a08ba92] | 428 | }
|
---|
[c8ffe20b] | 429 |
|
---|
[522363e] | 430 | void LinkReferenceToTypes::postvisit( UnionInstType *unionInst ) {
|
---|
| 431 | UnionDecl *un = local_indexer->lookupUnion( unionInst->get_name() );
|
---|
[0dd3a2f] | 432 | // it's not a semantic error if the union is not found, just an implicit forward declaration
|
---|
| 433 | if ( un ) {
|
---|
| 434 | unionInst->set_baseUnion( un );
|
---|
| 435 | } // if
|
---|
| 436 | if ( ! un || un->get_members().empty() ) {
|
---|
| 437 | // use of forward declaration
|
---|
| 438 | forwardUnions[ unionInst->get_name() ].push_back( unionInst );
|
---|
| 439 | } // if
|
---|
[48fa824] | 440 | checkGenericParameters( unionInst );
|
---|
[a08ba92] | 441 | }
|
---|
[c8ffe20b] | 442 |
|
---|
[be9036d] | 443 | template< typename Decl >
|
---|
| 444 | void normalizeAssertions( std::list< Decl * > & assertions ) {
|
---|
| 445 | // ensure no duplicate trait members after the clone
|
---|
| 446 | auto pred = [](Decl * d1, Decl * d2) {
|
---|
| 447 | // only care if they're equal
|
---|
| 448 | DeclarationWithType * dwt1 = dynamic_cast<DeclarationWithType *>( d1 );
|
---|
| 449 | DeclarationWithType * dwt2 = dynamic_cast<DeclarationWithType *>( d2 );
|
---|
| 450 | if ( dwt1 && dwt2 ) {
|
---|
| 451 | if ( dwt1->get_name() == dwt2->get_name() && ResolvExpr::typesCompatible( dwt1->get_type(), dwt2->get_type(), SymTab::Indexer() ) ) {
|
---|
| 452 | // std::cerr << "=========== equal:" << std::endl;
|
---|
| 453 | // std::cerr << "d1: " << d1 << std::endl;
|
---|
| 454 | // std::cerr << "d2: " << d2 << std::endl;
|
---|
| 455 | return false;
|
---|
| 456 | }
|
---|
[2c57025] | 457 | }
|
---|
[be9036d] | 458 | return d1 < d2;
|
---|
| 459 | };
|
---|
| 460 | std::set<Decl *, decltype(pred)> unique_members( assertions.begin(), assertions.end(), pred );
|
---|
| 461 | // if ( unique_members.size() != assertions.size() ) {
|
---|
| 462 | // std::cerr << "============different" << std::endl;
|
---|
| 463 | // std::cerr << unique_members.size() << " " << assertions.size() << std::endl;
|
---|
| 464 | // }
|
---|
| 465 |
|
---|
| 466 | std::list< Decl * > order;
|
---|
| 467 | order.splice( order.end(), assertions );
|
---|
| 468 | std::copy_if( order.begin(), order.end(), back_inserter( assertions ), [&]( Decl * decl ) {
|
---|
| 469 | return unique_members.count( decl );
|
---|
| 470 | });
|
---|
| 471 | }
|
---|
| 472 |
|
---|
| 473 | // expand assertions from trait instance, performing the appropriate type variable substitutions
|
---|
| 474 | template< typename Iterator >
|
---|
| 475 | void expandAssertions( TraitInstType * inst, Iterator out ) {
|
---|
| 476 | assertf( inst->baseTrait, "Trait instance not linked to base trait: %s", toString( inst ).c_str() );
|
---|
| 477 | std::list< DeclarationWithType * > asserts;
|
---|
| 478 | for ( Declaration * decl : inst->baseTrait->members ) {
|
---|
[e3e16bc] | 479 | asserts.push_back( strict_dynamic_cast<DeclarationWithType *>( decl->clone() ) );
|
---|
[2c57025] | 480 | }
|
---|
[be9036d] | 481 | // substitute trait decl parameters for instance parameters
|
---|
| 482 | applySubstitution( inst->baseTrait->parameters.begin(), inst->baseTrait->parameters.end(), inst->parameters.begin(), asserts.begin(), asserts.end(), out );
|
---|
| 483 | }
|
---|
| 484 |
|
---|
[522363e] | 485 | void LinkReferenceToTypes::postvisit( TraitDecl * traitDecl ) {
|
---|
[be9036d] | 486 | if ( traitDecl->name == "sized" ) {
|
---|
| 487 | // "sized" is a special trait - flick the sized status on for the type variable
|
---|
| 488 | assertf( traitDecl->parameters.size() == 1, "Built-in trait 'sized' has incorrect number of parameters: %zd", traitDecl->parameters.size() );
|
---|
| 489 | TypeDecl * td = traitDecl->parameters.front();
|
---|
| 490 | td->set_sized( true );
|
---|
| 491 | }
|
---|
| 492 |
|
---|
| 493 | // move assertions from type parameters into the body of the trait
|
---|
| 494 | for ( TypeDecl * td : traitDecl->parameters ) {
|
---|
| 495 | for ( DeclarationWithType * assert : td->assertions ) {
|
---|
| 496 | if ( TraitInstType * inst = dynamic_cast< TraitInstType * >( assert->get_type() ) ) {
|
---|
| 497 | expandAssertions( inst, back_inserter( traitDecl->members ) );
|
---|
| 498 | } else {
|
---|
| 499 | traitDecl->members.push_back( assert->clone() );
|
---|
| 500 | }
|
---|
| 501 | }
|
---|
| 502 | deleteAll( td->assertions );
|
---|
| 503 | td->assertions.clear();
|
---|
| 504 | } // for
|
---|
| 505 | }
|
---|
[2ae171d8] | 506 |
|
---|
[522363e] | 507 | void LinkReferenceToTypes::postvisit( TraitInstType * traitInst ) {
|
---|
[2ae171d8] | 508 | // handle other traits
|
---|
[522363e] | 509 | TraitDecl *traitDecl = local_indexer->lookupTrait( traitInst->name );
|
---|
[4a9ccc3] | 510 | if ( ! traitDecl ) {
|
---|
[a16764a6] | 511 | SemanticError( traitInst->location, "use of undeclared trait " + traitInst->name );
|
---|
[17cd4eb] | 512 | } // if
|
---|
[4a9ccc3] | 513 | if ( traitDecl->get_parameters().size() != traitInst->get_parameters().size() ) {
|
---|
[a16764a6] | 514 | SemanticError( traitInst, "incorrect number of trait parameters: " );
|
---|
[4a9ccc3] | 515 | } // if
|
---|
[be9036d] | 516 | traitInst->baseTrait = traitDecl;
|
---|
[79970ed] | 517 |
|
---|
[4a9ccc3] | 518 | // need to carry over the 'sized' status of each decl in the instance
|
---|
| 519 | for ( auto p : group_iterate( traitDecl->get_parameters(), traitInst->get_parameters() ) ) {
|
---|
[5c4d27f] | 520 | TypeExpr * expr = dynamic_cast< TypeExpr * >( std::get<1>(p) );
|
---|
| 521 | if ( ! expr ) {
|
---|
[a16764a6] | 522 | SemanticError( std::get<1>(p), "Expression parameters for trait instances are currently unsupported: " );
|
---|
[5c4d27f] | 523 | }
|
---|
[4a9ccc3] | 524 | if ( TypeInstType * inst = dynamic_cast< TypeInstType * >( expr->get_type() ) ) {
|
---|
| 525 | TypeDecl * formalDecl = std::get<0>(p);
|
---|
| 526 | TypeDecl * instDecl = inst->get_baseType();
|
---|
| 527 | if ( formalDecl->get_sized() ) instDecl->set_sized( true );
|
---|
| 528 | }
|
---|
| 529 | }
|
---|
[be9036d] | 530 | // normalizeAssertions( traitInst->members );
|
---|
[a08ba92] | 531 | }
|
---|
[c8ffe20b] | 532 |
|
---|
[522363e] | 533 | void LinkReferenceToTypes::postvisit( EnumDecl *enumDecl ) {
|
---|
[c0aa336] | 534 | // visit enum members first so that the types of self-referencing members are updated properly
|
---|
| 535 | if ( ! enumDecl->get_members().empty() ) {
|
---|
| 536 | ForwardEnumsType::iterator fwds = forwardEnums.find( enumDecl->get_name() );
|
---|
| 537 | if ( fwds != forwardEnums.end() ) {
|
---|
| 538 | for ( std::list< EnumInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
|
---|
| 539 | (*inst )->set_baseEnum( enumDecl );
|
---|
| 540 | } // for
|
---|
| 541 | forwardEnums.erase( fwds );
|
---|
| 542 | } // if
|
---|
| 543 | } // if
|
---|
| 544 | }
|
---|
| 545 |
|
---|
[b95fe40] | 546 | void LinkReferenceToTypes::renameGenericParams( std::list< TypeDecl * > & params ) {
|
---|
| 547 | // rename generic type parameters uniquely so that they do not conflict with user-defined function forall parameters, e.g.
|
---|
| 548 | // forall(otype T)
|
---|
| 549 | // struct Box {
|
---|
| 550 | // T x;
|
---|
| 551 | // };
|
---|
| 552 | // forall(otype T)
|
---|
| 553 | // void f(Box(T) b) {
|
---|
| 554 | // ...
|
---|
| 555 | // }
|
---|
| 556 | // The T in Box and the T in f are different, so internally the naming must reflect that.
|
---|
| 557 | GuardValue( inGeneric );
|
---|
| 558 | inGeneric = ! params.empty();
|
---|
| 559 | for ( TypeDecl * td : params ) {
|
---|
| 560 | td->name = "__" + td->name + "_generic_";
|
---|
| 561 | }
|
---|
| 562 | }
|
---|
| 563 |
|
---|
| 564 | void LinkReferenceToTypes::previsit( StructDecl * structDecl ) {
|
---|
| 565 | renameGenericParams( structDecl->parameters );
|
---|
| 566 | }
|
---|
| 567 |
|
---|
| 568 | void LinkReferenceToTypes::previsit( UnionDecl * unionDecl ) {
|
---|
| 569 | renameGenericParams( unionDecl->parameters );
|
---|
| 570 | }
|
---|
| 571 |
|
---|
[522363e] | 572 | void LinkReferenceToTypes::postvisit( StructDecl *structDecl ) {
|
---|
[677c1be] | 573 | // visit struct members first so that the types of self-referencing members are updated properly
|
---|
[522363e] | 574 | // xxx - need to ensure that type parameters match up between forward declarations and definition (most importantly, number of type parameters and their defaults)
|
---|
[0dd3a2f] | 575 | if ( ! structDecl->get_members().empty() ) {
|
---|
| 576 | ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() );
|
---|
| 577 | if ( fwds != forwardStructs.end() ) {
|
---|
| 578 | for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
|
---|
| 579 | (*inst )->set_baseStruct( structDecl );
|
---|
| 580 | } // for
|
---|
| 581 | forwardStructs.erase( fwds );
|
---|
| 582 | } // if
|
---|
| 583 | } // if
|
---|
[a08ba92] | 584 | }
|
---|
[c8ffe20b] | 585 |
|
---|
[522363e] | 586 | void LinkReferenceToTypes::postvisit( UnionDecl *unionDecl ) {
|
---|
[0dd3a2f] | 587 | if ( ! unionDecl->get_members().empty() ) {
|
---|
| 588 | ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() );
|
---|
| 589 | if ( fwds != forwardUnions.end() ) {
|
---|
| 590 | for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
|
---|
| 591 | (*inst )->set_baseUnion( unionDecl );
|
---|
| 592 | } // for
|
---|
| 593 | forwardUnions.erase( fwds );
|
---|
| 594 | } // if
|
---|
| 595 | } // if
|
---|
[a08ba92] | 596 | }
|
---|
[c8ffe20b] | 597 |
|
---|
[522363e] | 598 | void LinkReferenceToTypes::postvisit( TypeInstType *typeInst ) {
|
---|
[b95fe40] | 599 | // ensure generic parameter instances are renamed like the base type
|
---|
| 600 | if ( inGeneric && typeInst->baseType ) typeInst->name = typeInst->baseType->name;
|
---|
[522363e] | 601 | if ( NamedTypeDecl *namedTypeDecl = local_indexer->lookupType( typeInst->get_name() ) ) {
|
---|
[0dd3a2f] | 602 | if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
|
---|
| 603 | typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype );
|
---|
| 604 | } // if
|
---|
| 605 | } // if
|
---|
[a08ba92] | 606 | }
|
---|
[c8ffe20b] | 607 |
|
---|
[4a9ccc3] | 608 | /// Fix up assertions - flattens assertion lists, removing all trait instances
|
---|
[8b11840] | 609 | void forallFixer( std::list< TypeDecl * > & forall, BaseSyntaxNode * node ) {
|
---|
| 610 | for ( TypeDecl * type : forall ) {
|
---|
[be9036d] | 611 | std::list< DeclarationWithType * > asserts;
|
---|
| 612 | asserts.splice( asserts.end(), type->assertions );
|
---|
| 613 | // expand trait instances into their members
|
---|
| 614 | for ( DeclarationWithType * assertion : asserts ) {
|
---|
| 615 | if ( TraitInstType *traitInst = dynamic_cast< TraitInstType * >( assertion->get_type() ) ) {
|
---|
| 616 | // expand trait instance into all of its members
|
---|
| 617 | expandAssertions( traitInst, back_inserter( type->assertions ) );
|
---|
| 618 | delete traitInst;
|
---|
| 619 | } else {
|
---|
| 620 | // pass other assertions through
|
---|
| 621 | type->assertions.push_back( assertion );
|
---|
| 622 | } // if
|
---|
| 623 | } // for
|
---|
| 624 | // apply FixFunction to every assertion to check for invalid void type
|
---|
| 625 | for ( DeclarationWithType *& assertion : type->assertions ) {
|
---|
[4bda2cf] | 626 | bool isVoid = fixFunction( assertion );
|
---|
| 627 | if ( isVoid ) {
|
---|
[a16764a6] | 628 | SemanticError( node, "invalid type void in assertion of function " );
|
---|
[be9036d] | 629 | } // if
|
---|
| 630 | } // for
|
---|
| 631 | // normalizeAssertions( type->assertions );
|
---|
[0dd3a2f] | 632 | } // for
|
---|
[a08ba92] | 633 | }
|
---|
[c8ffe20b] | 634 |
|
---|
[522363e] | 635 | void ForallPointerDecay::previsit( ObjectDecl *object ) {
|
---|
[3d2b7bc] | 636 | // ensure that operator names only apply to functions or function pointers
|
---|
| 637 | if ( CodeGen::isOperator( object->name ) && ! dynamic_cast< FunctionType * >( object->type->stripDeclarator() ) ) {
|
---|
| 638 | SemanticError( object->location, toCString( "operator ", object->name.c_str(), " is not a function or function pointer." ) );
|
---|
| 639 | }
|
---|
[0dd3a2f] | 640 | object->fixUniqueId();
|
---|
[a08ba92] | 641 | }
|
---|
[c8ffe20b] | 642 |
|
---|
[522363e] | 643 | void ForallPointerDecay::previsit( FunctionDecl *func ) {
|
---|
[0dd3a2f] | 644 | func->fixUniqueId();
|
---|
[a08ba92] | 645 | }
|
---|
[c8ffe20b] | 646 |
|
---|
[bbf3fda] | 647 | void ForallPointerDecay::previsit( FunctionType * ftype ) {
|
---|
| 648 | forallFixer( ftype->forall, ftype );
|
---|
| 649 | }
|
---|
| 650 |
|
---|
[bd7e609] | 651 | void ForallPointerDecay::previsit( StructDecl * aggrDecl ) {
|
---|
| 652 | forallFixer( aggrDecl->parameters, aggrDecl );
|
---|
| 653 | }
|
---|
| 654 |
|
---|
| 655 | void ForallPointerDecay::previsit( UnionDecl * aggrDecl ) {
|
---|
| 656 | forallFixer( aggrDecl->parameters, aggrDecl );
|
---|
| 657 | }
|
---|
| 658 |
|
---|
[de91427b] | 659 | void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) {
|
---|
[0db6fc0] | 660 | PassVisitor<ReturnChecker> checker;
|
---|
[de91427b] | 661 | acceptAll( translationUnit, checker );
|
---|
| 662 | }
|
---|
| 663 |
|
---|
[0db6fc0] | 664 | void ReturnChecker::previsit( FunctionDecl * functionDecl ) {
|
---|
[0508ab3] | 665 | GuardValue( returnVals );
|
---|
[de91427b] | 666 | returnVals = functionDecl->get_functionType()->get_returnVals();
|
---|
| 667 | }
|
---|
| 668 |
|
---|
[0db6fc0] | 669 | void ReturnChecker::previsit( ReturnStmt * returnStmt ) {
|
---|
[74d1804] | 670 | // Previously this also checked for the existence of an expr paired with no return values on
|
---|
| 671 | // the function return type. This is incorrect, since you can have an expression attached to
|
---|
| 672 | // a return statement in a void-returning function in C. The expression is treated as if it
|
---|
| 673 | // were cast to void.
|
---|
[30f9072] | 674 | if ( ! returnStmt->get_expr() && returnVals.size() != 0 ) {
|
---|
[a16764a6] | 675 | SemanticError( returnStmt, "Non-void function returns no values: " );
|
---|
[de91427b] | 676 | }
|
---|
| 677 | }
|
---|
| 678 |
|
---|
| 679 |
|
---|
[a08ba92] | 680 | bool isTypedef( Declaration *decl ) {
|
---|
[0dd3a2f] | 681 | return dynamic_cast< TypedefDecl * >( decl );
|
---|
[a08ba92] | 682 | }
|
---|
[c8ffe20b] | 683 |
|
---|
[a08ba92] | 684 | void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
|
---|
[a506df4] | 685 | PassVisitor<EliminateTypedef> eliminator;
|
---|
[0dd3a2f] | 686 | mutateAll( translationUnit, eliminator );
|
---|
[a506df4] | 687 | if ( eliminator.pass.typedefNames.count( "size_t" ) ) {
|
---|
[5f98ce5] | 688 | // grab and remember declaration of size_t
|
---|
[a506df4] | 689 | SizeType = eliminator.pass.typedefNames["size_t"].first->get_base()->clone();
|
---|
[5f98ce5] | 690 | } else {
|
---|
[40e636a] | 691 | // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong
|
---|
| 692 | // eventually should have a warning for this case.
|
---|
| 693 | SizeType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
|
---|
[5f98ce5] | 694 | }
|
---|
[0dd3a2f] | 695 | filter( translationUnit, isTypedef, true );
|
---|
[a08ba92] | 696 | }
|
---|
[c8ffe20b] | 697 |
|
---|
[a506df4] | 698 | Type * EliminateTypedef::postmutate( TypeInstType * typeInst ) {
|
---|
[9cb8e88d] | 699 | // instances of typedef types will come here. If it is an instance
|
---|
[cc79d97] | 700 | // of a typdef type, link the instance to its actual type.
|
---|
| 701 | TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() );
|
---|
[0dd3a2f] | 702 | if ( def != typedefNames.end() ) {
|
---|
[f53836b] | 703 | Type *ret = def->second.first->base->clone();
|
---|
[6f95000] | 704 | ret->get_qualifiers() |= typeInst->get_qualifiers();
|
---|
[1f370451] | 705 | // attributes are not carried over from typedef to function parameters/return values
|
---|
| 706 | if ( ! inFunctionType ) {
|
---|
| 707 | ret->attributes.splice( ret->attributes.end(), typeInst->attributes );
|
---|
| 708 | } else {
|
---|
| 709 | deleteAll( ret->attributes );
|
---|
| 710 | ret->attributes.clear();
|
---|
| 711 | }
|
---|
[0215a76f] | 712 | // place instance parameters on the typedef'd type
|
---|
[f53836b] | 713 | if ( ! typeInst->parameters.empty() ) {
|
---|
[0215a76f] | 714 | ReferenceToType *rtt = dynamic_cast<ReferenceToType*>(ret);
|
---|
| 715 | if ( ! rtt ) {
|
---|
[a16764a6] | 716 | SemanticError( typeInst->location, "Cannot apply type parameters to base type of " + typeInst->name );
|
---|
[0215a76f] | 717 | }
|
---|
| 718 | rtt->get_parameters().clear();
|
---|
[f53836b] | 719 | cloneAll( typeInst->parameters, rtt->parameters );
|
---|
| 720 | mutateAll( rtt->parameters, *visitor ); // recursively fix typedefs on parameters
|
---|
[1db21619] | 721 | } // if
|
---|
[0dd3a2f] | 722 | delete typeInst;
|
---|
| 723 | return ret;
|
---|
[679864e1] | 724 | } else {
|
---|
| 725 | TypeDeclMap::const_iterator base = typedeclNames.find( typeInst->get_name() );
|
---|
[f53836b] | 726 | assertf( base != typedeclNames.end(), "Cannot find typedecl name %s", typeInst->name.c_str() );
|
---|
[1e8b02f5] | 727 | typeInst->set_baseType( base->second );
|
---|
[0dd3a2f] | 728 | } // if
|
---|
| 729 | return typeInst;
|
---|
[a08ba92] | 730 | }
|
---|
[c8ffe20b] | 731 |
|
---|
[f53836b] | 732 | struct VarLenChecker : WithShortCircuiting {
|
---|
| 733 | void previsit( FunctionType * ) { visit_children = false; }
|
---|
| 734 | void previsit( ArrayType * at ) {
|
---|
| 735 | isVarLen |= at->isVarLen;
|
---|
| 736 | }
|
---|
| 737 | bool isVarLen = false;
|
---|
| 738 | };
|
---|
| 739 |
|
---|
| 740 | bool isVariableLength( Type * t ) {
|
---|
| 741 | PassVisitor<VarLenChecker> varLenChecker;
|
---|
| 742 | maybeAccept( t, varLenChecker );
|
---|
| 743 | return varLenChecker.pass.isVarLen;
|
---|
| 744 | }
|
---|
| 745 |
|
---|
[a506df4] | 746 | Declaration *EliminateTypedef::postmutate( TypedefDecl * tyDecl ) {
|
---|
[cc79d97] | 747 | if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) {
|
---|
[9cb8e88d] | 748 | // typedef to the same name from the same scope
|
---|
[cc79d97] | 749 | // must be from the same type
|
---|
| 750 |
|
---|
| 751 | Type * t1 = tyDecl->get_base();
|
---|
| 752 | Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base();
|
---|
[1cbca6e] | 753 | if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) {
|
---|
[a16764a6] | 754 | SemanticError( tyDecl->location, "Cannot redefine typedef: " + tyDecl->name );
|
---|
[f53836b] | 755 | }
|
---|
[4b97770] | 756 | // Cannot redefine VLA typedefs. Note: this is slightly incorrect, because our notion of VLAs
|
---|
| 757 | // at this point in the translator is imprecise. In particular, this will disallow redefining typedefs
|
---|
| 758 | // with arrays whose dimension is an enumerator or a cast of a constant/enumerator. The effort required
|
---|
| 759 | // to fix this corner case likely outweighs the utility of allowing it.
|
---|
[f53836b] | 760 | if ( isVariableLength( t1 ) || isVariableLength( t2 ) ) {
|
---|
[a16764a6] | 761 | SemanticError( tyDecl->location, "Cannot redefine typedef: " + tyDecl->name );
|
---|
[85c4ef0] | 762 | }
|
---|
[cc79d97] | 763 | } else {
|
---|
[46f6134] | 764 | typedefNames[ tyDecl->get_name() ] = std::make_pair( TypedefDeclPtr( tyDecl ), scopeLevel );
|
---|
[cc79d97] | 765 | } // if
|
---|
| 766 |
|
---|
[0dd3a2f] | 767 | // When a typedef is a forward declaration:
|
---|
| 768 | // typedef struct screen SCREEN;
|
---|
| 769 | // the declaration portion must be retained:
|
---|
| 770 | // struct screen;
|
---|
| 771 | // because the expansion of the typedef is:
|
---|
| 772 | // void rtn( SCREEN *p ) => void rtn( struct screen *p )
|
---|
| 773 | // hence the type-name "screen" must be defined.
|
---|
| 774 | // Note, qualifiers on the typedef are superfluous for the forward declaration.
|
---|
[6f95000] | 775 |
|
---|
| 776 | Type *designatorType = tyDecl->get_base()->stripDeclarator();
|
---|
| 777 | if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( designatorType ) ) {
|
---|
[cbce272] | 778 | return new StructDecl( aggDecl->get_name(), DeclarationNode::Struct, noAttributes, tyDecl->get_linkage() );
|
---|
[6f95000] | 779 | } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( designatorType ) ) {
|
---|
[cbce272] | 780 | return new UnionDecl( aggDecl->get_name(), noAttributes, tyDecl->get_linkage() );
|
---|
[6f95000] | 781 | } else if ( EnumInstType *enumDecl = dynamic_cast< EnumInstType * >( designatorType ) ) {
|
---|
[cbce272] | 782 | return new EnumDecl( enumDecl->get_name(), noAttributes, tyDecl->get_linkage() );
|
---|
[0dd3a2f] | 783 | } else {
|
---|
[a506df4] | 784 | return tyDecl->clone();
|
---|
[0dd3a2f] | 785 | } // if
|
---|
[a08ba92] | 786 | }
|
---|
[c8ffe20b] | 787 |
|
---|
[a506df4] | 788 | void EliminateTypedef::premutate( TypeDecl * typeDecl ) {
|
---|
[cc79d97] | 789 | TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() );
|
---|
[0dd3a2f] | 790 | if ( i != typedefNames.end() ) {
|
---|
| 791 | typedefNames.erase( i ) ;
|
---|
| 792 | } // if
|
---|
[679864e1] | 793 |
|
---|
| 794 | typedeclNames[ typeDecl->get_name() ] = typeDecl;
|
---|
[a08ba92] | 795 | }
|
---|
[c8ffe20b] | 796 |
|
---|
[a506df4] | 797 | void EliminateTypedef::premutate( FunctionDecl * ) {
|
---|
| 798 | GuardScope( typedefNames );
|
---|
[a08ba92] | 799 | }
|
---|
[c8ffe20b] | 800 |
|
---|
[a506df4] | 801 | void EliminateTypedef::premutate( ObjectDecl * ) {
|
---|
| 802 | GuardScope( typedefNames );
|
---|
| 803 | }
|
---|
[dd020c0] | 804 |
|
---|
[a506df4] | 805 | DeclarationWithType *EliminateTypedef::postmutate( ObjectDecl * objDecl ) {
|
---|
| 806 | if ( FunctionType *funtype = dynamic_cast<FunctionType *>( objDecl->get_type() ) ) { // function type?
|
---|
[02e5ab6] | 807 | // replace the current object declaration with a function declaration
|
---|
[a506df4] | 808 | FunctionDecl * newDecl = new FunctionDecl( objDecl->get_name(), objDecl->get_storageClasses(), objDecl->get_linkage(), funtype, 0, objDecl->get_attributes(), objDecl->get_funcSpec() );
|
---|
[0a86a30] | 809 | objDecl->get_attributes().clear();
|
---|
[dbe8f244] | 810 | objDecl->set_type( nullptr );
|
---|
[0a86a30] | 811 | delete objDecl;
|
---|
| 812 | return newDecl;
|
---|
[1db21619] | 813 | } // if
|
---|
[a506df4] | 814 | return objDecl;
|
---|
[a08ba92] | 815 | }
|
---|
[c8ffe20b] | 816 |
|
---|
[a506df4] | 817 | void EliminateTypedef::premutate( CastExpr * ) {
|
---|
| 818 | GuardScope( typedefNames );
|
---|
[a08ba92] | 819 | }
|
---|
[c8ffe20b] | 820 |
|
---|
[a506df4] | 821 | void EliminateTypedef::premutate( CompoundStmt * ) {
|
---|
| 822 | GuardScope( typedefNames );
|
---|
[cc79d97] | 823 | scopeLevel += 1;
|
---|
[a506df4] | 824 | GuardAction( [this](){ scopeLevel -= 1; } );
|
---|
| 825 | }
|
---|
| 826 |
|
---|
| 827 | CompoundStmt *EliminateTypedef::postmutate( CompoundStmt * compoundStmt ) {
|
---|
[2bf9c37] | 828 | // remove and delete decl stmts
|
---|
| 829 | filter( compoundStmt->kids, [](Statement * stmt) {
|
---|
| 830 | if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( stmt ) ) {
|
---|
[0dd3a2f] | 831 | if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) {
|
---|
[2bf9c37] | 832 | return true;
|
---|
[0dd3a2f] | 833 | } // if
|
---|
| 834 | } // if
|
---|
[2bf9c37] | 835 | return false;
|
---|
| 836 | }, true);
|
---|
[a506df4] | 837 | return compoundStmt;
|
---|
[a08ba92] | 838 | }
|
---|
[85c4ef0] | 839 |
|
---|
[43c89a7] | 840 | // there may be typedefs nested within aggregates. in order for everything to work properly, these should be removed
|
---|
[45161b4d] | 841 | // as well
|
---|
[85c4ef0] | 842 | template<typename AggDecl>
|
---|
| 843 | AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) {
|
---|
[2bf9c37] | 844 | filter( aggDecl->members, isTypedef, true );
|
---|
[85c4ef0] | 845 | return aggDecl;
|
---|
| 846 | }
|
---|
| 847 |
|
---|
[45161b4d] | 848 | template<typename AggDecl>
|
---|
| 849 | void EliminateTypedef::addImplicitTypedef( AggDecl * aggDecl ) {
|
---|
| 850 | if ( typedefNames.count( aggDecl->get_name() ) == 0 ) {
|
---|
[62e5546] | 851 | Type *type = nullptr;
|
---|
[45161b4d] | 852 | if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( aggDecl ) ) {
|
---|
| 853 | type = new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() );
|
---|
| 854 | } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( aggDecl ) ) {
|
---|
| 855 | type = new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() );
|
---|
| 856 | } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( aggDecl ) ) {
|
---|
| 857 | type = new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() );
|
---|
| 858 | } // if
|
---|
[0b0f1dd] | 859 | TypedefDeclPtr tyDecl( new TypedefDecl( aggDecl->get_name(), aggDecl->location, Type::StorageClasses(), type, aggDecl->get_linkage() ) );
|
---|
[46f6134] | 860 | typedefNames[ aggDecl->get_name() ] = std::make_pair( std::move( tyDecl ), scopeLevel );
|
---|
[45161b4d] | 861 | } // if
|
---|
| 862 | }
|
---|
[4e06c1e] | 863 |
|
---|
[a506df4] | 864 | void EliminateTypedef::premutate( StructDecl * structDecl ) {
|
---|
[45161b4d] | 865 | addImplicitTypedef( structDecl );
|
---|
[a506df4] | 866 | }
|
---|
| 867 |
|
---|
| 868 |
|
---|
| 869 | Declaration *EliminateTypedef::postmutate( StructDecl * structDecl ) {
|
---|
[85c4ef0] | 870 | return handleAggregate( structDecl );
|
---|
| 871 | }
|
---|
| 872 |
|
---|
[a506df4] | 873 | void EliminateTypedef::premutate( UnionDecl * unionDecl ) {
|
---|
[45161b4d] | 874 | addImplicitTypedef( unionDecl );
|
---|
[a506df4] | 875 | }
|
---|
| 876 |
|
---|
| 877 | Declaration *EliminateTypedef::postmutate( UnionDecl * unionDecl ) {
|
---|
[85c4ef0] | 878 | return handleAggregate( unionDecl );
|
---|
| 879 | }
|
---|
| 880 |
|
---|
[a506df4] | 881 | void EliminateTypedef::premutate( EnumDecl * enumDecl ) {
|
---|
[45161b4d] | 882 | addImplicitTypedef( enumDecl );
|
---|
[a506df4] | 883 | }
|
---|
| 884 |
|
---|
| 885 | Declaration *EliminateTypedef::postmutate( EnumDecl * enumDecl ) {
|
---|
[85c4ef0] | 886 | return handleAggregate( enumDecl );
|
---|
| 887 | }
|
---|
| 888 |
|
---|
[a506df4] | 889 | Declaration *EliminateTypedef::postmutate( TraitDecl * traitDecl ) {
|
---|
| 890 | return handleAggregate( traitDecl );
|
---|
[85c4ef0] | 891 | }
|
---|
| 892 |
|
---|
[1f370451] | 893 | void EliminateTypedef::premutate( FunctionType * ) {
|
---|
| 894 | GuardValue( inFunctionType );
|
---|
| 895 | inFunctionType = true;
|
---|
| 896 | }
|
---|
| 897 |
|
---|
[d1969a6] | 898 | void VerifyCtorDtorAssign::verify( std::list< Declaration * > & translationUnit ) {
|
---|
[0db6fc0] | 899 | PassVisitor<VerifyCtorDtorAssign> verifier;
|
---|
[9cb8e88d] | 900 | acceptAll( translationUnit, verifier );
|
---|
| 901 | }
|
---|
| 902 |
|
---|
[0db6fc0] | 903 | void VerifyCtorDtorAssign::previsit( FunctionDecl * funcDecl ) {
|
---|
[9cb8e88d] | 904 | FunctionType * funcType = funcDecl->get_functionType();
|
---|
| 905 | std::list< DeclarationWithType * > &returnVals = funcType->get_returnVals();
|
---|
| 906 | std::list< DeclarationWithType * > ¶ms = funcType->get_parameters();
|
---|
| 907 |
|
---|
[bff227f] | 908 | if ( CodeGen::isCtorDtorAssign( funcDecl->get_name() ) ) { // TODO: also check /=, etc.
|
---|
[9cb8e88d] | 909 | if ( params.size() == 0 ) {
|
---|
[a16764a6] | 910 | SemanticError( funcDecl, "Constructors, destructors, and assignment functions require at least one parameter " );
|
---|
[9cb8e88d] | 911 | }
|
---|
[ce8c12f] | 912 | ReferenceType * refType = dynamic_cast< ReferenceType * >( params.front()->get_type() );
|
---|
[084fecc] | 913 | if ( ! refType ) {
|
---|
[a16764a6] | 914 | SemanticError( funcDecl, "First parameter of a constructor, destructor, or assignment function must be a reference " );
|
---|
[9cb8e88d] | 915 | }
|
---|
[bff227f] | 916 | if ( CodeGen::isCtorDtor( funcDecl->get_name() ) && returnVals.size() != 0 ) {
|
---|
[a16764a6] | 917 | SemanticError( funcDecl, "Constructors and destructors cannot have explicit return values " );
|
---|
[9cb8e88d] | 918 | }
|
---|
| 919 | }
|
---|
| 920 | }
|
---|
[70a06f6] | 921 |
|
---|
[11ab8ea8] | 922 | template< typename Aggr >
|
---|
| 923 | void validateGeneric( Aggr * inst ) {
|
---|
| 924 | std::list< TypeDecl * > * params = inst->get_baseParameters();
|
---|
[30f9072] | 925 | if ( params ) {
|
---|
[11ab8ea8] | 926 | std::list< Expression * > & args = inst->get_parameters();
|
---|
[67cf18c] | 927 |
|
---|
| 928 | // insert defaults arguments when a type argument is missing (currently only supports missing arguments at the end of the list).
|
---|
| 929 | // A substitution is used to ensure that defaults are replaced correctly, e.g.,
|
---|
| 930 | // forall(otype T, otype alloc = heap_allocator(T)) struct vector;
|
---|
| 931 | // vector(int) v;
|
---|
| 932 | // after insertion of default values becomes
|
---|
| 933 | // vector(int, heap_allocator(T))
|
---|
| 934 | // and the substitution is built with T=int so that after substitution, the result is
|
---|
| 935 | // vector(int, heap_allocator(int))
|
---|
| 936 | TypeSubstitution sub;
|
---|
| 937 | auto paramIter = params->begin();
|
---|
| 938 | for ( size_t i = 0; paramIter != params->end(); ++paramIter, ++i ) {
|
---|
| 939 | if ( i < args.size() ) {
|
---|
[e3e16bc] | 940 | TypeExpr * expr = strict_dynamic_cast< TypeExpr * >( *std::next( args.begin(), i ) );
|
---|
[67cf18c] | 941 | sub.add( (*paramIter)->get_name(), expr->get_type()->clone() );
|
---|
| 942 | } else if ( i == args.size() ) {
|
---|
| 943 | Type * defaultType = (*paramIter)->get_init();
|
---|
| 944 | if ( defaultType ) {
|
---|
| 945 | args.push_back( new TypeExpr( defaultType->clone() ) );
|
---|
| 946 | sub.add( (*paramIter)->get_name(), defaultType->clone() );
|
---|
| 947 | }
|
---|
| 948 | }
|
---|
| 949 | }
|
---|
| 950 |
|
---|
| 951 | sub.apply( inst );
|
---|
[a16764a6] | 952 | if ( args.size() < params->size() ) SemanticError( inst, "Too few type arguments in generic type " );
|
---|
| 953 | if ( args.size() > params->size() ) SemanticError( inst, "Too many type arguments in generic type " );
|
---|
[11ab8ea8] | 954 | }
|
---|
| 955 | }
|
---|
| 956 |
|
---|
[0db6fc0] | 957 | void ValidateGenericParameters::previsit( StructInstType * inst ) {
|
---|
[11ab8ea8] | 958 | validateGeneric( inst );
|
---|
| 959 | }
|
---|
[9cb8e88d] | 960 |
|
---|
[0db6fc0] | 961 | void ValidateGenericParameters::previsit( UnionInstType * inst ) {
|
---|
[11ab8ea8] | 962 | validateGeneric( inst );
|
---|
[9cb8e88d] | 963 | }
|
---|
[70a06f6] | 964 |
|
---|
[d24d4e1] | 965 | void CompoundLiteral::premutate( ObjectDecl *objectDecl ) {
|
---|
[a7c90d4] | 966 | storageClasses = objectDecl->get_storageClasses();
|
---|
[630a82a] | 967 | }
|
---|
| 968 |
|
---|
[d24d4e1] | 969 | Expression *CompoundLiteral::postmutate( CompoundLiteralExpr *compLitExpr ) {
|
---|
[630a82a] | 970 | // transform [storage_class] ... (struct S){ 3, ... };
|
---|
| 971 | // into [storage_class] struct S temp = { 3, ... };
|
---|
| 972 | static UniqueName indexName( "_compLit" );
|
---|
| 973 |
|
---|
[d24d4e1] | 974 | ObjectDecl *tempvar = new ObjectDecl( indexName.newName(), storageClasses, LinkageSpec::C, nullptr, compLitExpr->get_result(), compLitExpr->get_initializer() );
|
---|
| 975 | compLitExpr->set_result( nullptr );
|
---|
| 976 | compLitExpr->set_initializer( nullptr );
|
---|
[630a82a] | 977 | delete compLitExpr;
|
---|
[d24d4e1] | 978 | declsToAddBefore.push_back( tempvar ); // add modified temporary to current block
|
---|
| 979 | return new VariableExpr( tempvar );
|
---|
[630a82a] | 980 | }
|
---|
[cce9429] | 981 |
|
---|
| 982 | void ReturnTypeFixer::fix( std::list< Declaration * > &translationUnit ) {
|
---|
[0db6fc0] | 983 | PassVisitor<ReturnTypeFixer> fixer;
|
---|
[cce9429] | 984 | acceptAll( translationUnit, fixer );
|
---|
| 985 | }
|
---|
| 986 |
|
---|
[0db6fc0] | 987 | void ReturnTypeFixer::postvisit( FunctionDecl * functionDecl ) {
|
---|
[9facf3b] | 988 | FunctionType * ftype = functionDecl->get_functionType();
|
---|
| 989 | std::list< DeclarationWithType * > & retVals = ftype->get_returnVals();
|
---|
[56e49b0] | 990 | assertf( retVals.size() == 0 || retVals.size() == 1, "Function %s has too many return values: %zu", functionDecl->get_name().c_str(), retVals.size() );
|
---|
[9facf3b] | 991 | if ( retVals.size() == 1 ) {
|
---|
[861799c7] | 992 | // 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).
|
---|
| 993 | // ensure other return values have a name.
|
---|
[9facf3b] | 994 | DeclarationWithType * ret = retVals.front();
|
---|
| 995 | if ( ret->get_name() == "" ) {
|
---|
| 996 | ret->set_name( toString( "_retval_", CodeGen::genName( functionDecl ) ) );
|
---|
| 997 | }
|
---|
[c6d2e93] | 998 | ret->get_attributes().push_back( new Attribute( "unused" ) );
|
---|
[9facf3b] | 999 | }
|
---|
| 1000 | }
|
---|
[cce9429] | 1001 |
|
---|
[0db6fc0] | 1002 | void ReturnTypeFixer::postvisit( FunctionType * ftype ) {
|
---|
[cce9429] | 1003 | // xxx - need to handle named return values - this information needs to be saved somehow
|
---|
| 1004 | // so that resolution has access to the names.
|
---|
| 1005 | // Note that this pass needs to happen early so that other passes which look for tuple types
|
---|
| 1006 | // find them in all of the right places, including function return types.
|
---|
| 1007 | std::list< DeclarationWithType * > & retVals = ftype->get_returnVals();
|
---|
| 1008 | if ( retVals.size() > 1 ) {
|
---|
| 1009 | // generate a single return parameter which is the tuple of all of the return values
|
---|
[e3e16bc] | 1010 | TupleType * tupleType = strict_dynamic_cast< TupleType * >( ResolvExpr::extractResultType( ftype ) );
|
---|
[cce9429] | 1011 | // ensure return value is not destructed by explicitly creating an empty ListInit node wherein maybeConstruct is false.
|
---|
[68fe077a] | 1012 | ObjectDecl * newRet = new ObjectDecl( "", Type::StorageClasses(), LinkageSpec::Cforall, 0, tupleType, new ListInit( std::list<Initializer*>(), noDesignators, false ) );
|
---|
[cce9429] | 1013 | deleteAll( retVals );
|
---|
| 1014 | retVals.clear();
|
---|
| 1015 | retVals.push_back( newRet );
|
---|
| 1016 | }
|
---|
| 1017 | }
|
---|
[fbd7ad6] | 1018 |
|
---|
| 1019 | void ArrayLength::computeLength( std::list< Declaration * > & translationUnit ) {
|
---|
[0db6fc0] | 1020 | PassVisitor<ArrayLength> len;
|
---|
[fbd7ad6] | 1021 | acceptAll( translationUnit, len );
|
---|
| 1022 | }
|
---|
| 1023 |
|
---|
[0db6fc0] | 1024 | void ArrayLength::previsit( ObjectDecl * objDecl ) {
|
---|
[fbd7ad6] | 1025 | if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->get_type() ) ) {
|
---|
[30f9072] | 1026 | if ( at->get_dimension() ) return;
|
---|
[fbd7ad6] | 1027 | if ( ListInit * init = dynamic_cast< ListInit * >( objDecl->get_init() ) ) {
|
---|
| 1028 | at->set_dimension( new ConstantExpr( Constant::from_ulong( init->get_initializers().size() ) ) );
|
---|
| 1029 | }
|
---|
| 1030 | }
|
---|
| 1031 | }
|
---|
[4fbdfae0] | 1032 |
|
---|
[5809461] | 1033 | struct LabelFinder {
|
---|
| 1034 | std::set< Label > & labels;
|
---|
| 1035 | LabelFinder( std::set< Label > & labels ) : labels( labels ) {}
|
---|
| 1036 | void previsit( Statement * stmt ) {
|
---|
| 1037 | for ( Label & l : stmt->labels ) {
|
---|
| 1038 | labels.insert( l );
|
---|
| 1039 | }
|
---|
| 1040 | }
|
---|
| 1041 | };
|
---|
| 1042 |
|
---|
| 1043 | void LabelAddressFixer::premutate( FunctionDecl * funcDecl ) {
|
---|
| 1044 | GuardValue( labels );
|
---|
| 1045 | PassVisitor<LabelFinder> finder( labels );
|
---|
| 1046 | funcDecl->accept( finder );
|
---|
| 1047 | }
|
---|
| 1048 |
|
---|
| 1049 | Expression * LabelAddressFixer::postmutate( AddressExpr * addrExpr ) {
|
---|
| 1050 | // convert &&label into label address
|
---|
| 1051 | if ( AddressExpr * inner = dynamic_cast< AddressExpr * >( addrExpr->arg ) ) {
|
---|
| 1052 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( inner->arg ) ) {
|
---|
| 1053 | if ( labels.count( nameExpr->name ) ) {
|
---|
| 1054 | Label name = nameExpr->name;
|
---|
| 1055 | delete addrExpr;
|
---|
| 1056 | return new LabelAddressExpr( name );
|
---|
| 1057 | }
|
---|
| 1058 | }
|
---|
| 1059 | }
|
---|
| 1060 | return addrExpr;
|
---|
| 1061 | }
|
---|
| 1062 |
|
---|
[4fbdfae0] | 1063 | void FindSpecialDeclarations::previsit( FunctionDecl * funcDecl ) {
|
---|
| 1064 | if ( ! dereferenceOperator ) {
|
---|
| 1065 | if ( funcDecl->get_name() == "*?" && funcDecl->get_linkage() == LinkageSpec::Intrinsic ) {
|
---|
| 1066 | FunctionType * ftype = funcDecl->get_functionType();
|
---|
| 1067 | if ( ftype->get_parameters().size() == 1 && ftype->get_parameters().front()->get_type()->get_qualifiers() == Type::Qualifiers() ) {
|
---|
| 1068 | dereferenceOperator = funcDecl;
|
---|
| 1069 | }
|
---|
| 1070 | }
|
---|
| 1071 | }
|
---|
| 1072 | }
|
---|
[51b73452] | 1073 | } // namespace SymTab
|
---|
[0dd3a2f] | 1074 |
|
---|
| 1075 | // Local Variables: //
|
---|
| 1076 | // tab-width: 4 //
|
---|
| 1077 | // mode: c++ //
|
---|
| 1078 | // compile-command: "make install" //
|
---|
| 1079 | // End: //
|
---|