| 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 | // GenInit.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Rob Schluntz
|
|---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
|---|
| 11 | // Last Modified By : Rob Schluntz
|
|---|
| 12 | // Last Modified On : Fri May 06 16:11:15 2016
|
|---|
| 13 | // Update Count : 166
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include <stack>
|
|---|
| 17 | #include <list>
|
|---|
| 18 | #include "GenInit.h"
|
|---|
| 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"
|
|---|
| 25 | #include "SymTab/Autogen.h"
|
|---|
| 26 | #include "GenPoly/PolyMutator.h"
|
|---|
| 27 |
|
|---|
| 28 | namespace InitTweak {
|
|---|
| 29 | namespace {
|
|---|
| 30 | const std::list<Label> noLabels;
|
|---|
| 31 | const std::list<Expression *> noDesignators;
|
|---|
| 32 | }
|
|---|
| 33 |
|
|---|
| 34 | class ReturnFixer : public GenPoly::PolyMutator {
|
|---|
| 35 | public:
|
|---|
| 36 | /// consistently allocates a temporary variable for the return value
|
|---|
| 37 | /// of a function so that anything which the resolver decides can be constructed
|
|---|
| 38 | /// into the return type of a function can be returned.
|
|---|
| 39 | static void makeReturnTemp( std::list< Declaration * > &translationUnit );
|
|---|
| 40 |
|
|---|
| 41 | ReturnFixer();
|
|---|
| 42 |
|
|---|
| 43 | virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
|
|---|
| 44 |
|
|---|
| 45 | virtual Statement * mutate( ReturnStmt * returnStmt );
|
|---|
| 46 |
|
|---|
| 47 | protected:
|
|---|
| 48 | std::list<DeclarationWithType*> returnVals;
|
|---|
| 49 | UniqueName tempNamer;
|
|---|
| 50 | std::string funcName;
|
|---|
| 51 | };
|
|---|
| 52 |
|
|---|
| 53 | class CtorDtor : public GenPoly::PolyMutator {
|
|---|
| 54 | public:
|
|---|
| 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 );
|
|---|
| 60 |
|
|---|
| 61 | CtorDtor() : inFunction( false ) {}
|
|---|
| 62 |
|
|---|
| 63 | virtual DeclarationWithType * mutate( ObjectDecl * );
|
|---|
| 64 | virtual DeclarationWithType * mutate( FunctionDecl *functionDecl );
|
|---|
| 65 | virtual Declaration* mutate( StructDecl *aggregateDecl );
|
|---|
| 66 | virtual Declaration* mutate( UnionDecl *aggregateDecl );
|
|---|
| 67 | virtual Declaration* mutate( EnumDecl *aggregateDecl );
|
|---|
| 68 | virtual Declaration* mutate( TraitDecl *aggregateDecl );
|
|---|
| 69 | virtual TypeDecl* mutate( TypeDecl *typeDecl );
|
|---|
| 70 | virtual Declaration* mutate( TypedefDecl *typeDecl );
|
|---|
| 71 |
|
|---|
| 72 | virtual Type * mutate( FunctionType *funcType );
|
|---|
| 73 |
|
|---|
| 74 | protected:
|
|---|
| 75 | bool inFunction;
|
|---|
| 76 | };
|
|---|
| 77 |
|
|---|
| 78 | void genInit( std::list< Declaration * > & translationUnit ) {
|
|---|
| 79 | ReturnFixer::makeReturnTemp( translationUnit );
|
|---|
| 80 | CtorDtor::generateCtorDtor( translationUnit );
|
|---|
| 81 | }
|
|---|
| 82 |
|
|---|
| 83 | void ReturnFixer::makeReturnTemp( std::list< Declaration * > & translationUnit ) {
|
|---|
| 84 | ReturnFixer fixer;
|
|---|
| 85 | mutateAll( translationUnit, fixer );
|
|---|
| 86 | }
|
|---|
| 87 |
|
|---|
| 88 | ReturnFixer::ReturnFixer() : tempNamer( "_retVal" ) {}
|
|---|
| 89 |
|
|---|
| 90 | Statement *ReturnFixer::mutate( ReturnStmt *returnStmt ) {
|
|---|
| 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
|
|---|
| 95 | if ( returnStmt->get_expr() && returnVals.size() == 1 && funcName != "?=?" && ! returnVals.front()->get_type()->get_isLvalue() ) {
|
|---|
| 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 ) );
|
|---|
| 99 | stmtsToAdd.push_back( new DeclStmt( noLabels, newObj ) );
|
|---|
| 100 |
|
|---|
| 101 | // and explicitly create the constructor expression separately
|
|---|
| 102 | UntypedExpr *construct = new UntypedExpr( new NameExpr( "?{}" ) );
|
|---|
| 103 | construct->get_args().push_back( new AddressExpr( new VariableExpr( newObj ) ) );
|
|---|
| 104 | construct->get_args().push_back( returnStmt->get_expr() );
|
|---|
| 105 | stmtsToAdd.push_back(new ExprStmt(noLabels, construct));
|
|---|
| 106 |
|
|---|
| 107 | returnStmt->set_expr( new VariableExpr( newObj ) );
|
|---|
| 108 | } // if
|
|---|
| 109 | return returnStmt;
|
|---|
| 110 | }
|
|---|
| 111 |
|
|---|
| 112 | DeclarationWithType* ReturnFixer::mutate( FunctionDecl *functionDecl ) {
|
|---|
| 113 | std::list<DeclarationWithType*> oldReturnVals = returnVals;
|
|---|
| 114 | std::string oldFuncName = funcName;
|
|---|
| 115 |
|
|---|
| 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 | }
|
|---|
| 124 |
|
|---|
| 125 |
|
|---|
| 126 | void CtorDtor::generateCtorDtor( std::list< Declaration * > & translationUnit ) {
|
|---|
| 127 | CtorDtor ctordtor;
|
|---|
| 128 | mutateAll( translationUnit, ctordtor );
|
|---|
| 129 | }
|
|---|
| 130 |
|
|---|
| 131 | bool tryConstruct( ObjectDecl * objDecl ) {
|
|---|
| 132 | // xxx - handle designations
|
|---|
| 133 | return ! LinkageSpec::isBuiltin( objDecl->get_linkage() ) &&
|
|---|
| 134 | (objDecl->get_init() == NULL ||
|
|---|
| 135 | ( objDecl->get_init() != NULL && objDecl->get_init()->get_maybeConstructed() ));
|
|---|
| 136 | }
|
|---|
| 137 | namespace {
|
|---|
| 138 |
|
|---|
| 139 | Expression * makeCtorDtorExpr( std::string name, ObjectDecl * objDecl, std::list< Expression * > args ) {
|
|---|
| 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 );
|
|---|
| 143 | return expr;
|
|---|
| 144 | }
|
|---|
| 145 |
|
|---|
| 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 | }
|
|---|
| 158 |
|
|---|
| 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 | }
|
|---|
| 165 | }
|
|---|
| 166 |
|
|---|
| 167 | std::list< Expression * > makeInitList( Initializer * init ) {
|
|---|
| 168 | InitExpander expander;
|
|---|
| 169 | maybeAccept( init, expander );
|
|---|
| 170 | return expander.argList;
|
|---|
| 171 | }
|
|---|
| 172 | }
|
|---|
| 173 |
|
|---|
| 174 | DeclarationWithType * CtorDtor::mutate( ObjectDecl * objDecl ) {
|
|---|
| 175 | // hands off if designated or if @=
|
|---|
| 176 | if ( tryConstruct( objDecl ) ) {
|
|---|
| 177 | if ( inFunction ) {
|
|---|
| 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
|
|---|
| 180 | // TODO: walk initializer and generate appropriate copy ctor if element has initializer
|
|---|
| 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() ) );
|
|---|
| 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
|
|---|
| 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() ) );
|
|---|
| 210 | }
|
|---|
| 211 | }
|
|---|
| 212 | }
|
|---|
| 213 | return Mutator::mutate( objDecl );
|
|---|
| 214 | }
|
|---|
| 215 |
|
|---|
| 216 | DeclarationWithType * CtorDtor::mutate( FunctionDecl *functionDecl ) {
|
|---|
| 217 | // parameters should not be constructed and destructed, so don't mutate FunctionType
|
|---|
| 218 | bool oldInFunc = inFunction;
|
|---|
| 219 | mutateAll( functionDecl->get_oldDecls(), *this );
|
|---|
| 220 | inFunction = true;
|
|---|
| 221 | functionDecl->set_statements( maybeMutate( functionDecl->get_statements(), *this ) );
|
|---|
| 222 | inFunction = oldInFunc;
|
|---|
| 223 | return functionDecl;
|
|---|
| 224 | }
|
|---|
| 225 |
|
|---|
| 226 | // should not traverse into any of these declarations to find objects
|
|---|
| 227 | // that need to be constructed or destructed
|
|---|
| 228 | Declaration* CtorDtor::mutate( StructDecl *aggregateDecl ) { return aggregateDecl; }
|
|---|
| 229 | Declaration* CtorDtor::mutate( UnionDecl *aggregateDecl ) { return aggregateDecl; }
|
|---|
| 230 | Declaration* CtorDtor::mutate( EnumDecl *aggregateDecl ) { return aggregateDecl; }
|
|---|
| 231 | Declaration* CtorDtor::mutate( TraitDecl *aggregateDecl ) { return aggregateDecl; }
|
|---|
| 232 | TypeDecl* CtorDtor::mutate( TypeDecl *typeDecl ) { return typeDecl; }
|
|---|
| 233 | Declaration* CtorDtor::mutate( TypedefDecl *typeDecl ) { return typeDecl; }
|
|---|
| 234 | Type* CtorDtor::mutate( FunctionType *funcType ) { return funcType; }
|
|---|
| 235 |
|
|---|
| 236 | } // namespace InitTweak
|
|---|
| 237 |
|
|---|
| 238 | // Local Variables: //
|
|---|
| 239 | // tab-width: 4 //
|
|---|
| 240 | // mode: c++ //
|
|---|
| 241 | // compile-command: "make install" //
|
|---|
| 242 | // End: //
|
|---|