Skip to content

Commit f09fd94

Browse files
authored
[clang][bytecode] Restructure Program::CurrentDeclaration handling (#127456)
Properly reset to the last ID and return the current ID from getCurrentDecl().
1 parent b302829 commit f09fd94

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

clang/lib/AST/ByteCode/Compiler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ namespace interp {
2929
template <class Emitter> class DeclScope final : public LocalScope<Emitter> {
3030
public:
3131
DeclScope(Compiler<Emitter> *Ctx, const ValueDecl *VD)
32-
: LocalScope<Emitter>(Ctx, VD), Scope(Ctx->P, VD),
32+
: LocalScope<Emitter>(Ctx, VD), Scope(Ctx->P),
3333
OldInitializingDecl(Ctx->InitializingDecl) {
3434
Ctx->InitializingDecl = VD;
3535
Ctx->InitStack.push_back(InitLink::Decl(VD));

clang/lib/AST/ByteCode/Program.h

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -132,20 +132,22 @@ class Program final {
132132
/// Context to manage declaration lifetimes.
133133
class DeclScope {
134134
public:
135-
DeclScope(Program &P, const ValueDecl *VD) : P(P) {
136-
P.startDeclaration(VD);
135+
DeclScope(Program &P) : P(P), PrevDecl(P.CurrentDeclaration) {
136+
++P.LastDeclaration;
137+
P.CurrentDeclaration = P.LastDeclaration;
137138
}
138-
~DeclScope() { P.endDeclaration(); }
139+
~DeclScope() { P.CurrentDeclaration = PrevDecl; }
139140

140141
private:
141142
Program &P;
143+
unsigned PrevDecl;
142144
};
143145

144146
/// Returns the current declaration ID.
145147
std::optional<unsigned> getCurrentDecl() const {
146148
if (CurrentDeclaration == NoDeclaration)
147-
return std::optional<unsigned>{};
148-
return LastDeclaration;
149+
return std::nullopt;
150+
return CurrentDeclaration;
149151
}
150152

151153
private:
@@ -218,21 +220,12 @@ class Program final {
218220
}
219221

220222
/// No declaration ID.
221-
static constexpr unsigned NoDeclaration = (unsigned)-1;
223+
static constexpr unsigned NoDeclaration = ~0u;
222224
/// Last declaration ID.
223225
unsigned LastDeclaration = 0;
224226
/// Current declaration ID.
225227
unsigned CurrentDeclaration = NoDeclaration;
226228

227-
/// Starts evaluating a declaration.
228-
void startDeclaration(const ValueDecl *Decl) {
229-
LastDeclaration += 1;
230-
CurrentDeclaration = LastDeclaration;
231-
}
232-
233-
/// Ends a global declaration.
234-
void endDeclaration() { CurrentDeclaration = NoDeclaration; }
235-
236229
public:
237230
/// Dumps the disassembled bytecode to \c llvm::errs().
238231
void dump() const;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %clang_cc1 -std=c++2c -fexperimental-new-constant-interpreter -verify=expected,both %s
2+
// RUN: %clang_cc1 -std=c++2c -verify=ref,both %s
3+
4+
// both-no-diagnostics
5+
6+
namespace std {
7+
constexpr int
8+
midpoint(int __a, int ) {
9+
constexpr unsigned __half_diff = 0;
10+
return __half_diff;
11+
}
12+
}
13+
struct Tuple {
14+
int min;
15+
int mid;
16+
constexpr Tuple() {
17+
min = 0;
18+
mid = std::midpoint(min, min);
19+
}
20+
};
21+
constexpr Tuple tup;
22+

0 commit comments

Comments
 (0)