1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
|
//===-- lib/Evaluate/type.cpp ---------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "flang/Evaluate/type.h"
#include "flang/Common/idioms.h"
#include "flang/Evaluate/expression.h"
#include "flang/Evaluate/fold.h"
#include "flang/Evaluate/target.h"
#include "flang/Parser/characters.h"
#include "flang/Semantics/scope.h"
#include "flang/Semantics/symbol.h"
#include "flang/Semantics/tools.h"
#include "flang/Semantics/type.h"
#include <algorithm>
#include <optional>
#include <string>
// IsDescriptor() predicate: true when a symbol is implemented
// at runtime with a descriptor.
namespace Fortran::semantics {
static bool IsDescriptor(const DeclTypeSpec *type) {
if (type) {
if (auto dynamicType{evaluate::DynamicType::From(*type)}) {
return dynamicType->RequiresDescriptor();
}
}
return false;
}
static bool IsDescriptor(const ObjectEntityDetails &details) {
if (IsDescriptor(details.type())) {
return true;
}
for (const ShapeSpec &shapeSpec : details.shape()) {
const auto &lb{shapeSpec.lbound().GetExplicit()};
const auto &ub{shapeSpec.ubound().GetExplicit()};
if (!lb || !ub || !IsConstantExpr(*lb) || !IsConstantExpr(*ub)) {
return true;
}
}
return false;
}
bool IsDescriptor(const Symbol &symbol) {
return common::visit(
common::visitors{
[&](const ObjectEntityDetails &d) {
return IsAllocatableOrPointer(symbol) || IsDescriptor(d);
},
[&](const ProcEntityDetails &d) { return false; },
[&](const EntityDetails &d) { return IsDescriptor(d.type()); },
[](const AssocEntityDetails &d) {
if (const auto &expr{d.expr()}) {
if (expr->Rank() > 0) {
return true;
}
if (const auto dynamicType{expr->GetType()}) {
if (dynamicType->RequiresDescriptor()) {
return true;
}
}
}
return false;
},
[](const SubprogramDetails &d) {
return d.isFunction() && IsDescriptor(d.result());
},
[](const UseDetails &d) { return IsDescriptor(d.symbol()); },
[](const HostAssocDetails &d) { return IsDescriptor(d.symbol()); },
[](const auto &) { return false; },
},
symbol.details());
}
bool IsPassedViaDescriptor(const Symbol &symbol) {
if (!IsDescriptor(symbol)) {
return false;
}
if (IsAllocatableOrPointer(symbol)) {
return true;
}
if (const auto *object{
symbol.GetUltimate().detailsIf<ObjectEntityDetails>()}) {
if (object->isDummy()) {
if (object->type() &&
object->type()->category() == DeclTypeSpec::Character) {
return false;
}
if (object->IsAssumedSize()) {
return false;
}
bool isExplicitShape{true};
for (const ShapeSpec &shapeSpec : object->shape()) {
if (!shapeSpec.lbound().GetExplicit() ||
!shapeSpec.ubound().GetExplicit()) {
isExplicitShape = false;
break;
}
}
if (isExplicitShape) {
return false; // explicit shape but non-constant bounds
}
}
}
return true;
}
} // namespace Fortran::semantics
namespace Fortran::evaluate {
DynamicType::DynamicType(int k, const semantics::ParamValue &pv)
: category_{TypeCategory::Character}, kind_{k} {
CHECK(IsValidKindOfIntrinsicType(category_, kind_));
if (auto n{ToInt64(pv.GetExplicit())}) {
knownLength_ = *n > 0 ? *n : 0;
} else {
charLengthParamValue_ = &pv;
}
}
template <typename A> inline bool PointeeComparison(const A *x, const A *y) {
return x == y || (x && y && *x == *y);
}
bool DynamicType::operator==(const DynamicType &that) const {
return category_ == that.category_ && kind_ == that.kind_ &&
PointeeComparison(charLengthParamValue_, that.charLengthParamValue_) &&
knownLength().has_value() == that.knownLength().has_value() &&
(!knownLength() || *knownLength() == *that.knownLength()) &&
PointeeComparison(derived_, that.derived_);
}
std::optional<Expr<SubscriptInteger>> DynamicType::GetCharLength() const {
if (category_ == TypeCategory::Character) {
if (knownLength()) {
return AsExpr(Constant<SubscriptInteger>(*knownLength()));
} else if (charLengthParamValue_) {
if (auto length{charLengthParamValue_->GetExplicit()}) {
return ConvertToType<SubscriptInteger>(std::move(*length));
}
}
}
return std::nullopt;
}
std::size_t DynamicType::GetAlignment(
const TargetCharacteristics &targetCharacteristics) const {
if (category_ == TypeCategory::Derived) {
switch (GetDerivedTypeSpec().category()) {
SWITCH_COVERS_ALL_CASES
case semantics::DerivedTypeSpec::Category::DerivedType:
if (derived_ && derived_->scope()) {
return derived_->scope()->alignment().value_or(1);
}
break;
case semantics::DerivedTypeSpec::Category::IntrinsicVector:
case semantics::DerivedTypeSpec::Category::PairVector:
case semantics::DerivedTypeSpec::Category::QuadVector:
if (derived_ && derived_->scope()) {
return derived_->scope()->size();
} else {
common::die("Missing scope for Vector type.");
}
}
} else {
return targetCharacteristics.GetAlignment(category_, kind_);
}
return 1; // needs to be after switch to dodge a bogus gcc warning
}
std::optional<Expr<SubscriptInteger>> DynamicType::MeasureSizeInBytes(
FoldingContext &context, bool aligned,
std::optional<std::int64_t> charLength) const {
switch (category_) {
case TypeCategory::Integer:
case TypeCategory::Real:
case TypeCategory::Complex:
case TypeCategory::Logical:
return Expr<SubscriptInteger>{
context.targetCharacteristics().GetByteSize(category_, kind_)};
case TypeCategory::Character:
if (auto len{charLength ? Expr<SubscriptInteger>{Constant<SubscriptInteger>{
*charLength}}
: GetCharLength()}) {
return Fold(context,
Expr<SubscriptInteger>{
context.targetCharacteristics().GetByteSize(category_, kind_)} *
std::move(*len));
}
break;
case TypeCategory::Derived:
if (!IsPolymorphic() && derived_ && derived_->scope()) {
auto size{derived_->scope()->size()};
auto align{aligned ? derived_->scope()->alignment().value_or(0) : 0};
auto alignedSize{align > 0 ? ((size + align - 1) / align) * align : size};
return Expr<SubscriptInteger>{
static_cast<ConstantSubscript>(alignedSize)};
}
break;
}
return std::nullopt;
}
bool DynamicType::IsAssumedLengthCharacter() const {
return category_ == TypeCategory::Character && charLengthParamValue_ &&
charLengthParamValue_->isAssumed();
}
bool DynamicType::IsNonConstantLengthCharacter() const {
if (category_ != TypeCategory::Character) {
return false;
} else if (knownLength()) {
return false;
} else if (!charLengthParamValue_) {
return true;
} else if (const auto &expr{charLengthParamValue_->GetExplicit()}) {
return !IsConstantExpr(*expr);
} else {
return true;
}
}
bool DynamicType::IsTypelessIntrinsicArgument() const {
return category_ == TypeCategory::Integer && kind_ == TypelessKind;
}
const semantics::DerivedTypeSpec *GetDerivedTypeSpec(
const std::optional<DynamicType> &type) {
return type ? GetDerivedTypeSpec(*type) : nullptr;
}
const semantics::DerivedTypeSpec *GetDerivedTypeSpec(const DynamicType &type) {
if (type.category() == TypeCategory::Derived &&
!type.IsUnlimitedPolymorphic()) {
return &type.GetDerivedTypeSpec();
} else {
return nullptr;
}
}
static const semantics::Symbol *FindParentComponent(
const semantics::DerivedTypeSpec &derived) {
const semantics::Symbol &typeSymbol{derived.typeSymbol()};
const semantics::Scope *scope{derived.scope()};
if (!scope) {
scope = typeSymbol.scope();
}
if (scope) {
const auto &dtDetails{typeSymbol.get<semantics::DerivedTypeDetails>()};
// TODO: Combine with semantics::DerivedTypeDetails::GetParentComponent
if (auto extends{dtDetails.GetParentComponentName()}) {
if (auto iter{scope->find(*extends)}; iter != scope->cend()) {
if (const semantics::Symbol & symbol{*iter->second};
symbol.test(semantics::Symbol::Flag::ParentComp)) {
return &symbol;
}
}
}
}
return nullptr;
}
const semantics::DerivedTypeSpec *GetParentTypeSpec(
const semantics::DerivedTypeSpec &derived) {
if (const semantics::Symbol * parent{FindParentComponent(derived)}) {
return &parent->get<semantics::ObjectEntityDetails>()
.type()
->derivedTypeSpec();
} else {
return nullptr;
}
}
// Compares two derived type representations to see whether they both
// represent the "same type" in the sense of section 7.5.2.4.
using SetOfDerivedTypePairs =
std::set<std::pair<const semantics::DerivedTypeSpec *,
const semantics::DerivedTypeSpec *>>;
static bool AreSameDerivedType(const semantics::DerivedTypeSpec &,
const semantics::DerivedTypeSpec &, bool ignoreTypeParameterValues,
bool ignoreLenParameters, SetOfDerivedTypePairs &inProgress);
// F2023 7.5.3.2
static bool AreSameComponent(const semantics::Symbol &x,
const semantics::Symbol &y, SetOfDerivedTypePairs &inProgress) {
if (x.attrs() != y.attrs()) {
return false;
}
if (x.attrs().test(semantics::Attr::PRIVATE)) {
return false;
}
if (x.size() && y.size()) {
if (x.offset() != y.offset() || x.size() != y.size()) {
return false;
}
}
const auto *xObj{x.detailsIf<semantics::ObjectEntityDetails>()};
const auto *yObj{y.detailsIf<semantics::ObjectEntityDetails>()};
const auto *xProc{x.detailsIf<semantics::ProcEntityDetails>()};
const auto *yProc{y.detailsIf<semantics::ProcEntityDetails>()};
if (!xObj != !yObj || !xProc != !yProc) {
return false;
}
auto xType{DynamicType::From(x)};
auto yType{DynamicType::From(y)};
if (xType && yType) {
if (xType->category() == TypeCategory::Derived) {
if (yType->category() != TypeCategory::Derived ||
!xType->IsUnlimitedPolymorphic() !=
!yType->IsUnlimitedPolymorphic() ||
(!xType->IsUnlimitedPolymorphic() &&
!AreSameDerivedType(xType->GetDerivedTypeSpec(),
yType->GetDerivedTypeSpec(), false, false, inProgress))) {
return false;
}
} else if (!xType->IsTkLenCompatibleWith(*yType)) {
return false;
}
} else if (xType || yType || !(xProc && yProc)) {
return false;
}
if (xProc) {
// TODO: compare argument types, &c.
}
return true;
}
// TODO: These utilities were cloned out of Semantics to avoid a cyclic
// dependency and should be repackaged into then "namespace semantics"
// part of Evaluate/tools.cpp.
static const semantics::Symbol *GetParentComponent(
const semantics::DerivedTypeDetails &details,
const semantics::Scope &scope) {
if (auto extends{details.GetParentComponentName()}) {
if (auto iter{scope.find(*extends)}; iter != scope.cend()) {
if (const Symbol & symbol{*iter->second};
symbol.test(semantics::Symbol::Flag::ParentComp)) {
return &symbol;
}
}
}
return nullptr;
}
static const semantics::Symbol *GetParentComponent(
const semantics::Symbol *symbol, const semantics::Scope &scope) {
if (symbol) {
if (const auto *dtDetails{
symbol->detailsIf<semantics::DerivedTypeDetails>()}) {
return GetParentComponent(*dtDetails, scope);
}
}
return nullptr;
}
static const semantics::DerivedTypeSpec *GetParentTypeSpec(
const semantics::Symbol *symbol, const semantics::Scope &scope) {
if (const Symbol * parentComponent{GetParentComponent(symbol, scope)}) {
return &parentComponent->get<semantics::ObjectEntityDetails>()
.type()
->derivedTypeSpec();
} else {
return nullptr;
}
}
static const semantics::Scope *GetDerivedTypeParent(
const semantics::Scope *scope) {
if (scope) {
CHECK(scope->IsDerivedType());
if (const auto *parent{GetParentTypeSpec(scope->GetSymbol(), *scope)}) {
return parent->scope();
}
}
return nullptr;
}
static const semantics::Symbol *FindComponent(
const semantics::Scope *scope, parser::CharBlock name) {
if (!scope) {
return nullptr;
}
CHECK(scope->IsDerivedType());
auto found{scope->find(name)};
if (found != scope->end()) {
return &*found->second;
} else {
return FindComponent(GetDerivedTypeParent(scope), name);
}
}
static bool AreTypeParamCompatible(const semantics::DerivedTypeSpec &x,
const semantics::DerivedTypeSpec &y, bool ignoreLenParameters) {
const auto *xScope{x.typeSymbol().scope()};
const auto *yScope{y.typeSymbol().scope()};
for (const auto &[paramName, value] : x.parameters()) {
const auto *yValue{y.FindParameter(paramName)};
if (!yValue) {
return false;
}
const auto *xParm{FindComponent(xScope, paramName)};
const auto *yParm{FindComponent(yScope, paramName)};
if (xParm && yParm) {
const auto *xTPD{xParm->detailsIf<semantics::TypeParamDetails>()};
const auto *yTPD{yParm->detailsIf<semantics::TypeParamDetails>()};
if (xTPD && yTPD) {
if (xTPD->attr() != yTPD->attr()) {
return false;
}
if (!ignoreLenParameters ||
xTPD->attr() != common::TypeParamAttr::Len) {
auto xExpr{value.GetExplicit()};
auto yExpr{yValue->GetExplicit()};
if (xExpr && yExpr) {
auto xVal{ToInt64(*xExpr)};
auto yVal{ToInt64(*yExpr)};
if (xVal && yVal && *xVal != *yVal) {
return false;
}
}
}
}
}
}
for (const auto &[paramName, _] : y.parameters()) {
if (!x.FindParameter(paramName)) {
return false; // y has more parameters than x
}
}
return true;
}
// F2023 7.5.3.2
static bool AreSameDerivedType(const semantics::DerivedTypeSpec &x,
const semantics::DerivedTypeSpec &y, bool ignoreTypeParameterValues,
bool ignoreLenParameters, SetOfDerivedTypePairs &inProgress) {
if (&x == &y) {
return true;
}
if (!ignoreTypeParameterValues &&
!AreTypeParamCompatible(x, y, ignoreLenParameters)) {
return false;
}
const auto &xSymbol{x.typeSymbol().GetUltimate()};
const auto &ySymbol{y.typeSymbol().GetUltimate()};
if (xSymbol == ySymbol) {
return true;
}
if (xSymbol.name() != ySymbol.name()) {
return false;
}
auto thisQuery{std::make_pair(&x, &y)};
if (inProgress.find(thisQuery) != inProgress.end()) {
return true; // recursive use of types in components
}
inProgress.insert(thisQuery);
const auto &xDetails{xSymbol.get<semantics::DerivedTypeDetails>()};
const auto &yDetails{ySymbol.get<semantics::DerivedTypeDetails>()};
if (!(xDetails.sequence() && yDetails.sequence()) &&
!(xSymbol.attrs().test(semantics::Attr::BIND_C) &&
ySymbol.attrs().test(semantics::Attr::BIND_C))) {
// PGI does not enforce this requirement; all other Fortran
// compilers do with a hard error when violations are caught.
return false;
}
// Compare the component lists in their orders of declaration.
auto xEnd{xDetails.componentNames().cend()};
auto yComponentName{yDetails.componentNames().cbegin()};
auto yEnd{yDetails.componentNames().cend()};
for (auto xComponentName{xDetails.componentNames().cbegin()};
xComponentName != xEnd; ++xComponentName, ++yComponentName) {
if (yComponentName == yEnd || *xComponentName != *yComponentName ||
!xSymbol.scope() || !ySymbol.scope()) {
return false;
}
const auto xLookup{xSymbol.scope()->find(*xComponentName)};
const auto yLookup{ySymbol.scope()->find(*yComponentName)};
if (xLookup == xSymbol.scope()->end() ||
yLookup == ySymbol.scope()->end() ||
!AreSameComponent(*xLookup->second, *yLookup->second, inProgress)) {
return false;
}
}
return yComponentName == yEnd;
}
bool AreSameDerivedType(
const semantics::DerivedTypeSpec &x, const semantics::DerivedTypeSpec &y) {
SetOfDerivedTypePairs inProgress;
return AreSameDerivedType(x, y, false, false, inProgress);
}
static bool AreCompatibleDerivedTypes(const semantics::DerivedTypeSpec *x,
const semantics::DerivedTypeSpec *y, bool isPolymorphic,
bool ignoreTypeParameterValues, bool ignoreLenTypeParameters) {
if (!x || !y) {
return false;
} else {
SetOfDerivedTypePairs inProgress;
if (AreSameDerivedType(*x, *y, ignoreTypeParameterValues,
ignoreLenTypeParameters, inProgress)) {
return true;
} else {
return isPolymorphic &&
AreCompatibleDerivedTypes(x, GetParentTypeSpec(*y), true,
ignoreTypeParameterValues, ignoreLenTypeParameters);
}
}
}
static bool AreCompatibleTypes(const DynamicType &x, const DynamicType &y,
bool ignoreTypeParameterValues, bool ignoreLengths) {
if (x.IsUnlimitedPolymorphic()) {
return true;
} else if (y.IsUnlimitedPolymorphic()) {
return false;
} else if (x.category() != y.category()) {
return false;
} else if (x.category() == TypeCategory::Character) {
const auto xLen{x.knownLength()};
const auto yLen{y.knownLength()};
return x.kind() == y.kind() &&
(ignoreLengths || !xLen || !yLen || *xLen == *yLen);
} else if (x.category() != TypeCategory::Derived) {
return x.kind() == y.kind();
} else {
const auto *xdt{GetDerivedTypeSpec(x)};
const auto *ydt{GetDerivedTypeSpec(y)};
return AreCompatibleDerivedTypes(
xdt, ydt, x.IsPolymorphic(), ignoreTypeParameterValues, false);
}
}
// See 7.3.2.3 (5) & 15.5.2.4
bool DynamicType::IsTkCompatibleWith(const DynamicType &that) const {
return AreCompatibleTypes(*this, that, false, true);
}
bool DynamicType::IsTkCompatibleWith(
const DynamicType &that, common::IgnoreTKRSet ignoreTKR) const {
if (ignoreTKR.test(common::IgnoreTKR::Type) &&
(category() == TypeCategory::Derived ||
that.category() == TypeCategory::Derived ||
category() != that.category())) {
return true;
} else if (ignoreTKR.test(common::IgnoreTKR::Kind) &&
category() == that.category()) {
return true;
} else {
return AreCompatibleTypes(*this, that, false, true);
}
}
bool DynamicType::IsTkLenCompatibleWith(const DynamicType &that) const {
return AreCompatibleTypes(*this, that, false, false);
}
// 16.9.165
std::optional<bool> DynamicType::SameTypeAs(const DynamicType &that) const {
bool x{AreCompatibleTypes(*this, that, true, true)};
bool y{AreCompatibleTypes(that, *this, true, true)};
if (!x && !y) {
return false;
} else if (x && y && !IsPolymorphic() && !that.IsPolymorphic()) {
return true;
} else {
return std::nullopt;
}
}
// 16.9.76
std::optional<bool> DynamicType::ExtendsTypeOf(const DynamicType &that) const {
if (IsUnlimitedPolymorphic() || that.IsUnlimitedPolymorphic()) {
return std::nullopt; // unknown
}
const auto *thisDts{evaluate::GetDerivedTypeSpec(*this)};
const auto *thatDts{evaluate::GetDerivedTypeSpec(that)};
if (!thisDts || !thatDts) {
return std::nullopt;
} else if (!AreCompatibleDerivedTypes(thatDts, thisDts, true, true, true)) {
// Note that I check *thisDts, not its parent, so that EXTENDS_TYPE_OF()
// is .true. when they are the same type. This is technically
// an implementation-defined case in the standard, but every other
// compiler works this way.
if (IsPolymorphic() &&
AreCompatibleDerivedTypes(thisDts, thatDts, true, true, true)) {
// 'that' is *this or an extension of *this, and so runtime *this
// could be an extension of 'that'
return std::nullopt;
} else {
return false;
}
} else if (that.IsPolymorphic()) {
return std::nullopt; // unknown
} else {
return true;
}
}
std::optional<DynamicType> DynamicType::From(
const semantics::DeclTypeSpec &type) {
if (const auto *intrinsic{type.AsIntrinsic()}) {
if (auto kind{ToInt64(intrinsic->kind())}) {
TypeCategory category{intrinsic->category()};
if (IsValidKindOfIntrinsicType(category, *kind)) {
if (category == TypeCategory::Character) {
const auto &charType{type.characterTypeSpec()};
return DynamicType{static_cast<int>(*kind), charType.length()};
} else {
return DynamicType{category, static_cast<int>(*kind)};
}
}
}
} else if (const auto *derived{type.AsDerived()}) {
return DynamicType{
*derived, type.category() == semantics::DeclTypeSpec::ClassDerived};
} else if (type.category() == semantics::DeclTypeSpec::ClassStar) {
return DynamicType::UnlimitedPolymorphic();
} else if (type.category() == semantics::DeclTypeSpec::TypeStar) {
return DynamicType::AssumedType();
} else {
common::die("DynamicType::From(DeclTypeSpec): failed");
}
return std::nullopt;
}
std::optional<DynamicType> DynamicType::From(const semantics::Symbol &symbol) {
return From(symbol.GetType()); // Symbol -> DeclTypeSpec -> DynamicType
}
DynamicType DynamicType::ResultTypeForMultiply(const DynamicType &that) const {
switch (category_) {
case TypeCategory::Integer:
switch (that.category_) {
case TypeCategory::Integer:
return DynamicType{TypeCategory::Integer, std::max(kind_, that.kind_)};
case TypeCategory::Real:
case TypeCategory::Complex:
return that;
default:
CRASH_NO_CASE;
}
break;
case TypeCategory::Real:
switch (that.category_) {
case TypeCategory::Integer:
return *this;
case TypeCategory::Real:
return DynamicType{TypeCategory::Real, std::max(kind_, that.kind_)};
case TypeCategory::Complex:
return DynamicType{TypeCategory::Complex, std::max(kind_, that.kind_)};
default:
CRASH_NO_CASE;
}
break;
case TypeCategory::Complex:
switch (that.category_) {
case TypeCategory::Integer:
return *this;
case TypeCategory::Real:
case TypeCategory::Complex:
return DynamicType{TypeCategory::Complex, std::max(kind_, that.kind_)};
default:
CRASH_NO_CASE;
}
break;
case TypeCategory::Logical:
switch (that.category_) {
case TypeCategory::Logical:
return DynamicType{TypeCategory::Logical, std::max(kind_, that.kind_)};
default:
CRASH_NO_CASE;
}
break;
default:
CRASH_NO_CASE;
}
return *this;
}
bool DynamicType::RequiresDescriptor() const {
return IsPolymorphic() || IsNonConstantLengthCharacter() ||
(derived_ && CountNonConstantLenParameters(*derived_) > 0);
}
bool DynamicType::HasDeferredTypeParameter() const {
if (derived_) {
for (const auto &pair : derived_->parameters()) {
if (pair.second.isDeferred()) {
return true;
}
}
}
return charLengthParamValue_ && charLengthParamValue_->isDeferred();
}
bool SomeKind<TypeCategory::Derived>::operator==(
const SomeKind<TypeCategory::Derived> &that) const {
return PointeeComparison(derivedTypeSpec_, that.derivedTypeSpec_);
}
int SelectedCharKind(const std::string &s, int defaultKind) { // 16.9.168
auto lower{parser::ToLowerCaseLetters(s)};
auto n{lower.size()};
while (n > 0 && lower[0] == ' ') {
lower.erase(0, 1);
--n;
}
while (n > 0 && lower[n - 1] == ' ') {
lower.erase(--n, 1);
}
if (lower == "ascii") {
return 1;
} else if (lower == "ucs-2") {
return 2;
} else if (lower == "iso_10646" || lower == "ucs-4") {
return 4;
} else if (lower == "default") {
return defaultKind;
} else {
return -1;
}
}
std::optional<DynamicType> ComparisonType(
const DynamicType &t1, const DynamicType &t2) {
switch (t1.category()) {
case TypeCategory::Integer:
switch (t2.category()) {
case TypeCategory::Integer:
return DynamicType{TypeCategory::Integer, std::max(t1.kind(), t2.kind())};
case TypeCategory::Real:
case TypeCategory::Complex:
return t2;
default:
return std::nullopt;
}
case TypeCategory::Real:
switch (t2.category()) {
case TypeCategory::Integer:
return t1;
case TypeCategory::Real:
case TypeCategory::Complex:
return DynamicType{t2.category(), std::max(t1.kind(), t2.kind())};
default:
return std::nullopt;
}
case TypeCategory::Complex:
switch (t2.category()) {
case TypeCategory::Integer:
return t1;
case TypeCategory::Real:
case TypeCategory::Complex:
return DynamicType{TypeCategory::Complex, std::max(t1.kind(), t2.kind())};
default:
return std::nullopt;
}
case TypeCategory::Character:
switch (t2.category()) {
case TypeCategory::Character:
return DynamicType{
TypeCategory::Character, std::max(t1.kind(), t2.kind())};
default:
return std::nullopt;
}
case TypeCategory::Logical:
switch (t2.category()) {
case TypeCategory::Logical:
return DynamicType{TypeCategory::Logical, LogicalResult::kind};
default:
return std::nullopt;
}
default:
return std::nullopt;
}
}
bool IsInteroperableIntrinsicType(const DynamicType &type,
const common::LanguageFeatureControl *features, bool checkCharLength) {
switch (type.category()) {
case TypeCategory::Integer:
return true;
case TypeCategory::Real:
case TypeCategory::Complex:
return (features && features->IsEnabled(common::LanguageFeature::CUDA)) ||
type.kind() >= 4; // no short or half floats
case TypeCategory::Logical:
return type.kind() == 1; // C_BOOL
case TypeCategory::Character:
if (checkCharLength && type.knownLength().value_or(0) != 1) {
return false;
}
return type.kind() == 1 /* C_CHAR */;
default:
// Derived types are tested in Semantics/check-declarations.cpp
return false;
}
}
bool IsCUDAIntrinsicType(const DynamicType &type) {
switch (type.category()) {
case TypeCategory::Integer:
case TypeCategory::Logical:
return type.kind() <= 8;
case TypeCategory::Real:
return type.kind() >= 2 && type.kind() <= 8;
case TypeCategory::Complex:
return type.kind() == 2 || type.kind() == 4 || type.kind() == 8;
case TypeCategory::Character:
return type.kind() == 1;
default:
// Derived types are tested in Semantics/check-declarations.cpp
return false;
}
}
} // namespace Fortran::evaluate
|