| 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 | //
|
|---|
| 7 | // Lvalue.cc --
|
|---|
| 8 | //
|
|---|
| 9 | // Author : Richard C. Bilson
|
|---|
| 10 | // Created On : Mon May 18 07:44:20 2015
|
|---|
| 11 | // Last Modified By : Peter A. Buhr
|
|---|
| 12 | // Last Modified On : Fri Mar 17 09:11:18 2017
|
|---|
| 13 | // Update Count : 5
|
|---|
| 14 | //
|
|---|
| 15 |
|
|---|
| 16 | #include <cassert> // for strict_dynamic_cast
|
|---|
| 17 | #include <string> // for string
|
|---|
| 18 |
|
|---|
| 19 | #include "Common/PassVisitor.h"
|
|---|
| 20 | #include "GenPoly.h" // for isPolyType
|
|---|
| 21 | #include "Lvalue.h"
|
|---|
| 22 |
|
|---|
| 23 | #include "Parser/LinkageSpec.h" // for Spec, isBuiltin, Intrinsic
|
|---|
| 24 | #include "ResolvExpr/TypeEnvironment.h" // for AssertionSet, OpenVarSet
|
|---|
| 25 | #include "ResolvExpr/Unify.h" // for unify
|
|---|
| 26 | #include "ResolvExpr/typeops.h"
|
|---|
| 27 | #include "SymTab/Autogen.h"
|
|---|
| 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
|
|---|
| 35 |
|
|---|
| 36 | #if 0
|
|---|
| 37 | #define PRINT(x) x
|
|---|
| 38 | #else
|
|---|
| 39 | #define PRINT(x)
|
|---|
| 40 | #endif
|
|---|
| 41 |
|
|---|
| 42 | namespace GenPoly {
|
|---|
| 43 | namespace {
|
|---|
| 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 );
|
|---|
| 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() );
|
|---|
| 51 | ApplicationExpr * ret = new ApplicationExpr( deref, { arg } );
|
|---|
| 52 | delete ret->result;
|
|---|
| 53 | ret->result = base->clone();
|
|---|
| 54 | ret->result->set_lvalue( true );
|
|---|
| 55 | return ret;
|
|---|
| 56 | } else {
|
|---|
| 57 | return UntypedExpr::createDeref( arg );
|
|---|
| 58 | }
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | struct ReferenceConversions final {
|
|---|
| 62 | Expression * postmutate( CastExpr * castExpr );
|
|---|
| 63 | Expression * postmutate( AddressExpr * addrExpr );
|
|---|
| 64 | };
|
|---|
| 65 |
|
|---|
| 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 {
|
|---|
| 68 | Expression * postmutate( ApplicationExpr * appExpr );
|
|---|
| 69 | };
|
|---|
| 70 |
|
|---|
| 71 | struct FixIntrinsicResult final : public WithGuards {
|
|---|
| 72 | Expression * postmutate( ApplicationExpr * appExpr );
|
|---|
| 73 | void premutate( FunctionDecl * funcDecl );
|
|---|
| 74 | bool inIntrinsic = false;
|
|---|
| 75 | };
|
|---|
| 76 |
|
|---|
| 77 | /// Replace reference types with pointer types
|
|---|
| 78 | struct ReferenceTypeElimination final {
|
|---|
| 79 | Type * postmutate( ReferenceType * refType );
|
|---|
| 80 | };
|
|---|
| 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)
|
|---|
| 85 | struct GeneralizedLvalue final : public WithVisitorRef<GeneralizedLvalue> {
|
|---|
| 86 | Expression * postmutate( AddressExpr * addressExpr );
|
|---|
| 87 | Expression * postmutate( MemberExpr * memExpr );
|
|---|
| 88 |
|
|---|
| 89 | template<typename Func>
|
|---|
| 90 | Expression * applyTransformation( Expression * expr, Expression * arg, Func mkExpr );
|
|---|
| 91 | };
|
|---|
| 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 | };
|
|---|
| 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 | };
|
|---|
| 108 | } // namespace
|
|---|
| 109 |
|
|---|
| 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 |
|
|---|
| 116 | void convertLvalue( std::list< Declaration* >& translationUnit ) {
|
|---|
| 117 | PassVisitor<ReferenceConversions> refCvt;
|
|---|
| 118 | PassVisitor<ReferenceTypeElimination> elim;
|
|---|
| 119 | PassVisitor<GeneralizedLvalue> genLval;
|
|---|
| 120 | PassVisitor<FixIntrinsicArgs> fixer;
|
|---|
| 121 | PassVisitor<CollapseAddrDeref> collapser;
|
|---|
| 122 | PassVisitor<AddrRef> addrRef;
|
|---|
| 123 | PassVisitor<FixIntrinsicResult> intrinsicResults;
|
|---|
| 124 | mutateAll( translationUnit, intrinsicResults );
|
|---|
| 125 | mutateAll( translationUnit, addrRef );
|
|---|
| 126 | mutateAll( translationUnit, refCvt );
|
|---|
| 127 | mutateAll( translationUnit, fixer );
|
|---|
| 128 | mutateAll( translationUnit, collapser );
|
|---|
| 129 | mutateAll( translationUnit, genLval );
|
|---|
| 130 | mutateAll( translationUnit, elim ); // last because other passes need reference types to work
|
|---|
| 131 |
|
|---|
| 132 | // from this point forward, no other pass should create reference types.
|
|---|
| 133 | referencesEliminated = true;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | Expression * generalizedLvalue( Expression * expr ) {
|
|---|
| 137 | PassVisitor<GeneralizedLvalue> genLval;
|
|---|
| 138 | return expr->acceptMutator( genLval );
|
|---|
| 139 | }
|
|---|
| 140 |
|
|---|
| 141 | namespace {
|
|---|
| 142 | // true for intrinsic function calls that return a reference
|
|---|
| 143 | bool isIntrinsicReference( Expression * expr ) {
|
|---|
| 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 == "?[?]";
|
|---|
| 148 | } else if ( ApplicationExpr * appExpr = dynamic_cast< ApplicationExpr * > ( expr ) ) {
|
|---|
| 149 | if ( DeclarationWithType * func = InitTweak::getFunction( appExpr ) ) {
|
|---|
| 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() );
|
|---|
| 152 | Type * ret = ftype->get_returnVals().empty() ? nullptr : ftype->get_returnVals().front()->get_type();
|
|---|
| 153 | return func->get_linkage() == LinkageSpec::Intrinsic && dynamic_cast<ReferenceType *>( ret );
|
|---|
| 154 | }
|
|---|
| 155 | }
|
|---|
| 156 | return false;
|
|---|
| 157 | }
|
|---|
| 158 |
|
|---|
| 159 | Expression * FixIntrinsicResult::postmutate( ApplicationExpr * appExpr ) {
|
|---|
| 160 | if ( isIntrinsicReference( appExpr ) ) {
|
|---|
| 161 | // eliminate reference types from intrinsic applications - now they return lvalues
|
|---|
| 162 | Type * result = appExpr->get_result();
|
|---|
| 163 | appExpr->set_result( result->stripReferences()->clone() );
|
|---|
| 164 | appExpr->get_result()->set_lvalue( true );
|
|---|
| 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 );
|
|---|
| 169 | ret->set_env( appExpr->get_env() );
|
|---|
| 170 | appExpr->set_env( nullptr );
|
|---|
| 171 | return ret;
|
|---|
| 172 | }
|
|---|
| 173 | delete result;
|
|---|
| 174 | }
|
|---|
| 175 | return appExpr;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | void FixIntrinsicResult::premutate( FunctionDecl * funcDecl ) {
|
|---|
| 179 | GuardValue( inIntrinsic );
|
|---|
| 180 | inIntrinsic = funcDecl->linkage == LinkageSpec::Intrinsic;
|
|---|
| 181 | }
|
|---|
| 182 |
|
|---|
| 183 | Expression * FixIntrinsicArgs::postmutate( ApplicationExpr * appExpr ) {
|
|---|
| 184 | // intrinsic functions don't really take reference-typed parameters, so they require an implicit dereference on their arguments.
|
|---|
| 185 | if ( DeclarationWithType * function = InitTweak::getFunction( appExpr ) ) {
|
|---|
| 186 | FunctionType * ftype = GenPoly::getFunctionType( function->get_type() );
|
|---|
| 187 | assertf( ftype, "Function declaration does not have function type." );
|
|---|
| 188 | // can be of differing lengths only when function is variadic
|
|---|
| 189 | assertf( ftype->get_parameters().size() == appExpr->get_args().size() || ftype->get_isVarArgs(), "ApplicationExpr args do not match formal parameter type." );
|
|---|
| 190 |
|
|---|
| 191 |
|
|---|
| 192 | unsigned int i = 0;
|
|---|
| 193 | const unsigned int end = ftype->get_parameters().size();
|
|---|
| 194 | for ( auto p : unsafe_group_iterate( appExpr->get_args(), ftype->get_parameters() ) ) {
|
|---|
| 195 | if (i == end) break;
|
|---|
| 196 | Expression *& arg = std::get<0>( p );
|
|---|
| 197 | Type * formal = std::get<1>( p )->get_type();
|
|---|
| 198 | PRINT(
|
|---|
| 199 | std::cerr << "pair<0>: " << arg << std::endl;
|
|---|
| 200 | std::cerr << "pair<1>: " << formal << std::endl;
|
|---|
| 201 | )
|
|---|
| 202 | if ( dynamic_cast<ReferenceType*>( formal ) ) {
|
|---|
| 203 | if ( isIntrinsicReference( arg ) ) { // do not combine conditions, because that changes the meaning of the else if
|
|---|
| 204 | if ( function->get_linkage() != LinkageSpec::Intrinsic ) { // intrinsic functions that turn pointers into references
|
|---|
| 205 | // if argument is dereference or array subscript, the result isn't REALLY a reference, so it's not necessary to fix the argument
|
|---|
| 206 | PRINT(
|
|---|
| 207 | std::cerr << "===is intrinsic arg in non-intrinsic call - adding address" << std::endl;
|
|---|
| 208 | )
|
|---|
| 209 | arg = new AddressExpr( arg );
|
|---|
| 210 | }
|
|---|
| 211 | } else if ( function->get_linkage() == LinkageSpec::Intrinsic ) {
|
|---|
| 212 | // std::cerr << "===adding deref to arg" << std::endl;
|
|---|
| 213 | // if the parameter is a reference, add a dereference to the reference-typed argument.
|
|---|
| 214 | Type * baseType = InitTweak::getPointerBase( arg->get_result() );
|
|---|
| 215 | assertf( baseType, "parameter is reference, arg must be pointer or reference: %s", toString( arg->get_result() ).c_str() );
|
|---|
| 216 | PointerType * ptrType = new PointerType( Type::Qualifiers(), baseType->clone() );
|
|---|
| 217 | delete arg->get_result();
|
|---|
| 218 | arg->set_result( ptrType );
|
|---|
| 219 | arg = mkDeref( arg );
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|
| 222 | ++i;
|
|---|
| 223 | }
|
|---|
| 224 | }
|
|---|
| 225 | return appExpr;
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | // idea: &&&E: get outer &, inner &
|
|---|
| 229 | // at inner &, record depth D of reference type
|
|---|
| 230 | // at outer &, add D derefs.
|
|---|
| 231 | void AddrRef::premutate( Expression * ) {
|
|---|
| 232 | GuardValue( current );
|
|---|
| 233 | GuardValue( first );
|
|---|
| 234 | current = false;
|
|---|
| 235 | first = true;
|
|---|
| 236 | }
|
|---|
| 237 |
|
|---|
| 238 | void AddrRef::premutate( AddressExpr * ) {
|
|---|
| 239 | GuardValue( current );
|
|---|
| 240 | GuardValue( first );
|
|---|
| 241 | current = first;
|
|---|
| 242 | first = false;
|
|---|
| 243 | if ( current ) {
|
|---|
| 244 | GuardValue( refDepth );
|
|---|
| 245 | refDepth = 0;
|
|---|
| 246 | }
|
|---|
| 247 | }
|
|---|
| 248 |
|
|---|
| 249 | Expression * AddrRef::postmutate( AddressExpr * addrExpr ) {
|
|---|
| 250 | if ( refDepth == 0 ) {
|
|---|
| 251 | if ( ! isIntrinsicReference( addrExpr->get_arg() ) ) {
|
|---|
| 252 | // try to avoid ?[?]
|
|---|
| 253 | refDepth = addrExpr->get_arg()->get_result()->referenceDepth();
|
|---|
| 254 | }
|
|---|
| 255 | }
|
|---|
| 256 | if ( current ) {
|
|---|
| 257 | Expression * ret = addrExpr;
|
|---|
| 258 | while ( refDepth ) {
|
|---|
| 259 | ret = mkDeref( ret );
|
|---|
| 260 | refDepth--;
|
|---|
| 261 | }
|
|---|
| 262 | return ret;
|
|---|
| 263 | }
|
|---|
| 264 | return addrExpr;
|
|---|
| 265 | }
|
|---|
| 266 |
|
|---|
| 267 | Expression * ReferenceConversions::postmutate( AddressExpr * addrExpr ) {
|
|---|
| 268 | // Inner expression may have been lvalue to reference conversion, which becomes an address expression.
|
|---|
| 269 | // In this case, remove the outer address expression and return the argument.
|
|---|
| 270 | // TODO: It's possible that this might catch too much and require a more sophisticated check.
|
|---|
| 271 | return addrExpr;
|
|---|
| 272 | }
|
|---|
| 273 |
|
|---|
| 274 | Expression * ReferenceConversions::postmutate( CastExpr * castExpr ) {
|
|---|
| 275 | // xxx - is it possible to convert directly between reference types with a different base? E.g.,
|
|---|
| 276 | // int x;
|
|---|
| 277 | // (double&)x;
|
|---|
| 278 | // At the moment, I am working off of the assumption that this is illegal, thus the cast becomes redundant
|
|---|
| 279 | // after this pass, so trash the cast altogether. If that changes, care must be taken to insert the correct
|
|---|
| 280 | // pointer casts in the right places.
|
|---|
| 281 |
|
|---|
| 282 | // conversion to reference type
|
|---|
| 283 | if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( castExpr->get_result() ) ) {
|
|---|
| 284 | (void)refType;
|
|---|
| 285 | if ( ReferenceType * otherRef = dynamic_cast< ReferenceType * >( castExpr->get_arg()->get_result() ) ) {
|
|---|
| 286 | // nothing to do if casting from reference to reference.
|
|---|
| 287 | (void)otherRef;
|
|---|
| 288 | PRINT( std::cerr << "convert reference to reference -- nop" << std::endl; )
|
|---|
| 289 | if ( isIntrinsicReference( castExpr->get_arg() ) ) {
|
|---|
| 290 | Expression * callExpr = castExpr->get_arg();
|
|---|
| 291 | PRINT(
|
|---|
| 292 | std::cerr << "but arg is deref -- &" << std::endl;
|
|---|
| 293 | std::cerr << callExpr << std::endl;
|
|---|
| 294 | )
|
|---|
| 295 | callExpr = new AddressExpr( callExpr ); // this doesn't work properly for multiple casts
|
|---|
| 296 | delete callExpr->get_result();
|
|---|
| 297 | callExpr->set_result( refType->clone() );
|
|---|
| 298 | // move environment out to new top-level
|
|---|
| 299 | callExpr->set_env( castExpr->get_env() );
|
|---|
| 300 | castExpr->set_arg( nullptr );
|
|---|
| 301 | castExpr->set_env( nullptr );
|
|---|
| 302 | delete castExpr;
|
|---|
| 303 | return callExpr;
|
|---|
| 304 | }
|
|---|
| 305 | int depth1 = refType->referenceDepth();
|
|---|
| 306 | int depth2 = otherRef->referenceDepth();
|
|---|
| 307 | int diff = depth1-depth2;
|
|---|
| 308 | if ( diff == 0 ) {
|
|---|
| 309 | // conversion between references of the same depth
|
|---|
| 310 | assertf( depth1 == depth2, "non-intrinsic reference with cast of reference to reference not yet supported: %d %d %s", depth1, depth2, toString( castExpr ).c_str() );
|
|---|
| 311 | PRINT( std::cerr << castExpr << std::endl; )
|
|---|
| 312 | return castExpr;
|
|---|
| 313 | } else if ( diff < 0 ) {
|
|---|
| 314 | // conversion from reference to reference with less depth (e.g. int && -> int &): add dereferences
|
|---|
| 315 | Expression * ret = castExpr->arg;
|
|---|
| 316 | for ( int i = 0; i < diff; ++i ) {
|
|---|
| 317 | ret = mkDeref( ret );
|
|---|
| 318 | }
|
|---|
| 319 | ret->env = castExpr->env;
|
|---|
| 320 | delete ret->result;
|
|---|
| 321 | ret->result = castExpr->result;
|
|---|
| 322 | ret->result->set_lvalue( true ); // ensure result is lvalue
|
|---|
| 323 | castExpr->env = nullptr;
|
|---|
| 324 | castExpr->arg = nullptr;
|
|---|
| 325 | castExpr->result = nullptr;
|
|---|
| 326 | delete castExpr;
|
|---|
| 327 | return ret;
|
|---|
| 328 | } else if ( diff > 0 ) {
|
|---|
| 329 | // conversion from reference to reference with more depth (e.g. int & -> int &&): add address-of
|
|---|
| 330 | Expression * ret = castExpr->arg;
|
|---|
| 331 | for ( int i = 0; i < diff; ++i ) {
|
|---|
| 332 | ret = new AddressExpr( ret );
|
|---|
| 333 | }
|
|---|
| 334 | ret->env = castExpr->env;
|
|---|
| 335 | delete ret->result;
|
|---|
| 336 | ret->result = castExpr->result;
|
|---|
| 337 | castExpr->env = nullptr;
|
|---|
| 338 | castExpr->arg = nullptr;
|
|---|
| 339 | castExpr->result = nullptr;
|
|---|
| 340 | delete castExpr;
|
|---|
| 341 | return ret;
|
|---|
| 342 | }
|
|---|
| 343 |
|
|---|
| 344 | assertf( depth1 == depth2, "non-intrinsic reference with cast of reference to reference not yet supported: %d %d %s", depth1, depth2, toString( castExpr ).c_str() );
|
|---|
| 345 | PRINT( std::cerr << castExpr << std::endl; )
|
|---|
| 346 | return castExpr;
|
|---|
| 347 | } else if ( castExpr->arg->result->get_lvalue() ) {
|
|---|
| 348 | // conversion from lvalue to reference
|
|---|
| 349 | // xxx - keep cast, but turn into pointer cast??
|
|---|
| 350 | // xxx - memory
|
|---|
| 351 | PRINT(
|
|---|
| 352 | std::cerr << "convert lvalue to reference -- &" << std::endl;
|
|---|
| 353 | std::cerr << castExpr->arg << std::endl;
|
|---|
| 354 | )
|
|---|
| 355 | AddressExpr * ret = new AddressExpr( castExpr->arg );
|
|---|
| 356 | if ( refType->base->get_qualifiers() != castExpr->arg->result->get_qualifiers() ) {
|
|---|
| 357 | // must keep cast if cast-to type is different from the actual type
|
|---|
| 358 | castExpr->arg = ret;
|
|---|
| 359 | return castExpr;
|
|---|
| 360 | }
|
|---|
| 361 | ret->env = castExpr->env;
|
|---|
| 362 | delete ret->result;
|
|---|
| 363 | ret->result = castExpr->result;
|
|---|
| 364 | castExpr->env = nullptr;
|
|---|
| 365 | castExpr->arg = nullptr;
|
|---|
| 366 | castExpr->result = nullptr;
|
|---|
| 367 | delete castExpr;
|
|---|
| 368 | return ret;
|
|---|
| 369 | } else {
|
|---|
| 370 | // rvalue to reference conversion -- introduce temporary
|
|---|
| 371 | }
|
|---|
| 372 | assertf( false, "Only conversions to reference from lvalue are currently supported: %s", toString( castExpr ).c_str() );
|
|---|
| 373 | } else if ( ReferenceType * refType = dynamic_cast< ReferenceType * >( castExpr->arg->result ) ) {
|
|---|
| 374 | (void)refType;
|
|---|
| 375 | // conversion from reference to rvalue
|
|---|
| 376 | PRINT(
|
|---|
| 377 | std::cerr << "convert reference to rvalue -- *" << std::endl;
|
|---|
| 378 | std::cerr << "was = " << castExpr << std::endl;
|
|---|
| 379 | )
|
|---|
| 380 | Expression * ret = castExpr->arg;
|
|---|
| 381 | TypeSubstitution * env = castExpr->env;
|
|---|
| 382 | castExpr->set_env( nullptr );
|
|---|
| 383 | if ( ! isIntrinsicReference( ret ) ) {
|
|---|
| 384 | // dereference if not already dereferenced
|
|---|
| 385 | ret = mkDeref( ret );
|
|---|
| 386 | }
|
|---|
| 387 | if ( ResolvExpr::typesCompatibleIgnoreQualifiers( castExpr->result, castExpr->arg->result->stripReferences(), SymTab::Indexer() ) ) {
|
|---|
| 388 | // can remove cast if types are compatible, changing expression type to value type
|
|---|
| 389 | ret->result = castExpr->result->clone();
|
|---|
| 390 | ret->result->set_lvalue( true ); // ensure result is lvalue
|
|---|
| 391 | castExpr->arg = nullptr;
|
|---|
| 392 | delete castExpr;
|
|---|
| 393 | } else {
|
|---|
| 394 | // must keep cast if types are different
|
|---|
| 395 | castExpr->arg = ret;
|
|---|
| 396 | ret = castExpr;
|
|---|
| 397 | }
|
|---|
| 398 | ret->set_env( env );
|
|---|
| 399 | PRINT( std::cerr << "now: " << ret << std::endl; )
|
|---|
| 400 | return ret;
|
|---|
| 401 | }
|
|---|
| 402 | return castExpr;
|
|---|
| 403 | }
|
|---|
| 404 |
|
|---|
| 405 | Type * ReferenceTypeElimination::postmutate( ReferenceType * refType ) {
|
|---|
| 406 | Type * base = refType->get_base();
|
|---|
| 407 | Type::Qualifiers qualifiers = refType->get_qualifiers();
|
|---|
| 408 | refType->set_base( nullptr );
|
|---|
| 409 | delete refType;
|
|---|
| 410 | return new PointerType( qualifiers, base );
|
|---|
| 411 | }
|
|---|
| 412 |
|
|---|
| 413 | template<typename Func>
|
|---|
| 414 | Expression * GeneralizedLvalue::applyTransformation( Expression * expr, Expression * arg, Func mkExpr ) {
|
|---|
| 415 | if ( CommaExpr * commaExpr = dynamic_cast< CommaExpr * >( arg ) ) {
|
|---|
| 416 | Expression * arg1 = commaExpr->get_arg1()->clone();
|
|---|
| 417 | Expression * arg2 = commaExpr->get_arg2()->clone();
|
|---|
| 418 | Expression * ret = new CommaExpr( arg1, mkExpr( arg2 )->acceptMutator( *visitor ) );
|
|---|
| 419 | ret->set_env( expr->get_env() );
|
|---|
| 420 | expr->set_env( nullptr );
|
|---|
| 421 | delete expr;
|
|---|
| 422 | return ret;
|
|---|
| 423 | } else if ( ConditionalExpr * condExpr = dynamic_cast< ConditionalExpr * >( arg ) ) {
|
|---|
| 424 | Expression * arg1 = condExpr->get_arg1()->clone();
|
|---|
| 425 | Expression * arg2 = condExpr->get_arg2()->clone();
|
|---|
| 426 | Expression * arg3 = condExpr->get_arg3()->clone();
|
|---|
| 427 | ConditionalExpr * ret = new ConditionalExpr( arg1, mkExpr( arg2 )->acceptMutator( *visitor ), mkExpr( arg3 )->acceptMutator( *visitor ) );
|
|---|
| 428 | ret->set_env( expr->get_env() );
|
|---|
| 429 | expr->set_env( nullptr );
|
|---|
| 430 | delete expr;
|
|---|
| 431 |
|
|---|
| 432 | // conditional expr type may not be either of the argument types, need to unify
|
|---|
| 433 | using namespace ResolvExpr;
|
|---|
| 434 | Type* commonType = nullptr;
|
|---|
| 435 | TypeEnvironment newEnv;
|
|---|
| 436 | AssertionSet needAssertions, haveAssertions;
|
|---|
| 437 | OpenVarSet openVars;
|
|---|
| 438 | unify( ret->get_arg2()->get_result(), ret->get_arg3()->get_result(), newEnv, needAssertions, haveAssertions, openVars, SymTab::Indexer(), commonType );
|
|---|
| 439 | ret->set_result( commonType ? commonType : ret->get_arg2()->get_result()->clone() );
|
|---|
| 440 | return ret;
|
|---|
| 441 | }
|
|---|
| 442 | return expr;
|
|---|
| 443 | }
|
|---|
| 444 |
|
|---|
| 445 | Expression * GeneralizedLvalue::postmutate( MemberExpr * memExpr ) {
|
|---|
| 446 | return applyTransformation( memExpr, memExpr->get_aggregate(), [=]( Expression * aggr ) { return new MemberExpr( memExpr->get_member(), aggr ); } );
|
|---|
| 447 | }
|
|---|
| 448 |
|
|---|
| 449 | Expression * GeneralizedLvalue::postmutate( AddressExpr * addrExpr ) {
|
|---|
| 450 | return applyTransformation( addrExpr, addrExpr->get_arg(), []( Expression * arg ) { return new AddressExpr( arg ); } );
|
|---|
| 451 | }
|
|---|
| 452 |
|
|---|
| 453 | Expression * CollapseAddrDeref::postmutate( AddressExpr * addrExpr ) {
|
|---|
| 454 | Expression * arg = addrExpr->get_arg();
|
|---|
| 455 | if ( isIntrinsicReference( arg ) ) {
|
|---|
| 456 | std::string fname = InitTweak::getFunctionName( arg );
|
|---|
| 457 | if ( fname == "*?" ) {
|
|---|
| 458 | Expression *& arg0 = InitTweak::getCallArg( arg, 0 );
|
|---|
| 459 | Expression * ret = arg0;
|
|---|
| 460 | ret->set_env( addrExpr->get_env() );
|
|---|
| 461 | arg0 = nullptr;
|
|---|
| 462 | addrExpr->set_env( nullptr );
|
|---|
| 463 | delete addrExpr;
|
|---|
| 464 | return ret;
|
|---|
| 465 | }
|
|---|
| 466 | } else if ( CastExpr * castExpr = dynamic_cast< CastExpr * > ( arg ) ) {
|
|---|
| 467 | // need to move cast to pointer type out a level since address of pointer
|
|---|
| 468 | // is not valid C code (can be introduced in prior passes, e.g., InstantiateGeneric)
|
|---|
| 469 | if ( InitTweak::getPointerBase( castExpr->result ) ) {
|
|---|
| 470 | addrExpr->arg = castExpr->arg;
|
|---|
| 471 | castExpr->arg = addrExpr;
|
|---|
| 472 | castExpr->result = new PointerType( Type::Qualifiers(), castExpr->result );
|
|---|
| 473 | return castExpr;
|
|---|
| 474 | }
|
|---|
| 475 | }
|
|---|
| 476 | return addrExpr;
|
|---|
| 477 | }
|
|---|
| 478 |
|
|---|
| 479 | Expression * CollapseAddrDeref::postmutate( ApplicationExpr * appExpr ) {
|
|---|
| 480 | if ( isIntrinsicReference( appExpr ) ) {
|
|---|
| 481 | std::string fname = InitTweak::getFunctionName( appExpr );
|
|---|
| 482 | if ( fname == "*?" ) {
|
|---|
| 483 | Expression * arg = InitTweak::getCallArg( appExpr, 0 );
|
|---|
| 484 | // xxx - this isn't right, because it can remove casts that should be there...
|
|---|
| 485 | // while ( CastExpr * castExpr = dynamic_cast< CastExpr * >( arg ) ) {
|
|---|
| 486 | // arg = castExpr->get_arg();
|
|---|
| 487 | // }
|
|---|
| 488 | if ( AddressExpr * addrExpr = dynamic_cast< AddressExpr * >( arg ) ) {
|
|---|
| 489 | Expression * ret = addrExpr->get_arg();
|
|---|
| 490 | ret->set_env( appExpr->get_env() );
|
|---|
| 491 | addrExpr->set_arg( nullptr );
|
|---|
| 492 | appExpr->set_env( nullptr );
|
|---|
| 493 | delete appExpr;
|
|---|
| 494 | return ret;
|
|---|
| 495 | }
|
|---|
| 496 | }
|
|---|
| 497 | }
|
|---|
| 498 | return appExpr;
|
|---|
| 499 | }
|
|---|
| 500 | } // namespace
|
|---|
| 501 | } // namespace GenPoly
|
|---|
| 502 |
|
|---|
| 503 | // Local Variables: //
|
|---|
| 504 | // tab-width: 4 //
|
|---|
| 505 | // mode: c++ //
|
|---|
| 506 | // compile-command: "make install" //
|
|---|
| 507 | // End: //
|
|---|