1 //===-- lib/Evaluate/tools.cpp --------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "flang/Evaluate/tools.h" 10 #include "flang/Common/idioms.h" 11 #include "flang/Evaluate/characteristics.h" 12 #include "flang/Evaluate/traverse.h" 13 #include "flang/Parser/message.h" 14 #include "flang/Semantics/tools.h" 15 #include <algorithm> 16 #include <variant> 17 18 using namespace Fortran::parser::literals; 19 20 namespace Fortran::evaluate { 21 22 // Can x*(a,b) be represented as (x*a,x*b)? This code duplication 23 // of the subexpression "x" cannot (yet?) be reliably undone by 24 // common subexpression elimination in lowering, so it's disabled 25 // here for now to avoid the risk of potential duplication of 26 // expensive subexpressions (e.g., large array expressions, references 27 // to expensive functions) in generate code. 28 static constexpr bool allowOperandDuplication{false}; 29 30 std::optional<Expr<SomeType>> AsGenericExpr(DataRef &&ref) { 31 if (auto dyType{DynamicType::From(ref.GetLastSymbol())}) { 32 return TypedWrapper<Designator, DataRef>(*dyType, std::move(ref)); 33 } else { 34 return std::nullopt; 35 } 36 } 37 38 std::optional<Expr<SomeType>> AsGenericExpr(const Symbol &symbol) { 39 return AsGenericExpr(DataRef{symbol}); 40 } 41 42 Expr<SomeType> Parenthesize(Expr<SomeType> &&expr) { 43 return common::visit( 44 [&](auto &&x) { 45 using T = std::decay_t<decltype(x)>; 46 if constexpr (common::HasMember<T, TypelessExpression>) { 47 return expr; // no parentheses around typeless 48 } else if constexpr (std::is_same_v<T, Expr<SomeDerived>>) { 49 return AsGenericExpr(Parentheses<SomeDerived>{std::move(x)}); 50 } else { 51 return common::visit( 52 [](auto &&y) { 53 using T = ResultType<decltype(y)>; 54 return AsGenericExpr(Parentheses<T>{std::move(y)}); 55 }, 56 std::move(x.u)); 57 } 58 }, 59 std::move(expr.u)); 60 } 61 62 std::optional<DataRef> ExtractDataRef( 63 const ActualArgument &arg, bool intoSubstring, bool intoComplexPart) { 64 return ExtractDataRef(arg.UnwrapExpr(), intoSubstring, intoComplexPart); 65 } 66 67 std::optional<DataRef> ExtractSubstringBase(const Substring &substring) { 68 return common::visit( 69 common::visitors{ 70 [&](const DataRef &x) -> std::optional<DataRef> { return x; }, 71 [&](const StaticDataObject::Pointer &) -> std::optional<DataRef> { 72 return std::nullopt; 73 }, 74 }, 75 substring.parent()); 76 } 77 78 // IsVariable() 79 80 auto IsVariableHelper::operator()(const Symbol &symbol) const -> Result { 81 // ASSOCIATE(x => expr) -- x counts as a variable, but undefinable 82 const Symbol &ultimate{symbol.GetUltimate()}; 83 return !IsNamedConstant(ultimate) && 84 (ultimate.has<semantics::ObjectEntityDetails>() || 85 (ultimate.has<semantics::EntityDetails>() && 86 ultimate.attrs().test(semantics::Attr::TARGET)) || 87 ultimate.has<semantics::AssocEntityDetails>()); 88 } 89 auto IsVariableHelper::operator()(const Component &x) const -> Result { 90 const Symbol &comp{x.GetLastSymbol()}; 91 return (*this)(comp) && (IsPointer(comp) || (*this)(x.base())); 92 } 93 auto IsVariableHelper::operator()(const ArrayRef &x) const -> Result { 94 return (*this)(x.base()); 95 } 96 auto IsVariableHelper::operator()(const Substring &x) const -> Result { 97 return (*this)(x.GetBaseObject()); 98 } 99 auto IsVariableHelper::operator()(const ProcedureDesignator &x) const 100 -> Result { 101 if (const Symbol * symbol{x.GetSymbol()}) { 102 const Symbol *result{FindFunctionResult(*symbol)}; 103 return result && IsPointer(*result) && !IsProcedurePointer(*result); 104 } 105 return false; 106 } 107 108 // Conversions of COMPLEX component expressions to REAL. 109 ConvertRealOperandsResult ConvertRealOperands( 110 parser::ContextualMessages &messages, Expr<SomeType> &&x, 111 Expr<SomeType> &&y, int defaultRealKind) { 112 return common::visit( 113 common::visitors{ 114 [&](Expr<SomeInteger> &&ix, 115 Expr<SomeInteger> &&iy) -> ConvertRealOperandsResult { 116 // Can happen in a CMPLX() constructor. Per F'2018, 117 // both integer operands are converted to default REAL. 118 return {AsSameKindExprs<TypeCategory::Real>( 119 ConvertToKind<TypeCategory::Real>( 120 defaultRealKind, std::move(ix)), 121 ConvertToKind<TypeCategory::Real>( 122 defaultRealKind, std::move(iy)))}; 123 }, 124 [&](Expr<SomeInteger> &&ix, 125 Expr<SomeReal> &&ry) -> ConvertRealOperandsResult { 126 return {AsSameKindExprs<TypeCategory::Real>( 127 ConvertTo(ry, std::move(ix)), std::move(ry))}; 128 }, 129 [&](Expr<SomeReal> &&rx, 130 Expr<SomeInteger> &&iy) -> ConvertRealOperandsResult { 131 return {AsSameKindExprs<TypeCategory::Real>( 132 std::move(rx), ConvertTo(rx, std::move(iy)))}; 133 }, 134 [&](Expr<SomeReal> &&rx, 135 Expr<SomeReal> &&ry) -> ConvertRealOperandsResult { 136 return {AsSameKindExprs<TypeCategory::Real>( 137 std::move(rx), std::move(ry))}; 138 }, 139 [&](Expr<SomeInteger> &&ix, 140 BOZLiteralConstant &&by) -> ConvertRealOperandsResult { 141 return {AsSameKindExprs<TypeCategory::Real>( 142 ConvertToKind<TypeCategory::Real>( 143 defaultRealKind, std::move(ix)), 144 ConvertToKind<TypeCategory::Real>( 145 defaultRealKind, std::move(by)))}; 146 }, 147 [&](BOZLiteralConstant &&bx, 148 Expr<SomeInteger> &&iy) -> ConvertRealOperandsResult { 149 return {AsSameKindExprs<TypeCategory::Real>( 150 ConvertToKind<TypeCategory::Real>( 151 defaultRealKind, std::move(bx)), 152 ConvertToKind<TypeCategory::Real>( 153 defaultRealKind, std::move(iy)))}; 154 }, 155 [&](Expr<SomeReal> &&rx, 156 BOZLiteralConstant &&by) -> ConvertRealOperandsResult { 157 return {AsSameKindExprs<TypeCategory::Real>( 158 std::move(rx), ConvertTo(rx, std::move(by)))}; 159 }, 160 [&](BOZLiteralConstant &&bx, 161 Expr<SomeReal> &&ry) -> ConvertRealOperandsResult { 162 return {AsSameKindExprs<TypeCategory::Real>( 163 ConvertTo(ry, std::move(bx)), std::move(ry))}; 164 }, 165 [&](auto &&, auto &&) -> ConvertRealOperandsResult { // C718 166 messages.Say("operands must be INTEGER or REAL"_err_en_US); 167 return std::nullopt; 168 }, 169 }, 170 std::move(x.u), std::move(y.u)); 171 } 172 173 // Helpers for NumericOperation and its subroutines below. 174 static std::optional<Expr<SomeType>> NoExpr() { return std::nullopt; } 175 176 template <TypeCategory CAT> 177 std::optional<Expr<SomeType>> Package(Expr<SomeKind<CAT>> &&catExpr) { 178 return {AsGenericExpr(std::move(catExpr))}; 179 } 180 template <TypeCategory CAT> 181 std::optional<Expr<SomeType>> Package( 182 std::optional<Expr<SomeKind<CAT>>> &&catExpr) { 183 if (catExpr) { 184 return {AsGenericExpr(std::move(*catExpr))}; 185 } else { 186 return std::nullopt; 187 } 188 } 189 190 // Mixed REAL+INTEGER operations. REAL**INTEGER is a special case that 191 // does not require conversion of the exponent expression. 192 template <template <typename> class OPR> 193 std::optional<Expr<SomeType>> MixedRealLeft( 194 Expr<SomeReal> &&rx, Expr<SomeInteger> &&iy) { 195 return Package(common::visit( 196 [&](auto &&rxk) -> Expr<SomeReal> { 197 using resultType = ResultType<decltype(rxk)>; 198 if constexpr (std::is_same_v<OPR<resultType>, Power<resultType>>) { 199 return AsCategoryExpr( 200 RealToIntPower<resultType>{std::move(rxk), std::move(iy)}); 201 } 202 // G++ 8.1.0 emits bogus warnings about missing return statements if 203 // this statement is wrapped in an "else", as it should be. 204 return AsCategoryExpr(OPR<resultType>{ 205 std::move(rxk), ConvertToType<resultType>(std::move(iy))}); 206 }, 207 std::move(rx.u))); 208 } 209 210 template <int KIND> 211 Expr<SomeComplex> MakeComplex(Expr<Type<TypeCategory::Real, KIND>> &&re, 212 Expr<Type<TypeCategory::Real, KIND>> &&im) { 213 return AsCategoryExpr(ComplexConstructor<KIND>{std::move(re), std::move(im)}); 214 } 215 216 std::optional<Expr<SomeComplex>> ConstructComplex( 217 parser::ContextualMessages &messages, Expr<SomeType> &&real, 218 Expr<SomeType> &&imaginary, int defaultRealKind) { 219 if (auto converted{ConvertRealOperands( 220 messages, std::move(real), std::move(imaginary), defaultRealKind)}) { 221 return {common::visit( 222 [](auto &&pair) { 223 return MakeComplex(std::move(pair[0]), std::move(pair[1])); 224 }, 225 std::move(*converted))}; 226 } 227 return std::nullopt; 228 } 229 230 std::optional<Expr<SomeComplex>> ConstructComplex( 231 parser::ContextualMessages &messages, std::optional<Expr<SomeType>> &&real, 232 std::optional<Expr<SomeType>> &&imaginary, int defaultRealKind) { 233 if (auto parts{common::AllPresent(std::move(real), std::move(imaginary))}) { 234 return ConstructComplex(messages, std::get<0>(std::move(*parts)), 235 std::get<1>(std::move(*parts)), defaultRealKind); 236 } 237 return std::nullopt; 238 } 239 240 // Extracts the real or imaginary part of the result of a COMPLEX 241 // expression, when that expression is simple enough to be duplicated. 242 template <bool GET_IMAGINARY> struct ComplexPartExtractor { 243 template <typename A> static std::optional<Expr<SomeReal>> Get(const A &) { 244 return std::nullopt; 245 } 246 247 template <int KIND> 248 static std::optional<Expr<SomeReal>> Get( 249 const Parentheses<Type<TypeCategory::Complex, KIND>> &kz) { 250 if (auto x{Get(kz.left())}) { 251 return AsGenericExpr(AsSpecificExpr( 252 Parentheses<Type<TypeCategory::Real, KIND>>{std::move(*x)})); 253 } else { 254 return std::nullopt; 255 } 256 } 257 258 template <int KIND> 259 static std::optional<Expr<SomeReal>> Get( 260 const Negate<Type<TypeCategory::Complex, KIND>> &kz) { 261 if (auto x{Get(kz.left())}) { 262 return AsGenericExpr(AsSpecificExpr( 263 Negate<Type<TypeCategory::Real, KIND>>{std::move(*x)})); 264 } else { 265 return std::nullopt; 266 } 267 } 268 269 template <int KIND> 270 static std::optional<Expr<SomeReal>> Get( 271 const Convert<Type<TypeCategory::Complex, KIND>, TypeCategory::Complex> 272 &kz) { 273 if (auto x{Get(kz.left())}) { 274 return AsGenericExpr(AsSpecificExpr( 275 Convert<Type<TypeCategory::Real, KIND>, TypeCategory::Real>{ 276 AsGenericExpr(std::move(*x))})); 277 } else { 278 return std::nullopt; 279 } 280 } 281 282 template <int KIND> 283 static std::optional<Expr<SomeReal>> Get(const ComplexConstructor<KIND> &kz) { 284 return GET_IMAGINARY ? Get(kz.right()) : Get(kz.left()); 285 } 286 287 template <int KIND> 288 static std::optional<Expr<SomeReal>> Get( 289 const Constant<Type<TypeCategory::Complex, KIND>> &kz) { 290 if (auto cz{kz.GetScalarValue()}) { 291 return AsGenericExpr( 292 AsSpecificExpr(GET_IMAGINARY ? cz->AIMAG() : cz->REAL())); 293 } else { 294 return std::nullopt; 295 } 296 } 297 298 template <int KIND> 299 static std::optional<Expr<SomeReal>> Get( 300 const Designator<Type<TypeCategory::Complex, KIND>> &kz) { 301 if (const auto *symbolRef{std::get_if<SymbolRef>(&kz.u)}) { 302 return AsGenericExpr(AsSpecificExpr( 303 Designator<Type<TypeCategory::Complex, KIND>>{ComplexPart{ 304 DataRef{*symbolRef}, 305 GET_IMAGINARY ? ComplexPart::Part::IM : ComplexPart::Part::RE}})); 306 } else { 307 return std::nullopt; 308 } 309 } 310 311 template <int KIND> 312 static std::optional<Expr<SomeReal>> Get( 313 const Expr<Type<TypeCategory::Complex, KIND>> &kz) { 314 return Get(kz.u); 315 } 316 317 static std::optional<Expr<SomeReal>> Get(const Expr<SomeComplex> &z) { 318 return Get(z.u); 319 } 320 }; 321 322 // Convert REAL to COMPLEX of the same kind. Preserving the real operand kind 323 // and then applying complex operand promotion rules allows the result to have 324 // the highest precision of REAL and COMPLEX operands as required by Fortran 325 // 2018 10.9.1.3. 326 Expr<SomeComplex> PromoteRealToComplex(Expr<SomeReal> &&someX) { 327 return common::visit( 328 [](auto &&x) { 329 using RT = ResultType<decltype(x)>; 330 return AsCategoryExpr(ComplexConstructor<RT::kind>{ 331 std::move(x), AsExpr(Constant<RT>{Scalar<RT>{}})}); 332 }, 333 std::move(someX.u)); 334 } 335 336 // Handle mixed COMPLEX+REAL (or INTEGER) operations in a better way 337 // than just converting the second operand to COMPLEX and performing the 338 // corresponding COMPLEX+COMPLEX operation. 339 template <template <typename> class OPR, TypeCategory RCAT> 340 std::optional<Expr<SomeType>> MixedComplexLeft( 341 parser::ContextualMessages &messages, const Expr<SomeComplex> &zx, 342 const Expr<SomeKind<RCAT>> &iry, [[maybe_unused]] int defaultRealKind) { 343 if constexpr (RCAT == TypeCategory::Integer && 344 std::is_same_v<OPR<LargestReal>, Power<LargestReal>>) { 345 // COMPLEX**INTEGER is a special case that doesn't convert the exponent. 346 return Package(common::visit( 347 [&](const auto &zxk) { 348 using Ty = ResultType<decltype(zxk)>; 349 return AsCategoryExpr(AsExpr( 350 RealToIntPower<Ty>{common::Clone(zxk), common::Clone(iry)})); 351 }, 352 zx.u)); 353 } 354 std::optional<Expr<SomeReal>> zr{ComplexPartExtractor<false>{}.Get(zx)}; 355 std::optional<Expr<SomeReal>> zi{ComplexPartExtractor<true>{}.Get(zx)}; 356 if (!zr || !zi) { 357 } else if constexpr (std::is_same_v<OPR<LargestReal>, Add<LargestReal>> || 358 std::is_same_v<OPR<LargestReal>, Subtract<LargestReal>>) { 359 // (a,b) + x -> (a+x, b) 360 // (a,b) - x -> (a-x, b) 361 if (std::optional<Expr<SomeType>> rr{ 362 NumericOperation<OPR>(messages, AsGenericExpr(std::move(*zr)), 363 AsGenericExpr(common::Clone(iry)), defaultRealKind)}) { 364 return Package(ConstructComplex(messages, std::move(*rr), 365 AsGenericExpr(std::move(*zi)), defaultRealKind)); 366 } 367 } else if constexpr (allowOperandDuplication && 368 (std::is_same_v<OPR<LargestReal>, Multiply<LargestReal>> || 369 std::is_same_v<OPR<LargestReal>, Divide<LargestReal>>)) { 370 // (a,b) * x -> (a*x, b*x) 371 // (a,b) / x -> (a/x, b/x) 372 auto copy{iry}; 373 auto rr{NumericOperation<OPR>(messages, AsGenericExpr(std::move(*zr)), 374 AsGenericExpr(common::Clone(iry)), defaultRealKind)}; 375 auto ri{NumericOperation<OPR>(messages, AsGenericExpr(std::move(*zi)), 376 AsGenericExpr(std::move(copy)), defaultRealKind)}; 377 if (auto parts{common::AllPresent(std::move(rr), std::move(ri))}) { 378 return Package(ConstructComplex(messages, std::get<0>(std::move(*parts)), 379 std::get<1>(std::move(*parts)), defaultRealKind)); 380 } 381 } 382 return std::nullopt; 383 } 384 385 // Mixed COMPLEX operations with the COMPLEX operand on the right. 386 // x + (a,b) -> (x+a, b) 387 // x - (a,b) -> (x-a, -b) 388 // x * (a,b) -> (x*a, x*b) 389 // x / (a,b) -> (x,0) / (a,b) (and **) 390 template <template <typename> class OPR, TypeCategory LCAT> 391 std::optional<Expr<SomeType>> MixedComplexRight( 392 parser::ContextualMessages &messages, const Expr<SomeKind<LCAT>> &irx, 393 const Expr<SomeComplex> &zy, [[maybe_unused]] int defaultRealKind) { 394 if constexpr (std::is_same_v<OPR<LargestReal>, Add<LargestReal>>) { 395 // x + (a,b) -> (a,b) + x -> (a+x, b) 396 return MixedComplexLeft<OPR, LCAT>(messages, zy, irx, defaultRealKind); 397 } else if constexpr (allowOperandDuplication && 398 std::is_same_v<OPR<LargestReal>, Multiply<LargestReal>>) { 399 // x * (a,b) -> (a,b) * x -> (a*x, b*x) 400 return MixedComplexLeft<OPR, LCAT>(messages, zy, irx, defaultRealKind); 401 } else if constexpr (std::is_same_v<OPR<LargestReal>, 402 Subtract<LargestReal>>) { 403 // x - (a,b) -> (x-a, -b) 404 std::optional<Expr<SomeReal>> zr{ComplexPartExtractor<false>{}.Get(zy)}; 405 std::optional<Expr<SomeReal>> zi{ComplexPartExtractor<true>{}.Get(zy)}; 406 if (zr && zi) { 407 if (std::optional<Expr<SomeType>> rr{NumericOperation<Subtract>(messages, 408 AsGenericExpr(common::Clone(irx)), AsGenericExpr(std::move(*zr)), 409 defaultRealKind)}) { 410 return Package(ConstructComplex(messages, std::move(*rr), 411 AsGenericExpr(-std::move(*zi)), defaultRealKind)); 412 } 413 } 414 } 415 return std::nullopt; 416 } 417 418 // Promotes REAL(rk) and COMPLEX(zk) operands COMPLEX(max(rk,zk)) 419 // then combine them with an operator. 420 template <template <typename> class OPR, TypeCategory XCAT, TypeCategory YCAT> 421 Expr<SomeComplex> PromoteMixedComplexReal( 422 Expr<SomeKind<XCAT>> &&x, Expr<SomeKind<YCAT>> &&y) { 423 static_assert(XCAT == TypeCategory::Complex || YCAT == TypeCategory::Complex); 424 static_assert(XCAT == TypeCategory::Real || YCAT == TypeCategory::Real); 425 return common::visit( 426 [&](const auto &kx, const auto &ky) { 427 constexpr int maxKind{std::max( 428 ResultType<decltype(kx)>::kind, ResultType<decltype(ky)>::kind)}; 429 using ZTy = Type<TypeCategory::Complex, maxKind>; 430 return Expr<SomeComplex>{ 431 Expr<ZTy>{OPR<ZTy>{ConvertToType<ZTy>(std::move(x)), 432 ConvertToType<ZTy>(std::move(y))}}}; 433 }, 434 x.u, y.u); 435 } 436 437 // N.B. When a "typeless" BOZ literal constant appears as one (not both!) of 438 // the operands to a dyadic operation where one is permitted, it assumes the 439 // type and kind of the other operand. 440 template <template <typename> class OPR> 441 std::optional<Expr<SomeType>> NumericOperation( 442 parser::ContextualMessages &messages, Expr<SomeType> &&x, 443 Expr<SomeType> &&y, int defaultRealKind) { 444 return common::visit( 445 common::visitors{ 446 [](Expr<SomeInteger> &&ix, Expr<SomeInteger> &&iy) { 447 return Package(PromoteAndCombine<OPR, TypeCategory::Integer>( 448 std::move(ix), std::move(iy))); 449 }, 450 [](Expr<SomeReal> &&rx, Expr<SomeReal> &&ry) { 451 return Package(PromoteAndCombine<OPR, TypeCategory::Real>( 452 std::move(rx), std::move(ry))); 453 }, 454 // Mixed REAL/INTEGER operations 455 [](Expr<SomeReal> &&rx, Expr<SomeInteger> &&iy) { 456 return MixedRealLeft<OPR>(std::move(rx), std::move(iy)); 457 }, 458 [](Expr<SomeInteger> &&ix, Expr<SomeReal> &&ry) { 459 return Package(common::visit( 460 [&](auto &&ryk) -> Expr<SomeReal> { 461 using resultType = ResultType<decltype(ryk)>; 462 return AsCategoryExpr( 463 OPR<resultType>{ConvertToType<resultType>(std::move(ix)), 464 std::move(ryk)}); 465 }, 466 std::move(ry.u))); 467 }, 468 // Homogeneous and mixed COMPLEX operations 469 [](Expr<SomeComplex> &&zx, Expr<SomeComplex> &&zy) { 470 return Package(PromoteAndCombine<OPR, TypeCategory::Complex>( 471 std::move(zx), std::move(zy))); 472 }, 473 [&](Expr<SomeComplex> &&zx, Expr<SomeInteger> &&iy) { 474 if (auto result{ 475 MixedComplexLeft<OPR>(messages, zx, iy, defaultRealKind)}) { 476 return result; 477 } else { 478 return Package(PromoteAndCombine<OPR, TypeCategory::Complex>( 479 std::move(zx), ConvertTo(zx, std::move(iy)))); 480 } 481 }, 482 [&](Expr<SomeComplex> &&zx, Expr<SomeReal> &&ry) { 483 if (auto result{ 484 MixedComplexLeft<OPR>(messages, zx, ry, defaultRealKind)}) { 485 return result; 486 } else { 487 return Package( 488 PromoteMixedComplexReal<OPR>(std::move(zx), std::move(ry))); 489 } 490 }, 491 [&](Expr<SomeInteger> &&ix, Expr<SomeComplex> &&zy) { 492 if (auto result{MixedComplexRight<OPR>( 493 messages, ix, zy, defaultRealKind)}) { 494 return result; 495 } else { 496 return Package(PromoteAndCombine<OPR, TypeCategory::Complex>( 497 ConvertTo(zy, std::move(ix)), std::move(zy))); 498 } 499 }, 500 [&](Expr<SomeReal> &&rx, Expr<SomeComplex> &&zy) { 501 if (auto result{MixedComplexRight<OPR>( 502 messages, rx, zy, defaultRealKind)}) { 503 return result; 504 } else { 505 return Package( 506 PromoteMixedComplexReal<OPR>(std::move(rx), std::move(zy))); 507 } 508 }, 509 // Operations with one typeless operand 510 [&](BOZLiteralConstant &&bx, Expr<SomeInteger> &&iy) { 511 return NumericOperation<OPR>(messages, 512 AsGenericExpr(ConvertTo(iy, std::move(bx))), std::move(y), 513 defaultRealKind); 514 }, 515 [&](BOZLiteralConstant &&bx, Expr<SomeReal> &&ry) { 516 return NumericOperation<OPR>(messages, 517 AsGenericExpr(ConvertTo(ry, std::move(bx))), std::move(y), 518 defaultRealKind); 519 }, 520 [&](Expr<SomeInteger> &&ix, BOZLiteralConstant &&by) { 521 return NumericOperation<OPR>(messages, std::move(x), 522 AsGenericExpr(ConvertTo(ix, std::move(by))), defaultRealKind); 523 }, 524 [&](Expr<SomeReal> &&rx, BOZLiteralConstant &&by) { 525 return NumericOperation<OPR>(messages, std::move(x), 526 AsGenericExpr(ConvertTo(rx, std::move(by))), defaultRealKind); 527 }, 528 // Default case 529 [&](auto &&, auto &&) { 530 messages.Say("non-numeric operands to numeric operation"_err_en_US); 531 return NoExpr(); 532 }, 533 }, 534 std::move(x.u), std::move(y.u)); 535 } 536 537 template std::optional<Expr<SomeType>> NumericOperation<Power>( 538 parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&, 539 int defaultRealKind); 540 template std::optional<Expr<SomeType>> NumericOperation<Multiply>( 541 parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&, 542 int defaultRealKind); 543 template std::optional<Expr<SomeType>> NumericOperation<Divide>( 544 parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&, 545 int defaultRealKind); 546 template std::optional<Expr<SomeType>> NumericOperation<Add>( 547 parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&, 548 int defaultRealKind); 549 template std::optional<Expr<SomeType>> NumericOperation<Subtract>( 550 parser::ContextualMessages &, Expr<SomeType> &&, Expr<SomeType> &&, 551 int defaultRealKind); 552 553 std::optional<Expr<SomeType>> Negation( 554 parser::ContextualMessages &messages, Expr<SomeType> &&x) { 555 return common::visit( 556 common::visitors{ 557 [&](BOZLiteralConstant &&) { 558 messages.Say("BOZ literal cannot be negated"_err_en_US); 559 return NoExpr(); 560 }, 561 [&](NullPointer &&) { 562 messages.Say("NULL() cannot be negated"_err_en_US); 563 return NoExpr(); 564 }, 565 [&](ProcedureDesignator &&) { 566 messages.Say("Subroutine cannot be negated"_err_en_US); 567 return NoExpr(); 568 }, 569 [&](ProcedureRef &&) { 570 messages.Say("Pointer to subroutine cannot be negated"_err_en_US); 571 return NoExpr(); 572 }, 573 [&](Expr<SomeInteger> &&x) { return Package(-std::move(x)); }, 574 [&](Expr<SomeReal> &&x) { return Package(-std::move(x)); }, 575 [&](Expr<SomeComplex> &&x) { return Package(-std::move(x)); }, 576 [&](Expr<SomeCharacter> &&) { 577 messages.Say("CHARACTER cannot be negated"_err_en_US); 578 return NoExpr(); 579 }, 580 [&](Expr<SomeLogical> &&) { 581 messages.Say("LOGICAL cannot be negated"_err_en_US); 582 return NoExpr(); 583 }, 584 [&](Expr<SomeDerived> &&) { 585 messages.Say("Operand cannot be negated"_err_en_US); 586 return NoExpr(); 587 }, 588 }, 589 std::move(x.u)); 590 } 591 592 Expr<SomeLogical> LogicalNegation(Expr<SomeLogical> &&x) { 593 return common::visit( 594 [](auto &&xk) { return AsCategoryExpr(LogicalNegation(std::move(xk))); }, 595 std::move(x.u)); 596 } 597 598 template <TypeCategory CAT> 599 Expr<LogicalResult> PromoteAndRelate( 600 RelationalOperator opr, Expr<SomeKind<CAT>> &&x, Expr<SomeKind<CAT>> &&y) { 601 return common::visit( 602 [=](auto &&xy) { 603 return PackageRelation(opr, std::move(xy[0]), std::move(xy[1])); 604 }, 605 AsSameKindExprs(std::move(x), std::move(y))); 606 } 607 608 std::optional<Expr<LogicalResult>> Relate(parser::ContextualMessages &messages, 609 RelationalOperator opr, Expr<SomeType> &&x, Expr<SomeType> &&y) { 610 return common::visit( 611 common::visitors{ 612 [=](Expr<SomeInteger> &&ix, 613 Expr<SomeInteger> &&iy) -> std::optional<Expr<LogicalResult>> { 614 return PromoteAndRelate(opr, std::move(ix), std::move(iy)); 615 }, 616 [=](Expr<SomeReal> &&rx, 617 Expr<SomeReal> &&ry) -> std::optional<Expr<LogicalResult>> { 618 return PromoteAndRelate(opr, std::move(rx), std::move(ry)); 619 }, 620 [&](Expr<SomeReal> &&rx, Expr<SomeInteger> &&iy) { 621 return Relate(messages, opr, std::move(x), 622 AsGenericExpr(ConvertTo(rx, std::move(iy)))); 623 }, 624 [&](Expr<SomeInteger> &&ix, Expr<SomeReal> &&ry) { 625 return Relate(messages, opr, 626 AsGenericExpr(ConvertTo(ry, std::move(ix))), std::move(y)); 627 }, 628 [&](Expr<SomeComplex> &&zx, 629 Expr<SomeComplex> &&zy) -> std::optional<Expr<LogicalResult>> { 630 if (opr == RelationalOperator::EQ || 631 opr == RelationalOperator::NE) { 632 return PromoteAndRelate(opr, std::move(zx), std::move(zy)); 633 } else { 634 messages.Say( 635 "COMPLEX data may be compared only for equality"_err_en_US); 636 return std::nullopt; 637 } 638 }, 639 [&](Expr<SomeComplex> &&zx, Expr<SomeInteger> &&iy) { 640 return Relate(messages, opr, std::move(x), 641 AsGenericExpr(ConvertTo(zx, std::move(iy)))); 642 }, 643 [&](Expr<SomeComplex> &&zx, Expr<SomeReal> &&ry) { 644 return Relate(messages, opr, std::move(x), 645 AsGenericExpr(ConvertTo(zx, std::move(ry)))); 646 }, 647 [&](Expr<SomeInteger> &&ix, Expr<SomeComplex> &&zy) { 648 return Relate(messages, opr, 649 AsGenericExpr(ConvertTo(zy, std::move(ix))), std::move(y)); 650 }, 651 [&](Expr<SomeReal> &&rx, Expr<SomeComplex> &&zy) { 652 return Relate(messages, opr, 653 AsGenericExpr(ConvertTo(zy, std::move(rx))), std::move(y)); 654 }, 655 [&](Expr<SomeCharacter> &&cx, Expr<SomeCharacter> &&cy) { 656 return common::visit( 657 [&](auto &&cxk, 658 auto &&cyk) -> std::optional<Expr<LogicalResult>> { 659 using Ty = ResultType<decltype(cxk)>; 660 if constexpr (std::is_same_v<Ty, ResultType<decltype(cyk)>>) { 661 return PackageRelation(opr, std::move(cxk), std::move(cyk)); 662 } else { 663 messages.Say( 664 "CHARACTER operands do not have same KIND"_err_en_US); 665 return std::nullopt; 666 } 667 }, 668 std::move(cx.u), std::move(cy.u)); 669 }, 670 // Default case 671 [&](auto &&, auto &&) { 672 DIE("invalid types for relational operator"); 673 return std::optional<Expr<LogicalResult>>{}; 674 }, 675 }, 676 std::move(x.u), std::move(y.u)); 677 } 678 679 Expr<SomeLogical> BinaryLogicalOperation( 680 LogicalOperator opr, Expr<SomeLogical> &&x, Expr<SomeLogical> &&y) { 681 CHECK(opr != LogicalOperator::Not); 682 return common::visit( 683 [=](auto &&xy) { 684 using Ty = ResultType<decltype(xy[0])>; 685 return Expr<SomeLogical>{BinaryLogicalOperation<Ty::kind>( 686 opr, std::move(xy[0]), std::move(xy[1]))}; 687 }, 688 AsSameKindExprs(std::move(x), std::move(y))); 689 } 690 691 template <TypeCategory TO> 692 std::optional<Expr<SomeType>> ConvertToNumeric(int kind, Expr<SomeType> &&x) { 693 static_assert(common::IsNumericTypeCategory(TO)); 694 return common::visit( 695 [=](auto &&cx) -> std::optional<Expr<SomeType>> { 696 using cxType = std::decay_t<decltype(cx)>; 697 if constexpr (!common::HasMember<cxType, TypelessExpression>) { 698 if constexpr (IsNumericTypeCategory(ResultType<cxType>::category)) { 699 return Expr<SomeType>{ConvertToKind<TO>(kind, std::move(cx))}; 700 } 701 } 702 return std::nullopt; 703 }, 704 std::move(x.u)); 705 } 706 707 std::optional<Expr<SomeType>> ConvertToType( 708 const DynamicType &type, Expr<SomeType> &&x) { 709 if (type.IsTypelessIntrinsicArgument()) { 710 return std::nullopt; 711 } 712 switch (type.category()) { 713 case TypeCategory::Integer: 714 if (auto *boz{std::get_if<BOZLiteralConstant>(&x.u)}) { 715 // Extension to C7109: allow BOZ literals to appear in integer contexts 716 // when the type is unambiguous. 717 return Expr<SomeType>{ 718 ConvertToKind<TypeCategory::Integer>(type.kind(), std::move(*boz))}; 719 } 720 return ConvertToNumeric<TypeCategory::Integer>(type.kind(), std::move(x)); 721 case TypeCategory::Real: 722 if (auto *boz{std::get_if<BOZLiteralConstant>(&x.u)}) { 723 return Expr<SomeType>{ 724 ConvertToKind<TypeCategory::Real>(type.kind(), std::move(*boz))}; 725 } 726 return ConvertToNumeric<TypeCategory::Real>(type.kind(), std::move(x)); 727 case TypeCategory::Complex: 728 return ConvertToNumeric<TypeCategory::Complex>(type.kind(), std::move(x)); 729 case TypeCategory::Character: 730 if (auto *cx{UnwrapExpr<Expr<SomeCharacter>>(x)}) { 731 auto converted{ 732 ConvertToKind<TypeCategory::Character>(type.kind(), std::move(*cx))}; 733 if (auto length{type.GetCharLength()}) { 734 converted = common::visit( 735 [&](auto &&x) { 736 using CharacterType = ResultType<decltype(x)>; 737 return Expr<SomeCharacter>{ 738 Expr<CharacterType>{SetLength<CharacterType::kind>{ 739 std::move(x), std::move(*length)}}}; 740 }, 741 std::move(converted.u)); 742 } 743 return Expr<SomeType>{std::move(converted)}; 744 } 745 break; 746 case TypeCategory::Logical: 747 if (auto *cx{UnwrapExpr<Expr<SomeLogical>>(x)}) { 748 return Expr<SomeType>{ 749 ConvertToKind<TypeCategory::Logical>(type.kind(), std::move(*cx))}; 750 } 751 break; 752 case TypeCategory::Derived: 753 if (auto fromType{x.GetType()}) { 754 if (type.IsTkCompatibleWith(*fromType)) { 755 // "x" could be assigned or passed to "type", or appear in a 756 // structure constructor as a value for a component with "type" 757 return std::move(x); 758 } 759 } 760 break; 761 } 762 return std::nullopt; 763 } 764 765 std::optional<Expr<SomeType>> ConvertToType( 766 const DynamicType &to, std::optional<Expr<SomeType>> &&x) { 767 if (x) { 768 return ConvertToType(to, std::move(*x)); 769 } else { 770 return std::nullopt; 771 } 772 } 773 774 std::optional<Expr<SomeType>> ConvertToType( 775 const Symbol &symbol, Expr<SomeType> &&x) { 776 if (auto symType{DynamicType::From(symbol)}) { 777 return ConvertToType(*symType, std::move(x)); 778 } 779 return std::nullopt; 780 } 781 782 std::optional<Expr<SomeType>> ConvertToType( 783 const Symbol &to, std::optional<Expr<SomeType>> &&x) { 784 if (x) { 785 return ConvertToType(to, std::move(*x)); 786 } else { 787 return std::nullopt; 788 } 789 } 790 791 bool IsAssumedRank(const Symbol &original) { 792 if (const auto *assoc{original.detailsIf<semantics::AssocEntityDetails>()}) { 793 if (assoc->rank()) { 794 return false; // in RANK(n) or RANK(*) 795 } else if (assoc->IsAssumedRank()) { 796 return true; // RANK DEFAULT 797 } 798 } 799 const Symbol &symbol{semantics::ResolveAssociations(original)}; 800 const auto *object{symbol.detailsIf<semantics::ObjectEntityDetails>()}; 801 return object && object->IsAssumedRank(); 802 } 803 804 bool IsAssumedRank(const ActualArgument &arg) { 805 if (const auto *expr{arg.UnwrapExpr()}) { 806 return IsAssumedRank(*expr); 807 } else { 808 const Symbol *assumedTypeDummy{arg.GetAssumedTypeDummy()}; 809 CHECK(assumedTypeDummy); 810 return IsAssumedRank(*assumedTypeDummy); 811 } 812 } 813 814 bool IsCoarray(const ActualArgument &arg) { 815 const auto *expr{arg.UnwrapExpr()}; 816 return expr && IsCoarray(*expr); 817 } 818 819 bool IsCoarray(const Symbol &symbol) { 820 return GetAssociationRoot(symbol).Corank() > 0; 821 } 822 823 bool IsProcedureDesignator(const Expr<SomeType> &expr) { 824 return std::holds_alternative<ProcedureDesignator>(expr.u); 825 } 826 bool IsFunctionDesignator(const Expr<SomeType> &expr) { 827 const auto *designator{std::get_if<ProcedureDesignator>(&expr.u)}; 828 return designator && designator->GetType().has_value(); 829 } 830 831 bool IsPointer(const Expr<SomeType> &expr) { 832 return IsObjectPointer(expr) || IsProcedurePointer(expr); 833 } 834 835 bool IsProcedurePointer(const Expr<SomeType> &expr) { 836 if (IsNullProcedurePointer(expr)) { 837 return true; 838 } else if (const auto *funcRef{UnwrapProcedureRef(expr)}) { 839 if (const Symbol * proc{funcRef->proc().GetSymbol()}) { 840 const Symbol *result{FindFunctionResult(*proc)}; 841 return result && IsProcedurePointer(*result); 842 } else { 843 return false; 844 } 845 } else if (const auto *proc{std::get_if<ProcedureDesignator>(&expr.u)}) { 846 return IsProcedurePointer(proc->GetSymbol()); 847 } else { 848 return false; 849 } 850 } 851 852 bool IsProcedure(const Expr<SomeType> &expr) { 853 return IsProcedureDesignator(expr) || IsProcedurePointer(expr); 854 } 855 856 bool IsProcedurePointerTarget(const Expr<SomeType> &expr) { 857 return common::visit(common::visitors{ 858 [](const NullPointer &) { return true; }, 859 [](const ProcedureDesignator &) { return true; }, 860 [](const ProcedureRef &) { return true; }, 861 [&](const auto &) { 862 const Symbol *last{GetLastSymbol(expr)}; 863 return last && IsProcedurePointer(*last); 864 }, 865 }, 866 expr.u); 867 } 868 869 bool IsObjectPointer(const Expr<SomeType> &expr) { 870 if (IsNullObjectPointer(expr)) { 871 return true; 872 } else if (IsProcedurePointerTarget(expr)) { 873 return false; 874 } else if (const auto *funcRef{UnwrapProcedureRef(expr)}) { 875 return IsVariable(*funcRef); 876 } else if (const Symbol * symbol{UnwrapWholeSymbolOrComponentDataRef(expr)}) { 877 return IsPointer(symbol->GetUltimate()); 878 } else { 879 return false; 880 } 881 } 882 883 // IsNullPointer() & variations 884 885 template <bool IS_PROC_PTR> struct IsNullPointerHelper { 886 template <typename A> bool operator()(const A &) const { return false; } 887 bool operator()(const ProcedureRef &call) const { 888 if constexpr (IS_PROC_PTR) { 889 const auto *intrinsic{call.proc().GetSpecificIntrinsic()}; 890 return intrinsic && 891 intrinsic->characteristics.value().attrs.test( 892 characteristics::Procedure::Attr::NullPointer); 893 } else { 894 return false; 895 } 896 } 897 template <typename T> bool operator()(const FunctionRef<T> &call) const { 898 if constexpr (IS_PROC_PTR) { 899 return false; 900 } else { 901 const auto *intrinsic{call.proc().GetSpecificIntrinsic()}; 902 return intrinsic && 903 intrinsic->characteristics.value().attrs.test( 904 characteristics::Procedure::Attr::NullPointer); 905 } 906 } 907 template <typename T> bool operator()(const Designator<T> &x) const { 908 if (const auto *component{std::get_if<Component>(&x.u)}) { 909 if (const auto *baseSym{std::get_if<SymbolRef>(&component->base().u)}) { 910 const Symbol &base{**baseSym}; 911 if (const auto *object{ 912 base.detailsIf<semantics::ObjectEntityDetails>()}) { 913 // TODO: nested component and array references 914 if (IsNamedConstant(base) && object->init()) { 915 if (auto structCons{ 916 GetScalarConstantValue<SomeDerived>(*object->init())}) { 917 auto iter{structCons->values().find(component->GetLastSymbol())}; 918 if (iter != structCons->values().end()) { 919 return (*this)(iter->second.value()); 920 } 921 } 922 } 923 } 924 } 925 } 926 return false; 927 } 928 bool operator()(const NullPointer &) const { return true; } 929 template <typename T> bool operator()(const Parentheses<T> &x) const { 930 return (*this)(x.left()); 931 } 932 template <typename T> bool operator()(const Expr<T> &x) const { 933 return common::visit(*this, x.u); 934 } 935 }; 936 937 bool IsNullObjectPointer(const Expr<SomeType> &expr) { 938 return IsNullPointerHelper<false>{}(expr); 939 } 940 941 bool IsNullProcedurePointer(const Expr<SomeType> &expr) { 942 return IsNullPointerHelper<true>{}(expr); 943 } 944 945 bool IsNullPointer(const Expr<SomeType> &expr) { 946 return IsNullObjectPointer(expr) || IsNullProcedurePointer(expr); 947 } 948 949 bool IsBareNullPointer(const Expr<SomeType> *expr) { 950 return expr && std::holds_alternative<NullPointer>(expr->u); 951 } 952 953 // GetSymbolVector() 954 auto GetSymbolVectorHelper::operator()(const Symbol &x) const -> Result { 955 if (const auto *details{x.detailsIf<semantics::AssocEntityDetails>()}) { 956 if (IsVariable(details->expr()) && !UnwrapProcedureRef(*details->expr())) { 957 // associate(x => variable that is not a pointer returned by a function) 958 return (*this)(details->expr()); 959 } 960 } 961 return {x.GetUltimate()}; 962 } 963 auto GetSymbolVectorHelper::operator()(const Component &x) const -> Result { 964 Result result{(*this)(x.base())}; 965 result.emplace_back(x.GetLastSymbol()); 966 return result; 967 } 968 auto GetSymbolVectorHelper::operator()(const ArrayRef &x) const -> Result { 969 return GetSymbolVector(x.base()); 970 } 971 auto GetSymbolVectorHelper::operator()(const CoarrayRef &x) const -> Result { 972 return x.base(); 973 } 974 975 const Symbol *GetLastTarget(const SymbolVector &symbols) { 976 auto end{std::crend(symbols)}; 977 // N.B. Neither clang nor g++ recognizes "symbols.crbegin()" here. 978 auto iter{std::find_if(std::crbegin(symbols), end, [](const Symbol &x) { 979 return x.attrs().HasAny( 980 {semantics::Attr::POINTER, semantics::Attr::TARGET}); 981 })}; 982 return iter == end ? nullptr : &**iter; 983 } 984 985 struct CollectSymbolsHelper 986 : public SetTraverse<CollectSymbolsHelper, semantics::UnorderedSymbolSet> { 987 using Base = SetTraverse<CollectSymbolsHelper, semantics::UnorderedSymbolSet>; 988 CollectSymbolsHelper() : Base{*this} {} 989 using Base::operator(); 990 semantics::UnorderedSymbolSet operator()(const Symbol &symbol) const { 991 return {symbol}; 992 } 993 }; 994 template <typename A> semantics::UnorderedSymbolSet CollectSymbols(const A &x) { 995 return CollectSymbolsHelper{}(x); 996 } 997 template semantics::UnorderedSymbolSet CollectSymbols(const Expr<SomeType> &); 998 template semantics::UnorderedSymbolSet CollectSymbols( 999 const Expr<SomeInteger> &); 1000 template semantics::UnorderedSymbolSet CollectSymbols( 1001 const Expr<SubscriptInteger> &); 1002 1003 // HasVectorSubscript() 1004 struct HasVectorSubscriptHelper 1005 : public AnyTraverse<HasVectorSubscriptHelper, bool, 1006 /*TraverseAssocEntityDetails=*/false> { 1007 using Base = AnyTraverse<HasVectorSubscriptHelper, bool, false>; 1008 HasVectorSubscriptHelper() : Base{*this} {} 1009 using Base::operator(); 1010 bool operator()(const Subscript &ss) const { 1011 return !std::holds_alternative<Triplet>(ss.u) && ss.Rank() > 0; 1012 } 1013 bool operator()(const ProcedureRef &) const { 1014 return false; // don't descend into function call arguments 1015 } 1016 }; 1017 1018 bool HasVectorSubscript(const Expr<SomeType> &expr) { 1019 return HasVectorSubscriptHelper{}(expr); 1020 } 1021 1022 parser::Message *AttachDeclaration( 1023 parser::Message &message, const Symbol &symbol) { 1024 const Symbol *unhosted{&symbol}; 1025 while ( 1026 const auto *assoc{unhosted->detailsIf<semantics::HostAssocDetails>()}) { 1027 unhosted = &assoc->symbol(); 1028 } 1029 if (const auto *binding{ 1030 unhosted->detailsIf<semantics::ProcBindingDetails>()}) { 1031 if (binding->symbol().name() != symbol.name()) { 1032 message.Attach(binding->symbol().name(), 1033 "Procedure '%s' of type '%s' is bound to '%s'"_en_US, symbol.name(), 1034 symbol.owner().GetName().value(), binding->symbol().name()); 1035 return &message; 1036 } 1037 unhosted = &binding->symbol(); 1038 } 1039 if (const auto *use{symbol.detailsIf<semantics::UseDetails>()}) { 1040 message.Attach(use->location(), 1041 "'%s' is USE-associated with '%s' in module '%s'"_en_US, symbol.name(), 1042 unhosted->name(), GetUsedModule(*use).name()); 1043 } else { 1044 message.Attach( 1045 unhosted->name(), "Declaration of '%s'"_en_US, unhosted->name()); 1046 } 1047 return &message; 1048 } 1049 1050 parser::Message *AttachDeclaration( 1051 parser::Message *message, const Symbol &symbol) { 1052 return message ? AttachDeclaration(*message, symbol) : nullptr; 1053 } 1054 1055 class FindImpureCallHelper 1056 : public AnyTraverse<FindImpureCallHelper, std::optional<std::string>, 1057 /*TraverseAssocEntityDetails=*/false> { 1058 using Result = std::optional<std::string>; 1059 using Base = AnyTraverse<FindImpureCallHelper, Result, false>; 1060 1061 public: 1062 explicit FindImpureCallHelper(FoldingContext &c) : Base{*this}, context_{c} {} 1063 using Base::operator(); 1064 Result operator()(const ProcedureRef &call) const { 1065 if (auto chars{characteristics::Procedure::Characterize( 1066 call.proc(), context_, /*emitError=*/false)}) { 1067 if (chars->attrs.test(characteristics::Procedure::Attr::Pure)) { 1068 return (*this)(call.arguments()); 1069 } 1070 } 1071 return call.proc().GetName(); 1072 } 1073 1074 private: 1075 FoldingContext &context_; 1076 }; 1077 1078 std::optional<std::string> FindImpureCall( 1079 FoldingContext &context, const Expr<SomeType> &expr) { 1080 return FindImpureCallHelper{context}(expr); 1081 } 1082 std::optional<std::string> FindImpureCall( 1083 FoldingContext &context, const ProcedureRef &proc) { 1084 return FindImpureCallHelper{context}(proc); 1085 } 1086 1087 // Common handling for procedure pointer compatibility of left- and right-hand 1088 // sides. Returns nullopt if they're compatible. Otherwise, it returns a 1089 // message that needs to be augmented by the names of the left and right sides 1090 // and the content of the "whyNotCompatible" string. 1091 std::optional<parser::MessageFixedText> CheckProcCompatibility(bool isCall, 1092 const std::optional<characteristics::Procedure> &lhsProcedure, 1093 const characteristics::Procedure *rhsProcedure, 1094 const SpecificIntrinsic *specificIntrinsic, std::string &whyNotCompatible, 1095 std::optional<std::string> &warning, bool ignoreImplicitVsExplicit) { 1096 std::optional<parser::MessageFixedText> msg; 1097 if (!lhsProcedure) { 1098 msg = "In assignment to object %s, the target '%s' is a procedure" 1099 " designator"_err_en_US; 1100 } else if (!rhsProcedure) { 1101 msg = "In assignment to procedure %s, the characteristics of the target" 1102 " procedure '%s' could not be determined"_err_en_US; 1103 } else if (!isCall && lhsProcedure->functionResult && 1104 rhsProcedure->functionResult && 1105 !lhsProcedure->functionResult->IsCompatibleWith( 1106 *rhsProcedure->functionResult, &whyNotCompatible)) { 1107 msg = 1108 "Function %s associated with incompatible function designator '%s': %s"_err_en_US; 1109 } else if (lhsProcedure->IsCompatibleWith(*rhsProcedure, 1110 ignoreImplicitVsExplicit, &whyNotCompatible, specificIntrinsic, 1111 &warning)) { 1112 // OK 1113 } else if (isCall) { 1114 msg = "Procedure %s associated with result of reference to function '%s'" 1115 " that is an incompatible procedure pointer: %s"_err_en_US; 1116 } else if (lhsProcedure->IsPure() && !rhsProcedure->IsPure()) { 1117 msg = "PURE procedure %s may not be associated with non-PURE" 1118 " procedure designator '%s'"_err_en_US; 1119 } else if (lhsProcedure->IsFunction() && rhsProcedure->IsSubroutine()) { 1120 msg = "Function %s may not be associated with subroutine" 1121 " designator '%s'"_err_en_US; 1122 } else if (lhsProcedure->IsSubroutine() && rhsProcedure->IsFunction()) { 1123 msg = "Subroutine %s may not be associated with function" 1124 " designator '%s'"_err_en_US; 1125 } else if (lhsProcedure->HasExplicitInterface() && 1126 !rhsProcedure->HasExplicitInterface()) { 1127 // Section 10.2.2.4, paragraph 3 prohibits associating a procedure pointer 1128 // that has an explicit interface with a procedure whose characteristics 1129 // don't match. That's the case if the target procedure has an implicit 1130 // interface. But this case is allowed by several other compilers as long 1131 // as the explicit interface can be called via an implicit interface. 1132 if (!lhsProcedure->CanBeCalledViaImplicitInterface()) { 1133 msg = "Procedure %s with explicit interface that cannot be called via " 1134 "an implicit interface cannot be associated with procedure " 1135 "designator with an implicit interface"_err_en_US; 1136 } 1137 } else if (!lhsProcedure->HasExplicitInterface() && 1138 rhsProcedure->HasExplicitInterface()) { 1139 // OK if the target can be called via an implicit interface 1140 if (!rhsProcedure->CanBeCalledViaImplicitInterface() && 1141 !specificIntrinsic) { 1142 msg = "Procedure %s with implicit interface may not be associated " 1143 "with procedure designator '%s' with explicit interface that " 1144 "cannot be called via an implicit interface"_err_en_US; 1145 } 1146 } else { 1147 msg = "Procedure %s associated with incompatible procedure" 1148 " designator '%s': %s"_err_en_US; 1149 } 1150 return msg; 1151 } 1152 1153 // GetLastPointerSymbol() 1154 static const Symbol *GetLastPointerSymbol(const Symbol &symbol) { 1155 return IsPointer(GetAssociationRoot(symbol)) ? &symbol : nullptr; 1156 } 1157 static const Symbol *GetLastPointerSymbol(const SymbolRef &symbol) { 1158 return GetLastPointerSymbol(*symbol); 1159 } 1160 static const Symbol *GetLastPointerSymbol(const Component &x) { 1161 const Symbol &c{x.GetLastSymbol()}; 1162 return IsPointer(c) ? &c : GetLastPointerSymbol(x.base()); 1163 } 1164 static const Symbol *GetLastPointerSymbol(const NamedEntity &x) { 1165 const auto *c{x.UnwrapComponent()}; 1166 return c ? GetLastPointerSymbol(*c) : GetLastPointerSymbol(x.GetLastSymbol()); 1167 } 1168 static const Symbol *GetLastPointerSymbol(const ArrayRef &x) { 1169 return GetLastPointerSymbol(x.base()); 1170 } 1171 static const Symbol *GetLastPointerSymbol(const CoarrayRef &x) { 1172 return nullptr; 1173 } 1174 const Symbol *GetLastPointerSymbol(const DataRef &x) { 1175 return common::visit( 1176 [](const auto &y) { return GetLastPointerSymbol(y); }, x.u); 1177 } 1178 1179 template <TypeCategory TO, TypeCategory FROM> 1180 static std::optional<Expr<SomeType>> DataConstantConversionHelper( 1181 FoldingContext &context, const DynamicType &toType, 1182 const Expr<SomeType> &expr) { 1183 DynamicType sizedType{FROM, toType.kind()}; 1184 if (auto sized{ 1185 Fold(context, ConvertToType(sizedType, Expr<SomeType>{expr}))}) { 1186 if (const auto *someExpr{UnwrapExpr<Expr<SomeKind<FROM>>>(*sized)}) { 1187 return common::visit( 1188 [](const auto &w) -> std::optional<Expr<SomeType>> { 1189 using FromType = ResultType<decltype(w)>; 1190 static constexpr int kind{FromType::kind}; 1191 if constexpr (IsValidKindOfIntrinsicType(TO, kind)) { 1192 if (const auto *fromConst{UnwrapExpr<Constant<FromType>>(w)}) { 1193 using FromWordType = typename FromType::Scalar; 1194 using LogicalType = value::Logical<FromWordType::bits>; 1195 using ElementType = 1196 std::conditional_t<TO == TypeCategory::Logical, LogicalType, 1197 typename LogicalType::Word>; 1198 std::vector<ElementType> values; 1199 auto at{fromConst->lbounds()}; 1200 auto shape{fromConst->shape()}; 1201 for (auto n{GetSize(shape)}; n-- > 0; 1202 fromConst->IncrementSubscripts(at)) { 1203 auto elt{fromConst->At(at)}; 1204 if constexpr (TO == TypeCategory::Logical) { 1205 values.emplace_back(std::move(elt)); 1206 } else { 1207 values.emplace_back(elt.word()); 1208 } 1209 } 1210 return {AsGenericExpr(AsExpr(Constant<Type<TO, kind>>{ 1211 std::move(values), std::move(shape)}))}; 1212 } 1213 } 1214 return std::nullopt; 1215 }, 1216 someExpr->u); 1217 } 1218 } 1219 return std::nullopt; 1220 } 1221 1222 std::optional<Expr<SomeType>> DataConstantConversionExtension( 1223 FoldingContext &context, const DynamicType &toType, 1224 const Expr<SomeType> &expr0) { 1225 Expr<SomeType> expr{Fold(context, Expr<SomeType>{expr0})}; 1226 if (!IsActuallyConstant(expr)) { 1227 return std::nullopt; 1228 } 1229 if (auto fromType{expr.GetType()}) { 1230 if (toType.category() == TypeCategory::Logical && 1231 fromType->category() == TypeCategory::Integer) { 1232 return DataConstantConversionHelper<TypeCategory::Logical, 1233 TypeCategory::Integer>(context, toType, expr); 1234 } 1235 if (toType.category() == TypeCategory::Integer && 1236 fromType->category() == TypeCategory::Logical) { 1237 return DataConstantConversionHelper<TypeCategory::Integer, 1238 TypeCategory::Logical>(context, toType, expr); 1239 } 1240 } 1241 return std::nullopt; 1242 } 1243 1244 bool IsAllocatableOrPointerObject(const Expr<SomeType> &expr) { 1245 const semantics::Symbol *sym{UnwrapWholeSymbolOrComponentDataRef(expr)}; 1246 return (sym && 1247 semantics::IsAllocatableOrObjectPointer(&sym->GetUltimate())) || 1248 evaluate::IsObjectPointer(expr); 1249 } 1250 1251 bool IsAllocatableDesignator(const Expr<SomeType> &expr) { 1252 // Allocatable sub-objects are not themselves allocatable (9.5.3.1 NOTE 2). 1253 if (const semantics::Symbol * 1254 sym{UnwrapWholeSymbolOrComponentOrCoarrayRef(expr)}) { 1255 return semantics::IsAllocatable(sym->GetUltimate()); 1256 } 1257 return false; 1258 } 1259 1260 bool MayBePassedAsAbsentOptional(const Expr<SomeType> &expr) { 1261 const semantics::Symbol *sym{UnwrapWholeSymbolOrComponentDataRef(expr)}; 1262 // 15.5.2.12 1. is pretty clear that an unallocated allocatable/pointer actual 1263 // may be passed to a non-allocatable/non-pointer optional dummy. Note that 1264 // other compilers (like nag, nvfortran, ifort, gfortran and xlf) seems to 1265 // ignore this point in intrinsic contexts (e.g CMPLX argument). 1266 return (sym && semantics::IsOptional(*sym)) || 1267 IsAllocatableOrPointerObject(expr); 1268 } 1269 1270 std::optional<Expr<SomeType>> HollerithToBOZ(FoldingContext &context, 1271 const Expr<SomeType> &expr, const DynamicType &type) { 1272 if (std::optional<std::string> chValue{GetScalarConstantValue<Ascii>(expr)}) { 1273 // Pad on the right with spaces when short, truncate the right if long. 1274 // TODO: big-endian targets 1275 auto bytes{static_cast<std::size_t>( 1276 ToInt64(type.MeasureSizeInBytes(context, false)).value())}; 1277 BOZLiteralConstant bits{0}; 1278 for (std::size_t j{0}; j < bytes; ++j) { 1279 char ch{j >= chValue->size() ? ' ' : chValue->at(j)}; 1280 BOZLiteralConstant chBOZ{static_cast<unsigned char>(ch)}; 1281 bits = bits.IOR(chBOZ.SHIFTL(8 * j)); 1282 } 1283 return ConvertToType(type, Expr<SomeType>{bits}); 1284 } else { 1285 return std::nullopt; 1286 } 1287 } 1288 1289 // Extracts a whole symbol being used as a bound of a dummy argument, 1290 // possibly wrapped with parentheses or MAX(0, ...). 1291 template <int KIND> 1292 static const Symbol *GetBoundSymbol( 1293 const Expr<Type<TypeCategory::Integer, KIND>> &expr) { 1294 using T = Type<TypeCategory::Integer, KIND>; 1295 return common::visit( 1296 common::visitors{ 1297 [](const Extremum<T> &max) -> const Symbol * { 1298 if (max.ordering == Ordering::Greater) { 1299 if (auto zero{ToInt64(max.left())}; zero && *zero == 0) { 1300 return GetBoundSymbol(max.right()); 1301 } 1302 } 1303 return nullptr; 1304 }, 1305 [](const Parentheses<T> &x) { return GetBoundSymbol(x.left()); }, 1306 [](const Designator<T> &x) -> const Symbol * { 1307 if (const auto *ref{std::get_if<SymbolRef>(&x.u)}) { 1308 return &**ref; 1309 } 1310 return nullptr; 1311 }, 1312 [](const Convert<T, TypeCategory::Integer> &x) { 1313 return common::visit( 1314 [](const auto &y) -> const Symbol * { 1315 using yType = std::decay_t<decltype(y)>; 1316 using yResult = typename yType::Result; 1317 if constexpr (yResult::kind <= KIND) { 1318 return GetBoundSymbol(y); 1319 } else { 1320 return nullptr; 1321 } 1322 }, 1323 x.left().u); 1324 }, 1325 [](const auto &) -> const Symbol * { return nullptr; }, 1326 }, 1327 expr.u); 1328 } 1329 1330 std::optional<bool> AreEquivalentInInterface( 1331 const Expr<SubscriptInteger> &x, const Expr<SubscriptInteger> &y) { 1332 auto xVal{ToInt64(x)}; 1333 auto yVal{ToInt64(y)}; 1334 if (xVal && yVal) { 1335 return *xVal == *yVal; 1336 } else if (xVal || yVal) { 1337 return false; 1338 } 1339 const Symbol *xSym{GetBoundSymbol(x)}; 1340 const Symbol *ySym{GetBoundSymbol(y)}; 1341 if (xSym && ySym) { 1342 if (&xSym->GetUltimate() == &ySym->GetUltimate()) { 1343 return true; // USE/host associated same symbol 1344 } 1345 auto xNum{semantics::GetDummyArgumentNumber(xSym)}; 1346 auto yNum{semantics::GetDummyArgumentNumber(ySym)}; 1347 if (xNum && yNum) { 1348 if (*xNum == *yNum) { 1349 auto xType{DynamicType::From(*xSym)}; 1350 auto yType{DynamicType::From(*ySym)}; 1351 return xType && yType && xType->IsEquivalentTo(*yType); 1352 } 1353 } 1354 return false; 1355 } else if (xSym || ySym) { 1356 return false; 1357 } 1358 // Neither expression is an integer constant or a whole symbol. 1359 if (x == y) { 1360 return true; 1361 } else { 1362 return std::nullopt; // not sure 1363 } 1364 } 1365 1366 bool CheckForCoindexedObject(parser::ContextualMessages &messages, 1367 const std::optional<ActualArgument> &arg, const std::string &procName, 1368 const std::string &argName) { 1369 if (arg && ExtractCoarrayRef(arg->UnwrapExpr())) { 1370 messages.Say(arg->sourceLocation(), 1371 "'%s' argument to '%s' may not be a coindexed object"_err_en_US, 1372 argName, procName); 1373 return false; 1374 } else { 1375 return true; 1376 } 1377 } 1378 1379 } // namespace Fortran::evaluate 1380 1381 namespace Fortran::semantics { 1382 1383 const Symbol &ResolveAssociations(const Symbol &original) { 1384 const Symbol &symbol{original.GetUltimate()}; 1385 if (const auto *details{symbol.detailsIf<AssocEntityDetails>()}) { 1386 if (!details->rank()) { // Not RANK(n) or RANK(*) 1387 if (const Symbol * nested{UnwrapWholeSymbolDataRef(details->expr())}) { 1388 return ResolveAssociations(*nested); 1389 } 1390 } 1391 } 1392 return symbol; 1393 } 1394 1395 // When a construct association maps to a variable, and that variable 1396 // is not an array with a vector-valued subscript, return the base 1397 // Symbol of that variable, else nullptr. Descends into other construct 1398 // associations when one associations maps to another. 1399 static const Symbol *GetAssociatedVariable(const AssocEntityDetails &details) { 1400 if (const auto &expr{details.expr()}) { 1401 if (IsVariable(*expr) && !HasVectorSubscript(*expr)) { 1402 if (const Symbol * varSymbol{GetFirstSymbol(*expr)}) { 1403 return &GetAssociationRoot(*varSymbol); 1404 } 1405 } 1406 } 1407 return nullptr; 1408 } 1409 1410 const Symbol &GetAssociationRoot(const Symbol &original) { 1411 const Symbol &symbol{ResolveAssociations(original)}; 1412 if (const auto *details{symbol.detailsIf<AssocEntityDetails>()}) { 1413 if (const Symbol * root{GetAssociatedVariable(*details)}) { 1414 return *root; 1415 } 1416 } 1417 return symbol; 1418 } 1419 1420 const Symbol *GetMainEntry(const Symbol *symbol) { 1421 if (symbol) { 1422 if (const auto *subpDetails{symbol->detailsIf<SubprogramDetails>()}) { 1423 if (const Scope * scope{subpDetails->entryScope()}) { 1424 if (const Symbol * main{scope->symbol()}) { 1425 return main; 1426 } 1427 } 1428 } 1429 } 1430 return symbol; 1431 } 1432 1433 bool IsVariableName(const Symbol &original) { 1434 const Symbol &ultimate{original.GetUltimate()}; 1435 return !IsNamedConstant(ultimate) && 1436 (ultimate.has<ObjectEntityDetails>() || 1437 ultimate.has<AssocEntityDetails>()); 1438 } 1439 1440 static bool IsPureProcedureImpl( 1441 const Symbol &original, semantics::UnorderedSymbolSet &set) { 1442 // An ENTRY is pure if its containing subprogram is 1443 const Symbol &symbol{DEREF(GetMainEntry(&original.GetUltimate()))}; 1444 if (set.find(symbol) != set.end()) { 1445 return true; 1446 } 1447 set.emplace(symbol); 1448 if (const auto *procDetails{symbol.detailsIf<ProcEntityDetails>()}) { 1449 if (procDetails->procInterface()) { 1450 // procedure with a pure interface 1451 return IsPureProcedureImpl(*procDetails->procInterface(), set); 1452 } 1453 } else if (const auto *details{symbol.detailsIf<ProcBindingDetails>()}) { 1454 return IsPureProcedureImpl(details->symbol(), set); 1455 } else if (!IsProcedure(symbol)) { 1456 return false; 1457 } 1458 if (IsStmtFunction(symbol)) { 1459 // Section 15.7(1) states that a statement function is PURE if it does not 1460 // reference an IMPURE procedure or a VOLATILE variable 1461 if (const auto &expr{symbol.get<SubprogramDetails>().stmtFunction()}) { 1462 for (const SymbolRef &ref : evaluate::CollectSymbols(*expr)) { 1463 if (&*ref == &symbol) { 1464 return false; // error recovery, recursion is caught elsewhere 1465 } 1466 if (IsFunction(*ref) && !IsPureProcedureImpl(*ref, set)) { 1467 return false; 1468 } 1469 if (ref->GetUltimate().attrs().test(Attr::VOLATILE)) { 1470 return false; 1471 } 1472 } 1473 } 1474 return true; // statement function was not found to be impure 1475 } 1476 return symbol.attrs().test(Attr::PURE) || 1477 (symbol.attrs().test(Attr::ELEMENTAL) && 1478 !symbol.attrs().test(Attr::IMPURE)); 1479 } 1480 1481 bool IsPureProcedure(const Symbol &original) { 1482 semantics::UnorderedSymbolSet set; 1483 return IsPureProcedureImpl(original, set); 1484 } 1485 1486 bool IsPureProcedure(const Scope &scope) { 1487 const Symbol *symbol{scope.GetSymbol()}; 1488 return symbol && IsPureProcedure(*symbol); 1489 } 1490 1491 bool IsExplicitlyImpureProcedure(const Symbol &original) { 1492 // An ENTRY is IMPURE if its containing subprogram is so 1493 return DEREF(GetMainEntry(&original.GetUltimate())) 1494 .attrs() 1495 .test(Attr::IMPURE); 1496 } 1497 1498 bool IsElementalProcedure(const Symbol &original) { 1499 // An ENTRY is elemental if its containing subprogram is 1500 const Symbol &symbol{DEREF(GetMainEntry(&original.GetUltimate()))}; 1501 if (IsProcedure(symbol)) { 1502 auto &foldingContext{symbol.owner().context().foldingContext()}; 1503 auto restorer{foldingContext.messages().DiscardMessages()}; 1504 auto proc{evaluate::characteristics::Procedure::Characterize( 1505 symbol, foldingContext)}; 1506 return proc && 1507 proc->attrs.test(evaluate::characteristics::Procedure::Attr::Elemental); 1508 } else { 1509 return false; 1510 } 1511 } 1512 1513 bool IsFunction(const Symbol &symbol) { 1514 const Symbol &ultimate{symbol.GetUltimate()}; 1515 return ultimate.test(Symbol::Flag::Function) || 1516 (!ultimate.test(Symbol::Flag::Subroutine) && 1517 common::visit( 1518 common::visitors{ 1519 [](const SubprogramDetails &x) { return x.isFunction(); }, 1520 [](const ProcEntityDetails &x) { 1521 const Symbol *ifc{x.procInterface()}; 1522 return x.type() || (ifc && IsFunction(*ifc)); 1523 }, 1524 [](const ProcBindingDetails &x) { 1525 return IsFunction(x.symbol()); 1526 }, 1527 [](const auto &) { return false; }, 1528 }, 1529 ultimate.details())); 1530 } 1531 1532 bool IsFunction(const Scope &scope) { 1533 const Symbol *symbol{scope.GetSymbol()}; 1534 return symbol && IsFunction(*symbol); 1535 } 1536 1537 bool IsProcedure(const Symbol &symbol) { 1538 return common::visit(common::visitors{ 1539 [&symbol](const SubprogramDetails &) { 1540 const Scope *scope{symbol.scope()}; 1541 // Main programs & BLOCK DATA are not procedures. 1542 return !scope || 1543 scope->kind() == Scope::Kind::Subprogram; 1544 }, 1545 [](const SubprogramNameDetails &) { return true; }, 1546 [](const ProcEntityDetails &) { return true; }, 1547 [](const GenericDetails &) { return true; }, 1548 [](const ProcBindingDetails &) { return true; }, 1549 [](const auto &) { return false; }, 1550 }, 1551 symbol.GetUltimate().details()); 1552 } 1553 1554 bool IsProcedure(const Scope &scope) { 1555 const Symbol *symbol{scope.GetSymbol()}; 1556 return symbol && IsProcedure(*symbol); 1557 } 1558 1559 bool IsProcedurePointer(const Symbol &original) { 1560 const Symbol &symbol{GetAssociationRoot(original)}; 1561 return IsPointer(symbol) && IsProcedure(symbol); 1562 } 1563 1564 bool IsProcedurePointer(const Symbol *symbol) { 1565 return symbol && IsProcedurePointer(*symbol); 1566 } 1567 1568 bool IsObjectPointer(const Symbol *original) { 1569 if (original) { 1570 const Symbol &symbol{GetAssociationRoot(*original)}; 1571 return IsPointer(symbol) && !IsProcedure(symbol); 1572 } else { 1573 return false; 1574 } 1575 } 1576 1577 bool IsAllocatableOrObjectPointer(const Symbol *original) { 1578 if (original) { 1579 const Symbol &ultimate{original->GetUltimate()}; 1580 if (const auto *assoc{ultimate.detailsIf<AssocEntityDetails>()}) { 1581 // Only SELECT RANK construct entities can be ALLOCATABLE/POINTER. 1582 return (assoc->rank() || assoc->IsAssumedSize() || 1583 assoc->IsAssumedRank()) && 1584 IsAllocatableOrObjectPointer(UnwrapWholeSymbolDataRef(assoc->expr())); 1585 } else { 1586 return IsAllocatable(ultimate) || 1587 (IsPointer(ultimate) && !IsProcedure(ultimate)); 1588 } 1589 } else { 1590 return false; 1591 } 1592 } 1593 1594 const Symbol *FindCommonBlockContaining(const Symbol &original) { 1595 const Symbol &root{GetAssociationRoot(original)}; 1596 const auto *details{root.detailsIf<ObjectEntityDetails>()}; 1597 return details ? details->commonBlock() : nullptr; 1598 } 1599 1600 // 3.11 automatic data object 1601 bool IsAutomatic(const Symbol &original) { 1602 const Symbol &symbol{original.GetUltimate()}; 1603 if (const auto *object{symbol.detailsIf<ObjectEntityDetails>()}) { 1604 if (!object->isDummy() && !IsAllocatable(symbol) && !IsPointer(symbol)) { 1605 if (const DeclTypeSpec * type{symbol.GetType()}) { 1606 // If a type parameter value is not a constant expression, the 1607 // object is automatic. 1608 if (type->category() == DeclTypeSpec::Character) { 1609 if (const auto &length{ 1610 type->characterTypeSpec().length().GetExplicit()}) { 1611 if (!evaluate::IsConstantExpr(*length)) { 1612 return true; 1613 } 1614 } 1615 } else if (const DerivedTypeSpec * derived{type->AsDerived()}) { 1616 for (const auto &pair : derived->parameters()) { 1617 if (const auto &value{pair.second.GetExplicit()}) { 1618 if (!evaluate::IsConstantExpr(*value)) { 1619 return true; 1620 } 1621 } 1622 } 1623 } 1624 } 1625 // If an array bound is not a constant expression, the object is 1626 // automatic. 1627 for (const ShapeSpec &dim : object->shape()) { 1628 if (const auto &lb{dim.lbound().GetExplicit()}) { 1629 if (!evaluate::IsConstantExpr(*lb)) { 1630 return true; 1631 } 1632 } 1633 if (const auto &ub{dim.ubound().GetExplicit()}) { 1634 if (!evaluate::IsConstantExpr(*ub)) { 1635 return true; 1636 } 1637 } 1638 } 1639 } 1640 } 1641 return false; 1642 } 1643 1644 bool IsSaved(const Symbol &original) { 1645 const Symbol &symbol{GetAssociationRoot(original)}; 1646 const Scope &scope{symbol.owner()}; 1647 const common::LanguageFeatureControl &features{ 1648 scope.context().languageFeatures()}; 1649 auto scopeKind{scope.kind()}; 1650 if (symbol.has<AssocEntityDetails>()) { 1651 return false; // ASSOCIATE(non-variable) 1652 } else if (scopeKind == Scope::Kind::DerivedType) { 1653 return false; // this is a component 1654 } else if (symbol.attrs().test(Attr::SAVE)) { 1655 return true; // explicit SAVE attribute 1656 } else if (IsDummy(symbol) || IsFunctionResult(symbol) || 1657 IsAutomatic(symbol) || IsNamedConstant(symbol)) { 1658 return false; 1659 } else if (scopeKind == Scope::Kind::Module || 1660 (scopeKind == Scope::Kind::MainProgram && 1661 (symbol.attrs().test(Attr::TARGET) || evaluate::IsCoarray(symbol)))) { 1662 // 8.5.16p4 1663 // In main programs, implied SAVE matters only for pointer 1664 // initialization targets and coarrays. 1665 return true; 1666 } else if (scopeKind == Scope::Kind::MainProgram && 1667 (features.IsEnabled(common::LanguageFeature::SaveMainProgram) || 1668 (features.IsEnabled( 1669 common::LanguageFeature::SaveBigMainProgramVariables) && 1670 symbol.size() > 32))) { 1671 // With SaveBigMainProgramVariables, keeping all unsaved main program 1672 // variables of 32 bytes or less on the stack allows keeping numerical and 1673 // logical scalars, small scalar characters or derived, small arrays, and 1674 // scalar descriptors on the stack. This leaves more room for lower level 1675 // optimizers to do register promotion or get easy aliasing information. 1676 return true; 1677 } else if (features.IsEnabled(common::LanguageFeature::DefaultSave) && 1678 (scopeKind == Scope::Kind::MainProgram || 1679 (scope.kind() == Scope::Kind::Subprogram && 1680 !(scope.symbol() && 1681 scope.symbol()->attrs().test(Attr::RECURSIVE))))) { 1682 // -fno-automatic/-save/-Msave option applies to all objects in executable 1683 // main programs and subprograms unless they are explicitly RECURSIVE. 1684 return true; 1685 } else if (symbol.test(Symbol::Flag::InDataStmt)) { 1686 return true; 1687 } else if (const auto *object{symbol.detailsIf<ObjectEntityDetails>()}; 1688 object && object->init()) { 1689 return true; 1690 } else if (IsProcedurePointer(symbol) && symbol.has<ProcEntityDetails>() && 1691 symbol.get<ProcEntityDetails>().init()) { 1692 return true; 1693 } else if (scope.hasSAVE()) { 1694 return true; // bare SAVE statement 1695 } else if (const Symbol * block{FindCommonBlockContaining(symbol)}; 1696 block && block->attrs().test(Attr::SAVE)) { 1697 return true; // in COMMON with SAVE 1698 } else { 1699 return false; 1700 } 1701 } 1702 1703 bool IsDummy(const Symbol &symbol) { 1704 return common::visit( 1705 common::visitors{[](const EntityDetails &x) { return x.isDummy(); }, 1706 [](const ObjectEntityDetails &x) { return x.isDummy(); }, 1707 [](const ProcEntityDetails &x) { return x.isDummy(); }, 1708 [](const SubprogramDetails &x) { return x.isDummy(); }, 1709 [](const auto &) { return false; }}, 1710 ResolveAssociations(symbol).details()); 1711 } 1712 1713 bool IsAssumedShape(const Symbol &symbol) { 1714 const Symbol &ultimate{ResolveAssociations(symbol)}; 1715 const auto *object{ultimate.detailsIf<ObjectEntityDetails>()}; 1716 return object && object->IsAssumedShape() && 1717 !semantics::IsAllocatableOrObjectPointer(&ultimate); 1718 } 1719 1720 bool IsDeferredShape(const Symbol &symbol) { 1721 const Symbol &ultimate{ResolveAssociations(symbol)}; 1722 const auto *object{ultimate.detailsIf<ObjectEntityDetails>()}; 1723 return object && object->CanBeDeferredShape() && 1724 semantics::IsAllocatableOrObjectPointer(&ultimate); 1725 } 1726 1727 bool IsFunctionResult(const Symbol &original) { 1728 const Symbol &symbol{GetAssociationRoot(original)}; 1729 return common::visit( 1730 common::visitors{ 1731 [](const EntityDetails &x) { return x.isFuncResult(); }, 1732 [](const ObjectEntityDetails &x) { return x.isFuncResult(); }, 1733 [](const ProcEntityDetails &x) { return x.isFuncResult(); }, 1734 [](const auto &) { return false; }, 1735 }, 1736 symbol.details()); 1737 } 1738 1739 bool IsKindTypeParameter(const Symbol &symbol) { 1740 const auto *param{symbol.GetUltimate().detailsIf<TypeParamDetails>()}; 1741 return param && param->attr() == common::TypeParamAttr::Kind; 1742 } 1743 1744 bool IsLenTypeParameter(const Symbol &symbol) { 1745 const auto *param{symbol.GetUltimate().detailsIf<TypeParamDetails>()}; 1746 return param && param->attr() == common::TypeParamAttr::Len; 1747 } 1748 1749 bool IsExtensibleType(const DerivedTypeSpec *derived) { 1750 return !IsSequenceOrBindCType(derived) && !IsIsoCType(derived); 1751 } 1752 1753 bool IsSequenceOrBindCType(const DerivedTypeSpec *derived) { 1754 return derived && 1755 (derived->typeSymbol().attrs().test(Attr::BIND_C) || 1756 derived->typeSymbol().get<DerivedTypeDetails>().sequence()); 1757 } 1758 1759 static bool IsSameModule(const Scope *x, const Scope *y) { 1760 if (x == y) { 1761 return true; 1762 } else if (x && y) { 1763 // Allow for a builtin module to be read from distinct paths 1764 const Symbol *xSym{x->symbol()}; 1765 const Symbol *ySym{y->symbol()}; 1766 if (xSym && ySym && xSym->name() == ySym->name()) { 1767 const auto *xMod{xSym->detailsIf<ModuleDetails>()}; 1768 const auto *yMod{ySym->detailsIf<ModuleDetails>()}; 1769 if (xMod && yMod) { 1770 auto xHash{xMod->moduleFileHash()}; 1771 auto yHash{yMod->moduleFileHash()}; 1772 return xHash && yHash && *xHash == *yHash; 1773 } 1774 } 1775 } 1776 return false; 1777 } 1778 1779 bool IsBuiltinDerivedType(const DerivedTypeSpec *derived, const char *name) { 1780 if (derived) { 1781 const auto &symbol{derived->typeSymbol()}; 1782 const Scope &scope{symbol.owner()}; 1783 return symbol.name() == "__builtin_"s + name && 1784 IsSameModule(&scope, scope.context().GetBuiltinsScope()); 1785 } else { 1786 return false; 1787 } 1788 } 1789 1790 bool IsBuiltinCPtr(const Symbol &symbol) { 1791 if (const DeclTypeSpec *declType = symbol.GetType()) { 1792 if (const DerivedTypeSpec *derived = declType->AsDerived()) { 1793 return IsIsoCType(derived); 1794 } 1795 } 1796 return false; 1797 } 1798 1799 bool IsIsoCType(const DerivedTypeSpec *derived) { 1800 return IsBuiltinDerivedType(derived, "c_ptr") || 1801 IsBuiltinDerivedType(derived, "c_funptr"); 1802 } 1803 1804 bool IsEventType(const DerivedTypeSpec *derived) { 1805 return IsBuiltinDerivedType(derived, "event_type"); 1806 } 1807 1808 bool IsLockType(const DerivedTypeSpec *derived) { 1809 return IsBuiltinDerivedType(derived, "lock_type"); 1810 } 1811 1812 bool IsNotifyType(const DerivedTypeSpec *derived) { 1813 return IsBuiltinDerivedType(derived, "notify_type"); 1814 } 1815 1816 bool IsIeeeFlagType(const DerivedTypeSpec *derived) { 1817 return IsBuiltinDerivedType(derived, "ieee_flag_type"); 1818 } 1819 1820 bool IsIeeeRoundType(const DerivedTypeSpec *derived) { 1821 return IsBuiltinDerivedType(derived, "ieee_round_type"); 1822 } 1823 1824 bool IsTeamType(const DerivedTypeSpec *derived) { 1825 return IsBuiltinDerivedType(derived, "team_type"); 1826 } 1827 1828 bool IsBadCoarrayType(const DerivedTypeSpec *derived) { 1829 return IsTeamType(derived) || IsIsoCType(derived); 1830 } 1831 1832 bool IsEventTypeOrLockType(const DerivedTypeSpec *derivedTypeSpec) { 1833 return IsEventType(derivedTypeSpec) || IsLockType(derivedTypeSpec); 1834 } 1835 1836 int CountLenParameters(const DerivedTypeSpec &type) { 1837 return llvm::count_if( 1838 type.parameters(), [](const auto &pair) { return pair.second.isLen(); }); 1839 } 1840 1841 int CountNonConstantLenParameters(const DerivedTypeSpec &type) { 1842 return llvm::count_if(type.parameters(), [](const auto &pair) { 1843 if (!pair.second.isLen()) { 1844 return false; 1845 } else if (const auto &expr{pair.second.GetExplicit()}) { 1846 return !IsConstantExpr(*expr); 1847 } else { 1848 return true; 1849 } 1850 }); 1851 } 1852 1853 const Symbol &GetUsedModule(const UseDetails &details) { 1854 return DEREF(details.symbol().owner().symbol()); 1855 } 1856 1857 static const Symbol *FindFunctionResult( 1858 const Symbol &original, UnorderedSymbolSet &seen) { 1859 const Symbol &root{GetAssociationRoot(original)}; 1860 ; 1861 if (!seen.insert(root).second) { 1862 return nullptr; // don't loop 1863 } 1864 return common::visit( 1865 common::visitors{[](const SubprogramDetails &subp) { 1866 return subp.isFunction() ? &subp.result() : nullptr; 1867 }, 1868 [&](const ProcEntityDetails &proc) { 1869 const Symbol *iface{proc.procInterface()}; 1870 return iface ? FindFunctionResult(*iface, seen) : nullptr; 1871 }, 1872 [&](const ProcBindingDetails &binding) { 1873 return FindFunctionResult(binding.symbol(), seen); 1874 }, 1875 [](const auto &) -> const Symbol * { return nullptr; }}, 1876 root.details()); 1877 } 1878 1879 const Symbol *FindFunctionResult(const Symbol &symbol) { 1880 UnorderedSymbolSet seen; 1881 return FindFunctionResult(symbol, seen); 1882 } 1883 1884 // These are here in Evaluate/tools.cpp so that Evaluate can use 1885 // them; they cannot be defined in symbol.h due to the dependence 1886 // on Scope. 1887 1888 bool SymbolSourcePositionCompare::operator()( 1889 const SymbolRef &x, const SymbolRef &y) const { 1890 return x->GetSemanticsContext().allCookedSources().Precedes( 1891 x->name(), y->name()); 1892 } 1893 bool SymbolSourcePositionCompare::operator()( 1894 const MutableSymbolRef &x, const MutableSymbolRef &y) const { 1895 return x->GetSemanticsContext().allCookedSources().Precedes( 1896 x->name(), y->name()); 1897 } 1898 1899 SemanticsContext &Symbol::GetSemanticsContext() const { 1900 return DEREF(owner_).context(); 1901 } 1902 1903 bool AreTkCompatibleTypes(const DeclTypeSpec *x, const DeclTypeSpec *y) { 1904 if (x && y) { 1905 if (auto xDt{evaluate::DynamicType::From(*x)}) { 1906 if (auto yDt{evaluate::DynamicType::From(*y)}) { 1907 return xDt->IsTkCompatibleWith(*yDt); 1908 } 1909 } 1910 } 1911 return false; 1912 } 1913 1914 common::IgnoreTKRSet GetIgnoreTKR(const Symbol &symbol) { 1915 common::IgnoreTKRSet result; 1916 if (const auto *object{symbol.detailsIf<ObjectEntityDetails>()}) { 1917 result = object->ignoreTKR(); 1918 if (const Symbol * ownerSymbol{symbol.owner().symbol()}) { 1919 if (const auto *ownerSubp{ownerSymbol->detailsIf<SubprogramDetails>()}) { 1920 if (ownerSubp->defaultIgnoreTKR()) { 1921 result |= common::ignoreTKRAll; 1922 } 1923 } 1924 } 1925 } 1926 return result; 1927 } 1928 1929 std::optional<int> GetDummyArgumentNumber(const Symbol *symbol) { 1930 if (symbol) { 1931 if (IsDummy(*symbol)) { 1932 if (const Symbol * subpSym{symbol->owner().symbol()}) { 1933 if (const auto *subp{subpSym->detailsIf<SubprogramDetails>()}) { 1934 int j{0}; 1935 for (const Symbol *dummy : subp->dummyArgs()) { 1936 if (dummy == symbol) { 1937 return j; 1938 } 1939 ++j; 1940 } 1941 } 1942 } 1943 } 1944 } 1945 return std::nullopt; 1946 } 1947 1948 } // namespace Fortran::semantics 1949