1//===--- ParseDeclCXX.cpp - C++ Declaration Parsing -------------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the C++ Declaration portions of the Parser interfaces.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/AST/ASTContext.h"
14#include "clang/AST/DeclTemplate.h"
15#include "clang/AST/PrettyDeclStackTrace.h"
16#include "clang/Basic/AttributeCommonInfo.h"
17#include "clang/Basic/Attributes.h"
18#include "clang/Basic/CharInfo.h"
19#include "clang/Basic/DiagnosticParse.h"
20#include "clang/Basic/TargetInfo.h"
21#include "clang/Basic/TokenKinds.h"
22#include "clang/Lex/LiteralSupport.h"
23#include "clang/Parse/ParseHLSLRootSignature.h"
24#include "clang/Parse/Parser.h"
25#include "clang/Parse/RAIIObjectsForParser.h"
26#include "clang/Sema/DeclSpec.h"
27#include "clang/Sema/EnterExpressionEvaluationContext.h"
28#include "clang/Sema/Lookup.h"
29#include "clang/Sema/ParsedTemplate.h"
30#include "clang/Sema/Scope.h"
31#include "clang/Sema/SemaCodeCompletion.h"
32#include "llvm/Support/TimeProfiler.h"
33#include <optional>
34
35using namespace clang;
36
37Parser::DeclGroupPtrTy Parser::ParseNamespace(DeclaratorContext Context,
38 SourceLocation &DeclEnd,
39 SourceLocation InlineLoc) {
40 assert(Tok.is(tok::kw_namespace) && "Not a namespace!");
41 SourceLocation NamespaceLoc = ConsumeToken(); // eat the 'namespace'.
42 ObjCDeclContextSwitch ObjCDC(*this);
43
44 if (Tok.is(K: tok::code_completion)) {
45 cutOffParsing();
46 Actions.CodeCompletion().CodeCompleteNamespaceDecl(S: getCurScope());
47 return nullptr;
48 }
49
50 SourceLocation IdentLoc;
51 IdentifierInfo *Ident = nullptr;
52 InnerNamespaceInfoList ExtraNSs;
53 SourceLocation FirstNestedInlineLoc;
54
55 ParsedAttributes attrs(AttrFactory);
56
57 while (MaybeParseGNUAttributes(Attrs&: attrs) || isAllowedCXX11AttributeSpecifier()) {
58 if (isAllowedCXX11AttributeSpecifier()) {
59 if (getLangOpts().CPlusPlus11)
60 Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
61 ? diag::warn_cxx14_compat_ns_enum_attribute
62 : diag::ext_ns_enum_attribute)
63 << 0 /*namespace*/;
64 ParseCXX11Attributes(attrs);
65 }
66 }
67
68 if (Tok.is(K: tok::identifier)) {
69 Ident = Tok.getIdentifierInfo();
70 IdentLoc = ConsumeToken(); // eat the identifier.
71 while (Tok.is(K: tok::coloncolon) &&
72 (NextToken().is(K: tok::identifier) ||
73 (NextToken().is(K: tok::kw_inline) &&
74 GetLookAheadToken(N: 2).is(K: tok::identifier)))) {
75
76 InnerNamespaceInfo Info;
77 Info.NamespaceLoc = ConsumeToken();
78
79 if (Tok.is(K: tok::kw_inline)) {
80 Info.InlineLoc = ConsumeToken();
81 if (FirstNestedInlineLoc.isInvalid())
82 FirstNestedInlineLoc = Info.InlineLoc;
83 }
84
85 Info.Ident = Tok.getIdentifierInfo();
86 Info.IdentLoc = ConsumeToken();
87
88 ExtraNSs.push_back(Elt: Info);
89 }
90 }
91
92 DiagnoseAndSkipCXX11Attributes();
93 MaybeParseGNUAttributes(Attrs&: attrs);
94 DiagnoseAndSkipCXX11Attributes();
95
96 SourceLocation attrLoc = attrs.Range.getBegin();
97
98 // A nested namespace definition cannot have attributes.
99 if (!ExtraNSs.empty() && attrLoc.isValid())
100 Diag(attrLoc, diag::err_unexpected_nested_namespace_attribute);
101
102 if (Tok.is(K: tok::equal)) {
103 if (!Ident) {
104 Diag(Tok, diag::err_expected) << tok::identifier;
105 // Skip to end of the definition and eat the ';'.
106 SkipUntil(T: tok::semi);
107 return nullptr;
108 }
109 if (!ExtraNSs.empty()) {
110 Diag(ExtraNSs.front().NamespaceLoc,
111 diag::err_unexpected_qualified_namespace_alias)
112 << SourceRange(ExtraNSs.front().NamespaceLoc,
113 ExtraNSs.back().IdentLoc);
114 SkipUntil(T: tok::semi);
115 return nullptr;
116 }
117 if (attrLoc.isValid())
118 Diag(attrLoc, diag::err_unexpected_namespace_attributes_alias);
119 if (InlineLoc.isValid())
120 Diag(InlineLoc, diag::err_inline_namespace_alias)
121 << FixItHint::CreateRemoval(InlineLoc);
122 Decl *NSAlias = ParseNamespaceAlias(NamespaceLoc, AliasLoc: IdentLoc, Alias: Ident, DeclEnd);
123 return Actions.ConvertDeclToDeclGroup(Ptr: NSAlias);
124 }
125
126 BalancedDelimiterTracker T(*this, tok::l_brace);
127 if (T.consumeOpen()) {
128 if (Ident)
129 Diag(Tok, diag::err_expected) << tok::l_brace;
130 else
131 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
132 return nullptr;
133 }
134
135 if (getCurScope()->isClassScope() || getCurScope()->isTemplateParamScope() ||
136 getCurScope()->isInObjcMethodScope() || getCurScope()->getBlockParent() ||
137 getCurScope()->getFnParent()) {
138 Diag(T.getOpenLocation(), diag::err_namespace_nonnamespace_scope);
139 SkipUntil(T: tok::r_brace);
140 return nullptr;
141 }
142
143 if (ExtraNSs.empty()) {
144 // Normal namespace definition, not a nested-namespace-definition.
145 } else if (InlineLoc.isValid()) {
146 Diag(InlineLoc, diag::err_inline_nested_namespace_definition);
147 } else if (getLangOpts().CPlusPlus20) {
148 Diag(ExtraNSs[0].NamespaceLoc,
149 diag::warn_cxx14_compat_nested_namespace_definition);
150 if (FirstNestedInlineLoc.isValid())
151 Diag(FirstNestedInlineLoc,
152 diag::warn_cxx17_compat_inline_nested_namespace_definition);
153 } else if (getLangOpts().CPlusPlus17) {
154 Diag(ExtraNSs[0].NamespaceLoc,
155 diag::warn_cxx14_compat_nested_namespace_definition);
156 if (FirstNestedInlineLoc.isValid())
157 Diag(FirstNestedInlineLoc, diag::ext_inline_nested_namespace_definition);
158 } else {
159 TentativeParsingAction TPA(*this);
160 SkipUntil(T: tok::r_brace, Flags: StopBeforeMatch);
161 Token rBraceToken = Tok;
162 TPA.Revert();
163
164 if (!rBraceToken.is(K: tok::r_brace)) {
165 Diag(ExtraNSs[0].NamespaceLoc, diag::ext_nested_namespace_definition)
166 << SourceRange(ExtraNSs.front().NamespaceLoc,
167 ExtraNSs.back().IdentLoc);
168 } else {
169 std::string NamespaceFix;
170 for (const auto &ExtraNS : ExtraNSs) {
171 NamespaceFix += " { ";
172 if (ExtraNS.InlineLoc.isValid())
173 NamespaceFix += "inline ";
174 NamespaceFix += "namespace ";
175 NamespaceFix += ExtraNS.Ident->getName();
176 }
177
178 std::string RBraces;
179 for (unsigned i = 0, e = ExtraNSs.size(); i != e; ++i)
180 RBraces += "} ";
181
182 Diag(ExtraNSs[0].NamespaceLoc, diag::ext_nested_namespace_definition)
183 << FixItHint::CreateReplacement(
184 SourceRange(ExtraNSs.front().NamespaceLoc,
185 ExtraNSs.back().IdentLoc),
186 NamespaceFix)
187 << FixItHint::CreateInsertion(rBraceToken.getLocation(), RBraces);
188 }
189
190 // Warn about nested inline namespaces.
191 if (FirstNestedInlineLoc.isValid())
192 Diag(FirstNestedInlineLoc, diag::ext_inline_nested_namespace_definition);
193 }
194
195 // If we're still good, complain about inline namespaces in non-C++0x now.
196 if (InlineLoc.isValid())
197 Diag(InlineLoc, getLangOpts().CPlusPlus11
198 ? diag::warn_cxx98_compat_inline_namespace
199 : diag::ext_inline_namespace);
200
201 // Enter a scope for the namespace.
202 ParseScope NamespaceScope(this, Scope::DeclScope);
203
204 UsingDirectiveDecl *ImplicitUsingDirectiveDecl = nullptr;
205 Decl *NamespcDecl = Actions.ActOnStartNamespaceDef(
206 S: getCurScope(), InlineLoc, NamespaceLoc, IdentLoc, Ident,
207 LBrace: T.getOpenLocation(), AttrList: attrs, UsingDecl&: ImplicitUsingDirectiveDecl, IsNested: false);
208
209 PrettyDeclStackTraceEntry CrashInfo(Actions.Context, NamespcDecl,
210 NamespaceLoc, "parsing namespace");
211
212 // Parse the contents of the namespace. This includes parsing recovery on
213 // any improperly nested namespaces.
214 ParseInnerNamespace(InnerNSs: ExtraNSs, index: 0, InlineLoc, attrs, Tracker&: T);
215
216 // Leave the namespace scope.
217 NamespaceScope.Exit();
218
219 DeclEnd = T.getCloseLocation();
220 Actions.ActOnFinishNamespaceDef(Dcl: NamespcDecl, RBrace: DeclEnd);
221
222 return Actions.ConvertDeclToDeclGroup(NamespcDecl,
223 ImplicitUsingDirectiveDecl);
224}
225
226void Parser::ParseInnerNamespace(const InnerNamespaceInfoList &InnerNSs,
227 unsigned int index, SourceLocation &InlineLoc,
228 ParsedAttributes &attrs,
229 BalancedDelimiterTracker &Tracker) {
230 if (index == InnerNSs.size()) {
231 while (!tryParseMisplacedModuleImport() && Tok.isNot(K: tok::r_brace) &&
232 Tok.isNot(K: tok::eof)) {
233 ParsedAttributes DeclAttrs(AttrFactory);
234 MaybeParseCXX11Attributes(Attrs&: DeclAttrs);
235 ParsedAttributes EmptyDeclSpecAttrs(AttrFactory);
236 ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs&: EmptyDeclSpecAttrs);
237 }
238
239 // The caller is what called check -- we are simply calling
240 // the close for it.
241 Tracker.consumeClose();
242
243 return;
244 }
245
246 // Handle a nested namespace definition.
247 // FIXME: Preserve the source information through to the AST rather than
248 // desugaring it here.
249 ParseScope NamespaceScope(this, Scope::DeclScope);
250 UsingDirectiveDecl *ImplicitUsingDirectiveDecl = nullptr;
251 Decl *NamespcDecl = Actions.ActOnStartNamespaceDef(
252 S: getCurScope(), InlineLoc: InnerNSs[index].InlineLoc, NamespaceLoc: InnerNSs[index].NamespaceLoc,
253 IdentLoc: InnerNSs[index].IdentLoc, Ident: InnerNSs[index].Ident,
254 LBrace: Tracker.getOpenLocation(), AttrList: attrs, UsingDecl&: ImplicitUsingDirectiveDecl, IsNested: true);
255 assert(!ImplicitUsingDirectiveDecl &&
256 "nested namespace definition cannot define anonymous namespace");
257
258 ParseInnerNamespace(InnerNSs, index: ++index, InlineLoc, attrs, Tracker);
259
260 NamespaceScope.Exit();
261 Actions.ActOnFinishNamespaceDef(Dcl: NamespcDecl, RBrace: Tracker.getCloseLocation());
262}
263
264Decl *Parser::ParseNamespaceAlias(SourceLocation NamespaceLoc,
265 SourceLocation AliasLoc,
266 IdentifierInfo *Alias,
267 SourceLocation &DeclEnd) {
268 assert(Tok.is(tok::equal) && "Not equal token");
269
270 ConsumeToken(); // eat the '='.
271
272 if (Tok.is(K: tok::code_completion)) {
273 cutOffParsing();
274 Actions.CodeCompletion().CodeCompleteNamespaceAliasDecl(S: getCurScope());
275 return nullptr;
276 }
277
278 CXXScopeSpec SS;
279 // Parse (optional) nested-name-specifier.
280 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
281 /*ObjectHasErrors=*/false,
282 /*EnteringContext=*/false,
283 /*MayBePseudoDestructor=*/nullptr,
284 /*IsTypename=*/false,
285 /*LastII=*/nullptr,
286 /*OnlyNamespace=*/true);
287
288 if (Tok.isNot(K: tok::identifier)) {
289 Diag(Tok, diag::err_expected_namespace_name);
290 // Skip to end of the definition and eat the ';'.
291 SkipUntil(T: tok::semi);
292 return nullptr;
293 }
294
295 if (SS.isInvalid()) {
296 // Diagnostics have been emitted in ParseOptionalCXXScopeSpecifier.
297 // Skip to end of the definition and eat the ';'.
298 SkipUntil(T: tok::semi);
299 return nullptr;
300 }
301
302 // Parse identifier.
303 IdentifierInfo *Ident = Tok.getIdentifierInfo();
304 SourceLocation IdentLoc = ConsumeToken();
305
306 // Eat the ';'.
307 DeclEnd = Tok.getLocation();
308 if (ExpectAndConsume(tok::semi, diag::err_expected_semi_after_namespace_name))
309 SkipUntil(T: tok::semi);
310
311 return Actions.ActOnNamespaceAliasDef(CurScope: getCurScope(), NamespaceLoc, AliasLoc,
312 Alias, SS, IdentLoc, Ident);
313}
314
315Decl *Parser::ParseLinkage(ParsingDeclSpec &DS, DeclaratorContext Context) {
316 assert(isTokenStringLiteral() && "Not a string literal!");
317 ExprResult Lang = ParseUnevaluatedStringLiteralExpression();
318
319 ParseScope LinkageScope(this, Scope::DeclScope);
320 Decl *LinkageSpec =
321 Lang.isInvalid()
322 ? nullptr
323 : Actions.ActOnStartLinkageSpecification(
324 S: getCurScope(), ExternLoc: DS.getSourceRange().getBegin(), LangStr: Lang.get(),
325 LBraceLoc: Tok.is(K: tok::l_brace) ? Tok.getLocation() : SourceLocation());
326
327 ParsedAttributes DeclAttrs(AttrFactory);
328 ParsedAttributes DeclSpecAttrs(AttrFactory);
329
330 while (MaybeParseCXX11Attributes(Attrs&: DeclAttrs) ||
331 MaybeParseGNUAttributes(Attrs&: DeclSpecAttrs))
332 ;
333
334 if (Tok.isNot(K: tok::l_brace)) {
335 // Reset the source range in DS, as the leading "extern"
336 // does not really belong to the inner declaration ...
337 DS.SetRangeStart(SourceLocation());
338 DS.SetRangeEnd(SourceLocation());
339 // ... but anyway remember that such an "extern" was seen.
340 DS.setExternInLinkageSpec(true);
341 ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs, DS: &DS);
342 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
343 S: getCurScope(), LinkageSpec, RBraceLoc: SourceLocation())
344 : nullptr;
345 }
346
347 DS.abort();
348
349 ProhibitAttributes(Attrs&: DeclAttrs);
350
351 BalancedDelimiterTracker T(*this, tok::l_brace);
352 T.consumeOpen();
353
354 unsigned NestedModules = 0;
355 while (true) {
356 switch (Tok.getKind()) {
357 case tok::annot_module_begin:
358 ++NestedModules;
359 ParseTopLevelDecl();
360 continue;
361
362 case tok::annot_module_end:
363 if (!NestedModules)
364 break;
365 --NestedModules;
366 ParseTopLevelDecl();
367 continue;
368
369 case tok::annot_module_include:
370 ParseTopLevelDecl();
371 continue;
372
373 case tok::eof:
374 break;
375
376 case tok::r_brace:
377 if (!NestedModules)
378 break;
379 [[fallthrough]];
380 default:
381 ParsedAttributes DeclAttrs(AttrFactory);
382 ParsedAttributes DeclSpecAttrs(AttrFactory);
383 while (MaybeParseCXX11Attributes(Attrs&: DeclAttrs) ||
384 MaybeParseGNUAttributes(Attrs&: DeclSpecAttrs))
385 ;
386 ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs);
387 continue;
388 }
389
390 break;
391 }
392
393 T.consumeClose();
394 return LinkageSpec ? Actions.ActOnFinishLinkageSpecification(
395 S: getCurScope(), LinkageSpec, RBraceLoc: T.getCloseLocation())
396 : nullptr;
397}
398
399Decl *Parser::ParseExportDeclaration() {
400 assert(Tok.is(tok::kw_export));
401 SourceLocation ExportLoc = ConsumeToken();
402
403 if (Tok.is(K: tok::code_completion)) {
404 cutOffParsing();
405 Actions.CodeCompletion().CodeCompleteOrdinaryName(
406 S: getCurScope(), CompletionContext: PP.isIncrementalProcessingEnabled()
407 ? SemaCodeCompletion::PCC_TopLevelOrExpression
408 : SemaCodeCompletion::PCC_Namespace);
409 return nullptr;
410 }
411
412 ParseScope ExportScope(this, Scope::DeclScope);
413 Decl *ExportDecl = Actions.ActOnStartExportDecl(
414 S: getCurScope(), ExportLoc,
415 LBraceLoc: Tok.is(K: tok::l_brace) ? Tok.getLocation() : SourceLocation());
416
417 if (Tok.isNot(K: tok::l_brace)) {
418 // FIXME: Factor out a ParseExternalDeclarationWithAttrs.
419 ParsedAttributes DeclAttrs(AttrFactory);
420 MaybeParseCXX11Attributes(Attrs&: DeclAttrs);
421 ParsedAttributes EmptyDeclSpecAttrs(AttrFactory);
422 ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs&: EmptyDeclSpecAttrs);
423 return Actions.ActOnFinishExportDecl(S: getCurScope(), ExportDecl,
424 RBraceLoc: SourceLocation());
425 }
426
427 BalancedDelimiterTracker T(*this, tok::l_brace);
428 T.consumeOpen();
429
430 while (!tryParseMisplacedModuleImport() && Tok.isNot(K: tok::r_brace) &&
431 Tok.isNot(K: tok::eof)) {
432 ParsedAttributes DeclAttrs(AttrFactory);
433 MaybeParseCXX11Attributes(Attrs&: DeclAttrs);
434 ParsedAttributes EmptyDeclSpecAttrs(AttrFactory);
435 ParseExternalDeclaration(DeclAttrs, DeclSpecAttrs&: EmptyDeclSpecAttrs);
436 }
437
438 T.consumeClose();
439 return Actions.ActOnFinishExportDecl(S: getCurScope(), ExportDecl,
440 RBraceLoc: T.getCloseLocation());
441}
442
443Parser::DeclGroupPtrTy Parser::ParseUsingDirectiveOrDeclaration(
444 DeclaratorContext Context, const ParsedTemplateInfo &TemplateInfo,
445 SourceLocation &DeclEnd, ParsedAttributes &Attrs) {
446 assert(Tok.is(tok::kw_using) && "Not using token");
447 ObjCDeclContextSwitch ObjCDC(*this);
448
449 // Eat 'using'.
450 SourceLocation UsingLoc = ConsumeToken();
451
452 if (Tok.is(K: tok::code_completion)) {
453 cutOffParsing();
454 Actions.CodeCompletion().CodeCompleteUsing(S: getCurScope());
455 return nullptr;
456 }
457
458 // Consume unexpected 'template' keywords.
459 while (Tok.is(K: tok::kw_template)) {
460 SourceLocation TemplateLoc = ConsumeToken();
461 Diag(TemplateLoc, diag::err_unexpected_template_after_using)
462 << FixItHint::CreateRemoval(TemplateLoc);
463 }
464
465 // 'using namespace' means this is a using-directive.
466 if (Tok.is(K: tok::kw_namespace)) {
467 // Template parameters are always an error here.
468 if (TemplateInfo.Kind != ParsedTemplateKind::NonTemplate) {
469 SourceRange R = TemplateInfo.getSourceRange();
470 Diag(UsingLoc, diag::err_templated_using_directive_declaration)
471 << 0 /* directive */ << R << FixItHint::CreateRemoval(R);
472 }
473
474 Decl *UsingDir = ParseUsingDirective(Context, UsingLoc, DeclEnd, attrs&: Attrs);
475 return Actions.ConvertDeclToDeclGroup(Ptr: UsingDir);
476 }
477
478 // Otherwise, it must be a using-declaration or an alias-declaration.
479 return ParseUsingDeclaration(Context, TemplateInfo, UsingLoc, DeclEnd, Attrs,
480 AS: AS_none);
481}
482
483Decl *Parser::ParseUsingDirective(DeclaratorContext Context,
484 SourceLocation UsingLoc,
485 SourceLocation &DeclEnd,
486 ParsedAttributes &attrs) {
487 assert(Tok.is(tok::kw_namespace) && "Not 'namespace' token");
488
489 // Eat 'namespace'.
490 SourceLocation NamespcLoc = ConsumeToken();
491
492 if (Tok.is(K: tok::code_completion)) {
493 cutOffParsing();
494 Actions.CodeCompletion().CodeCompleteUsingDirective(S: getCurScope());
495 return nullptr;
496 }
497
498 CXXScopeSpec SS;
499 // Parse (optional) nested-name-specifier.
500 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
501 /*ObjectHasErrors=*/false,
502 /*EnteringContext=*/false,
503 /*MayBePseudoDestructor=*/nullptr,
504 /*IsTypename=*/false,
505 /*LastII=*/nullptr,
506 /*OnlyNamespace=*/true);
507
508 IdentifierInfo *NamespcName = nullptr;
509 SourceLocation IdentLoc = SourceLocation();
510
511 // Parse namespace-name.
512 if (Tok.isNot(K: tok::identifier)) {
513 Diag(Tok, diag::err_expected_namespace_name);
514 // If there was invalid namespace name, skip to end of decl, and eat ';'.
515 SkipUntil(T: tok::semi);
516 // FIXME: Are there cases, when we would like to call ActOnUsingDirective?
517 return nullptr;
518 }
519
520 if (SS.isInvalid()) {
521 // Diagnostics have been emitted in ParseOptionalCXXScopeSpecifier.
522 // Skip to end of the definition and eat the ';'.
523 SkipUntil(T: tok::semi);
524 return nullptr;
525 }
526
527 // Parse identifier.
528 NamespcName = Tok.getIdentifierInfo();
529 IdentLoc = ConsumeToken();
530
531 // Parse (optional) attributes (most likely GNU strong-using extension).
532 bool GNUAttr = false;
533 if (Tok.is(K: tok::kw___attribute)) {
534 GNUAttr = true;
535 ParseGNUAttributes(Attrs&: attrs);
536 }
537
538 // Eat ';'.
539 DeclEnd = Tok.getLocation();
540 if (ExpectAndConsume(tok::semi,
541 GNUAttr ? diag::err_expected_semi_after_attribute_list
542 : diag::err_expected_semi_after_namespace_name))
543 SkipUntil(T: tok::semi);
544
545 return Actions.ActOnUsingDirective(CurScope: getCurScope(), UsingLoc, NamespcLoc, SS,
546 IdentLoc, NamespcName, AttrList: attrs);
547}
548
549bool Parser::ParseUsingDeclarator(DeclaratorContext Context,
550 UsingDeclarator &D) {
551 D.clear();
552
553 // Ignore optional 'typename'.
554 // FIXME: This is wrong; we should parse this as a typename-specifier.
555 TryConsumeToken(Expected: tok::kw_typename, Loc&: D.TypenameLoc);
556
557 if (Tok.is(K: tok::kw___super)) {
558 Diag(Tok.getLocation(), diag::err_super_in_using_declaration);
559 return true;
560 }
561
562 // Parse nested-name-specifier.
563 const IdentifierInfo *LastII = nullptr;
564 if (ParseOptionalCXXScopeSpecifier(SS&: D.SS, /*ObjectType=*/nullptr,
565 /*ObjectHasErrors=*/false,
566 /*EnteringContext=*/false,
567 /*MayBePseudoDtor=*/MayBePseudoDestructor: nullptr,
568 /*IsTypename=*/false,
569 /*LastII=*/&LastII,
570 /*OnlyNamespace=*/false,
571 /*InUsingDeclaration=*/true))
572
573 return true;
574 if (D.SS.isInvalid())
575 return true;
576
577 // Parse the unqualified-id. We allow parsing of both constructor and
578 // destructor names and allow the action module to diagnose any semantic
579 // errors.
580 //
581 // C++11 [class.qual]p2:
582 // [...] in a using-declaration that is a member-declaration, if the name
583 // specified after the nested-name-specifier is the same as the identifier
584 // or the simple-template-id's template-name in the last component of the
585 // nested-name-specifier, the name is [...] considered to name the
586 // constructor.
587 if (getLangOpts().CPlusPlus11 && Context == DeclaratorContext::Member &&
588 Tok.is(K: tok::identifier) &&
589 (NextToken().is(K: tok::semi) || NextToken().is(K: tok::comma) ||
590 NextToken().is(K: tok::ellipsis) || NextToken().is(K: tok::l_square) ||
591 NextToken().isRegularKeywordAttribute() ||
592 NextToken().is(K: tok::kw___attribute)) &&
593 D.SS.isNotEmpty() && LastII == Tok.getIdentifierInfo() &&
594 !D.SS.getScopeRep()->getAsNamespace() &&
595 !D.SS.getScopeRep()->getAsNamespaceAlias()) {
596 SourceLocation IdLoc = ConsumeToken();
597 ParsedType Type =
598 Actions.getInheritingConstructorName(SS&: D.SS, NameLoc: IdLoc, Name: *LastII);
599 D.Name.setConstructorName(ClassType: Type, ClassNameLoc: IdLoc, EndLoc: IdLoc);
600 } else {
601 if (ParseUnqualifiedId(
602 SS&: D.SS, /*ObjectType=*/nullptr,
603 /*ObjectHadErrors=*/false, /*EnteringContext=*/false,
604 /*AllowDestructorName=*/true,
605 /*AllowConstructorName=*/
606 !(Tok.is(K: tok::identifier) && NextToken().is(K: tok::equal)),
607 /*AllowDeductionGuide=*/false, TemplateKWLoc: nullptr, Result&: D.Name))
608 return true;
609 }
610
611 if (TryConsumeToken(tok::ellipsis, D.EllipsisLoc))
612 Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
613 ? diag::warn_cxx17_compat_using_declaration_pack
614 : diag::ext_using_declaration_pack);
615
616 return false;
617}
618
619Parser::DeclGroupPtrTy Parser::ParseUsingDeclaration(
620 DeclaratorContext Context, const ParsedTemplateInfo &TemplateInfo,
621 SourceLocation UsingLoc, SourceLocation &DeclEnd,
622 ParsedAttributes &PrefixAttrs, AccessSpecifier AS) {
623 SourceLocation UELoc;
624 bool InInitStatement = Context == DeclaratorContext::SelectionInit ||
625 Context == DeclaratorContext::ForInit;
626
627 if (TryConsumeToken(Expected: tok::kw_enum, Loc&: UELoc) && !InInitStatement) {
628 // C++20 using-enum
629 Diag(UELoc, getLangOpts().CPlusPlus20
630 ? diag::warn_cxx17_compat_using_enum_declaration
631 : diag::ext_using_enum_declaration);
632
633 DiagnoseCXX11AttributeExtension(Attrs&: PrefixAttrs);
634
635 if (TemplateInfo.Kind != ParsedTemplateKind::NonTemplate) {
636 SourceRange R = TemplateInfo.getSourceRange();
637 Diag(UsingLoc, diag::err_templated_using_directive_declaration)
638 << 1 /* declaration */ << R << FixItHint::CreateRemoval(R);
639 SkipUntil(T: tok::semi);
640 return nullptr;
641 }
642 CXXScopeSpec SS;
643 if (ParseOptionalCXXScopeSpecifier(SS, /*ParsedType=*/ObjectType: nullptr,
644 /*ObectHasErrors=*/ObjectHasErrors: false,
645 /*EnteringConttext=*/EnteringContext: false,
646 /*MayBePseudoDestructor=*/nullptr,
647 /*IsTypename=*/true,
648 /*IdentifierInfo=*/LastII: nullptr,
649 /*OnlyNamespace=*/false,
650 /*InUsingDeclaration=*/true)) {
651 SkipUntil(T: tok::semi);
652 return nullptr;
653 }
654
655 if (Tok.is(K: tok::code_completion)) {
656 cutOffParsing();
657 Actions.CodeCompletion().CodeCompleteUsing(S: getCurScope());
658 return nullptr;
659 }
660
661 Decl *UED = nullptr;
662
663 // FIXME: identifier and annot_template_id handling is very similar to
664 // ParseBaseTypeSpecifier. It should be factored out into a function.
665 if (Tok.is(K: tok::identifier)) {
666 IdentifierInfo *IdentInfo = Tok.getIdentifierInfo();
667 SourceLocation IdentLoc = ConsumeToken();
668
669 ParsedType Type = Actions.getTypeName(
670 II: *IdentInfo, NameLoc: IdentLoc, S: getCurScope(), SS: &SS, /*isClassName=*/true,
671 /*HasTrailingDot=*/false,
672 /*ObjectType=*/nullptr, /*IsCtorOrDtorName=*/false,
673 /*WantNontrivialTypeSourceInfo=*/true);
674
675 UED = Actions.ActOnUsingEnumDeclaration(
676 CurScope: getCurScope(), AS, UsingLoc, EnumLoc: UELoc, TyLoc: IdentLoc, II: *IdentInfo, Ty: Type, SS: &SS);
677 } else if (Tok.is(K: tok::annot_template_id)) {
678 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(tok: Tok);
679
680 if (TemplateId->mightBeType()) {
681 AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename: ImplicitTypenameContext::No,
682 /*IsClassName=*/true);
683
684 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
685 TypeResult Type = getTypeAnnotation(Tok);
686 SourceRange Loc = Tok.getAnnotationRange();
687 ConsumeAnnotationToken();
688
689 UED = Actions.ActOnUsingEnumDeclaration(CurScope: getCurScope(), AS, UsingLoc,
690 EnumLoc: UELoc, TyLoc: Loc, II: *TemplateId->Name,
691 Ty: Type.get(), SS: &SS);
692 } else {
693 Diag(Tok.getLocation(), diag::err_using_enum_not_enum)
694 << TemplateId->Name->getName()
695 << SourceRange(TemplateId->TemplateNameLoc, TemplateId->RAngleLoc);
696 }
697 } else {
698 Diag(Tok.getLocation(), diag::err_using_enum_expect_identifier)
699 << Tok.is(tok::kw_enum);
700 SkipUntil(T: tok::semi);
701 return nullptr;
702 }
703
704 if (!UED) {
705 SkipUntil(T: tok::semi);
706 return nullptr;
707 }
708
709 DeclEnd = Tok.getLocation();
710 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
711 "using-enum declaration"))
712 SkipUntil(T: tok::semi);
713
714 return Actions.ConvertDeclToDeclGroup(Ptr: UED);
715 }
716
717 // Check for misplaced attributes before the identifier in an
718 // alias-declaration.
719 ParsedAttributes MisplacedAttrs(AttrFactory);
720 MaybeParseCXX11Attributes(Attrs&: MisplacedAttrs);
721
722 if (InInitStatement && Tok.isNot(K: tok::identifier))
723 return nullptr;
724
725 UsingDeclarator D;
726 bool InvalidDeclarator = ParseUsingDeclarator(Context, D);
727
728 ParsedAttributes Attrs(AttrFactory);
729 MaybeParseAttributes(WhichAttrKinds: PAKM_GNU | PAKM_CXX11, Attrs);
730
731 // If we had any misplaced attributes from earlier, this is where they
732 // should have been written.
733 if (MisplacedAttrs.Range.isValid()) {
734 auto *FirstAttr =
735 MisplacedAttrs.empty() ? nullptr : &MisplacedAttrs.front();
736 auto &Range = MisplacedAttrs.Range;
737 (FirstAttr && FirstAttr->isRegularKeywordAttribute()
738 ? Diag(Range.getBegin(), diag::err_keyword_not_allowed) << FirstAttr
739 : Diag(Range.getBegin(), diag::err_attributes_not_allowed))
740 << FixItHint::CreateInsertionFromRange(
741 Tok.getLocation(), CharSourceRange::getTokenRange(Range))
742 << FixItHint::CreateRemoval(Range);
743 Attrs.takeAllFrom(Other&: MisplacedAttrs);
744 }
745
746 // Maybe this is an alias-declaration.
747 if (Tok.is(K: tok::equal) || InInitStatement) {
748 if (InvalidDeclarator) {
749 SkipUntil(T: tok::semi);
750 return nullptr;
751 }
752
753 ProhibitAttributes(Attrs&: PrefixAttrs);
754
755 Decl *DeclFromDeclSpec = nullptr;
756 Scope *CurScope = getCurScope();
757 if (CurScope)
758 CurScope->setFlags(Scope::ScopeFlags::TypeAliasScope |
759 CurScope->getFlags());
760
761 Decl *AD = ParseAliasDeclarationAfterDeclarator(
762 TemplateInfo, UsingLoc, D, DeclEnd, AS, Attrs, OwnedType: &DeclFromDeclSpec);
763 return Actions.ConvertDeclToDeclGroup(Ptr: AD, OwnedType: DeclFromDeclSpec);
764 }
765
766 DiagnoseCXX11AttributeExtension(Attrs&: PrefixAttrs);
767
768 // Diagnose an attempt to declare a templated using-declaration.
769 // In C++11, alias-declarations can be templates:
770 // template <...> using id = type;
771 if (TemplateInfo.Kind != ParsedTemplateKind::NonTemplate) {
772 SourceRange R = TemplateInfo.getSourceRange();
773 Diag(UsingLoc, diag::err_templated_using_directive_declaration)
774 << 1 /* declaration */ << R << FixItHint::CreateRemoval(R);
775
776 // Unfortunately, we have to bail out instead of recovering by
777 // ignoring the parameters, just in case the nested name specifier
778 // depends on the parameters.
779 return nullptr;
780 }
781
782 SmallVector<Decl *, 8> DeclsInGroup;
783 while (true) {
784 // Parse (optional) attributes.
785 MaybeParseAttributes(WhichAttrKinds: PAKM_GNU | PAKM_CXX11, Attrs);
786 DiagnoseCXX11AttributeExtension(Attrs);
787 Attrs.addAll(B: PrefixAttrs.begin(), E: PrefixAttrs.end());
788
789 if (InvalidDeclarator)
790 SkipUntil(T1: tok::comma, T2: tok::semi, Flags: StopBeforeMatch);
791 else {
792 // "typename" keyword is allowed for identifiers only,
793 // because it may be a type definition.
794 if (D.TypenameLoc.isValid() &&
795 D.Name.getKind() != UnqualifiedIdKind::IK_Identifier) {
796 Diag(D.Name.getSourceRange().getBegin(),
797 diag::err_typename_identifiers_only)
798 << FixItHint::CreateRemoval(SourceRange(D.TypenameLoc));
799 // Proceed parsing, but discard the typename keyword.
800 D.TypenameLoc = SourceLocation();
801 }
802
803 Decl *UD = Actions.ActOnUsingDeclaration(CurScope: getCurScope(), AS, UsingLoc,
804 TypenameLoc: D.TypenameLoc, SS&: D.SS, Name&: D.Name,
805 EllipsisLoc: D.EllipsisLoc, AttrList: Attrs);
806 if (UD)
807 DeclsInGroup.push_back(Elt: UD);
808 }
809
810 if (!TryConsumeToken(Expected: tok::comma))
811 break;
812
813 // Parse another using-declarator.
814 Attrs.clear();
815 InvalidDeclarator = ParseUsingDeclarator(Context, D);
816 }
817
818 if (DeclsInGroup.size() > 1)
819 Diag(Tok.getLocation(),
820 getLangOpts().CPlusPlus17
821 ? diag::warn_cxx17_compat_multi_using_declaration
822 : diag::ext_multi_using_declaration);
823
824 // Eat ';'.
825 DeclEnd = Tok.getLocation();
826 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
827 !Attrs.empty() ? "attributes list"
828 : UELoc.isValid() ? "using-enum declaration"
829 : "using declaration"))
830 SkipUntil(T: tok::semi);
831
832 return Actions.BuildDeclaratorGroup(Group: DeclsInGroup);
833}
834
835Decl *Parser::ParseAliasDeclarationAfterDeclarator(
836 const ParsedTemplateInfo &TemplateInfo, SourceLocation UsingLoc,
837 UsingDeclarator &D, SourceLocation &DeclEnd, AccessSpecifier AS,
838 ParsedAttributes &Attrs, Decl **OwnedType) {
839 if (ExpectAndConsume(ExpectedTok: tok::equal)) {
840 SkipUntil(T: tok::semi);
841 return nullptr;
842 }
843
844 Diag(Tok.getLocation(), getLangOpts().CPlusPlus11
845 ? diag::warn_cxx98_compat_alias_declaration
846 : diag::ext_alias_declaration);
847
848 // Type alias templates cannot be specialized.
849 int SpecKind = -1;
850 if (TemplateInfo.Kind == ParsedTemplateKind::Template &&
851 D.Name.getKind() == UnqualifiedIdKind::IK_TemplateId)
852 SpecKind = 0;
853 if (TemplateInfo.Kind == ParsedTemplateKind::ExplicitSpecialization)
854 SpecKind = 1;
855 if (TemplateInfo.Kind == ParsedTemplateKind::ExplicitInstantiation)
856 SpecKind = 2;
857 if (SpecKind != -1) {
858 SourceRange Range;
859 if (SpecKind == 0)
860 Range = SourceRange(D.Name.TemplateId->LAngleLoc,
861 D.Name.TemplateId->RAngleLoc);
862 else
863 Range = TemplateInfo.getSourceRange();
864 Diag(Range.getBegin(), diag::err_alias_declaration_specialization)
865 << SpecKind << Range;
866 SkipUntil(T: tok::semi);
867 return nullptr;
868 }
869
870 // Name must be an identifier.
871 if (D.Name.getKind() != UnqualifiedIdKind::IK_Identifier) {
872 Diag(D.Name.StartLocation, diag::err_alias_declaration_not_identifier);
873 // No removal fixit: can't recover from this.
874 SkipUntil(T: tok::semi);
875 return nullptr;
876 } else if (D.TypenameLoc.isValid())
877 Diag(D.TypenameLoc, diag::err_alias_declaration_not_identifier)
878 << FixItHint::CreateRemoval(
879 SourceRange(D.TypenameLoc, D.SS.isNotEmpty() ? D.SS.getEndLoc()
880 : D.TypenameLoc));
881 else if (D.SS.isNotEmpty())
882 Diag(D.SS.getBeginLoc(), diag::err_alias_declaration_not_identifier)
883 << FixItHint::CreateRemoval(D.SS.getRange());
884 if (D.EllipsisLoc.isValid())
885 Diag(D.EllipsisLoc, diag::err_alias_declaration_pack_expansion)
886 << FixItHint::CreateRemoval(SourceRange(D.EllipsisLoc));
887
888 Decl *DeclFromDeclSpec = nullptr;
889 TypeResult TypeAlias =
890 ParseTypeName(Range: nullptr,
891 Context: TemplateInfo.Kind != ParsedTemplateKind::NonTemplate ? DeclaratorContext::AliasTemplate
892 : DeclaratorContext::AliasDecl,
893 AS, OwnedType: &DeclFromDeclSpec, Attrs: &Attrs);
894 if (OwnedType)
895 *OwnedType = DeclFromDeclSpec;
896
897 // Eat ';'.
898 DeclEnd = Tok.getLocation();
899 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
900 !Attrs.empty() ? "attributes list"
901 : "alias declaration"))
902 SkipUntil(T: tok::semi);
903
904 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
905 MultiTemplateParamsArg TemplateParamsArg(
906 TemplateParams ? TemplateParams->data() : nullptr,
907 TemplateParams ? TemplateParams->size() : 0);
908 return Actions.ActOnAliasDeclaration(CurScope: getCurScope(), AS, TemplateParams: TemplateParamsArg,
909 UsingLoc, Name&: D.Name, AttrList: Attrs, Type: TypeAlias,
910 DeclFromDeclSpec);
911}
912
913static FixItHint getStaticAssertNoMessageFixIt(const Expr *AssertExpr,
914 SourceLocation EndExprLoc) {
915 if (const auto *BO = dyn_cast_or_null<BinaryOperator>(Val: AssertExpr)) {
916 if (BO->getOpcode() == BO_LAnd &&
917 isa<StringLiteral>(Val: BO->getRHS()->IgnoreImpCasts()))
918 return FixItHint::CreateReplacement(RemoveRange: BO->getOperatorLoc(), Code: ",");
919 }
920 return FixItHint::CreateInsertion(InsertionLoc: EndExprLoc, Code: ", \"\"");
921}
922
923Decl *Parser::ParseStaticAssertDeclaration(SourceLocation &DeclEnd) {
924 assert(Tok.isOneOf(tok::kw_static_assert, tok::kw__Static_assert) &&
925 "Not a static_assert declaration");
926
927 // Save the token name used for static assertion.
928 const char *TokName = Tok.getName();
929
930 if (Tok.is(K: tok::kw__Static_assert))
931 diagnoseUseOfC11Keyword(Tok);
932 else if (Tok.is(K: tok::kw_static_assert)) {
933 if (!getLangOpts().CPlusPlus) {
934 if (getLangOpts().C23)
935 Diag(Tok, diag::warn_c23_compat_keyword) << Tok.getName();
936 else
937 Diag(Tok, diag::ext_ms_static_assert) << FixItHint::CreateReplacement(
938 Tok.getLocation(), "_Static_assert");
939 } else
940 Diag(Tok, diag::warn_cxx98_compat_static_assert);
941 }
942
943 SourceLocation StaticAssertLoc = ConsumeToken();
944
945 BalancedDelimiterTracker T(*this, tok::l_paren);
946 if (T.consumeOpen()) {
947 Diag(Tok, diag::err_expected) << tok::l_paren;
948 SkipMalformedDecl();
949 return nullptr;
950 }
951
952 EnterExpressionEvaluationContext ConstantEvaluated(
953 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
954 ExprResult AssertExpr(ParseConstantExpressionInExprEvalContext());
955 if (AssertExpr.isInvalid()) {
956 SkipMalformedDecl();
957 return nullptr;
958 }
959
960 ExprResult AssertMessage;
961 if (Tok.is(K: tok::r_paren)) {
962 unsigned DiagVal;
963 if (getLangOpts().CPlusPlus17)
964 DiagVal = diag::warn_cxx14_compat_static_assert_no_message;
965 else if (getLangOpts().CPlusPlus)
966 DiagVal = diag::ext_cxx_static_assert_no_message;
967 else if (getLangOpts().C23)
968 DiagVal = diag::warn_c17_compat_static_assert_no_message;
969 else
970 DiagVal = diag::ext_c_static_assert_no_message;
971 Diag(Tok, DiagID: DiagVal) << getStaticAssertNoMessageFixIt(AssertExpr: AssertExpr.get(),
972 EndExprLoc: Tok.getLocation());
973 } else {
974 if (ExpectAndConsume(ExpectedTok: tok::comma)) {
975 SkipUntil(T: tok::semi);
976 return nullptr;
977 }
978
979 bool ParseAsExpression = false;
980 if (getLangOpts().CPlusPlus11) {
981 for (unsigned I = 0;; ++I) {
982 const Token &T = GetLookAheadToken(N: I);
983 if (T.is(K: tok::r_paren))
984 break;
985 if (!tokenIsLikeStringLiteral(Tok: T, LO: getLangOpts()) || T.hasUDSuffix()) {
986 ParseAsExpression = true;
987 break;
988 }
989 }
990 }
991
992 if (ParseAsExpression) {
993 Diag(Tok,
994 getLangOpts().CPlusPlus26
995 ? diag::warn_cxx20_compat_static_assert_user_generated_message
996 : diag::ext_cxx_static_assert_user_generated_message);
997 AssertMessage = ParseConstantExpressionInExprEvalContext();
998 } else if (tokenIsLikeStringLiteral(Tok, LO: getLangOpts()))
999 AssertMessage = ParseUnevaluatedStringLiteralExpression();
1000 else {
1001 Diag(Tok, diag::err_expected_string_literal)
1002 << /*Source='static_assert'*/ 1;
1003 SkipMalformedDecl();
1004 return nullptr;
1005 }
1006
1007 if (AssertMessage.isInvalid()) {
1008 SkipMalformedDecl();
1009 return nullptr;
1010 }
1011 }
1012
1013 if (T.consumeClose())
1014 return nullptr;
1015
1016 DeclEnd = Tok.getLocation();
1017 ExpectAndConsumeSemi(diag::err_expected_semi_after_static_assert, TokName);
1018
1019 return Actions.ActOnStaticAssertDeclaration(StaticAssertLoc, AssertExpr: AssertExpr.get(),
1020 AssertMessageExpr: AssertMessage.get(),
1021 RParenLoc: T.getCloseLocation());
1022}
1023
1024SourceLocation Parser::ParseDecltypeSpecifier(DeclSpec &DS) {
1025 assert(Tok.isOneOf(tok::kw_decltype, tok::annot_decltype) &&
1026 "Not a decltype specifier");
1027
1028 ExprResult Result;
1029 SourceLocation StartLoc = Tok.getLocation();
1030 SourceLocation EndLoc;
1031
1032 if (Tok.is(K: tok::annot_decltype)) {
1033 Result = getExprAnnotation(Tok);
1034 EndLoc = Tok.getAnnotationEndLoc();
1035 // Unfortunately, we don't know the LParen source location as the annotated
1036 // token doesn't have it.
1037 DS.setTypeArgumentRange(SourceRange(SourceLocation(), EndLoc));
1038 ConsumeAnnotationToken();
1039 if (Result.isInvalid()) {
1040 DS.SetTypeSpecError();
1041 return EndLoc;
1042 }
1043 } else {
1044 if (Tok.getIdentifierInfo()->isStr("decltype"))
1045 Diag(Tok, diag::warn_cxx98_compat_decltype);
1046
1047 ConsumeToken();
1048
1049 BalancedDelimiterTracker T(*this, tok::l_paren);
1050 if (T.expectAndConsume(diag::err_expected_lparen_after, "decltype",
1051 tok::r_paren)) {
1052 DS.SetTypeSpecError();
1053 return T.getOpenLocation() == Tok.getLocation() ? StartLoc
1054 : T.getOpenLocation();
1055 }
1056
1057 // Check for C++1y 'decltype(auto)'.
1058 if (Tok.is(K: tok::kw_auto) && NextToken().is(K: tok::r_paren)) {
1059 // the typename-specifier in a function-style cast expression may
1060 // be 'auto' since C++23.
1061 Diag(Tok.getLocation(),
1062 getLangOpts().CPlusPlus14
1063 ? diag::warn_cxx11_compat_decltype_auto_type_specifier
1064 : diag::ext_decltype_auto_type_specifier);
1065 ConsumeToken();
1066 } else {
1067 // Parse the expression
1068
1069 // C++11 [dcl.type.simple]p4:
1070 // The operand of the decltype specifier is an unevaluated operand.
1071 EnterExpressionEvaluationContext Unevaluated(
1072 Actions, Sema::ExpressionEvaluationContext::Unevaluated, nullptr,
1073 Sema::ExpressionEvaluationContextRecord::EK_Decltype);
1074 Result = Actions.CorrectDelayedTyposInExpr(
1075 ER: ParseExpression(), /*InitDecl=*/nullptr,
1076 /*RecoverUncorrectedTypos=*/false,
1077 Filter: [](Expr *E) { return E->hasPlaceholderType() ? ExprError() : E; });
1078 if (Result.isInvalid()) {
1079 DS.SetTypeSpecError();
1080 if (SkipUntil(T: tok::r_paren, Flags: StopAtSemi | StopBeforeMatch)) {
1081 EndLoc = ConsumeParen();
1082 } else {
1083 if (PP.isBacktrackEnabled() && Tok.is(K: tok::semi)) {
1084 // Backtrack to get the location of the last token before the semi.
1085 PP.RevertCachedTokens(N: 2);
1086 ConsumeToken(); // the semi.
1087 EndLoc = ConsumeAnyToken();
1088 assert(Tok.is(tok::semi));
1089 } else {
1090 EndLoc = Tok.getLocation();
1091 }
1092 }
1093 return EndLoc;
1094 }
1095
1096 Result = Actions.ActOnDecltypeExpression(E: Result.get());
1097 }
1098
1099 // Match the ')'
1100 T.consumeClose();
1101 DS.setTypeArgumentRange(T.getRange());
1102 if (T.getCloseLocation().isInvalid()) {
1103 DS.SetTypeSpecError();
1104 // FIXME: this should return the location of the last token
1105 // that was consumed (by "consumeClose()")
1106 return T.getCloseLocation();
1107 }
1108
1109 if (Result.isInvalid()) {
1110 DS.SetTypeSpecError();
1111 return T.getCloseLocation();
1112 }
1113
1114 EndLoc = T.getCloseLocation();
1115 }
1116 assert(!Result.isInvalid());
1117
1118 const char *PrevSpec = nullptr;
1119 unsigned DiagID;
1120 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
1121 // Check for duplicate type specifiers (e.g. "int decltype(a)").
1122 if (Result.get() ? DS.SetTypeSpecType(T: DeclSpec::TST_decltype, Loc: StartLoc,
1123 PrevSpec, DiagID, Rep: Result.get(), policy: Policy)
1124 : DS.SetTypeSpecType(T: DeclSpec::TST_decltype_auto, Loc: StartLoc,
1125 PrevSpec, DiagID, Policy)) {
1126 Diag(Loc: StartLoc, DiagID) << PrevSpec;
1127 DS.SetTypeSpecError();
1128 }
1129 return EndLoc;
1130}
1131
1132void Parser::AnnotateExistingDecltypeSpecifier(const DeclSpec &DS,
1133 SourceLocation StartLoc,
1134 SourceLocation EndLoc) {
1135 // make sure we have a token we can turn into an annotation token
1136 if (PP.isBacktrackEnabled()) {
1137 PP.RevertCachedTokens(N: 1);
1138 if (DS.getTypeSpecType() == TST_error) {
1139 // We encountered an error in parsing 'decltype(...)' so lets annotate all
1140 // the tokens in the backtracking cache - that we likely had to skip over
1141 // to get to a token that allows us to resume parsing, such as a
1142 // semi-colon.
1143 EndLoc = PP.getLastCachedTokenLocation();
1144 }
1145 } else
1146 PP.EnterToken(Tok, /*IsReinject*/ true);
1147
1148 Tok.setKind(tok::annot_decltype);
1149 setExprAnnotation(Tok,
1150 ER: DS.getTypeSpecType() == TST_decltype ? DS.getRepAsExpr()
1151 : DS.getTypeSpecType() == TST_decltype_auto ? ExprResult()
1152 : ExprError());
1153 Tok.setAnnotationEndLoc(EndLoc);
1154 Tok.setLocation(StartLoc);
1155 PP.AnnotateCachedTokens(Tok);
1156}
1157
1158SourceLocation Parser::ParsePackIndexingType(DeclSpec &DS) {
1159 assert(Tok.isOneOf(tok::annot_pack_indexing_type, tok::identifier) &&
1160 "Expected an identifier");
1161
1162 TypeResult Type;
1163 SourceLocation StartLoc;
1164 SourceLocation EllipsisLoc;
1165 const char *PrevSpec;
1166 unsigned DiagID;
1167 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
1168
1169 if (Tok.is(K: tok::annot_pack_indexing_type)) {
1170 StartLoc = Tok.getLocation();
1171 SourceLocation EndLoc;
1172 Type = getTypeAnnotation(Tok);
1173 EndLoc = Tok.getAnnotationEndLoc();
1174 // Unfortunately, we don't know the LParen source location as the annotated
1175 // token doesn't have it.
1176 DS.setTypeArgumentRange(SourceRange(SourceLocation(), EndLoc));
1177 ConsumeAnnotationToken();
1178 if (Type.isInvalid()) {
1179 DS.SetTypeSpecError();
1180 return EndLoc;
1181 }
1182 DS.SetTypeSpecType(T: DeclSpec::TST_typename_pack_indexing, Loc: StartLoc, PrevSpec,
1183 DiagID, Rep: Type, Policy);
1184 return EndLoc;
1185 }
1186 if (!NextToken().is(K: tok::ellipsis) ||
1187 !GetLookAheadToken(N: 2).is(K: tok::l_square)) {
1188 DS.SetTypeSpecError();
1189 return Tok.getEndLoc();
1190 }
1191
1192 ParsedType Ty = Actions.getTypeName(II: *Tok.getIdentifierInfo(),
1193 NameLoc: Tok.getLocation(), S: getCurScope());
1194 if (!Ty) {
1195 DS.SetTypeSpecError();
1196 return Tok.getEndLoc();
1197 }
1198 Type = Ty;
1199
1200 StartLoc = ConsumeToken();
1201 EllipsisLoc = ConsumeToken();
1202 BalancedDelimiterTracker T(*this, tok::l_square);
1203 T.consumeOpen();
1204 ExprResult IndexExpr = ParseConstantExpression();
1205 T.consumeClose();
1206
1207 DS.SetRangeStart(StartLoc);
1208 DS.SetRangeEnd(T.getCloseLocation());
1209
1210 if (!IndexExpr.isUsable()) {
1211 ASTContext &C = Actions.getASTContext();
1212 IndexExpr = IntegerLiteral::Create(C, V: C.MakeIntValue(Value: 0, Type: C.getSizeType()),
1213 type: C.getSizeType(), l: SourceLocation());
1214 }
1215
1216 DS.SetTypeSpecType(T: DeclSpec::TST_typename, Loc: StartLoc, PrevSpec, DiagID, Rep: Type,
1217 Policy);
1218 DS.SetPackIndexingExpr(EllipsisLoc, Pack: IndexExpr.get());
1219 return T.getCloseLocation();
1220}
1221
1222void Parser::AnnotateExistingIndexedTypeNamePack(ParsedType T,
1223 SourceLocation StartLoc,
1224 SourceLocation EndLoc) {
1225 // make sure we have a token we can turn into an annotation token
1226 if (PP.isBacktrackEnabled()) {
1227 PP.RevertCachedTokens(N: 1);
1228 if (!T) {
1229 // We encountered an error in parsing 'decltype(...)' so lets annotate all
1230 // the tokens in the backtracking cache - that we likely had to skip over
1231 // to get to a token that allows us to resume parsing, such as a
1232 // semi-colon.
1233 EndLoc = PP.getLastCachedTokenLocation();
1234 }
1235 } else
1236 PP.EnterToken(Tok, /*IsReinject*/ true);
1237
1238 Tok.setKind(tok::annot_pack_indexing_type);
1239 setTypeAnnotation(Tok, T);
1240 Tok.setAnnotationEndLoc(EndLoc);
1241 Tok.setLocation(StartLoc);
1242 PP.AnnotateCachedTokens(Tok);
1243}
1244
1245DeclSpec::TST Parser::TypeTransformTokToDeclSpec() {
1246 switch (Tok.getKind()) {
1247#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) \
1248 case tok::kw___##Trait: \
1249 return DeclSpec::TST_##Trait;
1250#include "clang/Basic/TransformTypeTraits.def"
1251 default:
1252 llvm_unreachable("passed in an unhandled type transformation built-in");
1253 }
1254}
1255
1256bool Parser::MaybeParseTypeTransformTypeSpecifier(DeclSpec &DS) {
1257 if (!NextToken().is(K: tok::l_paren)) {
1258 Tok.setKind(tok::identifier);
1259 return false;
1260 }
1261 DeclSpec::TST TypeTransformTST = TypeTransformTokToDeclSpec();
1262 SourceLocation StartLoc = ConsumeToken();
1263
1264 BalancedDelimiterTracker T(*this, tok::l_paren);
1265 if (T.expectAndConsume(diag::err_expected_lparen_after, Tok.getName(),
1266 tok::r_paren))
1267 return true;
1268
1269 TypeResult Result = ParseTypeName();
1270 if (Result.isInvalid()) {
1271 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
1272 return true;
1273 }
1274
1275 T.consumeClose();
1276 if (T.getCloseLocation().isInvalid())
1277 return true;
1278
1279 const char *PrevSpec = nullptr;
1280 unsigned DiagID;
1281 if (DS.SetTypeSpecType(T: TypeTransformTST, Loc: StartLoc, PrevSpec, DiagID,
1282 Rep: Result.get(),
1283 Policy: Actions.getASTContext().getPrintingPolicy()))
1284 Diag(Loc: StartLoc, DiagID) << PrevSpec;
1285 DS.setTypeArgumentRange(T.getRange());
1286 return true;
1287}
1288
1289TypeResult Parser::ParseBaseTypeSpecifier(SourceLocation &BaseLoc,
1290 SourceLocation &EndLocation) {
1291 // Ignore attempts to use typename
1292 if (Tok.is(K: tok::kw_typename)) {
1293 Diag(Tok, diag::err_expected_class_name_not_template)
1294 << FixItHint::CreateRemoval(Tok.getLocation());
1295 ConsumeToken();
1296 }
1297
1298 // Parse optional nested-name-specifier
1299 CXXScopeSpec SS;
1300 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
1301 /*ObjectHasErrors=*/false,
1302 /*EnteringContext=*/false))
1303 return true;
1304
1305 BaseLoc = Tok.getLocation();
1306
1307 // Parse decltype-specifier
1308 // tok == kw_decltype is just error recovery, it can only happen when SS
1309 // isn't empty
1310 if (Tok.isOneOf(K1: tok::kw_decltype, K2: tok::annot_decltype)) {
1311 if (SS.isNotEmpty())
1312 Diag(SS.getBeginLoc(), diag::err_unexpected_scope_on_base_decltype)
1313 << FixItHint::CreateRemoval(SS.getRange());
1314 // Fake up a Declarator to use with ActOnTypeName.
1315 DeclSpec DS(AttrFactory);
1316
1317 EndLocation = ParseDecltypeSpecifier(DS);
1318
1319 Declarator DeclaratorInfo(DS, ParsedAttributesView::none(),
1320 DeclaratorContext::TypeName);
1321 return Actions.ActOnTypeName(D&: DeclaratorInfo);
1322 }
1323
1324 if (Tok.is(K: tok::annot_pack_indexing_type)) {
1325 DeclSpec DS(AttrFactory);
1326 ParsePackIndexingType(DS);
1327 Declarator DeclaratorInfo(DS, ParsedAttributesView::none(),
1328 DeclaratorContext::TypeName);
1329 return Actions.ActOnTypeName(D&: DeclaratorInfo);
1330 }
1331
1332 // Check whether we have a template-id that names a type.
1333 // FIXME: identifier and annot_template_id handling in ParseUsingDeclaration
1334 // work very similarly. It should be refactored into a separate function.
1335 if (Tok.is(K: tok::annot_template_id)) {
1336 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(tok: Tok);
1337 if (TemplateId->mightBeType()) {
1338 AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename: ImplicitTypenameContext::No,
1339 /*IsClassName=*/true);
1340
1341 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
1342 TypeResult Type = getTypeAnnotation(Tok);
1343 EndLocation = Tok.getAnnotationEndLoc();
1344 ConsumeAnnotationToken();
1345 return Type;
1346 }
1347
1348 // Fall through to produce an error below.
1349 }
1350
1351 if (Tok.isNot(K: tok::identifier)) {
1352 Diag(Tok, diag::err_expected_class_name);
1353 return true;
1354 }
1355
1356 IdentifierInfo *Id = Tok.getIdentifierInfo();
1357 SourceLocation IdLoc = ConsumeToken();
1358
1359 if (Tok.is(K: tok::less)) {
1360 // It looks the user intended to write a template-id here, but the
1361 // template-name was wrong. Try to fix that.
1362 // FIXME: Invoke ParseOptionalCXXScopeSpecifier in a "'template' is neither
1363 // required nor permitted" mode, and do this there.
1364 TemplateNameKind TNK = TNK_Non_template;
1365 TemplateTy Template;
1366 if (!Actions.DiagnoseUnknownTemplateName(II: *Id, IILoc: IdLoc, S: getCurScope(), SS: &SS,
1367 SuggestedTemplate&: Template, SuggestedKind&: TNK)) {
1368 Diag(IdLoc, diag::err_unknown_template_name) << Id;
1369 }
1370
1371 // Form the template name
1372 UnqualifiedId TemplateName;
1373 TemplateName.setIdentifier(Id, IdLoc);
1374
1375 // Parse the full template-id, then turn it into a type.
1376 if (AnnotateTemplateIdToken(Template, TNK, SS, TemplateKWLoc: SourceLocation(),
1377 TemplateName))
1378 return true;
1379 if (Tok.is(K: tok::annot_template_id) &&
1380 takeTemplateIdAnnotation(tok: Tok)->mightBeType())
1381 AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename: ImplicitTypenameContext::No,
1382 /*IsClassName=*/true);
1383
1384 // If we didn't end up with a typename token, there's nothing more we
1385 // can do.
1386 if (Tok.isNot(K: tok::annot_typename))
1387 return true;
1388
1389 // Retrieve the type from the annotation token, consume that token, and
1390 // return.
1391 EndLocation = Tok.getAnnotationEndLoc();
1392 TypeResult Type = getTypeAnnotation(Tok);
1393 ConsumeAnnotationToken();
1394 return Type;
1395 }
1396
1397 // We have an identifier; check whether it is actually a type.
1398 IdentifierInfo *CorrectedII = nullptr;
1399 ParsedType Type = Actions.getTypeName(
1400 II: *Id, NameLoc: IdLoc, S: getCurScope(), SS: &SS, /*isClassName=*/true, HasTrailingDot: false, ObjectType: nullptr,
1401 /*IsCtorOrDtorName=*/false,
1402 /*WantNontrivialTypeSourceInfo=*/true,
1403 /*IsClassTemplateDeductionContext=*/false, AllowImplicitTypename: ImplicitTypenameContext::No,
1404 CorrectedII: &CorrectedII);
1405 if (!Type) {
1406 Diag(IdLoc, diag::err_expected_class_name);
1407 return true;
1408 }
1409
1410 // Consume the identifier.
1411 EndLocation = IdLoc;
1412
1413 // Fake up a Declarator to use with ActOnTypeName.
1414 DeclSpec DS(AttrFactory);
1415 DS.SetRangeStart(IdLoc);
1416 DS.SetRangeEnd(EndLocation);
1417 DS.getTypeSpecScope() = SS;
1418
1419 const char *PrevSpec = nullptr;
1420 unsigned DiagID;
1421 DS.SetTypeSpecType(T: TST_typename, Loc: IdLoc, PrevSpec, DiagID, Rep: Type,
1422 Policy: Actions.getASTContext().getPrintingPolicy());
1423
1424 Declarator DeclaratorInfo(DS, ParsedAttributesView::none(),
1425 DeclaratorContext::TypeName);
1426 return Actions.ActOnTypeName(D&: DeclaratorInfo);
1427}
1428
1429void Parser::ParseMicrosoftInheritanceClassAttributes(ParsedAttributes &attrs) {
1430 while (Tok.isOneOf(K1: tok::kw___single_inheritance,
1431 Ks: tok::kw___multiple_inheritance,
1432 Ks: tok::kw___virtual_inheritance)) {
1433 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1434 auto Kind = Tok.getKind();
1435 SourceLocation AttrNameLoc = ConsumeToken();
1436 attrs.addNew(attrName: AttrName, attrRange: AttrNameLoc, scopeName: nullptr, scopeLoc: AttrNameLoc, args: nullptr, numArgs: 0, form: Kind);
1437 }
1438}
1439
1440void Parser::ParseNullabilityClassAttributes(ParsedAttributes &attrs) {
1441 while (Tok.is(K: tok::kw__Nullable)) {
1442 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1443 auto Kind = Tok.getKind();
1444 SourceLocation AttrNameLoc = ConsumeToken();
1445 attrs.addNew(attrName: AttrName, attrRange: AttrNameLoc, scopeName: nullptr, scopeLoc: AttrNameLoc, args: nullptr, numArgs: 0, form: Kind);
1446 }
1447}
1448
1449bool Parser::isValidAfterTypeSpecifier(bool CouldBeBitfield) {
1450 // This switch enumerates the valid "follow" set for type-specifiers.
1451 switch (Tok.getKind()) {
1452 default:
1453 if (Tok.isRegularKeywordAttribute())
1454 return true;
1455 break;
1456 case tok::semi: // struct foo {...} ;
1457 case tok::star: // struct foo {...} * P;
1458 case tok::amp: // struct foo {...} & R = ...
1459 case tok::ampamp: // struct foo {...} && R = ...
1460 case tok::identifier: // struct foo {...} V ;
1461 case tok::r_paren: //(struct foo {...} ) {4}
1462 case tok::coloncolon: // struct foo {...} :: a::b;
1463 case tok::annot_cxxscope: // struct foo {...} a:: b;
1464 case tok::annot_typename: // struct foo {...} a ::b;
1465 case tok::annot_template_id: // struct foo {...} a<int> ::b;
1466 case tok::kw_decltype: // struct foo {...} decltype (a)::b;
1467 case tok::l_paren: // struct foo {...} ( x);
1468 case tok::comma: // __builtin_offsetof(struct foo{...} ,
1469 case tok::kw_operator: // struct foo operator ++() {...}
1470 case tok::kw___declspec: // struct foo {...} __declspec(...)
1471 case tok::l_square: // void f(struct f [ 3])
1472 case tok::ellipsis: // void f(struct f ... [Ns])
1473 // FIXME: we should emit semantic diagnostic when declaration
1474 // attribute is in type attribute position.
1475 case tok::kw___attribute: // struct foo __attribute__((used)) x;
1476 case tok::annot_pragma_pack: // struct foo {...} _Pragma(pack(pop));
1477 // struct foo {...} _Pragma(section(...));
1478 case tok::annot_pragma_ms_pragma:
1479 // struct foo {...} _Pragma(vtordisp(pop));
1480 case tok::annot_pragma_ms_vtordisp:
1481 // struct foo {...} _Pragma(pointers_to_members(...));
1482 case tok::annot_pragma_ms_pointers_to_members:
1483 return true;
1484 case tok::colon:
1485 return CouldBeBitfield || // enum E { ... } : 2;
1486 ColonIsSacred; // _Generic(..., enum E : 2);
1487 // Microsoft compatibility
1488 case tok::kw___cdecl: // struct foo {...} __cdecl x;
1489 case tok::kw___fastcall: // struct foo {...} __fastcall x;
1490 case tok::kw___stdcall: // struct foo {...} __stdcall x;
1491 case tok::kw___thiscall: // struct foo {...} __thiscall x;
1492 case tok::kw___vectorcall: // struct foo {...} __vectorcall x;
1493 // We will diagnose these calling-convention specifiers on non-function
1494 // declarations later, so claim they are valid after a type specifier.
1495 return getLangOpts().MicrosoftExt;
1496 // Type qualifiers
1497 case tok::kw_const: // struct foo {...} const x;
1498 case tok::kw_volatile: // struct foo {...} volatile x;
1499 case tok::kw_restrict: // struct foo {...} restrict x;
1500 case tok::kw__Atomic: // struct foo {...} _Atomic x;
1501 case tok::kw___unaligned: // struct foo {...} __unaligned *x;
1502 // Function specifiers
1503 // Note, no 'explicit'. An explicit function must be either a conversion
1504 // operator or a constructor. Either way, it can't have a return type.
1505 case tok::kw_inline: // struct foo inline f();
1506 case tok::kw_virtual: // struct foo virtual f();
1507 case tok::kw_friend: // struct foo friend f();
1508 // Storage-class specifiers
1509 case tok::kw_static: // struct foo {...} static x;
1510 case tok::kw_extern: // struct foo {...} extern x;
1511 case tok::kw_typedef: // struct foo {...} typedef x;
1512 case tok::kw_register: // struct foo {...} register x;
1513 case tok::kw_auto: // struct foo {...} auto x;
1514 case tok::kw_mutable: // struct foo {...} mutable x;
1515 case tok::kw_thread_local: // struct foo {...} thread_local x;
1516 case tok::kw_constexpr: // struct foo {...} constexpr x;
1517 case tok::kw_consteval: // struct foo {...} consteval x;
1518 case tok::kw_constinit: // struct foo {...} constinit x;
1519 // As shown above, type qualifiers and storage class specifiers absolutely
1520 // can occur after class specifiers according to the grammar. However,
1521 // almost no one actually writes code like this. If we see one of these,
1522 // it is much more likely that someone missed a semi colon and the
1523 // type/storage class specifier we're seeing is part of the *next*
1524 // intended declaration, as in:
1525 //
1526 // struct foo { ... }
1527 // typedef int X;
1528 //
1529 // We'd really like to emit a missing semicolon error instead of emitting
1530 // an error on the 'int' saying that you can't have two type specifiers in
1531 // the same declaration of X. Because of this, we look ahead past this
1532 // token to see if it's a type specifier. If so, we know the code is
1533 // otherwise invalid, so we can produce the expected semi error.
1534 if (!isKnownToBeTypeSpecifier(Tok: NextToken()))
1535 return true;
1536 break;
1537 case tok::r_brace: // struct bar { struct foo {...} }
1538 // Missing ';' at end of struct is accepted as an extension in C mode.
1539 if (!getLangOpts().CPlusPlus)
1540 return true;
1541 break;
1542 case tok::greater:
1543 // template<class T = class X>
1544 return getLangOpts().CPlusPlus;
1545 }
1546 return false;
1547}
1548
1549void Parser::ParseClassSpecifier(tok::TokenKind TagTokKind,
1550 SourceLocation StartLoc, DeclSpec &DS,
1551 ParsedTemplateInfo &TemplateInfo,
1552 AccessSpecifier AS, bool EnteringContext,
1553 DeclSpecContext DSC,
1554 ParsedAttributes &Attributes) {
1555 DeclSpec::TST TagType;
1556 if (TagTokKind == tok::kw_struct)
1557 TagType = DeclSpec::TST_struct;
1558 else if (TagTokKind == tok::kw___interface)
1559 TagType = DeclSpec::TST_interface;
1560 else if (TagTokKind == tok::kw_class)
1561 TagType = DeclSpec::TST_class;
1562 else {
1563 assert(TagTokKind == tok::kw_union && "Not a class specifier");
1564 TagType = DeclSpec::TST_union;
1565 }
1566
1567 if (Tok.is(K: tok::code_completion)) {
1568 // Code completion for a struct, class, or union name.
1569 cutOffParsing();
1570 Actions.CodeCompletion().CodeCompleteTag(S: getCurScope(), TagSpec: TagType);
1571 return;
1572 }
1573
1574 // C++20 [temp.class.spec] 13.7.5/10
1575 // The usual access checking rules do not apply to non-dependent names
1576 // used to specify template arguments of the simple-template-id of the
1577 // partial specialization.
1578 // C++20 [temp.spec] 13.9/6:
1579 // The usual access checking rules do not apply to names in a declaration
1580 // of an explicit instantiation or explicit specialization...
1581 const bool shouldDelayDiagsInTag =
1582 (TemplateInfo.Kind != ParsedTemplateKind::NonTemplate);
1583 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
1584
1585 ParsedAttributes attrs(AttrFactory);
1586 // If attributes exist after tag, parse them.
1587 for (;;) {
1588 MaybeParseAttributes(WhichAttrKinds: PAKM_CXX11 | PAKM_Declspec | PAKM_GNU, Attrs&: attrs);
1589 // Parse inheritance specifiers.
1590 if (Tok.isOneOf(K1: tok::kw___single_inheritance,
1591 Ks: tok::kw___multiple_inheritance,
1592 Ks: tok::kw___virtual_inheritance)) {
1593 ParseMicrosoftInheritanceClassAttributes(attrs);
1594 continue;
1595 }
1596 if (Tok.is(K: tok::kw__Nullable)) {
1597 ParseNullabilityClassAttributes(attrs);
1598 continue;
1599 }
1600 break;
1601 }
1602
1603 // Source location used by FIXIT to insert misplaced
1604 // C++11 attributes
1605 SourceLocation AttrFixitLoc = Tok.getLocation();
1606
1607 if (TagType == DeclSpec::TST_struct && Tok.isNot(K: tok::identifier) &&
1608 !Tok.isAnnotation() && Tok.getIdentifierInfo() &&
1609 Tok.isOneOf(
1610#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) tok::kw___##Trait,
1611#include "clang/Basic/TransformTypeTraits.def"
1612 Ks: tok::kw___is_abstract,
1613 Ks: tok::kw___is_aggregate,
1614 Ks: tok::kw___is_arithmetic,
1615 Ks: tok::kw___is_array,
1616 Ks: tok::kw___is_assignable,
1617 Ks: tok::kw___is_base_of,
1618 Ks: tok::kw___is_bounded_array,
1619 Ks: tok::kw___is_class,
1620 Ks: tok::kw___is_complete_type,
1621 Ks: tok::kw___is_compound,
1622 Ks: tok::kw___is_const,
1623 Ks: tok::kw___is_constructible,
1624 Ks: tok::kw___is_convertible,
1625 Ks: tok::kw___is_convertible_to,
1626 Ks: tok::kw___is_destructible,
1627 Ks: tok::kw___is_empty,
1628 Ks: tok::kw___is_enum,
1629 Ks: tok::kw___is_floating_point,
1630 Ks: tok::kw___is_final,
1631 Ks: tok::kw___is_function,
1632 Ks: tok::kw___is_fundamental,
1633 Ks: tok::kw___is_integral,
1634 Ks: tok::kw___is_interface_class,
1635 Ks: tok::kw___is_literal,
1636 Ks: tok::kw___is_lvalue_expr,
1637 Ks: tok::kw___is_lvalue_reference,
1638 Ks: tok::kw___is_member_function_pointer,
1639 Ks: tok::kw___is_member_object_pointer,
1640 Ks: tok::kw___is_member_pointer,
1641 Ks: tok::kw___is_nothrow_assignable,
1642 Ks: tok::kw___is_nothrow_constructible,
1643 Ks: tok::kw___is_nothrow_convertible,
1644 Ks: tok::kw___is_nothrow_destructible,
1645 Ks: tok::kw___is_object,
1646 Ks: tok::kw___is_pod,
1647 Ks: tok::kw___is_pointer,
1648 Ks: tok::kw___is_polymorphic,
1649 Ks: tok::kw___is_reference,
1650 Ks: tok::kw___is_rvalue_expr,
1651 Ks: tok::kw___is_rvalue_reference,
1652 Ks: tok::kw___is_same,
1653 Ks: tok::kw___is_scalar,
1654 Ks: tok::kw___is_scoped_enum,
1655 Ks: tok::kw___is_sealed,
1656 Ks: tok::kw___is_signed,
1657 Ks: tok::kw___is_standard_layout,
1658 Ks: tok::kw___is_trivial,
1659 Ks: tok::kw___is_trivially_equality_comparable,
1660 Ks: tok::kw___is_trivially_assignable,
1661 Ks: tok::kw___is_trivially_constructible,
1662 Ks: tok::kw___is_trivially_copyable,
1663 Ks: tok::kw___is_unbounded_array,
1664 Ks: tok::kw___is_union,
1665 Ks: tok::kw___is_unsigned,
1666 Ks: tok::kw___is_void,
1667 Ks: tok::kw___is_volatile
1668 ))
1669 // GNU libstdc++ 4.2 and libc++ use certain intrinsic names as the
1670 // name of struct templates, but some are keywords in GCC >= 4.3
1671 // and Clang. Therefore, when we see the token sequence "struct
1672 // X", make X into a normal identifier rather than a keyword, to
1673 // allow libstdc++ 4.2 and libc++ to work properly.
1674 TryKeywordIdentFallback(DisableKeyword: true);
1675
1676 struct PreserveAtomicIdentifierInfoRAII {
1677 PreserveAtomicIdentifierInfoRAII(Token &Tok, bool Enabled)
1678 : AtomicII(nullptr) {
1679 if (!Enabled)
1680 return;
1681 assert(Tok.is(tok::kw__Atomic));
1682 AtomicII = Tok.getIdentifierInfo();
1683 AtomicII->revertTokenIDToIdentifier();
1684 Tok.setKind(tok::identifier);
1685 }
1686 ~PreserveAtomicIdentifierInfoRAII() {
1687 if (!AtomicII)
1688 return;
1689 AtomicII->revertIdentifierToTokenID(TK: tok::kw__Atomic);
1690 }
1691 IdentifierInfo *AtomicII;
1692 };
1693
1694 // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
1695 // implementation for VS2013 uses _Atomic as an identifier for one of the
1696 // classes in <atomic>. When we are parsing 'struct _Atomic', don't consider
1697 // '_Atomic' to be a keyword. We are careful to undo this so that clang can
1698 // use '_Atomic' in its own header files.
1699 bool ShouldChangeAtomicToIdentifier = getLangOpts().MSVCCompat &&
1700 Tok.is(K: tok::kw__Atomic) &&
1701 TagType == DeclSpec::TST_struct;
1702 PreserveAtomicIdentifierInfoRAII AtomicTokenGuard(
1703 Tok, ShouldChangeAtomicToIdentifier);
1704
1705 // Parse the (optional) nested-name-specifier.
1706 CXXScopeSpec &SS = DS.getTypeSpecScope();
1707 if (getLangOpts().CPlusPlus) {
1708 // "FOO : BAR" is not a potential typo for "FOO::BAR". In this context it
1709 // is a base-specifier-list.
1710 ColonProtectionRAIIObject X(*this);
1711
1712 CXXScopeSpec Spec;
1713 if (TemplateInfo.TemplateParams)
1714 Spec.setTemplateParamLists(*TemplateInfo.TemplateParams);
1715
1716 bool HasValidSpec = true;
1717 if (ParseOptionalCXXScopeSpecifier(SS&: Spec, /*ObjectType=*/nullptr,
1718 /*ObjectHasErrors=*/false,
1719 EnteringContext)) {
1720 DS.SetTypeSpecError();
1721 HasValidSpec = false;
1722 }
1723 if (Spec.isSet())
1724 if (Tok.isNot(K: tok::identifier) && Tok.isNot(K: tok::annot_template_id)) {
1725 Diag(Tok, diag::err_expected) << tok::identifier;
1726 HasValidSpec = false;
1727 }
1728 if (HasValidSpec)
1729 SS = Spec;
1730 }
1731
1732 TemplateParameterLists *TemplateParams = TemplateInfo.TemplateParams;
1733
1734 auto RecoverFromUndeclaredTemplateName = [&](IdentifierInfo *Name,
1735 SourceLocation NameLoc,
1736 SourceRange TemplateArgRange,
1737 bool KnownUndeclared) {
1738 Diag(NameLoc, diag::err_explicit_spec_non_template)
1739 << (TemplateInfo.Kind == ParsedTemplateKind::ExplicitInstantiation)
1740 << TagTokKind << Name << TemplateArgRange << KnownUndeclared;
1741
1742 // Strip off the last template parameter list if it was empty, since
1743 // we've removed its template argument list.
1744 if (TemplateParams && TemplateInfo.LastParameterListWasEmpty) {
1745 if (TemplateParams->size() > 1) {
1746 TemplateParams->pop_back();
1747 } else {
1748 TemplateParams = nullptr;
1749 TemplateInfo.Kind = ParsedTemplateKind::NonTemplate;
1750 }
1751 } else if (TemplateInfo.Kind == ParsedTemplateKind::ExplicitInstantiation) {
1752 // Pretend this is just a forward declaration.
1753 TemplateParams = nullptr;
1754 TemplateInfo.Kind = ParsedTemplateKind::NonTemplate;
1755 TemplateInfo.TemplateLoc = SourceLocation();
1756 TemplateInfo.ExternLoc = SourceLocation();
1757 }
1758 };
1759
1760 // Parse the (optional) class name or simple-template-id.
1761 IdentifierInfo *Name = nullptr;
1762 SourceLocation NameLoc;
1763 TemplateIdAnnotation *TemplateId = nullptr;
1764 if (Tok.is(K: tok::identifier)) {
1765 Name = Tok.getIdentifierInfo();
1766 NameLoc = ConsumeToken();
1767 DS.SetRangeEnd(NameLoc);
1768
1769 if (Tok.is(K: tok::less) && getLangOpts().CPlusPlus) {
1770 // The name was supposed to refer to a template, but didn't.
1771 // Eat the template argument list and try to continue parsing this as
1772 // a class (or template thereof).
1773 TemplateArgList TemplateArgs;
1774 SourceLocation LAngleLoc, RAngleLoc;
1775 if (ParseTemplateIdAfterTemplateName(ConsumeLastToken: true, LAngleLoc, TemplateArgs,
1776 RAngleLoc)) {
1777 // We couldn't parse the template argument list at all, so don't
1778 // try to give any location information for the list.
1779 LAngleLoc = RAngleLoc = SourceLocation();
1780 }
1781 RecoverFromUndeclaredTemplateName(
1782 Name, NameLoc, SourceRange(LAngleLoc, RAngleLoc), false);
1783 }
1784 } else if (Tok.is(K: tok::annot_template_id)) {
1785 TemplateId = takeTemplateIdAnnotation(tok: Tok);
1786 NameLoc = ConsumeAnnotationToken();
1787
1788 if (TemplateId->Kind == TNK_Undeclared_template) {
1789 // Try to resolve the template name to a type template. May update Kind.
1790 Actions.ActOnUndeclaredTypeTemplateName(
1791 S: getCurScope(), Name&: TemplateId->Template, TNK&: TemplateId->Kind, NameLoc, II&: Name);
1792 if (TemplateId->Kind == TNK_Undeclared_template) {
1793 RecoverFromUndeclaredTemplateName(
1794 Name, NameLoc,
1795 SourceRange(TemplateId->LAngleLoc, TemplateId->RAngleLoc), true);
1796 TemplateId = nullptr;
1797 }
1798 }
1799
1800 if (TemplateId && !TemplateId->mightBeType()) {
1801 // The template-name in the simple-template-id refers to
1802 // something other than a type template. Give an appropriate
1803 // error message and skip to the ';'.
1804 SourceRange Range(NameLoc);
1805 if (SS.isNotEmpty())
1806 Range.setBegin(SS.getBeginLoc());
1807
1808 // FIXME: Name may be null here.
1809 Diag(TemplateId->LAngleLoc, diag::err_template_spec_syntax_non_template)
1810 << TemplateId->Name << static_cast<int>(TemplateId->Kind) << Range;
1811
1812 DS.SetTypeSpecError();
1813 SkipUntil(T: tok::semi, Flags: StopBeforeMatch);
1814 return;
1815 }
1816 }
1817
1818 // There are four options here.
1819 // - If we are in a trailing return type, this is always just a reference,
1820 // and we must not try to parse a definition. For instance,
1821 // [] () -> struct S { };
1822 // does not define a type.
1823 // - If we have 'struct foo {...', 'struct foo :...',
1824 // 'struct foo final :' or 'struct foo final {', then this is a definition.
1825 // - If we have 'struct foo;', then this is either a forward declaration
1826 // or a friend declaration, which have to be treated differently.
1827 // - Otherwise we have something like 'struct foo xyz', a reference.
1828 //
1829 // We also detect these erroneous cases to provide better diagnostic for
1830 // C++11 attributes parsing.
1831 // - attributes follow class name:
1832 // struct foo [[]] {};
1833 // - attributes appear before or after 'final':
1834 // struct foo [[]] final [[]] {};
1835 //
1836 // However, in type-specifier-seq's, things look like declarations but are
1837 // just references, e.g.
1838 // new struct s;
1839 // or
1840 // &T::operator struct s;
1841 // For these, DSC is DeclSpecContext::DSC_type_specifier or
1842 // DeclSpecContext::DSC_alias_declaration.
1843
1844 // If there are attributes after class name, parse them.
1845 MaybeParseCXX11Attributes(Attrs&: Attributes);
1846
1847 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
1848 TagUseKind TUK;
1849
1850 // C++26 [class.mem.general]p10: If a name-declaration matches the
1851 // syntactic requirements of friend-type-declaration, it is a
1852 // friend-type-declaration.
1853 if (getLangOpts().CPlusPlus && DS.isFriendSpecifiedFirst() &&
1854 Tok.isOneOf(K1: tok::comma, K2: tok::ellipsis))
1855 TUK = TagUseKind::Friend;
1856 else if (isDefiningTypeSpecifierContext(DSC, IsCPlusPlus: getLangOpts().CPlusPlus) ==
1857 AllowDefiningTypeSpec::No ||
1858 (getLangOpts().OpenMP && OpenMPDirectiveParsing))
1859 TUK = TagUseKind::Reference;
1860 else if (Tok.is(K: tok::l_brace) ||
1861 (DSC != DeclSpecContext::DSC_association &&
1862 getLangOpts().CPlusPlus && Tok.is(K: tok::colon)) ||
1863 (isClassCompatibleKeyword() &&
1864 (NextToken().is(K: tok::l_brace) || NextToken().is(K: tok::colon) ||
1865 isClassCompatibleKeyword(Tok: NextToken())))) {
1866 if (DS.isFriendSpecified()) {
1867 // C++ [class.friend]p2:
1868 // A class shall not be defined in a friend declaration.
1869 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
1870 << SourceRange(DS.getFriendSpecLoc());
1871
1872 // Skip everything up to the semicolon, so that this looks like a proper
1873 // friend class (or template thereof) declaration.
1874 SkipUntil(T: tok::semi, Flags: StopBeforeMatch);
1875 TUK = TagUseKind::Friend;
1876 } else {
1877 // Okay, this is a class definition.
1878 TUK = TagUseKind::Definition;
1879 }
1880 } else if (isClassCompatibleKeyword() &&
1881 (NextToken().is(K: tok::l_square) ||
1882 NextToken().is(K: tok::kw_alignas) ||
1883 NextToken().isRegularKeywordAttribute() ||
1884 isCXX11VirtSpecifier(Tok: NextToken()) != VirtSpecifiers::VS_None ||
1885 isCXX2CTriviallyRelocatableKeyword())) {
1886 // We can't tell if this is a definition or reference
1887 // until we skipped the 'final' and C++11 attribute specifiers.
1888 TentativeParsingAction PA(*this);
1889
1890 // Skip the 'final', abstract'... keywords.
1891 while (isClassCompatibleKeyword())
1892 ConsumeToken();
1893
1894 // Skip C++11 attribute specifiers.
1895 while (true) {
1896 if (Tok.is(K: tok::l_square) && NextToken().is(K: tok::l_square)) {
1897 ConsumeBracket();
1898 if (!SkipUntil(T: tok::r_square, Flags: StopAtSemi))
1899 break;
1900 } else if (Tok.is(K: tok::kw_alignas) && NextToken().is(K: tok::l_paren)) {
1901 ConsumeToken();
1902 ConsumeParen();
1903 if (!SkipUntil(T: tok::r_paren, Flags: StopAtSemi))
1904 break;
1905 } else if (Tok.isRegularKeywordAttribute()) {
1906 bool TakesArgs = doesKeywordAttributeTakeArgs(Kind: Tok.getKind());
1907 ConsumeToken();
1908 if (TakesArgs) {
1909 BalancedDelimiterTracker T(*this, tok::l_paren);
1910 if (!T.consumeOpen())
1911 T.skipToEnd();
1912 }
1913 } else {
1914 break;
1915 }
1916 }
1917
1918 if (Tok.isOneOf(K1: tok::l_brace, K2: tok::colon))
1919 TUK = TagUseKind::Definition;
1920 else
1921 TUK = TagUseKind::Reference;
1922
1923 PA.Revert();
1924 } else if (!isTypeSpecifier(DSC) &&
1925 (Tok.is(K: tok::semi) ||
1926 (Tok.isAtStartOfLine() && !isValidAfterTypeSpecifier(CouldBeBitfield: false)))) {
1927 TUK = DS.isFriendSpecified() ? TagUseKind::Friend : TagUseKind::Declaration;
1928 if (Tok.isNot(K: tok::semi)) {
1929 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
1930 // A semicolon was missing after this declaration. Diagnose and recover.
1931 ExpectAndConsume(tok::semi, diag::err_expected_after,
1932 DeclSpec::getSpecifierName(TagType, PPol));
1933 PP.EnterToken(Tok, /*IsReinject*/ true);
1934 Tok.setKind(tok::semi);
1935 }
1936 } else
1937 TUK = TagUseKind::Reference;
1938
1939 // Forbid misplaced attributes. In cases of a reference, we pass attributes
1940 // to caller to handle.
1941 if (TUK != TagUseKind::Reference) {
1942 // If this is not a reference, then the only possible
1943 // valid place for C++11 attributes to appear here
1944 // is between class-key and class-name. If there are
1945 // any attributes after class-name, we try a fixit to move
1946 // them to the right place.
1947 SourceRange AttrRange = Attributes.Range;
1948 if (AttrRange.isValid()) {
1949 auto *FirstAttr = Attributes.empty() ? nullptr : &Attributes.front();
1950 auto Loc = AttrRange.getBegin();
1951 (FirstAttr && FirstAttr->isRegularKeywordAttribute()
1952 ? Diag(Loc, diag::err_keyword_not_allowed) << FirstAttr
1953 : Diag(Loc, diag::err_attributes_not_allowed))
1954 << AttrRange
1955 << FixItHint::CreateInsertionFromRange(
1956 AttrFixitLoc, CharSourceRange(AttrRange, true))
1957 << FixItHint::CreateRemoval(AttrRange);
1958
1959 // Recover by adding misplaced attributes to the attribute list
1960 // of the class so they can be applied on the class later.
1961 attrs.takeAllFrom(Other&: Attributes);
1962 }
1963 }
1964
1965 if (!Name && !TemplateId &&
1966 (DS.getTypeSpecType() == DeclSpec::TST_error ||
1967 TUK != TagUseKind::Definition)) {
1968 if (DS.getTypeSpecType() != DeclSpec::TST_error) {
1969 // We have a declaration or reference to an anonymous class.
1970 Diag(StartLoc, diag::err_anon_type_definition)
1971 << DeclSpec::getSpecifierName(TagType, Policy);
1972 }
1973
1974 // If we are parsing a definition and stop at a base-clause, continue on
1975 // until the semicolon. Continuing from the comma will just trick us into
1976 // thinking we are seeing a variable declaration.
1977 if (TUK == TagUseKind::Definition && Tok.is(K: tok::colon))
1978 SkipUntil(T: tok::semi, Flags: StopBeforeMatch);
1979 else
1980 SkipUntil(T: tok::comma, Flags: StopAtSemi);
1981 return;
1982 }
1983
1984 // Create the tag portion of the class or class template.
1985 DeclResult TagOrTempResult = true; // invalid
1986 TypeResult TypeResult = true; // invalid
1987
1988 bool Owned = false;
1989 SkipBodyInfo SkipBody;
1990 if (TemplateId) {
1991 // Explicit specialization, class template partial specialization,
1992 // or explicit instantiation.
1993 ASTTemplateArgsPtr TemplateArgsPtr(TemplateId->getTemplateArgs(),
1994 TemplateId->NumArgs);
1995 if (TemplateId->isInvalid()) {
1996 // Can't build the declaration.
1997 } else if (TemplateInfo.Kind == ParsedTemplateKind::ExplicitInstantiation &&
1998 TUK == TagUseKind::Declaration) {
1999 // This is an explicit instantiation of a class template.
2000 ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed,
2001 diag::err_keyword_not_allowed,
2002 /*DiagnoseEmptyAttrs=*/true);
2003
2004 TagOrTempResult = Actions.ActOnExplicitInstantiation(
2005 S: getCurScope(), ExternLoc: TemplateInfo.ExternLoc, TemplateLoc: TemplateInfo.TemplateLoc,
2006 TagSpec: TagType, KWLoc: StartLoc, SS, Template: TemplateId->Template,
2007 TemplateNameLoc: TemplateId->TemplateNameLoc, LAngleLoc: TemplateId->LAngleLoc, TemplateArgs: TemplateArgsPtr,
2008 RAngleLoc: TemplateId->RAngleLoc, Attr: attrs);
2009
2010 // Friend template-ids are treated as references unless
2011 // they have template headers, in which case they're ill-formed
2012 // (FIXME: "template <class T> friend class A<T>::B<int>;").
2013 // We diagnose this error in ActOnClassTemplateSpecialization.
2014 } else if (TUK == TagUseKind::Reference ||
2015 (TUK == TagUseKind::Friend &&
2016 TemplateInfo.Kind == ParsedTemplateKind::NonTemplate)) {
2017 ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed,
2018 diag::err_keyword_not_allowed,
2019 /*DiagnoseEmptyAttrs=*/true);
2020 TypeResult = Actions.ActOnTagTemplateIdType(
2021 TUK, TagSpec: TagType, TagLoc: StartLoc, SS, TemplateKWLoc: TemplateId->TemplateKWLoc,
2022 TemplateD: TemplateId->Template, TemplateLoc: TemplateId->TemplateNameLoc,
2023 LAngleLoc: TemplateId->LAngleLoc, TemplateArgsIn: TemplateArgsPtr, RAngleLoc: TemplateId->RAngleLoc);
2024 } else {
2025 // This is an explicit specialization or a class template
2026 // partial specialization.
2027 TemplateParameterLists FakedParamLists;
2028 if (TemplateInfo.Kind == ParsedTemplateKind::ExplicitInstantiation) {
2029 // This looks like an explicit instantiation, because we have
2030 // something like
2031 //
2032 // template class Foo<X>
2033 //
2034 // but it actually has a definition. Most likely, this was
2035 // meant to be an explicit specialization, but the user forgot
2036 // the '<>' after 'template'.
2037 // It this is friend declaration however, since it cannot have a
2038 // template header, it is most likely that the user meant to
2039 // remove the 'template' keyword.
2040 assert((TUK == TagUseKind::Definition || TUK == TagUseKind::Friend) &&
2041 "Expected a definition here");
2042
2043 if (TUK == TagUseKind::Friend) {
2044 Diag(DS.getFriendSpecLoc(), diag::err_friend_explicit_instantiation);
2045 TemplateParams = nullptr;
2046 } else {
2047 SourceLocation LAngleLoc =
2048 PP.getLocForEndOfToken(Loc: TemplateInfo.TemplateLoc);
2049 Diag(TemplateId->TemplateNameLoc,
2050 diag::err_explicit_instantiation_with_definition)
2051 << SourceRange(TemplateInfo.TemplateLoc)
2052 << FixItHint::CreateInsertion(LAngleLoc, "<>");
2053
2054 // Create a fake template parameter list that contains only
2055 // "template<>", so that we treat this construct as a class
2056 // template specialization.
2057 FakedParamLists.push_back(Elt: Actions.ActOnTemplateParameterList(
2058 Depth: 0, ExportLoc: SourceLocation(), TemplateLoc: TemplateInfo.TemplateLoc, LAngleLoc, Params: {},
2059 RAngleLoc: LAngleLoc, RequiresClause: nullptr));
2060 TemplateParams = &FakedParamLists;
2061 }
2062 }
2063
2064 // Build the class template specialization.
2065 TagOrTempResult = Actions.ActOnClassTemplateSpecialization(
2066 S: getCurScope(), TagSpec: TagType, TUK, KWLoc: StartLoc, ModulePrivateLoc: DS.getModulePrivateSpecLoc(),
2067 SS, TemplateId&: *TemplateId, Attr: attrs,
2068 TemplateParameterLists: MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0]
2069 : nullptr,
2070 TemplateParams ? TemplateParams->size() : 0),
2071 SkipBody: &SkipBody);
2072 }
2073 } else if (TemplateInfo.Kind == ParsedTemplateKind::ExplicitInstantiation &&
2074 TUK == TagUseKind::Declaration) {
2075 // Explicit instantiation of a member of a class template
2076 // specialization, e.g.,
2077 //
2078 // template struct Outer<int>::Inner;
2079 //
2080 ProhibitAttributes(Attrs&: attrs);
2081
2082 TagOrTempResult = Actions.ActOnExplicitInstantiation(
2083 S: getCurScope(), ExternLoc: TemplateInfo.ExternLoc, TemplateLoc: TemplateInfo.TemplateLoc,
2084 TagSpec: TagType, KWLoc: StartLoc, SS, Name, NameLoc, Attr: attrs);
2085 } else if (TUK == TagUseKind::Friend &&
2086 TemplateInfo.Kind != ParsedTemplateKind::NonTemplate) {
2087 ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed,
2088 diag::err_keyword_not_allowed,
2089 /*DiagnoseEmptyAttrs=*/true);
2090
2091 // Consume '...' first so we error on the ',' after it if there is one.
2092 SourceLocation EllipsisLoc;
2093 TryConsumeToken(Expected: tok::ellipsis, Loc&: EllipsisLoc);
2094
2095 // CWG 2917: In a template-declaration whose declaration is a
2096 // friend-type-declaration, the friend-type-specifier-list shall
2097 // consist of exactly one friend-type-specifier.
2098 //
2099 // Essentially, the following is obviously nonsense, so disallow it:
2100 //
2101 // template <typename>
2102 // friend class S, int;
2103 //
2104 if (Tok.is(K: tok::comma)) {
2105 Diag(Tok.getLocation(),
2106 diag::err_friend_template_decl_multiple_specifiers);
2107 SkipUntil(T: tok::semi, Flags: StopBeforeMatch);
2108 }
2109
2110 TagOrTempResult = Actions.ActOnTemplatedFriendTag(
2111 S: getCurScope(), FriendLoc: DS.getFriendSpecLoc(), TagSpec: TagType, TagLoc: StartLoc, SS, Name,
2112 NameLoc, EllipsisLoc, Attr: attrs,
2113 TempParamLists: MultiTemplateParamsArg(TemplateParams ? &(*TemplateParams)[0] : nullptr,
2114 TemplateParams ? TemplateParams->size() : 0));
2115 } else {
2116 if (TUK != TagUseKind::Declaration && TUK != TagUseKind::Definition)
2117 ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed,
2118 diag::err_keyword_not_allowed,
2119 /* DiagnoseEmptyAttrs=*/true);
2120
2121 if (TUK == TagUseKind::Definition &&
2122 TemplateInfo.Kind == ParsedTemplateKind::ExplicitInstantiation) {
2123 // If the declarator-id is not a template-id, issue a diagnostic and
2124 // recover by ignoring the 'template' keyword.
2125 Diag(Tok, diag::err_template_defn_explicit_instantiation)
2126 << 1 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
2127 TemplateParams = nullptr;
2128 }
2129
2130 bool IsDependent = false;
2131
2132 // Don't pass down template parameter lists if this is just a tag
2133 // reference. For example, we don't need the template parameters here:
2134 // template <class T> class A *makeA(T t);
2135 MultiTemplateParamsArg TParams;
2136 if (TUK != TagUseKind::Reference && TemplateParams)
2137 TParams =
2138 MultiTemplateParamsArg(&(*TemplateParams)[0], TemplateParams->size());
2139
2140 stripTypeAttributesOffDeclSpec(Attrs&: attrs, DS, TUK);
2141
2142 // Declaration or definition of a class type
2143 TagOrTempResult = Actions.ActOnTag(
2144 S: getCurScope(), TagSpec: TagType, TUK, KWLoc: StartLoc, SS, Name, NameLoc, Attr: attrs, AS,
2145 ModulePrivateLoc: DS.getModulePrivateSpecLoc(), TemplateParameterLists: TParams, OwnedDecl&: Owned, IsDependent,
2146 ScopedEnumKWLoc: SourceLocation(), ScopedEnumUsesClassTag: false, UnderlyingType: clang::TypeResult(),
2147 IsTypeSpecifier: DSC == DeclSpecContext::DSC_type_specifier,
2148 IsTemplateParamOrArg: DSC == DeclSpecContext::DSC_template_param ||
2149 DSC == DeclSpecContext::DSC_template_type_arg,
2150 OOK: OffsetOfState, SkipBody: &SkipBody);
2151
2152 // If ActOnTag said the type was dependent, try again with the
2153 // less common call.
2154 if (IsDependent) {
2155 assert(TUK == TagUseKind::Reference || TUK == TagUseKind::Friend);
2156 TypeResult = Actions.ActOnDependentTag(S: getCurScope(), TagSpec: TagType, TUK, SS,
2157 Name, TagLoc: StartLoc, NameLoc);
2158 }
2159 }
2160
2161 // If this is an elaborated type specifier in function template,
2162 // and we delayed diagnostics before,
2163 // just merge them into the current pool.
2164 if (shouldDelayDiagsInTag) {
2165 diagsFromTag.done();
2166 if (TUK == TagUseKind::Reference &&
2167 TemplateInfo.Kind == ParsedTemplateKind::Template)
2168 diagsFromTag.redelay();
2169 }
2170
2171 // If there is a body, parse it and inform the actions module.
2172 if (TUK == TagUseKind::Definition) {
2173 assert(Tok.is(tok::l_brace) ||
2174 (getLangOpts().CPlusPlus && Tok.is(tok::colon)) ||
2175 isClassCompatibleKeyword());
2176 if (SkipBody.ShouldSkip)
2177 SkipCXXMemberSpecification(StartLoc, AttrFixitLoc, TagType,
2178 TagDecl: TagOrTempResult.get());
2179 else if (getLangOpts().CPlusPlus)
2180 ParseCXXMemberSpecification(StartLoc, AttrFixitLoc, Attrs&: attrs, TagType,
2181 TagDecl: TagOrTempResult.get());
2182 else {
2183 Decl *D =
2184 SkipBody.CheckSameAsPrevious ? SkipBody.New : TagOrTempResult.get();
2185 // Parse the definition body.
2186 ParseStructUnionBody(StartLoc, TagType, TagDecl: cast<RecordDecl>(Val: D));
2187 if (SkipBody.CheckSameAsPrevious &&
2188 !Actions.ActOnDuplicateDefinition(S: getCurScope(),
2189 Prev: TagOrTempResult.get(), SkipBody)) {
2190 DS.SetTypeSpecError();
2191 return;
2192 }
2193 }
2194 }
2195
2196 if (!TagOrTempResult.isInvalid())
2197 // Delayed processing of attributes.
2198 Actions.ProcessDeclAttributeDelayed(D: TagOrTempResult.get(), AttrList: attrs);
2199
2200 const char *PrevSpec = nullptr;
2201 unsigned DiagID;
2202 bool Result;
2203 if (!TypeResult.isInvalid()) {
2204 Result = DS.SetTypeSpecType(T: DeclSpec::TST_typename, TagKwLoc: StartLoc,
2205 TagNameLoc: NameLoc.isValid() ? NameLoc : StartLoc,
2206 PrevSpec, DiagID, Rep: TypeResult.get(), Policy);
2207 } else if (!TagOrTempResult.isInvalid()) {
2208 Result = DS.SetTypeSpecType(
2209 T: TagType, TagKwLoc: StartLoc, TagNameLoc: NameLoc.isValid() ? NameLoc : StartLoc, PrevSpec,
2210 DiagID, Rep: TagOrTempResult.get(), Owned, Policy);
2211 } else {
2212 DS.SetTypeSpecError();
2213 return;
2214 }
2215
2216 if (Result)
2217 Diag(Loc: StartLoc, DiagID) << PrevSpec;
2218
2219 // At this point, we've successfully parsed a class-specifier in 'definition'
2220 // form (e.g. "struct foo { int x; }". While we could just return here, we're
2221 // going to look at what comes after it to improve error recovery. If an
2222 // impossible token occurs next, we assume that the programmer forgot a ; at
2223 // the end of the declaration and recover that way.
2224 //
2225 // Also enforce C++ [temp]p3:
2226 // In a template-declaration which defines a class, no declarator
2227 // is permitted.
2228 //
2229 // After a type-specifier, we don't expect a semicolon. This only happens in
2230 // C, since definitions are not permitted in this context in C++.
2231 if (TUK == TagUseKind::Definition &&
2232 (getLangOpts().CPlusPlus || !isTypeSpecifier(DSC)) &&
2233 (TemplateInfo.Kind != ParsedTemplateKind::NonTemplate || !isValidAfterTypeSpecifier(CouldBeBitfield: false))) {
2234 if (Tok.isNot(K: tok::semi)) {
2235 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
2236 ExpectAndConsume(tok::semi, diag::err_expected_after,
2237 DeclSpec::getSpecifierName(TagType, PPol));
2238 // Push this token back into the preprocessor and change our current token
2239 // to ';' so that the rest of the code recovers as though there were an
2240 // ';' after the definition.
2241 PP.EnterToken(Tok, /*IsReinject=*/true);
2242 Tok.setKind(tok::semi);
2243 }
2244 }
2245}
2246
2247void Parser::ParseBaseClause(Decl *ClassDecl) {
2248 assert(Tok.is(tok::colon) && "Not a base clause");
2249 ConsumeToken();
2250
2251 // Build up an array of parsed base specifiers.
2252 SmallVector<CXXBaseSpecifier *, 8> BaseInfo;
2253
2254 while (true) {
2255 // Parse a base-specifier.
2256 BaseResult Result = ParseBaseSpecifier(ClassDecl);
2257 if (Result.isInvalid()) {
2258 // Skip the rest of this base specifier, up until the comma or
2259 // opening brace.
2260 SkipUntil(T1: tok::comma, T2: tok::l_brace, Flags: StopAtSemi | StopBeforeMatch);
2261 } else {
2262 // Add this to our array of base specifiers.
2263 BaseInfo.push_back(Elt: Result.get());
2264 }
2265
2266 // If the next token is a comma, consume it and keep reading
2267 // base-specifiers.
2268 if (!TryConsumeToken(Expected: tok::comma))
2269 break;
2270 }
2271
2272 // Attach the base specifiers
2273 Actions.ActOnBaseSpecifiers(ClassDecl, Bases: BaseInfo);
2274}
2275
2276BaseResult Parser::ParseBaseSpecifier(Decl *ClassDecl) {
2277 bool IsVirtual = false;
2278 SourceLocation StartLoc = Tok.getLocation();
2279
2280 ParsedAttributes Attributes(AttrFactory);
2281 MaybeParseCXX11Attributes(Attrs&: Attributes);
2282
2283 // Parse the 'virtual' keyword.
2284 if (TryConsumeToken(Expected: tok::kw_virtual))
2285 IsVirtual = true;
2286
2287 CheckMisplacedCXX11Attribute(Attrs&: Attributes, CorrectLocation: StartLoc);
2288
2289 // Parse an (optional) access specifier.
2290 AccessSpecifier Access = getAccessSpecifierIfPresent();
2291 if (Access != AS_none) {
2292 ConsumeToken();
2293 if (getLangOpts().HLSL)
2294 Diag(Tok.getLocation(), diag::ext_hlsl_access_specifiers);
2295 }
2296
2297 CheckMisplacedCXX11Attribute(Attrs&: Attributes, CorrectLocation: StartLoc);
2298
2299 // Parse the 'virtual' keyword (again!), in case it came after the
2300 // access specifier.
2301 if (Tok.is(K: tok::kw_virtual)) {
2302 SourceLocation VirtualLoc = ConsumeToken();
2303 if (IsVirtual) {
2304 // Complain about duplicate 'virtual'
2305 Diag(VirtualLoc, diag::err_dup_virtual)
2306 << FixItHint::CreateRemoval(VirtualLoc);
2307 }
2308
2309 IsVirtual = true;
2310 }
2311
2312 if (getLangOpts().HLSL && IsVirtual)
2313 Diag(Tok.getLocation(), diag::err_hlsl_virtual_inheritance);
2314
2315 CheckMisplacedCXX11Attribute(Attrs&: Attributes, CorrectLocation: StartLoc);
2316
2317 // Parse the class-name.
2318
2319 // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
2320 // implementation for VS2013 uses _Atomic as an identifier for one of the
2321 // classes in <atomic>. Treat '_Atomic' to be an identifier when we are
2322 // parsing the class-name for a base specifier.
2323 if (getLangOpts().MSVCCompat && Tok.is(K: tok::kw__Atomic) &&
2324 NextToken().is(K: tok::less))
2325 Tok.setKind(tok::identifier);
2326
2327 SourceLocation EndLocation;
2328 SourceLocation BaseLoc;
2329 TypeResult BaseType = ParseBaseTypeSpecifier(BaseLoc, EndLocation);
2330 if (BaseType.isInvalid())
2331 return true;
2332
2333 // Parse the optional ellipsis (for a pack expansion). The ellipsis is
2334 // actually part of the base-specifier-list grammar productions, but we
2335 // parse it here for convenience.
2336 SourceLocation EllipsisLoc;
2337 TryConsumeToken(Expected: tok::ellipsis, Loc&: EllipsisLoc);
2338
2339 // Find the complete source range for the base-specifier.
2340 SourceRange Range(StartLoc, EndLocation);
2341
2342 // Notify semantic analysis that we have parsed a complete
2343 // base-specifier.
2344 return Actions.ActOnBaseSpecifier(classdecl: ClassDecl, SpecifierRange: Range, Attrs: Attributes, Virtual: IsVirtual,
2345 Access, basetype: BaseType.get(), BaseLoc,
2346 EllipsisLoc);
2347}
2348
2349AccessSpecifier Parser::getAccessSpecifierIfPresent() const {
2350 switch (Tok.getKind()) {
2351 default:
2352 return AS_none;
2353 case tok::kw_private:
2354 return AS_private;
2355 case tok::kw_protected:
2356 return AS_protected;
2357 case tok::kw_public:
2358 return AS_public;
2359 }
2360}
2361
2362void Parser::HandleMemberFunctionDeclDelays(Declarator &DeclaratorInfo,
2363 Decl *ThisDecl) {
2364 DeclaratorChunk::FunctionTypeInfo &FTI = DeclaratorInfo.getFunctionTypeInfo();
2365 // If there was a late-parsed exception-specification, we'll need a
2366 // late parse
2367 bool NeedLateParse = FTI.getExceptionSpecType() == EST_Unparsed;
2368
2369 if (!NeedLateParse) {
2370 // Look ahead to see if there are any default args
2371 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx) {
2372 const auto *Param = cast<ParmVarDecl>(Val: FTI.Params[ParamIdx].Param);
2373 if (Param->hasUnparsedDefaultArg()) {
2374 NeedLateParse = true;
2375 break;
2376 }
2377 }
2378 }
2379
2380 if (NeedLateParse) {
2381 // Push this method onto the stack of late-parsed method
2382 // declarations.
2383 auto LateMethod = new LateParsedMethodDeclaration(this, ThisDecl);
2384 getCurrentClass().LateParsedDeclarations.push_back(Elt: LateMethod);
2385
2386 // Push tokens for each parameter. Those that do not have defaults will be
2387 // NULL. We need to track all the parameters so that we can push them into
2388 // scope for later parameters and perhaps for the exception specification.
2389 LateMethod->DefaultArgs.reserve(N: FTI.NumParams);
2390 for (unsigned ParamIdx = 0; ParamIdx < FTI.NumParams; ++ParamIdx)
2391 LateMethod->DefaultArgs.push_back(Elt: LateParsedDefaultArgument(
2392 FTI.Params[ParamIdx].Param,
2393 std::move(FTI.Params[ParamIdx].DefaultArgTokens)));
2394
2395 // Stash the exception-specification tokens in the late-pased method.
2396 if (FTI.getExceptionSpecType() == EST_Unparsed) {
2397 LateMethod->ExceptionSpecTokens = FTI.ExceptionSpecTokens;
2398 FTI.ExceptionSpecTokens = nullptr;
2399 }
2400 }
2401}
2402
2403VirtSpecifiers::Specifier Parser::isCXX11VirtSpecifier(const Token &Tok) const {
2404 if (!getLangOpts().CPlusPlus || Tok.isNot(K: tok::identifier))
2405 return VirtSpecifiers::VS_None;
2406
2407 const IdentifierInfo *II = Tok.getIdentifierInfo();
2408
2409 // Initialize the contextual keywords.
2410 if (!Ident_final) {
2411 Ident_final = &PP.getIdentifierTable().get(Name: "final");
2412 if (getLangOpts().GNUKeywords)
2413 Ident_GNU_final = &PP.getIdentifierTable().get(Name: "__final");
2414 if (getLangOpts().MicrosoftExt) {
2415 Ident_sealed = &PP.getIdentifierTable().get(Name: "sealed");
2416 Ident_abstract = &PP.getIdentifierTable().get(Name: "abstract");
2417 }
2418 Ident_override = &PP.getIdentifierTable().get(Name: "override");
2419 }
2420
2421 if (II == Ident_override)
2422 return VirtSpecifiers::VS_Override;
2423
2424 if (II == Ident_sealed)
2425 return VirtSpecifiers::VS_Sealed;
2426
2427 if (II == Ident_abstract)
2428 return VirtSpecifiers::VS_Abstract;
2429
2430 if (II == Ident_final)
2431 return VirtSpecifiers::VS_Final;
2432
2433 if (II == Ident_GNU_final)
2434 return VirtSpecifiers::VS_GNU_Final;
2435
2436 return VirtSpecifiers::VS_None;
2437}
2438
2439void Parser::ParseOptionalCXX11VirtSpecifierSeq(VirtSpecifiers &VS,
2440 bool IsInterface,
2441 SourceLocation FriendLoc) {
2442 while (true) {
2443 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
2444 if (Specifier == VirtSpecifiers::VS_None)
2445 return;
2446
2447 if (FriendLoc.isValid()) {
2448 Diag(Tok.getLocation(), diag::err_friend_decl_spec)
2449 << VirtSpecifiers::getSpecifierName(Specifier)
2450 << FixItHint::CreateRemoval(Tok.getLocation())
2451 << SourceRange(FriendLoc, FriendLoc);
2452 ConsumeToken();
2453 continue;
2454 }
2455
2456 // C++ [class.mem]p8:
2457 // A virt-specifier-seq shall contain at most one of each virt-specifier.
2458 const char *PrevSpec = nullptr;
2459 if (VS.SetSpecifier(Specifier, Tok.getLocation(), PrevSpec))
2460 Diag(Tok.getLocation(), diag::err_duplicate_virt_specifier)
2461 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2462
2463 if (IsInterface && (Specifier == VirtSpecifiers::VS_Final ||
2464 Specifier == VirtSpecifiers::VS_Sealed)) {
2465 Diag(Tok.getLocation(), diag::err_override_control_interface)
2466 << VirtSpecifiers::getSpecifierName(Specifier);
2467 } else if (Specifier == VirtSpecifiers::VS_Sealed) {
2468 Diag(Tok.getLocation(), diag::ext_ms_sealed_keyword);
2469 } else if (Specifier == VirtSpecifiers::VS_Abstract) {
2470 Diag(Tok.getLocation(), diag::ext_ms_abstract_keyword);
2471 } else if (Specifier == VirtSpecifiers::VS_GNU_Final) {
2472 Diag(Tok.getLocation(), diag::ext_warn_gnu_final);
2473 } else {
2474 Diag(Tok.getLocation(),
2475 getLangOpts().CPlusPlus11
2476 ? diag::warn_cxx98_compat_override_control_keyword
2477 : diag::ext_override_control_keyword)
2478 << VirtSpecifiers::getSpecifierName(Specifier);
2479 }
2480 ConsumeToken();
2481 }
2482}
2483
2484bool Parser::isCXX11FinalKeyword() const {
2485 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier();
2486 return Specifier == VirtSpecifiers::VS_Final ||
2487 Specifier == VirtSpecifiers::VS_GNU_Final ||
2488 Specifier == VirtSpecifiers::VS_Sealed;
2489}
2490
2491bool Parser::isCXX2CTriviallyRelocatableKeyword(Token Tok) const {
2492 if (!getLangOpts().CPlusPlus || Tok.isNot(K: tok::identifier))
2493 return false;
2494 if (!Ident_trivially_relocatable_if_eligible)
2495 Ident_trivially_relocatable_if_eligible =
2496 &PP.getIdentifierTable().get(Name: "trivially_relocatable_if_eligible");
2497 IdentifierInfo *II = Tok.getIdentifierInfo();
2498 return II == Ident_trivially_relocatable_if_eligible;
2499}
2500
2501bool Parser::isCXX2CTriviallyRelocatableKeyword() const {
2502 return isCXX2CTriviallyRelocatableKeyword(Tok);
2503}
2504
2505void Parser::ParseCXX2CTriviallyRelocatableSpecifier(SourceLocation &TRS) {
2506 assert(isCXX2CTriviallyRelocatableKeyword() &&
2507 "expected a trivially_relocatable specifier");
2508
2509 Diag(Tok.getLocation(), getLangOpts().CPlusPlus26
2510 ? diag::warn_relocatable_keyword
2511 : diag::ext_relocatable_keyword)
2512 << /*relocatable*/ 0;
2513
2514 TRS = ConsumeToken();
2515}
2516
2517bool Parser::isCXX2CReplaceableKeyword(Token Tok) const {
2518 if (!getLangOpts().CPlusPlus || Tok.isNot(K: tok::identifier))
2519 return false;
2520 if (!Ident_replaceable_if_eligible)
2521 Ident_replaceable_if_eligible =
2522 &PP.getIdentifierTable().get(Name: "replaceable_if_eligible");
2523 IdentifierInfo *II = Tok.getIdentifierInfo();
2524 return II == Ident_replaceable_if_eligible;
2525}
2526
2527bool Parser::isCXX2CReplaceableKeyword() const {
2528 return isCXX2CReplaceableKeyword(Tok);
2529}
2530
2531void Parser::ParseCXX2CReplaceableSpecifier(SourceLocation &MRS) {
2532 assert(isCXX2CReplaceableKeyword() &&
2533 "expected a replaceable_if_eligible specifier");
2534
2535 Diag(Tok.getLocation(), getLangOpts().CPlusPlus26
2536 ? diag::warn_relocatable_keyword
2537 : diag::ext_relocatable_keyword)
2538 << /*replaceable*/ 1;
2539
2540 MRS = ConsumeToken();
2541}
2542
2543bool Parser::isClassCompatibleKeyword(Token Tok) const {
2544 if (isCXX2CTriviallyRelocatableKeyword(Tok) || isCXX2CReplaceableKeyword(Tok))
2545 return true;
2546 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
2547 return Specifier == VirtSpecifiers::VS_Final ||
2548 Specifier == VirtSpecifiers::VS_GNU_Final ||
2549 Specifier == VirtSpecifiers::VS_Sealed ||
2550 Specifier == VirtSpecifiers::VS_Abstract;
2551}
2552
2553bool Parser::isClassCompatibleKeyword() const {
2554 return isClassCompatibleKeyword(Tok);
2555}
2556
2557/// Parse a C++ member-declarator up to, but not including, the optional
2558/// brace-or-equal-initializer or pure-specifier.
2559bool Parser::ParseCXXMemberDeclaratorBeforeInitializer(
2560 Declarator &DeclaratorInfo, VirtSpecifiers &VS, ExprResult &BitfieldSize,
2561 LateParsedAttrList &LateParsedAttrs) {
2562 // member-declarator:
2563 // declarator virt-specifier-seq[opt] pure-specifier[opt]
2564 // declarator requires-clause
2565 // declarator brace-or-equal-initializer[opt]
2566 // identifier attribute-specifier-seq[opt] ':' constant-expression
2567 // brace-or-equal-initializer[opt]
2568 // ':' constant-expression
2569 //
2570 // NOTE: the latter two productions are a proposed bugfix rather than the
2571 // current grammar rules as of C++20.
2572 if (Tok.isNot(K: tok::colon))
2573 ParseDeclarator(D&: DeclaratorInfo);
2574 else
2575 DeclaratorInfo.SetIdentifier(Id: nullptr, IdLoc: Tok.getLocation());
2576
2577 if (getLangOpts().HLSL)
2578 MaybeParseHLSLAnnotations(D&: DeclaratorInfo, EndLoc: nullptr,
2579 /*CouldBeBitField*/ true);
2580
2581 if (!DeclaratorInfo.isFunctionDeclarator() && TryConsumeToken(Expected: tok::colon)) {
2582 assert(DeclaratorInfo.isPastIdentifier() &&
2583 "don't know where identifier would go yet?");
2584 BitfieldSize = ParseConstantExpression();
2585 if (BitfieldSize.isInvalid())
2586 SkipUntil(T: tok::comma, Flags: StopAtSemi | StopBeforeMatch);
2587 } else if (Tok.is(K: tok::kw_requires)) {
2588 ParseTrailingRequiresClause(D&: DeclaratorInfo);
2589 } else {
2590 ParseOptionalCXX11VirtSpecifierSeq(
2591 VS, IsInterface: getCurrentClass().IsInterface,
2592 FriendLoc: DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
2593 if (!VS.isUnset())
2594 MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(D&: DeclaratorInfo,
2595 VS);
2596 }
2597
2598 // If a simple-asm-expr is present, parse it.
2599 if (Tok.is(K: tok::kw_asm)) {
2600 SourceLocation Loc;
2601 ExprResult AsmLabel(ParseSimpleAsm(/*ForAsmLabel*/ true, EndLoc: &Loc));
2602 if (AsmLabel.isInvalid())
2603 SkipUntil(T: tok::comma, Flags: StopAtSemi | StopBeforeMatch);
2604
2605 DeclaratorInfo.setAsmLabel(AsmLabel.get());
2606 DeclaratorInfo.SetRangeEnd(Loc);
2607 }
2608
2609 // If attributes exist after the declarator, but before an '{', parse them.
2610 // However, this does not apply for [[]] attributes (which could show up
2611 // before or after the __attribute__ attributes).
2612 DiagnoseAndSkipCXX11Attributes();
2613 MaybeParseGNUAttributes(D&: DeclaratorInfo, LateAttrs: &LateParsedAttrs);
2614 DiagnoseAndSkipCXX11Attributes();
2615
2616 // For compatibility with code written to older Clang, also accept a
2617 // virt-specifier *after* the GNU attributes.
2618 if (BitfieldSize.isUnset() && VS.isUnset()) {
2619 ParseOptionalCXX11VirtSpecifierSeq(
2620 VS, IsInterface: getCurrentClass().IsInterface,
2621 FriendLoc: DeclaratorInfo.getDeclSpec().getFriendSpecLoc());
2622 if (!VS.isUnset()) {
2623 // If we saw any GNU-style attributes that are known to GCC followed by a
2624 // virt-specifier, issue a GCC-compat warning.
2625 for (const ParsedAttr &AL : DeclaratorInfo.getAttributes())
2626 if (AL.isKnownToGCC() && !AL.isCXX11Attribute())
2627 Diag(AL.getLoc(), diag::warn_gcc_attribute_location);
2628
2629 MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(D&: DeclaratorInfo,
2630 VS);
2631 }
2632 }
2633
2634 // If this has neither a name nor a bit width, something has gone seriously
2635 // wrong. Skip until the semi-colon or }.
2636 if (!DeclaratorInfo.hasName() && BitfieldSize.isUnset()) {
2637 // If so, skip until the semi-colon or a }.
2638 SkipUntil(T: tok::r_brace, Flags: StopAtSemi | StopBeforeMatch);
2639 return true;
2640 }
2641 return false;
2642}
2643
2644void Parser::MaybeParseAndDiagnoseDeclSpecAfterCXX11VirtSpecifierSeq(
2645 Declarator &D, VirtSpecifiers &VS) {
2646 DeclSpec DS(AttrFactory);
2647
2648 // GNU-style and C++11 attributes are not allowed here, but they will be
2649 // handled by the caller. Diagnose everything else.
2650 ParseTypeQualifierListOpt(
2651 DS, AttrReqs: AR_NoAttributesParsed, /*AtomicOrPtrauthAllowed=*/false,
2652 /*IdentifierRequired=*/false, CodeCompletionHandler: [&]() {
2653 Actions.CodeCompletion().CodeCompleteFunctionQualifiers(DS, D, VS: &VS);
2654 });
2655 D.ExtendWithDeclSpec(DS);
2656
2657 if (D.isFunctionDeclarator()) {
2658 auto &Function = D.getFunctionTypeInfo();
2659 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
2660 auto DeclSpecCheck = [&](DeclSpec::TQ TypeQual, StringRef FixItName,
2661 SourceLocation SpecLoc) {
2662 FixItHint Insertion;
2663 auto &MQ = Function.getOrCreateMethodQualifiers();
2664 if (!(MQ.getTypeQualifiers() & TypeQual)) {
2665 std::string Name(FixItName.data());
2666 Name += " ";
2667 Insertion = FixItHint::CreateInsertion(InsertionLoc: VS.getFirstLocation(), Code: Name);
2668 MQ.SetTypeQual(T: TypeQual, Loc: SpecLoc);
2669 }
2670 Diag(SpecLoc, diag::err_declspec_after_virtspec)
2671 << FixItName
2672 << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier())
2673 << FixItHint::CreateRemoval(SpecLoc) << Insertion;
2674 };
2675 DS.forEachQualifier(Handle: DeclSpecCheck);
2676 }
2677
2678 // Parse ref-qualifiers.
2679 bool RefQualifierIsLValueRef = true;
2680 SourceLocation RefQualifierLoc;
2681 if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc)) {
2682 const char *Name = (RefQualifierIsLValueRef ? "& " : "&& ");
2683 FixItHint Insertion =
2684 FixItHint::CreateInsertion(InsertionLoc: VS.getFirstLocation(), Code: Name);
2685 Function.RefQualifierIsLValueRef = RefQualifierIsLValueRef;
2686 Function.RefQualifierLoc = RefQualifierLoc;
2687
2688 Diag(RefQualifierLoc, diag::err_declspec_after_virtspec)
2689 << (RefQualifierIsLValueRef ? "&" : "&&")
2690 << VirtSpecifiers::getSpecifierName(VS.getLastSpecifier())
2691 << FixItHint::CreateRemoval(RefQualifierLoc) << Insertion;
2692 D.SetRangeEnd(RefQualifierLoc);
2693 }
2694 }
2695}
2696
2697Parser::DeclGroupPtrTy Parser::ParseCXXClassMemberDeclaration(
2698 AccessSpecifier AS, ParsedAttributes &AccessAttrs,
2699 ParsedTemplateInfo &TemplateInfo, ParsingDeclRAIIObject *TemplateDiags) {
2700 assert(getLangOpts().CPlusPlus &&
2701 "ParseCXXClassMemberDeclaration should only be called in C++ mode");
2702 if (Tok.is(K: tok::at)) {
2703 if (getLangOpts().ObjC && NextToken().isObjCAtKeyword(tok::objc_defs))
2704 Diag(Tok, diag::err_at_defs_cxx);
2705 else
2706 Diag(Tok, diag::err_at_in_class);
2707
2708 ConsumeToken();
2709 SkipUntil(T: tok::r_brace, Flags: StopAtSemi);
2710 return nullptr;
2711 }
2712
2713 // Turn on colon protection early, while parsing declspec, although there is
2714 // nothing to protect there. It prevents from false errors if error recovery
2715 // incorrectly determines where the declspec ends, as in the example:
2716 // struct A { enum class B { C }; };
2717 // const int C = 4;
2718 // struct D { A::B : C; };
2719 ColonProtectionRAIIObject X(*this);
2720
2721 // Access declarations.
2722 bool MalformedTypeSpec = false;
2723 if (TemplateInfo.Kind == ParsedTemplateKind::NonTemplate &&
2724 Tok.isOneOf(K1: tok::identifier, Ks: tok::coloncolon, Ks: tok::kw___super)) {
2725 if (TryAnnotateCXXScopeToken())
2726 MalformedTypeSpec = true;
2727
2728 bool isAccessDecl;
2729 if (Tok.isNot(K: tok::annot_cxxscope))
2730 isAccessDecl = false;
2731 else if (NextToken().is(K: tok::identifier))
2732 isAccessDecl = GetLookAheadToken(N: 2).is(K: tok::semi);
2733 else
2734 isAccessDecl = NextToken().is(K: tok::kw_operator);
2735
2736 if (isAccessDecl) {
2737 // Collect the scope specifier token we annotated earlier.
2738 CXXScopeSpec SS;
2739 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
2740 /*ObjectHasErrors=*/false,
2741 /*EnteringContext=*/false);
2742
2743 if (SS.isInvalid()) {
2744 SkipUntil(T: tok::semi);
2745 return nullptr;
2746 }
2747
2748 // Try to parse an unqualified-id.
2749 SourceLocation TemplateKWLoc;
2750 UnqualifiedId Name;
2751 if (ParseUnqualifiedId(SS, /*ObjectType=*/nullptr,
2752 /*ObjectHadErrors=*/false, EnteringContext: false, AllowDestructorName: true, AllowConstructorName: true,
2753 AllowDeductionGuide: false, TemplateKWLoc: &TemplateKWLoc, Result&: Name)) {
2754 SkipUntil(T: tok::semi);
2755 return nullptr;
2756 }
2757
2758 // TODO: recover from mistakenly-qualified operator declarations.
2759 if (ExpectAndConsume(tok::semi, diag::err_expected_after,
2760 "access declaration")) {
2761 SkipUntil(T: tok::semi);
2762 return nullptr;
2763 }
2764
2765 // FIXME: We should do something with the 'template' keyword here.
2766 return DeclGroupPtrTy::make(P: DeclGroupRef(Actions.ActOnUsingDeclaration(
2767 CurScope: getCurScope(), AS, /*UsingLoc*/ SourceLocation(),
2768 /*TypenameLoc*/ SourceLocation(), SS, Name,
2769 /*EllipsisLoc*/ SourceLocation(),
2770 /*AttrList*/ ParsedAttributesView())));
2771 }
2772 }
2773
2774 // static_assert-declaration. A templated static_assert declaration is
2775 // diagnosed in Parser::ParseDeclarationAfterTemplate.
2776 if (TemplateInfo.Kind == ParsedTemplateKind::NonTemplate &&
2777 Tok.isOneOf(K1: tok::kw_static_assert, K2: tok::kw__Static_assert)) {
2778 SourceLocation DeclEnd;
2779 return DeclGroupPtrTy::make(
2780 P: DeclGroupRef(ParseStaticAssertDeclaration(DeclEnd)));
2781 }
2782
2783 if (Tok.is(K: tok::kw_template)) {
2784 assert(!TemplateInfo.TemplateParams &&
2785 "Nested template improperly parsed?");
2786 ObjCDeclContextSwitch ObjCDC(*this);
2787 SourceLocation DeclEnd;
2788 return ParseTemplateDeclarationOrSpecialization(Context: DeclaratorContext::Member,
2789 DeclEnd, AccessAttrs, AS);
2790 }
2791
2792 // Handle: member-declaration ::= '__extension__' member-declaration
2793 if (Tok.is(K: tok::kw___extension__)) {
2794 // __extension__ silences extension warnings in the subexpression.
2795 ExtensionRAIIObject O(Diags); // Use RAII to do this.
2796 ConsumeToken();
2797 return ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo,
2798 TemplateDiags);
2799 }
2800
2801 ParsedAttributes DeclAttrs(AttrFactory);
2802 // Optional C++11 attribute-specifier
2803 MaybeParseCXX11Attributes(Attrs&: DeclAttrs);
2804
2805 // The next token may be an OpenMP pragma annotation token. That would
2806 // normally be handled from ParseCXXClassMemberDeclarationWithPragmas, but in
2807 // this case, it came from an *attribute* rather than a pragma. Handle it now.
2808 if (Tok.is(K: tok::annot_attr_openmp))
2809 return ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs&: DeclAttrs);
2810
2811 if (Tok.is(K: tok::kw_using)) {
2812 // Eat 'using'.
2813 SourceLocation UsingLoc = ConsumeToken();
2814
2815 // Consume unexpected 'template' keywords.
2816 while (Tok.is(K: tok::kw_template)) {
2817 SourceLocation TemplateLoc = ConsumeToken();
2818 Diag(TemplateLoc, diag::err_unexpected_template_after_using)
2819 << FixItHint::CreateRemoval(TemplateLoc);
2820 }
2821
2822 if (Tok.is(K: tok::kw_namespace)) {
2823 Diag(UsingLoc, diag::err_using_namespace_in_class);
2824 SkipUntil(T: tok::semi, Flags: StopBeforeMatch);
2825 return nullptr;
2826 }
2827 SourceLocation DeclEnd;
2828 // Otherwise, it must be a using-declaration or an alias-declaration.
2829 return ParseUsingDeclaration(Context: DeclaratorContext::Member, TemplateInfo,
2830 UsingLoc, DeclEnd, PrefixAttrs&: DeclAttrs, AS);
2831 }
2832
2833 ParsedAttributes DeclSpecAttrs(AttrFactory);
2834 // Hold late-parsed attributes so we can attach a Decl to them later.
2835 LateParsedAttrList CommonLateParsedAttrs;
2836
2837 while (MaybeParseCXX11Attributes(Attrs&: DeclAttrs) ||
2838 MaybeParseGNUAttributes(Attrs&: DeclSpecAttrs, LateAttrs: &CommonLateParsedAttrs) ||
2839 MaybeParseMicrosoftAttributes(Attrs&: DeclSpecAttrs))
2840 ;
2841
2842 SourceLocation DeclStart;
2843 if (DeclAttrs.Range.isValid()) {
2844 DeclStart = DeclSpecAttrs.Range.isInvalid()
2845 ? DeclAttrs.Range.getBegin()
2846 : std::min(a: DeclAttrs.Range.getBegin(),
2847 b: DeclSpecAttrs.Range.getBegin());
2848 } else {
2849 DeclStart = DeclSpecAttrs.Range.getBegin();
2850 }
2851
2852 // decl-specifier-seq:
2853 // Parse the common declaration-specifiers piece.
2854 ParsingDeclSpec DS(*this, TemplateDiags);
2855 DS.takeAttributesFrom(attrs&: DeclSpecAttrs);
2856
2857 if (MalformedTypeSpec)
2858 DS.SetTypeSpecError();
2859
2860 // Turn off usual access checking for templates explicit specialization
2861 // and instantiation.
2862 // C++20 [temp.spec] 13.9/6.
2863 // This disables the access checking rules for member function template
2864 // explicit instantiation and explicit specialization.
2865 bool IsTemplateSpecOrInst =
2866 (TemplateInfo.Kind == ParsedTemplateKind::ExplicitInstantiation ||
2867 TemplateInfo.Kind == ParsedTemplateKind::ExplicitSpecialization);
2868 SuppressAccessChecks diagsFromTag(*this, IsTemplateSpecOrInst);
2869
2870 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC: DeclSpecContext::DSC_class,
2871 LateAttrs: &CommonLateParsedAttrs);
2872
2873 if (IsTemplateSpecOrInst)
2874 diagsFromTag.done();
2875
2876 // Turn off colon protection that was set for declspec.
2877 X.restore();
2878
2879 if (DeclStart.isValid())
2880 DS.SetRangeStart(DeclStart);
2881
2882 // If we had a free-standing type definition with a missing semicolon, we
2883 // may get this far before the problem becomes obvious.
2884 if (DS.hasTagDefinition() &&
2885 TemplateInfo.Kind == ParsedTemplateKind::NonTemplate &&
2886 DiagnoseMissingSemiAfterTagDefinition(DS, AS, DSContext: DeclSpecContext::DSC_class,
2887 LateAttrs: &CommonLateParsedAttrs))
2888 return nullptr;
2889
2890 MultiTemplateParamsArg TemplateParams(
2891 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->data()
2892 : nullptr,
2893 TemplateInfo.TemplateParams ? TemplateInfo.TemplateParams->size() : 0);
2894
2895 if (TryConsumeToken(Expected: tok::semi)) {
2896 if (DS.isFriendSpecified())
2897 ProhibitAttributes(Attrs&: DeclAttrs);
2898
2899 RecordDecl *AnonRecord = nullptr;
2900 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(
2901 S: getCurScope(), AS, DS, DeclAttrs, TemplateParams, IsExplicitInstantiation: false, AnonRecord);
2902 Actions.ActOnDefinedDeclarationSpecifier(D: TheDecl);
2903 DS.complete(D: TheDecl);
2904 if (AnonRecord) {
2905 Decl *decls[] = {AnonRecord, TheDecl};
2906 return Actions.BuildDeclaratorGroup(decls);
2907 }
2908 return Actions.ConvertDeclToDeclGroup(Ptr: TheDecl);
2909 }
2910
2911 if (DS.hasTagDefinition())
2912 Actions.ActOnDefinedDeclarationSpecifier(D: DS.getRepAsDecl());
2913
2914 // Handle C++26's variadic friend declarations. These don't even have
2915 // declarators, so we get them out of the way early here.
2916 if (DS.isFriendSpecifiedFirst() && Tok.isOneOf(K1: tok::comma, K2: tok::ellipsis)) {
2917 Diag(Tok.getLocation(), getLangOpts().CPlusPlus26
2918 ? diag::warn_cxx23_variadic_friends
2919 : diag::ext_variadic_friends);
2920
2921 SourceLocation FriendLoc = DS.getFriendSpecLoc();
2922 SmallVector<Decl *> Decls;
2923
2924 // Handles a single friend-type-specifier.
2925 auto ParsedFriendDecl = [&](ParsingDeclSpec &DeclSpec) {
2926 SourceLocation VariadicLoc;
2927 TryConsumeToken(Expected: tok::ellipsis, Loc&: VariadicLoc);
2928
2929 RecordDecl *AnonRecord = nullptr;
2930 Decl *D = Actions.ParsedFreeStandingDeclSpec(
2931 S: getCurScope(), AS, DS&: DeclSpec, DeclAttrs, TemplateParams, IsExplicitInstantiation: false,
2932 AnonRecord, EllipsisLoc: VariadicLoc);
2933 DeclSpec.complete(D);
2934 if (!D) {
2935 SkipUntil(T1: tok::semi, T2: tok::r_brace);
2936 return true;
2937 }
2938
2939 Decls.push_back(Elt: D);
2940 return false;
2941 };
2942
2943 if (ParsedFriendDecl(DS))
2944 return nullptr;
2945
2946 while (TryConsumeToken(Expected: tok::comma)) {
2947 ParsingDeclSpec DeclSpec(*this, TemplateDiags);
2948 const char *PrevSpec = nullptr;
2949 unsigned DiagId = 0;
2950 DeclSpec.SetFriendSpec(Loc: FriendLoc, PrevSpec, DiagID&: DiagId);
2951 ParseDeclarationSpecifiers(DS&: DeclSpec, TemplateInfo, AS,
2952 DSC: DeclSpecContext::DSC_class, LateAttrs: nullptr);
2953 if (ParsedFriendDecl(DeclSpec))
2954 return nullptr;
2955 }
2956
2957 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt,
2958 "friend declaration");
2959
2960 return Actions.BuildDeclaratorGroup(Group: Decls);
2961 }
2962
2963 // Befriending a concept is invalid and would already fail if
2964 // we did nothing here, but this allows us to issue a more
2965 // helpful diagnostic.
2966 if (Tok.is(K: tok::kw_concept)) {
2967 Diag(
2968 Tok.getLocation(),
2969 DS.isFriendSpecified() || NextToken().is(tok::kw_friend)
2970 ? llvm::to_underlying(diag::err_friend_concept)
2971 : llvm::to_underlying(
2972 diag::
2973 err_concept_decls_may_only_appear_in_global_namespace_scope));
2974 SkipUntil(T1: tok::semi, T2: tok::r_brace, Flags: StopBeforeMatch);
2975 return nullptr;
2976 }
2977
2978 ParsingDeclarator DeclaratorInfo(*this, DS, DeclAttrs,
2979 DeclaratorContext::Member);
2980 if (TemplateInfo.TemplateParams)
2981 DeclaratorInfo.setTemplateParameterLists(TemplateParams);
2982 VirtSpecifiers VS;
2983
2984 // Hold late-parsed attributes so we can attach a Decl to them later.
2985 LateParsedAttrList LateParsedAttrs;
2986
2987 SourceLocation EqualLoc;
2988 SourceLocation PureSpecLoc;
2989
2990 auto TryConsumePureSpecifier = [&](bool AllowDefinition) {
2991 if (Tok.isNot(K: tok::equal))
2992 return false;
2993
2994 auto &Zero = NextToken();
2995 SmallString<8> Buffer;
2996 if (Zero.isNot(K: tok::numeric_constant) ||
2997 PP.getSpelling(Tok: Zero, Buffer) != "0")
2998 return false;
2999
3000 auto &After = GetLookAheadToken(N: 2);
3001 if (!After.isOneOf(K1: tok::semi, K2: tok::comma) &&
3002 !(AllowDefinition &&
3003 After.isOneOf(K1: tok::l_brace, Ks: tok::colon, Ks: tok::kw_try)))
3004 return false;
3005
3006 EqualLoc = ConsumeToken();
3007 PureSpecLoc = ConsumeToken();
3008 return true;
3009 };
3010
3011 SmallVector<Decl *, 8> DeclsInGroup;
3012 ExprResult BitfieldSize;
3013 ExprResult TrailingRequiresClause;
3014 bool ExpectSemi = true;
3015
3016 // C++20 [temp.spec] 13.9/6.
3017 // This disables the access checking rules for member function template
3018 // explicit instantiation and explicit specialization.
3019 SuppressAccessChecks SAC(*this, IsTemplateSpecOrInst);
3020
3021 // Parse the first declarator.
3022 if (ParseCXXMemberDeclaratorBeforeInitializer(
3023 DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs)) {
3024 TryConsumeToken(Expected: tok::semi);
3025 return nullptr;
3026 }
3027
3028 if (IsTemplateSpecOrInst)
3029 SAC.done();
3030
3031 // Check for a member function definition.
3032 if (BitfieldSize.isUnset()) {
3033 // MSVC permits pure specifier on inline functions defined at class scope.
3034 // Hence check for =0 before checking for function definition.
3035 if (getLangOpts().MicrosoftExt && DeclaratorInfo.isDeclarationOfFunction())
3036 TryConsumePureSpecifier(/*AllowDefinition*/ true);
3037
3038 FunctionDefinitionKind DefinitionKind = FunctionDefinitionKind::Declaration;
3039 // function-definition:
3040 //
3041 // In C++11, a non-function declarator followed by an open brace is a
3042 // braced-init-list for an in-class member initialization, not an
3043 // erroneous function definition.
3044 if (Tok.is(K: tok::l_brace) && !getLangOpts().CPlusPlus11) {
3045 DefinitionKind = FunctionDefinitionKind::Definition;
3046 } else if (DeclaratorInfo.isFunctionDeclarator()) {
3047 if (Tok.isOneOf(K1: tok::l_brace, Ks: tok::colon, Ks: tok::kw_try)) {
3048 DefinitionKind = FunctionDefinitionKind::Definition;
3049 } else if (Tok.is(K: tok::equal)) {
3050 const Token &KW = NextToken();
3051 if (KW.is(K: tok::kw_default))
3052 DefinitionKind = FunctionDefinitionKind::Defaulted;
3053 else if (KW.is(K: tok::kw_delete))
3054 DefinitionKind = FunctionDefinitionKind::Deleted;
3055 else if (KW.is(K: tok::code_completion)) {
3056 cutOffParsing();
3057 Actions.CodeCompletion().CodeCompleteAfterFunctionEquals(
3058 D&: DeclaratorInfo);
3059 return nullptr;
3060 }
3061 }
3062 }
3063 DeclaratorInfo.setFunctionDefinitionKind(DefinitionKind);
3064
3065 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
3066 // to a friend declaration, that declaration shall be a definition.
3067 if (DeclaratorInfo.isFunctionDeclarator() &&
3068 DefinitionKind == FunctionDefinitionKind::Declaration &&
3069 DS.isFriendSpecified()) {
3070 // Diagnose attributes that appear before decl specifier:
3071 // [[]] friend int foo();
3072 ProhibitAttributes(Attrs&: DeclAttrs);
3073 }
3074
3075 if (DefinitionKind != FunctionDefinitionKind::Declaration) {
3076 if (!DeclaratorInfo.isFunctionDeclarator()) {
3077 Diag(DeclaratorInfo.getIdentifierLoc(), diag::err_func_def_no_params);
3078 ConsumeBrace();
3079 SkipUntil(T: tok::r_brace);
3080
3081 // Consume the optional ';'
3082 TryConsumeToken(Expected: tok::semi);
3083
3084 return nullptr;
3085 }
3086
3087 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3088 Diag(DeclaratorInfo.getIdentifierLoc(),
3089 diag::err_function_declared_typedef);
3090
3091 // Recover by treating the 'typedef' as spurious.
3092 DS.ClearStorageClassSpecs();
3093 }
3094
3095 Decl *FunDecl = ParseCXXInlineMethodDef(AS, AccessAttrs, D&: DeclaratorInfo,
3096 TemplateInfo, VS, PureSpecLoc);
3097
3098 if (FunDecl) {
3099 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i) {
3100 CommonLateParsedAttrs[i]->addDecl(D: FunDecl);
3101 }
3102 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i) {
3103 LateParsedAttrs[i]->addDecl(D: FunDecl);
3104 }
3105 }
3106 LateParsedAttrs.clear();
3107
3108 // Consume the ';' - it's optional unless we have a delete or default
3109 if (Tok.is(K: tok::semi))
3110 ConsumeExtraSemi(Kind: ExtraSemiKind::AfterMemberFunctionDefinition);
3111
3112 return DeclGroupPtrTy::make(P: DeclGroupRef(FunDecl));
3113 }
3114 }
3115
3116 // member-declarator-list:
3117 // member-declarator
3118 // member-declarator-list ',' member-declarator
3119
3120 while (true) {
3121 InClassInitStyle HasInClassInit = ICIS_NoInit;
3122 bool HasStaticInitializer = false;
3123 if (Tok.isOneOf(K1: tok::equal, K2: tok::l_brace) && PureSpecLoc.isInvalid()) {
3124 // DRXXXX: Anonymous bit-fields cannot have a brace-or-equal-initializer.
3125 if (BitfieldSize.isUsable() && !DeclaratorInfo.hasName()) {
3126 // Diagnose the error and pretend there is no in-class initializer.
3127 Diag(Tok, diag::err_anon_bitfield_member_init);
3128 SkipUntil(T: tok::comma, Flags: StopAtSemi | StopBeforeMatch);
3129 } else if (DeclaratorInfo.isDeclarationOfFunction()) {
3130 // It's a pure-specifier.
3131 if (!TryConsumePureSpecifier(/*AllowFunctionDefinition*/ false))
3132 // Parse it as an expression so that Sema can diagnose it.
3133 HasStaticInitializer = true;
3134 } else if (DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
3135 DeclSpec::SCS_static &&
3136 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
3137 DeclSpec::SCS_typedef &&
3138 !DS.isFriendSpecified() &&
3139 TemplateInfo.Kind == ParsedTemplateKind::NonTemplate) {
3140 // It's a default member initializer.
3141 if (BitfieldSize.get())
3142 Diag(Tok, getLangOpts().CPlusPlus20
3143 ? diag::warn_cxx17_compat_bitfield_member_init
3144 : diag::ext_bitfield_member_init);
3145 HasInClassInit = Tok.is(K: tok::equal) ? ICIS_CopyInit : ICIS_ListInit;
3146 } else {
3147 HasStaticInitializer = true;
3148 }
3149 }
3150
3151 // NOTE: If Sema is the Action module and declarator is an instance field,
3152 // this call will *not* return the created decl; It will return null.
3153 // See Sema::ActOnCXXMemberDeclarator for details.
3154
3155 NamedDecl *ThisDecl = nullptr;
3156 if (DS.isFriendSpecified()) {
3157 // C++11 [dcl.attr.grammar] p4: If an attribute-specifier-seq appertains
3158 // to a friend declaration, that declaration shall be a definition.
3159 //
3160 // Diagnose attributes that appear in a friend member function declarator:
3161 // friend int foo [[]] ();
3162 for (const ParsedAttr &AL : DeclaratorInfo.getAttributes())
3163 if (AL.isCXX11Attribute() || AL.isRegularKeywordAttribute()) {
3164 auto Loc = AL.getRange().getBegin();
3165 (AL.isRegularKeywordAttribute()
3166 ? Diag(Loc, diag::err_keyword_not_allowed) << AL
3167 : Diag(Loc, diag::err_attributes_not_allowed))
3168 << AL.getRange();
3169 }
3170
3171 ThisDecl = Actions.ActOnFriendFunctionDecl(S: getCurScope(), D&: DeclaratorInfo,
3172 TemplateParams);
3173 } else {
3174 ThisDecl = Actions.ActOnCXXMemberDeclarator(
3175 S: getCurScope(), AS, D&: DeclaratorInfo, TemplateParameterLists: TemplateParams, BitfieldWidth: BitfieldSize.get(),
3176 VS, InitStyle: HasInClassInit);
3177
3178 if (VarTemplateDecl *VT =
3179 ThisDecl ? dyn_cast<VarTemplateDecl>(Val: ThisDecl) : nullptr)
3180 // Re-direct this decl to refer to the templated decl so that we can
3181 // initialize it.
3182 ThisDecl = VT->getTemplatedDecl();
3183
3184 if (ThisDecl)
3185 Actions.ProcessDeclAttributeList(getCurScope(), ThisDecl, AccessAttrs);
3186 }
3187
3188 // Error recovery might have converted a non-static member into a static
3189 // member.
3190 if (HasInClassInit != ICIS_NoInit &&
3191 DeclaratorInfo.getDeclSpec().getStorageClassSpec() ==
3192 DeclSpec::SCS_static) {
3193 HasInClassInit = ICIS_NoInit;
3194 HasStaticInitializer = true;
3195 }
3196
3197 if (PureSpecLoc.isValid() && VS.getAbstractLoc().isValid()) {
3198 Diag(PureSpecLoc, diag::err_duplicate_virt_specifier) << "abstract";
3199 }
3200 if (ThisDecl && PureSpecLoc.isValid())
3201 Actions.ActOnPureSpecifier(ThisDecl, PureSpecLoc);
3202 else if (ThisDecl && VS.getAbstractLoc().isValid())
3203 Actions.ActOnPureSpecifier(ThisDecl, VS.getAbstractLoc());
3204
3205 // Handle the initializer.
3206 if (HasInClassInit != ICIS_NoInit) {
3207 // The initializer was deferred; parse it and cache the tokens.
3208 Diag(Tok, getLangOpts().CPlusPlus11
3209 ? diag::warn_cxx98_compat_nonstatic_member_init
3210 : diag::ext_nonstatic_member_init);
3211
3212 if (DeclaratorInfo.isArrayOfUnknownBound()) {
3213 // C++11 [dcl.array]p3: An array bound may also be omitted when the
3214 // declarator is followed by an initializer.
3215 //
3216 // A brace-or-equal-initializer for a member-declarator is not an
3217 // initializer in the grammar, so this is ill-formed.
3218 Diag(Tok, diag::err_incomplete_array_member_init);
3219 SkipUntil(T: tok::comma, Flags: StopAtSemi | StopBeforeMatch);
3220
3221 // Avoid later warnings about a class member of incomplete type.
3222 if (ThisDecl)
3223 ThisDecl->setInvalidDecl();
3224 } else
3225 ParseCXXNonStaticMemberInitializer(ThisDecl);
3226 } else if (HasStaticInitializer) {
3227 // Normal initializer.
3228 ExprResult Init = ParseCXXMemberInitializer(
3229 ThisDecl, DeclaratorInfo.isDeclarationOfFunction(), EqualLoc);
3230
3231 if (Init.isInvalid()) {
3232 if (ThisDecl)
3233 Actions.ActOnUninitializedDecl(ThisDecl);
3234 SkipUntil(T: tok::comma, Flags: StopAtSemi | StopBeforeMatch);
3235 } else if (ThisDecl)
3236 Actions.AddInitializerToDecl(ThisDecl, Init.get(),
3237 EqualLoc.isInvalid());
3238 } else if (ThisDecl && DeclaratorInfo.isStaticMember())
3239 // No initializer.
3240 Actions.ActOnUninitializedDecl(ThisDecl);
3241
3242 if (ThisDecl) {
3243 if (!ThisDecl->isInvalidDecl()) {
3244 // Set the Decl for any late parsed attributes
3245 for (unsigned i = 0, ni = CommonLateParsedAttrs.size(); i < ni; ++i)
3246 CommonLateParsedAttrs[i]->addDecl(ThisDecl);
3247
3248 for (unsigned i = 0, ni = LateParsedAttrs.size(); i < ni; ++i)
3249 LateParsedAttrs[i]->addDecl(ThisDecl);
3250 }
3251 Actions.FinalizeDeclaration(ThisDecl);
3252 DeclsInGroup.push_back(ThisDecl);
3253
3254 if (DeclaratorInfo.isFunctionDeclarator() &&
3255 DeclaratorInfo.getDeclSpec().getStorageClassSpec() !=
3256 DeclSpec::SCS_typedef)
3257 HandleMemberFunctionDeclDelays(DeclaratorInfo, ThisDecl);
3258 }
3259 LateParsedAttrs.clear();
3260
3261 DeclaratorInfo.complete(ThisDecl);
3262
3263 // If we don't have a comma, it is either the end of the list (a ';')
3264 // or an error, bail out.
3265 SourceLocation CommaLoc;
3266 if (!TryConsumeToken(Expected: tok::comma, Loc&: CommaLoc))
3267 break;
3268
3269 if (Tok.isAtStartOfLine() &&
3270 !MightBeDeclarator(Context: DeclaratorContext::Member)) {
3271 // This comma was followed by a line-break and something which can't be
3272 // the start of a declarator. The comma was probably a typo for a
3273 // semicolon.
3274 Diag(CommaLoc, diag::err_expected_semi_declaration)
3275 << FixItHint::CreateReplacement(CommaLoc, ";");
3276 ExpectSemi = false;
3277 break;
3278 }
3279
3280 // C++23 [temp.pre]p5:
3281 // In a template-declaration, explicit specialization, or explicit
3282 // instantiation the init-declarator-list in the declaration shall
3283 // contain at most one declarator.
3284 if (TemplateInfo.Kind != ParsedTemplateKind::NonTemplate &&
3285 DeclaratorInfo.isFirstDeclarator()) {
3286 Diag(CommaLoc, diag::err_multiple_template_declarators)
3287 << TemplateInfo.Kind;
3288 }
3289
3290 // Parse the next declarator.
3291 DeclaratorInfo.clear();
3292 VS.clear();
3293 BitfieldSize = ExprResult(/*Invalid=*/false);
3294 EqualLoc = PureSpecLoc = SourceLocation();
3295 DeclaratorInfo.setCommaLoc(CommaLoc);
3296
3297 // GNU attributes are allowed before the second and subsequent declarator.
3298 // However, this does not apply for [[]] attributes (which could show up
3299 // before or after the __attribute__ attributes).
3300 DiagnoseAndSkipCXX11Attributes();
3301 MaybeParseGNUAttributes(D&: DeclaratorInfo);
3302 DiagnoseAndSkipCXX11Attributes();
3303
3304 if (ParseCXXMemberDeclaratorBeforeInitializer(
3305 DeclaratorInfo, VS, BitfieldSize, LateParsedAttrs))
3306 break;
3307 }
3308
3309 if (ExpectSemi &&
3310 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list)) {
3311 // Skip to end of block or statement.
3312 SkipUntil(T: tok::r_brace, Flags: StopAtSemi | StopBeforeMatch);
3313 // If we stopped at a ';', eat it.
3314 TryConsumeToken(Expected: tok::semi);
3315 return nullptr;
3316 }
3317
3318 return Actions.FinalizeDeclaratorGroup(S: getCurScope(), DS, Group: DeclsInGroup);
3319}
3320
3321ExprResult Parser::ParseCXXMemberInitializer(Decl *D, bool IsFunction,
3322 SourceLocation &EqualLoc) {
3323 assert(Tok.isOneOf(tok::equal, tok::l_brace) &&
3324 "Data member initializer not starting with '=' or '{'");
3325
3326 bool IsFieldInitialization = isa_and_present<FieldDecl>(Val: D);
3327
3328 EnterExpressionEvaluationContext Context(
3329 Actions,
3330 IsFieldInitialization
3331 ? Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed
3332 : Sema::ExpressionEvaluationContext::PotentiallyEvaluated,
3333 D);
3334
3335 // CWG2760
3336 // Default member initializers used to initialize a base or member subobject
3337 // [...] are considered to be part of the function body
3338 Actions.ExprEvalContexts.back().InImmediateEscalatingFunctionContext =
3339 IsFieldInitialization;
3340
3341 if (TryConsumeToken(Expected: tok::equal, Loc&: EqualLoc)) {
3342 if (Tok.is(K: tok::kw_delete)) {
3343 // In principle, an initializer of '= delete p;' is legal, but it will
3344 // never type-check. It's better to diagnose it as an ill-formed
3345 // expression than as an ill-formed deleted non-function member. An
3346 // initializer of '= delete p, foo' will never be parsed, because a
3347 // top-level comma always ends the initializer expression.
3348 const Token &Next = NextToken();
3349 if (IsFunction || Next.isOneOf(K1: tok::semi, Ks: tok::comma, Ks: tok::eof)) {
3350 if (IsFunction)
3351 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
3352 << 1 /* delete */;
3353 else
3354 Diag(ConsumeToken(), diag::err_deleted_non_function);
3355 SkipDeletedFunctionBody();
3356 return ExprError();
3357 }
3358 } else if (Tok.is(K: tok::kw_default)) {
3359 if (IsFunction)
3360 Diag(Tok, diag::err_default_delete_in_multiple_declaration)
3361 << 0 /* default */;
3362 else
3363 Diag(ConsumeToken(), diag::err_default_special_members)
3364 << getLangOpts().CPlusPlus20;
3365 return ExprError();
3366 }
3367 }
3368 if (const auto *PD = dyn_cast_or_null<MSPropertyDecl>(Val: D)) {
3369 Diag(Tok, diag::err_ms_property_initializer) << PD;
3370 return ExprError();
3371 }
3372 return ParseInitializer();
3373}
3374
3375void Parser::SkipCXXMemberSpecification(SourceLocation RecordLoc,
3376 SourceLocation AttrFixitLoc,
3377 unsigned TagType, Decl *TagDecl) {
3378 // Skip the optional 'final' keyword.
3379 while (isClassCompatibleKeyword())
3380 ConsumeToken();
3381
3382 // Diagnose any C++11 attributes after 'final' keyword.
3383 // We deliberately discard these attributes.
3384 ParsedAttributes Attrs(AttrFactory);
3385 CheckMisplacedCXX11Attribute(Attrs, CorrectLocation: AttrFixitLoc);
3386
3387 // This can only happen if we had malformed misplaced attributes;
3388 // we only get called if there is a colon or left-brace after the
3389 // attributes.
3390 if (Tok.isNot(K: tok::colon) && Tok.isNot(K: tok::l_brace))
3391 return;
3392
3393 // Skip the base clauses. This requires actually parsing them, because
3394 // otherwise we can't be sure where they end (a left brace may appear
3395 // within a template argument).
3396 if (Tok.is(K: tok::colon)) {
3397 // Enter the scope of the class so that we can correctly parse its bases.
3398 ParseScope ClassScope(this, Scope::ClassScope | Scope::DeclScope);
3399 ParsingClassDefinition ParsingDef(*this, TagDecl, /*NonNestedClass*/ true,
3400 TagType == DeclSpec::TST_interface);
3401 auto OldContext =
3402 Actions.ActOnTagStartSkippedDefinition(S: getCurScope(), TD: TagDecl);
3403
3404 // Parse the bases but don't attach them to the class.
3405 ParseBaseClause(ClassDecl: nullptr);
3406
3407 Actions.ActOnTagFinishSkippedDefinition(Context: OldContext);
3408
3409 if (!Tok.is(K: tok::l_brace)) {
3410 Diag(PP.getLocForEndOfToken(PrevTokLocation),
3411 diag::err_expected_lbrace_after_base_specifiers);
3412 return;
3413 }
3414 }
3415
3416 // Skip the body.
3417 assert(Tok.is(tok::l_brace));
3418 BalancedDelimiterTracker T(*this, tok::l_brace);
3419 T.consumeOpen();
3420 T.skipToEnd();
3421
3422 // Parse and discard any trailing attributes.
3423 if (Tok.is(K: tok::kw___attribute)) {
3424 ParsedAttributes Attrs(AttrFactory);
3425 MaybeParseGNUAttributes(Attrs);
3426 }
3427}
3428
3429Parser::DeclGroupPtrTy Parser::ParseCXXClassMemberDeclarationWithPragmas(
3430 AccessSpecifier &AS, ParsedAttributes &AccessAttrs, DeclSpec::TST TagType,
3431 Decl *TagDecl) {
3432 ParenBraceBracketBalancer BalancerRAIIObj(*this);
3433
3434 switch (Tok.getKind()) {
3435 case tok::kw___if_exists:
3436 case tok::kw___if_not_exists:
3437 ParseMicrosoftIfExistsClassDeclaration(TagType, AccessAttrs, CurAS&: AS);
3438 return nullptr;
3439
3440 case tok::semi:
3441 // Check for extraneous top-level semicolon.
3442 ConsumeExtraSemi(Kind: ExtraSemiKind::InsideStruct, T: TagType);
3443 return nullptr;
3444
3445 // Handle pragmas that can appear as member declarations.
3446 case tok::annot_pragma_vis:
3447 HandlePragmaVisibility();
3448 return nullptr;
3449 case tok::annot_pragma_pack:
3450 HandlePragmaPack();
3451 return nullptr;
3452 case tok::annot_pragma_align:
3453 HandlePragmaAlign();
3454 return nullptr;
3455 case tok::annot_pragma_ms_pointers_to_members:
3456 HandlePragmaMSPointersToMembers();
3457 return nullptr;
3458 case tok::annot_pragma_ms_pragma:
3459 HandlePragmaMSPragma();
3460 return nullptr;
3461 case tok::annot_pragma_ms_vtordisp:
3462 HandlePragmaMSVtorDisp();
3463 return nullptr;
3464 case tok::annot_pragma_dump:
3465 HandlePragmaDump();
3466 return nullptr;
3467
3468 case tok::kw_namespace:
3469 // If we see a namespace here, a close brace was missing somewhere.
3470 DiagnoseUnexpectedNamespace(Context: cast<NamedDecl>(Val: TagDecl));
3471 return nullptr;
3472
3473 case tok::kw_private:
3474 // FIXME: We don't accept GNU attributes on access specifiers in OpenCL mode
3475 // yet.
3476 if (getLangOpts().OpenCL && !NextToken().is(K: tok::colon)) {
3477 ParsedTemplateInfo TemplateInfo;
3478 return ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo);
3479 }
3480 [[fallthrough]];
3481 case tok::kw_public:
3482 case tok::kw_protected: {
3483 if (getLangOpts().HLSL)
3484 Diag(Tok.getLocation(), diag::ext_hlsl_access_specifiers);
3485 AccessSpecifier NewAS = getAccessSpecifierIfPresent();
3486 assert(NewAS != AS_none);
3487 // Current token is a C++ access specifier.
3488 AS = NewAS;
3489 SourceLocation ASLoc = Tok.getLocation();
3490 unsigned TokLength = Tok.getLength();
3491 ConsumeToken();
3492 AccessAttrs.clear();
3493 MaybeParseGNUAttributes(Attrs&: AccessAttrs);
3494
3495 SourceLocation EndLoc;
3496 if (TryConsumeToken(Expected: tok::colon, Loc&: EndLoc)) {
3497 } else if (TryConsumeToken(Expected: tok::semi, Loc&: EndLoc)) {
3498 Diag(EndLoc, diag::err_expected)
3499 << tok::colon << FixItHint::CreateReplacement(EndLoc, ":");
3500 } else {
3501 EndLoc = ASLoc.getLocWithOffset(Offset: TokLength);
3502 Diag(EndLoc, diag::err_expected)
3503 << tok::colon << FixItHint::CreateInsertion(EndLoc, ":");
3504 }
3505
3506 // The Microsoft extension __interface does not permit non-public
3507 // access specifiers.
3508 if (TagType == DeclSpec::TST_interface && AS != AS_public) {
3509 Diag(ASLoc, diag::err_access_specifier_interface) << (AS == AS_protected);
3510 }
3511
3512 if (Actions.ActOnAccessSpecifier(Access: NewAS, ASLoc, ColonLoc: EndLoc, Attrs: AccessAttrs)) {
3513 // found another attribute than only annotations
3514 AccessAttrs.clear();
3515 }
3516
3517 return nullptr;
3518 }
3519
3520 case tok::annot_attr_openmp:
3521 case tok::annot_pragma_openmp:
3522 return ParseOpenMPDeclarativeDirectiveWithExtDecl(
3523 AS, Attrs&: AccessAttrs, /*Delayed=*/true, TagType, TagDecl);
3524 case tok::annot_pragma_openacc:
3525 return ParseOpenACCDirectiveDecl(AS, Attrs&: AccessAttrs, TagType, TagDecl);
3526
3527 default:
3528 if (tok::isPragmaAnnotation(K: Tok.getKind())) {
3529 Diag(Tok.getLocation(), diag::err_pragma_misplaced_in_decl)
3530 << DeclSpec::getSpecifierName(
3531 TagType, Actions.getASTContext().getPrintingPolicy());
3532 ConsumeAnnotationToken();
3533 return nullptr;
3534 }
3535 ParsedTemplateInfo TemplateInfo;
3536 return ParseCXXClassMemberDeclaration(AS, AccessAttrs, TemplateInfo);
3537 }
3538}
3539
3540void Parser::ParseCXXMemberSpecification(SourceLocation RecordLoc,
3541 SourceLocation AttrFixitLoc,
3542 ParsedAttributes &Attrs,
3543 unsigned TagType, Decl *TagDecl) {
3544 assert((TagType == DeclSpec::TST_struct ||
3545 TagType == DeclSpec::TST_interface ||
3546 TagType == DeclSpec::TST_union || TagType == DeclSpec::TST_class) &&
3547 "Invalid TagType!");
3548
3549 llvm::TimeTraceScope TimeScope("ParseClass", [&]() {
3550 if (auto *TD = dyn_cast_or_null<NamedDecl>(Val: TagDecl))
3551 return TD->getQualifiedNameAsString();
3552 return std::string("<anonymous>");
3553 });
3554
3555 PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc,
3556 "parsing struct/union/class body");
3557
3558 // Determine whether this is a non-nested class. Note that local
3559 // classes are *not* considered to be nested classes.
3560 bool NonNestedClass = true;
3561 if (!ClassStack.empty()) {
3562 for (const Scope *S = getCurScope(); S; S = S->getParent()) {
3563 if (S->isClassScope()) {
3564 // We're inside a class scope, so this is a nested class.
3565 NonNestedClass = false;
3566
3567 // The Microsoft extension __interface does not permit nested classes.
3568 if (getCurrentClass().IsInterface) {
3569 Diag(RecordLoc, diag::err_invalid_member_in_interface)
3570 << /*ErrorType=*/6
3571 << (isa<NamedDecl>(TagDecl)
3572 ? cast<NamedDecl>(TagDecl)->getQualifiedNameAsString()
3573 : "(anonymous)");
3574 }
3575 break;
3576 }
3577
3578 if (S->isFunctionScope())
3579 // If we're in a function or function template then this is a local
3580 // class rather than a nested class.
3581 break;
3582 }
3583 }
3584
3585 // Enter a scope for the class.
3586 ParseScope ClassScope(this, Scope::ClassScope | Scope::DeclScope);
3587
3588 // Note that we are parsing a new (potentially-nested) class definition.
3589 ParsingClassDefinition ParsingDef(*this, TagDecl, NonNestedClass,
3590 TagType == DeclSpec::TST_interface);
3591
3592 if (TagDecl)
3593 Actions.ActOnTagStartDefinition(S: getCurScope(), TagDecl);
3594
3595 SourceLocation FinalLoc;
3596 SourceLocation AbstractLoc;
3597 bool IsFinalSpelledSealed = false;
3598 bool IsAbstract = false;
3599 SourceLocation TriviallyRelocatable;
3600 SourceLocation Replaceable;
3601
3602 // Parse the optional 'final' keyword.
3603 if (getLangOpts().CPlusPlus && Tok.is(K: tok::identifier)) {
3604 while (true) {
3605 VirtSpecifiers::Specifier Specifier = isCXX11VirtSpecifier(Tok);
3606 if (Specifier == VirtSpecifiers::VS_None) {
3607 if (isCXX2CTriviallyRelocatableKeyword(Tok)) {
3608 if (TriviallyRelocatable.isValid()) {
3609 auto Skipped = Tok;
3610 ConsumeToken();
3611 Diag(Skipped, diag::err_duplicate_class_relocation_specifier)
3612 << /*trivial_relocatable*/ 0 << TriviallyRelocatable;
3613 } else {
3614 ParseCXX2CTriviallyRelocatableSpecifier(TRS&: TriviallyRelocatable);
3615 }
3616 continue;
3617 }
3618 if (isCXX2CReplaceableKeyword(Tok)) {
3619 if (Replaceable.isValid()) {
3620 auto Skipped = Tok;
3621 ConsumeToken();
3622 Diag(Skipped, diag::err_duplicate_class_relocation_specifier)
3623 << /*replaceable*/ 1 << Replaceable;
3624 } else {
3625 ParseCXX2CReplaceableSpecifier(MRS&: Replaceable);
3626 }
3627 continue;
3628 }
3629 break;
3630 }
3631 if (isCXX11FinalKeyword()) {
3632 if (FinalLoc.isValid()) {
3633 auto Skipped = ConsumeToken();
3634 Diag(Skipped, diag::err_duplicate_class_virt_specifier)
3635 << VirtSpecifiers::getSpecifierName(Specifier);
3636 } else {
3637 FinalLoc = ConsumeToken();
3638 if (Specifier == VirtSpecifiers::VS_Sealed)
3639 IsFinalSpelledSealed = true;
3640 }
3641 } else {
3642 if (AbstractLoc.isValid()) {
3643 auto Skipped = ConsumeToken();
3644 Diag(Skipped, diag::err_duplicate_class_virt_specifier)
3645 << VirtSpecifiers::getSpecifierName(Specifier);
3646 } else {
3647 AbstractLoc = ConsumeToken();
3648 IsAbstract = true;
3649 }
3650 }
3651 if (TagType == DeclSpec::TST_interface)
3652 Diag(FinalLoc, diag::err_override_control_interface)
3653 << VirtSpecifiers::getSpecifierName(Specifier);
3654 else if (Specifier == VirtSpecifiers::VS_Final)
3655 Diag(FinalLoc, getLangOpts().CPlusPlus11
3656 ? diag::warn_cxx98_compat_override_control_keyword
3657 : diag::ext_override_control_keyword)
3658 << VirtSpecifiers::getSpecifierName(Specifier);
3659 else if (Specifier == VirtSpecifiers::VS_Sealed)
3660 Diag(FinalLoc, diag::ext_ms_sealed_keyword);
3661 else if (Specifier == VirtSpecifiers::VS_Abstract)
3662 Diag(AbstractLoc, diag::ext_ms_abstract_keyword);
3663 else if (Specifier == VirtSpecifiers::VS_GNU_Final)
3664 Diag(FinalLoc, diag::ext_warn_gnu_final);
3665 }
3666 assert((FinalLoc.isValid() || AbstractLoc.isValid() ||
3667 TriviallyRelocatable.isValid() || Replaceable.isValid()) &&
3668 "not a class definition");
3669
3670 // Parse any C++11 attributes after 'final' keyword.
3671 // These attributes are not allowed to appear here,
3672 // and the only possible place for them to appertain
3673 // to the class would be between class-key and class-name.
3674 CheckMisplacedCXX11Attribute(Attrs, CorrectLocation: AttrFixitLoc);
3675
3676 // ParseClassSpecifier() does only a superficial check for attributes before
3677 // deciding to call this method. For example, for
3678 // `class C final alignas ([l) {` it will decide that this looks like a
3679 // misplaced attribute since it sees `alignas '(' ')'`. But the actual
3680 // attribute parsing code will try to parse the '[' as a constexpr lambda
3681 // and consume enough tokens that the alignas parsing code will eat the
3682 // opening '{'. So bail out if the next token isn't one we expect.
3683 if (!Tok.is(K: tok::colon) && !Tok.is(K: tok::l_brace)) {
3684 if (TagDecl)
3685 Actions.ActOnTagDefinitionError(S: getCurScope(), TagDecl);
3686 return;
3687 }
3688 }
3689
3690 if (Tok.is(K: tok::colon)) {
3691 ParseScope InheritanceScope(this, getCurScope()->getFlags() |
3692 Scope::ClassInheritanceScope);
3693
3694 ParseBaseClause(ClassDecl: TagDecl);
3695 if (!Tok.is(K: tok::l_brace)) {
3696 bool SuggestFixIt = false;
3697 SourceLocation BraceLoc = PP.getLocForEndOfToken(Loc: PrevTokLocation);
3698 if (Tok.isAtStartOfLine()) {
3699 switch (Tok.getKind()) {
3700 case tok::kw_private:
3701 case tok::kw_protected:
3702 case tok::kw_public:
3703 SuggestFixIt = NextToken().getKind() == tok::colon;
3704 break;
3705 case tok::kw_static_assert:
3706 case tok::r_brace:
3707 case tok::kw_using:
3708 // base-clause can have simple-template-id; 'template' can't be there
3709 case tok::kw_template:
3710 SuggestFixIt = true;
3711 break;
3712 case tok::identifier:
3713 SuggestFixIt = isConstructorDeclarator(Unqualified: true);
3714 break;
3715 default:
3716 SuggestFixIt = isCXXSimpleDeclaration(/*AllowForRangeDecl=*/false);
3717 break;
3718 }
3719 }
3720 DiagnosticBuilder LBraceDiag =
3721 Diag(BraceLoc, diag::err_expected_lbrace_after_base_specifiers);
3722 if (SuggestFixIt) {
3723 LBraceDiag << FixItHint::CreateInsertion(InsertionLoc: BraceLoc, Code: " {");
3724 // Try recovering from missing { after base-clause.
3725 PP.EnterToken(Tok, /*IsReinject*/ true);
3726 Tok.setKind(tok::l_brace);
3727 } else {
3728 if (TagDecl)
3729 Actions.ActOnTagDefinitionError(S: getCurScope(), TagDecl);
3730 return;
3731 }
3732 }
3733 }
3734
3735 assert(Tok.is(tok::l_brace));
3736 BalancedDelimiterTracker T(*this, tok::l_brace);
3737 T.consumeOpen();
3738
3739 if (TagDecl)
3740 Actions.ActOnStartCXXMemberDeclarations(
3741 S: getCurScope(), TagDecl, FinalLoc, IsFinalSpelledSealed, IsAbstract,
3742 TriviallyRelocatable, Replaceable, LBraceLoc: T.getOpenLocation());
3743
3744 // C++ 11p3: Members of a class defined with the keyword class are private
3745 // by default. Members of a class defined with the keywords struct or union
3746 // are public by default.
3747 // HLSL: In HLSL members of a class are public by default.
3748 AccessSpecifier CurAS;
3749 if (TagType == DeclSpec::TST_class && !getLangOpts().HLSL)
3750 CurAS = AS_private;
3751 else
3752 CurAS = AS_public;
3753 ParsedAttributes AccessAttrs(AttrFactory);
3754
3755 if (TagDecl) {
3756 // While we still have something to read, read the member-declarations.
3757 while (!tryParseMisplacedModuleImport() && Tok.isNot(K: tok::r_brace) &&
3758 Tok.isNot(K: tok::eof)) {
3759 // Each iteration of this loop reads one member-declaration.
3760 ParseCXXClassMemberDeclarationWithPragmas(
3761 AS&: CurAS, AccessAttrs, TagType: static_cast<DeclSpec::TST>(TagType), TagDecl);
3762 MaybeDestroyTemplateIds();
3763 }
3764 T.consumeClose();
3765 } else {
3766 SkipUntil(T: tok::r_brace);
3767 }
3768
3769 // If attributes exist after class contents, parse them.
3770 ParsedAttributes attrs(AttrFactory);
3771 MaybeParseGNUAttributes(Attrs&: attrs);
3772
3773 if (TagDecl)
3774 Actions.ActOnFinishCXXMemberSpecification(S: getCurScope(), RLoc: RecordLoc, TagDecl,
3775 LBrac: T.getOpenLocation(),
3776 RBrac: T.getCloseLocation(), AttrList: attrs);
3777
3778 // C++11 [class.mem]p2:
3779 // Within the class member-specification, the class is regarded as complete
3780 // within function bodies, default arguments, exception-specifications, and
3781 // brace-or-equal-initializers for non-static data members (including such
3782 // things in nested classes).
3783 if (TagDecl && NonNestedClass) {
3784 // We are not inside a nested class. This class and its nested classes
3785 // are complete and we can parse the delayed portions of method
3786 // declarations and the lexed inline method definitions, along with any
3787 // delayed attributes.
3788
3789 SourceLocation SavedPrevTokLocation = PrevTokLocation;
3790 ParseLexedPragmas(Class&: getCurrentClass());
3791 ParseLexedAttributes(Class&: getCurrentClass());
3792 ParseLexedMethodDeclarations(Class&: getCurrentClass());
3793
3794 // We've finished with all pending member declarations.
3795 Actions.ActOnFinishCXXMemberDecls();
3796
3797 ParseLexedMemberInitializers(Class&: getCurrentClass());
3798 ParseLexedMethodDefs(Class&: getCurrentClass());
3799 PrevTokLocation = SavedPrevTokLocation;
3800
3801 // We've finished parsing everything, including default argument
3802 // initializers.
3803 Actions.ActOnFinishCXXNonNestedClass();
3804 }
3805
3806 if (TagDecl)
3807 Actions.ActOnTagFinishDefinition(S: getCurScope(), TagDecl, BraceRange: T.getRange());
3808
3809 // Leave the class scope.
3810 ParsingDef.Pop();
3811 ClassScope.Exit();
3812}
3813
3814void Parser::DiagnoseUnexpectedNamespace(NamedDecl *D) {
3815 assert(Tok.is(tok::kw_namespace));
3816
3817 // FIXME: Suggest where the close brace should have gone by looking
3818 // at indentation changes within the definition body.
3819 Diag(D->getLocation(), diag::err_missing_end_of_definition) << D;
3820 Diag(Tok.getLocation(), diag::note_missing_end_of_definition_before) << D;
3821
3822 // Push '};' onto the token stream to recover.
3823 PP.EnterToken(Tok, /*IsReinject*/ true);
3824
3825 Tok.startToken();
3826 Tok.setLocation(PP.getLocForEndOfToken(Loc: PrevTokLocation));
3827 Tok.setKind(tok::semi);
3828 PP.EnterToken(Tok, /*IsReinject*/ true);
3829
3830 Tok.setKind(tok::r_brace);
3831}
3832
3833void Parser::ParseConstructorInitializer(Decl *ConstructorDecl) {
3834 assert(Tok.is(tok::colon) &&
3835 "Constructor initializer always starts with ':'");
3836
3837 // Poison the SEH identifiers so they are flagged as illegal in constructor
3838 // initializers.
3839 PoisonSEHIdentifiersRAIIObject PoisonSEHIdentifiers(*this, true);
3840 SourceLocation ColonLoc = ConsumeToken();
3841
3842 SmallVector<CXXCtorInitializer *, 4> MemInitializers;
3843 bool AnyErrors = false;
3844
3845 do {
3846 if (Tok.is(K: tok::code_completion)) {
3847 cutOffParsing();
3848 Actions.CodeCompletion().CodeCompleteConstructorInitializer(
3849 Constructor: ConstructorDecl, Initializers: MemInitializers);
3850 return;
3851 }
3852
3853 MemInitResult MemInit = ParseMemInitializer(ConstructorDecl);
3854 if (!MemInit.isInvalid())
3855 MemInitializers.push_back(Elt: MemInit.get());
3856 else
3857 AnyErrors = true;
3858
3859 if (Tok.is(K: tok::comma))
3860 ConsumeToken();
3861 else if (Tok.is(K: tok::l_brace))
3862 break;
3863 // If the previous initializer was valid and the next token looks like a
3864 // base or member initializer, assume that we're just missing a comma.
3865 else if (!MemInit.isInvalid() &&
3866 Tok.isOneOf(K1: tok::identifier, K2: tok::coloncolon)) {
3867 SourceLocation Loc = PP.getLocForEndOfToken(Loc: PrevTokLocation);
3868 Diag(Loc, diag::err_ctor_init_missing_comma)
3869 << FixItHint::CreateInsertion(Loc, ", ");
3870 } else {
3871 // Skip over garbage, until we get to '{'. Don't eat the '{'.
3872 if (!MemInit.isInvalid())
3873 Diag(Tok.getLocation(), diag::err_expected_either)
3874 << tok::l_brace << tok::comma;
3875 SkipUntil(T: tok::l_brace, Flags: StopAtSemi | StopBeforeMatch);
3876 break;
3877 }
3878 } while (true);
3879
3880 Actions.ActOnMemInitializers(ConstructorDecl, ColonLoc, MemInits: MemInitializers,
3881 AnyErrors);
3882}
3883
3884MemInitResult Parser::ParseMemInitializer(Decl *ConstructorDecl) {
3885 // parse '::'[opt] nested-name-specifier[opt]
3886 CXXScopeSpec SS;
3887 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
3888 /*ObjectHasErrors=*/false,
3889 /*EnteringContext=*/false))
3890 return true;
3891
3892 // : identifier
3893 IdentifierInfo *II = nullptr;
3894 SourceLocation IdLoc = Tok.getLocation();
3895 // : declype(...)
3896 DeclSpec DS(AttrFactory);
3897 // : template_name<...>
3898 TypeResult TemplateTypeTy;
3899
3900 if (Tok.is(K: tok::identifier)) {
3901 // Get the identifier. This may be a member name or a class name,
3902 // but we'll let the semantic analysis determine which it is.
3903 II = Tok.getIdentifierInfo();
3904 ConsumeToken();
3905 } else if (Tok.is(K: tok::annot_decltype)) {
3906 // Get the decltype expression, if there is one.
3907 // Uses of decltype will already have been converted to annot_decltype by
3908 // ParseOptionalCXXScopeSpecifier at this point.
3909 // FIXME: Can we get here with a scope specifier?
3910 ParseDecltypeSpecifier(DS);
3911 } else if (Tok.is(K: tok::annot_pack_indexing_type)) {
3912 // Uses of T...[N] will already have been converted to
3913 // annot_pack_indexing_type by ParseOptionalCXXScopeSpecifier at this point.
3914 ParsePackIndexingType(DS);
3915 } else {
3916 TemplateIdAnnotation *TemplateId = Tok.is(K: tok::annot_template_id)
3917 ? takeTemplateIdAnnotation(tok: Tok)
3918 : nullptr;
3919 if (TemplateId && TemplateId->mightBeType()) {
3920 AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename: ImplicitTypenameContext::No,
3921 /*IsClassName=*/true);
3922 assert(Tok.is(tok::annot_typename) && "template-id -> type failed");
3923 TemplateTypeTy = getTypeAnnotation(Tok);
3924 ConsumeAnnotationToken();
3925 } else {
3926 Diag(Tok, diag::err_expected_member_or_base_name);
3927 return true;
3928 }
3929 }
3930
3931 // Parse the '('.
3932 if (getLangOpts().CPlusPlus11 && Tok.is(K: tok::l_brace)) {
3933 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
3934
3935 // FIXME: Add support for signature help inside initializer lists.
3936 ExprResult InitList = ParseBraceInitializer();
3937 if (InitList.isInvalid())
3938 return true;
3939
3940 SourceLocation EllipsisLoc;
3941 TryConsumeToken(Expected: tok::ellipsis, Loc&: EllipsisLoc);
3942
3943 if (TemplateTypeTy.isInvalid())
3944 return true;
3945 return Actions.ActOnMemInitializer(ConstructorD: ConstructorDecl, S: getCurScope(), SS, MemberOrBase: II,
3946 TemplateTypeTy: TemplateTypeTy.get(), DS, IdLoc,
3947 InitList: InitList.get(), EllipsisLoc);
3948 } else if (Tok.is(K: tok::l_paren)) {
3949 BalancedDelimiterTracker T(*this, tok::l_paren);
3950 T.consumeOpen();
3951
3952 // Parse the optional expression-list.
3953 ExprVector ArgExprs;
3954 auto RunSignatureHelp = [&] {
3955 if (TemplateTypeTy.isInvalid())
3956 return QualType();
3957 QualType PreferredType =
3958 Actions.CodeCompletion().ProduceCtorInitMemberSignatureHelp(
3959 ConstructorDecl, SS, TemplateTypeTy: TemplateTypeTy.get(), ArgExprs, II,
3960 OpenParLoc: T.getOpenLocation(), /*Braced=*/false);
3961 CalledSignatureHelp = true;
3962 return PreferredType;
3963 };
3964 if (Tok.isNot(K: tok::r_paren) && ParseExpressionList(Exprs&: ArgExprs, ExpressionStarts: [&] {
3965 PreferredType.enterFunctionArgument(Tok.getLocation(),
3966 RunSignatureHelp);
3967 })) {
3968 if (PP.isCodeCompletionReached() && !CalledSignatureHelp)
3969 RunSignatureHelp();
3970 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
3971 return true;
3972 }
3973
3974 T.consumeClose();
3975
3976 SourceLocation EllipsisLoc;
3977 TryConsumeToken(Expected: tok::ellipsis, Loc&: EllipsisLoc);
3978
3979 if (TemplateTypeTy.isInvalid())
3980 return true;
3981 return Actions.ActOnMemInitializer(
3982 ConstructorD: ConstructorDecl, S: getCurScope(), SS, MemberOrBase: II, TemplateTypeTy: TemplateTypeTy.get(), DS, IdLoc,
3983 LParenLoc: T.getOpenLocation(), Args: ArgExprs, RParenLoc: T.getCloseLocation(), EllipsisLoc);
3984 }
3985
3986 if (TemplateTypeTy.isInvalid())
3987 return true;
3988
3989 if (getLangOpts().CPlusPlus11)
3990 return Diag(Tok, diag::err_expected_either) << tok::l_paren << tok::l_brace;
3991 else
3992 return Diag(Tok, diag::err_expected) << tok::l_paren;
3993}
3994
3995ExceptionSpecificationType Parser::tryParseExceptionSpecification(
3996 bool Delayed, SourceRange &SpecificationRange,
3997 SmallVectorImpl<ParsedType> &DynamicExceptions,
3998 SmallVectorImpl<SourceRange> &DynamicExceptionRanges,
3999 ExprResult &NoexceptExpr, CachedTokens *&ExceptionSpecTokens) {
4000 ExceptionSpecificationType Result = EST_None;
4001 ExceptionSpecTokens = nullptr;
4002
4003 // Handle delayed parsing of exception-specifications.
4004 if (Delayed) {
4005 if (Tok.isNot(K: tok::kw_throw) && Tok.isNot(K: tok::kw_noexcept))
4006 return EST_None;
4007
4008 // Consume and cache the starting token.
4009 bool IsNoexcept = Tok.is(K: tok::kw_noexcept);
4010 Token StartTok = Tok;
4011 SpecificationRange = SourceRange(ConsumeToken());
4012
4013 // Check for a '('.
4014 if (!Tok.is(K: tok::l_paren)) {
4015 // If this is a bare 'noexcept', we're done.
4016 if (IsNoexcept) {
4017 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
4018 NoexceptExpr = nullptr;
4019 return EST_BasicNoexcept;
4020 }
4021
4022 Diag(Tok, diag::err_expected_lparen_after) << "throw";
4023 return EST_DynamicNone;
4024 }
4025
4026 // Cache the tokens for the exception-specification.
4027 ExceptionSpecTokens = new CachedTokens;
4028 ExceptionSpecTokens->push_back(Elt: StartTok); // 'throw' or 'noexcept'
4029 ExceptionSpecTokens->push_back(Elt: Tok); // '('
4030 SpecificationRange.setEnd(ConsumeParen()); // '('
4031
4032 ConsumeAndStoreUntil(T1: tok::r_paren, Toks&: *ExceptionSpecTokens,
4033 /*StopAtSemi=*/true,
4034 /*ConsumeFinalToken=*/true);
4035 SpecificationRange.setEnd(ExceptionSpecTokens->back().getLocation());
4036
4037 return EST_Unparsed;
4038 }
4039
4040 // See if there's a dynamic specification.
4041 if (Tok.is(K: tok::kw_throw)) {
4042 Result = ParseDynamicExceptionSpecification(
4043 SpecificationRange, Exceptions&: DynamicExceptions, Ranges&: DynamicExceptionRanges);
4044 assert(DynamicExceptions.size() == DynamicExceptionRanges.size() &&
4045 "Produced different number of exception types and ranges.");
4046 }
4047
4048 // If there's no noexcept specification, we're done.
4049 if (Tok.isNot(K: tok::kw_noexcept))
4050 return Result;
4051
4052 Diag(Tok, diag::warn_cxx98_compat_noexcept_decl);
4053
4054 // If we already had a dynamic specification, parse the noexcept for,
4055 // recovery, but emit a diagnostic and don't store the results.
4056 SourceRange NoexceptRange;
4057 ExceptionSpecificationType NoexceptType = EST_None;
4058
4059 SourceLocation KeywordLoc = ConsumeToken();
4060 if (Tok.is(K: tok::l_paren)) {
4061 // There is an argument.
4062 BalancedDelimiterTracker T(*this, tok::l_paren);
4063 T.consumeOpen();
4064
4065 EnterExpressionEvaluationContext ConstantEvaluated(
4066 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
4067 NoexceptExpr = ParseConstantExpressionInExprEvalContext();
4068
4069 T.consumeClose();
4070 if (!NoexceptExpr.isInvalid()) {
4071 NoexceptExpr =
4072 Actions.ActOnNoexceptSpec(NoexceptExpr: NoexceptExpr.get(), EST&: NoexceptType);
4073 NoexceptRange = SourceRange(KeywordLoc, T.getCloseLocation());
4074 } else {
4075 NoexceptType = EST_BasicNoexcept;
4076 }
4077 } else {
4078 // There is no argument.
4079 NoexceptType = EST_BasicNoexcept;
4080 NoexceptRange = SourceRange(KeywordLoc, KeywordLoc);
4081 }
4082
4083 if (Result == EST_None) {
4084 SpecificationRange = NoexceptRange;
4085 Result = NoexceptType;
4086
4087 // If there's a dynamic specification after a noexcept specification,
4088 // parse that and ignore the results.
4089 if (Tok.is(K: tok::kw_throw)) {
4090 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
4091 ParseDynamicExceptionSpecification(SpecificationRange&: NoexceptRange, Exceptions&: DynamicExceptions,
4092 Ranges&: DynamicExceptionRanges);
4093 }
4094 } else {
4095 Diag(Tok.getLocation(), diag::err_dynamic_and_noexcept_specification);
4096 }
4097
4098 return Result;
4099}
4100
4101static void diagnoseDynamicExceptionSpecification(Parser &P, SourceRange Range,
4102 bool IsNoexcept) {
4103 if (P.getLangOpts().CPlusPlus11) {
4104 const char *Replacement = IsNoexcept ? "noexcept" : "noexcept(false)";
4105 P.Diag(Range.getBegin(), P.getLangOpts().CPlusPlus17 && !IsNoexcept
4106 ? diag::ext_dynamic_exception_spec
4107 : diag::warn_exception_spec_deprecated)
4108 << Range;
4109 P.Diag(Range.getBegin(), diag::note_exception_spec_deprecated)
4110 << Replacement << FixItHint::CreateReplacement(Range, Replacement);
4111 }
4112}
4113
4114ExceptionSpecificationType Parser::ParseDynamicExceptionSpecification(
4115 SourceRange &SpecificationRange, SmallVectorImpl<ParsedType> &Exceptions,
4116 SmallVectorImpl<SourceRange> &Ranges) {
4117 assert(Tok.is(tok::kw_throw) && "expected throw");
4118
4119 SpecificationRange.setBegin(ConsumeToken());
4120 BalancedDelimiterTracker T(*this, tok::l_paren);
4121 if (T.consumeOpen()) {
4122 Diag(Tok, diag::err_expected_lparen_after) << "throw";
4123 SpecificationRange.setEnd(SpecificationRange.getBegin());
4124 return EST_DynamicNone;
4125 }
4126
4127 // Parse throw(...), a Microsoft extension that means "this function
4128 // can throw anything".
4129 if (Tok.is(K: tok::ellipsis)) {
4130 SourceLocation EllipsisLoc = ConsumeToken();
4131 if (!getLangOpts().MicrosoftExt)
4132 Diag(EllipsisLoc, diag::ext_ellipsis_exception_spec);
4133 T.consumeClose();
4134 SpecificationRange.setEnd(T.getCloseLocation());
4135 diagnoseDynamicExceptionSpecification(P&: *this, Range: SpecificationRange, IsNoexcept: false);
4136 return EST_MSAny;
4137 }
4138
4139 // Parse the sequence of type-ids.
4140 SourceRange Range;
4141 while (Tok.isNot(K: tok::r_paren)) {
4142 TypeResult Res(ParseTypeName(Range: &Range));
4143
4144 if (Tok.is(K: tok::ellipsis)) {
4145 // C++0x [temp.variadic]p5:
4146 // - In a dynamic-exception-specification (15.4); the pattern is a
4147 // type-id.
4148 SourceLocation Ellipsis = ConsumeToken();
4149 Range.setEnd(Ellipsis);
4150 if (!Res.isInvalid())
4151 Res = Actions.ActOnPackExpansion(Type: Res.get(), EllipsisLoc: Ellipsis);
4152 }
4153
4154 if (!Res.isInvalid()) {
4155 Exceptions.push_back(Elt: Res.get());
4156 Ranges.push_back(Elt: Range);
4157 }
4158
4159 if (!TryConsumeToken(Expected: tok::comma))
4160 break;
4161 }
4162
4163 T.consumeClose();
4164 SpecificationRange.setEnd(T.getCloseLocation());
4165 diagnoseDynamicExceptionSpecification(P&: *this, Range: SpecificationRange,
4166 IsNoexcept: Exceptions.empty());
4167 return Exceptions.empty() ? EST_DynamicNone : EST_Dynamic;
4168}
4169
4170TypeResult Parser::ParseTrailingReturnType(SourceRange &Range,
4171 bool MayBeFollowedByDirectInit) {
4172 assert(Tok.is(tok::arrow) && "expected arrow");
4173
4174 ConsumeToken();
4175
4176 return ParseTypeName(Range: &Range, Context: MayBeFollowedByDirectInit
4177 ? DeclaratorContext::TrailingReturnVar
4178 : DeclaratorContext::TrailingReturn);
4179}
4180
4181void Parser::ParseTrailingRequiresClause(Declarator &D) {
4182 assert(Tok.is(tok::kw_requires) && "expected requires");
4183
4184 SourceLocation RequiresKWLoc = ConsumeToken();
4185
4186 // C++23 [basic.scope.namespace]p1:
4187 // For each non-friend redeclaration or specialization whose target scope
4188 // is or is contained by the scope, the portion after the declarator-id,
4189 // class-head-name, or enum-head-name is also included in the scope.
4190 // C++23 [basic.scope.class]p1:
4191 // For each non-friend redeclaration or specialization whose target scope
4192 // is or is contained by the scope, the portion after the declarator-id,
4193 // class-head-name, or enum-head-name is also included in the scope.
4194 //
4195 // FIXME: We should really be calling ParseTrailingRequiresClause in
4196 // ParseDirectDeclarator, when we are already in the declarator scope.
4197 // This would also correctly suppress access checks for specializations
4198 // and explicit instantiations, which we currently do not do.
4199 CXXScopeSpec &SS = D.getCXXScopeSpec();
4200 DeclaratorScopeObj DeclScopeObj(*this, SS);
4201 if (SS.isValid() && Actions.ShouldEnterDeclaratorScope(S: getCurScope(), SS))
4202 DeclScopeObj.EnterDeclaratorScope();
4203
4204 ExprResult TrailingRequiresClause;
4205 ParseScope ParamScope(this, Scope::DeclScope |
4206 Scope::FunctionDeclarationScope |
4207 Scope::FunctionPrototypeScope);
4208
4209 Actions.ActOnStartTrailingRequiresClause(S: getCurScope(), D);
4210
4211 std::optional<Sema::CXXThisScopeRAII> ThisScope;
4212 InitCXXThisScopeForDeclaratorIfRelevant(D, DS: D.getDeclSpec(), ThisScope&: ThisScope);
4213
4214 TrailingRequiresClause =
4215 ParseConstraintLogicalOrExpression(/*IsTrailingRequiresClause=*/true);
4216
4217 TrailingRequiresClause =
4218 Actions.ActOnFinishTrailingRequiresClause(ConstraintExpr: TrailingRequiresClause);
4219
4220 if (!D.isDeclarationOfFunction()) {
4221 Diag(RequiresKWLoc,
4222 diag::err_requires_clause_on_declarator_not_declaring_a_function);
4223 return;
4224 }
4225
4226 if (TrailingRequiresClause.isInvalid())
4227 SkipUntil(Toks: {tok::l_brace, tok::arrow, tok::kw_try, tok::comma, tok::colon},
4228 Flags: StopAtSemi | StopBeforeMatch);
4229 else
4230 D.setTrailingRequiresClause(TrailingRequiresClause.get());
4231
4232 // Did the user swap the trailing return type and requires clause?
4233 if (D.isFunctionDeclarator() && Tok.is(K: tok::arrow) &&
4234 D.getDeclSpec().getTypeSpecType() == TST_auto) {
4235 SourceLocation ArrowLoc = Tok.getLocation();
4236 SourceRange Range;
4237 TypeResult TrailingReturnType =
4238 ParseTrailingReturnType(Range, /*MayBeFollowedByDirectInit=*/false);
4239
4240 if (!TrailingReturnType.isInvalid()) {
4241 Diag(ArrowLoc,
4242 diag::err_requires_clause_must_appear_after_trailing_return)
4243 << Range;
4244 auto &FunctionChunk = D.getFunctionTypeInfo();
4245 FunctionChunk.HasTrailingReturnType = TrailingReturnType.isUsable();
4246 FunctionChunk.TrailingReturnType = TrailingReturnType.get();
4247 FunctionChunk.TrailingReturnTypeLoc = Range.getBegin();
4248 } else
4249 SkipUntil(Toks: {tok::equal, tok::l_brace, tok::arrow, tok::kw_try, tok::comma},
4250 Flags: StopAtSemi | StopBeforeMatch);
4251 }
4252}
4253
4254Sema::ParsingClassState Parser::PushParsingClass(Decl *ClassDecl,
4255 bool NonNestedClass,
4256 bool IsInterface) {
4257 assert((NonNestedClass || !ClassStack.empty()) &&
4258 "Nested class without outer class");
4259 ClassStack.push(x: new ParsingClass(ClassDecl, NonNestedClass, IsInterface));
4260 return Actions.PushParsingClass();
4261}
4262
4263void Parser::DeallocateParsedClasses(Parser::ParsingClass *Class) {
4264 for (unsigned I = 0, N = Class->LateParsedDeclarations.size(); I != N; ++I)
4265 delete Class->LateParsedDeclarations[I];
4266 delete Class;
4267}
4268
4269void Parser::PopParsingClass(Sema::ParsingClassState state) {
4270 assert(!ClassStack.empty() && "Mismatched push/pop for class parsing");
4271
4272 Actions.PopParsingClass(state);
4273
4274 ParsingClass *Victim = ClassStack.top();
4275 ClassStack.pop();
4276 if (Victim->TopLevelClass) {
4277 // Deallocate all of the nested classes of this class,
4278 // recursively: we don't need to keep any of this information.
4279 DeallocateParsedClasses(Class: Victim);
4280 return;
4281 }
4282 assert(!ClassStack.empty() && "Missing top-level class?");
4283
4284 if (Victim->LateParsedDeclarations.empty()) {
4285 // The victim is a nested class, but we will not need to perform
4286 // any processing after the definition of this class since it has
4287 // no members whose handling was delayed. Therefore, we can just
4288 // remove this nested class.
4289 DeallocateParsedClasses(Class: Victim);
4290 return;
4291 }
4292
4293 // This nested class has some members that will need to be processed
4294 // after the top-level class is completely defined. Therefore, add
4295 // it to the list of nested classes within its parent.
4296 assert(getCurScope()->isClassScope() &&
4297 "Nested class outside of class scope?");
4298 ClassStack.top()->LateParsedDeclarations.push_back(
4299 Elt: new LateParsedClass(this, Victim));
4300}
4301
4302IdentifierInfo *Parser::TryParseCXX11AttributeIdentifier(
4303 SourceLocation &Loc, SemaCodeCompletion::AttributeCompletion Completion,
4304 const IdentifierInfo *Scope) {
4305 switch (Tok.getKind()) {
4306 default:
4307 // Identifiers and keywords have identifier info attached.
4308 if (!Tok.isAnnotation()) {
4309 if (IdentifierInfo *II = Tok.getIdentifierInfo()) {
4310 Loc = ConsumeToken();
4311 return II;
4312 }
4313 }
4314 return nullptr;
4315
4316 case tok::code_completion:
4317 cutOffParsing();
4318 Actions.CodeCompletion().CodeCompleteAttribute(
4319 Syntax: getLangOpts().CPlusPlus ? ParsedAttr::AS_CXX11 : ParsedAttr::AS_C23,
4320 Completion, Scope);
4321 return nullptr;
4322
4323 case tok::numeric_constant: {
4324 // If we got a numeric constant, check to see if it comes from a macro that
4325 // corresponds to the predefined __clang__ macro. If it does, warn the user
4326 // and recover by pretending they said _Clang instead.
4327 if (Tok.getLocation().isMacroID()) {
4328 SmallString<8> ExpansionBuf;
4329 SourceLocation ExpansionLoc =
4330 PP.getSourceManager().getExpansionLoc(Loc: Tok.getLocation());
4331 StringRef Spelling = PP.getSpelling(loc: ExpansionLoc, buffer&: ExpansionBuf);
4332 if (Spelling == "__clang__") {
4333 SourceRange TokRange(
4334 ExpansionLoc,
4335 PP.getSourceManager().getExpansionLoc(Loc: Tok.getEndLoc()));
4336 Diag(Tok, diag::warn_wrong_clang_attr_namespace)
4337 << FixItHint::CreateReplacement(TokRange, "_Clang");
4338 Loc = ConsumeToken();
4339 return &PP.getIdentifierTable().get(Name: "_Clang");
4340 }
4341 }
4342 return nullptr;
4343 }
4344
4345 case tok::ampamp: // 'and'
4346 case tok::pipe: // 'bitor'
4347 case tok::pipepipe: // 'or'
4348 case tok::caret: // 'xor'
4349 case tok::tilde: // 'compl'
4350 case tok::amp: // 'bitand'
4351 case tok::ampequal: // 'and_eq'
4352 case tok::pipeequal: // 'or_eq'
4353 case tok::caretequal: // 'xor_eq'
4354 case tok::exclaim: // 'not'
4355 case tok::exclaimequal: // 'not_eq'
4356 // Alternative tokens do not have identifier info, but their spelling
4357 // starts with an alphabetical character.
4358 SmallString<8> SpellingBuf;
4359 SourceLocation SpellingLoc =
4360 PP.getSourceManager().getSpellingLoc(Loc: Tok.getLocation());
4361 StringRef Spelling = PP.getSpelling(loc: SpellingLoc, buffer&: SpellingBuf);
4362 if (isLetter(c: Spelling[0])) {
4363 Loc = ConsumeToken();
4364 return &PP.getIdentifierTable().get(Name: Spelling);
4365 }
4366 return nullptr;
4367 }
4368}
4369
4370void Parser::ParseOpenMPAttributeArgs(const IdentifierInfo *AttrName,
4371 CachedTokens &OpenMPTokens) {
4372 // Both 'sequence' and 'directive' attributes require arguments, so parse the
4373 // open paren for the argument list.
4374 BalancedDelimiterTracker T(*this, tok::l_paren);
4375 if (T.consumeOpen()) {
4376 Diag(Tok, diag::err_expected) << tok::l_paren;
4377 return;
4378 }
4379
4380 if (AttrName->isStr(Str: "directive")) {
4381 // If the attribute is named `directive`, we can consume its argument list
4382 // and push the tokens from it into the cached token stream for a new OpenMP
4383 // pragma directive.
4384 Token OMPBeginTok;
4385 OMPBeginTok.startToken();
4386 OMPBeginTok.setKind(tok::annot_attr_openmp);
4387 OMPBeginTok.setLocation(Tok.getLocation());
4388 OpenMPTokens.push_back(Elt: OMPBeginTok);
4389
4390 ConsumeAndStoreUntil(T1: tok::r_paren, Toks&: OpenMPTokens, /*StopAtSemi=*/false,
4391 /*ConsumeFinalToken*/ false);
4392 Token OMPEndTok;
4393 OMPEndTok.startToken();
4394 OMPEndTok.setKind(tok::annot_pragma_openmp_end);
4395 OMPEndTok.setLocation(Tok.getLocation());
4396 OpenMPTokens.push_back(Elt: OMPEndTok);
4397 } else {
4398 assert(AttrName->isStr("sequence") &&
4399 "Expected either 'directive' or 'sequence'");
4400 // If the attribute is named 'sequence', its argument is a list of one or
4401 // more OpenMP attributes (either 'omp::directive' or 'omp::sequence',
4402 // where the 'omp::' is optional).
4403 do {
4404 // We expect to see one of the following:
4405 // * An identifier (omp) for the attribute namespace followed by ::
4406 // * An identifier (directive) or an identifier (sequence).
4407 SourceLocation IdentLoc;
4408 const IdentifierInfo *Ident = TryParseCXX11AttributeIdentifier(Loc&: IdentLoc);
4409
4410 // If there is an identifier and it is 'omp', a double colon is required
4411 // followed by the actual identifier we're after.
4412 if (Ident && Ident->isStr(Str: "omp") && !ExpectAndConsume(ExpectedTok: tok::coloncolon))
4413 Ident = TryParseCXX11AttributeIdentifier(Loc&: IdentLoc);
4414
4415 // If we failed to find an identifier (scoped or otherwise), or we found
4416 // an unexpected identifier, diagnose.
4417 if (!Ident || (!Ident->isStr(Str: "directive") && !Ident->isStr(Str: "sequence"))) {
4418 Diag(Tok.getLocation(), diag::err_expected_sequence_or_directive);
4419 SkipUntil(T: tok::r_paren, Flags: StopBeforeMatch);
4420 continue;
4421 }
4422 // We read an identifier. If the identifier is one of the ones we
4423 // expected, we can recurse to parse the args.
4424 ParseOpenMPAttributeArgs(AttrName: Ident, OpenMPTokens);
4425
4426 // There may be a comma to signal that we expect another directive in the
4427 // sequence.
4428 } while (TryConsumeToken(Expected: tok::comma));
4429 }
4430 // Parse the closing paren for the argument list.
4431 T.consumeClose();
4432}
4433
4434static bool IsBuiltInOrStandardCXX11Attribute(IdentifierInfo *AttrName,
4435 IdentifierInfo *ScopeName) {
4436 switch (
4437 ParsedAttr::getParsedKind(Name: AttrName, Scope: ScopeName, SyntaxUsed: ParsedAttr::AS_CXX11)) {
4438 case ParsedAttr::AT_CarriesDependency:
4439 case ParsedAttr::AT_Deprecated:
4440 case ParsedAttr::AT_FallThrough:
4441 case ParsedAttr::AT_CXX11NoReturn:
4442 case ParsedAttr::AT_NoUniqueAddress:
4443 case ParsedAttr::AT_Likely:
4444 case ParsedAttr::AT_Unlikely:
4445 return true;
4446 case ParsedAttr::AT_WarnUnusedResult:
4447 return !ScopeName && AttrName->getName() == "nodiscard";
4448 case ParsedAttr::AT_Unused:
4449 return !ScopeName && AttrName->getName() == "maybe_unused";
4450 default:
4451 return false;
4452 }
4453}
4454
4455bool Parser::ParseCXXAssumeAttributeArg(
4456 ParsedAttributes &Attrs, IdentifierInfo *AttrName,
4457 SourceLocation AttrNameLoc, IdentifierInfo *ScopeName,
4458 SourceLocation ScopeLoc, SourceLocation *EndLoc, ParsedAttr::Form Form) {
4459 assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list");
4460 BalancedDelimiterTracker T(*this, tok::l_paren);
4461 T.consumeOpen();
4462
4463 // [dcl.attr.assume]: The expression is potentially evaluated.
4464 EnterExpressionEvaluationContext Unevaluated(
4465 Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated);
4466
4467 TentativeParsingAction TPA(*this);
4468 ExprResult Res(
4469 Actions.CorrectDelayedTyposInExpr(ER: ParseConditionalExpression()));
4470 if (Res.isInvalid()) {
4471 TPA.Commit();
4472 SkipUntil(T1: tok::r_paren, T2: tok::r_square, Flags: StopAtSemi | StopBeforeMatch);
4473 if (Tok.is(K: tok::r_paren))
4474 T.consumeClose();
4475 return true;
4476 }
4477
4478 if (!Tok.isOneOf(K1: tok::r_paren, K2: tok::r_square)) {
4479 // Emit a better diagnostic if this is an otherwise valid expression that
4480 // is not allowed here.
4481 TPA.Revert();
4482 Res = ParseExpression();
4483 if (!Res.isInvalid()) {
4484 auto *E = Res.get();
4485 Diag(E->getExprLoc(), diag::err_assume_attr_expects_cond_expr)
4486 << AttrName << FixItHint::CreateInsertion(E->getBeginLoc(), "(")
4487 << FixItHint::CreateInsertion(PP.getLocForEndOfToken(E->getEndLoc()),
4488 ")")
4489 << E->getSourceRange();
4490 }
4491
4492 T.consumeClose();
4493 return true;
4494 }
4495
4496 TPA.Commit();
4497 ArgsUnion Assumption = Res.get();
4498 auto RParen = Tok.getLocation();
4499 T.consumeClose();
4500 Attrs.addNew(attrName: AttrName, attrRange: SourceRange(AttrNameLoc, RParen), scopeName: ScopeName, scopeLoc: ScopeLoc,
4501 args: &Assumption, numArgs: 1, form: Form);
4502
4503 if (EndLoc)
4504 *EndLoc = RParen;
4505
4506 return false;
4507}
4508
4509bool Parser::ParseCXX11AttributeArgs(
4510 IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
4511 ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
4512 SourceLocation ScopeLoc, CachedTokens &OpenMPTokens) {
4513 assert(Tok.is(tok::l_paren) && "Not a C++11 attribute argument list");
4514 SourceLocation LParenLoc = Tok.getLocation();
4515 const LangOptions &LO = getLangOpts();
4516 ParsedAttr::Form Form =
4517 LO.CPlusPlus ? ParsedAttr::Form::CXX11() : ParsedAttr::Form::C23();
4518
4519 // Try parsing microsoft attributes
4520 if (getLangOpts().MicrosoftExt || getLangOpts().HLSL) {
4521 if (hasAttribute(Syntax: AttributeCommonInfo::Syntax::AS_Microsoft, Scope: ScopeName,
4522 Attr: AttrName, Target: getTargetInfo(), LangOpts: getLangOpts()))
4523 Form = ParsedAttr::Form::Microsoft();
4524 }
4525
4526 // If the attribute isn't known, we will not attempt to parse any
4527 // arguments.
4528 if (Form.getSyntax() != ParsedAttr::AS_Microsoft &&
4529 !hasAttribute(Syntax: LO.CPlusPlus ? AttributeCommonInfo::Syntax::AS_CXX11
4530 : AttributeCommonInfo::Syntax::AS_C23,
4531 Scope: ScopeName, Attr: AttrName, Target: getTargetInfo(), LangOpts: getLangOpts())) {
4532 // Eat the left paren, then skip to the ending right paren.
4533 ConsumeParen();
4534 SkipUntil(T: tok::r_paren);
4535 return false;
4536 }
4537
4538 if (ScopeName && (ScopeName->isStr(Str: "gnu") || ScopeName->isStr(Str: "__gnu__"))) {
4539 // GNU-scoped attributes have some special cases to handle GNU-specific
4540 // behaviors.
4541 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
4542 ScopeLoc, Form, D: nullptr);
4543 return true;
4544 }
4545
4546 // [[omp::directive]] and [[omp::sequence]] need special handling.
4547 if (ScopeName && ScopeName->isStr(Str: "omp") &&
4548 (AttrName->isStr(Str: "directive") || AttrName->isStr(Str: "sequence"))) {
4549 Diag(AttrNameLoc, getLangOpts().OpenMP >= 51
4550 ? diag::warn_omp51_compat_attributes
4551 : diag::ext_omp_attributes);
4552
4553 ParseOpenMPAttributeArgs(AttrName, OpenMPTokens);
4554
4555 // We claim that an attribute was parsed and added so that one is not
4556 // created for us by the caller.
4557 return true;
4558 }
4559
4560 unsigned NumArgs;
4561 // Some Clang-scoped attributes have some special parsing behavior.
4562 if (ScopeName && (ScopeName->isStr(Str: "clang") || ScopeName->isStr(Str: "_Clang")))
4563 NumArgs = ParseClangAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc,
4564 ScopeName, ScopeLoc, Form);
4565 // So does C++23's assume() attribute.
4566 else if (!ScopeName && AttrName->isStr(Str: "assume")) {
4567 if (ParseCXXAssumeAttributeArg(Attrs, AttrName, AttrNameLoc, ScopeName: nullptr,
4568 ScopeLoc: SourceLocation{}, EndLoc, Form))
4569 return true;
4570 NumArgs = 1;
4571 } else
4572 NumArgs = ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
4573 ScopeName, ScopeLoc, Form);
4574
4575 if (!Attrs.empty() &&
4576 IsBuiltInOrStandardCXX11Attribute(AttrName, ScopeName)) {
4577 ParsedAttr &Attr = Attrs.back();
4578
4579 // Ignore attributes that don't exist for the target.
4580 if (!Attr.existsInTarget(Target: getTargetInfo())) {
4581 Diag(LParenLoc, diag::warn_unknown_attribute_ignored) << AttrName;
4582 Attr.setInvalid(true);
4583 return true;
4584 }
4585
4586 // If the attribute is a standard or built-in attribute and we are
4587 // parsing an argument list, we need to determine whether this attribute
4588 // was allowed to have an argument list (such as [[deprecated]]), and how
4589 // many arguments were parsed (so we can diagnose on [[deprecated()]]).
4590 if (Attr.getMaxArgs() && !NumArgs) {
4591 // The attribute was allowed to have arguments, but none were provided
4592 // even though the attribute parsed successfully. This is an error.
4593 Diag(LParenLoc, diag::err_attribute_requires_arguments) << AttrName;
4594 Attr.setInvalid(true);
4595 } else if (!Attr.getMaxArgs()) {
4596 // The attribute parsed successfully, but was not allowed to have any
4597 // arguments. It doesn't matter whether any were provided -- the
4598 // presence of the argument list (even if empty) is diagnosed.
4599 Diag(LParenLoc, diag::err_cxx11_attribute_forbids_arguments)
4600 << AttrName
4601 << FixItHint::CreateRemoval(SourceRange(LParenLoc, *EndLoc));
4602 Attr.setInvalid(true);
4603 }
4604 }
4605 return true;
4606}
4607
4608void Parser::ParseCXX11AttributeSpecifierInternal(ParsedAttributes &Attrs,
4609 CachedTokens &OpenMPTokens,
4610 SourceLocation *EndLoc) {
4611 if (Tok.is(K: tok::kw_alignas)) {
4612 // alignas is a valid token in C23 but it is not an attribute, it's a type-
4613 // specifier-qualifier, which means it has different parsing behavior. We
4614 // handle this in ParseDeclarationSpecifiers() instead of here in C. We
4615 // should not get here for C any longer.
4616 assert(getLangOpts().CPlusPlus && "'alignas' is not an attribute in C");
4617 Diag(Tok.getLocation(), diag::warn_cxx98_compat_alignas);
4618 ParseAlignmentSpecifier(Attrs, endLoc: EndLoc);
4619 return;
4620 }
4621
4622 if (Tok.isRegularKeywordAttribute()) {
4623 SourceLocation Loc = Tok.getLocation();
4624 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
4625 ParsedAttr::Form Form = ParsedAttr::Form(Tok.getKind());
4626 bool TakesArgs = doesKeywordAttributeTakeArgs(Kind: Tok.getKind());
4627 ConsumeToken();
4628 if (TakesArgs) {
4629 if (!Tok.is(K: tok::l_paren))
4630 Diag(Tok.getLocation(), diag::err_expected_lparen_after) << AttrName;
4631 else
4632 ParseAttributeArgsCommon(AttrName, AttrNameLoc: Loc, Attrs, EndLoc,
4633 /*ScopeName*/ nullptr,
4634 /*ScopeLoc*/ Loc, Form);
4635 } else
4636 Attrs.addNew(attrName: AttrName, attrRange: Loc, scopeName: nullptr, scopeLoc: Loc, args: nullptr, numArgs: 0, form: Form);
4637 return;
4638 }
4639
4640 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square) &&
4641 "Not a double square bracket attribute list");
4642
4643 SourceLocation OpenLoc = Tok.getLocation();
4644 if (getLangOpts().CPlusPlus) {
4645 Diag(OpenLoc, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_attribute
4646 : diag::warn_ext_cxx11_attributes);
4647 } else {
4648 Diag(OpenLoc, getLangOpts().C23 ? diag::warn_pre_c23_compat_attributes
4649 : diag::warn_ext_c23_attributes);
4650 }
4651
4652 ConsumeBracket();
4653 checkCompoundToken(FirstTokLoc: OpenLoc, FirstTokKind: tok::l_square, Op: CompoundToken::AttrBegin);
4654 ConsumeBracket();
4655
4656 SourceLocation CommonScopeLoc;
4657 IdentifierInfo *CommonScopeName = nullptr;
4658 if (Tok.is(K: tok::kw_using)) {
4659 Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
4660 ? diag::warn_cxx14_compat_using_attribute_ns
4661 : diag::ext_using_attribute_ns);
4662 ConsumeToken();
4663
4664 CommonScopeName = TryParseCXX11AttributeIdentifier(
4665 Loc&: CommonScopeLoc, Completion: SemaCodeCompletion::AttributeCompletion::Scope);
4666 if (!CommonScopeName) {
4667 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
4668 SkipUntil(T1: tok::r_square, T2: tok::colon, Flags: StopBeforeMatch);
4669 }
4670 if (!TryConsumeToken(tok::colon) && CommonScopeName)
4671 Diag(Tok.getLocation(), diag::err_expected) << tok::colon;
4672 }
4673
4674 bool AttrParsed = false;
4675 while (!Tok.isOneOf(K1: tok::r_square, Ks: tok::semi, Ks: tok::eof)) {
4676 if (AttrParsed) {
4677 // If we parsed an attribute, a comma is required before parsing any
4678 // additional attributes.
4679 if (ExpectAndConsume(ExpectedTok: tok::comma)) {
4680 SkipUntil(T: tok::r_square, Flags: StopAtSemi | StopBeforeMatch);
4681 continue;
4682 }
4683 AttrParsed = false;
4684 }
4685
4686 // Eat all remaining superfluous commas before parsing the next attribute.
4687 while (TryConsumeToken(Expected: tok::comma))
4688 ;
4689
4690 SourceLocation ScopeLoc, AttrLoc;
4691 IdentifierInfo *ScopeName = nullptr, *AttrName = nullptr;
4692
4693 AttrName = TryParseCXX11AttributeIdentifier(
4694 Loc&: AttrLoc, Completion: SemaCodeCompletion::AttributeCompletion::Attribute,
4695 Scope: CommonScopeName);
4696 if (!AttrName)
4697 // Break out to the "expected ']'" diagnostic.
4698 break;
4699
4700 // scoped attribute
4701 if (TryConsumeToken(Expected: tok::coloncolon)) {
4702 ScopeName = AttrName;
4703 ScopeLoc = AttrLoc;
4704
4705 AttrName = TryParseCXX11AttributeIdentifier(
4706 Loc&: AttrLoc, Completion: SemaCodeCompletion::AttributeCompletion::Attribute,
4707 Scope: ScopeName);
4708 if (!AttrName) {
4709 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
4710 SkipUntil(T1: tok::r_square, T2: tok::comma, Flags: StopAtSemi | StopBeforeMatch);
4711 continue;
4712 }
4713 }
4714
4715 if (CommonScopeName) {
4716 if (ScopeName) {
4717 Diag(ScopeLoc, diag::err_using_attribute_ns_conflict)
4718 << SourceRange(CommonScopeLoc);
4719 } else {
4720 ScopeName = CommonScopeName;
4721 ScopeLoc = CommonScopeLoc;
4722 }
4723 }
4724
4725 // Parse attribute arguments
4726 if (Tok.is(K: tok::l_paren))
4727 AttrParsed = ParseCXX11AttributeArgs(AttrName, AttrNameLoc: AttrLoc, Attrs, EndLoc,
4728 ScopeName, ScopeLoc, OpenMPTokens);
4729
4730 if (!AttrParsed) {
4731 Attrs.addNew(
4732 attrName: AttrName,
4733 attrRange: SourceRange(ScopeLoc.isValid() ? ScopeLoc : AttrLoc, AttrLoc),
4734 scopeName: ScopeName, scopeLoc: ScopeLoc, args: nullptr, numArgs: 0,
4735 form: getLangOpts().CPlusPlus ? ParsedAttr::Form::CXX11()
4736 : ParsedAttr::Form::C23());
4737 AttrParsed = true;
4738 }
4739
4740 if (TryConsumeToken(tok::ellipsis))
4741 Diag(Tok, diag::err_cxx11_attribute_forbids_ellipsis) << AttrName;
4742 }
4743
4744 // If we hit an error and recovered by parsing up to a semicolon, eat the
4745 // semicolon and don't issue further diagnostics about missing brackets.
4746 if (Tok.is(K: tok::semi)) {
4747 ConsumeToken();
4748 return;
4749 }
4750
4751 SourceLocation CloseLoc = Tok.getLocation();
4752 if (ExpectAndConsume(ExpectedTok: tok::r_square))
4753 SkipUntil(T: tok::r_square);
4754 else if (Tok.is(K: tok::r_square))
4755 checkCompoundToken(FirstTokLoc: CloseLoc, FirstTokKind: tok::r_square, Op: CompoundToken::AttrEnd);
4756 if (EndLoc)
4757 *EndLoc = Tok.getLocation();
4758 if (ExpectAndConsume(ExpectedTok: tok::r_square))
4759 SkipUntil(T: tok::r_square);
4760}
4761
4762void Parser::ParseCXX11Attributes(ParsedAttributes &Attrs) {
4763 SourceLocation StartLoc = Tok.getLocation();
4764 SourceLocation EndLoc = StartLoc;
4765
4766 do {
4767 ParseCXX11AttributeSpecifier(Attrs, EndLoc: &EndLoc);
4768 } while (isAllowedCXX11AttributeSpecifier());
4769
4770 Attrs.Range = SourceRange(StartLoc, EndLoc);
4771}
4772
4773void Parser::DiagnoseAndSkipCXX11Attributes() {
4774 auto Keyword =
4775 Tok.isRegularKeywordAttribute() ? Tok.getIdentifierInfo() : nullptr;
4776 // Start and end location of an attribute or an attribute list.
4777 SourceLocation StartLoc = Tok.getLocation();
4778 SourceLocation EndLoc = SkipCXX11Attributes();
4779
4780 if (EndLoc.isValid()) {
4781 SourceRange Range(StartLoc, EndLoc);
4782 (Keyword ? Diag(StartLoc, diag::err_keyword_not_allowed) << Keyword
4783 : Diag(StartLoc, diag::err_attributes_not_allowed))
4784 << Range;
4785 }
4786}
4787
4788SourceLocation Parser::SkipCXX11Attributes() {
4789 SourceLocation EndLoc;
4790
4791 if (isCXX11AttributeSpecifier() == CXX11AttributeKind::NotAttributeSpecifier)
4792 return EndLoc;
4793
4794 do {
4795 if (Tok.is(K: tok::l_square)) {
4796 BalancedDelimiterTracker T(*this, tok::l_square);
4797 T.consumeOpen();
4798 T.skipToEnd();
4799 EndLoc = T.getCloseLocation();
4800 } else if (Tok.isRegularKeywordAttribute() &&
4801 !doesKeywordAttributeTakeArgs(Kind: Tok.getKind())) {
4802 EndLoc = Tok.getLocation();
4803 ConsumeToken();
4804 } else {
4805 assert((Tok.is(tok::kw_alignas) || Tok.isRegularKeywordAttribute()) &&
4806 "not an attribute specifier");
4807 ConsumeToken();
4808 BalancedDelimiterTracker T(*this, tok::l_paren);
4809 if (!T.consumeOpen())
4810 T.skipToEnd();
4811 EndLoc = T.getCloseLocation();
4812 }
4813 } while (isCXX11AttributeSpecifier() !=
4814 CXX11AttributeKind::NotAttributeSpecifier);
4815
4816 return EndLoc;
4817}
4818
4819void Parser::ParseMicrosoftUuidAttributeArgs(ParsedAttributes &Attrs) {
4820 assert(Tok.is(tok::identifier) && "Not a Microsoft attribute list");
4821 IdentifierInfo *UuidIdent = Tok.getIdentifierInfo();
4822 assert(UuidIdent->getName() == "uuid" && "Not a Microsoft attribute list");
4823
4824 SourceLocation UuidLoc = Tok.getLocation();
4825 ConsumeToken();
4826
4827 // Ignore the left paren location for now.
4828 BalancedDelimiterTracker T(*this, tok::l_paren);
4829 if (T.consumeOpen()) {
4830 Diag(Tok, diag::err_expected) << tok::l_paren;
4831 return;
4832 }
4833
4834 ArgsVector ArgExprs;
4835 if (isTokenStringLiteral()) {
4836 // Easy case: uuid("...") -- quoted string.
4837 ExprResult StringResult = ParseUnevaluatedStringLiteralExpression();
4838 if (StringResult.isInvalid())
4839 return;
4840 ArgExprs.push_back(Elt: StringResult.get());
4841 } else {
4842 // something like uuid({000000A0-0000-0000-C000-000000000049}) -- no
4843 // quotes in the parens. Just append the spelling of all tokens encountered
4844 // until the closing paren.
4845
4846 SmallString<42> StrBuffer; // 2 "", 36 bytes UUID, 2 optional {}, 1 nul
4847 StrBuffer += "\"";
4848
4849 // Since none of C++'s keywords match [a-f]+, accepting just tok::l_brace,
4850 // tok::r_brace, tok::minus, tok::identifier (think C000) and
4851 // tok::numeric_constant (0000) should be enough. But the spelling of the
4852 // uuid argument is checked later anyways, so there's no harm in accepting
4853 // almost anything here.
4854 // cl is very strict about whitespace in this form and errors out if any
4855 // is present, so check the space flags on the tokens.
4856 SourceLocation StartLoc = Tok.getLocation();
4857 while (Tok.isNot(K: tok::r_paren)) {
4858 if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) {
4859 Diag(Tok, diag::err_attribute_uuid_malformed_guid);
4860 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
4861 return;
4862 }
4863 SmallString<16> SpellingBuffer;
4864 SpellingBuffer.resize(N: Tok.getLength() + 1);
4865 bool Invalid = false;
4866 StringRef TokSpelling = PP.getSpelling(Tok, Buffer&: SpellingBuffer, Invalid: &Invalid);
4867 if (Invalid) {
4868 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
4869 return;
4870 }
4871 StrBuffer += TokSpelling;
4872 ConsumeAnyToken();
4873 }
4874 StrBuffer += "\"";
4875
4876 if (Tok.hasLeadingSpace() || Tok.isAtStartOfLine()) {
4877 Diag(Tok, diag::err_attribute_uuid_malformed_guid);
4878 ConsumeParen();
4879 return;
4880 }
4881
4882 // Pretend the user wrote the appropriate string literal here.
4883 // ActOnStringLiteral() copies the string data into the literal, so it's
4884 // ok that the Token points to StrBuffer.
4885 Token Toks[1];
4886 Toks[0].startToken();
4887 Toks[0].setKind(tok::string_literal);
4888 Toks[0].setLocation(StartLoc);
4889 Toks[0].setLiteralData(StrBuffer.data());
4890 Toks[0].setLength(StrBuffer.size());
4891 StringLiteral *UuidString =
4892 cast<StringLiteral>(Val: Actions.ActOnUnevaluatedStringLiteral(StringToks: Toks).get());
4893 ArgExprs.push_back(UuidString);
4894 }
4895
4896 if (!T.consumeClose()) {
4897 Attrs.addNew(attrName: UuidIdent, attrRange: SourceRange(UuidLoc, T.getCloseLocation()), scopeName: nullptr,
4898 scopeLoc: SourceLocation(), args: ArgExprs.data(), numArgs: ArgExprs.size(),
4899 form: ParsedAttr::Form::Microsoft());
4900 }
4901}
4902
4903void Parser::ParseMicrosoftRootSignatureAttributeArgs(ParsedAttributes &Attrs) {
4904 assert(Tok.is(tok::identifier) &&
4905 "Expected an identifier to denote which MS attribute to consider");
4906 IdentifierInfo *RootSignatureIdent = Tok.getIdentifierInfo();
4907 assert(RootSignatureIdent->getName() == "RootSignature" &&
4908 "Expected RootSignature identifier for root signature attribute");
4909
4910 SourceLocation RootSignatureLoc = Tok.getLocation();
4911 ConsumeToken();
4912
4913 // Ignore the left paren location for now.
4914 BalancedDelimiterTracker T(*this, tok::l_paren);
4915 if (T.consumeOpen()) {
4916 Diag(Tok, diag::err_expected) << tok::l_paren;
4917 return;
4918 }
4919
4920 auto ProcessStringLiteral = [this]() -> std::optional<StringLiteral *> {
4921 if (!isTokenStringLiteral())
4922 return std::nullopt;
4923
4924 ExprResult StringResult = ParseUnevaluatedStringLiteralExpression();
4925 if (StringResult.isInvalid())
4926 return std::nullopt;
4927
4928 if (auto Lit = dyn_cast<StringLiteral>(Val: StringResult.get()))
4929 return Lit;
4930
4931 return std::nullopt;
4932 };
4933
4934 auto StrLiteral = ProcessStringLiteral();
4935 if (!StrLiteral.has_value()) {
4936 Diag(Tok, diag::err_expected_string_literal)
4937 << /*in attributes...*/ 4 << RootSignatureIdent->getName();
4938 SkipUntil(T: tok::r_paren, Flags: StopAtSemi | StopBeforeMatch);
4939 T.consumeClose();
4940 return;
4941 }
4942
4943 // Construct our identifier
4944 StringRef Signature = StrLiteral.value()->getString();
4945 auto Hash = llvm::hash_value(S: Signature);
4946 std::string IdStr = "__hlsl_rootsig_decl_" + std::to_string(val: Hash);
4947 IdentifierInfo *DeclIdent = &(Actions.getASTContext().Idents.get(Name: IdStr));
4948
4949 LookupResult R(Actions, DeclIdent, SourceLocation(),
4950 Sema::LookupOrdinaryName);
4951 // Check if we have already found a decl of the same name, if we haven't
4952 // then parse the root signature string and construct the in-memory elements
4953 if (!Actions.LookupQualifiedName(R, LookupCtx: Actions.CurContext)) {
4954 SourceLocation SignatureLoc =
4955 StrLiteral.value()->getExprLoc().getLocWithOffset(
4956 1); // offset 1 for '"'
4957 // Invoke the root signature parser to construct the in-memory constructs
4958 hlsl::RootSignatureLexer Lexer(Signature, SignatureLoc);
4959 SmallVector<llvm::hlsl::rootsig::RootElement> RootElements;
4960 hlsl::RootSignatureParser Parser(RootElements, Lexer, PP);
4961 if (Parser.parse()) {
4962 T.consumeClose();
4963 return;
4964 }
4965
4966 // Create the Root Signature
4967 auto *SignatureDecl = HLSLRootSignatureDecl::Create(
4968 C&: Actions.getASTContext(), /*DeclContext=*/DC: Actions.CurContext,
4969 Loc: RootSignatureLoc, ID: DeclIdent, RootElements);
4970 SignatureDecl->setImplicit();
4971 Actions.PushOnScopeChains(SignatureDecl, getCurScope());
4972 }
4973
4974 // Create the arg for the ParsedAttr
4975 IdentifierLoc *ILoc = ::new (Actions.getASTContext())
4976 IdentifierLoc(RootSignatureLoc, DeclIdent);
4977
4978 ArgsVector Args = {ILoc};
4979
4980 if (!T.consumeClose())
4981 Attrs.addNew(attrName: RootSignatureIdent,
4982 attrRange: SourceRange(RootSignatureLoc, T.getCloseLocation()), scopeName: nullptr,
4983 scopeLoc: SourceLocation(), args: Args.data(), numArgs: Args.size(),
4984 form: ParsedAttr::Form::Microsoft());
4985}
4986
4987void Parser::ParseMicrosoftAttributes(ParsedAttributes &Attrs) {
4988 assert(Tok.is(tok::l_square) && "Not a Microsoft attribute list");
4989
4990 SourceLocation StartLoc = Tok.getLocation();
4991 SourceLocation EndLoc = StartLoc;
4992 do {
4993 // FIXME: If this is actually a C++11 attribute, parse it as one.
4994 BalancedDelimiterTracker T(*this, tok::l_square);
4995 T.consumeOpen();
4996
4997 // Skip most ms attributes except for a specific list.
4998 while (true) {
4999 SkipUntil(T1: tok::r_square, T2: tok::identifier,
5000 Flags: StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
5001 if (Tok.is(K: tok::code_completion)) {
5002 cutOffParsing();
5003 Actions.CodeCompletion().CodeCompleteAttribute(
5004 Syntax: AttributeCommonInfo::AS_Microsoft,
5005 Completion: SemaCodeCompletion::AttributeCompletion::Attribute,
5006 /*Scope=*/nullptr);
5007 break;
5008 }
5009 if (Tok.isNot(K: tok::identifier)) // ']', but also eof
5010 break;
5011 if (Tok.getIdentifierInfo()->getName() == "uuid")
5012 ParseMicrosoftUuidAttributeArgs(Attrs);
5013 else if (Tok.getIdentifierInfo()->getName() == "RootSignature")
5014 ParseMicrosoftRootSignatureAttributeArgs(Attrs);
5015 else {
5016 IdentifierInfo *II = Tok.getIdentifierInfo();
5017 SourceLocation NameLoc = Tok.getLocation();
5018 ConsumeToken();
5019 ParsedAttr::Kind AttrKind =
5020 ParsedAttr::getParsedKind(Name: II, Scope: nullptr, SyntaxUsed: ParsedAttr::AS_Microsoft);
5021 // For HLSL we want to handle all attributes, but for MSVC compat, we
5022 // silently ignore unknown Microsoft attributes.
5023 if (getLangOpts().HLSL || AttrKind != ParsedAttr::UnknownAttribute) {
5024 bool AttrParsed = false;
5025 if (Tok.is(K: tok::l_paren)) {
5026 CachedTokens OpenMPTokens;
5027 AttrParsed =
5028 ParseCXX11AttributeArgs(AttrName: II, AttrNameLoc: NameLoc, Attrs, EndLoc: &EndLoc, ScopeName: nullptr,
5029 ScopeLoc: SourceLocation(), OpenMPTokens);
5030 ReplayOpenMPAttributeTokens(OpenMPTokens);
5031 }
5032 if (!AttrParsed) {
5033 Attrs.addNew(attrName: II, attrRange: NameLoc, scopeName: nullptr, scopeLoc: SourceLocation(), args: nullptr, numArgs: 0,
5034 form: ParsedAttr::Form::Microsoft());
5035 }
5036 }
5037 }
5038 }
5039
5040 T.consumeClose();
5041 EndLoc = T.getCloseLocation();
5042 } while (Tok.is(K: tok::l_square));
5043
5044 Attrs.Range = SourceRange(StartLoc, EndLoc);
5045}
5046
5047void Parser::ParseMicrosoftIfExistsClassDeclaration(
5048 DeclSpec::TST TagType, ParsedAttributes &AccessAttrs,
5049 AccessSpecifier &CurAS) {
5050 IfExistsCondition Result;
5051 if (ParseMicrosoftIfExistsCondition(Result))
5052 return;
5053
5054 BalancedDelimiterTracker Braces(*this, tok::l_brace);
5055 if (Braces.consumeOpen()) {
5056 Diag(Tok, diag::err_expected) << tok::l_brace;
5057 return;
5058 }
5059
5060 switch (Result.Behavior) {
5061 case IfExistsBehavior::Parse:
5062 // Parse the declarations below.
5063 break;
5064
5065 case IfExistsBehavior::Dependent:
5066 Diag(Result.KeywordLoc, diag::warn_microsoft_dependent_exists)
5067 << Result.IsIfExists;
5068 // Fall through to skip.
5069 [[fallthrough]];
5070
5071 case IfExistsBehavior::Skip:
5072 Braces.skipToEnd();
5073 return;
5074 }
5075
5076 while (Tok.isNot(K: tok::r_brace) && !isEofOrEom()) {
5077 // __if_exists, __if_not_exists can nest.
5078 if (Tok.isOneOf(K1: tok::kw___if_exists, K2: tok::kw___if_not_exists)) {
5079 ParseMicrosoftIfExistsClassDeclaration(TagType, AccessAttrs, CurAS);
5080 continue;
5081 }
5082
5083 // Check for extraneous top-level semicolon.
5084 if (Tok.is(K: tok::semi)) {
5085 ConsumeExtraSemi(Kind: ExtraSemiKind::InsideStruct, T: TagType);
5086 continue;
5087 }
5088
5089 AccessSpecifier AS = getAccessSpecifierIfPresent();
5090 if (AS != AS_none) {
5091 // Current token is a C++ access specifier.
5092 CurAS = AS;
5093 SourceLocation ASLoc = Tok.getLocation();
5094 ConsumeToken();
5095 if (Tok.is(K: tok::colon))
5096 Actions.ActOnAccessSpecifier(Access: AS, ASLoc, ColonLoc: Tok.getLocation(),
5097 Attrs: ParsedAttributesView{});
5098 else
5099 Diag(Tok, diag::err_expected) << tok::colon;
5100 ConsumeToken();
5101 continue;
5102 }
5103
5104 ParsedTemplateInfo TemplateInfo;
5105 // Parse all the comma separated declarators.
5106 ParseCXXClassMemberDeclaration(AS: CurAS, AccessAttrs, TemplateInfo);
5107 }
5108
5109 Braces.consumeClose();
5110}
5111

Provided by KDAB

Privacy Policy
Learn to use CMake with our Intro Training
Find out more

source code of clang/lib/Parse/ParseDeclCXX.cpp