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