Skip to content

Commit 8d9f986

Browse files
committed
Do not allow function definitions in class scope in C mode
Fixes #570 Signed-off-by: Roberto Raggi <[email protected]>
1 parent 84d7373 commit 8d9f986

File tree

1 file changed

+51
-34
lines changed

1 file changed

+51
-34
lines changed

src/parser/cxx/parser.cc

Lines changed: 51 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -850,33 +850,38 @@ auto Parser::parse_unqualified_id(UnqualifiedIdAST*& yyast,
850850
NestedNameSpecifierAST* nestedNameSpecifier,
851851
bool isTemplateIntroduced,
852852
bool inRequiresClause) -> bool {
853-
if (SourceLocation tildeLoc; match(TokenKind::T_TILDE, tildeLoc)) {
854-
if (DecltypeSpecifierAST* decltypeSpecifier = nullptr;
855-
parse_decltype_specifier(decltypeSpecifier)) {
856-
auto decltypeName = make_node<DecltypeIdAST>(pool_);
857-
decltypeName->decltypeSpecifier = decltypeSpecifier;
853+
if (is_parsing_cxx()) {
854+
// allow destructor only in C++ mode
855+
if (SourceLocation tildeLoc; match(TokenKind::T_TILDE, tildeLoc)) {
856+
if (DecltypeSpecifierAST* decltypeSpecifier = nullptr;
857+
parse_decltype_specifier(decltypeSpecifier)) {
858+
auto decltypeName = make_node<DecltypeIdAST>(pool_);
859+
decltypeName->decltypeSpecifier = decltypeSpecifier;
860+
861+
auto ast = make_node<DestructorIdAST>(pool_);
862+
yyast = ast;
863+
ast->tildeLoc = tildeLoc;
864+
ast->id = decltypeName;
865+
866+
return true;
867+
}
868+
869+
UnqualifiedIdAST* name = nullptr;
870+
if (!parse_type_name(name, nestedNameSpecifier, isTemplateIntroduced))
871+
return false;
858872

859873
auto ast = make_node<DestructorIdAST>(pool_);
860874
yyast = ast;
861875
ast->tildeLoc = tildeLoc;
862-
ast->id = decltypeName;
876+
ast->id = name;
863877

864878
return true;
865879
}
866-
867-
UnqualifiedIdAST* name = nullptr;
868-
if (!parse_type_name(name, nestedNameSpecifier, isTemplateIntroduced))
869-
return false;
870-
871-
auto ast = make_node<DestructorIdAST>(pool_);
872-
yyast = ast;
873-
ast->tildeLoc = tildeLoc;
874-
ast->id = name;
875-
876-
return true;
877880
}
878881

879882
auto lookat_template_id = [&] {
883+
if (!is_parsing_cxx()) return false;
884+
880885
LookaheadParser lookahead{this};
881886
if (!parse_template_id(yyast, nestedNameSpecifier, isTemplateIntroduced))
882887
return false;
@@ -913,13 +918,17 @@ auto Parser::parse_unqualified_id(UnqualifiedIdAST*& yyast,
913918

914919
void Parser::parse_optional_nested_name_specifier(
915920
NestedNameSpecifierAST*& yyast, NestedNameSpecifierContext ctx) {
921+
if (!is_parsing_cxx()) return;
922+
916923
LookaheadParser lookahead(this);
917924
if (!parse_nested_name_specifier(yyast, ctx)) return;
918925
lookahead.commit();
919926
}
920927

921928
auto Parser::parse_decltype_nested_name_specifier(
922929
NestedNameSpecifierAST*& yyast, NestedNameSpecifierContext ctx) -> bool {
930+
if (!is_parsing_cxx()) return false;
931+
923932
LookaheadParser lookahead{this};
924933

925934
SourceLocation decltypeLoc;
@@ -7978,6 +7987,8 @@ auto Parser::parse_member_declaration_helper(DeclarationAST*& yyast) -> bool {
79787987
}
79797988

79807989
auto lookat_function_definition = [&] {
7990+
if (!is_parsing_cxx()) return false;
7991+
79817992
LookaheadParser lookahead{this};
79827993

79837994
auto functionDeclarator = getFunctionPrototype(declarator);
@@ -8154,32 +8165,34 @@ auto Parser::parse_member_declarator(InitDeclaratorAST*& yyast,
81548165
ast->declarator = declarator;
81558166
ast->symbol = symbol;
81568167

8157-
if (auto functionDeclarator = getFunctionPrototype(declarator)) {
8158-
RequiresClauseAST* requiresClause = nullptr;
8168+
if (is_parsing_cxx()) {
8169+
if (auto functionDeclarator = getFunctionPrototype(declarator)) {
8170+
RequiresClauseAST* requiresClause = nullptr;
81598171

8160-
if (parse_requires_clause(requiresClause)) {
8161-
ast->requiresClause = requiresClause;
8162-
} else {
8163-
parse_virt_specifier_seq(functionDeclarator);
8172+
if (parse_requires_clause(requiresClause)) {
8173+
ast->requiresClause = requiresClause;
8174+
} else {
8175+
parse_virt_specifier_seq(functionDeclarator);
81648176

8165-
if (!functionDeclarator->attributeList) {
8166-
parse_optional_attribute_specifier_seq(
8167-
functionDeclarator->attributeList);
8168-
}
8177+
if (!functionDeclarator->attributeList) {
8178+
parse_optional_attribute_specifier_seq(
8179+
functionDeclarator->attributeList);
8180+
}
81698181

8170-
SourceLocation equalLoc;
8171-
SourceLocation zeroLoc;
8182+
SourceLocation equalLoc;
8183+
SourceLocation zeroLoc;
81728184

8173-
const auto isPure = parse_pure_specifier(equalLoc, zeroLoc);
8185+
const auto isPure = parse_pure_specifier(equalLoc, zeroLoc);
81748186

8175-
functionDeclarator->isPure = isPure;
8187+
functionDeclarator->isPure = isPure;
8188+
}
8189+
8190+
return true;
81768191
}
81778192

8178-
return true;
8193+
(void)parse_brace_or_equal_initializer(ast->initializer);
81798194
}
81808195

8181-
(void)parse_brace_or_equal_initializer(ast->initializer);
8182-
81838196
return true;
81848197
}
81858198

@@ -9160,6 +9173,8 @@ auto Parser::parse_function_operator_template_id(
91609173
auto Parser::parse_template_id(UnqualifiedIdAST*& yyast,
91619174
NestedNameSpecifierAST* nestedNameSpecifier,
91629175
bool isTemplateIntroduced) -> bool {
9176+
if (!is_parsing_cxx()) return false;
9177+
91639178
if (LiteralOperatorTemplateIdAST* templateName = nullptr;
91649179
parse_literal_operator_template_id(templateName, nestedNameSpecifier)) {
91659180
yyast = templateName;
@@ -9287,6 +9302,8 @@ auto Parser::parse_constraint_expression(ExpressionAST*& yyast) -> bool {
92879302
auto Parser::parse_deduction_guide(DeclarationAST*& yyast,
92889303
TemplateDeclarationAST* templateHead)
92899304
-> bool {
9305+
if (!is_parsing_cxx()) return false;
9306+
92909307
SpecifierAST* explicitSpecifier = nullptr;
92919308
SourceLocation identifierLoc;
92929309
SourceLocation lparenLoc;

0 commit comments

Comments
 (0)