source: src/SymTab/Validate.cc@ 2794fff

ADT aaron-thesis arm-eh ast-experimental cleanup-dtors ctor deferred_resn demangler enum forall-pointer-decay gc_noraii jacob/cs343-translation jenkins-sandbox memory 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 2794fff was 145f1fc, checked in by Rob Schluntz <rschlunt@…>, 11 years ago

modified ForStmt to have a list of statements for the initialization portion, and reverted to hoisting the initialization

  • Property mode set to 100644
File size: 35.9 KB
RevLine 
[0dd3a2f]1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// Validate.cc --
8//
9// Author : Richard C. Bilson
10// Created On : Sun May 17 21:50:04 2015
[1869adf]11// Last Modified By : Rob Schluntz
[145f1fc]12// Last Modified On : Tue Jul 14 12:27:54 2015
13// Update Count : 186
[0dd3a2f]14//
15
16// The "validate" phase of translation is used to take a syntax tree and convert it into a standard form that aims to be
17// as regular in structure as possible. Some assumptions can be made regarding the state of the tree after this pass is
18// complete, including:
19//
20// - No nested structure or union definitions; any in the input are "hoisted" to the level of the containing struct or
21// union.
22//
23// - All enumeration constants have type EnumInstType.
24//
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.
[51b73452]39
40#include <list>
41#include <iterator>
42#include "Validate.h"
43#include "SynTree/Visitor.h"
44#include "SynTree/Mutator.h"
45#include "SynTree/Type.h"
46#include "SynTree/Statement.h"
47#include "SynTree/TypeSubstitution.h"
[68cd1ce]48#include "Indexer.h"
[51b73452]49#include "FixFunction.h"
[cc79d97]50// #include "ImplementationType.h"
[51b73452]51#include "utility.h"
52#include "UniqueName.h"
53#include "AddVisit.h"
[f6d7e0f]54#include "MakeLibCfa.h"
[cc79d97]55#include "TypeEquality.h"
[51b73452]56
[c8ffe20b]57#define debugPrint( x ) if ( doDebug ) { std::cout << x; }
[51b73452]58
59namespace SymTab {
[a08ba92]60 class HoistStruct : public Visitor {
61 public:
[0dd3a2f]62 static void hoistStruct( std::list< Declaration * > &translationUnit );
[c8ffe20b]63
[0dd3a2f]64 std::list< Declaration * > &get_declsToAdd() { return declsToAdd; }
[c8ffe20b]65
[0dd3a2f]66 virtual void visit( StructDecl *aggregateDecl );
67 virtual void visit( UnionDecl *aggregateDecl );
[c8ffe20b]68
[0dd3a2f]69 virtual void visit( CompoundStmt *compoundStmt );
70 virtual void visit( IfStmt *ifStmt );
71 virtual void visit( WhileStmt *whileStmt );
72 virtual void visit( ForStmt *forStmt );
73 virtual void visit( SwitchStmt *switchStmt );
74 virtual void visit( ChooseStmt *chooseStmt );
75 virtual void visit( CaseStmt *caseStmt );
76 virtual void visit( CatchStmt *catchStmt );
[a08ba92]77 private:
[0dd3a2f]78 HoistStruct();
[c8ffe20b]79
[0dd3a2f]80 template< typename AggDecl > void handleAggregate( AggDecl *aggregateDecl );
[c8ffe20b]81
[0dd3a2f]82 std::list< Declaration * > declsToAdd;
83 bool inStruct;
[a08ba92]84 };
[c8ffe20b]85
[a08ba92]86 class Pass1 : public Visitor {
[0dd3a2f]87 typedef Visitor Parent;
88 virtual void visit( EnumDecl *aggregateDecl );
89 virtual void visit( FunctionType *func );
[a08ba92]90 };
[c8ffe20b]91
[a08ba92]92 class Pass2 : public Indexer {
[0dd3a2f]93 typedef Indexer Parent;
[a08ba92]94 public:
[0dd3a2f]95 Pass2( bool doDebug, const Indexer *indexer );
[a08ba92]96 private:
[0dd3a2f]97 virtual void visit( StructInstType *structInst );
98 virtual void visit( UnionInstType *unionInst );
99 virtual void visit( ContextInstType *contextInst );
100 virtual void visit( StructDecl *structDecl );
101 virtual void visit( UnionDecl *unionDecl );
102 virtual void visit( TypeInstType *typeInst );
103
104 const Indexer *indexer;
105
106 typedef std::map< std::string, std::list< StructInstType * > > ForwardStructsType;
107 typedef std::map< std::string, std::list< UnionInstType * > > ForwardUnionsType;
108 ForwardStructsType forwardStructs;
109 ForwardUnionsType forwardUnions;
[a08ba92]110 };
[c8ffe20b]111
[a08ba92]112 class Pass3 : public Indexer {
[0dd3a2f]113 typedef Indexer Parent;
[a08ba92]114 public:
[0dd3a2f]115 Pass3( const Indexer *indexer );
[a08ba92]116 private:
[0dd3a2f]117 virtual void visit( ObjectDecl *object );
118 virtual void visit( FunctionDecl *func );
[c8ffe20b]119
[0dd3a2f]120 const Indexer *indexer;
[a08ba92]121 };
[c8ffe20b]122
[a08ba92]123 class AddStructAssignment : public Visitor {
124 public:
[0dd3a2f]125 static void addStructAssignment( std::list< Declaration * > &translationUnit );
[c8ffe20b]126
[0dd3a2f]127 std::list< Declaration * > &get_declsToAdd() { return declsToAdd; }
[c8ffe20b]128
[28a8cf9]129 virtual void visit( EnumDecl *enumDecl );
[0dd3a2f]130 virtual void visit( StructDecl *structDecl );
131 virtual void visit( UnionDecl *structDecl );
132 virtual void visit( TypeDecl *typeDecl );
133 virtual void visit( ContextDecl *ctxDecl );
134 virtual void visit( FunctionDecl *functionDecl );
[c8ffe20b]135
[0dd3a2f]136 virtual void visit( FunctionType *ftype );
137 virtual void visit( PointerType *ftype );
[c8ffe20b]138
[0dd3a2f]139 virtual void visit( CompoundStmt *compoundStmt );
140 virtual void visit( IfStmt *ifStmt );
141 virtual void visit( WhileStmt *whileStmt );
142 virtual void visit( ForStmt *forStmt );
143 virtual void visit( SwitchStmt *switchStmt );
144 virtual void visit( ChooseStmt *chooseStmt );
145 virtual void visit( CaseStmt *caseStmt );
146 virtual void visit( CatchStmt *catchStmt );
[3c70d38]147
[0dd3a2f]148 AddStructAssignment() : functionNesting( 0 ) {}
[a08ba92]149 private:
[0dd3a2f]150 template< typename StmtClass > void visitStatement( StmtClass *stmt );
[c8ffe20b]151
[0dd3a2f]152 std::list< Declaration * > declsToAdd;
153 std::set< std::string > structsDone;
154 unsigned int functionNesting; // current level of nested functions
[a08ba92]155 };
[c8ffe20b]156
[a08ba92]157 class EliminateTypedef : public Mutator {
158 public:
[cc79d97]159 EliminateTypedef() : scopeLevel( 0 ) {}
[0dd3a2f]160 static void eliminateTypedef( std::list< Declaration * > &translationUnit );
[a08ba92]161 private:
[0dd3a2f]162 virtual Declaration *mutate( TypedefDecl *typeDecl );
163 virtual TypeDecl *mutate( TypeDecl *typeDecl );
164 virtual DeclarationWithType *mutate( FunctionDecl *funcDecl );
165 virtual ObjectDecl *mutate( ObjectDecl *objDecl );
166 virtual CompoundStmt *mutate( CompoundStmt *compoundStmt );
167 virtual Type *mutate( TypeInstType *aggregateUseType );
168 virtual Expression *mutate( CastExpr *castExpr );
[cc79d97]169
[85c4ef0]170 virtual Declaration *mutate( StructDecl * structDecl );
171 virtual Declaration *mutate( UnionDecl * unionDecl );
172 virtual Declaration *mutate( EnumDecl * enumDecl );
173 virtual Declaration *mutate( ContextDecl * contextDecl );
174
175 template<typename AggDecl>
176 AggDecl *handleAggregate( AggDecl * aggDecl );
177
[cc79d97]178 typedef std::map< std::string, std::pair< TypedefDecl *, int > > TypedefMap;
179 TypedefMap typedefNames;
180 int scopeLevel;
[a08ba92]181 };
[c8ffe20b]182
[a08ba92]183 void validate( std::list< Declaration * > &translationUnit, bool doDebug ) {
[0dd3a2f]184 Pass1 pass1;
185 Pass2 pass2( doDebug, 0 );
186 Pass3 pass3( 0 );
187 EliminateTypedef::eliminateTypedef( translationUnit );
188 HoistStruct::hoistStruct( translationUnit );
189 acceptAll( translationUnit, pass1 );
190 acceptAll( translationUnit, pass2 );
[1869adf]191 // need to collect all of the assignment operators prior to
192 // this point and only generate assignment operators if one doesn't exist
[0dd3a2f]193 AddStructAssignment::addStructAssignment( translationUnit );
194 acceptAll( translationUnit, pass3 );
[a08ba92]195 }
196
197 void validateType( Type *type, const Indexer *indexer ) {
[0dd3a2f]198 Pass1 pass1;
199 Pass2 pass2( false, indexer );
200 Pass3 pass3( indexer );
201 type->accept( pass1 );
202 type->accept( pass2 );
203 type->accept( pass3 );
[a08ba92]204 }
[c8ffe20b]205
[a08ba92]206 template< typename Visitor >
207 void acceptAndAdd( std::list< Declaration * > &translationUnit, Visitor &visitor, bool addBefore ) {
[0dd3a2f]208 std::list< Declaration * >::iterator i = translationUnit.begin();
209 while ( i != translationUnit.end() ) {
210 (*i)->accept( visitor );
211 std::list< Declaration * >::iterator next = i;
212 next++;
213 if ( ! visitor.get_declsToAdd().empty() ) {
214 translationUnit.splice( addBefore ? i : next, visitor.get_declsToAdd() );
215 } // if
216 i = next;
217 } // while
[a08ba92]218 }
[c8ffe20b]219
[a08ba92]220 void HoistStruct::hoistStruct( std::list< Declaration * > &translationUnit ) {
[0dd3a2f]221 HoistStruct hoister;
222 acceptAndAdd( translationUnit, hoister, true );
[a08ba92]223 }
[c8ffe20b]224
[a08ba92]225 HoistStruct::HoistStruct() : inStruct( false ) {
226 }
[c8ffe20b]227
[a08ba92]228 void filter( std::list< Declaration * > &declList, bool (*pred)( Declaration * ), bool doDelete ) {
[0dd3a2f]229 std::list< Declaration * >::iterator i = declList.begin();
230 while ( i != declList.end() ) {
231 std::list< Declaration * >::iterator next = i;
232 ++next;
233 if ( pred( *i ) ) {
234 if ( doDelete ) {
235 delete *i;
236 } // if
237 declList.erase( i );
238 } // if
239 i = next;
240 } // while
[a08ba92]241 }
[c8ffe20b]242
[a08ba92]243 bool isStructOrUnion( Declaration *decl ) {
[0dd3a2f]244 return dynamic_cast< StructDecl * >( decl ) || dynamic_cast< UnionDecl * >( decl );
[a08ba92]245 }
[51b73452]246
[a08ba92]247 template< typename AggDecl >
248 void HoistStruct::handleAggregate( AggDecl *aggregateDecl ) {
[0dd3a2f]249 if ( inStruct ) {
250 // Add elements in stack order corresponding to nesting structure.
251 declsToAdd.push_front( aggregateDecl );
252 Visitor::visit( aggregateDecl );
253 } else {
254 inStruct = true;
255 Visitor::visit( aggregateDecl );
256 inStruct = false;
257 } // if
258 // Always remove the hoisted aggregate from the inner structure.
259 filter( aggregateDecl->get_members(), isStructOrUnion, false );
[a08ba92]260 }
[c8ffe20b]261
[a08ba92]262 void HoistStruct::visit( StructDecl *aggregateDecl ) {
[0dd3a2f]263 handleAggregate( aggregateDecl );
[a08ba92]264 }
[c8ffe20b]265
[a08ba92]266 void HoistStruct::visit( UnionDecl *aggregateDecl ) {
[0dd3a2f]267 handleAggregate( aggregateDecl );
[a08ba92]268 }
[c8ffe20b]269
[a08ba92]270 void HoistStruct::visit( CompoundStmt *compoundStmt ) {
[0dd3a2f]271 addVisit( compoundStmt, *this );
[a08ba92]272 }
[c8ffe20b]273
[a08ba92]274 void HoistStruct::visit( IfStmt *ifStmt ) {
[0dd3a2f]275 addVisit( ifStmt, *this );
[a08ba92]276 }
[c8ffe20b]277
[a08ba92]278 void HoistStruct::visit( WhileStmt *whileStmt ) {
[0dd3a2f]279 addVisit( whileStmt, *this );
[a08ba92]280 }
[c8ffe20b]281
[a08ba92]282 void HoistStruct::visit( ForStmt *forStmt ) {
[0dd3a2f]283 addVisit( forStmt, *this );
[a08ba92]284 }
[c8ffe20b]285
[a08ba92]286 void HoistStruct::visit( SwitchStmt *switchStmt ) {
[0dd3a2f]287 addVisit( switchStmt, *this );
[a08ba92]288 }
[c8ffe20b]289
[a08ba92]290 void HoistStruct::visit( ChooseStmt *switchStmt ) {
[0dd3a2f]291 addVisit( switchStmt, *this );
[a08ba92]292 }
[c8ffe20b]293
[a08ba92]294 void HoistStruct::visit( CaseStmt *caseStmt ) {
[0dd3a2f]295 addVisit( caseStmt, *this );
[a08ba92]296 }
[c8ffe20b]297
[a08ba92]298 void HoistStruct::visit( CatchStmt *cathStmt ) {
[0dd3a2f]299 addVisit( cathStmt, *this );
[a08ba92]300 }
[c8ffe20b]301
[a08ba92]302 void Pass1::visit( EnumDecl *enumDecl ) {
[0dd3a2f]303 // Set the type of each member of the enumeration to be EnumConstant
[c8ffe20b]304
[0dd3a2f]305 for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) {
[f6d7e0f]306 ObjectDecl * obj = dynamic_cast< ObjectDecl * >( *i );
[0dd3a2f]307 assert( obj );
[f6d7e0f]308 // obj->set_type( new EnumInstType( Type::Qualifiers( true, false, false, false, false, false ), enumDecl->get_name() ) );
309 BasicType * enumType = new BasicType( Type::Qualifiers(), BasicType::SignedInt );
310 obj->set_type( enumType ) ;
[0dd3a2f]311 } // for
312 Parent::visit( enumDecl );
[a08ba92]313 }
[51b73452]314
[a08ba92]315 namespace {
[0dd3a2f]316 template< typename DWTIterator >
317 void fixFunctionList( DWTIterator begin, DWTIterator end, FunctionType *func ) {
318 // the only case in which "void" is valid is where it is the only one in the list; then it should be removed
319 // entirely other fix ups are handled by the FixFunction class
320 if ( begin == end ) return;
321 FixFunction fixer;
322 DWTIterator i = begin;
323 *i = (*i )->acceptMutator( fixer );
324 if ( fixer.get_isVoid() ) {
325 DWTIterator j = i;
326 ++i;
327 func->get_parameters().erase( j );
328 if ( i != end ) {
329 throw SemanticError( "invalid type void in function type ", func );
330 } // if
331 } else {
332 ++i;
333 for ( ; i != end; ++i ) {
334 FixFunction fixer;
335 *i = (*i )->acceptMutator( fixer );
336 if ( fixer.get_isVoid() ) {
337 throw SemanticError( "invalid type void in function type ", func );
338 } // if
339 } // for
340 } // if
341 }
[a08ba92]342 }
[c8ffe20b]343
[a08ba92]344 void Pass1::visit( FunctionType *func ) {
[0dd3a2f]345 // Fix up parameters and return types
346 fixFunctionList( func->get_parameters().begin(), func->get_parameters().end(), func );
347 fixFunctionList( func->get_returnVals().begin(), func->get_returnVals().end(), func );
348 Visitor::visit( func );
[a08ba92]349 }
[c8ffe20b]350
[a08ba92]351 Pass2::Pass2( bool doDebug, const Indexer *other_indexer ) : Indexer( doDebug ) {
[0dd3a2f]352 if ( other_indexer ) {
353 indexer = other_indexer;
354 } else {
355 indexer = this;
356 } // if
[a08ba92]357 }
[c8ffe20b]358
[a08ba92]359 void Pass2::visit( StructInstType *structInst ) {
[0dd3a2f]360 Parent::visit( structInst );
361 StructDecl *st = indexer->lookupStruct( structInst->get_name() );
362 // it's not a semantic error if the struct is not found, just an implicit forward declaration
363 if ( st ) {
364 assert( ! structInst->get_baseStruct() || structInst->get_baseStruct()->get_members().empty() || ! st->get_members().empty() );
365 structInst->set_baseStruct( st );
366 } // if
367 if ( ! st || st->get_members().empty() ) {
368 // use of forward declaration
369 forwardStructs[ structInst->get_name() ].push_back( structInst );
370 } // if
[a08ba92]371 }
[c8ffe20b]372
[a08ba92]373 void Pass2::visit( UnionInstType *unionInst ) {
[0dd3a2f]374 Parent::visit( unionInst );
375 UnionDecl *un = indexer->lookupUnion( unionInst->get_name() );
376 // it's not a semantic error if the union is not found, just an implicit forward declaration
377 if ( un ) {
378 unionInst->set_baseUnion( un );
379 } // if
380 if ( ! un || un->get_members().empty() ) {
381 // use of forward declaration
382 forwardUnions[ unionInst->get_name() ].push_back( unionInst );
383 } // if
[a08ba92]384 }
[c8ffe20b]385
[a08ba92]386 void Pass2::visit( ContextInstType *contextInst ) {
[0dd3a2f]387 Parent::visit( contextInst );
388 ContextDecl *ctx = indexer->lookupContext( contextInst->get_name() );
389 if ( ! ctx ) {
390 throw SemanticError( "use of undeclared context " + contextInst->get_name() );
[17cd4eb]391 } // if
[0dd3a2f]392 for ( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) {
393 for ( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) {
394 if ( ContextInstType *otherCtx = dynamic_cast< ContextInstType * >(*assert ) ) {
395 cloneAll( otherCtx->get_members(), contextInst->get_members() );
396 } else {
397 contextInst->get_members().push_back( (*assert )->clone() );
398 } // if
399 } // for
400 } // for
401 applySubstitution( ctx->get_parameters().begin(), ctx->get_parameters().end(), contextInst->get_parameters().begin(), ctx->get_members().begin(), ctx->get_members().end(), back_inserter( contextInst->get_members() ) );
[a08ba92]402 }
[c8ffe20b]403
[a08ba92]404 void Pass2::visit( StructDecl *structDecl ) {
[0dd3a2f]405 if ( ! structDecl->get_members().empty() ) {
406 ForwardStructsType::iterator fwds = forwardStructs.find( structDecl->get_name() );
407 if ( fwds != forwardStructs.end() ) {
408 for ( std::list< StructInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
409 (*inst )->set_baseStruct( structDecl );
410 } // for
411 forwardStructs.erase( fwds );
412 } // if
413 } // if
414 Indexer::visit( structDecl );
[a08ba92]415 }
[c8ffe20b]416
[a08ba92]417 void Pass2::visit( UnionDecl *unionDecl ) {
[0dd3a2f]418 if ( ! unionDecl->get_members().empty() ) {
419 ForwardUnionsType::iterator fwds = forwardUnions.find( unionDecl->get_name() );
420 if ( fwds != forwardUnions.end() ) {
421 for ( std::list< UnionInstType * >::iterator inst = fwds->second.begin(); inst != fwds->second.end(); ++inst ) {
422 (*inst )->set_baseUnion( unionDecl );
423 } // for
424 forwardUnions.erase( fwds );
425 } // if
426 } // if
427 Indexer::visit( unionDecl );
[a08ba92]428 }
[c8ffe20b]429
[a08ba92]430 void Pass2::visit( TypeInstType *typeInst ) {
[0dd3a2f]431 if ( NamedTypeDecl *namedTypeDecl = lookupType( typeInst->get_name() ) ) {
432 if ( TypeDecl *typeDecl = dynamic_cast< TypeDecl * >( namedTypeDecl ) ) {
433 typeInst->set_isFtype( typeDecl->get_kind() == TypeDecl::Ftype );
434 } // if
435 } // if
[a08ba92]436 }
[c8ffe20b]437
[a08ba92]438 Pass3::Pass3( const Indexer *other_indexer ) : Indexer( false ) {
[0dd3a2f]439 if ( other_indexer ) {
440 indexer = other_indexer;
441 } else {
442 indexer = this;
443 } // if
[a08ba92]444 }
[c8ffe20b]445
[a08ba92]446 void forallFixer( Type *func ) {
[0dd3a2f]447 // Fix up assertions
448 for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) {
449 std::list< DeclarationWithType * > toBeDone, nextRound;
450 toBeDone.splice( toBeDone.end(), (*type )->get_assertions() );
451 while ( ! toBeDone.empty() ) {
452 for ( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) {
453 if ( ContextInstType *ctx = dynamic_cast< ContextInstType * >( (*assertion )->get_type() ) ) {
454 for ( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) {
455 DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i );
456 assert( dwt );
457 nextRound.push_back( dwt->clone() );
458 }
459 delete ctx;
460 } else {
461 FixFunction fixer;
462 *assertion = (*assertion )->acceptMutator( fixer );
463 if ( fixer.get_isVoid() ) {
464 throw SemanticError( "invalid type void in assertion of function ", func );
465 }
466 (*type )->get_assertions().push_back( *assertion );
467 } // if
468 } // for
469 toBeDone.clear();
470 toBeDone.splice( toBeDone.end(), nextRound );
471 } // while
472 } // for
[a08ba92]473 }
[c8ffe20b]474
[a08ba92]475 void Pass3::visit( ObjectDecl *object ) {
[0dd3a2f]476 forallFixer( object->get_type() );
477 if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) {
478 forallFixer( pointer->get_base() );
479 } // if
480 Parent::visit( object );
481 object->fixUniqueId();
[a08ba92]482 }
[c8ffe20b]483
[a08ba92]484 void Pass3::visit( FunctionDecl *func ) {
[0dd3a2f]485 forallFixer( func->get_type() );
486 Parent::visit( func );
487 func->fixUniqueId();
[a08ba92]488 }
[c8ffe20b]489
[a08ba92]490 static const std::list< std::string > noLabels;
[c8ffe20b]491
[a08ba92]492 void AddStructAssignment::addStructAssignment( std::list< Declaration * > &translationUnit ) {
[0dd3a2f]493 AddStructAssignment visitor;
494 acceptAndAdd( translationUnit, visitor, false );
[a08ba92]495 }
[c8ffe20b]496
[a08ba92]497 template< typename OutputIterator >
498 void makeScalarAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, DeclarationWithType *member, OutputIterator out ) {
[0dd3a2f]499 ObjectDecl *obj = dynamic_cast<ObjectDecl *>( member );
500 // unnamed bit fields are not copied as they cannot be accessed
501 if ( obj != NULL && obj->get_name() == "" && obj->get_bitfieldWidth() != NULL ) return;
[c8ffe20b]502
[0dd3a2f]503 UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) );
[c8ffe20b]504
[0dd3a2f]505 UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
506 derefExpr->get_args().push_back( new VariableExpr( dstParam ) );
[c8ffe20b]507
[0dd3a2f]508 // do something special for unnamed members
509 Expression *dstselect = new AddressExpr( new MemberExpr( member, derefExpr ) );
510 assignExpr->get_args().push_back( dstselect );
[c8ffe20b]511
[0dd3a2f]512 Expression *srcselect = new MemberExpr( member, new VariableExpr( srcParam ) );
513 assignExpr->get_args().push_back( srcselect );
[c8ffe20b]514
[0dd3a2f]515 *out++ = new ExprStmt( noLabels, assignExpr );
[a08ba92]516 }
[c8ffe20b]517
[a08ba92]518 template< typename OutputIterator >
519 void makeArrayAssignment( ObjectDecl *srcParam, ObjectDecl *dstParam, DeclarationWithType *member, ArrayType *array, OutputIterator out ) {
[0dd3a2f]520 static UniqueName indexName( "_index" );
[c8ffe20b]521
[0dd3a2f]522 // for a flexible array member nothing is done -- user must define own assignment
523 if ( ! array->get_dimension() ) return;
[c8ffe20b]524
[68cd1ce]525 ObjectDecl *index = new ObjectDecl( indexName.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, new BasicType( Type::Qualifiers(), BasicType::SignedInt ), 0 );
[0dd3a2f]526 *out++ = new DeclStmt( noLabels, index );
[c8ffe20b]527
[0dd3a2f]528 UntypedExpr *init = new UntypedExpr( new NameExpr( "?=?" ) );
529 init->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
530 init->get_args().push_back( new NameExpr( "0" ) );
531 Statement *initStmt = new ExprStmt( noLabels, init );
[145f1fc]532 std::list<Statement *> initList;
533 initList.push_back( initStmt );
534
[0dd3a2f]535 UntypedExpr *cond = new UntypedExpr( new NameExpr( "?<?" ) );
536 cond->get_args().push_back( new VariableExpr( index ) );
537 cond->get_args().push_back( array->get_dimension()->clone() );
[c8ffe20b]538
[0dd3a2f]539 UntypedExpr *inc = new UntypedExpr( new NameExpr( "++?" ) );
540 inc->get_args().push_back( new AddressExpr( new VariableExpr( index ) ) );
[c8ffe20b]541
[0dd3a2f]542 UntypedExpr *assignExpr = new UntypedExpr( new NameExpr( "?=?" ) );
[c8ffe20b]543
[0dd3a2f]544 UntypedExpr *derefExpr = new UntypedExpr( new NameExpr( "*?" ) );
545 derefExpr->get_args().push_back( new VariableExpr( dstParam ) );
[c8ffe20b]546
[0dd3a2f]547 Expression *dstselect = new MemberExpr( member, derefExpr );
548 UntypedExpr *dstIndex = new UntypedExpr( new NameExpr( "?+?" ) );
549 dstIndex->get_args().push_back( dstselect );
550 dstIndex->get_args().push_back( new VariableExpr( index ) );
551 assignExpr->get_args().push_back( dstIndex );
[c8ffe20b]552
[0dd3a2f]553 Expression *srcselect = new MemberExpr( member, new VariableExpr( srcParam ) );
554 UntypedExpr *srcIndex = new UntypedExpr( new NameExpr( "?[?]" ) );
555 srcIndex->get_args().push_back( srcselect );
556 srcIndex->get_args().push_back( new VariableExpr( index ) );
557 assignExpr->get_args().push_back( srcIndex );
[c8ffe20b]558
[145f1fc]559 *out++ = new ForStmt( noLabels, initList, cond, inc, new ExprStmt( noLabels, assignExpr ) );
[a08ba92]560 }
[c8ffe20b]561
[f6d7e0f]562 //E ?=?(E volatile*, int),
563 // ?=?(E _Atomic volatile*, int);
564 void makeEnumAssignment( EnumDecl *enumDecl, EnumInstType *refType, unsigned int functionNesting, std::list< Declaration * > &declsToAdd ) {
565 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
566
567 ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
568 assignType->get_returnVals().push_back( returnVal );
569
570 // need two assignment operators with different types
571 FunctionType * assignType2 = assignType->clone();
572
573 // E ?=?(E volatile *, E)
574 Type *etype = refType->clone();
[8686f31]575 // etype->get_qualifiers() += Type::Qualifiers(false, true, false, false, false, false);
[f6d7e0f]576
577 ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), etype ), 0 );
578 assignType->get_parameters().push_back( dstParam );
579
580 ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, etype->clone(), 0 );
581 assignType->get_parameters().push_back( srcParam );
582
583 // E ?=?(E volatile *, int)
584 assignType2->get_parameters().push_back( dstParam->clone() );
585 BasicType * paramType = new BasicType(Type::Qualifiers(), BasicType::SignedInt);
586 ObjectDecl *srcParam2 = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, paramType, 0 );
587 assignType2->get_parameters().push_back( srcParam2 );
588
589 // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
590 // because each unit generates copies of the default routines for each aggregate.
591
592 // since there is no definition, these should not be inline
593 // make these intrinsic so that the code generator does not make use of them
594 FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, assignType, 0, false, false );
595 assignDecl->fixUniqueId();
596 FunctionDecl *assignDecl2 = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::Intrinsic, assignType2, 0, false, false );
597 assignDecl2->fixUniqueId();
598
[cc79d97]599 // these should be built in the same way that the prelude
600 // functions are, so build a list containing the prototypes
601 // and allow MakeLibCfa to autogenerate the bodies.
[f6d7e0f]602 std::list< Declaration * > assigns;
603 assigns.push_back( assignDecl );
604 assigns.push_back( assignDecl2 );
605
606 LibCfa::makeLibCfa( assigns );
607
[cc79d97]608 // need to remove the prototypes, since this may be nested in a routine
[8686f31]609 for (int start = 0, end = assigns.size()/2; start < end; start++) {
610 delete assigns.front();
611 assigns.pop_front();
612 }
613
[f6d7e0f]614 declsToAdd.insert( declsToAdd.begin(), assigns.begin(), assigns.end() );
615 }
616
617
[a08ba92]618 Declaration *makeStructAssignment( StructDecl *aggregateDecl, StructInstType *refType, unsigned int functionNesting ) {
[0dd3a2f]619 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
620
[68cd1ce]621 ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
[0dd3a2f]622 assignType->get_returnVals().push_back( returnVal );
623
[68cd1ce]624 ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), refType->clone() ), 0 );
[0dd3a2f]625 assignType->get_parameters().push_back( dstParam );
626
[68cd1ce]627 ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType, 0 );
[0dd3a2f]628 assignType->get_parameters().push_back( srcParam );
629
630 // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
631 // because each unit generates copies of the default routines for each aggregate.
[de62360d]632 FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false );
[0dd3a2f]633 assignDecl->fixUniqueId();
634
635 for ( std::list< Declaration * >::const_iterator member = aggregateDecl->get_members().begin(); member != aggregateDecl->get_members().end(); ++member ) {
636 if ( DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *member ) ) {
[367e082]637 // query the type qualifiers of this field and skip assigning it if it is marked const.
638 // If it is an array type, we need to strip off the array layers to find its qualifiers.
639 Type * type = dwt->get_type();
640 while ( ArrayType * at = dynamic_cast< ArrayType * >( type ) ) {
641 type = at->get_base();
642 }
643
644 if ( type->get_qualifiers().isConst ) {
[53a2e97]645 // don't assign const members
646 continue;
647 }
648
[0dd3a2f]649 if ( ArrayType *array = dynamic_cast< ArrayType * >( dwt->get_type() ) ) {
650 makeArrayAssignment( srcParam, dstParam, dwt, array, back_inserter( assignDecl->get_statements()->get_kids() ) );
651 } else {
652 makeScalarAssignment( srcParam, dstParam, dwt, back_inserter( assignDecl->get_statements()->get_kids() ) );
653 } // if
654 } // if
655 } // for
656 assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
[c8ffe20b]657
[0dd3a2f]658 return assignDecl;
[a08ba92]659 }
[c8ffe20b]660
[a08ba92]661 Declaration *makeUnionAssignment( UnionDecl *aggregateDecl, UnionInstType *refType, unsigned int functionNesting ) {
[0dd3a2f]662 FunctionType *assignType = new FunctionType( Type::Qualifiers(), false );
[c8ffe20b]663
[68cd1ce]664 ObjectDecl *returnVal = new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType->clone(), 0 );
[0dd3a2f]665 assignType->get_returnVals().push_back( returnVal );
[c8ffe20b]666
[68cd1ce]667 ObjectDecl *dstParam = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), refType->clone() ), 0 );
[0dd3a2f]668 assignType->get_parameters().push_back( dstParam );
[c8ffe20b]669
[68cd1ce]670 ObjectDecl *srcParam = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, refType, 0 );
[0dd3a2f]671 assignType->get_parameters().push_back( srcParam );
[c8ffe20b]672
[0dd3a2f]673 // Routines at global scope marked "static" to prevent multiple definitions is separate translation units
674 // because each unit generates copies of the default routines for each aggregate.
[de62360d]675 FunctionDecl *assignDecl = new FunctionDecl( "?=?", functionNesting > 0 ? DeclarationNode::NoStorageClass : DeclarationNode::Static, LinkageSpec::AutoGen, assignType, new CompoundStmt( noLabels ), true, false );
[0dd3a2f]676 assignDecl->fixUniqueId();
[c8ffe20b]677
[0dd3a2f]678 UntypedExpr *copy = new UntypedExpr( new NameExpr( "__builtin_memcpy" ) );
679 copy->get_args().push_back( new VariableExpr( dstParam ) );
680 copy->get_args().push_back( new AddressExpr( new VariableExpr( srcParam ) ) );
681 copy->get_args().push_back( new SizeofExpr( refType->clone() ) );
[c8ffe20b]682
[0dd3a2f]683 assignDecl->get_statements()->get_kids().push_back( new ExprStmt( noLabels, copy ) );
684 assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
[c8ffe20b]685
[0dd3a2f]686 return assignDecl;
[a08ba92]687 }
[c8ffe20b]688
[f6d7e0f]689 void AddStructAssignment::visit( EnumDecl *enumDecl ) {
690 if ( ! enumDecl->get_members().empty() ) {
691 EnumInstType *enumInst = new EnumInstType( Type::Qualifiers(), enumDecl->get_name() );
692 // enumInst->set_baseEnum( enumDecl );
693 // declsToAdd.push_back(
694 makeEnumAssignment( enumDecl, enumInst, functionNesting, declsToAdd );
695 }
696 }
697
[a08ba92]698 void AddStructAssignment::visit( StructDecl *structDecl ) {
[0dd3a2f]699 if ( ! structDecl->get_members().empty() && structsDone.find( structDecl->get_name() ) == structsDone.end() ) {
700 StructInstType *structInst = new StructInstType( Type::Qualifiers(), structDecl->get_name() );
701 structInst->set_baseStruct( structDecl );
702 declsToAdd.push_back( makeStructAssignment( structDecl, structInst, functionNesting ) );
703 structsDone.insert( structDecl->get_name() );
704 } // if
[a08ba92]705 }
[c8ffe20b]706
[a08ba92]707 void AddStructAssignment::visit( UnionDecl *unionDecl ) {
[0dd3a2f]708 if ( ! unionDecl->get_members().empty() ) {
709 UnionInstType *unionInst = new UnionInstType( Type::Qualifiers(), unionDecl->get_name() );
710 unionInst->set_baseUnion( unionDecl );
711 declsToAdd.push_back( makeUnionAssignment( unionDecl, unionInst, functionNesting ) );
712 } // if
[a08ba92]713 }
[c8ffe20b]714
[a08ba92]715 void AddStructAssignment::visit( TypeDecl *typeDecl ) {
[0dd3a2f]716 CompoundStmt *stmts = 0;
717 TypeInstType *typeInst = new TypeInstType( Type::Qualifiers(), typeDecl->get_name(), false );
718 typeInst->set_baseType( typeDecl );
[68cd1ce]719 ObjectDecl *src = new ObjectDecl( "_src", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst->clone(), 0 );
720 ObjectDecl *dst = new ObjectDecl( "_dst", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, new PointerType( Type::Qualifiers(), typeInst->clone() ), 0 );
[0dd3a2f]721 if ( typeDecl->get_base() ) {
722 stmts = new CompoundStmt( std::list< Label >() );
723 UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
724 assign->get_args().push_back( new CastExpr( new VariableExpr( dst ), new PointerType( Type::Qualifiers(), typeDecl->get_base()->clone() ) ) );
725 assign->get_args().push_back( new CastExpr( new VariableExpr( src ), typeDecl->get_base()->clone() ) );
726 stmts->get_kids().push_back( new ReturnStmt( std::list< Label >(), assign ) );
727 } // if
728 FunctionType *type = new FunctionType( Type::Qualifiers(), false );
[68cd1ce]729 type->get_returnVals().push_back( new ObjectDecl( "", DeclarationNode::NoStorageClass, LinkageSpec::Cforall, 0, typeInst, 0 ) );
[0dd3a2f]730 type->get_parameters().push_back( dst );
731 type->get_parameters().push_back( src );
[de62360d]732 FunctionDecl *func = new FunctionDecl( "?=?", DeclarationNode::NoStorageClass, LinkageSpec::AutoGen, type, stmts, false, false );
[0dd3a2f]733 declsToAdd.push_back( func );
[a08ba92]734 }
[c8ffe20b]735
[a08ba92]736 void addDecls( std::list< Declaration * > &declsToAdd, std::list< Statement * > &statements, std::list< Statement * >::iterator i ) {
[5f2f2d7]737 for ( std::list< Declaration * >::iterator decl = declsToAdd.begin(); decl != declsToAdd.end(); ++decl ) {
738 statements.insert( i, new DeclStmt( noLabels, *decl ) );
739 } // for
740 declsToAdd.clear();
[a08ba92]741 }
[c8ffe20b]742
[a08ba92]743 void AddStructAssignment::visit( FunctionType *) {
[0dd3a2f]744 // ensure that we don't add assignment ops for types defined as part of the function
[a08ba92]745 }
[c8ffe20b]746
[a08ba92]747 void AddStructAssignment::visit( PointerType *) {
[0dd3a2f]748 // ensure that we don't add assignment ops for types defined as part of the pointer
[a08ba92]749 }
[c8ffe20b]750
[a08ba92]751 void AddStructAssignment::visit( ContextDecl *) {
[0dd3a2f]752 // ensure that we don't add assignment ops for types defined as part of the context
[a08ba92]753 }
[c8ffe20b]754
[a08ba92]755 template< typename StmtClass >
756 inline void AddStructAssignment::visitStatement( StmtClass *stmt ) {
[0dd3a2f]757 std::set< std::string > oldStructs = structsDone;
758 addVisit( stmt, *this );
759 structsDone = oldStructs;
[a08ba92]760 }
[c8ffe20b]761
[a08ba92]762 void AddStructAssignment::visit( FunctionDecl *functionDecl ) {
[0dd3a2f]763 maybeAccept( functionDecl->get_functionType(), *this );
764 acceptAll( functionDecl->get_oldDecls(), *this );
765 functionNesting += 1;
766 maybeAccept( functionDecl->get_statements(), *this );
767 functionNesting -= 1;
[a08ba92]768 }
[3c70d38]769
[a08ba92]770 void AddStructAssignment::visit( CompoundStmt *compoundStmt ) {
[0dd3a2f]771 visitStatement( compoundStmt );
[a08ba92]772 }
[c8ffe20b]773
[a08ba92]774 void AddStructAssignment::visit( IfStmt *ifStmt ) {
[0dd3a2f]775 visitStatement( ifStmt );
[a08ba92]776 }
[c8ffe20b]777
[a08ba92]778 void AddStructAssignment::visit( WhileStmt *whileStmt ) {
[0dd3a2f]779 visitStatement( whileStmt );
[a08ba92]780 }
[c8ffe20b]781
[a08ba92]782 void AddStructAssignment::visit( ForStmt *forStmt ) {
[0dd3a2f]783 visitStatement( forStmt );
[a08ba92]784 }
[c8ffe20b]785
[a08ba92]786 void AddStructAssignment::visit( SwitchStmt *switchStmt ) {
[0dd3a2f]787 visitStatement( switchStmt );
[a08ba92]788 }
[c8ffe20b]789
[a08ba92]790 void AddStructAssignment::visit( ChooseStmt *switchStmt ) {
[0dd3a2f]791 visitStatement( switchStmt );
[a08ba92]792 }
[c8ffe20b]793
[a08ba92]794 void AddStructAssignment::visit( CaseStmt *caseStmt ) {
[0dd3a2f]795 visitStatement( caseStmt );
[a08ba92]796 }
[c8ffe20b]797
[a08ba92]798 void AddStructAssignment::visit( CatchStmt *cathStmt ) {
[0dd3a2f]799 visitStatement( cathStmt );
[a08ba92]800 }
[c8ffe20b]801
[a08ba92]802 bool isTypedef( Declaration *decl ) {
[0dd3a2f]803 return dynamic_cast< TypedefDecl * >( decl );
[a08ba92]804 }
[c8ffe20b]805
[a08ba92]806 void EliminateTypedef::eliminateTypedef( std::list< Declaration * > &translationUnit ) {
[0dd3a2f]807 EliminateTypedef eliminator;
808 mutateAll( translationUnit, eliminator );
809 filter( translationUnit, isTypedef, true );
[a08ba92]810 }
[c8ffe20b]811
[85c4ef0]812 Type *EliminateTypedef::mutate( TypeInstType * typeInst ) {
[cc79d97]813 // instances of typedef types will come here. If it is an instance
814 // of a typdef type, link the instance to its actual type.
815 TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() );
[0dd3a2f]816 if ( def != typedefNames.end() ) {
[cc79d97]817 Type *ret = def->second.first->get_base()->clone();
[0dd3a2f]818 ret->get_qualifiers() += typeInst->get_qualifiers();
819 delete typeInst;
820 return ret;
821 } // if
822 return typeInst;
[a08ba92]823 }
[c8ffe20b]824
[85c4ef0]825 Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) {
[0dd3a2f]826 Declaration *ret = Mutator::mutate( tyDecl );
[cc79d97]827 if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) {
828 // typedef to the same name from the same scope
829 // must be from the same type
830
831 Type * t1 = tyDecl->get_base();
832 Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base();
833 if ( ! typeEquals( t1, t2, true ) ) {
834 throw SemanticError( "cannot redefine typedef: " + tyDecl->get_name() );
[85c4ef0]835 }
[cc79d97]836 } else {
837 typedefNames[ tyDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel );
838 } // if
839
[0dd3a2f]840 // When a typedef is a forward declaration:
841 // typedef struct screen SCREEN;
842 // the declaration portion must be retained:
843 // struct screen;
844 // because the expansion of the typedef is:
845 // void rtn( SCREEN *p ) => void rtn( struct screen *p )
846 // hence the type-name "screen" must be defined.
847 // Note, qualifiers on the typedef are superfluous for the forward declaration.
848 if ( StructInstType *aggDecl = dynamic_cast< StructInstType * >( tyDecl->get_base() ) ) {
849 return new StructDecl( aggDecl->get_name() );
850 } else if ( UnionInstType *aggDecl = dynamic_cast< UnionInstType * >( tyDecl->get_base() ) ) {
851 return new UnionDecl( aggDecl->get_name() );
852 } else {
853 return ret;
854 } // if
[a08ba92]855 }
[c8ffe20b]856
[85c4ef0]857 TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) {
[cc79d97]858 TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() );
[0dd3a2f]859 if ( i != typedefNames.end() ) {
860 typedefNames.erase( i ) ;
861 } // if
862 return typeDecl;
[a08ba92]863 }
[c8ffe20b]864
[85c4ef0]865 DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) {
[cc79d97]866 TypedefMap oldNames = typedefNames;
[0dd3a2f]867 DeclarationWithType *ret = Mutator::mutate( funcDecl );
868 typedefNames = oldNames;
869 return ret;
[a08ba92]870 }
[c8ffe20b]871
[85c4ef0]872 ObjectDecl *EliminateTypedef::mutate( ObjectDecl * objDecl ) {
[cc79d97]873 TypedefMap oldNames = typedefNames;
[0dd3a2f]874 ObjectDecl *ret = Mutator::mutate( objDecl );
875 typedefNames = oldNames;
876 return ret;
[a08ba92]877 }
[c8ffe20b]878
[85c4ef0]879 Expression *EliminateTypedef::mutate( CastExpr * castExpr ) {
[cc79d97]880 TypedefMap oldNames = typedefNames;
[0dd3a2f]881 Expression *ret = Mutator::mutate( castExpr );
882 typedefNames = oldNames;
883 return ret;
[a08ba92]884 }
[c8ffe20b]885
[85c4ef0]886 CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) {
[cc79d97]887 TypedefMap oldNames = typedefNames;
888 scopeLevel += 1;
[0dd3a2f]889 CompoundStmt *ret = Mutator::mutate( compoundStmt );
[cc79d97]890 scopeLevel -= 1;
[0dd3a2f]891 std::list< Statement * >::iterator i = compoundStmt->get_kids().begin();
892 while ( i != compoundStmt->get_kids().end() ) {
[85c4ef0]893 std::list< Statement * >::iterator next = i+1;
[0dd3a2f]894 if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) {
895 if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) {
896 delete *i;
897 compoundStmt->get_kids().erase( i );
898 } // if
899 } // if
900 i = next;
901 } // while
902 typedefNames = oldNames;
903 return ret;
[a08ba92]904 }
[85c4ef0]905
906 // there may be typedefs nested within aggregates
907 // in order for everything to work properly, these
908 // should be removed as well
909 template<typename AggDecl>
910 AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) {
911 std::list<Declaration *>::iterator it = aggDecl->get_members().begin();
912 for ( ; it != aggDecl->get_members().end(); ) {
913 std::list< Declaration * >::iterator next = it+1;
914 if ( dynamic_cast< TypedefDecl * >( *it ) ) {
915 delete *it;
916 aggDecl->get_members().erase( it );
917 } // if
918 it = next;
919 }
920 return aggDecl;
921 }
922
923 Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) {
924 Mutator::mutate( structDecl );
925 return handleAggregate( structDecl );
926 }
927
928 Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) {
929 Mutator::mutate( unionDecl );
930 return handleAggregate( unionDecl );
931 }
932
933 Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) {
934 Mutator::mutate( enumDecl );
935 return handleAggregate( enumDecl );
936 }
937
938 Declaration *EliminateTypedef::mutate( ContextDecl * contextDecl ) {
939 Mutator::mutate( contextDecl );
940 return handleAggregate( contextDecl );
941 }
942
[51b73452]943} // namespace SymTab
[0dd3a2f]944
945// Local Variables: //
946// tab-width: 4 //
947// mode: c++ //
948// compile-command: "make install" //
949// End: //
Note: See TracBrowser for help on using the repository browser.