[flang-commits] [flang] 8f971ca - [flang] Move DumpEvaluateExpr from Lower to Semantics (#128723)

via flang-commits flang-commits at lists.llvm.org
Mon Mar 3 13:38:46 PST 2025


Author: Krzysztof Parzyszek
Date: 2025-03-03T15:38:42-06:00
New Revision: 8f971ca1d939d65ca077ec5f86cd33652d09feee

URL: https://github.com/llvm/llvm-project/commit/8f971ca1d939d65ca077ec5f86cd33652d09feee
DIFF: https://github.com/llvm/llvm-project/commit/8f971ca1d939d65ca077ec5f86cd33652d09feee.diff

LOG: [flang] Move DumpEvaluateExpr from Lower to Semantics (#128723)

Since evaluate::Expr can show up in the parse tree in the semantic
analysis step, make it possible to dump its structure in the Semantics
module.

The Lower module depends on Semantics, so the code is still accessible
in it.

Added: 
    flang/include/flang/Semantics/dump-expr.h
    flang/lib/Semantics/dump-expr.cpp

Modified: 
    flang/lib/Lower/CMakeLists.txt
    flang/lib/Lower/ConvertExpr.cpp
    flang/lib/Lower/ConvertExprToHLFIR.cpp
    flang/lib/Semantics/CMakeLists.txt

Removed: 
    flang/include/flang/Lower/DumpEvaluateExpr.h
    flang/lib/Lower/DumpEvaluateExpr.cpp


################################################################################
diff  --git a/flang/include/flang/Lower/DumpEvaluateExpr.h b/flang/include/flang/Lower/DumpEvaluateExpr.h
deleted file mode 100644
index 88f53e96a81c2..0000000000000
--- a/flang/include/flang/Lower/DumpEvaluateExpr.h
+++ /dev/null
@@ -1,212 +0,0 @@
-//===-- Lower/DumpEvaluateExpr.h --------------------------------*- C++ -*-===//
-//
-// 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
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef FORTRAN_LOWER_DUMPEVALUATEEXPR_H
-#define FORTRAN_LOWER_DUMPEVALUATEEXPR_H
-
-#include "flang/Evaluate/tools.h"
-#include "flang/Lower/Support/Utils.h"
-#include "llvm/ADT/StringRef.h"
-#include "llvm/ADT/Twine.h"
-
-namespace Fortran::lower {
-
-/// Class to dump Fortran::evaluate::Expr trees out in a user readable way.
-///
-/// FIXME: This can be improved to dump more information in some cases.
-class DumpEvaluateExpr {
-public:
-  DumpEvaluateExpr() : outs(llvm::errs()) {}
-  DumpEvaluateExpr(llvm::raw_ostream &str) : outs(str) {}
-
-  template <typename A>
-  static void dump(const A &x) {
-    DumpEvaluateExpr{}.show(x);
-  }
-  template <typename A>
-  static void dump(llvm::raw_ostream &stream, const A &x) {
-    DumpEvaluateExpr{stream}.show(x);
-  }
-
-private:
-  template <typename A, bool C>
-  void show(const Fortran::common::Indirection<A, C> &x) {
-    show(x.value());
-  }
-  template <typename A>
-  void show(const Fortran::semantics::SymbolRef x) {
-    show(*x);
-  }
-  template <typename A>
-  void show(const std::unique_ptr<A> &x) {
-    show(x.get());
-  }
-  template <typename A>
-  void show(const std::shared_ptr<A> &x) {
-    show(x.get());
-  }
-  template <typename A>
-  void show(const A *x) {
-    if (x) {
-      show(*x);
-      return;
-    }
-    print("nullptr");
-  }
-  template <typename A>
-  void show(const std::optional<A> &x) {
-    if (x) {
-      show(*x);
-      return;
-    }
-    print("None");
-  }
-  template <typename... A>
-  void show(const std::variant<A...> &u) {
-    Fortran::common::visit([&](const auto &v) { show(v); }, u);
-  }
-  template <typename A>
-  void show(const std::vector<A> &x) {
-    indent("vector");
-    for (const auto &v : x)
-      show(v);
-    outdent();
-  }
-  void show(const Fortran::evaluate::BOZLiteralConstant &);
-  void show(const Fortran::evaluate::NullPointer &);
-  template <typename T>
-  void show(const Fortran::evaluate::Constant<T> &x) {
-    if constexpr (T::category == Fortran::common::TypeCategory::Derived) {
-      indent("derived constant");
-      for (const auto &map : x.values())
-        for (const auto &pair : map)
-          show(pair.second.value());
-      outdent();
-    } else {
-      print("constant");
-    }
-  }
-  void show(const Fortran::semantics::Symbol &symbol);
-  void show(const Fortran::evaluate::StaticDataObject &);
-  void show(const Fortran::evaluate::ImpliedDoIndex &);
-  void show(const Fortran::evaluate::BaseObject &x);
-  void show(const Fortran::evaluate::Component &x);
-  void show(const Fortran::evaluate::NamedEntity &x);
-  void show(const Fortran::evaluate::TypeParamInquiry &x);
-  void show(const Fortran::evaluate::Triplet &x);
-  void show(const Fortran::evaluate::Subscript &x);
-  void show(const Fortran::evaluate::ArrayRef &x);
-  void show(const Fortran::evaluate::CoarrayRef &x);
-  void show(const Fortran::evaluate::DataRef &x);
-  void show(const Fortran::evaluate::Substring &x);
-  void show(const Fortran::evaluate::ComplexPart &x);
-  template <typename T>
-  void show(const Fortran::evaluate::Designator<T> &x) {
-    indent("designator");
-    show(x.u);
-    outdent();
-  }
-  template <typename T>
-  void show(const Fortran::evaluate::Variable<T> &x) {
-    indent("variable");
-    show(x.u);
-    outdent();
-  }
-  void show(const Fortran::evaluate::DescriptorInquiry &x);
-  void show(const Fortran::evaluate::SpecificIntrinsic &);
-  void show(const Fortran::evaluate::ProcedureDesignator &x);
-  void show(const Fortran::evaluate::ActualArgument &x);
-  void show(const Fortran::evaluate::ProcedureRef &x) {
-    indent("procedure ref");
-    show(x.proc());
-    show(x.arguments());
-    outdent();
-  }
-  template <typename T>
-  void show(const Fortran::evaluate::FunctionRef<T> &x) {
-    indent("function ref");
-    show(x.proc());
-    show(x.arguments());
-    outdent();
-  }
-  template <typename T>
-  void show(const Fortran::evaluate::ArrayConstructorValue<T> &x) {
-    show(x.u);
-  }
-  template <typename T>
-  void show(const Fortran::evaluate::ArrayConstructorValues<T> &x) {
-    indent("array constructor value");
-    for (auto &v : x)
-      show(v);
-    outdent();
-  }
-  template <typename T>
-  void show(const Fortran::evaluate::ImpliedDo<T> &x) {
-    indent("implied do");
-    show(x.lower());
-    show(x.upper());
-    show(x.stride());
-    show(x.values());
-    outdent();
-  }
-  void show(const Fortran::semantics::ParamValue &x);
-  void
-  show(const Fortran::semantics::DerivedTypeSpec::ParameterMapType::value_type
-           &x);
-  void show(const Fortran::semantics::DerivedTypeSpec &x);
-  void show(const Fortran::evaluate::StructureConstructorValues::value_type &x);
-  void show(const Fortran::evaluate::StructureConstructor &x);
-  template <typename D, typename R, typename O>
-  void show(const Fortran::evaluate::Operation<D, R, O> &op) {
-    indent("unary op");
-    show(op.left());
-    outdent();
-  }
-  template <typename D, typename R, typename LO, typename RO>
-  void show(const Fortran::evaluate::Operation<D, R, LO, RO> &op) {
-    indent("binary op");
-    show(op.left());
-    show(op.right());
-    outdent();
-  }
-  void
-  show(const Fortran::evaluate::Relational<Fortran::evaluate::SomeType> &x);
-  template <typename T>
-  void show(const Fortran::evaluate::Expr<T> &x) {
-    indent("expr T");
-    show(x.u);
-    outdent();
-  }
-
-  const char *getIndentString() const;
-  void print(llvm::Twine s);
-  void indent(llvm::StringRef s);
-  void outdent();
-
-  llvm::raw_ostream &outs;
-  unsigned level = 0;
-};
-
-LLVM_DUMP_METHOD void
-dumpEvExpr(const Fortran::evaluate::Expr<Fortran::evaluate::SomeType> &x);
-LLVM_DUMP_METHOD void dumpEvExpr(
-    const Fortran::evaluate::Expr<
-        Fortran::evaluate::Type<Fortran::common::TypeCategory::Integer, 4>> &x);
-LLVM_DUMP_METHOD void dumpEvExpr(
-    const Fortran::evaluate::Expr<
-        Fortran::evaluate::Type<Fortran::common::TypeCategory::Integer, 8>> &x);
-LLVM_DUMP_METHOD void dumpEvExpr(const Fortran::evaluate::ArrayRef &x);
-LLVM_DUMP_METHOD void dumpEvExpr(const Fortran::evaluate::DataRef &x);
-LLVM_DUMP_METHOD void dumpEvExpr(const Fortran::evaluate::Substring &x);
-LLVM_DUMP_METHOD void dumpEvExpr(
-    const Fortran::evaluate::Designator<
-        Fortran::evaluate::Type<Fortran::common::TypeCategory::Integer, 4>> &x);
-
-} // namespace Fortran::lower
-
-#endif // FORTRAN_LOWER_DUMPEVALUATEEXPR_H

diff  --git a/flang/include/flang/Semantics/dump-expr.h b/flang/include/flang/Semantics/dump-expr.h
new file mode 100644
index 0000000000000..54c41300ecf36
--- /dev/null
+++ b/flang/include/flang/Semantics/dump-expr.h
@@ -0,0 +1,197 @@
+//===-- Semantics/dump-expr.h -----------------------------------*- C++ -*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef FORTRAN_SEMANTICS_DUMPEXPR_H
+#define FORTRAN_SEMANTICS_DUMPEXPR_H
+
+#include "flang/Evaluate/tools.h"
+#include "flang/Semantics/symbol.h"
+#include "llvm/ADT/StringRef.h"
+#include "llvm/ADT/Twine.h"
+
+#include <memory>
+#include <optional>
+#include <variant>
+#include <vector>
+
+namespace Fortran::semantics {
+
+/// Class to dump evaluate::Expr trees out in a user readable way.
+///
+/// FIXME: This can be improved to dump more information in some cases.
+class DumpEvaluateExpr {
+public:
+  DumpEvaluateExpr() : outs_(llvm::errs()) {}
+  DumpEvaluateExpr(llvm::raw_ostream &str) : outs_(str) {}
+
+  template <typename A> static void Dump(const A &x) {
+    DumpEvaluateExpr{}.Show(x);
+  }
+  template <typename A>
+  static void Dump(llvm::raw_ostream &stream, const A &x) {
+    DumpEvaluateExpr{stream}.Show(x);
+  }
+
+private:
+  template <typename A, bool C> void Show(const common::Indirection<A, C> &x) {
+    Show(x.value());
+  }
+  template <typename A> void Show(const SymbolRef x) { Show(*x); }
+  template <typename A> void Show(const std::unique_ptr<A> &x) {
+    Show(x.get());
+  }
+  template <typename A> void Show(const std::shared_ptr<A> &x) {
+    Show(x.get());
+  }
+  template <typename A> void Show(const A *x) {
+    if (x) {
+      Show(*x);
+      return;
+    }
+    Print("nullptr");
+  }
+  template <typename A> void Show(const std::optional<A> &x) {
+    if (x) {
+      Show(*x);
+      return;
+    }
+    Print("None");
+  }
+  template <typename... A> void Show(const std::variant<A...> &u) {
+    common::visit([&](const auto &v) { Show(v); }, u);
+  }
+  template <typename A> void Show(const std::vector<A> &x) {
+    Indent("vector");
+    for (const auto &v : x) {
+      Show(v);
+    }
+    Outdent();
+  }
+  void Show(const evaluate::BOZLiteralConstant &);
+  void Show(const evaluate::NullPointer &);
+  template <typename T> void Show(const evaluate::Constant<T> &x) {
+    if constexpr (T::category == common::TypeCategory::Derived) {
+      Indent("derived constant");
+      for (const auto &map : x.values()) {
+        for (const auto &pair : map) {
+          Show(pair.second.value());
+        }
+      }
+      Outdent();
+    } else {
+      Print("constant");
+    }
+  }
+  void Show(const Symbol &symbol);
+  void Show(const evaluate::StaticDataObject &);
+  void Show(const evaluate::ImpliedDoIndex &);
+  void Show(const evaluate::BaseObject &x);
+  void Show(const evaluate::Component &x);
+  void Show(const evaluate::NamedEntity &x);
+  void Show(const evaluate::TypeParamInquiry &x);
+  void Show(const evaluate::Triplet &x);
+  void Show(const evaluate::Subscript &x);
+  void Show(const evaluate::ArrayRef &x);
+  void Show(const evaluate::CoarrayRef &x);
+  void Show(const evaluate::DataRef &x);
+  void Show(const evaluate::Substring &x);
+  void Show(const evaluate::ComplexPart &x);
+  template <typename T> void Show(const evaluate::Designator<T> &x) {
+    Indent("designator");
+    Show(x.u);
+    Outdent();
+  }
+  template <typename T> void Show(const evaluate::Variable<T> &x) {
+    Indent("variable");
+    Show(x.u);
+    Outdent();
+  }
+  void Show(const evaluate::DescriptorInquiry &x);
+  void Show(const evaluate::SpecificIntrinsic &);
+  void Show(const evaluate::ProcedureDesignator &x);
+  void Show(const evaluate::ActualArgument &x);
+  void Show(const evaluate::ProcedureRef &x) {
+    Indent("procedure ref");
+    Show(x.proc());
+    Show(x.arguments());
+    Outdent();
+  }
+  template <typename T> void Show(const evaluate::FunctionRef<T> &x) {
+    Indent("function ref");
+    Show(x.proc());
+    Show(x.arguments());
+    Outdent();
+  }
+  template <typename T> void Show(const evaluate::ArrayConstructorValue<T> &x) {
+    Show(x.u);
+  }
+  template <typename T>
+  void Show(const evaluate::ArrayConstructorValues<T> &x) {
+    Indent("array constructor value");
+    for (auto &v : x) {
+      Show(v);
+    }
+    Outdent();
+  }
+  template <typename T> void Show(const evaluate::ImpliedDo<T> &x) {
+    Indent("implied do");
+    Show(x.lower());
+    Show(x.upper());
+    Show(x.stride());
+    Show(x.values());
+    Outdent();
+  }
+  void Show(const ParamValue &x);
+  void Show(const DerivedTypeSpec::ParameterMapType::value_type &x);
+  void Show(const DerivedTypeSpec &x);
+  void Show(const evaluate::StructureConstructorValues::value_type &x);
+  void Show(const evaluate::StructureConstructor &x);
+  template <typename D, typename R, typename O>
+  void Show(const evaluate::Operation<D, R, O> &op) {
+    Indent("unary op");
+    Show(op.left());
+    Outdent();
+  }
+  template <typename D, typename R, typename LO, typename RO>
+  void Show(const evaluate::Operation<D, R, LO, RO> &op) {
+    Indent("binary op");
+    Show(op.left());
+    Show(op.right());
+    Outdent();
+  }
+  void Show(const evaluate::Relational<evaluate::SomeType> &x);
+  template <typename T> void Show(const evaluate::Expr<T> &x) {
+    Indent("expr T");
+    Show(x.u);
+    Outdent();
+  }
+
+  const char *GetIndentString() const;
+  void Print(llvm::Twine s);
+  void Indent(llvm::StringRef s);
+  void Outdent();
+
+  llvm::raw_ostream &outs_;
+  unsigned level_{0};
+};
+
+LLVM_DUMP_METHOD void DumpEvExpr(const evaluate::Expr<evaluate::SomeType> &x);
+LLVM_DUMP_METHOD void DumpEvExpr(
+    const evaluate::Expr<evaluate::Type<common::TypeCategory::Integer, 4>> &x);
+LLVM_DUMP_METHOD void DumpEvExpr(
+    const evaluate::Expr<evaluate::Type<common::TypeCategory::Integer, 8>> &x);
+LLVM_DUMP_METHOD void DumpEvExpr(const evaluate::ArrayRef &x);
+LLVM_DUMP_METHOD void DumpEvExpr(const evaluate::DataRef &x);
+LLVM_DUMP_METHOD void DumpEvExpr(const evaluate::Substring &x);
+LLVM_DUMP_METHOD void DumpEvExpr(
+    const evaluate::Designator<evaluate::Type<common::TypeCategory::Integer, 4>>
+        &x);
+
+} // namespace Fortran::semantics
+
+#endif // FORTRAN_SEMANTICS_DUMPEXPR_H

diff  --git a/flang/lib/Lower/CMakeLists.txt b/flang/lib/Lower/CMakeLists.txt
index 87dc2a052796a..0bd9a47cd040f 100644
--- a/flang/lib/Lower/CMakeLists.txt
+++ b/flang/lib/Lower/CMakeLists.txt
@@ -16,7 +16,6 @@ add_flang_library(FortranLower
   ConvertType.cpp
   ConvertVariable.cpp
   CustomIntrinsicCall.cpp
-  DumpEvaluateExpr.cpp
   HlfirIntrinsics.cpp
   HostAssociations.cpp
   IO.cpp

diff  --git a/flang/lib/Lower/ConvertExpr.cpp b/flang/lib/Lower/ConvertExpr.cpp
index b33baf90582b8..b677a136a74aa 100644
--- a/flang/lib/Lower/ConvertExpr.cpp
+++ b/flang/lib/Lower/ConvertExpr.cpp
@@ -27,7 +27,6 @@
 #include "flang/Lower/ConvertType.h"
 #include "flang/Lower/ConvertVariable.h"
 #include "flang/Lower/CustomIntrinsicCall.h"
-#include "flang/Lower/DumpEvaluateExpr.h"
 #include "flang/Lower/Mangler.h"
 #include "flang/Lower/Runtime.h"
 #include "flang/Lower/Support/Utils.h"
@@ -47,6 +46,7 @@
 #include "flang/Optimizer/Dialect/FIROpsSupport.h"
 #include "flang/Optimizer/Support/FatalError.h"
 #include "flang/Runtime/support.h"
+#include "flang/Semantics/dump-expr.h"
 #include "flang/Semantics/expression.h"
 #include "flang/Semantics/symbol.h"
 #include "flang/Semantics/tools.h"
@@ -3925,7 +3925,7 @@ class ArrayExprLowering {
   /// determine the actual number of iterations when slicing triples are
   /// present. Lower these expressions here.
   bool determineShapeWithSlice(const Fortran::lower::SomeExpr &lhs) {
-    LLVM_DEBUG(Fortran::lower::DumpEvaluateExpr::dump(
+    LLVM_DEBUG(Fortran::semantics::DumpEvaluateExpr::Dump(
         llvm::dbgs() << "determine shape of:\n", lhs));
     // FIXME: We may not want to use ExtractDataRef here since it doesn't deal
     // with substrings, etc.
@@ -5073,7 +5073,7 @@ class ArrayExprLowering {
 
   template <typename A>
   CC genarr(const Fortran::evaluate::Expr<A> &x) {
-    LLVM_DEBUG(Fortran::lower::DumpEvaluateExpr::dump(llvm::dbgs(), x));
+    LLVM_DEBUG(Fortran::semantics::DumpEvaluateExpr::Dump(llvm::dbgs(), x));
     if (isArray(x) || (explicitSpaceIsActive() && isLeftHandSide()) ||
         isElementalProcWithArrayArgs(x))
       return Fortran::common::visit([&](const auto &e) { return genarr(e); },

diff  --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp
index 91daa6f0ad6ea..dc00e0b13f583 100644
--- a/flang/lib/Lower/ConvertExprToHLFIR.cpp
+++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp
@@ -21,7 +21,6 @@
 #include "flang/Lower/ConvertProcedureDesignator.h"
 #include "flang/Lower/ConvertType.h"
 #include "flang/Lower/ConvertVariable.h"
-#include "flang/Lower/DumpEvaluateExpr.h"
 #include "flang/Lower/StatementContext.h"
 #include "flang/Lower/SymbolMap.h"
 #include "flang/Optimizer/Builder/Complex.h"

diff  --git a/flang/lib/Lower/DumpEvaluateExpr.cpp b/flang/lib/Lower/DumpEvaluateExpr.cpp
deleted file mode 100644
index 9273e94d702f8..0000000000000
--- a/flang/lib/Lower/DumpEvaluateExpr.cpp
+++ /dev/null
@@ -1,272 +0,0 @@
-//===-- Lower/DumpEvaluateExpr.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/Lower/DumpEvaluateExpr.h"
-#include <iostream>
-
-static constexpr char whiteSpacePadding[] =
-    ">>                                               ";
-static constexpr auto whiteSize = sizeof(whiteSpacePadding) - 1;
-
-inline const char *Fortran::lower::DumpEvaluateExpr::getIndentString() const {
-  auto count = (level * 2 >= whiteSize) ? whiteSize : level * 2;
-  return whiteSpacePadding + whiteSize - count;
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::CoarrayRef &x) {
-  indent("coarray ref");
-  show(x.base());
-  show(x.subscript());
-  show(x.cosubscript());
-  show(x.stat());
-  show(x.team());
-  outdent();
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::BOZLiteralConstant &) {
-  print("BOZ literal constant");
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::NullPointer &) {
-  print("null pointer");
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::semantics::Symbol &symbol) {
-  const auto &ultimate{symbol.GetUltimate()};
-  print("symbol: "s + std::string(toStringRef(symbol.name())));
-  if (const auto *assoc =
-          ultimate.detailsIf<Fortran::semantics::AssocEntityDetails>()) {
-    indent("assoc details");
-    show(assoc->expr());
-    outdent();
-  }
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::StaticDataObject &) {
-  print("static data object");
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::ImpliedDoIndex &) {
-  print("implied do index");
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::BaseObject &x) {
-  indent("base object");
-  show(x.u);
-  outdent();
-}
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::Component &x) {
-  indent("component");
-  show(x.base());
-  show(x.GetLastSymbol());
-  outdent();
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::NamedEntity &x) {
-  indent("named entity");
-  if (const auto *component = x.UnwrapComponent())
-    show(*component);
-  else
-    show(x.GetFirstSymbol());
-  outdent();
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::TypeParamInquiry &x) {
-  indent("type inquiry");
-  show(x.base());
-  outdent();
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::Triplet &x) {
-  indent("triplet");
-  show(x.lower());
-  show(x.upper());
-  show(x.stride());
-  outdent();
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::Subscript &x) {
-  indent("subscript");
-  show(x.u);
-  outdent();
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::ArrayRef &x) {
-  indent("array ref");
-  show(x.base());
-  show(x.subscript());
-  outdent();
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::DataRef &x) {
-  indent("data ref");
-  show(x.u);
-  outdent();
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::Substring &x) {
-  indent("substring");
-  show(x.parent());
-  show(x.lower());
-  show(x.upper());
-  outdent();
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::semantics::ParamValue &x) {
-  indent("param value");
-  show(x.GetExplicit());
-  outdent();
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::semantics::DerivedTypeSpec::ParameterMapType::value_type
-        &x) {
-  show(x.second);
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::semantics::DerivedTypeSpec &x) {
-  indent("derived type spec");
-  for (auto &v : x.parameters())
-    show(v);
-  outdent();
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::StructureConstructorValues::value_type &x) {
-  show(x.second);
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::StructureConstructor &x) {
-  indent("structure constructor");
-  show(x.derivedTypeSpec());
-  for (auto &v : x)
-    show(v);
-  outdent();
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::Relational<Fortran::evaluate::SomeType> &x) {
-  indent("expr some type");
-  show(x.u);
-  outdent();
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::ComplexPart &x) {
-  indent("complex part");
-  show(x.complex());
-  outdent();
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::ActualArgument &x) {
-  indent("actual argument");
-  if (const auto *symbol = x.GetAssumedTypeDummy())
-    show(*symbol);
-  else
-    show(x.UnwrapExpr());
-  outdent();
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::ProcedureDesignator &x) {
-  indent("procedure designator");
-  if (const auto *component = x.GetComponent())
-    show(*component);
-  else if (const auto *symbol = x.GetSymbol())
-    show(*symbol);
-  else
-    show(DEREF(x.GetSpecificIntrinsic()));
-  outdent();
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::SpecificIntrinsic &) {
-  print("specific intrinsic");
-}
-
-void Fortran::lower::DumpEvaluateExpr::show(
-    const Fortran::evaluate::DescriptorInquiry &x) {
-  indent("descriptor inquiry");
-  show(x.base());
-  outdent();
-}
-
-void Fortran::lower::DumpEvaluateExpr::print(llvm::Twine twine) {
-  outs << getIndentString() << twine << '\n';
-}
-
-void Fortran::lower::DumpEvaluateExpr::indent(llvm::StringRef s) {
-  print(s + " {");
-  level++;
-}
-
-void Fortran::lower::DumpEvaluateExpr::outdent() {
-  if (level)
-    level--;
-  print("}");
-}
-
-//===----------------------------------------------------------------------===//
-// Boilerplate entry points that the debugger can find.
-//===----------------------------------------------------------------------===//
-
-void Fortran::lower::dumpEvExpr(const Fortran::semantics::SomeExpr &x) {
-  DumpEvaluateExpr::dump(x);
-}
-
-void Fortran::lower::dumpEvExpr(
-    const Fortran::evaluate::Expr<
-        Fortran::evaluate::Type<Fortran::common::TypeCategory::Integer, 4>>
-        &x) {
-  DumpEvaluateExpr::dump(x);
-}
-
-void Fortran::lower::dumpEvExpr(
-    const Fortran::evaluate::Expr<
-        Fortran::evaluate::Type<Fortran::common::TypeCategory::Integer, 8>>
-        &x) {
-  DumpEvaluateExpr::dump(x);
-}
-
-void Fortran::lower::dumpEvExpr(const Fortran::evaluate::ArrayRef &x) {
-  DumpEvaluateExpr::dump(x);
-}
-
-void Fortran::lower::dumpEvExpr(const Fortran::evaluate::DataRef &x) {
-  DumpEvaluateExpr::dump(x);
-}
-
-void Fortran::lower::dumpEvExpr(const Fortran::evaluate::Substring &x) {
-  DumpEvaluateExpr::dump(x);
-}
-
-void Fortran::lower::dumpEvExpr(
-    const Fortran::evaluate::Designator<
-        Fortran::evaluate::Type<Fortran::common::TypeCategory::Integer, 4>>
-        &x) {
-  DumpEvaluateExpr::dump(x);
-}

diff  --git a/flang/lib/Semantics/CMakeLists.txt b/flang/lib/Semantics/CMakeLists.txt
index 00108dde49dbd..93bf0c7c5facd 100644
--- a/flang/lib/Semantics/CMakeLists.txt
+++ b/flang/lib/Semantics/CMakeLists.txt
@@ -29,6 +29,7 @@ add_flang_library(FortranSemantics
   compute-offsets.cpp
   data-to-inits.cpp
   definable.cpp
+  dump-expr.cpp
   expression.cpp
   mod-file.cpp
   openmp-modifiers.cpp

diff  --git a/flang/lib/Semantics/dump-expr.cpp b/flang/lib/Semantics/dump-expr.cpp
new file mode 100644
index 0000000000000..850904bf897b9
--- /dev/null
+++ b/flang/lib/Semantics/dump-expr.cpp
@@ -0,0 +1,242 @@
+//===-- Semantics/dump-expr.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/Semantics/dump-expr.h"
+
+namespace Fortran::semantics {
+
+static constexpr char whiteSpacePadding[]{
+    ">>                                               "};
+static constexpr auto whiteSize{sizeof(whiteSpacePadding) - 1};
+
+inline const char *DumpEvaluateExpr::GetIndentString() const {
+  auto count{(level_ * 2 >= whiteSize) ? whiteSize : level_ * 2};
+  return whiteSpacePadding + whiteSize - count;
+}
+
+void DumpEvaluateExpr::Show(const evaluate::CoarrayRef &x) {
+  Indent("coarray ref");
+  Show(x.base());
+  Show(x.subscript());
+  Show(x.cosubscript());
+  Show(x.stat());
+  Show(x.team());
+  Outdent();
+}
+
+void DumpEvaluateExpr::Show(const evaluate::BOZLiteralConstant &) {
+  Print("BOZ literal constant");
+}
+
+void DumpEvaluateExpr::Show(const evaluate::NullPointer &) {
+  Print("null pointer");
+}
+
+void DumpEvaluateExpr::Show(const Symbol &symbol) {
+  const auto &ultimate{symbol.GetUltimate()};
+  Print("symbol: "s + symbol.name().ToString());
+  if (const auto *assoc{ultimate.detailsIf<AssocEntityDetails>()}) {
+    Indent("assoc details");
+    Show(assoc->expr());
+    Outdent();
+  }
+}
+
+void DumpEvaluateExpr::Show(const evaluate::StaticDataObject &) {
+  Print("static data object");
+}
+
+void DumpEvaluateExpr::Show(const evaluate::ImpliedDoIndex &) {
+  Print("implied do index");
+}
+
+void DumpEvaluateExpr::Show(const evaluate::BaseObject &x) {
+  Indent("base object");
+  Show(x.u);
+  Outdent();
+}
+void DumpEvaluateExpr::Show(const evaluate::Component &x) {
+  Indent("component");
+  Show(x.base());
+  Show(x.GetLastSymbol());
+  Outdent();
+}
+
+void DumpEvaluateExpr::Show(const evaluate::NamedEntity &x) {
+  Indent("named entity");
+  if (const auto *component{x.UnwrapComponent()}) {
+    Show(*component);
+  } else {
+    Show(x.GetFirstSymbol());
+  }
+  Outdent();
+}
+
+void DumpEvaluateExpr::Show(const evaluate::TypeParamInquiry &x) {
+  Indent("type inquiry");
+  Show(x.base());
+  Outdent();
+}
+
+void DumpEvaluateExpr::Show(const evaluate::Triplet &x) {
+  Indent("triplet");
+  Show(x.lower());
+  Show(x.upper());
+  Show(x.stride());
+  Outdent();
+}
+
+void DumpEvaluateExpr::Show(const evaluate::Subscript &x) {
+  Indent("subscript");
+  Show(x.u);
+  Outdent();
+}
+
+void DumpEvaluateExpr::Show(const evaluate::ArrayRef &x) {
+  Indent("array ref");
+  Show(x.base());
+  Show(x.subscript());
+  Outdent();
+}
+
+void DumpEvaluateExpr::Show(const evaluate::DataRef &x) {
+  Indent("data ref");
+  Show(x.u);
+  Outdent();
+}
+
+void DumpEvaluateExpr::Show(const evaluate::Substring &x) {
+  Indent("substring");
+  Show(x.parent());
+  Show(x.lower());
+  Show(x.upper());
+  Outdent();
+}
+
+void DumpEvaluateExpr::Show(const ParamValue &x) {
+  Indent("param value");
+  Show(x.GetExplicit());
+  Outdent();
+}
+
+void DumpEvaluateExpr::Show(
+    const DerivedTypeSpec::ParameterMapType::value_type &x) {
+  Show(x.second);
+}
+
+void DumpEvaluateExpr::Show(const DerivedTypeSpec &x) {
+  Indent("derived type spec");
+  for (auto &v : x.parameters()) {
+    Show(v);
+  }
+  Outdent();
+}
+
+void DumpEvaluateExpr::Show(
+    const evaluate::StructureConstructorValues::value_type &x) {
+  Show(x.second);
+}
+
+void DumpEvaluateExpr::Show(const evaluate::StructureConstructor &x) {
+  Indent("structure constructor");
+  Show(x.derivedTypeSpec());
+  for (auto &v : x) {
+    Show(v);
+  }
+  Outdent();
+}
+
+void DumpEvaluateExpr::Show(const evaluate::Relational<evaluate::SomeType> &x) {
+  Indent("expr some type");
+  Show(x.u);
+  Outdent();
+}
+
+void DumpEvaluateExpr::Show(const evaluate::ComplexPart &x) {
+  Indent("complex part");
+  Show(x.complex());
+  Outdent();
+}
+
+void DumpEvaluateExpr::Show(const evaluate::ActualArgument &x) {
+  Indent("actual argument");
+  if (const auto *symbol{x.GetAssumedTypeDummy()}) {
+    Show(*symbol);
+  } else {
+    Show(x.UnwrapExpr());
+  }
+  Outdent();
+}
+
+void DumpEvaluateExpr::Show(const evaluate::ProcedureDesignator &x) {
+  Indent("procedure designator");
+  if (const auto *component{x.GetComponent()}) {
+    Show(*component);
+  } else if (const auto *symbol{x.GetSymbol()}) {
+    Show(*symbol);
+  } else {
+    Show(DEREF(x.GetSpecificIntrinsic()));
+  }
+  Outdent();
+}
+
+void DumpEvaluateExpr::Show(const evaluate::SpecificIntrinsic &) {
+  Print("specific intrinsic");
+}
+
+void DumpEvaluateExpr::Show(const evaluate::DescriptorInquiry &x) {
+  Indent("descriptor inquiry");
+  Show(x.base());
+  Outdent();
+}
+
+void DumpEvaluateExpr::Print(llvm::Twine twine) {
+  outs_ << GetIndentString() << twine << '\n';
+}
+
+void DumpEvaluateExpr::Indent(llvm::StringRef s) {
+  Print(s + " {");
+  level_++;
+}
+
+void DumpEvaluateExpr::Outdent() {
+  if (level_) {
+    level_--;
+  }
+  Print("}");
+}
+
+//===----------------------------------------------------------------------===//
+// Boilerplate entry points that the debugger can find.
+//===----------------------------------------------------------------------===//
+
+void DumpEvExpr(const SomeExpr &x) { DumpEvaluateExpr::Dump(x); }
+
+void DumpEvExpr(
+    const evaluate::Expr<evaluate::Type<common::TypeCategory::Integer, 4>> &x) {
+  DumpEvaluateExpr::Dump(x);
+}
+
+void DumpEvExpr(
+    const evaluate::Expr<evaluate::Type<common::TypeCategory::Integer, 8>> &x) {
+  DumpEvaluateExpr::Dump(x);
+}
+
+void DumpEvExpr(const evaluate::ArrayRef &x) { DumpEvaluateExpr::Dump(x); }
+
+void DumpEvExpr(const evaluate::DataRef &x) { DumpEvaluateExpr::Dump(x); }
+
+void DumpEvExpr(const evaluate::Substring &x) { DumpEvaluateExpr::Dump(x); }
+
+void DumpEvExpr(
+    const evaluate::Designator<evaluate::Type<common::TypeCategory::Integer, 4>>
+        &x) {
+  DumpEvaluateExpr::Dump(x);
+}
+
+} // namespace Fortran::semantics


        


More information about the flang-commits mailing list