source: src/SymTab/Validate.cc @ 9163b9c

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 9163b9c was 9163b9c, checked in by Aaron Moss <a3moss@…>, 9 years ago

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

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