source: src/SymTab/Validate.cc @ 1e8f143

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsctordeferred_resndemanglerenumforall-pointer-decaygc_noraiijacob/cs343-translationjenkins-sandboxmemorynew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newstringwith_gc
Last change on this file since 1e8f143 was 73737e5, checked in by Aaron Moss <a3moss@…>, 9 years ago

Add doc comments

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