Changeset 02c7d04


Ignore:
Timestamp:
Jan 11, 2016, 4:14:18 PM (8 years ago)
Author:
Rob Schluntz <rschlunt@…>
Branches:
ADT, aaron-thesis, arm-eh, ast-experimental, cleanup-dtors, ctor, deferred_resn, demangler, enum, forall-pointer-decay, gc_noraii, jacob/cs343-translation, jenkins-sandbox, master, memory, new-ast, new-ast-unique-expr, new-env, no_list, persistent-indexer, pthread-emulation, qualifiedEnum, resolv-new, with_gc
Children:
71f4e4f
Parents:
77acda06
Message:

inserts constructor and destructor statements for object declarations - to be resolved

Location:
src/InitTweak
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/RemoveInit.cc

    r77acda06 r02c7d04  
    1010// Created On       : Mon May 18 07:44:20 2015
    1111// Last Modified By : Rob Schluntz
    12 // Last Modified On : Mon Jan 11 14:41:26 2016
    13 // Update Count     : 118
     12// Last Modified On : Mon Jan 11 16:10:45 2016
     13// Update Count     : 150
    1414//
    1515
     
    3232        class RemoveInit : public GenPoly::PolyMutator {
    3333          public:
     34                /// removes and replaces initialization for polymorphic value objects
     35                /// with assignment (TODO: constructor) statements.
     36                /// also consistently allocates a temporary variable for the return value
     37                /// of a function so that anything which the resolver decides can be assigned
     38                /// into the return type of a function can be returned.
     39                static void removeInitializers( std::list< Declaration * > &translationUnit );
     40
    3441                RemoveInit();
    3542                virtual ObjectDecl * mutate( ObjectDecl *objDecl );
     
    4653        class CtorDtor : public GenPoly::PolyMutator {
    4754          public:
    48                 // CtorDtor();
     55                /// create constructor and destructor statements for object declarations.
     56                /// Destructors are inserted directly into the code, whereas constructors
     57                /// will be added in after the resolver has run so that the initializer expression
     58                /// is only removed if a constructor is found
     59                static void generateCtorDtor( std::list< Declaration * > &translationUnit );
    4960
    5061                virtual ObjectDecl * mutate( ObjectDecl * );
     62                virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
     63                virtual Declaration* mutate( StructDecl *aggregateDecl );
     64                virtual Declaration* mutate( UnionDecl *aggregateDecl );
     65                virtual Declaration* mutate( EnumDecl *aggregateDecl );
     66                virtual Declaration* mutate( ContextDecl *aggregateDecl );
     67                virtual TypeDecl* mutate( TypeDecl *typeDecl );
     68                virtual Declaration* mutate( TypedefDecl *typeDecl );
    5169
    5270                virtual CompoundStmt * mutate( CompoundStmt * compoundStmt );
    5371
    5472          protected:
    55                 typedef std::map< std::string, DeclarationWithType * > MMMMAP;
    56                 std::stack< MMMMAP > constructedObjects;
    5773
    5874                // to be added before block ends - use push_front so order is correct
     
    6076        };
    6177
    62         void tweak( std::list< Declaration * > translationUnit ) {
     78        void tweak( std::list< Declaration * > & translationUnit ) {
     79                RemoveInit::removeInitializers( translationUnit );
     80                CtorDtor::generateCtorDtor( translationUnit );
     81        }
     82
     83        void RemoveInit::removeInitializers( std::list< Declaration * > & translationUnit ) {
    6384                RemoveInit remover;
    64                 CtorDtor ctordtor;
    6585                mutateAll( translationUnit, remover );
    66                 mutateAll( translationUnit, ctordtor );
    6786        }
    6887
     
    115134        }
    116135
    117         bool tryConstruct( ObjectDecl * objDecl ) {
    118                 // xxx - handle designations
    119                 return objDecl->get_init() == NULL ||
    120                         ( objDecl->get_init() != NULL && objDecl->get_init()->get_maybeConstructed() );
    121         }
    122 
    123         ExprStmt * makeCtorDtorStmt( std::string name, ObjectDecl * objDecl, std::list< Expression * > args ) {
    124                 UntypedExpr * expr = new UntypedExpr( new NameExpr( name ) );
    125                 expr->get_args().push_back( new VariableExpr( objDecl ) );
    126                 expr->get_args().splice( expr->get_args().end(), args );
    127                 return new ExprStmt( noLabels, expr );
    128         }
    129 
    130         // InitExpander ctor/dtor being marked as weak symbols
    131         // this is causing a bug - Rodolfo's InitExpander is being constructed and destructed
    132         // with different fields, which causes a segfault
    133 
    134         class InitExpander : public Visitor {
    135           public:
    136           InitExpander() {}
    137           // ~InitExpander() {}
    138                 virtual void visit( SingleInit * singleInit );
    139                 virtual void visit( ListInit * listInit );
    140                 std::list< Expression * > argList;
    141         };
    142 
    143         void InitExpander::visit( SingleInit * singleInit ) {
    144                 argList.push_back( singleInit->get_value()->clone() );
    145         }
    146 
    147         void InitExpander::visit( ListInit * listInit ) {
    148                 // xxx - for now, assume no nested list inits
    149                 std::list<Initializer*>::iterator it = listInit->begin_initializers();
    150                 for ( ; it != listInit->end_initializers(); ++it ) {
    151                         (*it)->accept( *this );
    152                 }
    153         }
    154 
    155         std::list< Expression * > makeInitList( Initializer * init ) {
    156                 if ( init ) {
     136
     137        void CtorDtor::generateCtorDtor( std::list< Declaration * > & translationUnit ) {
     138                CtorDtor ctordtor;
     139                mutateAll( translationUnit, ctordtor );
     140        }
     141
     142        namespace {
     143                bool tryConstruct( ObjectDecl * objDecl ) {
     144                        // xxx - handle designations
     145                        return ! LinkageSpec::isBuiltin( objDecl->get_linkage() ) &&
     146                                (objDecl->get_init() == NULL ||
     147                                ( objDecl->get_init() != NULL && objDecl->get_init()->get_maybeConstructed() ));
     148                }
     149
     150                ExprStmt * makeCtorDtorStmt( std::string name, ObjectDecl * objDecl, std::list< Expression * > args ) {
     151                        UntypedExpr * expr = new UntypedExpr( new NameExpr( name ) );
     152                        expr->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
     153                        expr->get_args().splice( expr->get_args().end(), args );
     154                        return new ExprStmt( noLabels, expr );
     155                }
     156
     157                class InitExpander : public Visitor {
     158                  public:
     159                  InitExpander() {}
     160                  // ~InitExpander() {}
     161                        virtual void visit( SingleInit * singleInit );
     162                        virtual void visit( ListInit * listInit );
     163                        std::list< Expression * > argList;
     164                };
     165
     166                void InitExpander::visit( SingleInit * singleInit ) {
     167                        argList.push_back( singleInit->get_value()->clone() );
     168                }
     169
     170                void InitExpander::visit( ListInit * listInit ) {
     171                        // xxx - for now, assume no nested list inits
     172                        std::list<Initializer*>::iterator it = listInit->begin_initializers();
     173                        for ( ; it != listInit->end_initializers(); ++it ) {
     174                                (*it)->accept( *this );
     175                        }
     176                }
     177
     178                std::list< Expression * > makeInitList( Initializer * init ) {
    157179                        InitExpander expander;
    158                         // init->accept( expander );
    159                         // std::list< Expression * > l = expander.argList;
    160                         std::list< Expression * > l;
    161                         return l;
    162                 } else {
    163                         std::list< Expression * > l;
    164                         return l;
     180                        maybeAccept( init, expander );
     181                        return expander.argList;
    165182                }
    166183        }
     
    179196                }
    180197                return objDecl;
     198        }
     199
     200        DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) {
     201                // parameters should not be constructed and destructed, so don't mutate FunctionType
     202                mutateAll( functionDecl->get_oldDecls(), *this );
     203                functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );
     204                return functionDecl;
    181205        }
    182206
     
    190214        }
    191215
    192 
     216        // should not traverse into any of these declarations to find objects
     217        // that need to be constructed or destructed
     218        Declaration* CtorDtor::mutate( StructDecl *aggregateDecl ) { return aggregateDecl; }
     219        Declaration* CtorDtor::mutate( UnionDecl *aggregateDecl ) { return aggregateDecl; }
     220        Declaration* CtorDtor::mutate( EnumDecl *aggregateDecl ) { return aggregateDecl; }
     221        Declaration* CtorDtor::mutate( ContextDecl *aggregateDecl ) { return aggregateDecl; }
     222        TypeDecl* CtorDtor::mutate( TypeDecl *typeDecl ) { return typeDecl; }
     223        Declaration* CtorDtor::mutate( TypedefDecl *typeDecl ) { return typeDecl; }
    193224
    194225} // namespace InitTweak
  • src/InitTweak/RemoveInit.h

    r77acda06 r02c7d04  
    55// file "LICENCE" distributed with Cforall.
    66//
    7 // RemoveInit.h -- 
     7// RemoveInit.h --
    88//
    99// Author           : Rodolfo G. Esteves
    1010// Created On       : Mon May 18 07:44:20 2015
    11 // Last Modified By : Peter A. Buhr
    12 // Last Modified On : Fri Nov 27 17:00:47 2015
    13 // Update Count     : 2
     11// Last Modified By : Rob Schluntz
     12// Last Modified On : Mon Jan 11 16:02:44 2016
     13// Update Count     : 3
    1414//
    1515
     
    2626namespace InitTweak {
    2727        /// Adds assignment statements for polymorphic type initializers
    28         void tweak( std::list< Declaration * > translationUnit );
    29 } // namespace 
     28        void tweak( std::list< Declaration * > & translationUnit );
     29} // namespace
    3030
    3131#endif // GENPOLY_POLYMUTATOR_H
Note: See TracChangeset for help on using the changeset viewer.