source: src/SymTab/Validate.cc@ d88f256a

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 d88f256a was d1969a6, checked in by Rob Schluntz <rschlunt@…>, 9 years ago

check that assignment routines have a reasonable signature, add noreturn attribute to assert_fail_f to silence warnings

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