[51587aa] | 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 | //
|
---|
[a0fdbd5] | 7 | // GenInit.cc --
|
---|
[51587aa] | 8 | //
|
---|
[cf16f94] | 9 | // Author : Rob Schluntz
|
---|
[51587aa] | 10 | // Created On : Mon May 18 07:44:20 2015
|
---|
[1e9d87b] | 11 | // Last Modified By : Rob Schluntz
|
---|
[a0fdbd5] | 12 | // Last Modified On : Thu Apr 28 12:26:47 2016
|
---|
[d63eeb0] | 13 | // Update Count : 166
|
---|
[51587aa] | 14 | //
|
---|
[a08ba92] | 15 |
|
---|
[974906e2] | 16 | #include <stack>
|
---|
| 17 | #include <list>
|
---|
[a0fdbd5] | 18 | #include "GenInit.h"
|
---|
[42e2ad7] | 19 | #include "SynTree/Declaration.h"
|
---|
| 20 | #include "SynTree/Type.h"
|
---|
| 21 | #include "SynTree/Expression.h"
|
---|
| 22 | #include "SynTree/Statement.h"
|
---|
| 23 | #include "SynTree/Initializer.h"
|
---|
| 24 | #include "SynTree/Mutator.h"
|
---|
[620cb95] | 25 | #include "SymTab/Autogen.h"
|
---|
[1e9d87b] | 26 | #include "GenPoly/PolyMutator.h"
|
---|
[42e2ad7] | 27 |
|
---|
| 28 | namespace InitTweak {
|
---|
[a08ba92] | 29 | namespace {
|
---|
| 30 | const std::list<Label> noLabels;
|
---|
[5b40f30] | 31 | const std::list<Expression *> noDesignators;
|
---|
[a08ba92] | 32 | }
|
---|
[1e9d87b] | 33 |
|
---|
[a0fdbd5] | 34 | class ReturnFixer : public GenPoly::PolyMutator {
|
---|
[cf16f94] | 35 | public:
|
---|
[a0fdbd5] | 36 | /// consistently allocates a temporary variable for the return value
|
---|
| 37 | /// of a function so that anything which the resolver decides can be constructed
|
---|
[02c7d04] | 38 | /// into the return type of a function can be returned.
|
---|
[a0fdbd5] | 39 | static void makeReturnTemp( std::list< Declaration * > &translationUnit );
|
---|
[02c7d04] | 40 |
|
---|
[a0fdbd5] | 41 | ReturnFixer();
|
---|
[db4ecc5] | 42 |
|
---|
[cf16f94] | 43 | virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
|
---|
| 44 |
|
---|
| 45 | virtual Statement * mutate( ReturnStmt * returnStmt );
|
---|
[1e9d87b] | 46 |
|
---|
| 47 | protected:
|
---|
[cf16f94] | 48 | std::list<DeclarationWithType*> returnVals;
|
---|
| 49 | UniqueName tempNamer;
|
---|
| 50 | std::string funcName;
|
---|
| 51 | };
|
---|
[42e2ad7] | 52 |
|
---|
[974906e2] | 53 | class CtorDtor : public GenPoly::PolyMutator {
|
---|
| 54 | public:
|
---|
[02c7d04] | 55 | /// create constructor and destructor statements for object declarations.
|
---|
| 56 | /// Destructors are inserted directly into the code, whereas constructors
|
---|
| 57 | /// will be added in after the resolver has run so that the initializer expression
|
---|
| 58 | /// is only removed if a constructor is found
|
---|
| 59 | static void generateCtorDtor( std::list< Declaration * > &translationUnit );
|
---|
[974906e2] | 60 |
|
---|
[f1e012b] | 61 | CtorDtor() : inFunction( false ) {}
|
---|
| 62 |
|
---|
[db4ecc5] | 63 | virtual DeclarationWithType * mutate( ObjectDecl * );
|
---|
[02c7d04] | 64 | virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
|
---|
| 65 | virtual Declaration* mutate( StructDecl *aggregateDecl );
|
---|
| 66 | virtual Declaration* mutate( UnionDecl *aggregateDecl );
|
---|
| 67 | virtual Declaration* mutate( EnumDecl *aggregateDecl );
|
---|
[a5a71d0] | 68 | virtual Declaration* mutate( TraitDecl *aggregateDecl );
|
---|
[02c7d04] | 69 | virtual TypeDecl* mutate( TypeDecl *typeDecl );
|
---|
| 70 | virtual Declaration* mutate( TypedefDecl *typeDecl );
|
---|
[974906e2] | 71 |
|
---|
[db4ecc5] | 72 | virtual Type * mutate( FunctionType *funcType );
|
---|
| 73 |
|
---|
[974906e2] | 74 | protected:
|
---|
[f1e012b] | 75 | bool inFunction;
|
---|
[974906e2] | 76 | };
|
---|
| 77 |
|
---|
[a0fdbd5] | 78 | void genInit( std::list< Declaration * > & translationUnit ) {
|
---|
| 79 | ReturnFixer::makeReturnTemp( translationUnit );
|
---|
[02c7d04] | 80 | CtorDtor::generateCtorDtor( translationUnit );
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[a0fdbd5] | 83 | void ReturnFixer::makeReturnTemp( std::list< Declaration * > & translationUnit ) {
|
---|
| 84 | ReturnFixer fixer;
|
---|
| 85 | mutateAll( translationUnit, fixer );
|
---|
[a08ba92] | 86 | }
|
---|
[42e2ad7] | 87 |
|
---|
[a0fdbd5] | 88 | ReturnFixer::ReturnFixer() : tempNamer( "_retVal" ) {}
|
---|
[cf16f94] | 89 |
|
---|
[a0fdbd5] | 90 | Statement *ReturnFixer::mutate( ReturnStmt *returnStmt ) {
|
---|
[cf16f94] | 91 | // update for multiple return values
|
---|
| 92 | assert( returnVals.size() == 0 || returnVals.size() == 1 );
|
---|
| 93 | // hands off if the function returns an lvalue - we don't want to allocate a temporary if a variable's address
|
---|
| 94 | // is being returned
|
---|
[7eabc25] | 95 | if ( returnStmt->get_expr() && returnVals.size() == 1 && funcName != "?=?" && ! returnVals.front()->get_type()->get_isLvalue() ) {
|
---|
[5b40f30] | 96 | // ensure return value is not destructed by explicitly creating
|
---|
| 97 | // an empty SingleInit node wherein maybeConstruct is false
|
---|
| 98 | ObjectDecl *newObj = new ObjectDecl( tempNamer.newName(), DeclarationNode::NoStorageClass, LinkageSpec::C, 0, returnVals.front()->get_type()->clone(), new ListInit( std::list<Initializer*>(), noDesignators, false ) );
|
---|
[1e9d87b] | 99 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
|
---|
| 100 |
|
---|
[5b40f30] | 101 | // and explicitly create the constructor expression separately
|
---|
| 102 | UntypedExpr *construct = new UntypedExpr( new NameExpr( "?{}" ) );
|
---|
[7eabc25] | 103 | construct->get_args().push_back( new AddressExpr( new VariableExpr( newObj ) ) );
|
---|
[5b40f30] | 104 | construct->get_args().push_back( returnStmt->get_expr() );
|
---|
| 105 | stmtsToAdd.push_back(new ExprStmt(noLabels, construct));
|
---|
[cf16f94] | 106 |
|
---|
| 107 | returnStmt->set_expr( new VariableExpr( newObj ) );
|
---|
| 108 | } // if
|
---|
| 109 | return returnStmt;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[a0fdbd5] | 112 | DeclarationWithType* ReturnFixer::mutate( FunctionDecl *functionDecl ) {
|
---|
[cf16f94] | 113 | std::list<DeclarationWithType*> oldReturnVals = returnVals;
|
---|
| 114 | std::string oldFuncName = funcName;
|
---|
[1e9d87b] | 115 |
|
---|
[cf16f94] | 116 | FunctionType * type = functionDecl->get_functionType();
|
---|
| 117 | returnVals = type->get_returnVals();
|
---|
| 118 | funcName = functionDecl->get_name();
|
---|
| 119 | DeclarationWithType * decl = Mutator::mutate( functionDecl );
|
---|
| 120 | returnVals = oldReturnVals;
|
---|
| 121 | funcName = oldFuncName;
|
---|
| 122 | return decl;
|
---|
| 123 | }
|
---|
[974906e2] | 124 |
|
---|
| 125 |
|
---|
[02c7d04] | 126 | void CtorDtor::generateCtorDtor( std::list< Declaration * > & translationUnit ) {
|
---|
| 127 | CtorDtor ctordtor;
|
---|
| 128 | mutateAll( translationUnit, ctordtor );
|
---|
[974906e2] | 129 | }
|
---|
| 130 |
|
---|
[02c7d04] | 131 | namespace {
|
---|
| 132 | bool tryConstruct( ObjectDecl * objDecl ) {
|
---|
| 133 | // xxx - handle designations
|
---|
| 134 | return ! LinkageSpec::isBuiltin( objDecl->get_linkage() ) &&
|
---|
| 135 | (objDecl->get_init() == NULL ||
|
---|
| 136 | ( objDecl->get_init() != NULL && objDecl->get_init()->get_maybeConstructed() ));
|
---|
| 137 | }
|
---|
[974906e2] | 138 |
|
---|
[71f4e4f] | 139 | Expression * makeCtorDtorExpr( std::string name, ObjectDecl * objDecl, std::list< Expression * > args ) {
|
---|
[02c7d04] | 140 | UntypedExpr * expr = new UntypedExpr( new NameExpr( name ) );
|
---|
| 141 | expr->get_args().push_back( new AddressExpr( new VariableExpr( objDecl ) ) );
|
---|
| 142 | expr->get_args().splice( expr->get_args().end(), args );
|
---|
[71f4e4f] | 143 | return expr;
|
---|
[02c7d04] | 144 | }
|
---|
[974906e2] | 145 |
|
---|
[02c7d04] | 146 | class InitExpander : public Visitor {
|
---|
| 147 | public:
|
---|
| 148 | InitExpander() {}
|
---|
| 149 | // ~InitExpander() {}
|
---|
| 150 | virtual void visit( SingleInit * singleInit );
|
---|
| 151 | virtual void visit( ListInit * listInit );
|
---|
| 152 | std::list< Expression * > argList;
|
---|
| 153 | };
|
---|
| 154 |
|
---|
| 155 | void InitExpander::visit( SingleInit * singleInit ) {
|
---|
| 156 | argList.push_back( singleInit->get_value()->clone() );
|
---|
| 157 | }
|
---|
[974906e2] | 158 |
|
---|
[02c7d04] | 159 | void InitExpander::visit( ListInit * listInit ) {
|
---|
| 160 | // xxx - for now, assume no nested list inits
|
---|
| 161 | std::list<Initializer*>::iterator it = listInit->begin_initializers();
|
---|
| 162 | for ( ; it != listInit->end_initializers(); ++it ) {
|
---|
| 163 | (*it)->accept( *this );
|
---|
| 164 | }
|
---|
[974906e2] | 165 | }
|
---|
| 166 |
|
---|
[02c7d04] | 167 | std::list< Expression * > makeInitList( Initializer * init ) {
|
---|
[974906e2] | 168 | InitExpander expander;
|
---|
[02c7d04] | 169 | maybeAccept( init, expander );
|
---|
| 170 | return expander.argList;
|
---|
[974906e2] | 171 | }
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[db4ecc5] | 174 | DeclarationWithType * CtorDtor::mutate( ObjectDecl * objDecl ) {
|
---|
[974906e2] | 175 | // hands off if designated or if @=
|
---|
| 176 | if ( tryConstruct( objDecl ) ) {
|
---|
[f1e012b] | 177 | if ( inFunction ) {
|
---|
[071a31a] | 178 | if ( ArrayType * at = dynamic_cast< ArrayType * >( objDecl->get_type() ) ) {
|
---|
| 179 | // call into makeArrayFunction from validate.cc to generate calls to ctor/dtor for each element of array
|
---|
[620cb95] | 180 | // TODO: walk initializer and generate appropriate copy ctor if element has initializer
|
---|
[5b2f5bb] | 181 | std::list< Statement * > ctor;
|
---|
| 182 | std::list< Statement * > dtor;
|
---|
| 183 |
|
---|
| 184 | SymTab::makeArrayFunction( NULL, new VariableExpr( objDecl ), at, "?{}", back_inserter( ctor ) );
|
---|
| 185 | SymTab::makeArrayFunction( NULL, new VariableExpr( objDecl ), at, "^?{}", front_inserter( dtor ), false );
|
---|
| 186 |
|
---|
| 187 | // Currently makeArrayFunction produces a single Statement - a CompoundStmt
|
---|
| 188 | // which wraps everything that needs to happen. As such, it's technically
|
---|
| 189 | // possible to use a Statement ** in the above calls, but this is inherently
|
---|
| 190 | // unsafe, so instead we take the slightly less efficient route, but will be
|
---|
| 191 | // immediately informed if somehow the above assumption is broken. In this case,
|
---|
| 192 | // we could always wrap the list of statements at this point with a CompoundStmt,
|
---|
| 193 | // but it seems reasonable at the moment for this to be done by makeArrayFunction
|
---|
| 194 | // itself
|
---|
| 195 | assert( ctor.size() == 1 );
|
---|
| 196 | assert( dtor.size() == 1 );
|
---|
| 197 |
|
---|
| 198 | objDecl->set_init( new ConstructorInit( ctor.front(), dtor.front(), objDecl->get_init() ) );
|
---|
[071a31a] | 199 | } else {
|
---|
| 200 | // it's sufficient to attempt to call the ctor/dtor for the given object and its initializer
|
---|
| 201 | Expression * ctor = makeCtorDtorExpr( "?{}", objDecl, makeInitList( objDecl->get_init() ) );
|
---|
| 202 | Expression * dtor = makeCtorDtorExpr( "^?{}", objDecl, std::list< Expression * >() );
|
---|
| 203 |
|
---|
| 204 | // need to remember init expression, in case no ctors exist
|
---|
| 205 | // if ctor does exist, want to use ctor expression instead of init
|
---|
| 206 | // push this decision to the resolver
|
---|
[5b2f5bb] | 207 | ExprStmt * ctorStmt = new ExprStmt( noLabels, ctor );
|
---|
| 208 | ExprStmt * dtorStmt = new ExprStmt( noLabels, dtor );
|
---|
| 209 | objDecl->set_init( new ConstructorInit( ctorStmt, dtorStmt, objDecl->get_init() ) );
|
---|
[071a31a] | 210 | }
|
---|
[f1e012b] | 211 | } else {
|
---|
| 212 | // xxx - find a way to construct/destruct globals
|
---|
| 213 | // hack: implicit "static" initialization routine for each struct type? or something similar?
|
---|
| 214 | // --ties into module system
|
---|
[071a31a] | 215 | // this can be done by mangling main and replacing it with our own main which calls each
|
---|
| 216 | // module initialization routine in some decided order (order given in link command?)
|
---|
| 217 | // and finally calls mangled main
|
---|
[f1e012b] | 218 | }
|
---|
[974906e2] | 219 | }
|
---|
[db4ecc5] | 220 | return Mutator::mutate( objDecl );
|
---|
[974906e2] | 221 | }
|
---|
| 222 |
|
---|
[02c7d04] | 223 | DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) {
|
---|
| 224 | // parameters should not be constructed and destructed, so don't mutate FunctionType
|
---|
[f1e012b] | 225 | bool oldInFunc = inFunction;
|
---|
[02c7d04] | 226 | mutateAll( functionDecl->get_oldDecls(), *this );
|
---|
[f1e012b] | 227 | inFunction = true;
|
---|
[02c7d04] | 228 | functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );
|
---|
[f1e012b] | 229 | inFunction = oldInFunc;
|
---|
[02c7d04] | 230 | return functionDecl;
|
---|
| 231 | }
|
---|
| 232 |
|
---|
| 233 | // should not traverse into any of these declarations to find objects
|
---|
| 234 | // that need to be constructed or destructed
|
---|
| 235 | Declaration* CtorDtor::mutate( StructDecl *aggregateDecl ) { return aggregateDecl; }
|
---|
| 236 | Declaration* CtorDtor::mutate( UnionDecl *aggregateDecl ) { return aggregateDecl; }
|
---|
| 237 | Declaration* CtorDtor::mutate( EnumDecl *aggregateDecl ) { return aggregateDecl; }
|
---|
[a5a71d0] | 238 | Declaration* CtorDtor::mutate( TraitDecl *aggregateDecl ) { return aggregateDecl; }
|
---|
[02c7d04] | 239 | TypeDecl* CtorDtor::mutate( TypeDecl *typeDecl ) { return typeDecl; }
|
---|
| 240 | Declaration* CtorDtor::mutate( TypedefDecl *typeDecl ) { return typeDecl; }
|
---|
[db4ecc5] | 241 | Type* CtorDtor::mutate( FunctionType *funcType ) { return funcType; }
|
---|
[974906e2] | 242 |
|
---|
[42e2ad7] | 243 | } // namespace InitTweak
|
---|
| 244 |
|
---|
[51587aa] | 245 | // Local Variables: //
|
---|
| 246 | // tab-width: 4 //
|
---|
| 247 | // mode: c++ //
|
---|
| 248 | // compile-command: "make install" //
|
---|
| 249 | // End: //
|
---|