Changeset 22bc276


Ignore:
Timestamp:
Sep 21, 2017, 3:23:33 PM (7 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, deferred_resn, demangler, enum, forall-pointer-decay, jacob/cs343-translation, jenkins-sandbox, master, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
05807e9, 29bc63e
Parents:
e4d6335
git-author:
Rob Schluntz <rschlunt@…> (09/21/17 15:19:47)
git-committer:
Rob Schluntz <rschlunt@…> (09/21/17 15:23:33)
Message:

Convert HoistArrayDimension? to PassVisitor?, cleanup in InitTweak? and CodeGen?

Location:
src
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/CodeGen/CodeGenerator.cc

    re4d6335 r22bc276  
    443443        void CodeGenerator::postvisit( UntypedExpr * untypedExpr ) {
    444444                extension( untypedExpr );
    445                 if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->get_function() ) ) {
     445                if ( NameExpr * nameExpr = dynamic_cast< NameExpr* >( untypedExpr->function ) ) {
    446446                        OperatorInfo opInfo;
    447                         if ( operatorLookup( nameExpr->get_name(), opInfo ) ) {
    448                                 std::list< Expression* >::iterator arg = untypedExpr->get_args().begin();
     447                        if ( operatorLookup( nameExpr->name, opInfo ) ) {
     448                                std::list< Expression* >::iterator arg = untypedExpr->args.begin();
    449449                                switch ( opInfo.type ) {
    450450                                  case OT_INDEX:
    451                                         assert( untypedExpr->get_args().size() == 2 );
     451                                        assert( untypedExpr->args.size() == 2 );
    452452                                        (*arg++)->accept( *visitor );
    453453                                        output << "[";
     
    461461                                  case OT_CTOR:
    462462                                  case OT_DTOR:
    463                                         if ( untypedExpr->get_args().size() == 1 ) {
     463                                        if ( untypedExpr->args.size() == 1 ) {
    464464                                                // the expression fed into a single parameter constructor or destructor may contain side
    465465                                                // effects, so must still output this expression
     
    480480                                                (*arg++)->accept( *visitor );
    481481                                                output << opInfo.symbol << "{ ";
    482                                                 genCommaList( arg, untypedExpr->get_args().end() );
     482                                                genCommaList( arg, untypedExpr->args.end() );
    483483                                                output << "}) /* " << opInfo.inputName << " */";
    484484                                        } // if
     
    488488                                  case OT_PREFIXASSIGN:
    489489                                  case OT_LABELADDRESS:
    490                                         assert( untypedExpr->get_args().size() == 1 );
     490                                        assert( untypedExpr->args.size() == 1 );
    491491                                        output << "(";
    492492                                        output << opInfo.symbol;
     
    497497                                  case OT_POSTFIX:
    498498                                  case OT_POSTFIXASSIGN:
    499                                         assert( untypedExpr->get_args().size() == 1 );
     499                                        assert( untypedExpr->args.size() == 1 );
    500500                                        (*arg)->accept( *visitor );
    501501                                        output << opInfo.symbol;
     
    504504                                  case OT_INFIX:
    505505                                  case OT_INFIXASSIGN:
    506                                         assert( untypedExpr->get_args().size() == 2 );
     506                                        assert( untypedExpr->args.size() == 2 );
    507507                                        output << "(";
    508508                                        (*arg++)->accept( *visitor );
     
    517517                                } // switch
    518518                        } else {
    519                                 if ( nameExpr->get_name() == "..." ) { // case V1 ... V2 or case V1~V2
    520                                         assert( untypedExpr->get_args().size() == 2 );
    521                                         (*untypedExpr->get_args().begin())->accept( *visitor );
    522                                         output << " ... ";
    523                                         (*--untypedExpr->get_args().end())->accept( *visitor );
    524                                 } else {                                                                // builtin routines
    525                                         nameExpr->accept( *visitor );
    526                                         output << "(";
    527                                         genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
    528                                         output << ")";
    529                                 } // if
     519                                // builtin routines
     520                                nameExpr->accept( *visitor );
     521                                output << "(";
     522                                genCommaList( untypedExpr->args.begin(), untypedExpr->args.end() );
     523                                output << ")";
    530524                        } // if
    531525                } else {
    532                         untypedExpr->get_function()->accept( *visitor );
     526                        untypedExpr->function->accept( *visitor );
    533527                        output << "(";
    534                         genCommaList( untypedExpr->get_args().begin(), untypedExpr->get_args().end() );
     528                        genCommaList( untypedExpr->args.begin(), untypedExpr->args.end() );
    535529                        output << ")";
    536530                } // if
     
    538532
    539533        void CodeGenerator::postvisit( RangeExpr * rangeExpr ) {
    540                 rangeExpr->get_low()->accept( *visitor );
     534                rangeExpr->low->accept( *visitor );
    541535                output << " ... ";
    542                 rangeExpr->get_high()->accept( *visitor );
     536                rangeExpr->high->accept( *visitor );
    543537        }
    544538
  • src/InitTweak/GenInit.cc

    re4d6335 r22bc276  
    6262        };
    6363
    64         struct CtorDtor : public WithGuards, public WithShortCircuiting  {
     64        struct CtorDtor : public WithGuards, public WithShortCircuiting, public WithVisitorRef<CtorDtor>  {
    6565                /// create constructor and destructor statements for object declarations.
    6666                /// the actual call statements will be added in after the resolver has run
     
    7575                // that need to be constructed or destructed
    7676                void previsit( StructDecl *aggregateDecl );
    77                 void previsit( __attribute__((unused)) UnionDecl    * aggregateDecl ) { visit_children = false; }
    78                 void previsit( __attribute__((unused)) EnumDecl     * aggregateDecl ) { visit_children = false; }
    79                 void previsit( __attribute__((unused)) TraitDecl    * aggregateDecl ) { visit_children = false; }
    80                 void previsit( __attribute__((unused)) TypeDecl     * typeDecl )      { visit_children = false; }
    81                 void previsit( __attribute__((unused)) TypedefDecl  * typeDecl )      { visit_children = false; }
    82                 void previsit( __attribute__((unused)) FunctionType * funcType )      { visit_children = false; }
     77                void premutate( AggregateDecl * ) { visit_children = false; }
     78                void premutate( NamedTypeDecl * ) { visit_children = false; }
     79                void previsit( FunctionType * ) { visit_children = false; }
    8380
    8481                void previsit( CompoundStmt * compoundStmt );
     
    9693        };
    9794
    98         class HoistArrayDimension final : public GenPoly::DeclMutator {
    99           public:
    100                 typedef GenPoly::DeclMutator Parent;
    101 
     95        struct HoistArrayDimension final : public WithDeclsToAdd, public WithShortCircuiting, public WithGuards {
    10296                /// hoist dimension from array types in object declaration so that it uses a single
    10397                /// const variable of type size_t, so that side effecting array dimensions are only
     
    10599                static void hoistArrayDimension( std::list< Declaration * > & translationUnit );
    106100
    107           private:
    108                 using Parent::mutate;
    109 
    110                 virtual DeclarationWithType * mutate( ObjectDecl * objectDecl ) override;
    111                 virtual DeclarationWithType * mutate( FunctionDecl *functionDecl ) override;
     101                void premutate( ObjectDecl * objectDecl );
     102                DeclarationWithType * postmutate( ObjectDecl * objectDecl );
     103                void premutate( FunctionDecl *functionDecl );
    112104                // should not traverse into any of these declarations to find objects
    113105                // that need to be constructed or destructed
    114                 virtual Declaration* mutate( StructDecl *aggregateDecl ) override { return aggregateDecl; }
    115                 virtual Declaration* mutate( UnionDecl *aggregateDecl ) override { return aggregateDecl; }
    116                 virtual Declaration* mutate( EnumDecl *aggregateDecl ) override { return aggregateDecl; }
    117                 virtual Declaration* mutate( TraitDecl *aggregateDecl ) override { return aggregateDecl; }
    118                 virtual TypeDecl* mutate( TypeDecl *typeDecl ) override { return typeDecl; }
    119                 virtual Declaration* mutate( TypedefDecl *typeDecl ) override { return typeDecl; }
    120 
    121                 virtual Type* mutate( FunctionType *funcType ) override { return funcType; }
     106                void premutate( AggregateDecl * ) { visit_children = false; }
     107                void premutate( NamedTypeDecl * ) { visit_children = false; }
     108                void premutate( FunctionType * ) { visit_children = false; }
    122109
    123110                void hoist( Type * type );
     
    143130                // hands off if the function returns a reference - we don't want to allocate a temporary if a variable's address
    144131                // is being returned
    145                 if ( returnStmt->get_expr() && returnVals.size() == 1 && ! dynamic_cast< ReferenceType * >( returnVals.front()->get_type() ) ) {
     132                if ( returnStmt->get_expr() && returnVals.size() == 1 && tryConstruct( returnVals.front() ) ) {
    146133                        // explicitly construct the return value using the return expression and the retVal object
    147134                        assertf( returnVals.front()->get_name() != "", "Function %s has unnamed return value\n", funcName.c_str() );
     
    158145                GuardValue( funcName );
    159146
    160                 ftype = functionDecl->get_functionType();
    161                 funcName = functionDecl->get_name();
     147                ftype = functionDecl->type;
     148                funcName = functionDecl->name;
    162149        }
    163150
     
    165152        // which would be incorrect if it is a side-effecting computation.
    166153        void HoistArrayDimension::hoistArrayDimension( std::list< Declaration * > & translationUnit ) {
    167                 HoistArrayDimension hoister;
    168                 hoister.mutateDeclarationList( translationUnit );
    169         }
    170 
    171         DeclarationWithType * HoistArrayDimension::mutate( ObjectDecl * objectDecl ) {
     154                PassVisitor<HoistArrayDimension> hoister;
     155                mutateAll( translationUnit, hoister );
     156        }
     157
     158        void HoistArrayDimension::premutate( ObjectDecl * objectDecl ) {
     159                GuardValue( storageClasses );
    172160                storageClasses = objectDecl->get_storageClasses();
    173                 DeclarationWithType * temp = Parent::mutate( objectDecl );
     161        }
     162
     163        DeclarationWithType * HoistArrayDimension::postmutate( ObjectDecl * objectDecl ) {
    174164                hoist( objectDecl->get_type() );
    175                 return temp;
     165                return objectDecl;
    176166        }
    177167
     
    194184
    195185                        arrayType->set_dimension( new VariableExpr( arrayDimension ) );
    196                         addDeclaration( arrayDimension );
     186                        declsToAddBefore.push_back( arrayDimension );
    197187
    198188                        hoist( arrayType->get_base() );
     
    201191        }
    202192
    203         DeclarationWithType * HoistArrayDimension::mutate( FunctionDecl *functionDecl ) {
    204                 ValueGuard< bool > oldInFunc( inFunction );
    205                 inFunction = true;
    206                 DeclarationWithType * decl = Parent::mutate( functionDecl );
    207                 return decl;
     193        void HoistArrayDimension::premutate( FunctionDecl * ) {
     194                GuardValue( inFunction );
    208195        }
    209196
     
    214201
    215202        bool CtorDtor::isManaged( Type * type ) const {
    216                 // at least for now, references are never constructed
     203                // references are never constructed
    217204                if ( dynamic_cast< ReferenceType * >( type ) ) return false;
    218205                // need to clear and reset qualifiers when determining if a type is managed
     
    221208                if ( TupleType * tupleType = dynamic_cast< TupleType * > ( type ) ) {
    222209                        // tuple is also managed if any of its components are managed
    223                         if ( std::any_of( tupleType->get_types().begin(), tupleType->get_types().end(), [&](Type * type) { return isManaged( type ); }) ) {
     210                        if ( std::any_of( tupleType->types.begin(), tupleType->types.end(), [&](Type * type) { return isManaged( type ); }) ) {
    224211                                return true;
    225212                        }
     
    305292
    306293        void CtorDtor::previsit( FunctionDecl *functionDecl ) {
     294                visit_children = false;  // do not try and construct parameters or forall parameters
    307295                GuardValue( inFunction );
    308296                inFunction = true;
     
    318306                }
    319307
    320                 PassVisitor<CtorDtor> newCtorDtor;
    321                 newCtorDtor.pass = *this;
    322                 maybeAccept( functionDecl->get_statements(), newCtorDtor );
    323                 visit_children = false;  // do not try and construct parameters or forall parameters - must happen after maybeAccept
     308                maybeAccept( functionDecl->get_statements(), *visitor );
    324309        }
    325310
     
    340325        }
    341326
    342         void CtorDtor::previsit( __attribute__((unused)) CompoundStmt * compoundStmt ) {
     327        void CtorDtor::previsit( CompoundStmt * ) {
    343328                GuardScope( managedTypes );
    344329        }
  • src/InitTweak/InitTweak.cc

    re4d6335 r22bc276  
    1 #include <stddef.h>                // for NULL
    21#include <algorithm>               // for find, all_of
    32#include <cassert>                 // for assertf, assert, strict_dynamic_cast
     
    184183                        callExpr->get_args().splice( callExpr->get_args().end(), args );
    185184
    186                         *out++ = new IfStmt( noLabels, cond, new ExprStmt( noLabels, callExpr ), NULL );
     185                        *out++ = new IfStmt( noLabels, cond, new ExprStmt( noLabels, callExpr ), nullptr );
    187186
    188187                        UntypedExpr * increment = new UntypedExpr( new NameExpr( "++?" ) );
     
    250249        // To accomplish this, generate switch statement, consuming all of expander's elements
    251250        Statement * InitImpl::buildListInit( UntypedExpr * dst, std::list< Expression * > & indices ) {
    252                 if ( ! init ) return NULL;
     251                if ( ! init ) return nullptr;
    253252                CompoundStmt * block = new CompoundStmt( noLabels );
    254253                build( dst, indices.begin(), indices.end(), init, back_inserter( block->get_kids() ) );
    255254                if ( block->get_kids().empty() ) {
    256255                        delete block;
    257                         return NULL;
     256                        return nullptr;
    258257                } else {
    259                         init = NULL; // init was consumed in creating the list init
     258                        init = nullptr; // init was consumed in creating the list init
    260259                        return block;
    261260                }
    262261        }
    263262
    264         Statement * ExprImpl::buildListInit( __attribute((unused)) UntypedExpr * dst, __attribute((unused)) std::list< Expression * > & indices ) {
    265                 return NULL;
     263        Statement * ExprImpl::buildListInit( UntypedExpr *, std::list< Expression * > & ) {
     264                return nullptr;
    266265        }
    267266
     
    270269        }
    271270
    272         bool tryConstruct( ObjectDecl * objDecl ) {
     271        bool tryConstruct( DeclarationWithType * dwt ) {
     272                ObjectDecl * objDecl = dynamic_cast< ObjectDecl * >( dwt );
     273                if ( ! objDecl ) return false;
    273274                return ! LinkageSpec::isBuiltin( objDecl->get_linkage() ) &&
    274                         (objDecl->get_init() == NULL ||
    275                                 ( objDecl->get_init() != NULL && objDecl->get_init()->get_maybeConstructed() ))
    276                         && ! objDecl->get_storageClasses().is_extern;
     275                        (objDecl->get_init() == nullptr ||
     276                                ( objDecl->get_init() != nullptr && objDecl->get_init()->get_maybeConstructed() ))
     277                        && ! objDecl->get_storageClasses().is_extern
     278                        && ! dynamic_cast< ReferenceType * >( objDecl->type );
    277279        }
    278280
     
    314316                collectCtorDtorCalls( stmt, matches );
    315317                assert( matches.size() <= 1 );
    316                 return matches.size() == 1 ? matches.front() : NULL;
     318                return matches.size() == 1 ? matches.front() : nullptr;
    317319        }
    318320
     
    359361        ApplicationExpr * isIntrinsicCallExpr( Expression * expr ) {
    360362                ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr );
    361                 if ( ! appExpr ) return NULL;
     363                if ( ! appExpr ) return nullptr;
    362364                DeclarationWithType * function = getCalledFunction( appExpr->get_function() );
    363365                assertf( function, "getCalledFunction returned nullptr: %s", toString( appExpr->get_function() ).c_str() );
    364366                // check for Intrinsic only - don't want to remove all overridable ctor/dtors because autogenerated ctor/dtor
    365367                // will call all member dtors, and some members may have a user defined dtor.
    366                 return function->get_linkage() == LinkageSpec::Intrinsic ? appExpr : NULL;
     368                return function->get_linkage() == LinkageSpec::Intrinsic ? appExpr : nullptr;
    367369        }
    368370
     
    482484                        return refType->get_base();
    483485                } else {
    484                         return NULL;
     486                        return nullptr;
    485487                }
    486488        }
     
    488490        Type * isPointerType( Type * type ) {
    489491                if ( getPointerBase( type ) ) return type;
    490                 else return NULL;
     492                else return nullptr;
    491493        }
    492494
  • src/InitTweak/InitTweak.h

    re4d6335 r22bc276  
    3333        std::list< Expression * > makeInitList( Initializer * init );
    3434
    35         /// True if the resolver should try to construct objDecl
    36         bool tryConstruct( ObjectDecl * objDecl );
     35        /// True if the resolver should try to construct dwt
     36        bool tryConstruct( DeclarationWithType * dwt );
    3737
    3838        /// True if the Initializer contains designations
Note: See TracChangeset for help on using the changeset viewer.