diff --git a/src/parser/cxx/binder.cc b/src/parser/cxx/binder.cc index 070f8d11..4e1575b7 100644 --- a/src/parser/cxx/binder.cc +++ b/src/parser/cxx/binder.cc @@ -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) { @@ -344,13 +352,15 @@ void Binder::bind(DecltypeSpecifierAST* ast) { void Binder::bind(EnumeratorAST* ast, const Type* type, std::optional 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(scope()->owner())) { auto enumeratorSymbol = @@ -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; + } } } diff --git a/src/parser/cxx/binder.h b/src/parser/cxx/binder.h index b55813c4..6723bca8 100644 --- a/src/parser/cxx/binder.h +++ b/src/parser/cxx/binder.h @@ -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; diff --git a/tests/unit_tests/sema/enum_c_01.c b/tests/unit_tests/sema/enum_c_01.c new file mode 100644 index 00000000..cb999ae1 --- /dev/null +++ b/tests/unit_tests/sema/enum_c_01.c @@ -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 \ No newline at end of file