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