Skip to content
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

Closed
wants to merge 1 commit into from

Conversation

kparzysz
Copy link
Contributor

@kparzysz kparzysz commented Feb 9, 2024

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.

@llvmbot llvmbot added flang Flang issues not falling into any other category flang:semantics labels Feb 9, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Feb 9, 2024

@llvm/pr-subscribers-flang-semantics

Author: Krzysztof Parzyszek (kparzysz)

Changes

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.


Full diff: https://github.com/llvm/llvm-project/pull/81265.diff

1 Files Affected:

  • (modified) flang/include/flang/Evaluate/tools.h (+23)
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) {

Copy link

github-actions bot commented Feb 9, 2024

✅ 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.
@clementval
Copy link
Contributor

Can you add you use case to this PR or push this when you have a need for it?

@kparzysz
Copy link
Contributor Author

kparzysz commented Feb 9, 2024

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...

@clementval
Copy link
Contributor

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.

@kparzysz
Copy link
Contributor Author

kparzysz commented Feb 9, 2024

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.

@kparzysz
Copy link
Contributor Author

Included in another PR: #81623

@kparzysz kparzysz closed this Feb 13, 2024
@kparzysz kparzysz deleted the users/kparzysz/a03a-tools branch February 13, 2024 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
flang:semantics flang Flang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants