source: src/SymTab/Validate.cc@ 4e2b9710

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor 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 stuck-waitfor-destruct with_gc
Last change on this file since 4e2b9710 was 79970ed, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

implement warnings for missing struct member constructor calls, remove bad clones

  • Property mode set to 100644
File size: 26.6 KB
Line 
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 : Tue Jul 12 17:49:21 2016
13// Update Count : 298
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; neither do tuple types. A function
26// taking no arguments has no argument types, and tuples are flattened.
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/utility.h"
43#include "Common/UniqueName.h"
44#include "Validate.h"
45#include "SynTree/Visitor.h"
46#include "SynTree/Mutator.h"
47#include "SynTree/Type.h"
48#include "SynTree/Expression.h"
49#include "SynTree/Statement.h"
50#include "SynTree/TypeSubstitution.h"
51#include "Indexer.h"
52#include "FixFunction.h"
53// #include "ImplementationType.h"
54#include "GenPoly/DeclMutator.h"
55#include "AddVisit.h"
56#include "MakeLibCfa.h"
57#include "TypeEquality.h"
58#include "Autogen.h"
59#include "ResolvExpr/typeops.h"
60#include <algorithm>
61
62#define debugPrint( x ) if ( doDebug ) { std::cout << x; }
63
64namespace SymTab {
65 class HoistStruct : public Visitor {
66 public:
67 /// Flattens nested struct types
68 static void hoistStruct( std::list< Declaration * > &translationUnit );
69
70 std::list< Declaration * > &get_declsToAdd() { return declsToAdd; }
71
72 virtual void visit( StructDecl *aggregateDecl );
73 virtual void visit( UnionDecl *aggregateDecl );
74
75 virtual void visit( CompoundStmt *compoundStmt );
76 virtual void visit( SwitchStmt *switchStmt );
77 private:
78 HoistStruct();
79
80 template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl );
81
82 std::list< Declaration * > declsToAdd;
83 bool inStruct;
84 };
85
86 /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers.
87 class Pass1 : public Visitor {
88 typedef Visitor Parent;
89 virtual void visit( EnumDecl *aggregateDecl );
90 virtual void visit( FunctionType *func );
91 };
92
93 /// Associates forward declarations of aggregates with their definitions
94 class Pass2 : public Indexer {
95 typedef Indexer Parent;
96 public:
97 Pass2( bool doDebug, const Indexer *indexer );
98 private:
99 virtual void visit( StructInstType *structInst );
100 virtual void visit( UnionInstType *unionInst );
101 virtual void visit( TraitInstType *contextInst );
102 virtual void visit( StructDecl *structDecl );
103 virtual void visit( UnionDecl *unionDecl );
104 virtual void visit( TypeInstType *typeInst );
105
106 const Indexer *indexer;
107
108 typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType;
109 typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType;
110 ForwardStructsType forwardStructs;
111 ForwardUnionsType forwardUnions;
112 };
113
114 /// Replaces array and function types in forall lists by appropriate pointer type
115 class Pass3 : public Indexer {
116 typedef Indexer Parent;
117 public:
118 Pass3( const Indexer *indexer );
119 private:
120 virtual void visit( ObjectDecl *object );
121 virtual void visit( FunctionDecl *func );
122
123 const Indexer *indexer;
124 };
125
126 class ReturnChecker : public Visitor {
127 public:
128 /// Checks that return statements return nothing if their return type is void
129 /// and return something if the return type is non-void.
130 static void checkFunctionReturns( std::list< Declaration * > & translationUnit );
131 private:
132 virtual void visit( FunctionDecl * functionDecl );
133
134 virtual void visit( ReturnStmt * returnStmt );
135
136 std::list< DeclarationWithType * > returnVals;
137 };
138
139 class EliminateTypedef : public Mutator {
140 public:
141 EliminateTypedef() : scopeLevel( 0 ) {}
142 /// Replaces typedefs by forward declarations
143 static void eliminateTypedef( std::list< Declaration * > &translationUnit );
144 private:
145 virtual Declaration *mutate( TypedefDecl *typeDecl );
146 virtual TypeDecl *mutate( TypeDecl *typeDecl );
147 virtual DeclarationWithType *mutate( FunctionDecl *funcDecl );
148 virtual DeclarationWithType *mutate( ObjectDecl *objDecl );
149 virtual CompoundStmt *mutate( CompoundStmt *compoundStmt );
150 virtual Type *mutate( TypeInstType *aggregateUseType );
151 virtual Expression *mutate( CastExpr *castExpr );
152
153 virtual Declaration *mutate( StructDecl * structDecl );
154 virtual Declaration *mutate( UnionDecl * unionDecl );
155 virtual Declaration *mutate( EnumDecl * enumDecl );
156 virtual Declaration *mutate( TraitDecl * contextDecl );
157
158 template<typename AggDecl>
159 AggDecl *handleAggregate( AggDecl * aggDecl );
160
161 template<typename AggDecl>
162 void addImplicitTypedef( AggDecl * aggDecl );
163
164 typedef std::map< std::string, std::pair< TypedefDecl *, int > > TypedefMap;
165 typedef std::map< std::string, TypeDecl * > TypeDeclMap;
166 TypedefMap typedefNames;
167 TypeDeclMap typedeclNames;
168 int scopeLevel;
169 };
170
171 class VerifyCtorDtor : public Visitor {
172 public:
173 /// ensure that constructors and destructors have at least one
174 /// parameter, the first of which must be a pointer, and no
175 /// return values.
176 static void verify( std::list< Declaration * > &translationUnit );
177
178 virtual void visit( FunctionDecl *funcDecl );
179 };
180
181 class CompoundLiteral : public GenPoly::DeclMutator {
182 DeclarationNode::StorageClass storageclass = DeclarationNode::NoStorageClass;
183
184 virtual DeclarationWithType * mutate( ObjectDecl *objectDecl );
185 virtual Expression *mutate( CompoundLiteralExpr *compLitExpr );
186 };
187
188 void validate( std::list< Declaration * > &translationUnit, bool doDebug ) {
189 Pass1 pass1;
190 Pass2 pass2( doDebug, 0 );
191 Pass3 pass3( 0 );
192 CompoundLiteral compoundliteral;
193
194 EliminateTypedef::eliminateTypedef( translationUnit );
195 HoistStruct::hoistStruct( translationUnit );
196 autogenerateRoutines( translationUnit ); // moved up, used to be below compoundLiteral - currently needs Pass1
197 acceptAll( translationUnit, pass1 );
198 acceptAll( translationUnit, pass2 );
199 ReturnChecker::checkFunctionReturns( translationUnit );
200 compoundliteral.mutateDeclarationList( translationUnit );
201 acceptAll( translationUnit, pass3 );
202 VerifyCtorDtor::verify( translationUnit );
203 }
204
205 void validateType( Type *type, const Indexer *indexer ) {
206 Pass1 pass1;
207 Pass2 pass2( false, indexer );
208 Pass3 pass3( indexer );
209 type->accept( pass1 );
210 type->accept( pass2 );
211 type->accept( pass3 );
212 }
213
214 void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) {
215 HoistStruct hoister;
216 acceptAndAdd( translationUnit, hoister, true );
217 }
218
219 HoistStruct::HoistStruct() : inStruct( false ) {
220 }
221
222 void filter( std::list< Declaration * > &declList, bool (*pred)( Declaration * ), bool doDelete ) {
223 std::list< Declaration * >::iterator i = declList.begin();
224 while ( i != declList.end() ) {
225 std::list< Declaration * >::iterator next = i;
226 ++next;
227 if ( pred( *i ) ) {
228 if ( doDelete ) {
229 delete *i;
230 } // if
231 declList.erase( i );
232 } // if
233 i = next;
234 } // while
235 }
236
237 bool isStructOrUnion( Declaration *decl ) {
238 return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl );
239 }
240
241 template< typename AggDecl >
242 void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) {
243 if ( inStruct ) {
244 // Add elements in stack order corresponding to nesting structure.
245 declsToAdd.push_front( aggregateDecl );
246 Visitor::visit( aggregateDecl );
247 } else {
248 inStruct = true;
249 Visitor::visit( aggregateDecl );
250 inStruct = false;
251 } // if
252 // Always remove the hoisted aggregate from the inner structure.
253 filter( aggregateDecl->get_members(), isStructOrUnion, false );
254 }
255
256 void HoistStruct::visit( StructDecl *aggregateDecl ) {
257 handleAggregate( aggregateDecl );
258 }
259
260 void HoistStruct::visit( UnionDecl *aggregateDecl ) {
261 handleAggregate( aggregateDecl );
262 }
263
264 void HoistStruct::visit( CompoundStmt *compoundStmt ) {
265 addVisit( compoundStmt, *this );
266 }
267
268 void HoistStruct::visit( SwitchStmt *switchStmt ) {
269 addVisit( switchStmt, *this );
270 }
271
272 void Pass1::visit( EnumDecl *enumDecl ) {
273 // Set the type of each member of the enumeration to be EnumConstant
274 for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) {
275 ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i );
276 assert( obj );
277 obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false, false, false ), enumDecl->get_name() ) );
278 } // for
279 Parent::visit( enumDecl );
280 }
281
282 namespace {
283 template< typename DWTList >
284 void fixFunctionList( DWTList & dwts, FunctionType * func ) {
285 // the only case in which "void" is valid is where it is the only one in the list; then it should be removed
286 // entirely other fix ups are handled by the FixFunction class
287 typedef typename DWTList::iterator DWTIterator;
288 DWTIterator begin( dwts.begin() ), end( dwts.end() );
289 if ( begin == end ) return;
290 FixFunction fixer;
291 DWTIterator i = begin;
292 *i = (*i)->acceptMutator( fixer );
293 if ( fixer.get_isVoid() ) {
294 DWTIterator j = i;
295 ++i;
296 dwts.erase( j );
297 if ( i != end ) {
298 throw SemanticError( "invalid type void in function type ", func );
299 } // if
300 } else {
301 ++i;
302 for ( ; i != end; ++i ) {
303 FixFunction fixer;
304 *i = (*i )->acceptMutator( fixer );
305 if ( fixer.get_isVoid() ) {
306 throw SemanticError( "invalid type void in function type ", func );
307 } // if
308 } // for
309 } // if
310 }
311 }
312
313 void Pass1::visit( FunctionType *func ) {
314 // Fix up parameters and return types
315 fixFunctionList( func->get_parameters(), func );
316 fixFunctionList( func->get_returnVals(), func );
317 Visitor::visit( func );
318 }
319
320 Pass2::Pass2( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug ) {
321 if ( other_indexer ) {
322 indexer = other_indexer;
323 } else {
324 indexer = this;
325 } // if
326 }
327
328 void Pass2::visit( StructInstType *structInst ) {
329 Parent::visit( structInst );
330 StructDecl *st = indexer->lookupStruct( structInst->get_name() );
331 // it's not a semantic error if the struct is not found, just an implicit forward declaration
332 if ( st ) {
333 //assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() );
334 structInst->set_baseStruct( st );
335 } // if
336 if ( ! st || st->get_members().empty() ) {
337 // use of forward declaration
338 forwardStructs[ structInst->get_name() ].push_back( structInst );
339 } // if
340 }
341
342 void Pass2::visit( UnionInstType *unionInst ) {
343 Parent::visit( unionInst );
344 UnionDecl *un = indexer->lookupUnion( unionInst->get_name() );
345 // it's not a semantic error if the union is not found, just an implicit forward declaration
346 if ( un ) {
347 unionInst->set_baseUnion( un );
348 } // if
349 if ( ! un || un->get_members().empty() ) {
350 // use of forward declaration
351 forwardUnions[ unionInst->get_name() ].push_back( unionInst );
352 } // if
353 }
354
355 void Pass2::visit( TraitInstType *contextInst ) {
356 Parent::visit( contextInst );
357 TraitDecl *ctx = indexer->lookupTrait( contextInst->get_name() );
358 if ( ! ctx ) {
359 throw SemanticError( "use of undeclared context " + contextInst->get_name() );
360 } // if
361 for ( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) {
362 for ( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) {
363 if ( TraitInstType *otherCtx = dynamic_cast< TraitInstType * >(*assert ) ) {
364 cloneAll( otherCtx->get_members(), contextInst->get_members() );
365 } else {
366 contextInst->get_members().push_back( (*assert )->clone() );
367 } // if
368 } // for
369 } // for
370
371 if ( ctx->get_parameters().size() != contextInst->get_parameters().size() ) {
372 throw SemanticError( "incorrect number of context parameters: ", contextInst );
373 } // if
374
375 // need to clone members of the context for ownership purposes
376 std::list< Declaration * > members;
377 std::transform( ctx->get_members().begin(), ctx->get_members().end(), back_inserter( members ), [](Declaration * dwt) { return dwt->clone(); } );
378
379 applySubstitution( ctx->get_parameters().begin(), ctx->get_parameters().end(), contextInst->get_parameters().begin(), members.begin(), members.end(), back_inserter( contextInst->get_members() ) );
380 }
381
382 void Pass2::visit( StructDecl *structDecl ) {
383 // visit struct members first so that the types of self-referencing members are updated properly
384 Parent::visit( structDecl );
385 if ( ! structDecl->get_members().empty() ) {
386 ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() );
387 if ( fwds != forwardStructs.end() ) {
388 for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
389 (*inst )->set_baseStruct( structDecl );
390 } // for
391 forwardStructs.erase( fwds );
392 } // if
393 } // if
394 }
395
396 void Pass2::visit( UnionDecl *unionDecl ) {
397 Parent::visit( unionDecl );
398 if ( ! unionDecl->get_members().empty() ) {
399 ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() );
400 if ( fwds != forwardUnions.end() ) {
401 for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
402 (*inst )->set_baseUnion( unionDecl );
403 } // for
404 forwardUnions.erase( fwds );
405 } // if
406 } // if
407 }
408
409 void Pass2::visit( TypeInstType *typeInst ) {
410 if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) {
411 if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
412 typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype );
413 } // if
414 } // if
415 }
416
417 Pass3::Pass3( const Indexer *other_indexer ) : Indexer( false ) {
418 if ( other_indexer ) {
419 indexer = other_indexer;
420 } else {
421 indexer = this;
422 } // if
423 }
424
425 /// Fix up assertions
426 void forallFixer( Type *func ) {
427 for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) {
428 std::list< DeclarationWithType * > toBeDone, nextRound;
429 toBeDone.splice( toBeDone.end(), (*type )->get_assertions() );
430 while ( ! toBeDone.empty() ) {
431 for ( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) {
432 if ( TraitInstType *ctx = dynamic_cast< TraitInstType * >( (*assertion )->get_type() ) ) {
433 for ( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) {
434 DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i );
435 assert( dwt );
436 nextRound.push_back( dwt->clone() );
437 }
438 delete ctx;
439 } else {
440 FixFunction fixer;
441 *assertion = (*assertion )->acceptMutator( fixer );
442 if ( fixer.get_isVoid() ) {
443 throw SemanticError( "invalid type void in assertion of function ", func );
444 }
445 (*type )->get_assertions().push_back( *assertion );
446 } // if
447 } // for
448 toBeDone.clear();
449 toBeDone.splice( toBeDone.end(), nextRound );
450 } // while
451 } // for
452 }
453
454 void Pass3::visit( ObjectDecl *object ) {
455 forallFixer( object->get_type() );
456 if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) {
457 forallFixer( pointer->get_base() );
458 } // if
459 Parent::visit( object );
460 object->fixUniqueId();
461 }
462
463 void Pass3::visit( FunctionDecl *func ) {
464 forallFixer( func->get_type() );
465 Parent::visit( func );
466 func->fixUniqueId();
467 }
468
469 void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) {
470 ReturnChecker checker;
471 acceptAll( translationUnit, checker );
472 }
473
474 void ReturnChecker::visit( FunctionDecl * functionDecl ) {
475 std::list< DeclarationWithType * > oldReturnVals = returnVals;
476 returnVals = functionDecl->get_functionType()->get_returnVals();
477 Visitor::visit( functionDecl );
478 returnVals = oldReturnVals;
479 }
480
481 void ReturnChecker::visit( ReturnStmt * returnStmt ) {
482 // Previously this also checked for the existence of an expr paired with no return values on
483 // the function return type. This is incorrect, since you can have an expression attached to
484 // a return statement in a void-returning function in C. The expression is treated as if it
485 // were cast to void.
486 if ( returnStmt->get_expr() == NULL && returnVals.size() != 0 ) {
487 throw SemanticError( "Non-void function returns no values: " , returnStmt );
488 }
489 }
490
491
492 bool isTypedef( Declaration *decl ) {
493 return dynamic_cast< TypedefDecl * >( decl );
494 }
495
496 void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
497 EliminateTypedef eliminator;
498 mutateAll( translationUnit, eliminator );
499 if ( eliminator.typedefNames.count( "size_t" ) ) {
500 // grab and remember declaration of size_t
501 SizeType = eliminator.typedefNames["size_t"].first->get_base()->clone();
502 } else {
503 // xxx - missing global typedef for size_t - default to long unsigned int, even though that may be wrong
504 // eventually should have a warning for this case.
505 SizeType = new BasicType( Type::Qualifiers(), BasicType::LongUnsignedInt );
506 }
507 filter( translationUnit, isTypedef, true );
508
509 }
510
511 Type *EliminateTypedef::mutate( TypeInstType * typeInst ) {
512 // instances of typedef types will come here. If it is an instance
513 // of a typdef type, link the instance to its actual type.
514 TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() );
515 if ( def != typedefNames.end() ) {
516 Type *ret = def->second.first->get_base()->clone();
517 ret->get_qualifiers() += typeInst->get_qualifiers();
518 // place instance parameters on the typedef'd type
519 if ( ! typeInst->get_parameters().empty() ) {
520 ReferenceToType *rtt = dynamic_cast<ReferenceToType*>(ret);
521 if ( ! rtt ) {
522 throw SemanticError("cannot apply type parameters to base type of " + typeInst->get_name());
523 }
524 rtt->get_parameters().clear();
525 cloneAll( typeInst->get_parameters(), rtt->get_parameters() );
526 mutateAll( rtt->get_parameters(), *this ); // recursively fix typedefs on parameters
527 } // if
528 delete typeInst;
529 return ret;
530 } else {
531 TypeDeclMap::const_iterator base = typedeclNames.find( typeInst->get_name() );
532 assert( base != typedeclNames.end() );
533 typeInst->set_baseType( base->second->clone() );
534 } // if
535 return typeInst;
536 }
537
538 Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) {
539 Declaration *ret = Mutator::mutate( tyDecl );
540
541 if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) {
542 // typedef to the same name from the same scope
543 // must be from the same type
544
545 Type * t1 = tyDecl->get_base();
546 Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base();
547 if ( ! ResolvExpr::typesCompatible( t1, t2, Indexer() ) ) {
548 throw SemanticError( "cannot redefine typedef: " + tyDecl->get_name() );
549 }
550 } else {
551 typedefNames[ tyDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel );
552 } // if
553
554 // When a typedef is a forward declaration:
555 // typedef struct screen SCREEN;
556 // the declaration portion must be retained:
557 // struct screen;
558 // because the expansion of the typedef is:
559 // void rtn( SCREEN *p ) => void rtn( struct screen *p )
560 // hence the type-name "screen" must be defined.
561 // Note, qualifiers on the typedef are superfluous for the forward declaration.
562 if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( tyDecl->get_base() ) ) {
563 return new StructDecl( aggDecl->get_name() );
564 } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( tyDecl->get_base() ) ) {
565 return new UnionDecl( aggDecl->get_name() );
566 } else if ( EnumInstType *enumDecl = dynamic_cast< EnumInstType * >( tyDecl->get_base() ) ) {
567 return new EnumDecl( enumDecl->get_name() );
568 } else {
569 return ret;
570 } // if
571 }
572
573 TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) {
574 TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() );
575 if ( i != typedefNames.end() ) {
576 typedefNames.erase( i ) ;
577 } // if
578
579 typedeclNames[ typeDecl->get_name() ] = typeDecl;
580 return typeDecl;
581 }
582
583 DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) {
584 TypedefMap oldNames = typedefNames;
585 DeclarationWithType *ret = Mutator::mutate( funcDecl );
586 typedefNames = oldNames;
587 return ret;
588 }
589
590 DeclarationWithType *EliminateTypedef::mutate( ObjectDecl * objDecl ) {
591 TypedefMap oldNames = typedefNames;
592 DeclarationWithType *ret = Mutator::mutate( objDecl );
593 typedefNames = oldNames;
594 // is the type a function?
595 if ( FunctionType *funtype = dynamic_cast<FunctionType *>( ret->get_type() ) ) {
596 // replace the current object declaration with a function declaration
597 return new FunctionDecl( ret->get_name(), ret->get_storageClass(), ret->get_linkage(), funtype, 0, ret->get_isInline(), ret->get_isNoreturn() );
598 } else if ( objDecl->get_isInline() || objDecl->get_isNoreturn() ) {
599 throw SemanticError( "invalid inline or _Noreturn specification in declaration of ", objDecl );
600 } // if
601 return ret;
602 }
603
604 Expression *EliminateTypedef::mutate( CastExpr * castExpr ) {
605 TypedefMap oldNames = typedefNames;
606 Expression *ret = Mutator::mutate( castExpr );
607 typedefNames = oldNames;
608 return ret;
609 }
610
611 CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) {
612 TypedefMap oldNames = typedefNames;
613 scopeLevel += 1;
614 CompoundStmt *ret = Mutator::mutate( compoundStmt );
615 scopeLevel -= 1;
616 std::list< Statement * >::iterator i = compoundStmt->get_kids().begin();
617 while ( i != compoundStmt->get_kids().end() ) {
618 std::list< Statement * >::iterator next = i+1;
619 if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) {
620 if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) {
621 delete *i;
622 compoundStmt->get_kids().erase( i );
623 } // if
624 } // if
625 i = next;
626 } // while
627 typedefNames = oldNames;
628 return ret;
629 }
630
631 // there may be typedefs nested within aggregates in order for everything to work properly, these should be removed
632 // as well
633 template<typename AggDecl>
634 AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) {
635 std::list<Declaration *>::iterator it = aggDecl->get_members().begin();
636 for ( ; it != aggDecl->get_members().end(); ) {
637 std::list< Declaration * >::iterator next = it+1;
638 if ( dynamic_cast< TypedefDecl * >( *it ) ) {
639 delete *it;
640 aggDecl->get_members().erase( it );
641 } // if
642 it = next;
643 }
644 return aggDecl;
645 }
646
647 template<typename AggDecl>
648 void EliminateTypedef::addImplicitTypedef( AggDecl * aggDecl ) {
649 if ( typedefNames.count( aggDecl->get_name() ) == 0 ) {
650 Type *type;
651 if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( aggDecl ) ) {
652 type = new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() );
653 } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( aggDecl ) ) {
654 type = new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() );
655 } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( aggDecl ) ) {
656 type = new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() );
657 } // if
658 TypedefDecl * tyDecl = new TypedefDecl( aggDecl->get_name(), DeclarationNode::NoStorageClass, type );
659 typedefNames[ aggDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel );
660 } // if
661 }
662
663 Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) {
664 addImplicitTypedef( structDecl );
665 Mutator::mutate( structDecl );
666 return handleAggregate( structDecl );
667 }
668
669 Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) {
670 addImplicitTypedef( unionDecl );
671 Mutator::mutate( unionDecl );
672 return handleAggregate( unionDecl );
673 }
674
675 Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) {
676 addImplicitTypedef( enumDecl );
677 Mutator::mutate( enumDecl );
678 return handleAggregate( enumDecl );
679 }
680
681 Declaration *EliminateTypedef::mutate( TraitDecl * contextDecl ) {
682 Mutator::mutate( contextDecl );
683 return handleAggregate( contextDecl );
684 }
685
686 void VerifyCtorDtor::verify( std::list< Declaration * > & translationUnit ) {
687 VerifyCtorDtor verifier;
688 acceptAll( translationUnit, verifier );
689 }
690
691 void VerifyCtorDtor::visit( FunctionDecl * funcDecl ) {
692 FunctionType * funcType = funcDecl->get_functionType();
693 std::list< DeclarationWithType * > &returnVals = funcType->get_returnVals();
694 std::list< DeclarationWithType * > &params = funcType->get_parameters();
695
696 if ( funcDecl->get_name() == "?{}" || funcDecl->get_name() == "^?{}" ) {
697 if ( params.size() == 0 ) {
698 throw SemanticError( "Constructors and destructors require at least one parameter ", funcDecl );
699 }
700 if ( ! dynamic_cast< PointerType * >( params.front()->get_type() ) ) {
701 throw SemanticError( "First parameter of a constructor or destructor must be a pointer ", funcDecl );
702 }
703 if ( returnVals.size() != 0 ) {
704 throw SemanticError( "Constructors and destructors cannot have explicit return values ", funcDecl );
705 }
706 }
707
708 Visitor::visit( funcDecl );
709 }
710
711 DeclarationWithType * CompoundLiteral::mutate( ObjectDecl *objectDecl ) {
712 storageclass = objectDecl->get_storageClass();
713 DeclarationWithType * temp = Mutator::mutate( objectDecl );
714 storageclass = DeclarationNode::NoStorageClass;
715 return temp;
716 }
717
718 Expression *CompoundLiteral::mutate( CompoundLiteralExpr *compLitExpr ) {
719 // transform [storage_class] ... (struct S){ 3, ... };
720 // into [storage_class] struct S temp = { 3, ... };
721 static UniqueName indexName( "_compLit" );
722
723 ObjectDecl *tempvar = new ObjectDecl( indexName.newName(), storageclass, LinkageSpec::C, 0, compLitExpr->get_type(), compLitExpr->get_initializer() );
724 compLitExpr->set_type( 0 );
725 compLitExpr->set_initializer( 0 );
726 delete compLitExpr;
727 DeclarationWithType * newtempvar = mutate( tempvar );
728 addDeclaration( newtempvar ); // add modified temporary to current block
729 return new VariableExpr( newtempvar );
730 }
731} // namespace SymTab
732
733// Local Variables: //
734// tab-width: 4 //
735// mode: c++ //
736// compile-command: "make install" //
737// End: //
Note: See TracBrowser for help on using the repository browser.