source: src/InitTweak/FixGlobalInit.cc @ f5c3b6c

ADTaaron-thesisarm-ehast-experimentalcleanup-dtorsdeferred_resndemanglerenumforall-pointer-decayjacob/cs343-translationjenkins-sandboxnew-astnew-ast-unique-exprnew-envno_listpersistent-indexerpthread-emulationqualifiedEnumresolv-newwith_gc
Last change on this file since f5c3b6c was ea6332d, checked in by Thierry Delisle <tdelisle@…>, 7 years ago

Big header cleaning pass - commit 3

  • Property mode set to 100644
File size: 6.3 KB
Line 
1//
2// Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
3//
4// The contents of this file are covered under the licence agreement in the
5// file "LICENCE" distributed with Cforall.
6//
7// FixGlobalInit.cc --
8//
9// Author           : Rob Schluntz
10// Created On       : Mon May 04 15:14:56 2016
11// Last Modified By : Peter A. Buhr
12// Last Modified On : Thu Mar 16 07:53:11 2017
13// Update Count     : 18
14//
15
16#include "FixGlobalInit.h"
17
18#include <cassert>                 // for assert
19#include <stddef.h>                // for NULL
20#include <algorithm>               // for replace_if
21
22#include "Common/SemanticError.h"  // for SemanticError
23#include "Common/UniqueName.h"     // for UniqueName
24#include "InitTweak.h"             // for isIntrinsicSingleArgCallStmt
25#include "Parser/LinkageSpec.h"    // for C
26#include "SynTree/Attribute.h"     // for Attribute
27#include "SynTree/Constant.h"      // for Constant
28#include "SynTree/Declaration.h"   // for FunctionDecl, ObjectDecl, Declaration
29#include "SynTree/Expression.h"    // for ConstantExpr, Expression (ptr only)
30#include "SynTree/Initializer.h"   // for ConstructorInit, Initializer
31#include "SynTree/Label.h"         // for Label, noLabels
32#include "SynTree/Statement.h"     // for CompoundStmt, Statement (ptr only)
33#include "SynTree/Type.h"          // for Type, Type::StorageClasses, Functi...
34#include "SynTree/Visitor.h"       // for acceptAll, Visitor
35
36namespace InitTweak {
37        class GlobalFixer : public Visitor {
38          public:
39                GlobalFixer( const std::string & name, bool inLibrary );
40
41                virtual void visit( ObjectDecl *objDecl );
42                virtual void visit( FunctionDecl *functionDecl );
43                virtual void visit( StructDecl *aggregateDecl );
44                virtual void visit( UnionDecl *aggregateDecl );
45                virtual void visit( EnumDecl *aggregateDecl );
46                virtual void visit( TraitDecl *aggregateDecl );
47                virtual void visit( TypeDecl *typeDecl );
48
49                UniqueName tempNamer;
50                FunctionDecl * initFunction;
51                FunctionDecl * destroyFunction;
52        };
53
54        void fixGlobalInit( std::list< Declaration * > & translationUnit, const std::string & name, bool inLibrary ) {
55                GlobalFixer fixer( name, inLibrary );
56                acceptAll( translationUnit, fixer );
57                // don't need to include function if it's empty
58                if ( fixer.initFunction->get_statements()->get_kids().empty() ) {
59                        delete fixer.initFunction;
60                } else {
61                        translationUnit.push_back( fixer.initFunction );
62                } // if
63
64                if ( fixer.destroyFunction->get_statements()->get_kids().empty() ) {
65                        delete fixer.destroyFunction;
66                } else {
67                        translationUnit.push_back( fixer.destroyFunction );
68                } // if
69        }
70
71  std::string globalFunctionName( const std::string & name ) {
72        // get basename
73        std::string ret = name.substr( 0, name.find( '.' ) );
74        // replace invalid characters with _
75                static std::string invalid = "/-";
76        replace_if( ret.begin(), ret.end(), []( char c ) { return invalid.find(c) != std::string::npos; }, '_' );
77        return ret;
78  }
79
80        GlobalFixer::GlobalFixer( const std::string & name, bool inLibrary ) : tempNamer( "_global_init" ) {
81                std::string fixedName = globalFunctionName( name );
82                std::list< Expression * > ctorParameters;
83                std::list< Expression * > dtorParameters;
84                if ( inLibrary ) {
85                        // Constructor/destructor attributes take a single parameter which
86                        // is the priority, with lower numbers meaning higher priority.
87                        // Functions specified with priority are guaranteed to run before
88                        // functions without a priority. To ensure that constructors and destructors
89                        // for library code are run before constructors and destructors for user code,
90                        // specify a priority when building the library. Priorities 0-100 are reserved by gcc.
91                        ctorParameters.push_back( new ConstantExpr( Constant::from_int( 102 ) ) );
92                        dtorParameters.push_back( new ConstantExpr( Constant::from_int( 102 ) ) );
93                }
94                initFunction = new FunctionDecl( "_init_" + fixedName, Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ) );
95                initFunction->get_attributes().push_back( new Attribute( "constructor", ctorParameters ) );
96                destroyFunction = new FunctionDecl( "_destroy_" + fixedName, Type::StorageClasses( Type::Static ), LinkageSpec::C, new FunctionType( Type::Qualifiers(), false ), new CompoundStmt( noLabels ) );
97                destroyFunction->get_attributes().push_back( new Attribute( "destructor", dtorParameters ) );
98        }
99
100        void GlobalFixer::visit( ObjectDecl *objDecl ) {
101                std::list< Statement * > & initStatements = initFunction->get_statements()->get_kids();
102                std::list< Statement * > & destroyStatements = destroyFunction->get_statements()->get_kids();
103
104                // C allows you to initialize objects with constant expressions
105                // xxx - this is an optimization. Need to first resolve constructors before we decide
106                // to keep C-style initializer.
107                // if ( isConstExpr( objDecl->get_init() ) ) return;
108
109                if ( ConstructorInit * ctorInit = dynamic_cast< ConstructorInit * >( objDecl->get_init() ) ) {
110                        // a decision should have been made by the resolver, so ctor and init are not both non-NULL
111                        assert( ! ctorInit->get_ctor() || ! ctorInit->get_init() );
112
113                        Statement * dtor = ctorInit->get_dtor();
114                        if ( dtor && ! isIntrinsicSingleArgCallStmt( dtor ) ) {
115                                // don't need to call intrinsic dtor, because it does nothing, but
116                                // non-intrinsic dtors must be called
117                                destroyStatements.push_front( dtor );
118                                ctorInit->set_dtor( NULL );
119                        } // if
120                        if ( Statement * ctor = ctorInit->get_ctor() ) {
121                                initStatements.push_back( ctor );
122                                objDecl->set_init( NULL );
123                                ctorInit->set_ctor( NULL );
124                        } else if ( Initializer * init = ctorInit->get_init() ) {
125                                objDecl->set_init( init );
126                                ctorInit->set_init( NULL );
127                        } else {
128                                // no constructor and no initializer, which is okay
129                                objDecl->set_init( NULL );
130                        } // if
131                        delete ctorInit;
132                } // if
133        }
134
135        // only modify global variables
136        void GlobalFixer::visit( __attribute__((unused)) FunctionDecl *functionDecl ) {}
137        void GlobalFixer::visit( __attribute__((unused)) StructDecl *aggregateDecl ) {}
138        void GlobalFixer::visit( __attribute__((unused)) UnionDecl *aggregateDecl ) {}
139        void GlobalFixer::visit( __attribute__((unused)) EnumDecl *aggregateDecl ) {}
140        void GlobalFixer::visit( __attribute__((unused)) TraitDecl *aggregateDecl ) {}
141        void GlobalFixer::visit( __attribute__((unused)) TypeDecl *typeDecl ) {}
142
143} // namespace InitTweak
144
145// Local Variables: //
146// tab-width: 4 //
147// mode: c++ //
148// compile-command: "make install" //
149// End: //
Note: See TracBrowser for help on using the repository browser.