Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Validate.cc

    r0215a76f r85c4ef0  
    1010// Created On       : Sun May 17 21:50:04 2015
    1111// Last Modified By : Rob Schluntz
    12 // Last Modified On : Wed Jul 08 12:49:36 2015
    13 // Update Count     : 166
     12// Last Modified On : Mon Jul 13 14:38:19 2015
     13// Update Count     : 184
    1414//
    1515
     
    6060        class HoistStruct : public Visitor {
    6161          public:
    62                 /// Flattens nested struct types
    6362                static void hoistStruct( std::list< Declaration * > &translationUnit );
    6463 
     
    8584        };
    8685
    87         /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers
    8886        class Pass1 : public Visitor {
    8987                typedef Visitor Parent;
     
    9189                virtual void visit( FunctionType *func );
    9290        };
    93 
    94         /// Associates forward declarations of aggregates with their definitions
     91 
    9592        class Pass2 : public Indexer {
    9693                typedef Indexer Parent;
     
    113110        };
    114111
    115         /// Replaces array and function types in forall lists by appropriate pointer type
    116112        class Pass3 : public Indexer {
    117113                typedef Indexer Parent;
     
    127123        class AddStructAssignment : public Visitor {
    128124          public:
    129                 /// Generates assignment operators for aggregate types as required
    130125                static void addStructAssignment( std::list< Declaration * > &translationUnit );
    131126
     
    172167                virtual Type *mutate( TypeInstType *aggregateUseType );
    173168                virtual Expression *mutate( CastExpr *castExpr );
     169
     170                virtual Declaration *mutate( StructDecl * structDecl );
     171                virtual Declaration *mutate( UnionDecl * unionDecl );
     172                virtual Declaration *mutate( EnumDecl * enumDecl );
     173                virtual Declaration *mutate( ContextDecl * contextDecl );
     174
     175                template<typename AggDecl>
     176                AggDecl *handleAggregate( AggDecl * aggDecl );
    174177
    175178                typedef std::map< std::string, std::pair< TypedefDecl *, int > > TypedefMap;
     
    441444        }
    442445
    443         /// Fix up assertions
    444446        void forallFixer( Type *func ) {
     447                // Fix up assertions
    445448                for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) {
    446449                        std::list< DeclarationWithType * > toBeDone, nextRound;
     
    805808        }
    806809
    807         Type *EliminateTypedef::mutate( TypeInstType *typeInst ) {
     810        Type *EliminateTypedef::mutate( TypeInstType * typeInst ) {
    808811                // instances of typedef types will come here. If it is an instance
    809812                // of a typdef type, link the instance to its actual type.
     
    812815                        Type *ret = def->second.first->get_base()->clone();
    813816                        ret->get_qualifiers() += typeInst->get_qualifiers();
    814                         // place instance parameters on the typedef'd type
    815                         if ( ! typeInst->get_parameters().empty() ) {
    816                                 ReferenceToType *rtt = dynamic_cast<ReferenceToType*>(ret);
    817                                 if ( ! rtt ) {
    818                                         throw SemanticError("cannot apply type parameters to base type of " + typeInst->get_name());
    819                                 }
    820                                 rtt->get_parameters().clear();
    821                                 cloneAll(typeInst->get_parameters(), rtt->get_parameters());
    822                         }
    823817                        delete typeInst;
    824818                        return ret;
     
    827821        }
    828822
    829         Declaration *EliminateTypedef::mutate( TypedefDecl *tyDecl ) {
     823        Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) {
    830824                Declaration *ret = Mutator::mutate( tyDecl );
    831825                if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) {
     
    837831                        if ( ! typeEquals( t1, t2, true ) ) {
    838832                                throw SemanticError( "cannot redefine typedef: " + tyDecl->get_name() );
    839                         } 
     833                        }
    840834                } else {
    841835                        typedefNames[ tyDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel );
     
    859853        }
    860854
    861         TypeDecl *EliminateTypedef::mutate( TypeDecl *typeDecl ) {
     855        TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) {
    862856                TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() );
    863857                if ( i != typedefNames.end() ) {
     
    867861        }
    868862
    869         DeclarationWithType *EliminateTypedef::mutate( FunctionDecl *funcDecl ) {
     863        DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) {
    870864                TypedefMap oldNames = typedefNames;
    871865                DeclarationWithType *ret = Mutator::mutate( funcDecl );
     
    874868        }
    875869
    876         ObjectDecl *EliminateTypedef::mutate( ObjectDecl *objDecl ) {
     870        ObjectDecl *EliminateTypedef::mutate( ObjectDecl * objDecl ) {
    877871                TypedefMap oldNames = typedefNames;
    878872                ObjectDecl *ret = Mutator::mutate( objDecl );
     
    881875        }
    882876
    883         Expression *EliminateTypedef::mutate( CastExpr *castExpr ) {
     877        Expression *EliminateTypedef::mutate( CastExpr * castExpr ) {
    884878                TypedefMap oldNames = typedefNames;
    885879                Expression *ret = Mutator::mutate( castExpr );
     
    888882        }
    889883
    890         CompoundStmt *EliminateTypedef::mutate( CompoundStmt *compoundStmt ) {
     884        CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) {
    891885                TypedefMap oldNames = typedefNames;
    892886                scopeLevel += 1;
     
    895889                std::list< Statement * >::iterator i = compoundStmt->get_kids().begin();
    896890                while ( i != compoundStmt->get_kids().end() ) {
    897                         std::list< Statement * >::iterator next = i;
    898                         ++next;
     891                        std::list< Statement * >::iterator next = i+1;
    899892                        if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) {
    900893                                if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) {
     
    908901                return ret;
    909902        }
     903
     904        // there may be typedefs nested within aggregates
     905        // in order for everything to work properly, these
     906        // should be removed as well
     907        template<typename AggDecl>
     908        AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) {
     909                std::list<Declaration *>::iterator it = aggDecl->get_members().begin();
     910                for ( ; it != aggDecl->get_members().end(); ) {
     911                        std::list< Declaration * >::iterator next = it+1;
     912                        if ( dynamic_cast< TypedefDecl * >( *it ) ) {
     913                                delete *it;
     914                                aggDecl->get_members().erase( it );
     915                        } // if
     916                        it = next;
     917                }
     918                return aggDecl;
     919        }
     920
     921        Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) {
     922                Mutator::mutate( structDecl );
     923                return handleAggregate( structDecl );
     924        }
     925
     926        Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) {
     927                Mutator::mutate( unionDecl );
     928                return handleAggregate( unionDecl );
     929        }
     930
     931        Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) {
     932                Mutator::mutate( enumDecl );
     933                return handleAggregate( enumDecl );
     934        }
     935
     936                Declaration *EliminateTypedef::mutate( ContextDecl * contextDecl ) {
     937                Mutator::mutate( contextDecl );
     938                return handleAggregate( contextDecl );
     939        }
     940
    910941} // namespace SymTab
    911942
Note: See TracChangeset for help on using the changeset viewer.