source: src/SymTab/Validate.cc@ ad28abb

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 ad28abb was b128d3e, checked in by Peter A. Buhr <pabuhr@…>, 8 years ago

remove contraction from error message

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