Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Validate.cc

    r85c4ef0 r82dd287  
    1010// Created On       : Sun May 17 21:50:04 2015
    1111// Last Modified By : Rob Schluntz
    12 // Last Modified On : Mon Jul 13 14:38:19 2015
    13 // Update Count     : 184
     12// Last Modified On : Tue Jul 07 10:41:23 2015
     13// Update Count     : 136
    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 #include "TypeEquality.h"
     55
    5656
    5757#define debugPrint( x ) if ( doDebug ) { std::cout << x; }
     
    6060        class HoistStruct : public Visitor {
    6161          public:
     62                /// Flattens nested struct types
    6263                static void hoistStruct( std::list< Declaration * > &translationUnit );
    6364 
     
    8485        };
    8586
     87        /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers
    8688        class Pass1 : public Visitor {
    8789                typedef Visitor Parent;
     
    8991                virtual void visit( FunctionType *func );
    9092        };
    91  
     93
     94        /// Associates forward declarations of aggregates with their definitions
    9295        class Pass2 : public Indexer {
    9396                typedef Indexer Parent;
     
    110113        };
    111114
     115        /// Replaces array and function types in forall lists by appropriate pointer type
    112116        class Pass3 : public Indexer {
    113117                typedef Indexer Parent;
     
    123127        class AddStructAssignment : public Visitor {
    124128          public:
     129                /// Generates assignment operators for aggregate types as required
    125130                static void addStructAssignment( std::list< Declaration * > &translationUnit );
    126131
     
    157162        class EliminateTypedef : public Mutator {
    158163          public:
    159           EliminateTypedef() : scopeLevel( 0 ) {}
    160164                static void eliminateTypedef( std::list< Declaration * > &translationUnit );
    161165          private:
     
    167171                virtual Type *mutate( TypeInstType *aggregateUseType );
    168172                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 );
    177 
    178                 typedef std::map< std::string, std::pair< TypedefDecl *, int > > TypedefMap;
    179                 TypedefMap typedefNames;
    180                 int scopeLevel;
     173 
     174                std::map< std::string, TypedefDecl * > typedefNames;
    181175        };
    182176
     
    444438        }
    445439
     440        /// Fix up assertions
    446441        void forallFixer( Type *func ) {
    447                 // Fix up assertions
    448442                for ( std::list< TypeDecl * >::iterator type = func->get_forall().begin(); type != func->get_forall().end(); ++type ) {
    449443                        std::list< DeclarationWithType * > toBeDone, nextRound;
     
    595589                assignDecl2->fixUniqueId();
    596590
    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.
    600591                std::list< Declaration * > assigns;
    601592                assigns.push_back( assignDecl );
     
    604595                LibCfa::makeLibCfa( assigns );
    605596
    606                 // need to remove the prototypes, since this may be nested in a routine
     597                // need to remove the prototypes, since these can appear nested in a routine
    607598                for (int start = 0, end = assigns.size()/2; start < end; start++) {
    608599                        delete assigns.front();
     
    611602
    612603                declsToAdd.insert( declsToAdd.begin(), assigns.begin(), assigns.end() );
     604
     605                // return assignDecl;
    613606        }
    614607
     
    808801        }
    809802
    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() );
     803        Type *EliminateTypedef::mutate( TypeInstType *typeInst ) {
     804                std::map< std::string, TypedefDecl * >::const_iterator def = typedefNames.find( typeInst->get_name() );
    814805                if ( def != typedefNames.end() ) {
    815                         Type *ret = def->second.first->get_base()->clone();
     806                        Type *ret = def->second->get_base()->clone();
    816807                        ret->get_qualifiers() += typeInst->get_qualifiers();
    817808                        delete typeInst;
     
    821812        }
    822813
    823         Declaration *EliminateTypedef::mutate( TypedefDecl * tyDecl ) {
     814        Declaration *EliminateTypedef::mutate( TypedefDecl *tyDecl ) {
    824815                Declaration *ret = Mutator::mutate( 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 
     816                typedefNames[ tyDecl->get_name() ] = tyDecl;
    838817                // When a typedef is a forward declaration:
    839818                //    typedef struct screen SCREEN;
     
    853832        }
    854833
    855         TypeDecl *EliminateTypedef::mutate( TypeDecl * typeDecl ) {
    856                 TypedefMap::iterator i = typedefNames.find( typeDecl->get_name() );
     834        TypeDecl *EliminateTypedef::mutate( TypeDecl *typeDecl ) {
     835                std::map< std::string, TypedefDecl * >::iterator i = typedefNames.find( typeDecl->get_name() );
    857836                if ( i != typedefNames.end() ) {
    858837                        typedefNames.erase( i ) ;
     
    861840        }
    862841
    863         DeclarationWithType *EliminateTypedef::mutate( FunctionDecl * funcDecl ) {
    864                 TypedefMap oldNames = typedefNames;
     842        DeclarationWithType *EliminateTypedef::mutate( FunctionDecl *funcDecl ) {
     843                std::map< std::string, TypedefDecl * > oldNames = typedefNames;
    865844                DeclarationWithType *ret = Mutator::mutate( funcDecl );
    866845                typedefNames = oldNames;
     
    868847        }
    869848
    870         ObjectDecl *EliminateTypedef::mutate( ObjectDecl * objDecl ) {
    871                 TypedefMap oldNames = typedefNames;
     849        ObjectDecl *EliminateTypedef::mutate( ObjectDecl *objDecl ) {
     850                std::map< std::string, TypedefDecl * > oldNames = typedefNames;
    872851                ObjectDecl *ret = Mutator::mutate( objDecl );
    873852                typedefNames = oldNames;
     
    875854        }
    876855
    877         Expression *EliminateTypedef::mutate( CastExpr * castExpr ) {
    878                 TypedefMap oldNames = typedefNames;
     856        Expression *EliminateTypedef::mutate( CastExpr *castExpr ) {
     857                std::map< std::string, TypedefDecl * > oldNames = typedefNames;
    879858                Expression *ret = Mutator::mutate( castExpr );
    880859                typedefNames = oldNames;
     
    882861        }
    883862
    884         CompoundStmt *EliminateTypedef::mutate( CompoundStmt * compoundStmt ) {
    885                 TypedefMap oldNames = typedefNames;
    886                 scopeLevel += 1;
     863        CompoundStmt *EliminateTypedef::mutate( CompoundStmt *compoundStmt ) {
     864                std::map< std::string, TypedefDecl * > oldNames = typedefNames;
    887865                CompoundStmt *ret = Mutator::mutate( compoundStmt );
    888                 scopeLevel -= 1;
    889866                std::list< Statement * >::iterator i = compoundStmt->get_kids().begin();
    890867                while ( i != compoundStmt->get_kids().end() ) {
    891                         std::list< Statement * >::iterator next = i+1;
     868                        std::list< Statement * >::iterator next = i;
     869                        ++next;
    892870                        if ( DeclStmt *declStmt = dynamic_cast< DeclStmt * >( *i ) ) {
    893871                                if ( dynamic_cast< TypedefDecl * >( declStmt->get_decl() ) ) {
     
    901879                return ret;
    902880        }
    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 
    941881} // namespace SymTab
    942882
Note: See TracChangeset for help on using the changeset viewer.