Skip to content

Commit 7df5b7f

Browse files
committed
[flang][Evaluate] Add ExtractSubstring to evaluate/tools.h
There are utility functions to extract a DataRef, optionally traversing into a substring, but there is no function to get the subscript itself. This patch adds one.
1 parent fdb16e6 commit 7df5b7f

File tree

1 file changed

+22
-0
lines changed
  • flang/include/flang/Evaluate

1 file changed

+22
-0
lines changed

flang/include/flang/Evaluate/tools.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,28 @@ template <typename A> std::optional<CoarrayRef> ExtractCoarrayRef(const A &x) {
430430
}
431431
}
432432

433+
struct ExtractSubstringHelper {
434+
template <typename T> static std::optional<Substring> visit(T &&) {
435+
return std::nullopt;
436+
}
437+
438+
static std::optional<Substring> visit(const Substring &e) { return e; }
439+
440+
template <typename T>
441+
static std::optional<Substring> visit(const Designator<T> &e) {
442+
return std::visit([](auto &&s) { return visit(s); }, e.u);
443+
}
444+
445+
template <typename T>
446+
static std::optional<Substring> visit(const Expr<T> &e) {
447+
return std::visit([](auto &&s) { return visit(s); }, e.u);
448+
}
449+
};
450+
451+
template <typename A> std::optional<Substring> ExtractSubstring(const A &x) {
452+
return ExtractSubstringHelper::visit(x);
453+
}
454+
433455
// If an expression is simply a whole symbol data designator,
434456
// extract and return that symbol, else null.
435457
template <typename A> const Symbol *UnwrapWholeSymbolDataRef(const A &x) {

0 commit comments

Comments
 (0)