source: src/SymTab/Validate.cc@ 67a67af

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 67a67af was cbce272, checked in by Andrew Beach <ajbeach@…>, 8 years ago

Structure based exception handling.

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