Skip to content

Commit

Permalink
[clang][bytecode] Restructure Program::CurrentDeclaration handling (#…
Browse files Browse the repository at this point in the history
…127456)

Properly reset to the last ID and return the current ID from
getCurrentDecl().
  • Loading branch information
tbaederr authored Feb 17, 2025
1 parent b302829 commit f09fd94
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
2 changes: 1 addition & 1 deletion clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace interp {
template <class Emitter> class DeclScope final : public LocalScope<Emitter> {
public:
DeclScope(Compiler<Emitter> *Ctx, const ValueDecl *VD)
: LocalScope<Emitter>(Ctx, VD), Scope(Ctx->P, VD),
: LocalScope<Emitter>(Ctx, VD), Scope(Ctx->P),
OldInitializingDecl(Ctx->InitializingDecl) {
Ctx->InitializingDecl = VD;
Ctx->InitStack.push_back(InitLink::Decl(VD));
Expand Down
23 changes: 8 additions & 15 deletions clang/lib/AST/ByteCode/Program.h
Original file line number Diff line number Diff line change
Expand Up @@ -132,20 +132,22 @@ class Program final {
/// Context to manage declaration lifetimes.
class DeclScope {
public:
DeclScope(Program &P, const ValueDecl *VD) : P(P) {
P.startDeclaration(VD);
DeclScope(Program &P) : P(P), PrevDecl(P.CurrentDeclaration) {
++P.LastDeclaration;
P.CurrentDeclaration = P.LastDeclaration;
}
~DeclScope() { P.endDeclaration(); }
~DeclScope() { P.CurrentDeclaration = PrevDecl; }

private:
Program &P;
unsigned PrevDecl;
};

/// Returns the current declaration ID.
std::optional<unsigned> getCurrentDecl() const {
if (CurrentDeclaration == NoDeclaration)
return std::optional<unsigned>{};
return LastDeclaration;
return std::nullopt;
return CurrentDeclaration;
}

private:
Expand Down Expand Up @@ -218,21 +220,12 @@ class Program final {
}

/// No declaration ID.
static constexpr unsigned NoDeclaration = (unsigned)-1;
static constexpr unsigned NoDeclaration = ~0u;
/// Last declaration ID.
unsigned LastDeclaration = 0;
/// Current declaration ID.
unsigned CurrentDeclaration = NoDeclaration;

/// Starts evaluating a declaration.
void startDeclaration(const ValueDecl *Decl) {
LastDeclaration += 1;
CurrentDeclaration = LastDeclaration;
}

/// Ends a global declaration.
void endDeclaration() { CurrentDeclaration = NoDeclaration; }

public:
/// Dumps the disassembled bytecode to \c llvm::errs().
void dump() const;
Expand Down
22 changes: 22 additions & 0 deletions clang/test/AST/ByteCode/libcxx/global-decl-id.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %clang_cc1 -std=c++2c -fexperimental-new-constant-interpreter -verify=expected,both %s
// RUN: %clang_cc1 -std=c++2c -verify=ref,both %s

// both-no-diagnostics

namespace std {
constexpr int
midpoint(int __a, int ) {
constexpr unsigned __half_diff = 0;
return __half_diff;
}
}
struct Tuple {
int min;
int mid;
constexpr Tuple() {
min = 0;
mid = std::midpoint(min, min);
}
};
constexpr Tuple tup;

0 comments on commit f09fd94

Please sign in to comment.