1 //===-- lib/Evaluate/shape.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/shape.h" 10 #include "flang/Common/idioms.h" 11 #include "flang/Common/template.h" 12 #include "flang/Evaluate/characteristics.h" 13 #include "flang/Evaluate/check-expression.h" 14 #include "flang/Evaluate/fold.h" 15 #include "flang/Evaluate/intrinsics.h" 16 #include "flang/Evaluate/tools.h" 17 #include "flang/Evaluate/type.h" 18 #include "flang/Parser/message.h" 19 #include "flang/Semantics/symbol.h" 20 #include <functional> 21 22 using namespace std::placeholders; // _1, _2, &c. for std::bind() 23 24 namespace Fortran::evaluate { 25 26 bool IsImpliedShape(const Symbol &original) { 27 const Symbol &symbol{ResolveAssociations(original)}; 28 const auto *details{symbol.detailsIf<semantics::ObjectEntityDetails>()}; 29 return details && symbol.attrs().test(semantics::Attr::PARAMETER) && 30 details->shape().CanBeImpliedShape(); 31 } 32 33 bool IsExplicitShape(const Symbol &original) { 34 const Symbol &symbol{ResolveAssociations(original)}; 35 if (const auto *details{symbol.detailsIf<semantics::ObjectEntityDetails>()}) { 36 const auto &shape{details->shape()}; 37 return shape.Rank() == 0 || 38 shape.IsExplicitShape(); // true when scalar, too 39 } else { 40 return symbol 41 .has<semantics::AssocEntityDetails>(); // exprs have explicit shape 42 } 43 } 44 45 Shape GetShapeHelper::ConstantShape(const Constant<ExtentType> &arrayConstant) { 46 CHECK(arrayConstant.Rank() == 1); 47 Shape result; 48 std::size_t dimensions{arrayConstant.size()}; 49 for (std::size_t j{0}; j < dimensions; ++j) { 50 Scalar<ExtentType> extent{arrayConstant.values().at(j)}; 51 result.emplace_back(MaybeExtentExpr{ExtentExpr{std::move(extent)}}); 52 } 53 return result; 54 } 55 56 auto GetShapeHelper::AsShapeResult(ExtentExpr &&arrayExpr) const -> Result { 57 if (context_) { 58 arrayExpr = Fold(*context_, std::move(arrayExpr)); 59 } 60 if (const auto *constArray{UnwrapConstantValue<ExtentType>(arrayExpr)}) { 61 return ConstantShape(*constArray); 62 } 63 if (auto *constructor{UnwrapExpr<ArrayConstructor<ExtentType>>(arrayExpr)}) { 64 Shape result; 65 for (auto &value : *constructor) { 66 auto *expr{std::get_if<ExtentExpr>(&value.u)}; 67 if (expr && expr->Rank() == 0) { 68 result.emplace_back(std::move(*expr)); 69 } else { 70 return std::nullopt; 71 } 72 } 73 return result; 74 } else { 75 return std::nullopt; 76 } 77 } 78 79 Shape GetShapeHelper::CreateShape(int rank, NamedEntity &base) const { 80 Shape shape; 81 for (int dimension{0}; dimension < rank; ++dimension) { 82 shape.emplace_back(GetExtent(base, dimension, invariantOnly_)); 83 } 84 return shape; 85 } 86 87 std::optional<ExtentExpr> AsExtentArrayExpr(const Shape &shape) { 88 ArrayConstructorValues<ExtentType> values; 89 for (const auto &dim : shape) { 90 if (dim) { 91 values.Push(common::Clone(*dim)); 92 } else { 93 return std::nullopt; 94 } 95 } 96 return ExtentExpr{ArrayConstructor<ExtentType>{std::move(values)}}; 97 } 98 99 std::optional<Constant<ExtentType>> AsConstantShape( 100 FoldingContext &context, const Shape &shape) { 101 if (auto shapeArray{AsExtentArrayExpr(shape)}) { 102 auto folded{Fold(context, std::move(*shapeArray))}; 103 if (auto *p{UnwrapConstantValue<ExtentType>(folded)}) { 104 return std::move(*p); 105 } 106 } 107 return std::nullopt; 108 } 109 110 Constant<SubscriptInteger> AsConstantShape(const ConstantSubscripts &shape) { 111 using IntType = Scalar<SubscriptInteger>; 112 std::vector<IntType> result; 113 for (auto dim : shape) { 114 result.emplace_back(dim); 115 } 116 return {std::move(result), ConstantSubscripts{GetRank(shape)}}; 117 } 118 119 ConstantSubscripts AsConstantExtents(const Constant<ExtentType> &shape) { 120 ConstantSubscripts result; 121 for (const auto &extent : shape.values()) { 122 result.push_back(extent.ToInt64()); 123 } 124 return result; 125 } 126 127 std::optional<ConstantSubscripts> AsConstantExtents( 128 FoldingContext &context, const Shape &shape) { 129 if (auto shapeConstant{AsConstantShape(context, shape)}) { 130 return AsConstantExtents(*shapeConstant); 131 } else { 132 return std::nullopt; 133 } 134 } 135 136 Shape AsShape(const ConstantSubscripts &shape) { 137 Shape result; 138 for (const auto &extent : shape) { 139 result.emplace_back(ExtentExpr{extent}); 140 } 141 return result; 142 } 143 144 std::optional<Shape> AsShape(const std::optional<ConstantSubscripts> &shape) { 145 if (shape) { 146 return AsShape(*shape); 147 } else { 148 return std::nullopt; 149 } 150 } 151 152 Shape Fold(FoldingContext &context, Shape &&shape) { 153 for (auto &dim : shape) { 154 dim = Fold(context, std::move(dim)); 155 } 156 return std::move(shape); 157 } 158 159 std::optional<Shape> Fold( 160 FoldingContext &context, std::optional<Shape> &&shape) { 161 if (shape) { 162 return Fold(context, std::move(*shape)); 163 } else { 164 return std::nullopt; 165 } 166 } 167 168 static ExtentExpr ComputeTripCount( 169 ExtentExpr &&lower, ExtentExpr &&upper, ExtentExpr &&stride) { 170 ExtentExpr strideCopy{common::Clone(stride)}; 171 ExtentExpr span{ 172 (std::move(upper) - std::move(lower) + std::move(strideCopy)) / 173 std::move(stride)}; 174 return ExtentExpr{ 175 Extremum<ExtentType>{Ordering::Greater, std::move(span), ExtentExpr{0}}}; 176 } 177 178 ExtentExpr CountTrips( 179 ExtentExpr &&lower, ExtentExpr &&upper, ExtentExpr &&stride) { 180 return ComputeTripCount( 181 std::move(lower), std::move(upper), std::move(stride)); 182 } 183 184 ExtentExpr CountTrips(const ExtentExpr &lower, const ExtentExpr &upper, 185 const ExtentExpr &stride) { 186 return ComputeTripCount( 187 common::Clone(lower), common::Clone(upper), common::Clone(stride)); 188 } 189 190 MaybeExtentExpr CountTrips(MaybeExtentExpr &&lower, MaybeExtentExpr &&upper, 191 MaybeExtentExpr &&stride) { 192 std::function<ExtentExpr(ExtentExpr &&, ExtentExpr &&, ExtentExpr &&)> bound{ 193 std::bind(ComputeTripCount, _1, _2, _3)}; 194 return common::MapOptional( 195 std::move(bound), std::move(lower), std::move(upper), std::move(stride)); 196 } 197 198 MaybeExtentExpr GetSize(Shape &&shape) { 199 ExtentExpr extent{1}; 200 for (auto &&dim : std::move(shape)) { 201 if (dim) { 202 extent = std::move(extent) * std::move(*dim); 203 } else { 204 return std::nullopt; 205 } 206 } 207 return extent; 208 } 209 210 ConstantSubscript GetSize(const ConstantSubscripts &shape) { 211 ConstantSubscript size{1}; 212 for (auto dim : shape) { 213 CHECK(dim >= 0); 214 size *= dim; 215 } 216 return size; 217 } 218 219 bool ContainsAnyImpliedDoIndex(const ExtentExpr &expr) { 220 struct MyVisitor : public AnyTraverse<MyVisitor> { 221 using Base = AnyTraverse<MyVisitor>; 222 MyVisitor() : Base{*this} {} 223 using Base::operator(); 224 bool operator()(const ImpliedDoIndex &) { return true; } 225 }; 226 return MyVisitor{}(expr); 227 } 228 229 // Determines lower bound on a dimension. This can be other than 1 only 230 // for a reference to a whole array object or component. (See LBOUND, 16.9.109). 231 // ASSOCIATE construct entities may require traversal of their referents. 232 template <typename RESULT, bool LBOUND_SEMANTICS> 233 class GetLowerBoundHelper 234 : public Traverse<GetLowerBoundHelper<RESULT, LBOUND_SEMANTICS>, RESULT> { 235 public: 236 using Result = RESULT; 237 using Base = Traverse<GetLowerBoundHelper, RESULT>; 238 using Base::operator(); 239 explicit GetLowerBoundHelper( 240 int d, FoldingContext *context, bool invariantOnly) 241 : Base{*this}, dimension_{d}, context_{context}, 242 invariantOnly_{invariantOnly} {} 243 static Result Default() { return Result{1}; } 244 static Result Combine(Result &&, Result &&) { 245 // Operator results and array references always have lower bounds == 1 246 return Result{1}; 247 } 248 249 Result GetLowerBound(const Symbol &symbol0, NamedEntity &&base) const { 250 const Symbol &symbol{symbol0.GetUltimate()}; 251 if (const auto *object{ 252 symbol.detailsIf<semantics::ObjectEntityDetails>()}) { 253 int rank{object->shape().Rank()}; 254 if (dimension_ < rank) { 255 const semantics::ShapeSpec &shapeSpec{object->shape()[dimension_]}; 256 if (shapeSpec.lbound().isExplicit()) { 257 if (const auto &lbound{shapeSpec.lbound().GetExplicit()}) { 258 if constexpr (LBOUND_SEMANTICS) { 259 bool ok{false}; 260 auto lbValue{ToInt64(*lbound)}; 261 if (dimension_ == rank - 1 && object->IsAssumedSize()) { 262 // last dimension of assumed-size dummy array: don't worry 263 // about handling an empty dimension 264 ok = !invariantOnly_ || IsScopeInvariantExpr(*lbound); 265 } else if (lbValue.value_or(0) == 1) { 266 // Lower bound is 1, regardless of extent 267 ok = true; 268 } else if (const auto &ubound{shapeSpec.ubound().GetExplicit()}) { 269 // If we can't prove that the dimension is nonempty, 270 // we must be conservative. 271 // TODO: simple symbolic math in expression rewriting to 272 // cope with cases like A(J:J) 273 if (context_) { 274 auto extent{ToInt64(Fold(*context_, 275 ExtentExpr{*ubound} - ExtentExpr{*lbound} + 276 ExtentExpr{1}))}; 277 if (extent) { 278 if (extent <= 0) { 279 return Result{1}; 280 } 281 ok = true; 282 } else { 283 ok = false; 284 } 285 } else { 286 auto ubValue{ToInt64(*ubound)}; 287 if (lbValue && ubValue) { 288 if (*lbValue > *ubValue) { 289 return Result{1}; 290 } 291 ok = true; 292 } else { 293 ok = false; 294 } 295 } 296 } 297 return ok ? *lbound : Result{}; 298 } else { 299 return *lbound; 300 } 301 } else { 302 return Result{1}; 303 } 304 } 305 if (IsDescriptor(symbol)) { 306 return ExtentExpr{DescriptorInquiry{std::move(base), 307 DescriptorInquiry::Field::LowerBound, dimension_}}; 308 } 309 } 310 } else if (const auto *assoc{ 311 symbol.detailsIf<semantics::AssocEntityDetails>()}) { 312 if (assoc->IsAssumedSize()) { // RANK(*) 313 return Result{1}; 314 } else if (assoc->IsAssumedRank()) { // RANK DEFAULT 315 } else if (assoc->rank()) { // RANK(n) 316 const Symbol &resolved{ResolveAssociations(symbol)}; 317 if (IsDescriptor(resolved) && dimension_ < *assoc->rank()) { 318 return ExtentExpr{DescriptorInquiry{std::move(base), 319 DescriptorInquiry::Field::LowerBound, dimension_}}; 320 } 321 } else { 322 Result exprLowerBound{((*this)(assoc->expr()))}; 323 if (IsActuallyConstant(exprLowerBound)) { 324 return std::move(exprLowerBound); 325 } else { 326 // If the lower bound of the associated entity is not resolved to a 327 // constant expression at the time of the association, it is unsafe 328 // to re-evaluate it later in the associate construct. Statements 329 // in between may have modified its operands value. 330 return ExtentExpr{DescriptorInquiry{std::move(base), 331 DescriptorInquiry::Field::LowerBound, dimension_}}; 332 } 333 } 334 } 335 if constexpr (LBOUND_SEMANTICS) { 336 return Result{}; 337 } else { 338 return Result{1}; 339 } 340 } 341 342 Result operator()(const Symbol &symbol) const { 343 return GetLowerBound(symbol, NamedEntity{symbol}); 344 } 345 346 Result operator()(const Component &component) const { 347 if (component.base().Rank() == 0) { 348 return GetLowerBound( 349 component.GetLastSymbol(), NamedEntity{common::Clone(component)}); 350 } 351 return Result{1}; 352 } 353 354 template <typename T> Result operator()(const Expr<T> &expr) const { 355 if (const Symbol * whole{UnwrapWholeSymbolOrComponentDataRef(expr)}) { 356 return (*this)(*whole); 357 } else if constexpr (common::HasMember<Constant<T>, decltype(expr.u)>) { 358 if (const auto *con{std::get_if<Constant<T>>(&expr.u)}) { 359 ConstantSubscripts lb{con->lbounds()}; 360 if (dimension_ < GetRank(lb)) { 361 return Result{lb[dimension_]}; 362 } 363 } else { // operation 364 return Result{1}; 365 } 366 } else { 367 return (*this)(expr.u); 368 } 369 if constexpr (LBOUND_SEMANTICS) { 370 return Result{}; 371 } else { 372 return Result{1}; 373 } 374 } 375 376 private: 377 int dimension_; // zero-based 378 FoldingContext *context_{nullptr}; 379 bool invariantOnly_{false}; 380 }; 381 382 ExtentExpr GetRawLowerBound( 383 const NamedEntity &base, int dimension, bool invariantOnly) { 384 return GetLowerBoundHelper<ExtentExpr, false>{ 385 dimension, nullptr, invariantOnly}(base); 386 } 387 388 ExtentExpr GetRawLowerBound(FoldingContext &context, const NamedEntity &base, 389 int dimension, bool invariantOnly) { 390 return Fold(context, 391 GetLowerBoundHelper<ExtentExpr, false>{ 392 dimension, &context, invariantOnly}(base)); 393 } 394 395 MaybeExtentExpr GetLBOUND( 396 const NamedEntity &base, int dimension, bool invariantOnly) { 397 return GetLowerBoundHelper<MaybeExtentExpr, true>{ 398 dimension, nullptr, invariantOnly}(base); 399 } 400 401 MaybeExtentExpr GetLBOUND(FoldingContext &context, const NamedEntity &base, 402 int dimension, bool invariantOnly) { 403 return Fold(context, 404 GetLowerBoundHelper<MaybeExtentExpr, true>{ 405 dimension, &context, invariantOnly}(base)); 406 } 407 408 Shape GetRawLowerBounds(const NamedEntity &base, bool invariantOnly) { 409 Shape result; 410 int rank{base.Rank()}; 411 for (int dim{0}; dim < rank; ++dim) { 412 result.emplace_back(GetRawLowerBound(base, dim, invariantOnly)); 413 } 414 return result; 415 } 416 417 Shape GetRawLowerBounds( 418 FoldingContext &context, const NamedEntity &base, bool invariantOnly) { 419 Shape result; 420 int rank{base.Rank()}; 421 for (int dim{0}; dim < rank; ++dim) { 422 result.emplace_back(GetRawLowerBound(context, base, dim, invariantOnly)); 423 } 424 return result; 425 } 426 427 Shape GetLBOUNDs(const NamedEntity &base, bool invariantOnly) { 428 Shape result; 429 int rank{base.Rank()}; 430 for (int dim{0}; dim < rank; ++dim) { 431 result.emplace_back(GetLBOUND(base, dim, invariantOnly)); 432 } 433 return result; 434 } 435 436 Shape GetLBOUNDs( 437 FoldingContext &context, const NamedEntity &base, bool invariantOnly) { 438 Shape result; 439 int rank{base.Rank()}; 440 for (int dim{0}; dim < rank; ++dim) { 441 result.emplace_back(GetLBOUND(context, base, dim, invariantOnly)); 442 } 443 return result; 444 } 445 446 // If the upper and lower bounds are constant, return a constant expression for 447 // the extent. In particular, if the upper bound is less than the lower bound, 448 // return zero. 449 static MaybeExtentExpr GetNonNegativeExtent( 450 const semantics::ShapeSpec &shapeSpec, bool invariantOnly) { 451 const auto &ubound{shapeSpec.ubound().GetExplicit()}; 452 const auto &lbound{shapeSpec.lbound().GetExplicit()}; 453 std::optional<ConstantSubscript> uval{ToInt64(ubound)}; 454 std::optional<ConstantSubscript> lval{ToInt64(lbound)}; 455 if (uval && lval) { 456 if (*uval < *lval) { 457 return ExtentExpr{0}; 458 } else { 459 return ExtentExpr{*uval - *lval + 1}; 460 } 461 } else if (lbound && ubound && 462 (!invariantOnly || 463 (IsScopeInvariantExpr(*lbound) && IsScopeInvariantExpr(*ubound)))) { 464 // Apply effective IDIM (MAX calculation with 0) so thet the 465 // result is never negative 466 if (lval.value_or(0) == 1) { 467 return ExtentExpr{Extremum<SubscriptInteger>{ 468 Ordering::Greater, ExtentExpr{0}, common::Clone(*ubound)}}; 469 } else { 470 return ExtentExpr{ 471 Extremum<SubscriptInteger>{Ordering::Greater, ExtentExpr{0}, 472 common::Clone(*ubound) - common::Clone(*lbound) + ExtentExpr{1}}}; 473 } 474 } else { 475 return std::nullopt; 476 } 477 } 478 479 static MaybeExtentExpr GetAssociatedExtent( 480 const Symbol &symbol, int dimension) { 481 if (const auto *assoc{symbol.detailsIf<semantics::AssocEntityDetails>()}; 482 assoc && !assoc->rank()) { // not SELECT RANK case 483 if (auto shape{GetShape(assoc->expr())}; 484 shape && dimension < static_cast<int>(shape->size())) { 485 if (auto &extent{shape->at(dimension)}; 486 // Don't return a non-constant extent, as the variables that 487 // determine the shape of the selector's expression may change 488 // during execution of the construct. 489 extent && IsActuallyConstant(*extent)) { 490 return std::move(extent); 491 } 492 } 493 } 494 return ExtentExpr{DescriptorInquiry{ 495 NamedEntity{symbol}, DescriptorInquiry::Field::Extent, dimension}}; 496 } 497 498 MaybeExtentExpr GetExtent( 499 const NamedEntity &base, int dimension, bool invariantOnly) { 500 CHECK(dimension >= 0); 501 const Symbol &last{base.GetLastSymbol()}; 502 const Symbol &symbol{ResolveAssociations(last)}; 503 if (const auto *assoc{last.detailsIf<semantics::AssocEntityDetails>()}) { 504 if (assoc->IsAssumedSize() || assoc->IsAssumedRank()) { // RANK(*)/DEFAULT 505 return std::nullopt; 506 } else if (assoc->rank()) { // RANK(n) 507 if (semantics::IsDescriptor(symbol) && dimension < *assoc->rank()) { 508 return ExtentExpr{DescriptorInquiry{ 509 NamedEntity{base}, DescriptorInquiry::Field::Extent, dimension}}; 510 } else { 511 return std::nullopt; 512 } 513 } else { 514 return GetAssociatedExtent(last, dimension); 515 } 516 } 517 if (const auto *details{symbol.detailsIf<semantics::ObjectEntityDetails>()}) { 518 if (IsImpliedShape(symbol) && details->init()) { 519 if (auto shape{GetShape(symbol, invariantOnly)}) { 520 if (dimension < static_cast<int>(shape->size())) { 521 return std::move(shape->at(dimension)); 522 } 523 } 524 } else { 525 int j{0}; 526 for (const auto &shapeSpec : details->shape()) { 527 if (j++ == dimension) { 528 if (auto extent{GetNonNegativeExtent(shapeSpec, invariantOnly)}) { 529 return extent; 530 } else if (details->IsAssumedSize() && j == symbol.Rank()) { 531 break; 532 } else if (semantics::IsDescriptor(symbol)) { 533 return ExtentExpr{DescriptorInquiry{NamedEntity{base}, 534 DescriptorInquiry::Field::Extent, dimension}}; 535 } else { 536 break; 537 } 538 } 539 } 540 } 541 } 542 return std::nullopt; 543 } 544 545 MaybeExtentExpr GetExtent(FoldingContext &context, const NamedEntity &base, 546 int dimension, bool invariantOnly) { 547 return Fold(context, GetExtent(base, dimension, invariantOnly)); 548 } 549 550 MaybeExtentExpr GetExtent(const Subscript &subscript, const NamedEntity &base, 551 int dimension, bool invariantOnly) { 552 return common::visit( 553 common::visitors{ 554 [&](const Triplet &triplet) -> MaybeExtentExpr { 555 MaybeExtentExpr upper{triplet.upper()}; 556 if (!upper) { 557 upper = GetUBOUND(base, dimension, invariantOnly); 558 } 559 MaybeExtentExpr lower{triplet.lower()}; 560 if (!lower) { 561 lower = GetLBOUND(base, dimension, invariantOnly); 562 } 563 return CountTrips(std::move(lower), std::move(upper), 564 MaybeExtentExpr{triplet.stride()}); 565 }, 566 [&](const IndirectSubscriptIntegerExpr &subs) -> MaybeExtentExpr { 567 if (auto shape{GetShape(subs.value())}) { 568 if (GetRank(*shape) > 0) { 569 CHECK(GetRank(*shape) == 1); // vector-valued subscript 570 return std::move(shape->at(0)); 571 } 572 } 573 return std::nullopt; 574 }, 575 }, 576 subscript.u); 577 } 578 579 MaybeExtentExpr GetExtent(FoldingContext &context, const Subscript &subscript, 580 const NamedEntity &base, int dimension, bool invariantOnly) { 581 return Fold(context, GetExtent(subscript, base, dimension, invariantOnly)); 582 } 583 584 MaybeExtentExpr ComputeUpperBound( 585 ExtentExpr &&lower, MaybeExtentExpr &&extent) { 586 if (extent) { 587 if (ToInt64(lower).value_or(0) == 1) { 588 return std::move(*extent); 589 } else { 590 return std::move(*extent) + std::move(lower) - ExtentExpr{1}; 591 } 592 } else { 593 return std::nullopt; 594 } 595 } 596 597 MaybeExtentExpr ComputeUpperBound( 598 FoldingContext &context, ExtentExpr &&lower, MaybeExtentExpr &&extent) { 599 return Fold(context, ComputeUpperBound(std::move(lower), std::move(extent))); 600 } 601 602 MaybeExtentExpr GetRawUpperBound( 603 const NamedEntity &base, int dimension, bool invariantOnly) { 604 const Symbol &symbol{ResolveAssociations(base.GetLastSymbol())}; 605 if (const auto *details{symbol.detailsIf<semantics::ObjectEntityDetails>()}) { 606 int rank{details->shape().Rank()}; 607 if (dimension < rank) { 608 const auto &bound{details->shape()[dimension].ubound().GetExplicit()}; 609 if (bound && (!invariantOnly || IsScopeInvariantExpr(*bound))) { 610 return *bound; 611 } else if (details->IsAssumedSize() && dimension + 1 == symbol.Rank()) { 612 return std::nullopt; 613 } else { 614 return ComputeUpperBound( 615 GetRawLowerBound(base, dimension), GetExtent(base, dimension)); 616 } 617 } 618 } else if (const auto *assoc{ 619 symbol.detailsIf<semantics::AssocEntityDetails>()}) { 620 if (assoc->IsAssumedSize() || assoc->IsAssumedRank()) { 621 return std::nullopt; 622 } else if (assoc->rank() && dimension >= *assoc->rank()) { 623 return std::nullopt; 624 } else if (auto extent{GetAssociatedExtent(symbol, dimension)}) { 625 return ComputeUpperBound( 626 GetRawLowerBound(base, dimension), std::move(extent)); 627 } 628 } 629 return std::nullopt; 630 } 631 632 MaybeExtentExpr GetRawUpperBound(FoldingContext &context, 633 const NamedEntity &base, int dimension, bool invariantOnly) { 634 return Fold(context, GetRawUpperBound(base, dimension, invariantOnly)); 635 } 636 637 static MaybeExtentExpr GetExplicitUBOUND(FoldingContext *context, 638 const semantics::ShapeSpec &shapeSpec, bool invariantOnly) { 639 const auto &ubound{shapeSpec.ubound().GetExplicit()}; 640 if (ubound && (!invariantOnly || IsScopeInvariantExpr(*ubound))) { 641 if (auto extent{GetNonNegativeExtent(shapeSpec, invariantOnly)}) { 642 if (auto cstExtent{ToInt64( 643 context ? Fold(*context, std::move(*extent)) : *extent)}) { 644 if (cstExtent > 0) { 645 return *ubound; 646 } else if (cstExtent == 0) { 647 return ExtentExpr{0}; 648 } 649 } 650 } 651 } 652 return std::nullopt; 653 } 654 655 static MaybeExtentExpr GetUBOUND(FoldingContext *context, 656 const NamedEntity &base, int dimension, bool invariantOnly) { 657 const Symbol &symbol{ResolveAssociations(base.GetLastSymbol())}; 658 if (const auto *details{symbol.detailsIf<semantics::ObjectEntityDetails>()}) { 659 int rank{details->shape().Rank()}; 660 if (dimension < rank) { 661 const semantics::ShapeSpec &shapeSpec{details->shape()[dimension]}; 662 if (auto ubound{GetExplicitUBOUND(context, shapeSpec, invariantOnly)}) { 663 return *ubound; 664 } else if (details->IsAssumedSize() && dimension + 1 == symbol.Rank()) { 665 return std::nullopt; // UBOUND() folding replaces with -1 666 } else if (auto lb{GetLBOUND(base, dimension, invariantOnly)}) { 667 return ComputeUpperBound( 668 std::move(*lb), GetExtent(base, dimension, invariantOnly)); 669 } 670 } 671 } else if (const auto *assoc{ 672 symbol.detailsIf<semantics::AssocEntityDetails>()}) { 673 if (assoc->IsAssumedSize() || assoc->IsAssumedRank()) { 674 return std::nullopt; 675 } else if (assoc->rank()) { // RANK (n) 676 const Symbol &resolved{ResolveAssociations(symbol)}; 677 if (IsDescriptor(resolved) && dimension < *assoc->rank()) { 678 ExtentExpr lb{DescriptorInquiry{NamedEntity{base}, 679 DescriptorInquiry::Field::LowerBound, dimension}}; 680 ExtentExpr extent{DescriptorInquiry{ 681 std::move(base), DescriptorInquiry::Field::Extent, dimension}}; 682 return ComputeUpperBound(std::move(lb), std::move(extent)); 683 } 684 } else if (auto extent{GetAssociatedExtent(symbol, dimension)}) { 685 if (auto lb{GetLBOUND(base, dimension, invariantOnly)}) { 686 return ComputeUpperBound(std::move(*lb), std::move(extent)); 687 } 688 } 689 } 690 return std::nullopt; 691 } 692 693 MaybeExtentExpr GetUBOUND( 694 const NamedEntity &base, int dimension, bool invariantOnly) { 695 return GetUBOUND(nullptr, base, dimension, invariantOnly); 696 } 697 698 MaybeExtentExpr GetUBOUND(FoldingContext &context, const NamedEntity &base, 699 int dimension, bool invariantOnly) { 700 return Fold(context, GetUBOUND(&context, base, dimension, invariantOnly)); 701 } 702 703 static Shape GetUBOUNDs( 704 FoldingContext *context, const NamedEntity &base, bool invariantOnly) { 705 Shape result; 706 int rank{base.Rank()}; 707 for (int dim{0}; dim < rank; ++dim) { 708 result.emplace_back(GetUBOUND(context, base, dim, invariantOnly)); 709 } 710 return result; 711 } 712 713 Shape GetUBOUNDs( 714 FoldingContext &context, const NamedEntity &base, bool invariantOnly) { 715 return Fold(context, GetUBOUNDs(&context, base, invariantOnly)); 716 } 717 718 Shape GetUBOUNDs(const NamedEntity &base, bool invariantOnly) { 719 return GetUBOUNDs(nullptr, base, invariantOnly); 720 } 721 722 auto GetShapeHelper::operator()(const Symbol &symbol) const -> Result { 723 return common::visit( 724 common::visitors{ 725 [&](const semantics::ObjectEntityDetails &object) { 726 if (IsImpliedShape(symbol) && object.init()) { 727 return (*this)(object.init()); 728 } else if (IsAssumedRank(symbol)) { 729 return Result{}; 730 } else { 731 int n{object.shape().Rank()}; 732 NamedEntity base{symbol}; 733 return Result{CreateShape(n, base)}; 734 } 735 }, 736 [](const semantics::EntityDetails &) { 737 return ScalarShape(); // no dimensions seen 738 }, 739 [&](const semantics::ProcEntityDetails &proc) { 740 if (const Symbol * interface{proc.procInterface()}) { 741 return (*this)(*interface); 742 } else { 743 return ScalarShape(); 744 } 745 }, 746 [&](const semantics::AssocEntityDetails &assoc) { 747 NamedEntity base{symbol}; 748 if (assoc.rank()) { // SELECT RANK case 749 int n{assoc.rank().value()}; 750 return Result{CreateShape(n, base)}; 751 } else { 752 auto exprShape{((*this)(assoc.expr()))}; 753 if (exprShape) { 754 int rank{static_cast<int>(exprShape->size())}; 755 for (int dimension{0}; dimension < rank; ++dimension) { 756 auto &extent{(*exprShape)[dimension]}; 757 if (extent && !IsActuallyConstant(*extent)) { 758 extent = GetExtent(base, dimension); 759 } 760 } 761 } 762 return exprShape; 763 } 764 }, 765 [&](const semantics::SubprogramDetails &subp) -> Result { 766 if (subp.isFunction()) { 767 auto resultShape{(*this)(subp.result())}; 768 if (resultShape && !useResultSymbolShape_) { 769 // Ensure the shape is constant. Otherwise, it may be referring 770 // to symbols that belong to the function's scope and are 771 // meaningless on the caller side without the related call 772 // expression. 773 for (auto &extent : *resultShape) { 774 if (extent && !IsActuallyConstant(*extent)) { 775 extent.reset(); 776 } 777 } 778 } 779 return resultShape; 780 } else { 781 return Result{}; 782 } 783 }, 784 [&](const semantics::ProcBindingDetails &binding) { 785 return (*this)(binding.symbol()); 786 }, 787 [](const semantics::TypeParamDetails &) { return ScalarShape(); }, 788 [](const auto &) { return Result{}; }, 789 }, 790 symbol.GetUltimate().details()); 791 } 792 793 auto GetShapeHelper::operator()(const Component &component) const -> Result { 794 const Symbol &symbol{component.GetLastSymbol()}; 795 int rank{symbol.Rank()}; 796 if (rank == 0) { 797 return (*this)(component.base()); 798 } else if (symbol.has<semantics::ObjectEntityDetails>()) { 799 NamedEntity base{Component{component}}; 800 return CreateShape(rank, base); 801 } else { 802 return (*this)(symbol); 803 } 804 } 805 806 auto GetShapeHelper::operator()(const ArrayRef &arrayRef) const -> Result { 807 Shape shape; 808 int dimension{0}; 809 const NamedEntity &base{arrayRef.base()}; 810 for (const Subscript &ss : arrayRef.subscript()) { 811 if (ss.Rank() > 0) { 812 shape.emplace_back(GetExtent(ss, base, dimension)); 813 } 814 ++dimension; 815 } 816 if (shape.empty()) { 817 if (const Component * component{base.UnwrapComponent()}) { 818 return (*this)(component->base()); 819 } 820 } 821 return shape; 822 } 823 824 auto GetShapeHelper::operator()(const CoarrayRef &coarrayRef) const -> Result { 825 NamedEntity base{coarrayRef.GetBase()}; 826 if (coarrayRef.subscript().empty()) { 827 return (*this)(base); 828 } else { 829 Shape shape; 830 int dimension{0}; 831 for (const Subscript &ss : coarrayRef.subscript()) { 832 if (ss.Rank() > 0) { 833 shape.emplace_back(GetExtent(ss, base, dimension)); 834 } 835 ++dimension; 836 } 837 return shape; 838 } 839 } 840 841 auto GetShapeHelper::operator()(const Substring &substring) const -> Result { 842 return (*this)(substring.parent()); 843 } 844 845 auto GetShapeHelper::operator()(const ProcedureRef &call) const -> Result { 846 if (call.Rank() == 0) { 847 return ScalarShape(); 848 } else if (call.IsElemental()) { 849 // Use the shape of an actual array argument associated with a 850 // non-OPTIONAL dummy object argument. 851 if (context_) { 852 if (auto chars{characteristics::Procedure::FromActuals( 853 call.proc(), call.arguments(), *context_)}) { 854 std::size_t j{0}; 855 std::size_t anyArrayArgRank{0}; 856 for (const auto &arg : call.arguments()) { 857 if (arg && arg->Rank() > 0 && j < chars->dummyArguments.size()) { 858 anyArrayArgRank = arg->Rank(); 859 if (!chars->dummyArguments[j].IsOptional()) { 860 return (*this)(*arg); 861 } 862 } 863 ++j; 864 } 865 if (anyArrayArgRank) { 866 // All dummy array arguments of the procedure are OPTIONAL. 867 // We cannot take the shape from just any array argument, 868 // because all of them might be OPTIONAL dummy arguments 869 // of the caller. Return unknown shape ranked according 870 // to the last actual array argument. 871 return Shape(anyArrayArgRank, MaybeExtentExpr{}); 872 } 873 } 874 } 875 return ScalarShape(); 876 } else if (const Symbol * symbol{call.proc().GetSymbol()}) { 877 auto restorer{common::ScopedSet(useResultSymbolShape_, false)}; 878 return (*this)(*symbol); 879 } else if (const auto *intrinsic{call.proc().GetSpecificIntrinsic()}) { 880 if (intrinsic->name == "shape" || intrinsic->name == "lbound" || 881 intrinsic->name == "ubound") { 882 // For LBOUND/UBOUND, these are the array-valued cases (no DIM=) 883 if (!call.arguments().empty() && call.arguments().front()) { 884 return Shape{ 885 MaybeExtentExpr{ExtentExpr{call.arguments().front()->Rank()}}}; 886 } 887 } else if (intrinsic->name == "all" || intrinsic->name == "any" || 888 intrinsic->name == "count" || intrinsic->name == "iall" || 889 intrinsic->name == "iany" || intrinsic->name == "iparity" || 890 intrinsic->name == "maxval" || intrinsic->name == "minval" || 891 intrinsic->name == "norm2" || intrinsic->name == "parity" || 892 intrinsic->name == "product" || intrinsic->name == "sum") { 893 // Reduction with DIM= 894 if (call.arguments().size() >= 2) { 895 auto arrayShape{ 896 (*this)(UnwrapExpr<Expr<SomeType>>(call.arguments().at(0)))}; 897 const auto *dimArg{UnwrapExpr<Expr<SomeType>>(call.arguments().at(1))}; 898 if (arrayShape && dimArg) { 899 if (auto dim{ToInt64(*dimArg)}) { 900 if (*dim >= 1 && 901 static_cast<std::size_t>(*dim) <= arrayShape->size()) { 902 arrayShape->erase(arrayShape->begin() + (*dim - 1)); 903 return std::move(*arrayShape); 904 } 905 } 906 } 907 } 908 } else if (intrinsic->name == "findloc" || intrinsic->name == "maxloc" || 909 intrinsic->name == "minloc") { 910 std::size_t dimIndex{intrinsic->name == "findloc" ? 2u : 1u}; 911 if (call.arguments().size() > dimIndex) { 912 if (auto arrayShape{ 913 (*this)(UnwrapExpr<Expr<SomeType>>(call.arguments().at(0)))}) { 914 auto rank{static_cast<int>(arrayShape->size())}; 915 if (const auto *dimArg{ 916 UnwrapExpr<Expr<SomeType>>(call.arguments()[dimIndex])}) { 917 auto dim{ToInt64(*dimArg)}; 918 if (dim && *dim >= 1 && *dim <= rank) { 919 arrayShape->erase(arrayShape->begin() + (*dim - 1)); 920 return std::move(*arrayShape); 921 } 922 } else { 923 // xxxLOC(no DIM=) result is vector(1:RANK(ARRAY=)) 924 return Shape{ExtentExpr{rank}}; 925 } 926 } 927 } 928 } else if (intrinsic->name == "cshift" || intrinsic->name == "eoshift") { 929 if (!call.arguments().empty()) { 930 return (*this)(call.arguments()[0]); 931 } 932 } else if (intrinsic->name == "matmul") { 933 if (call.arguments().size() == 2) { 934 if (auto ashape{(*this)(call.arguments()[0])}) { 935 if (auto bshape{(*this)(call.arguments()[1])}) { 936 if (ashape->size() == 1 && bshape->size() == 2) { 937 bshape->erase(bshape->begin()); 938 return std::move(*bshape); // matmul(vector, matrix) 939 } else if (ashape->size() == 2 && bshape->size() == 1) { 940 ashape->pop_back(); 941 return std::move(*ashape); // matmul(matrix, vector) 942 } else if (ashape->size() == 2 && bshape->size() == 2) { 943 (*ashape)[1] = std::move((*bshape)[1]); 944 return std::move(*ashape); // matmul(matrix, matrix) 945 } 946 } 947 } 948 } 949 } else if (intrinsic->name == "pack") { 950 if (call.arguments().size() >= 3 && call.arguments().at(2)) { 951 // SHAPE(PACK(,,VECTOR=v)) -> SHAPE(v) 952 return (*this)(call.arguments().at(2)); 953 } else if (call.arguments().size() >= 2 && context_) { 954 if (auto maskShape{(*this)(call.arguments().at(1))}) { 955 if (maskShape->size() == 0) { 956 // Scalar MASK= -> [MERGE(SIZE(ARRAY=), 0, mask)] 957 if (auto arrayShape{(*this)(call.arguments().at(0))}) { 958 if (auto arraySize{GetSize(std::move(*arrayShape))}) { 959 ActualArguments toMerge{ 960 ActualArgument{AsGenericExpr(std::move(*arraySize))}, 961 ActualArgument{AsGenericExpr(ExtentExpr{0})}, 962 common::Clone(call.arguments().at(1))}; 963 auto specific{context_->intrinsics().Probe( 964 CallCharacteristics{"merge"}, toMerge, *context_)}; 965 CHECK(specific); 966 return Shape{ExtentExpr{FunctionRef<ExtentType>{ 967 ProcedureDesignator{std::move(specific->specificIntrinsic)}, 968 std::move(specific->arguments)}}}; 969 } 970 } 971 } else { 972 // Non-scalar MASK= -> [COUNT(mask)] 973 ActualArguments toCount{ActualArgument{common::Clone( 974 DEREF(call.arguments().at(1).value().UnwrapExpr()))}}; 975 auto specific{context_->intrinsics().Probe( 976 CallCharacteristics{"count"}, toCount, *context_)}; 977 CHECK(specific); 978 return Shape{ExtentExpr{FunctionRef<ExtentType>{ 979 ProcedureDesignator{std::move(specific->specificIntrinsic)}, 980 std::move(specific->arguments)}}}; 981 } 982 } 983 } 984 } else if (intrinsic->name == "reshape") { 985 if (call.arguments().size() >= 2 && call.arguments().at(1)) { 986 // SHAPE(RESHAPE(array,shape)) -> shape 987 if (const auto *shapeExpr{ 988 call.arguments().at(1).value().UnwrapExpr()}) { 989 auto shapeArg{std::get<Expr<SomeInteger>>(shapeExpr->u)}; 990 if (auto result{AsShapeResult( 991 ConvertToType<ExtentType>(std::move(shapeArg)))}) { 992 return result; 993 } 994 } 995 } 996 } else if (intrinsic->name == "spread") { 997 // SHAPE(SPREAD(ARRAY,DIM,NCOPIES)) = SHAPE(ARRAY) with NCOPIES inserted 998 // at position DIM. 999 if (call.arguments().size() == 3) { 1000 auto arrayShape{ 1001 (*this)(UnwrapExpr<Expr<SomeType>>(call.arguments().at(0)))}; 1002 const auto *dimArg{UnwrapExpr<Expr<SomeType>>(call.arguments().at(1))}; 1003 const auto *nCopies{ 1004 UnwrapExpr<Expr<SomeInteger>>(call.arguments().at(2))}; 1005 if (arrayShape && dimArg && nCopies) { 1006 if (auto dim{ToInt64(*dimArg)}) { 1007 if (*dim >= 1 && 1008 static_cast<std::size_t>(*dim) <= arrayShape->size() + 1) { 1009 arrayShape->emplace(arrayShape->begin() + *dim - 1, 1010 ConvertToType<ExtentType>(common::Clone(*nCopies))); 1011 return std::move(*arrayShape); 1012 } 1013 } 1014 } 1015 } 1016 } else if (intrinsic->name == "transfer") { 1017 if (call.arguments().size() == 3 && call.arguments().at(2)) { 1018 // SIZE= is present; shape is vector [SIZE=] 1019 if (const auto *size{ 1020 UnwrapExpr<Expr<SomeInteger>>(call.arguments().at(2))}) { 1021 return Shape{ 1022 MaybeExtentExpr{ConvertToType<ExtentType>(common::Clone(*size))}}; 1023 } 1024 } else if (context_) { 1025 if (auto moldTypeAndShape{characteristics::TypeAndShape::Characterize( 1026 call.arguments().at(1), *context_)}) { 1027 if (GetRank(moldTypeAndShape->shape()) == 0) { 1028 // SIZE= is absent and MOLD= is scalar: result is scalar 1029 return ScalarShape(); 1030 } else { 1031 // SIZE= is absent and MOLD= is array: result is vector whose 1032 // length is determined by sizes of types. See 16.9.193p4 case(ii). 1033 // Note that if sourceBytes is not known to be empty, we 1034 // can fold only when moldElementBytes is known to not be zero; 1035 // the most general case risks a division by zero otherwise. 1036 if (auto sourceTypeAndShape{ 1037 characteristics::TypeAndShape::Characterize( 1038 call.arguments().at(0), *context_)}) { 1039 if (auto sourceBytes{ 1040 sourceTypeAndShape->MeasureSizeInBytes(*context_)}) { 1041 *sourceBytes = Fold(*context_, std::move(*sourceBytes)); 1042 if (auto sourceBytesConst{ToInt64(*sourceBytes)}) { 1043 if (*sourceBytesConst == 0) { 1044 return Shape{ExtentExpr{0}}; 1045 } 1046 } 1047 if (auto moldElementBytes{ 1048 moldTypeAndShape->MeasureElementSizeInBytes( 1049 *context_, true)}) { 1050 *moldElementBytes = 1051 Fold(*context_, std::move(*moldElementBytes)); 1052 auto moldElementBytesConst{ToInt64(*moldElementBytes)}; 1053 if (moldElementBytesConst && *moldElementBytesConst != 0) { 1054 ExtentExpr extent{Fold(*context_, 1055 (std::move(*sourceBytes) + 1056 common::Clone(*moldElementBytes) - ExtentExpr{1}) / 1057 common::Clone(*moldElementBytes))}; 1058 return Shape{MaybeExtentExpr{std::move(extent)}}; 1059 } 1060 } 1061 } 1062 } 1063 } 1064 } 1065 } 1066 } else if (intrinsic->name == "transpose") { 1067 if (call.arguments().size() >= 1) { 1068 if (auto shape{(*this)(call.arguments().at(0))}) { 1069 if (shape->size() == 2) { 1070 std::swap((*shape)[0], (*shape)[1]); 1071 return shape; 1072 } 1073 } 1074 } 1075 } else if (intrinsic->name == "unpack") { 1076 if (call.arguments().size() >= 2) { 1077 return (*this)(call.arguments()[1]); // MASK= 1078 } 1079 } else if (intrinsic->characteristics.value().attrs.test(characteristics:: 1080 Procedure::Attr::NullPointer)) { // NULL(MOLD=) 1081 return (*this)(call.arguments()); 1082 } else { 1083 // TODO: shapes of other non-elemental intrinsic results 1084 } 1085 } 1086 // The rank is always known even if the extents are not. 1087 return Shape(static_cast<std::size_t>(call.Rank()), MaybeExtentExpr{}); 1088 } 1089 1090 void GetShapeHelper::AccumulateExtent( 1091 ExtentExpr &result, ExtentExpr &&n) const { 1092 result = std::move(result) + std::move(n); 1093 if (context_) { 1094 // Fold during expression creation to avoid creating an expression so 1095 // large we can't evaluate it without overflowing the stack. 1096 result = Fold(*context_, std::move(result)); 1097 } 1098 } 1099 1100 // Check conformance of the passed shapes. 1101 std::optional<bool> CheckConformance(parser::ContextualMessages &messages, 1102 const Shape &left, const Shape &right, CheckConformanceFlags::Flags flags, 1103 const char *leftIs, const char *rightIs) { 1104 int n{GetRank(left)}; 1105 if (n == 0 && (flags & CheckConformanceFlags::LeftScalarExpandable)) { 1106 return true; 1107 } 1108 int rn{GetRank(right)}; 1109 if (rn == 0 && (flags & CheckConformanceFlags::RightScalarExpandable)) { 1110 return true; 1111 } 1112 if (n != rn) { 1113 messages.Say("Rank of %1$s is %2$d, but %3$s has rank %4$d"_err_en_US, 1114 leftIs, n, rightIs, rn); 1115 return false; 1116 } 1117 for (int j{0}; j < n; ++j) { 1118 if (auto leftDim{ToInt64(left[j])}) { 1119 if (auto rightDim{ToInt64(right[j])}) { 1120 if (*leftDim != *rightDim) { 1121 messages.Say("Dimension %1$d of %2$s has extent %3$jd, " 1122 "but %4$s has extent %5$jd"_err_en_US, 1123 j + 1, leftIs, *leftDim, rightIs, *rightDim); 1124 return false; 1125 } 1126 } else if (!(flags & CheckConformanceFlags::RightIsDeferredShape)) { 1127 return std::nullopt; 1128 } 1129 } else if (!(flags & CheckConformanceFlags::LeftIsDeferredShape)) { 1130 return std::nullopt; 1131 } 1132 } 1133 return true; 1134 } 1135 1136 bool IncrementSubscripts( 1137 ConstantSubscripts &indices, const ConstantSubscripts &extents) { 1138 std::size_t rank(indices.size()); 1139 CHECK(rank <= extents.size()); 1140 for (std::size_t j{0}; j < rank; ++j) { 1141 if (extents[j] < 1) { 1142 return false; 1143 } 1144 } 1145 for (std::size_t j{0}; j < rank; ++j) { 1146 if (indices[j]++ < extents[j]) { 1147 return true; 1148 } 1149 indices[j] = 1; 1150 } 1151 return false; 1152 } 1153 1154 } // namespace Fortran::evaluate 1155