Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Validate.cc

    r82dd287 r85c4ef0  
    1010// Created On       : Sun May 17 21:50:04 2015
    1111// Last Modified By : Rob Schluntz
    12 // Last Modified On : Tue Jul 07 10:41:23 2015
    13 // Update Count     : 136
     12// Last Modified On : Mon Jul 13 14:38:19 2015
     13// Update Count     : 184
    1414//
    1515
     
    4848#include "Indexer.h"
    4949#include "FixFunction.h"
    50 #include "ImplementationType.h"
     50// #include "ImplementationType.h"
    5151#include "utility.h"
    5252#include "UniqueName.h"
    5353#include "AddVisit.h"
    5454#include "MakeLibCfa.h"
    55 
     55#include "TypeEquality.h"
    5656
    5757#define debugPrint( x ) if ( doDebug ) { std::cout << x; }
     
    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
     
    162157        class EliminateTypedef : public Mutator {
    163158          public:
     159          EliminateTypedef() : scopeLevel( 0 ) {}
    164160                static void eliminateTypedef( std::list< Declaration * > &translationUnit );
    165161          private:
     
    171167                virtual Type *mutate( TypeInstType *aggregateUseType );
    172168                virtual Expression *mutate( CastExpr *castExpr );
    173  
    174                 std::map< std::string, TypedefDecl * > typedefNames;
     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 );
     177
     178                typedef std::map< std::string, std::pair< TypedefDecl *, int > > TypedefMap;
     179                TypedefMap typedefNames;
     180                int scopeLevel;
    175181        };
    176182
     
    438444        }
    439445
    440         /// Fix up assertions
    441446        void forallFixer( Type *func ) {
     447                // Fix up assertions
    442448                for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) {
    443449                        std::list< DeclarationWithType * > toBeDone, nextRound;
     
    589595                assignDecl2->fixUniqueId();
    590596
     597                // these should be built in the same way that the prelude
     598                // functions are, so build a list containing the prototypes
     599                // and allow MakeLibCfa to autogenerate the bodies.
    591600                std::list< Declaration * > assigns;
    592601                assigns.push_back( assignDecl );
     
    595604                LibCfa::makeLibCfa( assigns );
    596605
    597                 // need to remove the prototypes, since these can appear nested in a routine
     606                // need to remove the prototypes, since this may be nested in a routine
    598607                for (int start = 0, end = assigns.size()/2; start < end; start++) {
    599608                        delete assigns.front();
     
    602611
    603612                declsToAdd.insert( declsToAdd.begin(), assigns.begin(), assigns.end() );
    604 
    605                 // return assignDecl;
    606613        }
    607614
     
    801808        }
    802809
    803         Type *EliminateTypedef::mutate( TypeInstType *typeInst ) {
    804                 std::map< std::string, TypedefDecl * >::const_iterator def = typedefNames.find( typeInst->get_name() );
     810        Type *EliminateTypedef::mutate( TypeInstType * typeInst ) {
     811                // instances of typedef types will come here. If it is an instance
     812                // of a typdef type, link the instance to its actual type.
     813                TypedefMap::const_iterator def = typedefNames.find( typeInst->get_name() );
    805814                if ( def != typedefNames.end() ) {
    806                         Type *ret = def->second->get_base()->clone();
     815                        Type *ret = def->second.first->get_base()->clone();
    807816                        ret->get_qualifiers() += typeInst->get_qualifiers();
    808817                        delete typeInst;
     
    812821        }
    813822
    814         Declaration *EliminateTypedef::mutate( TypedefDecl *tyDecl ) {
     823        Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) {
    815824                Declaration *ret = Mutator::mutate( tyDecl );
    816                 typedefNames[ tyDecl->get_name() ] = tyDecl;
     825                if ( typedefNames.count( tyDecl->get_name() ) == 1 && typedefNames[ tyDecl->get_name() ].second == scopeLevel ) {
     826                        // typedef to the same name from the same scope
     827                        // must be from the same type
     828
     829                        Type * t1 = tyDecl->get_base();
     830                        Type * t2 = typedefNames[ tyDecl->get_name() ].first->get_base();
     831                        if ( ! typeEquals( t1, t2, true ) ) {
     832                                throw SemanticError( "cannot redefine typedef: " + tyDecl->get_name() );
     833                        }
     834                } else {
     835                        typedefNames[ tyDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel );
     836                } // if
     837
    817838                // When a typedef is a forward declaration:
    818839                //    typedef struct screen SCREEN;
     
    832853        }
    833854
    834         TypeDecl *EliminateTypedef::mutate( TypeDecl *typeDecl ) {
    835                 std::map< std::string, TypedefDecl * >::iterator i = typedefNames.find( typeDecl->get_name() );
     855        TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) {
     856                TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() );
    836857                if ( i != typedefNames.end() ) {
    837858                        typedefNames.erase( i ) ;
     
    840861        }
    841862
    842         DeclarationWithType *EliminateTypedef::mutate( FunctionDecl *funcDecl ) {
    843                 std::map< std::string, TypedefDecl * > oldNames = typedefNames;
     863        DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) {
     864                TypedefMap oldNames = typedefNames;
    844865                DeclarationWithType *ret = Mutator::mutate( funcDecl );
    845866                typedefNames = oldNames;
     
    847868        }
    848869
    849         ObjectDecl *EliminateTypedef::mutate( ObjectDecl *objDecl ) {
    850                 std::map< std::string, TypedefDecl * > oldNames = typedefNames;
     870        ObjectDecl *EliminateTypedef::mutate( ObjectDecl * objDecl ) {
     871                TypedefMap oldNames = typedefNames;
    851872                ObjectDecl *ret = Mutator::mutate( objDecl );
    852873                typedefNames = oldNames;
     
    854875        }
    855876
    856         Expression *EliminateTypedef::mutate( CastExpr *castExpr ) {
    857                 std::map< std::string, TypedefDecl * > oldNames = typedefNames;
     877        Expression *EliminateTypedef::mutate( CastExpr * castExpr ) {
     878                TypedefMap oldNames = typedefNames;
    858879                Expression *ret = Mutator::mutate( castExpr );
    859880                typedefNames = oldNames;
     
    861882        }
    862883
    863         CompoundStmt *EliminateTypedef::mutate( CompoundStmt *compoundStmt ) {
    864                 std::map< std::string, TypedefDecl * > oldNames = typedefNames;
     884        CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) {
     885                TypedefMap oldNames = typedefNames;
     886                scopeLevel += 1;
    865887                CompoundStmt *ret = Mutator::mutate( compoundStmt );
     888                scopeLevel -= 1;
    866889                std::list< Statement * >::iterator i = compoundStmt->get_kids().begin();
    867890                while ( i != compoundStmt->get_kids().end() ) {
    868                         std::list< Statement * >::iterator next = i;
    869                         ++next;
     891                        std::list< Statement * >::iterator next = i+1;
    870892                        if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) {
    871893                                if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) {
     
    879901                return ret;
    880902        }
     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
    881941} // namespace SymTab
    882942
Note: See TracChangeset for help on using the changeset viewer.