Changeset 4e24610 for src/InitTweak


Ignore:
Timestamp:
May 6, 2016, 1:56:08 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:
d029162
Parents:
711eee5
Message:

Add constructor attribute to global initializer function, don't try to fix const globals, add Constructor/Destructor? attributes to FunctionDecl?

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/FixGlobalInit.cc

    r711eee5 r4e24610  
    1010// Created On       : Mon May 04 15:14:56 2016
    1111// Last Modified By : Rob Schluntz
    12 // Last Modified On : Wed May 04 16:53:12 2016
     12// Last Modified On : Fri May 06 13:51:00 2016
    1313// Update Count     : 2
    1414//
     
    3030        class GlobalFixer : public Visitor {
    3131          public:
    32                 GlobalFixer( const std::string & fileName );
     32                GlobalFixer();
    3333
    3434                virtual void visit( ObjectDecl *objDecl );
    3535                virtual void visit( FunctionDecl *functionDecl );
     36                virtual void visit( StructDecl *aggregateDecl );
     37                virtual void visit( UnionDecl *aggregateDecl );
     38                virtual void visit( EnumDecl *aggregateDecl );
     39                virtual void visit( TraitDecl *aggregateDecl );
     40                virtual void visit( TypeDecl *typeDecl );
    3641
    3742                UniqueName tempNamer;
    38                 FunctionDecl * initFunction;
     43                CompoundStmt * block;
    3944        };
    4045
     
    8388
    8489        void fixGlobalInit( std::list< Declaration * > & translationUnit, const std::string & name ) {
    85                 GlobalFixer fixer( name );
     90                GlobalFixer fixer;
    8691                acceptAll( translationUnit, fixer );
    87                 translationUnit.push_back( fixer.initFunction );
     92                // attribute only appears on the forward declaration, so need to make two function decls:
     93                // one with the body, one with the attribute.
     94                FunctionDecl * initFunction = new FunctionDecl( initName( name ), DeclarationNode::NoStorageClass, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), 0, false, false );
     95                FunctionDecl * forward = initFunction->clone();
     96                forward->set_attribute( FunctionDecl::Constructor );
     97                initFunction->set_statements( fixer.block );
     98                translationUnit.push_back( forward );
     99                translationUnit.push_back( initFunction );
    88100        }
    89101
     
    97109  }
    98110
    99         GlobalFixer::GlobalFixer( const std::string & fileName ) : tempNamer( "_global_init" ) {
    100                 initFunction = new FunctionDecl( initName( fileName ), DeclarationNode::NoStorageClass, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false );
     111        GlobalFixer::GlobalFixer() : tempNamer( "_global_init" ), block( new CompoundStmt( noLabels ) ) {
    101112        }
    102113
    103114        void GlobalFixer::visit( ObjectDecl *objDecl ) {
    104                 std::list< Statement * > & statements = initFunction->get_statements()->get_kids();
     115                std::list< Statement * > & statements = block->get_kids();
    105116
     117                if ( objDecl->get_init() == NULL ) return;
     118                if ( objDecl->get_type()->get_isConst() ) return; // temporary: can't assign to a const variable
    106119                // C allows you to initialize objects with constant expressions
    107                 if ( isConstExpr( objDecl->get_init() ) ) return;
     120                // xxx - this is an optimization. Need to first resolve constructors before we decide
     121                // to keep C-style initializer.
     122                // if ( isConstExpr( objDecl->get_init() ) ) return;
    108123
    109124                // steal initializer from object and attach it to a new temporary
     
    121136        }
    122137
    123         void GlobalFixer::visit( FunctionDecl *functionDecl ) {
    124                 // only modify global variables
    125         }
     138        // only modify global variables
     139        void GlobalFixer::visit( FunctionDecl *functionDecl ) {}
     140        void GlobalFixer::visit( StructDecl *aggregateDecl ) {}
     141        void GlobalFixer::visit( UnionDecl *aggregateDecl ) {}
     142        void GlobalFixer::visit( EnumDecl *aggregateDecl ) {}
     143        void GlobalFixer::visit( TraitDecl *aggregateDecl ) {}
     144        void GlobalFixer::visit( TypeDecl *typeDecl ) {}
     145
    126146} // namespace InitTweak
    127147
Note: See TracChangeset for help on using the changeset viewer.