| [2b46a13] | 1 | #include "InitTweak.h" | 
|---|
|  | 2 | #include "SynTree/Visitor.h" | 
|---|
|  | 3 | #include "SynTree/Statement.h" | 
|---|
|  | 4 | #include "SynTree/Initializer.h" | 
|---|
|  | 5 | #include "SynTree/Expression.h" | 
|---|
|  | 6 | #include "GenPoly/GenPoly.h" | 
|---|
|  | 7 |  | 
|---|
|  | 8 | namespace InitTweak { | 
|---|
| [64071c2] | 9 | namespace { | 
|---|
|  | 10 | class HasDesignations : public Visitor { | 
|---|
|  | 11 | public: | 
|---|
|  | 12 | bool hasDesignations = false; | 
|---|
|  | 13 | template<typename Init> | 
|---|
|  | 14 | void handleInit( Init * init ) { | 
|---|
|  | 15 | if ( ! init->get_designators().empty() ) hasDesignations = true; | 
|---|
|  | 16 | else Visitor::visit( init ); | 
|---|
|  | 17 | } | 
|---|
|  | 18 | virtual void visit( SingleInit * singleInit ) { handleInit( singleInit); } | 
|---|
|  | 19 | virtual void visit( ListInit * listInit ) { handleInit( listInit); } | 
|---|
|  | 20 | }; | 
|---|
| [2b46a13] | 21 |  | 
|---|
| [64071c2] | 22 | class InitExpander : public Visitor { | 
|---|
|  | 23 | public: | 
|---|
|  | 24 | InitExpander() {} | 
|---|
|  | 25 | virtual void visit( SingleInit * singleInit ); | 
|---|
|  | 26 | virtual void visit( ListInit * listInit ); | 
|---|
|  | 27 | std::list< Expression * > argList; | 
|---|
|  | 28 | }; | 
|---|
| [2b46a13] | 29 |  | 
|---|
| [64071c2] | 30 | void InitExpander::visit( SingleInit * singleInit ) { | 
|---|
|  | 31 | argList.push_back( singleInit->get_value()->clone() ); | 
|---|
|  | 32 | } | 
|---|
| [2b46a13] | 33 |  | 
|---|
| [64071c2] | 34 | void InitExpander::visit( ListInit * listInit ) { | 
|---|
|  | 35 | // xxx - for now, assume no nested list inits | 
|---|
|  | 36 | std::list<Initializer*>::iterator it = listInit->begin_initializers(); | 
|---|
|  | 37 | for ( ; it != listInit->end_initializers(); ++it ) { | 
|---|
|  | 38 | (*it)->accept( *this ); | 
|---|
|  | 39 | } | 
|---|
|  | 40 | } | 
|---|
|  | 41 | } | 
|---|
| [2b46a13] | 42 |  | 
|---|
| [64071c2] | 43 | std::list< Expression * > makeInitList( Initializer * init ) { | 
|---|
|  | 44 | InitExpander expander; | 
|---|
|  | 45 | maybeAccept( init, expander ); | 
|---|
|  | 46 | return expander.argList; | 
|---|
|  | 47 | } | 
|---|
| [2b46a13] | 48 |  | 
|---|
| [64071c2] | 49 | bool isDesignated( Initializer * init ) { | 
|---|
|  | 50 | HasDesignations finder; | 
|---|
|  | 51 | maybeAccept( init, finder ); | 
|---|
|  | 52 | return finder.hasDesignations; | 
|---|
|  | 53 | } | 
|---|
| [2b46a13] | 54 |  | 
|---|
| [64071c2] | 55 | bool tryConstruct( ObjectDecl * objDecl ) { | 
|---|
|  | 56 | return ! LinkageSpec::isBuiltin( objDecl->get_linkage() ) && | 
|---|
|  | 57 | (objDecl->get_init() == NULL || | 
|---|
|  | 58 | ( objDecl->get_init() != NULL && objDecl->get_init()->get_maybeConstructed() )) && | 
|---|
|  | 59 | ! isDesignated( objDecl->get_init() ); | 
|---|
|  | 60 | } | 
|---|
| [2b46a13] | 61 |  | 
|---|
| [64071c2] | 62 | Expression * getCtorDtorCall( Statement * stmt ) { | 
|---|
|  | 63 | if ( stmt == NULL ) return NULL; | 
|---|
|  | 64 | if ( ExprStmt * exprStmt = dynamic_cast< ExprStmt * >( stmt ) ) { | 
|---|
|  | 65 | return exprStmt->get_expr(); | 
|---|
|  | 66 | } else if ( CompoundStmt * compoundStmt = dynamic_cast< CompoundStmt * >( stmt ) ) { | 
|---|
|  | 67 | // could also be a compound statement with a loop, in the case of an array | 
|---|
| [cad355a] | 68 | if( compoundStmt->get_kids().size() == 2 ) { | 
|---|
|  | 69 | // loop variable and loop | 
|---|
|  | 70 | ForStmt * forStmt = dynamic_cast< ForStmt * >( compoundStmt->get_kids().back() ); | 
|---|
|  | 71 | assert( forStmt && forStmt->get_body() ); | 
|---|
|  | 72 | return getCtorDtorCall( forStmt->get_body() ); | 
|---|
|  | 73 | } else if ( compoundStmt->get_kids().size() == 1 ) { | 
|---|
|  | 74 | // should be the call statement, but in any case there's only one option | 
|---|
|  | 75 | return getCtorDtorCall( compoundStmt->get_kids().front() ); | 
|---|
|  | 76 | } else { | 
|---|
|  | 77 | assert( false && "too many statements in compoundStmt for getCtorDtorCall" ); | 
|---|
|  | 78 | } | 
|---|
| [64071c2] | 79 | } if ( ImplicitCtorDtorStmt * impCtorDtorStmt = dynamic_cast< ImplicitCtorDtorStmt * > ( stmt ) ) { | 
|---|
|  | 80 | return getCtorDtorCall( impCtorDtorStmt->get_callStmt() ); | 
|---|
|  | 81 | } else { | 
|---|
|  | 82 | // should never get here | 
|---|
|  | 83 | assert( false && "encountered unknown call statement" ); | 
|---|
|  | 84 | } | 
|---|
|  | 85 | } | 
|---|
| [70f89d00] | 86 |  | 
|---|
| [64071c2] | 87 | bool isInstrinsicSingleArgCallStmt( Statement * stmt ) { | 
|---|
|  | 88 | Expression * callExpr = getCtorDtorCall( stmt ); | 
|---|
|  | 89 | if ( ! callExpr ) return false; | 
|---|
|  | 90 | ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr ); | 
|---|
|  | 91 | assert( appExpr ); | 
|---|
|  | 92 | VariableExpr * function = dynamic_cast< VariableExpr * >( appExpr->get_function() ); | 
|---|
|  | 93 | assert( function ); | 
|---|
|  | 94 | // check for Intrinsic only - don't want to remove all overridable ctor/dtors because autogenerated ctor/dtor | 
|---|
|  | 95 | // will call all member dtors, and some members may have a user defined dtor. | 
|---|
|  | 96 | FunctionType * funcType = GenPoly::getFunctionType( function->get_var()->get_type() ); | 
|---|
|  | 97 | assert( funcType ); | 
|---|
|  | 98 | return function->get_var()->get_linkage() == LinkageSpec::Intrinsic && funcType->get_parameters().size() == 1; | 
|---|
|  | 99 | } | 
|---|
| [f1b1e4c] | 100 |  | 
|---|
| [64071c2] | 101 | namespace { | 
|---|
|  | 102 | template<typename CallExpr> | 
|---|
|  | 103 | Expression *& callArg( CallExpr * callExpr, unsigned int pos ) { | 
|---|
|  | 104 | if ( pos >= callExpr->get_args().size() ) assert( false && "asking for argument that doesn't exist. Return NULL/throw exception?" ); | 
|---|
|  | 105 | for ( Expression *& arg : callExpr->get_args() ) { | 
|---|
|  | 106 | if ( pos == 0 ) return arg; | 
|---|
|  | 107 | pos--; | 
|---|
|  | 108 | } | 
|---|
|  | 109 | assert( false ); | 
|---|
|  | 110 | } | 
|---|
|  | 111 | } | 
|---|
| [f1b1e4c] | 112 |  | 
|---|
| [64071c2] | 113 | Expression *& getCallArg( Expression * callExpr, unsigned int pos ) { | 
|---|
|  | 114 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( callExpr ) ) { | 
|---|
|  | 115 | return callArg( appExpr, pos ); | 
|---|
|  | 116 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * >( callExpr ) ) { | 
|---|
|  | 117 | return callArg( untypedExpr, pos ); | 
|---|
|  | 118 | } else { | 
|---|
|  | 119 | assert( false && "Unexpected expression type passed to getCallArg" ); | 
|---|
|  | 120 | } | 
|---|
|  | 121 | } | 
|---|
| [f1b1e4c] | 122 |  | 
|---|
| [64071c2] | 123 | namespace { | 
|---|
| [c738ca4] | 124 | std::string funcName( Expression * func ) { | 
|---|
| [64071c2] | 125 | if ( NameExpr * nameExpr = dynamic_cast< NameExpr * >( func ) ) { | 
|---|
|  | 126 | return nameExpr->get_name(); | 
|---|
|  | 127 | } else if ( VariableExpr * varExpr = dynamic_cast< VariableExpr * >( func ) ) { | 
|---|
|  | 128 | return varExpr->get_var()->get_name(); | 
|---|
| [c738ca4] | 129 | }       else if ( CastExpr * castExpr = dynamic_cast< CastExpr * >( func ) ) { | 
|---|
|  | 130 | return funcName( castExpr->get_arg() ); | 
|---|
| [64071c2] | 131 | } else { | 
|---|
|  | 132 | assert( false && "Unexpected expression type being called as a function in call expression" ); | 
|---|
|  | 133 | } | 
|---|
|  | 134 | } | 
|---|
|  | 135 | } | 
|---|
| [70f89d00] | 136 |  | 
|---|
| [64071c2] | 137 | std::string getFunctionName( Expression * expr ) { | 
|---|
|  | 138 | if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * >( expr ) ) { | 
|---|
| [c738ca4] | 139 | return funcName( appExpr->get_function() ); | 
|---|
| [64071c2] | 140 | } else if ( UntypedExpr * untypedExpr = dynamic_cast< UntypedExpr * > ( expr ) ) { | 
|---|
| [c738ca4] | 141 | return funcName( untypedExpr->get_function() ); | 
|---|
| [64071c2] | 142 | } else { | 
|---|
| [c738ca4] | 143 | std::cerr << expr << std::endl; | 
|---|
| [64071c2] | 144 | assert( false && "Unexpected expression type passed to getFunctionName" ); | 
|---|
|  | 145 | } | 
|---|
|  | 146 | } | 
|---|
| [10a7775] | 147 |  | 
|---|
| [64071c2] | 148 | Type * getPointerBase( Type * type ) { | 
|---|
|  | 149 | if ( PointerType * ptrType = dynamic_cast< PointerType * >( type ) ) { | 
|---|
|  | 150 | return ptrType->get_base(); | 
|---|
|  | 151 | } else if ( ArrayType * arrayType = dynamic_cast< ArrayType * >( type ) ) { | 
|---|
|  | 152 | return arrayType->get_base(); | 
|---|
|  | 153 | } else { | 
|---|
|  | 154 | return NULL; | 
|---|
|  | 155 | } | 
|---|
|  | 156 | } | 
|---|
| [10a7775] | 157 |  | 
|---|
| [64071c2] | 158 | Type * isPointerType( Type * type ) { | 
|---|
|  | 159 | if ( getPointerBase( type ) ) return type; | 
|---|
|  | 160 | else return NULL; | 
|---|
|  | 161 | } | 
|---|
| [2b46a13] | 162 | } | 
|---|