-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[flang][Evaluate] Add ExtractSubstring
to evaluate/tools.h
#81265
Conversation
@llvm/pr-subscribers-flang-semantics Author: Krzysztof Parzyszek (kparzysz) ChangesThere 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. Full diff: https://github.com/llvm/llvm-project/pull/81265.diff 1 Files Affected:
diff --git a/flang/include/flang/Evaluate/tools.h b/flang/include/flang/Evaluate/tools.h
index d257da1a709642..e9999974944e88 100644
--- a/flang/include/flang/Evaluate/tools.h
+++ b/flang/include/flang/Evaluate/tools.h
@@ -430,6 +430,29 @@ template <typename A> std::optional<CoarrayRef> ExtractCoarrayRef(const A &x) {
}
}
+struct ExtractSubstringHelper {
+ template <typename T> static std::optional<Substring> visit(T &&) {
+ return std::nullopt;
+ }
+
+ static std::optional<Substring> visit(const Substring &e) { return e; }
+
+ template <typename T>
+ static std::optional<Substring> visit(const Designator<T> &e) {
+ return std::visit([](auto &&s) { return visit(s); }, e.u);
+ }
+
+ template <typename T>
+ static std::optional<Substring> visit(const Expr<T> &e) {
+ return std::visit([](auto &&s) { return visit(s); }, e.u);
+ }
+};
+
+template <typename A>
+std::optional<Substring> ExtractSubstring(const A &x) {
+ return ExtractSubstringHelper::visit(x);
+}
+
// If an expression is simply a whole symbol data designator,
// extract and return that symbol, else null.
template <typename A> const Symbol *UnwrapWholeSymbolDataRef(const A &x) {
|
✅ With the latest revision this PR passed the C/C++ code formatter. |
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.
351e276
to
7df5b7f
Compare
Can you add you use case to this PR or push this when you have a need for it? |
The use case is in an upcoming PR. I wanted to split things into smaller pieces and this looked general enough to exist on its own... |
It's better to put it together with its use case or use stacked PR. |
I'll be putting up a PR stack soon that uses this. I'll include this in in there. If this gets merged before then, it will have no impact, but it won't have its own separate commit anymore. |
Included in another PR: #81623 |
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.