[7fffb1b] | 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 | // LvalueNew.cpp -- Clean up lvalues and remove references. |
---|
| 8 | // |
---|
| 9 | // Author : Andrew Beach |
---|
| 10 | // Created On : Thu Sep 15 14:08:00 2022 |
---|
| 11 | // Last Modified By : Andrew Beach |
---|
| 12 | // Last Modified On : Wed Oct 6 9:59:00 2022 |
---|
| 13 | // Update Count : 0 |
---|
| 14 | // |
---|
| 15 | |
---|
| 16 | #include "Lvalue.h" |
---|
| 17 | |
---|
| 18 | #include <set> |
---|
| 19 | #include <iostream> |
---|
| 20 | |
---|
| 21 | #include "AST/Copy.hpp" // for deepCopy |
---|
| 22 | #include "AST/Expr.hpp" |
---|
| 23 | #include "AST/Inspect.hpp" |
---|
| 24 | #include "AST/LinkageSpec.hpp" // for Linkage |
---|
| 25 | #include "AST/Pass.hpp" |
---|
| 26 | #include "Common/SemanticError.h" // for SemanticWarning |
---|
[9feb34b] | 27 | #include "Common/ToString.hpp" // for toCString |
---|
[7fffb1b] | 28 | #include "Common/UniqueName.h" // for UniqueName |
---|
| 29 | #include "GenPoly/GenPoly.h" // for genFunctionType |
---|
| 30 | #include "ResolvExpr/typeops.h" // for typesCompatible |
---|
| 31 | #include "ResolvExpr/Unify.h" // for unify |
---|
| 32 | |
---|
| 33 | #if 0 |
---|
| 34 | #define PRINT(x) x |
---|
| 35 | #else |
---|
| 36 | #define PRINT(x) |
---|
| 37 | #endif |
---|
| 38 | |
---|
| 39 | namespace GenPoly { |
---|
| 40 | |
---|
| 41 | namespace { |
---|
| 42 | |
---|
| 43 | /// Intrinsic functions that return references now instead return lvalues. |
---|
| 44 | struct FixIntrinsicResults final : public ast::WithGuards { |
---|
| 45 | enum { |
---|
| 46 | NoSkip, |
---|
| 47 | Skip, |
---|
| 48 | SkipInProgress, |
---|
| 49 | } skip = NoSkip; |
---|
| 50 | |
---|
| 51 | void previsit( ast::AsmExpr const * ) { |
---|
| 52 | GuardValue( skip ) = Skip; |
---|
| 53 | } |
---|
| 54 | void previsit( ast::ApplicationExpr const * ) { |
---|
| 55 | GuardValue( skip ) = (skip == Skip) ? SkipInProgress : NoSkip; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | ast::Expr const * postvisit( ast::ApplicationExpr const * expr ); |
---|
| 59 | void previsit( ast::FunctionDecl const * decl ); |
---|
| 60 | bool inIntrinsic = false; |
---|
| 61 | }; |
---|
| 62 | |
---|
| 63 | /// Add de-references around address-of operations on reference types. |
---|
| 64 | struct AddressRef final : |
---|
| 65 | public ast::WithConstTranslationUnit, |
---|
| 66 | public ast::WithGuards, |
---|
| 67 | public ast::WithShortCircuiting, |
---|
| 68 | public ast::WithVisitorRef<AddressRef> { |
---|
| 69 | void previsit( ast::AddressExpr const * expr ); |
---|
| 70 | ast::Expr const * postvisit( ast::AddressExpr const * expr ); |
---|
| 71 | void previsit( ast::Expr const * expr ); |
---|
| 72 | ast::ApplicationExpr const * previsit( ast::ApplicationExpr const * expr ); |
---|
| 73 | void previsit( ast::SingleInit const * init ); |
---|
| 74 | |
---|
| 75 | void handleNonAddr( ast::Expr const * expr ); |
---|
| 76 | |
---|
| 77 | bool first = true; |
---|
| 78 | bool current = false; |
---|
| 79 | bool addCast = false; |
---|
| 80 | int refDepth = 0; |
---|
| 81 | }; |
---|
| 82 | |
---|
| 83 | /// Handles casts between references and pointers, |
---|
| 84 | /// creating temporaries for the conversion. |
---|
| 85 | struct ReferenceConversions final : |
---|
| 86 | public ast::WithConstTranslationUnit, |
---|
| 87 | public ast::WithGuards, public ast::WithStmtsToAdd<> { |
---|
| 88 | ast::Expr const * postvisit( ast::CastExpr const * expr ); |
---|
| 89 | ast::Expr const * postvisit( ast::AddressExpr const * expr ); |
---|
| 90 | }; |
---|
| 91 | |
---|
| 92 | /// Intrinsic functions that take reference parameters don't actually do. |
---|
| 93 | /// Their reference arguments must be implicity dereferenced. |
---|
| 94 | /// TODO Also appears to contain redundent code with AddressRef |
---|
| 95 | struct FixIntrinsicArgs final : |
---|
| 96 | public ast::WithConstTranslationUnit { |
---|
| 97 | ast::Expr const * postvisit( ast::ApplicationExpr const * expr ); |
---|
| 98 | }; |
---|
| 99 | |
---|
| 100 | /// Removes redundant &* / *& patterns that may be generated. |
---|
| 101 | struct CollapseAddressDeref final { |
---|
| 102 | ast::Expr const * postvisit( ast::AddressExpr const * expr ); |
---|
| 103 | ast::Expr const * postvisit( ast::ApplicationExpr const * expr ); |
---|
| 104 | }; |
---|
| 105 | |
---|
| 106 | /// GCC-like Generalized Lvalues (which have since been removed from GCC). |
---|
| 107 | /// https://gcc.gnu.org/onlinedocs/gcc-3.4.6/gcc/Lvalues.html#Lvalues |
---|
| 108 | /// Replaces &(a,b) with (a, &b), &(a ? b : c) with (a ? &b : &c) |
---|
| 109 | struct GeneralizedLvalue final : |
---|
| 110 | public ast::WithVisitorRef<GeneralizedLvalue> { |
---|
| 111 | ast::Expr const * postvisit( ast::AddressExpr const * expr ); |
---|
| 112 | ast::Expr const * postvisit( ast::MemberExpr const * expr ); |
---|
| 113 | |
---|
| 114 | template<typename Node, typename Func> |
---|
| 115 | ast::Expr const * applyTransformation( |
---|
| 116 | Node const * expr, ast::ptr<ast::Expr> Node::*field, Func mkExpr ); |
---|
| 117 | }; |
---|
| 118 | |
---|
| 119 | /// Replace all reference types with pointer types. |
---|
| 120 | struct ReferenceTypeElimination final { |
---|
| 121 | ast::Type const * postvisit( ast::ReferenceType const * type ); |
---|
| 122 | }; |
---|
| 123 | |
---|
| 124 | /// True for intrinsic function calls that return an lvalue in C. |
---|
| 125 | bool isIntrinsicReference( ast::Expr const * expr ) { |
---|
| 126 | // The known intrinsic-reference prelude functions. |
---|
| 127 | static std::set<std::string> const lvalueFunctions = { "*?", "?[?]" }; |
---|
| 128 | if ( auto untyped = dynamic_cast<ast::UntypedExpr const *>( expr ) ) { |
---|
| 129 | std::string fname = ast::getFunctionName( untyped ); |
---|
| 130 | return lvalueFunctions.count( fname ); |
---|
| 131 | } else if ( auto app = dynamic_cast<ast::ApplicationExpr const *>( expr ) ) { |
---|
| 132 | if ( auto func = ast::getFunction( app ) ) { |
---|
| 133 | return func->linkage == ast::Linkage::Intrinsic |
---|
| 134 | && lvalueFunctions.count( func->name ); |
---|
| 135 | } |
---|
| 136 | } |
---|
| 137 | return false; |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | // A maybe typed variant of the createDeref function (only UntypedExpr). |
---|
| 141 | ast::Expr * mkDeref( |
---|
| 142 | ast::TranslationGlobal const & global, ast::Expr const * arg ) { |
---|
| 143 | if ( global.dereference ) { |
---|
| 144 | // Note: Reference depth can be arbitrarily deep here, |
---|
| 145 | // so peel off the outermost pointer/reference, not just |
---|
| 146 | // pointer because they are effecitvely equivalent in this pass |
---|
| 147 | ast::VariableExpr * deref = new ast::VariableExpr( |
---|
| 148 | arg->location, global.dereference ); |
---|
| 149 | deref->result = new ast::PointerType( deref->result ); |
---|
| 150 | ast::Type const * base = ast::getPointerBase( arg->result ); |
---|
| 151 | assertf( base, "expected pointer type in dereference (type was %s)", toString( arg->result ).c_str() ); |
---|
| 152 | ast::ApplicationExpr * ret = |
---|
| 153 | new ast::ApplicationExpr( arg->location, deref, { arg } ); |
---|
| 154 | ret->result = ast::deepCopy( base ); |
---|
| 155 | return ret; |
---|
| 156 | } else { |
---|
| 157 | return ast::UntypedExpr::createDeref( arg->location, arg ); |
---|
| 158 | } |
---|
| 159 | } |
---|
| 160 | |
---|
| 161 | ast::Expr const * FixIntrinsicResults::postvisit( |
---|
| 162 | ast::ApplicationExpr const * expr ) { |
---|
| 163 | if ( skip == SkipInProgress || !isIntrinsicReference( expr ) ) { |
---|
| 164 | return expr; |
---|
| 165 | } |
---|
| 166 | // Eliminate reference types from intrinsic applications |
---|
| 167 | // now they return lvalues. |
---|
| 168 | ast::ptr<ast::ReferenceType> result = |
---|
| 169 | expr->result.strict_as<ast::ReferenceType>(); |
---|
| 170 | expr = ast::mutate_field( expr, &ast::ApplicationExpr::result, |
---|
| 171 | ast::deepCopy( result->base ) ); |
---|
| 172 | if ( inIntrinsic ) { |
---|
| 173 | return expr; |
---|
| 174 | } |
---|
| 175 | // When not in an intrinsic function, add a cast to don't add cast when |
---|
| 176 | // in an intrinsic function, since they already have the cast. |
---|
| 177 | auto * ret = new ast::CastExpr( expr->location, expr, result.get() ); |
---|
| 178 | ret->env = expr->env; |
---|
| 179 | return ret; |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | void FixIntrinsicResults::previsit( ast::FunctionDecl const * decl ) { |
---|
| 183 | GuardValue( inIntrinsic ) = decl->linkage == ast::Linkage::Intrinsic; |
---|
| 184 | } |
---|
| 185 | |
---|
| 186 | void AddressRef::previsit( ast::AddressExpr const * ) { |
---|
| 187 | // Is this the first address-of in the chain? |
---|
| 188 | GuardValue( current ) = first; |
---|
| 189 | // Later references will not be for next address-of to be first in chain. |
---|
| 190 | GuardValue( first ) = false; |
---|
| 191 | // If is the outermost address-of in a chain: |
---|
| 192 | if ( current ) { |
---|
| 193 | // Set depth to 0 so that postvisit can |
---|
| 194 | // find the innermost address-of easily. |
---|
| 195 | GuardValue( refDepth ) = 0; |
---|
| 196 | } |
---|
| 197 | } |
---|
| 198 | |
---|
| 199 | ast::Expr const * AddressRef::postvisit( ast::AddressExpr const * expr ) { |
---|
| 200 | PRINT( std::cerr << "addr ref at " << expr << std::endl; ) |
---|
| 201 | if ( 0 == refDepth ) { |
---|
| 202 | PRINT( std::cerr << "depth 0, get new depth..." << std::endl; ) |
---|
| 203 | // Is this the innermost address-of in a chain? record depth D. |
---|
| 204 | if ( isIntrinsicReference( expr->arg ) ) { |
---|
| 205 | assertf( false, "AddrRef : address-of should not have intrinsic reference argument: %s", toCString( expr->arg ) ); |
---|
| 206 | } else { |
---|
| 207 | // try to avoid ?[?] |
---|
| 208 | // TODO is this condition still necessary? intrinsicReferences |
---|
| 209 | // should have a cast around them at this point, so I don't think |
---|
| 210 | // this condition ever fires. |
---|
| 211 | refDepth = expr->arg->result->referenceDepth(); |
---|
| 212 | PRINT( std::cerr << "arg not intrinsic reference, new depth is: " << refDepth << std::endl; ) |
---|
| 213 | } |
---|
| 214 | } |
---|
| 215 | if ( current ) { |
---|
| 216 | PRINT( std::cerr << "current, depth is: " << refDepth << std::endl; ) |
---|
| 217 | ast::Expr const * ret = expr; |
---|
| 218 | while ( refDepth ) { |
---|
| 219 | // Add one dereference for each address-of in the chain. |
---|
| 220 | ret = mkDeref( transUnit().global, ret ); |
---|
| 221 | --refDepth; |
---|
| 222 | } |
---|
| 223 | |
---|
| 224 | // if addrExpr depth is 0, then the result is a pointer because the |
---|
| 225 | // arg was depth 1 and not lvalue. This means the dereference result |
---|
| 226 | // is not a reference, is lvalue, and one less pointer depth than the |
---|
| 227 | // addrExpr. Thus the cast is meaningless. |
---|
| 228 | // TODO: One thing to double check is whether it is possible for the |
---|
| 229 | // types to differ outside of the single pointer level (i.e. can the |
---|
| 230 | // base type of addrExpr differ from the type of addrExpr-arg?). If |
---|
| 231 | // so then the cast might need to be added, conditional on a more |
---|
| 232 | // sophisticated check. |
---|
| 233 | if ( addCast && 0 != expr->result->referenceDepth() ) { |
---|
| 234 | PRINT( std::cerr << "adding cast to " << expr->result << std::endl; ) |
---|
| 235 | return new ast::CastExpr( expr->location, |
---|
| 236 | ret, ast::deepCopy( expr->result ) ); |
---|
| 237 | } |
---|
| 238 | return ret; |
---|
| 239 | } |
---|
| 240 | PRINT( std::cerr << "not current..." << std::endl; ) |
---|
| 241 | return expr; |
---|
| 242 | } |
---|
| 243 | |
---|
| 244 | void AddressRef::previsit( ast::Expr const * expr ) { |
---|
| 245 | handleNonAddr( expr ); |
---|
| 246 | GuardValue( addCast ) = false; |
---|
| 247 | } |
---|
| 248 | |
---|
| 249 | // So we want to skip traversing to the head? |
---|
| 250 | ast::ApplicationExpr const * AddressRef::previsit( |
---|
| 251 | ast::ApplicationExpr const * expr ) { |
---|
| 252 | visit_children = false; |
---|
| 253 | GuardValue( addCast ); |
---|
| 254 | handleNonAddr( expr ); |
---|
| 255 | auto mutExpr = ast::mutate( expr ); |
---|
| 256 | for ( ast::ptr<ast::Expr> & arg : mutExpr->args ) { |
---|
| 257 | addCast = true; |
---|
| 258 | arg = arg->accept( *visitor ); |
---|
| 259 | } |
---|
| 260 | return mutExpr; |
---|
| 261 | } |
---|
| 262 | |
---|
| 263 | void AddressRef::previsit( ast::SingleInit const * ) { |
---|
| 264 | // Each initialization context with address-of requires a cast. |
---|
| 265 | GuardValue( addCast ) = true; |
---|
| 266 | } |
---|
| 267 | |
---|
| 268 | // idea: &&&E: get outer &, inner & |
---|
| 269 | // at inner &, record depth D of reference type of argument of &. |
---|
| 270 | // at auter &, add D derefs. |
---|
| 271 | void AddressRef::handleNonAddr( ast::Expr const * ) { |
---|
| 272 | // non-address-of: reset status variables: |
---|
| 273 | // * current expr is NOT the first address-of expr in an address-of chain. |
---|
| 274 | // * next seen address-of expr IS the first in the chain. |
---|
| 275 | GuardValue( current ) = false; |
---|
| 276 | GuardValue( first ) = true; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | ast::Expr const * ReferenceConversions::postvisit( |
---|
| 280 | ast::CastExpr const * expr ) { |
---|
| 281 | // TODO: Is it possible to convert directly between reference types with |
---|
| 282 | // a different base. e.g. |
---|
| 283 | // int x; |
---|
| 284 | // (double&)x; |
---|
| 285 | // At the moment, I (who?) am working off of the assumption that this is |
---|
| 286 | // illegal, thus the cast becomes redundant after this pass, so trash the |
---|
| 287 | // cast altogether. If that changes, care must be taken to insert the |
---|
| 288 | // correct pointer casts in the right places. |
---|
| 289 | |
---|
| 290 | // Note: reference depth difference is the determining factor in what |
---|
| 291 | // code is run, rather than whether something is reference type or not, |
---|
| 292 | // since conversion still needs to occur when both types are references |
---|
| 293 | // that differ in depth. |
---|
| 294 | ast::Type const * dstType = expr->result.get(); |
---|
| 295 | ast::Type const * srcType = expr->arg->result.get(); |
---|
| 296 | assertf( dstType, "Cast to no type in: %s", toCString( expr ) ); |
---|
| 297 | assertf( srcType, "Cast from no type in: %s", toCString( expr ) ); |
---|
| 298 | int dstDepth = dstType->referenceDepth(); |
---|
| 299 | int srcDepth = srcType->referenceDepth(); |
---|
| 300 | int diff = dstDepth - srcDepth; |
---|
| 301 | |
---|
| 302 | if ( 0 < diff && !expr->arg->get_lvalue() ) { |
---|
| 303 | // rvalue to reference conversion -- introduce temporary |
---|
| 304 | // know that reference depth of cast argument is 0 |
---|
| 305 | // (int &&&)3; |
---|
| 306 | // becomes |
---|
| 307 | // int __ref_tmp_0 = 3; |
---|
| 308 | // int & __ref_tmp_1 = &__ref_tmp_0; |
---|
| 309 | // int && __ref_tmp_2 = &__ref_tmp_1; |
---|
| 310 | // &__ref_tmp_2; |
---|
| 311 | // The last & comes from the remaining reference conversion code. |
---|
| 312 | SemanticWarning( expr->arg->location, |
---|
| 313 | Warning::RvalueToReferenceConversion, toCString( expr->arg ) ); |
---|
| 314 | |
---|
| 315 | static UniqueName tmpNamer( "__ref_tmp_" ); |
---|
| 316 | ast::ObjectDecl * tmp = new ast::ObjectDecl( expr->arg->location, |
---|
| 317 | tmpNamer.newName(), |
---|
| 318 | ast::deepCopy( expr->arg->result ), |
---|
| 319 | new ast::SingleInit( expr->arg->location, expr->arg ) ); |
---|
| 320 | PRINT( std::cerr << "make tmp: " << tmp << std::endl; ) |
---|
| 321 | stmtsToAddBefore.push_back( new ast::DeclStmt( tmp->location, tmp ) ); |
---|
| 322 | for ( int i = 0 ; i < dstDepth - 1 ; ++i ) { |
---|
| 323 | ast::ObjectDecl * newTmp = new ast::ObjectDecl( tmp->location, |
---|
| 324 | tmpNamer.newName(), |
---|
| 325 | new ast::ReferenceType( ast::deepCopy( tmp->type ) ), |
---|
| 326 | new ast::SingleInit( tmp->location, |
---|
| 327 | new ast::AddressExpr( tmp->location, |
---|
| 328 | new ast::VariableExpr( tmp->location, tmp ) ) ) ); |
---|
| 329 | PRINT( std::cerr << "make tmp: " << i << ": " << newTmp << std::endl; ) |
---|
| 330 | stmtsToAddBefore.push_back( |
---|
| 331 | new ast::DeclStmt( newTmp->location, newTmp ) ); |
---|
| 332 | tmp = newTmp; |
---|
| 333 | } |
---|
| 334 | // Update diff so that remaining code works out correctly. |
---|
| 335 | expr = ast::mutate_field( expr, &ast::CastExpr::arg, |
---|
| 336 | new ast::VariableExpr( tmp->location, tmp ) ); |
---|
| 337 | PRINT( std::cerr << "update cast to: " << expr << std::endl; ) |
---|
| 338 | srcType = expr->arg->result; |
---|
| 339 | srcDepth = srcType->referenceDepth(); |
---|
| 340 | diff = dstDepth - srcDepth; |
---|
| 341 | assert( 1 == diff ); |
---|
| 342 | } |
---|
| 343 | |
---|
| 344 | // Handle conversion between different depths. |
---|
| 345 | PRINT( |
---|
| 346 | if ( dstDepth || srcDepth ) { |
---|
| 347 | std::cerr << "dstType: " << dstType << " / srcType: " << srcType << '\n'; |
---|
| 348 | std::cerr << "depth: " << dstDepth << " / " << srcDepth << std::endl; |
---|
| 349 | } |
---|
| 350 | ) |
---|
| 351 | // Conversion to type with more depth/more references. |
---|
| 352 | // Add address-of for each level of difference. |
---|
| 353 | if ( 0 < diff ) { |
---|
| 354 | ast::Expr * ret = ast::mutate( expr->arg.get() ); |
---|
| 355 | for ( int i = 0 ; i < diff ; ++i ) { |
---|
| 356 | ret = new ast::AddressExpr( ret->location, ret ); |
---|
| 357 | } |
---|
| 358 | if ( expr->arg->get_lvalue() && |
---|
| 359 | !ResolvExpr::typesCompatible( |
---|
| 360 | srcType, |
---|
[251ce80] | 361 | strict_dynamic_cast<ast::ReferenceType const *>( dstType )->base ) ) { |
---|
[7fffb1b] | 362 | // Must keep cast if cast-to type is different from the actual type. |
---|
| 363 | return ast::mutate_field( expr, &ast::CastExpr::arg, ret ); |
---|
| 364 | } |
---|
| 365 | ret->env = expr->env; |
---|
| 366 | ret->result = expr->result; |
---|
| 367 | return ret; |
---|
| 368 | // Conversion to type with less depth/fewer references. |
---|
| 369 | // Add dereferences for each level of difference. |
---|
| 370 | } else if ( diff < 0 ) { |
---|
| 371 | ast::Expr * ret = ast::mutate( expr->arg.get() ); |
---|
| 372 | for ( int i = 0 ; i < -diff ; ++i ) { |
---|
| 373 | ret = mkDeref( transUnit().global, ret ); |
---|
| 374 | } |
---|
| 375 | // Must keep cast if types are different. |
---|
| 376 | if ( !ResolvExpr::typesCompatibleIgnoreQualifiers( |
---|
| 377 | dstType->stripReferences(), |
---|
[251ce80] | 378 | srcType->stripReferences() ) ) { |
---|
[7fffb1b] | 379 | return ast::mutate_field( expr, &ast::CastExpr::arg, ret ); |
---|
| 380 | } |
---|
| 381 | ret->env = expr->env; |
---|
| 382 | ret->result = expr->result; |
---|
| 383 | // The result must be an lvalue. |
---|
| 384 | assert( ret->get_lvalue() ); |
---|
| 385 | return ret; |
---|
| 386 | // Conversion with the same depth. |
---|
| 387 | } else { |
---|
| 388 | assert( 0 == diff ); |
---|
| 389 | // Remove useless generated casts. |
---|
| 390 | if ( expr->isGenerated && |
---|
| 391 | ResolvExpr::typesCompatible( |
---|
| 392 | expr->result, |
---|
[251ce80] | 393 | expr->arg->result ) ) { |
---|
[7fffb1b] | 394 | PRINT( |
---|
| 395 | std::cerr << "types are compatible, removing cast: " << expr << '\n'; |
---|
| 396 | std::cerr << "-- " << expr->result << '\n'; |
---|
| 397 | std::cerr << "-- " << expr->arg->result << std::endl; |
---|
| 398 | ) |
---|
| 399 | return ast::mutate_field( expr->arg.get(), |
---|
| 400 | &ast::Expr::env, expr->env.get() ); |
---|
| 401 | } |
---|
| 402 | return expr; |
---|
| 403 | } |
---|
| 404 | } |
---|
| 405 | |
---|
| 406 | ast::Expr const * ReferenceConversions::postvisit( |
---|
| 407 | ast::AddressExpr const * expr ) { |
---|
| 408 | // Inner expression may have been lvalue to reference conversion, which |
---|
| 409 | // becomes an address expression. In this case, remove the outer address |
---|
| 410 | // expression and return the argument. |
---|
| 411 | // TODO: It's possible that this might catch too much and require a more |
---|
| 412 | // sophisticated check. TODO What check are we talking about here? |
---|
| 413 | return expr; |
---|
| 414 | } |
---|
| 415 | |
---|
| 416 | ast::Expr const * FixIntrinsicArgs::postvisit( |
---|
| 417 | ast::ApplicationExpr const * expr ) { |
---|
| 418 | // Intrinsic functions don't really take reference-typed parameters, |
---|
| 419 | // so they require an implicit dereference on their arguments. |
---|
| 420 | auto function = ast::getFunction( expr ); |
---|
| 421 | if ( function == nullptr ) { |
---|
| 422 | return expr; |
---|
| 423 | } |
---|
| 424 | |
---|
| 425 | ast::FunctionType const * ftype = GenPoly::getFunctionType( function->get_type() ); |
---|
| 426 | assertf( ftype, "Function declaration does not have function type." ); |
---|
| 427 | // Can be of different lengths only when function is variadic. |
---|
| 428 | assertf( ftype->params.size() == expr->args.size() || ftype->isVarArgs, |
---|
| 429 | "ApplicationExpr args do not match formal parameter type." ); |
---|
| 430 | assertf( ftype->params.size() <= expr->args.size(), |
---|
| 431 | "Cannot have more parameters than arguments." ); |
---|
| 432 | |
---|
| 433 | unsigned int i = 0; |
---|
| 434 | unsigned int const end = ftype->params.size(); |
---|
| 435 | |
---|
| 436 | // This is used to make sure we get a zip on shortests. |
---|
| 437 | if ( end == i ) return expr; |
---|
| 438 | |
---|
| 439 | // This mutate could be redundent, but it is simpler this way. |
---|
| 440 | auto mutExpr = ast::mutate( expr ); |
---|
| 441 | |
---|
| 442 | for ( auto pair : unsafe_group_iterate( mutExpr->args, ftype->params ) ) { |
---|
| 443 | ast::ptr<ast::Expr> & arg = std::get<0>( pair ); |
---|
| 444 | ast::ptr<ast::Type> const & formal = std::get<1>( pair ); |
---|
| 445 | PRINT( |
---|
| 446 | std::cerr << "pair<0>: " << arg.get() << std::endl; |
---|
| 447 | std::cerr << " -- " << arg->result << std::endl; |
---|
| 448 | std::cerr << "pair<1>: " << formal << std::endl; |
---|
| 449 | ) |
---|
| 450 | //if ( dynamic_cast<ast::ReferenceType const *>( formal.get() ) ) { |
---|
| 451 | if ( formal.as<ast::ReferenceType>() ) { |
---|
| 452 | PRINT( std::cerr << "===formal is reference" << std::endl; ) |
---|
| 453 | // TODO: It's likely that the second condition should be |
---|
| 454 | // `... && ! isIntrinsicReference( arg )`, but this requires |
---|
| 455 | // investigation. |
---|
| 456 | |
---|
| 457 | if ( ast::Linkage::Intrinsic != function->linkage |
---|
| 458 | && isIntrinsicReference( arg ) ) { |
---|
| 459 | // Needed for definition of prelude functions, etc. |
---|
| 460 | // If argument is dereference or array subscript, the result |
---|
| 461 | // isn't REALLY a reference, but non-intrinsic functions |
---|
| 462 | // expect a reference: take address |
---|
| 463 | |
---|
| 464 | // TODO: OK, so this should be cut?! |
---|
| 465 | // NODE: Previously, this condition fixed |
---|
| 466 | // void f(int *&); |
---|
| 467 | // int & x = ...; |
---|
| 468 | // f(&x); |
---|
| 469 | // But now this is taken care of by a reference cast added by |
---|
| 470 | // AddressRef. Need to find a new example or remove this |
---|
| 471 | // branch. |
---|
| 472 | PRINT( |
---|
| 473 | std::cerr << "===is intrinsic arg in non-intrinsic call - adding address" << std::endl; |
---|
| 474 | ) |
---|
| 475 | arg = new ast::AddressExpr( arg->location, arg ); |
---|
| 476 | } else if ( ast::Linkage::Intrinsic == function->linkage |
---|
| 477 | && arg->result->referenceDepth() != 0 ) { |
---|
| 478 | // Argument is a 'real' reference, but function expects a C |
---|
| 479 | // lvalue: Add a dereference to the reference-typed argument. |
---|
| 480 | PRINT( |
---|
| 481 | std::cerr << "===is non-intrinsic arg in intrinsic call - adding deref to arg" << std::endl; |
---|
| 482 | ) |
---|
| 483 | ast::Type const * base = ast::getPointerBase( arg->result ); |
---|
| 484 | assertf( base, "parameter is reference, arg must be pointer or reference: %s", toString( arg->result ).c_str() ); |
---|
| 485 | ast::PointerType * ptr = new ast::PointerType( ast::deepCopy( base ) ); |
---|
| 486 | arg = ast::mutate_field( arg.get(), |
---|
| 487 | &ast::ApplicationExpr::result, ptr ); |
---|
| 488 | arg = mkDeref( transUnit().global, arg ); |
---|
| 489 | } |
---|
| 490 | } |
---|
| 491 | ++i; |
---|
| 492 | if ( end == i ) break; |
---|
| 493 | } |
---|
| 494 | return mutExpr; |
---|
| 495 | } |
---|
| 496 | |
---|
| 497 | ast::Expr const * CollapseAddressDeref::postvisit( |
---|
| 498 | ast::AddressExpr const * expr ) { |
---|
| 499 | ast::Expr const * arg = expr->arg; |
---|
| 500 | if ( isIntrinsicReference( arg ) ) { |
---|
| 501 | std::string fname = ast::getFunctionName( arg ); |
---|
| 502 | if ( fname == "*?" ) { |
---|
| 503 | ast::Expr const * arg0 = ast::getCallArg( arg, 0 ); |
---|
| 504 | ast::Expr * ret = ast::mutate( arg0 ); |
---|
| 505 | ret->env = expr->env; |
---|
| 506 | return ret; |
---|
| 507 | } |
---|
| 508 | } else if ( auto cast = dynamic_cast<ast::CastExpr const *>( arg ) ) { |
---|
| 509 | // Need to move cast to pointer type out a level since address of |
---|
| 510 | // pointer is not valid C code (can be introduced in prior passes, |
---|
| 511 | // e.g., InstantiateGeneric) |
---|
| 512 | if ( ast::getPointerBase( cast->result ) ) { |
---|
| 513 | auto mutExpr = ast::mutate( expr ); |
---|
| 514 | auto mutCast = strict_dynamic_cast<ast::CastExpr *>( |
---|
| 515 | ast::mutate( mutExpr->arg.release() ) ); |
---|
| 516 | mutExpr->arg = mutCast->arg; |
---|
| 517 | mutCast->arg = mutExpr; |
---|
| 518 | mutCast->result = new ast::PointerType( mutCast->result ); |
---|
| 519 | return mutCast; |
---|
| 520 | } |
---|
| 521 | } |
---|
| 522 | return expr; |
---|
| 523 | } |
---|
| 524 | |
---|
| 525 | ast::Expr const * CollapseAddressDeref::postvisit( |
---|
| 526 | ast::ApplicationExpr const * expr ) { |
---|
| 527 | if ( isIntrinsicReference( expr ) ) { |
---|
| 528 | std::string fname = ast::getFunctionName( expr ); |
---|
| 529 | if ( fname == "*?" ) { |
---|
| 530 | assert( 1 == expr->args.size() ); |
---|
| 531 | ast::Expr const * arg = ast::getCallArg( expr, 0 ); |
---|
| 532 | // xxx - this isn't right, because it can remove casts that |
---|
| 533 | // should be there... |
---|
| 534 | // while ( auto cast = dynamic_cast< ast::CastExpr const * >( arg ) ) { |
---|
| 535 | // arg = cast->arg; |
---|
| 536 | // } |
---|
| 537 | if ( auto addr = dynamic_cast<ast::AddressExpr const *>( arg ) ) { |
---|
| 538 | return ast::mutate_field( addr->arg.get(), |
---|
| 539 | &ast::Expr::env, expr->env.get() ); |
---|
| 540 | } |
---|
| 541 | } |
---|
| 542 | } |
---|
| 543 | return expr; |
---|
| 544 | } |
---|
| 545 | |
---|
| 546 | ast::Expr const * GeneralizedLvalue::postvisit( |
---|
| 547 | ast::AddressExpr const * expr ) { |
---|
| 548 | return applyTransformation( expr, &ast::AddressExpr::arg, |
---|
| 549 | []( ast::Expr const * arg ) { |
---|
| 550 | return new ast::AddressExpr( arg->location, arg ); |
---|
| 551 | } |
---|
| 552 | ); |
---|
| 553 | } |
---|
| 554 | |
---|
| 555 | ast::Expr const * GeneralizedLvalue::postvisit( |
---|
| 556 | ast::MemberExpr const * expr ) { |
---|
| 557 | return applyTransformation( expr, &ast::MemberExpr::aggregate, |
---|
| 558 | [expr]( ast::Expr const * aggr ) { |
---|
| 559 | return new ast::MemberExpr( aggr->location, expr->member, aggr ); |
---|
| 560 | } |
---|
| 561 | ); |
---|
| 562 | } |
---|
| 563 | |
---|
| 564 | template<typename Node, typename Func> |
---|
| 565 | ast::Expr const * GeneralizedLvalue::applyTransformation( |
---|
| 566 | Node const * expr, ast::ptr<ast::Expr> Node::*field, Func mkExpr ) { |
---|
| 567 | ast::ptr<ast::Expr> const & arg = expr->*field; |
---|
| 568 | if ( auto commaArg = arg.as<ast::CommaExpr>() ) { |
---|
| 569 | ast::Expr const * arg1 = ast::deepCopy( commaArg->arg1 ); |
---|
| 570 | ast::Expr const * arg2 = ast::deepCopy( commaArg->arg2 ); |
---|
| 571 | ast::Expr const * ret = new ast::CommaExpr( |
---|
| 572 | commaArg->location, arg1, mkExpr( arg2 )->accept( *visitor ) ); |
---|
| 573 | return ret; |
---|
| 574 | } else if ( auto condArg = arg.as<ast::ConditionalExpr>() ) { |
---|
| 575 | ast::Expr const * arg1 = ast::deepCopy( condArg->arg1 ); |
---|
| 576 | ast::Expr const * arg2 = ast::deepCopy( condArg->arg2 ); |
---|
| 577 | ast::Expr const * arg3 = ast::deepCopy( condArg->arg3 ); |
---|
| 578 | ast::ConditionalExpr * ret = new ast::ConditionalExpr( |
---|
| 579 | condArg->location, arg1, mkExpr( arg2 )->accept( *visitor ), |
---|
| 580 | mkExpr( arg3 )->accept( *visitor ) ); |
---|
| 581 | |
---|
| 582 | // Conditional expr type may not be either of the arguments, |
---|
| 583 | // so unify to get the result. |
---|
| 584 | // TODO: Maybe I could create a wrapper for this. |
---|
| 585 | ast::ptr<ast::Type> common = nullptr; |
---|
| 586 | ast::TypeEnvironment newEnv; |
---|
| 587 | ast::AssertionSet needAssertions, haveAssertions; |
---|
| 588 | ast::OpenVarSet openVars; |
---|
| 589 | ResolvExpr::unify( ret->arg2->result, ret->arg3->result, newEnv, |
---|
[251ce80] | 590 | needAssertions, haveAssertions, openVars, common ); |
---|
[7fffb1b] | 591 | ret->result = common ? common : ast::deepCopy( ret->arg2->result ); |
---|
| 592 | return ret; |
---|
| 593 | } |
---|
| 594 | return expr; |
---|
| 595 | } |
---|
| 596 | |
---|
| 597 | ast::Type const * ReferenceTypeElimination::postvisit( |
---|
| 598 | ast::ReferenceType const * type ) { |
---|
| 599 | return new ast::PointerType( type->base, type->qualifiers ); |
---|
| 600 | } |
---|
| 601 | |
---|
| 602 | } // namespace |
---|
| 603 | |
---|
| 604 | // Stored elsewhere (Lvalue2, initially false): |
---|
| 605 | extern bool referencesEliminated; |
---|
| 606 | |
---|
| 607 | void convertLvalue( ast::TranslationUnit & translationUnit ) { |
---|
| 608 | ast::Pass<FixIntrinsicResults>::run( translationUnit ); |
---|
| 609 | ast::Pass<AddressRef>::run( translationUnit ); |
---|
| 610 | ast::Pass<ReferenceConversions>::run( translationUnit ); |
---|
| 611 | ast::Pass<FixIntrinsicArgs>::run( translationUnit ); |
---|
| 612 | ast::Pass<CollapseAddressDeref>::run( translationUnit ); |
---|
| 613 | ast::Pass<GeneralizedLvalue>::run( translationUnit ); |
---|
| 614 | // Last because other passes need reference types to work. |
---|
| 615 | ast::Pass<ReferenceTypeElimination>::run( translationUnit ); |
---|
| 616 | // From this point forward, nothing should create reference types. |
---|
| 617 | referencesEliminated = true; |
---|
| 618 | } |
---|
| 619 | |
---|
| 620 | ast::Expr const * generalizedLvalue( ast::Expr const * expr ) { |
---|
| 621 | ast::Pass<GeneralizedLvalue> visitor; |
---|
| 622 | return expr->accept( visitor ); |
---|
| 623 | } |
---|
| 624 | |
---|
| 625 | } // namespace GenPoly |
---|
| 626 | |
---|
| 627 | // Local Variables: // |
---|
| 628 | // tab-width: 4 // |
---|
| 629 | // mode: c++ // |
---|
| 630 | // compile-command: "make install" // |
---|
| 631 | // End: // |
---|