Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/InitTweak/FixGlobalInit.cc

    r7b3f66b r7baed7d  
    2222#include "SynTree/Initializer.h"
    2323#include "SynTree/Visitor.h"
     24#include "SynTree/Attribute.h"
    2425#include <algorithm>
    2526
     
    116117        GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) {
    117118                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 ) );
     119                std::list< Expression * > ctorParameters;
     120                std::list< Expression * > dtorParameters;
     121                if ( inLibrary ) {
     122                        // Constructor/destructor attributes take a single parameter which
     123                        // is the priority, with lower numbers meaning higher priority.
     124                        // Functions specified with priority are guaranteed to run before
     125                        // functions without a priority. To ensure that constructors and destructors
     126                        // for library code are run before constructors and destructors for user code,
     127                        // specify a priority when building the library. Priorities 0-100 are reserved by gcc.
     128                        ctorParameters.push_back( new ConstantExpr( Constant::from_int( 101 ) ) );
     129                        dtorParameters.push_back( new ConstantExpr( Constant::from_int( 101 ) ) );
     130                }
     131                initFunction = new FunctionDecl( "_init_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false );
     132                initFunction->get_attributes().push_back( new Attribute( "constructor", ctorParameters ) );
     133                destroyFunction = new FunctionDecl( "_destroy_" + fixedName, DeclarationNode::Static, LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ), false, false );
     134                destroyFunction->get_attributes().push_back( new Attribute( "destructor", dtorParameters ) );
    121135        }
    122136
     
    125139                std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids();
    126140
    127                 // if ( objDecl->get_init() == NULL ) return;
    128141                if ( ! tryConstruct( objDecl ) ) return; // don't construct @= or designated objects
    129                 if ( objDecl->get_type()->get_isConst() ) return; // temporary: can't assign to a const variable
    130142                if ( objDecl->get_storageClass() == DeclarationNode::Extern ) return;
    131143                // C allows you to initialize objects with constant expressions
     
    146158                        init->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
    147159                        init->get_args().push_back( new VariableExpr( newObj ) );
    148                         initStatements.push_back( new ExprStmt( noLabels, init ) );
     160                        initStatements.push_back( new ImplicitCtorDtorStmt( new ExprStmt( noLabels, init ) ) );
    149161
    150162                        // add destructor calls to global destroy function
    151163                        UntypedExpr * destroy = new UntypedExpr( new NameExpr( "^?{}" ) );
    152164                        destroy->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
    153                         destroyStatements.push_front( new ExprStmt( noLabels, destroy ) );
     165                        destroyStatements.push_front( new ImplicitCtorDtorStmt( new ExprStmt( noLabels, destroy ) ) );
    154166                }
    155167        }
Note: See TracChangeset for help on using the changeset viewer.