Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 20 additions & 6 deletions src/parser/cxx/binder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ auto Binder::control() const -> Control* {
return unit_ ? unit_->control() : nullptr;
}

auto Binder::is_parsing_c() const {
return unit_->language() == LanguageKind::kC;
}

auto Binder::is_parsing_cxx() const {
return unit_->language() == LanguageKind::kCXX;
}

auto Binder::reportErrors() const -> bool { return reportErrors_; }

void Binder::setReportErrors(bool reportErrors) {
Expand Down Expand Up @@ -344,13 +352,15 @@ void Binder::bind(DecltypeSpecifierAST* ast) {

void Binder::bind(EnumeratorAST* ast, const Type* type,
std::optional<ConstValue> value) {
auto symbol = control()->newEnumeratorSymbol(scope(), ast->identifierLoc);
ast->symbol = symbol;
if (is_parsing_cxx()) {
auto symbol = control()->newEnumeratorSymbol(scope(), ast->identifierLoc);
ast->symbol = symbol;

symbol->setName(ast->identifier);
symbol->setType(type);
ast->symbol->setValue(value);
scope()->addSymbol(symbol);
symbol->setName(ast->identifier);
symbol->setType(type);
ast->symbol->setValue(value);
scope()->addSymbol(symbol);
}

if (auto enumSymbol = symbol_cast<EnumSymbol>(scope()->owner())) {
auto enumeratorSymbol =
Expand All @@ -361,6 +371,10 @@ void Binder::bind(EnumeratorAST* ast, const Type* type,

auto parentScope = enumSymbol->enclosingScope();
parentScope->addSymbol(enumeratorSymbol);

if (!is_parsing_cxx()) {
ast->symbol = enumeratorSymbol;
}
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/parser/cxx/binder.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ class Binder {
~ScopeGuard() { p->setScope(savedScope); }
};

[[nodiscard]] auto is_parsing_c() const;
[[nodiscard]] auto is_parsing_cxx() const;

private:
TranslationUnit* unit_ = nullptr;
Scope* scope_ = nullptr;
Expand Down
16 changes: 16 additions & 0 deletions tests/unit_tests/sema/enum_c_01.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// RUN: %cxx -verify -fcheck -dump-symbols -xc %s | %filecheck %s

enum X {
A,
B,
C,
};

enum X x;

// CHECK:namespace
// CHECK-NEXT:enum X : int
// CHECK-NEXT:enumerator X A
// CHECK-NEXT:enumerator X B
// CHECK-NEXT:enumerator X C
// CHECK-NEXT:variable X x