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