Ignore:
Timestamp:
May 13, 2016, 1:18:35 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:
d668182
Parents:
fb24492
Message:

Don't construct global extern variables, handle global array construction separately, stub for array initialization list with constructors

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/FixGlobalInit.cc

    rfb24492 r7b3f66b  
    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 13 11:37:30 2016
    1313// Update Count     : 2
    1414//
    1515
    1616#include "FixGlobalInit.h"
    17 #include "GenInit.h"
     17#include "InitTweak.h"
    1818#include "SynTree/Declaration.h"
    1919#include "SynTree/Type.h"
     
    125125                std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids();
    126126
    127                 if ( objDecl->get_init() == NULL ) return;
     127                // if ( objDecl->get_init() == NULL ) return;
    128128                if ( ! tryConstruct( objDecl ) ) return; // don't construct @= or designated objects
    129129                if ( objDecl->get_type()->get_isConst() ) return; // temporary: can't assign to a const variable
     130                if ( objDecl->get_storageClass() == DeclarationNode::Extern ) return;
    130131                // C allows you to initialize objects with constant expressions
    131132                // xxx - this is an optimization. Need to first resolve constructors before we decide
     
    133134                // if ( isConstExpr( objDecl->get_init() ) ) return;
    134135
    135                 // steal initializer from object and attach it to a new temporary
    136                 ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, objDecl->get_type()->clone(), objDecl->get_init() );
    137                 objDecl->set_init( NULL );
    138                 initStatements.push_back( new DeclStmt( noLabels, newObj ) );
     136                if ( ArrayType * at = dynamic_cast< ArrayType * > ( objDecl->get_type() ) ) {
     137                        // xxx - initialize each element of the array
     138                } else {
     139                        // steal initializer from object and attach it to a new temporary
     140                        ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, objDecl->get_type()->clone(), objDecl->get_init() );
     141                        objDecl->set_init( NULL );
     142                        initStatements.push_back( new DeclStmt( noLabels, newObj ) );
    139143
    140                 // copy construct objDecl using temporary
    141                 UntypedExpr * init = new UntypedExpr( new NameExpr( "?{}" ) );
    142                 init->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
    143                 init->get_args().push_back( new VariableExpr( newObj ) );
    144                 initStatements.push_back( new ExprStmt( noLabels, init ) );
     144                        // copy construct objDecl using temporary
     145                        UntypedExpr * init = new UntypedExpr( new NameExpr( "?{}" ) );
     146                        init->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
     147                        init->get_args().push_back( new VariableExpr( newObj ) );
     148                        initStatements.push_back( new ExprStmt( noLabels, init ) );
    145149
    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 ) );
     150                        // add destructor calls to global destroy function
     151                        UntypedExpr * destroy = new UntypedExpr( new NameExpr( "^?{}" ) );
     152                        destroy->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
     153                        destroyStatements.push_front( new ExprStmt( noLabels, destroy ) );
     154                }
    150155        }
    151156
Note: See TracChangeset for help on using the changeset viewer.