FLANG
dump-expr.h
1//===-- Semantics/dump-expr.h -----------------------------------*- C++ -*-===//
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#ifndef FORTRAN_SEMANTICS_DUMPEXPR_H
10#define FORTRAN_SEMANTICS_DUMPEXPR_H
11
12#include "flang/Evaluate/tools.h"
13#include "flang/Semantics/symbol.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/Twine.h"
16
17#include <memory>
18#include <optional>
19#include <string>
20#include <variant>
21#include <vector>
22
23namespace Fortran::semantics {
24
28class DumpEvaluateExpr {
29public:
30 DumpEvaluateExpr() : outs_(llvm::errs()) {}
31 DumpEvaluateExpr(llvm::raw_ostream &str) : outs_(str) {}
32
33 template <typename A> static void Dump(const A &x) {
34 DumpEvaluateExpr{}.Show(x);
35 }
36 template <typename A>
37 static void Dump(llvm::raw_ostream &stream, const A &x) {
38 DumpEvaluateExpr{stream}.Show(x);
39 }
40
41private:
42 template <typename T> struct TypeOf {
43 static constexpr std::string_view get() {
44#if defined(__GNUC__)
45#define DUMP_EXPR_SHOW_TYPE
46 std::string_view v(__PRETTY_FUNCTION__);
47 // Extract the "xyz" from the "pretty function" string:
48 // "... [with T = xyz; std::string_view = ...]"
49 std::string_view front("with T = ");
50 std::string_view back("; std::string_view =");
51
52#elif defined(_MSC_VER)
53#define DUMP_EXPR_SHOW_TYPE
54 std::string_view v(__FUNCSIG__);
55 // Extract the "xyz" from the "pretty function" string:
56 // "...TypeOf<xyz>::get(void)"
57 std::string_view front("TypeOf<");
58 std::string_view back(">::get(void)");
59
60#endif
61
62#if defined(DUMP_EXPR_SHOW_TYPE)
63#undef DUMP_EXPR_SHOW_TYPE
64 if (auto fpos{v.find(front)}; fpos != v.npos) {
65 v.remove_prefix(fpos + front.size());
66 if (auto bpos{v.find(back)}; bpos != v.npos) {
67 v.remove_suffix(v.size() - bpos);
68 return v;
69 }
70 }
71#endif
72
73 return "";
74 }
75
76 static constexpr std::string_view name{TypeOf<T>::get()};
77 };
78
79 template <typename A, bool C> void Show(const common::Indirection<A, C> &x) {
80 Show(x.value());
81 }
82 template <typename A> void Show(const SymbolRef x) { Show(*x); }
83 template <typename A> void Show(const std::unique_ptr<A> &x) {
84 Show(x.get());
85 }
86 template <typename A> void Show(const std::shared_ptr<A> &x) {
87 Show(x.get());
88 }
89 template <typename A> void Show(const A *x) {
90 if (x) {
91 Show(*x);
92 return;
93 }
94 Print("nullptr");
95 }
96 template <typename A> void Show(const std::optional<A> &x) {
97 if (x) {
98 Show(*x);
99 return;
100 }
101 Print("None");
102 }
103 template <typename... A> void Show(const std::variant<A...> &u) {
104 common::visit([&](const auto &v) { Show(v); }, u);
105 }
106 template <typename A> void Show(const std::vector<A> &x) {
107 Indent("vector");
108 for (const auto &v : x) {
109 Show(v);
110 }
111 Outdent();
112 }
113 void Show(const evaluate::BOZLiteralConstant &);
114 void Show(const evaluate::NullPointer &);
115 template <typename T> void Show(const evaluate::Constant<T> &x) {
116 if constexpr (T::category == common::TypeCategory::Derived) {
117 Indent("derived constant "s + std::string(TypeOf<T>::name));
118 for (const auto &map : x.values()) {
119 for (const auto &pair : map) {
120 Show(pair.second.value());
121 }
122 }
123 Outdent();
124 } else {
125 Print("constant "s + std::string(TypeOf<T>::name));
126 }
127 }
128 void Show(const Symbol &symbol);
129 void Show(const evaluate::StaticDataObject &);
130 void Show(const evaluate::ImpliedDoIndex &);
131 void Show(const evaluate::BaseObject &x);
132 void Show(const evaluate::Component &x);
133 void Show(const evaluate::NamedEntity &x);
134 void Show(const evaluate::TypeParamInquiry &x);
135 void Show(const evaluate::Triplet &x);
136 void Show(const evaluate::Subscript &x);
137 void Show(const evaluate::ArrayRef &x);
138 void Show(const evaluate::CoarrayRef &x);
139 void Show(const evaluate::DataRef &x);
140 void Show(const evaluate::Substring &x);
141 void Show(const evaluate::ComplexPart &x);
142 template <typename T> void Show(const evaluate::Designator<T> &x) {
143 Indent("designator "s + std::string(TypeOf<T>::name));
144 Show(x.u);
145 Outdent();
146 }
147 void Show(const evaluate::DescriptorInquiry &x);
148 void Show(const evaluate::SpecificIntrinsic &);
149 void Show(const evaluate::ProcedureDesignator &x);
150 void Show(const evaluate::ActualArgument &x);
151 void Show(const evaluate::ProcedureRef &x) {
152 Indent("procedure ref");
153 Show(x.proc());
154 Show(x.arguments());
155 Outdent();
156 }
157 template <typename T> void Show(const evaluate::FunctionRef<T> &x) {
158 Indent("function ref "s + std::string(TypeOf<T>::name));
159 Show(x.proc());
160 Show(x.arguments());
161 Outdent();
162 }
163 template <typename T> void Show(const evaluate::ArrayConstructorValue<T> &x) {
164 Show(x.u);
165 }
166 template <typename T>
167 void Show(const evaluate::ArrayConstructorValues<T> &x) {
168 Indent("array constructor value "s + std::string(TypeOf<T>::name));
169 for (auto &v : x) {
170 Show(v);
171 }
172 Outdent();
173 }
174 template <typename T> void Show(const evaluate::ImpliedDo<T> &x) {
175 Indent("implied do "s + std::string(TypeOf<T>::name));
176 Show(x.lower());
177 Show(x.upper());
178 Show(x.stride());
179 Show(x.values());
180 Outdent();
181 }
182 void Show(const ParamValue &x);
183 void Show(const DerivedTypeSpec::ParameterMapType::value_type &x);
184 void Show(const DerivedTypeSpec &x);
185 void Show(const evaluate::StructureConstructorValues::value_type &x);
186 void Show(const evaluate::StructureConstructor &x);
187 template <typename D, typename R, typename O>
188 void Show(const evaluate::Operation<D, R, O> &op) {
189 Indent("unary op "s + std::string(TypeOf<D>::name));
190 Show(op.left());
191 Outdent();
192 }
193 template <typename D, typename R, typename LO, typename RO>
194 void Show(const evaluate::Operation<D, R, LO, RO> &op) {
195 Indent("binary op "s + std::string(TypeOf<D>::name));
196 Show(op.left());
197 Show(op.right());
198 Outdent();
199 }
201 template <typename T> void Show(const evaluate::Expr<T> &x) {
202 Indent("expr <" + std::string(TypeOf<T>::name) + ">");
203 Show(x.u);
204 Outdent();
205 }
206
207 const char *GetIndentString() const;
208 void Print(llvm::Twine s);
209 void Indent(llvm::StringRef s);
210 void Outdent();
211
212 llvm::raw_ostream &outs_;
213 unsigned level_{0};
214};
215
216LLVM_DUMP_METHOD void DumpEvExpr(const evaluate::Expr<evaluate::SomeType> &x);
217LLVM_DUMP_METHOD void DumpEvExpr(
219LLVM_DUMP_METHOD void DumpEvExpr(
221LLVM_DUMP_METHOD void DumpEvExpr(const evaluate::ArrayRef &x);
222LLVM_DUMP_METHOD void DumpEvExpr(const evaluate::DataRef &x);
223LLVM_DUMP_METHOD void DumpEvExpr(const evaluate::Substring &x);
224LLVM_DUMP_METHOD void DumpEvExpr(
226 &x);
227
228} // namespace Fortran::semantics
229
230#endif // FORTRAN_SEMANTICS_DUMPEXPR_H
Definition indirection.h:31
Definition variable.h:205
Definition variable.h:243
Definition variable.h:353
Definition variable.h:73
Definition constant.h:147
Definition variable.h:409
Definition variable.h:377
Definition common.h:214
Definition call.h:282
Definition expression.h:404
Definition variable.h:101
Definition expression.h:114
Definition call.h:232
Definition expression.h:656
Definition static-data.h:29
Definition expression.h:740
Definition variable.h:300
Definition variable.h:160
Definition variable.h:136
Definition type.h:57
Definition type.h:96
Definition symbol.h:781
Definition expression.h:432
Definition variable.h:50
Definition variable.h:284
Definition expression.h:396
Definition expression.h:827
Definition variable.h:191