| [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 | 
 | 
|---|
| [e3e16bc] | 16 | #include <cassert>                       // for strict_dynamic_cast
 | 
|---|
| [08fc48f] | 17 | #include <string>                        // for string
 | 
|---|
| [51b73452] | 18 | 
 | 
|---|
| [8135d4c] | 19 | #include "Common/PassVisitor.h"
 | 
|---|
| [08fc48f] | 20 | #include "GenPoly.h"                     // for isPolyType
 | 
|---|
| [51b73452] | 21 | #include "Lvalue.h"
 | 
|---|
 | 22 | 
 | 
|---|
| [08fc48f] | 23 | #include "Parser/LinkageSpec.h"          // for Spec, isBuiltin, Intrinsic
 | 
|---|
 | 24 | #include "ResolvExpr/TypeEnvironment.h"  // for AssertionSet, OpenVarSet
 | 
|---|
 | 25 | #include "ResolvExpr/Unify.h"            // for unify
 | 
|---|
| [51b73452] | 26 | #include "ResolvExpr/typeops.h"
 | 
|---|
| [8135d4c] | 27 | #include "SymTab/Autogen.h"
 | 
|---|
| [08fc48f] | 28 | #include "SymTab/Indexer.h"              // for Indexer
 | 
|---|
 | 29 | #include "SynTree/Declaration.h"         // for Declaration, FunctionDecl
 | 
|---|
 | 30 | #include "SynTree/Expression.h"          // for Expression, ConditionalExpr
 | 
|---|
 | 31 | #include "SynTree/Mutator.h"             // for mutateAll, Mutator
 | 
|---|
 | 32 | #include "SynTree/Statement.h"           // for ReturnStmt, Statement (ptr o...
 | 
|---|
 | 33 | #include "SynTree/Type.h"                // for PointerType, Type, FunctionType
 | 
|---|
 | 34 | #include "SynTree/Visitor.h"             // for Visitor, acceptAll
 | 
|---|
| [1d776fd] | 35 | 
 | 
|---|
| [cb43451] | 36 | #if 0
 | 
|---|
 | 37 | #define PRINT(x) x
 | 
|---|
 | 38 | #else
 | 
|---|
 | 39 | #define PRINT(x)
 | 
|---|
 | 40 | #endif
 | 
|---|
 | 41 | 
 | 
|---|
| [51b73452] | 42 | namespace GenPoly {
 | 
|---|
| [01aeade] | 43 |         namespace {
 | 
|---|
| [9a34b5a] | 44 |                 // TODO: fold this into the general createDeref function??
 | 
|---|
 | 45 |                 Expression * mkDeref( Expression * arg ) {
 | 
|---|
 | 46 |                         if ( SymTab::dereferenceOperator ) {
 | 
|---|
| [a9b1b0c] | 47 |                                 // note: reference depth can be arbitrarily deep here, so peel off the outermost pointer/reference, not just pointer because they are effecitvely equivalent in this pass
 | 
|---|
| [9a34b5a] | 48 |                                 VariableExpr * deref = new VariableExpr( SymTab::dereferenceOperator );
 | 
|---|
| [0690350] | 49 |                                 deref->result = new PointerType( Type::Qualifiers(), deref->result );
 | 
|---|
 | 50 |                                 Type * base = InitTweak::getPointerBase( arg->result );
 | 
|---|
 | 51 |                                 assertf( base, "expected pointer type in dereference (type was %s)", toString( arg->result ).c_str() );
 | 
|---|
| [9a34b5a] | 52 |                                 ApplicationExpr * ret = new ApplicationExpr( deref, { arg } );
 | 
|---|
| [0690350] | 53 |                                 delete ret->result;
 | 
|---|
 | 54 |                                 ret->result = base->clone();
 | 
|---|
 | 55 |                                 ret->result->set_lvalue( true );
 | 
|---|
| [9a34b5a] | 56 |                                 return ret;
 | 
|---|
 | 57 |                         } else {
 | 
|---|
 | 58 |                                 return UntypedExpr::createDeref( arg );
 | 
|---|
 | 59 |                         }
 | 
|---|
 | 60 |                 }
 | 
|---|
 | 61 | 
 | 
|---|
| [31cb252] | 62 |                 struct ReferenceConversions final : public WithStmtsToAdd {
 | 
|---|
| [1d776fd] | 63 |                         Expression * postmutate( CastExpr * castExpr );
 | 
|---|
| [8a6cf7e] | 64 |                         Expression * postmutate( AddressExpr * addrExpr );
 | 
|---|
| [01aeade] | 65 |                 };
 | 
|---|
 | 66 | 
 | 
|---|
| [1d776fd] | 67 |                 /// Intrinsic functions that take reference parameters don't REALLY take reference parameters -- their reference arguments must always be implicitly dereferenced.
 | 
|---|
 | 68 |                 struct FixIntrinsicArgs final {
 | 
|---|
| [8499c707] | 69 |                         Expression * postmutate( ApplicationExpr * appExpr );
 | 
|---|
| [1d776fd] | 70 |                 };
 | 
|---|
 | 71 | 
 | 
|---|
| [9aaac6e9] | 72 |                 struct FixIntrinsicResult final : public WithGuards {
 | 
|---|
| [8499c707] | 73 |                         Expression * postmutate( ApplicationExpr * appExpr );
 | 
|---|
| [9aaac6e9] | 74 |                         void premutate( FunctionDecl * funcDecl );
 | 
|---|
 | 75 |                         bool inIntrinsic = false;
 | 
|---|
| [8499c707] | 76 |                 };
 | 
|---|
| [1d776fd] | 77 | 
 | 
|---|
 | 78 |                 /// Replace reference types with pointer types
 | 
|---|
 | 79 |                 struct ReferenceTypeElimination final {
 | 
|---|
 | 80 |                         Type * postmutate( ReferenceType * refType );
 | 
|---|
| [01aeade] | 81 |                 };
 | 
|---|
| [b6fd751] | 82 | 
 | 
|---|
 | 83 |                 /// GCC-like Generalized Lvalues (which have since been removed from GCC)
 | 
|---|
 | 84 |                 /// https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Lvalues.html#Lvalues
 | 
|---|
 | 85 |                 /// Replaces &(a,b) with (a, &b), &(a ? b : c) with (a ? &b : &c)
 | 
|---|
| [d335627] | 86 |                 struct GeneralizedLvalue final : public WithVisitorRef<GeneralizedLvalue> {
 | 
|---|
| [1d776fd] | 87 |                         Expression * postmutate( AddressExpr * addressExpr );
 | 
|---|
| [9236060] | 88 |                         Expression * postmutate( MemberExpr * memExpr );
 | 
|---|
| [acd7c5dd] | 89 | 
 | 
|---|
 | 90 |                         template<typename Func>
 | 
|---|
 | 91 |                         Expression * applyTransformation( Expression * expr, Expression * arg, Func mkExpr );
 | 
|---|
| [b6fd751] | 92 |                 };
 | 
|---|
| [cb43451] | 93 | 
 | 
|---|
 | 94 |                 /// Removes redundant &*/*& pattern that this pass can generate
 | 
|---|
 | 95 |                 struct CollapseAddrDeref final {
 | 
|---|
 | 96 |                         Expression * postmutate( AddressExpr * addressExpr );
 | 
|---|
 | 97 |                         Expression * postmutate( ApplicationExpr * appExpr );
 | 
|---|
 | 98 |                 };
 | 
|---|
| [8499c707] | 99 | 
 | 
|---|
| [a3323db1] | 100 |                 struct AddrRef final : public WithGuards, public WithVisitorRef<AddrRef>, public WithShortCircuiting {
 | 
|---|
| [8499c707] | 101 |                         void premutate( AddressExpr * addrExpr );
 | 
|---|
 | 102 |                         Expression * postmutate( AddressExpr * addrExpr );
 | 
|---|
 | 103 |                         void premutate( Expression * expr );
 | 
|---|
| [a3323db1] | 104 |                         void premutate( ApplicationExpr * appExpr );
 | 
|---|
 | 105 |                         void premutate( SingleInit * init );
 | 
|---|
 | 106 | 
 | 
|---|
 | 107 |                         void handleNonAddr( Expression * );
 | 
|---|
| [8499c707] | 108 | 
 | 
|---|
 | 109 |                         bool first = true;
 | 
|---|
 | 110 |                         bool current = false;
 | 
|---|
 | 111 |                         int refDepth = 0;
 | 
|---|
| [a3323db1] | 112 |                         bool addCast = false;
 | 
|---|
| [8499c707] | 113 |                 };
 | 
|---|
| [01aeade] | 114 |         } // namespace
 | 
|---|
 | 115 | 
 | 
|---|
| [b0440b7] | 116 |         static bool referencesEliminated = false;
 | 
|---|
 | 117 |         // used by UntypedExpr::createDeref to determine whether result type of dereference should be ReferenceType or value type.
 | 
|---|
 | 118 |         bool referencesPermissable() {
 | 
|---|
 | 119 |                 return ! referencesEliminated;
 | 
|---|
 | 120 |         }
 | 
|---|
 | 121 | 
 | 
|---|
| [b1ccdfd] | 122 |         void convertLvalue( std::list< Declaration* > & translationUnit ) {
 | 
|---|
| [1d776fd] | 123 |                 PassVisitor<ReferenceConversions> refCvt;
 | 
|---|
 | 124 |                 PassVisitor<ReferenceTypeElimination> elim;
 | 
|---|
 | 125 |                 PassVisitor<GeneralizedLvalue> genLval;
 | 
|---|
 | 126 |                 PassVisitor<FixIntrinsicArgs> fixer;
 | 
|---|
| [cb43451] | 127 |                 PassVisitor<CollapseAddrDeref> collapser;
 | 
|---|
| [8499c707] | 128 |                 PassVisitor<AddrRef> addrRef;
 | 
|---|
 | 129 |                 PassVisitor<FixIntrinsicResult> intrinsicResults;
 | 
|---|
 | 130 |                 mutateAll( translationUnit, intrinsicResults );
 | 
|---|
 | 131 |                 mutateAll( translationUnit, addrRef );
 | 
|---|
| [1d776fd] | 132 |                 mutateAll( translationUnit, refCvt );
 | 
|---|
 | 133 |                 mutateAll( translationUnit, fixer );
 | 
|---|
| [cb43451] | 134 |                 mutateAll( translationUnit, collapser );
 | 
|---|
| [8499c707] | 135 |                 mutateAll( translationUnit, genLval );
 | 
|---|
| [8a6cf7e] | 136 |                 mutateAll( translationUnit, elim );  // last because other passes need reference types to work
 | 
|---|
| [b0440b7] | 137 | 
 | 
|---|
 | 138 |                 // from this point forward, no other pass should create reference types.
 | 
|---|
 | 139 |                 referencesEliminated = true;
 | 
|---|
| [01aeade] | 140 |         }
 | 
|---|
 | 141 | 
 | 
|---|
| [acd7c5dd] | 142 |         Expression * generalizedLvalue( Expression * expr ) {
 | 
|---|
| [9236060] | 143 |                 PassVisitor<GeneralizedLvalue> genLval;
 | 
|---|
| [acd7c5dd] | 144 |                 return expr->acceptMutator( genLval );
 | 
|---|
 | 145 |         }
 | 
|---|
 | 146 | 
 | 
|---|
| [01aeade] | 147 |         namespace {
 | 
|---|
| [8a6cf7e] | 148 |                 // true for intrinsic function calls that return a reference
 | 
|---|
| [1d776fd] | 149 |                 bool isIntrinsicReference( Expression * expr ) {
 | 
|---|
| [8a6cf7e] | 150 |                         if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * >( expr ) ) {
 | 
|---|
 | 151 |                                 std::string fname = InitTweak::getFunctionName( untyped );
 | 
|---|
 | 152 |                                 // known intrinsic-reference prelude functions
 | 
|---|
 | 153 |                                 return fname == "*?" || fname == "?[?]";
 | 
|---|
| [1d776fd] | 154 |                         } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) {
 | 
|---|
 | 155 |                                 if ( DeclarationWithType * func = InitTweak::getFunction( appExpr ) ) {
 | 
|---|
| [8a6cf7e] | 156 |                                         // use type of return variable rather than expr result type, since it may have been changed to a pointer type
 | 
|---|
 | 157 |                                         FunctionType * ftype = GenPoly::getFunctionType( func->get_type() );
 | 
|---|
| [b1ccdfd] | 158 |                                         Type * ret = ftype->returnVals.empty() ? nullptr : ftype->returnVals.front()->get_type();
 | 
|---|
 | 159 |                                         return func->linkage == LinkageSpec::Intrinsic && dynamic_cast<ReferenceType *>( ret );
 | 
|---|
| [1d776fd] | 160 |                                 }
 | 
|---|
| [ce8c12f] | 161 |                         }
 | 
|---|
| [1d776fd] | 162 |                         return false;
 | 
|---|
| [ce8c12f] | 163 |                 }
 | 
|---|
 | 164 | 
 | 
|---|
| [8499c707] | 165 |                 Expression * FixIntrinsicResult::postmutate( ApplicationExpr * appExpr ) {
 | 
|---|
 | 166 |                         if ( isIntrinsicReference( appExpr ) ) {
 | 
|---|
 | 167 |                                 // eliminate reference types from intrinsic applications - now they return lvalues
 | 
|---|
| [c1ec14f] | 168 |                                 ReferenceType * result = strict_dynamic_cast< ReferenceType * >( appExpr->result );
 | 
|---|
 | 169 |                                 appExpr->result = result->base->clone();
 | 
|---|
| [b1ccdfd] | 170 |                                 appExpr->result->set_lvalue( true );
 | 
|---|
| [9aaac6e9] | 171 |                                 if ( ! inIntrinsic ) {
 | 
|---|
 | 172 |                                         // when not in an intrinsic function, add a cast to
 | 
|---|
 | 173 |                                         // don't add cast when in an intrinsic function, since they already have the cast
 | 
|---|
 | 174 |                                         Expression * ret = new CastExpr( appExpr, result );
 | 
|---|
| [b1ccdfd] | 175 |                                         std::swap( ret->env, appExpr->env );
 | 
|---|
| [9aaac6e9] | 176 |                                         return ret;
 | 
|---|
 | 177 |                                 }
 | 
|---|
 | 178 |                                 delete result;
 | 
|---|
| [8499c707] | 179 |                         }
 | 
|---|
 | 180 |                         return appExpr;
 | 
|---|
 | 181 |                 }
 | 
|---|
 | 182 | 
 | 
|---|
| [9aaac6e9] | 183 |                 void FixIntrinsicResult::premutate( FunctionDecl * funcDecl ) {
 | 
|---|
 | 184 |                         GuardValue( inIntrinsic );
 | 
|---|
| [c1ec14f] | 185 |                         inIntrinsic = funcDecl->linkage == LinkageSpec::Intrinsic;
 | 
|---|
| [9aaac6e9] | 186 |                 }
 | 
|---|
 | 187 | 
 | 
|---|
| [1d776fd] | 188 |                 Expression * FixIntrinsicArgs::postmutate( ApplicationExpr * appExpr ) {
 | 
|---|
| [9a34b5a] | 189 |                         // intrinsic functions don't really take reference-typed parameters, so they require an implicit dereference on their arguments.
 | 
|---|
| [1d776fd] | 190 |                         if ( DeclarationWithType * function = InitTweak::getFunction( appExpr ) ) {
 | 
|---|
| [d335627] | 191 |                                 FunctionType * ftype = GenPoly::getFunctionType( function->get_type() );
 | 
|---|
 | 192 |                                 assertf( ftype, "Function declaration does not have function type." );
 | 
|---|
 | 193 |                                 // can be of differing lengths only when function is variadic
 | 
|---|
| [b1ccdfd] | 194 |                                 assertf( ftype->parameters.size() == appExpr->args.size() || ftype->isVarArgs, "ApplicationExpr args do not match formal parameter type." );
 | 
|---|
| [b0440b7] | 195 | 
 | 
|---|
 | 196 | 
 | 
|---|
| [d335627] | 197 |                                 unsigned int i = 0;
 | 
|---|
| [b1ccdfd] | 198 |                                 const unsigned int end = ftype->parameters.size();
 | 
|---|
 | 199 |                                 for ( auto p : unsafe_group_iterate( appExpr->args, ftype->parameters ) ) {
 | 
|---|
| [d335627] | 200 |                                         if (i == end) break;
 | 
|---|
 | 201 |                                         Expression *& arg = std::get<0>( p );
 | 
|---|
 | 202 |                                         Type * formal = std::get<1>( p )->get_type();
 | 
|---|
 | 203 |                                         PRINT(
 | 
|---|
 | 204 |                                                 std::cerr << "pair<0>: " << arg << std::endl;
 | 
|---|
| [a9b1b0c] | 205 |                                                 std::cerr << " -- " << arg->result << std::endl;
 | 
|---|
| [d335627] | 206 |                                                 std::cerr << "pair<1>: " << formal << std::endl;
 | 
|---|
 | 207 |                                         )
 | 
|---|
 | 208 |                                         if ( dynamic_cast<ReferenceType*>( formal ) ) {
 | 
|---|
| [a9b1b0c] | 209 |                                                 PRINT(
 | 
|---|
 | 210 |                                                         std::cerr << "===formal is reference" << std::endl;
 | 
|---|
 | 211 |                                                 )
 | 
|---|
 | 212 |                                                 // TODO: it's likely that the second condition should be ... && ! isIntrinsicReference( arg ), but this requires investigation.
 | 
|---|
| [207b496] | 213 | 
 | 
|---|
| [a9b1b0c] | 214 |                                                 if ( function->get_linkage() != LinkageSpec::Intrinsic && isIntrinsicReference( arg ) ) {
 | 
|---|
| [207b496] | 215 |                                                         // needed for definition of prelude functions, etc.
 | 
|---|
| [a9b1b0c] | 216 |                                                         // if argument is dereference or array subscript, the result isn't REALLY a reference, but non-intrinsic functions expect a reference: take address
 | 
|---|
| [a3323db1] | 217 | 
 | 
|---|
 | 218 |                                                         // NOTE: previously, this condition fixed
 | 
|---|
 | 219 |                                                         //   void f(int *&);
 | 
|---|
 | 220 |                                                         //   int & x = ...;
 | 
|---|
 | 221 |                                                         //   f(&x);
 | 
|---|
 | 222 |                                                         // But now this is taken care of by a reference cast added by AddrRef. Need to find a new
 | 
|---|
 | 223 |                                                         // example or remove this branch.
 | 
|---|
| [207b496] | 224 | 
 | 
|---|
| [a9b1b0c] | 225 |                                                         PRINT(
 | 
|---|
 | 226 |                                                                 std::cerr << "===is intrinsic arg in non-intrinsic call - adding address" << std::endl;
 | 
|---|
 | 227 |                                                         )
 | 
|---|
 | 228 |                                                         arg = new AddressExpr( arg );
 | 
|---|
| [da7fe39] | 229 |                                                 // } else if ( function->get_linkage() == LinkageSpec::Intrinsic && InitTweak::getPointerBase( arg->result ) ) {
 | 
|---|
| [a9b1b0c] | 230 |                                                 } else if ( function->get_linkage() == LinkageSpec::Intrinsic && arg->result->referenceDepth() != 0 ) {
 | 
|---|
 | 231 |                                                         // argument is a 'real' reference, but function expects a C lvalue: add a dereference to the reference-typed argument
 | 
|---|
 | 232 |                                                         PRINT(
 | 
|---|
 | 233 |                                                                 std::cerr << "===is non-intrinsic arg in intrinsic call - adding deref to arg" << std::endl;
 | 
|---|
 | 234 |                                                         )
 | 
|---|
| [b1ccdfd] | 235 |                                                         Type * baseType = InitTweak::getPointerBase( arg->result );
 | 
|---|
 | 236 |                                                         assertf( baseType, "parameter is reference, arg must be pointer or reference: %s", toString( arg->result ).c_str() );
 | 
|---|
| [d335627] | 237 |                                                         PointerType * ptrType = new PointerType( Type::Qualifiers(), baseType->clone() );
 | 
|---|
| [b1ccdfd] | 238 |                                                         delete arg->result;
 | 
|---|
| [207b496] | 239 |                                                         arg->result = ptrType;
 | 
|---|
| [d335627] | 240 |                                                         arg = mkDeref( arg );
 | 
|---|
| [207b496] | 241 |                                                         // assertf( arg->result->referenceDepth() == 0, "Reference types should have been eliminated from intrinsic function calls, but weren't: %s", toCString( arg->result ) );
 | 
|---|
| [1d776fd] | 242 |                                                 }
 | 
|---|
 | 243 |                                         }
 | 
|---|
| [d335627] | 244 |                                         ++i;
 | 
|---|
| [baba5d8] | 245 |                                 }
 | 
|---|
| [1d776fd] | 246 |                         }
 | 
|---|
 | 247 |                         return appExpr;
 | 
|---|
| [01aeade] | 248 |                 }
 | 
|---|
 | 249 | 
 | 
|---|
| [8499c707] | 250 |                 // idea: &&&E: get outer &, inner &
 | 
|---|
| [a3323db1] | 251 |                 // at inner &, record depth D of reference type of argument of &
 | 
|---|
| [8499c707] | 252 |                 // at outer &, add D derefs.
 | 
|---|
| [a3323db1] | 253 |                 void AddrRef::handleNonAddr( Expression * ) {
 | 
|---|
 | 254 |                         // non-address-of: reset status variables:
 | 
|---|
 | 255 |                         // * current expr is NOT the first address-of expr in an address-of chain
 | 
|---|
 | 256 |                         // * next seen address-of expr IS the first in the chain.
 | 
|---|
| [8499c707] | 257 |                         GuardValue( current );
 | 
|---|
 | 258 |                         GuardValue( first );
 | 
|---|
 | 259 |                         current = false;
 | 
|---|
 | 260 |                         first = true;
 | 
|---|
 | 261 |                 }
 | 
|---|
 | 262 | 
 | 
|---|
| [a3323db1] | 263 |                 void AddrRef::premutate( Expression * expr ) {
 | 
|---|
 | 264 |                         handleNonAddr( expr );
 | 
|---|
 | 265 |                         GuardValue( addCast );
 | 
|---|
 | 266 |                         addCast = false;
 | 
|---|
 | 267 |                 }
 | 
|---|
 | 268 | 
 | 
|---|
| [8135d4c] | 269 |                 void AddrRef::premutate( AddressExpr * ) {
 | 
|---|
| [8499c707] | 270 |                         GuardValue( current );
 | 
|---|
 | 271 |                         GuardValue( first );
 | 
|---|
| [a3323db1] | 272 |                         current = first; // is this the first address-of in the chain?
 | 
|---|
 | 273 |                         first = false;   // from here out, no longer possible for next address-of to be first in chain
 | 
|---|
 | 274 |                         if ( current ) { // this is the outermost address-of in a chain
 | 
|---|
| [8499c707] | 275 |                                 GuardValue( refDepth );
 | 
|---|
| [a3323db1] | 276 |                                 refDepth = 0;  // set depth to 0 so that postmutate can find the innermost address-of easily
 | 
|---|
| [8499c707] | 277 |                         }
 | 
|---|
 | 278 |                 }
 | 
|---|
 | 279 | 
 | 
|---|
 | 280 |                 Expression * AddrRef::postmutate( AddressExpr * addrExpr ) {
 | 
|---|
| [f74eb47] | 281 |                         PRINT( std::cerr << "addr ref at " << addrExpr << std::endl; )
 | 
|---|
| [8499c707] | 282 |                         if ( refDepth == 0 ) {
 | 
|---|
| [f74eb47] | 283 |                                 PRINT( std::cerr << "depth 0, get new depth..." << std::endl; )
 | 
|---|
| [a3323db1] | 284 |                                 // this is the innermost address-of in a chain, record depth D
 | 
|---|
| [b1ccdfd] | 285 |                                 if ( ! isIntrinsicReference( addrExpr->arg ) ) {
 | 
|---|
| [8499c707] | 286 |                                         // try to avoid ?[?]
 | 
|---|
| [a3323db1] | 287 |                                         // xxx - is this condition still necessary? intrinsicReferences should have a cast around them at this point, so I don't think this condition ever fires.
 | 
|---|
| [b1ccdfd] | 288 |                                         refDepth = addrExpr->arg->result->referenceDepth();
 | 
|---|
| [f74eb47] | 289 |                                         PRINT( std::cerr << "arg not intrinsic reference, new depth is: " << refDepth << std::endl; )
 | 
|---|
| [a3323db1] | 290 |                                 } else {
 | 
|---|
 | 291 |                                         assertf( false, "AddrRef : address-of should not have intrinsic reference argument: %s", toCString( addrExpr->arg ) );
 | 
|---|
| [8499c707] | 292 |                                 }
 | 
|---|
 | 293 |                         }
 | 
|---|
| [a3323db1] | 294 |                         if ( current ) { // this is the outermost address-of in a chain
 | 
|---|
| [f74eb47] | 295 |                                 PRINT( std::cerr << "current, depth is: " << refDepth << std::endl; )
 | 
|---|
| [8499c707] | 296 |                                 Expression * ret = addrExpr;
 | 
|---|
 | 297 |                                 while ( refDepth ) {
 | 
|---|
| [a3323db1] | 298 |                                         // add one dereference for each
 | 
|---|
| [8499c707] | 299 |                                         ret = mkDeref( ret );
 | 
|---|
 | 300 |                                         refDepth--;
 | 
|---|
 | 301 |                                 }
 | 
|---|
| [a3323db1] | 302 | 
 | 
|---|
| [57acae0] | 303 |                                 // if addrExpr depth is 0, then the result is a pointer because the arg was depth 1 and not lvalue.
 | 
|---|
 | 304 |                                 // This means the dereference result is not a reference, is lvalue, and one less pointer depth than
 | 
|---|
 | 305 |                                 // the addrExpr. Thus the cast is meaningless.
 | 
|---|
 | 306 |                                 // TODO: One thing to double check is whether it is possible for the types to differ outside of the single
 | 
|---|
 | 307 |                                 // pointer level (i.e. can the base type of addrExpr differ from the type of addrExpr-arg?).
 | 
|---|
 | 308 |                                 // If so then the cast might need to be added, conditional on a more sophisticated check.
 | 
|---|
 | 309 |                                 if ( addCast && addrExpr->result->referenceDepth() != 0 ) {
 | 
|---|
 | 310 |                                         PRINT( std::cerr << "adding cast to " << addrExpr->result << std::endl; )
 | 
|---|
| [a3323db1] | 311 |                                         return new CastExpr( ret, addrExpr->result->clone() );
 | 
|---|
 | 312 |                                 }
 | 
|---|
| [8499c707] | 313 |                                 return ret;
 | 
|---|
 | 314 |                         }
 | 
|---|
| [f74eb47] | 315 |                         PRINT( std::cerr << "not current..." << std::endl; )
 | 
|---|
| [8499c707] | 316 |                         return addrExpr;
 | 
|---|
 | 317 |                 }
 | 
|---|
 | 318 | 
 | 
|---|
| [a3323db1] | 319 |                 void AddrRef::premutate( ApplicationExpr * appExpr ) {
 | 
|---|
 | 320 |                         visit_children = false;
 | 
|---|
 | 321 |                         GuardValue( addCast );
 | 
|---|
 | 322 |                         handleNonAddr( appExpr );
 | 
|---|
 | 323 |                         for ( Expression *& arg : appExpr->args ) {
 | 
|---|
 | 324 |                                 // each argument with address-of requires a cast
 | 
|---|
 | 325 |                                 addCast = true;
 | 
|---|
 | 326 |                                 arg = arg->acceptMutator( *visitor );
 | 
|---|
 | 327 |                         }
 | 
|---|
 | 328 |                 }
 | 
|---|
 | 329 | 
 | 
|---|
 | 330 |                 void AddrRef::premutate( SingleInit * ) {
 | 
|---|
 | 331 |                         GuardValue( addCast );
 | 
|---|
 | 332 |                         // each initialization context with address-of requires a cast
 | 
|---|
 | 333 |                         addCast = true;
 | 
|---|
 | 334 |                 }
 | 
|---|
 | 335 | 
 | 
|---|
 | 336 | 
 | 
|---|
| [8a6cf7e] | 337 |                 Expression * ReferenceConversions::postmutate( AddressExpr * addrExpr ) {
 | 
|---|
 | 338 |                         // Inner expression may have been lvalue to reference conversion, which becomes an address expression.
 | 
|---|
 | 339 |                         // In this case, remove the outer address expression and return the argument.
 | 
|---|
 | 340 |                         // TODO: It's possible that this might catch too much and require a more sophisticated check.
 | 
|---|
 | 341 |                         return addrExpr;
 | 
|---|
 | 342 |                 }
 | 
|---|
 | 343 | 
 | 
|---|
| [1d776fd] | 344 |                 Expression * ReferenceConversions::postmutate( CastExpr * castExpr ) {
 | 
|---|
| [9a34b5a] | 345 |                         // xxx - is it possible to convert directly between reference types with a different base? E.g.,
 | 
|---|
 | 346 |                         //   int x;
 | 
|---|
 | 347 |                         //   (double&)x;
 | 
|---|
 | 348 |                         // At the moment, I am working off of the assumption that this is illegal, thus the cast becomes redundant
 | 
|---|
 | 349 |                         // after this pass, so trash the cast altogether. If that changes, care must be taken to insert the correct
 | 
|---|
 | 350 |                         // pointer casts in the right places.
 | 
|---|
 | 351 | 
 | 
|---|
| [a4d188f] | 352 |                         // Note: reference depth difference is the determining factor in what code is run, rather than whether something is
 | 
|---|
 | 353 |                         // reference type or not, since conversion still needs to occur when both types are references that differ in depth.
 | 
|---|
| [31cb252] | 354 | 
 | 
|---|
 | 355 |                         Type * destType = castExpr->result;
 | 
|---|
 | 356 |                         Type * srcType = castExpr->arg->result;
 | 
|---|
 | 357 |                         int depth1 = destType->referenceDepth();
 | 
|---|
 | 358 |                         int depth2 = srcType->referenceDepth();
 | 
|---|
 | 359 |                         int diff = depth1 - depth2;
 | 
|---|
 | 360 | 
 | 
|---|
 | 361 |                         if ( diff > 0 && ! srcType->get_lvalue() ) {
 | 
|---|
 | 362 |                                 // rvalue to reference conversion -- introduce temporary
 | 
|---|
 | 363 |                                 // know that reference depth of cast argument is 0, need to introduce n temporaries for reference depth of n, e.g.
 | 
|---|
 | 364 |                                 //   (int &&&)3;
 | 
|---|
 | 365 |                                 // becomes
 | 
|---|
 | 366 |                                 //   int __ref_tmp_0 = 3;
 | 
|---|
 | 367 |                                 //   int & __ref_tmp_1 = _&_ref_tmp_0;
 | 
|---|
 | 368 |                                 //   int && __ref_tmp_2 = &__ref_tmp_1;
 | 
|---|
 | 369 |                                 //   &__ref_tmp_2;
 | 
|---|
| [453b586] | 370 |                                 // the last & comes from the remaining reference conversion code
 | 
|---|
| [2348881] | 371 |                                 SemanticWarning( castExpr->arg->location, Warning::RvalueToReferenceConversion, toCString( castExpr->arg ) );
 | 
|---|
| [31cb252] | 372 | 
 | 
|---|
 | 373 |                                 static UniqueName tempNamer( "__ref_tmp_" );
 | 
|---|
 | 374 |                                 ObjectDecl * temp = ObjectDecl::newObject( tempNamer.newName(), castExpr->arg->result->clone(), new SingleInit( castExpr->arg ) );
 | 
|---|
 | 375 |                                 PRINT( std::cerr << "made temp: " << temp << std::endl; )
 | 
|---|
 | 376 |                                 stmtsToAddBefore.push_back( new DeclStmt( temp ) );
 | 
|---|
| [a4d188f] | 377 |                                 for ( int i = 0; i < depth1-1; i++ ) { // xxx - maybe this should be diff-1? check how this works with reference type for srcType
 | 
|---|
| [31cb252] | 378 |                                         ObjectDecl * newTemp = ObjectDecl::newObject( tempNamer.newName(), new ReferenceType( Type::Qualifiers(), temp->type->clone() ), new SingleInit( new AddressExpr( new VariableExpr( temp ) ) ) );
 | 
|---|
 | 379 |                                         PRINT( std::cerr << "made temp" << i << ": " << newTemp << std::endl; )
 | 
|---|
 | 380 |                                         stmtsToAddBefore.push_back( new DeclStmt( newTemp ) );
 | 
|---|
 | 381 |                                         temp = newTemp;
 | 
|---|
 | 382 |                                 }
 | 
|---|
 | 383 |                                 // update diff so that remaining code works out correctly
 | 
|---|
 | 384 |                                 castExpr->arg = new VariableExpr( temp );
 | 
|---|
 | 385 |                                 PRINT( std::cerr << "update cast to: " << castExpr << std::endl; )
 | 
|---|
 | 386 |                                 srcType = castExpr->arg->result;
 | 
|---|
 | 387 |                                 depth2 = srcType->referenceDepth();
 | 
|---|
 | 388 |                                 diff = depth1 - depth2;
 | 
|---|
 | 389 |                                 assert( diff == 1 );
 | 
|---|
 | 390 |                         }
 | 
|---|
| [fc56cdbf] | 391 | 
 | 
|---|
| [a4d188f] | 392 |                         // handle conversion between different depths
 | 
|---|
| [31cb252] | 393 |                         PRINT (
 | 
|---|
 | 394 |                                 if ( depth1 || depth2 ) {
 | 
|---|
 | 395 |                                         std::cerr << "destType: " << destType << " / srcType: " << srcType << std::endl;
 | 
|---|
 | 396 |                                         std::cerr << "depth: " << depth1 << " / " << depth2 << std::endl;
 | 
|---|
 | 397 |                                 }
 | 
|---|
 | 398 |                         )
 | 
|---|
 | 399 |                         if ( diff > 0 ) {
 | 
|---|
 | 400 |                                 // conversion to type with more depth (e.g. int & -> int &&): add address-of for each level of difference
 | 
|---|
 | 401 |                                 Expression * ret = castExpr->arg;
 | 
|---|
 | 402 |                                 for ( int i = 0; i < diff; ++i ) {
 | 
|---|
 | 403 |                                         ret = new AddressExpr( ret );
 | 
|---|
 | 404 |                                 }
 | 
|---|
| [6b8c4a8] | 405 |                                 if ( srcType->get_lvalue() && ! ResolvExpr::typesCompatible( srcType, strict_dynamic_cast<ReferenceType *>( destType )->base, SymTab::Indexer() ) ) {
 | 
|---|
| [31cb252] | 406 |                                         // must keep cast if cast-to type is different from the actual type
 | 
|---|
 | 407 |                                         castExpr->arg = ret;
 | 
|---|
| [1d776fd] | 408 |                                         return castExpr;
 | 
|---|
 | 409 |                                 }
 | 
|---|
| [31cb252] | 410 |                                 ret->env = castExpr->env;
 | 
|---|
 | 411 |                                 delete ret->result;
 | 
|---|
 | 412 |                                 ret->result = castExpr->result;
 | 
|---|
 | 413 |                                 castExpr->env = nullptr;
 | 
|---|
 | 414 |                                 castExpr->arg = nullptr;
 | 
|---|
 | 415 |                                 castExpr->result = nullptr;
 | 
|---|
 | 416 |                                 delete castExpr;
 | 
|---|
 | 417 |                                 return ret;
 | 
|---|
 | 418 |                         } else if ( diff < 0 ) {
 | 
|---|
 | 419 |                                 // conversion to type with less depth (e.g. int && -> int &): add dereferences for each level of difference
 | 
|---|
 | 420 |                                 diff = -diff; // care only about magnitude now
 | 
|---|
| [0690350] | 421 |                                 Expression * ret = castExpr->arg;
 | 
|---|
| [31cb252] | 422 |                                 for ( int i = 0; i < diff; ++i ) {
 | 
|---|
| [9191a8e] | 423 |                                         ret = mkDeref( ret );
 | 
|---|
| [ba89e9b7] | 424 |                                         // xxx - try removing one reference here? actually, looks like mkDeref already does this, so more closely look at the types generated.
 | 
|---|
| [9191a8e] | 425 |                                 }
 | 
|---|
| [31cb252] | 426 |                                 if ( ! ResolvExpr::typesCompatibleIgnoreQualifiers( destType->stripReferences(), srcType->stripReferences(), SymTab::Indexer() ) ) {
 | 
|---|
| [8a6cf7e] | 427 |                                         // must keep cast if types are different
 | 
|---|
| [0690350] | 428 |                                         castExpr->arg = ret;
 | 
|---|
| [31cb252] | 429 |                                         return castExpr;
 | 
|---|
| [8a6cf7e] | 430 |                                 }
 | 
|---|
| [31cb252] | 431 |                                 ret->env = castExpr->env;
 | 
|---|
 | 432 |                                 delete ret->result;
 | 
|---|
 | 433 |                                 ret->result = castExpr->result;
 | 
|---|
 | 434 |                                 ret->result->set_lvalue( true ); // ensure result is lvalue
 | 
|---|
 | 435 |                                 castExpr->env = nullptr;
 | 
|---|
 | 436 |                                 castExpr->arg = nullptr;
 | 
|---|
 | 437 |                                 castExpr->result = nullptr;
 | 
|---|
 | 438 |                                 delete castExpr;
 | 
|---|
| [9191a8e] | 439 |                                 return ret;
 | 
|---|
| [31cb252] | 440 |                         } else {
 | 
|---|
 | 441 |                                 assert( diff == 0 );
 | 
|---|
 | 442 |                                 // conversion between references of the same depth
 | 
|---|
| [70a5acf] | 443 |                                 if ( ResolvExpr::typesCompatible( castExpr->result, castExpr->arg->result, SymTab::Indexer() ) && castExpr->isGenerated ) {
 | 
|---|
 | 444 |                                         // Remove useless generated casts
 | 
|---|
 | 445 |                                         PRINT(
 | 
|---|
 | 446 |                                                 std::cerr << "types are compatible, removing cast: " << castExpr << std::endl;
 | 
|---|
 | 447 |                                                 std::cerr << "-- " << castExpr->result << std::endl;
 | 
|---|
 | 448 |                                                 std::cerr << "-- " << castExpr->arg->result << std::endl;
 | 
|---|
 | 449 |                                         )
 | 
|---|
 | 450 |                                         Expression * ret = castExpr->arg;
 | 
|---|
 | 451 |                                         castExpr->arg = nullptr;
 | 
|---|
 | 452 |                                         std::swap( castExpr->env, ret->env );
 | 
|---|
 | 453 |                                         delete castExpr;
 | 
|---|
 | 454 |                                         return ret;
 | 
|---|
 | 455 |                                 }
 | 
|---|
| [31cb252] | 456 |                                 return castExpr;
 | 
|---|
| [1d776fd] | 457 |                         }
 | 
|---|
| [01aeade] | 458 |                 }
 | 
|---|
| [b6fd751] | 459 | 
 | 
|---|
| [1d776fd] | 460 |                 Type * ReferenceTypeElimination::postmutate( ReferenceType * refType ) {
 | 
|---|
| [b1ccdfd] | 461 |                         Type * base = refType->base;
 | 
|---|
| [8a6cf7e] | 462 |                         Type::Qualifiers qualifiers = refType->get_qualifiers();
 | 
|---|
| [b1ccdfd] | 463 |                         refType->base = nullptr;
 | 
|---|
| [1d776fd] | 464 |                         delete refType;
 | 
|---|
| [8a6cf7e] | 465 |                         return new PointerType( qualifiers, base );
 | 
|---|
| [ce8c12f] | 466 |                 }
 | 
|---|
 | 467 | 
 | 
|---|
| [acd7c5dd] | 468 |                 template<typename Func>
 | 
|---|
 | 469 |                 Expression * GeneralizedLvalue::applyTransformation( Expression * expr, Expression * arg, Func mkExpr ) {
 | 
|---|
 | 470 |                         if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( arg ) ) {
 | 
|---|
| [b1ccdfd] | 471 |                                 Expression * arg1 = commaExpr->arg1->clone();
 | 
|---|
 | 472 |                                 Expression * arg2 = commaExpr->arg2->clone();
 | 
|---|
| [9236060] | 473 |                                 Expression * ret = new CommaExpr( arg1, mkExpr( arg2 )->acceptMutator( *visitor ) );
 | 
|---|
| [b1ccdfd] | 474 |                                 ret->env = expr->env;
 | 
|---|
 | 475 |                                 expr->env = nullptr;
 | 
|---|
| [acd7c5dd] | 476 |                                 delete expr;
 | 
|---|
| [8499c707] | 477 |                                 return ret;
 | 
|---|
| [acd7c5dd] | 478 |                         } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( arg ) ) {
 | 
|---|
| [b1ccdfd] | 479 |                                 Expression * arg1 = condExpr->arg1->clone();
 | 
|---|
 | 480 |                                 Expression * arg2 = condExpr->arg2->clone();
 | 
|---|
 | 481 |                                 Expression * arg3 = condExpr->arg3->clone();
 | 
|---|
| [9236060] | 482 |                                 ConditionalExpr * ret = new ConditionalExpr( arg1, mkExpr( arg2 )->acceptMutator( *visitor ), mkExpr( arg3 )->acceptMutator( *visitor ) );
 | 
|---|
| [b1ccdfd] | 483 |                                 ret->env = expr->env;
 | 
|---|
 | 484 |                                 expr->env = nullptr;
 | 
|---|
| [acd7c5dd] | 485 |                                 delete expr;
 | 
|---|
 | 486 | 
 | 
|---|
 | 487 |                                 // conditional expr type may not be either of the argument types, need to unify
 | 
|---|
 | 488 |                                 using namespace ResolvExpr;
 | 
|---|
 | 489 |                                 Type* commonType = nullptr;
 | 
|---|
 | 490 |                                 TypeEnvironment newEnv;
 | 
|---|
 | 491 |                                 AssertionSet needAssertions, haveAssertions;
 | 
|---|
 | 492 |                                 OpenVarSet openVars;
 | 
|---|
| [b1ccdfd] | 493 |                                 unify( ret->arg2->result, ret->arg3->result, newEnv, needAssertions, haveAssertions, openVars, SymTab::Indexer(), commonType );
 | 
|---|
 | 494 |                                 ret->result = commonType ? commonType : ret->arg2->result->clone();
 | 
|---|
| [8499c707] | 495 |                                 return ret;
 | 
|---|
| [b6fd751] | 496 |                         }
 | 
