Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Validate.cc

    r45161b4d r61a4875  
    1010// Created On       : Sun May 17 21:50:04 2015
    1111// Last Modified By : Peter A. Buhr
    12 // Last Modified On : Wed Apr 13 16:39:30 2016
    13 // Update Count     : 251
     12// Last Modified On : Wed Jan 27 22:03:12 2016
     13// Update Count     : 225
    1414//
    1515
     
    4040#include <list>
    4141#include <iterator>
    42 #include "Common/utility.h"
    43 #include "Common/UniqueName.h"
    4442#include "Validate.h"
    4543#include "SynTree/Visitor.h"
    4644#include "SynTree/Mutator.h"
    4745#include "SynTree/Type.h"
    48 #include "SynTree/Expression.h"
    4946#include "SynTree/Statement.h"
    5047#include "SynTree/TypeSubstitution.h"
     
    5249#include "FixFunction.h"
    5350// #include "ImplementationType.h"
    54 #include "GenPoly/DeclMutator.h"
     51#include "Common/utility.h"
     52#include "Common/UniqueName.h"
    5553#include "AddVisit.h"
    5654#include "MakeLibCfa.h"
     
    7270
    7371                virtual void visit( CompoundStmt *compoundStmt );
     72                virtual void visit( IfStmt *ifStmt );
     73                virtual void visit( WhileStmt *whileStmt );
     74                virtual void visit( ForStmt *forStmt );
    7475                virtual void visit( SwitchStmt *switchStmt );
    7576                virtual void visit( ChooseStmt *chooseStmt );
    76                 // virtual void visit( CaseStmt *caseStmt );
     77                virtual void visit( CaseStmt *caseStmt );
     78                virtual void visit( CatchStmt *catchStmt );
    7779          private:
    7880                HoistStruct();
     
    99101                virtual void visit( StructInstType *structInst );
    100102                virtual void visit( UnionInstType *unionInst );
    101                 virtual void visit( TraitInstType *contextInst );
     103                virtual void visit( ContextInstType *contextInst );
    102104                virtual void visit( StructDecl *structDecl );
    103105                virtual void visit( UnionDecl *unionDecl );
     
    135137                virtual void visit( UnionDecl *structDecl );
    136138                virtual void visit( TypeDecl *typeDecl );
    137                 virtual void visit( TraitDecl *ctxDecl );
     139                virtual void visit( ContextDecl *ctxDecl );
    138140                virtual void visit( FunctionDecl *functionDecl );
    139141
     
    142144
    143145                virtual void visit( CompoundStmt *compoundStmt );
     146                virtual void visit( IfStmt *ifStmt );
     147                virtual void visit( WhileStmt *whileStmt );
     148                virtual void visit( ForStmt *forStmt );
    144149                virtual void visit( SwitchStmt *switchStmt );
    145150                virtual void visit( ChooseStmt *chooseStmt );
    146                 // virtual void visit( CaseStmt *caseStmt );
     151                virtual void visit( CaseStmt *caseStmt );
     152                virtual void visit( CatchStmt *catchStmt );
    147153
    148154                AutogenerateRoutines() : functionNesting( 0 ) {}
     
    160166                /// and return something if the return type is non-void.
    161167                static void checkFunctionReturns( std::list< Declaration * > & translationUnit );
     168
    162169          private:
    163170                virtual void visit( FunctionDecl * functionDecl );
     
    185192                virtual Declaration *mutate( UnionDecl * unionDecl );
    186193                virtual Declaration *mutate( EnumDecl * enumDecl );
    187                 virtual Declaration *mutate( TraitDecl * contextDecl );
     194                virtual Declaration *mutate( ContextDecl * contextDecl );
    188195
    189196                template<typename AggDecl>
    190197                AggDecl *handleAggregate( AggDecl * aggDecl );
    191198
    192                 template<typename AggDecl>
    193                 void addImplicitTypedef( AggDecl * aggDecl );
    194                
    195199                typedef std::map< std::string, std::pair< TypedefDecl *, int > > TypedefMap;
    196200                TypedefMap typedefNames;
     
    198202        };
    199203
    200         class CompoundLiteral : public GenPoly::DeclMutator {
    201                 DeclarationNode::StorageClass storageclass = DeclarationNode::NoStorageClass;
    202 
    203                 virtual DeclarationWithType * mutate( ObjectDecl *objectDecl );
    204                 virtual Expression *mutate( CompoundLiteralExpr *compLitExpr );
    205         };
    206 
    207204        void validate( std::list< Declaration * > &translationUnit, bool doDebug ) {
    208205                Pass1 pass1;
    209206                Pass2 pass2( doDebug, 0 );
    210207                Pass3 pass3( 0 );
    211                 CompoundLiteral compoundliteral;
    212 
    213208                EliminateTypedef::eliminateTypedef( translationUnit );
    214209                HoistStruct::hoistStruct( translationUnit );
     
    216211                acceptAll( translationUnit, pass2 );
    217212                ReturnChecker::checkFunctionReturns( translationUnit );
    218                 mutateAll( translationUnit, compoundliteral );
    219213                AutogenerateRoutines::autogenerateRoutines( translationUnit );
    220214                acceptAll( translationUnit, pass3 );
     
    298292        }
    299293
     294        void HoistStruct::visit( IfStmt *ifStmt ) {
     295                addVisit( ifStmt, *this );
     296        }
     297
     298        void HoistStruct::visit( WhileStmt *whileStmt ) {
     299                addVisit( whileStmt, *this );
     300        }
     301
     302        void HoistStruct::visit( ForStmt *forStmt ) {
     303                addVisit( forStmt, *this );
     304        }
     305
    300306        void HoistStruct::visit( SwitchStmt *switchStmt ) {
    301307                addVisit( switchStmt, *this );
     
    306312        }
    307313
    308         // void HoistStruct::visit( CaseStmt *caseStmt ) {
    309         //      addVisit( caseStmt, *this );
    310         // }
     314        void HoistStruct::visit( CaseStmt *caseStmt ) {
     315                addVisit( caseStmt, *this );
     316        }
     317
     318        void HoistStruct::visit( CatchStmt *cathStmt ) {
     319                addVisit( cathStmt, *this );
     320        }
    311321
    312322        void Pass1::visit( EnumDecl *enumDecl ) {
     
    394404        }
    395405
    396         void Pass2::visit( TraitInstType *contextInst ) {
     406        void Pass2::visit( ContextInstType *contextInst ) {
    397407                Parent::visit( contextInst );
    398                 TraitDecl *ctx = indexer->lookupTrait( contextInst->get_name() );
     408                ContextDecl *ctx = indexer->lookupContext( contextInst->get_name() );
    399409                if ( ! ctx ) {
    400410                        throw SemanticError( "use of undeclared context " + contextInst->get_name() );
     
    402412                for ( std::list< TypeDecl * >::const_iterator i = ctx->get_parameters().begin(); i != ctx->get_parameters().end(); ++i ) {
    403413                        for ( std::list< DeclarationWithType * >::const_iterator assert = (*i )->get_assertions().begin(); assert != (*i )->get_assertions().end(); ++assert ) {
    404                                 if ( TraitInstType *otherCtx = dynamic_cast< TraitInstType * >(*assert ) ) {
     414                                if ( ContextInstType *otherCtx = dynamic_cast< ContextInstType * >(*assert ) ) {
    405415                                        cloneAll( otherCtx->get_members(), contextInst->get_members() );
    406416                                } else {
     
    466476                        while ( ! toBeDone.empty() ) {
    467477                                for ( std::list< DeclarationWithType * >::iterator assertion = toBeDone.begin(); assertion != toBeDone.end(); ++assertion ) {
    468                                         if ( TraitInstType *ctx = dynamic_cast< TraitInstType * >( (*assertion )->get_type() ) ) {
     478                                        if ( ContextInstType *ctx = dynamic_cast< ContextInstType * >( (*assertion )->get_type() ) ) {
    469479                                                for ( std::list< Declaration * >::const_iterator i = ctx->get_members().begin(); i != ctx->get_members().end(); ++i ) {
    470480                                                        DeclarationWithType *dwt = dynamic_cast< DeclarationWithType * >( *i );
     
    773783                makeUnionFieldsAssignment( srcParam, dstParam, cloneWithParams( refType, unionParams ), back_inserter( assignDecl->get_statements()->get_kids() ) );
    774784                if ( isGeneric ) makeUnionFieldsAssignment( srcParam, returnVal, cloneWithParams( refType, unionParams ), back_inserter( assignDecl->get_statements()->get_kids() ) );
    775                
     785
    776786                if ( ! isGeneric ) assignDecl->get_statements()->get_kids().push_back( new ReturnStmt( noLabels, new VariableExpr( srcParam ) ) );
    777787
     
    841851        }
    842852
    843         void AutogenerateRoutines::visit( TraitDecl *) {
     853        void AutogenerateRoutines::visit( ContextDecl *) {
    844854                // ensure that we don't add assignment ops for types defined as part of the context
    845855        }
     
    864874        }
    865875
     876        void AutogenerateRoutines::visit( IfStmt *ifStmt ) {
     877                visitStatement( ifStmt );
     878        }
     879
     880        void AutogenerateRoutines::visit( WhileStmt *whileStmt ) {
     881                visitStatement( whileStmt );
     882        }
     883
     884        void AutogenerateRoutines::visit( ForStmt *forStmt ) {
     885                visitStatement( forStmt );
     886        }
     887
    866888        void AutogenerateRoutines::visit( SwitchStmt *switchStmt ) {
    867889                visitStatement( switchStmt );
     
    872894        }
    873895
    874         // void AutogenerateRoutines::visit( CaseStmt *caseStmt ) {
    875         //      visitStatement( caseStmt );
    876         // }
     896        void AutogenerateRoutines::visit( CaseStmt *caseStmt ) {
     897                visitStatement( caseStmt );
     898        }
     899
     900        void AutogenerateRoutines::visit( CatchStmt *cathStmt ) {
     901                visitStatement( cathStmt );
     902        }
    877903
    878904        void ReturnChecker::checkFunctionReturns( std::list< Declaration * > & translationUnit ) {
     
    10171043        }
    10181044
    1019         // there may be typedefs nested within aggregates in order for everything to work properly, these should be removed
    1020         // as well
     1045        // there may be typedefs nested within aggregates
     1046        // in order for everything to work properly, these
     1047        // should be removed as well
    10211048        template<typename AggDecl>
    10221049        AggDecl *EliminateTypedef::handleAggregate( AggDecl * aggDecl ) {
     
    10321059                return aggDecl;
    10331060        }
    1034        
    1035         template<typename AggDecl>
    1036         void EliminateTypedef::addImplicitTypedef( AggDecl * aggDecl ) {
    1037                 if ( typedefNames.count( aggDecl->get_name() ) == 0 ) {
    1038                         Type *type;
    1039                         if ( StructDecl * newDeclStructDecl = dynamic_cast< StructDecl * >( aggDecl ) ) {
    1040                                 type = new StructInstType( Type::Qualifiers(), newDeclStructDecl->get_name() );
    1041                         } else if ( UnionDecl * newDeclUnionDecl = dynamic_cast< UnionDecl * >( aggDecl ) ) {
    1042                                 type = new UnionInstType( Type::Qualifiers(), newDeclUnionDecl->get_name() );
    1043                         } else if ( EnumDecl * newDeclEnumDecl = dynamic_cast< EnumDecl * >( aggDecl )  ) {
    1044                                 type = new EnumInstType( Type::Qualifiers(), newDeclEnumDecl->get_name() );
    1045                         } // if
    1046                         TypedefDecl * tyDecl = new TypedefDecl( aggDecl->get_name(), DeclarationNode::NoStorageClass, type );
    1047                         typedefNames[ aggDecl->get_name() ] = std::make_pair( tyDecl, scopeLevel );
    1048                 } // if
    1049         }
     1061
    10501062        Declaration *EliminateTypedef::mutate( StructDecl * structDecl ) {
    1051                 addImplicitTypedef( structDecl );
    10521063                Mutator::mutate( structDecl );
    10531064                return handleAggregate( structDecl );
     
    10551066
    10561067        Declaration *EliminateTypedef::mutate( UnionDecl * unionDecl ) {
    1057                 addImplicitTypedef( unionDecl );
    10581068                Mutator::mutate( unionDecl );
    10591069                return handleAggregate( unionDecl );
     
    10611071
    10621072        Declaration *EliminateTypedef::mutate( EnumDecl * enumDecl ) {
    1063                 addImplicitTypedef( enumDecl );
    10641073                Mutator::mutate( enumDecl );
    10651074                return handleAggregate( enumDecl );
    10661075        }
    10671076
    1068         Declaration *EliminateTypedef::mutate( TraitDecl * contextDecl ) {
     1077                Declaration *EliminateTypedef::mutate( ContextDecl * contextDecl ) {
    10691078                Mutator::mutate( contextDecl );
    10701079                return handleAggregate( contextDecl );
    10711080        }
    10721081
    1073         DeclarationWithType * CompoundLiteral::mutate( ObjectDecl *objectDecl ) {
    1074                 storageclass = objectDecl->get_storageClass();
    1075                 DeclarationWithType * temp = Mutator::mutate( objectDecl );
    1076                 storageclass = DeclarationNode::NoStorageClass;
    1077                 return temp;
    1078         }
    1079 
    1080         Expression *CompoundLiteral::mutate( CompoundLiteralExpr *compLitExpr ) {
    1081                 // transform [storage_class] ... (struct S){ 3, ... };
    1082                 // into [storage_class] struct S temp =  { 3, ... };
    1083                 static UniqueName indexName( "_compLit" );
    1084 
    1085                 ObjectDecl *tempvar = new ObjectDecl( indexName.newName(), storageclass, LinkageSpec::C, 0, compLitExpr->get_type(), compLitExpr->get_initializer() );
    1086                 compLitExpr->set_type( 0 );
    1087                 compLitExpr->set_initializer( 0 );
    1088                 delete compLitExpr;
    1089                 DeclarationWithType * newtempvar = mutate( tempvar );
    1090                 addDeclaration( newtempvar );                                   // add modified temporary to current block
    1091                 return new VariableExpr( newtempvar );
    1092         }
    10931082} // namespace SymTab
    10941083
Note: See TracChangeset for help on using the changeset viewer.