source: src/SymTab/Validate.cc@ 676cc8c

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors deferred_resn demangler enum forall-pointer-decay jacob/cs343-translation jenkins-sandbox new-ast new-ast-unique-expr new-env no_list persistent-indexer pthread-emulation qualifiedEnum resolv-new with_gc
Last change on this file since 676cc8c was 67cf18c, checked in by Rob Schluntz <rschlunt@…>, 8 years ago

implement default type arguments for generic types [closes #13]

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