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