[flang-commits] [flang] [flang][Evaluate] Add `AsGenericExpr` that takes std::optional (PR #81264)

Krzysztof Parzyszek via flang-commits flang-commits at lists.llvm.org
Fri Feb 9 08:33:36 PST 2024


https://github.com/kparzysz updated https://github.com/llvm/llvm-project/pull/81264

>From 936fac7adf03d7a1d30ed37577986a6309b42575 Mon Sep 17 00:00:00 2001
From: Krzysztof Parzyszek <Krzysztof.Parzyszek at amd.com>
Date: Thu, 8 Feb 2024 08:32:41 -0600
Subject: [PATCH] [flang][Evaluate] Add `AsGenericExpr` that takes
 std::optional

`AsGenericExpr` already returns optional Expr. Making it accept an
optional Expr as input would reduce the number of necessary checks
when handling frequent optional values in evaluator.
---
 flang/include/flang/Evaluate/tools.h | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/flang/include/flang/Evaluate/tools.h b/flang/include/flang/Evaluate/tools.h
index d257da1a709642..f7f155a5da9e13 100644
--- a/flang/include/flang/Evaluate/tools.h
+++ b/flang/include/flang/Evaluate/tools.h
@@ -148,6 +148,14 @@ inline Expr<SomeType> AsGenericExpr(Expr<SomeType> &&x) { return std::move(x); }
 std::optional<Expr<SomeType>> AsGenericExpr(DataRef &&);
 std::optional<Expr<SomeType>> AsGenericExpr(const Symbol &);
 
+// Propagate std::optional from input to output.
+template <typename A>
+std::optional<Expr<SomeType>> AsGenericExpr(std::optional<A> &&x) {
+  if (!x)
+    return std::nullopt;
+  return AsGenericExpr(std::move(*x));
+}
+
 template <typename A>
 common::IfNoLvalue<Expr<SomeKind<ResultType<A>::category>>, A> AsCategoryExpr(
     A &&x) {



More information about the flang-commits mailing list