[flang-commits] [PATCH] D114030: [flang] Deal with negative character lengths in semantics

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Tue Nov 16 13:50:24 PST 2021


klausler created this revision.
klausler added a reviewer: jeanPerier.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
klausler requested review of this revision.

Fortran defines LEN(X) = 0 after CHARACTER(LEN=-1)::X so
apply MAX(0, ...) to character length expressions.


https://reviews.llvm.org/D114030

Files:
  flang/include/flang/Evaluate/tools.h
  flang/lib/Evaluate/variable.cpp
  flang/lib/Semantics/runtime-type-info.cpp


Index: flang/lib/Semantics/runtime-type-info.cpp
===================================================================
--- flang/lib/Semantics/runtime-type-info.cpp
+++ flang/lib/Semantics/runtime-type-info.cpp
@@ -679,6 +679,14 @@
     len = Fold(foldingContext, std::move(len));
   }
   if (dyType.category() == TypeCategory::Character && len) {
+    // Ignore IDIM(x) (represented as MAX(0, x))
+    if (const auto *clamped{evaluate::UnwrapExpr<
+            evaluate::Extremum<evaluate::SubscriptInteger>>(*len)}) {
+      if (clamped->ordering == evaluate::Ordering::Greater &&
+          clamped->left() == evaluate::Expr<evaluate::SubscriptInteger>{0}) {
+        len = clamped->right();
+      }
+    }
     AddValue(values, componentSchema_, "characterlen"s,
         evaluate::AsGenericExpr(GetValue(len, parameters)));
   } else {
Index: flang/lib/Evaluate/variable.cpp
===================================================================
--- flang/lib/Evaluate/variable.cpp
+++ flang/lib/Evaluate/variable.cpp
@@ -264,14 +264,19 @@
     if (const auto *chExpr{UnwrapExpr<Expr<SomeCharacter>>(assoc->expr())}) {
       return chExpr->LEN();
     }
-  } else if (auto dyType{DynamicType::From(ultimate)}) {
+  }
+  if (auto dyType{DynamicType::From(ultimate)}) {
     if (auto len{dyType->GetCharLength()}) {
-      return len;
-    } else if (IsDescriptor(ultimate) && !ultimate.owner().IsDerivedType()) {
-      return Expr<SubscriptInteger>{DescriptorInquiry{
-          NamedEntity{symbol}, DescriptorInquiry::Field::Len}};
+      if (ultimate.owner().IsDerivedType() || IsScopeInvariantExpr(*len)) {
+        return AsExpr(Extremum<SubscriptInteger>{
+            Ordering::Greater, Expr<SubscriptInteger>{0}, std::move(*len)});
+      }
     }
   }
+  if (IsDescriptor(ultimate) && !ultimate.owner().IsDerivedType()) {
+    return Expr<SubscriptInteger>{
+        DescriptorInquiry{NamedEntity{symbol}, DescriptorInquiry::Field::Len}};
+  }
   return std::nullopt;
 }
 
Index: flang/include/flang/Evaluate/tools.h
===================================================================
--- flang/include/flang/Evaluate/tools.h
+++ flang/include/flang/Evaluate/tools.h
@@ -236,7 +236,7 @@
 // a pointer to the Symbol with TypeParamDetails.
 template <typename A> const Symbol *ExtractBareLenParameter(const A &expr) {
   if (const auto *typeParam{
-          evaluate::UnwrapConvertedExpr<evaluate::TypeParamInquiry>(expr)}) {
+          UnwrapConvertedExpr<evaluate::TypeParamInquiry>(expr)}) {
     if (!typeParam->base()) {
       const Symbol &symbol{typeParam->parameter()};
       if (const auto *tpd{symbol.detailsIf<semantics::TypeParamDetails>()}) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D114030.387754.patch
Type: text/x-patch
Size: 2667 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20211116/e0650946/attachment-0001.bin>


More information about the flang-commits mailing list