1//===--- ParseDecl.cpp - 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 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/AddressSpaces.h"
17#include "clang/Basic/AttributeCommonInfo.h"
18#include "clang/Basic/Attributes.h"
19#include "clang/Basic/CharInfo.h"
20#include "clang/Basic/DiagnosticParse.h"
21#include "clang/Basic/TargetInfo.h"
22#include "clang/Basic/TokenKinds.h"
23#include "clang/Parse/Parser.h"
24#include "clang/Parse/RAIIObjectsForParser.h"
25#include "clang/Sema/EnterExpressionEvaluationContext.h"
26#include "clang/Sema/Lookup.h"
27#include "clang/Sema/ParsedAttr.h"
28#include "clang/Sema/ParsedTemplate.h"
29#include "clang/Sema/Scope.h"
30#include "clang/Sema/SemaCUDA.h"
31#include "clang/Sema/SemaCodeCompletion.h"
32#include "clang/Sema/SemaObjC.h"
33#include "clang/Sema/SemaOpenMP.h"
34#include "llvm/ADT/SmallSet.h"
35#include "llvm/ADT/StringSwitch.h"
36#include <optional>
37
38using namespace clang;
39
40//===----------------------------------------------------------------------===//
41// C99 6.7: Declarations.
42//===----------------------------------------------------------------------===//
43
44TypeResult Parser::ParseTypeName(SourceRange *Range, DeclaratorContext Context,
45 AccessSpecifier AS, Decl **OwnedType,
46 ParsedAttributes *Attrs) {
47 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
48 if (DSC == DeclSpecContext::DSC_normal)
49 DSC = DeclSpecContext::DSC_type_specifier;
50
51 // Parse the common declaration-specifiers piece.
52 DeclSpec DS(AttrFactory);
53 if (Attrs)
54 DS.addAttributes(AL: *Attrs);
55 ParseSpecifierQualifierList(DS, AS, DSC);
56 if (OwnedType)
57 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : nullptr;
58
59 // Move declspec attributes to ParsedAttributes
60 if (Attrs) {
61 llvm::SmallVector<ParsedAttr *, 1> ToBeMoved;
62 for (ParsedAttr &AL : DS.getAttributes()) {
63 if (AL.isDeclspecAttribute())
64 ToBeMoved.push_back(Elt: &AL);
65 }
66
67 for (ParsedAttr *AL : ToBeMoved)
68 Attrs->takeOneFrom(Other&: DS.getAttributes(), PA: AL);
69 }
70
71 // Parse the abstract-declarator, if present.
72 Declarator DeclaratorInfo(DS, ParsedAttributesView::none(), Context);
73 ParseDeclarator(D&: DeclaratorInfo);
74 if (Range)
75 *Range = DeclaratorInfo.getSourceRange();
76
77 if (DeclaratorInfo.isInvalidType())
78 return true;
79
80 return Actions.ActOnTypeName(D&: DeclaratorInfo);
81}
82
83/// Normalizes an attribute name by dropping prefixed and suffixed __.
84static StringRef normalizeAttrName(StringRef Name) {
85 if (Name.size() >= 4 && Name.starts_with(Prefix: "__") && Name.ends_with(Suffix: "__"))
86 return Name.drop_front(N: 2).drop_back(N: 2);
87 return Name;
88}
89
90/// returns true iff attribute is annotated with `LateAttrParseExperimentalExt`
91/// in `Attr.td`.
92static bool IsAttributeLateParsedExperimentalExt(const IdentifierInfo &II) {
93#define CLANG_ATTR_LATE_PARSED_EXPERIMENTAL_EXT_LIST
94 return llvm::StringSwitch<bool>(normalizeAttrName(Name: II.getName()))
95#include "clang/Parse/AttrParserStringSwitches.inc"
96 .Default(Value: false);
97#undef CLANG_ATTR_LATE_PARSED_EXPERIMENTAL_EXT_LIST
98}
99
100/// returns true iff attribute is annotated with `LateAttrParseStandard` in
101/// `Attr.td`.
102static bool IsAttributeLateParsedStandard(const IdentifierInfo &II) {
103#define CLANG_ATTR_LATE_PARSED_LIST
104 return llvm::StringSwitch<bool>(normalizeAttrName(Name: II.getName()))
105#include "clang/Parse/AttrParserStringSwitches.inc"
106 .Default(Value: false);
107#undef CLANG_ATTR_LATE_PARSED_LIST
108}
109
110/// Check if the a start and end source location expand to the same macro.
111static bool FindLocsWithCommonFileID(Preprocessor &PP, SourceLocation StartLoc,
112 SourceLocation EndLoc) {
113 if (!StartLoc.isMacroID() || !EndLoc.isMacroID())
114 return false;
115
116 SourceManager &SM = PP.getSourceManager();
117 if (SM.getFileID(SpellingLoc: StartLoc) != SM.getFileID(SpellingLoc: EndLoc))
118 return false;
119
120 bool AttrStartIsInMacro =
121 Lexer::isAtStartOfMacroExpansion(loc: StartLoc, SM, LangOpts: PP.getLangOpts());
122 bool AttrEndIsInMacro =
123 Lexer::isAtEndOfMacroExpansion(loc: EndLoc, SM, LangOpts: PP.getLangOpts());
124 return AttrStartIsInMacro && AttrEndIsInMacro;
125}
126
127void Parser::ParseAttributes(unsigned WhichAttrKinds, ParsedAttributes &Attrs,
128 LateParsedAttrList *LateAttrs) {
129 bool MoreToParse;
130 do {
131 // Assume there's nothing left to parse, but if any attributes are in fact
132 // parsed, loop to ensure all specified attribute combinations are parsed.
133 MoreToParse = false;
134 if (WhichAttrKinds & PAKM_CXX11)
135 MoreToParse |= MaybeParseCXX11Attributes(Attrs);
136 if (WhichAttrKinds & PAKM_GNU)
137 MoreToParse |= MaybeParseGNUAttributes(Attrs, LateAttrs);
138 if (WhichAttrKinds & PAKM_Declspec)
139 MoreToParse |= MaybeParseMicrosoftDeclSpecs(Attrs);
140 } while (MoreToParse);
141}
142
143bool Parser::ParseSingleGNUAttribute(ParsedAttributes &Attrs,
144 SourceLocation &EndLoc,
145 LateParsedAttrList *LateAttrs,
146 Declarator *D) {
147 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
148 if (!AttrName)
149 return true;
150
151 SourceLocation AttrNameLoc = ConsumeToken();
152
153 if (Tok.isNot(K: tok::l_paren)) {
154 Attrs.addNew(attrName: AttrName, attrRange: AttrNameLoc, scope: AttributeScopeInfo(), args: nullptr, numArgs: 0,
155 form: ParsedAttr::Form::GNU());
156 return false;
157 }
158
159 bool LateParse = false;
160 if (!LateAttrs)
161 LateParse = false;
162 else if (LateAttrs->lateAttrParseExperimentalExtOnly()) {
163 // The caller requested that this attribute **only** be late
164 // parsed for `LateAttrParseExperimentalExt` attributes. This will
165 // only be late parsed if the experimental language option is enabled.
166 LateParse = getLangOpts().ExperimentalLateParseAttributes &&
167 IsAttributeLateParsedExperimentalExt(II: *AttrName);
168 } else {
169 // The caller did not restrict late parsing to only
170 // `LateAttrParseExperimentalExt` attributes so late parse
171 // both `LateAttrParseStandard` and `LateAttrParseExperimentalExt`
172 // attributes.
173 LateParse = IsAttributeLateParsedExperimentalExt(II: *AttrName) ||
174 IsAttributeLateParsedStandard(II: *AttrName);
175 }
176
177 // Handle "parameterized" attributes
178 if (!LateParse) {
179 ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, EndLoc: &EndLoc, ScopeName: nullptr,
180 ScopeLoc: SourceLocation(), Form: ParsedAttr::Form::GNU(), D);
181 return false;
182 }
183
184 // Handle attributes with arguments that require late parsing.
185 LateParsedAttribute *LA =
186 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
187 LateAttrs->push_back(Elt: LA);
188
189 // Attributes in a class are parsed at the end of the class, along
190 // with other late-parsed declarations.
191 if (!ClassStack.empty() && !LateAttrs->parseSoon())
192 getCurrentClass().LateParsedDeclarations.push_back(Elt: LA);
193
194 // Be sure ConsumeAndStoreUntil doesn't see the start l_paren, since it
195 // recursively consumes balanced parens.
196 LA->Toks.push_back(Elt: Tok);
197 ConsumeParen();
198 // Consume everything up to and including the matching right parens.
199 ConsumeAndStoreUntil(T1: tok::r_paren, Toks&: LA->Toks, /*StopAtSemi=*/true);
200
201 Token Eof;
202 Eof.startToken();
203 Eof.setLocation(Tok.getLocation());
204 LA->Toks.push_back(Elt: Eof);
205
206 return false;
207}
208
209void Parser::ParseGNUAttributes(ParsedAttributes &Attrs,
210 LateParsedAttrList *LateAttrs, Declarator *D) {
211 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
212
213 SourceLocation StartLoc = Tok.getLocation();
214 SourceLocation EndLoc = StartLoc;
215
216 while (Tok.is(K: tok::kw___attribute)) {
217 SourceLocation AttrTokLoc = ConsumeToken();
218 unsigned OldNumAttrs = Attrs.size();
219 unsigned OldNumLateAttrs = LateAttrs ? LateAttrs->size() : 0;
220
221 if (ExpectAndConsume(ExpectedTok: tok::l_paren, Diag: diag::err_expected_lparen_after,
222 DiagMsg: "attribute")) {
223 SkipUntil(T: tok::r_paren, Flags: StopAtSemi); // skip until ) or ;
224 return;
225 }
226 if (ExpectAndConsume(ExpectedTok: tok::l_paren, Diag: diag::err_expected_lparen_after, DiagMsg: "(")) {
227 SkipUntil(T: tok::r_paren, Flags: StopAtSemi); // skip until ) or ;
228 return;
229 }
230 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
231 do {
232 // Eat preceeding commas to allow __attribute__((,,,foo))
233 while (TryConsumeToken(Expected: tok::comma))
234 ;
235
236 // Expect an identifier or declaration specifier (const, int, etc.)
237 if (Tok.isAnnotation())
238 break;
239 if (Tok.is(K: tok::code_completion)) {
240 cutOffParsing();
241 Actions.CodeCompletion().CodeCompleteAttribute(
242 Syntax: AttributeCommonInfo::Syntax::AS_GNU);
243 break;
244 }
245
246 if (ParseSingleGNUAttribute(Attrs, EndLoc, LateAttrs, D))
247 break;
248 } while (Tok.is(K: tok::comma));
249
250 if (ExpectAndConsume(ExpectedTok: tok::r_paren))
251 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
252 SourceLocation Loc = Tok.getLocation();
253 if (ExpectAndConsume(ExpectedTok: tok::r_paren))
254 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
255 EndLoc = Loc;
256
257 // If this was declared in a macro, attach the macro IdentifierInfo to the
258 // parsed attribute.
259 auto &SM = PP.getSourceManager();
260 if (!SM.isWrittenInBuiltinFile(Loc: SM.getSpellingLoc(Loc: AttrTokLoc)) &&
261 FindLocsWithCommonFileID(PP, StartLoc: AttrTokLoc, EndLoc: Loc)) {
262 CharSourceRange ExpansionRange = SM.getExpansionRange(Loc: AttrTokLoc);
263 StringRef FoundName =
264 Lexer::getSourceText(Range: ExpansionRange, SM, LangOpts: PP.getLangOpts());
265 IdentifierInfo *MacroII = PP.getIdentifierInfo(Name: FoundName);
266
267 for (unsigned i = OldNumAttrs; i < Attrs.size(); ++i)
268 Attrs[i].setMacroIdentifier(MacroName: MacroII, Loc: ExpansionRange.getBegin());
269
270 if (LateAttrs) {
271 for (unsigned i = OldNumLateAttrs; i < LateAttrs->size(); ++i)
272 (*LateAttrs)[i]->MacroII = MacroII;
273 }
274 }
275 }
276
277 Attrs.Range = SourceRange(StartLoc, EndLoc);
278}
279
280/// Determine whether the given attribute has an identifier argument.
281static bool attributeHasIdentifierArg(const llvm::Triple &T,
282 const IdentifierInfo &II,
283 ParsedAttr::Syntax Syntax,
284 IdentifierInfo *ScopeName) {
285#define CLANG_ATTR_IDENTIFIER_ARG_LIST
286 return llvm::StringSwitch<bool>(normalizeAttrName(Name: II.getName()))
287#include "clang/Parse/AttrParserStringSwitches.inc"
288 .Default(Value: false);
289#undef CLANG_ATTR_IDENTIFIER_ARG_LIST
290}
291
292/// Determine whether the given attribute has string arguments.
293static ParsedAttributeArgumentsProperties
294attributeStringLiteralListArg(const llvm::Triple &T, const IdentifierInfo &II,
295 ParsedAttr::Syntax Syntax,
296 IdentifierInfo *ScopeName) {
297#define CLANG_ATTR_STRING_LITERAL_ARG_LIST
298 return llvm::StringSwitch<uint32_t>(normalizeAttrName(Name: II.getName()))
299#include "clang/Parse/AttrParserStringSwitches.inc"
300 .Default(Value: 0);
301#undef CLANG_ATTR_STRING_LITERAL_ARG_LIST
302}
303
304/// Determine whether the given attribute has a variadic identifier argument.
305static bool attributeHasVariadicIdentifierArg(const IdentifierInfo &II,
306 ParsedAttr::Syntax Syntax,
307 IdentifierInfo *ScopeName) {
308#define CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST
309 return llvm::StringSwitch<bool>(normalizeAttrName(Name: II.getName()))
310#include "clang/Parse/AttrParserStringSwitches.inc"
311 .Default(Value: false);
312#undef CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST
313}
314
315/// Determine whether the given attribute treats kw_this as an identifier.
316static bool attributeTreatsKeywordThisAsIdentifier(const IdentifierInfo &II,
317 ParsedAttr::Syntax Syntax,
318 IdentifierInfo *ScopeName) {
319#define CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST
320 return llvm::StringSwitch<bool>(normalizeAttrName(Name: II.getName()))
321#include "clang/Parse/AttrParserStringSwitches.inc"
322 .Default(Value: false);
323#undef CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST
324}
325
326/// Determine if an attribute accepts parameter packs.
327static bool attributeAcceptsExprPack(const IdentifierInfo &II,
328 ParsedAttr::Syntax Syntax,
329 IdentifierInfo *ScopeName) {
330#define CLANG_ATTR_ACCEPTS_EXPR_PACK
331 return llvm::StringSwitch<bool>(normalizeAttrName(Name: II.getName()))
332#include "clang/Parse/AttrParserStringSwitches.inc"
333 .Default(Value: false);
334#undef CLANG_ATTR_ACCEPTS_EXPR_PACK
335}
336
337/// Determine whether the given attribute parses a type argument.
338static bool attributeIsTypeArgAttr(const IdentifierInfo &II,
339 ParsedAttr::Syntax Syntax,
340 IdentifierInfo *ScopeName) {
341#define CLANG_ATTR_TYPE_ARG_LIST
342 return llvm::StringSwitch<bool>(normalizeAttrName(Name: II.getName()))
343#include "clang/Parse/AttrParserStringSwitches.inc"
344 .Default(Value: false);
345#undef CLANG_ATTR_TYPE_ARG_LIST
346}
347
348/// Determine whether the given attribute takes a strict identifier argument.
349static bool attributeHasStrictIdentifierArgs(const IdentifierInfo &II,
350 ParsedAttr::Syntax Syntax,
351 IdentifierInfo *ScopeName) {
352#define CLANG_ATTR_STRICT_IDENTIFIER_ARG_LIST
353 return llvm::StringSwitch<bool>(normalizeAttrName(Name: II.getName()))
354#include "clang/Parse/AttrParserStringSwitches.inc"
355 .Default(Value: false);
356#undef CLANG_ATTR_STRICT_IDENTIFIER_ARG_LIST
357}
358
359/// Determine whether the given attribute requires parsing its arguments
360/// in an unevaluated context or not.
361static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II,
362 ParsedAttr::Syntax Syntax,
363 IdentifierInfo *ScopeName) {
364#define CLANG_ATTR_ARG_CONTEXT_LIST
365 return llvm::StringSwitch<bool>(normalizeAttrName(Name: II.getName()))
366#include "clang/Parse/AttrParserStringSwitches.inc"
367 .Default(Value: false);
368#undef CLANG_ATTR_ARG_CONTEXT_LIST
369}
370
371IdentifierLoc *Parser::ParseIdentifierLoc() {
372 assert(Tok.is(tok::identifier) && "expected an identifier");
373 IdentifierLoc *IL = new (Actions.Context)
374 IdentifierLoc(Tok.getLocation(), Tok.getIdentifierInfo());
375 ConsumeToken();
376 return IL;
377}
378
379void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
380 SourceLocation AttrNameLoc,
381 ParsedAttributes &Attrs,
382 IdentifierInfo *ScopeName,
383 SourceLocation ScopeLoc,
384 ParsedAttr::Form Form) {
385 BalancedDelimiterTracker Parens(*this, tok::l_paren);
386 Parens.consumeOpen();
387
388 TypeResult T;
389 if (Tok.isNot(K: tok::r_paren))
390 T = ParseTypeName();
391
392 if (Parens.consumeClose())
393 return;
394
395 if (T.isInvalid())
396 return;
397
398 if (T.isUsable())
399 Attrs.addNewTypeAttr(
400 attrName: &AttrName, attrRange: SourceRange(AttrNameLoc, Parens.getCloseLocation()),
401 scope: AttributeScopeInfo(ScopeName, ScopeLoc), typeArg: T.get(), formUsed: Form);
402 else
403 Attrs.addNew(attrName: &AttrName, attrRange: SourceRange(AttrNameLoc, Parens.getCloseLocation()),
404 scope: AttributeScopeInfo(ScopeName, ScopeLoc), args: nullptr, numArgs: 0, form: Form);
405}
406
407ExprResult
408Parser::ParseUnevaluatedStringInAttribute(const IdentifierInfo &AttrName) {
409 if (Tok.is(K: tok::l_paren)) {
410 BalancedDelimiterTracker Paren(*this, tok::l_paren);
411 Paren.consumeOpen();
412 ExprResult Res = ParseUnevaluatedStringInAttribute(AttrName);
413 Paren.consumeClose();
414 return Res;
415 }
416 if (!isTokenStringLiteral()) {
417 Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected_string_literal)
418 << /*in attribute...*/ 4 << AttrName.getName();
419 return ExprError();
420 }
421 return ParseUnevaluatedStringLiteralExpression();
422}
423
424bool Parser::ParseAttributeArgumentList(
425 const IdentifierInfo &AttrName, SmallVectorImpl<Expr *> &Exprs,
426 ParsedAttributeArgumentsProperties ArgsProperties) {
427 bool SawError = false;
428 unsigned Arg = 0;
429 while (true) {
430 ExprResult Expr;
431 if (ArgsProperties.isStringLiteralArg(I: Arg)) {
432 Expr = ParseUnevaluatedStringInAttribute(AttrName);
433 } else if (getLangOpts().CPlusPlus11 && Tok.is(K: tok::l_brace)) {
434 Diag(Tok, DiagID: diag::warn_cxx98_compat_generalized_initializer_lists);
435 Expr = ParseBraceInitializer();
436 } else {
437 Expr = ParseAssignmentExpression();
438 }
439
440 if (Tok.is(K: tok::ellipsis))
441 Expr = Actions.ActOnPackExpansion(Pattern: Expr.get(), EllipsisLoc: ConsumeToken());
442 else if (Tok.is(K: tok::code_completion)) {
443 // There's nothing to suggest in here as we parsed a full expression.
444 // Instead fail and propagate the error since caller might have something
445 // the suggest, e.g. signature help in function call. Note that this is
446 // performed before pushing the \p Expr, so that signature help can report
447 // current argument correctly.
448 SawError = true;
449 cutOffParsing();
450 break;
451 }
452
453 if (Expr.isInvalid()) {
454 SawError = true;
455 break;
456 }
457
458 if (Actions.DiagnoseUnexpandedParameterPack(E: Expr.get())) {
459 SawError = true;
460 break;
461 }
462
463 Exprs.push_back(Elt: Expr.get());
464
465 if (Tok.isNot(K: tok::comma))
466 break;
467 // Move to the next argument, remember where the comma was.
468 Token Comma = Tok;
469 ConsumeToken();
470 checkPotentialAngleBracketDelimiter(OpToken: Comma);
471 Arg++;
472 }
473
474 return SawError;
475}
476
477unsigned Parser::ParseAttributeArgsCommon(
478 IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
479 ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
480 SourceLocation ScopeLoc, ParsedAttr::Form Form) {
481 // Ignore the left paren location for now.
482 ConsumeParen();
483
484 bool ChangeKWThisToIdent = attributeTreatsKeywordThisAsIdentifier(
485 II: *AttrName, Syntax: Form.getSyntax(), ScopeName);
486 bool AttributeIsTypeArgAttr =
487 attributeIsTypeArgAttr(II: *AttrName, Syntax: Form.getSyntax(), ScopeName);
488 bool AttributeHasVariadicIdentifierArg =
489 attributeHasVariadicIdentifierArg(II: *AttrName, Syntax: Form.getSyntax(), ScopeName);
490
491 // Interpret "kw_this" as an identifier if the attributed requests it.
492 if (ChangeKWThisToIdent && Tok.is(K: tok::kw_this))
493 Tok.setKind(tok::identifier);
494
495 ArgsVector ArgExprs;
496 if (Tok.is(K: tok::identifier)) {
497 // If this attribute wants an 'identifier' argument, make it so.
498 bool IsIdentifierArg =
499 AttributeHasVariadicIdentifierArg ||
500 attributeHasIdentifierArg(T: getTargetInfo().getTriple(), II: *AttrName,
501 Syntax: Form.getSyntax(), ScopeName);
502 ParsedAttr::Kind AttrKind =
503 ParsedAttr::getParsedKind(Name: AttrName, Scope: ScopeName, SyntaxUsed: Form.getSyntax());
504
505 // If we don't know how to parse this attribute, but this is the only
506 // token in this argument, assume it's meant to be an identifier.
507 if (AttrKind == ParsedAttr::UnknownAttribute ||
508 AttrKind == ParsedAttr::IgnoredAttribute) {
509 const Token &Next = NextToken();
510 IsIdentifierArg = Next.isOneOf(Ks: tok::r_paren, Ks: tok::comma);
511 }
512
513 if (IsIdentifierArg)
514 ArgExprs.push_back(Elt: ParseIdentifierLoc());
515 }
516
517 ParsedType TheParsedType;
518 if (!ArgExprs.empty() ? Tok.is(K: tok::comma) : Tok.isNot(K: tok::r_paren)) {
519 // Eat the comma.
520 if (!ArgExprs.empty())
521 ConsumeToken();
522
523 if (AttributeIsTypeArgAttr) {
524 // FIXME: Multiple type arguments are not implemented.
525 TypeResult T = ParseTypeName();
526 if (T.isInvalid()) {
527 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
528 return 0;
529 }
530 if (T.isUsable())
531 TheParsedType = T.get();
532 } else if (AttributeHasVariadicIdentifierArg ||
533 attributeHasStrictIdentifierArgs(II: *AttrName, Syntax: Form.getSyntax(),
534 ScopeName)) {
535 // Parse variadic identifier arg. This can either consume identifiers or
536 // expressions. Variadic identifier args do not support parameter packs
537 // because those are typically used for attributes with enumeration
538 // arguments, and those enumerations are not something the user could
539 // express via a pack.
540 do {
541 // Interpret "kw_this" as an identifier if the attributed requests it.
542 if (ChangeKWThisToIdent && Tok.is(K: tok::kw_this))
543 Tok.setKind(tok::identifier);
544
545 ExprResult ArgExpr;
546 if (Tok.is(K: tok::identifier)) {
547 ArgExprs.push_back(Elt: ParseIdentifierLoc());
548 } else {
549 bool Uneval = attributeParsedArgsUnevaluated(
550 II: *AttrName, Syntax: Form.getSyntax(), ScopeName);
551 EnterExpressionEvaluationContext Unevaluated(
552 Actions,
553 Uneval ? Sema::ExpressionEvaluationContext::Unevaluated
554 : Sema::ExpressionEvaluationContext::ConstantEvaluated,
555 nullptr,
556 Sema::ExpressionEvaluationContextRecord::EK_AttrArgument);
557
558 ExprResult ArgExpr = ParseAssignmentExpression();
559 if (ArgExpr.isInvalid()) {
560 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
561 return 0;
562 }
563 ArgExprs.push_back(Elt: ArgExpr.get());
564 }
565 // Eat the comma, move to the next argument
566 } while (TryConsumeToken(Expected: tok::comma));
567 } else {
568 // General case. Parse all available expressions.
569 bool Uneval = attributeParsedArgsUnevaluated(II: *AttrName, Syntax: Form.getSyntax(),
570 ScopeName);
571 EnterExpressionEvaluationContext Unevaluated(
572 Actions,
573 Uneval ? Sema::ExpressionEvaluationContext::Unevaluated
574 : Sema::ExpressionEvaluationContext::ConstantEvaluated,
575 nullptr,
576 Sema::ExpressionEvaluationContextRecord::ExpressionKind::
577 EK_AttrArgument);
578
579 ExprVector ParsedExprs;
580 ParsedAttributeArgumentsProperties ArgProperties =
581 attributeStringLiteralListArg(T: getTargetInfo().getTriple(), II: *AttrName,
582 Syntax: Form.getSyntax(), ScopeName);
583 if (ParseAttributeArgumentList(AttrName: *AttrName, Exprs&: ParsedExprs, ArgsProperties: ArgProperties)) {
584 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
585 return 0;
586 }
587
588 // Pack expansion must currently be explicitly supported by an attribute.
589 for (size_t I = 0; I < ParsedExprs.size(); ++I) {
590 if (!isa<PackExpansionExpr>(Val: ParsedExprs[I]))
591 continue;
592
593 if (!attributeAcceptsExprPack(II: *AttrName, Syntax: Form.getSyntax(), ScopeName)) {
594 Diag(Loc: Tok.getLocation(),
595 DiagID: diag::err_attribute_argument_parm_pack_not_supported)
596 << AttrName;
597 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
598 return 0;
599 }
600 }
601
602 llvm::append_range(C&: ArgExprs, R&: ParsedExprs);
603 }
604 }
605
606 SourceLocation RParen = Tok.getLocation();
607 if (!ExpectAndConsume(ExpectedTok: tok::r_paren)) {
608 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
609
610 if (AttributeIsTypeArgAttr && !TheParsedType.get().isNull()) {
611 Attrs.addNewTypeAttr(attrName: AttrName, attrRange: SourceRange(AttrNameLoc, RParen),
612 scope: AttributeScopeInfo(ScopeName, ScopeLoc),
613 typeArg: TheParsedType, formUsed: Form);
614 } else {
615 Attrs.addNew(attrName: AttrName, attrRange: SourceRange(AttrLoc, RParen),
616 scope: AttributeScopeInfo(ScopeName, ScopeLoc), args: ArgExprs.data(),
617 numArgs: ArgExprs.size(), form: Form);
618 }
619 }
620
621 if (EndLoc)
622 *EndLoc = RParen;
623
624 return static_cast<unsigned>(ArgExprs.size() + !TheParsedType.get().isNull());
625}
626
627void Parser::ParseGNUAttributeArgs(
628 IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
629 ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
630 SourceLocation ScopeLoc, ParsedAttr::Form Form, Declarator *D) {
631
632 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
633
634 ParsedAttr::Kind AttrKind =
635 ParsedAttr::getParsedKind(Name: AttrName, Scope: ScopeName, SyntaxUsed: Form.getSyntax());
636
637 if (AttrKind == ParsedAttr::AT_Availability) {
638 ParseAvailabilityAttribute(Availability&: *AttrName, AvailabilityLoc: AttrNameLoc, attrs&: Attrs, endLoc: EndLoc, ScopeName,
639 ScopeLoc, Form);
640 return;
641 } else if (AttrKind == ParsedAttr::AT_ExternalSourceSymbol) {
642 ParseExternalSourceSymbolAttribute(ExternalSourceSymbol&: *AttrName, Loc: AttrNameLoc, Attrs, EndLoc,
643 ScopeName, ScopeLoc, Form);
644 return;
645 } else if (AttrKind == ParsedAttr::AT_ObjCBridgeRelated) {
646 ParseObjCBridgeRelatedAttribute(ObjCBridgeRelated&: *AttrName, ObjCBridgeRelatedLoc: AttrNameLoc, Attrs, EndLoc,
647 ScopeName, ScopeLoc, Form);
648 return;
649 } else if (AttrKind == ParsedAttr::AT_SwiftNewType) {
650 ParseSwiftNewTypeAttribute(AttrName&: *AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
651 ScopeLoc, Form);
652 return;
653 } else if (AttrKind == ParsedAttr::AT_TypeTagForDatatype) {
654 ParseTypeTagForDatatypeAttribute(AttrName&: *AttrName, AttrNameLoc, Attrs, EndLoc,
655 ScopeName, ScopeLoc, Form);
656 return;
657 } else if (attributeIsTypeArgAttr(II: *AttrName, Syntax: Form.getSyntax(), ScopeName)) {
658 ParseAttributeWithTypeArg(AttrName&: *AttrName, AttrNameLoc, Attrs, ScopeName,
659 ScopeLoc, Form);
660 return;
661 } else if (AttrKind == ParsedAttr::AT_CountedBy ||
662 AttrKind == ParsedAttr::AT_CountedByOrNull ||
663 AttrKind == ParsedAttr::AT_SizedBy ||
664 AttrKind == ParsedAttr::AT_SizedByOrNull) {
665 ParseBoundsAttribute(AttrName&: *AttrName, AttrNameLoc, Attrs, ScopeName, ScopeLoc,
666 Form);
667 return;
668 } else if (AttrKind == ParsedAttr::AT_CXXAssume) {
669 ParseCXXAssumeAttributeArg(Attrs, AttrName, AttrNameLoc, ScopeName,
670 ScopeLoc, EndLoc, Form);
671 return;
672 }
673
674 // These may refer to the function arguments, but need to be parsed early to
675 // participate in determining whether it's a redeclaration.
676 std::optional<ParseScope> PrototypeScope;
677 if (normalizeAttrName(Name: AttrName->getName()) == "enable_if" &&
678 D && D->isFunctionDeclarator()) {
679 DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo();
680 PrototypeScope.emplace(args: this, args: Scope::FunctionPrototypeScope |
681 Scope::FunctionDeclarationScope |
682 Scope::DeclScope);
683 for (unsigned i = 0; i != FTI.NumParams; ++i) {
684 ParmVarDecl *Param = cast<ParmVarDecl>(Val: FTI.Params[i].Param);
685 Actions.ActOnReenterCXXMethodParameter(S: getCurScope(), Param);
686 }
687 }
688
689 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
690 ScopeLoc, Form);
691}
692
693unsigned Parser::ParseClangAttributeArgs(
694 IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
695 ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
696 SourceLocation ScopeLoc, ParsedAttr::Form Form) {
697 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
698
699 ParsedAttr::Kind AttrKind =
700 ParsedAttr::getParsedKind(Name: AttrName, Scope: ScopeName, SyntaxUsed: Form.getSyntax());
701
702 switch (AttrKind) {
703 default:
704 return ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
705 ScopeName, ScopeLoc, Form);
706 case ParsedAttr::AT_ExternalSourceSymbol:
707 ParseExternalSourceSymbolAttribute(ExternalSourceSymbol&: *AttrName, Loc: AttrNameLoc, Attrs, EndLoc,
708 ScopeName, ScopeLoc, Form);
709 break;
710 case ParsedAttr::AT_Availability:
711 ParseAvailabilityAttribute(Availability&: *AttrName, AvailabilityLoc: AttrNameLoc, attrs&: Attrs, endLoc: EndLoc, ScopeName,
712 ScopeLoc, Form);
713 break;
714 case ParsedAttr::AT_ObjCBridgeRelated:
715 ParseObjCBridgeRelatedAttribute(ObjCBridgeRelated&: *AttrName, ObjCBridgeRelatedLoc: AttrNameLoc, Attrs, EndLoc,
716 ScopeName, ScopeLoc, Form);
717 break;
718 case ParsedAttr::AT_SwiftNewType:
719 ParseSwiftNewTypeAttribute(AttrName&: *AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
720 ScopeLoc, Form);
721 break;
722 case ParsedAttr::AT_TypeTagForDatatype:
723 ParseTypeTagForDatatypeAttribute(AttrName&: *AttrName, AttrNameLoc, Attrs, EndLoc,
724 ScopeName, ScopeLoc, Form);
725 break;
726
727 case ParsedAttr::AT_CXXAssume:
728 ParseCXXAssumeAttributeArg(Attrs, AttrName, AttrNameLoc, ScopeName,
729 ScopeLoc, EndLoc, Form);
730 break;
731 }
732 return !Attrs.empty() ? Attrs.begin()->getNumArgs() : 0;
733}
734
735bool Parser::ParseMicrosoftDeclSpecArgs(IdentifierInfo *AttrName,
736 SourceLocation AttrNameLoc,
737 ParsedAttributes &Attrs) {
738 unsigned ExistingAttrs = Attrs.size();
739
740 // If the attribute isn't known, we will not attempt to parse any
741 // arguments.
742 if (!hasAttribute(Syntax: AttributeCommonInfo::Syntax::AS_Declspec, Scope: nullptr, Attr: AttrName,
743 Target: getTargetInfo(), LangOpts: getLangOpts())) {
744 // Eat the left paren, then skip to the ending right paren.
745 ConsumeParen();
746 SkipUntil(T: tok::r_paren);
747 return false;
748 }
749
750 SourceLocation OpenParenLoc = Tok.getLocation();
751
752 if (AttrName->getName() == "property") {
753 // The property declspec is more complex in that it can take one or two
754 // assignment expressions as a parameter, but the lhs of the assignment
755 // must be named get or put.
756
757 BalancedDelimiterTracker T(*this, tok::l_paren);
758 T.expectAndConsume(DiagID: diag::err_expected_lparen_after,
759 Msg: AttrName->getNameStart(), SkipToTok: tok::r_paren);
760
761 enum AccessorKind {
762 AK_Invalid = -1,
763 AK_Put = 0,
764 AK_Get = 1 // indices into AccessorNames
765 };
766 IdentifierInfo *AccessorNames[] = {nullptr, nullptr};
767 bool HasInvalidAccessor = false;
768
769 // Parse the accessor specifications.
770 while (true) {
771 // Stop if this doesn't look like an accessor spec.
772 if (!Tok.is(K: tok::identifier)) {
773 // If the user wrote a completely empty list, use a special diagnostic.
774 if (Tok.is(K: tok::r_paren) && !HasInvalidAccessor &&
775 AccessorNames[AK_Put] == nullptr &&
776 AccessorNames[AK_Get] == nullptr) {
777 Diag(Loc: AttrNameLoc, DiagID: diag::err_ms_property_no_getter_or_putter);
778 break;
779 }
780
781 Diag(Loc: Tok.getLocation(), DiagID: diag::err_ms_property_unknown_accessor);
782 break;
783 }
784
785 AccessorKind Kind;
786 SourceLocation KindLoc = Tok.getLocation();
787 StringRef KindStr = Tok.getIdentifierInfo()->getName();
788 if (KindStr == "get") {
789 Kind = AK_Get;
790 } else if (KindStr == "put") {
791 Kind = AK_Put;
792
793 // Recover from the common mistake of using 'set' instead of 'put'.
794 } else if (KindStr == "set") {
795 Diag(Loc: KindLoc, DiagID: diag::err_ms_property_has_set_accessor)
796 << FixItHint::CreateReplacement(RemoveRange: KindLoc, Code: "put");
797 Kind = AK_Put;
798
799 // Handle the mistake of forgetting the accessor kind by skipping
800 // this accessor.
801 } else if (NextToken().is(K: tok::comma) || NextToken().is(K: tok::r_paren)) {
802 Diag(Loc: KindLoc, DiagID: diag::err_ms_property_missing_accessor_kind);
803 ConsumeToken();
804 HasInvalidAccessor = true;
805 goto next_property_accessor;
806
807 // Otherwise, complain about the unknown accessor kind.
808 } else {
809 Diag(Loc: KindLoc, DiagID: diag::err_ms_property_unknown_accessor);
810 HasInvalidAccessor = true;
811 Kind = AK_Invalid;
812
813 // Try to keep parsing unless it doesn't look like an accessor spec.
814 if (!NextToken().is(K: tok::equal))
815 break;
816 }
817
818 // Consume the identifier.
819 ConsumeToken();
820
821 // Consume the '='.
822 if (!TryConsumeToken(Expected: tok::equal)) {
823 Diag(Loc: Tok.getLocation(), DiagID: diag::err_ms_property_expected_equal)
824 << KindStr;
825 break;
826 }
827
828 // Expect the method name.
829 if (!Tok.is(K: tok::identifier)) {
830 Diag(Loc: Tok.getLocation(), DiagID: diag::err_ms_property_expected_accessor_name);
831 break;
832 }
833
834 if (Kind == AK_Invalid) {
835 // Just drop invalid accessors.
836 } else if (AccessorNames[Kind] != nullptr) {
837 // Complain about the repeated accessor, ignore it, and keep parsing.
838 Diag(Loc: KindLoc, DiagID: diag::err_ms_property_duplicate_accessor) << KindStr;
839 } else {
840 AccessorNames[Kind] = Tok.getIdentifierInfo();
841 }
842 ConsumeToken();
843
844 next_property_accessor:
845 // Keep processing accessors until we run out.
846 if (TryConsumeToken(Expected: tok::comma))
847 continue;
848
849 // If we run into the ')', stop without consuming it.
850 if (Tok.is(K: tok::r_paren))
851 break;
852
853 Diag(Loc: Tok.getLocation(), DiagID: diag::err_ms_property_expected_comma_or_rparen);
854 break;
855 }
856
857 // Only add the property attribute if it was well-formed.
858 if (!HasInvalidAccessor)
859 Attrs.addNewPropertyAttr(attrName: AttrName, attrRange: AttrNameLoc, scope: AttributeScopeInfo(),
860 getterId: AccessorNames[AK_Get], setterId: AccessorNames[AK_Put],
861 formUsed: ParsedAttr::Form::Declspec());
862 T.skipToEnd();
863 return !HasInvalidAccessor;
864 }
865
866 unsigned NumArgs =
867 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc: nullptr, ScopeName: nullptr,
868 ScopeLoc: SourceLocation(), Form: ParsedAttr::Form::Declspec());
869
870 // If this attribute's args were parsed, and it was expected to have
871 // arguments but none were provided, emit a diagnostic.
872 if (ExistingAttrs < Attrs.size() && Attrs.back().getMaxArgs() && !NumArgs) {
873 Diag(Loc: OpenParenLoc, DiagID: diag::err_attribute_requires_arguments) << AttrName;
874 return false;
875 }
876 return true;
877}
878
879void Parser::ParseMicrosoftDeclSpecs(ParsedAttributes &Attrs) {
880 assert(getLangOpts().DeclSpecKeyword && "__declspec keyword is not enabled");
881 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
882
883 SourceLocation StartLoc = Tok.getLocation();
884 SourceLocation EndLoc = StartLoc;
885
886 while (Tok.is(K: tok::kw___declspec)) {
887 ConsumeToken();
888 BalancedDelimiterTracker T(*this, tok::l_paren);
889 if (T.expectAndConsume(DiagID: diag::err_expected_lparen_after, Msg: "__declspec",
890 SkipToTok: tok::r_paren))
891 return;
892
893 // An empty declspec is perfectly legal and should not warn. Additionally,
894 // you can specify multiple attributes per declspec.
895 while (Tok.isNot(K: tok::r_paren)) {
896 // Attribute not present.
897 if (TryConsumeToken(Expected: tok::comma))
898 continue;
899
900 if (Tok.is(K: tok::code_completion)) {
901 cutOffParsing();
902 Actions.CodeCompletion().CodeCompleteAttribute(
903 Syntax: AttributeCommonInfo::AS_Declspec);
904 return;
905 }
906
907 // We expect either a well-known identifier or a generic string. Anything
908 // else is a malformed declspec.
909 bool IsString = Tok.getKind() == tok::string_literal;
910 if (!IsString && Tok.getKind() != tok::identifier &&
911 Tok.getKind() != tok::kw_restrict) {
912 Diag(Tok, DiagID: diag::err_ms_declspec_type);
913 T.skipToEnd();
914 return;
915 }
916
917 IdentifierInfo *AttrName;
918 SourceLocation AttrNameLoc;
919 if (IsString) {
920 SmallString<8> StrBuffer;
921 bool Invalid = false;
922 StringRef Str = PP.getSpelling(Tok, Buffer&: StrBuffer, Invalid: &Invalid);
923 if (Invalid) {
924 T.skipToEnd();
925 return;
926 }
927 AttrName = PP.getIdentifierInfo(Name: Str);
928 AttrNameLoc = ConsumeStringToken();
929 } else {
930 AttrName = Tok.getIdentifierInfo();
931 AttrNameLoc = ConsumeToken();
932 }
933
934 bool AttrHandled = false;
935
936 // Parse attribute arguments.
937 if (Tok.is(K: tok::l_paren))
938 AttrHandled = ParseMicrosoftDeclSpecArgs(AttrName, AttrNameLoc, Attrs);
939 else if (AttrName->getName() == "property")
940 // The property attribute must have an argument list.
941 Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected_lparen_after)
942 << AttrName->getName();
943
944 if (!AttrHandled)
945 Attrs.addNew(attrName: AttrName, attrRange: AttrNameLoc, scope: AttributeScopeInfo(), args: nullptr, numArgs: 0,
946 form: ParsedAttr::Form::Declspec());
947 }
948 T.consumeClose();
949 EndLoc = T.getCloseLocation();
950 }
951
952 Attrs.Range = SourceRange(StartLoc, EndLoc);
953}
954
955void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
956 // Treat these like attributes
957 while (true) {
958 auto Kind = Tok.getKind();
959 switch (Kind) {
960 case tok::kw___fastcall:
961 case tok::kw___stdcall:
962 case tok::kw___thiscall:
963 case tok::kw___regcall:
964 case tok::kw___cdecl:
965 case tok::kw___vectorcall:
966 case tok::kw___ptr64:
967 case tok::kw___w64:
968 case tok::kw___ptr32:
969 case tok::kw___sptr:
970 case tok::kw___uptr: {
971 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
972 SourceLocation AttrNameLoc = ConsumeToken();
973 attrs.addNew(attrName: AttrName, attrRange: AttrNameLoc, scope: AttributeScopeInfo(), args: nullptr, numArgs: 0,
974 form: Kind);
975 break;
976 }
977 default:
978 return;
979 }
980 }
981}
982
983void Parser::ParseWebAssemblyFuncrefTypeAttribute(ParsedAttributes &attrs) {
984 assert(Tok.is(tok::kw___funcref));
985 SourceLocation StartLoc = Tok.getLocation();
986 if (!getTargetInfo().getTriple().isWasm()) {
987 ConsumeToken();
988 Diag(Loc: StartLoc, DiagID: diag::err_wasm_funcref_not_wasm);
989 return;
990 }
991
992 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
993 SourceLocation AttrNameLoc = ConsumeToken();
994 attrs.addNew(attrName: AttrName, attrRange: AttrNameLoc, scope: AttributeScopeInfo(), /*Args=*/args: nullptr,
995 /*numArgs=*/0, form: tok::kw___funcref);
996}
997
998void Parser::DiagnoseAndSkipExtendedMicrosoftTypeAttributes() {
999 SourceLocation StartLoc = Tok.getLocation();
1000 SourceLocation EndLoc = SkipExtendedMicrosoftTypeAttributes();
1001
1002 if (EndLoc.isValid()) {
1003 SourceRange Range(StartLoc, EndLoc);
1004 Diag(Loc: StartLoc, DiagID: diag::warn_microsoft_qualifiers_ignored) << Range;
1005 }
1006}
1007
1008SourceLocation Parser::SkipExtendedMicrosoftTypeAttributes() {
1009 SourceLocation EndLoc;
1010
1011 while (true) {
1012 switch (Tok.getKind()) {
1013 case tok::kw_const:
1014 case tok::kw_volatile:
1015 case tok::kw___fastcall:
1016 case tok::kw___stdcall:
1017 case tok::kw___thiscall:
1018 case tok::kw___cdecl:
1019 case tok::kw___vectorcall:
1020 case tok::kw___ptr32:
1021 case tok::kw___ptr64:
1022 case tok::kw___w64:
1023 case tok::kw___unaligned:
1024 case tok::kw___sptr:
1025 case tok::kw___uptr:
1026 EndLoc = ConsumeToken();
1027 break;
1028 default:
1029 return EndLoc;
1030 }
1031 }
1032}
1033
1034void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
1035 // Treat these like attributes
1036 while (Tok.is(K: tok::kw___pascal)) {
1037 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1038 SourceLocation AttrNameLoc = ConsumeToken();
1039 attrs.addNew(attrName: AttrName, attrRange: AttrNameLoc, scope: AttributeScopeInfo(), args: nullptr, numArgs: 0,
1040 form: tok::kw___pascal);
1041 }
1042}
1043
1044void Parser::ParseOpenCLKernelAttributes(ParsedAttributes &attrs) {
1045 // Treat these like attributes
1046 while (Tok.is(K: tok::kw___kernel)) {
1047 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1048 SourceLocation AttrNameLoc = ConsumeToken();
1049 attrs.addNew(attrName: AttrName, attrRange: AttrNameLoc, scope: AttributeScopeInfo(), args: nullptr, numArgs: 0,
1050 form: tok::kw___kernel);
1051 }
1052}
1053
1054void Parser::ParseCUDAFunctionAttributes(ParsedAttributes &attrs) {
1055 while (Tok.is(K: tok::kw___noinline__)) {
1056 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1057 SourceLocation AttrNameLoc = ConsumeToken();
1058 attrs.addNew(attrName: AttrName, attrRange: AttrNameLoc, scope: AttributeScopeInfo(), args: nullptr, numArgs: 0,
1059 form: tok::kw___noinline__);
1060 }
1061}
1062
1063void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) {
1064 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1065 SourceLocation AttrNameLoc = Tok.getLocation();
1066 Attrs.addNew(attrName: AttrName, attrRange: AttrNameLoc, scope: AttributeScopeInfo(), args: nullptr, numArgs: 0,
1067 form: Tok.getKind());
1068}
1069
1070bool Parser::isHLSLQualifier(const Token &Tok) const {
1071 return Tok.is(K: tok::kw_groupshared);
1072}
1073
1074void Parser::ParseHLSLQualifiers(ParsedAttributes &Attrs) {
1075 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1076 auto Kind = Tok.getKind();
1077 SourceLocation AttrNameLoc = ConsumeToken();
1078 Attrs.addNew(attrName: AttrName, attrRange: AttrNameLoc, scope: AttributeScopeInfo(), args: nullptr, numArgs: 0, form: Kind);
1079}
1080
1081void Parser::ParseNullabilityTypeSpecifiers(ParsedAttributes &attrs) {
1082 // Treat these like attributes, even though they're type specifiers.
1083 while (true) {
1084 auto Kind = Tok.getKind();
1085 switch (Kind) {
1086 case tok::kw__Nonnull:
1087 case tok::kw__Nullable:
1088 case tok::kw__Nullable_result:
1089 case tok::kw__Null_unspecified: {
1090 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1091 SourceLocation AttrNameLoc = ConsumeToken();
1092 if (!getLangOpts().ObjC)
1093 Diag(Loc: AttrNameLoc, DiagID: diag::ext_nullability)
1094 << AttrName;
1095 attrs.addNew(attrName: AttrName, attrRange: AttrNameLoc, scope: AttributeScopeInfo(), args: nullptr, numArgs: 0,
1096 form: Kind);
1097 break;
1098 }
1099 default:
1100 return;
1101 }
1102 }
1103}
1104
1105static bool VersionNumberSeparator(const char Separator) {
1106 return (Separator == '.' || Separator == '_');
1107}
1108
1109VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
1110 Range = SourceRange(Tok.getLocation(), Tok.getEndLoc());
1111
1112 if (!Tok.is(K: tok::numeric_constant)) {
1113 Diag(Tok, DiagID: diag::err_expected_version);
1114 SkipUntil(T1: tok::comma, T2: tok::r_paren,
1115 Flags: StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
1116 return VersionTuple();
1117 }
1118
1119 // Parse the major (and possibly minor and subminor) versions, which
1120 // are stored in the numeric constant. We utilize a quirk of the
1121 // lexer, which is that it handles something like 1.2.3 as a single
1122 // numeric constant, rather than two separate tokens.
1123 SmallString<512> Buffer;
1124 Buffer.resize(N: Tok.getLength()+1);
1125 const char *ThisTokBegin = &Buffer[0];
1126
1127 // Get the spelling of the token, which eliminates trigraphs, etc.
1128 bool Invalid = false;
1129 unsigned ActualLength = PP.getSpelling(Tok, Buffer&: ThisTokBegin, Invalid: &Invalid);
1130 if (Invalid)
1131 return VersionTuple();
1132
1133 // Parse the major version.
1134 unsigned AfterMajor = 0;
1135 unsigned Major = 0;
1136 while (AfterMajor < ActualLength && isDigit(c: ThisTokBegin[AfterMajor])) {
1137 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
1138 ++AfterMajor;
1139 }
1140
1141 if (AfterMajor == 0) {
1142 Diag(Tok, DiagID: diag::err_expected_version);
1143 SkipUntil(T1: tok::comma, T2: tok::r_paren,
1144 Flags: StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
1145 return VersionTuple();
1146 }
1147
1148 if (AfterMajor == ActualLength) {
1149 ConsumeToken();
1150
1151 // We only had a single version component.
1152 if (Major == 0) {
1153 Diag(Tok, DiagID: diag::err_zero_version);
1154 return VersionTuple();
1155 }
1156
1157 return VersionTuple(Major);
1158 }
1159
1160 const char AfterMajorSeparator = ThisTokBegin[AfterMajor];
1161 if (!VersionNumberSeparator(Separator: AfterMajorSeparator)
1162 || (AfterMajor + 1 == ActualLength)) {
1163 Diag(Tok, DiagID: diag::err_expected_version);
1164 SkipUntil(T1: tok::comma, T2: tok::r_paren,
1165 Flags: StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
1166 return VersionTuple();
1167 }
1168
1169 // Parse the minor version.
1170 unsigned AfterMinor = AfterMajor + 1;
1171 unsigned Minor = 0;
1172 while (AfterMinor < ActualLength && isDigit(c: ThisTokBegin[AfterMinor])) {
1173 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
1174 ++AfterMinor;
1175 }
1176
1177 if (AfterMinor == ActualLength) {
1178 ConsumeToken();
1179
1180 // We had major.minor.
1181 if (Major == 0 && Minor == 0) {
1182 Diag(Tok, DiagID: diag::err_zero_version);
1183 return VersionTuple();
1184 }
1185
1186 return VersionTuple(Major, Minor);
1187 }
1188
1189 const char AfterMinorSeparator = ThisTokBegin[AfterMinor];
1190 // If what follows is not a '.' or '_', we have a problem.
1191 if (!VersionNumberSeparator(Separator: AfterMinorSeparator)) {
1192 Diag(Tok, DiagID: diag::err_expected_version);
1193 SkipUntil(T1: tok::comma, T2: tok::r_paren,
1194 Flags: StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
1195 return VersionTuple();
1196 }
1197
1198 // Warn if separators, be it '.' or '_', do not match.
1199 if (AfterMajorSeparator != AfterMinorSeparator)
1200 Diag(Tok, DiagID: diag::warn_expected_consistent_version_separator);
1201
1202 // Parse the subminor version.
1203 unsigned AfterSubminor = AfterMinor + 1;
1204 unsigned Subminor = 0;
1205 while (AfterSubminor < ActualLength && isDigit(c: ThisTokBegin[AfterSubminor])) {
1206 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
1207 ++AfterSubminor;
1208 }
1209
1210 if (AfterSubminor != ActualLength) {
1211 Diag(Tok, DiagID: diag::err_expected_version);
1212 SkipUntil(T1: tok::comma, T2: tok::r_paren,
1213 Flags: StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
1214 return VersionTuple();
1215 }
1216 ConsumeToken();
1217 return VersionTuple(Major, Minor, Subminor);
1218}
1219
1220void Parser::ParseAvailabilityAttribute(
1221 IdentifierInfo &Availability, SourceLocation AvailabilityLoc,
1222 ParsedAttributes &attrs, SourceLocation *endLoc, IdentifierInfo *ScopeName,
1223 SourceLocation ScopeLoc, ParsedAttr::Form Form) {
1224 enum { Introduced, Deprecated, Obsoleted, Unknown };
1225 AvailabilityChange Changes[Unknown];
1226 ExprResult MessageExpr, ReplacementExpr;
1227 IdentifierLoc *EnvironmentLoc = nullptr;
1228
1229 // Opening '('.
1230 BalancedDelimiterTracker T(*this, tok::l_paren);
1231 if (T.consumeOpen()) {
1232 Diag(Tok, DiagID: diag::err_expected) << tok::l_paren;
1233 return;
1234 }
1235
1236 // Parse the platform name.
1237 if (Tok.isNot(K: tok::identifier)) {
1238 Diag(Tok, DiagID: diag::err_availability_expected_platform);
1239 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
1240 return;
1241 }
1242 IdentifierLoc *Platform = ParseIdentifierLoc();
1243 if (const IdentifierInfo *const Ident = Platform->getIdentifierInfo()) {
1244 // Disallow xrOS for availability attributes.
1245 if (Ident->getName().contains(Other: "xrOS") || Ident->getName().contains(Other: "xros"))
1246 Diag(Loc: Platform->getLoc(), DiagID: diag::warn_availability_unknown_platform)
1247 << Ident;
1248 // Canonicalize platform name from "macosx" to "macos".
1249 else if (Ident->getName() == "macosx")
1250 Platform->setIdentifierInfo(PP.getIdentifierInfo(Name: "macos"));
1251 // Canonicalize platform name from "macosx_app_extension" to
1252 // "macos_app_extension".
1253 else if (Ident->getName() == "macosx_app_extension")
1254 Platform->setIdentifierInfo(PP.getIdentifierInfo(Name: "macos_app_extension"));
1255 else
1256 Platform->setIdentifierInfo(PP.getIdentifierInfo(
1257 Name: AvailabilityAttr::canonicalizePlatformName(Platform: Ident->getName())));
1258 }
1259
1260 // Parse the ',' following the platform name.
1261 if (ExpectAndConsume(ExpectedTok: tok::comma)) {
1262 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
1263 return;
1264 }
1265
1266 // If we haven't grabbed the pointers for the identifiers
1267 // "introduced", "deprecated", and "obsoleted", do so now.
1268 if (!Ident_introduced) {
1269 Ident_introduced = PP.getIdentifierInfo(Name: "introduced");
1270 Ident_deprecated = PP.getIdentifierInfo(Name: "deprecated");
1271 Ident_obsoleted = PP.getIdentifierInfo(Name: "obsoleted");
1272 Ident_unavailable = PP.getIdentifierInfo(Name: "unavailable");
1273 Ident_message = PP.getIdentifierInfo(Name: "message");
1274 Ident_strict = PP.getIdentifierInfo(Name: "strict");
1275 Ident_replacement = PP.getIdentifierInfo(Name: "replacement");
1276 Ident_environment = PP.getIdentifierInfo(Name: "environment");
1277 }
1278
1279 // Parse the optional "strict", the optional "replacement" and the set of
1280 // introductions/deprecations/removals.
1281 SourceLocation UnavailableLoc, StrictLoc;
1282 do {
1283 if (Tok.isNot(K: tok::identifier)) {
1284 Diag(Tok, DiagID: diag::err_availability_expected_change);
1285 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
1286 return;
1287 }
1288 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
1289 SourceLocation KeywordLoc = ConsumeToken();
1290
1291 if (Keyword == Ident_strict) {
1292 if (StrictLoc.isValid()) {
1293 Diag(Loc: KeywordLoc, DiagID: diag::err_availability_redundant)
1294 << Keyword << SourceRange(StrictLoc);
1295 }
1296 StrictLoc = KeywordLoc;
1297 continue;
1298 }
1299
1300 if (Keyword == Ident_unavailable) {
1301 if (UnavailableLoc.isValid()) {
1302 Diag(Loc: KeywordLoc, DiagID: diag::err_availability_redundant)
1303 << Keyword << SourceRange(UnavailableLoc);
1304 }
1305 UnavailableLoc = KeywordLoc;
1306 continue;
1307 }
1308
1309 if (Keyword == Ident_deprecated && Platform->getIdentifierInfo() &&
1310 Platform->getIdentifierInfo()->isStr(Str: "swift")) {
1311 // For swift, we deprecate for all versions.
1312 if (Changes[Deprecated].KeywordLoc.isValid()) {
1313 Diag(Loc: KeywordLoc, DiagID: diag::err_availability_redundant)
1314 << Keyword
1315 << SourceRange(Changes[Deprecated].KeywordLoc);
1316 }
1317
1318 Changes[Deprecated].KeywordLoc = KeywordLoc;
1319 // Use a fake version here.
1320 Changes[Deprecated].Version = VersionTuple(1);
1321 continue;
1322 }
1323
1324 if (Keyword == Ident_environment) {
1325 if (EnvironmentLoc != nullptr) {
1326 Diag(Loc: KeywordLoc, DiagID: diag::err_availability_redundant)
1327 << Keyword << SourceRange(EnvironmentLoc->getLoc());
1328 }
1329 }
1330
1331 if (Tok.isNot(K: tok::equal)) {
1332 Diag(Tok, DiagID: diag::err_expected_after) << Keyword << tok::equal;
1333 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
1334 return;
1335 }
1336 ConsumeToken();
1337 if (Keyword == Ident_message || Keyword == Ident_replacement) {
1338 if (!isTokenStringLiteral()) {
1339 Diag(Tok, DiagID: diag::err_expected_string_literal)
1340 << /*Source='availability attribute'*/2;
1341 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
1342 return;
1343 }
1344 if (Keyword == Ident_message) {
1345 MessageExpr = ParseUnevaluatedStringLiteralExpression();
1346 break;
1347 } else {
1348 ReplacementExpr = ParseUnevaluatedStringLiteralExpression();
1349 continue;
1350 }
1351 }
1352 if (Keyword == Ident_environment) {
1353 if (Tok.isNot(K: tok::identifier)) {
1354 Diag(Tok, DiagID: diag::err_availability_expected_environment);
1355 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
1356 return;
1357 }
1358 EnvironmentLoc = ParseIdentifierLoc();
1359 continue;
1360 }
1361
1362 // Special handling of 'NA' only when applied to introduced or
1363 // deprecated.
1364 if ((Keyword == Ident_introduced || Keyword == Ident_deprecated) &&
1365 Tok.is(K: tok::identifier)) {
1366 IdentifierInfo *NA = Tok.getIdentifierInfo();
1367 if (NA->getName() == "NA") {
1368 ConsumeToken();
1369 if (Keyword == Ident_introduced)
1370 UnavailableLoc = KeywordLoc;
1371 continue;
1372 }
1373 }
1374
1375 SourceRange VersionRange;
1376 VersionTuple Version = ParseVersionTuple(Range&: VersionRange);
1377
1378 if (Version.empty()) {
1379 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
1380 return;
1381 }
1382
1383 unsigned Index;
1384 if (Keyword == Ident_introduced)
1385 Index = Introduced;
1386 else if (Keyword == Ident_deprecated)
1387 Index = Deprecated;
1388 else if (Keyword == Ident_obsoleted)
1389 Index = Obsoleted;
1390 else
1391 Index = Unknown;
1392
1393 if (Index < Unknown) {
1394 if (!Changes[Index].KeywordLoc.isInvalid()) {
1395 Diag(Loc: KeywordLoc, DiagID: diag::err_availability_redundant)
1396 << Keyword
1397 << SourceRange(Changes[Index].KeywordLoc,
1398 Changes[Index].VersionRange.getEnd());
1399 }
1400
1401 Changes[Index].KeywordLoc = KeywordLoc;
1402 Changes[Index].Version = Version;
1403 Changes[Index].VersionRange = VersionRange;
1404 } else {
1405 Diag(Loc: KeywordLoc, DiagID: diag::err_availability_unknown_change)
1406 << Keyword << VersionRange;
1407 }
1408
1409 } while (TryConsumeToken(Expected: tok::comma));
1410
1411 // Closing ')'.
1412 if (T.consumeClose())
1413 return;
1414
1415 if (endLoc)
1416 *endLoc = T.getCloseLocation();
1417
1418 // The 'unavailable' availability cannot be combined with any other
1419 // availability changes. Make sure that hasn't happened.
1420 if (UnavailableLoc.isValid()) {
1421 bool Complained = false;
1422 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
1423 if (Changes[Index].KeywordLoc.isValid()) {
1424 if (!Complained) {
1425 Diag(Loc: UnavailableLoc, DiagID: diag::warn_availability_and_unavailable)
1426 << SourceRange(Changes[Index].KeywordLoc,
1427 Changes[Index].VersionRange.getEnd());
1428 Complained = true;
1429 }
1430
1431 // Clear out the availability.
1432 Changes[Index] = AvailabilityChange();
1433 }
1434 }
1435 }
1436
1437 // Record this attribute
1438 attrs.addNew(attrName: &Availability,
1439 attrRange: SourceRange(AvailabilityLoc, T.getCloseLocation()),
1440 scope: AttributeScopeInfo(ScopeName, ScopeLoc), Param: Platform,
1441 introduced: Changes[Introduced], deprecated: Changes[Deprecated], obsoleted: Changes[Obsoleted],
1442 unavailable: UnavailableLoc, MessageExpr: MessageExpr.get(), form: Form, strict: StrictLoc,
1443 ReplacementExpr: ReplacementExpr.get(), EnvironmentLoc);
1444}
1445
1446void Parser::ParseExternalSourceSymbolAttribute(
1447 IdentifierInfo &ExternalSourceSymbol, SourceLocation Loc,
1448 ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
1449 SourceLocation ScopeLoc, ParsedAttr::Form Form) {
1450 // Opening '('.
1451 BalancedDelimiterTracker T(*this, tok::l_paren);
1452 if (T.expectAndConsume())
1453 return;
1454
1455 // Initialize the pointers for the keyword identifiers when required.
1456 if (!Ident_language) {
1457 Ident_language = PP.getIdentifierInfo(Name: "language");
1458 Ident_defined_in = PP.getIdentifierInfo(Name: "defined_in");
1459 Ident_generated_declaration = PP.getIdentifierInfo(Name: "generated_declaration");
1460 Ident_USR = PP.getIdentifierInfo(Name: "USR");
1461 }
1462
1463 ExprResult Language;
1464 bool HasLanguage = false;
1465 ExprResult DefinedInExpr;
1466 bool HasDefinedIn = false;
1467 IdentifierLoc *GeneratedDeclaration = nullptr;
1468 ExprResult USR;
1469 bool HasUSR = false;
1470
1471 // Parse the language/defined_in/generated_declaration keywords
1472 do {
1473 if (Tok.isNot(K: tok::identifier)) {
1474 Diag(Tok, DiagID: diag::err_external_source_symbol_expected_keyword);
1475 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
1476 return;
1477 }
1478
1479 SourceLocation KeywordLoc = Tok.getLocation();
1480 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
1481 if (Keyword == Ident_generated_declaration) {
1482 if (GeneratedDeclaration) {
1483 Diag(Tok, DiagID: diag::err_external_source_symbol_duplicate_clause) << Keyword;
1484 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
1485 return;
1486 }
1487 GeneratedDeclaration = ParseIdentifierLoc();
1488 continue;
1489 }
1490
1491 if (Keyword != Ident_language && Keyword != Ident_defined_in &&
1492 Keyword != Ident_USR) {
1493 Diag(Tok, DiagID: diag::err_external_source_symbol_expected_keyword);
1494 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
1495 return;
1496 }
1497
1498 ConsumeToken();
1499 if (ExpectAndConsume(ExpectedTok: tok::equal, Diag: diag::err_expected_after,
1500 DiagMsg: Keyword->getName())) {
1501 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
1502 return;
1503 }
1504
1505 bool HadLanguage = HasLanguage, HadDefinedIn = HasDefinedIn,
1506 HadUSR = HasUSR;
1507 if (Keyword == Ident_language)
1508 HasLanguage = true;
1509 else if (Keyword == Ident_USR)
1510 HasUSR = true;
1511 else
1512 HasDefinedIn = true;
1513
1514 if (!isTokenStringLiteral()) {
1515 Diag(Tok, DiagID: diag::err_expected_string_literal)
1516 << /*Source='external_source_symbol attribute'*/ 3
1517 << /*language | source container | USR*/ (
1518 Keyword == Ident_language
1519 ? 0
1520 : (Keyword == Ident_defined_in ? 1 : 2));
1521 SkipUntil(T1: tok::comma, T2: tok::r_paren, Flags: StopAtSemi | StopBeforeMatch);
1522 continue;
1523 }
1524 if (Keyword == Ident_language) {
1525 if (HadLanguage) {
1526 Diag(Loc: KeywordLoc, DiagID: diag::err_external_source_symbol_duplicate_clause)
1527 << Keyword;
1528 ParseUnevaluatedStringLiteralExpression();
1529 continue;
1530 }
1531 Language = ParseUnevaluatedStringLiteralExpression();
1532 } else if (Keyword == Ident_USR) {
1533 if (HadUSR) {
1534 Diag(Loc: KeywordLoc, DiagID: diag::err_external_source_symbol_duplicate_clause)
1535 << Keyword;
1536 ParseUnevaluatedStringLiteralExpression();
1537 continue;
1538 }
1539 USR = ParseUnevaluatedStringLiteralExpression();
1540 } else {
1541 assert(Keyword == Ident_defined_in && "Invalid clause keyword!");
1542 if (HadDefinedIn) {
1543 Diag(Loc: KeywordLoc, DiagID: diag::err_external_source_symbol_duplicate_clause)
1544 << Keyword;
1545 ParseUnevaluatedStringLiteralExpression();
1546 continue;
1547 }
1548 DefinedInExpr = ParseUnevaluatedStringLiteralExpression();
1549 }
1550 } while (TryConsumeToken(Expected: tok::comma));
1551
1552 // Closing ')'.
1553 if (T.consumeClose())
1554 return;
1555 if (EndLoc)
1556 *EndLoc = T.getCloseLocation();
1557
1558 ArgsUnion Args[] = {Language.get(), DefinedInExpr.get(), GeneratedDeclaration,
1559 USR.get()};
1560 Attrs.addNew(attrName: &ExternalSourceSymbol, attrRange: SourceRange(Loc, T.getCloseLocation()),
1561 scope: AttributeScopeInfo(ScopeName, ScopeLoc), args: Args, numArgs: std::size(Args),
1562 form: Form);
1563}
1564
1565void Parser::ParseObjCBridgeRelatedAttribute(
1566 IdentifierInfo &ObjCBridgeRelated, SourceLocation ObjCBridgeRelatedLoc,
1567 ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
1568 SourceLocation ScopeLoc, ParsedAttr::Form Form) {
1569 // Opening '('.
1570 BalancedDelimiterTracker T(*this, tok::l_paren);
1571 if (T.consumeOpen()) {
1572 Diag(Tok, DiagID: diag::err_expected) << tok::l_paren;
1573 return;
1574 }
1575
1576 // Parse the related class name.
1577 if (Tok.isNot(K: tok::identifier)) {
1578 Diag(Tok, DiagID: diag::err_objcbridge_related_expected_related_class);
1579 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
1580 return;
1581 }
1582 IdentifierLoc *RelatedClass = ParseIdentifierLoc();
1583 if (ExpectAndConsume(ExpectedTok: tok::comma)) {
1584 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
1585 return;
1586 }
1587
1588 // Parse class method name. It's non-optional in the sense that a trailing
1589 // comma is required, but it can be the empty string, and then we record a
1590 // nullptr.
1591 IdentifierLoc *ClassMethod = nullptr;
1592 if (Tok.is(K: tok::identifier)) {
1593 ClassMethod = ParseIdentifierLoc();
1594 if (!TryConsumeToken(Expected: tok::colon)) {
1595 Diag(Tok, DiagID: diag::err_objcbridge_related_selector_name);
1596 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
1597 return;
1598 }
1599 }
1600 if (!TryConsumeToken(Expected: tok::comma)) {
1601 if (Tok.is(K: tok::colon))
1602 Diag(Tok, DiagID: diag::err_objcbridge_related_selector_name);
1603 else
1604 Diag(Tok, DiagID: diag::err_expected) << tok::comma;
1605 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
1606 return;
1607 }
1608
1609 // Parse instance method name. Also non-optional but empty string is
1610 // permitted.
1611 IdentifierLoc *InstanceMethod = nullptr;
1612 if (Tok.is(K: tok::identifier))
1613 InstanceMethod = ParseIdentifierLoc();
1614 else if (Tok.isNot(K: tok::r_paren)) {
1615 Diag(Tok, DiagID: diag::err_expected) << tok::r_paren;
1616 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
1617 return;
1618 }
1619
1620 // Closing ')'.
1621 if (T.consumeClose())
1622 return;
1623
1624 if (EndLoc)
1625 *EndLoc = T.getCloseLocation();
1626
1627 // Record this attribute
1628 Attrs.addNew(attrName: &ObjCBridgeRelated,
1629 attrRange: SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()),
1630 scope: AttributeScopeInfo(ScopeName, ScopeLoc), Param1: RelatedClass,
1631 Param2: ClassMethod, Param3: InstanceMethod, form: Form);
1632}
1633
1634void Parser::ParseSwiftNewTypeAttribute(
1635 IdentifierInfo &AttrName, SourceLocation AttrNameLoc,
1636 ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
1637 SourceLocation ScopeLoc, ParsedAttr::Form Form) {
1638 BalancedDelimiterTracker T(*this, tok::l_paren);
1639
1640 // Opening '('
1641 if (T.consumeOpen()) {
1642 Diag(Tok, DiagID: diag::err_expected) << tok::l_paren;
1643 return;
1644 }
1645
1646 if (Tok.is(K: tok::r_paren)) {
1647 Diag(Loc: Tok.getLocation(), DiagID: diag::err_argument_required_after_attribute);
1648 T.consumeClose();
1649 return;
1650 }
1651 if (Tok.isNot(K: tok::kw_struct) && Tok.isNot(K: tok::kw_enum)) {
1652 Diag(Tok, DiagID: diag::warn_attribute_type_not_supported)
1653 << &AttrName << Tok.getIdentifierInfo();
1654 if (!isTokenSpecial())
1655 ConsumeToken();
1656 T.consumeClose();
1657 return;
1658 }
1659
1660 auto *SwiftType = new (Actions.Context)
1661 IdentifierLoc(Tok.getLocation(), Tok.getIdentifierInfo());
1662 ConsumeToken();
1663
1664 // Closing ')'
1665 if (T.consumeClose())
1666 return;
1667 if (EndLoc)
1668 *EndLoc = T.getCloseLocation();
1669
1670 ArgsUnion Args[] = {SwiftType};
1671 Attrs.addNew(attrName: &AttrName, attrRange: SourceRange(AttrNameLoc, T.getCloseLocation()),
1672 scope: AttributeScopeInfo(ScopeName, ScopeLoc), args: Args, numArgs: std::size(Args),
1673 form: Form);
1674}
1675
1676void Parser::ParseTypeTagForDatatypeAttribute(
1677 IdentifierInfo &AttrName, SourceLocation AttrNameLoc,
1678 ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
1679 SourceLocation ScopeLoc, ParsedAttr::Form Form) {
1680 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1681
1682 BalancedDelimiterTracker T(*this, tok::l_paren);
1683 T.consumeOpen();
1684
1685 if (Tok.isNot(K: tok::identifier)) {
1686 Diag(Tok, DiagID: diag::err_expected) << tok::identifier;
1687 T.skipToEnd();
1688 return;
1689 }
1690 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
1691
1692 if (ExpectAndConsume(ExpectedTok: tok::comma)) {
1693 T.skipToEnd();
1694 return;
1695 }
1696
1697 SourceRange MatchingCTypeRange;
1698 TypeResult MatchingCType = ParseTypeName(Range: &MatchingCTypeRange);
1699 if (MatchingCType.isInvalid()) {
1700 T.skipToEnd();
1701 return;
1702 }
1703
1704 bool LayoutCompatible = false;
1705 bool MustBeNull = false;
1706 while (TryConsumeToken(Expected: tok::comma)) {
1707 if (Tok.isNot(K: tok::identifier)) {
1708 Diag(Tok, DiagID: diag::err_expected) << tok::identifier;
1709 T.skipToEnd();
1710 return;
1711 }
1712 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1713 if (Flag->isStr(Str: "layout_compatible"))
1714 LayoutCompatible = true;
1715 else if (Flag->isStr(Str: "must_be_null"))
1716 MustBeNull = true;
1717 else {
1718 Diag(Tok, DiagID: diag::err_type_safety_unknown_flag) << Flag;
1719 T.skipToEnd();
1720 return;
1721 }
1722 ConsumeToken(); // consume flag
1723 }
1724
1725 if (!T.consumeClose()) {
1726 Attrs.addNewTypeTagForDatatype(
1727 attrName: &AttrName, attrRange: AttrNameLoc, scope: AttributeScopeInfo(ScopeName, ScopeLoc),
1728 argumentKind: ArgumentKind, matchingCType: MatchingCType.get(), layoutCompatible: LayoutCompatible, mustBeNull: MustBeNull, form: Form);
1729 }
1730
1731 if (EndLoc)
1732 *EndLoc = T.getCloseLocation();
1733}
1734
1735bool Parser::DiagnoseProhibitedCXX11Attribute() {
1736 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1737
1738 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1739 case CXX11AttributeKind::NotAttributeSpecifier:
1740 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1741 return false;
1742
1743 case CXX11AttributeKind::InvalidAttributeSpecifier:
1744 Diag(Loc: Tok.getLocation(), DiagID: diag::err_l_square_l_square_not_attribute);
1745 return false;
1746
1747 case CXX11AttributeKind::AttributeSpecifier:
1748 // Parse and discard the attributes.
1749 SourceLocation BeginLoc = ConsumeBracket();
1750 ConsumeBracket();
1751 SkipUntil(T: tok::r_square);
1752 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1753 SourceLocation EndLoc = ConsumeBracket();
1754 Diag(Loc: BeginLoc, DiagID: diag::err_attributes_not_allowed)
1755 << SourceRange(BeginLoc, EndLoc);
1756 return true;
1757 }
1758 llvm_unreachable("All cases handled above.");
1759}
1760
1761void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributes &Attrs,
1762 SourceLocation CorrectLocation) {
1763 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1764 Tok.is(tok::kw_alignas) || Tok.isRegularKeywordAttribute());
1765
1766 // Consume the attributes.
1767 auto Keyword =
1768 Tok.isRegularKeywordAttribute() ? Tok.getIdentifierInfo() : nullptr;
1769 SourceLocation Loc = Tok.getLocation();
1770 ParseCXX11Attributes(attrs&: Attrs);
1771 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1772 // FIXME: use err_attributes_misplaced
1773 (Keyword ? Diag(Loc, DiagID: diag::err_keyword_not_allowed) << Keyword
1774 : Diag(Loc, DiagID: diag::err_attributes_not_allowed))
1775 << FixItHint::CreateInsertionFromRange(InsertionLoc: CorrectLocation, FromRange: AttrRange)
1776 << FixItHint::CreateRemoval(RemoveRange: AttrRange);
1777}
1778
1779void Parser::DiagnoseProhibitedAttributes(
1780 const ParsedAttributesView &Attrs, const SourceLocation CorrectLocation) {
1781 auto *FirstAttr = Attrs.empty() ? nullptr : &Attrs.front();
1782 if (CorrectLocation.isValid()) {
1783 CharSourceRange AttrRange(Attrs.Range, true);
1784 (FirstAttr && FirstAttr->isRegularKeywordAttribute()
1785 ? Diag(Loc: CorrectLocation, DiagID: diag::err_keyword_misplaced) << FirstAttr
1786 : Diag(Loc: CorrectLocation, DiagID: diag::err_attributes_misplaced))
1787 << FixItHint::CreateInsertionFromRange(InsertionLoc: CorrectLocation, FromRange: AttrRange)
1788 << FixItHint::CreateRemoval(RemoveRange: AttrRange);
1789 } else {
1790 const SourceRange &Range = Attrs.Range;
1791 (FirstAttr && FirstAttr->isRegularKeywordAttribute()
1792 ? Diag(Loc: Range.getBegin(), DiagID: diag::err_keyword_not_allowed) << FirstAttr
1793 : Diag(Loc: Range.getBegin(), DiagID: diag::err_attributes_not_allowed))
1794 << Range;
1795 }
1796}
1797
1798void Parser::ProhibitCXX11Attributes(ParsedAttributes &Attrs,
1799 unsigned AttrDiagID,
1800 unsigned KeywordDiagID,
1801 bool DiagnoseEmptyAttrs,
1802 bool WarnOnUnknownAttrs) {
1803
1804 if (DiagnoseEmptyAttrs && Attrs.empty() && Attrs.Range.isValid()) {
1805 // An attribute list has been parsed, but it was empty.
1806 // This is the case for [[]].
1807 const auto &LangOpts = getLangOpts();
1808 auto &SM = PP.getSourceManager();
1809 Token FirstLSquare;
1810 Lexer::getRawToken(Loc: Attrs.Range.getBegin(), Result&: FirstLSquare, SM, LangOpts);
1811
1812 if (FirstLSquare.is(K: tok::l_square)) {
1813 std::optional<Token> SecondLSquare =
1814 Lexer::findNextToken(Loc: FirstLSquare.getLocation(), SM, LangOpts);
1815
1816 if (SecondLSquare && SecondLSquare->is(K: tok::l_square)) {
1817 // The attribute range starts with [[, but is empty. So this must
1818 // be [[]], which we are supposed to diagnose because
1819 // DiagnoseEmptyAttrs is true.
1820 Diag(Loc: Attrs.Range.getBegin(), DiagID: AttrDiagID) << Attrs.Range;
1821 return;
1822 }
1823 }
1824 }
1825
1826 for (const ParsedAttr &AL : Attrs) {
1827 if (AL.isRegularKeywordAttribute()) {
1828 Diag(Loc: AL.getLoc(), DiagID: KeywordDiagID) << AL;
1829 AL.setInvalid();
1830 continue;
1831 }
1832 if (!AL.isStandardAttributeSyntax())
1833 continue;
1834 if (AL.getKind() == ParsedAttr::UnknownAttribute) {
1835 if (WarnOnUnknownAttrs) {
1836 Actions.DiagnoseUnknownAttribute(AL);
1837 AL.setInvalid();
1838 }
1839 } else {
1840 Diag(Loc: AL.getLoc(), DiagID: AttrDiagID) << AL;
1841 AL.setInvalid();
1842 }
1843 }
1844}
1845
1846void Parser::DiagnoseCXX11AttributeExtension(ParsedAttributes &Attrs) {
1847 for (const ParsedAttr &PA : Attrs) {
1848 if (PA.isStandardAttributeSyntax() || PA.isRegularKeywordAttribute())
1849 Diag(Loc: PA.getLoc(), DiagID: diag::ext_cxx11_attr_placement)
1850 << PA << PA.isRegularKeywordAttribute() << PA.getRange();
1851 }
1852}
1853
1854void Parser::stripTypeAttributesOffDeclSpec(ParsedAttributes &Attrs,
1855 DeclSpec &DS, TagUseKind TUK) {
1856 if (TUK == TagUseKind::Reference)
1857 return;
1858
1859 llvm::SmallVector<ParsedAttr *, 1> ToBeMoved;
1860
1861 for (ParsedAttr &AL : DS.getAttributes()) {
1862 if ((AL.getKind() == ParsedAttr::AT_Aligned &&
1863 AL.isDeclspecAttribute()) ||
1864 AL.isMicrosoftAttribute())
1865 ToBeMoved.push_back(Elt: &AL);
1866 }
1867
1868 for (ParsedAttr *AL : ToBeMoved) {
1869 DS.getAttributes().remove(ToBeRemoved: AL);
1870 Attrs.addAtEnd(newAttr: AL);
1871 }
1872}
1873
1874Parser::DeclGroupPtrTy Parser::ParseDeclaration(DeclaratorContext Context,
1875 SourceLocation &DeclEnd,
1876 ParsedAttributes &DeclAttrs,
1877 ParsedAttributes &DeclSpecAttrs,
1878 SourceLocation *DeclSpecStart) {
1879 ParenBraceBracketBalancer BalancerRAIIObj(*this);
1880 // Must temporarily exit the objective-c container scope for
1881 // parsing c none objective-c decls.
1882 ObjCDeclContextSwitch ObjCDC(*this);
1883
1884 Decl *SingleDecl = nullptr;
1885 switch (Tok.getKind()) {
1886 case tok::kw_template:
1887 case tok::kw_export:
1888 ProhibitAttributes(Attrs&: DeclAttrs);
1889 ProhibitAttributes(Attrs&: DeclSpecAttrs);
1890 return ParseDeclarationStartingWithTemplate(Context, DeclEnd, AccessAttrs&: DeclAttrs);
1891 case tok::kw_inline:
1892 // Could be the start of an inline namespace. Allowed as an ext in C++03.
1893 if (getLangOpts().CPlusPlus && NextToken().is(K: tok::kw_namespace)) {
1894 ProhibitAttributes(Attrs&: DeclAttrs);
1895 ProhibitAttributes(Attrs&: DeclSpecAttrs);
1896 SourceLocation InlineLoc = ConsumeToken();
1897 return ParseNamespace(Context, DeclEnd, InlineLoc);
1898 }
1899 return ParseSimpleDeclaration(Context, DeclEnd, DeclAttrs, DeclSpecAttrs,
1900 RequireSemi: true, FRI: nullptr, DeclSpecStart);
1901
1902 case tok::kw_cbuffer:
1903 case tok::kw_tbuffer:
1904 SingleDecl = ParseHLSLBuffer(DeclEnd);
1905 break;
1906 case tok::kw_namespace:
1907 ProhibitAttributes(Attrs&: DeclAttrs);
1908 ProhibitAttributes(Attrs&: DeclSpecAttrs);
1909 return ParseNamespace(Context, DeclEnd);
1910 case tok::kw_using: {
1911 takeAndConcatenateAttrs(First&: DeclAttrs, Second: std::move(DeclSpecAttrs));
1912 return ParseUsingDirectiveOrDeclaration(Context, TemplateInfo: ParsedTemplateInfo(),
1913 DeclEnd, Attrs&: DeclAttrs);
1914 }
1915 case tok::kw_static_assert:
1916 case tok::kw__Static_assert:
1917 ProhibitAttributes(Attrs&: DeclAttrs);
1918 ProhibitAttributes(Attrs&: DeclSpecAttrs);
1919 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
1920 break;
1921 default:
1922 return ParseSimpleDeclaration(Context, DeclEnd, DeclAttrs, DeclSpecAttrs,
1923 RequireSemi: true, FRI: nullptr, DeclSpecStart);
1924 }
1925
1926 // This routine returns a DeclGroup, if the thing we parsed only contains a
1927 // single decl, convert it now.
1928 return Actions.ConvertDeclToDeclGroup(Ptr: SingleDecl);
1929}
1930
1931Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(
1932 DeclaratorContext Context, SourceLocation &DeclEnd,
1933 ParsedAttributes &DeclAttrs, ParsedAttributes &DeclSpecAttrs,
1934 bool RequireSemi, ForRangeInit *FRI, SourceLocation *DeclSpecStart) {
1935 // Need to retain these for diagnostics before we add them to the DeclSepc.
1936 ParsedAttributesView OriginalDeclSpecAttrs;
1937 OriginalDeclSpecAttrs.addAll(B: DeclSpecAttrs.begin(), E: DeclSpecAttrs.end());
1938 OriginalDeclSpecAttrs.Range = DeclSpecAttrs.Range;
1939
1940 // Parse the common declaration-specifiers piece.
1941 ParsingDeclSpec DS(*this);
1942 DS.takeAttributesFrom(attrs&: DeclSpecAttrs);
1943
1944 ParsedTemplateInfo TemplateInfo;
1945 DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
1946 ParseDeclarationSpecifiers(DS, TemplateInfo, AS: AS_none, DSC: DSContext);
1947
1948 // If we had a free-standing type definition with a missing semicolon, we
1949 // may get this far before the problem becomes obvious.
1950 if (DS.hasTagDefinition() &&
1951 DiagnoseMissingSemiAfterTagDefinition(DS, AS: AS_none, DSContext))
1952 return nullptr;
1953
1954 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1955 // declaration-specifiers init-declarator-list[opt] ';'
1956 if (Tok.is(K: tok::semi)) {
1957 ProhibitAttributes(Attrs&: DeclAttrs);
1958 DeclEnd = Tok.getLocation();
1959 if (RequireSemi) ConsumeToken();
1960 RecordDecl *AnonRecord = nullptr;
1961 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(
1962 S: getCurScope(), AS: AS_none, DS, DeclAttrs: ParsedAttributesView::none(), AnonRecord);
1963 Actions.ActOnDefinedDeclarationSpecifier(D: TheDecl);
1964 DS.complete(D: TheDecl);
1965 if (AnonRecord) {
1966 Decl* decls[] = {AnonRecord, TheDecl};
1967 return Actions.BuildDeclaratorGroup(Group: decls);
1968 }
1969 return Actions.ConvertDeclToDeclGroup(Ptr: TheDecl);
1970 }
1971
1972 if (DS.hasTagDefinition())
1973 Actions.ActOnDefinedDeclarationSpecifier(D: DS.getRepAsDecl());
1974
1975 if (DeclSpecStart)
1976 DS.SetRangeStart(*DeclSpecStart);
1977
1978 return ParseDeclGroup(DS, Context, Attrs&: DeclAttrs, TemplateInfo, DeclEnd: &DeclEnd, FRI);
1979}
1980
1981bool Parser::MightBeDeclarator(DeclaratorContext Context) {
1982 switch (Tok.getKind()) {
1983 case tok::annot_cxxscope:
1984 case tok::annot_template_id:
1985 case tok::caret:
1986 case tok::code_completion:
1987 case tok::coloncolon:
1988 case tok::ellipsis:
1989 case tok::kw___attribute:
1990 case tok::kw_operator:
1991 case tok::l_paren:
1992 case tok::star:
1993 return true;
1994
1995 case tok::amp:
1996 case tok::ampamp:
1997 return getLangOpts().CPlusPlus;
1998
1999 case tok::l_square: // Might be an attribute on an unnamed bit-field.
2000 return Context == DeclaratorContext::Member && getLangOpts().CPlusPlus11 &&
2001 NextToken().is(K: tok::l_square);
2002
2003 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
2004 return Context == DeclaratorContext::Member || getLangOpts().CPlusPlus;
2005
2006 case tok::identifier:
2007 switch (NextToken().getKind()) {
2008 case tok::code_completion:
2009 case tok::coloncolon:
2010 case tok::comma:
2011 case tok::equal:
2012 case tok::equalequal: // Might be a typo for '='.
2013 case tok::kw_alignas:
2014 case tok::kw_asm:
2015 case tok::kw___attribute:
2016 case tok::l_brace:
2017 case tok::l_paren:
2018 case tok::l_square:
2019 case tok::less:
2020 case tok::r_brace:
2021 case tok::r_paren:
2022 case tok::r_square:
2023 case tok::semi:
2024 return true;
2025
2026 case tok::colon:
2027 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
2028 // and in block scope it's probably a label. Inside a class definition,
2029 // this is a bit-field.
2030 return Context == DeclaratorContext::Member ||
2031 (getLangOpts().CPlusPlus && Context == DeclaratorContext::File);
2032
2033 case tok::identifier: // Possible virt-specifier.
2034 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(Tok: NextToken());
2035
2036 default:
2037 return Tok.isRegularKeywordAttribute();
2038 }
2039
2040 default:
2041 return Tok.isRegularKeywordAttribute();
2042 }
2043}
2044
2045void Parser::SkipMalformedDecl() {
2046 while (true) {
2047 switch (Tok.getKind()) {
2048 case tok::l_brace:
2049 // Skip until matching }, then stop. We've probably skipped over
2050 // a malformed class or function definition or similar.
2051 ConsumeBrace();
2052 SkipUntil(T: tok::r_brace);
2053 if (Tok.isOneOf(Ks: tok::comma, Ks: tok::l_brace, Ks: tok::kw_try)) {
2054 // This declaration isn't over yet. Keep skipping.
2055 continue;
2056 }
2057 TryConsumeToken(Expected: tok::semi);
2058 return;
2059
2060 case tok::l_square:
2061 ConsumeBracket();
2062 SkipUntil(T: tok::r_square);
2063 continue;
2064
2065 case tok::l_paren:
2066 ConsumeParen();
2067 SkipUntil(T: tok::r_paren);
2068 continue;
2069
2070 case tok::r_brace:
2071 return;
2072
2073 case tok::semi:
2074 ConsumeToken();
2075 return;
2076
2077 case tok::kw_inline:
2078 // 'inline namespace' at the start of a line is almost certainly
2079 // a good place to pick back up parsing, except in an Objective-C
2080 // @interface context.
2081 if (Tok.isAtStartOfLine() && NextToken().is(K: tok::kw_namespace) &&
2082 (!ParsingInObjCContainer || CurParsedObjCImpl))
2083 return;
2084 break;
2085
2086 case tok::kw_namespace:
2087 // 'namespace' at the start of a line is almost certainly a good
2088 // place to pick back up parsing, except in an Objective-C
2089 // @interface context.
2090 if (Tok.isAtStartOfLine() &&
2091 (!ParsingInObjCContainer || CurParsedObjCImpl))
2092 return;
2093 break;
2094
2095 case tok::at:
2096 // @end is very much like } in Objective-C contexts.
2097 if (NextToken().isObjCAtKeyword(objcKey: tok::objc_end) &&
2098 ParsingInObjCContainer)
2099 return;
2100 break;
2101
2102 case tok::minus:
2103 case tok::plus:
2104 // - and + probably start new method declarations in Objective-C contexts.
2105 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
2106 return;
2107 break;
2108
2109 case tok::eof:
2110 case tok::annot_module_begin:
2111 case tok::annot_module_end:
2112 case tok::annot_module_include:
2113 case tok::annot_repl_input_end:
2114 return;
2115
2116 default:
2117 break;
2118 }
2119
2120 ConsumeAnyToken();
2121 }
2122}
2123
2124Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
2125 DeclaratorContext Context,
2126 ParsedAttributes &Attrs,
2127 ParsedTemplateInfo &TemplateInfo,
2128 SourceLocation *DeclEnd,
2129 ForRangeInit *FRI) {
2130 // Parse the first declarator.
2131 // Consume all of the attributes from `Attrs` by moving them to our own local
2132 // list. This ensures that we will not attempt to interpret them as statement
2133 // attributes higher up the callchain.
2134 ParsedAttributes LocalAttrs(AttrFactory);
2135 LocalAttrs.takeAllFrom(Other&: Attrs);
2136 ParsingDeclarator D(*this, DS, LocalAttrs, Context);
2137 if (TemplateInfo.TemplateParams)
2138 D.setTemplateParameterLists(*TemplateInfo.TemplateParams);
2139
2140 bool IsTemplateSpecOrInst =
2141 (TemplateInfo.Kind == ParsedTemplateKind::ExplicitInstantiation ||
2142 TemplateInfo.Kind == ParsedTemplateKind::ExplicitSpecialization);
2143 SuppressAccessChecks SAC(*this, IsTemplateSpecOrInst);
2144
2145 ParseDeclarator(D);
2146
2147 if (IsTemplateSpecOrInst)
2148 SAC.done();
2149
2150 // Bail out if the first declarator didn't seem well-formed.
2151 if (!D.hasName() && !D.mayOmitIdentifier()) {
2152 SkipMalformedDecl();
2153 return nullptr;
2154 }
2155
2156 if (getLangOpts().HLSL)
2157 while (MaybeParseHLSLAnnotations(D))
2158 ;
2159
2160 if (Tok.is(K: tok::kw_requires))
2161 ParseTrailingRequiresClause(D);
2162
2163 // Save late-parsed attributes for now; they need to be parsed in the
2164 // appropriate function scope after the function Decl has been constructed.
2165 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
2166 LateParsedAttrList LateParsedAttrs(true);
2167 if (D.isFunctionDeclarator()) {
2168 MaybeParseGNUAttributes(D, LateAttrs: &LateParsedAttrs);
2169
2170 // The _Noreturn keyword can't appear here, unlike the GNU noreturn
2171 // attribute. If we find the keyword here, tell the user to put it
2172 // at the start instead.
2173 if (Tok.is(K: tok::kw__Noreturn)) {
2174 SourceLocation Loc = ConsumeToken();
2175 const char *PrevSpec;
2176 unsigned DiagID;
2177
2178 // We can offer a fixit if it's valid to mark this function as _Noreturn
2179 // and we don't have any other declarators in this declaration.
2180 bool Fixit = !DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
2181 MaybeParseGNUAttributes(D, LateAttrs: &LateParsedAttrs);
2182 Fixit &= Tok.isOneOf(Ks: tok::semi, Ks: tok::l_brace, Ks: tok::kw_try);
2183
2184 Diag(Loc, DiagID: diag::err_c11_noreturn_misplaced)
2185 << (Fixit ? FixItHint::CreateRemoval(RemoveRange: Loc) : FixItHint())
2186 << (Fixit ? FixItHint::CreateInsertion(InsertionLoc: D.getBeginLoc(), Code: "_Noreturn ")
2187 : FixItHint());
2188 }
2189
2190 // Check to see if we have a function *definition* which must have a body.
2191 if (Tok.is(K: tok::equal) && NextToken().is(K: tok::code_completion)) {
2192 cutOffParsing();
2193 Actions.CodeCompletion().CodeCompleteAfterFunctionEquals(D);
2194 return nullptr;
2195 }
2196 // We're at the point where the parsing of function declarator is finished.
2197 //
2198 // A common error is that users accidently add a virtual specifier
2199 // (e.g. override) in an out-line method definition.
2200 // We attempt to recover by stripping all these specifiers coming after
2201 // the declarator.
2202 while (auto Specifier = isCXX11VirtSpecifier()) {
2203 Diag(Tok, DiagID: diag::err_virt_specifier_outside_class)
2204 << VirtSpecifiers::getSpecifierName(VS: Specifier)
2205 << FixItHint::CreateRemoval(RemoveRange: Tok.getLocation());
2206 ConsumeToken();
2207 }
2208 // Look at the next token to make sure that this isn't a function
2209 // declaration. We have to check this because __attribute__ might be the
2210 // start of a function definition in GCC-extended K&R C.
2211 if (!isDeclarationAfterDeclarator()) {
2212
2213 // Function definitions are only allowed at file scope and in C++ classes.
2214 // The C++ inline method definition case is handled elsewhere, so we only
2215 // need to handle the file scope definition case.
2216 if (Context == DeclaratorContext::File) {
2217 if (isStartOfFunctionDefinition(Declarator: D)) {
2218 // C++23 [dcl.typedef] p1:
2219 // The typedef specifier shall not be [...], and it shall not be
2220 // used in the decl-specifier-seq of a parameter-declaration nor in
2221 // the decl-specifier-seq of a function-definition.
2222 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2223 // If the user intended to write 'typename', we should have already
2224 // suggested adding it elsewhere. In any case, recover by ignoring
2225 // 'typedef' and suggest removing it.
2226 Diag(Loc: DS.getStorageClassSpecLoc(),
2227 DiagID: diag::err_function_declared_typedef)
2228 << FixItHint::CreateRemoval(RemoveRange: DS.getStorageClassSpecLoc());
2229 DS.ClearStorageClassSpecs();
2230 }
2231 Decl *TheDecl = nullptr;
2232
2233 if (TemplateInfo.Kind == ParsedTemplateKind::ExplicitInstantiation) {
2234 if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) {
2235 // If the declarator-id is not a template-id, issue a diagnostic
2236 // and recover by ignoring the 'template' keyword.
2237 Diag(Tok, DiagID: diag::err_template_defn_explicit_instantiation) << 0;
2238 TheDecl = ParseFunctionDefinition(D, TemplateInfo: ParsedTemplateInfo(),
2239 LateParsedAttrs: &LateParsedAttrs);
2240 } else {
2241 SourceLocation LAngleLoc =
2242 PP.getLocForEndOfToken(Loc: TemplateInfo.TemplateLoc);
2243 Diag(Loc: D.getIdentifierLoc(),
2244 DiagID: diag::err_explicit_instantiation_with_definition)
2245 << SourceRange(TemplateInfo.TemplateLoc)
2246 << FixItHint::CreateInsertion(InsertionLoc: LAngleLoc, Code: "<>");
2247
2248 // Recover as if it were an explicit specialization.
2249 TemplateParameterLists FakedParamLists;
2250 FakedParamLists.push_back(Elt: Actions.ActOnTemplateParameterList(
2251 Depth: 0, ExportLoc: SourceLocation(), TemplateLoc: TemplateInfo.TemplateLoc, LAngleLoc, Params: {},
2252 RAngleLoc: LAngleLoc, RequiresClause: nullptr));
2253
2254 TheDecl = ParseFunctionDefinition(
2255 D,
2256 TemplateInfo: ParsedTemplateInfo(&FakedParamLists,
2257 /*isSpecialization=*/true,
2258 /*lastParameterListWasEmpty=*/true),
2259 LateParsedAttrs: &LateParsedAttrs);
2260 }
2261 } else {
2262 TheDecl =
2263 ParseFunctionDefinition(D, TemplateInfo, LateParsedAttrs: &LateParsedAttrs);
2264 }
2265
2266 return Actions.ConvertDeclToDeclGroup(Ptr: TheDecl);
2267 }
2268
2269 if (isDeclarationSpecifier(AllowImplicitTypename: ImplicitTypenameContext::No) ||
2270 Tok.is(K: tok::kw_namespace)) {
2271 // If there is an invalid declaration specifier or a namespace
2272 // definition right after the function prototype, then we must be in a
2273 // missing semicolon case where this isn't actually a body. Just fall
2274 // through into the code that handles it as a prototype, and let the
2275 // top-level code handle the erroneous declspec where it would
2276 // otherwise expect a comma or semicolon. Note that
2277 // isDeclarationSpecifier already covers 'inline namespace', since
2278 // 'inline' can be a declaration specifier.
2279 } else {
2280 Diag(Tok, DiagID: diag::err_expected_fn_body);
2281 SkipUntil(T: tok::semi);
2282 return nullptr;
2283 }
2284 } else {
2285 if (Tok.is(K: tok::l_brace)) {
2286 Diag(Tok, DiagID: diag::err_function_definition_not_allowed);
2287 SkipMalformedDecl();
2288 return nullptr;
2289 }
2290 }
2291 }
2292 }
2293
2294 if (ParseAsmAttributesAfterDeclarator(D))
2295 return nullptr;
2296
2297 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
2298 // must parse and analyze the for-range-initializer before the declaration is
2299 // analyzed.
2300 //
2301 // Handle the Objective-C for-in loop variable similarly, although we
2302 // don't need to parse the container in advance.
2303 if (FRI && (Tok.is(K: tok::colon) || isTokIdentifier_in())) {
2304 bool IsForRangeLoop = false;
2305 if (TryConsumeToken(Expected: tok::colon, Loc&: FRI->ColonLoc)) {
2306 IsForRangeLoop = true;
2307 EnterExpressionEvaluationContext ForRangeInitContext(
2308 Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated,
2309 /*LambdaContextDecl=*/nullptr,
2310 Sema::ExpressionEvaluationContextRecord::EK_Other,
2311 getLangOpts().CPlusPlus23);
2312
2313 // P2718R0 - Lifetime extension in range-based for loops.
2314 if (getLangOpts().CPlusPlus23) {
2315 auto &LastRecord = Actions.currentEvaluationContext();
2316 LastRecord.InLifetimeExtendingContext = true;
2317 LastRecord.RebuildDefaultArgOrDefaultInit = true;
2318 }
2319
2320 if (getLangOpts().OpenMP)
2321 Actions.OpenMP().startOpenMPCXXRangeFor();
2322 if (Tok.is(K: tok::l_brace))
2323 FRI->RangeExpr = ParseBraceInitializer();
2324 else
2325 FRI->RangeExpr = ParseExpression();
2326
2327 // Before c++23, ForRangeLifetimeExtendTemps should be empty.
2328 assert(
2329 getLangOpts().CPlusPlus23 ||
2330 Actions.ExprEvalContexts.back().ForRangeLifetimeExtendTemps.empty());
2331
2332 // Move the collected materialized temporaries into ForRangeInit before
2333 // ForRangeInitContext exit.
2334 FRI->LifetimeExtendTemps = std::move(
2335 Actions.ExprEvalContexts.back().ForRangeLifetimeExtendTemps);
2336 }
2337
2338 Decl *ThisDecl = Actions.ActOnDeclarator(S: getCurScope(), D);
2339 if (IsForRangeLoop) {
2340 Actions.ActOnCXXForRangeDecl(D: ThisDecl);
2341 } else {
2342 // Obj-C for loop
2343 if (auto *VD = dyn_cast_or_null<VarDecl>(Val: ThisDecl))
2344 VD->setObjCForDecl(true);
2345 }
2346 Actions.FinalizeDeclaration(D: ThisDecl);
2347 D.complete(D: ThisDecl);
2348 return Actions.FinalizeDeclaratorGroup(S: getCurScope(), DS, Group: ThisDecl);
2349 }
2350
2351 SmallVector<Decl *, 8> DeclsInGroup;
2352 Decl *FirstDecl =
2353 ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo, FRI);
2354 if (LateParsedAttrs.size() > 0)
2355 ParseLexedAttributeList(LAs&: LateParsedAttrs, D: FirstDecl, EnterScope: true, OnDefinition: false);
2356 D.complete(D: FirstDecl);
2357 if (FirstDecl)
2358 DeclsInGroup.push_back(Elt: FirstDecl);
2359
2360 bool ExpectSemi = Context != DeclaratorContext::ForInit;
2361
2362 // If we don't have a comma, it is either the end of the list (a ';') or an
2363 // error, bail out.
2364 SourceLocation CommaLoc;
2365 while (TryConsumeToken(Expected: tok::comma, Loc&: CommaLoc)) {
2366 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
2367 // This comma was followed by a line-break and something which can't be
2368 // the start of a declarator. The comma was probably a typo for a
2369 // semicolon.
2370 Diag(Loc: CommaLoc, DiagID: diag::err_expected_semi_declaration)
2371 << FixItHint::CreateReplacement(RemoveRange: CommaLoc, Code: ";");
2372 ExpectSemi = false;
2373 break;
2374 }
2375
2376 // C++23 [temp.pre]p5:
2377 // In a template-declaration, explicit specialization, or explicit
2378 // instantiation the init-declarator-list in the declaration shall
2379 // contain at most one declarator.
2380 if (TemplateInfo.Kind != ParsedTemplateKind::NonTemplate &&
2381 D.isFirstDeclarator()) {
2382 Diag(Loc: CommaLoc, DiagID: diag::err_multiple_template_declarators)
2383 << TemplateInfo.Kind;
2384 }
2385
2386 // Parse the next declarator.
2387 D.clear();
2388 D.setCommaLoc(CommaLoc);
2389
2390 // Accept attributes in an init-declarator. In the first declarator in a
2391 // declaration, these would be part of the declspec. In subsequent
2392 // declarators, they become part of the declarator itself, so that they
2393 // don't apply to declarators after *this* one. Examples:
2394 // short __attribute__((common)) var; -> declspec
2395 // short var __attribute__((common)); -> declarator
2396 // short x, __attribute__((common)) var; -> declarator
2397 MaybeParseGNUAttributes(D);
2398
2399 // MSVC parses but ignores qualifiers after the comma as an extension.
2400 if (getLangOpts().MicrosoftExt)
2401 DiagnoseAndSkipExtendedMicrosoftTypeAttributes();
2402
2403 ParseDeclarator(D);
2404
2405 if (getLangOpts().HLSL)
2406 MaybeParseHLSLAnnotations(D);
2407
2408 if (!D.isInvalidType()) {
2409 // C++2a [dcl.decl]p1
2410 // init-declarator:
2411 // declarator initializer[opt]
2412 // declarator requires-clause
2413 if (Tok.is(K: tok::kw_requires))
2414 ParseTrailingRequiresClause(D);
2415 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D, TemplateInfo);
2416 D.complete(D: ThisDecl);
2417 if (ThisDecl)
2418 DeclsInGroup.push_back(Elt: ThisDecl);
2419 }
2420 }
2421
2422 if (DeclEnd)
2423 *DeclEnd = Tok.getLocation();
2424
2425 if (ExpectSemi && ExpectAndConsumeSemi(
2426 DiagID: Context == DeclaratorContext::File
2427 ? diag::err_invalid_token_after_toplevel_declarator
2428 : diag::err_expected_semi_declaration)) {
2429 // Okay, there was no semicolon and one was expected. If we see a
2430 // declaration specifier, just assume it was missing and continue parsing.
2431 // Otherwise things are very confused and we skip to recover.
2432 if (!isDeclarationSpecifier(AllowImplicitTypename: ImplicitTypenameContext::No))
2433 SkipMalformedDecl();
2434 }
2435
2436 return Actions.FinalizeDeclaratorGroup(S: getCurScope(), DS, Group: DeclsInGroup);
2437}
2438
2439bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
2440 // If a simple-asm-expr is present, parse it.
2441 if (Tok.is(K: tok::kw_asm)) {
2442 SourceLocation Loc;
2443 ExprResult AsmLabel(ParseSimpleAsm(/*ForAsmLabel*/ true, EndLoc: &Loc));
2444 if (AsmLabel.isInvalid()) {
2445 SkipUntil(T: tok::semi, Flags: StopBeforeMatch);
2446 return true;
2447 }
2448
2449 D.setAsmLabel(AsmLabel.get());
2450 D.SetRangeEnd(Loc);
2451 }
2452
2453 MaybeParseGNUAttributes(D);
2454 return false;
2455}
2456
2457Decl *Parser::ParseDeclarationAfterDeclarator(
2458 Declarator &D, const ParsedTemplateInfo &TemplateInfo) {
2459 if (ParseAsmAttributesAfterDeclarator(D))
2460 return nullptr;
2461
2462 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
2463}
2464
2465Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(
2466 Declarator &D, const ParsedTemplateInfo &TemplateInfo, ForRangeInit *FRI) {
2467 // RAII type used to track whether we're inside an initializer.
2468 struct InitializerScopeRAII {
2469 Parser &P;
2470 Declarator &D;
2471 Decl *ThisDecl;
2472 bool Entered;
2473
2474 InitializerScopeRAII(Parser &P, Declarator &D, Decl *ThisDecl)
2475 : P(P), D(D), ThisDecl(ThisDecl), Entered(false) {
2476 if (ThisDecl && P.getLangOpts().CPlusPlus) {
2477 Scope *S = nullptr;
2478 if (D.getCXXScopeSpec().isSet()) {
2479 P.EnterScope(ScopeFlags: 0);
2480 S = P.getCurScope();
2481 }
2482 if (ThisDecl && !ThisDecl->isInvalidDecl()) {
2483 P.Actions.ActOnCXXEnterDeclInitializer(S, Dcl: ThisDecl);
2484 Entered = true;
2485 }
2486 }
2487 }
2488 ~InitializerScopeRAII() {
2489 if (ThisDecl && P.getLangOpts().CPlusPlus) {
2490 Scope *S = nullptr;
2491 if (D.getCXXScopeSpec().isSet())
2492 S = P.getCurScope();
2493
2494 if (Entered)
2495 P.Actions.ActOnCXXExitDeclInitializer(S, Dcl: ThisDecl);
2496 if (S)
2497 P.ExitScope();
2498 }
2499 ThisDecl = nullptr;
2500 }
2501 };
2502
2503 enum class InitKind { Uninitialized, Equal, CXXDirect, CXXBraced };
2504 InitKind TheInitKind;
2505 // If a '==' or '+=' is found, suggest a fixit to '='.
2506 if (isTokenEqualOrEqualTypo())
2507 TheInitKind = InitKind::Equal;
2508 else if (Tok.is(K: tok::l_paren))
2509 TheInitKind = InitKind::CXXDirect;
2510 else if (getLangOpts().CPlusPlus11 && Tok.is(K: tok::l_brace) &&
2511 (!CurParsedObjCImpl || !D.isFunctionDeclarator()))
2512 TheInitKind = InitKind::CXXBraced;
2513 else
2514 TheInitKind = InitKind::Uninitialized;
2515 if (TheInitKind != InitKind::Uninitialized)
2516 D.setHasInitializer();
2517
2518 // Inform Sema that we just parsed this declarator.
2519 Decl *ThisDecl = nullptr;
2520 Decl *OuterDecl = nullptr;
2521 switch (TemplateInfo.Kind) {
2522 case ParsedTemplateKind::NonTemplate:
2523 ThisDecl = Actions.ActOnDeclarator(S: getCurScope(), D);
2524 break;
2525
2526 case ParsedTemplateKind::Template:
2527 case ParsedTemplateKind::ExplicitSpecialization: {
2528 ThisDecl = Actions.ActOnTemplateDeclarator(S: getCurScope(),
2529 TemplateParameterLists: *TemplateInfo.TemplateParams,
2530 D);
2531 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(Val: ThisDecl)) {
2532 // Re-direct this decl to refer to the templated decl so that we can
2533 // initialize it.
2534 ThisDecl = VT->getTemplatedDecl();
2535 OuterDecl = VT;
2536 }
2537 break;
2538 }
2539 case ParsedTemplateKind::ExplicitInstantiation: {
2540 if (Tok.is(K: tok::semi)) {
2541 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
2542 S: getCurScope(), ExternLoc: TemplateInfo.ExternLoc, TemplateLoc: TemplateInfo.TemplateLoc, D);
2543 if (ThisRes.isInvalid()) {
2544 SkipUntil(T: tok::semi, Flags: StopBeforeMatch);
2545 return nullptr;
2546 }
2547 ThisDecl = ThisRes.get();
2548 } else {
2549 // FIXME: This check should be for a variable template instantiation only.
2550
2551 // Check that this is a valid instantiation
2552 if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) {
2553 // If the declarator-id is not a template-id, issue a diagnostic and
2554 // recover by ignoring the 'template' keyword.
2555 Diag(Tok, DiagID: diag::err_template_defn_explicit_instantiation)
2556 << 2 << FixItHint::CreateRemoval(RemoveRange: TemplateInfo.TemplateLoc);
2557 ThisDecl = Actions.ActOnDeclarator(S: getCurScope(), D);
2558 } else {
2559 SourceLocation LAngleLoc =
2560 PP.getLocForEndOfToken(Loc: TemplateInfo.TemplateLoc);
2561 Diag(Loc: D.getIdentifierLoc(),
2562 DiagID: diag::err_explicit_instantiation_with_definition)
2563 << SourceRange(TemplateInfo.TemplateLoc)
2564 << FixItHint::CreateInsertion(InsertionLoc: LAngleLoc, Code: "<>");
2565
2566 // Recover as if it were an explicit specialization.
2567 TemplateParameterLists FakedParamLists;
2568 FakedParamLists.push_back(Elt: Actions.ActOnTemplateParameterList(
2569 Depth: 0, ExportLoc: SourceLocation(), TemplateLoc: TemplateInfo.TemplateLoc, LAngleLoc, Params: {},
2570 RAngleLoc: LAngleLoc, RequiresClause: nullptr));
2571
2572 ThisDecl =
2573 Actions.ActOnTemplateDeclarator(S: getCurScope(), TemplateParameterLists: FakedParamLists, D);
2574 }
2575 }
2576 break;
2577 }
2578 }
2579
2580 SemaCUDA::CUDATargetContextRAII X(Actions.CUDA(),
2581 SemaCUDA::CTCK_InitGlobalVar, ThisDecl);
2582 switch (TheInitKind) {
2583 // Parse declarator '=' initializer.
2584 case InitKind::Equal: {
2585 SourceLocation EqualLoc = ConsumeToken();
2586
2587 if (Tok.is(K: tok::kw_delete)) {
2588 if (D.isFunctionDeclarator())
2589 Diag(Loc: ConsumeToken(), DiagID: diag::err_default_delete_in_multiple_declaration)
2590 << 1 /* delete */;
2591 else
2592 Diag(Loc: ConsumeToken(), DiagID: diag::err_deleted_non_function);
2593 SkipDeletedFunctionBody();
2594 } else if (Tok.is(K: tok::kw_default)) {
2595 if (D.isFunctionDeclarator())
2596 Diag(Loc: ConsumeToken(), DiagID: diag::err_default_delete_in_multiple_declaration)
2597 << 0 /* default */;
2598 else
2599 Diag(Loc: ConsumeToken(), DiagID: diag::err_default_special_members)
2600 << getLangOpts().CPlusPlus20;
2601 } else {
2602 InitializerScopeRAII InitScope(*this, D, ThisDecl);
2603
2604 if (Tok.is(K: tok::code_completion)) {
2605 cutOffParsing();
2606 Actions.CodeCompletion().CodeCompleteInitializer(S: getCurScope(),
2607 D: ThisDecl);
2608 Actions.FinalizeDeclaration(D: ThisDecl);
2609 return nullptr;
2610 }
2611
2612 PreferredType.enterVariableInit(Tok: Tok.getLocation(), D: ThisDecl);
2613 ExprResult Init = ParseInitializer();
2614
2615 // If this is the only decl in (possibly) range based for statement,
2616 // our best guess is that the user meant ':' instead of '='.
2617 if (Tok.is(K: tok::r_paren) && FRI && D.isFirstDeclarator()) {
2618 Diag(Loc: EqualLoc, DiagID: diag::err_single_decl_assign_in_for_range)
2619 << FixItHint::CreateReplacement(RemoveRange: EqualLoc, Code: ":");
2620 // We are trying to stop parser from looking for ';' in this for
2621 // statement, therefore preventing spurious errors to be issued.
2622 FRI->ColonLoc = EqualLoc;
2623 Init = ExprError();
2624 FRI->RangeExpr = Init;
2625 }
2626
2627 if (Init.isInvalid()) {
2628 SmallVector<tok::TokenKind, 2> StopTokens;
2629 StopTokens.push_back(Elt: tok::comma);
2630 if (D.getContext() == DeclaratorContext::ForInit ||
2631 D.getContext() == DeclaratorContext::SelectionInit)
2632 StopTokens.push_back(Elt: tok::r_paren);
2633 SkipUntil(Toks: StopTokens, Flags: StopAtSemi | StopBeforeMatch);
2634 Actions.ActOnInitializerError(Dcl: ThisDecl);
2635 } else
2636 Actions.AddInitializerToDecl(dcl: ThisDecl, init: Init.get(),
2637 /*DirectInit=*/false);
2638 }
2639 break;
2640 }
2641 case InitKind::CXXDirect: {
2642 // Parse C++ direct initializer: '(' expression-list ')'
2643 BalancedDelimiterTracker T(*this, tok::l_paren);
2644 T.consumeOpen();
2645
2646 ExprVector Exprs;
2647
2648 InitializerScopeRAII InitScope(*this, D, ThisDecl);
2649
2650 auto ThisVarDecl = dyn_cast_or_null<VarDecl>(Val: ThisDecl);
2651 auto RunSignatureHelp = [&]() {
2652 QualType PreferredType =
2653 Actions.CodeCompletion().ProduceConstructorSignatureHelp(
2654 Type: ThisVarDecl->getType()->getCanonicalTypeInternal(),
2655 Loc: ThisDecl->getLocation(), Args: Exprs, OpenParLoc: T.getOpenLocation(),
2656 /*Braced=*/false);
2657 CalledSignatureHelp = true;
2658 return PreferredType;
2659 };
2660 auto SetPreferredType = [&] {
2661 PreferredType.enterFunctionArgument(Tok: Tok.getLocation(), ComputeType: RunSignatureHelp);
2662 };
2663
2664 llvm::function_ref<void()> ExpressionStarts;
2665 if (ThisVarDecl) {
2666 // ParseExpressionList can sometimes succeed even when ThisDecl is not
2667 // VarDecl. This is an error and it is reported in a call to
2668 // Actions.ActOnInitializerError(). However, we call
2669 // ProduceConstructorSignatureHelp only on VarDecls.
2670 ExpressionStarts = SetPreferredType;
2671 }
2672
2673 bool SawError = ParseExpressionList(Exprs, ExpressionStarts);
2674
2675 if (SawError) {
2676 if (ThisVarDecl && PP.isCodeCompletionReached() && !CalledSignatureHelp) {
2677 Actions.CodeCompletion().ProduceConstructorSignatureHelp(
2678 Type: ThisVarDecl->getType()->getCanonicalTypeInternal(),
2679 Loc: ThisDecl->getLocation(), Args: Exprs, OpenParLoc: T.getOpenLocation(),
2680 /*Braced=*/false);
2681 CalledSignatureHelp = true;
2682 }
2683 Actions.ActOnInitializerError(Dcl: ThisDecl);
2684 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
2685 } else {
2686 // Match the ')'.
2687 T.consumeClose();
2688
2689 ExprResult Initializer = Actions.ActOnParenListExpr(L: T.getOpenLocation(),
2690 R: T.getCloseLocation(),
2691 Val: Exprs);
2692 Actions.AddInitializerToDecl(dcl: ThisDecl, init: Initializer.get(),
2693 /*DirectInit=*/true);
2694 }
2695 break;
2696 }
2697 case InitKind::CXXBraced: {
2698 // Parse C++0x braced-init-list.
2699 Diag(Tok, DiagID: diag::warn_cxx98_compat_generalized_initializer_lists);
2700
2701 InitializerScopeRAII InitScope(*this, D, ThisDecl);
2702
2703 PreferredType.enterVariableInit(Tok: Tok.getLocation(), D: ThisDecl);
2704 ExprResult Init(ParseBraceInitializer());
2705
2706 if (Init.isInvalid()) {
2707 Actions.ActOnInitializerError(Dcl: ThisDecl);
2708 } else
2709 Actions.AddInitializerToDecl(dcl: ThisDecl, init: Init.get(), /*DirectInit=*/true);
2710 break;
2711 }
2712 case InitKind::Uninitialized: {
2713 Actions.ActOnUninitializedDecl(dcl: ThisDecl);
2714 break;
2715 }
2716 }
2717
2718 Actions.FinalizeDeclaration(D: ThisDecl);
2719 return OuterDecl ? OuterDecl : ThisDecl;
2720}
2721
2722void Parser::ParseSpecifierQualifierList(
2723 DeclSpec &DS, ImplicitTypenameContext AllowImplicitTypename,
2724 AccessSpecifier AS, DeclSpecContext DSC) {
2725 ParsedTemplateInfo TemplateInfo;
2726 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
2727 /// parse declaration-specifiers and complain about extra stuff.
2728 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
2729 ParseDeclarationSpecifiers(DS, TemplateInfo, AS, DSC, LateAttrs: nullptr,
2730 AllowImplicitTypename);
2731
2732 // Validate declspec for type-name.
2733 unsigned Specs = DS.getParsedSpecifiers();
2734 if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) {
2735 Diag(Tok, DiagID: diag::err_expected_type);
2736 DS.SetTypeSpecError();
2737 } else if (Specs == DeclSpec::PQ_None && !DS.hasAttributes()) {
2738 Diag(Tok, DiagID: diag::err_typename_requires_specqual);
2739 if (!DS.hasTypeSpecifier())
2740 DS.SetTypeSpecError();
2741 }
2742
2743 // Issue diagnostic and remove storage class if present.
2744 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
2745 if (DS.getStorageClassSpecLoc().isValid())
2746 Diag(Loc: DS.getStorageClassSpecLoc(),DiagID: diag::err_typename_invalid_storageclass);
2747 else
2748 Diag(Loc: DS.getThreadStorageClassSpecLoc(),
2749 DiagID: diag::err_typename_invalid_storageclass);
2750 DS.ClearStorageClassSpecs();
2751 }
2752
2753 // Issue diagnostic and remove function specifier if present.
2754 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
2755 if (DS.isInlineSpecified())
2756 Diag(Loc: DS.getInlineSpecLoc(), DiagID: diag::err_typename_invalid_functionspec);
2757 if (DS.isVirtualSpecified())
2758 Diag(Loc: DS.getVirtualSpecLoc(), DiagID: diag::err_typename_invalid_functionspec);
2759 if (DS.hasExplicitSpecifier())
2760 Diag(Loc: DS.getExplicitSpecLoc(), DiagID: diag::err_typename_invalid_functionspec);
2761 if (DS.isNoreturnSpecified())
2762 Diag(Loc: DS.getNoreturnSpecLoc(), DiagID: diag::err_typename_invalid_functionspec);
2763 DS.ClearFunctionSpecs();
2764 }
2765
2766 // Issue diagnostic and remove constexpr specifier if present.
2767 if (DS.hasConstexprSpecifier() && DSC != DeclSpecContext::DSC_condition) {
2768 Diag(Loc: DS.getConstexprSpecLoc(), DiagID: diag::err_typename_invalid_constexpr)
2769 << static_cast<int>(DS.getConstexprSpecifier());
2770 DS.ClearConstexprSpec();
2771 }
2772}
2773
2774/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2775/// specified token is valid after the identifier in a declarator which
2776/// immediately follows the declspec. For example, these things are valid:
2777///
2778/// int x [ 4]; // direct-declarator
2779/// int x ( int y); // direct-declarator
2780/// int(int x ) // direct-declarator
2781/// int x ; // simple-declaration
2782/// int x = 17; // init-declarator-list
2783/// int x , y; // init-declarator-list
2784/// int x __asm__ ("foo"); // init-declarator-list
2785/// int x : 4; // struct-declarator
2786/// int x { 5}; // C++'0x unified initializers
2787///
2788/// This is not, because 'x' does not immediately follow the declspec (though
2789/// ')' happens to be valid anyway).
2790/// int (x)
2791///
2792static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2793 return T.isOneOf(Ks: tok::l_square, Ks: tok::l_paren, Ks: tok::r_paren, Ks: tok::semi,
2794 Ks: tok::comma, Ks: tok::equal, Ks: tok::kw_asm, Ks: tok::l_brace,
2795 Ks: tok::colon);
2796}
2797
2798bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
2799 ParsedTemplateInfo &TemplateInfo,
2800 AccessSpecifier AS, DeclSpecContext DSC,
2801 ParsedAttributes &Attrs) {
2802 assert(Tok.is(tok::identifier) && "should have identifier");
2803
2804 SourceLocation Loc = Tok.getLocation();
2805 // If we see an identifier that is not a type name, we normally would
2806 // parse it as the identifier being declared. However, when a typename
2807 // is typo'd or the definition is not included, this will incorrectly
2808 // parse the typename as the identifier name and fall over misparsing
2809 // later parts of the diagnostic.
2810 //
2811 // As such, we try to do some look-ahead in cases where this would
2812 // otherwise be an "implicit-int" case to see if this is invalid. For
2813 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2814 // an identifier with implicit int, we'd get a parse error because the
2815 // next token is obviously invalid for a type. Parse these as a case
2816 // with an invalid type specifier.
2817 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
2818
2819 // Since we know that this either implicit int (which is rare) or an
2820 // error, do lookahead to try to do better recovery. This never applies
2821 // within a type specifier. Outside of C++, we allow this even if the
2822 // language doesn't "officially" support implicit int -- we support
2823 // implicit int as an extension in some language modes.
2824 if (!isTypeSpecifier(DSC) && getLangOpts().isImplicitIntAllowed() &&
2825 isValidAfterIdentifierInDeclarator(T: NextToken())) {
2826 // If this token is valid for implicit int, e.g. "static x = 4", then
2827 // we just avoid eating the identifier, so it will be parsed as the
2828 // identifier in the declarator.
2829 return false;
2830 }
2831
2832 // Early exit as Sema has a dedicated missing_actual_pipe_type diagnostic
2833 // for incomplete declarations such as `pipe p`.
2834 if (getLangOpts().OpenCLCPlusPlus && DS.isTypeSpecPipe())
2835 return false;
2836
2837 if (getLangOpts().CPlusPlus &&
2838 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2839 // Don't require a type specifier if we have the 'auto' storage class
2840 // specifier in C++98 -- we'll promote it to a type specifier.
2841 if (SS)
2842 AnnotateScopeToken(SS&: *SS, /*IsNewAnnotation*/false);
2843 return false;
2844 }
2845
2846 if (getLangOpts().CPlusPlus && (!SS || SS->isEmpty()) &&
2847 getLangOpts().MSVCCompat) {
2848 // Lookup of an unqualified type name has failed in MSVC compatibility mode.
2849 // Give Sema a chance to recover if we are in a template with dependent base
2850 // classes.
2851 if (ParsedType T = Actions.ActOnMSVCUnknownTypeName(
2852 II: *Tok.getIdentifierInfo(), NameLoc: Tok.getLocation(),
2853 IsTemplateTypeArg: DSC == DeclSpecContext::DSC_template_type_arg)) {
2854 const char *PrevSpec;
2855 unsigned DiagID;
2856 DS.SetTypeSpecType(T: DeclSpec::TST_typename, Loc, PrevSpec, DiagID, Rep: T,
2857 Policy: Actions.getASTContext().getPrintingPolicy());
2858 DS.SetRangeEnd(Tok.getLocation());
2859 ConsumeToken();
2860 return false;
2861 }
2862 }
2863
2864 // Otherwise, if we don't consume this token, we are going to emit an
2865 // error anyway. Try to recover from various common problems. Check
2866 // to see if this was a reference to a tag name without a tag specified.
2867 // This is a common problem in C (saying 'foo' instead of 'struct foo').
2868 //
2869 // C++ doesn't need this, and isTagName doesn't take SS.
2870 if (SS == nullptr) {
2871 const char *TagName = nullptr, *FixitTagName = nullptr;
2872 tok::TokenKind TagKind = tok::unknown;
2873
2874 switch (Actions.isTagName(II&: *Tok.getIdentifierInfo(), S: getCurScope())) {
2875 default: break;
2876 case DeclSpec::TST_enum:
2877 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2878 case DeclSpec::TST_union:
2879 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2880 case DeclSpec::TST_struct:
2881 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
2882 case DeclSpec::TST_interface:
2883 TagName="__interface"; FixitTagName = "__interface ";
2884 TagKind=tok::kw___interface;break;
2885 case DeclSpec::TST_class:
2886 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
2887 }
2888
2889 if (TagName) {
2890 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2891 LookupResult R(Actions, TokenName, SourceLocation(),
2892 Sema::LookupOrdinaryName);
2893
2894 Diag(Loc, DiagID: diag::err_use_of_tag_name_without_tag)
2895 << TokenName << TagName << getLangOpts().CPlusPlus
2896 << FixItHint::CreateInsertion(InsertionLoc: Tok.getLocation(), Code: FixitTagName);
2897
2898 if (Actions.LookupName(R, S: getCurScope())) {
2899 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2900 I != IEnd; ++I)
2901 Diag(Loc: (*I)->getLocation(), DiagID: diag::note_decl_hiding_tag_type)
2902 << TokenName << TagName;
2903 }
2904
2905 // Parse this as a tag as if the missing tag were present.
2906 if (TagKind == tok::kw_enum)
2907 ParseEnumSpecifier(TagLoc: Loc, DS, TemplateInfo, AS,
2908 DSC: DeclSpecContext::DSC_normal);
2909 else
2910 ParseClassSpecifier(TagTokKind: TagKind, TagLoc: Loc, DS, TemplateInfo, AS,
2911 /*EnteringContext*/ false,
2912 DSC: DeclSpecContext::DSC_normal, Attributes&: Attrs);
2913 return true;
2914 }
2915 }
2916
2917 // Determine whether this identifier could plausibly be the name of something
2918 // being declared (with a missing type).
2919 if (!isTypeSpecifier(DSC) && (!SS || DSC == DeclSpecContext::DSC_top_level ||
2920 DSC == DeclSpecContext::DSC_class)) {
2921 // Look ahead to the next token to try to figure out what this declaration
2922 // was supposed to be.
2923 switch (NextToken().getKind()) {
2924 case tok::l_paren: {
2925 // static x(4); // 'x' is not a type
2926 // x(int n); // 'x' is not a type
2927 // x (*p)[]; // 'x' is a type
2928 //
2929 // Since we're in an error case, we can afford to perform a tentative
2930 // parse to determine which case we're in.
2931 TentativeParsingAction PA(*this);
2932 ConsumeToken();
2933 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2934 PA.Revert();
2935
2936 if (TPR != TPResult::False) {
2937 // The identifier is followed by a parenthesized declarator.
2938 // It's supposed to be a type.
2939 break;
2940 }
2941
2942 // If we're in a context where we could be declaring a constructor,
2943 // check whether this is a constructor declaration with a bogus name.
2944 if (DSC == DeclSpecContext::DSC_class ||
2945 (DSC == DeclSpecContext::DSC_top_level && SS)) {
2946 IdentifierInfo *II = Tok.getIdentifierInfo();
2947 if (Actions.isCurrentClassNameTypo(II, SS)) {
2948 Diag(Loc, DiagID: diag::err_constructor_bad_name)
2949 << Tok.getIdentifierInfo() << II
2950 << FixItHint::CreateReplacement(RemoveRange: Tok.getLocation(), Code: II->getName());
2951 Tok.setIdentifierInfo(II);
2952 }
2953 }
2954 // Fall through.
2955 [[fallthrough]];
2956 }
2957 case tok::comma:
2958 case tok::equal:
2959 case tok::kw_asm:
2960 case tok::l_brace:
2961 case tok::l_square:
2962 case tok::semi:
2963 // This looks like a variable or function declaration. The type is
2964 // probably missing. We're done parsing decl-specifiers.
2965 // But only if we are not in a function prototype scope.
2966 if (getCurScope()->isFunctionPrototypeScope())
2967 break;
2968 if (SS)
2969 AnnotateScopeToken(SS&: *SS, /*IsNewAnnotation*/false);
2970 return false;
2971
2972 default:
2973 // This is probably supposed to be a type. This includes cases like:
2974 // int f(itn);
2975 // struct S { unsigned : 4; };
2976 break;
2977 }
2978 }
2979
2980 // This is almost certainly an invalid type name. Let Sema emit a diagnostic
2981 // and attempt to recover.
2982 ParsedType T;
2983 IdentifierInfo *II = Tok.getIdentifierInfo();
2984 bool IsTemplateName = getLangOpts().CPlusPlus && NextToken().is(K: tok::less);
2985 Actions.DiagnoseUnknownTypeName(II, IILoc: Loc, S: getCurScope(), SS, SuggestedType&: T,
2986 IsTemplateName);
2987 if (T) {
2988 // The action has suggested that the type T could be used. Set that as
2989 // the type in the declaration specifiers, consume the would-be type
2990 // name token, and we're done.
2991 const char *PrevSpec;
2992 unsigned DiagID;
2993 DS.SetTypeSpecType(T: DeclSpec::TST_typename, Loc, PrevSpec, DiagID, Rep: T,
2994 Policy: Actions.getASTContext().getPrintingPolicy());
2995 DS.SetRangeEnd(Tok.getLocation());
2996 ConsumeToken();
2997 // There may be other declaration specifiers after this.
2998 return true;
2999 } else if (II != Tok.getIdentifierInfo()) {
3000 // If no type was suggested, the correction is to a keyword
3001 Tok.setKind(II->getTokenID());
3002 // There may be other declaration specifiers after this.
3003 return true;
3004 }
3005
3006 // Otherwise, the action had no suggestion for us. Mark this as an error.
3007 DS.SetTypeSpecError();
3008 DS.SetRangeEnd(Tok.getLocation());
3009 ConsumeToken();
3010
3011 // Eat any following template arguments.
3012 if (IsTemplateName) {
3013 SourceLocation LAngle, RAngle;
3014 TemplateArgList Args;
3015 ParseTemplateIdAfterTemplateName(ConsumeLastToken: true, LAngleLoc&: LAngle, TemplateArgs&: Args, RAngleLoc&: RAngle);
3016 }
3017
3018 // TODO: Could inject an invalid typedef decl in an enclosing scope to
3019 // avoid rippling error messages on subsequent uses of the same type,
3020 // could be useful if #include was forgotten.
3021 return true;
3022}
3023
3024Parser::DeclSpecContext
3025Parser::getDeclSpecContextFromDeclaratorContext(DeclaratorContext Context) {
3026 switch (Context) {
3027 case DeclaratorContext::Member:
3028 return DeclSpecContext::DSC_class;
3029 case DeclaratorContext::File:
3030 return DeclSpecContext::DSC_top_level;
3031 case DeclaratorContext::TemplateParam:
3032 return DeclSpecContext::DSC_template_param;
3033 case DeclaratorContext::TemplateArg:
3034 return DeclSpecContext::DSC_template_arg;
3035 case DeclaratorContext::TemplateTypeArg:
3036 return DeclSpecContext::DSC_template_type_arg;
3037 case DeclaratorContext::TrailingReturn:
3038 case DeclaratorContext::TrailingReturnVar:
3039 return DeclSpecContext::DSC_trailing;
3040 case DeclaratorContext::AliasDecl:
3041 case DeclaratorContext::AliasTemplate:
3042 return DeclSpecContext::DSC_alias_declaration;
3043 case DeclaratorContext::Association:
3044 return DeclSpecContext::DSC_association;
3045 case DeclaratorContext::TypeName:
3046 return DeclSpecContext::DSC_type_specifier;
3047 case DeclaratorContext::Condition:
3048 return DeclSpecContext::DSC_condition;
3049 case DeclaratorContext::ConversionId:
3050 return DeclSpecContext::DSC_conv_operator;
3051 case DeclaratorContext::CXXNew:
3052 return DeclSpecContext::DSC_new;
3053 case DeclaratorContext::Prototype:
3054 case DeclaratorContext::ObjCResult:
3055 case DeclaratorContext::ObjCParameter:
3056 case DeclaratorContext::KNRTypeList:
3057 case DeclaratorContext::FunctionalCast:
3058 case DeclaratorContext::Block:
3059 case DeclaratorContext::ForInit:
3060 case DeclaratorContext::SelectionInit:
3061 case DeclaratorContext::CXXCatch:
3062 case DeclaratorContext::ObjCCatch:
3063 case DeclaratorContext::BlockLiteral:
3064 case DeclaratorContext::LambdaExpr:
3065 case DeclaratorContext::LambdaExprParameter:
3066 case DeclaratorContext::RequiresExpr:
3067 return DeclSpecContext::DSC_normal;
3068 }
3069
3070 llvm_unreachable("Missing DeclaratorContext case");
3071}
3072
3073ExprResult Parser::ParseAlignArgument(StringRef KWName, SourceLocation Start,
3074 SourceLocation &EllipsisLoc, bool &IsType,
3075 ParsedType &TypeResult) {
3076 ExprResult ER;
3077 if (isTypeIdInParens()) {
3078 SourceLocation TypeLoc = Tok.getLocation();
3079 ParsedType Ty = ParseTypeName().get();
3080 SourceRange TypeRange(Start, Tok.getLocation());
3081 if (Actions.ActOnAlignasTypeArgument(KWName, Ty, OpLoc: TypeLoc, R: TypeRange))
3082 return ExprError();
3083 TypeResult = Ty;
3084 IsType = true;
3085 } else {
3086 ER = ParseConstantExpression();
3087 IsType = false;
3088 }
3089
3090 if (getLangOpts().CPlusPlus11)
3091 TryConsumeToken(Expected: tok::ellipsis, Loc&: EllipsisLoc);
3092
3093 return ER;
3094}
3095
3096void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
3097 SourceLocation *EndLoc) {
3098 assert(Tok.isOneOf(tok::kw_alignas, tok::kw__Alignas) &&
3099 "Not an alignment-specifier!");
3100 Token KWTok = Tok;
3101 IdentifierInfo *KWName = KWTok.getIdentifierInfo();
3102 auto Kind = KWTok.getKind();
3103 SourceLocation KWLoc = ConsumeToken();
3104
3105 BalancedDelimiterTracker T(*this, tok::l_paren);
3106 if (T.expectAndConsume())
3107 return;
3108
3109 bool IsType;
3110 ParsedType TypeResult;
3111 SourceLocation EllipsisLoc;
3112 ExprResult ArgExpr =
3113 ParseAlignArgument(KWName: PP.getSpelling(Tok: KWTok), Start: T.getOpenLocation(),
3114 EllipsisLoc, IsType, TypeResult);
3115 if (ArgExpr.isInvalid()) {
3116 T.skipToEnd();
3117 return;
3118 }
3119
3120 T.consumeClose();
3121 if (EndLoc)
3122 *EndLoc = T.getCloseLocation();
3123
3124 if (IsType) {
3125 Attrs.addNewTypeAttr(attrName: KWName, attrRange: KWLoc, scope: AttributeScopeInfo(), typeArg: TypeResult, formUsed: Kind,
3126 ellipsisLoc: EllipsisLoc);
3127 } else {
3128 ArgsVector ArgExprs;
3129 ArgExprs.push_back(Elt: ArgExpr.get());
3130 Attrs.addNew(attrName: KWName, attrRange: KWLoc, scope: AttributeScopeInfo(), args: ArgExprs.data(), numArgs: 1, form: Kind,
3131 ellipsisLoc: EllipsisLoc);
3132 }
3133}
3134
3135void Parser::DistributeCLateParsedAttrs(Decl *Dcl,
3136 LateParsedAttrList *LateAttrs) {
3137 if (!LateAttrs)
3138 return;
3139
3140 if (Dcl) {
3141 for (auto *LateAttr : *LateAttrs) {
3142 if (LateAttr->Decls.empty())
3143 LateAttr->addDecl(D: Dcl);
3144 }
3145 }
3146}
3147
3148void Parser::ParsePtrauthQualifier(ParsedAttributes &Attrs) {
3149 assert(Tok.is(tok::kw___ptrauth));
3150
3151 IdentifierInfo *KwName = Tok.getIdentifierInfo();
3152 SourceLocation KwLoc = ConsumeToken();
3153
3154 BalancedDelimiterTracker T(*this, tok::l_paren);
3155 if (T.expectAndConsume())
3156 return;
3157
3158 ArgsVector ArgExprs;
3159 do {
3160 ExprResult ER = ParseAssignmentExpression();
3161 if (ER.isInvalid()) {
3162 T.skipToEnd();
3163 return;
3164 }
3165 ArgExprs.push_back(Elt: ER.get());
3166 } while (TryConsumeToken(Expected: tok::comma));
3167
3168 T.consumeClose();
3169 SourceLocation EndLoc = T.getCloseLocation();
3170
3171 if (ArgExprs.empty() || ArgExprs.size() > 3) {
3172 Diag(Loc: KwLoc, DiagID: diag::err_ptrauth_qualifier_bad_arg_count);
3173 return;
3174 }
3175
3176 Attrs.addNew(attrName: KwName, attrRange: SourceRange(KwLoc, EndLoc), scope: AttributeScopeInfo(),
3177 args: ArgExprs.data(), numArgs: ArgExprs.size(),
3178 form: ParsedAttr::Form::Keyword(/*IsAlignAs=*/IsAlignas: false,
3179 /*IsRegularKeywordAttribute=*/false));
3180}
3181
3182void Parser::ParseBoundsAttribute(IdentifierInfo &AttrName,
3183 SourceLocation AttrNameLoc,
3184 ParsedAttributes &Attrs,
3185 IdentifierInfo *ScopeName,
3186 SourceLocation ScopeLoc,
3187 ParsedAttr::Form Form) {
3188 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
3189
3190 BalancedDelimiterTracker Parens(*this, tok::l_paren);
3191 Parens.consumeOpen();
3192
3193 if (Tok.is(K: tok::r_paren)) {
3194 Diag(Loc: Tok.getLocation(), DiagID: diag::err_argument_required_after_attribute);
3195 Parens.consumeClose();
3196 return;
3197 }
3198
3199 ArgsVector ArgExprs;
3200 // Don't evaluate argument when the attribute is ignored.
3201 using ExpressionKind =
3202 Sema::ExpressionEvaluationContextRecord::ExpressionKind;
3203 EnterExpressionEvaluationContext EC(
3204 Actions, Sema::ExpressionEvaluationContext::PotentiallyEvaluated, nullptr,
3205 ExpressionKind::EK_AttrArgument);
3206
3207 ExprResult ArgExpr = ParseAssignmentExpression();
3208 if (ArgExpr.isInvalid()) {
3209 Parens.skipToEnd();
3210 return;
3211 }
3212
3213 ArgExprs.push_back(Elt: ArgExpr.get());
3214 Parens.consumeClose();
3215
3216 ASTContext &Ctx = Actions.getASTContext();
3217
3218 ArgExprs.push_back(Elt: IntegerLiteral::Create(
3219 C: Ctx, V: llvm::APInt(Ctx.getTypeSize(T: Ctx.getSizeType()), 0),
3220 type: Ctx.getSizeType(), l: SourceLocation()));
3221
3222 Attrs.addNew(attrName: &AttrName, attrRange: SourceRange(AttrNameLoc, Parens.getCloseLocation()),
3223 scope: AttributeScopeInfo(), args: ArgExprs.data(), numArgs: ArgExprs.size(), form: Form);
3224}
3225
3226ExprResult Parser::ParseExtIntegerArgument() {
3227 assert(Tok.isOneOf(tok::kw__ExtInt, tok::kw__BitInt) &&
3228 "Not an extended int type");
3229 ConsumeToken();
3230
3231 BalancedDelimiterTracker T(*this, tok::l_paren);
3232 if (T.expectAndConsume())
3233 return ExprError();
3234
3235 ExprResult ER = ParseConstantExpression();
3236 if (ER.isInvalid()) {
3237 T.skipToEnd();
3238 return ExprError();
3239 }
3240
3241 if(T.consumeClose())
3242 return ExprError();
3243 return ER;
3244}
3245
3246bool
3247Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
3248 DeclSpecContext DSContext,
3249 LateParsedAttrList *LateAttrs) {
3250 assert(DS.hasTagDefinition() && "shouldn't call this");
3251
3252 bool EnteringContext = (DSContext == DeclSpecContext::DSC_class ||
3253 DSContext == DeclSpecContext::DSC_top_level);
3254
3255 if (getLangOpts().CPlusPlus &&
3256 Tok.isOneOf(Ks: tok::identifier, Ks: tok::coloncolon, Ks: tok::kw_decltype,
3257 Ks: tok::annot_template_id) &&
3258 TryAnnotateCXXScopeToken(EnteringContext)) {
3259 SkipMalformedDecl();
3260 return true;
3261 }
3262
3263 bool HasScope = Tok.is(K: tok::annot_cxxscope);
3264 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
3265 Token AfterScope = HasScope ? NextToken() : Tok;
3266
3267 // Determine whether the following tokens could possibly be a
3268 // declarator.
3269 bool MightBeDeclarator = true;
3270 if (Tok.isOneOf(Ks: tok::kw_typename, Ks: tok::annot_typename)) {
3271 // A declarator-id can't start with 'typename'.
3272 MightBeDeclarator = false;
3273 } else if (AfterScope.is(K: tok::annot_template_id)) {
3274 // If we have a type expressed as a template-id, this cannot be a
3275 // declarator-id (such a type cannot be redeclared in a simple-declaration).
3276 TemplateIdAnnotation *Annot =
3277 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
3278 if (Annot->Kind == TNK_Type_template)
3279 MightBeDeclarator = false;
3280 } else if (AfterScope.is(K: tok::identifier)) {
3281 const Token &Next = HasScope ? GetLookAheadToken(N: 2) : NextToken();
3282
3283 // These tokens cannot come after the declarator-id in a
3284 // simple-declaration, and are likely to come after a type-specifier.
3285 if (Next.isOneOf(Ks: tok::star, Ks: tok::amp, Ks: tok::ampamp, Ks: tok::identifier,
3286 Ks: tok::annot_cxxscope, Ks: tok::coloncolon)) {
3287 // Missing a semicolon.
3288 MightBeDeclarator = false;
3289 } else if (HasScope) {
3290 // If the declarator-id has a scope specifier, it must redeclare a
3291 // previously-declared entity. If that's a type (and this is not a
3292 // typedef), that's an error.
3293 CXXScopeSpec SS;
3294 Actions.RestoreNestedNameSpecifierAnnotation(
3295 Annotation: Tok.getAnnotationValue(), AnnotationRange: Tok.getAnnotationRange(), SS);
3296 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
3297 Sema::NameClassification Classification = Actions.ClassifyName(
3298 S: getCurScope(), SS, Name, NameLoc: AfterScope.getLocation(), NextToken: Next,
3299 /*CCC=*/nullptr);
3300 switch (Classification.getKind()) {
3301 case NameClassificationKind::Error:
3302 SkipMalformedDecl();
3303 return true;
3304
3305 case NameClassificationKind::Keyword:
3306 llvm_unreachable("typo correction is not possible here");
3307
3308 case NameClassificationKind::Type:
3309 case NameClassificationKind::TypeTemplate:
3310 case NameClassificationKind::UndeclaredNonType:
3311 case NameClassificationKind::UndeclaredTemplate:
3312 // Not a previously-declared non-type entity.
3313 MightBeDeclarator = false;
3314 break;
3315
3316 case NameClassificationKind::Unknown:
3317 case NameClassificationKind::NonType:
3318 case NameClassificationKind::DependentNonType:
3319 case NameClassificationKind::OverloadSet:
3320 case NameClassificationKind::VarTemplate:
3321 case NameClassificationKind::FunctionTemplate:
3322 case NameClassificationKind::Concept:
3323 // Might be a redeclaration of a prior entity.
3324 break;
3325 }
3326 }
3327 }
3328
3329 if (MightBeDeclarator)
3330 return false;
3331
3332 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
3333 Diag(Loc: PP.getLocForEndOfToken(Loc: DS.getRepAsDecl()->getEndLoc()),
3334 DiagID: diag::err_expected_after)
3335 << DeclSpec::getSpecifierName(T: DS.getTypeSpecType(), Policy: PPol) << tok::semi;
3336
3337 // Try to recover from the typo, by dropping the tag definition and parsing
3338 // the problematic tokens as a type.
3339 //
3340 // FIXME: Split the DeclSpec into pieces for the standalone
3341 // declaration and pieces for the following declaration, instead
3342 // of assuming that all the other pieces attach to new declaration,
3343 // and call ParsedFreeStandingDeclSpec as appropriate.
3344 DS.ClearTypeSpecType();
3345 ParsedTemplateInfo NotATemplate;
3346 ParseDeclarationSpecifiers(DS, TemplateInfo&: NotATemplate, AS, DSC: DSContext, LateAttrs);
3347 return false;
3348}
3349
3350void Parser::ParseDeclarationSpecifiers(
3351 DeclSpec &DS, ParsedTemplateInfo &TemplateInfo, AccessSpecifier AS,
3352 DeclSpecContext DSContext, LateParsedAttrList *LateAttrs,
3353 ImplicitTypenameContext AllowImplicitTypename) {
3354 if (DS.getSourceRange().isInvalid()) {
3355 // Start the range at the current token but make the end of the range
3356 // invalid. This will make the entire range invalid unless we successfully
3357 // consume a token.
3358 DS.SetRangeStart(Tok.getLocation());
3359 DS.SetRangeEnd(SourceLocation());
3360 }
3361
3362 // If we are in a operator context, convert it back into a type specifier
3363 // context for better error handling later on.
3364 if (DSContext == DeclSpecContext::DSC_conv_operator) {
3365 // No implicit typename here.
3366 AllowImplicitTypename = ImplicitTypenameContext::No;
3367 DSContext = DeclSpecContext::DSC_type_specifier;
3368 }
3369
3370 bool EnteringContext = (DSContext == DeclSpecContext::DSC_class ||
3371 DSContext == DeclSpecContext::DSC_top_level);
3372 bool AttrsLastTime = false;
3373 ParsedAttributes attrs(AttrFactory);
3374 // We use Sema's policy to get bool macros right.
3375 PrintingPolicy Policy = Actions.getPrintingPolicy();
3376 while (true) {
3377 bool isInvalid = false;
3378 bool isStorageClass = false;
3379 const char *PrevSpec = nullptr;
3380 unsigned DiagID = 0;
3381
3382 // This value needs to be set to the location of the last token if the last
3383 // token of the specifier is already consumed.
3384 SourceLocation ConsumedEnd;
3385
3386 // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
3387 // implementation for VS2013 uses _Atomic as an identifier for one of the
3388 // classes in <atomic>.
3389 //
3390 // A typedef declaration containing _Atomic<...> is among the places where
3391 // the class is used. If we are currently parsing such a declaration, treat
3392 // the token as an identifier.
3393 if (getLangOpts().MSVCCompat && Tok.is(K: tok::kw__Atomic) &&
3394 DS.getStorageClassSpec() == clang::DeclSpec::SCS_typedef &&
3395 !DS.hasTypeSpecifier() && GetLookAheadToken(N: 1).is(K: tok::less))
3396 Tok.setKind(tok::identifier);
3397
3398 SourceLocation Loc = Tok.getLocation();
3399
3400 // Helper for image types in OpenCL.
3401 auto handleOpenCLImageKW = [&] (StringRef Ext, TypeSpecifierType ImageTypeSpec) {
3402 // Check if the image type is supported and otherwise turn the keyword into an identifier
3403 // because image types from extensions are not reserved identifiers.
3404 if (!StringRef(Ext).empty() && !getActions().getOpenCLOptions().isSupported(Ext, LO: getLangOpts())) {
3405 Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
3406 Tok.setKind(tok::identifier);
3407 return false;
3408 }
3409 isInvalid = DS.SetTypeSpecType(T: ImageTypeSpec, Loc, PrevSpec, DiagID, Policy);
3410 return true;
3411 };
3412
3413 // Turn off usual access checking for template specializations and
3414 // instantiations.
3415 bool IsTemplateSpecOrInst =
3416 (TemplateInfo.Kind == ParsedTemplateKind::ExplicitInstantiation ||
3417 TemplateInfo.Kind == ParsedTemplateKind::ExplicitSpecialization);
3418
3419 switch (Tok.getKind()) {
3420 default:
3421 if (Tok.isRegularKeywordAttribute())
3422 goto Attribute;
3423
3424 DoneWithDeclSpec:
3425 if (!AttrsLastTime)
3426 ProhibitAttributes(Attrs&: attrs);
3427 else {
3428 // Reject C++11 / C23 attributes that aren't type attributes.
3429 for (const ParsedAttr &PA : attrs) {
3430 if (!PA.isCXX11Attribute() && !PA.isC23Attribute() &&
3431 !PA.isRegularKeywordAttribute())
3432 continue;
3433 if (PA.getKind() == ParsedAttr::UnknownAttribute)
3434 // We will warn about the unknown attribute elsewhere (in
3435 // SemaDeclAttr.cpp)
3436 continue;
3437 // GCC ignores this attribute when placed on the DeclSpec in [[]]
3438 // syntax, so we do the same.
3439 if (PA.getKind() == ParsedAttr::AT_VectorSize) {
3440 Diag(Loc: PA.getLoc(), DiagID: diag::warn_attribute_ignored) << PA;
3441 PA.setInvalid();
3442 continue;
3443 }
3444 // We reject AT_LifetimeBound and AT_AnyX86NoCfCheck, even though they
3445 // are type attributes, because we historically haven't allowed these
3446 // to be used as type attributes in C++11 / C23 syntax.
3447 if (PA.isTypeAttr() && PA.getKind() != ParsedAttr::AT_LifetimeBound &&
3448 PA.getKind() != ParsedAttr::AT_AnyX86NoCfCheck)
3449 continue;
3450
3451 if (PA.getKind() == ParsedAttr::AT_LifetimeBound)
3452 Diag(Loc: PA.getLoc(), DiagID: diag::err_attribute_wrong_decl_type)
3453 << PA << PA.isRegularKeywordAttribute()
3454 << ExpectedParameterOrImplicitObjectParameter;
3455 else
3456 Diag(Loc: PA.getLoc(), DiagID: diag::err_attribute_not_type_attr)
3457 << PA << PA.isRegularKeywordAttribute();
3458 PA.setInvalid();
3459 }
3460
3461 DS.takeAttributesFrom(attrs);
3462 }
3463
3464 // If this is not a declaration specifier token, we're done reading decl
3465 // specifiers. First verify that DeclSpec's are consistent.
3466 DS.Finish(S&: Actions, Policy);
3467 return;
3468
3469 // alignment-specifier
3470 case tok::kw__Alignas:
3471 diagnoseUseOfC11Keyword(Tok);
3472 [[fallthrough]];
3473 case tok::kw_alignas:
3474 // _Alignas and alignas (C23, not C++) should parse the same way. The C++
3475 // parsing for alignas happens through the usual attribute parsing. This
3476 // ensures that an alignas specifier can appear in a type position in C
3477 // despite that not being valid in C++.
3478 if (getLangOpts().C23 || Tok.getKind() == tok::kw__Alignas) {
3479 if (Tok.getKind() == tok::kw_alignas)
3480 Diag(Tok, DiagID: diag::warn_c23_compat_keyword) << Tok.getName();
3481 ParseAlignmentSpecifier(Attrs&: DS.getAttributes());
3482 continue;
3483 }
3484 [[fallthrough]];
3485 case tok::l_square:
3486 if (!isAllowedCXX11AttributeSpecifier())
3487 goto DoneWithDeclSpec;
3488
3489 Attribute:
3490 ProhibitAttributes(Attrs&: attrs);
3491 // FIXME: It would be good to recover by accepting the attributes,
3492 // but attempting to do that now would cause serious
3493 // madness in terms of diagnostics.
3494 attrs.clear();
3495 attrs.Range = SourceRange();
3496
3497 ParseCXX11Attributes(attrs);
3498 AttrsLastTime = true;
3499 continue;
3500
3501 case tok::code_completion: {
3502 SemaCodeCompletion::ParserCompletionContext CCC =
3503 SemaCodeCompletion::PCC_Namespace;
3504 if (DS.hasTypeSpecifier()) {
3505 bool AllowNonIdentifiers
3506 = (getCurScope()->getFlags() & (Scope::ControlScope |
3507 Scope::BlockScope |
3508 Scope::TemplateParamScope |
3509 Scope::FunctionPrototypeScope |
3510 Scope::AtCatchScope)) == 0;
3511 bool AllowNestedNameSpecifiers
3512 = DSContext == DeclSpecContext::DSC_top_level ||
3513 (DSContext == DeclSpecContext::DSC_class && DS.isFriendSpecified());
3514
3515 cutOffParsing();
3516 Actions.CodeCompletion().CodeCompleteDeclSpec(
3517 S: getCurScope(), DS, AllowNonIdentifiers, AllowNestedNameSpecifiers);
3518 return;
3519 }
3520
3521 // Class context can appear inside a function/block, so prioritise that.
3522 if (TemplateInfo.Kind != ParsedTemplateKind::NonTemplate)
3523 CCC = DSContext == DeclSpecContext::DSC_class
3524 ? SemaCodeCompletion::PCC_MemberTemplate
3525 : SemaCodeCompletion::PCC_Template;
3526 else if (DSContext == DeclSpecContext::DSC_class)
3527 CCC = SemaCodeCompletion::PCC_Class;
3528 else if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
3529 CCC = SemaCodeCompletion::PCC_LocalDeclarationSpecifiers;
3530 else if (CurParsedObjCImpl)
3531 CCC = SemaCodeCompletion::PCC_ObjCImplementation;
3532
3533 cutOffParsing();
3534 Actions.CodeCompletion().CodeCompleteOrdinaryName(S: getCurScope(), CompletionContext: CCC);
3535 return;
3536 }
3537
3538 case tok::coloncolon: // ::foo::bar
3539 // C++ scope specifier. Annotate and loop, or bail out on error.
3540 if (getLangOpts().CPlusPlus &&
3541 TryAnnotateCXXScopeToken(EnteringContext)) {
3542 if (!DS.hasTypeSpecifier())
3543 DS.SetTypeSpecError();
3544 goto DoneWithDeclSpec;
3545 }
3546 if (Tok.is(K: tok::coloncolon)) // ::new or ::delete
3547 goto DoneWithDeclSpec;
3548 continue;
3549
3550 case tok::annot_cxxscope: {
3551 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
3552 goto DoneWithDeclSpec;
3553
3554 CXXScopeSpec SS;
3555 if (TemplateInfo.TemplateParams)
3556 SS.setTemplateParamLists(*TemplateInfo.TemplateParams);
3557 Actions.RestoreNestedNameSpecifierAnnotation(Annotation: Tok.getAnnotationValue(),
3558 AnnotationRange: Tok.getAnnotationRange(),
3559 SS);
3560
3561 // We are looking for a qualified typename.
3562 Token Next = NextToken();
3563
3564 TemplateIdAnnotation *TemplateId = Next.is(K: tok::annot_template_id)
3565 ? takeTemplateIdAnnotation(tok: Next)
3566 : nullptr;
3567 if (TemplateId && TemplateId->hasInvalidName()) {
3568 // We found something like 'T::U<Args> x', but U is not a template.
3569 // Assume it was supposed to be a type.
3570 DS.SetTypeSpecError();
3571 ConsumeAnnotationToken();
3572 break;
3573 }
3574
3575 if (TemplateId && TemplateId->Kind == TNK_Type_template) {
3576 // We have a qualified template-id, e.g., N::A<int>
3577
3578 // If this would be a valid constructor declaration with template
3579 // arguments, we will reject the attempt to form an invalid type-id
3580 // referring to the injected-class-name when we annotate the token,
3581 // per C++ [class.qual]p2.
3582 //
3583 // To improve diagnostics for this case, parse the declaration as a
3584 // constructor (and reject the extra template arguments later).
3585 if ((DSContext == DeclSpecContext::DSC_top_level ||
3586 DSContext == DeclSpecContext::DSC_class) &&
3587 TemplateId->Name &&
3588 Actions.isCurrentClassName(II: *TemplateId->Name, S: getCurScope(), SS: &SS) &&
3589 isConstructorDeclarator(/*Unqualified=*/false,
3590 /*DeductionGuide=*/false,
3591 IsFriend: DS.isFriendSpecified())) {
3592 // The user meant this to be an out-of-line constructor
3593 // definition, but template arguments are not allowed
3594 // there. Just allow this as a constructor; we'll
3595 // complain about it later.
3596 goto DoneWithDeclSpec;
3597 }
3598
3599 DS.getTypeSpecScope() = SS;
3600 ConsumeAnnotationToken(); // The C++ scope.
3601 assert(Tok.is(tok::annot_template_id) &&
3602 "ParseOptionalCXXScopeSpecifier not working");
3603 AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename);
3604 continue;
3605 }
3606
3607 if (TemplateId && TemplateId->Kind == TNK_Concept_template) {
3608 DS.getTypeSpecScope() = SS;
3609 // This is probably a qualified placeholder-specifier, e.g., ::C<int>
3610 // auto ... Consume the scope annotation and continue to consume the
3611 // template-id as a placeholder-specifier. Let the next iteration
3612 // diagnose a missing auto.
3613 ConsumeAnnotationToken();
3614 continue;
3615 }
3616
3617 if (Next.is(K: tok::annot_typename)) {
3618 DS.getTypeSpecScope() = SS;
3619 ConsumeAnnotationToken(); // The C++ scope.
3620 TypeResult T = getTypeAnnotation(Tok);
3621 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_typename,
3622 Loc: Tok.getAnnotationEndLoc(),
3623 PrevSpec, DiagID, Rep: T, Policy);
3624 if (isInvalid)
3625 break;
3626 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
3627 ConsumeAnnotationToken(); // The typename
3628 }
3629
3630 if (AllowImplicitTypename == ImplicitTypenameContext::Yes &&
3631 Next.is(K: tok::annot_template_id) &&
3632 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
3633 ->Kind == TNK_Dependent_template_name) {
3634 DS.getTypeSpecScope() = SS;
3635 ConsumeAnnotationToken(); // The C++ scope.
3636 AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename);
3637 continue;
3638 }
3639
3640 if (Next.isNot(K: tok::identifier))
3641 goto DoneWithDeclSpec;
3642
3643 // Check whether this is a constructor declaration. If we're in a
3644 // context where the identifier could be a class name, and it has the
3645 // shape of a constructor declaration, process it as one.
3646 if ((DSContext == DeclSpecContext::DSC_top_level ||
3647 DSContext == DeclSpecContext::DSC_class) &&
3648 Actions.isCurrentClassName(II: *Next.getIdentifierInfo(), S: getCurScope(),
3649 SS: &SS) &&
3650 isConstructorDeclarator(/*Unqualified=*/false,
3651 /*DeductionGuide=*/false,
3652 IsFriend: DS.isFriendSpecified(),
3653 TemplateInfo: &TemplateInfo))
3654 goto DoneWithDeclSpec;
3655
3656 // C++20 [temp.spec] 13.9/6.
3657 // This disables the access checking rules for function template explicit
3658 // instantiation and explicit specialization:
3659 // - `return type`.
3660 SuppressAccessChecks SAC(*this, IsTemplateSpecOrInst);
3661
3662 ParsedType TypeRep = Actions.getTypeName(
3663 II: *Next.getIdentifierInfo(), NameLoc: Next.getLocation(), S: getCurScope(), SS: &SS,
3664 isClassName: false, HasTrailingDot: false, ObjectType: nullptr,
3665 /*IsCtorOrDtorName=*/false,
3666 /*WantNontrivialTypeSourceInfo=*/true,
3667 IsClassTemplateDeductionContext: isClassTemplateDeductionContext(DSC: DSContext), AllowImplicitTypename);
3668
3669 if (IsTemplateSpecOrInst)
3670 SAC.done();
3671
3672 // If the referenced identifier is not a type, then this declspec is
3673 // erroneous: We already checked about that it has no type specifier, and
3674 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
3675 // typename.
3676 if (!TypeRep) {
3677 if (TryAnnotateTypeConstraint())
3678 goto DoneWithDeclSpec;
3679 if (Tok.isNot(K: tok::annot_cxxscope) ||
3680 NextToken().isNot(K: tok::identifier))
3681 continue;
3682 // Eat the scope spec so the identifier is current.
3683 ConsumeAnnotationToken();
3684 ParsedAttributes Attrs(AttrFactory);
3685 if (ParseImplicitInt(DS, SS: &SS, TemplateInfo, AS, DSC: DSContext, Attrs)) {
3686 if (!Attrs.empty()) {
3687 AttrsLastTime = true;
3688 attrs.takeAllFrom(Other&: Attrs);
3689 }
3690 continue;
3691 }
3692 goto DoneWithDeclSpec;
3693 }
3694
3695 DS.getTypeSpecScope() = SS;
3696 ConsumeAnnotationToken(); // The C++ scope.
3697
3698 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_typename, Loc, PrevSpec,
3699 DiagID, Rep: TypeRep, Policy);
3700 if (isInvalid)
3701 break;
3702
3703 DS.SetRangeEnd(Tok.getLocation());
3704 ConsumeToken(); // The typename.
3705
3706 continue;
3707 }
3708
3709 case tok::annot_typename: {
3710 // If we've previously seen a tag definition, we were almost surely
3711 // missing a semicolon after it.
3712 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
3713 goto DoneWithDeclSpec;
3714
3715 TypeResult T = getTypeAnnotation(Tok);
3716 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_typename, Loc, PrevSpec,
3717 DiagID, Rep: T, Policy);
3718 if (isInvalid)
3719 break;
3720
3721 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
3722 ConsumeAnnotationToken(); // The typename
3723
3724 continue;
3725 }
3726
3727 case tok::kw___is_signed:
3728 // HACK: before 2022-12, libstdc++ uses __is_signed as an identifier,
3729 // but Clang typically treats it as a trait.
3730 // If we see __is_signed as it appears in libstdc++, e.g.,
3731 //
3732 // static const bool __is_signed;
3733 //
3734 // then treat __is_signed as an identifier rather than as a keyword.
3735 // This was fixed by libstdc++ in December 2022.
3736 if (DS.getTypeSpecType() == TST_bool &&
3737 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
3738 DS.getStorageClassSpec() == DeclSpec::SCS_static)
3739 TryKeywordIdentFallback(DisableKeyword: true);
3740
3741 // We're done with the declaration-specifiers.
3742 goto DoneWithDeclSpec;
3743
3744 // typedef-name
3745 case tok::kw___super:
3746 case tok::kw_decltype:
3747 case tok::identifier:
3748 ParseIdentifier: {
3749 // This identifier can only be a typedef name if we haven't already seen
3750 // a type-specifier. Without this check we misparse:
3751 // typedef int X; struct Y { short X; }; as 'short int'.
3752 if (DS.hasTypeSpecifier())
3753 goto DoneWithDeclSpec;
3754
3755 // If the token is an identifier named "__declspec" and Microsoft
3756 // extensions are not enabled, it is likely that there will be cascading
3757 // parse errors if this really is a __declspec attribute. Attempt to
3758 // recognize that scenario and recover gracefully.
3759 if (!getLangOpts().DeclSpecKeyword && Tok.is(K: tok::identifier) &&
3760 Tok.getIdentifierInfo()->getName() == "__declspec") {
3761 Diag(Loc, DiagID: diag::err_ms_attributes_not_enabled);
3762
3763 // The next token should be an open paren. If it is, eat the entire
3764 // attribute declaration and continue.
3765 if (NextToken().is(K: tok::l_paren)) {
3766 // Consume the __declspec identifier.
3767 ConsumeToken();
3768
3769 // Eat the parens and everything between them.
3770 BalancedDelimiterTracker T(*this, tok::l_paren);
3771 if (T.consumeOpen()) {
3772 assert(false && "Not a left paren?");
3773 return;
3774 }
3775 T.skipToEnd();
3776 continue;
3777 }
3778 }
3779
3780 // In C++, check to see if this is a scope specifier like foo::bar::, if
3781 // so handle it as such. This is important for ctor parsing.
3782 if (getLangOpts().CPlusPlus) {
3783 // C++20 [temp.spec] 13.9/6.
3784 // This disables the access checking rules for function template
3785 // explicit instantiation and explicit specialization:
3786 // - `return type`.
3787 SuppressAccessChecks SAC(*this, IsTemplateSpecOrInst);
3788
3789 const bool Success = TryAnnotateCXXScopeToken(EnteringContext);
3790
3791 if (IsTemplateSpecOrInst)
3792 SAC.done();
3793
3794 if (Success) {
3795 if (IsTemplateSpecOrInst)
3796 SAC.redelay();
3797 DS.SetTypeSpecError();
3798 goto DoneWithDeclSpec;
3799 }
3800
3801 if (!Tok.is(K: tok::identifier))
3802 continue;
3803 }
3804
3805 // Check for need to substitute AltiVec keyword tokens.
3806 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
3807 break;
3808
3809 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
3810 // allow the use of a typedef name as a type specifier.
3811 if (DS.isTypeAltiVecVector())
3812 goto DoneWithDeclSpec;
3813
3814 if (DSContext == DeclSpecContext::DSC_objc_method_result &&
3815 isObjCInstancetype()) {
3816 ParsedType TypeRep = Actions.ObjC().ActOnObjCInstanceType(Loc);
3817 assert(TypeRep);
3818 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_typename, Loc, PrevSpec,
3819 DiagID, Rep: TypeRep, Policy);
3820 if (isInvalid)
3821 break;
3822
3823 DS.SetRangeEnd(Loc);
3824 ConsumeToken();
3825 continue;
3826 }
3827
3828 // If we're in a context where the identifier could be a class name,
3829 // check whether this is a constructor declaration.
3830 if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class &&
3831 Actions.isCurrentClassName(II: *Tok.getIdentifierInfo(), S: getCurScope()) &&
3832 isConstructorDeclarator(/*Unqualified=*/true,
3833 /*DeductionGuide=*/false,
3834 IsFriend: DS.isFriendSpecified()))
3835 goto DoneWithDeclSpec;
3836
3837 ParsedType TypeRep = Actions.getTypeName(
3838 II: *Tok.getIdentifierInfo(), NameLoc: Tok.getLocation(), S: getCurScope(), SS: nullptr,
3839 isClassName: false, HasTrailingDot: false, ObjectType: nullptr, IsCtorOrDtorName: false, WantNontrivialTypeSourceInfo: false,
3840 IsClassTemplateDeductionContext: isClassTemplateDeductionContext(DSC: DSContext));
3841
3842 // If this is not a typedef name, don't parse it as part of the declspec,
3843 // it must be an implicit int or an error.
3844 if (!TypeRep) {
3845 if (TryAnnotateTypeConstraint())
3846 goto DoneWithDeclSpec;
3847 if (Tok.isNot(K: tok::identifier))
3848 continue;
3849 ParsedAttributes Attrs(AttrFactory);
3850 if (ParseImplicitInt(DS, SS: nullptr, TemplateInfo, AS, DSC: DSContext, Attrs)) {
3851 if (!Attrs.empty()) {
3852 AttrsLastTime = true;
3853 attrs.takeAllFrom(Other&: Attrs);
3854 }
3855 continue;
3856 }
3857 goto DoneWithDeclSpec;
3858 }
3859
3860 // Likewise, if this is a context where the identifier could be a template
3861 // name, check whether this is a deduction guide declaration.
3862 CXXScopeSpec SS;
3863 if (getLangOpts().CPlusPlus17 &&
3864 (DSContext == DeclSpecContext::DSC_class ||
3865 DSContext == DeclSpecContext::DSC_top_level) &&
3866 Actions.isDeductionGuideName(S: getCurScope(), Name: *Tok.getIdentifierInfo(),
3867 NameLoc: Tok.getLocation(), SS) &&
3868 isConstructorDeclarator(/*Unqualified*/ true,
3869 /*DeductionGuide*/ true))
3870 goto DoneWithDeclSpec;
3871
3872 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_typename, Loc, PrevSpec,
3873 DiagID, Rep: TypeRep, Policy);
3874 if (isInvalid)
3875 break;
3876
3877 DS.SetRangeEnd(Tok.getLocation());
3878 ConsumeToken(); // The identifier
3879
3880 // Objective-C supports type arguments and protocol references
3881 // following an Objective-C object or object pointer
3882 // type. Handle either one of them.
3883 if (Tok.is(K: tok::less) && getLangOpts().ObjC) {
3884 SourceLocation NewEndLoc;
3885 TypeResult NewTypeRep = parseObjCTypeArgsAndProtocolQualifiers(
3886 loc: Loc, type: TypeRep, /*consumeLastToken=*/true,
3887 endLoc&: NewEndLoc);
3888 if (NewTypeRep.isUsable()) {
3889 DS.UpdateTypeRep(Rep: NewTypeRep.get());
3890 DS.SetRangeEnd(NewEndLoc);
3891 }
3892 }
3893
3894 // Need to support trailing type qualifiers (e.g. "id<p> const").
3895 // If a type specifier follows, it will be diagnosed elsewhere.
3896 continue;
3897 }
3898
3899 // type-name or placeholder-specifier
3900 case tok::annot_template_id: {
3901 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(tok: Tok);
3902
3903 if (TemplateId->hasInvalidName()) {
3904 DS.SetTypeSpecError();
3905 break;
3906 }
3907
3908 if (TemplateId->Kind == TNK_Concept_template) {
3909 // If we've already diagnosed that this type-constraint has invalid
3910 // arguments, drop it and just form 'auto' or 'decltype(auto)'.
3911 if (TemplateId->hasInvalidArgs())
3912 TemplateId = nullptr;
3913
3914 // Any of the following tokens are likely the start of the user
3915 // forgetting 'auto' or 'decltype(auto)', so diagnose.
3916 // Note: if updating this list, please make sure we update
3917 // isCXXDeclarationSpecifier's check for IsPlaceholderSpecifier to have
3918 // a matching list.
3919 if (NextToken().isOneOf(Ks: tok::identifier, Ks: tok::kw_const,
3920 Ks: tok::kw_volatile, Ks: tok::kw_restrict, Ks: tok::amp,
3921 Ks: tok::ampamp)) {
3922 Diag(Loc, DiagID: diag::err_placeholder_expected_auto_or_decltype_auto)
3923 << FixItHint::CreateInsertion(InsertionLoc: NextToken().getLocation(), Code: "auto");
3924 // Attempt to continue as if 'auto' was placed here.
3925 isInvalid = DS.SetTypeSpecType(T: TST_auto, Loc, PrevSpec, DiagID,
3926 Rep: TemplateId, Policy);
3927 break;
3928 }
3929 if (!NextToken().isOneOf(Ks: tok::kw_auto, Ks: tok::kw_decltype))
3930 goto DoneWithDeclSpec;
3931
3932 if (TemplateId && !isInvalid && Actions.CheckTypeConstraint(TypeConstraint: TemplateId))
3933 TemplateId = nullptr;
3934
3935 ConsumeAnnotationToken();
3936 SourceLocation AutoLoc = Tok.getLocation();
3937 if (TryConsumeToken(Expected: tok::kw_decltype)) {
3938 BalancedDelimiterTracker Tracker(*this, tok::l_paren);
3939 if (Tracker.consumeOpen()) {
3940 // Something like `void foo(Iterator decltype i)`
3941 Diag(Tok, DiagID: diag::err_expected) << tok::l_paren;
3942 } else {
3943 if (!TryConsumeToken(Expected: tok::kw_auto)) {
3944 // Something like `void foo(Iterator decltype(int) i)`
3945 Tracker.skipToEnd();
3946 Diag(Tok, DiagID: diag::err_placeholder_expected_auto_or_decltype_auto)
3947 << FixItHint::CreateReplacement(RemoveRange: SourceRange(AutoLoc,
3948 Tok.getLocation()),
3949 Code: "auto");
3950 } else {
3951 Tracker.consumeClose();
3952 }
3953 }
3954 ConsumedEnd = Tok.getLocation();
3955 DS.setTypeArgumentRange(Tracker.getRange());
3956 // Even if something went wrong above, continue as if we've seen
3957 // `decltype(auto)`.
3958 isInvalid = DS.SetTypeSpecType(T: TST_decltype_auto, Loc, PrevSpec,
3959 DiagID, Rep: TemplateId, Policy);
3960 } else {
3961 isInvalid = DS.SetTypeSpecType(T: TST_auto, Loc: AutoLoc, PrevSpec, DiagID,
3962 Rep: TemplateId, Policy);
3963 }
3964 break;
3965 }
3966
3967 if (TemplateId->Kind != TNK_Type_template &&
3968 TemplateId->Kind != TNK_Undeclared_template) {
3969 // This template-id does not refer to a type name, so we're
3970 // done with the type-specifiers.
3971 goto DoneWithDeclSpec;
3972 }
3973
3974 // If we're in a context where the template-id could be a
3975 // constructor name or specialization, check whether this is a
3976 // constructor declaration.
3977 if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class &&
3978 Actions.isCurrentClassName(II: *TemplateId->Name, S: getCurScope()) &&
3979 isConstructorDeclarator(/*Unqualified=*/true,
3980 /*DeductionGuide=*/false,
3981 IsFriend: DS.isFriendSpecified()))
3982 goto DoneWithDeclSpec;
3983
3984 // Turn the template-id annotation token into a type annotation
3985 // token, then try again to parse it as a type-specifier.
3986 CXXScopeSpec SS;
3987 AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename);
3988 continue;
3989 }
3990
3991 // Attributes support.
3992 case tok::kw___attribute:
3993 case tok::kw___declspec:
3994 ParseAttributes(WhichAttrKinds: PAKM_GNU | PAKM_Declspec, Attrs&: DS.getAttributes(), LateAttrs);
3995 continue;
3996
3997 // Microsoft single token adornments.
3998 case tok::kw___forceinline: {
3999 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
4000 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
4001 SourceLocation AttrNameLoc = Tok.getLocation();
4002 DS.getAttributes().addNew(attrName: AttrName, attrRange: AttrNameLoc, scope: AttributeScopeInfo(),
4003 args: nullptr, numArgs: 0, form: tok::kw___forceinline);
4004 break;
4005 }
4006
4007 case tok::kw___unaligned:
4008 isInvalid = DS.SetTypeQual(T: DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID,
4009 Lang: getLangOpts());
4010 break;
4011
4012 // __ptrauth qualifier.
4013 case tok::kw___ptrauth:
4014 ParsePtrauthQualifier(Attrs&: DS.getAttributes());
4015 continue;
4016
4017 case tok::kw___sptr:
4018 case tok::kw___uptr:
4019 case tok::kw___ptr64:
4020 case tok::kw___ptr32:
4021 case tok::kw___w64:
4022 case tok::kw___cdecl:
4023 case tok::kw___stdcall:
4024 case tok::kw___fastcall:
4025 case tok::kw___thiscall:
4026 case tok::kw___regcall:
4027 case tok::kw___vectorcall:
4028 ParseMicrosoftTypeAttributes(attrs&: DS.getAttributes());
4029 continue;
4030
4031 case tok::kw___funcref:
4032 ParseWebAssemblyFuncrefTypeAttribute(attrs&: DS.getAttributes());
4033 continue;
4034
4035 // Borland single token adornments.
4036 case tok::kw___pascal:
4037 ParseBorlandTypeAttributes(attrs&: DS.getAttributes());
4038 continue;
4039
4040 // OpenCL single token adornments.
4041 case tok::kw___kernel:
4042 ParseOpenCLKernelAttributes(attrs&: DS.getAttributes());
4043 continue;
4044
4045 // CUDA/HIP single token adornments.
4046 case tok::kw___noinline__:
4047 ParseCUDAFunctionAttributes(attrs&: DS.getAttributes());
4048 continue;
4049
4050 // Nullability type specifiers.
4051 case tok::kw__Nonnull:
4052 case tok::kw__Nullable:
4053 case tok::kw__Nullable_result:
4054 case tok::kw__Null_unspecified:
4055 ParseNullabilityTypeSpecifiers(attrs&: DS.getAttributes());
4056 continue;
4057
4058 // Objective-C 'kindof' types.
4059 case tok::kw___kindof:
4060 DS.getAttributes().addNew(attrName: Tok.getIdentifierInfo(), attrRange: Loc,
4061 scope: AttributeScopeInfo(), args: nullptr, numArgs: 0,
4062 form: tok::kw___kindof);
4063 (void)ConsumeToken();
4064 continue;
4065
4066 // storage-class-specifier
4067 case tok::kw_typedef:
4068 isInvalid = DS.SetStorageClassSpec(S&: Actions, SC: DeclSpec::SCS_typedef, Loc,
4069 PrevSpec, DiagID, Policy);
4070 isStorageClass = true;
4071 break;
4072 case tok::kw_extern:
4073 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
4074 Diag(Tok, DiagID: diag::ext_thread_before) << "extern";
4075 isInvalid = DS.SetStorageClassSpec(S&: Actions, SC: DeclSpec::SCS_extern, Loc,
4076 PrevSpec, DiagID, Policy);
4077 isStorageClass = true;
4078 break;
4079 case tok::kw___private_extern__:
4080 isInvalid = DS.SetStorageClassSpec(S&: Actions, SC: DeclSpec::SCS_private_extern,
4081 Loc, PrevSpec, DiagID, Policy);
4082 isStorageClass = true;
4083 break;
4084 case tok::kw_static:
4085 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
4086 Diag(Tok, DiagID: diag::ext_thread_before) << "static";
4087 isInvalid = DS.SetStorageClassSpec(S&: Actions, SC: DeclSpec::SCS_static, Loc,
4088 PrevSpec, DiagID, Policy);
4089 isStorageClass = true;
4090 break;
4091 case tok::kw_auto:
4092 if (getLangOpts().CPlusPlus11 || getLangOpts().C23) {
4093 if (isKnownToBeTypeSpecifier(Tok: GetLookAheadToken(N: 1))) {
4094 isInvalid = DS.SetStorageClassSpec(S&: Actions, SC: DeclSpec::SCS_auto, Loc,
4095 PrevSpec, DiagID, Policy);
4096 if (!isInvalid && !getLangOpts().C23)
4097 Diag(Tok, DiagID: diag::ext_auto_storage_class)
4098 << FixItHint::CreateRemoval(RemoveRange: DS.getStorageClassSpecLoc());
4099 } else
4100 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_auto, Loc, PrevSpec,
4101 DiagID, Policy);
4102 } else
4103 isInvalid = DS.SetStorageClassSpec(S&: Actions, SC: DeclSpec::SCS_auto, Loc,
4104 PrevSpec, DiagID, Policy);
4105 isStorageClass = true;
4106 break;
4107 case tok::kw___auto_type:
4108 Diag(Tok, DiagID: diag::ext_auto_type);
4109 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_auto_type, Loc, PrevSpec,
4110 DiagID, Policy);
4111 break;
4112 case tok::kw_register:
4113 isInvalid = DS.SetStorageClassSpec(S&: Actions, SC: DeclSpec::SCS_register, Loc,
4114 PrevSpec, DiagID, Policy);
4115 isStorageClass = true;
4116 break;
4117 case tok::kw_mutable:
4118 isInvalid = DS.SetStorageClassSpec(S&: Actions, SC: DeclSpec::SCS_mutable, Loc,
4119 PrevSpec, DiagID, Policy);
4120 isStorageClass = true;
4121 break;
4122 case tok::kw___thread:
4123 isInvalid = DS.SetStorageClassSpecThread(TSC: DeclSpec::TSCS___thread, Loc,
4124 PrevSpec, DiagID);
4125 isStorageClass = true;
4126 break;
4127 case tok::kw_thread_local:
4128 if (getLangOpts().C23)
4129 Diag(Tok, DiagID: diag::warn_c23_compat_keyword) << Tok.getName();
4130 // We map thread_local to _Thread_local in C23 mode so it retains the C
4131 // semantics rather than getting the C++ semantics.
4132 // FIXME: diagnostics will show _Thread_local when the user wrote
4133 // thread_local in source in C23 mode; we need some general way to
4134 // identify which way the user spelled the keyword in source.
4135 isInvalid = DS.SetStorageClassSpecThread(
4136 TSC: getLangOpts().C23 ? DeclSpec::TSCS__Thread_local
4137 : DeclSpec::TSCS_thread_local,
4138 Loc, PrevSpec, DiagID);
4139 isStorageClass = true;
4140 break;
4141 case tok::kw__Thread_local:
4142 diagnoseUseOfC11Keyword(Tok);
4143 isInvalid = DS.SetStorageClassSpecThread(TSC: DeclSpec::TSCS__Thread_local,
4144 Loc, PrevSpec, DiagID);
4145 isStorageClass = true;
4146 break;
4147
4148 // function-specifier
4149 case tok::kw_inline:
4150 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
4151 break;
4152 case tok::kw_virtual:
4153 // C++ for OpenCL does not allow virtual function qualifier, to avoid
4154 // function pointers restricted in OpenCL v2.0 s6.9.a.
4155 if (getLangOpts().OpenCLCPlusPlus &&
4156 !getActions().getOpenCLOptions().isAvailableOption(
4157 Ext: "__cl_clang_function_pointers", LO: getLangOpts())) {
4158 DiagID = diag::err_openclcxx_virtual_function;
4159 PrevSpec = Tok.getIdentifierInfo()->getNameStart();
4160 isInvalid = true;
4161 } else if (getLangOpts().HLSL) {
4162 DiagID = diag::err_hlsl_virtual_function;
4163 PrevSpec = Tok.getIdentifierInfo()->getNameStart();
4164 isInvalid = true;
4165 } else {
4166 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
4167 }
4168 break;
4169 case tok::kw_explicit: {
4170 SourceLocation ExplicitLoc = Loc;
4171 SourceLocation CloseParenLoc;
4172 ExplicitSpecifier ExplicitSpec(nullptr, ExplicitSpecKind::ResolvedTrue);
4173 ConsumedEnd = ExplicitLoc;
4174 ConsumeToken(); // kw_explicit
4175 if (Tok.is(K: tok::l_paren)) {
4176 if (getLangOpts().CPlusPlus20 || isExplicitBool() == TPResult::True) {
4177 Diag(Loc: Tok.getLocation(), DiagID: getLangOpts().CPlusPlus20
4178 ? diag::warn_cxx17_compat_explicit_bool
4179 : diag::ext_explicit_bool);
4180
4181 ExprResult ExplicitExpr(static_cast<Expr *>(nullptr));
4182 BalancedDelimiterTracker Tracker(*this, tok::l_paren);
4183 Tracker.consumeOpen();
4184
4185 EnterExpressionEvaluationContext ConstantEvaluated(
4186 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
4187
4188 ExplicitExpr = ParseConstantExpressionInExprEvalContext();
4189 ConsumedEnd = Tok.getLocation();
4190 if (ExplicitExpr.isUsable()) {
4191 CloseParenLoc = Tok.getLocation();
4192 Tracker.consumeClose();
4193 ExplicitSpec =
4194 Actions.ActOnExplicitBoolSpecifier(E: ExplicitExpr.get());
4195 } else
4196 Tracker.skipToEnd();
4197 } else {
4198 Diag(Loc: Tok.getLocation(), DiagID: diag::warn_cxx20_compat_explicit_bool);
4199 }
4200 }
4201 isInvalid = DS.setFunctionSpecExplicit(Loc: ExplicitLoc, PrevSpec, DiagID,
4202 ExplicitSpec, CloseParenLoc);
4203 break;
4204 }
4205 case tok::kw__Noreturn:
4206 diagnoseUseOfC11Keyword(Tok);
4207 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
4208 break;
4209
4210 // friend
4211 case tok::kw_friend:
4212 if (DSContext == DeclSpecContext::DSC_class) {
4213 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
4214 Scope *CurS = getCurScope();
4215 if (!isInvalid && CurS)
4216 CurS->setFlags(CurS->getFlags() | Scope::FriendScope);
4217 } else {
4218 PrevSpec = ""; // not actually used by the diagnostic
4219 DiagID = diag::err_friend_invalid_in_context;
4220 isInvalid = true;
4221 }
4222 break;
4223
4224 // Modules
4225 case tok::kw___module_private__:
4226 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
4227 break;
4228
4229 // constexpr, consteval, constinit specifiers
4230 case tok::kw_constexpr:
4231 if (getLangOpts().C23)
4232 Diag(Tok, DiagID: diag::warn_c23_compat_keyword) << Tok.getName();
4233 isInvalid = DS.SetConstexprSpec(ConstexprKind: ConstexprSpecKind::Constexpr, Loc,
4234 PrevSpec, DiagID);
4235 break;
4236 case tok::kw_consteval:
4237 isInvalid = DS.SetConstexprSpec(ConstexprKind: ConstexprSpecKind::Consteval, Loc,
4238 PrevSpec, DiagID);
4239 break;
4240 case tok::kw_constinit:
4241 isInvalid = DS.SetConstexprSpec(ConstexprKind: ConstexprSpecKind::Constinit, Loc,
4242 PrevSpec, DiagID);
4243 break;
4244
4245 // type-specifier
4246 case tok::kw_short:
4247 isInvalid = DS.SetTypeSpecWidth(W: TypeSpecifierWidth::Short, Loc, PrevSpec,
4248 DiagID, Policy);
4249 break;
4250 case tok::kw_long:
4251 if (DS.getTypeSpecWidth() != TypeSpecifierWidth::Long)
4252 isInvalid = DS.SetTypeSpecWidth(W: TypeSpecifierWidth::Long, Loc, PrevSpec,
4253 DiagID, Policy);
4254 else
4255 isInvalid = DS.SetTypeSpecWidth(W: TypeSpecifierWidth::LongLong, Loc,
4256 PrevSpec, DiagID, Policy);
4257 break;
4258 case tok::kw___int64:
4259 isInvalid = DS.SetTypeSpecWidth(W: TypeSpecifierWidth::LongLong, Loc,
4260 PrevSpec, DiagID, Policy);
4261 break;
4262 case tok::kw_signed:
4263 isInvalid =
4264 DS.SetTypeSpecSign(S: TypeSpecifierSign::Signed, Loc, PrevSpec, DiagID);
4265 break;
4266 case tok::kw_unsigned:
4267 isInvalid = DS.SetTypeSpecSign(S: TypeSpecifierSign::Unsigned, Loc, PrevSpec,
4268 DiagID);
4269 break;
4270 case tok::kw__Complex:
4271 if (!getLangOpts().C99)
4272 Diag(Tok, DiagID: diag::ext_c99_feature) << Tok.getName();
4273 isInvalid = DS.SetTypeSpecComplex(C: DeclSpec::TSC_complex, Loc, PrevSpec,
4274 DiagID);
4275 break;
4276 case tok::kw__Imaginary:
4277 if (!getLangOpts().C99)
4278 Diag(Tok, DiagID: diag::ext_c99_feature) << Tok.getName();
4279 isInvalid = DS.SetTypeSpecComplex(C: DeclSpec::TSC_imaginary, Loc, PrevSpec,
4280 DiagID);
4281 break;
4282 case tok::kw_void:
4283 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_void, Loc, PrevSpec,
4284 DiagID, Policy);
4285 break;
4286 case tok::kw_char:
4287 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_char, Loc, PrevSpec,
4288 DiagID, Policy);
4289 break;
4290 case tok::kw_int:
4291 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_int, Loc, PrevSpec,
4292 DiagID, Policy);
4293 break;
4294 case tok::kw__ExtInt:
4295 case tok::kw__BitInt: {
4296 DiagnoseBitIntUse(Tok);
4297 ExprResult ER = ParseExtIntegerArgument();
4298 if (ER.isInvalid())
4299 continue;
4300 isInvalid = DS.SetBitIntType(KWLoc: Loc, BitWidth: ER.get(), PrevSpec, DiagID, Policy);
4301 ConsumedEnd = PrevTokLocation;
4302 break;
4303 }
4304 case tok::kw___int128:
4305 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_int128, Loc, PrevSpec,
4306 DiagID, Policy);
4307 break;
4308 case tok::kw_half:
4309 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_half, Loc, PrevSpec,
4310 DiagID, Policy);
4311 break;
4312 case tok::kw___bf16:
4313 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_BFloat16, Loc, PrevSpec,
4314 DiagID, Policy);
4315 break;
4316 case tok::kw_float:
4317 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_float, Loc, PrevSpec,
4318 DiagID, Policy);
4319 break;
4320 case tok::kw_double:
4321 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_double, Loc, PrevSpec,
4322 DiagID, Policy);
4323 break;
4324 case tok::kw__Float16:
4325 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_float16, Loc, PrevSpec,
4326 DiagID, Policy);
4327 break;
4328 case tok::kw__Accum:
4329 assert(getLangOpts().FixedPoint &&
4330 "This keyword is only used when fixed point types are enabled "
4331 "with `-ffixed-point`");
4332 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_accum, Loc, PrevSpec, DiagID,
4333 Policy);
4334 break;
4335 case tok::kw__Fract:
4336 assert(getLangOpts().FixedPoint &&
4337 "This keyword is only used when fixed point types are enabled "
4338 "with `-ffixed-point`");
4339 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_fract, Loc, PrevSpec, DiagID,
4340 Policy);
4341 break;
4342 case tok::kw__Sat:
4343 assert(getLangOpts().FixedPoint &&
4344 "This keyword is only used when fixed point types are enabled "
4345 "with `-ffixed-point`");
4346 isInvalid = DS.SetTypeSpecSat(Loc, PrevSpec, DiagID);
4347 break;
4348 case tok::kw___float128:
4349 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_float128, Loc, PrevSpec,
4350 DiagID, Policy);
4351 break;
4352 case tok::kw___ibm128:
4353 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_ibm128, Loc, PrevSpec,
4354 DiagID, Policy);
4355 break;
4356 case tok::kw_wchar_t:
4357 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_wchar, Loc, PrevSpec,
4358 DiagID, Policy);
4359 break;
4360 case tok::kw_char8_t:
4361 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_char8, Loc, PrevSpec,
4362 DiagID, Policy);
4363 break;
4364 case tok::kw_char16_t:
4365 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_char16, Loc, PrevSpec,
4366 DiagID, Policy);
4367 break;
4368 case tok::kw_char32_t:
4369 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_char32, Loc, PrevSpec,
4370 DiagID, Policy);
4371 break;
4372 case tok::kw_bool:
4373 if (getLangOpts().C23)
4374 Diag(Tok, DiagID: diag::warn_c23_compat_keyword) << Tok.getName();
4375 [[fallthrough]];
4376 case tok::kw__Bool:
4377 if (Tok.is(K: tok::kw__Bool) && !getLangOpts().C99)
4378 Diag(Tok, DiagID: diag::ext_c99_feature) << Tok.getName();
4379
4380 if (Tok.is(K: tok::kw_bool) &&
4381 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
4382 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
4383 PrevSpec = ""; // Not used by the diagnostic.
4384 DiagID = diag::err_bool_redeclaration;
4385 // For better error recovery.
4386 Tok.setKind(tok::identifier);
4387 isInvalid = true;
4388 } else {
4389 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_bool, Loc, PrevSpec,
4390 DiagID, Policy);
4391 }
4392 break;
4393 case tok::kw__Decimal32:
4394 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_decimal32, Loc, PrevSpec,
4395 DiagID, Policy);
4396 break;
4397 case tok::kw__Decimal64:
4398 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_decimal64, Loc, PrevSpec,
4399 DiagID, Policy);
4400 break;
4401 case tok::kw__Decimal128:
4402 isInvalid = DS.SetTypeSpecType(T: DeclSpec::TST_decimal128, Loc, PrevSpec,
4403 DiagID, Policy);
4404 break;
4405 case tok::kw___vector:
4406 isInvalid = DS.SetTypeAltiVecVector(isAltiVecVector: true, Loc, PrevSpec, DiagID, Policy);
4407 break;
4408 case tok::kw___pixel:
4409 isInvalid = DS.SetTypeAltiVecPixel(isAltiVecPixel: true, Loc, PrevSpec, DiagID, Policy);
4410 break;
4411 case tok::kw___bool:
4412 isInvalid = DS.SetTypeAltiVecBool(isAltiVecBool: true, Loc, PrevSpec, DiagID, Policy);
4413 break;
4414 case tok::kw_pipe:
4415 if (!getLangOpts().OpenCL ||
4416 getLangOpts().getOpenCLCompatibleVersion() < 200) {
4417 // OpenCL 2.0 and later define this keyword. OpenCL 1.2 and earlier
4418 // should support the "pipe" word as identifier.
4419 Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
4420 Tok.setKind(tok::identifier);
4421 goto DoneWithDeclSpec;
4422 } else if (!getLangOpts().OpenCLPipes) {
4423 DiagID = diag::err_opencl_unknown_type_specifier;
4424 PrevSpec = Tok.getIdentifierInfo()->getNameStart();
4425 isInvalid = true;
4426 } else
4427 isInvalid = DS.SetTypePipe(isPipe: true, Loc, PrevSpec, DiagID, Policy);
4428 break;
4429// We only need to enumerate each image type once.
4430#define IMAGE_READ_WRITE_TYPE(Type, Id, Ext)
4431#define IMAGE_WRITE_TYPE(Type, Id, Ext)
4432#define IMAGE_READ_TYPE(ImgType, Id, Ext) \
4433 case tok::kw_##ImgType##_t: \
4434 if (!handleOpenCLImageKW(Ext, DeclSpec::TST_##ImgType##_t)) \
4435 goto DoneWithDeclSpec; \
4436 break;
4437#include "clang/Basic/OpenCLImageTypes.def"
4438 case tok::kw___unknown_anytype:
4439 isInvalid = DS.SetTypeSpecType(T: TST_unknown_anytype, Loc,
4440 PrevSpec, DiagID, Policy);
4441 break;
4442
4443 // class-specifier:
4444 case tok::kw_class:
4445 case tok::kw_struct:
4446 case tok::kw___interface:
4447 case tok::kw_union: {
4448 tok::TokenKind Kind = Tok.getKind();
4449 ConsumeToken();
4450
4451 // These are attributes following class specifiers.
4452 // To produce better diagnostic, we parse them when
4453 // parsing class specifier.
4454 ParsedAttributes Attributes(AttrFactory);
4455 ParseClassSpecifier(TagTokKind: Kind, TagLoc: Loc, DS, TemplateInfo, AS,
4456 EnteringContext, DSC: DSContext, Attributes);
4457
4458 // If there are attributes following class specifier,
4459 // take them over and handle them here.
4460 if (!Attributes.empty()) {
4461 AttrsLastTime = true;
4462 attrs.takeAllFrom(Other&: Attributes);
4463 }
4464 continue;
4465 }
4466
4467 // enum-specifier:
4468 case tok::kw_enum:
4469 ConsumeToken();
4470 ParseEnumSpecifier(TagLoc: Loc, DS, TemplateInfo, AS, DSC: DSContext);
4471 continue;
4472
4473 // cv-qualifier:
4474 case tok::kw_const:
4475 isInvalid = DS.SetTypeQual(T: DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
4476 Lang: getLangOpts());
4477 break;
4478 case tok::kw_volatile:
4479 isInvalid = DS.SetTypeQual(T: DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
4480 Lang: getLangOpts());
4481 break;
4482 case tok::kw_restrict:
4483 isInvalid = DS.SetTypeQual(T: DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
4484 Lang: getLangOpts());
4485 break;
4486
4487 // C++ typename-specifier:
4488 case tok::kw_typename:
4489 if (TryAnnotateTypeOrScopeToken()) {
4490 DS.SetTypeSpecError();
4491 goto DoneWithDeclSpec;
4492 }
4493 if (!Tok.is(K: tok::kw_typename))
4494 continue;
4495 break;
4496
4497 // C23/GNU typeof support.
4498 case tok::kw_typeof:
4499 case tok::kw_typeof_unqual:
4500 ParseTypeofSpecifier(DS);
4501 continue;
4502
4503 case tok::annot_decltype:
4504 ParseDecltypeSpecifier(DS);
4505 continue;
4506
4507 case tok::annot_pack_indexing_type:
4508 ParsePackIndexingType(DS);
4509 continue;
4510
4511 case tok::annot_pragma_pack:
4512 HandlePragmaPack();
4513 continue;
4514
4515 case tok::annot_pragma_ms_pragma:
4516 HandlePragmaMSPragma();
4517 continue;
4518
4519 case tok::annot_pragma_ms_vtordisp:
4520 HandlePragmaMSVtorDisp();
4521 continue;
4522
4523 case tok::annot_pragma_ms_pointers_to_members:
4524 HandlePragmaMSPointersToMembers();
4525 continue;
4526
4527#define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait:
4528#include "clang/Basic/TransformTypeTraits.def"
4529 // HACK: libstdc++ already uses '__remove_cv' as an alias template so we
4530 // work around this by expecting all transform type traits to be suffixed
4531 // with '('. They're an identifier otherwise.
4532 if (!MaybeParseTypeTransformTypeSpecifier(DS))
4533 goto ParseIdentifier;
4534 continue;
4535
4536 case tok::kw__Atomic:
4537 // C11 6.7.2.4/4:
4538 // If the _Atomic keyword is immediately followed by a left parenthesis,
4539 // it is interpreted as a type specifier (with a type name), not as a
4540 // type qualifier.
4541 diagnoseUseOfC11Keyword(Tok);
4542 if (NextToken().is(K: tok::l_paren)) {
4543 ParseAtomicSpecifier(DS);
4544 continue;
4545 }
4546 isInvalid = DS.SetTypeQual(T: DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4547 Lang: getLangOpts());
4548 break;
4549
4550 // OpenCL address space qualifiers:
4551 case tok::kw___generic:
4552 // generic address space is introduced only in OpenCL v2.0
4553 // see OpenCL C Spec v2.0 s6.5.5
4554 // OpenCL v3.0 introduces __opencl_c_generic_address_space
4555 // feature macro to indicate if generic address space is supported
4556 if (!Actions.getLangOpts().OpenCLGenericAddressSpace) {
4557 DiagID = diag::err_opencl_unknown_type_specifier;
4558 PrevSpec = Tok.getIdentifierInfo()->getNameStart();
4559 isInvalid = true;
4560 break;
4561 }
4562 [[fallthrough]];
4563 case tok::kw_private:
4564 // It's fine (but redundant) to check this for __generic on the
4565 // fallthrough path; we only form the __generic token in OpenCL mode.
4566 if (!getLangOpts().OpenCL)
4567 goto DoneWithDeclSpec;
4568 [[fallthrough]];
4569 case tok::kw___private:
4570 case tok::kw___global:
4571 case tok::kw___local:
4572 case tok::kw___constant:
4573 // OpenCL access qualifiers:
4574 case tok::kw___read_only:
4575 case tok::kw___write_only:
4576 case tok::kw___read_write:
4577 ParseOpenCLQualifiers(Attrs&: DS.getAttributes());
4578 break;
4579
4580 case tok::kw_groupshared:
4581 case tok::kw_in:
4582 case tok::kw_inout:
4583 case tok::kw_out:
4584 // NOTE: ParseHLSLQualifiers will consume the qualifier token.
4585 ParseHLSLQualifiers(Attrs&: DS.getAttributes());
4586 continue;
4587
4588#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) \
4589 case tok::kw_##Name: \
4590 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_##Name, Loc, PrevSpec, \
4591 DiagID, Policy); \
4592 break;
4593#include "clang/Basic/HLSLIntangibleTypes.def"
4594
4595 case tok::less:
4596 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
4597 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
4598 // but we support it.
4599 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC)
4600 goto DoneWithDeclSpec;
4601
4602 SourceLocation StartLoc = Tok.getLocation();
4603 SourceLocation EndLoc;
4604 TypeResult Type = parseObjCProtocolQualifierType(rAngleLoc&: EndLoc);
4605 if (Type.isUsable()) {
4606 if (DS.SetTypeSpecType(T: DeclSpec::TST_typename, TagKwLoc: StartLoc, TagNameLoc: StartLoc,
4607 PrevSpec, DiagID, Rep: Type.get(),
4608 Policy: Actions.getASTContext().getPrintingPolicy()))
4609 Diag(Loc: StartLoc, DiagID) << PrevSpec;
4610
4611 DS.SetRangeEnd(EndLoc);
4612 } else {
4613 DS.SetTypeSpecError();
4614 }
4615
4616 // Need to support trailing type qualifiers (e.g. "id<p> const").
4617 // If a type specifier follows, it will be diagnosed elsewhere.
4618 continue;
4619 }
4620
4621 DS.SetRangeEnd(ConsumedEnd.isValid() ? ConsumedEnd : Tok.getLocation());
4622
4623 // If the specifier wasn't legal, issue a diagnostic.
4624 if (isInvalid) {
4625 assert(PrevSpec && "Method did not return previous specifier!");
4626 assert(DiagID);
4627
4628 if (DiagID == diag::ext_duplicate_declspec ||
4629 DiagID == diag::ext_warn_duplicate_declspec ||
4630 DiagID == diag::err_duplicate_declspec)
4631 Diag(Loc, DiagID) << PrevSpec
4632 << FixItHint::CreateRemoval(
4633 RemoveRange: SourceRange(Loc, DS.getEndLoc()));
4634 else if (DiagID == diag::err_opencl_unknown_type_specifier) {
4635 Diag(Loc, DiagID) << getLangOpts().getOpenCLVersionString() << PrevSpec
4636 << isStorageClass;
4637 } else
4638 Diag(Loc, DiagID) << PrevSpec;
4639 }
4640
4641 if (DiagID != diag::err_bool_redeclaration && ConsumedEnd.isInvalid())
4642 // After an error the next token can be an annotation token.
4643 ConsumeAnyToken();
4644
4645 AttrsLastTime = false;
4646 }
4647}
4648
4649static void DiagnoseCountAttributedTypeInUnnamedAnon(ParsingDeclSpec &DS,
4650 Parser &P) {
4651
4652 if (DS.getTypeSpecType() != DeclSpec::TST_struct)
4653 return;
4654
4655 auto *RD = dyn_cast<RecordDecl>(Val: DS.getRepAsDecl());
4656 // We're only interested in unnamed, non-anonymous struct
4657 if (!RD || !RD->getName().empty() || RD->isAnonymousStructOrUnion())
4658 return;
4659
4660 for (auto *I : RD->decls()) {
4661 auto *VD = dyn_cast<ValueDecl>(Val: I);
4662 if (!VD)
4663 continue;
4664
4665 auto *CAT = VD->getType()->getAs<CountAttributedType>();
4666 if (!CAT)
4667 continue;
4668
4669 for (const auto &DD : CAT->dependent_decls()) {
4670 if (!RD->containsDecl(D: DD.getDecl())) {
4671 P.Diag(Loc: VD->getBeginLoc(), DiagID: diag::err_count_attr_param_not_in_same_struct)
4672 << DD.getDecl() << CAT->getKind() << CAT->isArrayType();
4673 P.Diag(Loc: DD.getDecl()->getBeginLoc(),
4674 DiagID: diag::note_flexible_array_counted_by_attr_field)
4675 << DD.getDecl();
4676 }
4677 }
4678 }
4679}
4680
4681void Parser::ParseStructDeclaration(
4682 ParsingDeclSpec &DS,
4683 llvm::function_ref<Decl *(ParsingFieldDeclarator &)> FieldsCallback,
4684 LateParsedAttrList *LateFieldAttrs) {
4685
4686 if (Tok.is(K: tok::kw___extension__)) {
4687 // __extension__ silences extension warnings in the subexpression.
4688 ExtensionRAIIObject O(Diags); // Use RAII to do this.
4689 ConsumeToken();
4690 return ParseStructDeclaration(DS, FieldsCallback, LateFieldAttrs);
4691 }
4692
4693 // Parse leading attributes.
4694 ParsedAttributes Attrs(AttrFactory);
4695 MaybeParseCXX11Attributes(Attrs);
4696
4697 // Parse the common specifier-qualifiers-list piece.
4698 ParseSpecifierQualifierList(DS);
4699
4700 // If there are no declarators, this is a free-standing declaration
4701 // specifier. Let the actions module cope with it.
4702 if (Tok.is(K: tok::semi)) {
4703 // C23 6.7.2.1p9 : "The optional attribute specifier sequence in a
4704 // member declaration appertains to each of the members declared by the
4705 // member declarator list; it shall not appear if the optional member
4706 // declarator list is omitted."
4707 ProhibitAttributes(Attrs);
4708 RecordDecl *AnonRecord = nullptr;
4709 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(
4710 S: getCurScope(), AS: AS_none, DS, DeclAttrs: ParsedAttributesView::none(), AnonRecord);
4711 assert(!AnonRecord && "Did not expect anonymous struct or union here");
4712 DS.complete(D: TheDecl);
4713 return;
4714 }
4715
4716 // Read struct-declarators until we find the semicolon.
4717 bool FirstDeclarator = true;
4718 SourceLocation CommaLoc;
4719 while (true) {
4720 ParsingFieldDeclarator DeclaratorInfo(*this, DS, Attrs);
4721 DeclaratorInfo.D.setCommaLoc(CommaLoc);
4722
4723 // Attributes are only allowed here on successive declarators.
4724 if (!FirstDeclarator) {
4725 // However, this does not apply for [[]] attributes (which could show up
4726 // before or after the __attribute__ attributes).
4727 DiagnoseAndSkipCXX11Attributes();
4728 MaybeParseGNUAttributes(D&: DeclaratorInfo.D);
4729 DiagnoseAndSkipCXX11Attributes();
4730 }
4731
4732 /// struct-declarator: declarator
4733 /// struct-declarator: declarator[opt] ':' constant-expression
4734 if (Tok.isNot(K: tok::colon)) {
4735 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
4736 ColonProtectionRAIIObject X(*this);
4737 ParseDeclarator(D&: DeclaratorInfo.D);
4738 } else
4739 DeclaratorInfo.D.SetIdentifier(Id: nullptr, IdLoc: Tok.getLocation());
4740
4741 // Here, we now know that the unnamed struct is not an anonymous struct.
4742 // Report an error if a counted_by attribute refers to a field in a
4743 // different named struct.
4744 DiagnoseCountAttributedTypeInUnnamedAnon(DS, P&: *this);
4745
4746 if (TryConsumeToken(Expected: tok::colon)) {
4747 ExprResult Res(ParseConstantExpression());
4748 if (Res.isInvalid())
4749 SkipUntil(T: tok::semi, Flags: StopBeforeMatch);
4750 else
4751 DeclaratorInfo.BitfieldSize = Res.get();
4752 }
4753
4754 // If attributes exist after the declarator, parse them.
4755 MaybeParseGNUAttributes(D&: DeclaratorInfo.D, LateAttrs: LateFieldAttrs);
4756
4757 // We're done with this declarator; invoke the callback.
4758 Decl *Field = FieldsCallback(DeclaratorInfo);
4759 if (Field)
4760 DistributeCLateParsedAttrs(Dcl: Field, LateAttrs: LateFieldAttrs);
4761
4762 // If we don't have a comma, it is either the end of the list (a ';')
4763 // or an error, bail out.
4764 if (!TryConsumeToken(Expected: tok::comma, Loc&: CommaLoc))
4765 return;
4766
4767 FirstDeclarator = false;
4768 }
4769}
4770
4771// TODO: All callers of this function should be moved to
4772// `Parser::ParseLexedAttributeList`.
4773void Parser::ParseLexedCAttributeList(LateParsedAttrList &LAs, bool EnterScope,
4774 ParsedAttributes *OutAttrs) {
4775 assert(LAs.parseSoon() &&
4776 "Attribute list should be marked for immediate parsing.");
4777 for (auto *LA : LAs) {
4778 ParseLexedCAttribute(LA&: *LA, EnterScope, OutAttrs);
4779 delete LA;
4780 }
4781 LAs.clear();
4782}
4783
4784void Parser::ParseLexedCAttribute(LateParsedAttribute &LA, bool EnterScope,
4785 ParsedAttributes *OutAttrs) {
4786 // Create a fake EOF so that attribute parsing won't go off the end of the
4787 // attribute.
4788 Token AttrEnd;
4789 AttrEnd.startToken();
4790 AttrEnd.setKind(tok::eof);
4791 AttrEnd.setLocation(Tok.getLocation());
4792 AttrEnd.setEofData(LA.Toks.data());
4793 LA.Toks.push_back(Elt: AttrEnd);
4794
4795 // Append the current token at the end of the new token stream so that it
4796 // doesn't get lost.
4797 LA.Toks.push_back(Elt: Tok);
4798 PP.EnterTokenStream(Toks: LA.Toks, /*DisableMacroExpansion=*/true,
4799 /*IsReinject=*/true);
4800 // Drop the current token and bring the first cached one. It's the same token
4801 // as when we entered this function.
4802 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
4803
4804 // TODO: Use `EnterScope`
4805 (void)EnterScope;
4806
4807 ParsedAttributes Attrs(AttrFactory);
4808
4809 assert(LA.Decls.size() <= 1 &&
4810 "late field attribute expects to have at most one declaration.");
4811
4812 // Dispatch based on the attribute and parse it
4813 ParseGNUAttributeArgs(AttrName: &LA.AttrName, AttrNameLoc: LA.AttrNameLoc, Attrs, EndLoc: nullptr, ScopeName: nullptr,
4814 ScopeLoc: SourceLocation(), Form: ParsedAttr::Form::GNU(), D: nullptr);
4815
4816 for (auto *D : LA.Decls)
4817 Actions.ActOnFinishDelayedAttribute(S: getCurScope(), D, Attrs);
4818
4819 // Due to a parsing error, we either went over the cached tokens or
4820 // there are still cached tokens left, so we skip the leftover tokens.
4821 while (Tok.isNot(K: tok::eof))
4822 ConsumeAnyToken();
4823
4824 // Consume the fake EOF token if it's there
4825 if (Tok.is(K: tok::eof) && Tok.getEofData() == AttrEnd.getEofData())
4826 ConsumeAnyToken();
4827
4828 if (OutAttrs) {
4829 OutAttrs->takeAllFrom(Other&: Attrs);
4830 }
4831}
4832
4833void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
4834 DeclSpec::TST TagType, RecordDecl *TagDecl) {
4835 PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc,
4836 "parsing struct/union body");
4837 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
4838
4839 BalancedDelimiterTracker T(*this, tok::l_brace);
4840 if (T.consumeOpen())
4841 return;
4842
4843 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
4844 Actions.ActOnTagStartDefinition(S: getCurScope(), TagDecl);
4845
4846 // `LateAttrParseExperimentalExtOnly=true` requests that only attributes
4847 // marked with `LateAttrParseExperimentalExt` are late parsed.
4848 LateParsedAttrList LateFieldAttrs(/*PSoon=*/true,
4849 /*LateAttrParseExperimentalExtOnly=*/true);
4850
4851 // While we still have something to read, read the declarations in the struct.
4852 while (!tryParseMisplacedModuleImport() && Tok.isNot(K: tok::r_brace) &&
4853 Tok.isNot(K: tok::eof)) {
4854 // Each iteration of this loop reads one struct-declaration.
4855
4856 // Check for extraneous top-level semicolon.
4857 if (Tok.is(K: tok::semi)) {
4858 ConsumeExtraSemi(Kind: ExtraSemiKind::InsideStruct, T: TagType);
4859 continue;
4860 }
4861
4862 // Parse _Static_assert declaration.
4863 if (Tok.isOneOf(Ks: tok::kw__Static_assert, Ks: tok::kw_static_assert)) {
4864 SourceLocation DeclEnd;
4865 ParseStaticAssertDeclaration(DeclEnd);
4866 continue;
4867 }
4868
4869 if (Tok.is(K: tok::annot_pragma_pack)) {
4870 HandlePragmaPack();
4871 continue;
4872 }
4873
4874 if (Tok.is(K: tok::annot_pragma_align)) {
4875 HandlePragmaAlign();
4876 continue;
4877 }
4878
4879 if (Tok.isOneOf(Ks: tok::annot_pragma_openmp, Ks: tok::annot_attr_openmp)) {
4880 // Result can be ignored, because it must be always empty.
4881 AccessSpecifier AS = AS_none;
4882 ParsedAttributes Attrs(AttrFactory);
4883 (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs);
4884 continue;
4885 }
4886
4887 if (Tok.is(K: tok::annot_pragma_openacc)) {
4888 AccessSpecifier AS = AS_none;
4889 ParsedAttributes Attrs(AttrFactory);
4890 ParseOpenACCDirectiveDecl(AS, Attrs, TagType, TagDecl);
4891 continue;
4892 }
4893
4894 if (tok::isPragmaAnnotation(K: Tok.getKind())) {
4895 Diag(Loc: Tok.getLocation(), DiagID: diag::err_pragma_misplaced_in_decl)
4896 << DeclSpec::getSpecifierName(
4897 T: TagType, Policy: Actions.getASTContext().getPrintingPolicy());
4898 ConsumeAnnotationToken();
4899 continue;
4900 }
4901
4902 if (!Tok.is(K: tok::at)) {
4903 auto CFieldCallback = [&](ParsingFieldDeclarator &FD) -> Decl * {
4904 // Install the declarator into the current TagDecl.
4905 Decl *Field =
4906 Actions.ActOnField(S: getCurScope(), TagD: TagDecl,
4907 DeclStart: FD.D.getDeclSpec().getSourceRange().getBegin(),
4908 D&: FD.D, BitfieldWidth: FD.BitfieldSize);
4909 FD.complete(D: Field);
4910 return Field;
4911 };
4912
4913 // Parse all the comma separated declarators.
4914 ParsingDeclSpec DS(*this);
4915 ParseStructDeclaration(DS, FieldsCallback: CFieldCallback, LateFieldAttrs: &LateFieldAttrs);
4916 } else { // Handle @defs
4917 ConsumeToken();
4918 if (!Tok.isObjCAtKeyword(objcKey: tok::objc_defs)) {
4919 Diag(Tok, DiagID: diag::err_unexpected_at);
4920 SkipUntil(T: tok::semi);
4921 continue;
4922 }
4923 ConsumeToken();
4924 ExpectAndConsume(ExpectedTok: tok::l_paren);
4925 if (!Tok.is(K: tok::identifier)) {
4926 Diag(Tok, DiagID: diag::err_expected) << tok::identifier;
4927 SkipUntil(T: tok::semi);
4928 continue;
4929 }
4930 SmallVector<Decl *, 16> Fields;
4931 Actions.ObjC().ActOnDefs(S: getCurScope(), TagD: TagDecl, DeclStart: Tok.getLocation(),
4932 ClassName: Tok.getIdentifierInfo(), Decls&: Fields);
4933 ConsumeToken();
4934 ExpectAndConsume(ExpectedTok: tok::r_paren);
4935 }
4936
4937 if (TryConsumeToken(Expected: tok::semi))
4938 continue;
4939
4940 if (Tok.is(K: tok::r_brace)) {
4941 ExpectAndConsume(ExpectedTok: tok::semi, Diag: diag::ext_expected_semi_decl_list);
4942 break;
4943 }
4944
4945 ExpectAndConsume(ExpectedTok: tok::semi, Diag: diag::err_expected_semi_decl_list);
4946 // Skip to end of block or statement to avoid ext-warning on extra ';'.
4947 SkipUntil(T: tok::r_brace, Flags: StopAtSemi | StopBeforeMatch);
4948 // If we stopped at a ';', eat it.
4949 TryConsumeToken(Expected: tok::semi);
4950 }
4951
4952 T.consumeClose();
4953
4954 ParsedAttributes attrs(AttrFactory);
4955 // If attributes exist after struct contents, parse them.
4956 MaybeParseGNUAttributes(Attrs&: attrs, LateAttrs: &LateFieldAttrs);
4957
4958 // Late parse field attributes if necessary.
4959 ParseLexedCAttributeList(LAs&: LateFieldAttrs, /*EnterScope=*/false);
4960
4961 SmallVector<Decl *, 32> FieldDecls(TagDecl->fields());
4962
4963 Actions.ActOnFields(S: getCurScope(), RecLoc: RecordLoc, TagDecl, Fields: FieldDecls,
4964 LBrac: T.getOpenLocation(), RBrac: T.getCloseLocation(), AttrList: attrs);
4965 StructScope.Exit();
4966 Actions.ActOnTagFinishDefinition(S: getCurScope(), TagDecl, BraceRange: T.getRange());
4967}
4968
4969void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
4970 const ParsedTemplateInfo &TemplateInfo,
4971 AccessSpecifier AS, DeclSpecContext DSC) {
4972 // Parse the tag portion of this.
4973 if (Tok.is(K: tok::code_completion)) {
4974 // Code completion for an enum name.
4975 cutOffParsing();
4976 Actions.CodeCompletion().CodeCompleteTag(S: getCurScope(), TagSpec: DeclSpec::TST_enum);
4977 DS.SetTypeSpecError(); // Needed by ActOnUsingDeclaration.
4978 return;
4979 }
4980
4981 // If attributes exist after tag, parse them.
4982 ParsedAttributes attrs(AttrFactory);
4983 MaybeParseAttributes(WhichAttrKinds: PAKM_GNU | PAKM_Declspec | PAKM_CXX11, Attrs&: attrs);
4984
4985 SourceLocation ScopedEnumKWLoc;
4986 bool IsScopedUsingClassTag = false;
4987
4988 // In C++11, recognize 'enum class' and 'enum struct'.
4989 if (Tok.isOneOf(Ks: tok::kw_class, Ks: tok::kw_struct) && getLangOpts().CPlusPlus) {
4990 Diag(Tok, DiagID: getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
4991 : diag::ext_scoped_enum);
4992 IsScopedUsingClassTag = Tok.is(K: tok::kw_class);
4993 ScopedEnumKWLoc = ConsumeToken();
4994
4995 // Attributes are not allowed between these keywords. Diagnose,
4996 // but then just treat them like they appeared in the right place.
4997 ProhibitAttributes(Attrs&: attrs);
4998
4999 // They are allowed afterwards, though.
5000 MaybeParseAttributes(WhichAttrKinds: PAKM_GNU | PAKM_Declspec | PAKM_CXX11, Attrs&: attrs);
5001 }
5002
5003 // C++11 [temp.explicit]p12:
5004 // The usual access controls do not apply to names used to specify
5005 // explicit instantiations.
5006 // We extend this to also cover explicit specializations. Note that
5007 // we don't suppress if this turns out to be an elaborated type
5008 // specifier.
5009 bool shouldDelayDiagsInTag =
5010 (TemplateInfo.Kind == ParsedTemplateKind::ExplicitInstantiation ||
5011 TemplateInfo.Kind == ParsedTemplateKind::ExplicitSpecialization);
5012 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
5013
5014 // Determine whether this declaration is permitted to have an enum-base.
5015 AllowDefiningTypeSpec AllowEnumSpecifier =
5016 isDefiningTypeSpecifierContext(DSC, IsCPlusPlus: getLangOpts().CPlusPlus);
5017 bool CanBeOpaqueEnumDeclaration =
5018 DS.isEmpty() && isOpaqueEnumDeclarationContext(DSC);
5019 bool CanHaveEnumBase = (getLangOpts().CPlusPlus11 || getLangOpts().ObjC ||
5020 getLangOpts().MicrosoftExt) &&
5021 (AllowEnumSpecifier == AllowDefiningTypeSpec::Yes ||
5022 CanBeOpaqueEnumDeclaration);
5023
5024 CXXScopeSpec &SS = DS.getTypeSpecScope();
5025 if (getLangOpts().CPlusPlus) {
5026 // "enum foo : bar;" is not a potential typo for "enum foo::bar;".
5027 ColonProtectionRAIIObject X(*this);
5028
5029 CXXScopeSpec Spec;
5030 if (ParseOptionalCXXScopeSpecifier(SS&: Spec, /*ObjectType=*/nullptr,
5031 /*ObjectHasErrors=*/false,
5032 /*EnteringContext=*/true))
5033 return;
5034
5035 if (Spec.isSet() && Tok.isNot(K: tok::identifier)) {
5036 Diag(Tok, DiagID: diag::err_expected) << tok::identifier;
5037 DS.SetTypeSpecError();
5038 if (Tok.isNot(K: tok::l_brace)) {
5039 // Has no name and is not a definition.
5040 // Skip the rest of this declarator, up until the comma or semicolon.
5041 SkipUntil(T: tok::comma, Flags: StopAtSemi);
5042 return;
5043 }
5044 }
5045
5046 SS = Spec;
5047 }
5048
5049 // Must have either 'enum name' or 'enum {...}' or (rarely) 'enum : T { ... }'.
5050 if (Tok.isNot(K: tok::identifier) && Tok.isNot(K: tok::l_brace) &&
5051 Tok.isNot(K: tok::colon)) {
5052 Diag(Tok, DiagID: diag::err_expected_either) << tok::identifier << tok::l_brace;
5053
5054 DS.SetTypeSpecError();
5055 // Skip the rest of this declarator, up until the comma or semicolon.
5056 SkipUntil(T: tok::comma, Flags: StopAtSemi);
5057 return;
5058 }
5059
5060 // If an identifier is present, consume and remember it.
5061 IdentifierInfo *Name = nullptr;
5062 SourceLocation NameLoc;
5063 if (Tok.is(K: tok::identifier)) {
5064 Name = Tok.getIdentifierInfo();
5065 NameLoc = ConsumeToken();
5066 }
5067
5068 if (!Name && ScopedEnumKWLoc.isValid()) {
5069 // C++0x 7.2p2: The optional identifier shall not be omitted in the
5070 // declaration of a scoped enumeration.
5071 Diag(Tok, DiagID: diag::err_scoped_enum_missing_identifier);
5072 ScopedEnumKWLoc = SourceLocation();
5073 IsScopedUsingClassTag = false;
5074 }
5075
5076 // Okay, end the suppression area. We'll decide whether to emit the
5077 // diagnostics in a second.
5078 if (shouldDelayDiagsInTag)
5079 diagsFromTag.done();
5080
5081 TypeResult BaseType;
5082 SourceRange BaseRange;
5083
5084 bool CanBeBitfield =
5085 getCurScope()->isClassScope() && ScopedEnumKWLoc.isInvalid() && Name;
5086
5087 // Parse the fixed underlying type.
5088 if (Tok.is(K: tok::colon)) {
5089 // This might be an enum-base or part of some unrelated enclosing context.
5090 //
5091 // 'enum E : base' is permitted in two circumstances:
5092 //
5093 // 1) As a defining-type-specifier, when followed by '{'.
5094 // 2) As the sole constituent of a complete declaration -- when DS is empty
5095 // and the next token is ';'.
5096 //
5097 // The restriction to defining-type-specifiers is important to allow parsing
5098 // a ? new enum E : int{}
5099 // _Generic(a, enum E : int{})
5100 // properly.
5101 //
5102 // One additional consideration applies:
5103 //
5104 // C++ [dcl.enum]p1:
5105 // A ':' following "enum nested-name-specifier[opt] identifier" within
5106 // the decl-specifier-seq of a member-declaration is parsed as part of
5107 // an enum-base.
5108 //
5109 // Other language modes supporting enumerations with fixed underlying types
5110 // do not have clear rules on this, so we disambiguate to determine whether
5111 // the tokens form a bit-field width or an enum-base.
5112
5113 if (CanBeBitfield && !isEnumBase(AllowSemi: CanBeOpaqueEnumDeclaration)) {
5114 // Outside C++11, do not interpret the tokens as an enum-base if they do
5115 // not make sense as one. In C++11, it's an error if this happens.
5116 if (getLangOpts().CPlusPlus11)
5117 Diag(Loc: Tok.getLocation(), DiagID: diag::err_anonymous_enum_bitfield);
5118 } else if (CanHaveEnumBase || !ColonIsSacred) {
5119 SourceLocation ColonLoc = ConsumeToken();
5120
5121 // Parse a type-specifier-seq as a type. We can't just ParseTypeName here,
5122 // because under -fms-extensions,
5123 // enum E : int *p;
5124 // declares 'enum E : int; E *p;' not 'enum E : int*; E p;'.
5125 DeclSpec DS(AttrFactory);
5126 // enum-base is not assumed to be a type and therefore requires the
5127 // typename keyword [p0634r3].
5128 ParseSpecifierQualifierList(DS, AllowImplicitTypename: ImplicitTypenameContext::No, AS,
5129 DSC: DeclSpecContext::DSC_type_specifier);
5130 Declarator DeclaratorInfo(DS, ParsedAttributesView::none(),
5131 DeclaratorContext::TypeName);
5132 BaseType = Actions.ActOnTypeName(D&: DeclaratorInfo);
5133
5134 BaseRange = SourceRange(ColonLoc, DeclaratorInfo.getSourceRange().getEnd());
5135
5136 if (!getLangOpts().ObjC) {
5137 if (getLangOpts().CPlusPlus)
5138 DiagCompat(Loc: ColonLoc, CompatDiagId: diag_compat::enum_fixed_underlying_type)
5139 << BaseRange;
5140 else if (getLangOpts().MicrosoftExt && !getLangOpts().C23)
5141 Diag(Loc: ColonLoc, DiagID: diag::ext_ms_c_enum_fixed_underlying_type)
5142 << BaseRange;
5143 else
5144 Diag(Loc: ColonLoc, DiagID: getLangOpts().C23
5145 ? diag::warn_c17_compat_enum_fixed_underlying_type
5146 : diag::ext_c23_enum_fixed_underlying_type)
5147 << BaseRange;
5148 }
5149 }
5150 }
5151
5152 // There are four options here. If we have 'friend enum foo;' then this is a
5153 // friend declaration, and cannot have an accompanying definition. If we have
5154 // 'enum foo;', then this is a forward declaration. If we have
5155 // 'enum foo {...' then this is a definition. Otherwise we have something
5156 // like 'enum foo xyz', a reference.
5157 //
5158 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
5159 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
5160 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
5161 //
5162 TagUseKind TUK;
5163 if (AllowEnumSpecifier == AllowDefiningTypeSpec::No)
5164 TUK = TagUseKind::Reference;
5165 else if (Tok.is(K: tok::l_brace)) {
5166 if (DS.isFriendSpecified()) {
5167 Diag(Loc: Tok.getLocation(), DiagID: diag::err_friend_decl_defines_type)
5168 << SourceRange(DS.getFriendSpecLoc());
5169 ConsumeBrace();
5170 SkipUntil(T: tok::r_brace, Flags: StopAtSemi);
5171 // Discard any other definition-only pieces.
5172 attrs.clear();
5173 ScopedEnumKWLoc = SourceLocation();
5174 IsScopedUsingClassTag = false;
5175 BaseType = TypeResult();
5176 TUK = TagUseKind::Friend;
5177 } else {
5178 TUK = TagUseKind::Definition;
5179 }
5180 } else if (!isTypeSpecifier(DSC) &&
5181 (Tok.is(K: tok::semi) ||
5182 (Tok.isAtStartOfLine() &&
5183 !isValidAfterTypeSpecifier(CouldBeBitfield: CanBeBitfield)))) {
5184 // An opaque-enum-declaration is required to be standalone (no preceding or
5185 // following tokens in the declaration). Sema enforces this separately by
5186 // diagnosing anything else in the DeclSpec.
5187 TUK = DS.isFriendSpecified() ? TagUseKind::Friend : TagUseKind::Declaration;
5188 if (Tok.isNot(K: tok::semi)) {
5189 // A semicolon was missing after this declaration. Diagnose and recover.
5190 ExpectAndConsume(ExpectedTok: tok::semi, Diag: diag::err_expected_after, DiagMsg: "enum");
5191 PP.EnterToken(Tok, /*IsReinject=*/true);
5192 Tok.setKind(tok::semi);
5193 }
5194 } else {
5195 TUK = TagUseKind::Reference;
5196 }
5197
5198 bool IsElaboratedTypeSpecifier =
5199 TUK == TagUseKind::Reference || TUK == TagUseKind::Friend;
5200
5201 // If this is an elaborated type specifier nested in a larger declaration,
5202 // and we delayed diagnostics before, just merge them into the current pool.
5203 if (TUK == TagUseKind::Reference && shouldDelayDiagsInTag) {
5204 diagsFromTag.redelay();
5205 }
5206
5207 MultiTemplateParamsArg TParams;
5208 if (TemplateInfo.Kind != ParsedTemplateKind::NonTemplate &&
5209 TUK != TagUseKind::Reference) {
5210 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
5211 // Skip the rest of this declarator, up until the comma or semicolon.
5212 Diag(Tok, DiagID: diag::err_enum_template);
5213 SkipUntil(T: tok::comma, Flags: StopAtSemi);
5214 return;
5215 }
5216
5217 if (TemplateInfo.Kind == ParsedTemplateKind::ExplicitInstantiation) {
5218 // Enumerations can't be explicitly instantiated.
5219 DS.SetTypeSpecError();
5220 Diag(Loc: StartLoc, DiagID: diag::err_explicit_instantiation_enum);
5221 return;
5222 }
5223
5224 assert(TemplateInfo.TemplateParams && "no template parameters");
5225 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
5226 TemplateInfo.TemplateParams->size());
5227 SS.setTemplateParamLists(TParams);
5228 }
5229
5230 if (!Name && TUK != TagUseKind::Definition) {
5231 Diag(Tok, DiagID: diag::err_enumerator_unnamed_no_def);
5232
5233 DS.SetTypeSpecError();
5234 // Skip the rest of this declarator, up until the comma or semicolon.
5235 SkipUntil(T: tok::comma, Flags: StopAtSemi);
5236 return;
5237 }
5238
5239 // An elaborated-type-specifier has a much more constrained grammar:
5240 //
5241 // 'enum' nested-name-specifier[opt] identifier
5242 //
5243 // If we parsed any other bits, reject them now.
5244 //
5245 // MSVC and (for now at least) Objective-C permit a full enum-specifier
5246 // or opaque-enum-declaration anywhere.
5247 if (IsElaboratedTypeSpecifier && !getLangOpts().MicrosoftExt &&
5248 !getLangOpts().ObjC) {
5249 ProhibitCXX11Attributes(Attrs&: attrs, AttrDiagID: diag::err_attributes_not_allowed,
5250 KeywordDiagID: diag::err_keyword_not_allowed,
5251 /*DiagnoseEmptyAttrs=*/true);
5252 if (BaseType.isUsable())
5253 Diag(Loc: BaseRange.getBegin(), DiagID: diag::ext_enum_base_in_type_specifier)
5254 << (AllowEnumSpecifier == AllowDefiningTypeSpec::Yes) << BaseRange;
5255 else if (ScopedEnumKWLoc.isValid())
5256 Diag(Loc: ScopedEnumKWLoc, DiagID: diag::ext_elaborated_enum_class)
5257 << FixItHint::CreateRemoval(RemoveRange: ScopedEnumKWLoc) << IsScopedUsingClassTag;
5258 }
5259
5260 stripTypeAttributesOffDeclSpec(Attrs&: attrs, DS, TUK);
5261
5262 SkipBodyInfo SkipBody;
5263 if (!Name && TUK == TagUseKind::Definition && Tok.is(K: tok::l_brace) &&
5264 NextToken().is(K: tok::identifier))
5265 SkipBody = Actions.shouldSkipAnonEnumBody(S: getCurScope(),
5266 II: NextToken().getIdentifierInfo(),
5267 IILoc: NextToken().getLocation());
5268
5269 bool Owned = false;
5270 bool IsDependent = false;
5271 const char *PrevSpec = nullptr;
5272 unsigned DiagID;
5273 Decl *TagDecl =
5274 Actions.ActOnTag(S: getCurScope(), TagSpec: DeclSpec::TST_enum, TUK, KWLoc: StartLoc, SS,
5275 Name, NameLoc, Attr: attrs, AS, ModulePrivateLoc: DS.getModulePrivateSpecLoc(),
5276 TemplateParameterLists: TParams, OwnedDecl&: Owned, IsDependent, ScopedEnumKWLoc,
5277 ScopedEnumUsesClassTag: IsScopedUsingClassTag,
5278 UnderlyingType: BaseType, IsTypeSpecifier: DSC == DeclSpecContext::DSC_type_specifier,
5279 IsTemplateParamOrArg: DSC == DeclSpecContext::DSC_template_param ||
5280 DSC == DeclSpecContext::DSC_template_type_arg,
5281 OOK: OffsetOfState, SkipBody: &SkipBody).get();
5282
5283 if (SkipBody.ShouldSkip) {
5284 assert(TUK == TagUseKind::Definition && "can only skip a definition");
5285
5286 BalancedDelimiterTracker T(*this, tok::l_brace);
5287 T.consumeOpen();
5288 T.skipToEnd();
5289
5290 if (DS.SetTypeSpecType(T: DeclSpec::TST_enum, TagKwLoc: StartLoc,
5291 TagNameLoc: NameLoc.isValid() ? NameLoc : StartLoc,
5292 PrevSpec, DiagID, Rep: TagDecl, Owned,
5293 Policy: Actions.getASTContext().getPrintingPolicy()))
5294 Diag(Loc: StartLoc, DiagID) << PrevSpec;
5295 return;
5296 }
5297
5298 if (IsDependent) {
5299 // This enum has a dependent nested-name-specifier. Handle it as a
5300 // dependent tag.
5301 if (!Name) {
5302 DS.SetTypeSpecError();
5303 Diag(Tok, DiagID: diag::err_expected_type_name_after_typename);
5304 return;
5305 }
5306
5307 TypeResult Type = Actions.ActOnDependentTag(
5308 S: getCurScope(), TagSpec: DeclSpec::TST_enum, TUK, SS, Name, TagLoc: StartLoc, NameLoc);
5309 if (Type.isInvalid()) {
5310 DS.SetTypeSpecError();
5311 return;
5312 }
5313
5314 if (DS.SetTypeSpecType(T: DeclSpec::TST_typename, TagKwLoc: StartLoc,
5315 TagNameLoc: NameLoc.isValid() ? NameLoc : StartLoc,
5316 PrevSpec, DiagID, Rep: Type.get(),
5317 Policy: Actions.getASTContext().getPrintingPolicy()))
5318 Diag(Loc: StartLoc, DiagID) << PrevSpec;
5319
5320 return;
5321 }
5322
5323 if (!TagDecl) {
5324 // The action failed to produce an enumeration tag. If this is a
5325 // definition, consume the entire definition.
5326 if (Tok.is(K: tok::l_brace) && TUK != TagUseKind::Reference) {
5327 ConsumeBrace();
5328 SkipUntil(T: tok::r_brace, Flags: StopAtSemi);
5329 }
5330
5331 DS.SetTypeSpecError();
5332 return;
5333 }
5334
5335 if (Tok.is(K: tok::l_brace) && TUK == TagUseKind::Definition) {
5336 Decl *D = SkipBody.CheckSameAsPrevious ? SkipBody.New : TagDecl;
5337 ParseEnumBody(StartLoc, TagDecl: D, SkipBody: &SkipBody);
5338 if (SkipBody.CheckSameAsPrevious &&
5339 !Actions.ActOnDuplicateDefinition(S: getCurScope(), Prev: TagDecl, SkipBody)) {
5340 DS.SetTypeSpecError();
5341 return;
5342 }
5343 }
5344
5345 if (DS.SetTypeSpecType(T: DeclSpec::TST_enum, TagKwLoc: StartLoc,
5346 TagNameLoc: NameLoc.isValid() ? NameLoc : StartLoc,
5347 PrevSpec, DiagID, Rep: TagDecl, Owned,
5348 Policy: Actions.getASTContext().getPrintingPolicy()))
5349 Diag(Loc: StartLoc, DiagID) << PrevSpec;
5350}
5351
5352void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl,
5353 SkipBodyInfo *SkipBody) {
5354 // Enter the scope of the enum body and start the definition.
5355 ParseScope EnumScope(this, Scope::DeclScope | Scope::EnumScope);
5356 Actions.ActOnTagStartDefinition(S: getCurScope(), TagDecl: EnumDecl);
5357
5358 BalancedDelimiterTracker T(*this, tok::l_brace);
5359 T.consumeOpen();
5360
5361 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
5362 if (Tok.is(K: tok::r_brace) && !getLangOpts().CPlusPlus)
5363 Diag(Tok, DiagID: diag::err_empty_enum);
5364
5365 SmallVector<Decl *, 32> EnumConstantDecls;
5366 SmallVector<SuppressAccessChecks, 32> EnumAvailabilityDiags;
5367
5368 Decl *LastEnumConstDecl = nullptr;
5369
5370 // Parse the enumerator-list.
5371 while (Tok.isNot(K: tok::r_brace)) {
5372 // Parse enumerator. If failed, try skipping till the start of the next
5373 // enumerator definition.
5374 if (Tok.isNot(K: tok::identifier)) {
5375 Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected) << tok::identifier;
5376 if (SkipUntil(T1: tok::comma, T2: tok::r_brace, Flags: StopBeforeMatch) &&
5377 TryConsumeToken(Expected: tok::comma))
5378 continue;
5379 break;
5380 }
5381 IdentifierInfo *Ident = Tok.getIdentifierInfo();
5382 SourceLocation IdentLoc = ConsumeToken();
5383
5384 // If attributes exist after the enumerator, parse them.
5385 ParsedAttributes attrs(AttrFactory);
5386 MaybeParseGNUAttributes(Attrs&: attrs);
5387 if (isAllowedCXX11AttributeSpecifier()) {
5388 if (getLangOpts().CPlusPlus)
5389 Diag(Loc: Tok.getLocation(), DiagID: getLangOpts().CPlusPlus17
5390 ? diag::warn_cxx14_compat_ns_enum_attribute
5391 : diag::ext_ns_enum_attribute)
5392 << 1 /*enumerator*/;
5393 ParseCXX11Attributes(attrs);
5394 }
5395
5396 SourceLocation EqualLoc;
5397 ExprResult AssignedVal;
5398 EnumAvailabilityDiags.emplace_back(Args&: *this);
5399
5400 EnterExpressionEvaluationContext ConstantEvaluated(
5401 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
5402 if (TryConsumeToken(Expected: tok::equal, Loc&: EqualLoc)) {
5403 AssignedVal = ParseConstantExpressionInExprEvalContext();
5404 if (AssignedVal.isInvalid())
5405 SkipUntil(T1: tok::comma, T2: tok::r_brace, Flags: StopBeforeMatch);
5406 }
5407
5408 // Install the enumerator constant into EnumDecl.
5409 Decl *EnumConstDecl = Actions.ActOnEnumConstant(
5410 S: getCurScope(), EnumDecl, LastEnumConstant: LastEnumConstDecl, IdLoc: IdentLoc, Id: Ident, Attrs: attrs,
5411 EqualLoc, Val: AssignedVal.get(), SkipBody);
5412 EnumAvailabilityDiags.back().done();
5413
5414 EnumConstantDecls.push_back(Elt: EnumConstDecl);
5415 LastEnumConstDecl = EnumConstDecl;
5416
5417 if (Tok.is(K: tok::identifier)) {
5418 // We're missing a comma between enumerators.
5419 SourceLocation Loc = getEndOfPreviousToken();
5420 Diag(Loc, DiagID: diag::err_enumerator_list_missing_comma)
5421 << FixItHint::CreateInsertion(InsertionLoc: Loc, Code: ", ");
5422 continue;
5423 }
5424
5425 // Emumerator definition must be finished, only comma or r_brace are
5426 // allowed here.
5427 SourceLocation CommaLoc;
5428 if (Tok.isNot(K: tok::r_brace) && !TryConsumeToken(Expected: tok::comma, Loc&: CommaLoc)) {
5429 if (EqualLoc.isValid())
5430 Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected_either) << tok::r_brace
5431 << tok::comma;
5432 else
5433 Diag(Loc: Tok.getLocation(), DiagID: diag::err_expected_end_of_enumerator);
5434 if (SkipUntil(T1: tok::comma, T2: tok::r_brace, Flags: StopBeforeMatch)) {
5435 if (TryConsumeToken(Expected: tok::comma, Loc&: CommaLoc))
5436 continue;
5437 } else {
5438 break;
5439 }
5440 }
5441
5442 // If comma is followed by r_brace, emit appropriate warning.
5443 if (Tok.is(K: tok::r_brace) && CommaLoc.isValid()) {
5444 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
5445 Diag(Loc: CommaLoc, DiagID: getLangOpts().CPlusPlus ?
5446 diag::ext_enumerator_list_comma_cxx :
5447 diag::ext_enumerator_list_comma_c)
5448 << FixItHint::CreateRemoval(RemoveRange: CommaLoc);
5449 else if (getLangOpts().CPlusPlus11)
5450 Diag(Loc: CommaLoc, DiagID: diag::warn_cxx98_compat_enumerator_list_comma)
5451 << FixItHint::CreateRemoval(RemoveRange: CommaLoc);
5452 break;
5453 }
5454 }
5455
5456 // Eat the }.
5457 T.consumeClose();
5458
5459 // If attributes exist after the identifier list, parse them.
5460 ParsedAttributes attrs(AttrFactory);
5461 MaybeParseGNUAttributes(Attrs&: attrs);
5462
5463 Actions.ActOnEnumBody(EnumLoc: StartLoc, BraceRange: T.getRange(), EnumDecl, Elements: EnumConstantDecls,
5464 S: getCurScope(), Attr: attrs);
5465
5466 // Now handle enum constant availability diagnostics.
5467 assert(EnumConstantDecls.size() == EnumAvailabilityDiags.size());
5468 for (size_t i = 0, e = EnumConstantDecls.size(); i != e; ++i) {
5469 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
5470 EnumAvailabilityDiags[i].redelay();
5471 PD.complete(D: EnumConstantDecls[i]);
5472 }
5473
5474 EnumScope.Exit();
5475 Actions.ActOnTagFinishDefinition(S: getCurScope(), TagDecl: EnumDecl, BraceRange: T.getRange());
5476
5477 // The next token must be valid after an enum definition. If not, a ';'
5478 // was probably forgotten.
5479 bool CanBeBitfield = getCurScope()->isClassScope();
5480 if (!isValidAfterTypeSpecifier(CouldBeBitfield: CanBeBitfield)) {
5481 ExpectAndConsume(ExpectedTok: tok::semi, Diag: diag::err_expected_after, DiagMsg: "enum");
5482 // Push this token back into the preprocessor and change our current token
5483 // to ';' so that the rest of the code recovers as though there were an
5484 // ';' after the definition.
5485 PP.EnterToken(Tok, /*IsReinject=*/true);
5486 Tok.setKind(tok::semi);
5487 }
5488}
5489
5490bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
5491 switch (Tok.getKind()) {
5492 default: return false;
5493 // type-specifiers
5494 case tok::kw_short:
5495 case tok::kw_long:
5496 case tok::kw___int64:
5497 case tok::kw___int128:
5498 case tok::kw_signed:
5499 case tok::kw_unsigned:
5500 case tok::kw__Complex:
5501 case tok::kw__Imaginary:
5502 case tok::kw_void:
5503 case tok::kw_char:
5504 case tok::kw_wchar_t:
5505 case tok::kw_char8_t:
5506 case tok::kw_char16_t:
5507 case tok::kw_char32_t:
5508 case tok::kw_int:
5509 case tok::kw__ExtInt:
5510 case tok::kw__BitInt:
5511 case tok::kw___bf16:
5512 case tok::kw_half:
5513 case tok::kw_float:
5514 case tok::kw_double:
5515 case tok::kw__Accum:
5516 case tok::kw__Fract:
5517 case tok::kw__Float16:
5518 case tok::kw___float128:
5519 case tok::kw___ibm128:
5520 case tok::kw_bool:
5521 case tok::kw__Bool:
5522 case tok::kw__Decimal32:
5523 case tok::kw__Decimal64:
5524 case tok::kw__Decimal128:
5525 case tok::kw___vector:
5526#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
5527#include "clang/Basic/OpenCLImageTypes.def"
5528#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case tok::kw_##Name:
5529#include "clang/Basic/HLSLIntangibleTypes.def"
5530
5531 // struct-or-union-specifier (C99) or class-specifier (C++)
5532 case tok::kw_class:
5533 case tok::kw_struct:
5534 case tok::kw___interface:
5535 case tok::kw_union:
5536 // enum-specifier
5537 case tok::kw_enum:
5538
5539 // typedef-name
5540 case tok::annot_typename:
5541 return true;
5542 }
5543}
5544
5545bool Parser::isTypeSpecifierQualifier() {
5546 switch (Tok.getKind()) {
5547 default: return false;
5548
5549 case tok::identifier: // foo::bar
5550 if (TryAltiVecVectorToken())
5551 return true;
5552 [[fallthrough]];
5553 case tok::kw_typename: // typename T::type
5554 // Annotate typenames and C++ scope specifiers. If we get one, just
5555 // recurse to handle whatever we get.
5556 if (TryAnnotateTypeOrScopeToken())
5557 return true;
5558 if (Tok.is(K: tok::identifier))
5559 return false;
5560 return isTypeSpecifierQualifier();
5561
5562 case tok::coloncolon: // ::foo::bar
5563 if (NextToken().is(K: tok::kw_new) || // ::new
5564 NextToken().is(K: tok::kw_delete)) // ::delete
5565 return false;
5566
5567 if (TryAnnotateTypeOrScopeToken())
5568 return true;
5569 return isTypeSpecifierQualifier();
5570
5571 // GNU attributes support.
5572 case tok::kw___attribute:
5573 // C23/GNU typeof support.
5574 case tok::kw_typeof:
5575 case tok::kw_typeof_unqual:
5576
5577 // type-specifiers
5578 case tok::kw_short:
5579 case tok::kw_long:
5580 case tok::kw___int64:
5581 case tok::kw___int128:
5582 case tok::kw_signed:
5583 case tok::kw_unsigned:
5584 case tok::kw__Complex:
5585 case tok::kw__Imaginary:
5586 case tok::kw_void:
5587 case tok::kw_char:
5588 case tok::kw_wchar_t:
5589 case tok::kw_char8_t:
5590 case tok::kw_char16_t:
5591 case tok::kw_char32_t:
5592 case tok::kw_int:
5593 case tok::kw__ExtInt:
5594 case tok::kw__BitInt:
5595 case tok::kw_half:
5596 case tok::kw___bf16:
5597 case tok::kw_float:
5598 case tok::kw_double:
5599 case tok::kw__Accum:
5600 case tok::kw__Fract:
5601 case tok::kw__Float16:
5602 case tok::kw___float128:
5603 case tok::kw___ibm128:
5604 case tok::kw_bool:
5605 case tok::kw__Bool:
5606 case tok::kw__Decimal32:
5607 case tok::kw__Decimal64:
5608 case tok::kw__Decimal128:
5609 case tok::kw___vector:
5610#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
5611#include "clang/Basic/OpenCLImageTypes.def"
5612#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case tok::kw_##Name:
5613#include "clang/Basic/HLSLIntangibleTypes.def"
5614
5615 // struct-or-union-specifier (C99) or class-specifier (C++)
5616 case tok::kw_class:
5617 case tok::kw_struct:
5618 case tok::kw___interface:
5619 case tok::kw_union:
5620 // enum-specifier
5621 case tok::kw_enum:
5622
5623 // type-qualifier
5624 case tok::kw_const:
5625 case tok::kw_volatile:
5626 case tok::kw_restrict:
5627 case tok::kw__Sat:
5628
5629 // Debugger support.
5630 case tok::kw___unknown_anytype:
5631
5632 // typedef-name
5633 case tok::annot_typename:
5634 return true;
5635
5636 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
5637 case tok::less:
5638 return getLangOpts().ObjC;
5639
5640 case tok::kw___cdecl:
5641 case tok::kw___stdcall:
5642 case tok::kw___fastcall:
5643 case tok::kw___thiscall:
5644 case tok::kw___regcall:
5645 case tok::kw___vectorcall:
5646 case tok::kw___w64:
5647 case tok::kw___ptr64:
5648 case tok::kw___ptr32:
5649 case tok::kw___pascal:
5650 case tok::kw___unaligned:
5651 case tok::kw___ptrauth:
5652
5653 case tok::kw__Nonnull:
5654 case tok::kw__Nullable:
5655 case tok::kw__Nullable_result:
5656 case tok::kw__Null_unspecified:
5657
5658 case tok::kw___kindof:
5659
5660 case tok::kw___private:
5661 case tok::kw___local:
5662 case tok::kw___global:
5663 case tok::kw___constant:
5664 case tok::kw___generic:
5665 case tok::kw___read_only:
5666 case tok::kw___read_write:
5667 case tok::kw___write_only:
5668 case tok::kw___funcref:
5669 return true;
5670
5671 case tok::kw_private:
5672 return getLangOpts().OpenCL;
5673
5674 // C11 _Atomic
5675 case tok::kw__Atomic:
5676 return true;
5677
5678 // HLSL type qualifiers
5679 case tok::kw_groupshared:
5680 case tok::kw_in:
5681 case tok::kw_inout:
5682 case tok::kw_out:
5683 return getLangOpts().HLSL;
5684 }
5685}
5686
5687Parser::DeclGroupPtrTy Parser::ParseTopLevelStmtDecl() {
5688 assert(PP.isIncrementalProcessingEnabled() && "Not in incremental mode");
5689
5690 // Parse a top-level-stmt.
5691 Parser::StmtVector Stmts;
5692 ParsedStmtContext SubStmtCtx = ParsedStmtContext();
5693 ParseScope FnScope(this, Scope::FnScope | Scope::DeclScope |
5694 Scope::CompoundStmtScope);
5695 TopLevelStmtDecl *TLSD = Actions.ActOnStartTopLevelStmtDecl(S: getCurScope());
5696 StmtResult R = ParseStatementOrDeclaration(Stmts, StmtCtx: SubStmtCtx);
5697 if (!R.isUsable())
5698 R = Actions.ActOnNullStmt(SemiLoc: Tok.getLocation());
5699
5700 Actions.ActOnFinishTopLevelStmtDecl(D: TLSD, Statement: R.get());
5701
5702 if (Tok.is(K: tok::annot_repl_input_end) &&
5703 Tok.getAnnotationValue() != nullptr) {
5704 ConsumeAnnotationToken();
5705 TLSD->setSemiMissing();
5706 }
5707
5708 SmallVector<Decl *, 2> DeclsInGroup;
5709 DeclsInGroup.push_back(Elt: TLSD);
5710
5711 // Currently happens for things like -fms-extensions and use `__if_exists`.
5712 for (Stmt *S : Stmts) {
5713 // Here we should be safe as `__if_exists` and friends are not introducing
5714 // new variables which need to live outside file scope.
5715 TopLevelStmtDecl *D = Actions.ActOnStartTopLevelStmtDecl(S: getCurScope());
5716 Actions.ActOnFinishTopLevelStmtDecl(D, Statement: S);
5717 DeclsInGroup.push_back(Elt: D);
5718 }
5719
5720 return Actions.BuildDeclaratorGroup(Group: DeclsInGroup);
5721}
5722
5723bool Parser::isDeclarationSpecifier(
5724 ImplicitTypenameContext AllowImplicitTypename,
5725 bool DisambiguatingWithExpression) {
5726 switch (Tok.getKind()) {
5727 default: return false;
5728
5729 // OpenCL 2.0 and later define this keyword.
5730 case tok::kw_pipe:
5731 return getLangOpts().OpenCL &&
5732 getLangOpts().getOpenCLCompatibleVersion() >= 200;
5733
5734 case tok::identifier: // foo::bar
5735 // Unfortunate hack to support "Class.factoryMethod" notation.
5736 if (getLangOpts().ObjC && NextToken().is(K: tok::period))
5737 return false;
5738 if (TryAltiVecVectorToken())
5739 return true;
5740 [[fallthrough]];
5741 case tok::kw_decltype: // decltype(T())::type
5742 case tok::kw_typename: // typename T::type
5743 // Annotate typenames and C++ scope specifiers. If we get one, just
5744 // recurse to handle whatever we get.
5745 if (TryAnnotateTypeOrScopeToken(AllowImplicitTypename))
5746 return true;
5747 if (TryAnnotateTypeConstraint())
5748 return true;
5749 if (Tok.is(K: tok::identifier))
5750 return false;
5751
5752 // If we're in Objective-C and we have an Objective-C class type followed
5753 // by an identifier and then either ':' or ']', in a place where an
5754 // expression is permitted, then this is probably a class message send
5755 // missing the initial '['. In this case, we won't consider this to be
5756 // the start of a declaration.
5757 if (DisambiguatingWithExpression &&
5758 isStartOfObjCClassMessageMissingOpenBracket())
5759 return false;
5760
5761 return isDeclarationSpecifier(AllowImplicitTypename);
5762
5763 case tok::coloncolon: // ::foo::bar
5764 if (!getLangOpts().CPlusPlus)
5765 return false;
5766 if (NextToken().is(K: tok::kw_new) || // ::new
5767 NextToken().is(K: tok::kw_delete)) // ::delete
5768 return false;
5769
5770 // Annotate typenames and C++ scope specifiers. If we get one, just
5771 // recurse to handle whatever we get.
5772 if (TryAnnotateTypeOrScopeToken())
5773 return true;
5774 return isDeclarationSpecifier(AllowImplicitTypename: ImplicitTypenameContext::No);
5775
5776 // storage-class-specifier
5777 case tok::kw_typedef:
5778 case tok::kw_extern:
5779 case tok::kw___private_extern__:
5780 case tok::kw_static:
5781 case tok::kw_auto:
5782 case tok::kw___auto_type:
5783 case tok::kw_register:
5784 case tok::kw___thread:
5785 case tok::kw_thread_local:
5786 case tok::kw__Thread_local:
5787
5788 // Modules
5789 case tok::kw___module_private__:
5790
5791 // Debugger support
5792 case tok::kw___unknown_anytype:
5793
5794 // type-specifiers
5795 case tok::kw_short:
5796 case tok::kw_long:
5797 case tok::kw___int64:
5798 case tok::kw___int128:
5799 case tok::kw_signed:
5800 case tok::kw_unsigned:
5801 case tok::kw__Complex:
5802 case tok::kw__Imaginary:
5803 case tok::kw_void:
5804 case tok::kw_char:
5805 case tok::kw_wchar_t:
5806 case tok::kw_char8_t:
5807 case tok::kw_char16_t:
5808 case tok::kw_char32_t:
5809
5810 case tok::kw_int:
5811 case tok::kw__ExtInt:
5812 case tok::kw__BitInt:
5813 case tok::kw_half:
5814 case tok::kw___bf16:
5815 case tok::kw_float:
5816 case tok::kw_double:
5817 case tok::kw__Accum:
5818 case tok::kw__Fract:
5819 case tok::kw__Float16:
5820 case tok::kw___float128:
5821 case tok::kw___ibm128:
5822 case tok::kw_bool:
5823 case tok::kw__Bool:
5824 case tok::kw__Decimal32:
5825 case tok::kw__Decimal64:
5826 case tok::kw__Decimal128:
5827 case tok::kw___vector:
5828
5829 // struct-or-union-specifier (C99) or class-specifier (C++)
5830 case tok::kw_class:
5831 case tok::kw_struct:
5832 case tok::kw_union:
5833 case tok::kw___interface:
5834 // enum-specifier
5835 case tok::kw_enum:
5836
5837 // type-qualifier
5838 case tok::kw_const:
5839 case tok::kw_volatile:
5840 case tok::kw_restrict:
5841 case tok::kw__Sat:
5842
5843 // function-specifier
5844 case tok::kw_inline:
5845 case tok::kw_virtual:
5846 case tok::kw_explicit:
5847 case tok::kw__Noreturn:
5848
5849 // alignment-specifier
5850 case tok::kw__Alignas:
5851
5852 // friend keyword.
5853 case tok::kw_friend:
5854
5855 // static_assert-declaration
5856 case tok::kw_static_assert:
5857 case tok::kw__Static_assert:
5858
5859 // C23/GNU typeof support.
5860 case tok::kw_typeof:
5861 case tok::kw_typeof_unqual:
5862
5863 // GNU attributes.
5864 case tok::kw___attribute:
5865
5866 // C++11 decltype and constexpr.
5867 case tok::annot_decltype:
5868 case tok::annot_pack_indexing_type:
5869 case tok::kw_constexpr:
5870
5871 // C++20 consteval and constinit.
5872 case tok::kw_consteval:
5873 case tok::kw_constinit:
5874
5875 // C11 _Atomic
5876 case tok::kw__Atomic:
5877 return true;
5878
5879 case tok::kw_alignas:
5880 // alignas is a type-specifier-qualifier in C23, which is a kind of
5881 // declaration-specifier. Outside of C23 mode (including in C++), it is not.
5882 return getLangOpts().C23;
5883
5884 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
5885 case tok::less:
5886 return getLangOpts().ObjC;
5887
5888 // typedef-name
5889 case tok::annot_typename:
5890 return !DisambiguatingWithExpression ||
5891 !isStartOfObjCClassMessageMissingOpenBracket();
5892
5893 // placeholder-type-specifier
5894 case tok::annot_template_id: {
5895 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(tok: Tok);
5896 if (TemplateId->hasInvalidName())
5897 return true;
5898 // FIXME: What about type templates that have only been annotated as
5899 // annot_template_id, not as annot_typename?
5900 return isTypeConstraintAnnotation() &&
5901 (NextToken().is(K: tok::kw_auto) || NextToken().is(K: tok::kw_decltype));
5902 }
5903
5904 case tok::annot_cxxscope: {
5905 TemplateIdAnnotation *TemplateId =
5906 NextToken().is(K: tok::annot_template_id)
5907 ? takeTemplateIdAnnotation(tok: NextToken())
5908 : nullptr;
5909 if (TemplateId && TemplateId->hasInvalidName())
5910 return true;
5911 // FIXME: What about type templates that have only been annotated as
5912 // annot_template_id, not as annot_typename?
5913 if (NextToken().is(K: tok::identifier) && TryAnnotateTypeConstraint())
5914 return true;
5915 return isTypeConstraintAnnotation() &&
5916 GetLookAheadToken(N: 2).isOneOf(Ks: tok::kw_auto, Ks: tok::kw_decltype);
5917 }
5918
5919 case tok::kw___declspec:
5920 case tok::kw___cdecl:
5921 case tok::kw___stdcall:
5922 case tok::kw___fastcall:
5923 case tok::kw___thiscall:
5924 case tok::kw___regcall:
5925 case tok::kw___vectorcall:
5926 case tok::kw___w64:
5927 case tok::kw___sptr:
5928 case tok::kw___uptr:
5929 case tok::kw___ptr64:
5930 case tok::kw___ptr32:
5931 case tok::kw___forceinline:
5932 case tok::kw___pascal:
5933 case tok::kw___unaligned:
5934 case tok::kw___ptrauth:
5935
5936 case tok::kw__Nonnull:
5937 case tok::kw__Nullable:
5938 case tok::kw__Nullable_result:
5939 case tok::kw__Null_unspecified:
5940
5941 case tok::kw___kindof:
5942
5943 case tok::kw___private:
5944 case tok::kw___local:
5945 case tok::kw___global:
5946 case tok::kw___constant:
5947 case tok::kw___generic:
5948 case tok::kw___read_only:
5949 case tok::kw___read_write:
5950 case tok::kw___write_only:
5951#define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
5952#include "clang/Basic/OpenCLImageTypes.def"
5953#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case tok::kw_##Name:
5954#include "clang/Basic/HLSLIntangibleTypes.def"
5955
5956 case tok::kw___funcref:
5957 case tok::kw_groupshared:
5958 return true;
5959
5960 case tok::kw_private:
5961 return getLangOpts().OpenCL;
5962 }
5963}
5964
5965bool Parser::isConstructorDeclarator(bool IsUnqualified, bool DeductionGuide,
5966 DeclSpec::FriendSpecified IsFriend,
5967 const ParsedTemplateInfo *TemplateInfo) {
5968 RevertingTentativeParsingAction TPA(*this);
5969 // Parse the C++ scope specifier.
5970 CXXScopeSpec SS;
5971 if (TemplateInfo && TemplateInfo->TemplateParams)
5972 SS.setTemplateParamLists(*TemplateInfo->TemplateParams);
5973
5974 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
5975 /*ObjectHasErrors=*/false,
5976 /*EnteringContext=*/true)) {
5977 return false;
5978 }
5979
5980 // Parse the constructor name.
5981 if (Tok.is(K: tok::identifier)) {
5982 // We already know that we have a constructor name; just consume
5983 // the token.
5984 ConsumeToken();
5985 } else if (Tok.is(K: tok::annot_template_id)) {
5986 ConsumeAnnotationToken();
5987 } else {
5988 return false;
5989 }
5990
5991 // There may be attributes here, appertaining to the constructor name or type
5992 // we just stepped past.
5993 SkipCXX11Attributes();
5994
5995 // Current class name must be followed by a left parenthesis.
5996 if (Tok.isNot(K: tok::l_paren)) {
5997 return false;
5998 }
5999 ConsumeParen();
6000
6001 // A right parenthesis, or ellipsis followed by a right parenthesis signals
6002 // that we have a constructor.
6003 if (Tok.is(K: tok::r_paren) ||
6004 (Tok.is(K: tok::ellipsis) && NextToken().is(K: tok::r_paren))) {
6005 return true;
6006 }
6007
6008 // A C++11 attribute here signals that we have a constructor, and is an
6009 // attribute on the first constructor parameter.
6010 if (getLangOpts().CPlusPlus11 &&
6011 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
6012 /*OuterMightBeMessageSend*/ true) !=
6013 CXX11AttributeKind::NotAttributeSpecifier) {
6014 return true;
6015 }
6016
6017 // If we need to, enter the specified scope.
6018 DeclaratorScopeObj DeclScopeObj(*this, SS);
6019 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(S: getCurScope(), SS))
6020 DeclScopeObj.EnterDeclaratorScope();
6021
6022 // Optionally skip Microsoft attributes.
6023 ParsedAttributes Attrs(AttrFactory);
6024 MaybeParseMicrosoftAttributes(Attrs);
6025
6026 // Check whether the next token(s) are part of a declaration
6027 // specifier, in which case we have the start of a parameter and,
6028 // therefore, we know that this is a constructor.
6029 // Due to an ambiguity with implicit typename, the above is not enough.
6030 // Additionally, check to see if we are a friend.
6031 // If we parsed a scope specifier as well as friend,
6032 // we might be parsing a friend constructor.
6033 bool IsConstructor = false;
6034 ImplicitTypenameContext ITC = IsFriend && !SS.isSet()
6035 ? ImplicitTypenameContext::No
6036 : ImplicitTypenameContext::Yes;
6037 // Constructors cannot have this parameters, but we support that scenario here
6038 // to improve diagnostic.
6039 if (Tok.is(K: tok::kw_this)) {
6040 ConsumeToken();
6041 return isDeclarationSpecifier(AllowImplicitTypename: ITC);
6042 }
6043
6044 if (isDeclarationSpecifier(AllowImplicitTypename: ITC))
6045 IsConstructor = true;
6046 else if (Tok.is(K: tok::identifier) ||
6047 (Tok.is(K: tok::annot_cxxscope) && NextToken().is(K: tok::identifier))) {
6048 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
6049 // This might be a parenthesized member name, but is more likely to
6050 // be a constructor declaration with an invalid argument type. Keep
6051 // looking.
6052 if (Tok.is(K: tok::annot_cxxscope))
6053 ConsumeAnnotationToken();
6054 ConsumeToken();
6055
6056 // If this is not a constructor, we must be parsing a declarator,
6057 // which must have one of the following syntactic forms (see the
6058 // grammar extract at the start of ParseDirectDeclarator):
6059 switch (Tok.getKind()) {
6060 case tok::l_paren:
6061 // C(X ( int));
6062 case tok::l_square:
6063 // C(X [ 5]);
6064 // C(X [ [attribute]]);
6065 case tok::coloncolon:
6066 // C(X :: Y);
6067 // C(X :: *p);
6068 // Assume this isn't a constructor, rather than assuming it's a
6069 // constructor with an unnamed parameter of an ill-formed type.
6070 break;
6071
6072 case tok::r_paren:
6073 // C(X )
6074
6075 // Skip past the right-paren and any following attributes to get to
6076 // the function body or trailing-return-type.
6077 ConsumeParen();
6078 SkipCXX11Attributes();
6079
6080 if (DeductionGuide) {
6081 // C(X) -> ... is a deduction guide.
6082 IsConstructor = Tok.is(K: tok::arrow);
6083 break;
6084 }
6085 if (Tok.is(K: tok::colon) || Tok.is(K: tok::kw_try)) {
6086 // Assume these were meant to be constructors:
6087 // C(X) : (the name of a bit-field cannot be parenthesized).
6088 // C(X) try (this is otherwise ill-formed).
6089 IsConstructor = true;
6090 }
6091 if (Tok.is(K: tok::semi) || Tok.is(K: tok::l_brace)) {
6092 // If we have a constructor name within the class definition,
6093 // assume these were meant to be constructors:
6094 // C(X) {
6095 // C(X) ;
6096 // ... because otherwise we would be declaring a non-static data
6097 // member that is ill-formed because it's of the same type as its
6098 // surrounding class.
6099 //
6100 // FIXME: We can actually do this whether or not the name is qualified,
6101 // because if it is qualified in this context it must be being used as
6102 // a constructor name.
6103 // currently, so we're somewhat conservative here.
6104 IsConstructor = IsUnqualified;
6105 }
6106 break;
6107
6108 default:
6109 IsConstructor = true;
6110 break;
6111 }
6112 }
6113 return IsConstructor;
6114}
6115
6116void Parser::ParseTypeQualifierListOpt(
6117 DeclSpec &DS, unsigned AttrReqs, bool AtomicOrPtrauthAllowed,
6118 bool IdentifierRequired, llvm::function_ref<void()> CodeCompletionHandler) {
6119 if ((AttrReqs & AR_CXX11AttributesParsed) &&
6120 isAllowedCXX11AttributeSpecifier()) {
6121 ParsedAttributes Attrs(AttrFactory);
6122 ParseCXX11Attributes(attrs&: Attrs);
6123 DS.takeAttributesFrom(attrs&: Attrs);
6124 }
6125
6126 SourceLocation EndLoc;
6127
6128 while (true) {
6129 bool isInvalid = false;
6130 const char *PrevSpec = nullptr;
6131 unsigned DiagID = 0;
6132 SourceLocation Loc = Tok.getLocation();
6133
6134 switch (Tok.getKind()) {
6135 case tok::code_completion:
6136 cutOffParsing();
6137 if (CodeCompletionHandler)
6138 CodeCompletionHandler();
6139 else
6140 Actions.CodeCompletion().CodeCompleteTypeQualifiers(DS);
6141 return;
6142
6143 case tok::kw_const:
6144 isInvalid = DS.SetTypeQual(T: DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
6145 Lang: getLangOpts());
6146 break;
6147 case tok::kw_volatile:
6148 isInvalid = DS.SetTypeQual(T: DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
6149 Lang: getLangOpts());
6150 break;
6151 case tok::kw_restrict:
6152 isInvalid = DS.SetTypeQual(T: DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
6153 Lang: getLangOpts());
6154 break;
6155 case tok::kw__Atomic:
6156 if (!AtomicOrPtrauthAllowed)
6157 goto DoneWithTypeQuals;
6158 diagnoseUseOfC11Keyword(Tok);
6159 isInvalid = DS.SetTypeQual(T: DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
6160 Lang: getLangOpts());
6161 break;
6162
6163 // OpenCL qualifiers:
6164 case tok::kw_private:
6165 if (!getLangOpts().OpenCL)
6166 goto DoneWithTypeQuals;
6167 [[fallthrough]];
6168 case tok::kw___private:
6169 case tok::kw___global:
6170 case tok::kw___local:
6171 case tok::kw___constant:
6172 case tok::kw___generic:
6173 case tok::kw___read_only:
6174 case tok::kw___write_only:
6175 case tok::kw___read_write:
6176 ParseOpenCLQualifiers(Attrs&: DS.getAttributes());
6177 break;
6178
6179 case tok::kw_groupshared:
6180 case tok::kw_in:
6181 case tok::kw_inout:
6182 case tok::kw_out:
6183 // NOTE: ParseHLSLQualifiers will consume the qualifier token.
6184 ParseHLSLQualifiers(Attrs&: DS.getAttributes());
6185 continue;
6186
6187 // __ptrauth qualifier.
6188 case tok::kw___ptrauth:
6189 if (!AtomicOrPtrauthAllowed)
6190 goto DoneWithTypeQuals;
6191 ParsePtrauthQualifier(Attrs&: DS.getAttributes());
6192 EndLoc = PrevTokLocation;
6193 continue;
6194
6195 case tok::kw___unaligned:
6196 isInvalid = DS.SetTypeQual(T: DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID,
6197 Lang: getLangOpts());
6198 break;
6199 case tok::kw___uptr:
6200 // GNU libc headers in C mode use '__uptr' as an identifier which conflicts
6201 // with the MS modifier keyword.
6202 if ((AttrReqs & AR_DeclspecAttributesParsed) && !getLangOpts().CPlusPlus &&
6203 IdentifierRequired && DS.isEmpty() && NextToken().is(K: tok::semi)) {
6204 if (TryKeywordIdentFallback(DisableKeyword: false))
6205 continue;
6206 }
6207 [[fallthrough]];
6208 case tok::kw___sptr:
6209 case tok::kw___w64:
6210 case tok::kw___ptr64:
6211 case tok::kw___ptr32:
6212 case tok::kw___cdecl:
6213 case tok::kw___stdcall:
6214 case tok::kw___fastcall:
6215 case tok::kw___thiscall:
6216 case tok::kw___regcall:
6217 case tok::kw___vectorcall:
6218 if (AttrReqs & AR_DeclspecAttributesParsed) {
6219 ParseMicrosoftTypeAttributes(attrs&: DS.getAttributes());
6220 continue;
6221 }
6222 goto DoneWithTypeQuals;
6223
6224 case tok::kw___funcref:
6225 ParseWebAssemblyFuncrefTypeAttribute(attrs&: DS.getAttributes());
6226 continue;
6227 goto DoneWithTypeQuals;
6228
6229 case tok::kw___pascal:
6230 if (AttrReqs & AR_VendorAttributesParsed) {
6231 ParseBorlandTypeAttributes(attrs&: DS.getAttributes());
6232 continue;
6233 }
6234 goto DoneWithTypeQuals;
6235
6236 // Nullability type specifiers.
6237 case tok::kw__Nonnull:
6238 case tok::kw__Nullable:
6239 case tok::kw__Nullable_result:
6240 case tok::kw__Null_unspecified:
6241 ParseNullabilityTypeSpecifiers(attrs&: DS.getAttributes());
6242 continue;
6243
6244 // Objective-C 'kindof' types.
6245 case tok::kw___kindof:
6246 DS.getAttributes().addNew(attrName: Tok.getIdentifierInfo(), attrRange: Loc,
6247 scope: AttributeScopeInfo(), args: nullptr, numArgs: 0,
6248 form: tok::kw___kindof);
6249 (void)ConsumeToken();
6250 continue;
6251
6252 case tok::kw___attribute:
6253 if (AttrReqs & AR_GNUAttributesParsedAndRejected)
6254 // When GNU attributes are expressly forbidden, diagnose their usage.
6255 Diag(Tok, DiagID: diag::err_attributes_not_allowed);
6256
6257 // Parse the attributes even if they are rejected to ensure that error
6258 // recovery is graceful.
6259 if (AttrReqs & AR_GNUAttributesParsed ||
6260 AttrReqs & AR_GNUAttributesParsedAndRejected) {
6261 ParseGNUAttributes(Attrs&: DS.getAttributes());
6262 continue; // do *not* consume the next token!
6263 }
6264 // otherwise, FALL THROUGH!
6265 [[fallthrough]];
6266 default:
6267 DoneWithTypeQuals:
6268 // If this is not a type-qualifier token, we're done reading type
6269 // qualifiers. First verify that DeclSpec's are consistent.
6270 DS.Finish(S&: Actions, Policy: Actions.getASTContext().getPrintingPolicy());
6271 if (EndLoc.isValid())
6272 DS.SetRangeEnd(EndLoc);
6273 return;
6274 }
6275
6276 // If the specifier combination wasn't legal, issue a diagnostic.
6277 if (isInvalid) {
6278 assert(PrevSpec && "Method did not return previous specifier!");
6279 Diag(Tok, DiagID) << PrevSpec;
6280 }
6281 EndLoc = ConsumeToken();
6282 }
6283}
6284
6285void Parser::ParseDeclarator(Declarator &D) {
6286 /// This implements the 'declarator' production in the C grammar, then checks
6287 /// for well-formedness and issues diagnostics.
6288 Actions.runWithSufficientStackSpace(Loc: D.getBeginLoc(), Fn: [&] {
6289 ParseDeclaratorInternal(D, DirectDeclParser: &Parser::ParseDirectDeclarator);
6290 });
6291}
6292
6293static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang,
6294 DeclaratorContext TheContext) {
6295 if (Kind == tok::star || Kind == tok::caret)
6296 return true;
6297
6298 // OpenCL 2.0 and later define this keyword.
6299 if (Kind == tok::kw_pipe && Lang.OpenCL &&
6300 Lang.getOpenCLCompatibleVersion() >= 200)
6301 return true;
6302
6303 if (!Lang.CPlusPlus)
6304 return false;
6305
6306 if (Kind == tok::amp)
6307 return true;
6308
6309 // We parse rvalue refs in C++03, because otherwise the errors are scary.
6310 // But we must not parse them in conversion-type-ids and new-type-ids, since
6311 // those can be legitimately followed by a && operator.
6312 // (The same thing can in theory happen after a trailing-return-type, but
6313 // since those are a C++11 feature, there is no rejects-valid issue there.)
6314 if (Kind == tok::ampamp)
6315 return Lang.CPlusPlus11 || (TheContext != DeclaratorContext::ConversionId &&
6316 TheContext != DeclaratorContext::CXXNew);
6317
6318 return false;
6319}
6320
6321// Indicates whether the given declarator is a pipe declarator.
6322static bool isPipeDeclarator(const Declarator &D) {
6323 const unsigned NumTypes = D.getNumTypeObjects();
6324
6325 for (unsigned Idx = 0; Idx != NumTypes; ++Idx)
6326 if (DeclaratorChunk::Pipe == D.getTypeObject(i: Idx).Kind)
6327 return true;
6328
6329 return false;
6330}
6331
6332void Parser::ParseDeclaratorInternal(Declarator &D,
6333 DirectDeclParseFunction DirectDeclParser) {
6334 if (Diags.hasAllExtensionsSilenced())
6335 D.setExtension();
6336
6337 // C++ member pointers start with a '::' or a nested-name.
6338 // Member pointers get special handling, since there's no place for the
6339 // scope spec in the generic path below.
6340 if (getLangOpts().CPlusPlus &&
6341 (Tok.is(K: tok::coloncolon) || Tok.is(K: tok::kw_decltype) ||
6342 (Tok.is(K: tok::identifier) &&
6343 (NextToken().is(K: tok::coloncolon) || NextToken().is(K: tok::less))) ||
6344 Tok.is(K: tok::annot_cxxscope))) {
6345 TentativeParsingAction TPA(*this, /*Unannotated=*/true);
6346 bool EnteringContext = D.getContext() == DeclaratorContext::File ||
6347 D.getContext() == DeclaratorContext::Member;
6348 CXXScopeSpec SS;
6349 SS.setTemplateParamLists(D.getTemplateParameterLists());
6350
6351 if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
6352 /*ObjectHasErrors=*/false,
6353 /*EnteringContext=*/false,
6354 /*MayBePseudoDestructor=*/nullptr,
6355 /*IsTypename=*/false, /*LastII=*/nullptr,
6356 /*OnlyNamespace=*/false,
6357 /*InUsingDeclaration=*/false,
6358 /*Disambiguation=*/EnteringContext) ||
6359
6360 SS.isEmpty() || SS.isInvalid() || !EnteringContext ||
6361 Tok.is(K: tok::star)) {
6362 TPA.Commit();
6363 if (SS.isNotEmpty() && Tok.is(K: tok::star)) {
6364 if (SS.isValid()) {
6365 checkCompoundToken(FirstTokLoc: SS.getEndLoc(), FirstTokKind: tok::coloncolon,
6366 Op: CompoundToken::MemberPtr);
6367 }
6368
6369 SourceLocation StarLoc = ConsumeToken();
6370 D.SetRangeEnd(StarLoc);
6371 DeclSpec DS(AttrFactory);
6372 ParseTypeQualifierListOpt(DS);
6373 D.ExtendWithDeclSpec(DS);
6374
6375 // Recurse to parse whatever is left.
6376 Actions.runWithSufficientStackSpace(Loc: D.getBeginLoc(), Fn: [&] {
6377 ParseDeclaratorInternal(D, DirectDeclParser);
6378 });
6379
6380 // Sema will have to catch (syntactically invalid) pointers into global
6381 // scope. It has to catch pointers into namespace scope anyway.
6382 D.AddTypeInfo(TI: DeclaratorChunk::getMemberPointer(
6383 SS, TypeQuals: DS.getTypeQualifiers(), StarLoc, EndLoc: DS.getEndLoc()),
6384 attrs: std::move(DS.getAttributes()),
6385 /*EndLoc=*/SourceLocation());
6386 return;
6387 }
6388 } else {
6389 TPA.Revert();
6390 SS.clear();
6391 ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
6392 /*ObjectHasErrors=*/false,
6393 /*EnteringContext=*/true);
6394 }
6395
6396 if (SS.isNotEmpty()) {
6397 // The scope spec really belongs to the direct-declarator.
6398 if (D.mayHaveIdentifier())
6399 D.getCXXScopeSpec() = SS;
6400 else
6401 AnnotateScopeToken(SS, IsNewAnnotation: true);
6402
6403 if (DirectDeclParser)
6404 (this->*DirectDeclParser)(D);
6405 return;
6406 }
6407 }
6408
6409 tok::TokenKind Kind = Tok.getKind();
6410
6411 if (D.getDeclSpec().isTypeSpecPipe() && !isPipeDeclarator(D)) {
6412 DeclSpec DS(AttrFactory);
6413 ParseTypeQualifierListOpt(DS);
6414
6415 D.AddTypeInfo(
6416 TI: DeclaratorChunk::getPipe(TypeQuals: DS.getTypeQualifiers(), Loc: DS.getPipeLoc()),
6417 attrs: std::move(DS.getAttributes()), EndLoc: SourceLocation());
6418 }
6419
6420 // Not a pointer, C++ reference, or block.
6421 if (!isPtrOperatorToken(Kind, Lang: getLangOpts(), TheContext: D.getContext())) {
6422 if (DirectDeclParser)
6423 (this->*DirectDeclParser)(D);
6424 return;
6425 }
6426
6427 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
6428 // '&&' -> rvalue reference
6429 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
6430 D.SetRangeEnd(Loc);
6431
6432 if (Kind == tok::star || Kind == tok::caret) {
6433 // Is a pointer.
6434 DeclSpec DS(AttrFactory);
6435
6436 // GNU attributes are not allowed here in a new-type-id, but Declspec and
6437 // C++11 attributes are allowed.
6438 unsigned Reqs = AR_CXX11AttributesParsed | AR_DeclspecAttributesParsed |
6439 ((D.getContext() != DeclaratorContext::CXXNew)
6440 ? AR_GNUAttributesParsed
6441 : AR_GNUAttributesParsedAndRejected);
6442 ParseTypeQualifierListOpt(DS, AttrReqs: Reqs, /*AtomicOrPtrauthAllowed=*/true,
6443 IdentifierRequired: !D.mayOmitIdentifier());
6444 D.ExtendWithDeclSpec(DS);
6445
6446 // Recursively parse the declarator.
6447 Actions.runWithSufficientStackSpace(
6448 Loc: D.getBeginLoc(), Fn: [&] { ParseDeclaratorInternal(D, DirectDeclParser); });
6449 if (Kind == tok::star)
6450 // Remember that we parsed a pointer type, and remember the type-quals.
6451 D.AddTypeInfo(TI: DeclaratorChunk::getPointer(
6452 TypeQuals: DS.getTypeQualifiers(), Loc, ConstQualLoc: DS.getConstSpecLoc(),
6453 VolatileQualLoc: DS.getVolatileSpecLoc(), RestrictQualLoc: DS.getRestrictSpecLoc(),
6454 AtomicQualLoc: DS.getAtomicSpecLoc(), UnalignedQualLoc: DS.getUnalignedSpecLoc()),
6455 attrs: std::move(DS.getAttributes()), EndLoc: SourceLocation());
6456 else
6457 // Remember that we parsed a Block type, and remember the type-quals.
6458 D.AddTypeInfo(
6459 TI: DeclaratorChunk::getBlockPointer(TypeQuals: DS.getTypeQualifiers(), Loc),
6460 attrs: std::move(DS.getAttributes()), EndLoc: SourceLocation());
6461 } else {
6462 // Is a reference
6463 DeclSpec DS(AttrFactory);
6464
6465 // Complain about rvalue references in C++03, but then go on and build
6466 // the declarator.
6467 if (Kind == tok::ampamp)
6468 Diag(Loc, DiagID: getLangOpts().CPlusPlus11 ?
6469 diag::warn_cxx98_compat_rvalue_reference :
6470 diag::ext_rvalue_reference);
6471
6472 // GNU-style and C++11 attributes are allowed here, as is restrict.
6473 ParseTypeQualifierListOpt(DS);
6474 D.ExtendWithDeclSpec(DS);
6475
6476 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
6477 // cv-qualifiers are introduced through the use of a typedef or of a
6478 // template type argument, in which case the cv-qualifiers are ignored.
6479 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
6480 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
6481 Diag(Loc: DS.getConstSpecLoc(),
6482 DiagID: diag::err_invalid_reference_qualifier_application) << "const";
6483 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
6484 Diag(Loc: DS.getVolatileSpecLoc(),
6485 DiagID: diag::err_invalid_reference_qualifier_application) << "volatile";
6486 // 'restrict' is permitted as an extension.
6487 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
6488 Diag(Loc: DS.getAtomicSpecLoc(),
6489 DiagID: diag::err_invalid_reference_qualifier_application) << "_Atomic";
6490 }
6491
6492 // Recursively parse the declarator.
6493 Actions.runWithSufficientStackSpace(
6494 Loc: D.getBeginLoc(), Fn: [&] { ParseDeclaratorInternal(D, DirectDeclParser); });
6495
6496 if (D.getNumTypeObjects() > 0) {
6497 // C++ [dcl.ref]p4: There shall be no references to references.
6498 DeclaratorChunk& InnerChunk = D.getTypeObject(i: D.getNumTypeObjects() - 1);
6499 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
6500 if (const IdentifierInfo *II = D.getIdentifier())
6501 Diag(Loc: InnerChunk.Loc, DiagID: diag::err_illegal_decl_reference_to_reference)
6502 << II;
6503 else
6504 Diag(Loc: InnerChunk.Loc, DiagID: diag::err_illegal_decl_reference_to_reference)
6505 << "type name";
6506
6507 // Once we've complained about the reference-to-reference, we
6508 // can go ahead and build the (technically ill-formed)
6509 // declarator: reference collapsing will take care of it.
6510 }
6511 }
6512
6513 // Remember that we parsed a reference type.
6514 D.AddTypeInfo(TI: DeclaratorChunk::getReference(TypeQuals: DS.getTypeQualifiers(), Loc,
6515 lvalue: Kind == tok::amp),
6516 attrs: std::move(DS.getAttributes()), EndLoc: SourceLocation());
6517 }
6518}
6519
6520// When correcting from misplaced brackets before the identifier, the location
6521// is saved inside the declarator so that other diagnostic messages can use
6522// them. This extracts and returns that location, or returns the provided
6523// location if a stored location does not exist.
6524static SourceLocation getMissingDeclaratorIdLoc(Declarator &D,
6525 SourceLocation Loc) {
6526 if (D.getName().StartLocation.isInvalid() &&
6527 D.getName().EndLocation.isValid())
6528 return D.getName().EndLocation;
6529
6530 return Loc;
6531}
6532
6533void Parser::ParseDirectDeclarator(Declarator &D) {
6534 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
6535
6536 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
6537 // This might be a C++17 structured binding.
6538 if (Tok.is(K: tok::l_square) && !D.mayOmitIdentifier() &&
6539 D.getCXXScopeSpec().isEmpty())
6540 return ParseDecompositionDeclarator(D);
6541
6542 // Don't parse FOO:BAR as if it were a typo for FOO::BAR inside a class, in
6543 // this context it is a bitfield. Also in range-based for statement colon
6544 // may delimit for-range-declaration.
6545 ColonProtectionRAIIObject X(
6546 *this, D.getContext() == DeclaratorContext::Member ||
6547 (D.getContext() == DeclaratorContext::ForInit &&
6548 getLangOpts().CPlusPlus11));
6549
6550 // ParseDeclaratorInternal might already have parsed the scope.
6551 if (D.getCXXScopeSpec().isEmpty()) {
6552 bool EnteringContext = D.getContext() == DeclaratorContext::File ||
6553 D.getContext() == DeclaratorContext::Member;
6554 ParseOptionalCXXScopeSpecifier(
6555 SS&: D.getCXXScopeSpec(), /*ObjectType=*/nullptr,
6556 /*ObjectHasErrors=*/false, EnteringContext);
6557 }
6558
6559 // C++23 [basic.scope.namespace]p1:
6560 // For each non-friend redeclaration or specialization whose target scope
6561 // is or is contained by the scope, the portion after the declarator-id,
6562 // class-head-name, or enum-head-name is also included in the scope.
6563 // C++23 [basic.scope.class]p1:
6564 // For each non-friend redeclaration or specialization whose target scope
6565 // is or is contained by the scope, the portion after the declarator-id,
6566 // class-head-name, or enum-head-name is also included in the scope.
6567 //
6568 // FIXME: We should not be doing this for friend declarations; they have
6569 // their own special lookup semantics specified by [basic.lookup.unqual]p6.
6570 if (D.getCXXScopeSpec().isValid()) {
6571 if (Actions.ShouldEnterDeclaratorScope(S: getCurScope(),
6572 SS: D.getCXXScopeSpec()))
6573 // Change the declaration context for name lookup, until this function
6574 // is exited (and the declarator has been parsed).
6575 DeclScopeObj.EnterDeclaratorScope();
6576 else if (getObjCDeclContext()) {
6577 // Ensure that we don't interpret the next token as an identifier when
6578 // dealing with declarations in an Objective-C container.
6579 D.SetIdentifier(Id: nullptr, IdLoc: Tok.getLocation());
6580 D.setInvalidType(true);
6581 ConsumeToken();
6582 goto PastIdentifier;
6583 }
6584 }
6585
6586 // C++0x [dcl.fct]p14:
6587 // There is a syntactic ambiguity when an ellipsis occurs at the end of a
6588 // parameter-declaration-clause without a preceding comma. In this case,
6589 // the ellipsis is parsed as part of the abstract-declarator if the type
6590 // of the parameter either names a template parameter pack that has not
6591 // been expanded or contains auto; otherwise, it is parsed as part of the
6592 // parameter-declaration-clause.
6593 if (Tok.is(K: tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
6594 !((D.getContext() == DeclaratorContext::Prototype ||
6595 D.getContext() == DeclaratorContext::LambdaExprParameter ||
6596 D.getContext() == DeclaratorContext::BlockLiteral) &&
6597 NextToken().is(K: tok::r_paren) && !D.hasGroupingParens() &&
6598 !Actions.containsUnexpandedParameterPacks(D) &&
6599 D.getDeclSpec().getTypeSpecType() != TST_auto)) {
6600 SourceLocation EllipsisLoc = ConsumeToken();
6601 if (isPtrOperatorToken(Kind: Tok.getKind(), Lang: getLangOpts(), TheContext: D.getContext())) {
6602 // The ellipsis was put in the wrong place. Recover, and explain to
6603 // the user what they should have done.
6604 ParseDeclarator(D);
6605 if (EllipsisLoc.isValid())
6606 DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
6607 return;
6608 } else
6609 D.setEllipsisLoc(EllipsisLoc);
6610
6611 // The ellipsis can't be followed by a parenthesized declarator. We
6612 // check for that in ParseParenDeclarator, after we have disambiguated
6613 // the l_paren token.
6614 }
6615
6616 if (Tok.isOneOf(Ks: tok::identifier, Ks: tok::kw_operator, Ks: tok::annot_template_id,
6617 Ks: tok::tilde)) {
6618 // We found something that indicates the start of an unqualified-id.
6619 // Parse that unqualified-id.
6620 bool AllowConstructorName;
6621 bool AllowDeductionGuide;
6622 if (D.getDeclSpec().hasTypeSpecifier()) {
6623 AllowConstructorName = false;
6624 AllowDeductionGuide = false;
6625 } else if (D.getCXXScopeSpec().isSet()) {
6626 AllowConstructorName = (D.getContext() == DeclaratorContext::File ||
6627 D.getContext() == DeclaratorContext::Member);
6628 AllowDeductionGuide = false;
6629 } else {
6630 AllowConstructorName = (D.getContext() == DeclaratorContext::Member);
6631 AllowDeductionGuide = (D.getContext() == DeclaratorContext::File ||
6632 D.getContext() == DeclaratorContext::Member);
6633 }
6634
6635 bool HadScope = D.getCXXScopeSpec().isValid();
6636 SourceLocation TemplateKWLoc;
6637 if (ParseUnqualifiedId(SS&: D.getCXXScopeSpec(),
6638 /*ObjectType=*/nullptr,
6639 /*ObjectHadErrors=*/false,
6640 /*EnteringContext=*/true,
6641 /*AllowDestructorName=*/true, AllowConstructorName,
6642 AllowDeductionGuide, TemplateKWLoc: &TemplateKWLoc,
6643 Result&: D.getName()) ||
6644 // Once we're past the identifier, if the scope was bad, mark the
6645 // whole declarator bad.
6646 D.getCXXScopeSpec().isInvalid()) {
6647 D.SetIdentifier(Id: nullptr, IdLoc: Tok.getLocation());
6648 D.setInvalidType(true);
6649 } else {
6650 // ParseUnqualifiedId might have parsed a scope specifier during error
6651 // recovery. If it did so, enter that scope.
6652 if (!HadScope && D.getCXXScopeSpec().isValid() &&
6653 Actions.ShouldEnterDeclaratorScope(S: getCurScope(),
6654 SS: D.getCXXScopeSpec()))
6655 DeclScopeObj.EnterDeclaratorScope();
6656
6657 // Parsed the unqualified-id; update range information and move along.
6658 if (D.getSourceRange().getBegin().isInvalid())
6659 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
6660 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
6661 }
6662 goto PastIdentifier;
6663 }
6664
6665 if (D.getCXXScopeSpec().isNotEmpty()) {
6666 // We have a scope specifier but no following unqualified-id.
6667 Diag(Loc: PP.getLocForEndOfToken(Loc: D.getCXXScopeSpec().getEndLoc()),
6668 DiagID: diag::err_expected_unqualified_id)
6669 << /*C++*/1;
6670 D.SetIdentifier(Id: nullptr, IdLoc: Tok.getLocation());
6671 goto PastIdentifier;
6672 }
6673 } else if (Tok.is(K: tok::identifier) && D.mayHaveIdentifier()) {
6674 assert(!getLangOpts().CPlusPlus &&
6675 "There's a C++-specific check for tok::identifier above");
6676 assert(Tok.getIdentifierInfo() && "Not an identifier?");
6677 D.SetIdentifier(Id: Tok.getIdentifierInfo(), IdLoc: Tok.getLocation());
6678 D.SetRangeEnd(Tok.getLocation());
6679 ConsumeToken();
6680 goto PastIdentifier;
6681 } else if (Tok.is(K: tok::identifier) && !D.mayHaveIdentifier()) {
6682 // We're not allowed an identifier here, but we got one. Try to figure out
6683 // if the user was trying to attach a name to the type, or whether the name
6684 // is some unrelated trailing syntax.
6685 bool DiagnoseIdentifier = false;
6686 if (D.hasGroupingParens())
6687 // An identifier within parens is unlikely to be intended to be anything
6688 // other than a name being "declared".
6689 DiagnoseIdentifier = true;
6690 else if (D.getContext() == DeclaratorContext::TemplateArg)
6691 // T<int N> is an accidental identifier; T<int N indicates a missing '>'.
6692 DiagnoseIdentifier =
6693 NextToken().isOneOf(Ks: tok::comma, Ks: tok::greater, Ks: tok::greatergreater);
6694 else if (D.getContext() == DeclaratorContext::AliasDecl ||
6695 D.getContext() == DeclaratorContext::AliasTemplate)
6696 // The most likely error is that the ';' was forgotten.
6697 DiagnoseIdentifier = NextToken().isOneOf(Ks: tok::comma, Ks: tok::semi);
6698 else if ((D.getContext() == DeclaratorContext::TrailingReturn ||
6699 D.getContext() == DeclaratorContext::TrailingReturnVar) &&
6700 !isCXX11VirtSpecifier(Tok))
6701 DiagnoseIdentifier = NextToken().isOneOf(
6702 Ks: tok::comma, Ks: tok::semi, Ks: tok::equal, Ks: tok::l_brace, Ks: tok::kw_try);
6703 if (DiagnoseIdentifier) {
6704 Diag(Loc: Tok.getLocation(), DiagID: diag::err_unexpected_unqualified_id)
6705 << FixItHint::CreateRemoval(RemoveRange: Tok.getLocation());
6706 D.SetIdentifier(Id: nullptr, IdLoc: Tok.getLocation());
6707 ConsumeToken();
6708 goto PastIdentifier;
6709 }
6710 }
6711
6712 if (Tok.is(K: tok::l_paren)) {
6713 // If this might be an abstract-declarator followed by a direct-initializer,
6714 // check whether this is a valid declarator chunk. If it can't be, assume
6715 // that it's an initializer instead.
6716 if (D.mayOmitIdentifier() && D.mayBeFollowedByCXXDirectInit()) {
6717 RevertingTentativeParsingAction PA(*this);
6718 if (TryParseDeclarator(mayBeAbstract: true, mayHaveIdentifier: D.mayHaveIdentifier(), mayHaveDirectInit: true,
6719 mayHaveTrailingReturnType: D.getDeclSpec().getTypeSpecType() == TST_auto) ==
6720 TPResult::False) {
6721 D.SetIdentifier(Id: nullptr, IdLoc: Tok.getLocation());
6722 goto PastIdentifier;
6723 }
6724 }
6725
6726 // direct-declarator: '(' declarator ')'
6727 // direct-declarator: '(' attributes declarator ')'
6728 // Example: 'char (*X)' or 'int (*XX)(void)'
6729 ParseParenDeclarator(D);
6730
6731 // If the declarator was parenthesized, we entered the declarator
6732 // scope when parsing the parenthesized declarator, then exited
6733 // the scope already. Re-enter the scope, if we need to.
6734 if (D.getCXXScopeSpec().isSet()) {
6735 // If there was an error parsing parenthesized declarator, declarator
6736 // scope may have been entered before. Don't do it again.
6737 if (!D.isInvalidType() &&
6738 Actions.ShouldEnterDeclaratorScope(S: getCurScope(),
6739 SS: D.getCXXScopeSpec()))
6740 // Change the declaration context for name lookup, until this function
6741 // is exited (and the declarator has been parsed).
6742 DeclScopeObj.EnterDeclaratorScope();
6743 }
6744 } else if (D.mayOmitIdentifier()) {
6745 // This could be something simple like "int" (in which case the declarator
6746 // portion is empty), if an abstract-declarator is allowed.
6747 D.SetIdentifier(Id: nullptr, IdLoc: Tok.getLocation());
6748
6749 // The grammar for abstract-pack-declarator does not allow grouping parens.
6750 // FIXME: Revisit this once core issue 1488 is resolved.
6751 if (D.hasEllipsis() && D.hasGroupingParens())
6752 Diag(Loc: PP.getLocForEndOfToken(Loc: D.getEllipsisLoc()),
6753 DiagID: diag::ext_abstract_pack_declarator_parens);
6754 } else {
6755 if (Tok.getKind() == tok::annot_pragma_parser_crash)
6756 LLVM_BUILTIN_TRAP;
6757 if (Tok.is(K: tok::l_square))
6758 return ParseMisplacedBracketDeclarator(D);
6759 if (D.getContext() == DeclaratorContext::Member) {
6760 // Objective-C++: Detect C++ keywords and try to prevent further errors by
6761 // treating these keyword as valid member names.
6762 if (getLangOpts().ObjC && getLangOpts().CPlusPlus &&
6763 !Tok.isAnnotation() && Tok.getIdentifierInfo() &&
6764 Tok.getIdentifierInfo()->isCPlusPlusKeyword(LangOpts: getLangOpts())) {
6765 Diag(Loc: getMissingDeclaratorIdLoc(D, Loc: Tok.getLocation()),
6766 DiagID: diag::err_expected_member_name_or_semi_objcxx_keyword)
6767 << Tok.getIdentifierInfo()
6768 << (D.getDeclSpec().isEmpty() ? SourceRange()
6769 : D.getDeclSpec().getSourceRange());
6770 D.SetIdentifier(Id: Tok.getIdentifierInfo(), IdLoc: Tok.getLocation());
6771 D.SetRangeEnd(Tok.getLocation());
6772 ConsumeToken();
6773 goto PastIdentifier;
6774 }
6775 Diag(Loc: getMissingDeclaratorIdLoc(D, Loc: Tok.getLocation()),
6776 DiagID: diag::err_expected_member_name_or_semi)
6777 << (D.getDeclSpec().isEmpty() ? SourceRange()
6778 : D.getDeclSpec().getSourceRange());
6779 } else {
6780 if (Tok.getKind() == tok::TokenKind::kw_while) {
6781 Diag(Tok, DiagID: diag::err_while_loop_outside_of_a_function);
6782 } else if (getLangOpts().CPlusPlus) {
6783 if (Tok.isOneOf(Ks: tok::period, Ks: tok::arrow))
6784 Diag(Tok, DiagID: diag::err_invalid_operator_on_type) << Tok.is(K: tok::arrow);
6785 else {
6786 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
6787 if (Tok.isAtStartOfLine() && Loc.isValid())
6788 Diag(Loc: PP.getLocForEndOfToken(Loc), DiagID: diag::err_expected_unqualified_id)
6789 << getLangOpts().CPlusPlus;
6790 else
6791 Diag(Loc: getMissingDeclaratorIdLoc(D, Loc: Tok.getLocation()),
6792 DiagID: diag::err_expected_unqualified_id)
6793 << getLangOpts().CPlusPlus;
6794 }
6795 } else {
6796 Diag(Loc: getMissingDeclaratorIdLoc(D, Loc: Tok.getLocation()),
6797 DiagID: diag::err_expected_either)
6798 << tok::identifier << tok::l_paren;
6799 }
6800 }
6801 D.SetIdentifier(Id: nullptr, IdLoc: Tok.getLocation());
6802 D.setInvalidType(true);
6803 }
6804
6805 PastIdentifier:
6806 assert(D.isPastIdentifier() &&
6807 "Haven't past the location of the identifier yet?");
6808
6809 // Don't parse attributes unless we have parsed an unparenthesized name.
6810 if (D.hasName() && !D.getNumTypeObjects())
6811 MaybeParseCXX11Attributes(D);
6812
6813 while (true) {
6814 if (Tok.is(K: tok::l_paren)) {
6815 bool IsFunctionDeclaration = D.isFunctionDeclaratorAFunctionDeclaration();
6816 // Enter function-declaration scope, limiting any declarators to the
6817 // function prototype scope, including parameter declarators.
6818 ParseScope PrototypeScope(this,
6819 Scope::FunctionPrototypeScope|Scope::DeclScope|
6820 (IsFunctionDeclaration
6821 ? Scope::FunctionDeclarationScope : 0));
6822
6823 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
6824 // In such a case, check if we actually have a function declarator; if it
6825 // is not, the declarator has been fully parsed.
6826 bool IsAmbiguous = false;
6827 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
6828 // C++2a [temp.res]p5
6829 // A qualified-id is assumed to name a type if
6830 // - [...]
6831 // - it is a decl-specifier of the decl-specifier-seq of a
6832 // - [...]
6833 // - parameter-declaration in a member-declaration [...]
6834 // - parameter-declaration in a declarator of a function or function
6835 // template declaration whose declarator-id is qualified [...]
6836 auto AllowImplicitTypename = ImplicitTypenameContext::No;
6837 if (D.getCXXScopeSpec().isSet())
6838 AllowImplicitTypename =
6839 (ImplicitTypenameContext)Actions.isDeclaratorFunctionLike(D);
6840 else if (D.getContext() == DeclaratorContext::Member) {
6841 AllowImplicitTypename = ImplicitTypenameContext::Yes;
6842 }
6843
6844 // The name of the declarator, if any, is tentatively declared within
6845 // a possible direct initializer.
6846 TentativelyDeclaredIdentifiers.push_back(Elt: D.getIdentifier());
6847 bool IsFunctionDecl =
6848 isCXXFunctionDeclarator(IsAmbiguous: &IsAmbiguous, AllowImplicitTypename);
6849 TentativelyDeclaredIdentifiers.pop_back();
6850 if (!IsFunctionDecl)
6851 break;
6852 }
6853 ParsedAttributes attrs(AttrFactory);
6854 BalancedDelimiterTracker T(*this, tok::l_paren);
6855 T.consumeOpen();
6856 if (IsFunctionDeclaration)
6857 Actions.ActOnStartFunctionDeclarationDeclarator(D,
6858 TemplateParameterDepth);
6859 ParseFunctionDeclarator(D, FirstArgAttrs&: attrs, Tracker&: T, IsAmbiguous);
6860 if (IsFunctionDeclaration)
6861 Actions.ActOnFinishFunctionDeclarationDeclarator(D);
6862 PrototypeScope.Exit();
6863 } else if (Tok.is(K: tok::l_square)) {
6864 ParseBracketDeclarator(D);
6865 } else if (Tok.isRegularKeywordAttribute()) {
6866 // For consistency with attribute parsing.
6867 Diag(Tok, DiagID: diag::err_keyword_not_allowed) << Tok.getIdentifierInfo();
6868 bool TakesArgs = doesKeywordAttributeTakeArgs(Kind: Tok.getKind());
6869 ConsumeToken();
6870 if (TakesArgs) {
6871 BalancedDelimiterTracker T(*this, tok::l_paren);
6872 if (!T.consumeOpen())
6873 T.skipToEnd();
6874 }
6875 } else if (Tok.is(K: tok::kw_requires) && D.hasGroupingParens()) {
6876 // This declarator is declaring a function, but the requires clause is
6877 // in the wrong place:
6878 // void (f() requires true);
6879 // instead of
6880 // void f() requires true;
6881 // or
6882 // void (f()) requires true;
6883 Diag(Tok, DiagID: diag::err_requires_clause_inside_parens);
6884 ConsumeToken();
6885 ExprResult TrailingRequiresClause =
6886 ParseConstraintLogicalOrExpression(/*IsTrailingRequiresClause=*/true);
6887 if (TrailingRequiresClause.isUsable() && D.isFunctionDeclarator() &&
6888 !D.hasTrailingRequiresClause())
6889 // We're already ill-formed if we got here but we'll accept it anyway.
6890 D.setTrailingRequiresClause(TrailingRequiresClause.get());
6891 } else {
6892 break;
6893 }
6894 }
6895}
6896
6897void Parser::ParseDecompositionDeclarator(Declarator &D) {
6898 assert(Tok.is(tok::l_square));
6899
6900 TentativeParsingAction PA(*this);
6901 BalancedDelimiterTracker T(*this, tok::l_square);
6902 T.consumeOpen();
6903
6904 if (isCXX11AttributeSpecifier() != CXX11AttributeKind::NotAttributeSpecifier)
6905 DiagnoseAndSkipCXX11Attributes();
6906
6907 // If this doesn't look like a structured binding, maybe it's a misplaced
6908 // array declarator.
6909 if (!(Tok.isOneOf(Ks: tok::identifier, Ks: tok::ellipsis) &&
6910 NextToken().isOneOf(Ks: tok::comma, Ks: tok::r_square, Ks: tok::kw_alignas,
6911 Ks: tok::identifier, Ks: tok::l_square, Ks: tok::ellipsis)) &&
6912 !(Tok.is(K: tok::r_square) &&
6913 NextToken().isOneOf(Ks: tok::equal, Ks: tok::l_brace))) {
6914 PA.Revert();
6915 return ParseMisplacedBracketDeclarator(D);
6916 }
6917
6918 SourceLocation PrevEllipsisLoc;
6919 SmallVector<DecompositionDeclarator::Binding, 32> Bindings;
6920 while (Tok.isNot(K: tok::r_square)) {
6921 if (!Bindings.empty()) {
6922 if (Tok.is(K: tok::comma))
6923 ConsumeToken();
6924 else {
6925 if (Tok.is(K: tok::identifier)) {
6926 SourceLocation EndLoc = getEndOfPreviousToken();
6927 Diag(Loc: EndLoc, DiagID: diag::err_expected)
6928 << tok::comma << FixItHint::CreateInsertion(InsertionLoc: EndLoc, Code: ",");
6929 } else {
6930 Diag(Tok, DiagID: diag::err_expected_comma_or_rsquare);
6931 }
6932
6933 SkipUntil(Toks: {tok::r_square, tok::comma, tok::identifier, tok::ellipsis},
6934 Flags: StopAtSemi | StopBeforeMatch);
6935 if (Tok.is(K: tok::comma))
6936 ConsumeToken();
6937 else if (Tok.is(K: tok::r_square))
6938 break;
6939 }
6940 }
6941
6942 if (isCXX11AttributeSpecifier() !=
6943 CXX11AttributeKind::NotAttributeSpecifier)
6944 DiagnoseAndSkipCXX11Attributes();
6945
6946 SourceLocation EllipsisLoc;
6947
6948 if (Tok.is(K: tok::ellipsis)) {
6949 Diag(Tok, DiagID: getLangOpts().CPlusPlus26 ? diag::warn_cxx23_compat_binding_pack
6950 : diag::ext_cxx_binding_pack);
6951 if (PrevEllipsisLoc.isValid()) {
6952 Diag(Tok, DiagID: diag::err_binding_multiple_ellipses);
6953 Diag(Loc: PrevEllipsisLoc, DiagID: diag::note_previous_ellipsis);
6954 break;
6955 }
6956 EllipsisLoc = Tok.getLocation();
6957 PrevEllipsisLoc = EllipsisLoc;
6958 ConsumeToken();
6959 }
6960
6961 if (Tok.isNot(K: tok::identifier)) {
6962 Diag(Tok, DiagID: diag::err_expected) << tok::identifier;
6963 break;
6964 }
6965
6966 IdentifierInfo *II = Tok.getIdentifierInfo();
6967 SourceLocation Loc = Tok.getLocation();
6968 ConsumeToken();
6969
6970 if (Tok.is(K: tok::ellipsis) && !PrevEllipsisLoc.isValid()) {
6971 DiagnoseMisplacedEllipsis(EllipsisLoc: Tok.getLocation(), CorrectLoc: Loc, AlreadyHasEllipsis: EllipsisLoc.isValid(),
6972 IdentifierHasName: true);
6973 EllipsisLoc = Tok.getLocation();
6974 ConsumeToken();
6975 }
6976
6977 ParsedAttributes Attrs(AttrFactory);
6978 if (isCXX11AttributeSpecifier() !=
6979 CXX11AttributeKind::NotAttributeSpecifier) {
6980 Diag(Tok, DiagID: getLangOpts().CPlusPlus26
6981 ? diag::warn_cxx23_compat_decl_attrs_on_binding
6982 : diag::ext_decl_attrs_on_binding);
6983 MaybeParseCXX11Attributes(Attrs);
6984 }
6985
6986 Bindings.push_back(Elt: {.Name: II, .NameLoc: Loc, .Attrs: std::move(Attrs), .EllipsisLoc: EllipsisLoc});
6987 }
6988
6989 if (Tok.isNot(K: tok::r_square))
6990 // We've already diagnosed a problem here.
6991 T.skipToEnd();
6992 else {
6993 // C++17 does not allow the identifier-list in a structured binding
6994 // to be empty.
6995 if (Bindings.empty())
6996 Diag(Loc: Tok.getLocation(), DiagID: diag::ext_decomp_decl_empty);
6997
6998 T.consumeClose();
6999 }
7000
7001 PA.Commit();
7002
7003 return D.setDecompositionBindings(LSquareLoc: T.getOpenLocation(), Bindings,
7004 RSquareLoc: T.getCloseLocation());
7005}
7006
7007void Parser::ParseParenDeclarator(Declarator &D) {
7008 BalancedDelimiterTracker T(*this, tok::l_paren);
7009 T.consumeOpen();
7010
7011 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
7012
7013 // Eat any attributes before we look at whether this is a grouping or function
7014 // declarator paren. If this is a grouping paren, the attribute applies to
7015 // the type being built up, for example:
7016 // int (__attribute__(()) *x)(long y)
7017 // If this ends up not being a grouping paren, the attribute applies to the
7018 // first argument, for example:
7019 // int (__attribute__(()) int x)
7020 // In either case, we need to eat any attributes to be able to determine what
7021 // sort of paren this is.
7022 //
7023 ParsedAttributes attrs(AttrFactory);
7024 bool RequiresArg = false;
7025 if (Tok.is(K: tok::kw___attribute)) {
7026 ParseGNUAttributes(Attrs&: attrs);
7027
7028 // We require that the argument list (if this is a non-grouping paren) be
7029 // present even if the attribute list was empty.
7030 RequiresArg = true;
7031 }
7032
7033 // Eat any Microsoft extensions.
7034 ParseMicrosoftTypeAttributes(attrs);
7035
7036 // Eat any Borland extensions.
7037 if (Tok.is(K: tok::kw___pascal))
7038 ParseBorlandTypeAttributes(attrs);
7039
7040 // If we haven't past the identifier yet (or where the identifier would be
7041 // stored, if this is an abstract declarator), then this is probably just
7042 // grouping parens. However, if this could be an abstract-declarator, then
7043 // this could also be the start of function arguments (consider 'void()').
7044 bool isGrouping;
7045
7046 if (!D.mayOmitIdentifier()) {
7047 // If this can't be an abstract-declarator, this *must* be a grouping
7048 // paren, because we haven't seen the identifier yet.
7049 isGrouping = true;
7050 } else if (Tok.is(K: tok::r_paren) || // 'int()' is a function.
7051 ((getLangOpts().CPlusPlus || getLangOpts().C23) &&
7052 Tok.is(K: tok::ellipsis) &&
7053 NextToken().is(K: tok::r_paren)) || // C++ int(...)
7054 isDeclarationSpecifier(
7055 AllowImplicitTypename: ImplicitTypenameContext::No) || // 'int(int)' is a function.
7056 isCXX11AttributeSpecifier() !=
7057 CXX11AttributeKind::NotAttributeSpecifier) { // 'int([[]]int)'
7058 // is a function.
7059 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
7060 // considered to be a type, not a K&R identifier-list.
7061 isGrouping = false;
7062 } else {
7063 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
7064 isGrouping = true;
7065 }
7066
7067 // If this is a grouping paren, handle:
7068 // direct-declarator: '(' declarator ')'
7069 // direct-declarator: '(' attributes declarator ')'
7070 if (isGrouping) {
7071 SourceLocation EllipsisLoc = D.getEllipsisLoc();
7072 D.setEllipsisLoc(SourceLocation());
7073
7074 bool hadGroupingParens = D.hasGroupingParens();
7075 D.setGroupingParens(true);
7076 ParseDeclaratorInternal(D, DirectDeclParser: &Parser::ParseDirectDeclarator);
7077 // Match the ')'.
7078 T.consumeClose();
7079 D.AddTypeInfo(
7080 TI: DeclaratorChunk::getParen(LParenLoc: T.getOpenLocation(), RParenLoc: T.getCloseLocation()),
7081 attrs: std::move(attrs), EndLoc: T.getCloseLocation());
7082
7083 D.setGroupingParens(hadGroupingParens);
7084
7085 // An ellipsis cannot be placed outside parentheses.
7086 if (EllipsisLoc.isValid())
7087 DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
7088
7089 return;
7090 }
7091
7092 // Okay, if this wasn't a grouping paren, it must be the start of a function
7093 // argument list. Recognize that this declarator will never have an
7094 // identifier (and remember where it would have been), then call into
7095 // ParseFunctionDeclarator to handle of argument list.
7096 D.SetIdentifier(Id: nullptr, IdLoc: Tok.getLocation());
7097
7098 // Enter function-declaration scope, limiting any declarators to the
7099 // function prototype scope, including parameter declarators.
7100 ParseScope PrototypeScope(this,
7101 Scope::FunctionPrototypeScope | Scope::DeclScope |
7102 (D.isFunctionDeclaratorAFunctionDeclaration()
7103 ? Scope::FunctionDeclarationScope : 0));
7104 ParseFunctionDeclarator(D, FirstArgAttrs&: attrs, Tracker&: T, IsAmbiguous: false, RequiresArg);
7105 PrototypeScope.Exit();
7106}
7107
7108void Parser::InitCXXThisScopeForDeclaratorIfRelevant(
7109 const Declarator &D, const DeclSpec &DS,
7110 std::optional<Sema::CXXThisScopeRAII> &ThisScope) {
7111 // C++11 [expr.prim.general]p3:
7112 // If a declaration declares a member function or member function
7113 // template of a class X, the expression this is a prvalue of type
7114 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
7115 // and the end of the function-definition, member-declarator, or
7116 // declarator.
7117 // FIXME: currently, "static" case isn't handled correctly.
7118 bool IsCXX11MemberFunction =
7119 getLangOpts().CPlusPlus11 &&
7120 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
7121 (D.getContext() == DeclaratorContext::Member
7122 ? !D.getDeclSpec().isFriendSpecified()
7123 : D.getContext() == DeclaratorContext::File &&
7124 D.getCXXScopeSpec().isValid() &&
7125 Actions.CurContext->isRecord());
7126 if (!IsCXX11MemberFunction)
7127 return;
7128
7129 Qualifiers Q = Qualifiers::fromCVRUMask(CVRU: DS.getTypeQualifiers());
7130 if (D.getDeclSpec().hasConstexprSpecifier() && !getLangOpts().CPlusPlus14)
7131 Q.addConst();
7132 // FIXME: Collect C++ address spaces.
7133 // If there are multiple different address spaces, the source is invalid.
7134 // Carry on using the first addr space for the qualifiers of 'this'.
7135 // The diagnostic will be given later while creating the function
7136 // prototype for the method.
7137 if (getLangOpts().OpenCLCPlusPlus) {
7138 for (ParsedAttr &attr : DS.getAttributes()) {
7139 LangAS ASIdx = attr.asOpenCLLangAS();
7140 if (ASIdx != LangAS::Default) {
7141 Q.addAddressSpace(space: ASIdx);
7142 break;
7143 }
7144 }
7145 }
7146 ThisScope.emplace(args&: Actions, args: dyn_cast<CXXRecordDecl>(Val: Actions.CurContext), args&: Q,
7147 args&: IsCXX11MemberFunction);
7148}
7149
7150void Parser::ParseFunctionDeclarator(Declarator &D,
7151 ParsedAttributes &FirstArgAttrs,
7152 BalancedDelimiterTracker &Tracker,
7153 bool IsAmbiguous,
7154 bool RequiresArg) {
7155 assert(getCurScope()->isFunctionPrototypeScope() &&
7156 "Should call from a Function scope");
7157 // lparen is already consumed!
7158 assert(D.isPastIdentifier() && "Should not call before identifier!");
7159
7160 // This should be true when the function has typed arguments.
7161 // Otherwise, it is treated as a K&R-style function.
7162 bool HasProto = false;
7163 // Build up an array of information about the parsed arguments.
7164 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
7165 // Remember where we see an ellipsis, if any.
7166 SourceLocation EllipsisLoc;
7167
7168 DeclSpec DS(AttrFactory);
7169 bool RefQualifierIsLValueRef = true;
7170 SourceLocation RefQualifierLoc;
7171 ExceptionSpecificationType ESpecType = EST_None;
7172 SourceRange ESpecRange;
7173 SmallVector<ParsedType, 2> DynamicExceptions;
7174 SmallVector<SourceRange, 2> DynamicExceptionRanges;
7175 ExprResult NoexceptExpr;
7176 CachedTokens *ExceptionSpecTokens = nullptr;
7177 ParsedAttributes FnAttrs(AttrFactory);
7178 TypeResult TrailingReturnType;
7179 SourceLocation TrailingReturnTypeLoc;
7180
7181 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
7182 EndLoc is the end location for the function declarator.
7183 They differ for trailing return types. */
7184 SourceLocation StartLoc, LocalEndLoc, EndLoc;
7185 SourceLocation LParenLoc, RParenLoc;
7186 LParenLoc = Tracker.getOpenLocation();
7187 StartLoc = LParenLoc;
7188
7189 if (isFunctionDeclaratorIdentifierList()) {
7190 if (RequiresArg)
7191 Diag(Tok, DiagID: diag::err_argument_required_after_attribute);
7192
7193 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
7194
7195 Tracker.consumeClose();
7196 RParenLoc = Tracker.getCloseLocation();
7197 LocalEndLoc = RParenLoc;
7198 EndLoc = RParenLoc;
7199
7200 // If there are attributes following the identifier list, parse them and
7201 // prohibit them.
7202 MaybeParseCXX11Attributes(Attrs&: FnAttrs);
7203 ProhibitAttributes(Attrs&: FnAttrs);
7204 } else {
7205 if (Tok.isNot(K: tok::r_paren))
7206 ParseParameterDeclarationClause(D, attrs&: FirstArgAttrs, ParamInfo, EllipsisLoc);
7207 else if (RequiresArg)
7208 Diag(Tok, DiagID: diag::err_argument_required_after_attribute);
7209
7210 // OpenCL disallows functions without a prototype, but it doesn't enforce
7211 // strict prototypes as in C23 because it allows a function definition to
7212 // have an identifier list. See OpenCL 3.0 6.11/g for more details.
7213 HasProto = ParamInfo.size() || getLangOpts().requiresStrictPrototypes() ||
7214 getLangOpts().OpenCL;
7215
7216 // If we have the closing ')', eat it.
7217 Tracker.consumeClose();
7218 RParenLoc = Tracker.getCloseLocation();
7219 LocalEndLoc = RParenLoc;
7220 EndLoc = RParenLoc;
7221
7222 if (getLangOpts().CPlusPlus) {
7223 // FIXME: Accept these components in any order, and produce fixits to
7224 // correct the order if the user gets it wrong. Ideally we should deal
7225 // with the pure-specifier in the same way.
7226
7227 // Parse cv-qualifier-seq[opt].
7228 ParseTypeQualifierListOpt(
7229 DS, AttrReqs: AR_NoAttributesParsed,
7230 /*AtomicOrPtrauthAllowed=*/false,
7231 /*IdentifierRequired=*/false, CodeCompletionHandler: [&]() {
7232 Actions.CodeCompletion().CodeCompleteFunctionQualifiers(DS, D);
7233 });
7234 if (!DS.getSourceRange().getEnd().isInvalid()) {
7235 EndLoc = DS.getSourceRange().getEnd();
7236 }
7237
7238 // Parse ref-qualifier[opt].
7239 if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc))
7240 EndLoc = RefQualifierLoc;
7241
7242 std::optional<Sema::CXXThisScopeRAII> ThisScope;
7243 InitCXXThisScopeForDeclaratorIfRelevant(D, DS, ThisScope);
7244
7245 // C++ [class.mem.general]p8:
7246 // A complete-class context of a class (template) is a
7247 // - function body,
7248 // - default argument,
7249 // - default template argument,
7250 // - noexcept-specifier, or
7251 // - default member initializer
7252 // within the member-specification of the class or class template.
7253 //
7254 // Parse exception-specification[opt]. If we are in the
7255 // member-specification of a class or class template, this is a
7256 // complete-class context and parsing of the noexcept-specifier should be
7257 // delayed (even if this is a friend declaration).
7258 bool Delayed = D.getContext() == DeclaratorContext::Member &&
7259 D.isFunctionDeclaratorAFunctionDeclaration();
7260 if (Delayed && Actions.isLibstdcxxEagerExceptionSpecHack(D) &&
7261 GetLookAheadToken(N: 0).is(K: tok::kw_noexcept) &&
7262 GetLookAheadToken(N: 1).is(K: tok::l_paren) &&
7263 GetLookAheadToken(N: 2).is(K: tok::kw_noexcept) &&
7264 GetLookAheadToken(N: 3).is(K: tok::l_paren) &&
7265 GetLookAheadToken(N: 4).is(K: tok::identifier) &&
7266 GetLookAheadToken(N: 4).getIdentifierInfo()->isStr(Str: "swap")) {
7267 // HACK: We've got an exception-specification
7268 // noexcept(noexcept(swap(...)))
7269 // or
7270 // noexcept(noexcept(swap(...)) && noexcept(swap(...)))
7271 // on a 'swap' member function. This is a libstdc++ bug; the lookup
7272 // for 'swap' will only find the function we're currently declaring,
7273 // whereas it expects to find a non-member swap through ADL. Turn off
7274 // delayed parsing to give it a chance to find what it expects.
7275 Delayed = false;
7276 }
7277 ESpecType = tryParseExceptionSpecification(Delayed,
7278 SpecificationRange&: ESpecRange,
7279 DynamicExceptions,
7280 DynamicExceptionRanges,
7281 NoexceptExpr,
7282 ExceptionSpecTokens);
7283 if (ESpecType != EST_None)
7284 EndLoc = ESpecRange.getEnd();
7285
7286 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
7287 // after the exception-specification.
7288 MaybeParseCXX11Attributes(Attrs&: FnAttrs);
7289
7290 // Parse trailing-return-type[opt].
7291 LocalEndLoc = EndLoc;
7292 if (getLangOpts().CPlusPlus11 && Tok.is(K: tok::arrow)) {
7293 Diag(Tok, DiagID: diag::warn_cxx98_compat_trailing_return_type);
7294 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
7295 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
7296 LocalEndLoc = Tok.getLocation();
7297 SourceRange Range;
7298 TrailingReturnType =
7299 ParseTrailingReturnType(Range, MayBeFollowedByDirectInit: D.mayBeFollowedByCXXDirectInit());
7300 TrailingReturnTypeLoc = Range.getBegin();
7301 EndLoc = Range.getEnd();
7302 }
7303 } else {
7304 MaybeParseCXX11Attributes(Attrs&: FnAttrs);
7305 }
7306 }
7307
7308 // Collect non-parameter declarations from the prototype if this is a function
7309 // declaration. They will be moved into the scope of the function. Only do
7310 // this in C and not C++, where the decls will continue to live in the
7311 // surrounding context.
7312 SmallVector<NamedDecl *, 0> DeclsInPrototype;
7313 if (getCurScope()->isFunctionDeclarationScope() && !getLangOpts().CPlusPlus) {
7314 for (Decl *D : getCurScope()->decls()) {
7315 NamedDecl *ND = dyn_cast<NamedDecl>(Val: D);
7316 if (!ND || isa<ParmVarDecl>(Val: ND))
7317 continue;
7318 DeclsInPrototype.push_back(Elt: ND);
7319 }
7320 // Sort DeclsInPrototype based on raw encoding of the source location.
7321 // Scope::decls() is iterating over a SmallPtrSet so sort the Decls before
7322 // moving to DeclContext. This provides a stable ordering for traversing
7323 // Decls in DeclContext, which is important for tasks like ASTWriter for
7324 // deterministic output.
7325 llvm::sort(C&: DeclsInPrototype, Comp: [](Decl *D1, Decl *D2) {
7326 return D1->getLocation().getRawEncoding() <
7327 D2->getLocation().getRawEncoding();
7328 });
7329 }
7330
7331 // Remember that we parsed a function type, and remember the attributes.
7332 D.AddTypeInfo(TI: DeclaratorChunk::getFunction(
7333 HasProto, IsAmbiguous, LParenLoc, Params: ParamInfo.data(),
7334 NumParams: ParamInfo.size(), EllipsisLoc, RParenLoc,
7335 RefQualifierIsLvalueRef: RefQualifierIsLValueRef, RefQualifierLoc,
7336 /*MutableLoc=*/SourceLocation(),
7337 ESpecType, ESpecRange, Exceptions: DynamicExceptions.data(),
7338 ExceptionRanges: DynamicExceptionRanges.data(), NumExceptions: DynamicExceptions.size(),
7339 NoexceptExpr: NoexceptExpr.isUsable() ? NoexceptExpr.get() : nullptr,
7340 ExceptionSpecTokens, DeclsInPrototype, LocalRangeBegin: StartLoc,
7341 LocalRangeEnd: LocalEndLoc, TheDeclarator&: D, TrailingReturnType, TrailingReturnTypeLoc,
7342 MethodQualifiers: &DS),
7343 attrs: std::move(FnAttrs), EndLoc);
7344}
7345
7346bool Parser::ParseRefQualifier(bool &RefQualifierIsLValueRef,
7347 SourceLocation &RefQualifierLoc) {
7348 if (Tok.isOneOf(Ks: tok::amp, Ks: tok::ampamp)) {
7349 Diag(Tok, DiagID: getLangOpts().CPlusPlus11 ?
7350 diag::warn_cxx98_compat_ref_qualifier :
7351 diag::ext_ref_qualifier);
7352
7353 RefQualifierIsLValueRef = Tok.is(K: tok::amp);
7354 RefQualifierLoc = ConsumeToken();
7355 return true;
7356 }
7357 return false;
7358}
7359
7360bool Parser::isFunctionDeclaratorIdentifierList() {
7361 return !getLangOpts().requiresStrictPrototypes()
7362 && Tok.is(K: tok::identifier)
7363 && !TryAltiVecVectorToken()
7364 // K&R identifier lists can't have typedefs as identifiers, per C99
7365 // 6.7.5.3p11.
7366 && (TryAnnotateTypeOrScopeToken() || !Tok.is(K: tok::annot_typename))
7367 // Identifier lists follow a really simple grammar: the identifiers can
7368 // be followed *only* by a ", identifier" or ")". However, K&R
7369 // identifier lists are really rare in the brave new modern world, and
7370 // it is very common for someone to typo a type in a non-K&R style
7371 // list. If we are presented with something like: "void foo(intptr x,
7372 // float y)", we don't want to start parsing the function declarator as
7373 // though it is a K&R style declarator just because intptr is an
7374 // invalid type.
7375 //
7376 // To handle this, we check to see if the token after the first
7377 // identifier is a "," or ")". Only then do we parse it as an
7378 // identifier list.
7379 && (!Tok.is(K: tok::eof) &&
7380 (NextToken().is(K: tok::comma) || NextToken().is(K: tok::r_paren)));
7381}
7382
7383void Parser::ParseFunctionDeclaratorIdentifierList(
7384 Declarator &D,
7385 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
7386 // We should never reach this point in C23 or C++.
7387 assert(!getLangOpts().requiresStrictPrototypes() &&
7388 "Cannot parse an identifier list in C23 or C++");
7389
7390 // If there was no identifier specified for the declarator, either we are in
7391 // an abstract-declarator, or we are in a parameter declarator which was found
7392 // to be abstract. In abstract-declarators, identifier lists are not valid:
7393 // diagnose this.
7394 if (!D.getIdentifier())
7395 Diag(Tok, DiagID: diag::ext_ident_list_in_param);
7396
7397 // Maintain an efficient lookup of params we have seen so far.
7398 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
7399
7400 do {
7401 // If this isn't an identifier, report the error and skip until ')'.
7402 if (Tok.isNot(K: tok::identifier)) {
7403 Diag(Tok, DiagID: diag::err_expected) << tok::identifier;
7404 SkipUntil(T: tok::r_paren, Flags: StopAtSemi | StopBeforeMatch);
7405 // Forget we parsed anything.
7406 ParamInfo.clear();
7407 return;
7408 }
7409
7410 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
7411
7412 // Reject 'typedef int y; int test(x, y)', but continue parsing.
7413 if (Actions.getTypeName(II: *ParmII, NameLoc: Tok.getLocation(), S: getCurScope()))
7414 Diag(Tok, DiagID: diag::err_unexpected_typedef_ident) << ParmII;
7415
7416 // Verify that the argument identifier has not already been mentioned.
7417 if (!ParamsSoFar.insert(Ptr: ParmII).second) {
7418 Diag(Tok, DiagID: diag::err_param_redefinition) << ParmII;
7419 } else {
7420 // Remember this identifier in ParamInfo.
7421 ParamInfo.push_back(Elt: DeclaratorChunk::ParamInfo(ParmII,
7422 Tok.getLocation(),
7423 nullptr));
7424 }
7425
7426 // Eat the identifier.
7427 ConsumeToken();
7428 // The list continues if we see a comma.
7429 } while (TryConsumeToken(Expected: tok::comma));
7430}
7431
7432void Parser::ParseParameterDeclarationClause(
7433 DeclaratorContext DeclaratorCtx, ParsedAttributes &FirstArgAttrs,
7434 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
7435 SourceLocation &EllipsisLoc, bool IsACXXFunctionDeclaration) {
7436
7437 // Avoid exceeding the maximum function scope depth.
7438 // See https://bugs.llvm.org/show_bug.cgi?id=19607
7439 // Note Sema::ActOnParamDeclarator calls ParmVarDecl::setScopeInfo with
7440 // getFunctionPrototypeDepth() - 1.
7441 if (getCurScope()->getFunctionPrototypeDepth() - 1 >
7442 ParmVarDecl::getMaxFunctionScopeDepth()) {
7443 Diag(Loc: Tok.getLocation(), DiagID: diag::err_function_scope_depth_exceeded)
7444 << ParmVarDecl::getMaxFunctionScopeDepth();
7445 cutOffParsing();
7446 return;
7447 }
7448
7449 // C++2a [temp.res]p5
7450 // A qualified-id is assumed to name a type if
7451 // - [...]
7452 // - it is a decl-specifier of the decl-specifier-seq of a
7453 // - [...]
7454 // - parameter-declaration in a member-declaration [...]
7455 // - parameter-declaration in a declarator of a function or function
7456 // template declaration whose declarator-id is qualified [...]
7457 // - parameter-declaration in a lambda-declarator [...]
7458 auto AllowImplicitTypename = ImplicitTypenameContext::No;
7459 if (DeclaratorCtx == DeclaratorContext::Member ||
7460 DeclaratorCtx == DeclaratorContext::LambdaExpr ||
7461 DeclaratorCtx == DeclaratorContext::RequiresExpr ||
7462 IsACXXFunctionDeclaration) {
7463 AllowImplicitTypename = ImplicitTypenameContext::Yes;
7464 }
7465
7466 do {
7467 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
7468 // before deciding this was a parameter-declaration-clause.
7469 if (TryConsumeToken(Expected: tok::ellipsis, Loc&: EllipsisLoc))
7470 break;
7471
7472 // Parse the declaration-specifiers.
7473 // Just use the ParsingDeclaration "scope" of the declarator.
7474 DeclSpec DS(AttrFactory);
7475
7476 ParsedAttributes ArgDeclAttrs(AttrFactory);
7477 ParsedAttributes ArgDeclSpecAttrs(AttrFactory);
7478
7479 if (FirstArgAttrs.Range.isValid()) {
7480 // If the caller parsed attributes for the first argument, add them now.
7481 // Take them so that we only apply the attributes to the first parameter.
7482 // We have already started parsing the decl-specifier sequence, so don't
7483 // parse any parameter-declaration pieces that precede it.
7484 ArgDeclSpecAttrs.takeAllFrom(Other&: FirstArgAttrs);
7485 } else {
7486 // Parse any C++11 attributes.
7487 MaybeParseCXX11Attributes(Attrs&: ArgDeclAttrs);
7488
7489 // Skip any Microsoft attributes before a param.
7490 MaybeParseMicrosoftAttributes(Attrs&: ArgDeclSpecAttrs);
7491 }
7492
7493 SourceLocation DSStart = Tok.getLocation();
7494
7495 // Parse a C++23 Explicit Object Parameter
7496 // We do that in all language modes to produce a better diagnostic.
7497 SourceLocation ThisLoc;
7498 if (getLangOpts().CPlusPlus && Tok.is(K: tok::kw_this))
7499 ThisLoc = ConsumeToken();
7500
7501 ParsedTemplateInfo TemplateInfo;
7502 ParseDeclarationSpecifiers(DS, TemplateInfo, AS: AS_none,
7503 DSContext: DeclSpecContext::DSC_normal,
7504 /*LateAttrs=*/nullptr, AllowImplicitTypename);
7505
7506 DS.takeAttributesFrom(attrs&: ArgDeclSpecAttrs);
7507
7508 // Parse the declarator. This is "PrototypeContext" or
7509 // "LambdaExprParameterContext", because we must accept either
7510 // 'declarator' or 'abstract-declarator' here.
7511 Declarator ParmDeclarator(DS, ArgDeclAttrs,
7512 DeclaratorCtx == DeclaratorContext::RequiresExpr
7513 ? DeclaratorContext::RequiresExpr
7514 : DeclaratorCtx == DeclaratorContext::LambdaExpr
7515 ? DeclaratorContext::LambdaExprParameter
7516 : DeclaratorContext::Prototype);
7517 ParseDeclarator(D&: ParmDeclarator);
7518
7519 if (ThisLoc.isValid())
7520 ParmDeclarator.SetRangeBegin(ThisLoc);
7521
7522 // Parse GNU attributes, if present.
7523 MaybeParseGNUAttributes(D&: ParmDeclarator);
7524 if (getLangOpts().HLSL)
7525 MaybeParseHLSLAnnotations(Attrs&: DS.getAttributes());
7526
7527 if (Tok.is(K: tok::kw_requires)) {
7528 // User tried to define a requires clause in a parameter declaration,
7529 // which is surely not a function declaration.
7530 // void f(int (*g)(int, int) requires true);
7531 Diag(Tok,
7532 DiagID: diag::err_requires_clause_on_declarator_not_declaring_a_function);
7533 ConsumeToken();
7534 ParseConstraintLogicalOrExpression(/*IsTrailingRequiresClause=*/true);
7535 }
7536
7537 // Remember this parsed parameter in ParamInfo.
7538 const IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
7539
7540 // DefArgToks is used when the parsing of default arguments needs
7541 // to be delayed.
7542 std::unique_ptr<CachedTokens> DefArgToks;
7543
7544 // If no parameter was specified, verify that *something* was specified,
7545 // otherwise we have a missing type and identifier.
7546 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == nullptr &&
7547 ParmDeclarator.getNumTypeObjects() == 0) {
7548 // Completely missing, emit error.
7549 Diag(Loc: DSStart, DiagID: diag::err_missing_param);
7550 } else {
7551 // Otherwise, we have something. Add it and let semantic analysis try
7552 // to grok it and add the result to the ParamInfo we are building.
7553
7554 // Last chance to recover from a misplaced ellipsis in an attempted
7555 // parameter pack declaration.
7556 if (Tok.is(K: tok::ellipsis) &&
7557 (NextToken().isNot(K: tok::r_paren) ||
7558 (!ParmDeclarator.getEllipsisLoc().isValid() &&
7559 !Actions.isUnexpandedParameterPackPermitted())) &&
7560 Actions.containsUnexpandedParameterPacks(D&: ParmDeclarator))
7561 DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc: ConsumeToken(), D&: ParmDeclarator);
7562
7563 // Now we are at the point where declarator parsing is finished.
7564 //
7565 // Try to catch keywords in place of the identifier in a declarator, and
7566 // in particular the common case where:
7567 // 1 identifier comes at the end of the declarator
7568 // 2 if the identifier is dropped, the declarator is valid but anonymous
7569 // (no identifier)
7570 // 3 declarator parsing succeeds, and then we have a trailing keyword,
7571 // which is never valid in a param list (e.g. missing a ',')
7572 // And we can't handle this in ParseDeclarator because in general keywords
7573 // may be allowed to follow the declarator. (And in some cases there'd be
7574 // better recovery like inserting punctuation). ParseDeclarator is just
7575 // treating this as an anonymous parameter, and fortunately at this point
7576 // we've already almost done that.
7577 //
7578 // We care about case 1) where the declarator type should be known, and
7579 // the identifier should be null.
7580 if (!ParmDeclarator.isInvalidType() && !ParmDeclarator.hasName() &&
7581 Tok.isNot(K: tok::raw_identifier) && !Tok.isAnnotation() &&
7582 Tok.getIdentifierInfo() &&
7583 Tok.getIdentifierInfo()->isKeyword(LangOpts: getLangOpts())) {
7584 Diag(Tok, DiagID: diag::err_keyword_as_parameter) << PP.getSpelling(Tok);
7585 // Consume the keyword.
7586 ConsumeToken();
7587 }
7588
7589 // We can only store so many parameters
7590 // Skip until the the end of the parameter list, ignoring
7591 // parameters that would overflow.
7592 if (ParamInfo.size() == Type::FunctionTypeNumParamsLimit) {
7593 Diag(Loc: ParmDeclarator.getBeginLoc(),
7594 DiagID: diag::err_function_parameter_limit_exceeded);
7595 SkipUntil(T: tok::r_paren, Flags: SkipUntilFlags::StopBeforeMatch);
7596 break;
7597 }
7598
7599 // Inform the actions module about the parameter declarator, so it gets
7600 // added to the current scope.
7601 Decl *Param =
7602 Actions.ActOnParamDeclarator(S: getCurScope(), D&: ParmDeclarator, ExplicitThisLoc: ThisLoc);
7603 // Parse the default argument, if any. We parse the default
7604 // arguments in all dialects; the semantic analysis in
7605 // ActOnParamDefaultArgument will reject the default argument in
7606 // C.
7607 if (Tok.is(K: tok::equal)) {
7608 SourceLocation EqualLoc = Tok.getLocation();
7609
7610 // Parse the default argument
7611 if (DeclaratorCtx == DeclaratorContext::Member) {
7612 // If we're inside a class definition, cache the tokens
7613 // corresponding to the default argument. We'll actually parse
7614 // them when we see the end of the class definition.
7615 DefArgToks.reset(p: new CachedTokens);
7616
7617 SourceLocation ArgStartLoc = NextToken().getLocation();
7618 ConsumeAndStoreInitializer(Toks&: *DefArgToks,
7619 CIK: CachedInitKind::DefaultArgument);
7620 Actions.ActOnParamUnparsedDefaultArgument(param: Param, EqualLoc,
7621 ArgLoc: ArgStartLoc);
7622 } else {
7623 // Consume the '='.
7624 ConsumeToken();
7625
7626 // The argument isn't actually potentially evaluated unless it is
7627 // used.
7628 EnterExpressionEvaluationContext Eval(
7629 Actions,
7630 Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed,
7631 Param);
7632
7633 ExprResult DefArgResult;
7634 if (getLangOpts().CPlusPlus11 && Tok.is(K: tok::l_brace)) {
7635 Diag(Tok, DiagID: diag::warn_cxx98_compat_generalized_initializer_lists);
7636 DefArgResult = ParseBraceInitializer();
7637 } else {
7638 if (Tok.is(K: tok::l_paren) && NextToken().is(K: tok::l_brace)) {
7639 Diag(Tok, DiagID: diag::err_stmt_expr_in_default_arg) << 0;
7640 Actions.ActOnParamDefaultArgumentError(param: Param, EqualLoc,
7641 /*DefaultArg=*/nullptr);
7642 // Skip the statement expression and continue parsing
7643 SkipUntil(T: tok::comma, Flags: StopBeforeMatch);
7644 continue;
7645 }
7646 DefArgResult = ParseAssignmentExpression();
7647 }
7648 if (DefArgResult.isInvalid()) {
7649 Actions.ActOnParamDefaultArgumentError(param: Param, EqualLoc,
7650 /*DefaultArg=*/nullptr);
7651 SkipUntil(T1: tok::comma, T2: tok::r_paren, Flags: StopAtSemi | StopBeforeMatch);
7652 } else {
7653 // Inform the actions module about the default argument
7654 Actions.ActOnParamDefaultArgument(param: Param, EqualLoc,
7655 defarg: DefArgResult.get());
7656 }
7657 }
7658 }
7659
7660 ParamInfo.push_back(Elt: DeclaratorChunk::ParamInfo(ParmII,
7661 ParmDeclarator.getIdentifierLoc(),
7662 Param, std::move(DefArgToks)));
7663 }
7664
7665 if (TryConsumeToken(Expected: tok::ellipsis, Loc&: EllipsisLoc)) {
7666 if (getLangOpts().CPlusPlus26) {
7667 // C++26 [dcl.dcl.fct]p3:
7668 // A parameter-declaration-clause of the form
7669 // parameter-list '...' is deprecated.
7670 Diag(Loc: EllipsisLoc, DiagID: diag::warn_deprecated_missing_comma_before_ellipsis)
7671 << FixItHint::CreateInsertion(InsertionLoc: EllipsisLoc, Code: ", ");
7672 }
7673
7674 if (!getLangOpts().CPlusPlus) {
7675 // We have ellipsis without a preceding ',', which is ill-formed
7676 // in C. Complain and provide the fix.
7677 Diag(Loc: EllipsisLoc, DiagID: diag::err_missing_comma_before_ellipsis)
7678 << FixItHint::CreateInsertion(InsertionLoc: EllipsisLoc, Code: ", ");
7679 } else if (ParmDeclarator.getEllipsisLoc().isValid() ||
7680 Actions.containsUnexpandedParameterPacks(D&: ParmDeclarator)) {
7681 // It looks like this was supposed to be a parameter pack. Warn and
7682 // point out where the ellipsis should have gone.
7683 SourceLocation ParmEllipsis = ParmDeclarator.getEllipsisLoc();
7684 Diag(Loc: EllipsisLoc, DiagID: diag::warn_misplaced_ellipsis_vararg)
7685 << ParmEllipsis.isValid() << ParmEllipsis;
7686 if (ParmEllipsis.isValid()) {
7687 Diag(Loc: ParmEllipsis,
7688 DiagID: diag::note_misplaced_ellipsis_vararg_existing_ellipsis);
7689 } else {
7690 Diag(Loc: ParmDeclarator.getIdentifierLoc(),
7691 DiagID: diag::note_misplaced_ellipsis_vararg_add_ellipsis)
7692 << FixItHint::CreateInsertion(InsertionLoc: ParmDeclarator.getIdentifierLoc(),
7693 Code: "...")
7694 << !ParmDeclarator.hasName();
7695 }
7696 Diag(Loc: EllipsisLoc, DiagID: diag::note_misplaced_ellipsis_vararg_add_comma)
7697 << FixItHint::CreateInsertion(InsertionLoc: EllipsisLoc, Code: ", ");
7698 }
7699
7700 // We can't have any more parameters after an ellipsis.
7701 break;
7702 }
7703
7704 // If the next token is a comma, consume it and keep reading arguments.
7705 } while (TryConsumeToken(Expected: tok::comma));
7706}
7707
7708void Parser::ParseBracketDeclarator(Declarator &D) {
7709 if (CheckProhibitedCXX11Attribute())
7710 return;
7711
7712 BalancedDelimiterTracker T(*this, tok::l_square);
7713 T.consumeOpen();
7714
7715 // C array syntax has many features, but by-far the most common is [] and [4].
7716 // This code does a fast path to handle some of the most obvious cases.
7717 if (Tok.getKind() == tok::r_square) {
7718 T.consumeClose();
7719 ParsedAttributes attrs(AttrFactory);
7720 MaybeParseCXX11Attributes(Attrs&: attrs);
7721
7722 // Remember that we parsed the empty array type.
7723 D.AddTypeInfo(TI: DeclaratorChunk::getArray(TypeQuals: 0, isStatic: false, isStar: false, NumElts: nullptr,
7724 LBLoc: T.getOpenLocation(),
7725 RBLoc: T.getCloseLocation()),
7726 attrs: std::move(attrs), EndLoc: T.getCloseLocation());
7727 return;
7728 } else if (Tok.getKind() == tok::numeric_constant &&
7729 GetLookAheadToken(N: 1).is(K: tok::r_square)) {
7730 // [4] is very common. Parse the numeric constant expression.
7731 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, UDLScope: getCurScope()));
7732 ConsumeToken();
7733
7734 T.consumeClose();
7735 ParsedAttributes attrs(AttrFactory);
7736 MaybeParseCXX11Attributes(Attrs&: attrs);
7737
7738 // Remember that we parsed a array type, and remember its features.
7739 D.AddTypeInfo(TI: DeclaratorChunk::getArray(TypeQuals: 0, isStatic: false, isStar: false, NumElts: ExprRes.get(),
7740 LBLoc: T.getOpenLocation(),
7741 RBLoc: T.getCloseLocation()),
7742 attrs: std::move(attrs), EndLoc: T.getCloseLocation());
7743 return;
7744 } else if (Tok.getKind() == tok::code_completion) {
7745 cutOffParsing();
7746 Actions.CodeCompletion().CodeCompleteBracketDeclarator(S: getCurScope());
7747 return;
7748 }
7749
7750 // If valid, this location is the position where we read the 'static' keyword.
7751 SourceLocation StaticLoc;
7752 TryConsumeToken(Expected: tok::kw_static, Loc&: StaticLoc);
7753
7754 // If there is a type-qualifier-list, read it now.
7755 // Type qualifiers in an array subscript are a C99 feature.
7756 DeclSpec DS(AttrFactory);
7757 ParseTypeQualifierListOpt(DS, AttrReqs: AR_CXX11AttributesParsed);
7758
7759 // If we haven't already read 'static', check to see if there is one after the
7760 // type-qualifier-list.
7761 if (!StaticLoc.isValid())
7762 TryConsumeToken(Expected: tok::kw_static, Loc&: StaticLoc);
7763
7764 // Handle "direct-declarator [ type-qual-list[opt] * ]".
7765 bool isStar = false;
7766 ExprResult NumElements;
7767
7768 // Handle the case where we have '[*]' as the array size. However, a leading
7769 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
7770 // the token after the star is a ']'. Since stars in arrays are
7771 // infrequent, use of lookahead is not costly here.
7772 if (Tok.is(K: tok::star) && GetLookAheadToken(N: 1).is(K: tok::r_square)) {
7773 ConsumeToken(); // Eat the '*'.
7774
7775 if (StaticLoc.isValid()) {
7776 Diag(Loc: StaticLoc, DiagID: diag::err_unspecified_vla_size_with_static);
7777 StaticLoc = SourceLocation(); // Drop the static.
7778 }
7779 isStar = true;
7780 } else if (Tok.isNot(K: tok::r_square)) {
7781 // Note, in C89, this production uses the constant-expr production instead
7782 // of assignment-expr. The only difference is that assignment-expr allows
7783 // things like '=' and '*='. Sema rejects these in C89 mode because they
7784 // are not i-c-e's, so we don't need to distinguish between the two here.
7785
7786 // Parse the constant-expression or assignment-expression now (depending
7787 // on dialect).
7788 if (getLangOpts().CPlusPlus) {
7789 NumElements = ParseArrayBoundExpression();
7790 } else {
7791 EnterExpressionEvaluationContext Unevaluated(
7792 Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
7793 NumElements = ParseAssignmentExpression();
7794 }
7795 } else {
7796 if (StaticLoc.isValid()) {
7797 Diag(Loc: StaticLoc, DiagID: diag::err_unspecified_size_with_static);
7798 StaticLoc = SourceLocation(); // Drop the static.
7799 }
7800 }
7801
7802 // If there was an error parsing the assignment-expression, recover.
7803 if (NumElements.isInvalid()) {
7804 D.setInvalidType(true);
7805 // If the expression was invalid, skip it.
7806 SkipUntil(T: tok::r_square, Flags: StopAtSemi);
7807 return;
7808 }
7809
7810 T.consumeClose();
7811
7812 MaybeParseCXX11Attributes(Attrs&: DS.getAttributes());
7813
7814 // Remember that we parsed a array type, and remember its features.
7815 D.AddTypeInfo(
7816 TI: DeclaratorChunk::getArray(TypeQuals: DS.getTypeQualifiers(), isStatic: StaticLoc.isValid(),
7817 isStar, NumElts: NumElements.get(), LBLoc: T.getOpenLocation(),
7818 RBLoc: T.getCloseLocation()),
7819 attrs: std::move(DS.getAttributes()), EndLoc: T.getCloseLocation());
7820}
7821
7822void Parser::ParseMisplacedBracketDeclarator(Declarator &D) {
7823 assert(Tok.is(tok::l_square) && "Missing opening bracket");
7824 assert(!D.mayOmitIdentifier() && "Declarator cannot omit identifier");
7825
7826 SourceLocation StartBracketLoc = Tok.getLocation();
7827 Declarator TempDeclarator(D.getDeclSpec(), ParsedAttributesView::none(),
7828 D.getContext());
7829
7830 while (Tok.is(K: tok::l_square)) {
7831 ParseBracketDeclarator(D&: TempDeclarator);
7832 }
7833
7834 // Stuff the location of the start of the brackets into the Declarator.
7835 // The diagnostics from ParseDirectDeclarator will make more sense if
7836 // they use this location instead.
7837 if (Tok.is(K: tok::semi))
7838 D.getName().EndLocation = StartBracketLoc;
7839
7840 SourceLocation SuggestParenLoc = Tok.getLocation();
7841
7842 // Now that the brackets are removed, try parsing the declarator again.
7843 ParseDeclaratorInternal(D, DirectDeclParser: &Parser::ParseDirectDeclarator);
7844
7845 // Something went wrong parsing the brackets, in which case,
7846 // ParseBracketDeclarator has emitted an error, and we don't need to emit
7847 // one here.
7848 if (TempDeclarator.getNumTypeObjects() == 0)
7849 return;
7850
7851 // Determine if parens will need to be suggested in the diagnostic.
7852 bool NeedParens = false;
7853 if (D.getNumTypeObjects() != 0) {
7854 switch (D.getTypeObject(i: D.getNumTypeObjects() - 1).Kind) {
7855 case DeclaratorChunk::Pointer:
7856 case DeclaratorChunk::Reference:
7857 case DeclaratorChunk::BlockPointer:
7858 case DeclaratorChunk::MemberPointer:
7859 case DeclaratorChunk::Pipe:
7860 NeedParens = true;
7861 break;
7862 case DeclaratorChunk::Array:
7863 case DeclaratorChunk::Function:
7864 case DeclaratorChunk::Paren:
7865 break;
7866 }
7867 }
7868
7869 if (NeedParens) {
7870 // Create a DeclaratorChunk for the inserted parens.
7871 SourceLocation EndLoc = PP.getLocForEndOfToken(Loc: D.getEndLoc());
7872 D.AddTypeInfo(TI: DeclaratorChunk::getParen(LParenLoc: SuggestParenLoc, RParenLoc: EndLoc),
7873 EndLoc: SourceLocation());
7874 }
7875
7876 // Adding back the bracket info to the end of the Declarator.
7877 for (unsigned i = 0, e = TempDeclarator.getNumTypeObjects(); i < e; ++i) {
7878 const DeclaratorChunk &Chunk = TempDeclarator.getTypeObject(i);
7879 D.AddTypeInfo(TI: Chunk, OtherPool&: TempDeclarator.getAttributePool(), EndLoc: SourceLocation());
7880 }
7881
7882 // The missing identifier would have been diagnosed in ParseDirectDeclarator.
7883 // If parentheses are required, always suggest them.
7884 if (!D.getIdentifier() && !NeedParens)
7885 return;
7886
7887 SourceLocation EndBracketLoc = TempDeclarator.getEndLoc();
7888
7889 // Generate the move bracket error message.
7890 SourceRange BracketRange(StartBracketLoc, EndBracketLoc);
7891 SourceLocation EndLoc = PP.getLocForEndOfToken(Loc: D.getEndLoc());
7892
7893 if (NeedParens) {
7894 Diag(Loc: EndLoc, DiagID: diag::err_brackets_go_after_unqualified_id)
7895 << getLangOpts().CPlusPlus
7896 << FixItHint::CreateInsertion(InsertionLoc: SuggestParenLoc, Code: "(")
7897 << FixItHint::CreateInsertion(InsertionLoc: EndLoc, Code: ")")
7898 << FixItHint::CreateInsertionFromRange(
7899 InsertionLoc: EndLoc, FromRange: CharSourceRange(BracketRange, true))
7900 << FixItHint::CreateRemoval(RemoveRange: BracketRange);
7901 } else {
7902 Diag(Loc: EndLoc, DiagID: diag::err_brackets_go_after_unqualified_id)
7903 << getLangOpts().CPlusPlus
7904 << FixItHint::CreateInsertionFromRange(
7905 InsertionLoc: EndLoc, FromRange: CharSourceRange(BracketRange, true))
7906 << FixItHint::CreateRemoval(RemoveRange: BracketRange);
7907 }
7908}
7909
7910void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
7911 assert(Tok.isOneOf(tok::kw_typeof, tok::kw_typeof_unqual) &&
7912 "Not a typeof specifier");
7913
7914 bool IsUnqual = Tok.is(K: tok::kw_typeof_unqual);
7915 const IdentifierInfo *II = Tok.getIdentifierInfo();
7916 if (getLangOpts().C23 && !II->getName().starts_with(Prefix: "__"))
7917 Diag(Loc: Tok.getLocation(), DiagID: diag::warn_c23_compat_keyword) << Tok.getName();
7918
7919 Token OpTok = Tok;
7920 SourceLocation StartLoc = ConsumeToken();
7921 bool HasParens = Tok.is(K: tok::l_paren);
7922
7923 EnterExpressionEvaluationContext Unevaluated(
7924 Actions, Sema::ExpressionEvaluationContext::Unevaluated,
7925 Sema::ReuseLambdaContextDecl);
7926
7927 bool isCastExpr;
7928 ParsedType CastTy;
7929 SourceRange CastRange;
7930 ExprResult Operand =
7931 ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, CastTy, CastRange);
7932 if (HasParens)
7933 DS.setTypeArgumentRange(CastRange);
7934
7935 if (CastRange.getEnd().isInvalid())
7936 // FIXME: Not accurate, the range gets one token more than it should.
7937 DS.SetRangeEnd(Tok.getLocation());
7938 else
7939 DS.SetRangeEnd(CastRange.getEnd());
7940
7941 if (isCastExpr) {
7942 if (!CastTy) {
7943 DS.SetTypeSpecError();
7944 return;
7945 }
7946
7947 const char *PrevSpec = nullptr;
7948 unsigned DiagID;
7949 // Check for duplicate type specifiers (e.g. "int typeof(int)").
7950 if (DS.SetTypeSpecType(T: IsUnqual ? DeclSpec::TST_typeof_unqualType
7951 : DeclSpec::TST_typeofType,
7952 Loc: StartLoc, PrevSpec,
7953 DiagID, Rep: CastTy,
7954 Policy: Actions.getASTContext().getPrintingPolicy()))
7955 Diag(Loc: StartLoc, DiagID) << PrevSpec;
7956 return;
7957 }
7958
7959 // If we get here, the operand to the typeof was an expression.
7960 if (Operand.isInvalid()) {
7961 DS.SetTypeSpecError();
7962 return;
7963 }
7964
7965 // We might need to transform the operand if it is potentially evaluated.
7966 Operand = Actions.HandleExprEvaluationContextForTypeof(E: Operand.get());
7967 if (Operand.isInvalid()) {
7968 DS.SetTypeSpecError();
7969 return;
7970 }
7971
7972 const char *PrevSpec = nullptr;
7973 unsigned DiagID;
7974 // Check for duplicate type specifiers (e.g. "int typeof(int)").
7975 if (DS.SetTypeSpecType(T: IsUnqual ? DeclSpec::TST_typeof_unqualExpr
7976 : DeclSpec::TST_typeofExpr,
7977 Loc: StartLoc, PrevSpec,
7978 DiagID, Rep: Operand.get(),
7979 policy: Actions.getASTContext().getPrintingPolicy()))
7980 Diag(Loc: StartLoc, DiagID) << PrevSpec;
7981}
7982
7983void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
7984 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
7985 "Not an atomic specifier");
7986
7987 SourceLocation StartLoc = ConsumeToken();
7988 BalancedDelimiterTracker T(*this, tok::l_paren);
7989 if (T.consumeOpen())
7990 return;
7991
7992 TypeResult Result = ParseTypeName();
7993 if (Result.isInvalid()) {
7994 SkipUntil(T: tok::r_paren, Flags: StopAtSemi);
7995 return;
7996 }
7997
7998 // Match the ')'
7999 T.consumeClose();
8000
8001 if (T.getCloseLocation().isInvalid())
8002 return;
8003
8004 DS.setTypeArgumentRange(T.getRange());
8005 DS.SetRangeEnd(T.getCloseLocation());
8006
8007 const char *PrevSpec = nullptr;
8008 unsigned DiagID;
8009 if (DS.SetTypeSpecType(T: DeclSpec::TST_atomic, Loc: StartLoc, PrevSpec,
8010 DiagID, Rep: Result.get(),
8011 Policy: Actions.getASTContext().getPrintingPolicy()))
8012 Diag(Loc: StartLoc, DiagID) << PrevSpec;
8013}
8014
8015bool Parser::TryAltiVecVectorTokenOutOfLine() {
8016 Token Next = NextToken();
8017 switch (Next.getKind()) {
8018 default: return false;
8019 case tok::kw_short:
8020 case tok::kw_long:
8021 case tok::kw_signed:
8022 case tok::kw_unsigned:
8023 case tok::kw_void:
8024 case tok::kw_char:
8025 case tok::kw_int:
8026 case tok::kw_float:
8027 case tok::kw_double:
8028 case tok::kw_bool:
8029 case tok::kw__Bool:
8030 case tok::kw___bool:
8031 case tok::kw___pixel:
8032 Tok.setKind(tok::kw___vector);
8033 return true;
8034 case tok::identifier:
8035 if (Next.getIdentifierInfo() == Ident_pixel) {
8036 Tok.setKind(tok::kw___vector);
8037 return true;
8038 }
8039 if (Next.getIdentifierInfo() == Ident_bool ||
8040 Next.getIdentifierInfo() == Ident_Bool) {
8041 Tok.setKind(tok::kw___vector);
8042 return true;
8043 }
8044 return false;
8045 }
8046}
8047
8048bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
8049 const char *&PrevSpec, unsigned &DiagID,
8050 bool &isInvalid) {
8051 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
8052 if (Tok.getIdentifierInfo() == Ident_vector) {
8053 Token Next = NextToken();
8054 switch (Next.getKind()) {
8055 case tok::kw_short:
8056 case tok::kw_long:
8057 case tok::kw_signed:
8058 case tok::kw_unsigned:
8059 case tok::kw_void:
8060 case tok::kw_char:
8061 case tok::kw_int:
8062 case tok::kw_float:
8063 case tok::kw_double:
8064 case tok::kw_bool:
8065 case tok::kw__Bool:
8066 case tok::kw___bool:
8067 case tok::kw___pixel:
8068 isInvalid = DS.SetTypeAltiVecVector(isAltiVecVector: true, Loc, PrevSpec, DiagID, Policy);
8069 return true;
8070 case tok::identifier:
8071 if (Next.getIdentifierInfo() == Ident_pixel) {
8072 isInvalid = DS.SetTypeAltiVecVector(isAltiVecVector: true, Loc, PrevSpec, DiagID,Policy);
8073 return true;
8074 }
8075 if (Next.getIdentifierInfo() == Ident_bool ||
8076 Next.getIdentifierInfo() == Ident_Bool) {
8077 isInvalid =
8078 DS.SetTypeAltiVecVector(isAltiVecVector: true, Loc, PrevSpec, DiagID, Policy);
8079 return true;
8080 }
8081 break;
8082 default:
8083 break;
8084 }
8085 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
8086 DS.isTypeAltiVecVector()) {
8087 isInvalid = DS.SetTypeAltiVecPixel(isAltiVecPixel: true, Loc, PrevSpec, DiagID, Policy);
8088 return true;
8089 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
8090 DS.isTypeAltiVecVector()) {
8091 isInvalid = DS.SetTypeAltiVecBool(isAltiVecBool: true, Loc, PrevSpec, DiagID, Policy);
8092 return true;
8093 }
8094 return false;
8095}
8096
8097TypeResult Parser::ParseTypeFromString(StringRef TypeStr, StringRef Context,
8098 SourceLocation IncludeLoc) {
8099 // Consume (unexpanded) tokens up to the end-of-directive.
8100 SmallVector<Token, 4> Tokens;
8101 {
8102 // Create a new buffer from which we will parse the type.
8103 auto &SourceMgr = PP.getSourceManager();
8104 FileID FID = SourceMgr.createFileID(
8105 Buffer: llvm::MemoryBuffer::getMemBufferCopy(InputData: TypeStr, BufferName: Context), FileCharacter: SrcMgr::C_User,
8106 LoadedID: 0, LoadedOffset: 0, IncludeLoc);
8107
8108 // Form a new lexer that references the buffer.
8109 Lexer L(FID, SourceMgr.getBufferOrFake(FID), PP);
8110 L.setParsingPreprocessorDirective(true);
8111
8112 // Lex the tokens from that buffer.
8113 Token Tok;
8114 do {
8115 L.Lex(Result&: Tok);
8116 Tokens.push_back(Elt: Tok);
8117 } while (Tok.isNot(K: tok::eod));
8118 }
8119
8120 // Replace the "eod" token with an "eof" token identifying the end of
8121 // the provided string.
8122 Token &EndToken = Tokens.back();
8123 EndToken.startToken();
8124 EndToken.setKind(tok::eof);
8125 EndToken.setLocation(Tok.getLocation());
8126 EndToken.setEofData(TypeStr.data());
8127
8128 // Add the current token back.
8129 Tokens.push_back(Elt: Tok);
8130
8131 // Enter the tokens into the token stream.
8132 PP.EnterTokenStream(Toks: Tokens, /*DisableMacroExpansion=*/false,
8133 /*IsReinject=*/false);
8134
8135 // Consume the current token so that we'll start parsing the tokens we
8136 // added to the stream.
8137 ConsumeAnyToken();
8138
8139 // Enter a new scope.
8140 ParseScope LocalScope(this, 0);
8141
8142 // Parse the type.
8143 TypeResult Result = ParseTypeName(Range: nullptr);
8144
8145 // Check if we parsed the whole thing.
8146 if (Result.isUsable() &&
8147 (Tok.isNot(K: tok::eof) || Tok.getEofData() != TypeStr.data())) {
8148 Diag(Loc: Tok.getLocation(), DiagID: diag::err_type_unparsed);
8149 }
8150
8151 // There could be leftover tokens (e.g. because of an error).
8152 // Skip through until we reach the 'end of directive' token.
8153 while (Tok.isNot(K: tok::eof))
8154 ConsumeAnyToken();
8155
8156 // Consume the end token.
8157 if (Tok.is(K: tok::eof) && Tok.getEofData() == TypeStr.data())
8158 ConsumeAnyToken();
8159 return Result;
8160}
8161
8162void Parser::DiagnoseBitIntUse(const Token &Tok) {
8163 // If the token is for _ExtInt, diagnose it as being deprecated. Otherwise,
8164 // the token is about _BitInt and gets (potentially) diagnosed as use of an
8165 // extension.
8166 assert(Tok.isOneOf(tok::kw__ExtInt, tok::kw__BitInt) &&
8167 "expected either an _ExtInt or _BitInt token!");
8168
8169 SourceLocation Loc = Tok.getLocation();
8170 if (Tok.is(K: tok::kw__ExtInt)) {
8171 Diag(Loc, DiagID: diag::warn_ext_int_deprecated)
8172 << FixItHint::CreateReplacement(RemoveRange: Loc, Code: "_BitInt");
8173 } else {
8174 // In C23 mode, diagnose that the use is not compatible with pre-C23 modes.
8175 // Otherwise, diagnose that the use is a Clang extension.
8176 if (getLangOpts().C23)
8177 Diag(Loc, DiagID: diag::warn_c23_compat_keyword) << Tok.getName();
8178 else
8179 Diag(Loc, DiagID: diag::ext_bit_int) << getLangOpts().CPlusPlus;
8180 }
8181}
8182