| 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 | // RemoveInit.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Rodolfo G. Esteves
|
|---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
|---|
| 11 | // Last Modified By : Rob Schluntz
|
|---|
| 12 | // Last Modified On : Mon Nov 16 16:58:36 2015
|
|---|
| 13 | // Update Count : 30
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include "RemoveInit.h"
|
|---|
| 17 | #include "SynTree/Declaration.h"
|
|---|
| 18 | #include "SynTree/Type.h"
|
|---|
| 19 | #include "SynTree/Expression.h"
|
|---|
| 20 | #include "SynTree/Statement.h"
|
|---|
| 21 | #include "SynTree/Initializer.h"
|
|---|
| 22 | #include "SynTree/Mutator.h"
|
|---|
| 23 |
|
|---|
| 24 | // changes to Validate:
|
|---|
| 25 | // -check that ctor/dtor has >= 1 argument
|
|---|
| 26 | // -check that first argument to ctor/dtor has pointer type
|
|---|
| 27 | // -check that return type is void (0 return types)
|
|---|
| 28 | // -transform ctor to return its first argument
|
|---|
| 29 | // -generate ctors and dtors alongside ?=? for aggregate types
|
|---|
| 30 |
|
|---|
| 31 | // idea: modify this pass to decide whether an object declaration is
|
|---|
| 32 | // POD type.
|
|---|
| 33 | // - If it is not POD-type, initialization should be changed into
|
|---|
| 34 | // a constructor call.
|
|---|
| 35 | // - If it is a POD type, then check that there are no designations.
|
|---|
| 36 | // It is probably easiest to leave the declaration in C-initializer
|
|---|
| 37 | // form and resolve as normal, since we don't want to actually incur
|
|---|
| 38 | // the cost of a constructor unless we have to.
|
|---|
| 39 |
|
|---|
| 40 | // change indexer to remove all constructors for a type once a user-defined one appears?
|
|---|
| 41 |
|
|---|
| 42 | // question: if a destructor is declared before the scope of a variable ends,
|
|---|
| 43 | // should it be destructed? Or should we decide this at declaration point?
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 | // alternative (that I think I like better, if there aren't any flaws)
|
|---|
| 47 | // --flaw appears to be the exponential blowup in the number of ctors described below
|
|---|
| 48 | // change into constructor form if no designations
|
|---|
| 49 | // if not POD type, error out if there are designations
|
|---|
| 50 | // if there are designations, handle them in the resolver
|
|---|
| 51 |
|
|---|
| 52 | // ==MAYBE== even possible to rewrite designations not as ?=?, but as ?{} (see initialization.txt)
|
|---|
| 53 | // there may be some transformation that's required to bring this back into a reasonable form
|
|---|
| 54 | // for codegen, it'll depend on exactly what the expressions that are fed to the resolver look like
|
|---|
| 55 | // e.g.
|
|---|
| 56 |
|
|---|
| 57 | // struct A {
|
|---|
| 58 | // struct B { int x; } b;
|
|---|
| 59 | // struct C { int x, y, z } c;
|
|---|
| 60 | // struct D { int x; } d;
|
|---|
| 61 | // }
|
|---|
| 62 | //
|
|---|
| 63 | // A a = { 1, 2, 3, 4, 5 };
|
|---|
| 64 | // => struct A a;
|
|---|
| 65 | // ?{}(&a.b, (struct B){ 1 } );
|
|---|
| 66 | // ?{}(&a.c, (struct C){ 2, 3, 4 } );
|
|---|
| 67 | // ?{}(&a.d, (struct D){ 5 } );
|
|---|
| 68 | // (it obviously shouldn't look like this, but what should it look like??)
|
|---|
| 69 | //
|
|---|
| 70 | // (perhaps this?)
|
|---|
| 71 | // => struct A a;
|
|---|
| 72 | // ?{}(&a, (struct B){ 1 }, (struct C){ 2, 3, 4 }, (struct D){ 5 });
|
|---|
| 73 | // (of course, this requires me to do the grouping found here,
|
|---|
| 74 | // and remember that parts might be missing! That said, I'm essentially
|
|---|
| 75 | // already doing this in the resolver, so whatever I guess?)
|
|---|
| 76 | // (note this requires an alternative finder, because these may be
|
|---|
| 77 | // function calls, not just simple literals)
|
|---|
| 78 | // (this is a bit of a recursive problem - in order to know how to group
|
|---|
| 79 | // the expressions into a struct to be an argument to a constructor, I need to
|
|---|
| 80 | // know what the constructor's signature looks like - but in order to figure out
|
|---|
| 81 | // which constructor is being used (and thus what its signature looks like), I need
|
|---|
| 82 | // to group the values into a struct type)
|
|---|
| 83 | // (this seems to imply (to me, anyway) that C initializers can't be represented as
|
|---|
| 84 | // constructors without an exponential blowup in the number of constructors present)
|
|---|
| 85 |
|
|---|
| 86 | namespace InitTweak {
|
|---|
| 87 | namespace {
|
|---|
| 88 | const std::list<Label> noLabels;
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | void tweak( std::list< Declaration * > translationUnit ) {
|
|---|
| 92 | RemoveInit remover;
|
|---|
| 93 | mutateAll( translationUnit, remover );
|
|---|
| 94 | }
|
|---|
| 95 |
|
|---|
| 96 | void RemoveInit::mutateStatementList( std::list< Statement* > &statements ) {
|
|---|
| 97 | for ( std::list< Statement* >::iterator i = statements.begin(); i != statements.end(); ++i ) {
|
|---|
| 98 | if ( ! stmtsToAddAfter.empty() ) {
|
|---|
| 99 | statements.splice( i, stmtsToAddAfter );
|
|---|
| 100 | } // if
|
|---|
| 101 | *i = (*i)->acceptMutator( *this );
|
|---|
| 102 | } // for
|
|---|
| 103 | if ( ! stmtsToAddAfter.empty() ) {
|
|---|
| 104 | statements.splice( statements.end(), stmtsToAddAfter );
|
|---|
| 105 | } // if
|
|---|
| 106 | }
|
|---|
| 107 |
|
|---|
| 108 | CompoundStmt *RemoveInit::mutate(CompoundStmt *compoundStmt) {
|
|---|
| 109 | mutateStatementList( compoundStmt->get_kids() );
|
|---|
| 110 | return compoundStmt;
|
|---|
| 111 | }
|
|---|
| 112 |
|
|---|
| 113 | // in the case where an object has an initializer and a polymorphic type, insert an assignment
|
|---|
| 114 | // immediately after the declaration. This will (seemingly) cause the later phases to do the right
|
|---|
| 115 | // thing with the assignment
|
|---|
| 116 | ObjectDecl *RemoveInit::mutate( ObjectDecl *objDecl ) {
|
|---|
| 117 | if (objDecl->get_init() && dynamic_cast<TypeInstType*>(objDecl->get_type())) {
|
|---|
| 118 | if (SingleInit * single = dynamic_cast<SingleInit*>(objDecl->get_init())) {
|
|---|
| 119 | UntypedExpr *assign = new UntypedExpr( new NameExpr( "?=?" ) );
|
|---|
| 120 | assign->get_args().push_back( new AddressExpr (new NameExpr( objDecl->get_name() ) ) );
|
|---|
| 121 | assign->get_args().push_back( single->get_value()->clone() );
|
|---|
| 122 | stmtsToAddAfter.push_back(new ExprStmt(noLabels, assign));
|
|---|
| 123 | } // if
|
|---|
| 124 | } // if
|
|---|
| 125 | return objDecl;
|
|---|
| 126 | }
|
|---|
| 127 | } // namespace InitTweak
|
|---|
| 128 |
|
|---|
| 129 | // Local Variables: //
|
|---|
| 130 | // tab-width: 4 //
|
|---|
| 131 | // mode: c++ //
|
|---|
| 132 | // compile-command: "make install" //
|
|---|
| 133 | // End: //
|
|---|