[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 | //
|
---|
[906e24d] | 7 | // Lvalue.cc --
|
---|
[51587aa] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
---|
[01aeade] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[615a096] | 12 | // Last Modified On : Fri Mar 17 09:11:18 2017
|
---|
| 13 | // Update Count : 5
|
---|
[51587aa] | 14 | //
|
---|
[51b73452] | 15 |
|
---|
| 16 | #include <cassert>
|
---|
| 17 |
|
---|
| 18 | #include "Lvalue.h"
|
---|
| 19 |
|
---|
[02ad3f5] | 20 | #include "GenPoly.h"
|
---|
| 21 |
|
---|
[51b73452] | 22 | #include "SynTree/Declaration.h"
|
---|
| 23 | #include "SynTree/Type.h"
|
---|
| 24 | #include "SynTree/Expression.h"
|
---|
| 25 | #include "SynTree/Statement.h"
|
---|
| 26 | #include "SynTree/Visitor.h"
|
---|
| 27 | #include "SynTree/Mutator.h"
|
---|
| 28 | #include "SymTab/Indexer.h"
|
---|
| 29 | #include "ResolvExpr/Resolver.h"
|
---|
| 30 | #include "ResolvExpr/typeops.h"
|
---|
| 31 |
|
---|
[d3b7937] | 32 | #include "Common/UniqueName.h"
|
---|
| 33 | #include "Common/utility.h"
|
---|
[ce8c12f] | 34 | #include "InitTweak/InitTweak.h"
|
---|
[51b73452] | 35 |
|
---|
| 36 | namespace GenPoly {
|
---|
[01aeade] | 37 | namespace {
|
---|
| 38 | const std::list<Label> noLabels;
|
---|
[51b73452] | 39 |
|
---|
[f8b961b] | 40 | /// Replace uses of lvalue returns with appropriate pointers
|
---|
[01aeade] | 41 | class Pass1 : public Mutator {
|
---|
| 42 | public:
|
---|
[ce8c12f] | 43 | typedef Mutator Parent;
|
---|
[01aeade] | 44 | Pass1();
|
---|
[906e24d] | 45 |
|
---|
[ce8c12f] | 46 | // xxx - should this happen to every expression with reference result type? probably just appexpr and varexpr?
|
---|
| 47 | virtual Type *mutate( ReferenceType * refType );
|
---|
| 48 | virtual Expression *mutate( VariableExpr *varExpr );
|
---|
[01aeade] | 49 | virtual Expression *mutate( ApplicationExpr *appExpr );
|
---|
| 50 | virtual Statement *mutate( ReturnStmt *appExpr );
|
---|
| 51 | virtual DeclarationWithType *mutate( FunctionDecl *funDecl );
|
---|
| 52 | private:
|
---|
| 53 | DeclarationWithType* retval;
|
---|
| 54 | };
|
---|
| 55 |
|
---|
[f8b961b] | 56 | /// Replace declarations of lvalue returns with appropriate pointers
|
---|
[01aeade] | 57 | class Pass2 : public Visitor {
|
---|
| 58 | public:
|
---|
| 59 | virtual void visit( FunctionType *funType );
|
---|
| 60 | private:
|
---|
| 61 | };
|
---|
[b6fd751] | 62 |
|
---|
| 63 | /// GCC-like Generalized Lvalues (which have since been removed from GCC)
|
---|
| 64 | /// https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Lvalues.html#Lvalues
|
---|
| 65 | /// Replaces &(a,b) with (a, &b), &(a ? b : c) with (a ? &b : &c)
|
---|
| 66 | class GeneralizedLvalue : public Mutator {
|
---|
| 67 | typedef Mutator Parent;
|
---|
| 68 |
|
---|
| 69 | virtual Expression * mutate( AddressExpr * addressExpr );
|
---|
| 70 | };
|
---|
[01aeade] | 71 | } // namespace
|
---|
| 72 |
|
---|
| 73 | void convertLvalue( std::list< Declaration* >& translationUnit ) {
|
---|
| 74 | Pass1 p1;
|
---|
| 75 | Pass2 p2;
|
---|
[b6fd751] | 76 | GeneralizedLvalue genLval;
|
---|
[01aeade] | 77 | mutateAll( translationUnit, p1 );
|
---|
| 78 | acceptAll( translationUnit, p2 );
|
---|
[b6fd751] | 79 | mutateAll( translationUnit, genLval );
|
---|
[01aeade] | 80 | }
|
---|
| 81 |
|
---|
| 82 | namespace {
|
---|
[02ad3f5] | 83 | Type* isLvalueRet( FunctionType *function ) {
|
---|
| 84 | if ( function->get_returnVals().empty() ) return 0;
|
---|
| 85 | Type *ty = function->get_returnVals().front()->get_type();
|
---|
[615a096] | 86 | return ty->get_lvalue() ? ty : 0;
|
---|
[01aeade] | 87 | }
|
---|
| 88 |
|
---|
| 89 | bool isIntrinsicApp( ApplicationExpr *appExpr ) {
|
---|
| 90 | if ( VariableExpr *varExpr = dynamic_cast< VariableExpr* >( appExpr->get_function() ) ) {
|
---|
| 91 | return varExpr->get_var()->get_linkage() == LinkageSpec::Intrinsic;
|
---|
| 92 | } else {
|
---|
| 93 | return false;
|
---|
| 94 | } // if
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | Pass1::Pass1() {
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | DeclarationWithType * Pass1::mutate( FunctionDecl *funcDecl ) {
|
---|
| 101 | if ( funcDecl->get_statements() ) {
|
---|
| 102 | DeclarationWithType* oldRetval = retval;
|
---|
| 103 | retval = 0;
|
---|
| 104 | if ( ! LinkageSpec::isBuiltin( funcDecl->get_linkage() ) && isLvalueRet( funcDecl->get_functionType() ) ) {
|
---|
| 105 | retval = funcDecl->get_functionType()->get_returnVals().front();
|
---|
| 106 | }
|
---|
| 107 | // fix expressions and return statements in this function
|
---|
| 108 | funcDecl->set_statements( funcDecl->get_statements()->acceptMutator( *this ) );
|
---|
| 109 | retval = oldRetval;
|
---|
| 110 | } // if
|
---|
| 111 | return funcDecl;
|
---|
| 112 | }
|
---|
| 113 |
|
---|
[ce8c12f] | 114 | Type * Pass1::mutate( ReferenceType * refType ) {
|
---|
| 115 | Type * base = refType->get_base();
|
---|
| 116 | refType->set_base( nullptr );
|
---|
| 117 | delete refType;
|
---|
| 118 | return new PointerType( Type::Qualifiers(), base );
|
---|
| 119 | }
|
---|
| 120 |
|
---|
| 121 | Expression * Pass1::mutate( VariableExpr *varExpr ) {
|
---|
| 122 | if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( varExpr->get_result() ) ) {
|
---|
| 123 | varExpr->set_result( refType->acceptMutator( *this ) );
|
---|
| 124 | return UntypedExpr::createDeref( varExpr );
|
---|
| 125 | }
|
---|
| 126 | return Parent::mutate( varExpr );
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[01aeade] | 129 | Expression * Pass1::mutate( ApplicationExpr *appExpr ) {
|
---|
| 130 | appExpr->get_function()->acceptMutator( *this );
|
---|
| 131 | mutateAll( appExpr->get_args(), *this );
|
---|
| 132 |
|
---|
[906e24d] | 133 | PointerType *pointer = safe_dynamic_cast< PointerType* >( appExpr->get_function()->get_result() );
|
---|
| 134 | FunctionType *function = safe_dynamic_cast< FunctionType* >( pointer->get_base() );
|
---|
[01aeade] | 135 |
|
---|
[02ad3f5] | 136 | Type *funType = isLvalueRet( function );
|
---|
| 137 | if ( funType && ! isIntrinsicApp( appExpr ) ) {
|
---|
| 138 | Expression *expr = appExpr;
|
---|
[906e24d] | 139 | Type *appType = appExpr->get_result();
|
---|
[baba5d8] | 140 | if ( isPolyType( funType ) && ! isPolyType( appType ) ) {
|
---|
[02ad3f5] | 141 | // make sure cast for polymorphic type is inside dereference
|
---|
| 142 | expr = new CastExpr( appExpr, new PointerType( Type::Qualifiers(), appType->clone() ) );
|
---|
[baba5d8] | 143 | }
|
---|
[01aeade] | 144 | UntypedExpr *deref = new UntypedExpr( new NameExpr( "*?" ) );
|
---|
[906e24d] | 145 | deref->set_result( appType->clone() );
|
---|
| 146 | appExpr->set_result( new PointerType( Type::Qualifiers(), appType ) );
|
---|
[02ad3f5] | 147 | deref->get_args().push_back( expr );
|
---|
[01aeade] | 148 | return deref;
|
---|
| 149 | } else {
|
---|
| 150 | return appExpr;
|
---|
| 151 | } // if
|
---|
| 152 | }
|
---|
| 153 |
|
---|
| 154 | Statement * Pass1::mutate(ReturnStmt *retStmt) {
|
---|
| 155 | if ( retval && retStmt->get_expr() ) {
|
---|
[615a096] | 156 | if ( retStmt->get_expr()->get_result()->get_lvalue() ) {
|
---|
[cf16f94] | 157 | // ***** Code Removal ***** because casts may be stripped already
|
---|
| 158 |
|
---|
| 159 | // strip casts because not allowed to take address of cast
|
---|
| 160 | // while ( CastExpr *castExpr = dynamic_cast< CastExpr* >( retStmt->get_expr() ) ) {
|
---|
| 161 | // retStmt->set_expr( castExpr->get_arg() );
|
---|
| 162 | // retStmt->get_expr()->set_env( castExpr->get_env() );
|
---|
| 163 | // castExpr->set_env( 0 );
|
---|
| 164 | // castExpr->set_arg( 0 );
|
---|
| 165 | // delete castExpr;
|
---|
| 166 | // } // while
|
---|
[01aeade] | 167 | retStmt->set_expr( new AddressExpr( retStmt->get_expr()->acceptMutator( *this ) ) );
|
---|
| 168 | } else {
|
---|
| 169 | throw SemanticError( "Attempt to return non-lvalue from an lvalue-qualified function" );
|
---|
| 170 | } // if
|
---|
| 171 | } // if
|
---|
| 172 | return retStmt;
|
---|
| 173 | }
|
---|
| 174 |
|
---|
| 175 | void Pass2::visit( FunctionType *funType ) {
|
---|
| 176 | std::string typeName;
|
---|
| 177 | if ( isLvalueRet( funType ) ) {
|
---|
| 178 | DeclarationWithType *retParm = funType->get_returnVals().front();
|
---|
| 179 |
|
---|
| 180 | // make a new parameter that is a pointer to the type of the old return value
|
---|
| 181 | retParm->set_type( new PointerType( Type::Qualifiers(), retParm->get_type() ) );
|
---|
| 182 | } // if
|
---|
[906e24d] | 183 |
|
---|
[01aeade] | 184 | Visitor::visit( funType );
|
---|
| 185 | }
|
---|
[b6fd751] | 186 |
|
---|
[ce8c12f] | 187 | bool isDeref( Expression * expr ) {
|
---|
| 188 | if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * >( expr ) ) {
|
---|
| 189 | return InitTweak::getFunctionName( untyped ) == "*?";
|
---|
| 190 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) {
|
---|
| 191 | return InitTweak::getFunctionName( appExpr ) == "*?";
|
---|
| 192 | } else {
|
---|
| 193 | return false;
|
---|
| 194 | }
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[b6fd751] | 197 | Expression * GeneralizedLvalue::mutate( AddressExpr * addrExpr ) {
|
---|
| 198 | addrExpr = safe_dynamic_cast< AddressExpr * >( Parent::mutate( addrExpr ) );
|
---|
| 199 | if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( addrExpr->get_arg() ) ) {
|
---|
| 200 | Expression * arg1 = commaExpr->get_arg1()->clone();
|
---|
| 201 | Expression * arg2 = commaExpr->get_arg2()->clone();
|
---|
| 202 | delete addrExpr;
|
---|
| 203 | return new CommaExpr( arg1, new AddressExpr( arg2 ) );
|
---|
| 204 | } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( addrExpr->get_arg() ) ) {
|
---|
| 205 | Expression * arg1 = condExpr->get_arg1()->clone();
|
---|
| 206 | Expression * arg2 = condExpr->get_arg2()->clone();
|
---|
| 207 | Expression * arg3 = condExpr->get_arg3()->clone();
|
---|
| 208 | delete addrExpr;
|
---|
| 209 | return new ConditionalExpr( arg1, new AddressExpr( arg2 ), new AddressExpr( arg3 ) );
|
---|
[ce8c12f] | 210 | } else if ( isDeref( addrExpr->get_arg() ) ) {
|
---|
| 211 | // xxx - this doesn't belong here -- move it somewhere else
|
---|
| 212 | Expression *& arg = InitTweak::getCallArg( addrExpr->get_arg(), 0 );
|
---|
| 213 | Expression * inner = arg;
|
---|
| 214 | arg = nullptr;
|
---|
| 215 | delete addrExpr;
|
---|
| 216 | return inner;
|
---|
[b6fd751] | 217 | }
|
---|
| 218 | return addrExpr;
|
---|
| 219 | }
|
---|
[01aeade] | 220 | } // namespace
|
---|
[51b73452] | 221 | } // namespace GenPoly
|
---|
[01aeade] | 222 |
|
---|
[51587aa] | 223 | // Local Variables: //
|
---|
| 224 | // tab-width: 4 //
|
---|
| 225 | // mode: c++ //
|
---|
| 226 | // compile-command: "make install" //
|
---|
| 227 | // End: //
|
---|