| 1 |
//===-- lib/Evaluate/variable.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/variable.h" |
| 10 |
#include "flang/Common/idioms.h" |
| 11 |
#include "flang/Evaluate/check-expression.h" |
| 12 |
#include "flang/Evaluate/fold.h" |
| 13 |
#include "flang/Evaluate/tools.h" |
| 14 |
#include "flang/Parser/char-block.h" |
| 15 |
#include "flang/Parser/characters.h" |
| 16 |
#include "flang/Parser/message.h" |
| 17 |
#include "flang/Semantics/scope.h" |
| 18 |
#include "flang/Semantics/symbol.h" |
| 19 |
#include <type_traits> |
| 20 |
|
| 21 |
using namespace Fortran::parser::literals; |
| 22 |
|
| 23 |
namespace Fortran::evaluate { |
| 24 |
|
| 25 |
// Constructors, accessors, mutators |
| 26 |
|
| 27 |
Triplet::Triplet() : stride_{Expr<SubscriptInteger>{1}} {} |
| 28 |
|
| 29 |
Triplet::Triplet(std::optional<Expr<SubscriptInteger>> &&l, |
| 30 |
std::optional<Expr<SubscriptInteger>> &&u, |
| 31 |
std::optional<Expr<SubscriptInteger>> &&s) |
| 32 |
: stride_{s ? std::move(*s) : Expr<SubscriptInteger>{1}} { |
| 33 |
if (l) { |
| 34 |
lower_.emplace(std::move(*l)); |
| 35 |
} |
| 36 |
if (u) { |
| 37 |
upper_.emplace(std::move(*u)); |
| 38 |
} |
| 39 |
} |
| 40 |
|
| 41 |
std::optional<Expr<SubscriptInteger>> Triplet::lower() const { |
| 42 |
if (lower_) { |
| 43 |
return {lower_.value().value()}; |
| 44 |
} |
| 45 |
return std::nullopt; |
| 46 |
} |
| 47 |
|
| 48 |
Triplet &Triplet::set_lower(Expr<SubscriptInteger> &&expr) { |
| 49 |
lower_.emplace(std::move(expr)); |
| 50 |
return *this; |
| 51 |
} |
| 52 |
|
| 53 |
std::optional<Expr<SubscriptInteger>> Triplet::upper() const { |
| 54 |
if (upper_) { |
| 55 |
return {upper_.value().value()}; |
| 56 |
} |
| 57 |
return std::nullopt; |
| 58 |
} |
| 59 |
|
| 60 |
Triplet &Triplet::set_upper(Expr<SubscriptInteger> &&expr) { |
| 61 |
upper_.emplace(std::move(expr)); |
| 62 |
return *this; |
| 63 |
} |
| 64 |
|
| 65 |
Expr<SubscriptInteger> Triplet::stride() const { return stride_.value(); } |
| 66 |
|
| 67 |
Triplet &Triplet::set_stride(Expr<SubscriptInteger> &&expr) { |
| 68 |
stride_.value() = std::move(expr); |
| 69 |
return *this; |
| 70 |
} |
| 71 |
|
| 72 |
CoarrayRef::CoarrayRef(SymbolVector &&base, std::vector<Subscript> &&ss, |
| 73 |
std::vector<Expr<SubscriptInteger>> &&css) |
| 74 |
: base_{std::move(base)}, subscript_(std::move(ss)), |
| 75 |
cosubscript_(std::move(css)) { |
| 76 |
CHECK(!base_.empty()); |
| 77 |
CHECK(!cosubscript_.empty()); |
| 78 |
} |
| 79 |
|
| 80 |
std::optional<Expr<SomeInteger>> CoarrayRef::stat() const { |
| 81 |
if (stat_) { |
| 82 |
return stat_.value().value(); |
| 83 |
} else { |
| 84 |
return std::nullopt; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
std::optional<Expr<SomeInteger>> CoarrayRef::team() const { |
| 89 |
if (team_) { |
| 90 |
return team_.value().value(); |
| 91 |
} else { |
| 92 |
return std::nullopt; |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
CoarrayRef &CoarrayRef::set_stat(Expr<SomeInteger> &&v) { |
| 97 |
CHECK(IsVariable(v)); |
| 98 |
stat_.emplace(std::move(v)); |
| 99 |
return *this; |
| 100 |
} |
| 101 |
|
| 102 |
CoarrayRef &CoarrayRef::set_team(Expr<SomeInteger> &&v, bool isTeamNumber) { |
| 103 |
CHECK(IsVariable(v)); |
| 104 |
team_.emplace(std::move(v)); |
| 105 |
teamIsTeamNumber_ = isTeamNumber; |
| 106 |
return *this; |
| 107 |
} |
| 108 |
|
| 109 |
const Symbol &CoarrayRef::GetFirstSymbol() const { return base_.front(); } |
| 110 |
|
| 111 |
const Symbol &CoarrayRef::GetLastSymbol() const { return base_.back(); } |
| 112 |
|
| 113 |
void Substring::SetBounds(std::optional<Expr<SubscriptInteger>> &lower, |
| 114 |
std::optional<Expr<SubscriptInteger>> &upper) { |
| 115 |
if (lower) { |
| 116 |
set_lower(std::move(lower.value())); |
| 117 |
} |
| 118 |
if (upper) { |
| 119 |
set_upper(std::move(upper.value())); |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
Expr<SubscriptInteger> Substring::lower() const { |
| 124 |
if (lower_) { |
| 125 |
return lower_.value().value(); |
| 126 |
} else { |
| 127 |
return AsExpr(Constant<SubscriptInteger>{1}); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
Substring &Substring::set_lower(Expr<SubscriptInteger> &&expr) { |
| 132 |
lower_.emplace(std::move(expr)); |
| 133 |
return *this; |
| 134 |
} |
| 135 |
|
| 136 |
std::optional<Expr<SubscriptInteger>> Substring::upper() const { |
| 137 |
if (upper_) { |
| 138 |
return upper_.value().value(); |
| 139 |
} else { |
| 140 |
return common::visit( |
| 141 |
common::visitors{ |
| 142 |
[](const DataRef &dataRef) { return dataRef.LEN(); }, |
| 143 |
[](const StaticDataObject::Pointer &object) |
| 144 |
-> std::optional<Expr<SubscriptInteger>> { |
| 145 |
return AsExpr(Constant<SubscriptInteger>{object->data().size()}); |
| 146 |
}, |
| 147 |
}, |
| 148 |
parent_); |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
Substring &Substring::set_upper(Expr<SubscriptInteger> &&expr) { |
| 153 |
upper_.emplace(std::move(expr)); |
| 154 |
return *this; |
| 155 |
} |
| 156 |
|
| 157 |
std::optional<Expr<SomeCharacter>> Substring::Fold(FoldingContext &context) { |
| 158 |
if (!upper_) { |
| 159 |
upper_ = upper(); |
| 160 |
if (!upper_) { |
| 161 |
return std::nullopt; |
| 162 |
} |
| 163 |
} |
| 164 |
upper_.value() = evaluate::Fold(context, std::move(upper_.value().value())); |
| 165 |
std::optional<ConstantSubscript> ubi{ToInt64(upper_.value().value())}; |
| 166 |
if (!ubi) { |
| 167 |
return std::nullopt; |
| 168 |
} |
| 169 |
if (!lower_) { |
| 170 |
lower_ = AsExpr(Constant<SubscriptInteger>{1}); |
| 171 |
} |
| 172 |
lower_.value() = evaluate::Fold(context, std::move(lower_.value().value())); |
| 173 |
std::optional<ConstantSubscript> lbi{ToInt64(lower_.value().value())}; |
| 174 |
if (!lbi) { |
| 175 |
return std::nullopt; |
| 176 |
} |
| 177 |
if (*lbi > *ubi) { // empty result; canonicalize |
| 178 |
*lbi = 1; |
| 179 |
*ubi = 0; |
| 180 |
lower_ = AsExpr(Constant<SubscriptInteger>{*lbi}); |
| 181 |
upper_ = AsExpr(Constant<SubscriptInteger>{*ubi}); |
| 182 |
} |
| 183 |
std::optional<ConstantSubscript> length; |
| 184 |
std::optional<Expr<SomeCharacter>> strings; // a Constant<Character> |
| 185 |
if (const auto *literal{std::get_if<StaticDataObject::Pointer>(&parent_)}) { |
| 186 |
length = (*literal)->data().size(); |
| 187 |
if (auto str{(*literal)->AsString()}) { |
| 188 |
strings = |
| 189 |
Expr<SomeCharacter>(Expr<Ascii>(Constant<Ascii>{std::move(*str)})); |
| 190 |
} |
| 191 |
} else if (const auto *dataRef{std::get_if<DataRef>(&parent_)}) { |
| 192 |
if (auto expr{AsGenericExpr(DataRef{*dataRef})}) { |
| 193 |
auto folded{evaluate::Fold(context, std::move(*expr))}; |
| 194 |
if (IsActuallyConstant(folded)) { |
| 195 |
if (const auto *value{UnwrapExpr<Expr<SomeCharacter>>(folded)}) { |
| 196 |
strings = *value; |
| 197 |
} |
| 198 |
} |
| 199 |
} |
| 200 |
} |
| 201 |
std::optional<Expr<SomeCharacter>> result; |
| 202 |
if (strings) { |
| 203 |
result = common::visit( |
| 204 |
[&](const auto &expr) -> std::optional<Expr<SomeCharacter>> { |
| 205 |
using Type = typename std::decay_t<decltype(expr)>::Result; |
| 206 |
if (const auto *cc{std::get_if<Constant<Type>>(&expr.u)}) { |
| 207 |
if (auto substr{cc->Substring(*lbi, *ubi)}) { |
| 208 |
return Expr<SomeCharacter>{Expr<Type>{*substr}}; |
| 209 |
} |
| 210 |
} |
| 211 |
return std::nullopt; |
| 212 |
}, |
| 213 |
strings->u); |
| 214 |
} |
| 215 |
if (!result) { // error cases |
| 216 |
if (*lbi < 1) { |
| 217 |
if (context.languageFeatures().ShouldWarn(common::UsageWarning::Bounds)) { |
| 218 |
context.messages().Say( |
| 219 |
"Lower bound (%jd) on substring is less than one"_warn_en_US, |
| 220 |
static_cast<std::intmax_t>(*lbi)); |
| 221 |
} |
| 222 |
*lbi = 1; |
| 223 |
lower_ = AsExpr(Constant<SubscriptInteger>{1}); |
| 224 |
} |
| 225 |
if (length && *ubi > *length) { |
| 226 |
if (context.languageFeatures().ShouldWarn(common::UsageWarning::Bounds)) { |
| 227 |
context.messages().Say( |
| 228 |
"Upper bound (%jd) on substring is greater than character length (%jd)"_warn_en_US, |
| 229 |
static_cast<std::intmax_t>(*ubi), |
| 230 |
static_cast<std::intmax_t>(*length)); |
| 231 |
} |
| 232 |
*ubi = *length; |
| 233 |
upper_ = AsExpr(Constant<SubscriptInteger>{*ubi}); |
| 234 |
} |
| 235 |
} |
| 236 |
return result; |
| 237 |
} |
| 238 |
|
| 239 |
DescriptorInquiry::DescriptorInquiry( |
| 240 |
const NamedEntity &base, Field field, int dim) |
| 241 |
: base_{base}, field_{field}, dimension_{dim} { |
| 242 |
const Symbol &last{base_.GetLastSymbol()}; |
| 243 |
CHECK(IsDescriptor(last)); |
| 244 |
CHECK(((field == Field::Len || field == Field::Rank) && dim == 0) || |
| 245 |
(field != Field::Len && dim >= 0 && dim < last.Rank())); |
| 246 |
} |
| 247 |
|
| 248 |
DescriptorInquiry::DescriptorInquiry(NamedEntity &&base, Field field, int dim) |
| 249 |
: base_{std::move(base)}, field_{field}, dimension_{dim} { |
| 250 |
const Symbol &last{base_.GetLastSymbol()}; |
| 251 |
CHECK(IsDescriptor(last)); |
| 252 |
CHECK((field == Field::Len && dim == 0) || |
| 253 |
(field != Field::Len && dim >= 0 && dim < last.Rank())); |
| 254 |
} |
| 255 |
|
| 256 |
// LEN() |
| 257 |
static std::optional<Expr<SubscriptInteger>> SymbolLEN(const Symbol &symbol) { |
| 258 |
const Symbol &ultimate{symbol.GetUltimate()}; |
| 259 |
if (const auto *assoc{ultimate.detailsIf<semantics::AssocEntityDetails>()}) { |
| 260 |
if (const auto *chExpr{UnwrapExpr<Expr<SomeCharacter>>(assoc->expr())}) { |
| 261 |
return chExpr->LEN(); |
| 262 |
} |
| 263 |
} |
| 264 |
if (auto dyType{DynamicType::From(ultimate)}) { |
| 265 |
auto len{dyType->GetCharLength()}; |
| 266 |
if (!len && ultimate.attrs().test(semantics::Attr::PARAMETER)) { |
| 267 |
// Its initializer determines the length of an implied-length named |
| 268 |
// constant. |
| 269 |
if (const auto *object{ |
| 270 |
ultimate.detailsIf<semantics::ObjectEntityDetails>()}) { |
| 271 |
if (object->init()) { |
| 272 |
if (auto dyType2{DynamicType::From(*object->init())}) { |
| 273 |
len = dyType2->GetCharLength(); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
} |
| 278 |
if (len) { |
| 279 |
if (auto constLen{ToInt64(*len)}) { |
| 280 |
return Expr<SubscriptInteger>{std::max<std::int64_t>(*constLen, 0)}; |
| 281 |
} else if (ultimate.owner().IsDerivedType() || |
| 282 |
IsScopeInvariantExpr(*len)) { |
| 283 |
return AsExpr(Extremum<SubscriptInteger>{ |
| 284 |
Ordering::Greater, Expr<SubscriptInteger>{0}, std::move(*len)}); |
| 285 |
} |
| 286 |
} |
| 287 |
} |
| 288 |
if (IsDescriptor(ultimate) && !ultimate.owner().IsDerivedType()) { |
| 289 |
return Expr<SubscriptInteger>{ |
| 290 |
DescriptorInquiry{NamedEntity{symbol}, DescriptorInquiry::Field::Len}}; |
| 291 |
} |
| 292 |
return std::nullopt; |
| 293 |
} |
| 294 |
|
| 295 |
std::optional<Expr<SubscriptInteger>> BaseObject::LEN() const { |
| 296 |
return common::visit( |
| 297 |
common::visitors{ |
| 298 |
[](const Symbol &symbol) { return SymbolLEN(symbol); }, |
| 299 |
[](const StaticDataObject::Pointer &object) |
| 300 |
-> std::optional<Expr<SubscriptInteger>> { |
| 301 |
return AsExpr(Constant<SubscriptInteger>{object->data().size()}); |
| 302 |
}, |
| 303 |
}, |
| 304 |
u); |
| 305 |
} |
| 306 |
|
| 307 |
std::optional<Expr<SubscriptInteger>> Component::LEN() const { |
| 308 |
return SymbolLEN(GetLastSymbol()); |
| 309 |
} |
| 310 |
|
| 311 |
std::optional<Expr<SubscriptInteger>> NamedEntity::LEN() const { |
| 312 |
return SymbolLEN(GetLastSymbol()); |
| 313 |
} |
| 314 |
|
| 315 |
std::optional<Expr<SubscriptInteger>> ArrayRef::LEN() const { |
| 316 |
return base_.LEN(); |
| 317 |
} |
| 318 |
|
| 319 |
std::optional<Expr<SubscriptInteger>> CoarrayRef::LEN() const { |
| 320 |
return SymbolLEN(GetLastSymbol()); |
| 321 |
} |
| 322 |
|
| 323 |
std::optional<Expr<SubscriptInteger>> DataRef::LEN() const { |
| 324 |
return common::visit(common::visitors{ |
| 325 |
[](SymbolRef symbol) { return SymbolLEN(symbol); }, |
| 326 |
[](const auto &x) { return x.LEN(); }, |
| 327 |
}, |
| 328 |
u); |
| 329 |
} |
| 330 |
|
| 331 |
std::optional<Expr<SubscriptInteger>> Substring::LEN() const { |
| 332 |
if (auto top{upper()}) { |
| 333 |
return AsExpr(Extremum<SubscriptInteger>{Ordering::Greater, |
| 334 |
AsExpr(Constant<SubscriptInteger>{0}), |
| 335 |
*std::move(top) - lower() + AsExpr(Constant<SubscriptInteger>{1})}); |
| 336 |
} else { |
| 337 |
return std::nullopt; |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
template <typename T> |
| 342 |
std::optional<Expr<SubscriptInteger>> Designator<T>::LEN() const { |
| 343 |
if constexpr (T::category == TypeCategory::Character) { |
| 344 |
return common::visit(common::visitors{ |
| 345 |
[](SymbolRef symbol) { return SymbolLEN(symbol); }, |
| 346 |
[](const auto &x) { return x.LEN(); }, |
| 347 |
}, |
| 348 |
u); |
| 349 |
} else { |
| 350 |
common::die("Designator<non-char>::LEN() called"); |
| 351 |
return std::nullopt; |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
std::optional<Expr<SubscriptInteger>> ProcedureDesignator::LEN() const { |
| 356 |
using T = std::optional<Expr<SubscriptInteger>>; |
| 357 |
return common::visit( |
| 358 |
common::visitors{ |
| 359 |
[](SymbolRef symbol) -> T { return SymbolLEN(symbol); }, |
| 360 |
[](const common::CopyableIndirection<Component> &c) -> T { |
| 361 |
return c.value().LEN(); |
| 362 |
}, |
| 363 |
[](const SpecificIntrinsic &i) -> T { |
| 364 |
// Some cases whose results' lengths can be determined |
| 365 |
// from the lengths of their arguments are handled in |
| 366 |
// ProcedureRef::LEN() before coming here. |
| 367 |
if (const auto &result{i.characteristics.value().functionResult}) { |
| 368 |
if (const auto *type{result->GetTypeAndShape()}) { |
| 369 |
if (auto length{type->type().GetCharLength()}) { |
| 370 |
return std::move(*length); |
| 371 |
} |
| 372 |
} |
| 373 |
} |
| 374 |
return std::nullopt; |
| 375 |
}, |
| 376 |
}, |
| 377 |
u); |
| 378 |
} |
| 379 |
|
| 380 |
// Rank() |
| 381 |
int BaseObject::Rank() const { |
| 382 |
return common::visit(common::visitors{ |
| 383 |
[](SymbolRef symbol) { return symbol->Rank(); }, |
| 384 |
[](const StaticDataObject::Pointer &) { return 0; }, |
| 385 |
}, |
| 386 |
u); |
| 387 |
} |
| 388 |
|
| 389 |
int Component::Rank() const { |
| 390 |
if (int rank{symbol_->Rank()}; rank > 0) { |
| 391 |
return rank; |
| 392 |
} |
| 393 |
return base().Rank(); |
| 394 |
} |
| 395 |
|
| 396 |
int NamedEntity::Rank() const { |
| 397 |
return common::visit(common::visitors{ |
| 398 |
[](const SymbolRef s) { return s->Rank(); }, |
| 399 |
[](const Component &c) { return c.Rank(); }, |
| 400 |
}, |
| 401 |
u_); |
| 402 |
} |
| 403 |
|
| 404 |
int Subscript::Rank() const { |
| 405 |
return common::visit(common::visitors{ |
| 406 |
[](const IndirectSubscriptIntegerExpr &x) { |
| 407 |
return x.value().Rank(); |
| 408 |
}, |
| 409 |
[](const Triplet &) { return 1; }, |
| 410 |
}, |
| 411 |
u); |
| 412 |
} |
| 413 |
|
| 414 |
int ArrayRef::Rank() const { |
| 415 |
int rank{0}; |
| 416 |
for (const auto &expr : subscript_) { |
| 417 |
rank += expr.Rank(); |
| 418 |
} |
| 419 |
if (rank > 0) { |
| 420 |
return rank; |
| 421 |
} else if (const Component * component{base_.UnwrapComponent()}) { |
| 422 |
return component->base().Rank(); |
| 423 |
} else { |
| 424 |
return 0; |
| 425 |
} |
| 426 |
} |
| 427 |
|
| 428 |
int CoarrayRef::Rank() const { |
| 429 |
if (!subscript_.empty()) { |
| 430 |
int rank{0}; |
| 431 |
for (const auto &expr : subscript_) { |
| 432 |
rank += expr.Rank(); |
| 433 |
} |
| 434 |
return rank; |
| 435 |
} else { |
| 436 |
return base_.back()->Rank(); |
| 437 |
} |
| 438 |
} |
| 439 |
|
| 440 |
int DataRef::Rank() const { |
| 441 |
return common::visit(common::visitors{ |
| 442 |
[](SymbolRef symbol) { return symbol->Rank(); }, |
| 443 |
[](const auto &x) { return x.Rank(); }, |
| 444 |
}, |
| 445 |
u); |
| 446 |
} |
| 447 |
|
| 448 |
int Substring::Rank() const { |
| 449 |
return common::visit( |
| 450 |
common::visitors{ |
| 451 |
[](const DataRef &dataRef) { return dataRef.Rank(); }, |
| 452 |
[](const StaticDataObject::Pointer &) { return 0; }, |
| 453 |
}, |
| 454 |
parent_); |
| 455 |
} |
| 456 |
|
| 457 |
int ComplexPart::Rank() const { return complex_.Rank(); } |
| 458 |
|
| 459 |
template <typename T> int Designator<T>::Rank() const { |
| 460 |
return common::visit(common::visitors{ |
| 461 |
[](SymbolRef symbol) { return symbol->Rank(); }, |
| 462 |
[](const auto &x) { return x.Rank(); }, |
| 463 |
}, |
| 464 |
u); |
| 465 |
} |
| 466 |
|
| 467 |
// GetBaseObject(), GetFirstSymbol(), GetLastSymbol(), &c. |
| 468 |
const Symbol &Component::GetFirstSymbol() const { |
| 469 |
return base_.value().GetFirstSymbol(); |
| 470 |
} |
| 471 |
|
| 472 |
const Symbol &NamedEntity::GetFirstSymbol() const { |
| 473 |
return common::visit(common::visitors{ |
| 474 |
[](SymbolRef s) -> const Symbol & { return s; }, |
| 475 |
[](const Component &c) -> const Symbol & { |
| 476 |
return c.GetFirstSymbol(); |
| 477 |
}, |
| 478 |
}, |
| 479 |
u_); |
| 480 |
} |
| 481 |
|
| 482 |
const Symbol &NamedEntity::GetLastSymbol() const { |
| 483 |
return common::visit(common::visitors{ |
| 484 |
[](SymbolRef s) -> const Symbol & { return s; }, |
| 485 |
[](const Component &c) -> const Symbol & { |
| 486 |
return c.GetLastSymbol(); |
| 487 |
}, |
| 488 |
}, |
| 489 |
u_); |
| 490 |
} |
| 491 |
|
| 492 |
const SymbolRef *NamedEntity::UnwrapSymbolRef() const { |
| 493 |
return common::visit( |
| 494 |
common::visitors{ |
| 495 |
[](const SymbolRef &s) { return &s; }, |
| 496 |
[](const Component &) -> const SymbolRef * { return nullptr; }, |
| 497 |
}, |
| 498 |
u_); |
| 499 |
} |
| 500 |
|
| 501 |
SymbolRef *NamedEntity::UnwrapSymbolRef() { |
| 502 |
return common::visit(common::visitors{ |
| 503 |
[](SymbolRef &s) { return &s; }, |
| 504 |
[](Component &) -> SymbolRef * { return nullptr; }, |
| 505 |
}, |
| 506 |
u_); |
| 507 |
} |
| 508 |
|
| 509 |
const Component *NamedEntity::UnwrapComponent() const { |
| 510 |
return common::visit( |
| 511 |
common::visitors{ |
| 512 |
[](SymbolRef) -> const Component * { return nullptr; }, |
| 513 |
[](const Component &c) { return &c; }, |
| 514 |
}, |
| 515 |
u_); |
| 516 |
} |
| 517 |
|
| 518 |
Component *NamedEntity::UnwrapComponent() { |
| 519 |
return common::visit(common::visitors{ |
| 520 |
[](SymbolRef &) -> Component * { return nullptr; }, |
| 521 |
[](Component &c) { return &c; }, |
| 522 |
}, |
| 523 |
u_); |
| 524 |
} |
| 525 |
|
| 526 |
const Symbol &ArrayRef::GetFirstSymbol() const { |
| 527 |
return base_.GetFirstSymbol(); |
| 528 |
} |
| 529 |
|
| 530 |
const Symbol &ArrayRef::GetLastSymbol() const { return base_.GetLastSymbol(); } |
| 531 |
|
| 532 |
const Symbol &DataRef::GetFirstSymbol() const { |
| 533 |
return *common::visit(common::visitors{ |
| 534 |
[](SymbolRef symbol) { return &*symbol; }, |
| 535 |
[](const auto &x) { return &x.GetFirstSymbol(); }, |
| 536 |
}, |
| 537 |
u); |
| 538 |
} |
| 539 |
|
| 540 |
const Symbol &DataRef::GetLastSymbol() const { |
| 541 |
return *common::visit(common::visitors{ |
| 542 |
[](SymbolRef symbol) { return &*symbol; }, |
| 543 |
[](const auto &x) { return &x.GetLastSymbol(); }, |
| 544 |
}, |
| 545 |
u); |
| 546 |
} |
| 547 |
|
| 548 |
BaseObject Substring::GetBaseObject() const { |
| 549 |
return common::visit(common::visitors{ |
| 550 |
[](const DataRef &dataRef) { |
| 551 |
return BaseObject{dataRef.GetFirstSymbol()}; |
| 552 |
}, |
| 553 |
[](StaticDataObject::Pointer pointer) { |
| 554 |
return BaseObject{std::move(pointer)}; |
| 555 |
}, |
| 556 |
}, |
| 557 |
parent_); |
| 558 |
} |
| 559 |
|
| 560 |
const Symbol *Substring::GetLastSymbol() const { |
| 561 |
return common::visit( |
| 562 |
common::visitors{ |
| 563 |
[](const DataRef &dataRef) { return &dataRef.GetLastSymbol(); }, |
| 564 |
[](const auto &) -> const Symbol * { return nullptr; }, |
| 565 |
}, |
| 566 |
parent_); |
| 567 |
} |
| 568 |
|
| 569 |
template <typename T> BaseObject Designator<T>::GetBaseObject() const { |
| 570 |
return common::visit( |
| 571 |
common::visitors{ |
| 572 |
[](SymbolRef symbol) { return BaseObject{symbol}; }, |
| 573 |
[](const Substring &sstring) { return sstring.GetBaseObject(); }, |
| 574 |
[](const auto &x) { return BaseObject{x.GetFirstSymbol()}; }, |
| 575 |
}, |
| 576 |
u); |
| 577 |
} |
| 578 |
|
| 579 |
template <typename T> const Symbol *Designator<T>::GetLastSymbol() const { |
| 580 |
return common::visit( |
| 581 |
common::visitors{ |
| 582 |
[](SymbolRef symbol) { return &*symbol; }, |
| 583 |
[](const Substring &sstring) { return sstring.GetLastSymbol(); }, |
| 584 |
[](const auto &x) { return &x.GetLastSymbol(); }, |
| 585 |
}, |
| 586 |
u); |
| 587 |
} |
| 588 |
|
| 589 |
template <typename T> |
| 590 |
std::optional<DynamicType> Designator<T>::GetType() const { |
| 591 |
if constexpr (IsLengthlessIntrinsicType<Result>) { |
| 592 |
return Result::GetType(); |
| 593 |
} |
| 594 |
if constexpr (Result::category == TypeCategory::Character) { |
| 595 |
if (std::holds_alternative<Substring>(u)) { |
| 596 |
if (auto len{LEN()}) { |
| 597 |
if (auto n{ToInt64(*len)}) { |
| 598 |
return DynamicType{T::kind, *n}; |
| 599 |
} |
| 600 |
} |
| 601 |
return DynamicType{TypeCategory::Character, T::kind}; |
| 602 |
} |
| 603 |
} |
| 604 |
if (const Symbol * symbol{GetLastSymbol()}) { |
| 605 |
return DynamicType::From(*symbol); |
| 606 |
} |
| 607 |
return std::nullopt; |
| 608 |
} |
| 609 |
|
| 610 |
static NamedEntity AsNamedEntity(const SymbolVector &x) { |
| 611 |
CHECK(!x.empty()); |
| 612 |
NamedEntity result{x.front()}; |
| 613 |
int j{0}; |
| 614 |
for (const Symbol &symbol : x) { |
| 615 |
if (j++ != 0) { |
| 616 |
DataRef base{result.IsSymbol() ? DataRef{result.GetLastSymbol()} |
| 617 |
: DataRef{result.GetComponent()}}; |
| 618 |
result = NamedEntity{Component{std::move(base), symbol}}; |
| 619 |
} |
| 620 |
} |
| 621 |
return result; |
| 622 |
} |
| 623 |
|
| 624 |
NamedEntity CoarrayRef::GetBase() const { return AsNamedEntity(base_); } |
| 625 |
|
| 626 |
// Equality testing |
| 627 |
|
| 628 |
// For the purposes of comparing type parameter expressions while |
| 629 |
// testing the compatibility of procedure characteristics, two |
| 630 |
// dummy arguments with the same position are considered equal. |
| 631 |
static std::optional<int> GetDummyArgPosition(const Symbol &original) { |
| 632 |
const Symbol &symbol(original.GetUltimate()); |
| 633 |
if (IsDummy(symbol)) { |
| 634 |
if (const Symbol * proc{symbol.owner().symbol()}) { |
| 635 |
if (const auto *subp{proc->detailsIf<semantics::SubprogramDetails>()}) { |
| 636 |
int j{0}; |
| 637 |
for (const Symbol *arg : subp->dummyArgs()) { |
| 638 |
if (arg == &symbol) { |
| 639 |
return j; |
| 640 |
} |
| 641 |
++j; |
| 642 |
} |
| 643 |
} |
| 644 |
} |
| 645 |
} |
| 646 |
return std::nullopt; |
| 647 |
} |
| 648 |
|
| 649 |
static bool AreSameSymbol(const Symbol &x, const Symbol &y) { |
| 650 |
if (&x == &y) { |
| 651 |
return true; |
| 652 |
} |
| 653 |
if (auto xPos{GetDummyArgPosition(x)}) { |
| 654 |
if (auto yPos{GetDummyArgPosition(y)}) { |
| 655 |
return *xPos == *yPos; |
| 656 |
} |
| 657 |
} |
| 658 |
return false; |
| 659 |
} |
| 660 |
|
| 661 |
// Implements operator==() for a union type, using special case handling |
| 662 |
// for Symbol references. |
| 663 |
template <typename A> static bool TestVariableEquality(const A &x, const A &y) { |
| 664 |
const SymbolRef *xSymbol{std::get_if<SymbolRef>(&x.u)}; |
| 665 |
if (const SymbolRef * ySymbol{std::get_if<SymbolRef>(&y.u)}) { |
| 666 |
return xSymbol && AreSameSymbol(*xSymbol, *ySymbol); |
| 667 |
} else { |
| 668 |
return x.u == y.u; |
| 669 |
} |
| 670 |
} |
| 671 |
|
| 672 |
bool BaseObject::operator==(const BaseObject &that) const { |
| 673 |
return TestVariableEquality(*this, that); |
| 674 |
} |
| 675 |
bool Component::operator==(const Component &that) const { |
| 676 |
return base_ == that.base_ && &*symbol_ == &*that.symbol_; |
| 677 |
} |
| 678 |
bool NamedEntity::operator==(const NamedEntity &that) const { |
| 679 |
if (IsSymbol()) { |
| 680 |
return that.IsSymbol() && |
| 681 |
AreSameSymbol(GetFirstSymbol(), that.GetFirstSymbol()); |
| 682 |
} else { |
| 683 |
return !that.IsSymbol() && GetComponent() == that.GetComponent(); |
| 684 |
} |
| 685 |
} |
| 686 |
bool TypeParamInquiry::operator==(const TypeParamInquiry &that) const { |
| 687 |
return &*parameter_ == &*that.parameter_ && base_ == that.base_; |
| 688 |
} |
| 689 |
bool Triplet::operator==(const Triplet &that) const { |
| 690 |
return lower_ == that.lower_ && upper_ == that.upper_ && |
| 691 |
stride_ == that.stride_; |
| 692 |
} |
| 693 |
bool Subscript::operator==(const Subscript &that) const { return u == that.u; } |
| 694 |
bool ArrayRef::operator==(const ArrayRef &that) const { |
| 695 |
return base_ == that.base_ && subscript_ == that.subscript_; |
| 696 |
} |
| 697 |
bool CoarrayRef::operator==(const CoarrayRef &that) const { |
| 698 |
return base_ == that.base_ && subscript_ == that.subscript_ && |
| 699 |
cosubscript_ == that.cosubscript_ && stat_ == that.stat_ && |
| 700 |
team_ == that.team_ && teamIsTeamNumber_ == that.teamIsTeamNumber_; |
| 701 |
} |
| 702 |
bool DataRef::operator==(const DataRef &that) const { |
| 703 |
return TestVariableEquality(*this, that); |
| 704 |
} |
| 705 |
bool Substring::operator==(const Substring &that) const { |
| 706 |
return parent_ == that.parent_ && lower_ == that.lower_ && |
| 707 |
upper_ == that.upper_; |
| 708 |
} |
| 709 |
bool ComplexPart::operator==(const ComplexPart &that) const { |
| 710 |
return part_ == that.part_ && complex_ == that.complex_; |
| 711 |
} |
| 712 |
bool ProcedureRef::operator==(const ProcedureRef &that) const { |
| 713 |
return proc_ == that.proc_ && arguments_ == that.arguments_; |
| 714 |
} |
| 715 |
template <typename T> |
| 716 |
bool Designator<T>::operator==(const Designator<T> &that) const { |
| 717 |
return TestVariableEquality(*this, that); |
| 718 |
} |
| 719 |
bool DescriptorInquiry::operator==(const DescriptorInquiry &that) const { |
| 720 |
return field_ == that.field_ && base_ == that.base_ && |
| 721 |
dimension_ == that.dimension_; |
| 722 |
} |
| 723 |
|
| 724 |
#ifdef _MSC_VER // disable bogus warning about missing definitions |
| 725 |
#pragma warning(disable : 4661) |
| 726 |
#endif |
| 727 |
INSTANTIATE_VARIABLE_TEMPLATES |
| 728 |
} // namespace Fortran::evaluate |
| 729 |
|
| 730 |
template class Fortran::common::Indirection<Fortran::evaluate::Component, true>; |