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