[51587aa] | 1 | //
|
---|
| 2 | // Cforall Version 1.0.0 Copyright (C) 2015 University of Waterloo
|
---|
| 3 | //
|
---|
| 4 | // The contents of this file are covered under the licence agreement in the
|
---|
| 5 | // file "LICENCE" distributed with Cforall.
|
---|
| 6 | //
|
---|
[906e24d] | 7 | // Lvalue.cc --
|
---|
[51587aa] | 8 | //
|
---|
| 9 | // Author : Richard C. Bilson
|
---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
---|
[01aeade] | 11 | // Last Modified By : Peter A. Buhr
|
---|
[615a096] | 12 | // Last Modified On : Fri Mar 17 09:11:18 2017
|
---|
| 13 | // Update Count : 5
|
---|
[51587aa] | 14 | //
|
---|
[51b73452] | 15 |
|
---|
[e3e16bc] | 16 | #include <cassert> // for strict_dynamic_cast
|
---|
[08fc48f] | 17 | #include <string> // for string
|
---|
[51b73452] | 18 |
|
---|
[8135d4c] | 19 | #include "Common/PassVisitor.h"
|
---|
[08fc48f] | 20 | #include "GenPoly.h" // for isPolyType
|
---|
[51b73452] | 21 | #include "Lvalue.h"
|
---|
| 22 |
|
---|
[08fc48f] | 23 | #include "Parser/LinkageSpec.h" // for Spec, isBuiltin, Intrinsic
|
---|
| 24 | #include "ResolvExpr/TypeEnvironment.h" // for AssertionSet, OpenVarSet
|
---|
| 25 | #include "ResolvExpr/Unify.h" // for unify
|
---|
[51b73452] | 26 | #include "ResolvExpr/typeops.h"
|
---|
[8135d4c] | 27 | #include "SymTab/Autogen.h"
|
---|
[08fc48f] | 28 | #include "SymTab/Indexer.h" // for Indexer
|
---|
| 29 | #include "SynTree/Declaration.h" // for Declaration, FunctionDecl
|
---|
| 30 | #include "SynTree/Expression.h" // for Expression, ConditionalExpr
|
---|
| 31 | #include "SynTree/Mutator.h" // for mutateAll, Mutator
|
---|
| 32 | #include "SynTree/Statement.h" // for ReturnStmt, Statement (ptr o...
|
---|
| 33 | #include "SynTree/Type.h" // for PointerType, Type, FunctionType
|
---|
| 34 | #include "SynTree/Visitor.h" // for Visitor, acceptAll
|
---|
[1d776fd] | 35 |
|
---|
[cb43451] | 36 | #if 0
|
---|
| 37 | #define PRINT(x) x
|
---|
| 38 | #else
|
---|
| 39 | #define PRINT(x)
|
---|
| 40 | #endif
|
---|
| 41 |
|
---|
[51b73452] | 42 | namespace GenPoly {
|
---|
[01aeade] | 43 | namespace {
|
---|
[9a34b5a] | 44 | // TODO: fold this into the general createDeref function??
|
---|
| 45 | Expression * mkDeref( Expression * arg ) {
|
---|
| 46 | if ( SymTab::dereferenceOperator ) {
|
---|
| 47 | VariableExpr * deref = new VariableExpr( SymTab::dereferenceOperator );
|
---|
[0690350] | 48 | deref->result = new PointerType( Type::Qualifiers(), deref->result );
|
---|
| 49 | Type * base = InitTweak::getPointerBase( arg->result );
|
---|
| 50 | assertf( base, "expected pointer type in dereference (type was %s)", toString( arg->result ).c_str() );
|
---|
[9a34b5a] | 51 | ApplicationExpr * ret = new ApplicationExpr( deref, { arg } );
|
---|
[0690350] | 52 | delete ret->result;
|
---|
| 53 | ret->result = base->clone();
|
---|
| 54 | ret->result->set_lvalue( true );
|
---|
[9a34b5a] | 55 | return ret;
|
---|
| 56 | } else {
|
---|
| 57 | return UntypedExpr::createDeref( arg );
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
[31cb252] | 61 | struct ReferenceConversions final : public WithStmtsToAdd {
|
---|
[1d776fd] | 62 | Expression * postmutate( CastExpr * castExpr );
|
---|
[8a6cf7e] | 63 | Expression * postmutate( AddressExpr * addrExpr );
|
---|
[01aeade] | 64 | };
|
---|
| 65 |
|
---|
[1d776fd] | 66 | /// Intrinsic functions that take reference parameters don't REALLY take reference parameters -- their reference arguments must always be implicitly dereferenced.
|
---|
| 67 | struct FixIntrinsicArgs final {
|
---|
[8499c707] | 68 | Expression * postmutate( ApplicationExpr * appExpr );
|
---|
[1d776fd] | 69 | };
|
---|
| 70 |
|
---|
[9aaac6e9] | 71 | struct FixIntrinsicResult final : public WithGuards {
|
---|
[8499c707] | 72 | Expression * postmutate( ApplicationExpr * appExpr );
|
---|
[9aaac6e9] | 73 | void premutate( FunctionDecl * funcDecl );
|
---|
| 74 | bool inIntrinsic = false;
|
---|
[8499c707] | 75 | };
|
---|
[1d776fd] | 76 |
|
---|
| 77 | /// Replace reference types with pointer types
|
---|
| 78 | struct ReferenceTypeElimination final {
|
---|
| 79 | Type * postmutate( ReferenceType * refType );
|
---|
[01aeade] | 80 | };
|
---|
[b6fd751] | 81 |
|
---|
| 82 | /// GCC-like Generalized Lvalues (which have since been removed from GCC)
|
---|
| 83 | /// https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Lvalues.html#Lvalues
|
---|
| 84 | /// Replaces &(a,b) with (a, &b), &(a ? b : c) with (a ? &b : &c)
|
---|
[d335627] | 85 | struct GeneralizedLvalue final : public WithVisitorRef<GeneralizedLvalue> {
|
---|
[1d776fd] | 86 | Expression * postmutate( AddressExpr * addressExpr );
|
---|
[9236060] | 87 | Expression * postmutate( MemberExpr * memExpr );
|
---|
[acd7c5dd] | 88 |
|
---|
| 89 | template<typename Func>
|
---|
| 90 | Expression * applyTransformation( Expression * expr, Expression * arg, Func mkExpr );
|
---|
[b6fd751] | 91 | };
|
---|
[cb43451] | 92 |
|
---|
| 93 | /// Removes redundant &*/*& pattern that this pass can generate
|
---|
| 94 | struct CollapseAddrDeref final {
|
---|
| 95 | Expression * postmutate( AddressExpr * addressExpr );
|
---|
| 96 | Expression * postmutate( ApplicationExpr * appExpr );
|
---|
| 97 | };
|
---|
[8499c707] | 98 |
|
---|
| 99 | struct AddrRef final : public WithGuards {
|
---|
| 100 | void premutate( AddressExpr * addrExpr );
|
---|
| 101 | Expression * postmutate( AddressExpr * addrExpr );
|
---|
| 102 | void premutate( Expression * expr );
|
---|
| 103 |
|
---|
| 104 | bool first = true;
|
---|
| 105 | bool current = false;
|
---|
| 106 | int refDepth = 0;
|
---|
| 107 | };
|
---|
[01aeade] | 108 | } // namespace
|
---|
| 109 |
|
---|
[b0440b7] | 110 | static bool referencesEliminated = false;
|
---|
| 111 | // used by UntypedExpr::createDeref to determine whether result type of dereference should be ReferenceType or value type.
|
---|
| 112 | bool referencesPermissable() {
|
---|
| 113 | return ! referencesEliminated;
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[b1ccdfd] | 116 | void convertLvalue( std::list< Declaration* > & translationUnit ) {
|
---|
[1d776fd] | 117 | PassVisitor<ReferenceConversions> refCvt;
|
---|
| 118 | PassVisitor<ReferenceTypeElimination> elim;
|
---|
| 119 | PassVisitor<GeneralizedLvalue> genLval;
|
---|
| 120 | PassVisitor<FixIntrinsicArgs> fixer;
|
---|
[cb43451] | 121 | PassVisitor<CollapseAddrDeref> collapser;
|
---|
[8499c707] | 122 | PassVisitor<AddrRef> addrRef;
|
---|
| 123 | PassVisitor<FixIntrinsicResult> intrinsicResults;
|
---|
| 124 | mutateAll( translationUnit, intrinsicResults );
|
---|
| 125 | mutateAll( translationUnit, addrRef );
|
---|
[1d776fd] | 126 | mutateAll( translationUnit, refCvt );
|
---|
| 127 | mutateAll( translationUnit, fixer );
|
---|
[cb43451] | 128 | mutateAll( translationUnit, collapser );
|
---|
[8499c707] | 129 | mutateAll( translationUnit, genLval );
|
---|
[8a6cf7e] | 130 | mutateAll( translationUnit, elim ); // last because other passes need reference types to work
|
---|
[b0440b7] | 131 |
|
---|
| 132 | // from this point forward, no other pass should create reference types.
|
---|
| 133 | referencesEliminated = true;
|
---|
[01aeade] | 134 | }
|
---|
| 135 |
|
---|
[acd7c5dd] | 136 | Expression * generalizedLvalue( Expression * expr ) {
|
---|
[9236060] | 137 | PassVisitor<GeneralizedLvalue> genLval;
|
---|
[acd7c5dd] | 138 | return expr->acceptMutator( genLval );
|
---|
| 139 | }
|
---|
| 140 |
|
---|
[01aeade] | 141 | namespace {
|
---|
[8a6cf7e] | 142 | // true for intrinsic function calls that return a reference
|
---|
[1d776fd] | 143 | bool isIntrinsicReference( Expression * expr ) {
|
---|
[8a6cf7e] | 144 | if ( UntypedExpr * untyped = dynamic_cast< UntypedExpr * >( expr ) ) {
|
---|
| 145 | std::string fname = InitTweak::getFunctionName( untyped );
|
---|
| 146 | // known intrinsic-reference prelude functions
|
---|
| 147 | return fname == "*?" || fname == "?[?]";
|
---|
[1d776fd] | 148 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) {
|
---|
| 149 | if ( DeclarationWithType * func = InitTweak::getFunction( appExpr ) ) {
|
---|
[8a6cf7e] | 150 | // use type of return variable rather than expr result type, since it may have been changed to a pointer type
|
---|
| 151 | FunctionType * ftype = GenPoly::getFunctionType( func->get_type() );
|
---|
[b1ccdfd] | 152 | Type * ret = ftype->returnVals.empty() ? nullptr : ftype->returnVals.front()->get_type();
|
---|
| 153 | return func->linkage == LinkageSpec::Intrinsic && dynamic_cast<ReferenceType *>( ret );
|
---|
[1d776fd] | 154 | }
|
---|
[ce8c12f] | 155 | }
|
---|
[1d776fd] | 156 | return false;
|
---|
[ce8c12f] | 157 | }
|
---|
| 158 |
|
---|
[8499c707] | 159 | Expression * FixIntrinsicResult::postmutate( ApplicationExpr * appExpr ) {
|
---|
| 160 | if ( isIntrinsicReference( appExpr ) ) {
|
---|
| 161 | // eliminate reference types from intrinsic applications - now they return lvalues
|
---|
[b1ccdfd] | 162 | Type * result = appExpr->result;
|
---|
| 163 | appExpr->result = result->stripReferences()->clone();
|
---|
| 164 | appExpr->result->set_lvalue( true );
|
---|
[9aaac6e9] | 165 | if ( ! inIntrinsic ) {
|
---|
| 166 | // when not in an intrinsic function, add a cast to
|
---|
| 167 | // don't add cast when in an intrinsic function, since they already have the cast
|
---|
| 168 | Expression * ret = new CastExpr( appExpr, result );
|
---|
[b1ccdfd] | 169 | std::swap( ret->env, appExpr->env );
|
---|
[9aaac6e9] | 170 | return ret;
|
---|
| 171 | }
|
---|
| 172 | delete result;
|
---|
[8499c707] | 173 | }
|
---|
| 174 | return appExpr;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[9aaac6e9] | 177 | void FixIntrinsicResult::premutate( FunctionDecl * funcDecl ) {
|
---|
| 178 | GuardValue( inIntrinsic );
|
---|
| 179 | inIntrinsic = funcDecl->linkage == LinkageSpec::Intrinsic;
|
---|
| 180 | }
|
---|
| 181 |
|
---|
[1d776fd] | 182 | Expression * FixIntrinsicArgs::postmutate( ApplicationExpr * appExpr ) {
|
---|
[9a34b5a] | 183 | // intrinsic functions don't really take reference-typed parameters, so they require an implicit dereference on their arguments.
|
---|
[1d776fd] | 184 | if ( DeclarationWithType * function = InitTweak::getFunction( appExpr ) ) {
|
---|
[d335627] | 185 | FunctionType * ftype = GenPoly::getFunctionType( function->get_type() );
|
---|
| 186 | assertf( ftype, "Function declaration does not have function type." );
|
---|
| 187 | // can be of differing lengths only when function is variadic
|
---|
[b1ccdfd] | 188 | assertf( ftype->parameters.size() == appExpr->args.size() || ftype->isVarArgs, "ApplicationExpr args do not match formal parameter type." );
|
---|
[b0440b7] | 189 |
|
---|
| 190 |
|
---|
[d335627] | 191 | unsigned int i = 0;
|
---|
[b1ccdfd] | 192 | const unsigned int end = ftype->parameters.size();
|
---|
| 193 | for ( auto p : unsafe_group_iterate( appExpr->args, ftype->parameters ) ) {
|
---|
[d335627] | 194 | if (i == end) break;
|
---|
| 195 | Expression *& arg = std::get<0>( p );
|
---|
| 196 | Type * formal = std::get<1>( p )->get_type();
|
---|
| 197 | PRINT(
|
---|
| 198 | std::cerr << "pair<0>: " << arg << std::endl;
|
---|
| 199 | std::cerr << "pair<1>: " << formal << std::endl;
|
---|
| 200 | )
|
---|
| 201 | if ( dynamic_cast<ReferenceType*>( formal ) ) {
|
---|
[8499c707] | 202 | if ( isIntrinsicReference( arg ) ) { // do not combine conditions, because that changes the meaning of the else if
|
---|
[d335627] | 203 | if ( function->get_linkage() != LinkageSpec::Intrinsic ) { // intrinsic functions that turn pointers into references
|
---|
| 204 | // if argument is dereference or array subscript, the result isn't REALLY a reference, so it's not necessary to fix the argument
|
---|
[8499c707] | 205 | PRINT(
|
---|
| 206 | std::cerr << "===is intrinsic arg in non-intrinsic call - adding address" << std::endl;
|
---|
| 207 | )
|
---|
[d335627] | 208 | arg = new AddressExpr( arg );
|
---|
| 209 | }
|
---|
| 210 | } else if ( function->get_linkage() == LinkageSpec::Intrinsic ) {
|
---|
[8499c707] | 211 | // std::cerr << "===adding deref to arg" << std::endl;
|
---|
[d335627] | 212 | // if the parameter is a reference, add a dereference to the reference-typed argument.
|
---|
[b1ccdfd] | 213 | Type * baseType = InitTweak::getPointerBase( arg->result );
|
---|
| 214 | assertf( baseType, "parameter is reference, arg must be pointer or reference: %s", toString( arg->result ).c_str() );
|
---|
[d335627] | 215 | PointerType * ptrType = new PointerType( Type::Qualifiers(), baseType->clone() );
|
---|
[b1ccdfd] | 216 | delete arg->result;
|
---|
[d335627] | 217 | arg->set_result( ptrType );
|
---|
| 218 | arg = mkDeref( arg );
|
---|
[1d776fd] | 219 | }
|
---|
| 220 | }
|
---|
[d335627] | 221 | ++i;
|
---|
[baba5d8] | 222 | }
|
---|
[1d776fd] | 223 | }
|
---|
| 224 | return appExpr;
|
---|
[01aeade] | 225 | }
|
---|
| 226 |
|
---|
[8499c707] | 227 | // idea: &&&E: get outer &, inner &
|
---|
| 228 | // at inner &, record depth D of reference type
|
---|
| 229 | // at outer &, add D derefs.
|
---|
[8135d4c] | 230 | void AddrRef::premutate( Expression * ) {
|
---|
[8499c707] | 231 | GuardValue( current );
|
---|
| 232 | GuardValue( first );
|
---|
| 233 | current = false;
|
---|
| 234 | first = true;
|
---|
| 235 | }
|
---|
| 236 |
|
---|
[8135d4c] | 237 | void AddrRef::premutate( AddressExpr * ) {
|
---|
[8499c707] | 238 | GuardValue( current );
|
---|
| 239 | GuardValue( first );
|
---|
| 240 | current = first;
|
---|
| 241 | first = false;
|
---|
| 242 | if ( current ) {
|
---|
| 243 | GuardValue( refDepth );
|
---|
| 244 | refDepth = 0;
|
---|
| 245 | }
|
---|
| 246 | }
|
---|
| 247 |
|
---|
| 248 | Expression * AddrRef::postmutate( AddressExpr * addrExpr ) {
|
---|
| 249 | if ( refDepth == 0 ) {
|
---|
[b1ccdfd] | 250 | if ( ! isIntrinsicReference( addrExpr->arg ) ) {
|
---|
[8499c707] | 251 | // try to avoid ?[?]
|
---|
[b1ccdfd] | 252 | refDepth = addrExpr->arg->result->referenceDepth();
|
---|
[8499c707] | 253 | }
|
---|
| 254 | }
|
---|
| 255 | if ( current ) {
|
---|
| 256 | Expression * ret = addrExpr;
|
---|
| 257 | while ( refDepth ) {
|
---|
| 258 | ret = mkDeref( ret );
|
---|
| 259 | refDepth--;
|
---|
| 260 | }
|
---|
| 261 | return ret;
|
---|
| 262 | }
|
---|
| 263 | return addrExpr;
|
---|
| 264 | }
|
---|
| 265 |
|
---|
[8a6cf7e] | 266 | Expression * ReferenceConversions::postmutate( AddressExpr * addrExpr ) {
|
---|
| 267 | // Inner expression may have been lvalue to reference conversion, which becomes an address expression.
|
---|
| 268 | // In this case, remove the outer address expression and return the argument.
|
---|
| 269 | // TODO: It's possible that this might catch too much and require a more sophisticated check.
|
---|
| 270 | return addrExpr;
|
---|
| 271 | }
|
---|
| 272 |
|
---|
[1d776fd] | 273 | Expression * ReferenceConversions::postmutate( CastExpr * castExpr ) {
|
---|
[9a34b5a] | 274 | // xxx - is it possible to convert directly between reference types with a different base? E.g.,
|
---|
| 275 | // int x;
|
---|
| 276 | // (double&)x;
|
---|
| 277 | // At the moment, I am working off of the assumption that this is illegal, thus the cast becomes redundant
|
---|
| 278 | // after this pass, so trash the cast altogether. If that changes, care must be taken to insert the correct
|
---|
| 279 | // pointer casts in the right places.
|
---|
| 280 |
|
---|
[a4d188f] | 281 | // Note: reference depth difference is the determining factor in what code is run, rather than whether something is
|
---|
| 282 | // reference type or not, since conversion still needs to occur when both types are references that differ in depth.
|
---|
[31cb252] | 283 |
|
---|
| 284 | Type * destType = castExpr->result;
|
---|
| 285 | Type * srcType = castExpr->arg->result;
|
---|
| 286 | int depth1 = destType->referenceDepth();
|
---|
| 287 | int depth2 = srcType->referenceDepth();
|
---|
| 288 | int diff = depth1 - depth2;
|
---|
| 289 |
|
---|
| 290 | if ( diff > 0 && ! srcType->get_lvalue() ) {
|
---|
| 291 | // rvalue to reference conversion -- introduce temporary
|
---|
| 292 | // know that reference depth of cast argument is 0, need to introduce n temporaries for reference depth of n, e.g.
|
---|
| 293 | // (int &&&)3;
|
---|
| 294 | // becomes
|
---|
| 295 | // int __ref_tmp_0 = 3;
|
---|
| 296 | // int & __ref_tmp_1 = _&_ref_tmp_0;
|
---|
| 297 | // int && __ref_tmp_2 = &__ref_tmp_1;
|
---|
| 298 | // &__ref_tmp_2;
|
---|
[453b586] | 299 | // the last & comes from the remaining reference conversion code
|
---|
[2348881] | 300 | SemanticWarning( castExpr->arg->location, Warning::RvalueToReferenceConversion, toCString( castExpr->arg ) );
|
---|
[31cb252] | 301 |
|
---|
| 302 | static UniqueName tempNamer( "__ref_tmp_" );
|
---|
| 303 | ObjectDecl * temp = ObjectDecl::newObject( tempNamer.newName(), castExpr->arg->result->clone(), new SingleInit( castExpr->arg ) );
|
---|
| 304 | PRINT( std::cerr << "made temp: " << temp << std::endl; )
|
---|
| 305 | stmtsToAddBefore.push_back( new DeclStmt( temp ) );
|
---|
[a4d188f] | 306 | 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] | 307 | ObjectDecl * newTemp = ObjectDecl::newObject( tempNamer.newName(), new ReferenceType( Type::Qualifiers(), temp->type->clone() ), new SingleInit( new AddressExpr( new VariableExpr( temp ) ) ) );
|
---|
| 308 | PRINT( std::cerr << "made temp" << i << ": " << newTemp << std::endl; )
|
---|
| 309 | stmtsToAddBefore.push_back( new DeclStmt( newTemp ) );
|
---|
| 310 | temp = newTemp;
|
---|
| 311 | }
|
---|
| 312 | // update diff so that remaining code works out correctly
|
---|
| 313 | castExpr->arg = new VariableExpr( temp );
|
---|
| 314 | PRINT( std::cerr << "update cast to: " << castExpr << std::endl; )
|
---|
| 315 | srcType = castExpr->arg->result;
|
---|
| 316 | depth2 = srcType->referenceDepth();
|
---|
| 317 | diff = depth1 - depth2;
|
---|
| 318 | assert( diff == 1 );
|
---|
| 319 | }
|
---|
[fc56cdbf] | 320 |
|
---|
[a4d188f] | 321 | // handle conversion between different depths
|
---|
[31cb252] | 322 | PRINT (
|
---|
| 323 | if ( depth1 || depth2 ) {
|
---|
| 324 | std::cerr << "destType: " << destType << " / srcType: " << srcType << std::endl;
|
---|
| 325 | std::cerr << "depth: " << depth1 << " / " << depth2 << std::endl;
|
---|
| 326 | }
|
---|
| 327 | )
|
---|
| 328 | if ( diff > 0 ) {
|
---|
| 329 | // conversion to type with more depth (e.g. int & -> int &&): add address-of for each level of difference
|
---|
| 330 | Expression * ret = castExpr->arg;
|
---|
| 331 | for ( int i = 0; i < diff; ++i ) {
|
---|
| 332 | ret = new AddressExpr( ret );
|
---|
| 333 | }
|
---|
| 334 | if ( srcType->get_lvalue() && srcType->get_qualifiers() != strict_dynamic_cast<ReferenceType *>( destType )->base->get_qualifiers() ) {
|
---|
| 335 | // must keep cast if cast-to type is different from the actual type
|
---|
| 336 | castExpr->arg = ret;
|
---|
[1d776fd] | 337 | return castExpr;
|
---|
| 338 | }
|
---|
[31cb252] | 339 | ret->env = castExpr->env;
|
---|
| 340 | delete ret->result;
|
---|
| 341 | ret->result = castExpr->result;
|
---|
| 342 | castExpr->env = nullptr;
|
---|
| 343 | castExpr->arg = nullptr;
|
---|
| 344 | castExpr->result = nullptr;
|
---|
| 345 | delete castExpr;
|
---|
| 346 | return ret;
|
---|
| 347 | } else if ( diff < 0 ) {
|
---|
| 348 | // conversion to type with less depth (e.g. int && -> int &): add dereferences for each level of difference
|
---|
| 349 | diff = -diff; // care only about magnitude now
|
---|
[0690350] | 350 | Expression * ret = castExpr->arg;
|
---|
[31cb252] | 351 | for ( int i = 0; i < diff; ++i ) {
|
---|
[9191a8e] | 352 | ret = mkDeref( ret );
|
---|
| 353 | }
|
---|
[31cb252] | 354 | if ( ! ResolvExpr::typesCompatibleIgnoreQualifiers( destType->stripReferences(), srcType->stripReferences(), SymTab::Indexer() ) ) {
|
---|
[8a6cf7e] | 355 | // must keep cast if types are different
|
---|
[0690350] | 356 | castExpr->arg = ret;
|
---|
[31cb252] | 357 | return castExpr;
|
---|
[8a6cf7e] | 358 | }
|
---|
[31cb252] | 359 | ret->env = castExpr->env;
|
---|
| 360 | delete ret->result;
|
---|
| 361 | ret->result = castExpr->result;
|
---|
| 362 | ret->result->set_lvalue( true ); // ensure result is lvalue
|
---|
| 363 | castExpr->env = nullptr;
|
---|
| 364 | castExpr->arg = nullptr;
|
---|
| 365 | castExpr->result = nullptr;
|
---|
| 366 | delete castExpr;
|
---|
[9191a8e] | 367 | return ret;
|
---|
[31cb252] | 368 | } else {
|
---|
| 369 | assert( diff == 0 );
|
---|
| 370 | // conversion between references of the same depth
|
---|
| 371 | return castExpr;
|
---|
[1d776fd] | 372 | }
|
---|
[01aeade] | 373 | }
|
---|
[b6fd751] | 374 |
|
---|
[1d776fd] | 375 | Type * ReferenceTypeElimination::postmutate( ReferenceType * refType ) {
|
---|
[b1ccdfd] | 376 | Type * base = refType->base;
|
---|
[8a6cf7e] | 377 | Type::Qualifiers qualifiers = refType->get_qualifiers();
|
---|
[b1ccdfd] | 378 | refType->base = nullptr;
|
---|
[1d776fd] | 379 | delete refType;
|
---|
[8a6cf7e] | 380 | return new PointerType( qualifiers, base );
|
---|
[ce8c12f] | 381 | }
|
---|
| 382 |
|
---|
[acd7c5dd] | 383 | template<typename Func>
|
---|
| 384 | Expression * GeneralizedLvalue::applyTransformation( Expression * expr, Expression * arg, Func mkExpr ) {
|
---|
| 385 | if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( arg ) ) {
|
---|
[b1ccdfd] | 386 | Expression * arg1 = commaExpr->arg1->clone();
|
---|
| 387 | Expression * arg2 = commaExpr->arg2->clone();
|
---|
[9236060] | 388 | Expression * ret = new CommaExpr( arg1, mkExpr( arg2 )->acceptMutator( *visitor ) );
|
---|
[b1ccdfd] | 389 | ret->env = expr->env;
|
---|
| 390 | expr->env = nullptr;
|
---|
[acd7c5dd] | 391 | delete expr;
|
---|
[8499c707] | 392 | return ret;
|
---|
[acd7c5dd] | 393 | } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( arg ) ) {
|
---|
[b1ccdfd] | 394 | Expression * arg1 = condExpr->arg1->clone();
|
---|
| 395 | Expression * arg2 = condExpr->arg2->clone();
|
---|
| 396 | Expression * arg3 = condExpr->arg3->clone();
|
---|
[9236060] | 397 | ConditionalExpr * ret = new ConditionalExpr( arg1, mkExpr( arg2 )->acceptMutator( *visitor ), mkExpr( arg3 )->acceptMutator( *visitor ) );
|
---|
[b1ccdfd] | 398 | ret->env = expr->env;
|
---|
| 399 | expr->env = nullptr;
|
---|
[acd7c5dd] | 400 | delete expr;
|
---|
| 401 |
|
---|
| 402 | // conditional expr type may not be either of the argument types, need to unify
|
---|
| 403 | using namespace ResolvExpr;
|
---|
| 404 | Type* commonType = nullptr;
|
---|
| 405 | TypeEnvironment newEnv;
|
---|
| 406 | AssertionSet needAssertions, haveAssertions;
|
---|
| 407 | OpenVarSet openVars;
|
---|
[b1ccdfd] | 408 | unify( ret->arg2->result, ret->arg3->result, newEnv, needAssertions, haveAssertions, openVars, SymTab::Indexer(), commonType );
|
---|
| 409 | ret->result = commonType ? commonType : ret->arg2->result->clone();
|
---|
[8499c707] | 410 | return ret;
|
---|
[b6fd751] | 411 | }
|
---|
[acd7c5dd] | 412 | return expr;
|
---|
| 413 | }
|
---|
| 414 |
|
---|
[9236060] | 415 | Expression * GeneralizedLvalue::postmutate( MemberExpr * memExpr ) {
|
---|
[b1ccdfd] | 416 | return applyTransformation( memExpr, memExpr->aggregate, [=]( Expression * aggr ) { return new MemberExpr( memExpr->member, aggr ); } );
|
---|
[acd7c5dd] | 417 | }
|
---|
| 418 |
|
---|
[9236060] | 419 | Expression * GeneralizedLvalue::postmutate( AddressExpr * addrExpr ) {
|
---|
[b1ccdfd] | 420 | return applyTransformation( addrExpr, addrExpr->arg, []( Expression * arg ) { return new AddressExpr( arg ); } );
|
---|
[b6fd751] | 421 | }
|
---|
[cb43451] | 422 |
|
---|
[8499c707] | 423 | Expression * CollapseAddrDeref::postmutate( AddressExpr * addrExpr ) {
|
---|
[b1ccdfd] | 424 | Expression * arg = addrExpr->arg;
|
---|
[cb43451] | 425 | if ( isIntrinsicReference( arg ) ) {
|
---|
| 426 | std::string fname = InitTweak::getFunctionName( arg );
|
---|
| 427 | if ( fname == "*?" ) {
|
---|
| 428 | Expression *& arg0 = InitTweak::getCallArg( arg, 0 );
|
---|
| 429 | Expression * ret = arg0;
|
---|
[b1ccdfd] | 430 | ret->set_env( addrExpr->env );
|
---|
[cb43451] | 431 | arg0 = nullptr;
|
---|
[b1ccdfd] | 432 | addrExpr->env = nullptr;
|
---|
[8499c707] | 433 | delete addrExpr;
|
---|
[cb43451] | 434 | return ret;
|
---|
| 435 | }
|
---|
[bf7b6015] | 436 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * > ( arg ) ) {
|
---|
| 437 | // need to move cast to pointer type out a level since address of pointer
|
---|
| 438 | // is not valid C code (can be introduced in prior passes, e.g., InstantiateGeneric)
|
---|
| 439 | if ( InitTweak::getPointerBase( castExpr->result ) ) {
|
---|
| 440 | addrExpr->arg = castExpr->arg;
|
---|
| 441 | castExpr->arg = addrExpr;
|
---|
| 442 | castExpr->result = new PointerType( Type::Qualifiers(), castExpr->result );
|
---|
| 443 | return castExpr;
|
---|
| 444 | }
|
---|
[cb43451] | 445 | }
|
---|
[8499c707] | 446 | return addrExpr;
|
---|
[cb43451] | 447 | }
|
---|
| 448 |
|
---|
| 449 | Expression * CollapseAddrDeref::postmutate( ApplicationExpr * appExpr ) {
|
---|
| 450 | if ( isIntrinsicReference( appExpr ) ) {
|
---|
| 451 | std::string fname = InitTweak::getFunctionName( appExpr );
|
---|
| 452 | if ( fname == "*?" ) {
|
---|
| 453 | Expression * arg = InitTweak::getCallArg( appExpr, 0 );
|
---|
| 454 | // xxx - this isn't right, because it can remove casts that should be there...
|
---|
| 455 | // while ( CastExpr * castExpr = dynamic_cast< CastExpr * >( arg ) ) {
|
---|
| 456 | // arg = castExpr->get_arg();
|
---|
| 457 | // }
|
---|
| 458 | if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( arg ) ) {
|
---|
[b1ccdfd] | 459 | Expression * ret = addrExpr->arg;
|
---|
| 460 | ret->env = appExpr->env;
|
---|
| 461 | addrExpr->arg = nullptr;
|
---|
| 462 | appExpr->env = nullptr;
|
---|
[cb43451] | 463 | delete appExpr;
|
---|
| 464 | return ret;
|
---|
| 465 | }
|
---|
| 466 | }
|
---|
| 467 | }
|
---|
| 468 | return appExpr;
|
---|
| 469 | }
|
---|
[01aeade] | 470 | } // namespace
|
---|
[51b73452] | 471 | } // namespace GenPoly
|
---|
[01aeade] | 472 |
|
---|
[51587aa] | 473 | // Local Variables: //
|
---|
| 474 | // tab-width: 4 //
|
---|
| 475 | // mode: c++ //
|
---|
| 476 | // compile-command: "make install" //
|
---|
| 477 | // End: //
|
---|