Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/SymTab/Validate.cc

    rd24d4e1 rc6d2e93  
    106106
    107107        /// Fix return types so that every function returns exactly one value
    108         struct ReturnTypeFixer {
     108        class ReturnTypeFixer {
     109          public:
    109110                static void fix( std::list< Declaration * > &translationUnit );
    110111
     
    114115
    115116        /// Replaces enum types by int, and function or array types in function parameter and return lists by appropriate pointers.
    116         struct EnumAndPointerDecay {
    117                 void previsit( EnumDecl *aggregateDecl );
    118                 void previsit( FunctionType *func );
     117        class EnumAndPointerDecayPass final : public Visitor {
     118                typedef Visitor Parent;
     119                virtual void visit( EnumDecl *aggregateDecl );
     120                virtual void visit( FunctionType *func );
    119121        };
    120122
     
    124126          public:
    125127                LinkReferenceToTypes( bool doDebug, const Indexer *indexer );
     128          private:
    126129                using Parent::visit;
    127130                void visit( EnumInstType *enumInst ) final;
     
    133136                void visit( UnionDecl *unionDecl ) final;
    134137                void visit( TypeInstType *typeInst ) final;
    135           private:
     138
    136139                const Indexer *indexer;
    137140
     
    144147        };
    145148
    146         /// Replaces array and function types in forall lists by appropriate pointer type and assigns each Object and Function declaration a unique ID.
    147         class ForallPointerDecay final : public Indexer {
     149        /// Replaces array and function types in forall lists by appropriate pointer type
     150        class Pass3 final : public Indexer {
    148151                typedef Indexer Parent;
    149152          public:
    150153                using Parent::visit;
    151                 ForallPointerDecay( const Indexer *indexer );
    152 
     154                Pass3( const Indexer *indexer );
     155          private:
    153156                virtual void visit( ObjectDecl *object ) override;
    154157                virtual void visit( FunctionDecl *func ) override;
     
    157160        };
    158161
    159         struct ReturnChecker : public WithGuards {
     162        class ReturnChecker {
     163          public:
    160164                /// Checks that return statements return nothing if their return type is void
    161165                /// and return something if the return type is non-void.
    162166                static void checkFunctionReturns( std::list< Declaration * > & translationUnit );
    163 
     167          private:
    164168                void previsit( FunctionDecl * functionDecl );
     169                void postvisit( FunctionDecl * functionDecl );
    165170                void previsit( ReturnStmt * returnStmt );
    166171
    167172                typedef std::list< DeclarationWithType * > ReturnVals;
    168173                ReturnVals returnVals;
     174                std::stack< ReturnVals > returnValsStack;
    169175        };
    170176
     
    202208        };
    203209
    204         struct VerifyCtorDtorAssign {
     210        class VerifyCtorDtorAssign {
     211        public:
    205212                /// ensure that constructors, destructors, and assignment have at least one
    206213                /// parameter, the first of which must be a pointer, and that ctor/dtors have no
     
    212219
    213220        /// ensure that generic types have the correct number of type arguments
    214         struct ValidateGenericParameters {
     221        class ValidateGenericParameters {
     222        public:
    215223                void previsit( StructInstType * inst );
    216224                void previsit( UnionInstType * inst );
    217225        };
    218226
    219         struct ArrayLength {
     227        class ArrayLength {
     228        public:
    220229                /// for array types without an explicit length, compute the length and store it so that it
    221230                /// is known to the rest of the phases. For example,
     
    230239        };
    231240
    232         struct CompoundLiteral final : public WithDeclsToAdd, public WithVisitorRef<CompoundLiteral> {
     241        class CompoundLiteral final : public GenPoly::DeclMutator {
    233242                Type::StorageClasses storageClasses;
    234243
    235                 void premutate( ObjectDecl *objectDecl );
    236                 Expression * postmutate( CompoundLiteralExpr *compLitExpr );
     244                using GenPoly::DeclMutator::mutate;
     245                DeclarationWithType * mutate( ObjectDecl *objectDecl ) final;
     246                Expression *mutate( CompoundLiteralExpr *compLitExpr ) final;
    237247        };
    238248
    239249        void validate( std::list< Declaration * > &translationUnit, bool doDebug ) {
    240                 PassVisitor<EnumAndPointerDecay> epc;
     250                EnumAndPointerDecayPass epc;
    241251                LinkReferenceToTypes lrt( doDebug, 0 );
    242                 ForallPointerDecay fpd( 0 );
    243                 PassVisitor<CompoundLiteral> compoundliteral;
     252                Pass3 pass3( 0 );
     253                CompoundLiteral compoundliteral;
    244254                PassVisitor<ValidateGenericParameters> genericParams;
    245255
     
    252262                VerifyCtorDtorAssign::verify( translationUnit );  // must happen before autogen, because autogen examines existing ctor/dtors
    253263                Concurrency::applyKeywords( translationUnit );
    254                 autogenerateRoutines( translationUnit ); // moved up, used to be below compoundLiteral - currently needs EnumAndPointerDecay
     264                autogenerateRoutines( translationUnit ); // moved up, used to be below compoundLiteral - currently needs EnumAndPointerDecayPass
    255265                Concurrency::implementMutexFuncs( translationUnit );
    256266                Concurrency::implementThreadStarter( translationUnit );
    257267                ReturnChecker::checkFunctionReturns( translationUnit );
    258                 mutateAll( translationUnit, compoundliteral );
    259                 acceptAll( translationUnit, fpd );
     268                compoundliteral.mutateDeclarationList( translationUnit );
     269                acceptAll( translationUnit, pass3 );
    260270                ArrayLength::computeLength( translationUnit );
    261271        }
    262272
    263273        void validateType( Type *type, const Indexer *indexer ) {
    264                 PassVisitor<EnumAndPointerDecay> epc;
     274                EnumAndPointerDecayPass epc;
    265275                LinkReferenceToTypes lrt( false, indexer );
    266                 ForallPointerDecay fpd( indexer );
     276                Pass3 pass3( indexer );
    267277                type->accept( epc );
    268278                type->accept( lrt );
    269                 type->accept( fpd );
     279                type->accept( pass3 );
    270280        }
    271281
     
    346356        }
    347357
    348         void EnumAndPointerDecay::previsit( EnumDecl *enumDecl ) {
     358        void EnumAndPointerDecayPass::visit( EnumDecl *enumDecl ) {
    349359                // Set the type of each member of the enumeration to be EnumConstant
    350360                for ( std::list< Declaration * >::iterator i = enumDecl->get_members().begin(); i != enumDecl->get_members().end(); ++i ) {
     
    353363                        obj->set_type( new EnumInstType( Type::Qualifiers( Type::Const ), enumDecl->get_name() ) );
    354364                } // for
     365                Parent::visit( enumDecl );
    355366        }
    356367
     
    359370                void fixFunctionList( DWTList & dwts, FunctionType * func ) {
    360371                        // the only case in which "void" is valid is where it is the only one in the list; then it should be removed
    361                         // entirely. other fix ups are handled by the FixFunction class
     372                        // entirely other fix ups are handled by the FixFunction class
    362373                        typedef typename DWTList::iterator DWTIterator;
    363374                        DWTIterator begin( dwts.begin() ), end( dwts.end() );
     
    378389                                for ( ; i != end; ++i ) {
    379390                                        FixFunction fixer;
    380                                         *i = (*i)->acceptMutator( fixer );
     391                                        *i = (*i )->acceptMutator( fixer );
    381392                                        if ( fixer.get_isVoid() ) {
    382393                                                throw SemanticError( "invalid type void in function type ", func );
     
    387398        }
    388399
    389         void EnumAndPointerDecay::previsit( FunctionType *func ) {
     400        void EnumAndPointerDecayPass::visit( FunctionType *func ) {
    390401                // Fix up parameters and return types
    391402                fixFunctionList( func->get_parameters(), func );
    392403                fixFunctionList( func->get_returnVals(), func );
     404                Visitor::visit( func );
    393405        }
    394406
     
    537549        }
    538550
    539         ForallPointerDecay::ForallPointerDecay( const Indexer *other_indexer ) :  Indexer( false ) {
     551        Pass3::Pass3( const Indexer *other_indexer ) :  Indexer( false ) {
    540552                if ( other_indexer ) {
    541553                        indexer = other_indexer;
     
    575587        }
    576588
    577         void ForallPointerDecay::visit( ObjectDecl *object ) {
     589        void Pass3::visit( ObjectDecl *object ) {
    578590                forallFixer( object->get_type() );
    579591                if ( PointerType *pointer = dynamic_cast< PointerType * >( object->get_type() ) ) {
     
    584596        }
    585597
    586         void ForallPointerDecay::visit( FunctionDecl *func ) {
     598        void Pass3::visit( FunctionDecl *func ) {
    587599                forallFixer( func->get_type() );
    588600                Parent::visit( func );
     
    596608
    597609        void ReturnChecker::previsit( FunctionDecl * functionDecl ) {
    598                 GuardValue( returnVals );
     610                returnValsStack.push( returnVals );
    599611                returnVals = functionDecl->get_functionType()->get_returnVals();
     612        }
     613        void ReturnChecker::postvisit( FunctionDecl * functionDecl ) {
     614                returnVals = returnValsStack.top();
     615                returnValsStack.pop();
    600616        }
    601617
     
    876892        }
    877893
    878         void CompoundLiteral::premutate( ObjectDecl *objectDecl ) {
     894        DeclarationWithType * CompoundLiteral::mutate( ObjectDecl *objectDecl ) {
    879895                storageClasses = objectDecl->get_storageClasses();
    880         }
    881 
    882         Expression *CompoundLiteral::postmutate( CompoundLiteralExpr *compLitExpr ) {
     896                DeclarationWithType * temp = Mutator::mutate( objectDecl );
     897                return temp;
     898        }
     899
     900        Expression *CompoundLiteral::mutate( CompoundLiteralExpr *compLitExpr ) {
    883901                // transform [storage_class] ... (struct S){ 3, ... };
    884902                // into [storage_class] struct S temp =  { 3, ... };
    885903                static UniqueName indexName( "_compLit" );
    886904
    887                 ObjectDecl *tempvar = new ObjectDecl( indexName.newName(), storageClasses, LinkageSpec::C, nullptr, compLitExpr->get_result(), compLitExpr->get_initializer() );
    888                 compLitExpr->set_result( nullptr );
    889                 compLitExpr->set_initializer( nullptr );
     905                ObjectDecl *tempvar = new ObjectDecl( indexName.newName(), storageClasses, LinkageSpec::C, 0, compLitExpr->get_result(), compLitExpr->get_initializer() );
     906                compLitExpr->set_result( 0 );
     907                compLitExpr->set_initializer( 0 );
    890908                delete compLitExpr;
    891                 declsToAddBefore.push_back( tempvar );                                  // add modified temporary to current block
    892                 return new VariableExpr( tempvar );
     909                DeclarationWithType * newtempvar = mutate( tempvar );
     910                addDeclaration( newtempvar );                                   // add modified temporary to current block
     911                return new VariableExpr( newtempvar );
    893912        }
    894913
Note: See TracChangeset for help on using the changeset viewer.