Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/FixGlobalInit.cc

    rec79847 r03e5d14  
    1010// Created On       : Mon May 04 15:14:56 2016
    1111// Last Modified By : Rob Schluntz
    12 // Last Modified On : Mon May 09 11:44:29 2016
     12// Last Modified On : Fri May 06 15:40:48 2016
    1313// Update Count     : 2
    1414//
    1515
    1616#include "FixGlobalInit.h"
    17 #include "GenInit.h"
    1817#include "SynTree/Declaration.h"
    1918#include "SynTree/Type.h"
     
    4342                UniqueName tempNamer;
    4443                FunctionDecl * initFunction;
    45                 FunctionDecl * destroyFunction;
    4644        };
    4745
     
    9290                GlobalFixer fixer( name, inLibrary );
    9391                acceptAll( translationUnit, fixer );
    94                 // don't need to include function if it's empty
    95                 if ( fixer.initFunction->get_statements()->get_kids().empty() ) {
    96                         delete fixer.initFunction;
    97                 } else {
    98                         translationUnit.push_back( fixer.initFunction );
    99                 }
    100                 if ( fixer.destroyFunction->get_statements()->get_kids().empty() ) {
    101                         delete fixer.destroyFunction;
    102                 } else {
    103                         translationUnit.push_back( fixer.destroyFunction );
    104                 }
     92                translationUnit.push_back( fixer.initFunction );
    10593        }
    10694
    107   std::string globalFunctionName( const std::string & name ) {
     95  std::string initName( const std::string & name ) {
    10896        // get basename
    10997        std::string ret = name.substr( 0, name.find( '.' ) );
     
    11199                static std::string invalid = "/-";
    112100        replace_if( ret.begin(), ret.end(), []( char c ) { return invalid.find(c) != std::string::npos; }, '_' );
    113         return ret;
     101        return "_init_" + ret;
    114102  }
    115103
    116104        GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) {
    117                 std::string fixedName = globalFunctionName( name );
    118                 initFunction = new FunctionDecl( "_init_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false, FunctionDecl::Attribute( FunctionDecl::Attribute::Constructor, inLibrary ? FunctionDecl::Attribute::High : FunctionDecl::Attribute::Default ) );
    119 
    120                 destroyFunction = new FunctionDecl( "_destroy_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false, FunctionDecl::Attribute( FunctionDecl::Attribute::Destructor, inLibrary ? FunctionDecl::Attribute::High : FunctionDecl::Attribute::Default ) );
     105                initFunction = new FunctionDecl( initName( name ), DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false, FunctionDecl::Attribute( FunctionDecl::Attribute::Constructor, inLibrary ? FunctionDecl::Attribute::High : FunctionDecl::Attribute::Default ) );
    121106        }
    122107
    123108        void GlobalFixer::visit( ObjectDecl *objDecl ) {
    124                 std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids();
    125                 std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids();
     109                std::list< Statement * > & statements = initFunction->get_statements()->get_kids();
    126110
    127111                if ( objDecl->get_init() == NULL ) return;
    128                 if ( ! tryConstruct( objDecl ) ) return; // don't construct @= or designated objects
    129112                if ( objDecl->get_type()->get_isConst() ) return; // temporary: can't assign to a const variable
    130113                // C allows you to initialize objects with constant expressions
     
    136119                ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, objDecl->get_type()->clone(), objDecl->get_init() );
    137120                objDecl->set_init( NULL );
    138                 initStatements.push_back( new DeclStmt( noLabels, newObj ) );
     121                statements.push_back( new DeclStmt( noLabels, newObj ) );
    139122
    140                 // copy construct objDecl using temporary
    141                 UntypedExpr * init = new UntypedExpr( new NameExpr( "?{}" ) );
     123                // assign (later: copy construct) objDecl using temporary
     124                UntypedExpr * init = new UntypedExpr( new NameExpr( "?=?" ) );
    142125                init->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
    143126                init->get_args().push_back( new VariableExpr( newObj ) );
    144                 initStatements.push_back( new ExprStmt( noLabels, init ) );
     127                statements.push_back( new ExprStmt( noLabels, init ) );
    145128
    146                 // add destructor calls to global destroy function
    147                 UntypedExpr * destroy = new UntypedExpr( new NameExpr( "^?{}" ) );
    148                 destroy->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
    149                 destroyStatements.push_front( new ExprStmt( noLabels, destroy ) );
     129                // xxx- need to destruct objDecl atexit
    150130        }
    151131
Note: See TracChangeset for help on using the changeset viewer.