source: src/SymTab/Validate.cc@ 4673385

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 string with_gc
Last change on this file since 4673385 was 1cbca6e, checked in by Rob Schluntz <rschlunt@…>, 10 years ago

check length, etc. in unification of arrays, switch typedef equality for typesCompatible

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