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
23 changes: 15 additions & 8 deletions src/parser/cxx/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5206,11 +5206,6 @@ auto Parser::parse_elaborated_enum_specifier(SpecifierAST*& yyast,
SourceLocation enumLoc;
if (!match(TokenKind::T_ENUM, enumLoc)) return false;

auto globalScopeGuard = Binder::ScopeGuard{&binder_};
if (is_parsing_c()) {
setScope(globalScope_);
}

NestedNameSpecifierAST* nestedNameSpecifier = nullptr;
parse_optional_nested_name_specifier(
nestedNameSpecifier, NestedNameSpecifierContext::kDeclarative);
Expand Down Expand Up @@ -5251,8 +5246,9 @@ auto Parser::parse_elaborated_type_specifier(SpecifierAST*& yyast,
if (!parse_class_key(classLoc)) return false;

auto globalScopeGuard = Binder::ScopeGuard{&binder_};

if (is_parsing_c()) {
setScope(globalScope_);
setScope(getCurrentNonClassScope());
}

List<AttributeSpecifierAST*>* attributes = nullptr;
Expand Down Expand Up @@ -6408,8 +6404,9 @@ auto Parser::parse_enum_specifier(SpecifierAST*& yyast, DeclSpecs& specs)
if (!parse_enum_key(enumLoc, classLoc)) return false;

auto globalScopeGuard = Binder::ScopeGuard{&binder_};

if (is_parsing_c()) {
setScope(globalScope_);
setScope(getCurrentNonClassScope());
}

List<AttributeSpecifierAST*>* attributes = nullptr;
Expand Down Expand Up @@ -7700,8 +7697,9 @@ auto Parser::parse_class_specifier(ClassSpecifierAST*& yyast, DeclSpecs& specs)
if (!parse_class_key(classLoc)) return false;

auto globalScopeGuard = Binder::ScopeGuard{&binder_};

if (is_parsing_c()) {
setScope(globalScope_);
setScope(getCurrentNonClassScope());
}

List<AttributeSpecifierAST*>* attributeList = nullptr;
Expand Down Expand Up @@ -9634,6 +9632,15 @@ void Parser::completePendingFunctionDefinitions() {
}
}

auto Parser::getCurrentNonClassScope() const -> Scope* {
for (auto current = scope(); current; current = current->parent()) {
if (current->isClassOrNamespaceScope()) continue;
return current;
}

return globalScope_;
}

auto Parser::scope() const -> Scope* { return binder_.scope(); }

void Parser::setScope(Scope* scope) { binder_.setScope(scope); }
Expand Down
2 changes: 2 additions & 0 deletions src/parser/cxx/parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,8 @@ class Parser final {
void completePendingFunctionDefinitions();
void completeFunctionDefinition(FunctionDefinitionAST* ast);

[[nodiscard]] auto getCurrentNonClassScope() const -> Scope*;

[[nodiscard]] auto scope() const -> Scope*;
void setScope(Scope* scope);
void setScope(ScopedSymbol* symbol);
Expand Down
4 changes: 4 additions & 0 deletions src/parser/cxx/scope.cc
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ auto Scope::isTemplateParametersScope() const -> bool {
return owner_ && owner_->isTemplateParameters();
}

auto Scope::isFunctionParametersScope() const -> bool {
return owner_ && owner_->isFunctionParameters();
}

auto Scope::enclosingNamespaceScope() const -> Scope* {
for (auto scope = parent_; scope; scope = scope->parent()) {
if (scope->isNamespaceScope()) {
Expand Down
1 change: 1 addition & 0 deletions src/parser/cxx/scope.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class Scope {
[[nodiscard]] auto isBlockScope() const -> bool;
[[nodiscard]] auto isEnumScope() const -> bool;
[[nodiscard]] auto isTemplateParametersScope() const -> bool;
[[nodiscard]] auto isFunctionParametersScope() const -> bool;

[[nodiscard]] auto enclosingNamespaceScope() const -> Scope*;
[[nodiscard]] auto enclosingNonTemplateParametersScope() const -> Scope*;
Expand Down
5 changes: 4 additions & 1 deletion src/parser/cxx/symbol_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,10 @@ struct DumpSymbols {

void operator()(NamespaceSymbol* symbol) {
indent();
out << std::format("namespace {}\n", to_string(symbol->name()));
out << "namespace";
if (symbol->name())
out << std::format(" {}", to_string(symbol->name()));
out << "\n";
dumpScope(symbol->scope());
}

Expand Down
41 changes: 41 additions & 0 deletions tests/unit_tests/sema/struct_c_01.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// RUN: %cxx -fcheck -dump-symbols -xc %s | %filecheck %s --match-full-lines

enum E {
v,
};

struct A {
enum E e;
int x;
};

int main() {
enum K {
w,
};

struct B {
enum K k;
enum E e;
int y;
};

struct M;
}

// clang-format off
// CHECK:namespace
// CHECK-NEXT: enum E : int
// CHECK-NEXT: enumerator E v
// CHECK-NEXT: class A
// CHECK-NEXT: field E e
// CHECK-NEXT: field int x
// CHECK-NEXT: function int main()
// CHECK-NEXT: block
// CHECK-NEXT: enum K : int
// CHECK-NEXT: enumerator K w
// CHECK-NEXT: class B
// CHECK-NEXT: field K k
// CHECK-NEXT: field E e
// CHECK-NEXT: field int y
// CHECK-NEXT: class M