|---|
| [acd7c5dd] | 497 |                         return expr;
 | 
|---|
 | 498 |                 }
 | 
|---|
 | 499 | 
 | 
|---|
| [9236060] | 500 |                 Expression * GeneralizedLvalue::postmutate( MemberExpr * memExpr ) {
 | 
|---|
| [b1ccdfd] | 501 |                         return applyTransformation( memExpr, memExpr->aggregate, [=]( Expression * aggr ) { return new MemberExpr( memExpr->member, aggr ); } );
 | 
|---|
| [acd7c5dd] | 502 |                 }
 | 
|---|
 | 503 | 
 | 
|---|
| [9236060] | 504 |                 Expression * GeneralizedLvalue::postmutate( AddressExpr * addrExpr ) {
 | 
|---|
| [b1ccdfd] | 505 |                         return applyTransformation( addrExpr, addrExpr->arg, []( Expression * arg ) { return new AddressExpr( arg ); } );
 | 
|---|
| [b6fd751] | 506 |                 }
 | 
|---|
| [cb43451] | 507 | 
 | 
|---|
| [8499c707] | 508 |                 Expression * CollapseAddrDeref::postmutate( AddressExpr * addrExpr ) {
 | 
|---|
| [b1ccdfd] | 509 |                         Expression * arg = addrExpr->arg;
 | 
|---|
| [cb43451] | 510 |                         if ( isIntrinsicReference( arg ) ) {
 | 
|---|
 | 511 |                                 std::string fname = InitTweak::getFunctionName( arg );
 | 
|---|
 | 512 |                                 if ( fname == "*?" ) {
 | 
|---|
 | 513 |                                         Expression *& arg0 = InitTweak::getCallArg( arg, 0 );
 | 
|---|
 | 514 |                                         Expression * ret = arg0;
 | 
|---|
| [b1ccdfd] | 515 |                                         ret->set_env( addrExpr->env );
 | 
|---|
| [cb43451] | 516 |                                         arg0 = nullptr;
 | 
|---|
| [b1ccdfd] | 517 |                                         addrExpr->env = nullptr;
 | 
|---|
| [8499c707] | 518 |                                         delete addrExpr;
 | 
|---|
| [cb43451] | 519 |                                         return ret;
 | 
|---|
 | 520 |                                 }
 | 
|---|
| [bf7b6015] | 521 |                         } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * > ( arg ) ) {
 | 
|---|
 | 522 |                                 // need to move cast to pointer type out a level since address of pointer
 | 
|---|
 | 523 |                                 // is not valid C code (can be introduced in prior passes, e.g., InstantiateGeneric)
 | 
|---|
 | 524 |                                 if ( InitTweak::getPointerBase( castExpr->result ) ) {
 | 
|---|
 | 525 |                                         addrExpr->arg = castExpr->arg;
 | 
|---|
 | 526 |                                         castExpr->arg = addrExpr;
 | 
|---|
 | 527 |                                         castExpr->result = new PointerType( Type::Qualifiers(), castExpr->result );
 | 
|---|
 | 528 |                                         return castExpr;
 | 
|---|
 | 529 |                                 }
 | 
|---|
| [cb43451] | 530 |                         }
 | 
