[clang] deb039e - [clang][Interp][NFC] Provide Program accessor for global temporaries

Timm Bäder via cfe-commits cfe-commits at lists.llvm.org
Sat Jun 29 10:57:39 PDT 2024


Author: Timm Bäder
Date: 2024-06-29T19:55:30+02:00
New Revision: deb039e69ed7efc94ef517809f36298f40359c21

URL: https://github.com/llvm/llvm-project/commit/deb039e69ed7efc94ef517809f36298f40359c21
DIFF: https://github.com/llvm/llvm-project/commit/deb039e69ed7efc94ef517809f36298f40359c21.diff

LOG: [clang][Interp][NFC] Provide Program accessor for global temporaries

Just like the one we have taking a ValueDecl*, provide a getGlobal()
version taking an expression.

Added: 
    

Modified: 
    clang/lib/AST/Interp/Program.cpp
    clang/lib/AST/Interp/Program.h

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/Interp/Program.cpp b/clang/lib/AST/Interp/Program.cpp
index caff9d01cdfee..2a1ad4d4eb850 100644
--- a/clang/lib/AST/Interp/Program.cpp
+++ b/clang/lib/AST/Interp/Program.cpp
@@ -126,6 +126,12 @@ std::optional<unsigned> Program::getGlobal(const ValueDecl *VD) {
   return std::nullopt;
 }
 
+std::optional<unsigned> Program::getGlobal(const Expr *E) {
+  if (auto It = GlobalIndices.find(E); It != GlobalIndices.end())
+    return It->second;
+  return std::nullopt;
+}
+
 std::optional<unsigned> Program::getOrCreateGlobal(const ValueDecl *VD,
                                                    const Expr *Init) {
   if (auto Idx = getGlobal(VD))
@@ -195,7 +201,14 @@ std::optional<unsigned> Program::createGlobal(const ValueDecl *VD,
 }
 
 std::optional<unsigned> Program::createGlobal(const Expr *E) {
-  return createGlobal(E, E->getType(), /*isStatic=*/true, /*isExtern=*/false);
+  if (auto Idx = getGlobal(E))
+    return Idx;
+  if (auto Idx = createGlobal(E, E->getType(), /*isStatic=*/true,
+                              /*isExtern=*/false)) {
+    GlobalIndices[E] = *Idx;
+    return *Idx;
+  }
+  return std::nullopt;
 }
 
 std::optional<unsigned> Program::createGlobal(const DeclTy &D, QualType Ty,

diff  --git a/clang/lib/AST/Interp/Program.h b/clang/lib/AST/Interp/Program.h
index ec7c0744b8856..1cabc5212180f 100644
--- a/clang/lib/AST/Interp/Program.h
+++ b/clang/lib/AST/Interp/Program.h
@@ -77,6 +77,7 @@ class Program final {
 
   /// Finds a global's index.
   std::optional<unsigned> getGlobal(const ValueDecl *VD);
+  std::optional<unsigned> getGlobal(const Expr *E);
 
   /// Returns or creates a global an creates an index to it.
   std::optional<unsigned> getOrCreateGlobal(const ValueDecl *VD,


        


More information about the cfe-commits mailing list