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