27 #include "llvm/ADT/SmallString.h"
28 #include "llvm/Support/TimeProfiler.h"
30 using namespace clang;
62 assert(Tok.
is(tok::kw_namespace) &&
"Not a namespace!");
66 if (Tok.
is(tok::code_completion)) {
74 InnerNamespaceInfoList ExtraNSs;
79 auto ReadAttributes = [&] {
83 if (Tok.
is(tok::kw___attribute)) {
84 ParseGNUAttributes(attrs);
89 ? diag::warn_cxx14_compat_ns_enum_attribute
90 : diag::ext_ns_enum_attribute)
92 ParseCXX11Attributes(attrs);
95 }
while (MoreToParse);
100 if (Tok.
is(tok::identifier)) {
103 while (Tok.
is(tok::coloncolon) &&
106 GetLookAheadToken(2).is(tok::identifier)))) {
108 InnerNamespaceInfo Info;
111 if (Tok.
is(tok::kw_inline)) {
114 FirstNestedInlineLoc = Info.InlineLoc;
120 ExtraNSs.push_back(Info);
129 if (!ExtraNSs.empty() && attrLoc.
isValid())
130 Diag(attrLoc, diag::err_unexpected_nested_namespace_attribute);
132 if (Tok.
is(tok::equal)) {
134 Diag(Tok, diag::err_expected) << tok::identifier;
140 Diag(attrLoc, diag::err_unexpected_namespace_attributes_alias);
142 Diag(InlineLoc, diag::err_inline_namespace_alias)
144 Decl *NSAlias = ParseNamespaceAlias(NamespaceLoc, IdentLoc, Ident, DeclEnd);
149 if (T.consumeOpen()) {
151 Diag(Tok, diag::err_expected) << tok::l_brace;
153 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
160 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
165 if (ExtraNSs.empty()) {
167 }
else if (InlineLoc.
isValid()) {
168 Diag(InlineLoc, diag::err_inline_nested_namespace_definition);
170 Diag(ExtraNSs[0].NamespaceLoc,
171 diag::warn_cxx14_compat_nested_namespace_definition);
172 if (FirstNestedInlineLoc.
isValid())
173 Diag(FirstNestedInlineLoc,
174 diag::warn_cxx17_compat_inline_nested_namespace_definition);
176 Diag(ExtraNSs[0].NamespaceLoc,
177 diag::warn_cxx14_compat_nested_namespace_definition);
178 if (FirstNestedInlineLoc.
isValid())
179 Diag(FirstNestedInlineLoc, diag::ext_inline_nested_namespace_definition);
181 TentativeParsingAction TPA(*
this);
183 Token rBraceToken = Tok;
186 if (!rBraceToken.
is(tok::r_brace)) {
187 Diag(ExtraNSs[0].NamespaceLoc, diag::ext_nested_namespace_definition)
189 ExtraNSs.back().IdentLoc);
192 for (
const auto &ExtraNS : ExtraNSs) {
193 NamespaceFix +=
" { ";
194 if (ExtraNS.InlineLoc.isValid())
195 NamespaceFix +=
"inline ";
196 NamespaceFix +=
"namespace ";
197 NamespaceFix += ExtraNS.Ident->getName();
201 for (
unsigned i = 0, e = ExtraNSs.size(); i != e; ++i)
204 Diag(ExtraNSs[0].NamespaceLoc, diag::ext_nested_namespace_definition)
207 ExtraNSs.back().IdentLoc),
213 if (FirstNestedInlineLoc.
isValid())
214 Diag(FirstNestedInlineLoc, diag::ext_inline_nested_namespace_definition);
220 diag::warn_cxx98_compat_inline_namespace : diag::ext_inline_namespace);
227 getCurScope(), InlineLoc, NamespaceLoc, IdentLoc, Ident,
228 T.getOpenLocation(), attrs, ImplicitUsingDirectiveDecl);
231 NamespaceLoc,
"parsing namespace");
235 ParseInnerNamespace(ExtraNSs, 0, InlineLoc, attrs, T);
238 NamespaceScope.Exit();
240 DeclEnd = T.getCloseLocation();
244 ImplicitUsingDirectiveDecl);
248 void Parser::ParseInnerNamespace(
const InnerNamespaceInfoList &InnerNSs,
252 if (index == InnerNSs.size()) {
253 while (!tryParseMisplacedModuleImport() && Tok.
isNot(tok::r_brace) &&
256 MaybeParseCXX11Attributes(Attrs);
257 ParseExternalDeclaration(Attrs);
273 getCurScope(), InnerNSs[index].InlineLoc, InnerNSs[index].NamespaceLoc,
274 InnerNSs[index].IdentLoc, InnerNSs[index].Ident,
276 assert(!ImplicitUsingDirectiveDecl &&
277 "nested namespace definition cannot define anonymous namespace");
279 ParseInnerNamespace(InnerNSs, ++index, InlineLoc, attrs, Tracker);
281 NamespaceScope.Exit();
292 assert(Tok.
is(tok::equal) &&
"Not equal token");
296 if (Tok.
is(tok::code_completion)) {
304 ParseOptionalCXXScopeSpecifier(SS,
nullptr,
312 if (Tok.
isNot(tok::identifier)) {
313 Diag(Tok, diag::err_expected_namespace_name);
332 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name))
336 Alias, SS, IdentLoc, Ident);
347 assert(isTokenStringLiteral() &&
"Not a string literal!");
359 MaybeParseCXX11Attributes(DeclAttrs);
361 if (Tok.
isNot(tok::l_brace)) {
368 ParseExternalDeclaration(DeclAttrs, &DS);
376 ProhibitAttributes(DeclAttrs);
381 unsigned NestedModules = 0;
384 case tok::annot_module_begin:
389 case tok::annot_module_end:
396 case tok::annot_module_include:
409 MaybeParseCXX11Attributes(Attrs);
410 ParseExternalDeclaration(Attrs);
429 Decl *Parser::ParseExportDeclaration() {
430 assert(Tok.
is(tok::kw_export));
438 if (Tok.
isNot(tok::l_brace)) {
441 MaybeParseCXX11Attributes(Attrs);
442 ParseExternalDeclaration(Attrs);
453 Diag(ExportLoc, diag::err_export_empty)
457 while (!tryParseMisplacedModuleImport() && Tok.
isNot(tok::r_brace) &&
460 MaybeParseCXX11Attributes(Attrs);
461 ParseExternalDeclaration(Attrs);
466 T.getCloseLocation());
474 assert(Tok.
is(tok::kw_using) &&
"Not using token");
480 if (Tok.
is(tok::code_completion)) {
487 while (Tok.
is(tok::kw_template)) {
489 Diag(TemplateLoc, diag::err_unexpected_template_after_using)
494 if (Tok.
is(tok::kw_namespace)) {
496 if (TemplateInfo.Kind) {
498 Diag(UsingLoc, diag::err_templated_using_directive_declaration)
502 Decl *UsingDir = ParseUsingDirective(Context, UsingLoc, DeclEnd, Attrs);
507 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd, Attrs,
525 assert(Tok.
is(tok::kw_namespace) &&
"Not 'namespace' token");
530 if (Tok.
is(tok::code_completion)) {
538 ParseOptionalCXXScopeSpecifier(SS,
nullptr,
550 if (Tok.
isNot(tok::identifier)) {
551 Diag(Tok, diag::err_expected_namespace_name);
570 bool GNUAttr =
false;
571 if (Tok.
is(tok::kw___attribute)) {
573 ParseGNUAttributes(attrs);
578 if (ExpectAndConsume(tok::semi,
579 GNUAttr ? diag::err_expected_semi_after_attribute_list
580 : diag::err_expected_semi_after_namespace_name))
584 IdentLoc, NamespcName, attrs);
593 UsingDeclarator &D) {
600 if (Tok.
is(tok::kw___super)) {
607 if (ParseOptionalCXXScopeSpecifier(D.SS,
nullptr,
617 if (D.SS.isInvalid())
631 Tok.
is(tok::identifier) &&
636 !D.SS.getScopeRep()->getAsNamespace() &&
637 !D.SS.getScopeRep()->getAsNamespaceAlias()) {
641 D.Name.setConstructorName(
Type, IdLoc, IdLoc);
648 !(Tok.
is(tok::identifier) &&
NextToken().is(tok::equal)),
649 false,
nullptr, D.Name))
655 diag::warn_cxx17_compat_using_declaration_pack :
656 diag::ext_using_declaration_pack);
693 ? diag::warn_cxx17_compat_using_enum_declaration
694 : diag::ext_using_enum_declaration);
696 DiagnoseCXX11AttributeExtension(PrefixAttrs);
699 ParseEnumSpecifier(UELoc, DS, TemplateInfo, AS,
701 DeclSpecContext::DSC_trailing);
703 if (TemplateInfo.Kind) {
705 Diag(UsingLoc, diag::err_templated_using_directive_declaration)
714 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
715 "using-enum declaration"))
724 MaybeParseCXX11Attributes(MisplacedAttrs);
726 if (InInitStatement && Tok.
isNot(tok::identifier))
730 bool InvalidDeclarator = ParseUsingDeclarator(Context, D);
733 MaybeParseAttributes(PAKM_GNU | PAKM_CXX11, Attrs);
737 if (MisplacedAttrs.Range.isValid()) {
738 Diag(MisplacedAttrs.Range.getBegin(), diag::err_attributes_not_allowed)
747 if (Tok.
is(tok::equal) || InInitStatement) {
748 if (InvalidDeclarator) {
753 ProhibitAttributes(PrefixAttrs);
755 Decl *DeclFromDeclSpec =
nullptr;
756 Decl *AD = ParseAliasDeclarationAfterDeclarator(
757 TemplateInfo, UsingLoc, D, DeclEnd, AS, Attrs, &DeclFromDeclSpec);
761 DiagnoseCXX11AttributeExtension(PrefixAttrs);
766 if (TemplateInfo.Kind) {
768 Diag(UsingLoc, diag::err_templated_using_directive_declaration)
780 MaybeParseAttributes(PAKM_GNU | PAKM_CXX11, Attrs);
781 DiagnoseCXX11AttributeExtension(Attrs);
784 if (InvalidDeclarator)
789 if (D.TypenameLoc.isValid() &&
791 Diag(D.Name.getSourceRange().getBegin(),
792 diag::err_typename_identifiers_only)
799 D.TypenameLoc, D.SS, D.Name,
800 D.EllipsisLoc, Attrs);
802 DeclsInGroup.push_back(UD);
810 InvalidDeclarator = ParseUsingDeclarator(Context, D);
813 if (DeclsInGroup.size() > 1)
815 diag::warn_cxx17_compat_multi_using_declaration :
816 diag::ext_multi_using_declaration);
820 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
821 !Attrs.
empty() ?
"attributes list"
822 : UELoc.
isValid() ?
"using-enum declaration"
823 :
"using declaration"))
829 Decl *Parser::ParseAliasDeclarationAfterDeclarator(
833 if (ExpectAndConsume(tok::equal)) {
839 diag::warn_cxx98_compat_alias_declaration :
840 diag::ext_alias_declaration);
844 if (TemplateInfo.Kind == ParsedTemplateInfo::Template &&
847 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization)
849 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
851 if (SpecKind != -1) {
855 D.Name.TemplateId->RAngleLoc);
857 Range = TemplateInfo.getSourceRange();
858 Diag(
Range.getBegin(), diag::err_alias_declaration_specialization)
859 << SpecKind <<
Range;
866 Diag(D.Name.StartLocation, diag::err_alias_declaration_not_identifier);
870 }
else if (D.TypenameLoc.isValid())
871 Diag(D.TypenameLoc, diag::err_alias_declaration_not_identifier)
874 D.SS.isNotEmpty() ? D.SS.getEndLoc() : D.TypenameLoc));
875 else if (D.SS.isNotEmpty())
876 Diag(D.SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
878 if (D.EllipsisLoc.isValid())
879 Diag(D.EllipsisLoc, diag::err_alias_declaration_pack_expansion)
882 Decl *DeclFromDeclSpec =
nullptr;
887 AS, &DeclFromDeclSpec, &Attrs);
889 *OwnedType = DeclFromDeclSpec;
893 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
894 !Attrs.
empty() ?
"attributes list"
895 :
"alias declaration"))
900 TemplateParams ? TemplateParams->data() :
nullptr,
901 TemplateParams ? TemplateParams->size() : 0);
903 UsingLoc, D.Name, Attrs, TypeAlias,
909 if (
const auto *BO = dyn_cast_or_null<BinaryOperator>(AssertExpr)) {
910 if (BO->getOpcode() == BO_LAnd &&
911 isa<StringLiteral>(BO->getRHS()->IgnoreImpCasts()))
926 assert(Tok.
isOneOf(tok::kw_static_assert, tok::kw__Static_assert) &&
927 "Not a static_assert declaration");
931 if (Tok.
is(tok::kw_static_assert)) {
933 Diag(Tok, diag::ext_ms_static_assert)
936 Diag(Tok, diag::warn_cxx98_compat_static_assert);
942 if (T.consumeOpen()) {
943 Diag(Tok, diag::err_expected) << tok::l_paren;
951 if (AssertExpr.isInvalid()) {
957 if (Tok.
is(tok::r_paren)) {
960 DiagVal = diag::warn_cxx14_compat_static_assert_no_message;
962 DiagVal = diag::ext_cxx_static_assert_no_message;
964 DiagVal = diag::warn_c17_compat_static_assert_no_message;
966 DiagVal = diag::ext_c_static_assert_no_message;
970 if (ExpectAndConsume(tok::comma)) {
975 if (!isTokenStringLiteral()) {
976 Diag(Tok, diag::err_expected_string_literal)
992 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert);
997 T.getCloseLocation());
1006 assert(Tok.
isOneOf(tok::kw_decltype, tok::annot_decltype)
1007 &&
"Not a decltype specifier");
1013 if (Tok.
is(tok::annot_decltype)) {
1014 Result = getExprAnnotation(Tok);
1019 ConsumeAnnotationToken();
1020 if (Result.isInvalid()) {
1026 Diag(Tok, diag::warn_cxx98_compat_decltype);
1031 if (T.expectAndConsume(diag::err_expected_lparen_after,
1032 "decltype", tok::r_paren)) {
1035 StartLoc : T.getOpenLocation();
1039 if (Tok.
is(tok::kw_auto) &&
NextToken().is(tok::r_paren)) {
1044 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
1045 : diag::ext_decltype_auto_type_specifier);
1059 if (Result.isInvalid()) {
1062 EndLoc = ConsumeParen();
1069 assert(Tok.
is(tok::semi));
1083 if (T.getCloseLocation().isInvalid()) {
1087 return T.getCloseLocation();
1090 if (Result.isInvalid()) {
1092 return T.getCloseLocation();
1095 EndLoc = T.getCloseLocation();
1097 assert(!Result.isInvalid());
1099 const char *PrevSpec =
nullptr;
1105 DiagID, Result.get(), Policy)
1108 Diag(StartLoc, DiagID) << PrevSpec;
1114 void Parser::AnnotateExistingDecltypeSpecifier(
const DeclSpec& DS,
1131 Tok.
setKind(tok::annot_decltype);
1132 setExprAnnotation(Tok,
1141 void Parser::ParseUnderlyingTypeSpecifier(
DeclSpec &DS) {
1142 assert(Tok.
is(tok::kw___underlying_type) &&
1143 "Not an underlying type specifier");
1147 if (T.expectAndConsume(diag::err_expected_lparen_after,
1148 "__underlying_type", tok::r_paren)) {
1153 if (Result.isInvalid()) {
1160 if (T.getCloseLocation().isInvalid())
1163 const char *PrevSpec =
nullptr;
1166 DiagID, Result.get(),
1168 Diag(StartLoc, DiagID) << PrevSpec;
1193 if (Tok.
is(tok::kw_typename)) {
1194 Diag(Tok, diag::err_expected_class_name_not_template)
1201 if (ParseOptionalCXXScopeSpecifier(SS,
nullptr,
1211 if (Tok.
isOneOf(tok::kw_decltype, tok::annot_decltype)) {
1218 EndLocation = ParseDecltypeSpecifier(DS);
1226 if (Tok.
is(tok::annot_template_id)) {
1229 AnnotateTemplateIdTokenAsType(SS,
true);
1231 assert(Tok.
is(tok::annot_typename) &&
"template-id -> type failed");
1234 ConsumeAnnotationToken();
1241 if (Tok.
isNot(tok::identifier)) {
1242 Diag(Tok, diag::err_expected_class_name);
1249 if (Tok.
is(tok::less)) {
1257 &SS, Template, TNK)) {
1258 Diag(IdLoc, diag::err_unknown_template_name)
1270 if (Tok.
is(tok::annot_template_id) &&
1271 takeTemplateIdAnnotation(Tok)->mightBeType())
1272 AnnotateTemplateIdTokenAsType(SS,
true);
1276 if (Tok.
isNot(tok::annot_typename))
1283 ConsumeAnnotationToken();
1293 false, &CorrectedII);
1295 Diag(IdLoc, diag::err_expected_class_name);
1300 EndLocation = IdLoc;
1308 const char *PrevSpec =
nullptr;
1318 void Parser::ParseMicrosoftInheritanceClassAttributes(
ParsedAttributes &attrs) {
1319 while (Tok.
isOneOf(tok::kw___single_inheritance,
1320 tok::kw___multiple_inheritance,
1321 tok::kw___virtual_inheritance)) {
1324 attrs.
addNew(AttrName, AttrNameLoc,
nullptr, AttrNameLoc,
nullptr, 0,
1332 bool Parser::isValidAfterTypeSpecifier(
bool CouldBeBitfield) {
1340 case tok::identifier:
1342 case tok::coloncolon:
1343 case tok::annot_cxxscope:
1344 case tok::annot_typename:
1345 case tok::annot_template_id:
1346 case tok::kw_decltype:
1349 case tok::kw_operator:
1350 case tok::kw___declspec:
1355 case tok::kw___attribute:
1356 case tok::annot_pragma_pack:
1358 case tok::annot_pragma_ms_pragma:
1360 case tok::annot_pragma_ms_vtordisp:
1362 case tok::annot_pragma_ms_pointers_to_members:
1365 return CouldBeBitfield ||
1368 case tok::kw___cdecl:
1369 case tok::kw___fastcall:
1370 case tok::kw___stdcall:
1371 case tok::kw___thiscall:
1372 case tok::kw___vectorcall:
1378 case tok::kw_volatile:
1379 case tok::kw_restrict:
1380 case tok::kw__Atomic:
1381 case tok::kw___unaligned:
1385 case tok::kw_inline:
1386 case tok::kw_virtual:
1387 case tok::kw_friend:
1389 case tok::kw_static:
1390 case tok::kw_extern:
1391 case tok::kw_typedef:
1392 case tok::kw_register:
1394 case tok::kw_mutable:
1395 case tok::kw_thread_local:
1396 case tok::kw_constexpr:
1397 case tok::kw_consteval:
1398 case tok::kw_constinit:
1414 if (!isKnownToBeTypeSpecifier(
NextToken()))
1471 const ParsedTemplateInfo &TemplateInfo,
1473 DeclSpecContext DSC,
1476 if (TagTokKind == tok::kw_struct)
1478 else if (TagTokKind == tok::kw___interface)
1480 else if (TagTokKind == tok::kw_class)
1483 assert(TagTokKind == tok::kw_union &&
"Not a class specifier");
1487 if (Tok.
is(tok::code_completion)) {
1501 const bool shouldDelayDiagsInTag =
1502 (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate);
1507 MaybeParseAttributes(PAKM_CXX11 | PAKM_Declspec | PAKM_GNU, attrs);
1510 if (Tok.
isOneOf(tok::kw___single_inheritance,
1511 tok::kw___multiple_inheritance,
1512 tok::kw___virtual_inheritance))
1513 ParseMicrosoftInheritanceClassAttributes(attrs);
1516 MaybeParseAttributes(PAKM_CXX11 | PAKM_Declspec | PAKM_GNU, attrs);
1523 Tok.
isNot(tok::identifier) &&
1526 Tok.
isOneOf(tok::kw___is_abstract,
1527 tok::kw___is_aggregate,
1528 tok::kw___is_arithmetic,
1530 tok::kw___is_assignable,
1531 tok::kw___is_base_of,
1533 tok::kw___is_complete_type,
1534 tok::kw___is_compound,
1536 tok::kw___is_constructible,
1537 tok::kw___is_convertible,
1538 tok::kw___is_convertible_to,
1539 tok::kw___is_destructible,
1542 tok::kw___is_floating_point,
1544 tok::kw___is_function,
1545 tok::kw___is_fundamental,
1546 tok::kw___is_integral,
1547 tok::kw___is_interface_class,
1548 tok::kw___is_literal,
1549 tok::kw___is_lvalue_expr,
1550 tok::kw___is_lvalue_reference,
1551 tok::kw___is_member_function_pointer,
1552 tok::kw___is_member_object_pointer,
1553 tok::kw___is_member_pointer,
1554 tok::kw___is_nothrow_assignable,
1555 tok::kw___is_nothrow_constructible,
1556 tok::kw___is_nothrow_destructible,
1557 tok::kw___is_object,
1559 tok::kw___is_pointer,
1560 tok::kw___is_polymorphic,
1561 tok::kw___is_reference,
1562 tok::kw___is_rvalue_expr,
1563 tok::kw___is_rvalue_reference,
1565 tok::kw___is_scalar,
1566 tok::kw___is_sealed,
1567 tok::kw___is_signed,
1568 tok::kw___is_standard_layout,
1569 tok::kw___is_trivial,
1570 tok::kw___is_trivially_assignable,
1571 tok::kw___is_trivially_constructible,
1572 tok::kw___is_trivially_copyable,
1574 tok::kw___is_unsigned,
1576 tok::kw___is_volatile))
1582 TryKeywordIdentFallback(
true);
1584 struct PreserveAtomicIdentifierInfoRAII {
1585 PreserveAtomicIdentifierInfoRAII(
Token &Tok,
bool Enabled)
1586 : AtomicII(nullptr) {
1589 assert(Tok.
is(tok::kw__Atomic));
1594 ~PreserveAtomicIdentifierInfoRAII() {
1597 AtomicII->revertIdentifierToTokenID(tok::kw__Atomic);
1607 bool ShouldChangeAtomicToIdentifier =
getLangOpts().MSVCCompat &&
1608 Tok.
is(tok::kw__Atomic) &&
1610 PreserveAtomicIdentifierInfoRAII AtomicTokenGuard(
1611 Tok, ShouldChangeAtomicToIdentifier);
1621 bool HasValidSpec =
true;
1622 if (ParseOptionalCXXScopeSpecifier(Spec,
nullptr,
1626 HasValidSpec =
false;
1629 if (Tok.
isNot(tok::identifier) && Tok.
isNot(tok::annot_template_id)) {
1630 Diag(Tok, diag::err_expected) << tok::identifier;
1631 HasValidSpec =
false;
1639 auto RecoverFromUndeclaredTemplateName = [&](
IdentifierInfo *Name,
1642 bool KnownUndeclared) {
1643 Diag(NameLoc, diag::err_explicit_spec_non_template)
1644 << (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation)
1645 << TagTokKind << Name << TemplateArgRange << KnownUndeclared;
1649 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1650 if (TemplateParams->size() > 1) {
1651 TemplateParams->pop_back();
1653 TemplateParams =
nullptr;
1654 const_cast<ParsedTemplateInfo &
>(TemplateInfo).
Kind =
1655 ParsedTemplateInfo::NonTemplate;
1657 }
else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1659 TemplateParams =
nullptr;
1660 const_cast<ParsedTemplateInfo &
>(TemplateInfo).
Kind =
1661 ParsedTemplateInfo::NonTemplate;
1662 const_cast<ParsedTemplateInfo &
>(TemplateInfo).TemplateLoc =
1664 const_cast<ParsedTemplateInfo &
>(TemplateInfo).ExternLoc =
1673 if (Tok.
is(tok::identifier)) {
1681 TemplateArgList TemplateArgs;
1683 if (ParseTemplateIdAfterTemplateName(
true, LAngleLoc, TemplateArgs,
1689 RecoverFromUndeclaredTemplateName(
1690 Name, NameLoc,
SourceRange(LAngleLoc, RAngleLoc),
false);
1692 }
else if (Tok.
is(tok::annot_template_id)) {
1693 TemplateId = takeTemplateIdAnnotation(Tok);
1694 NameLoc = ConsumeAnnotationToken();
1701 RecoverFromUndeclaredTemplateName(
1704 TemplateId =
nullptr;
1717 Diag(TemplateId->
LAngleLoc, diag::err_template_spec_syntax_non_template)
1718 << TemplateId->
Name <<
static_cast<int>(TemplateId->
Kind) << Range;
1753 MaybeParseCXX11Attributes(Attributes);
1758 AllowDefiningTypeSpec::No ||
1761 else if (Tok.
is(tok::l_brace) ||
1762 (DSC != DeclSpecContext::DSC_association &&
1764 (isClassCompatibleKeyword() &&
1780 }
else if (isClassCompatibleKeyword() &&
1786 TentativeParsingAction PA(*
this);
1789 while (isClassCompatibleKeyword()) {
1795 if (Tok.
is(tok::l_square) &&
NextToken().is(tok::l_square)) {
1799 }
else if (Tok.
is(tok::kw_alignas) &&
NextToken().is(tok::l_paren)) {
1809 if (Tok.
isOneOf(tok::l_brace, tok::colon))
1815 }
else if (!isTypeSpecifier(DSC) &&
1816 (Tok.
is(tok::semi) ||
1819 if (Tok.
isNot(tok::semi)) {
1822 ExpectAndConsume(tok::semi, diag::err_expected_after,
1840 Diag(AttrRange.
getBegin(), diag::err_attributes_not_allowed)
1856 Diag(StartLoc, diag::err_anon_type_definition)
1883 }
else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
1886 ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed,
1890 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc,
1901 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate)) {
1902 ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed,
1916 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1929 "Expected a definition here");
1933 TemplateParams =
nullptr;
1938 diag::err_explicit_instantiation_with_definition)
1947 LAngleLoc,
nullptr));
1948 TemplateParams = &FakedParamLists;
1955 SS, *TemplateId, attrs,
1958 TemplateParams ? TemplateParams->size() : 0),
1961 }
else if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation &&
1968 ProhibitAttributes(attrs);
1971 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc,
1972 TagType, StartLoc, SS, Name, NameLoc, attrs);
1974 TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate) {
1975 ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed,
1982 TemplateParams ? TemplateParams->size() : 0));
1985 ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed,
1989 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
1992 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1994 TemplateParams =
nullptr;
1997 bool IsDependent =
false;
2007 stripTypeAttributesOffDeclSpec(attrs, DS, TUK);
2010 TagOrTempResult = Actions.
ActOnTag(
2014 DSC == DeclSpecContext::DSC_type_specifier,
2015 DSC == DeclSpecContext::DSC_template_param ||
2016 DSC == DeclSpecContext::DSC_template_type_arg,
2024 SS, Name, StartLoc, NameLoc);
2031 if (shouldDelayDiagsInTag) {
2032 diagsFromTag.done();
2034 TemplateInfo.Kind == ParsedTemplateInfo::Template)
2035 diagsFromTag.redelay();
2040 assert(Tok.
is(tok::l_brace) ||
2042 isClassCompatibleKeyword());
2044 SkipCXXMemberSpecification(StartLoc, AttrFixitLoc,
TagType,
2045 TagOrTempResult.
get());
2047 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, attrs,
TagType,
2048 TagOrTempResult.
get());
2053 ParseStructUnionBody(StartLoc,
TagType, cast<RecordDecl>(D));
2066 const char *PrevSpec =
nullptr;
2071 NameLoc.
isValid() ? NameLoc : StartLoc,
2073 }
else if (!TagOrTempResult.
isInvalid()) {
2075 NameLoc.
isValid() ? NameLoc : StartLoc,
2076 PrevSpec, DiagID, TagOrTempResult.
get(), Owned,
2084 Diag(StartLoc, DiagID) << PrevSpec;
2100 (TemplateInfo.Kind || !isValidAfterTypeSpecifier(
false))) {
2101 if (Tok.
isNot(tok::semi)) {
2103 ExpectAndConsume(tok::semi, diag::err_expected_after,
2121 void Parser::ParseBaseClause(
Decl *ClassDecl) {
2122 assert(Tok.
is(tok::colon) &&
"Not a base clause");
2130 BaseResult Result = ParseBaseSpecifier(ClassDecl);
2131 if (Result.isInvalid()) {
2137 BaseInfo.push_back(Result.get());
2162 bool IsVirtual =
false;
2166 MaybeParseCXX11Attributes(Attributes);
2172 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
2182 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
2186 if (Tok.
is(tok::kw_virtual)) {
2190 Diag(VirtualLoc, diag::err_dup_virtual)
2197 CheckMisplacedCXX11Attribute(Attributes, StartLoc);
2211 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
2227 Access, BaseType.
get(), BaseLoc,
2251 void Parser::HandleMemberFunctionDeclDelays(
Declarator& DeclaratorInfo,
2259 if (!NeedLateParse) {
2263 if (Param->hasUnparsedDefaultArg()) {
2264 NeedLateParse =
true;
2270 if (NeedLateParse) {
2273 auto LateMethod =
new LateParsedMethodDeclaration(
this, ThisDecl);
2274 getCurrentClass().LateParsedDeclarations.push_back(LateMethod);
2279 LateMethod->DefaultArgs.reserve(FTI.
NumParams);
2281 LateMethod->DefaultArgs.push_back(LateParsedDefaultArgument(
2318 if (II == Ident_override)
2321 if (II == Ident_sealed)
2324 if (II == Ident_abstract)
2327 if (II == Ident_final)
2330 if (II == Ident_GNU_final)
2341 void Parser::ParseOptionalCXX11VirtSpecifierSeq(
VirtSpecifiers &VS,
2360 const char *PrevSpec =
nullptr;
2379 ? diag::warn_cxx98_compat_override_control_keyword
2380 : diag::ext_override_control_keyword)
2389 bool Parser::isCXX11FinalKeyword()
const {
2398 bool Parser::isClassCompatibleKeyword()
const {
2408 bool Parser::ParseCXXMemberDeclaratorBeforeInitializer(
2410 LateParsedAttrList &LateParsedAttrs) {
2421 if (Tok.
isNot(tok::colon))
2422 ParseDeclarator(DeclaratorInfo);
2428 "don't know where identifier would go yet?");
2432 }
else if (Tok.
is(tok::kw_requires)) {
2433 ParseTrailingRequiresClause(DeclaratorInfo);
2435 ParseOptionalCXX11VirtSpecifierSeq(
2436 VS, getCurrentClass().IsInterface,
2439 MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS);
2443 if (Tok.
is(tok::kw_asm)) {
2445 ExprResult AsmLabel(ParseSimpleAsm(
true, &Loc));
2446 if (AsmLabel.isInvalid())
2456 DiagnoseAndSkipCXX11Attributes();
2457 MaybeParseGNUAttributes(DeclaratorInfo, &LateParsedAttrs);
2458 DiagnoseAndSkipCXX11Attributes();
2463 ParseOptionalCXX11VirtSpecifierSeq(
2464 VS, getCurrentClass().IsInterface,
2470 if (AL.isKnownToGCC() && !AL.isCXX11Attribute())
2471 Diag(AL.getLoc(), diag::warn_gcc_attribute_location);
2473 MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(DeclaratorInfo, VS);
2489 void Parser::MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(
2496 ParseTypeQualifierListOpt(
2497 DS, AR_NoAttributesParsed,
false,
2498 false, llvm::function_ref<
void()>([&]() {
2506 auto DeclSpecCheck = [&](
DeclSpec::TQ TypeQual, StringRef FixItName,
2509 auto &MQ =
Function.getOrCreateMethodQualifiers();
2510 if (!(MQ.getTypeQualifiers() & TypeQual)) {
2514 MQ.SetTypeQual(TypeQual, SpecLoc);
2516 Diag(SpecLoc, diag::err_declspec_after_virtspec)
2525 bool RefQualifierIsLValueRef =
true;
2527 if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc)) {
2528 const char *Name = (RefQualifierIsLValueRef ?
"& " :
"&& ");
2530 Function.RefQualifierIsLValueRef = RefQualifierIsLValueRef;
2531 Function.RefQualifierLoc = RefQualifierLoc;
2533 Diag(RefQualifierLoc, diag::err_declspec_after_virtspec)
2534 << (RefQualifierIsLValueRef ?
"&" :
"&&")
2583 const ParsedTemplateInfo &TemplateInfo,
2585 if (Tok.
is(tok::at)) {
2587 Diag(Tok, diag::err_at_defs_cxx);
2589 Diag(Tok, diag::err_at_in_class);
2605 bool MalformedTypeSpec =
false;
2606 if (!TemplateInfo.Kind &&
2607 Tok.
isOneOf(tok::identifier, tok::coloncolon, tok::kw___super)) {
2609 MalformedTypeSpec =
true;
2612 if (Tok.
isNot(tok::annot_cxxscope))
2613 isAccessDecl =
false;
2614 else if (
NextToken().is(tok::identifier))
2615 isAccessDecl = GetLookAheadToken(2).
is(tok::semi);
2622 ParseOptionalCXXScopeSpecifier(SS,
nullptr,
2635 false,
false,
true,
true,
2636 false, &TemplateKWLoc, Name)) {
2642 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
2643 "access declaration")) {
2659 if (!TemplateInfo.Kind &&
2660 Tok.
isOneOf(tok::kw_static_assert, tok::kw__Static_assert)) {
2666 if (Tok.
is(tok::kw_template)) {
2667 assert(!TemplateInfo.TemplateParams &&
2668 "Nested template improperly parsed?");
2677 if (Tok.
is(tok::kw___extension__)) {
2681 return ParseCXXClassMemberDeclaration(AS, AccessAttrs,
2682 TemplateInfo, TemplateDiags);
2687 MaybeParseCXX11Attributes(DeclAttrs);
2692 if (Tok.
is(tok::annot_attr_openmp))
2693 return ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, DeclAttrs);
2695 if (Tok.
is(tok::kw_using)) {
2700 while (Tok.
is(tok::kw_template)) {
2702 Diag(TemplateLoc, diag::err_unexpected_template_after_using)
2706 if (Tok.
is(tok::kw_namespace)) {
2707 Diag(UsingLoc, diag::err_using_namespace_in_class);
2714 UsingLoc, DeclEnd, DeclAttrs, AS);
2718 MaybeParseMicrosoftAttributes(DeclSpecAttrs);
2721 LateParsedAttrList CommonLateParsedAttrs;
2728 if (MalformedTypeSpec)
2736 bool IsTemplateSpecOrInst =
2737 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2738 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2741 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DeclSpecContext::DSC_class,
2742 &CommonLateParsedAttrs);
2744 if (IsTemplateSpecOrInst)
2745 diagsFromTag.done();
2753 TemplateInfo.Kind == ParsedTemplateInfo::NonTemplate &&
2754 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DeclSpecContext::DSC_class,
2755 &CommonLateParsedAttrs))
2759 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->data()
2761 TemplateInfo.TemplateParams? TemplateInfo.TemplateParams->size() : 0);
2765 ProhibitAttributes(DeclAttrs);
2769 getCurScope(), AS, DS, DeclAttrs, TemplateParams,
false, AnonRecord);
2770 DS.complete(TheDecl);
2772 Decl* decls[] = {AnonRecord, TheDecl};
2780 if (TemplateInfo.TemplateParams)
2785 LateParsedAttrList LateParsedAttrs;
2790 auto TryConsumePureSpecifier = [&] (
bool AllowDefinition) {
2791 if (Tok.
isNot(tok::equal))
2796 if (
Zero.isNot(tok::numeric_constant) ||
2800 auto &
After = GetLookAheadToken(2);
2801 if (!
After.isOneOf(tok::semi, tok::comma) &&
2802 !(AllowDefinition &&
2803 After.isOneOf(tok::l_brace, tok::colon, tok::kw_try)))
2814 bool ExpectSemi =
true;
2822 if (ParseCXXMemberDeclaratorBeforeInitializer(
2823 DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) {
2828 if (IsTemplateSpecOrInst)
2836 TryConsumePureSpecifier(
true);
2847 if (Tok.
isOneOf(tok::l_brace, tok::colon, tok::kw_try)) {
2849 }
else if (Tok.
is(tok::equal)) {
2851 if (KW.
is(tok::kw_default))
2853 else if (KW.
is(tok::kw_delete))
2855 else if (KW.
is(tok::code_completion)) {
2871 ProhibitAttributes(DeclAttrs);
2888 diag::err_function_declared_typedef);
2895 ParseCXXInlineMethodDef(AS, AccessAttrs, DeclaratorInfo, TemplateInfo,
2899 for (
unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
2900 CommonLateParsedAttrs[i]->addDecl(FunDecl);
2902 for (
unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
2903 LateParsedAttrs[i]->addDecl(FunDecl);
2906 LateParsedAttrs.clear();
2909 if (Tok.
is(tok::semi))
2910 ConsumeExtraSemi(AfterMemberFunctionDefinition);
2922 bool HasStaticInitializer =
false;
2927 Diag(Tok, diag::err_anon_bitfield_member_init);
2931 if (!TryConsumePureSpecifier(
false))
2933 HasStaticInitializer =
true;
2940 if (BitfieldSize.
get())
2942 ? diag::warn_cxx17_compat_bitfield_member_init
2943 : diag::ext_bitfield_member_init);
2946 HasStaticInitializer =
true;
2964 E = Ranges.end(); I != E; ++I)
2965 Diag((*I).getBegin(), diag::err_attributes_not_allowed) << *I;
2974 VS, HasInClassInit);
2977 ThisDecl ? dyn_cast<VarTemplateDecl>(ThisDecl) :
nullptr)
2980 ThisDecl = VT->getTemplatedDecl();
2992 HasStaticInitializer =
true;
2996 Diag(PureSpecLoc, diag::err_duplicate_virt_specifier) <<
"abstract";
2998 if (ThisDecl && PureSpecLoc.
isValid())
3007 ? diag::warn_cxx98_compat_nonstatic_member_init
3008 : diag::ext_nonstatic_member_init);
3016 Diag(Tok, diag::err_incomplete_array_member_init);
3023 ParseCXXNonStaticMemberInitializer(ThisDecl);
3024 }
else if (HasStaticInitializer) {
3029 if (Init.isInvalid()) {
3033 }
else if (ThisDecl)
3042 for (
unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
3043 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
3045 for (
unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
3046 LateParsedAttrs[i]->addDecl(ThisDecl);
3049 DeclsInGroup.push_back(ThisDecl);
3054 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
3056 LateParsedAttrs.clear();
3058 DeclaratorInfo.complete(ThisDecl);
3071 Diag(CommaLoc, diag::err_expected_semi_declaration)
3078 DeclaratorInfo.
clear();
3087 DiagnoseAndSkipCXX11Attributes();
3088 MaybeParseGNUAttributes(DeclaratorInfo);
3089 DiagnoseAndSkipCXX11Attributes();
3091 if (ParseCXXMemberDeclaratorBeforeInitializer(
3092 DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs))
3097 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
3128 ExprResult Parser::ParseCXXMemberInitializer(
Decl *D,
bool IsFunction,
3130 assert(Tok.
isOneOf(tok::equal, tok::l_brace)
3131 &&
"Data member initializer not starting with '=' or '{'");
3136 if (Tok.
is(tok::kw_delete)) {
3143 if (IsFunction || Next.isOneOf(tok::semi, tok::comma,
tok::eof)) {
3151 }
else if (Tok.
is(tok::kw_default)) {
3153 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
3161 if (
const auto *PD = dyn_cast_or_null<MSPropertyDecl>(D)) {
3162 Diag(Tok, diag::err_ms_property_initializer) << PD;
3165 return ParseInitializer();
3168 void Parser::SkipCXXMemberSpecification(
SourceLocation RecordLoc,
3173 assert(isCXX11FinalKeyword() &&
"not a class definition");
3179 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
3184 if (Tok.
isNot(tok::colon) && Tok.
isNot(tok::l_brace))
3191 if (Tok.
is(tok::colon)) {
3194 ParsingClassDefinition ParsingDef(*
this,
TagDecl,
true,
3200 ParseBaseClause(
nullptr);
3204 if (!Tok.
is(tok::l_brace)) {
3206 diag::err_expected_lbrace_after_base_specifiers);
3212 assert(Tok.
is(tok::l_brace));
3218 if (Tok.
is(tok::kw___attribute)) {
3220 MaybeParseGNUAttributes(Attrs);
3230 case tok::kw___if_exists:
3231 case tok::kw___if_not_exists:
3232 ParseMicrosoftIfExistsClassDeclaration(
TagType, AccessAttrs, AS);
3237 ConsumeExtraSemi(InsideStruct,
TagType);
3241 case tok::annot_pragma_vis:
3242 HandlePragmaVisibility();
3244 case tok::annot_pragma_pack:
3247 case tok::annot_pragma_align:
3248 HandlePragmaAlign();
3250 case tok::annot_pragma_ms_pointers_to_members:
3251 HandlePragmaMSPointersToMembers();
3253 case tok::annot_pragma_ms_pragma:
3254 HandlePragmaMSPragma();
3256 case tok::annot_pragma_ms_vtordisp:
3257 HandlePragmaMSVtorDisp();
3259 case tok::annot_pragma_dump:
3263 case tok::kw_namespace:
3265 DiagnoseUnexpectedNamespace(cast<NamedDecl>(
TagDecl));
3268 case tok::kw_private:
3272 return ParseCXXClassMemberDeclaration(AS, AccessAttrs);
3274 case tok::kw_public:
3275 case tok::kw_protected: {
3285 AccessAttrs.
clear();
3286 MaybeParseGNUAttributes(AccessAttrs);
3291 Diag(EndLoc, diag::err_expected)
3295 Diag(EndLoc, diag::err_expected)
3307 AccessAttrs.
clear();
3313 case tok::annot_attr_openmp:
3314 case tok::annot_pragma_openmp:
3315 return ParseOpenMPDeclarativeDirectiveWithExtDecl(
3323 ConsumeAnnotationToken();
3326 return ParseCXXClassMemberDeclaration(AS, AccessAttrs);
3336 void Parser::ParseCXXMemberSpecification(
SourceLocation RecordLoc,
3345 llvm::TimeTraceScope TimeScope(
"ParseClass", [&]() {
3346 if (
auto *TD = dyn_cast_or_null<NamedDecl>(
TagDecl))
3347 return TD->getQualifiedNameAsString();
3352 "parsing struct/union/class body");
3356 bool NonNestedClass =
true;
3357 if (!ClassStack.empty()) {
3359 if (S->isClassScope()) {
3361 NonNestedClass =
false;
3364 if (getCurrentClass().IsInterface) {
3365 Diag(RecordLoc, diag::err_invalid_member_in_interface)
3368 ? cast<NamedDecl>(
TagDecl)->getQualifiedNameAsString()
3374 if (S->isFunctionScope())
3385 ParsingClassDefinition ParsingDef(*
this,
TagDecl, NonNestedClass,
3393 bool IsFinalSpelledSealed =
false;
3394 bool IsAbstract =
false;
3402 if (isCXX11FinalKeyword()) {
3405 Diag(Skipped, diag::err_duplicate_class_virt_specifier)
3410 IsFinalSpelledSealed =
true;
3415 Diag(Skipped, diag::err_duplicate_class_virt_specifier)
3423 Diag(FinalLoc, diag::err_override_control_interface)
3427 ? diag::warn_cxx98_compat_override_control_keyword
3428 : diag::ext_override_control_keyword)
3431 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
3433 Diag(AbstractLoc, diag::ext_ms_abstract_keyword);
3435 Diag(FinalLoc, diag::ext_warn_gnu_final);
3438 "not a class definition");
3444 CheckMisplacedCXX11Attribute(Attrs, AttrFixitLoc);
3453 if (!Tok.
is(tok::colon) && !Tok.
is(tok::l_brace)) {
3460 if (Tok.
is(tok::colon)) {
3461 ParseScope InheritanceScope(
this,
getCurScope()->getFlags() |
3465 if (!Tok.
is(tok::l_brace)) {
3466 bool SuggestFixIt =
false;
3470 case tok::kw_private:
3471 case tok::kw_protected:
3472 case tok::kw_public:
3475 case tok::kw_static_assert:
3479 case tok::kw_template:
3480 SuggestFixIt =
true;
3482 case tok::identifier:
3483 SuggestFixIt = isConstructorDeclarator(
true);
3486 SuggestFixIt = isCXXSimpleDeclaration(
false);
3491 Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers);
3505 assert(Tok.
is(tok::l_brace));
3511 IsFinalSpelledSealed, IsAbstract,
3512 T.getOpenLocation());
3527 while (!tryParseMisplacedModuleImport() && Tok.
isNot(tok::r_brace) &&
3530 ParseCXXClassMemberDeclarationWithPragmas(
3532 MaybeDestroyTemplateIds();
3541 MaybeParseGNUAttributes(attrs);
3545 T.getOpenLocation(),
3546 T.getCloseLocation(), attrs);
3553 if (
TagDecl && NonNestedClass) {
3560 ParseLexedPragmas(getCurrentClass());
3561 ParseLexedAttributes(getCurrentClass());
3562 ParseLexedMethodDeclarations(getCurrentClass());
3567 ParseLexedMemberInitializers(getCurrentClass());
3568 ParseLexedMethodDefs(getCurrentClass());
3569 PrevTokLocation = SavedPrevTokLocation;
3584 void Parser::DiagnoseUnexpectedNamespace(
NamedDecl *D) {
3585 assert(Tok.
is(tok::kw_namespace));
3590 diag::err_missing_end_of_definition) << D;
3592 diag::note_missing_end_of_definition_before) << D;
3626 void Parser::ParseConstructorInitializer(
Decl *ConstructorDecl) {
3627 assert(Tok.
is(tok::colon) &&
3628 "Constructor initializer always starts with ':'");
3636 bool AnyErrors =
false;
3639 if (Tok.
is(tok::code_completion)) {
3646 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
3648 MemInitializers.push_back(MemInit.
get());
3652 if (Tok.
is(tok::comma))
3654 else if (Tok.
is(tok::l_brace))
3659 Tok.
isOneOf(tok::identifier, tok::coloncolon)) {
3661 Diag(Loc, diag::err_ctor_init_missing_comma)
3692 if (ParseOptionalCXXScopeSpecifier(SS,
nullptr,
3705 if (Tok.
is(tok::identifier)) {
3710 }
else if (Tok.
is(tok::annot_decltype)) {
3715 ParseDecltypeSpecifier(DS);
3718 ? takeTemplateIdAnnotation(Tok)
3721 AnnotateTemplateIdTokenAsType(SS,
true);
3722 assert(Tok.
is(tok::annot_typename) &&
"template-id -> type failed");
3724 ConsumeAnnotationToken();
3726 Diag(Tok, diag::err_expected_member_or_base_name);
3733 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3736 ExprResult InitList = ParseBraceInitializer();
3746 TemplateTypeTy.
get(), DS, IdLoc,
3747 InitList.
get(), EllipsisLoc);
3748 }
else if(Tok.
is(tok::l_paren)) {
3753 ExprVector ArgExprs;
3754 CommaLocsTy CommaLocs;
3755 auto RunSignatureHelp = [&] {
3759 ConstructorDecl, SS, TemplateTypeTy.
get(), ArgExprs, II,
3760 T.getOpenLocation(),
false);
3761 CalledSignatureHelp =
true;
3762 return PreferredType;
3764 if (Tok.
isNot(tok::r_paren) &&
3765 ParseExpressionList(ArgExprs, CommaLocs, [&] {
3766 PreferredType.enterFunctionArgument(Tok.
getLocation(),
3783 TemplateTypeTy.
get(), DS, IdLoc,
3784 T.getOpenLocation(), ArgExprs,
3785 T.getCloseLocation(), EllipsisLoc);
3792 return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
3794 return Diag(Tok, diag::err_expected) << tok::l_paren;
3807 Parser::tryParseExceptionSpecification(
bool Delayed,
3814 ExceptionSpecTokens =
nullptr;
3818 if (Tok.
isNot(tok::kw_throw) && Tok.
isNot(tok::kw_noexcept))
3822 bool IsNoexcept = Tok.
is(tok::kw_noexcept);
3823 Token StartTok = Tok;
3827 if (!Tok.
is(tok::l_paren)) {
3830 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3831 NoexceptExpr =
nullptr;
3835 Diag(Tok, diag::err_expected_lparen_after) <<
"throw";
3841 ExceptionSpecTokens->push_back(StartTok);
3842 ExceptionSpecTokens->push_back(Tok);
3843 SpecificationRange.
setEnd(ConsumeParen());
3845 ConsumeAndStoreUntil(tok::r_paren, *ExceptionSpecTokens,
3848 SpecificationRange.
setEnd(ExceptionSpecTokens->back().getLocation());
3854 if (Tok.
is(tok::kw_throw)) {
3855 Result = ParseDynamicExceptionSpecification(SpecificationRange,
3857 DynamicExceptionRanges);
3858 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
3859 "Produced different number of exception types and ranges.");
3863 if (Tok.
isNot(tok::kw_noexcept))
3866 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
3874 if (Tok.
is(tok::l_paren)) {
3883 NoexceptRange =
SourceRange(KeywordLoc, T.getCloseLocation());
3890 NoexceptRange =
SourceRange(KeywordLoc, KeywordLoc);
3894 SpecificationRange = NoexceptRange;
3895 Result = NoexceptType;
3899 if (Tok.
is(tok::kw_throw)) {
3900 Diag(Tok.
getLocation(), diag::err_dynamic_and_noexcept_specification);
3901 ParseDynamicExceptionSpecification(NoexceptRange, DynamicExceptions,
3902 DynamicExceptionRanges);
3905 Diag(Tok.
getLocation(), diag::err_dynamic_and_noexcept_specification);
3913 if (
P.getLangOpts().CPlusPlus11) {
3914 const char *Replacement = IsNoexcept ?
"noexcept" :
"noexcept(false)";
3915 P.Diag(Range.getBegin(),
3916 P.getLangOpts().CPlusPlus17 && !IsNoexcept
3917 ? diag::ext_dynamic_exception_spec
3918 : diag::warn_exception_spec_deprecated)
3920 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
3940 assert(Tok.
is(tok::kw_throw) &&
"expected throw");
3944 if (T.consumeOpen()) {
3945 Diag(Tok, diag::err_expected_lparen_after) <<
"throw";
3952 if (Tok.
is(tok::ellipsis)) {
3955 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
3957 SpecificationRange.
setEnd(T.getCloseLocation());
3964 while (Tok.
isNot(tok::r_paren)) {
3967 if (Tok.
is(tok::ellipsis)) {
3972 Range.setEnd(Ellipsis);
3973 if (!Res.isInvalid())
3977 if (!Res.isInvalid()) {
3978 Exceptions.push_back(Res.get());
3979 Ranges.push_back(Range);
3987 SpecificationRange.
setEnd(T.getCloseLocation());
3989 Exceptions.empty());
3996 bool MayBeFollowedByDirectInit) {
3997 assert(Tok.
is(tok::arrow) &&
"expected arrow");
4007 void Parser::ParseTrailingRequiresClause(
Declarator &D) {
4008 assert(Tok.
is(tok::kw_requires) &&
"expected requires");
4013 ParseScope ParamScope(
this,
4021 InitCXXThisScopeForDeclaratorIfRelevant(D, D.
getDeclSpec(), ThisScope);
4023 TrailingRequiresClause =
4026 TrailingRequiresClause =
4031 diag::err_requires_clause_on_declarator_not_declaring_a_function);
4036 SkipUntil({tok::l_brace, tok::arrow, tok::kw_try, tok::comma, tok::colon},
4047 ParseTrailingReturnType(Range,
false);
4051 diag::err_requires_clause_must_appear_after_trailing_return)
4055 FunctionChunk.TrailingReturnType = TrailingReturnType.
get();
4056 FunctionChunk.TrailingReturnTypeLoc =
Range.getBegin();
4058 SkipUntil({tok::equal, tok::l_brace, tok::arrow, tok::kw_try, tok::comma},
4067 Parser::PushParsingClass(
Decl *ClassDecl,
bool NonNestedClass,
4069 assert((NonNestedClass || !ClassStack.empty()) &&
4070 "Nested class without outer class");
4071 ClassStack.push(
new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
4077 void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
4078 for (
unsigned I = 0, N =
Class->LateParsedDeclarations.size(); I != N; ++I)
4079 delete Class->LateParsedDeclarations[I];
4090 assert(!ClassStack.empty() &&
"Mismatched push/pop for class parsing");
4094 ParsingClass *Victim = ClassStack.top();
4096 if (Victim->TopLevelClass) {
4099 DeallocateParsedClasses(Victim);
4102 assert(!ClassStack.empty() &&
"Missing top-level class?");
4104 if (Victim->LateParsedDeclarations.empty()) {
4109 DeallocateParsedClasses(Victim);
4116 assert(
getCurScope()->isClassScope() &&
"Nested class outside of class scope?");
4117 ClassStack.top()->LateParsedDeclarations.push_back(
4118 new LateParsedClass(
this, Victim));
4145 case tok::code_completion:
4152 case tok::numeric_constant: {
4160 StringRef Spelling = PP.
getSpelling(ExpansionLoc, ExpansionBuf);
4161 if (Spelling ==
"__clang__") {
4165 Diag(Tok, diag::warn_wrong_clang_attr_namespace)
4181 case tok::pipeequal:
4182 case tok::caretequal:
4184 case tok::exclaimequal:
4190 StringRef Spelling = PP.
getSpelling(SpellingLoc, SpellingBuf);
4204 if (T.consumeOpen()) {
4205 Diag(Tok, diag::err_expected) << tok::l_paren;
4209 if (AttrName->
isStr(
"directive")) {
4215 OMPBeginTok.
setKind(tok::annot_attr_openmp);
4217 OpenMPTokens.push_back(OMPBeginTok);
4219 ConsumeAndStoreUntil(tok::r_paren, OpenMPTokens,
false,
4223 OMPEndTok.
setKind(tok::annot_pragma_openmp_end);
4225 OpenMPTokens.push_back(OMPEndTok);
4227 assert(AttrName->
isStr(
"sequence") &&
4228 "Expected either 'directive' or 'sequence'");
4237 IdentifierInfo *Ident = TryParseCXX11AttributeIdentifier(IdentLoc);
4241 if (Ident && Ident->
isStr(
"omp") && !ExpectAndConsume(tok::coloncolon))
4242 Ident = TryParseCXX11AttributeIdentifier(IdentLoc);
4246 if (!Ident || (!Ident->
isStr(
"directive") && !Ident->
isStr(
"sequence"))) {
4253 ParseOpenMPAttributeArgs(Ident, OpenMPTokens);
4267 case ParsedAttr::AT_CarriesDependency:
4268 case ParsedAttr::AT_Deprecated:
4269 case ParsedAttr::AT_FallThrough:
4270 case ParsedAttr::AT_CXX11NoReturn:
4271 case ParsedAttr::AT_NoUniqueAddress:
4272 case ParsedAttr::AT_Likely:
4273 case ParsedAttr::AT_Unlikely:
4275 case ParsedAttr::AT_WarnUnusedResult:
4276 return !ScopeName && AttrName->
getName().equals(
"nodiscard");
4277 case ParsedAttr::AT_Unused:
4278 return !ScopeName && AttrName->
getName().equals(
"maybe_unused");
4305 assert(Tok.
is(tok::l_paren) &&
"Not a C++11 attribute argument list");
4313 if (
hasAttribute(AttributeCommonInfo::Syntax::AS_Microsoft, ScopeName,
4321 !
hasAttribute(LO.CPlusPlus ? AttributeCommonInfo::Syntax::AS_CXX11
4322 : AttributeCommonInfo::Syntax::AS_C2x,
4331 if (ScopeName && (ScopeName->
isStr(
"gnu") || ScopeName->
isStr(
"__gnu__"))) {
4334 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
4335 ScopeLoc, Syntax,
nullptr);
4339 if (ScopeName && ScopeName->
isStr(
"omp")) {
4341 ? diag::warn_omp51_compat_attributes
4342 : diag::ext_omp_attributes);
4344 ParseOpenMPAttributeArgs(AttrName, OpenMPTokens);
4353 if (ScopeName && (ScopeName->
isStr(
"clang") || ScopeName->
isStr(
"_Clang")))
4354 NumArgs = ParseClangAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc,
4355 ScopeName, ScopeLoc, Syntax);
4358 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
4359 ScopeName, ScopeLoc, Syntax);
4361 if (!Attrs.
empty() &&
4368 if (
Attr.getMaxArgs() && !NumArgs) {
4371 Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName;
4372 Attr.setInvalid(
true);
4373 }
else if (!
Attr.getMaxArgs()) {
4377 Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments)
4380 Attr.setInvalid(
true);
4413 if (Tok.
is(tok::kw_alignas)) {
4415 ParseAlignmentSpecifier(Attrs, EndLoc);
4419 assert(Tok.
is(tok::l_square) &&
NextToken().is(tok::l_square) &&
4420 "Not a double square bracket attribute list");
4423 Diag(OpenLoc, diag::warn_cxx98_compat_attribute);
4426 checkCompoundToken(OpenLoc, tok::l_square, CompoundToken::AttrBegin);
4431 if (Tok.
is(tok::kw_using)) {
4433 ? diag::warn_cxx14_compat_using_attribute_ns
4434 : diag::ext_using_attribute_ns);
4437 CommonScopeName = TryParseCXX11AttributeIdentifier(
4439 if (!CommonScopeName) {
4447 llvm::SmallDenseMap<IdentifierInfo*, SourceLocation, 4> SeenAttrs;
4449 bool AttrParsed =
false;
4454 if (ExpectAndConsume(tok::comma)) {
4468 AttrName = TryParseCXX11AttributeIdentifier(
4476 ScopeName = AttrName;
4479 AttrName = TryParseCXX11AttributeIdentifier(
4488 if (CommonScopeName) {
4490 Diag(ScopeLoc, diag::err_using_attribute_ns_conflict)
4493 ScopeName = CommonScopeName;
4494 ScopeLoc = CommonScopeLoc;
4499 if (Tok.
is(tok::l_paren))
4500 AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrLoc, Attrs, EndLoc,
4501 ScopeName, ScopeLoc, OpenMPTokens);
4507 ScopeName, ScopeLoc,
nullptr, 0,
4513 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis)
4519 if (Tok.
is(tok::semi)) {
4525 if (ExpectAndConsume(tok::r_square))
4527 else if (Tok.
is(tok::r_square))
4528 checkCompoundToken(CloseLoc, tok::r_square, CompoundToken::AttrEnd);
4531 if (ExpectAndConsume(tok::r_square))
4540 assert(standardAttributesAllowed());
4546 ParseCXX11AttributeSpecifier(Attrs, &EndLoc);
4547 }
while (isCXX11AttributeSpecifier());
4552 void Parser::DiagnoseAndSkipCXX11Attributes() {
4559 Diag(StartLoc, diag::err_attributes_not_allowed)
4567 if (!isCXX11AttributeSpecifier())
4571 if (Tok.
is(tok::l_square)) {
4575 EndLoc = T.getCloseLocation();
4577 assert(Tok.
is(tok::kw_alignas) &&
"not an attribute specifier");
4580 if (!T.consumeOpen())
4582 EndLoc = T.getCloseLocation();
4584 }
while (isCXX11AttributeSpecifier());
4591 assert(Tok.
is(tok::identifier) &&
"Not a Microsoft attribute list");
4593 assert(UuidIdent->
getName() ==
"uuid" &&
"Not a Microsoft attribute list");
4600 if (T.consumeOpen()) {
4601 Diag(Tok, diag::err_expected) << tok::l_paren;
4606 if (Tok.
is(tok::string_literal)) {
4611 ArgExprs.push_back(StringResult.
get());
4628 while (Tok.
isNot(tok::r_paren)) {
4630 Diag(Tok, diag::err_attribute_uuid_malformed_guid);
4635 SpellingBuffer.resize(Tok.
getLength() + 1);
4637 StringRef TokSpelling = PP.
getSpelling(Tok, SpellingBuffer, &Invalid);
4642 StrBuffer += TokSpelling;
4648 Diag(Tok, diag::err_attribute_uuid_malformed_guid);
4658 Toks[0].
setKind(tok::string_literal);
4664 ArgExprs.push_back(UuidString);
4667 if (!T.consumeClose()) {
4683 assert(Tok.
is(tok::l_square) &&
"Not a Microsoft attribute list");
4694 SkipUntil(tok::r_square, tok::identifier,
4696 if (Tok.
is(tok::code_completion)) {
4703 if (Tok.
isNot(tok::identifier))
4706 ParseMicrosoftUuidAttributeArgs(Attrs);
4716 bool AttrParsed =
false;
4717 if (Tok.
is(tok::l_paren)) {
4720 ParseCXX11AttributeArgs(II, NameLoc, Attrs, &EndLoc,
nullptr,
4722 ReplayOpenMPAttributeTokens(OpenMPTokens);
4733 EndLoc = T.getCloseLocation();
4734 }
while (Tok.
is(tok::l_square));
4739 void Parser::ParseMicrosoftIfExistsClassDeclaration(
4742 IfExistsCondition Result;
4743 if (ParseMicrosoftIfExistsCondition(Result))
4747 if (Braces.consumeOpen()) {
4748 Diag(Tok, diag::err_expected) << tok::l_brace;
4752 switch (Result.Behavior) {
4758 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
4759 << Result.IsIfExists;
4768 while (Tok.
isNot(tok::r_brace) && !isEofOrEom()) {
4770 if (Tok.
isOneOf(tok::kw___if_exists, tok::kw___if_not_exists)) {
4771 ParseMicrosoftIfExistsClassDeclaration(
TagType,
4772 AccessAttrs, CurAS);
4777 if (Tok.
is(tok::semi)) {
4778 ConsumeExtraSemi(InsideStruct,
TagType);
4788 if (Tok.
is(tok::colon))
4792 Diag(Tok, diag::err_expected) << tok::colon;
4798 ParseCXXClassMemberDeclaration(CurAS, AccessAttrs);
4801 Braces.consumeClose();