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