Skip to content

Commit bdb8f5e

Browse files
committed
Bind struct and enum specifiers in the global scope when in C mode
1 parent 455d92f commit bdb8f5e

File tree

1 file changed

+30
-1
lines changed

1 file changed

+30
-1
lines changed

src/parser/cxx/parser.cc

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ Parser::Parser(TranslationUnit* unit) : unit(unit), binder_(unit) {
170170
control_ = unit->control();
171171
diagnosticClient_ = unit->diagnosticsClient();
172172
cursor_ = 1;
173+
lang_ = unit->language();
173174

174175
pool_ = unit->arena();
175176

@@ -5097,6 +5098,11 @@ auto Parser::parse_elaborated_enum_specifier(SpecifierAST*& yyast,
50975098
SourceLocation enumLoc;
50985099
if (!match(TokenKind::T_ENUM, enumLoc)) return false;
50995100

5101+
auto globalScopeGuard = Binder::ScopeGuard{&binder_};
5102+
if (is_parsing_c()) {
5103+
setScope(globalScope_);
5104+
}
5105+
51005106
NestedNameSpecifierAST* nestedNameSpecifier = nullptr;
51015107
parse_optional_nested_name_specifier(
51025108
nestedNameSpecifier, NestedNameSpecifierContext::kDeclarative);
@@ -5106,13 +5112,21 @@ auto Parser::parse_elaborated_enum_specifier(SpecifierAST*& yyast,
51065112
parse_error("expected a name");
51075113
}
51085114

5115+
Symbol* symbol = nullptr;
5116+
5117+
if (name->identifier) {
5118+
symbol = Lookup(scope()).lookup(nestedNameSpecifier, name->identifier,
5119+
&Symbol::isEnum);
5120+
}
5121+
51095122
auto ast = make_node<ElaboratedTypeSpecifierAST>(pool_);
51105123
yyast = ast;
51115124

51125125
ast->classLoc = enumLoc;
51135126
ast->nestedNameSpecifier = nestedNameSpecifier;
51145127
ast->unqualifiedId = name;
51155128
ast->classKey = TokenKind::T_ENUM;
5129+
ast->symbol = symbol;
51165130

51175131
specs.setTypeSpecifier(ast);
51185132

@@ -5128,6 +5142,11 @@ auto Parser::parse_elaborated_type_specifier(SpecifierAST*& yyast,
51285142
SourceLocation classLoc;
51295143
if (!parse_class_key(classLoc)) return false;
51305144

5145+
auto globalScopeGuard = Binder::ScopeGuard{&binder_};
5146+
if (is_parsing_c()) {
5147+
setScope(globalScope_);
5148+
}
5149+
51315150
List<AttributeSpecifierAST*>* attributes = nullptr;
51325151
parse_optional_attribute_specifier_seq(attributes);
51335152

@@ -6223,6 +6242,11 @@ auto Parser::parse_enum_specifier(SpecifierAST*& yyast, DeclSpecs& specs)
62236242

62246243
if (!parse_enum_key(enumLoc, classLoc)) return false;
62256244

6245+
auto globalScopeGuard = Binder::ScopeGuard{&binder_};
6246+
if (is_parsing_c()) {
6247+
setScope(globalScope_);
6248+
}
6249+
62266250
List<AttributeSpecifierAST*>* attributes = nullptr;
62276251

62286252
parse_optional_attribute_specifier_seq(attributes);
@@ -7510,6 +7534,11 @@ auto Parser::parse_class_specifier(ClassSpecifierAST*& yyast, DeclSpecs& specs)
75107534
SourceLocation classLoc;
75117535
if (!parse_class_key(classLoc)) return false;
75127536

7537+
auto globalScopeGuard = Binder::ScopeGuard{&binder_};
7538+
if (is_parsing_c()) {
7539+
setScope(globalScope_);
7540+
}
7541+
75137542
List<AttributeSpecifierAST*>* attributeList = nullptr;
75147543
NestedNameSpecifierAST* nestedNameSpecifier = nullptr;
75157544
UnqualifiedIdAST* unqualifiedId = nullptr;
@@ -7872,7 +7901,7 @@ auto Parser::parse_bitfield_declarator(InitDeclaratorAST*& yyast,
78727901

78737902
Decl decl{declSpecs, declarator};
78747903

7875-
// ### set the bitfield offse
7904+
// ### set the bitfield offset
78767905

78777906
auto symbol = binder_.declareField(declarator, decl);
78787907
symbol->setBitFieldWidth(std::move(bitfieldWidth));

0 commit comments

Comments
 (0)