|---|
| [8499c707] | 531 |                         return addrExpr;
 | 
|---|
| [cb43451] | 532 |                 }
 | 
|---|
 | 533 | 
 | 
|---|
 | 534 |                 Expression * CollapseAddrDeref::postmutate( ApplicationExpr * appExpr ) {
 | 
|---|
 | 535 |                         if ( isIntrinsicReference( appExpr ) ) {
 | 
|---|
 | 536 |                                 std::string fname = InitTweak::getFunctionName( appExpr );
 | 
|---|
 | 537 |                                 if ( fname == "*?" ) {
 | 
|---|
 | 538 |                                         Expression * arg = InitTweak::getCallArg( appExpr, 0 );
 | 
|---|
 | 539 |                                         // xxx - this isn't right, because it can remove casts that should be there...
 | 
|---|
 | 540 |                                         // while ( CastExpr * castExpr = dynamic_cast< CastExpr * >( arg ) ) {
 | 
|---|
 | 541 |                                         //      arg = castExpr->get_arg();
 | 
|---|
 | 542 |                                         // }
 | 
|---|
 | 543 |                                         if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( arg ) ) {
 | 
|---|
| [b1ccdfd] | 544 |                                                 Expression * ret = addrExpr->arg;
 | 
|---|
 | 545 |                                                 ret->env = appExpr->env;
 | 
|---|
 | 546 |                                                 addrExpr->arg = nullptr;
 | 
|---|
 | 547 |                                                 appExpr->env = nullptr;
 | 
|---|
| [cb43451] | 548 |                                                 delete appExpr;
 | 
|---|
 | 549 |                                                 return ret;
 | 
|---|
 | 550 |                                         }
 | 
|---|
 | 551 |                                 }
 | 
|---|
 | 552 |                         }
 | 
|---|
 | 553 |                         return appExpr;
 | 
|---|
 | 554 |                 }
 | 
|---|
| [01aeade] | 555 |         } // namespace
 | 
|---|
| [51b73452] | 556 | } // namespace GenPoly
 | 
|---|
| [01aeade] | 557 | 
 | 
|---|
| [51587aa] | 558 | // Local Variables: //
 | 
|---|
 | 559 | // tab-width: 4 //
 | 
|---|
 | 560 | // mode: c++ //
 | 
|---|
 | 561 | // compile-command: "make install" //
 | 
|---|
 | 562 | // End: //
 | 
|---|