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