Skip to content

Commit deb039e

Browse files
committed
[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.
1 parent d957da8 commit deb039e

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clang/lib/AST/Interp/Program.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,12 @@ std::optional<unsigned> Program::getGlobal(const ValueDecl *VD) {
126126
return std::nullopt;
127127
}
128128

129+
std::optional<unsigned> Program::getGlobal(const Expr *E) {
130+
if (auto It = GlobalIndices.find(E); It != GlobalIndices.end())
131+
return It->second;
132+
return std::nullopt;
133+
}
134+
129135
std::optional<unsigned> Program::getOrCreateGlobal(const ValueDecl *VD,
130136
const Expr *Init) {
131137
if (auto Idx = getGlobal(VD))
@@ -195,7 +201,14 @@ std::optional<unsigned> Program::createGlobal(const ValueDecl *VD,
195201
}
196202

197203
std::optional<unsigned> Program::createGlobal(const Expr *E) {
198-
return createGlobal(E, E->getType(), /*isStatic=*/true, /*isExtern=*/false);
204+
if (auto Idx = getGlobal(E))
205+
return Idx;
206+
if (auto Idx = createGlobal(E, E->getType(), /*isStatic=*/true,
207+
/*isExtern=*/false)) {
208+
GlobalIndices[E] = *Idx;
209+
return *Idx;
210+
}
211+
return std::nullopt;
199212
}
200213

201214
std::optional<unsigned> Program::createGlobal(const DeclTy &D, QualType Ty,

clang/lib/AST/Interp/Program.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ class Program final {
7777

7878
/// Finds a global's index.
7979
std::optional<unsigned> getGlobal(const ValueDecl *VD);
80+
std::optional<unsigned> getGlobal(const Expr *E);
8081

8182
/// Returns or creates a global an creates an index to it.
8283
std::optional<unsigned> getOrCreateGlobal(const ValueDecl *VD,

0 commit comments

Comments
 (0)