Skip to content

Commit f48dd1c

Browse files
committed
chore: Avoid direct access to scope_ in the parser
Signed-off-by: Roberto Raggi <[email protected]>
1 parent 4da9adc commit f48dd1c

File tree

2 files changed

+53
-57
lines changed

2 files changed

+53
-57
lines changed

src/parser/cxx/parser.cc

Lines changed: 50 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -705,10 +705,10 @@ struct Parser::ScopeGuard {
705705

706706
explicit ScopeGuard(Parser* p, Scope* scope = nullptr)
707707
: p(p), savedScope(p->scope_) {
708-
if (scope) p->scope_ = scope;
708+
if (scope) p->setScope(scope);
709709
}
710710

711-
~ScopeGuard() { p->scope_ = savedScope; }
711+
~ScopeGuard() { p->setScope(savedScope); }
712712
};
713713

714714
struct Parser::ExprContext {
@@ -792,7 +792,7 @@ Parser::Parser(TranslationUnit* unit) : unit(unit) {
792792
overrideId_ = control_->getIdentifier("override");
793793

794794
globalScope_ = unit->globalScope();
795-
scope_ = globalScope_;
795+
setScope(globalScope_);
796796

797797
// temporary workarounds to the gnu until we have a proper
798798
// support for templates
@@ -1727,7 +1727,7 @@ auto Parser::parse_lambda_expression(ExpressionAST*& yyast) -> bool {
17271727

17281728
auto symbol = control_->newLambdaSymbol(scope_, currentLocation());
17291729

1730-
scope_ = symbol->scope();
1730+
setScope(symbol);
17311731

17321732
TemplateHeadContext templateHeadContext{this};
17331733

@@ -1786,9 +1786,9 @@ auto Parser::parse_lambda_expression(ExpressionAST*& yyast) -> bool {
17861786
auto lambdaScope = symbol->scope();
17871787
std::invoke(DeclareSymbol{this, lambdaScope},
17881788
params->functionParametersSymbol);
1789-
scope_ = params->functionParametersSymbol->scope();
1789+
setScope(params->functionParametersSymbol);
17901790
} else {
1791-
scope_ = symbol->scope();
1791+
setScope(symbol);
17921792
}
17931793

17941794
if (!lookat(TokenKind::T_LBRACE)) return false;
@@ -3964,7 +3964,7 @@ auto Parser::parse_compound_statement(CompoundStatementAST*& yyast, bool skip)
39643964

39653965
auto blockSymbol = control_->newBlockSymbol(scope_, lbraceLoc);
39663966
std::invoke(DeclareSymbol{this, scope_}, blockSymbol);
3967-
scope_ = blockSymbol->scope();
3967+
setScope(blockSymbol);
39683968

39693969
auto ast = make_node<CompoundStatementAST>(pool_);
39703970
yyast = ast;
@@ -4002,7 +4002,7 @@ auto Parser::parse_compound_statement(CompoundStatementAST*& yyast, bool skip)
40024002
void Parser::finish_compound_statement(CompoundStatementAST* ast) {
40034003
ScopeGuard scopeGuard{this};
40044004

4005-
scope_ = ast->symbol->scope();
4005+
setScope(ast->symbol);
40064006

40074007
bool skipping = false;
40084008

@@ -4048,7 +4048,7 @@ auto Parser::parse_if_statement(StatementAST*& yyast) -> bool {
40484048

40494049
auto blockSymbol = control_->newBlockSymbol(scope_, ifLoc);
40504050
std::invoke(DeclareSymbol{this, scope_}, blockSymbol);
4051-
scope_ = blockSymbol->scope();
4051+
setScope(blockSymbol);
40524052

40534053
if (LA().isOneOf(TokenKind::T_EXCLAIM, TokenKind::T_CONSTEVAL)) {
40544054
auto ast = make_node<ConstevalIfStatementAST>(pool_);
@@ -4131,7 +4131,7 @@ auto Parser::parse_while_statement(StatementAST*& yyast) -> bool {
41314131

41324132
auto blockSymbol = control_->newBlockSymbol(scope_, whileLoc);
41334133
std::invoke(DeclareSymbol{this, scope_}, blockSymbol);
4134-
scope_ = blockSymbol->scope();
4134+
setScope(blockSymbol);
41354135

41364136
auto ast = make_node<WhileStatementAST>(pool_);
41374137
yyast = ast;
@@ -4185,7 +4185,7 @@ auto Parser::parse_for_range_statement(StatementAST*& yyast) -> bool {
41854185
ScopeGuard scopeGuard{this};
41864186

41874187
auto blockSymbol = control_->newBlockSymbol(scope_, forLoc);
4188-
scope_ = blockSymbol->scope();
4188+
setScope(blockSymbol);
41894189

41904190
SourceLocation lparenLoc;
41914191
if (!match(TokenKind::T_LPAREN, lparenLoc)) return false;
@@ -4232,7 +4232,7 @@ auto Parser::parse_for_statement(StatementAST*& yyast) -> bool {
42324232

42334233
auto blockSymbol = control_->newBlockSymbol(scope_, forLoc);
42344234
std::invoke(DeclareSymbol{this, scope_}, blockSymbol);
4235-
scope_ = blockSymbol->scope();
4235+
setScope(blockSymbol);
42364236

42374237
auto ast = make_node<ForStatementAST>(pool_);
42384238
yyast = ast;
@@ -4562,8 +4562,7 @@ auto Parser::enterOrCreateNamespace(const Identifier* identifier,
45624562
if (!identifier) {
45634563
namespaceSymbol = parentNamespace->unnamedNamespace();
45644564
} else {
4565-
auto resolved = parentScope->find(identifier) |
4566-
std::views::filter(&Symbol::isNamespace);
4565+
auto resolved = parentScope->find(identifier) | views::namespaces;
45674566
if (std::ranges::distance(resolved) == 1) {
45684567
namespaceSymbol =
45694568
symbol_cast<NamespaceSymbol>(*std::ranges::begin(resolved));
@@ -4588,7 +4587,7 @@ auto Parser::enterOrCreateNamespace(const Identifier* identifier,
45884587
}
45894588
}
45904589

4591-
scope_ = namespaceSymbol->scope();
4590+
setScope(namespaceSymbol);
45924591

45934592
return namespaceSymbol;
45944593
}
@@ -4891,7 +4890,7 @@ auto Parser::parse_simple_declaration(
48914890
auto q = decl.getNestedNameSpecifier();
48924891

48934892
if (auto scope = decl.getScope()) {
4894-
scope_ = scope;
4893+
setScope(scope);
48954894
} else if (q && config_.checkTypes) {
48964895
parse_error(q->firstSourceLocation(),
48974896
std::format("unresolved class or namespace"));
@@ -4918,9 +4917,9 @@ auto Parser::parse_simple_declaration(
49184917
auto functionScope = functionSymbol->scope();
49194918
std::invoke(DeclareSymbol{this, functionScope},
49204919
params->functionParametersSymbol);
4921-
scope_ = params->functionParametersSymbol->scope();
4920+
setScope(params->functionParametersSymbol);
49224921
} else {
4923-
scope_ = functionSymbol->scope();
4922+
setScope(functionSymbol);
49244923
}
49254924

49264925
if (ctx == BindingContext::kTemplate) {
@@ -5005,7 +5004,7 @@ auto Parser::parse_notypespec_function_definition(
50055004
ScopeGuard scopeGuard{this};
50065005

50075006
if (auto scope = decl.getScope()) {
5008-
scope_ = scope;
5007+
setScope(scope);
50095008
} else if (auto q = decl.getNestedNameSpecifier()) {
50105009
if (config_.checkTypes) {
50115010
parse_error(q->firstSourceLocation(),
@@ -5091,9 +5090,9 @@ auto Parser::parse_notypespec_function_definition(
50915090
auto functionScope = functionSymbol->scope();
50925091
std::invoke(DeclareSymbol{this, functionScope},
50935092
params->functionParametersSymbol);
5094-
scope_ = params->functionParametersSymbol->scope();
5093+
setScope(params->functionParametersSymbol);
50955094
} else {
5096-
scope_ = functionSymbol->scope();
5095+
setScope(functionSymbol);
50975096
}
50985097

50995098
FunctionBodyAST* functionBody = nullptr;
@@ -6686,23 +6685,18 @@ auto Parser::parse_elaborated_type_specifier_helper(
66866685

66876686
auto symbol = Lookup{scope_}(nestedNameSpecifier, nameId->identifier);
66886687

6689-
auto class_symbols_view = std::views::filter(&Symbol::isClass);
6690-
6691-
auto enum_symbols_view = std::views::filter([](Symbol* symbol) {
6692-
return symbol->isEnum() || symbol->isScopedEnum();
6693-
});
6694-
66956688
if (ast->classKey == TokenKind::T_CLASS ||
66966689
ast->classKey == TokenKind::T_STRUCT ||
66976690
ast->classKey == TokenKind::T_UNION) {
6698-
for (auto symbol : SymbolChainView(symbol) | class_symbols_view) {
6691+
for (auto symbol : SymbolChainView(symbol) | views::classes) {
66996692
if (symbol->name() != nameId->identifier) continue;
67006693
ast->symbol = symbol;
67016694
specs.type = symbol->type();
67026695
break;
67036696
}
67046697
} else if (ast->classKey == TokenKind::T_ENUM) {
6705-
for (auto symbol : SymbolChainView(symbol) | enum_symbols_view) {
6698+
for (auto symbol :
6699+
SymbolChainView(symbol) | views::enum_or_scoped_enums) {
67066700
if (symbol->name() != nameId->identifier) continue;
67076701
ast->symbol = symbol;
67086702
specs.type = symbol->type();
@@ -6944,7 +6938,7 @@ auto Parser::parse_declarator(DeclaratorAST*& yyast, Decl& decl,
69446938
auto q = decl.getNestedNameSpecifier();
69456939

69466940
if (auto scope = decl.getScope()) {
6947-
scope_ = scope;
6941+
setScope(scope);
69486942
} else if (q && config_.checkTypes) {
69496943
parse_error(q->firstSourceLocation(),
69506944
std::format("unresolved class or namespace"));
@@ -7412,7 +7406,7 @@ auto Parser::parse_parameter_declaration_list(
74127406

74137407
functionParametersSymbol = control_->newFunctionParametersSymbol(scope_, {});
74147408

7415-
scope_ = functionParametersSymbol->scope();
7409+
setScope(functionParametersSymbol);
74167410

74177411
ParameterDeclarationAST* declaration = nullptr;
74187412

@@ -7849,7 +7843,7 @@ auto Parser::parse_enum_specifier(SpecifierAST*& yyast, DeclSpecs& specs)
78497843
enumSymbol->setUnderlyingType(underlyingType);
78507844
std::invoke(DeclareSymbol{this, scope_}, enumSymbol);
78517845

7852-
scope_ = enumSymbol->scope();
7846+
setScope(enumSymbol);
78537847
} else {
78547848
auto enumSymbol = control_->newEnumSymbol(scope_, location);
78557849
symbol = enumSymbol;
@@ -7858,7 +7852,7 @@ auto Parser::parse_enum_specifier(SpecifierAST*& yyast, DeclSpecs& specs)
78587852
enumSymbol->setUnderlyingType(underlyingType);
78597853
std::invoke(DeclareSymbol{this, scope_}, enumSymbol);
78607854

7861-
scope_ = enumSymbol->scope();
7855+
setScope(enumSymbol);
78627856
}
78637857

78647858
auto ast = make_node<EnumSpecifierAST>(pool_);
@@ -9281,13 +9275,11 @@ auto Parser::parse_class_head(ClassHead& classHead) -> bool {
92819275
enclosingScope = enclosingNamespace->scope();
92829276
}
92839277

9284-
if (!scope_) {
9285-
if (config_.checkTypes) {
9286-
parse_error(classHead.nestedNameSpecifier->firstSourceLocation(),
9287-
"unresolved nested name specifier");
9288-
}
9289-
} else {
9290-
scope_ = enclosingScope;
9278+
if (enclosingScope) {
9279+
setScope(enclosingScope);
9280+
} else if (config_.checkTypes) {
9281+
parse_error(classHead.nestedNameSpecifier->firstSourceLocation(),
9282+
"unresolved nested name specifier");
92919283
}
92929284
}
92939285
}
@@ -9309,17 +9301,14 @@ auto Parser::parse_class_head(ClassHead& classHead) -> bool {
93099301
ClassSymbol* classSymbol = nullptr;
93109302

93119303
if (id && !isTemplateSpecialization) {
9312-
for (auto previous :
9313-
scope_->find(id) | std::views::filter(&Symbol::isClass)) {
9314-
if (auto previousClass = symbol_cast<ClassSymbol>(previous)) {
9315-
if (previousClass->isComplete()) {
9316-
parse_error(classHead.name->firstSourceLocation(),
9317-
"class name already declared");
9318-
} else {
9319-
classSymbol = previousClass;
9320-
}
9321-
break;
9304+
for (auto previousClass : scope_->find(id) | views::classes) {
9305+
if (previousClass->isComplete()) {
9306+
parse_error(classHead.name->firstSourceLocation(),
9307+
"class name already declared");
9308+
} else {
9309+
classSymbol = previousClass;
93229310
}
9311+
break;
93239312
}
93249313
}
93259314

@@ -9337,7 +9326,7 @@ auto Parser::parse_class_head(ClassHead& classHead) -> bool {
93379326
classSymbol->setFinal(true);
93389327
}
93399328

9340-
scope_ = classSymbol->scope();
9329+
setScope(classSymbol);
93419330

93429331
(void)parse_base_clause(classSymbol, classHead.colonLoc,
93439332
classHead.baseSpecifierList);
@@ -9502,9 +9491,9 @@ auto Parser::parse_member_declaration_helper(DeclarationAST*& yyast) -> bool {
95029491
auto functionScope = functionSymbol->scope();
95039492
std::invoke(DeclareSymbol{this, functionScope},
95049493
params->functionParametersSymbol);
9505-
scope_ = params->functionParametersSymbol->scope();
9494+
setScope(params->functionParametersSymbol);
95069495
} else {
9507-
scope_ = functionSymbol->scope();
9496+
setScope(functionSymbol);
95089497
}
95099498

95109499
FunctionBodyAST* functionBody = nullptr;
@@ -10215,7 +10204,7 @@ auto Parser::parse_template_declaration(
1021510204

1021610205
std::invoke(DeclareSymbol{this, scope_}, templateParametersSymbol);
1021710206

10218-
scope_ = ast->symbol->scope();
10207+
setScope(ast->symbol);
1021910208

1022010209
templateDeclarations.push_back(ast);
1022110210

@@ -10482,13 +10471,13 @@ void Parser::parse_template_type_parameter(TemplateParameterAST*& yyast) {
1048210471
auto parameters =
1048310472
control_->newTemplateParametersSymbol(scope_, ast->templateLoc);
1048410473

10485-
scope_ = parameters->scope();
10474+
setScope(parameters);
1048610475

1048710476
parse_template_parameter_list(ast->templateParameterList);
1048810477

1048910478
expect(TokenKind::T_GREATER, ast->greaterLoc);
1049010479

10491-
scope_ = parameters->enclosingScope();
10480+
setScope(parameters->enclosingScope());
1049210481
}
1049310482

1049410483
(void)parse_requires_clause(ast->requiresClause);
@@ -11250,6 +11239,10 @@ void Parser::completePendingFunctionDefinitions() {
1125011239
}
1125111240
}
1125211241

11242+
void Parser::setScope(Scope* scope) { scope_ = scope; }
11243+
11244+
void Parser::setScope(ScopedSymbol* symbol) { setScope(symbol->scope()); }
11245+
1125311246
void Parser::completeFunctionDefinition(FunctionDefinitionAST* ast) {
1125411247
if (!ast->functionBody) return;
1125511248

@@ -11260,7 +11253,7 @@ void Parser::completeFunctionDefinition(FunctionDefinitionAST* ast) {
1126011253

1126111254
ScopeGuard scopeGuard{this};
1126211255

11263-
scope_ = ast->symbol->scope();
11256+
setScope(ast->symbol);
1126411257

1126511258
const auto saved = currentLocation();
1126611259

src/parser/cxx/parser.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -775,6 +775,9 @@ class Parser final {
775775
void completePendingFunctionDefinitions();
776776
void completeFunctionDefinition(FunctionDefinitionAST* ast);
777777

778+
void setScope(Scope* scope);
779+
void setScope(ScopedSymbol* symbol);
780+
778781
// lookup
779782
[[nodiscard]] auto convertName(UnqualifiedIdAST* id) -> const Name*;
780783

0 commit comments

Comments
 (0)