xref: /freebsd/contrib/llvm-project/clang/lib/Parse/ParseDecl.cpp (revision 297eecfb02bb25902531dbb5c3b9a88caf8adf29)
10b57cec5SDimitry Andric //===--- ParseDecl.cpp - Declaration Parsing --------------------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric //  This file implements the Declaration portions of the Parser interfaces.
100b57cec5SDimitry Andric //
110b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
120b57cec5SDimitry Andric 
130b57cec5SDimitry Andric #include "clang/AST/ASTContext.h"
140b57cec5SDimitry Andric #include "clang/AST/DeclTemplate.h"
150b57cec5SDimitry Andric #include "clang/AST/PrettyDeclStackTrace.h"
160b57cec5SDimitry Andric #include "clang/Basic/AddressSpaces.h"
1781ad6265SDimitry Andric #include "clang/Basic/AttributeCommonInfo.h"
180b57cec5SDimitry Andric #include "clang/Basic/Attributes.h"
190b57cec5SDimitry Andric #include "clang/Basic/CharInfo.h"
200b57cec5SDimitry Andric #include "clang/Basic/TargetInfo.h"
215f757f3fSDimitry Andric #include "clang/Basic/TokenKinds.h"
220b57cec5SDimitry Andric #include "clang/Parse/ParseDiagnostic.h"
2381ad6265SDimitry Andric #include "clang/Parse/Parser.h"
2481ad6265SDimitry Andric #include "clang/Parse/RAIIObjectsForParser.h"
2506c3fb27SDimitry Andric #include "clang/Sema/EnterExpressionEvaluationContext.h"
260b57cec5SDimitry Andric #include "clang/Sema/Lookup.h"
270b57cec5SDimitry Andric #include "clang/Sema/ParsedTemplate.h"
280b57cec5SDimitry Andric #include "clang/Sema/Scope.h"
29e8d8bef9SDimitry Andric #include "clang/Sema/SemaDiagnostic.h"
300b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h"
310b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
320b57cec5SDimitry Andric #include "llvm/ADT/StringSwitch.h"
33bdd1243dSDimitry Andric #include <optional>
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric using namespace clang;
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
380b57cec5SDimitry Andric // C99 6.7: Declarations.
390b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
400b57cec5SDimitry Andric 
410b57cec5SDimitry Andric /// ParseTypeName
420b57cec5SDimitry Andric ///       type-name: [C99 6.7.6]
430b57cec5SDimitry Andric ///         specifier-qualifier-list abstract-declarator[opt]
440b57cec5SDimitry Andric ///
450b57cec5SDimitry Andric /// Called type-id in C++.
4681ad6265SDimitry Andric TypeResult Parser::ParseTypeName(SourceRange *Range, DeclaratorContext Context,
4781ad6265SDimitry Andric                                  AccessSpecifier AS, Decl **OwnedType,
480b57cec5SDimitry Andric                                  ParsedAttributes *Attrs) {
490b57cec5SDimitry Andric   DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
500b57cec5SDimitry Andric   if (DSC == DeclSpecContext::DSC_normal)
510b57cec5SDimitry Andric     DSC = DeclSpecContext::DSC_type_specifier;
520b57cec5SDimitry Andric 
530b57cec5SDimitry Andric   // Parse the common declaration-specifiers piece.
540b57cec5SDimitry Andric   DeclSpec DS(AttrFactory);
550b57cec5SDimitry Andric   if (Attrs)
560b57cec5SDimitry Andric     DS.addAttributes(*Attrs);
570b57cec5SDimitry Andric   ParseSpecifierQualifierList(DS, AS, DSC);
580b57cec5SDimitry Andric   if (OwnedType)
590b57cec5SDimitry Andric     *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : nullptr;
600b57cec5SDimitry Andric 
6106c3fb27SDimitry Andric   // Move declspec attributes to ParsedAttributes
6206c3fb27SDimitry Andric   if (Attrs) {
6306c3fb27SDimitry Andric     llvm::SmallVector<ParsedAttr *, 1> ToBeMoved;
6406c3fb27SDimitry Andric     for (ParsedAttr &AL : DS.getAttributes()) {
6506c3fb27SDimitry Andric       if (AL.isDeclspecAttribute())
6606c3fb27SDimitry Andric         ToBeMoved.push_back(&AL);
6706c3fb27SDimitry Andric     }
6806c3fb27SDimitry Andric 
6906c3fb27SDimitry Andric     for (ParsedAttr *AL : ToBeMoved)
7006c3fb27SDimitry Andric       Attrs->takeOneFrom(DS.getAttributes(), AL);
7106c3fb27SDimitry Andric   }
7206c3fb27SDimitry Andric 
730b57cec5SDimitry Andric   // Parse the abstract-declarator, if present.
7481ad6265SDimitry Andric   Declarator DeclaratorInfo(DS, ParsedAttributesView::none(), Context);
750b57cec5SDimitry Andric   ParseDeclarator(DeclaratorInfo);
760b57cec5SDimitry Andric   if (Range)
770b57cec5SDimitry Andric     *Range = DeclaratorInfo.getSourceRange();
780b57cec5SDimitry Andric 
790b57cec5SDimitry Andric   if (DeclaratorInfo.isInvalidType())
800b57cec5SDimitry Andric     return true;
810b57cec5SDimitry Andric 
820b57cec5SDimitry Andric   return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
830b57cec5SDimitry Andric }
840b57cec5SDimitry Andric 
850b57cec5SDimitry Andric /// Normalizes an attribute name by dropping prefixed and suffixed __.
860b57cec5SDimitry Andric static StringRef normalizeAttrName(StringRef Name) {
875f757f3fSDimitry Andric   if (Name.size() >= 4 && Name.starts_with("__") && Name.ends_with("__"))
880b57cec5SDimitry Andric     return Name.drop_front(2).drop_back(2);
890b57cec5SDimitry Andric   return Name;
900b57cec5SDimitry Andric }
910b57cec5SDimitry Andric 
920b57cec5SDimitry Andric /// isAttributeLateParsed - Return true if the attribute has arguments that
930b57cec5SDimitry Andric /// require late parsing.
940b57cec5SDimitry Andric static bool isAttributeLateParsed(const IdentifierInfo &II) {
950b57cec5SDimitry Andric #define CLANG_ATTR_LATE_PARSED_LIST
960b57cec5SDimitry Andric     return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
970b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
980b57cec5SDimitry Andric         .Default(false);
990b57cec5SDimitry Andric #undef CLANG_ATTR_LATE_PARSED_LIST
1000b57cec5SDimitry Andric }
1010b57cec5SDimitry Andric 
1020b57cec5SDimitry Andric /// Check if the a start and end source location expand to the same macro.
103a7dea167SDimitry Andric static bool FindLocsWithCommonFileID(Preprocessor &PP, SourceLocation StartLoc,
1040b57cec5SDimitry Andric                                      SourceLocation EndLoc) {
1050b57cec5SDimitry Andric   if (!StartLoc.isMacroID() || !EndLoc.isMacroID())
1060b57cec5SDimitry Andric     return false;
1070b57cec5SDimitry Andric 
1080b57cec5SDimitry Andric   SourceManager &SM = PP.getSourceManager();
1090b57cec5SDimitry Andric   if (SM.getFileID(StartLoc) != SM.getFileID(EndLoc))
1100b57cec5SDimitry Andric     return false;
1110b57cec5SDimitry Andric 
1120b57cec5SDimitry Andric   bool AttrStartIsInMacro =
1130b57cec5SDimitry Andric       Lexer::isAtStartOfMacroExpansion(StartLoc, SM, PP.getLangOpts());
1140b57cec5SDimitry Andric   bool AttrEndIsInMacro =
1150b57cec5SDimitry Andric       Lexer::isAtEndOfMacroExpansion(EndLoc, SM, PP.getLangOpts());
1160b57cec5SDimitry Andric   return AttrStartIsInMacro && AttrEndIsInMacro;
1170b57cec5SDimitry Andric }
1180b57cec5SDimitry Andric 
11981ad6265SDimitry Andric void Parser::ParseAttributes(unsigned WhichAttrKinds, ParsedAttributes &Attrs,
120fe6060f1SDimitry Andric                              LateParsedAttrList *LateAttrs) {
121fe6060f1SDimitry Andric   bool MoreToParse;
122fe6060f1SDimitry Andric   do {
123fe6060f1SDimitry Andric     // Assume there's nothing left to parse, but if any attributes are in fact
124fe6060f1SDimitry Andric     // parsed, loop to ensure all specified attribute combinations are parsed.
125fe6060f1SDimitry Andric     MoreToParse = false;
126fe6060f1SDimitry Andric     if (WhichAttrKinds & PAKM_CXX11)
12781ad6265SDimitry Andric       MoreToParse |= MaybeParseCXX11Attributes(Attrs);
128fe6060f1SDimitry Andric     if (WhichAttrKinds & PAKM_GNU)
12981ad6265SDimitry Andric       MoreToParse |= MaybeParseGNUAttributes(Attrs, LateAttrs);
130fe6060f1SDimitry Andric     if (WhichAttrKinds & PAKM_Declspec)
13181ad6265SDimitry Andric       MoreToParse |= MaybeParseMicrosoftDeclSpecs(Attrs);
132fe6060f1SDimitry Andric   } while (MoreToParse);
133fe6060f1SDimitry Andric }
134fe6060f1SDimitry Andric 
1350b57cec5SDimitry Andric /// ParseGNUAttributes - Parse a non-empty attributes list.
1360b57cec5SDimitry Andric ///
1370b57cec5SDimitry Andric /// [GNU] attributes:
1380b57cec5SDimitry Andric ///         attribute
1390b57cec5SDimitry Andric ///         attributes attribute
1400b57cec5SDimitry Andric ///
1410b57cec5SDimitry Andric /// [GNU]  attribute:
1420b57cec5SDimitry Andric ///          '__attribute__' '(' '(' attribute-list ')' ')'
1430b57cec5SDimitry Andric ///
1440b57cec5SDimitry Andric /// [GNU]  attribute-list:
1450b57cec5SDimitry Andric ///          attrib
1460b57cec5SDimitry Andric ///          attribute_list ',' attrib
1470b57cec5SDimitry Andric ///
1480b57cec5SDimitry Andric /// [GNU]  attrib:
1490b57cec5SDimitry Andric ///          empty
1500b57cec5SDimitry Andric ///          attrib-name
1510b57cec5SDimitry Andric ///          attrib-name '(' identifier ')'
1520b57cec5SDimitry Andric ///          attrib-name '(' identifier ',' nonempty-expr-list ')'
1530b57cec5SDimitry Andric ///          attrib-name '(' argument-expression-list [C99 6.5.2] ')'
1540b57cec5SDimitry Andric ///
1550b57cec5SDimitry Andric /// [GNU]  attrib-name:
1560b57cec5SDimitry Andric ///          identifier
1570b57cec5SDimitry Andric ///          typespec
1580b57cec5SDimitry Andric ///          typequal
1590b57cec5SDimitry Andric ///          storageclass
1600b57cec5SDimitry Andric ///
1610b57cec5SDimitry Andric /// Whether an attribute takes an 'identifier' is determined by the
1620b57cec5SDimitry Andric /// attrib-name. GCC's behavior here is not worth imitating:
1630b57cec5SDimitry Andric ///
1640b57cec5SDimitry Andric ///  * In C mode, if the attribute argument list starts with an identifier
1650b57cec5SDimitry Andric ///    followed by a ',' or an ')', and the identifier doesn't resolve to
1660b57cec5SDimitry Andric ///    a type, it is parsed as an identifier. If the attribute actually
1670b57cec5SDimitry Andric ///    wanted an expression, it's out of luck (but it turns out that no
1680b57cec5SDimitry Andric ///    attributes work that way, because C constant expressions are very
1690b57cec5SDimitry Andric ///    limited).
1700b57cec5SDimitry Andric ///  * In C++ mode, if the attribute argument list starts with an identifier,
1710b57cec5SDimitry Andric ///    and the attribute *wants* an identifier, it is parsed as an identifier.
1720b57cec5SDimitry Andric ///    At block scope, any additional tokens between the identifier and the
1730b57cec5SDimitry Andric ///    ',' or ')' are ignored, otherwise they produce a parse error.
1740b57cec5SDimitry Andric ///
1750b57cec5SDimitry Andric /// We follow the C++ model, but don't allow junk after the identifier.
17681ad6265SDimitry Andric void Parser::ParseGNUAttributes(ParsedAttributes &Attrs,
177fe6060f1SDimitry Andric                                 LateParsedAttrList *LateAttrs, Declarator *D) {
1780b57cec5SDimitry Andric   assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
1790b57cec5SDimitry Andric 
18081ad6265SDimitry Andric   SourceLocation StartLoc = Tok.getLocation();
18181ad6265SDimitry Andric   SourceLocation EndLoc = StartLoc;
182fe6060f1SDimitry Andric 
1830b57cec5SDimitry Andric   while (Tok.is(tok::kw___attribute)) {
1840b57cec5SDimitry Andric     SourceLocation AttrTokLoc = ConsumeToken();
185fe6060f1SDimitry Andric     unsigned OldNumAttrs = Attrs.size();
1860b57cec5SDimitry Andric     unsigned OldNumLateAttrs = LateAttrs ? LateAttrs->size() : 0;
1870b57cec5SDimitry Andric 
1880b57cec5SDimitry Andric     if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
1890b57cec5SDimitry Andric                          "attribute")) {
1900b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
1910b57cec5SDimitry Andric       return;
1920b57cec5SDimitry Andric     }
1930b57cec5SDimitry Andric     if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
1940b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
1950b57cec5SDimitry Andric       return;
1960b57cec5SDimitry Andric     }
1970b57cec5SDimitry Andric     // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
1980b57cec5SDimitry Andric     do {
1990b57cec5SDimitry Andric       // Eat preceeding commas to allow __attribute__((,,,foo))
2000b57cec5SDimitry Andric       while (TryConsumeToken(tok::comma))
2010b57cec5SDimitry Andric         ;
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric       // Expect an identifier or declaration specifier (const, int, etc.)
2040b57cec5SDimitry Andric       if (Tok.isAnnotation())
2050b57cec5SDimitry Andric         break;
206349cc55cSDimitry Andric       if (Tok.is(tok::code_completion)) {
207349cc55cSDimitry Andric         cutOffParsing();
208349cc55cSDimitry Andric         Actions.CodeCompleteAttribute(AttributeCommonInfo::Syntax::AS_GNU);
209349cc55cSDimitry Andric         break;
210349cc55cSDimitry Andric       }
2110b57cec5SDimitry Andric       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
2120b57cec5SDimitry Andric       if (!AttrName)
2130b57cec5SDimitry Andric         break;
2140b57cec5SDimitry Andric 
2150b57cec5SDimitry Andric       SourceLocation AttrNameLoc = ConsumeToken();
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric       if (Tok.isNot(tok::l_paren)) {
218fe6060f1SDimitry Andric         Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
21906c3fb27SDimitry Andric                      ParsedAttr::Form::GNU());
2200b57cec5SDimitry Andric         continue;
2210b57cec5SDimitry Andric       }
2220b57cec5SDimitry Andric 
2230b57cec5SDimitry Andric       // Handle "parameterized" attributes
2240b57cec5SDimitry Andric       if (!LateAttrs || !isAttributeLateParsed(*AttrName)) {
22581ad6265SDimitry Andric         ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, &EndLoc, nullptr,
22606c3fb27SDimitry Andric                               SourceLocation(), ParsedAttr::Form::GNU(), D);
2270b57cec5SDimitry Andric         continue;
2280b57cec5SDimitry Andric       }
2290b57cec5SDimitry Andric 
2300b57cec5SDimitry Andric       // Handle attributes with arguments that require late parsing.
2310b57cec5SDimitry Andric       LateParsedAttribute *LA =
2320b57cec5SDimitry Andric           new LateParsedAttribute(this, *AttrName, AttrNameLoc);
2330b57cec5SDimitry Andric       LateAttrs->push_back(LA);
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric       // Attributes in a class are parsed at the end of the class, along
2360b57cec5SDimitry Andric       // with other late-parsed declarations.
2370b57cec5SDimitry Andric       if (!ClassStack.empty() && !LateAttrs->parseSoon())
2380b57cec5SDimitry Andric         getCurrentClass().LateParsedDeclarations.push_back(LA);
2390b57cec5SDimitry Andric 
2400b57cec5SDimitry Andric       // Be sure ConsumeAndStoreUntil doesn't see the start l_paren, since it
2410b57cec5SDimitry Andric       // recursively consumes balanced parens.
2420b57cec5SDimitry Andric       LA->Toks.push_back(Tok);
2430b57cec5SDimitry Andric       ConsumeParen();
2440b57cec5SDimitry Andric       // Consume everything up to and including the matching right parens.
2450b57cec5SDimitry Andric       ConsumeAndStoreUntil(tok::r_paren, LA->Toks, /*StopAtSemi=*/true);
2460b57cec5SDimitry Andric 
2470b57cec5SDimitry Andric       Token Eof;
2480b57cec5SDimitry Andric       Eof.startToken();
2490b57cec5SDimitry Andric       Eof.setLocation(Tok.getLocation());
2500b57cec5SDimitry Andric       LA->Toks.push_back(Eof);
2510b57cec5SDimitry Andric     } while (Tok.is(tok::comma));
2520b57cec5SDimitry Andric 
2530b57cec5SDimitry Andric     if (ExpectAndConsume(tok::r_paren))
2540b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
2550b57cec5SDimitry Andric     SourceLocation Loc = Tok.getLocation();
2560b57cec5SDimitry Andric     if (ExpectAndConsume(tok::r_paren))
2570b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
25881ad6265SDimitry Andric     EndLoc = Loc;
2590b57cec5SDimitry Andric 
2600b57cec5SDimitry Andric     // If this was declared in a macro, attach the macro IdentifierInfo to the
2610b57cec5SDimitry Andric     // parsed attribute.
2620b57cec5SDimitry Andric     auto &SM = PP.getSourceManager();
2630b57cec5SDimitry Andric     if (!SM.isWrittenInBuiltinFile(SM.getSpellingLoc(AttrTokLoc)) &&
2640b57cec5SDimitry Andric         FindLocsWithCommonFileID(PP, AttrTokLoc, Loc)) {
2650b57cec5SDimitry Andric       CharSourceRange ExpansionRange = SM.getExpansionRange(AttrTokLoc);
2660b57cec5SDimitry Andric       StringRef FoundName =
2670b57cec5SDimitry Andric           Lexer::getSourceText(ExpansionRange, SM, PP.getLangOpts());
2680b57cec5SDimitry Andric       IdentifierInfo *MacroII = PP.getIdentifierInfo(FoundName);
2690b57cec5SDimitry Andric 
270fe6060f1SDimitry Andric       for (unsigned i = OldNumAttrs; i < Attrs.size(); ++i)
271fe6060f1SDimitry Andric         Attrs[i].setMacroIdentifier(MacroII, ExpansionRange.getBegin());
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric       if (LateAttrs) {
2740b57cec5SDimitry Andric         for (unsigned i = OldNumLateAttrs; i < LateAttrs->size(); ++i)
2750b57cec5SDimitry Andric           (*LateAttrs)[i]->MacroII = MacroII;
2760b57cec5SDimitry Andric       }
2770b57cec5SDimitry Andric     }
2780b57cec5SDimitry Andric   }
279fe6060f1SDimitry Andric 
28081ad6265SDimitry Andric   Attrs.Range = SourceRange(StartLoc, EndLoc);
2810b57cec5SDimitry Andric }
2820b57cec5SDimitry Andric 
2830b57cec5SDimitry Andric /// Determine whether the given attribute has an identifier argument.
2840b57cec5SDimitry Andric static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
2850b57cec5SDimitry Andric #define CLANG_ATTR_IDENTIFIER_ARG_LIST
2860b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
2870b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
2880b57cec5SDimitry Andric            .Default(false);
2890b57cec5SDimitry Andric #undef CLANG_ATTR_IDENTIFIER_ARG_LIST
2900b57cec5SDimitry Andric }
2910b57cec5SDimitry Andric 
2925f757f3fSDimitry Andric /// Determine whether the given attribute has an identifier argument.
2935f757f3fSDimitry Andric static ParsedAttributeArgumentsProperties
2945f757f3fSDimitry Andric attributeStringLiteralListArg(const IdentifierInfo &II) {
2955f757f3fSDimitry Andric #define CLANG_ATTR_STRING_LITERAL_ARG_LIST
2965f757f3fSDimitry Andric   return llvm::StringSwitch<uint32_t>(normalizeAttrName(II.getName()))
2975f757f3fSDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
2985f757f3fSDimitry Andric       .Default(0);
2995f757f3fSDimitry Andric #undef CLANG_ATTR_STRING_LITERAL_ARG_LIST
3005f757f3fSDimitry Andric }
3015f757f3fSDimitry Andric 
3020b57cec5SDimitry Andric /// Determine whether the given attribute has a variadic identifier argument.
3030b57cec5SDimitry Andric static bool attributeHasVariadicIdentifierArg(const IdentifierInfo &II) {
3040b57cec5SDimitry Andric #define CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST
3050b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
3060b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
3070b57cec5SDimitry Andric            .Default(false);
3080b57cec5SDimitry Andric #undef CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST
3090b57cec5SDimitry Andric }
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric /// Determine whether the given attribute treats kw_this as an identifier.
3120b57cec5SDimitry Andric static bool attributeTreatsKeywordThisAsIdentifier(const IdentifierInfo &II) {
3130b57cec5SDimitry Andric #define CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST
3140b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
3150b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
3160b57cec5SDimitry Andric            .Default(false);
3170b57cec5SDimitry Andric #undef CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST
3180b57cec5SDimitry Andric }
3190b57cec5SDimitry Andric 
32081ad6265SDimitry Andric /// Determine if an attribute accepts parameter packs.
32181ad6265SDimitry Andric static bool attributeAcceptsExprPack(const IdentifierInfo &II) {
32281ad6265SDimitry Andric #define CLANG_ATTR_ACCEPTS_EXPR_PACK
32381ad6265SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
32481ad6265SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
32581ad6265SDimitry Andric       .Default(false);
32681ad6265SDimitry Andric #undef CLANG_ATTR_ACCEPTS_EXPR_PACK
32781ad6265SDimitry Andric }
32881ad6265SDimitry Andric 
3290b57cec5SDimitry Andric /// Determine whether the given attribute parses a type argument.
3300b57cec5SDimitry Andric static bool attributeIsTypeArgAttr(const IdentifierInfo &II) {
3310b57cec5SDimitry Andric #define CLANG_ATTR_TYPE_ARG_LIST
3320b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
3330b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
3340b57cec5SDimitry Andric            .Default(false);
3350b57cec5SDimitry Andric #undef CLANG_ATTR_TYPE_ARG_LIST
3360b57cec5SDimitry Andric }
3370b57cec5SDimitry Andric 
3380b57cec5SDimitry Andric /// Determine whether the given attribute requires parsing its arguments
3390b57cec5SDimitry Andric /// in an unevaluated context or not.
3400b57cec5SDimitry Andric static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) {
3410b57cec5SDimitry Andric #define CLANG_ATTR_ARG_CONTEXT_LIST
3420b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
3430b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
3440b57cec5SDimitry Andric            .Default(false);
3450b57cec5SDimitry Andric #undef CLANG_ATTR_ARG_CONTEXT_LIST
3460b57cec5SDimitry Andric }
3470b57cec5SDimitry Andric 
3480b57cec5SDimitry Andric IdentifierLoc *Parser::ParseIdentifierLoc() {
3490b57cec5SDimitry Andric   assert(Tok.is(tok::identifier) && "expected an identifier");
3500b57cec5SDimitry Andric   IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
3510b57cec5SDimitry Andric                                             Tok.getLocation(),
3520b57cec5SDimitry Andric                                             Tok.getIdentifierInfo());
3530b57cec5SDimitry Andric   ConsumeToken();
3540b57cec5SDimitry Andric   return IL;
3550b57cec5SDimitry Andric }
3560b57cec5SDimitry Andric 
3570b57cec5SDimitry Andric void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
3580b57cec5SDimitry Andric                                        SourceLocation AttrNameLoc,
3590b57cec5SDimitry Andric                                        ParsedAttributes &Attrs,
3600b57cec5SDimitry Andric                                        IdentifierInfo *ScopeName,
3610b57cec5SDimitry Andric                                        SourceLocation ScopeLoc,
36206c3fb27SDimitry Andric                                        ParsedAttr::Form Form) {
3630b57cec5SDimitry Andric   BalancedDelimiterTracker Parens(*this, tok::l_paren);
3640b57cec5SDimitry Andric   Parens.consumeOpen();
3650b57cec5SDimitry Andric 
3660b57cec5SDimitry Andric   TypeResult T;
3670b57cec5SDimitry Andric   if (Tok.isNot(tok::r_paren))
3680b57cec5SDimitry Andric     T = ParseTypeName();
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric   if (Parens.consumeClose())
3710b57cec5SDimitry Andric     return;
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric   if (T.isInvalid())
3740b57cec5SDimitry Andric     return;
3750b57cec5SDimitry Andric 
3760b57cec5SDimitry Andric   if (T.isUsable())
3770b57cec5SDimitry Andric     Attrs.addNewTypeAttr(&AttrName,
3780b57cec5SDimitry Andric                          SourceRange(AttrNameLoc, Parens.getCloseLocation()),
37906c3fb27SDimitry Andric                          ScopeName, ScopeLoc, T.get(), Form);
3800b57cec5SDimitry Andric   else
3810b57cec5SDimitry Andric     Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()),
38206c3fb27SDimitry Andric                  ScopeName, ScopeLoc, nullptr, 0, Form);
3830b57cec5SDimitry Andric }
3840b57cec5SDimitry Andric 
3855f757f3fSDimitry Andric ExprResult
3865f757f3fSDimitry Andric Parser::ParseUnevaluatedStringInAttribute(const IdentifierInfo &AttrName) {
3875f757f3fSDimitry Andric   if (Tok.is(tok::l_paren)) {
3885f757f3fSDimitry Andric     BalancedDelimiterTracker Paren(*this, tok::l_paren);
3895f757f3fSDimitry Andric     Paren.consumeOpen();
3905f757f3fSDimitry Andric     ExprResult Res = ParseUnevaluatedStringInAttribute(AttrName);
3915f757f3fSDimitry Andric     Paren.consumeClose();
3925f757f3fSDimitry Andric     return Res;
3935f757f3fSDimitry Andric   }
3945f757f3fSDimitry Andric   if (!isTokenStringLiteral()) {
3955f757f3fSDimitry Andric     Diag(Tok.getLocation(), diag::err_expected_string_literal)
3965f757f3fSDimitry Andric         << /*in attribute...*/ 4 << AttrName.getName();
3975f757f3fSDimitry Andric     return ExprError();
3985f757f3fSDimitry Andric   }
3995f757f3fSDimitry Andric   return ParseUnevaluatedStringLiteralExpression();
4005f757f3fSDimitry Andric }
4015f757f3fSDimitry Andric 
4025f757f3fSDimitry Andric bool Parser::ParseAttributeArgumentList(
4035f757f3fSDimitry Andric     const IdentifierInfo &AttrName, SmallVectorImpl<Expr *> &Exprs,
4045f757f3fSDimitry Andric     ParsedAttributeArgumentsProperties ArgsProperties) {
4055f757f3fSDimitry Andric   bool SawError = false;
4065f757f3fSDimitry Andric   unsigned Arg = 0;
4075f757f3fSDimitry Andric   while (true) {
4085f757f3fSDimitry Andric     ExprResult Expr;
4095f757f3fSDimitry Andric     if (ArgsProperties.isStringLiteralArg(Arg)) {
4105f757f3fSDimitry Andric       Expr = ParseUnevaluatedStringInAttribute(AttrName);
4115f757f3fSDimitry Andric     } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
4125f757f3fSDimitry Andric       Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
4135f757f3fSDimitry Andric       Expr = ParseBraceInitializer();
4145f757f3fSDimitry Andric     } else {
4155f757f3fSDimitry Andric       Expr = ParseAssignmentExpression();
4165f757f3fSDimitry Andric     }
4175f757f3fSDimitry Andric     Expr = Actions.CorrectDelayedTyposInExpr(Expr);
4185f757f3fSDimitry Andric 
4195f757f3fSDimitry Andric     if (Tok.is(tok::ellipsis))
4205f757f3fSDimitry Andric       Expr = Actions.ActOnPackExpansion(Expr.get(), ConsumeToken());
4215f757f3fSDimitry Andric     else if (Tok.is(tok::code_completion)) {
4225f757f3fSDimitry Andric       // There's nothing to suggest in here as we parsed a full expression.
4235f757f3fSDimitry Andric       // Instead fail and propagate the error since caller might have something
4245f757f3fSDimitry Andric       // the suggest, e.g. signature help in function call. Note that this is
4255f757f3fSDimitry Andric       // performed before pushing the \p Expr, so that signature help can report
4265f757f3fSDimitry Andric       // current argument correctly.
4275f757f3fSDimitry Andric       SawError = true;
4285f757f3fSDimitry Andric       cutOffParsing();
4295f757f3fSDimitry Andric       break;
4305f757f3fSDimitry Andric     }
4315f757f3fSDimitry Andric 
4325f757f3fSDimitry Andric     if (Expr.isInvalid()) {
4335f757f3fSDimitry Andric       SawError = true;
4345f757f3fSDimitry Andric       break;
4355f757f3fSDimitry Andric     }
4365f757f3fSDimitry Andric 
4375f757f3fSDimitry Andric     Exprs.push_back(Expr.get());
4385f757f3fSDimitry Andric 
4395f757f3fSDimitry Andric     if (Tok.isNot(tok::comma))
4405f757f3fSDimitry Andric       break;
4415f757f3fSDimitry Andric     // Move to the next argument, remember where the comma was.
4425f757f3fSDimitry Andric     Token Comma = Tok;
4435f757f3fSDimitry Andric     ConsumeToken();
4445f757f3fSDimitry Andric     checkPotentialAngleBracketDelimiter(Comma);
4455f757f3fSDimitry Andric     Arg++;
4465f757f3fSDimitry Andric   }
4475f757f3fSDimitry Andric 
4485f757f3fSDimitry Andric   if (SawError) {
4495f757f3fSDimitry Andric     // Ensure typos get diagnosed when errors were encountered while parsing the
4505f757f3fSDimitry Andric     // expression list.
4515f757f3fSDimitry Andric     for (auto &E : Exprs) {
4525f757f3fSDimitry Andric       ExprResult Expr = Actions.CorrectDelayedTyposInExpr(E);
4535f757f3fSDimitry Andric       if (Expr.isUsable())
4545f757f3fSDimitry Andric         E = Expr.get();
4555f757f3fSDimitry Andric     }
4565f757f3fSDimitry Andric   }
4575f757f3fSDimitry Andric   return SawError;
4585f757f3fSDimitry Andric }
4595f757f3fSDimitry Andric 
4600b57cec5SDimitry Andric unsigned Parser::ParseAttributeArgsCommon(
4610b57cec5SDimitry Andric     IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
4620b57cec5SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
46306c3fb27SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Form Form) {
4640b57cec5SDimitry Andric   // Ignore the left paren location for now.
4650b57cec5SDimitry Andric   ConsumeParen();
4660b57cec5SDimitry Andric 
4670b57cec5SDimitry Andric   bool ChangeKWThisToIdent = attributeTreatsKeywordThisAsIdentifier(*AttrName);
468a7dea167SDimitry Andric   bool AttributeIsTypeArgAttr = attributeIsTypeArgAttr(*AttrName);
46981ad6265SDimitry Andric   bool AttributeHasVariadicIdentifierArg =
47081ad6265SDimitry Andric       attributeHasVariadicIdentifierArg(*AttrName);
4710b57cec5SDimitry Andric 
4720b57cec5SDimitry Andric   // Interpret "kw_this" as an identifier if the attributed requests it.
4730b57cec5SDimitry Andric   if (ChangeKWThisToIdent && Tok.is(tok::kw_this))
4740b57cec5SDimitry Andric     Tok.setKind(tok::identifier);
4750b57cec5SDimitry Andric 
4760b57cec5SDimitry Andric   ArgsVector ArgExprs;
4770b57cec5SDimitry Andric   if (Tok.is(tok::identifier)) {
4780b57cec5SDimitry Andric     // If this attribute wants an 'identifier' argument, make it so.
47981ad6265SDimitry Andric     bool IsIdentifierArg = AttributeHasVariadicIdentifierArg ||
48081ad6265SDimitry Andric                            attributeHasIdentifierArg(*AttrName);
4810b57cec5SDimitry Andric     ParsedAttr::Kind AttrKind =
48206c3fb27SDimitry Andric         ParsedAttr::getParsedKind(AttrName, ScopeName, Form.getSyntax());
4830b57cec5SDimitry Andric 
4840b57cec5SDimitry Andric     // If we don't know how to parse this attribute, but this is the only
4850b57cec5SDimitry Andric     // token in this argument, assume it's meant to be an identifier.
4860b57cec5SDimitry Andric     if (AttrKind == ParsedAttr::UnknownAttribute ||
4870b57cec5SDimitry Andric         AttrKind == ParsedAttr::IgnoredAttribute) {
4880b57cec5SDimitry Andric       const Token &Next = NextToken();
4890b57cec5SDimitry Andric       IsIdentifierArg = Next.isOneOf(tok::r_paren, tok::comma);
4900b57cec5SDimitry Andric     }
4910b57cec5SDimitry Andric 
4920b57cec5SDimitry Andric     if (IsIdentifierArg)
4930b57cec5SDimitry Andric       ArgExprs.push_back(ParseIdentifierLoc());
4940b57cec5SDimitry Andric   }
4950b57cec5SDimitry Andric 
496a7dea167SDimitry Andric   ParsedType TheParsedType;
4970b57cec5SDimitry Andric   if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
4980b57cec5SDimitry Andric     // Eat the comma.
4990b57cec5SDimitry Andric     if (!ArgExprs.empty())
5000b57cec5SDimitry Andric       ConsumeToken();
5010b57cec5SDimitry Andric 
502a7dea167SDimitry Andric     if (AttributeIsTypeArgAttr) {
50381ad6265SDimitry Andric       // FIXME: Multiple type arguments are not implemented.
504a7dea167SDimitry Andric       TypeResult T = ParseTypeName();
505a7dea167SDimitry Andric       if (T.isInvalid()) {
506a7dea167SDimitry Andric         SkipUntil(tok::r_paren, StopAtSemi);
507a7dea167SDimitry Andric         return 0;
508a7dea167SDimitry Andric       }
509a7dea167SDimitry Andric       if (T.isUsable())
510a7dea167SDimitry Andric         TheParsedType = T.get();
51181ad6265SDimitry Andric     } else if (AttributeHasVariadicIdentifierArg) {
51281ad6265SDimitry Andric       // Parse variadic identifier arg. This can either consume identifiers or
51381ad6265SDimitry Andric       // expressions. Variadic identifier args do not support parameter packs
51481ad6265SDimitry Andric       // because those are typically used for attributes with enumeration
51581ad6265SDimitry Andric       // arguments, and those enumerations are not something the user could
51681ad6265SDimitry Andric       // express via a pack.
51781ad6265SDimitry Andric       do {
51881ad6265SDimitry Andric         // Interpret "kw_this" as an identifier if the attributed requests it.
51981ad6265SDimitry Andric         if (ChangeKWThisToIdent && Tok.is(tok::kw_this))
52081ad6265SDimitry Andric           Tok.setKind(tok::identifier);
52181ad6265SDimitry Andric 
52281ad6265SDimitry Andric         ExprResult ArgExpr;
52381ad6265SDimitry Andric         if (Tok.is(tok::identifier)) {
5240b57cec5SDimitry Andric           ArgExprs.push_back(ParseIdentifierLoc());
5250b57cec5SDimitry Andric         } else {
5260b57cec5SDimitry Andric           bool Uneval = attributeParsedArgsUnevaluated(*AttrName);
5270b57cec5SDimitry Andric           EnterExpressionEvaluationContext Unevaluated(
5280b57cec5SDimitry Andric               Actions,
5290b57cec5SDimitry Andric               Uneval ? Sema::ExpressionEvaluationContext::Unevaluated
5300b57cec5SDimitry Andric                      : Sema::ExpressionEvaluationContext::ConstantEvaluated);
5310b57cec5SDimitry Andric 
5320b57cec5SDimitry Andric           ExprResult ArgExpr(
5330b57cec5SDimitry Andric               Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()));
53481ad6265SDimitry Andric 
5350b57cec5SDimitry Andric           if (ArgExpr.isInvalid()) {
5360b57cec5SDimitry Andric             SkipUntil(tok::r_paren, StopAtSemi);
5370b57cec5SDimitry Andric             return 0;
5380b57cec5SDimitry Andric           }
5390b57cec5SDimitry Andric           ArgExprs.push_back(ArgExpr.get());
5400b57cec5SDimitry Andric         }
5410b57cec5SDimitry Andric         // Eat the comma, move to the next argument
5420b57cec5SDimitry Andric       } while (TryConsumeToken(tok::comma));
54381ad6265SDimitry Andric     } else {
54481ad6265SDimitry Andric       // General case. Parse all available expressions.
54581ad6265SDimitry Andric       bool Uneval = attributeParsedArgsUnevaluated(*AttrName);
54681ad6265SDimitry Andric       EnterExpressionEvaluationContext Unevaluated(
54781ad6265SDimitry Andric           Actions, Uneval
54881ad6265SDimitry Andric                        ? Sema::ExpressionEvaluationContext::Unevaluated
54981ad6265SDimitry Andric                        : Sema::ExpressionEvaluationContext::ConstantEvaluated);
55081ad6265SDimitry Andric 
55181ad6265SDimitry Andric       ExprVector ParsedExprs;
5525f757f3fSDimitry Andric       ParsedAttributeArgumentsProperties ArgProperties =
5535f757f3fSDimitry Andric           attributeStringLiteralListArg(*AttrName);
5545f757f3fSDimitry Andric       if (ParseAttributeArgumentList(*AttrName, ParsedExprs, ArgProperties)) {
55581ad6265SDimitry Andric         SkipUntil(tok::r_paren, StopAtSemi);
55681ad6265SDimitry Andric         return 0;
55781ad6265SDimitry Andric       }
55881ad6265SDimitry Andric 
55981ad6265SDimitry Andric       // Pack expansion must currently be explicitly supported by an attribute.
56081ad6265SDimitry Andric       for (size_t I = 0; I < ParsedExprs.size(); ++I) {
56181ad6265SDimitry Andric         if (!isa<PackExpansionExpr>(ParsedExprs[I]))
56281ad6265SDimitry Andric           continue;
56381ad6265SDimitry Andric 
56481ad6265SDimitry Andric         if (!attributeAcceptsExprPack(*AttrName)) {
56581ad6265SDimitry Andric           Diag(Tok.getLocation(),
56681ad6265SDimitry Andric                diag::err_attribute_argument_parm_pack_not_supported)
56781ad6265SDimitry Andric               << AttrName;
56881ad6265SDimitry Andric           SkipUntil(tok::r_paren, StopAtSemi);
56981ad6265SDimitry Andric           return 0;
57081ad6265SDimitry Andric         }
57181ad6265SDimitry Andric       }
57281ad6265SDimitry Andric 
57381ad6265SDimitry Andric       ArgExprs.insert(ArgExprs.end(), ParsedExprs.begin(), ParsedExprs.end());
57481ad6265SDimitry Andric     }
5750b57cec5SDimitry Andric   }
5760b57cec5SDimitry Andric 
5770b57cec5SDimitry Andric   SourceLocation RParen = Tok.getLocation();
5780b57cec5SDimitry Andric   if (!ExpectAndConsume(tok::r_paren)) {
5790b57cec5SDimitry Andric     SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
580a7dea167SDimitry Andric 
581a7dea167SDimitry Andric     if (AttributeIsTypeArgAttr && !TheParsedType.get().isNull()) {
582a7dea167SDimitry Andric       Attrs.addNewTypeAttr(AttrName, SourceRange(AttrNameLoc, RParen),
58306c3fb27SDimitry Andric                            ScopeName, ScopeLoc, TheParsedType, Form);
584a7dea167SDimitry Andric     } else {
5850b57cec5SDimitry Andric       Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
58606c3fb27SDimitry Andric                    ArgExprs.data(), ArgExprs.size(), Form);
5870b57cec5SDimitry Andric     }
588a7dea167SDimitry Andric   }
5890b57cec5SDimitry Andric 
5900b57cec5SDimitry Andric   if (EndLoc)
5910b57cec5SDimitry Andric     *EndLoc = RParen;
5920b57cec5SDimitry Andric 
593a7dea167SDimitry Andric   return static_cast<unsigned>(ArgExprs.size() + !TheParsedType.get().isNull());
5940b57cec5SDimitry Andric }
5950b57cec5SDimitry Andric 
5960b57cec5SDimitry Andric /// Parse the arguments to a parameterized GNU attribute or
5970b57cec5SDimitry Andric /// a C++11 attribute in "gnu" namespace.
59881ad6265SDimitry Andric void Parser::ParseGNUAttributeArgs(
59981ad6265SDimitry Andric     IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
60081ad6265SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
60106c3fb27SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Form Form, Declarator *D) {
6020b57cec5SDimitry Andric 
6030b57cec5SDimitry Andric   assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
6040b57cec5SDimitry Andric 
6050b57cec5SDimitry Andric   ParsedAttr::Kind AttrKind =
60606c3fb27SDimitry Andric       ParsedAttr::getParsedKind(AttrName, ScopeName, Form.getSyntax());
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric   if (AttrKind == ParsedAttr::AT_Availability) {
6090b57cec5SDimitry Andric     ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
61006c3fb27SDimitry Andric                                ScopeLoc, Form);
6110b57cec5SDimitry Andric     return;
6120b57cec5SDimitry Andric   } else if (AttrKind == ParsedAttr::AT_ExternalSourceSymbol) {
6130b57cec5SDimitry Andric     ParseExternalSourceSymbolAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
61406c3fb27SDimitry Andric                                        ScopeName, ScopeLoc, Form);
6150b57cec5SDimitry Andric     return;
6160b57cec5SDimitry Andric   } else if (AttrKind == ParsedAttr::AT_ObjCBridgeRelated) {
6170b57cec5SDimitry Andric     ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
61806c3fb27SDimitry Andric                                     ScopeName, ScopeLoc, Form);
6190b57cec5SDimitry Andric     return;
620e8d8bef9SDimitry Andric   } else if (AttrKind == ParsedAttr::AT_SwiftNewType) {
621e8d8bef9SDimitry Andric     ParseSwiftNewTypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
62206c3fb27SDimitry Andric                                ScopeLoc, Form);
623e8d8bef9SDimitry Andric     return;
6240b57cec5SDimitry Andric   } else if (AttrKind == ParsedAttr::AT_TypeTagForDatatype) {
6250b57cec5SDimitry Andric     ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
62606c3fb27SDimitry Andric                                      ScopeName, ScopeLoc, Form);
6270b57cec5SDimitry Andric     return;
6280b57cec5SDimitry Andric   } else if (attributeIsTypeArgAttr(*AttrName)) {
62981ad6265SDimitry Andric     ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, ScopeName,
63006c3fb27SDimitry Andric                               ScopeLoc, Form);
6310b57cec5SDimitry Andric     return;
6320b57cec5SDimitry Andric   }
6330b57cec5SDimitry Andric 
6340b57cec5SDimitry Andric   // These may refer to the function arguments, but need to be parsed early to
6350b57cec5SDimitry Andric   // participate in determining whether it's a redeclaration.
636bdd1243dSDimitry Andric   std::optional<ParseScope> PrototypeScope;
6370b57cec5SDimitry Andric   if (normalizeAttrName(AttrName->getName()) == "enable_if" &&
6380b57cec5SDimitry Andric       D && D->isFunctionDeclarator()) {
6390b57cec5SDimitry Andric     DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo();
6400b57cec5SDimitry Andric     PrototypeScope.emplace(this, Scope::FunctionPrototypeScope |
6410b57cec5SDimitry Andric                                      Scope::FunctionDeclarationScope |
6420b57cec5SDimitry Andric                                      Scope::DeclScope);
6430b57cec5SDimitry Andric     for (unsigned i = 0; i != FTI.NumParams; ++i) {
6440b57cec5SDimitry Andric       ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param);
6450b57cec5SDimitry Andric       Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param);
6460b57cec5SDimitry Andric     }
6470b57cec5SDimitry Andric   }
6480b57cec5SDimitry Andric 
6490b57cec5SDimitry Andric   ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
65006c3fb27SDimitry Andric                            ScopeLoc, Form);
6510b57cec5SDimitry Andric }
6520b57cec5SDimitry Andric 
6530b57cec5SDimitry Andric unsigned Parser::ParseClangAttributeArgs(
6540b57cec5SDimitry Andric     IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
6550b57cec5SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
65606c3fb27SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Form Form) {
6570b57cec5SDimitry Andric   assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
6580b57cec5SDimitry Andric 
6590b57cec5SDimitry Andric   ParsedAttr::Kind AttrKind =
66006c3fb27SDimitry Andric       ParsedAttr::getParsedKind(AttrName, ScopeName, Form.getSyntax());
6610b57cec5SDimitry Andric 
6620b57cec5SDimitry Andric   switch (AttrKind) {
6630b57cec5SDimitry Andric   default:
6640b57cec5SDimitry Andric     return ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
66506c3fb27SDimitry Andric                                     ScopeName, ScopeLoc, Form);
6660b57cec5SDimitry Andric   case ParsedAttr::AT_ExternalSourceSymbol:
6670b57cec5SDimitry Andric     ParseExternalSourceSymbolAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
66806c3fb27SDimitry Andric                                        ScopeName, ScopeLoc, Form);
6690b57cec5SDimitry Andric     break;
6700b57cec5SDimitry Andric   case ParsedAttr::AT_Availability:
6710b57cec5SDimitry Andric     ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
67206c3fb27SDimitry Andric                                ScopeLoc, Form);
6730b57cec5SDimitry Andric     break;
6740b57cec5SDimitry Andric   case ParsedAttr::AT_ObjCBridgeRelated:
6750b57cec5SDimitry Andric     ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
67606c3fb27SDimitry Andric                                     ScopeName, ScopeLoc, Form);
6770b57cec5SDimitry Andric     break;
678e8d8bef9SDimitry Andric   case ParsedAttr::AT_SwiftNewType:
679e8d8bef9SDimitry Andric     ParseSwiftNewTypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
68006c3fb27SDimitry Andric                                ScopeLoc, Form);
681e8d8bef9SDimitry Andric     break;
6820b57cec5SDimitry Andric   case ParsedAttr::AT_TypeTagForDatatype:
6830b57cec5SDimitry Andric     ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
68406c3fb27SDimitry Andric                                      ScopeName, ScopeLoc, Form);
6850b57cec5SDimitry Andric     break;
6860b57cec5SDimitry Andric   }
6870b57cec5SDimitry Andric   return !Attrs.empty() ? Attrs.begin()->getNumArgs() : 0;
6880b57cec5SDimitry Andric }
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric bool Parser::ParseMicrosoftDeclSpecArgs(IdentifierInfo *AttrName,
6910b57cec5SDimitry Andric                                         SourceLocation AttrNameLoc,
6920b57cec5SDimitry Andric                                         ParsedAttributes &Attrs) {
69381ad6265SDimitry Andric   unsigned ExistingAttrs = Attrs.size();
69481ad6265SDimitry Andric 
6950b57cec5SDimitry Andric   // If the attribute isn't known, we will not attempt to parse any
6960b57cec5SDimitry Andric   // arguments.
69781ad6265SDimitry Andric   if (!hasAttribute(AttributeCommonInfo::Syntax::AS_Declspec, nullptr, AttrName,
6980b57cec5SDimitry Andric                     getTargetInfo(), getLangOpts())) {
6990b57cec5SDimitry Andric     // Eat the left paren, then skip to the ending right paren.
7000b57cec5SDimitry Andric     ConsumeParen();
7010b57cec5SDimitry Andric     SkipUntil(tok::r_paren);
7020b57cec5SDimitry Andric     return false;
7030b57cec5SDimitry Andric   }
7040b57cec5SDimitry Andric 
7050b57cec5SDimitry Andric   SourceLocation OpenParenLoc = Tok.getLocation();
7060b57cec5SDimitry Andric 
7070b57cec5SDimitry Andric   if (AttrName->getName() == "property") {
7080b57cec5SDimitry Andric     // The property declspec is more complex in that it can take one or two
7090b57cec5SDimitry Andric     // assignment expressions as a parameter, but the lhs of the assignment
7100b57cec5SDimitry Andric     // must be named get or put.
7110b57cec5SDimitry Andric 
7120b57cec5SDimitry Andric     BalancedDelimiterTracker T(*this, tok::l_paren);
7130b57cec5SDimitry Andric     T.expectAndConsume(diag::err_expected_lparen_after,
7140b57cec5SDimitry Andric                        AttrName->getNameStart(), tok::r_paren);
7150b57cec5SDimitry Andric 
7160b57cec5SDimitry Andric     enum AccessorKind {
7170b57cec5SDimitry Andric       AK_Invalid = -1,
7180b57cec5SDimitry Andric       AK_Put = 0,
7190b57cec5SDimitry Andric       AK_Get = 1 // indices into AccessorNames
7200b57cec5SDimitry Andric     };
7210b57cec5SDimitry Andric     IdentifierInfo *AccessorNames[] = {nullptr, nullptr};
7220b57cec5SDimitry Andric     bool HasInvalidAccessor = false;
7230b57cec5SDimitry Andric 
7240b57cec5SDimitry Andric     // Parse the accessor specifications.
7250b57cec5SDimitry Andric     while (true) {
7260b57cec5SDimitry Andric       // Stop if this doesn't look like an accessor spec.
7270b57cec5SDimitry Andric       if (!Tok.is(tok::identifier)) {
7280b57cec5SDimitry Andric         // If the user wrote a completely empty list, use a special diagnostic.
7290b57cec5SDimitry Andric         if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
7300b57cec5SDimitry Andric             AccessorNames[AK_Put] == nullptr &&
7310b57cec5SDimitry Andric             AccessorNames[AK_Get] == nullptr) {
7320b57cec5SDimitry Andric           Diag(AttrNameLoc, diag::err_ms_property_no_getter_or_putter);
7330b57cec5SDimitry Andric           break;
7340b57cec5SDimitry Andric         }
7350b57cec5SDimitry Andric 
7360b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
7370b57cec5SDimitry Andric         break;
7380b57cec5SDimitry Andric       }
7390b57cec5SDimitry Andric 
7400b57cec5SDimitry Andric       AccessorKind Kind;
7410b57cec5SDimitry Andric       SourceLocation KindLoc = Tok.getLocation();
7420b57cec5SDimitry Andric       StringRef KindStr = Tok.getIdentifierInfo()->getName();
7430b57cec5SDimitry Andric       if (KindStr == "get") {
7440b57cec5SDimitry Andric         Kind = AK_Get;
7450b57cec5SDimitry Andric       } else if (KindStr == "put") {
7460b57cec5SDimitry Andric         Kind = AK_Put;
7470b57cec5SDimitry Andric 
7480b57cec5SDimitry Andric         // Recover from the common mistake of using 'set' instead of 'put'.
7490b57cec5SDimitry Andric       } else if (KindStr == "set") {
7500b57cec5SDimitry Andric         Diag(KindLoc, diag::err_ms_property_has_set_accessor)
7510b57cec5SDimitry Andric             << FixItHint::CreateReplacement(KindLoc, "put");
7520b57cec5SDimitry Andric         Kind = AK_Put;
7530b57cec5SDimitry Andric 
7540b57cec5SDimitry Andric         // Handle the mistake of forgetting the accessor kind by skipping
7550b57cec5SDimitry Andric         // this accessor.
7560b57cec5SDimitry Andric       } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
7570b57cec5SDimitry Andric         Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
7580b57cec5SDimitry Andric         ConsumeToken();
7590b57cec5SDimitry Andric         HasInvalidAccessor = true;
7600b57cec5SDimitry Andric         goto next_property_accessor;
7610b57cec5SDimitry Andric 
7620b57cec5SDimitry Andric         // Otherwise, complain about the unknown accessor kind.
7630b57cec5SDimitry Andric       } else {
7640b57cec5SDimitry Andric         Diag(KindLoc, diag::err_ms_property_unknown_accessor);
7650b57cec5SDimitry Andric         HasInvalidAccessor = true;
7660b57cec5SDimitry Andric         Kind = AK_Invalid;
7670b57cec5SDimitry Andric 
7680b57cec5SDimitry Andric         // Try to keep parsing unless it doesn't look like an accessor spec.
7690b57cec5SDimitry Andric         if (!NextToken().is(tok::equal))
7700b57cec5SDimitry Andric           break;
7710b57cec5SDimitry Andric       }
7720b57cec5SDimitry Andric 
7730b57cec5SDimitry Andric       // Consume the identifier.
7740b57cec5SDimitry Andric       ConsumeToken();
7750b57cec5SDimitry Andric 
7760b57cec5SDimitry Andric       // Consume the '='.
7770b57cec5SDimitry Andric       if (!TryConsumeToken(tok::equal)) {
7780b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
7790b57cec5SDimitry Andric             << KindStr;
7800b57cec5SDimitry Andric         break;
7810b57cec5SDimitry Andric       }
7820b57cec5SDimitry Andric 
7830b57cec5SDimitry Andric       // Expect the method name.
7840b57cec5SDimitry Andric       if (!Tok.is(tok::identifier)) {
7850b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
7860b57cec5SDimitry Andric         break;
7870b57cec5SDimitry Andric       }
7880b57cec5SDimitry Andric 
7890b57cec5SDimitry Andric       if (Kind == AK_Invalid) {
7900b57cec5SDimitry Andric         // Just drop invalid accessors.
7910b57cec5SDimitry Andric       } else if (AccessorNames[Kind] != nullptr) {
7920b57cec5SDimitry Andric         // Complain about the repeated accessor, ignore it, and keep parsing.
7930b57cec5SDimitry Andric         Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
7940b57cec5SDimitry Andric       } else {
7950b57cec5SDimitry Andric         AccessorNames[Kind] = Tok.getIdentifierInfo();
7960b57cec5SDimitry Andric       }
7970b57cec5SDimitry Andric       ConsumeToken();
7980b57cec5SDimitry Andric 
7990b57cec5SDimitry Andric     next_property_accessor:
8000b57cec5SDimitry Andric       // Keep processing accessors until we run out.
8010b57cec5SDimitry Andric       if (TryConsumeToken(tok::comma))
8020b57cec5SDimitry Andric         continue;
8030b57cec5SDimitry Andric 
8040b57cec5SDimitry Andric       // If we run into the ')', stop without consuming it.
8050b57cec5SDimitry Andric       if (Tok.is(tok::r_paren))
8060b57cec5SDimitry Andric         break;
8070b57cec5SDimitry Andric 
8080b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
8090b57cec5SDimitry Andric       break;
8100b57cec5SDimitry Andric     }
8110b57cec5SDimitry Andric 
8120b57cec5SDimitry Andric     // Only add the property attribute if it was well-formed.
8130b57cec5SDimitry Andric     if (!HasInvalidAccessor)
8140b57cec5SDimitry Andric       Attrs.addNewPropertyAttr(AttrName, AttrNameLoc, nullptr, SourceLocation(),
8150b57cec5SDimitry Andric                                AccessorNames[AK_Get], AccessorNames[AK_Put],
81606c3fb27SDimitry Andric                                ParsedAttr::Form::Declspec());
8170b57cec5SDimitry Andric     T.skipToEnd();
8180b57cec5SDimitry Andric     return !HasInvalidAccessor;
8190b57cec5SDimitry Andric   }
8200b57cec5SDimitry Andric 
8210b57cec5SDimitry Andric   unsigned NumArgs =
8220b57cec5SDimitry Andric       ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, nullptr, nullptr,
82306c3fb27SDimitry Andric                                SourceLocation(), ParsedAttr::Form::Declspec());
8240b57cec5SDimitry Andric 
8250b57cec5SDimitry Andric   // If this attribute's args were parsed, and it was expected to have
8260b57cec5SDimitry Andric   // arguments but none were provided, emit a diagnostic.
82781ad6265SDimitry Andric   if (ExistingAttrs < Attrs.size() && Attrs.back().getMaxArgs() && !NumArgs) {
8280b57cec5SDimitry Andric     Diag(OpenParenLoc, diag::err_attribute_requires_arguments) << AttrName;
8290b57cec5SDimitry Andric     return false;
8300b57cec5SDimitry Andric   }
8310b57cec5SDimitry Andric   return true;
8320b57cec5SDimitry Andric }
8330b57cec5SDimitry Andric 
8340b57cec5SDimitry Andric /// [MS] decl-specifier:
8350b57cec5SDimitry Andric ///             __declspec ( extended-decl-modifier-seq )
8360b57cec5SDimitry Andric ///
8370b57cec5SDimitry Andric /// [MS] extended-decl-modifier-seq:
8380b57cec5SDimitry Andric ///             extended-decl-modifier[opt]
8390b57cec5SDimitry Andric ///             extended-decl-modifier extended-decl-modifier-seq
84081ad6265SDimitry Andric void Parser::ParseMicrosoftDeclSpecs(ParsedAttributes &Attrs) {
8410b57cec5SDimitry Andric   assert(getLangOpts().DeclSpecKeyword && "__declspec keyword is not enabled");
8420b57cec5SDimitry Andric   assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
8430b57cec5SDimitry Andric 
84481ad6265SDimitry Andric   SourceLocation StartLoc = Tok.getLocation();
84581ad6265SDimitry Andric   SourceLocation EndLoc = StartLoc;
84681ad6265SDimitry Andric 
8470b57cec5SDimitry Andric   while (Tok.is(tok::kw___declspec)) {
8480b57cec5SDimitry Andric     ConsumeToken();
8490b57cec5SDimitry Andric     BalancedDelimiterTracker T(*this, tok::l_paren);
8500b57cec5SDimitry Andric     if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
8510b57cec5SDimitry Andric                            tok::r_paren))
8520b57cec5SDimitry Andric       return;
8530b57cec5SDimitry Andric 
8540b57cec5SDimitry Andric     // An empty declspec is perfectly legal and should not warn.  Additionally,
8550b57cec5SDimitry Andric     // you can specify multiple attributes per declspec.
8560b57cec5SDimitry Andric     while (Tok.isNot(tok::r_paren)) {
8570b57cec5SDimitry Andric       // Attribute not present.
8580b57cec5SDimitry Andric       if (TryConsumeToken(tok::comma))
8590b57cec5SDimitry Andric         continue;
8600b57cec5SDimitry Andric 
861349cc55cSDimitry Andric       if (Tok.is(tok::code_completion)) {
862349cc55cSDimitry Andric         cutOffParsing();
863349cc55cSDimitry Andric         Actions.CodeCompleteAttribute(AttributeCommonInfo::AS_Declspec);
864349cc55cSDimitry Andric         return;
865349cc55cSDimitry Andric       }
866349cc55cSDimitry Andric 
8670b57cec5SDimitry Andric       // We expect either a well-known identifier or a generic string.  Anything
8680b57cec5SDimitry Andric       // else is a malformed declspec.
8690b57cec5SDimitry Andric       bool IsString = Tok.getKind() == tok::string_literal;
8700b57cec5SDimitry Andric       if (!IsString && Tok.getKind() != tok::identifier &&
8710b57cec5SDimitry Andric           Tok.getKind() != tok::kw_restrict) {
8720b57cec5SDimitry Andric         Diag(Tok, diag::err_ms_declspec_type);
8730b57cec5SDimitry Andric         T.skipToEnd();
8740b57cec5SDimitry Andric         return;
8750b57cec5SDimitry Andric       }
8760b57cec5SDimitry Andric 
8770b57cec5SDimitry Andric       IdentifierInfo *AttrName;
8780b57cec5SDimitry Andric       SourceLocation AttrNameLoc;
8790b57cec5SDimitry Andric       if (IsString) {
8800b57cec5SDimitry Andric         SmallString<8> StrBuffer;
8810b57cec5SDimitry Andric         bool Invalid = false;
8820b57cec5SDimitry Andric         StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
8830b57cec5SDimitry Andric         if (Invalid) {
8840b57cec5SDimitry Andric           T.skipToEnd();
8850b57cec5SDimitry Andric           return;
8860b57cec5SDimitry Andric         }
8870b57cec5SDimitry Andric         AttrName = PP.getIdentifierInfo(Str);
8880b57cec5SDimitry Andric         AttrNameLoc = ConsumeStringToken();
8890b57cec5SDimitry Andric       } else {
8900b57cec5SDimitry Andric         AttrName = Tok.getIdentifierInfo();
8910b57cec5SDimitry Andric         AttrNameLoc = ConsumeToken();
8920b57cec5SDimitry Andric       }
8930b57cec5SDimitry Andric 
8940b57cec5SDimitry Andric       bool AttrHandled = false;
8950b57cec5SDimitry Andric 
8960b57cec5SDimitry Andric       // Parse attribute arguments.
8970b57cec5SDimitry Andric       if (Tok.is(tok::l_paren))
8980b57cec5SDimitry Andric         AttrHandled = ParseMicrosoftDeclSpecArgs(AttrName, AttrNameLoc, Attrs);
8990b57cec5SDimitry Andric       else if (AttrName->getName() == "property")
9000b57cec5SDimitry Andric         // The property attribute must have an argument list.
9010b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_expected_lparen_after)
9020b57cec5SDimitry Andric             << AttrName->getName();
9030b57cec5SDimitry Andric 
9040b57cec5SDimitry Andric       if (!AttrHandled)
9050b57cec5SDimitry Andric         Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
90606c3fb27SDimitry Andric                      ParsedAttr::Form::Declspec());
9070b57cec5SDimitry Andric     }
9080b57cec5SDimitry Andric     T.consumeClose();
90981ad6265SDimitry Andric     EndLoc = T.getCloseLocation();
9100b57cec5SDimitry Andric   }
91181ad6265SDimitry Andric 
91281ad6265SDimitry Andric   Attrs.Range = SourceRange(StartLoc, EndLoc);
9130b57cec5SDimitry Andric }
9140b57cec5SDimitry Andric 
9150b57cec5SDimitry Andric void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
9160b57cec5SDimitry Andric   // Treat these like attributes
9170b57cec5SDimitry Andric   while (true) {
91806c3fb27SDimitry Andric     auto Kind = Tok.getKind();
91906c3fb27SDimitry Andric     switch (Kind) {
9200b57cec5SDimitry Andric     case tok::kw___fastcall:
9210b57cec5SDimitry Andric     case tok::kw___stdcall:
9220b57cec5SDimitry Andric     case tok::kw___thiscall:
9230b57cec5SDimitry Andric     case tok::kw___regcall:
9240b57cec5SDimitry Andric     case tok::kw___cdecl:
9250b57cec5SDimitry Andric     case tok::kw___vectorcall:
9260b57cec5SDimitry Andric     case tok::kw___ptr64:
9270b57cec5SDimitry Andric     case tok::kw___w64:
9280b57cec5SDimitry Andric     case tok::kw___ptr32:
9290b57cec5SDimitry Andric     case tok::kw___sptr:
9300b57cec5SDimitry Andric     case tok::kw___uptr: {
9310b57cec5SDimitry Andric       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
9320b57cec5SDimitry Andric       SourceLocation AttrNameLoc = ConsumeToken();
9330b57cec5SDimitry Andric       attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
93406c3fb27SDimitry Andric                    Kind);
9350b57cec5SDimitry Andric       break;
9360b57cec5SDimitry Andric     }
9370b57cec5SDimitry Andric     default:
9380b57cec5SDimitry Andric       return;
9390b57cec5SDimitry Andric     }
9400b57cec5SDimitry Andric   }
9410b57cec5SDimitry Andric }
9420b57cec5SDimitry Andric 
94306c3fb27SDimitry Andric void Parser::ParseWebAssemblyFuncrefTypeAttribute(ParsedAttributes &attrs) {
94406c3fb27SDimitry Andric   assert(Tok.is(tok::kw___funcref));
94506c3fb27SDimitry Andric   SourceLocation StartLoc = Tok.getLocation();
94606c3fb27SDimitry Andric   if (!getTargetInfo().getTriple().isWasm()) {
94706c3fb27SDimitry Andric     ConsumeToken();
94806c3fb27SDimitry Andric     Diag(StartLoc, diag::err_wasm_funcref_not_wasm);
94906c3fb27SDimitry Andric     return;
95006c3fb27SDimitry Andric   }
95106c3fb27SDimitry Andric 
95206c3fb27SDimitry Andric   IdentifierInfo *AttrName = Tok.getIdentifierInfo();
95306c3fb27SDimitry Andric   SourceLocation AttrNameLoc = ConsumeToken();
95406c3fb27SDimitry Andric   attrs.addNew(AttrName, AttrNameLoc, /*ScopeName=*/nullptr,
95506c3fb27SDimitry Andric                /*ScopeLoc=*/SourceLocation{}, /*Args=*/nullptr, /*numArgs=*/0,
95606c3fb27SDimitry Andric                tok::kw___funcref);
95706c3fb27SDimitry Andric }
95806c3fb27SDimitry Andric 
9590b57cec5SDimitry Andric void Parser::DiagnoseAndSkipExtendedMicrosoftTypeAttributes() {
9600b57cec5SDimitry Andric   SourceLocation StartLoc = Tok.getLocation();
9610b57cec5SDimitry Andric   SourceLocation EndLoc = SkipExtendedMicrosoftTypeAttributes();
9620b57cec5SDimitry Andric 
9630b57cec5SDimitry Andric   if (EndLoc.isValid()) {
9640b57cec5SDimitry Andric     SourceRange Range(StartLoc, EndLoc);
9650b57cec5SDimitry Andric     Diag(StartLoc, diag::warn_microsoft_qualifiers_ignored) << Range;
9660b57cec5SDimitry Andric   }
9670b57cec5SDimitry Andric }
9680b57cec5SDimitry Andric 
9690b57cec5SDimitry Andric SourceLocation Parser::SkipExtendedMicrosoftTypeAttributes() {
9700b57cec5SDimitry Andric   SourceLocation EndLoc;
9710b57cec5SDimitry Andric 
9720b57cec5SDimitry Andric   while (true) {
9730b57cec5SDimitry Andric     switch (Tok.getKind()) {
9740b57cec5SDimitry Andric     case tok::kw_const:
9750b57cec5SDimitry Andric     case tok::kw_volatile:
9760b57cec5SDimitry Andric     case tok::kw___fastcall:
9770b57cec5SDimitry Andric     case tok::kw___stdcall:
9780b57cec5SDimitry Andric     case tok::kw___thiscall:
9790b57cec5SDimitry Andric     case tok::kw___cdecl:
9800b57cec5SDimitry Andric     case tok::kw___vectorcall:
9810b57cec5SDimitry Andric     case tok::kw___ptr32:
9820b57cec5SDimitry Andric     case tok::kw___ptr64:
9830b57cec5SDimitry Andric     case tok::kw___w64:
9840b57cec5SDimitry Andric     case tok::kw___unaligned:
9850b57cec5SDimitry Andric     case tok::kw___sptr:
9860b57cec5SDimitry Andric     case tok::kw___uptr:
9870b57cec5SDimitry Andric       EndLoc = ConsumeToken();
9880b57cec5SDimitry Andric       break;
9890b57cec5SDimitry Andric     default:
9900b57cec5SDimitry Andric       return EndLoc;
9910b57cec5SDimitry Andric     }
9920b57cec5SDimitry Andric   }
9930b57cec5SDimitry Andric }
9940b57cec5SDimitry Andric 
9950b57cec5SDimitry Andric void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
9960b57cec5SDimitry Andric   // Treat these like attributes
9970b57cec5SDimitry Andric   while (Tok.is(tok::kw___pascal)) {
9980b57cec5SDimitry Andric     IdentifierInfo *AttrName = Tok.getIdentifierInfo();
9990b57cec5SDimitry Andric     SourceLocation AttrNameLoc = ConsumeToken();
10000b57cec5SDimitry Andric     attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
100106c3fb27SDimitry Andric                  tok::kw___pascal);
10020b57cec5SDimitry Andric   }
10030b57cec5SDimitry Andric }
10040b57cec5SDimitry Andric 
10050b57cec5SDimitry Andric void Parser::ParseOpenCLKernelAttributes(ParsedAttributes &attrs) {
10060b57cec5SDimitry Andric   // Treat these like attributes
10070b57cec5SDimitry Andric   while (Tok.is(tok::kw___kernel)) {
10080b57cec5SDimitry Andric     IdentifierInfo *AttrName = Tok.getIdentifierInfo();
10090b57cec5SDimitry Andric     SourceLocation AttrNameLoc = ConsumeToken();
10100b57cec5SDimitry Andric     attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
101106c3fb27SDimitry Andric                  tok::kw___kernel);
10120b57cec5SDimitry Andric   }
10130b57cec5SDimitry Andric }
10140b57cec5SDimitry Andric 
101581ad6265SDimitry Andric void Parser::ParseCUDAFunctionAttributes(ParsedAttributes &attrs) {
101681ad6265SDimitry Andric   while (Tok.is(tok::kw___noinline__)) {
101781ad6265SDimitry Andric     IdentifierInfo *AttrName = Tok.getIdentifierInfo();
101881ad6265SDimitry Andric     SourceLocation AttrNameLoc = ConsumeToken();
101981ad6265SDimitry Andric     attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
102006c3fb27SDimitry Andric                  tok::kw___noinline__);
102181ad6265SDimitry Andric   }
102281ad6265SDimitry Andric }
102381ad6265SDimitry Andric 
10240b57cec5SDimitry Andric void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) {
10250b57cec5SDimitry Andric   IdentifierInfo *AttrName = Tok.getIdentifierInfo();
10260b57cec5SDimitry Andric   SourceLocation AttrNameLoc = Tok.getLocation();
10270b57cec5SDimitry Andric   Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
102806c3fb27SDimitry Andric                Tok.getKind());
10290b57cec5SDimitry Andric }
10300b57cec5SDimitry Andric 
1031bdd1243dSDimitry Andric bool Parser::isHLSLQualifier(const Token &Tok) const {
1032bdd1243dSDimitry Andric   return Tok.is(tok::kw_groupshared);
1033bdd1243dSDimitry Andric }
1034bdd1243dSDimitry Andric 
1035bdd1243dSDimitry Andric void Parser::ParseHLSLQualifiers(ParsedAttributes &Attrs) {
1036bdd1243dSDimitry Andric   IdentifierInfo *AttrName = Tok.getIdentifierInfo();
103706c3fb27SDimitry Andric   auto Kind = Tok.getKind();
1038bdd1243dSDimitry Andric   SourceLocation AttrNameLoc = ConsumeToken();
103906c3fb27SDimitry Andric   Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, Kind);
1040bdd1243dSDimitry Andric }
1041bdd1243dSDimitry Andric 
10420b57cec5SDimitry Andric void Parser::ParseNullabilityTypeSpecifiers(ParsedAttributes &attrs) {
10430b57cec5SDimitry Andric   // Treat these like attributes, even though they're type specifiers.
10440b57cec5SDimitry Andric   while (true) {
104506c3fb27SDimitry Andric     auto Kind = Tok.getKind();
104606c3fb27SDimitry Andric     switch (Kind) {
10470b57cec5SDimitry Andric     case tok::kw__Nonnull:
10480b57cec5SDimitry Andric     case tok::kw__Nullable:
1049e8d8bef9SDimitry Andric     case tok::kw__Nullable_result:
10500b57cec5SDimitry Andric     case tok::kw__Null_unspecified: {
10510b57cec5SDimitry Andric       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
10520b57cec5SDimitry Andric       SourceLocation AttrNameLoc = ConsumeToken();
10530b57cec5SDimitry Andric       if (!getLangOpts().ObjC)
10540b57cec5SDimitry Andric         Diag(AttrNameLoc, diag::ext_nullability)
10550b57cec5SDimitry Andric           << AttrName;
10560b57cec5SDimitry Andric       attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
105706c3fb27SDimitry Andric                    Kind);
10580b57cec5SDimitry Andric       break;
10590b57cec5SDimitry Andric     }
10600b57cec5SDimitry Andric     default:
10610b57cec5SDimitry Andric       return;
10620b57cec5SDimitry Andric     }
10630b57cec5SDimitry Andric   }
10640b57cec5SDimitry Andric }
10650b57cec5SDimitry Andric 
10660b57cec5SDimitry Andric static bool VersionNumberSeparator(const char Separator) {
10670b57cec5SDimitry Andric   return (Separator == '.' || Separator == '_');
10680b57cec5SDimitry Andric }
10690b57cec5SDimitry Andric 
10700b57cec5SDimitry Andric /// Parse a version number.
10710b57cec5SDimitry Andric ///
10720b57cec5SDimitry Andric /// version:
10730b57cec5SDimitry Andric ///   simple-integer
10740b57cec5SDimitry Andric ///   simple-integer '.' simple-integer
10750b57cec5SDimitry Andric ///   simple-integer '_' simple-integer
10760b57cec5SDimitry Andric ///   simple-integer '.' simple-integer '.' simple-integer
10770b57cec5SDimitry Andric ///   simple-integer '_' simple-integer '_' simple-integer
10780b57cec5SDimitry Andric VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
10790b57cec5SDimitry Andric   Range = SourceRange(Tok.getLocation(), Tok.getEndLoc());
10800b57cec5SDimitry Andric 
10810b57cec5SDimitry Andric   if (!Tok.is(tok::numeric_constant)) {
10820b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
10830b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
10840b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
10850b57cec5SDimitry Andric     return VersionTuple();
10860b57cec5SDimitry Andric   }
10870b57cec5SDimitry Andric 
10880b57cec5SDimitry Andric   // Parse the major (and possibly minor and subminor) versions, which
10890b57cec5SDimitry Andric   // are stored in the numeric constant. We utilize a quirk of the
10900b57cec5SDimitry Andric   // lexer, which is that it handles something like 1.2.3 as a single
10910b57cec5SDimitry Andric   // numeric constant, rather than two separate tokens.
10920b57cec5SDimitry Andric   SmallString<512> Buffer;
10930b57cec5SDimitry Andric   Buffer.resize(Tok.getLength()+1);
10940b57cec5SDimitry Andric   const char *ThisTokBegin = &Buffer[0];
10950b57cec5SDimitry Andric 
10960b57cec5SDimitry Andric   // Get the spelling of the token, which eliminates trigraphs, etc.
10970b57cec5SDimitry Andric   bool Invalid = false;
10980b57cec5SDimitry Andric   unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
10990b57cec5SDimitry Andric   if (Invalid)
11000b57cec5SDimitry Andric     return VersionTuple();
11010b57cec5SDimitry Andric 
11020b57cec5SDimitry Andric   // Parse the major version.
11030b57cec5SDimitry Andric   unsigned AfterMajor = 0;
11040b57cec5SDimitry Andric   unsigned Major = 0;
11050b57cec5SDimitry Andric   while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
11060b57cec5SDimitry Andric     Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
11070b57cec5SDimitry Andric     ++AfterMajor;
11080b57cec5SDimitry Andric   }
11090b57cec5SDimitry Andric 
11100b57cec5SDimitry Andric   if (AfterMajor == 0) {
11110b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
11120b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
11130b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
11140b57cec5SDimitry Andric     return VersionTuple();
11150b57cec5SDimitry Andric   }
11160b57cec5SDimitry Andric 
11170b57cec5SDimitry Andric   if (AfterMajor == ActualLength) {
11180b57cec5SDimitry Andric     ConsumeToken();
11190b57cec5SDimitry Andric 
11200b57cec5SDimitry Andric     // We only had a single version component.
11210b57cec5SDimitry Andric     if (Major == 0) {
11220b57cec5SDimitry Andric       Diag(Tok, diag::err_zero_version);
11230b57cec5SDimitry Andric       return VersionTuple();
11240b57cec5SDimitry Andric     }
11250b57cec5SDimitry Andric 
11260b57cec5SDimitry Andric     return VersionTuple(Major);
11270b57cec5SDimitry Andric   }
11280b57cec5SDimitry Andric 
11290b57cec5SDimitry Andric   const char AfterMajorSeparator = ThisTokBegin[AfterMajor];
11300b57cec5SDimitry Andric   if (!VersionNumberSeparator(AfterMajorSeparator)
11310b57cec5SDimitry Andric       || (AfterMajor + 1 == ActualLength)) {
11320b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
11330b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
11340b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
11350b57cec5SDimitry Andric     return VersionTuple();
11360b57cec5SDimitry Andric   }
11370b57cec5SDimitry Andric 
11380b57cec5SDimitry Andric   // Parse the minor version.
11390b57cec5SDimitry Andric   unsigned AfterMinor = AfterMajor + 1;
11400b57cec5SDimitry Andric   unsigned Minor = 0;
11410b57cec5SDimitry Andric   while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
11420b57cec5SDimitry Andric     Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
11430b57cec5SDimitry Andric     ++AfterMinor;
11440b57cec5SDimitry Andric   }
11450b57cec5SDimitry Andric 
11460b57cec5SDimitry Andric   if (AfterMinor == ActualLength) {
11470b57cec5SDimitry Andric     ConsumeToken();
11480b57cec5SDimitry Andric 
11490b57cec5SDimitry Andric     // We had major.minor.
11500b57cec5SDimitry Andric     if (Major == 0 && Minor == 0) {
11510b57cec5SDimitry Andric       Diag(Tok, diag::err_zero_version);
11520b57cec5SDimitry Andric       return VersionTuple();
11530b57cec5SDimitry Andric     }
11540b57cec5SDimitry Andric 
11550b57cec5SDimitry Andric     return VersionTuple(Major, Minor);
11560b57cec5SDimitry Andric   }
11570b57cec5SDimitry Andric 
11580b57cec5SDimitry Andric   const char AfterMinorSeparator = ThisTokBegin[AfterMinor];
11590b57cec5SDimitry Andric   // If what follows is not a '.' or '_', we have a problem.
11600b57cec5SDimitry Andric   if (!VersionNumberSeparator(AfterMinorSeparator)) {
11610b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
11620b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
11630b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
11640b57cec5SDimitry Andric     return VersionTuple();
11650b57cec5SDimitry Andric   }
11660b57cec5SDimitry Andric 
11670b57cec5SDimitry Andric   // Warn if separators, be it '.' or '_', do not match.
11680b57cec5SDimitry Andric   if (AfterMajorSeparator != AfterMinorSeparator)
11690b57cec5SDimitry Andric     Diag(Tok, diag::warn_expected_consistent_version_separator);
11700b57cec5SDimitry Andric 
11710b57cec5SDimitry Andric   // Parse the subminor version.
11720b57cec5SDimitry Andric   unsigned AfterSubminor = AfterMinor + 1;
11730b57cec5SDimitry Andric   unsigned Subminor = 0;
11740b57cec5SDimitry Andric   while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
11750b57cec5SDimitry Andric     Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
11760b57cec5SDimitry Andric     ++AfterSubminor;
11770b57cec5SDimitry Andric   }
11780b57cec5SDimitry Andric 
11790b57cec5SDimitry Andric   if (AfterSubminor != ActualLength) {
11800b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
11810b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
11820b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
11830b57cec5SDimitry Andric     return VersionTuple();
11840b57cec5SDimitry Andric   }
11850b57cec5SDimitry Andric   ConsumeToken();
11860b57cec5SDimitry Andric   return VersionTuple(Major, Minor, Subminor);
11870b57cec5SDimitry Andric }
11880b57cec5SDimitry Andric 
11890b57cec5SDimitry Andric /// Parse the contents of the "availability" attribute.
11900b57cec5SDimitry Andric ///
11910b57cec5SDimitry Andric /// availability-attribute:
11920b57cec5SDimitry Andric ///   'availability' '(' platform ',' opt-strict version-arg-list,
11930b57cec5SDimitry Andric ///                      opt-replacement, opt-message')'
11940b57cec5SDimitry Andric ///
11950b57cec5SDimitry Andric /// platform:
11960b57cec5SDimitry Andric ///   identifier
11970b57cec5SDimitry Andric ///
11980b57cec5SDimitry Andric /// opt-strict:
11990b57cec5SDimitry Andric ///   'strict' ','
12000b57cec5SDimitry Andric ///
12010b57cec5SDimitry Andric /// version-arg-list:
12020b57cec5SDimitry Andric ///   version-arg
12030b57cec5SDimitry Andric ///   version-arg ',' version-arg-list
12040b57cec5SDimitry Andric ///
12050b57cec5SDimitry Andric /// version-arg:
12060b57cec5SDimitry Andric ///   'introduced' '=' version
12070b57cec5SDimitry Andric ///   'deprecated' '=' version
12080b57cec5SDimitry Andric ///   'obsoleted' = version
12090b57cec5SDimitry Andric ///   'unavailable'
12100b57cec5SDimitry Andric /// opt-replacement:
12110b57cec5SDimitry Andric ///   'replacement' '=' <string>
12120b57cec5SDimitry Andric /// opt-message:
12130b57cec5SDimitry Andric ///   'message' '=' <string>
121406c3fb27SDimitry Andric void Parser::ParseAvailabilityAttribute(
121506c3fb27SDimitry Andric     IdentifierInfo &Availability, SourceLocation AvailabilityLoc,
121606c3fb27SDimitry Andric     ParsedAttributes &attrs, SourceLocation *endLoc, IdentifierInfo *ScopeName,
121706c3fb27SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Form Form) {
12180b57cec5SDimitry Andric   enum { Introduced, Deprecated, Obsoleted, Unknown };
12190b57cec5SDimitry Andric   AvailabilityChange Changes[Unknown];
12200b57cec5SDimitry Andric   ExprResult MessageExpr, ReplacementExpr;
12210b57cec5SDimitry Andric 
12220b57cec5SDimitry Andric   // Opening '('.
12230b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
12240b57cec5SDimitry Andric   if (T.consumeOpen()) {
12250b57cec5SDimitry Andric     Diag(Tok, diag::err_expected) << tok::l_paren;
12260b57cec5SDimitry Andric     return;
12270b57cec5SDimitry Andric   }
12280b57cec5SDimitry Andric 
12290b57cec5SDimitry Andric   // Parse the platform name.
12300b57cec5SDimitry Andric   if (Tok.isNot(tok::identifier)) {
12310b57cec5SDimitry Andric     Diag(Tok, diag::err_availability_expected_platform);
12320b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
12330b57cec5SDimitry Andric     return;
12340b57cec5SDimitry Andric   }
12350b57cec5SDimitry Andric   IdentifierLoc *Platform = ParseIdentifierLoc();
12360b57cec5SDimitry Andric   if (const IdentifierInfo *const Ident = Platform->Ident) {
12370b57cec5SDimitry Andric     // Canonicalize platform name from "macosx" to "macos".
12380b57cec5SDimitry Andric     if (Ident->getName() == "macosx")
12390b57cec5SDimitry Andric       Platform->Ident = PP.getIdentifierInfo("macos");
12400b57cec5SDimitry Andric     // Canonicalize platform name from "macosx_app_extension" to
12410b57cec5SDimitry Andric     // "macos_app_extension".
12420b57cec5SDimitry Andric     else if (Ident->getName() == "macosx_app_extension")
12430b57cec5SDimitry Andric       Platform->Ident = PP.getIdentifierInfo("macos_app_extension");
12440b57cec5SDimitry Andric     else
12450b57cec5SDimitry Andric       Platform->Ident = PP.getIdentifierInfo(
12460b57cec5SDimitry Andric           AvailabilityAttr::canonicalizePlatformName(Ident->getName()));
12470b57cec5SDimitry Andric   }
12480b57cec5SDimitry Andric 
12490b57cec5SDimitry Andric   // Parse the ',' following the platform name.
12500b57cec5SDimitry Andric   if (ExpectAndConsume(tok::comma)) {
12510b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
12520b57cec5SDimitry Andric     return;
12530b57cec5SDimitry Andric   }
12540b57cec5SDimitry Andric 
12550b57cec5SDimitry Andric   // If we haven't grabbed the pointers for the identifiers
12560b57cec5SDimitry Andric   // "introduced", "deprecated", and "obsoleted", do so now.
12570b57cec5SDimitry Andric   if (!Ident_introduced) {
12580b57cec5SDimitry Andric     Ident_introduced = PP.getIdentifierInfo("introduced");
12590b57cec5SDimitry Andric     Ident_deprecated = PP.getIdentifierInfo("deprecated");
12600b57cec5SDimitry Andric     Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
12610b57cec5SDimitry Andric     Ident_unavailable = PP.getIdentifierInfo("unavailable");
12620b57cec5SDimitry Andric     Ident_message = PP.getIdentifierInfo("message");
12630b57cec5SDimitry Andric     Ident_strict = PP.getIdentifierInfo("strict");
12640b57cec5SDimitry Andric     Ident_replacement = PP.getIdentifierInfo("replacement");
12650b57cec5SDimitry Andric   }
12660b57cec5SDimitry Andric 
12670b57cec5SDimitry Andric   // Parse the optional "strict", the optional "replacement" and the set of
12680b57cec5SDimitry Andric   // introductions/deprecations/removals.
12690b57cec5SDimitry Andric   SourceLocation UnavailableLoc, StrictLoc;
12700b57cec5SDimitry Andric   do {
12710b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
12720b57cec5SDimitry Andric       Diag(Tok, diag::err_availability_expected_change);
12730b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
12740b57cec5SDimitry Andric       return;
12750b57cec5SDimitry Andric     }
12760b57cec5SDimitry Andric     IdentifierInfo *Keyword = Tok.getIdentifierInfo();
12770b57cec5SDimitry Andric     SourceLocation KeywordLoc = ConsumeToken();
12780b57cec5SDimitry Andric 
12790b57cec5SDimitry Andric     if (Keyword == Ident_strict) {
12800b57cec5SDimitry Andric       if (StrictLoc.isValid()) {
12810b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_availability_redundant)
12820b57cec5SDimitry Andric           << Keyword << SourceRange(StrictLoc);
12830b57cec5SDimitry Andric       }
12840b57cec5SDimitry Andric       StrictLoc = KeywordLoc;
12850b57cec5SDimitry Andric       continue;
12860b57cec5SDimitry Andric     }
12870b57cec5SDimitry Andric 
12880b57cec5SDimitry Andric     if (Keyword == Ident_unavailable) {
12890b57cec5SDimitry Andric       if (UnavailableLoc.isValid()) {
12900b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_availability_redundant)
12910b57cec5SDimitry Andric           << Keyword << SourceRange(UnavailableLoc);
12920b57cec5SDimitry Andric       }
12930b57cec5SDimitry Andric       UnavailableLoc = KeywordLoc;
12940b57cec5SDimitry Andric       continue;
12950b57cec5SDimitry Andric     }
12960b57cec5SDimitry Andric 
12970b57cec5SDimitry Andric     if (Keyword == Ident_deprecated && Platform->Ident &&
12980b57cec5SDimitry Andric         Platform->Ident->isStr("swift")) {
12990b57cec5SDimitry Andric       // For swift, we deprecate for all versions.
13000b57cec5SDimitry Andric       if (Changes[Deprecated].KeywordLoc.isValid()) {
13010b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_availability_redundant)
13020b57cec5SDimitry Andric           << Keyword
13030b57cec5SDimitry Andric           << SourceRange(Changes[Deprecated].KeywordLoc);
13040b57cec5SDimitry Andric       }
13050b57cec5SDimitry Andric 
13060b57cec5SDimitry Andric       Changes[Deprecated].KeywordLoc = KeywordLoc;
13070b57cec5SDimitry Andric       // Use a fake version here.
13080b57cec5SDimitry Andric       Changes[Deprecated].Version = VersionTuple(1);
13090b57cec5SDimitry Andric       continue;
13100b57cec5SDimitry Andric     }
13110b57cec5SDimitry Andric 
13120b57cec5SDimitry Andric     if (Tok.isNot(tok::equal)) {
13130b57cec5SDimitry Andric       Diag(Tok, diag::err_expected_after) << Keyword << tok::equal;
13140b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
13150b57cec5SDimitry Andric       return;
13160b57cec5SDimitry Andric     }
13170b57cec5SDimitry Andric     ConsumeToken();
13180b57cec5SDimitry Andric     if (Keyword == Ident_message || Keyword == Ident_replacement) {
13195f757f3fSDimitry Andric       if (!isTokenStringLiteral()) {
13200b57cec5SDimitry Andric         Diag(Tok, diag::err_expected_string_literal)
13210b57cec5SDimitry Andric           << /*Source='availability attribute'*/2;
13220b57cec5SDimitry Andric         SkipUntil(tok::r_paren, StopAtSemi);
13230b57cec5SDimitry Andric         return;
13240b57cec5SDimitry Andric       }
13255f757f3fSDimitry Andric       if (Keyword == Ident_message) {
13265f757f3fSDimitry Andric         MessageExpr = ParseUnevaluatedStringLiteralExpression();
13270b57cec5SDimitry Andric         break;
13285f757f3fSDimitry Andric       } else {
13295f757f3fSDimitry Andric         ReplacementExpr = ParseUnevaluatedStringLiteralExpression();
13300b57cec5SDimitry Andric         continue;
13310b57cec5SDimitry Andric       }
13325f757f3fSDimitry Andric     }
13330b57cec5SDimitry Andric 
13340b57cec5SDimitry Andric     // Special handling of 'NA' only when applied to introduced or
13350b57cec5SDimitry Andric     // deprecated.
13360b57cec5SDimitry Andric     if ((Keyword == Ident_introduced || Keyword == Ident_deprecated) &&
13370b57cec5SDimitry Andric         Tok.is(tok::identifier)) {
13380b57cec5SDimitry Andric       IdentifierInfo *NA = Tok.getIdentifierInfo();
13390b57cec5SDimitry Andric       if (NA->getName() == "NA") {
13400b57cec5SDimitry Andric         ConsumeToken();
13410b57cec5SDimitry Andric         if (Keyword == Ident_introduced)
13420b57cec5SDimitry Andric           UnavailableLoc = KeywordLoc;
13430b57cec5SDimitry Andric         continue;
13440b57cec5SDimitry Andric       }
13450b57cec5SDimitry Andric     }
13460b57cec5SDimitry Andric 
13470b57cec5SDimitry Andric     SourceRange VersionRange;
13480b57cec5SDimitry Andric     VersionTuple Version = ParseVersionTuple(VersionRange);
13490b57cec5SDimitry Andric 
13500b57cec5SDimitry Andric     if (Version.empty()) {
13510b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
13520b57cec5SDimitry Andric       return;
13530b57cec5SDimitry Andric     }
13540b57cec5SDimitry Andric 
13550b57cec5SDimitry Andric     unsigned Index;
13560b57cec5SDimitry Andric     if (Keyword == Ident_introduced)
13570b57cec5SDimitry Andric       Index = Introduced;
13580b57cec5SDimitry Andric     else if (Keyword == Ident_deprecated)
13590b57cec5SDimitry Andric       Index = Deprecated;
13600b57cec5SDimitry Andric     else if (Keyword == Ident_obsoleted)
13610b57cec5SDimitry Andric       Index = Obsoleted;
13620b57cec5SDimitry Andric     else
13630b57cec5SDimitry Andric       Index = Unknown;
13640b57cec5SDimitry Andric 
13650b57cec5SDimitry Andric     if (Index < Unknown) {
13660b57cec5SDimitry Andric       if (!Changes[Index].KeywordLoc.isInvalid()) {
13670b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_availability_redundant)
13680b57cec5SDimitry Andric           << Keyword
13690b57cec5SDimitry Andric           << SourceRange(Changes[Index].KeywordLoc,
13700b57cec5SDimitry Andric                          Changes[Index].VersionRange.getEnd());
13710b57cec5SDimitry Andric       }
13720b57cec5SDimitry Andric 
13730b57cec5SDimitry Andric       Changes[Index].KeywordLoc = KeywordLoc;
13740b57cec5SDimitry Andric       Changes[Index].Version = Version;
13750b57cec5SDimitry Andric       Changes[Index].VersionRange = VersionRange;
13760b57cec5SDimitry Andric     } else {
13770b57cec5SDimitry Andric       Diag(KeywordLoc, diag::err_availability_unknown_change)
13780b57cec5SDimitry Andric         << Keyword << VersionRange;
13790b57cec5SDimitry Andric     }
13800b57cec5SDimitry Andric 
13810b57cec5SDimitry Andric   } while (TryConsumeToken(tok::comma));
13820b57cec5SDimitry Andric 
13830b57cec5SDimitry Andric   // Closing ')'.
13840b57cec5SDimitry Andric   if (T.consumeClose())
13850b57cec5SDimitry Andric     return;
13860b57cec5SDimitry Andric 
13870b57cec5SDimitry Andric   if (endLoc)
13880b57cec5SDimitry Andric     *endLoc = T.getCloseLocation();
13890b57cec5SDimitry Andric 
13900b57cec5SDimitry Andric   // The 'unavailable' availability cannot be combined with any other
13910b57cec5SDimitry Andric   // availability changes. Make sure that hasn't happened.
13920b57cec5SDimitry Andric   if (UnavailableLoc.isValid()) {
13930b57cec5SDimitry Andric     bool Complained = false;
13940b57cec5SDimitry Andric     for (unsigned Index = Introduced; Index != Unknown; ++Index) {
13950b57cec5SDimitry Andric       if (Changes[Index].KeywordLoc.isValid()) {
13960b57cec5SDimitry Andric         if (!Complained) {
13970b57cec5SDimitry Andric           Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
13980b57cec5SDimitry Andric             << SourceRange(Changes[Index].KeywordLoc,
13990b57cec5SDimitry Andric                            Changes[Index].VersionRange.getEnd());
14000b57cec5SDimitry Andric           Complained = true;
14010b57cec5SDimitry Andric         }
14020b57cec5SDimitry Andric 
14030b57cec5SDimitry Andric         // Clear out the availability.
14040b57cec5SDimitry Andric         Changes[Index] = AvailabilityChange();
14050b57cec5SDimitry Andric       }
14060b57cec5SDimitry Andric     }
14070b57cec5SDimitry Andric   }
14080b57cec5SDimitry Andric 
14090b57cec5SDimitry Andric   // Record this attribute
14100b57cec5SDimitry Andric   attrs.addNew(&Availability,
141106c3fb27SDimitry Andric                SourceRange(AvailabilityLoc, T.getCloseLocation()), ScopeName,
141206c3fb27SDimitry Andric                ScopeLoc, Platform, Changes[Introduced], Changes[Deprecated],
141306c3fb27SDimitry Andric                Changes[Obsoleted], UnavailableLoc, MessageExpr.get(), Form,
141406c3fb27SDimitry Andric                StrictLoc, ReplacementExpr.get());
14150b57cec5SDimitry Andric }
14160b57cec5SDimitry Andric 
14170b57cec5SDimitry Andric /// Parse the contents of the "external_source_symbol" attribute.
14180b57cec5SDimitry Andric ///
14190b57cec5SDimitry Andric /// external-source-symbol-attribute:
14200b57cec5SDimitry Andric ///   'external_source_symbol' '(' keyword-arg-list ')'
14210b57cec5SDimitry Andric ///
14220b57cec5SDimitry Andric /// keyword-arg-list:
14230b57cec5SDimitry Andric ///   keyword-arg
14240b57cec5SDimitry Andric ///   keyword-arg ',' keyword-arg-list
14250b57cec5SDimitry Andric ///
14260b57cec5SDimitry Andric /// keyword-arg:
14270b57cec5SDimitry Andric ///   'language' '=' <string>
14280b57cec5SDimitry Andric ///   'defined_in' '=' <string>
142906c3fb27SDimitry Andric ///   'USR' '=' <string>
14300b57cec5SDimitry Andric ///   'generated_declaration'
14310b57cec5SDimitry Andric void Parser::ParseExternalSourceSymbolAttribute(
14320b57cec5SDimitry Andric     IdentifierInfo &ExternalSourceSymbol, SourceLocation Loc,
14330b57cec5SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
143406c3fb27SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Form Form) {
14350b57cec5SDimitry Andric   // Opening '('.
14360b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
14370b57cec5SDimitry Andric   if (T.expectAndConsume())
14380b57cec5SDimitry Andric     return;
14390b57cec5SDimitry Andric 
14400b57cec5SDimitry Andric   // Initialize the pointers for the keyword identifiers when required.
14410b57cec5SDimitry Andric   if (!Ident_language) {
14420b57cec5SDimitry Andric     Ident_language = PP.getIdentifierInfo("language");
14430b57cec5SDimitry Andric     Ident_defined_in = PP.getIdentifierInfo("defined_in");
14440b57cec5SDimitry Andric     Ident_generated_declaration = PP.getIdentifierInfo("generated_declaration");
144506c3fb27SDimitry Andric     Ident_USR = PP.getIdentifierInfo("USR");
14460b57cec5SDimitry Andric   }
14470b57cec5SDimitry Andric 
14480b57cec5SDimitry Andric   ExprResult Language;
14490b57cec5SDimitry Andric   bool HasLanguage = false;
14500b57cec5SDimitry Andric   ExprResult DefinedInExpr;
14510b57cec5SDimitry Andric   bool HasDefinedIn = false;
14520b57cec5SDimitry Andric   IdentifierLoc *GeneratedDeclaration = nullptr;
145306c3fb27SDimitry Andric   ExprResult USR;
145406c3fb27SDimitry Andric   bool HasUSR = false;
14550b57cec5SDimitry Andric 
14560b57cec5SDimitry Andric   // Parse the language/defined_in/generated_declaration keywords
14570b57cec5SDimitry Andric   do {
14580b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
14590b57cec5SDimitry Andric       Diag(Tok, diag::err_external_source_symbol_expected_keyword);
14600b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
14610b57cec5SDimitry Andric       return;
14620b57cec5SDimitry Andric     }
14630b57cec5SDimitry Andric 
14640b57cec5SDimitry Andric     SourceLocation KeywordLoc = Tok.getLocation();
14650b57cec5SDimitry Andric     IdentifierInfo *Keyword = Tok.getIdentifierInfo();
14660b57cec5SDimitry Andric     if (Keyword == Ident_generated_declaration) {
14670b57cec5SDimitry Andric       if (GeneratedDeclaration) {
14680b57cec5SDimitry Andric         Diag(Tok, diag::err_external_source_symbol_duplicate_clause) << Keyword;
14690b57cec5SDimitry Andric         SkipUntil(tok::r_paren, StopAtSemi);
14700b57cec5SDimitry Andric         return;
14710b57cec5SDimitry Andric       }
14720b57cec5SDimitry Andric       GeneratedDeclaration = ParseIdentifierLoc();
14730b57cec5SDimitry Andric       continue;
14740b57cec5SDimitry Andric     }
14750b57cec5SDimitry Andric 
147606c3fb27SDimitry Andric     if (Keyword != Ident_language && Keyword != Ident_defined_in &&
147706c3fb27SDimitry Andric         Keyword != Ident_USR) {
14780b57cec5SDimitry Andric       Diag(Tok, diag::err_external_source_symbol_expected_keyword);
14790b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
14800b57cec5SDimitry Andric       return;
14810b57cec5SDimitry Andric     }
14820b57cec5SDimitry Andric 
14830b57cec5SDimitry Andric     ConsumeToken();
14840b57cec5SDimitry Andric     if (ExpectAndConsume(tok::equal, diag::err_expected_after,
14850b57cec5SDimitry Andric                          Keyword->getName())) {
14860b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
14870b57cec5SDimitry Andric       return;
14880b57cec5SDimitry Andric     }
14890b57cec5SDimitry Andric 
149006c3fb27SDimitry Andric     bool HadLanguage = HasLanguage, HadDefinedIn = HasDefinedIn,
149106c3fb27SDimitry Andric          HadUSR = HasUSR;
14920b57cec5SDimitry Andric     if (Keyword == Ident_language)
14930b57cec5SDimitry Andric       HasLanguage = true;
149406c3fb27SDimitry Andric     else if (Keyword == Ident_USR)
149506c3fb27SDimitry Andric       HasUSR = true;
14960b57cec5SDimitry Andric     else
14970b57cec5SDimitry Andric       HasDefinedIn = true;
14980b57cec5SDimitry Andric 
14995f757f3fSDimitry Andric     if (!isTokenStringLiteral()) {
15000b57cec5SDimitry Andric       Diag(Tok, diag::err_expected_string_literal)
15010b57cec5SDimitry Andric           << /*Source='external_source_symbol attribute'*/ 3
150206c3fb27SDimitry Andric           << /*language | source container | USR*/ (
150306c3fb27SDimitry Andric                  Keyword == Ident_language
150406c3fb27SDimitry Andric                      ? 0
150506c3fb27SDimitry Andric                      : (Keyword == Ident_defined_in ? 1 : 2));
15060b57cec5SDimitry Andric       SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
15070b57cec5SDimitry Andric       continue;
15080b57cec5SDimitry Andric     }
15090b57cec5SDimitry Andric     if (Keyword == Ident_language) {
15100b57cec5SDimitry Andric       if (HadLanguage) {
15110b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_external_source_symbol_duplicate_clause)
15120b57cec5SDimitry Andric             << Keyword;
15135f757f3fSDimitry Andric         ParseUnevaluatedStringLiteralExpression();
15140b57cec5SDimitry Andric         continue;
15150b57cec5SDimitry Andric       }
15165f757f3fSDimitry Andric       Language = ParseUnevaluatedStringLiteralExpression();
151706c3fb27SDimitry Andric     } else if (Keyword == Ident_USR) {
151806c3fb27SDimitry Andric       if (HadUSR) {
151906c3fb27SDimitry Andric         Diag(KeywordLoc, diag::err_external_source_symbol_duplicate_clause)
152006c3fb27SDimitry Andric             << Keyword;
15215f757f3fSDimitry Andric         ParseUnevaluatedStringLiteralExpression();
152206c3fb27SDimitry Andric         continue;
152306c3fb27SDimitry Andric       }
15245f757f3fSDimitry Andric       USR = ParseUnevaluatedStringLiteralExpression();
15250b57cec5SDimitry Andric     } else {
15260b57cec5SDimitry Andric       assert(Keyword == Ident_defined_in && "Invalid clause keyword!");
15270b57cec5SDimitry Andric       if (HadDefinedIn) {
15280b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_external_source_symbol_duplicate_clause)
15290b57cec5SDimitry Andric             << Keyword;
15305f757f3fSDimitry Andric         ParseUnevaluatedStringLiteralExpression();
15310b57cec5SDimitry Andric         continue;
15320b57cec5SDimitry Andric       }
15335f757f3fSDimitry Andric       DefinedInExpr = ParseUnevaluatedStringLiteralExpression();
15340b57cec5SDimitry Andric     }
15350b57cec5SDimitry Andric   } while (TryConsumeToken(tok::comma));
15360b57cec5SDimitry Andric 
15370b57cec5SDimitry Andric   // Closing ')'.
15380b57cec5SDimitry Andric   if (T.consumeClose())
15390b57cec5SDimitry Andric     return;
15400b57cec5SDimitry Andric   if (EndLoc)
15410b57cec5SDimitry Andric     *EndLoc = T.getCloseLocation();
15420b57cec5SDimitry Andric 
154306c3fb27SDimitry Andric   ArgsUnion Args[] = {Language.get(), DefinedInExpr.get(), GeneratedDeclaration,
154406c3fb27SDimitry Andric                       USR.get()};
15450b57cec5SDimitry Andric   Attrs.addNew(&ExternalSourceSymbol, SourceRange(Loc, T.getCloseLocation()),
154606c3fb27SDimitry Andric                ScopeName, ScopeLoc, Args, std::size(Args), Form);
15470b57cec5SDimitry Andric }
15480b57cec5SDimitry Andric 
15490b57cec5SDimitry Andric /// Parse the contents of the "objc_bridge_related" attribute.
15500b57cec5SDimitry Andric /// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')'
15510b57cec5SDimitry Andric /// related_class:
15520b57cec5SDimitry Andric ///     Identifier
15530b57cec5SDimitry Andric ///
15540b57cec5SDimitry Andric /// opt-class_method:
15550b57cec5SDimitry Andric ///     Identifier: | <empty>
15560b57cec5SDimitry Andric ///
15570b57cec5SDimitry Andric /// opt-instance_method:
15580b57cec5SDimitry Andric ///     Identifier | <empty>
15590b57cec5SDimitry Andric ///
156081ad6265SDimitry Andric void Parser::ParseObjCBridgeRelatedAttribute(
156181ad6265SDimitry Andric     IdentifierInfo &ObjCBridgeRelated, SourceLocation ObjCBridgeRelatedLoc,
156281ad6265SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
156306c3fb27SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Form Form) {
15640b57cec5SDimitry Andric   // Opening '('.
15650b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
15660b57cec5SDimitry Andric   if (T.consumeOpen()) {
15670b57cec5SDimitry Andric     Diag(Tok, diag::err_expected) << tok::l_paren;
15680b57cec5SDimitry Andric     return;
15690b57cec5SDimitry Andric   }
15700b57cec5SDimitry Andric 
15710b57cec5SDimitry Andric   // Parse the related class name.
15720b57cec5SDimitry Andric   if (Tok.isNot(tok::identifier)) {
15730b57cec5SDimitry Andric     Diag(Tok, diag::err_objcbridge_related_expected_related_class);
15740b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
15750b57cec5SDimitry Andric     return;
15760b57cec5SDimitry Andric   }
15770b57cec5SDimitry Andric   IdentifierLoc *RelatedClass = ParseIdentifierLoc();
15780b57cec5SDimitry Andric   if (ExpectAndConsume(tok::comma)) {
15790b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
15800b57cec5SDimitry Andric     return;
15810b57cec5SDimitry Andric   }
15820b57cec5SDimitry Andric 
15830b57cec5SDimitry Andric   // Parse class method name.  It's non-optional in the sense that a trailing
15840b57cec5SDimitry Andric   // comma is required, but it can be the empty string, and then we record a
15850b57cec5SDimitry Andric   // nullptr.
15860b57cec5SDimitry Andric   IdentifierLoc *ClassMethod = nullptr;
15870b57cec5SDimitry Andric   if (Tok.is(tok::identifier)) {
15880b57cec5SDimitry Andric     ClassMethod = ParseIdentifierLoc();
15890b57cec5SDimitry Andric     if (!TryConsumeToken(tok::colon)) {
15900b57cec5SDimitry Andric       Diag(Tok, diag::err_objcbridge_related_selector_name);
15910b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
15920b57cec5SDimitry Andric       return;
15930b57cec5SDimitry Andric     }
15940b57cec5SDimitry Andric   }
15950b57cec5SDimitry Andric   if (!TryConsumeToken(tok::comma)) {
15960b57cec5SDimitry Andric     if (Tok.is(tok::colon))
15970b57cec5SDimitry Andric       Diag(Tok, diag::err_objcbridge_related_selector_name);
15980b57cec5SDimitry Andric     else
15990b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::comma;
16000b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
16010b57cec5SDimitry Andric     return;
16020b57cec5SDimitry Andric   }
16030b57cec5SDimitry Andric 
16040b57cec5SDimitry Andric   // Parse instance method name.  Also non-optional but empty string is
16050b57cec5SDimitry Andric   // permitted.
16060b57cec5SDimitry Andric   IdentifierLoc *InstanceMethod = nullptr;
16070b57cec5SDimitry Andric   if (Tok.is(tok::identifier))
16080b57cec5SDimitry Andric     InstanceMethod = ParseIdentifierLoc();
16090b57cec5SDimitry Andric   else if (Tok.isNot(tok::r_paren)) {
16100b57cec5SDimitry Andric     Diag(Tok, diag::err_expected) << tok::r_paren;
16110b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
16120b57cec5SDimitry Andric     return;
16130b57cec5SDimitry Andric   }
16140b57cec5SDimitry Andric 
16150b57cec5SDimitry Andric   // Closing ')'.
16160b57cec5SDimitry Andric   if (T.consumeClose())
16170b57cec5SDimitry Andric     return;
16180b57cec5SDimitry Andric 
161981ad6265SDimitry Andric   if (EndLoc)
162081ad6265SDimitry Andric     *EndLoc = T.getCloseLocation();
16210b57cec5SDimitry Andric 
16220b57cec5SDimitry Andric   // Record this attribute
162381ad6265SDimitry Andric   Attrs.addNew(&ObjCBridgeRelated,
16240b57cec5SDimitry Andric                SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()),
162581ad6265SDimitry Andric                ScopeName, ScopeLoc, RelatedClass, ClassMethod, InstanceMethod,
162606c3fb27SDimitry Andric                Form);
16270b57cec5SDimitry Andric }
16280b57cec5SDimitry Andric 
1629e8d8bef9SDimitry Andric void Parser::ParseSwiftNewTypeAttribute(
1630e8d8bef9SDimitry Andric     IdentifierInfo &AttrName, SourceLocation AttrNameLoc,
1631e8d8bef9SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
163206c3fb27SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Form Form) {
1633e8d8bef9SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
1634e8d8bef9SDimitry Andric 
1635e8d8bef9SDimitry Andric   // Opening '('
1636e8d8bef9SDimitry Andric   if (T.consumeOpen()) {
1637e8d8bef9SDimitry Andric     Diag(Tok, diag::err_expected) << tok::l_paren;
1638e8d8bef9SDimitry Andric     return;
1639e8d8bef9SDimitry Andric   }
1640e8d8bef9SDimitry Andric 
1641e8d8bef9SDimitry Andric   if (Tok.is(tok::r_paren)) {
1642e8d8bef9SDimitry Andric     Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1643e8d8bef9SDimitry Andric     T.consumeClose();
1644e8d8bef9SDimitry Andric     return;
1645e8d8bef9SDimitry Andric   }
1646e8d8bef9SDimitry Andric   if (Tok.isNot(tok::kw_struct) && Tok.isNot(tok::kw_enum)) {
1647e8d8bef9SDimitry Andric     Diag(Tok, diag::warn_attribute_type_not_supported)
1648e8d8bef9SDimitry Andric         << &AttrName << Tok.getIdentifierInfo();
1649e8d8bef9SDimitry Andric     if (!isTokenSpecial())
1650e8d8bef9SDimitry Andric       ConsumeToken();
1651e8d8bef9SDimitry Andric     T.consumeClose();
1652e8d8bef9SDimitry Andric     return;
1653e8d8bef9SDimitry Andric   }
1654e8d8bef9SDimitry Andric 
1655e8d8bef9SDimitry Andric   auto *SwiftType = IdentifierLoc::create(Actions.Context, Tok.getLocation(),
1656e8d8bef9SDimitry Andric                                           Tok.getIdentifierInfo());
1657e8d8bef9SDimitry Andric   ConsumeToken();
1658e8d8bef9SDimitry Andric 
1659e8d8bef9SDimitry Andric   // Closing ')'
1660e8d8bef9SDimitry Andric   if (T.consumeClose())
1661e8d8bef9SDimitry Andric     return;
1662e8d8bef9SDimitry Andric   if (EndLoc)
1663e8d8bef9SDimitry Andric     *EndLoc = T.getCloseLocation();
1664e8d8bef9SDimitry Andric 
1665e8d8bef9SDimitry Andric   ArgsUnion Args[] = {SwiftType};
1666e8d8bef9SDimitry Andric   Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, T.getCloseLocation()),
166706c3fb27SDimitry Andric                ScopeName, ScopeLoc, Args, std::size(Args), Form);
1668e8d8bef9SDimitry Andric }
1669e8d8bef9SDimitry Andric 
167006c3fb27SDimitry Andric void Parser::ParseTypeTagForDatatypeAttribute(
167106c3fb27SDimitry Andric     IdentifierInfo &AttrName, SourceLocation AttrNameLoc,
167206c3fb27SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
167306c3fb27SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Form Form) {
16740b57cec5SDimitry Andric   assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
16750b57cec5SDimitry Andric 
16760b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
16770b57cec5SDimitry Andric   T.consumeOpen();
16780b57cec5SDimitry Andric 
16790b57cec5SDimitry Andric   if (Tok.isNot(tok::identifier)) {
16800b57cec5SDimitry Andric     Diag(Tok, diag::err_expected) << tok::identifier;
16810b57cec5SDimitry Andric     T.skipToEnd();
16820b57cec5SDimitry Andric     return;
16830b57cec5SDimitry Andric   }
16840b57cec5SDimitry Andric   IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
16850b57cec5SDimitry Andric 
16860b57cec5SDimitry Andric   if (ExpectAndConsume(tok::comma)) {
16870b57cec5SDimitry Andric     T.skipToEnd();
16880b57cec5SDimitry Andric     return;
16890b57cec5SDimitry Andric   }
16900b57cec5SDimitry Andric 
16910b57cec5SDimitry Andric   SourceRange MatchingCTypeRange;
16920b57cec5SDimitry Andric   TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
16930b57cec5SDimitry Andric   if (MatchingCType.isInvalid()) {
16940b57cec5SDimitry Andric     T.skipToEnd();
16950b57cec5SDimitry Andric     return;
16960b57cec5SDimitry Andric   }
16970b57cec5SDimitry Andric 
16980b57cec5SDimitry Andric   bool LayoutCompatible = false;
16990b57cec5SDimitry Andric   bool MustBeNull = false;
17000b57cec5SDimitry Andric   while (TryConsumeToken(tok::comma)) {
17010b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
17020b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::identifier;
17030b57cec5SDimitry Andric       T.skipToEnd();
17040b57cec5SDimitry Andric       return;
17050b57cec5SDimitry Andric     }
17060b57cec5SDimitry Andric     IdentifierInfo *Flag = Tok.getIdentifierInfo();
17070b57cec5SDimitry Andric     if (Flag->isStr("layout_compatible"))
17080b57cec5SDimitry Andric       LayoutCompatible = true;
17090b57cec5SDimitry Andric     else if (Flag->isStr("must_be_null"))
17100b57cec5SDimitry Andric       MustBeNull = true;
17110b57cec5SDimitry Andric     else {
17120b57cec5SDimitry Andric       Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
17130b57cec5SDimitry Andric       T.skipToEnd();
17140b57cec5SDimitry Andric       return;
17150b57cec5SDimitry Andric     }
17160b57cec5SDimitry Andric     ConsumeToken(); // consume flag
17170b57cec5SDimitry Andric   }
17180b57cec5SDimitry Andric 
17190b57cec5SDimitry Andric   if (!T.consumeClose()) {
17200b57cec5SDimitry Andric     Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, ScopeName, ScopeLoc,
17210b57cec5SDimitry Andric                                    ArgumentKind, MatchingCType.get(),
172206c3fb27SDimitry Andric                                    LayoutCompatible, MustBeNull, Form);
17230b57cec5SDimitry Andric   }
17240b57cec5SDimitry Andric 
17250b57cec5SDimitry Andric   if (EndLoc)
17260b57cec5SDimitry Andric     *EndLoc = T.getCloseLocation();
17270b57cec5SDimitry Andric }
17280b57cec5SDimitry Andric 
17290b57cec5SDimitry Andric /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
17300b57cec5SDimitry Andric /// of a C++11 attribute-specifier in a location where an attribute is not
17310b57cec5SDimitry Andric /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
17320b57cec5SDimitry Andric /// situation.
17330b57cec5SDimitry Andric ///
17340b57cec5SDimitry Andric /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
17350b57cec5SDimitry Andric /// this doesn't appear to actually be an attribute-specifier, and the caller
17360b57cec5SDimitry Andric /// should try to parse it.
17370b57cec5SDimitry Andric bool Parser::DiagnoseProhibitedCXX11Attribute() {
17380b57cec5SDimitry Andric   assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
17390b57cec5SDimitry Andric 
17400b57cec5SDimitry Andric   switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
17410b57cec5SDimitry Andric   case CAK_NotAttributeSpecifier:
17420b57cec5SDimitry Andric     // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
17430b57cec5SDimitry Andric     return false;
17440b57cec5SDimitry Andric 
17450b57cec5SDimitry Andric   case CAK_InvalidAttributeSpecifier:
17460b57cec5SDimitry Andric     Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
17470b57cec5SDimitry Andric     return false;
17480b57cec5SDimitry Andric 
17490b57cec5SDimitry Andric   case CAK_AttributeSpecifier:
17500b57cec5SDimitry Andric     // Parse and discard the attributes.
17510b57cec5SDimitry Andric     SourceLocation BeginLoc = ConsumeBracket();
17520b57cec5SDimitry Andric     ConsumeBracket();
17530b57cec5SDimitry Andric     SkipUntil(tok::r_square);
17540b57cec5SDimitry Andric     assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
17550b57cec5SDimitry Andric     SourceLocation EndLoc = ConsumeBracket();
17560b57cec5SDimitry Andric     Diag(BeginLoc, diag::err_attributes_not_allowed)
17570b57cec5SDimitry Andric       << SourceRange(BeginLoc, EndLoc);
17580b57cec5SDimitry Andric     return true;
17590b57cec5SDimitry Andric   }
17600b57cec5SDimitry Andric   llvm_unreachable("All cases handled above.");
17610b57cec5SDimitry Andric }
17620b57cec5SDimitry Andric 
17630b57cec5SDimitry Andric /// We have found the opening square brackets of a C++11
17640b57cec5SDimitry Andric /// attribute-specifier in a location where an attribute is not permitted, but
17650b57cec5SDimitry Andric /// we know where the attributes ought to be written. Parse them anyway, and
17660b57cec5SDimitry Andric /// provide a fixit moving them to the right place.
176781ad6265SDimitry Andric void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributes &Attrs,
17680b57cec5SDimitry Andric                                              SourceLocation CorrectLocation) {
17690b57cec5SDimitry Andric   assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
177006c3fb27SDimitry Andric          Tok.is(tok::kw_alignas) || Tok.isRegularKeywordAttribute());
17710b57cec5SDimitry Andric 
17720b57cec5SDimitry Andric   // Consume the attributes.
177306c3fb27SDimitry Andric   auto Keyword =
177406c3fb27SDimitry Andric       Tok.isRegularKeywordAttribute() ? Tok.getIdentifierInfo() : nullptr;
17750b57cec5SDimitry Andric   SourceLocation Loc = Tok.getLocation();
17760b57cec5SDimitry Andric   ParseCXX11Attributes(Attrs);
17770b57cec5SDimitry Andric   CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
17780b57cec5SDimitry Andric   // FIXME: use err_attributes_misplaced
177906c3fb27SDimitry Andric   (Keyword ? Diag(Loc, diag::err_keyword_not_allowed) << Keyword
178006c3fb27SDimitry Andric            : Diag(Loc, diag::err_attributes_not_allowed))
17810b57cec5SDimitry Andric       << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
17820b57cec5SDimitry Andric       << FixItHint::CreateRemoval(AttrRange);
17830b57cec5SDimitry Andric }
17840b57cec5SDimitry Andric 
17850b57cec5SDimitry Andric void Parser::DiagnoseProhibitedAttributes(
178606c3fb27SDimitry Andric     const ParsedAttributesView &Attrs, const SourceLocation CorrectLocation) {
178706c3fb27SDimitry Andric   auto *FirstAttr = Attrs.empty() ? nullptr : &Attrs.front();
17880b57cec5SDimitry Andric   if (CorrectLocation.isValid()) {
178906c3fb27SDimitry Andric     CharSourceRange AttrRange(Attrs.Range, true);
179006c3fb27SDimitry Andric     (FirstAttr && FirstAttr->isRegularKeywordAttribute()
179106c3fb27SDimitry Andric          ? Diag(CorrectLocation, diag::err_keyword_misplaced) << FirstAttr
179206c3fb27SDimitry Andric          : Diag(CorrectLocation, diag::err_attributes_misplaced))
17930b57cec5SDimitry Andric         << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
17940b57cec5SDimitry Andric         << FixItHint::CreateRemoval(AttrRange);
179506c3fb27SDimitry Andric   } else {
179606c3fb27SDimitry Andric     const SourceRange &Range = Attrs.Range;
179706c3fb27SDimitry Andric     (FirstAttr && FirstAttr->isRegularKeywordAttribute()
179806c3fb27SDimitry Andric          ? Diag(Range.getBegin(), diag::err_keyword_not_allowed) << FirstAttr
179906c3fb27SDimitry Andric          : Diag(Range.getBegin(), diag::err_attributes_not_allowed))
180006c3fb27SDimitry Andric         << Range;
180106c3fb27SDimitry Andric   }
18020b57cec5SDimitry Andric }
18030b57cec5SDimitry Andric 
180406c3fb27SDimitry Andric void Parser::ProhibitCXX11Attributes(ParsedAttributes &Attrs,
180506c3fb27SDimitry Andric                                      unsigned AttrDiagID,
180606c3fb27SDimitry Andric                                      unsigned KeywordDiagID,
180781ad6265SDimitry Andric                                      bool DiagnoseEmptyAttrs,
180881ad6265SDimitry Andric                                      bool WarnOnUnknownAttrs) {
1809fe6060f1SDimitry Andric 
1810fe6060f1SDimitry Andric   if (DiagnoseEmptyAttrs && Attrs.empty() && Attrs.Range.isValid()) {
1811fe6060f1SDimitry Andric     // An attribute list has been parsed, but it was empty.
1812fe6060f1SDimitry Andric     // This is the case for [[]].
1813fe6060f1SDimitry Andric     const auto &LangOpts = getLangOpts();
1814fe6060f1SDimitry Andric     auto &SM = PP.getSourceManager();
1815fe6060f1SDimitry Andric     Token FirstLSquare;
1816fe6060f1SDimitry Andric     Lexer::getRawToken(Attrs.Range.getBegin(), FirstLSquare, SM, LangOpts);
1817fe6060f1SDimitry Andric 
1818fe6060f1SDimitry Andric     if (FirstLSquare.is(tok::l_square)) {
1819bdd1243dSDimitry Andric       std::optional<Token> SecondLSquare =
1820fe6060f1SDimitry Andric           Lexer::findNextToken(FirstLSquare.getLocation(), SM, LangOpts);
1821fe6060f1SDimitry Andric 
1822fe6060f1SDimitry Andric       if (SecondLSquare && SecondLSquare->is(tok::l_square)) {
1823fe6060f1SDimitry Andric         // The attribute range starts with [[, but is empty. So this must
1824fe6060f1SDimitry Andric         // be [[]], which we are supposed to diagnose because
1825fe6060f1SDimitry Andric         // DiagnoseEmptyAttrs is true.
182606c3fb27SDimitry Andric         Diag(Attrs.Range.getBegin(), AttrDiagID) << Attrs.Range;
1827fe6060f1SDimitry Andric         return;
1828fe6060f1SDimitry Andric       }
1829fe6060f1SDimitry Andric     }
1830fe6060f1SDimitry Andric   }
1831fe6060f1SDimitry Andric 
18320b57cec5SDimitry Andric   for (const ParsedAttr &AL : Attrs) {
183306c3fb27SDimitry Andric     if (AL.isRegularKeywordAttribute()) {
183406c3fb27SDimitry Andric       Diag(AL.getLoc(), KeywordDiagID) << AL;
183506c3fb27SDimitry Andric       AL.setInvalid();
183606c3fb27SDimitry Andric       continue;
183706c3fb27SDimitry Andric     }
18385f757f3fSDimitry Andric     if (!AL.isStandardAttributeSyntax())
18390b57cec5SDimitry Andric       continue;
184081ad6265SDimitry Andric     if (AL.getKind() == ParsedAttr::UnknownAttribute) {
184181ad6265SDimitry Andric       if (WarnOnUnknownAttrs)
1842e8d8bef9SDimitry Andric         Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
1843e8d8bef9SDimitry Andric             << AL << AL.getRange();
184481ad6265SDimitry Andric     } else {
184506c3fb27SDimitry Andric       Diag(AL.getLoc(), AttrDiagID) << AL;
18460b57cec5SDimitry Andric       AL.setInvalid();
18470b57cec5SDimitry Andric     }
18480b57cec5SDimitry Andric   }
18490b57cec5SDimitry Andric }
18500b57cec5SDimitry Andric 
185181ad6265SDimitry Andric void Parser::DiagnoseCXX11AttributeExtension(ParsedAttributes &Attrs) {
1852fe6060f1SDimitry Andric   for (const ParsedAttr &PA : Attrs) {
18535f757f3fSDimitry Andric     if (PA.isStandardAttributeSyntax() || PA.isRegularKeywordAttribute())
185406c3fb27SDimitry Andric       Diag(PA.getLoc(), diag::ext_cxx11_attr_placement)
185506c3fb27SDimitry Andric           << PA << PA.isRegularKeywordAttribute() << PA.getRange();
1856fe6060f1SDimitry Andric   }
1857fe6060f1SDimitry Andric }
1858fe6060f1SDimitry Andric 
18590b57cec5SDimitry Andric // Usually, `__attribute__((attrib)) class Foo {} var` means that attribute
18600b57cec5SDimitry Andric // applies to var, not the type Foo.
18610b57cec5SDimitry Andric // As an exception to the rule, __declspec(align(...)) before the
18620b57cec5SDimitry Andric // class-key affects the type instead of the variable.
18630b57cec5SDimitry Andric // Also, Microsoft-style [attributes] seem to affect the type instead of the
18640b57cec5SDimitry Andric // variable.
18650b57cec5SDimitry Andric // This function moves attributes that should apply to the type off DS to Attrs.
186681ad6265SDimitry Andric void Parser::stripTypeAttributesOffDeclSpec(ParsedAttributes &Attrs,
18670b57cec5SDimitry Andric                                             DeclSpec &DS,
18680b57cec5SDimitry Andric                                             Sema::TagUseKind TUK) {
18690b57cec5SDimitry Andric   if (TUK == Sema::TUK_Reference)
18700b57cec5SDimitry Andric     return;
18710b57cec5SDimitry Andric 
18720b57cec5SDimitry Andric   llvm::SmallVector<ParsedAttr *, 1> ToBeMoved;
18730b57cec5SDimitry Andric 
18740b57cec5SDimitry Andric   for (ParsedAttr &AL : DS.getAttributes()) {
18750b57cec5SDimitry Andric     if ((AL.getKind() == ParsedAttr::AT_Aligned &&
18760b57cec5SDimitry Andric          AL.isDeclspecAttribute()) ||
18770b57cec5SDimitry Andric         AL.isMicrosoftAttribute())
18780b57cec5SDimitry Andric       ToBeMoved.push_back(&AL);
18790b57cec5SDimitry Andric   }
18800b57cec5SDimitry Andric 
18810b57cec5SDimitry Andric   for (ParsedAttr *AL : ToBeMoved) {
18820b57cec5SDimitry Andric     DS.getAttributes().remove(AL);
18830b57cec5SDimitry Andric     Attrs.addAtEnd(AL);
18840b57cec5SDimitry Andric   }
18850b57cec5SDimitry Andric }
18860b57cec5SDimitry Andric 
18870b57cec5SDimitry Andric /// ParseDeclaration - Parse a full 'declaration', which consists of
18880b57cec5SDimitry Andric /// declaration-specifiers, some number of declarators, and a semicolon.
18890b57cec5SDimitry Andric /// 'Context' should be a DeclaratorContext value.  This returns the
18900b57cec5SDimitry Andric /// location of the semicolon in DeclEnd.
18910b57cec5SDimitry Andric ///
18920b57cec5SDimitry Andric ///       declaration: [C99 6.7]
18930b57cec5SDimitry Andric ///         block-declaration ->
18940b57cec5SDimitry Andric ///           simple-declaration
18950b57cec5SDimitry Andric ///           others                   [FIXME]
18960b57cec5SDimitry Andric /// [C++]   template-declaration
18970b57cec5SDimitry Andric /// [C++]   namespace-definition
18980b57cec5SDimitry Andric /// [C++]   using-directive
18990b57cec5SDimitry Andric /// [C++]   using-declaration
19000b57cec5SDimitry Andric /// [C++11/C11] static_assert-declaration
19010b57cec5SDimitry Andric ///         others... [FIXME]
19020b57cec5SDimitry Andric ///
190381ad6265SDimitry Andric Parser::DeclGroupPtrTy Parser::ParseDeclaration(DeclaratorContext Context,
190481ad6265SDimitry Andric                                                 SourceLocation &DeclEnd,
190581ad6265SDimitry Andric                                                 ParsedAttributes &DeclAttrs,
190681ad6265SDimitry Andric                                                 ParsedAttributes &DeclSpecAttrs,
1907a7dea167SDimitry Andric                                                 SourceLocation *DeclSpecStart) {
19080b57cec5SDimitry Andric   ParenBraceBracketBalancer BalancerRAIIObj(*this);
19090b57cec5SDimitry Andric   // Must temporarily exit the objective-c container scope for
19100b57cec5SDimitry Andric   // parsing c none objective-c decls.
19110b57cec5SDimitry Andric   ObjCDeclContextSwitch ObjCDC(*this);
19120b57cec5SDimitry Andric 
19130b57cec5SDimitry Andric   Decl *SingleDecl = nullptr;
19140b57cec5SDimitry Andric   switch (Tok.getKind()) {
19150b57cec5SDimitry Andric   case tok::kw_template:
19160b57cec5SDimitry Andric   case tok::kw_export:
191781ad6265SDimitry Andric     ProhibitAttributes(DeclAttrs);
191881ad6265SDimitry Andric     ProhibitAttributes(DeclSpecAttrs);
191981ad6265SDimitry Andric     SingleDecl =
192081ad6265SDimitry Andric         ParseDeclarationStartingWithTemplate(Context, DeclEnd, DeclAttrs);
19210b57cec5SDimitry Andric     break;
19220b57cec5SDimitry Andric   case tok::kw_inline:
19230b57cec5SDimitry Andric     // Could be the start of an inline namespace. Allowed as an ext in C++03.
19240b57cec5SDimitry Andric     if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
192581ad6265SDimitry Andric       ProhibitAttributes(DeclAttrs);
192681ad6265SDimitry Andric       ProhibitAttributes(DeclSpecAttrs);
19270b57cec5SDimitry Andric       SourceLocation InlineLoc = ConsumeToken();
19280b57cec5SDimitry Andric       return ParseNamespace(Context, DeclEnd, InlineLoc);
19290b57cec5SDimitry Andric     }
193081ad6265SDimitry Andric     return ParseSimpleDeclaration(Context, DeclEnd, DeclAttrs, DeclSpecAttrs,
193181ad6265SDimitry Andric                                   true, nullptr, DeclSpecStart);
1932bdd1243dSDimitry Andric 
1933bdd1243dSDimitry Andric   case tok::kw_cbuffer:
1934bdd1243dSDimitry Andric   case tok::kw_tbuffer:
1935bdd1243dSDimitry Andric     SingleDecl = ParseHLSLBuffer(DeclEnd);
1936bdd1243dSDimitry Andric     break;
19370b57cec5SDimitry Andric   case tok::kw_namespace:
193881ad6265SDimitry Andric     ProhibitAttributes(DeclAttrs);
193981ad6265SDimitry Andric     ProhibitAttributes(DeclSpecAttrs);
19400b57cec5SDimitry Andric     return ParseNamespace(Context, DeclEnd);
194181ad6265SDimitry Andric   case tok::kw_using: {
194281ad6265SDimitry Andric     ParsedAttributes Attrs(AttrFactory);
194381ad6265SDimitry Andric     takeAndConcatenateAttrs(DeclAttrs, DeclSpecAttrs, Attrs);
19440b57cec5SDimitry Andric     return ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
194581ad6265SDimitry Andric                                             DeclEnd, Attrs);
194681ad6265SDimitry Andric   }
19470b57cec5SDimitry Andric   case tok::kw_static_assert:
19480b57cec5SDimitry Andric   case tok::kw__Static_assert:
194981ad6265SDimitry Andric     ProhibitAttributes(DeclAttrs);
195081ad6265SDimitry Andric     ProhibitAttributes(DeclSpecAttrs);
19510b57cec5SDimitry Andric     SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
19520b57cec5SDimitry Andric     break;
19530b57cec5SDimitry Andric   default:
195481ad6265SDimitry Andric     return ParseSimpleDeclaration(Context, DeclEnd, DeclAttrs, DeclSpecAttrs,
195581ad6265SDimitry Andric                                   true, nullptr, DeclSpecStart);
19560b57cec5SDimitry Andric   }
19570b57cec5SDimitry Andric 
19580b57cec5SDimitry Andric   // This routine returns a DeclGroup, if the thing we parsed only contains a
19590b57cec5SDimitry Andric   // single decl, convert it now.
19600b57cec5SDimitry Andric   return Actions.ConvertDeclToDeclGroup(SingleDecl);
19610b57cec5SDimitry Andric }
19620b57cec5SDimitry Andric 
19630b57cec5SDimitry Andric ///       simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
19640b57cec5SDimitry Andric ///         declaration-specifiers init-declarator-list[opt] ';'
19650b57cec5SDimitry Andric /// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
19660b57cec5SDimitry Andric ///             init-declarator-list ';'
19670b57cec5SDimitry Andric ///[C90/C++]init-declarator-list ';'                             [TODO]
19680b57cec5SDimitry Andric /// [OMP]   threadprivate-directive
19690b57cec5SDimitry Andric /// [OMP]   allocate-directive                                   [TODO]
19700b57cec5SDimitry Andric ///
19710b57cec5SDimitry Andric ///       for-range-declaration: [C++11 6.5p1: stmt.ranged]
19720b57cec5SDimitry Andric ///         attribute-specifier-seq[opt] type-specifier-seq declarator
19730b57cec5SDimitry Andric ///
19740b57cec5SDimitry Andric /// If RequireSemi is false, this does not check for a ';' at the end of the
19750b57cec5SDimitry Andric /// declaration.  If it is true, it checks for and eats it.
19760b57cec5SDimitry Andric ///
19770b57cec5SDimitry Andric /// If FRI is non-null, we might be parsing a for-range-declaration instead
19780b57cec5SDimitry Andric /// of a simple-declaration. If we find that we are, we also parse the
19790b57cec5SDimitry Andric /// for-range-initializer, and place it here.
1980a7dea167SDimitry Andric ///
1981a7dea167SDimitry Andric /// DeclSpecStart is used when decl-specifiers are parsed before parsing
1982a7dea167SDimitry Andric /// the Declaration. The SourceLocation for this Decl is set to
1983a7dea167SDimitry Andric /// DeclSpecStart if DeclSpecStart is non-null.
1984a7dea167SDimitry Andric Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(
1985a7dea167SDimitry Andric     DeclaratorContext Context, SourceLocation &DeclEnd,
198681ad6265SDimitry Andric     ParsedAttributes &DeclAttrs, ParsedAttributes &DeclSpecAttrs,
198781ad6265SDimitry Andric     bool RequireSemi, ForRangeInit *FRI, SourceLocation *DeclSpecStart) {
198881ad6265SDimitry Andric   // Need to retain these for diagnostics before we add them to the DeclSepc.
198981ad6265SDimitry Andric   ParsedAttributesView OriginalDeclSpecAttrs;
199081ad6265SDimitry Andric   OriginalDeclSpecAttrs.addAll(DeclSpecAttrs.begin(), DeclSpecAttrs.end());
199181ad6265SDimitry Andric   OriginalDeclSpecAttrs.Range = DeclSpecAttrs.Range;
199281ad6265SDimitry Andric 
19930b57cec5SDimitry Andric   // Parse the common declaration-specifiers piece.
19940b57cec5SDimitry Andric   ParsingDeclSpec DS(*this);
199581ad6265SDimitry Andric   DS.takeAttributesFrom(DeclSpecAttrs);
19960b57cec5SDimitry Andric 
19970b57cec5SDimitry Andric   DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
19980b57cec5SDimitry Andric   ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
19990b57cec5SDimitry Andric 
20000b57cec5SDimitry Andric   // If we had a free-standing type definition with a missing semicolon, we
20010b57cec5SDimitry Andric   // may get this far before the problem becomes obvious.
20020b57cec5SDimitry Andric   if (DS.hasTagDefinition() &&
20030b57cec5SDimitry Andric       DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
20040b57cec5SDimitry Andric     return nullptr;
20050b57cec5SDimitry Andric 
20060b57cec5SDimitry Andric   // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
20070b57cec5SDimitry Andric   // declaration-specifiers init-declarator-list[opt] ';'
20080b57cec5SDimitry Andric   if (Tok.is(tok::semi)) {
200981ad6265SDimitry Andric     ProhibitAttributes(DeclAttrs);
20100b57cec5SDimitry Andric     DeclEnd = Tok.getLocation();
20110b57cec5SDimitry Andric     if (RequireSemi) ConsumeToken();
20120b57cec5SDimitry Andric     RecordDecl *AnonRecord = nullptr;
201381ad6265SDimitry Andric     Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(
201481ad6265SDimitry Andric         getCurScope(), AS_none, DS, ParsedAttributesView::none(), AnonRecord);
20155f757f3fSDimitry Andric     Actions.ActOnDefinedDeclarationSpecifier(TheDecl);
20160b57cec5SDimitry Andric     DS.complete(TheDecl);
20170b57cec5SDimitry Andric     if (AnonRecord) {
20180b57cec5SDimitry Andric       Decl* decls[] = {AnonRecord, TheDecl};
20190b57cec5SDimitry Andric       return Actions.BuildDeclaratorGroup(decls);
20200b57cec5SDimitry Andric     }
20210b57cec5SDimitry Andric     return Actions.ConvertDeclToDeclGroup(TheDecl);
20220b57cec5SDimitry Andric   }
20230b57cec5SDimitry Andric 
20245f757f3fSDimitry Andric   if (DS.hasTagDefinition())
20255f757f3fSDimitry Andric     Actions.ActOnDefinedDeclarationSpecifier(DS.getRepAsDecl());
20265f757f3fSDimitry Andric 
2027a7dea167SDimitry Andric   if (DeclSpecStart)
2028a7dea167SDimitry Andric     DS.SetRangeStart(*DeclSpecStart);
2029a7dea167SDimitry Andric 
203081ad6265SDimitry Andric   return ParseDeclGroup(DS, Context, DeclAttrs, &DeclEnd, FRI);
20310b57cec5SDimitry Andric }
20320b57cec5SDimitry Andric 
20330b57cec5SDimitry Andric /// Returns true if this might be the start of a declarator, or a common typo
20340b57cec5SDimitry Andric /// for a declarator.
20350b57cec5SDimitry Andric bool Parser::MightBeDeclarator(DeclaratorContext Context) {
20360b57cec5SDimitry Andric   switch (Tok.getKind()) {
20370b57cec5SDimitry Andric   case tok::annot_cxxscope:
20380b57cec5SDimitry Andric   case tok::annot_template_id:
20390b57cec5SDimitry Andric   case tok::caret:
20400b57cec5SDimitry Andric   case tok::code_completion:
20410b57cec5SDimitry Andric   case tok::coloncolon:
20420b57cec5SDimitry Andric   case tok::ellipsis:
20430b57cec5SDimitry Andric   case tok::kw___attribute:
20440b57cec5SDimitry Andric   case tok::kw_operator:
20450b57cec5SDimitry Andric   case tok::l_paren:
20460b57cec5SDimitry Andric   case tok::star:
20470b57cec5SDimitry Andric     return true;
20480b57cec5SDimitry Andric 
20490b57cec5SDimitry Andric   case tok::amp:
20500b57cec5SDimitry Andric   case tok::ampamp:
20510b57cec5SDimitry Andric     return getLangOpts().CPlusPlus;
20520b57cec5SDimitry Andric 
20530b57cec5SDimitry Andric   case tok::l_square: // Might be an attribute on an unnamed bit-field.
2054e8d8bef9SDimitry Andric     return Context == DeclaratorContext::Member && getLangOpts().CPlusPlus11 &&
2055e8d8bef9SDimitry Andric            NextToken().is(tok::l_square);
20560b57cec5SDimitry Andric 
20570b57cec5SDimitry Andric   case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
2058e8d8bef9SDimitry Andric     return Context == DeclaratorContext::Member || getLangOpts().CPlusPlus;
20590b57cec5SDimitry Andric 
20600b57cec5SDimitry Andric   case tok::identifier:
20610b57cec5SDimitry Andric     switch (NextToken().getKind()) {
20620b57cec5SDimitry Andric     case tok::code_completion:
20630b57cec5SDimitry Andric     case tok::coloncolon:
20640b57cec5SDimitry Andric     case tok::comma:
20650b57cec5SDimitry Andric     case tok::equal:
20660b57cec5SDimitry Andric     case tok::equalequal: // Might be a typo for '='.
20670b57cec5SDimitry Andric     case tok::kw_alignas:
20680b57cec5SDimitry Andric     case tok::kw_asm:
20690b57cec5SDimitry Andric     case tok::kw___attribute:
20700b57cec5SDimitry Andric     case tok::l_brace:
20710b57cec5SDimitry Andric     case tok::l_paren:
20720b57cec5SDimitry Andric     case tok::l_square:
20730b57cec5SDimitry Andric     case tok::less:
20740b57cec5SDimitry Andric     case tok::r_brace:
20750b57cec5SDimitry Andric     case tok::r_paren:
20760b57cec5SDimitry Andric     case tok::r_square:
20770b57cec5SDimitry Andric     case tok::semi:
20780b57cec5SDimitry Andric       return true;
20790b57cec5SDimitry Andric 
20800b57cec5SDimitry Andric     case tok::colon:
20810b57cec5SDimitry Andric       // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
20820b57cec5SDimitry Andric       // and in block scope it's probably a label. Inside a class definition,
20830b57cec5SDimitry Andric       // this is a bit-field.
2084e8d8bef9SDimitry Andric       return Context == DeclaratorContext::Member ||
2085e8d8bef9SDimitry Andric              (getLangOpts().CPlusPlus && Context == DeclaratorContext::File);
20860b57cec5SDimitry Andric 
20870b57cec5SDimitry Andric     case tok::identifier: // Possible virt-specifier.
20880b57cec5SDimitry Andric       return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
20890b57cec5SDimitry Andric 
20900b57cec5SDimitry Andric     default:
209106c3fb27SDimitry Andric       return Tok.isRegularKeywordAttribute();
20920b57cec5SDimitry Andric     }
20930b57cec5SDimitry Andric 
20940b57cec5SDimitry Andric   default:
209506c3fb27SDimitry Andric     return Tok.isRegularKeywordAttribute();
20960b57cec5SDimitry Andric   }
20970b57cec5SDimitry Andric }
20980b57cec5SDimitry Andric 
20990b57cec5SDimitry Andric /// Skip until we reach something which seems like a sensible place to pick
21000b57cec5SDimitry Andric /// up parsing after a malformed declaration. This will sometimes stop sooner
21010b57cec5SDimitry Andric /// than SkipUntil(tok::r_brace) would, but will never stop later.
21020b57cec5SDimitry Andric void Parser::SkipMalformedDecl() {
21030b57cec5SDimitry Andric   while (true) {
21040b57cec5SDimitry Andric     switch (Tok.getKind()) {
21050b57cec5SDimitry Andric     case tok::l_brace:
21060b57cec5SDimitry Andric       // Skip until matching }, then stop. We've probably skipped over
21070b57cec5SDimitry Andric       // a malformed class or function definition or similar.
21080b57cec5SDimitry Andric       ConsumeBrace();
21090b57cec5SDimitry Andric       SkipUntil(tok::r_brace);
21100b57cec5SDimitry Andric       if (Tok.isOneOf(tok::comma, tok::l_brace, tok::kw_try)) {
21110b57cec5SDimitry Andric         // This declaration isn't over yet. Keep skipping.
21120b57cec5SDimitry Andric         continue;
21130b57cec5SDimitry Andric       }
21140b57cec5SDimitry Andric       TryConsumeToken(tok::semi);
21150b57cec5SDimitry Andric       return;
21160b57cec5SDimitry Andric 
21170b57cec5SDimitry Andric     case tok::l_square:
21180b57cec5SDimitry Andric       ConsumeBracket();
21190b57cec5SDimitry Andric       SkipUntil(tok::r_square);
21200b57cec5SDimitry Andric       continue;
21210b57cec5SDimitry Andric 
21220b57cec5SDimitry Andric     case tok::l_paren:
21230b57cec5SDimitry Andric       ConsumeParen();
21240b57cec5SDimitry Andric       SkipUntil(tok::r_paren);
21250b57cec5SDimitry Andric       continue;
21260b57cec5SDimitry Andric 
21270b57cec5SDimitry Andric     case tok::r_brace:
21280b57cec5SDimitry Andric       return;
21290b57cec5SDimitry Andric 
21300b57cec5SDimitry Andric     case tok::semi:
21310b57cec5SDimitry Andric       ConsumeToken();
21320b57cec5SDimitry Andric       return;
21330b57cec5SDimitry Andric 
21340b57cec5SDimitry Andric     case tok::kw_inline:
21350b57cec5SDimitry Andric       // 'inline namespace' at the start of a line is almost certainly
21360b57cec5SDimitry Andric       // a good place to pick back up parsing, except in an Objective-C
21370b57cec5SDimitry Andric       // @interface context.
21380b57cec5SDimitry Andric       if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
21390b57cec5SDimitry Andric           (!ParsingInObjCContainer || CurParsedObjCImpl))
21400b57cec5SDimitry Andric         return;
21410b57cec5SDimitry Andric       break;
21420b57cec5SDimitry Andric 
21430b57cec5SDimitry Andric     case tok::kw_namespace:
21440b57cec5SDimitry Andric       // 'namespace' at the start of a line is almost certainly a good
21450b57cec5SDimitry Andric       // place to pick back up parsing, except in an Objective-C
21460b57cec5SDimitry Andric       // @interface context.
21470b57cec5SDimitry Andric       if (Tok.isAtStartOfLine() &&
21480b57cec5SDimitry Andric           (!ParsingInObjCContainer || CurParsedObjCImpl))
21490b57cec5SDimitry Andric         return;
21500b57cec5SDimitry Andric       break;
21510b57cec5SDimitry Andric 
21520b57cec5SDimitry Andric     case tok::at:
21530b57cec5SDimitry Andric       // @end is very much like } in Objective-C contexts.
21540b57cec5SDimitry Andric       if (NextToken().isObjCAtKeyword(tok::objc_end) &&
21550b57cec5SDimitry Andric           ParsingInObjCContainer)
21560b57cec5SDimitry Andric         return;
21570b57cec5SDimitry Andric       break;
21580b57cec5SDimitry Andric 
21590b57cec5SDimitry Andric     case tok::minus:
21600b57cec5SDimitry Andric     case tok::plus:
21610b57cec5SDimitry Andric       // - and + probably start new method declarations in Objective-C contexts.
21620b57cec5SDimitry Andric       if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
21630b57cec5SDimitry Andric         return;
21640b57cec5SDimitry Andric       break;
21650b57cec5SDimitry Andric 
21660b57cec5SDimitry Andric     case tok::eof:
21670b57cec5SDimitry Andric     case tok::annot_module_begin:
21680b57cec5SDimitry Andric     case tok::annot_module_end:
21690b57cec5SDimitry Andric     case tok::annot_module_include:
217006c3fb27SDimitry Andric     case tok::annot_repl_input_end:
21710b57cec5SDimitry Andric       return;
21720b57cec5SDimitry Andric 
21730b57cec5SDimitry Andric     default:
21740b57cec5SDimitry Andric       break;
21750b57cec5SDimitry Andric     }
21760b57cec5SDimitry Andric 
21770b57cec5SDimitry Andric     ConsumeAnyToken();
21780b57cec5SDimitry Andric   }
21790b57cec5SDimitry Andric }
21800b57cec5SDimitry Andric 
21810b57cec5SDimitry Andric /// ParseDeclGroup - Having concluded that this is either a function
21820b57cec5SDimitry Andric /// definition or a group of object declarations, actually parse the
21830b57cec5SDimitry Andric /// result.
21840b57cec5SDimitry Andric Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
21850b57cec5SDimitry Andric                                               DeclaratorContext Context,
218681ad6265SDimitry Andric                                               ParsedAttributes &Attrs,
21870b57cec5SDimitry Andric                                               SourceLocation *DeclEnd,
21880b57cec5SDimitry Andric                                               ForRangeInit *FRI) {
21890b57cec5SDimitry Andric   // Parse the first declarator.
219081ad6265SDimitry Andric   // Consume all of the attributes from `Attrs` by moving them to our own local
219181ad6265SDimitry Andric   // list. This ensures that we will not attempt to interpret them as statement
219281ad6265SDimitry Andric   // attributes higher up the callchain.
219381ad6265SDimitry Andric   ParsedAttributes LocalAttrs(AttrFactory);
219481ad6265SDimitry Andric   LocalAttrs.takeAllFrom(Attrs);
219581ad6265SDimitry Andric   ParsingDeclarator D(*this, DS, LocalAttrs, Context);
21960b57cec5SDimitry Andric   ParseDeclarator(D);
21970b57cec5SDimitry Andric 
21980b57cec5SDimitry Andric   // Bail out if the first declarator didn't seem well-formed.
21990b57cec5SDimitry Andric   if (!D.hasName() && !D.mayOmitIdentifier()) {
22000b57cec5SDimitry Andric     SkipMalformedDecl();
22010b57cec5SDimitry Andric     return nullptr;
22020b57cec5SDimitry Andric   }
22030b57cec5SDimitry Andric 
2204bdd1243dSDimitry Andric   if (getLangOpts().HLSL)
2205bdd1243dSDimitry Andric     MaybeParseHLSLSemantics(D);
2206bdd1243dSDimitry Andric 
2207480093f4SDimitry Andric   if (Tok.is(tok::kw_requires))
2208480093f4SDimitry Andric     ParseTrailingRequiresClause(D);
2209480093f4SDimitry Andric 
22100b57cec5SDimitry Andric   // Save late-parsed attributes for now; they need to be parsed in the
22110b57cec5SDimitry Andric   // appropriate function scope after the function Decl has been constructed.
22120b57cec5SDimitry Andric   // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
22130b57cec5SDimitry Andric   LateParsedAttrList LateParsedAttrs(true);
22140b57cec5SDimitry Andric   if (D.isFunctionDeclarator()) {
22150b57cec5SDimitry Andric     MaybeParseGNUAttributes(D, &LateParsedAttrs);
22160b57cec5SDimitry Andric 
22170b57cec5SDimitry Andric     // The _Noreturn keyword can't appear here, unlike the GNU noreturn
22180b57cec5SDimitry Andric     // attribute. If we find the keyword here, tell the user to put it
22190b57cec5SDimitry Andric     // at the start instead.
22200b57cec5SDimitry Andric     if (Tok.is(tok::kw__Noreturn)) {
22210b57cec5SDimitry Andric       SourceLocation Loc = ConsumeToken();
22220b57cec5SDimitry Andric       const char *PrevSpec;
22230b57cec5SDimitry Andric       unsigned DiagID;
22240b57cec5SDimitry Andric 
22250b57cec5SDimitry Andric       // We can offer a fixit if it's valid to mark this function as _Noreturn
22260b57cec5SDimitry Andric       // and we don't have any other declarators in this declaration.
22270b57cec5SDimitry Andric       bool Fixit = !DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
22280b57cec5SDimitry Andric       MaybeParseGNUAttributes(D, &LateParsedAttrs);
22290b57cec5SDimitry Andric       Fixit &= Tok.isOneOf(tok::semi, tok::l_brace, tok::kw_try);
22300b57cec5SDimitry Andric 
22310b57cec5SDimitry Andric       Diag(Loc, diag::err_c11_noreturn_misplaced)
22320b57cec5SDimitry Andric           << (Fixit ? FixItHint::CreateRemoval(Loc) : FixItHint())
22330b57cec5SDimitry Andric           << (Fixit ? FixItHint::CreateInsertion(D.getBeginLoc(), "_Noreturn ")
22340b57cec5SDimitry Andric                     : FixItHint());
22350b57cec5SDimitry Andric     }
22360b57cec5SDimitry Andric 
22370b57cec5SDimitry Andric     // Check to see if we have a function *definition* which must have a body.
22385ffd83dbSDimitry Andric     if (Tok.is(tok::equal) && NextToken().is(tok::code_completion)) {
22395ffd83dbSDimitry Andric       cutOffParsing();
2240fe6060f1SDimitry Andric       Actions.CodeCompleteAfterFunctionEquals(D);
22415ffd83dbSDimitry Andric       return nullptr;
22425ffd83dbSDimitry Andric     }
2243349cc55cSDimitry Andric     // We're at the point where the parsing of function declarator is finished.
2244349cc55cSDimitry Andric     //
2245349cc55cSDimitry Andric     // A common error is that users accidently add a virtual specifier
2246349cc55cSDimitry Andric     // (e.g. override) in an out-line method definition.
2247349cc55cSDimitry Andric     // We attempt to recover by stripping all these specifiers coming after
2248349cc55cSDimitry Andric     // the declarator.
2249349cc55cSDimitry Andric     while (auto Specifier = isCXX11VirtSpecifier()) {
2250349cc55cSDimitry Andric       Diag(Tok, diag::err_virt_specifier_outside_class)
2251349cc55cSDimitry Andric           << VirtSpecifiers::getSpecifierName(Specifier)
2252349cc55cSDimitry Andric           << FixItHint::CreateRemoval(Tok.getLocation());
2253349cc55cSDimitry Andric       ConsumeToken();
2254349cc55cSDimitry Andric     }
22550b57cec5SDimitry Andric     // Look at the next token to make sure that this isn't a function
22560b57cec5SDimitry Andric     // declaration.  We have to check this because __attribute__ might be the
22570b57cec5SDimitry Andric     // start of a function definition in GCC-extended K&R C.
22585ffd83dbSDimitry Andric     if (!isDeclarationAfterDeclarator()) {
22590b57cec5SDimitry Andric 
22600b57cec5SDimitry Andric       // Function definitions are only allowed at file scope and in C++ classes.
22610b57cec5SDimitry Andric       // The C++ inline method definition case is handled elsewhere, so we only
22620b57cec5SDimitry Andric       // need to handle the file scope definition case.
2263e8d8bef9SDimitry Andric       if (Context == DeclaratorContext::File) {
22640b57cec5SDimitry Andric         if (isStartOfFunctionDefinition(D)) {
22650b57cec5SDimitry Andric           if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
22660b57cec5SDimitry Andric             Diag(Tok, diag::err_function_declared_typedef);
22670b57cec5SDimitry Andric 
22680b57cec5SDimitry Andric             // Recover by treating the 'typedef' as spurious.
22690b57cec5SDimitry Andric             DS.ClearStorageClassSpecs();
22700b57cec5SDimitry Andric           }
22710b57cec5SDimitry Andric 
22725ffd83dbSDimitry Andric           Decl *TheDecl = ParseFunctionDefinition(D, ParsedTemplateInfo(),
22735ffd83dbSDimitry Andric                                                   &LateParsedAttrs);
22740b57cec5SDimitry Andric           return Actions.ConvertDeclToDeclGroup(TheDecl);
22750b57cec5SDimitry Andric         }
22760b57cec5SDimitry Andric 
227706c3fb27SDimitry Andric         if (isDeclarationSpecifier(ImplicitTypenameContext::No) ||
227806c3fb27SDimitry Andric             Tok.is(tok::kw_namespace)) {
227906c3fb27SDimitry Andric           // If there is an invalid declaration specifier or a namespace
228006c3fb27SDimitry Andric           // definition right after the function prototype, then we must be in a
228106c3fb27SDimitry Andric           // missing semicolon case where this isn't actually a body.  Just fall
228206c3fb27SDimitry Andric           // through into the code that handles it as a prototype, and let the
228306c3fb27SDimitry Andric           // top-level code handle the erroneous declspec where it would
228406c3fb27SDimitry Andric           // otherwise expect a comma or semicolon. Note that
228506c3fb27SDimitry Andric           // isDeclarationSpecifier already covers 'inline namespace', since
228606c3fb27SDimitry Andric           // 'inline' can be a declaration specifier.
22870b57cec5SDimitry Andric         } else {
22880b57cec5SDimitry Andric           Diag(Tok, diag::err_expected_fn_body);
22890b57cec5SDimitry Andric           SkipUntil(tok::semi);
22900b57cec5SDimitry Andric           return nullptr;
22910b57cec5SDimitry Andric         }
22920b57cec5SDimitry Andric       } else {
22930b57cec5SDimitry Andric         if (Tok.is(tok::l_brace)) {
22940b57cec5SDimitry Andric           Diag(Tok, diag::err_function_definition_not_allowed);
22950b57cec5SDimitry Andric           SkipMalformedDecl();
22960b57cec5SDimitry Andric           return nullptr;
22970b57cec5SDimitry Andric         }
22980b57cec5SDimitry Andric       }
22990b57cec5SDimitry Andric     }
23005ffd83dbSDimitry Andric   }
23010b57cec5SDimitry Andric 
23020b57cec5SDimitry Andric   if (ParseAsmAttributesAfterDeclarator(D))
23030b57cec5SDimitry Andric     return nullptr;
23040b57cec5SDimitry Andric 
23050b57cec5SDimitry Andric   // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
23060b57cec5SDimitry Andric   // must parse and analyze the for-range-initializer before the declaration is
23070b57cec5SDimitry Andric   // analyzed.
23080b57cec5SDimitry Andric   //
23090b57cec5SDimitry Andric   // Handle the Objective-C for-in loop variable similarly, although we
23100b57cec5SDimitry Andric   // don't need to parse the container in advance.
23110b57cec5SDimitry Andric   if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
23120b57cec5SDimitry Andric     bool IsForRangeLoop = false;
23130b57cec5SDimitry Andric     if (TryConsumeToken(tok::colon, FRI->ColonLoc)) {
23140b57cec5SDimitry Andric       IsForRangeLoop = true;
2315a7dea167SDimitry Andric       if (getLangOpts().OpenMP)
2316a7dea167SDimitry Andric         Actions.startOpenMPCXXRangeFor();
23170b57cec5SDimitry Andric       if (Tok.is(tok::l_brace))
23180b57cec5SDimitry Andric         FRI->RangeExpr = ParseBraceInitializer();
23190b57cec5SDimitry Andric       else
23200b57cec5SDimitry Andric         FRI->RangeExpr = ParseExpression();
23210b57cec5SDimitry Andric     }
23220b57cec5SDimitry Andric 
23230b57cec5SDimitry Andric     Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
23240b57cec5SDimitry Andric     if (IsForRangeLoop) {
23250b57cec5SDimitry Andric       Actions.ActOnCXXForRangeDecl(ThisDecl);
23260b57cec5SDimitry Andric     } else {
23270b57cec5SDimitry Andric       // Obj-C for loop
23280b57cec5SDimitry Andric       if (auto *VD = dyn_cast_or_null<VarDecl>(ThisDecl))
23290b57cec5SDimitry Andric         VD->setObjCForDecl(true);
23300b57cec5SDimitry Andric     }
23310b57cec5SDimitry Andric     Actions.FinalizeDeclaration(ThisDecl);
23320b57cec5SDimitry Andric     D.complete(ThisDecl);
23330b57cec5SDimitry Andric     return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
23340b57cec5SDimitry Andric   }
23350b57cec5SDimitry Andric 
23360b57cec5SDimitry Andric   SmallVector<Decl *, 8> DeclsInGroup;
23370b57cec5SDimitry Andric   Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(
23380b57cec5SDimitry Andric       D, ParsedTemplateInfo(), FRI);
23390b57cec5SDimitry Andric   if (LateParsedAttrs.size() > 0)
23400b57cec5SDimitry Andric     ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
23410b57cec5SDimitry Andric   D.complete(FirstDecl);
23420b57cec5SDimitry Andric   if (FirstDecl)
23430b57cec5SDimitry Andric     DeclsInGroup.push_back(FirstDecl);
23440b57cec5SDimitry Andric 
2345e8d8bef9SDimitry Andric   bool ExpectSemi = Context != DeclaratorContext::ForInit;
23460b57cec5SDimitry Andric 
23470b57cec5SDimitry Andric   // If we don't have a comma, it is either the end of the list (a ';') or an
23480b57cec5SDimitry Andric   // error, bail out.
23490b57cec5SDimitry Andric   SourceLocation CommaLoc;
23500b57cec5SDimitry Andric   while (TryConsumeToken(tok::comma, CommaLoc)) {
23510b57cec5SDimitry Andric     if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
23520b57cec5SDimitry Andric       // This comma was followed by a line-break and something which can't be
23530b57cec5SDimitry Andric       // the start of a declarator. The comma was probably a typo for a
23540b57cec5SDimitry Andric       // semicolon.
23550b57cec5SDimitry Andric       Diag(CommaLoc, diag::err_expected_semi_declaration)
23560b57cec5SDimitry Andric         << FixItHint::CreateReplacement(CommaLoc, ";");
23570b57cec5SDimitry Andric       ExpectSemi = false;
23580b57cec5SDimitry Andric       break;
23590b57cec5SDimitry Andric     }
23600b57cec5SDimitry Andric 
23610b57cec5SDimitry Andric     // Parse the next declarator.
23620b57cec5SDimitry Andric     D.clear();
23630b57cec5SDimitry Andric     D.setCommaLoc(CommaLoc);
23640b57cec5SDimitry Andric 
23650b57cec5SDimitry Andric     // Accept attributes in an init-declarator.  In the first declarator in a
23660b57cec5SDimitry Andric     // declaration, these would be part of the declspec.  In subsequent
23670b57cec5SDimitry Andric     // declarators, they become part of the declarator itself, so that they
23680b57cec5SDimitry Andric     // don't apply to declarators after *this* one.  Examples:
23690b57cec5SDimitry Andric     //    short __attribute__((common)) var;    -> declspec
23700b57cec5SDimitry Andric     //    short var __attribute__((common));    -> declarator
23710b57cec5SDimitry Andric     //    short x, __attribute__((common)) var;    -> declarator
23720b57cec5SDimitry Andric     MaybeParseGNUAttributes(D);
23730b57cec5SDimitry Andric 
23740b57cec5SDimitry Andric     // MSVC parses but ignores qualifiers after the comma as an extension.
23750b57cec5SDimitry Andric     if (getLangOpts().MicrosoftExt)
23760b57cec5SDimitry Andric       DiagnoseAndSkipExtendedMicrosoftTypeAttributes();
23770b57cec5SDimitry Andric 
23780b57cec5SDimitry Andric     ParseDeclarator(D);
2379bdd1243dSDimitry Andric 
2380bdd1243dSDimitry Andric     if (getLangOpts().HLSL)
2381bdd1243dSDimitry Andric       MaybeParseHLSLSemantics(D);
2382bdd1243dSDimitry Andric 
23830b57cec5SDimitry Andric     if (!D.isInvalidType()) {
2384480093f4SDimitry Andric       // C++2a [dcl.decl]p1
2385480093f4SDimitry Andric       //    init-declarator:
2386480093f4SDimitry Andric       //	      declarator initializer[opt]
2387480093f4SDimitry Andric       //        declarator requires-clause
2388480093f4SDimitry Andric       if (Tok.is(tok::kw_requires))
2389480093f4SDimitry Andric         ParseTrailingRequiresClause(D);
23900b57cec5SDimitry Andric       Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
23910b57cec5SDimitry Andric       D.complete(ThisDecl);
23920b57cec5SDimitry Andric       if (ThisDecl)
23930b57cec5SDimitry Andric         DeclsInGroup.push_back(ThisDecl);
23940b57cec5SDimitry Andric     }
23950b57cec5SDimitry Andric   }
23960b57cec5SDimitry Andric 
23970b57cec5SDimitry Andric   if (DeclEnd)
23980b57cec5SDimitry Andric     *DeclEnd = Tok.getLocation();
23990b57cec5SDimitry Andric 
2400e8d8bef9SDimitry Andric   if (ExpectSemi && ExpectAndConsumeSemi(
2401e8d8bef9SDimitry Andric                         Context == DeclaratorContext::File
24020b57cec5SDimitry Andric                             ? diag::err_invalid_token_after_toplevel_declarator
24030b57cec5SDimitry Andric                             : diag::err_expected_semi_declaration)) {
24040b57cec5SDimitry Andric     // Okay, there was no semicolon and one was expected.  If we see a
24050b57cec5SDimitry Andric     // declaration specifier, just assume it was missing and continue parsing.
24060b57cec5SDimitry Andric     // Otherwise things are very confused and we skip to recover.
240706c3fb27SDimitry Andric     if (!isDeclarationSpecifier(ImplicitTypenameContext::No))
240806c3fb27SDimitry Andric       SkipMalformedDecl();
24090b57cec5SDimitry Andric   }
24100b57cec5SDimitry Andric 
24110b57cec5SDimitry Andric   return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
24120b57cec5SDimitry Andric }
24130b57cec5SDimitry Andric 
24140b57cec5SDimitry Andric /// Parse an optional simple-asm-expr and attributes, and attach them to a
24150b57cec5SDimitry Andric /// declarator. Returns true on an error.
24160b57cec5SDimitry Andric bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
24170b57cec5SDimitry Andric   // If a simple-asm-expr is present, parse it.
24180b57cec5SDimitry Andric   if (Tok.is(tok::kw_asm)) {
24190b57cec5SDimitry Andric     SourceLocation Loc;
2420480093f4SDimitry Andric     ExprResult AsmLabel(ParseSimpleAsm(/*ForAsmLabel*/ true, &Loc));
24210b57cec5SDimitry Andric     if (AsmLabel.isInvalid()) {
24220b57cec5SDimitry Andric       SkipUntil(tok::semi, StopBeforeMatch);
24230b57cec5SDimitry Andric       return true;
24240b57cec5SDimitry Andric     }
24250b57cec5SDimitry Andric 
24260b57cec5SDimitry Andric     D.setAsmLabel(AsmLabel.get());
24270b57cec5SDimitry Andric     D.SetRangeEnd(Loc);
24280b57cec5SDimitry Andric   }
24290b57cec5SDimitry Andric 
24300b57cec5SDimitry Andric   MaybeParseGNUAttributes(D);
24310b57cec5SDimitry Andric   return false;
24320b57cec5SDimitry Andric }
24330b57cec5SDimitry Andric 
24340b57cec5SDimitry Andric /// Parse 'declaration' after parsing 'declaration-specifiers
24350b57cec5SDimitry Andric /// declarator'. This method parses the remainder of the declaration
24360b57cec5SDimitry Andric /// (including any attributes or initializer, among other things) and
24370b57cec5SDimitry Andric /// finalizes the declaration.
24380b57cec5SDimitry Andric ///
24390b57cec5SDimitry Andric ///       init-declarator: [C99 6.7]
24400b57cec5SDimitry Andric ///         declarator
24410b57cec5SDimitry Andric ///         declarator '=' initializer
24420b57cec5SDimitry Andric /// [GNU]   declarator simple-asm-expr[opt] attributes[opt]
24430b57cec5SDimitry Andric /// [GNU]   declarator simple-asm-expr[opt] attributes[opt] '=' initializer
24440b57cec5SDimitry Andric /// [C++]   declarator initializer[opt]
24450b57cec5SDimitry Andric ///
24460b57cec5SDimitry Andric /// [C++] initializer:
24470b57cec5SDimitry Andric /// [C++]   '=' initializer-clause
24480b57cec5SDimitry Andric /// [C++]   '(' expression-list ')'
24490b57cec5SDimitry Andric /// [C++0x] '=' 'default'                                                [TODO]
24500b57cec5SDimitry Andric /// [C++0x] '=' 'delete'
24510b57cec5SDimitry Andric /// [C++0x] braced-init-list
24520b57cec5SDimitry Andric ///
24530b57cec5SDimitry Andric /// According to the standard grammar, =default and =delete are function
24540b57cec5SDimitry Andric /// definitions, but that definitely doesn't fit with the parser here.
24550b57cec5SDimitry Andric ///
24560b57cec5SDimitry Andric Decl *Parser::ParseDeclarationAfterDeclarator(
24570b57cec5SDimitry Andric     Declarator &D, const ParsedTemplateInfo &TemplateInfo) {
24580b57cec5SDimitry Andric   if (ParseAsmAttributesAfterDeclarator(D))
24590b57cec5SDimitry Andric     return nullptr;
24600b57cec5SDimitry Andric 
24610b57cec5SDimitry Andric   return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
24620b57cec5SDimitry Andric }
24630b57cec5SDimitry Andric 
24640b57cec5SDimitry Andric Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(
24650b57cec5SDimitry Andric     Declarator &D, const ParsedTemplateInfo &TemplateInfo, ForRangeInit *FRI) {
24660b57cec5SDimitry Andric   // RAII type used to track whether we're inside an initializer.
24670b57cec5SDimitry Andric   struct InitializerScopeRAII {
24680b57cec5SDimitry Andric     Parser &P;
24690b57cec5SDimitry Andric     Declarator &D;
24700b57cec5SDimitry Andric     Decl *ThisDecl;
24710b57cec5SDimitry Andric 
24720b57cec5SDimitry Andric     InitializerScopeRAII(Parser &P, Declarator &D, Decl *ThisDecl)
24730b57cec5SDimitry Andric         : P(P), D(D), ThisDecl(ThisDecl) {
24740b57cec5SDimitry Andric       if (ThisDecl && P.getLangOpts().CPlusPlus) {
24750b57cec5SDimitry Andric         Scope *S = nullptr;
24760b57cec5SDimitry Andric         if (D.getCXXScopeSpec().isSet()) {
24770b57cec5SDimitry Andric           P.EnterScope(0);
24780b57cec5SDimitry Andric           S = P.getCurScope();
24790b57cec5SDimitry Andric         }
24800b57cec5SDimitry Andric         P.Actions.ActOnCXXEnterDeclInitializer(S, ThisDecl);
24810b57cec5SDimitry Andric       }
24820b57cec5SDimitry Andric     }
24830b57cec5SDimitry Andric     ~InitializerScopeRAII() { pop(); }
24840b57cec5SDimitry Andric     void pop() {
24850b57cec5SDimitry Andric       if (ThisDecl && P.getLangOpts().CPlusPlus) {
24860b57cec5SDimitry Andric         Scope *S = nullptr;
24870b57cec5SDimitry Andric         if (D.getCXXScopeSpec().isSet())
24880b57cec5SDimitry Andric           S = P.getCurScope();
24890b57cec5SDimitry Andric         P.Actions.ActOnCXXExitDeclInitializer(S, ThisDecl);
24900b57cec5SDimitry Andric         if (S)
24910b57cec5SDimitry Andric           P.ExitScope();
24920b57cec5SDimitry Andric       }
24930b57cec5SDimitry Andric       ThisDecl = nullptr;
24940b57cec5SDimitry Andric     }
24950b57cec5SDimitry Andric   };
24960b57cec5SDimitry Andric 
2497e8d8bef9SDimitry Andric   enum class InitKind { Uninitialized, Equal, CXXDirect, CXXBraced };
2498e8d8bef9SDimitry Andric   InitKind TheInitKind;
2499e8d8bef9SDimitry Andric   // If a '==' or '+=' is found, suggest a fixit to '='.
2500e8d8bef9SDimitry Andric   if (isTokenEqualOrEqualTypo())
2501e8d8bef9SDimitry Andric     TheInitKind = InitKind::Equal;
2502e8d8bef9SDimitry Andric   else if (Tok.is(tok::l_paren))
2503e8d8bef9SDimitry Andric     TheInitKind = InitKind::CXXDirect;
2504e8d8bef9SDimitry Andric   else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
2505e8d8bef9SDimitry Andric            (!CurParsedObjCImpl || !D.isFunctionDeclarator()))
2506e8d8bef9SDimitry Andric     TheInitKind = InitKind::CXXBraced;
2507e8d8bef9SDimitry Andric   else
2508e8d8bef9SDimitry Andric     TheInitKind = InitKind::Uninitialized;
2509e8d8bef9SDimitry Andric   if (TheInitKind != InitKind::Uninitialized)
2510e8d8bef9SDimitry Andric     D.setHasInitializer();
2511e8d8bef9SDimitry Andric 
2512e8d8bef9SDimitry Andric   // Inform Sema that we just parsed this declarator.
25130b57cec5SDimitry Andric   Decl *ThisDecl = nullptr;
2514e8d8bef9SDimitry Andric   Decl *OuterDecl = nullptr;
25150b57cec5SDimitry Andric   switch (TemplateInfo.Kind) {
25160b57cec5SDimitry Andric   case ParsedTemplateInfo::NonTemplate:
25170b57cec5SDimitry Andric     ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
25180b57cec5SDimitry Andric     break;
25190b57cec5SDimitry Andric 
25200b57cec5SDimitry Andric   case ParsedTemplateInfo::Template:
25210b57cec5SDimitry Andric   case ParsedTemplateInfo::ExplicitSpecialization: {
25220b57cec5SDimitry Andric     ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
25230b57cec5SDimitry Andric                                                *TemplateInfo.TemplateParams,
25240b57cec5SDimitry Andric                                                D);
2525e8d8bef9SDimitry Andric     if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) {
25260b57cec5SDimitry Andric       // Re-direct this decl to refer to the templated decl so that we can
25270b57cec5SDimitry Andric       // initialize it.
25280b57cec5SDimitry Andric       ThisDecl = VT->getTemplatedDecl();
2529e8d8bef9SDimitry Andric       OuterDecl = VT;
2530e8d8bef9SDimitry Andric     }
25310b57cec5SDimitry Andric     break;
25320b57cec5SDimitry Andric   }
25330b57cec5SDimitry Andric   case ParsedTemplateInfo::ExplicitInstantiation: {
25340b57cec5SDimitry Andric     if (Tok.is(tok::semi)) {
25350b57cec5SDimitry Andric       DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
25360b57cec5SDimitry Andric           getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
25370b57cec5SDimitry Andric       if (ThisRes.isInvalid()) {
25380b57cec5SDimitry Andric         SkipUntil(tok::semi, StopBeforeMatch);
25390b57cec5SDimitry Andric         return nullptr;
25400b57cec5SDimitry Andric       }
25410b57cec5SDimitry Andric       ThisDecl = ThisRes.get();
25420b57cec5SDimitry Andric     } else {
25430b57cec5SDimitry Andric       // FIXME: This check should be for a variable template instantiation only.
25440b57cec5SDimitry Andric 
25450b57cec5SDimitry Andric       // Check that this is a valid instantiation
25460b57cec5SDimitry Andric       if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) {
25470b57cec5SDimitry Andric         // If the declarator-id is not a template-id, issue a diagnostic and
25480b57cec5SDimitry Andric         // recover by ignoring the 'template' keyword.
25490b57cec5SDimitry Andric         Diag(Tok, diag::err_template_defn_explicit_instantiation)
25500b57cec5SDimitry Andric             << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
25510b57cec5SDimitry Andric         ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
25520b57cec5SDimitry Andric       } else {
25530b57cec5SDimitry Andric         SourceLocation LAngleLoc =
25540b57cec5SDimitry Andric             PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
25550b57cec5SDimitry Andric         Diag(D.getIdentifierLoc(),
25560b57cec5SDimitry Andric              diag::err_explicit_instantiation_with_definition)
25570b57cec5SDimitry Andric             << SourceRange(TemplateInfo.TemplateLoc)
25580b57cec5SDimitry Andric             << FixItHint::CreateInsertion(LAngleLoc, "<>");
25590b57cec5SDimitry Andric 
25600b57cec5SDimitry Andric         // Recover as if it were an explicit specialization.
25610b57cec5SDimitry Andric         TemplateParameterLists FakedParamLists;
25620b57cec5SDimitry Andric         FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
2563bdd1243dSDimitry Andric             0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc,
2564bdd1243dSDimitry Andric             std::nullopt, LAngleLoc, nullptr));
25650b57cec5SDimitry Andric 
25660b57cec5SDimitry Andric         ThisDecl =
25670b57cec5SDimitry Andric             Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
25680b57cec5SDimitry Andric       }
25690b57cec5SDimitry Andric     }
25700b57cec5SDimitry Andric     break;
25710b57cec5SDimitry Andric     }
25720b57cec5SDimitry Andric   }
25730b57cec5SDimitry Andric 
25745f757f3fSDimitry Andric   Sema::CUDATargetContextRAII X(Actions, Sema::CTCK_InitGlobalVar, ThisDecl);
2575e8d8bef9SDimitry Andric   switch (TheInitKind) {
25760b57cec5SDimitry Andric   // Parse declarator '=' initializer.
2577e8d8bef9SDimitry Andric   case InitKind::Equal: {
25780b57cec5SDimitry Andric     SourceLocation EqualLoc = ConsumeToken();
25790b57cec5SDimitry Andric 
25800b57cec5SDimitry Andric     if (Tok.is(tok::kw_delete)) {
25810b57cec5SDimitry Andric       if (D.isFunctionDeclarator())
25820b57cec5SDimitry Andric         Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
25830b57cec5SDimitry Andric           << 1 /* delete */;
25840b57cec5SDimitry Andric       else
25850b57cec5SDimitry Andric         Diag(ConsumeToken(), diag::err_deleted_non_function);
25860b57cec5SDimitry Andric     } else if (Tok.is(tok::kw_default)) {
25870b57cec5SDimitry Andric       if (D.isFunctionDeclarator())
25880b57cec5SDimitry Andric         Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
25890b57cec5SDimitry Andric           << 0 /* default */;
25900b57cec5SDimitry Andric       else
2591480093f4SDimitry Andric         Diag(ConsumeToken(), diag::err_default_special_members)
25925ffd83dbSDimitry Andric             << getLangOpts().CPlusPlus20;
25930b57cec5SDimitry Andric     } else {
25940b57cec5SDimitry Andric       InitializerScopeRAII InitScope(*this, D, ThisDecl);
25950b57cec5SDimitry Andric 
25960b57cec5SDimitry Andric       if (Tok.is(tok::code_completion)) {
2597fe6060f1SDimitry Andric         cutOffParsing();
25980b57cec5SDimitry Andric         Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
25990b57cec5SDimitry Andric         Actions.FinalizeDeclaration(ThisDecl);
26000b57cec5SDimitry Andric         return nullptr;
26010b57cec5SDimitry Andric       }
26020b57cec5SDimitry Andric 
26030b57cec5SDimitry Andric       PreferredType.enterVariableInit(Tok.getLocation(), ThisDecl);
26040b57cec5SDimitry Andric       ExprResult Init = ParseInitializer();
26050b57cec5SDimitry Andric 
26060b57cec5SDimitry Andric       // If this is the only decl in (possibly) range based for statement,
26070b57cec5SDimitry Andric       // our best guess is that the user meant ':' instead of '='.
26080b57cec5SDimitry Andric       if (Tok.is(tok::r_paren) && FRI && D.isFirstDeclarator()) {
26090b57cec5SDimitry Andric         Diag(EqualLoc, diag::err_single_decl_assign_in_for_range)
26100b57cec5SDimitry Andric             << FixItHint::CreateReplacement(EqualLoc, ":");
26110b57cec5SDimitry Andric         // We are trying to stop parser from looking for ';' in this for
26120b57cec5SDimitry Andric         // statement, therefore preventing spurious errors to be issued.
26130b57cec5SDimitry Andric         FRI->ColonLoc = EqualLoc;
26140b57cec5SDimitry Andric         Init = ExprError();
26150b57cec5SDimitry Andric         FRI->RangeExpr = Init;
26160b57cec5SDimitry Andric       }
26170b57cec5SDimitry Andric 
26180b57cec5SDimitry Andric       InitScope.pop();
26190b57cec5SDimitry Andric 
26200b57cec5SDimitry Andric       if (Init.isInvalid()) {
26210b57cec5SDimitry Andric         SmallVector<tok::TokenKind, 2> StopTokens;
26220b57cec5SDimitry Andric         StopTokens.push_back(tok::comma);
2623e8d8bef9SDimitry Andric         if (D.getContext() == DeclaratorContext::ForInit ||
2624e8d8bef9SDimitry Andric             D.getContext() == DeclaratorContext::SelectionInit)
26250b57cec5SDimitry Andric           StopTokens.push_back(tok::r_paren);
26260b57cec5SDimitry Andric         SkipUntil(StopTokens, StopAtSemi | StopBeforeMatch);
26270b57cec5SDimitry Andric         Actions.ActOnInitializerError(ThisDecl);
26280b57cec5SDimitry Andric       } else
26290b57cec5SDimitry Andric         Actions.AddInitializerToDecl(ThisDecl, Init.get(),
26300b57cec5SDimitry Andric                                      /*DirectInit=*/false);
26310b57cec5SDimitry Andric     }
2632e8d8bef9SDimitry Andric     break;
2633e8d8bef9SDimitry Andric   }
2634e8d8bef9SDimitry Andric   case InitKind::CXXDirect: {
26350b57cec5SDimitry Andric     // Parse C++ direct initializer: '(' expression-list ')'
26360b57cec5SDimitry Andric     BalancedDelimiterTracker T(*this, tok::l_paren);
26370b57cec5SDimitry Andric     T.consumeOpen();
26380b57cec5SDimitry Andric 
26390b57cec5SDimitry Andric     ExprVector Exprs;
26400b57cec5SDimitry Andric 
26410b57cec5SDimitry Andric     InitializerScopeRAII InitScope(*this, D, ThisDecl);
26420b57cec5SDimitry Andric 
26430b57cec5SDimitry Andric     auto ThisVarDecl = dyn_cast_or_null<VarDecl>(ThisDecl);
26440b57cec5SDimitry Andric     auto RunSignatureHelp = [&]() {
26450b57cec5SDimitry Andric       QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
264604eeddc0SDimitry Andric           ThisVarDecl->getType()->getCanonicalTypeInternal(),
264704eeddc0SDimitry Andric           ThisDecl->getLocation(), Exprs, T.getOpenLocation(),
264804eeddc0SDimitry Andric           /*Braced=*/false);
26490b57cec5SDimitry Andric       CalledSignatureHelp = true;
26500b57cec5SDimitry Andric       return PreferredType;
26510b57cec5SDimitry Andric     };
26520b57cec5SDimitry Andric     auto SetPreferredType = [&] {
26530b57cec5SDimitry Andric       PreferredType.enterFunctionArgument(Tok.getLocation(), RunSignatureHelp);
26540b57cec5SDimitry Andric     };
26550b57cec5SDimitry Andric 
26560b57cec5SDimitry Andric     llvm::function_ref<void()> ExpressionStarts;
26570b57cec5SDimitry Andric     if (ThisVarDecl) {
26580b57cec5SDimitry Andric       // ParseExpressionList can sometimes succeed even when ThisDecl is not
26590b57cec5SDimitry Andric       // VarDecl. This is an error and it is reported in a call to
26600b57cec5SDimitry Andric       // Actions.ActOnInitializerError(). However, we call
26610b57cec5SDimitry Andric       // ProduceConstructorSignatureHelp only on VarDecls.
26620b57cec5SDimitry Andric       ExpressionStarts = SetPreferredType;
26630b57cec5SDimitry Andric     }
2664*297eecfbSDimitry Andric 
2665*297eecfbSDimitry Andric     bool SawError = ParseExpressionList(Exprs, ExpressionStarts);
2666*297eecfbSDimitry Andric 
2667*297eecfbSDimitry Andric     InitScope.pop();
2668*297eecfbSDimitry Andric 
2669*297eecfbSDimitry Andric     if (SawError) {
26700b57cec5SDimitry Andric       if (ThisVarDecl && PP.isCodeCompletionReached() && !CalledSignatureHelp) {
26710b57cec5SDimitry Andric         Actions.ProduceConstructorSignatureHelp(
267204eeddc0SDimitry Andric             ThisVarDecl->getType()->getCanonicalTypeInternal(),
267304eeddc0SDimitry Andric             ThisDecl->getLocation(), Exprs, T.getOpenLocation(),
267404eeddc0SDimitry Andric             /*Braced=*/false);
26750b57cec5SDimitry Andric         CalledSignatureHelp = true;
26760b57cec5SDimitry Andric       }
26770b57cec5SDimitry Andric       Actions.ActOnInitializerError(ThisDecl);
26780b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
26790b57cec5SDimitry Andric     } else {
26800b57cec5SDimitry Andric       // Match the ')'.
26810b57cec5SDimitry Andric       T.consumeClose();
26820b57cec5SDimitry Andric 
26830b57cec5SDimitry Andric       ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
26840b57cec5SDimitry Andric                                                           T.getCloseLocation(),
26850b57cec5SDimitry Andric                                                           Exprs);
26860b57cec5SDimitry Andric       Actions.AddInitializerToDecl(ThisDecl, Initializer.get(),
26870b57cec5SDimitry Andric                                    /*DirectInit=*/true);
26880b57cec5SDimitry Andric     }
2689e8d8bef9SDimitry Andric     break;
2690e8d8bef9SDimitry Andric   }
2691e8d8bef9SDimitry Andric   case InitKind::CXXBraced: {
26920b57cec5SDimitry Andric     // Parse C++0x braced-init-list.
26930b57cec5SDimitry Andric     Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
26940b57cec5SDimitry Andric 
26950b57cec5SDimitry Andric     InitializerScopeRAII InitScope(*this, D, ThisDecl);
26960b57cec5SDimitry Andric 
26975ffd83dbSDimitry Andric     PreferredType.enterVariableInit(Tok.getLocation(), ThisDecl);
26980b57cec5SDimitry Andric     ExprResult Init(ParseBraceInitializer());
26990b57cec5SDimitry Andric 
27000b57cec5SDimitry Andric     InitScope.pop();
27010b57cec5SDimitry Andric 
27020b57cec5SDimitry Andric     if (Init.isInvalid()) {
27030b57cec5SDimitry Andric       Actions.ActOnInitializerError(ThisDecl);
27040b57cec5SDimitry Andric     } else
27050b57cec5SDimitry Andric       Actions.AddInitializerToDecl(ThisDecl, Init.get(), /*DirectInit=*/true);
2706e8d8bef9SDimitry Andric     break;
2707e8d8bef9SDimitry Andric   }
2708e8d8bef9SDimitry Andric   case InitKind::Uninitialized: {
27090b57cec5SDimitry Andric     Actions.ActOnUninitializedDecl(ThisDecl);
2710e8d8bef9SDimitry Andric     break;
2711e8d8bef9SDimitry Andric   }
27120b57cec5SDimitry Andric   }
27130b57cec5SDimitry Andric 
27140b57cec5SDimitry Andric   Actions.FinalizeDeclaration(ThisDecl);
2715e8d8bef9SDimitry Andric   return OuterDecl ? OuterDecl : ThisDecl;
27160b57cec5SDimitry Andric }
27170b57cec5SDimitry Andric 
27180b57cec5SDimitry Andric /// ParseSpecifierQualifierList
27190b57cec5SDimitry Andric ///        specifier-qualifier-list:
27200b57cec5SDimitry Andric ///          type-specifier specifier-qualifier-list[opt]
27210b57cec5SDimitry Andric ///          type-qualifier specifier-qualifier-list[opt]
27220b57cec5SDimitry Andric /// [GNU]    attributes     specifier-qualifier-list[opt]
27230b57cec5SDimitry Andric ///
2724bdd1243dSDimitry Andric void Parser::ParseSpecifierQualifierList(
2725bdd1243dSDimitry Andric     DeclSpec &DS, ImplicitTypenameContext AllowImplicitTypename,
2726bdd1243dSDimitry Andric     AccessSpecifier AS, DeclSpecContext DSC) {
27270b57cec5SDimitry Andric   /// specifier-qualifier-list is a subset of declaration-specifiers.  Just
27280b57cec5SDimitry Andric   /// parse declaration-specifiers and complain about extra stuff.
27290b57cec5SDimitry Andric   /// TODO: diagnose attribute-specifiers and alignment-specifiers.
2730bdd1243dSDimitry Andric   ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC, nullptr,
2731bdd1243dSDimitry Andric                              AllowImplicitTypename);
27320b57cec5SDimitry Andric 
27330b57cec5SDimitry Andric   // Validate declspec for type-name.
27340b57cec5SDimitry Andric   unsigned Specs = DS.getParsedSpecifiers();
27350b57cec5SDimitry Andric   if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) {
27360b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_type);
27370b57cec5SDimitry Andric     DS.SetTypeSpecError();
27380b57cec5SDimitry Andric   } else if (Specs == DeclSpec::PQ_None && !DS.hasAttributes()) {
27390b57cec5SDimitry Andric     Diag(Tok, diag::err_typename_requires_specqual);
27400b57cec5SDimitry Andric     if (!DS.hasTypeSpecifier())
27410b57cec5SDimitry Andric       DS.SetTypeSpecError();
27420b57cec5SDimitry Andric   }
27430b57cec5SDimitry Andric 
27440b57cec5SDimitry Andric   // Issue diagnostic and remove storage class if present.
27450b57cec5SDimitry Andric   if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
27460b57cec5SDimitry Andric     if (DS.getStorageClassSpecLoc().isValid())
27470b57cec5SDimitry Andric       Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
27480b57cec5SDimitry Andric     else
27490b57cec5SDimitry Andric       Diag(DS.getThreadStorageClassSpecLoc(),
27500b57cec5SDimitry Andric            diag::err_typename_invalid_storageclass);
27510b57cec5SDimitry Andric     DS.ClearStorageClassSpecs();
27520b57cec5SDimitry Andric   }
27530b57cec5SDimitry Andric 
27540b57cec5SDimitry Andric   // Issue diagnostic and remove function specifier if present.
27550b57cec5SDimitry Andric   if (Specs & DeclSpec::PQ_FunctionSpecifier) {
27560b57cec5SDimitry Andric     if (DS.isInlineSpecified())
27570b57cec5SDimitry Andric       Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
27580b57cec5SDimitry Andric     if (DS.isVirtualSpecified())
27590b57cec5SDimitry Andric       Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
27600b57cec5SDimitry Andric     if (DS.hasExplicitSpecifier())
27610b57cec5SDimitry Andric       Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
2762bdd1243dSDimitry Andric     if (DS.isNoreturnSpecified())
2763bdd1243dSDimitry Andric       Diag(DS.getNoreturnSpecLoc(), diag::err_typename_invalid_functionspec);
27640b57cec5SDimitry Andric     DS.ClearFunctionSpecs();
27650b57cec5SDimitry Andric   }
27660b57cec5SDimitry Andric 
27670b57cec5SDimitry Andric   // Issue diagnostic and remove constexpr specifier if present.
27680b57cec5SDimitry Andric   if (DS.hasConstexprSpecifier() && DSC != DeclSpecContext::DSC_condition) {
27690b57cec5SDimitry Andric     Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr)
2770e8d8bef9SDimitry Andric         << static_cast<int>(DS.getConstexprSpecifier());
27710b57cec5SDimitry Andric     DS.ClearConstexprSpec();
27720b57cec5SDimitry Andric   }
27730b57cec5SDimitry Andric }
27740b57cec5SDimitry Andric 
27750b57cec5SDimitry Andric /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
27760b57cec5SDimitry Andric /// specified token is valid after the identifier in a declarator which
27770b57cec5SDimitry Andric /// immediately follows the declspec.  For example, these things are valid:
27780b57cec5SDimitry Andric ///
27790b57cec5SDimitry Andric ///      int x   [             4];         // direct-declarator
27800b57cec5SDimitry Andric ///      int x   (             int y);     // direct-declarator
27810b57cec5SDimitry Andric ///  int(int x   )                         // direct-declarator
27820b57cec5SDimitry Andric ///      int x   ;                         // simple-declaration
27830b57cec5SDimitry Andric ///      int x   =             17;         // init-declarator-list
27840b57cec5SDimitry Andric ///      int x   ,             y;          // init-declarator-list
27850b57cec5SDimitry Andric ///      int x   __asm__       ("foo");    // init-declarator-list
27860b57cec5SDimitry Andric ///      int x   :             4;          // struct-declarator
27870b57cec5SDimitry Andric ///      int x   {             5};         // C++'0x unified initializers
27880b57cec5SDimitry Andric ///
27890b57cec5SDimitry Andric /// This is not, because 'x' does not immediately follow the declspec (though
27900b57cec5SDimitry Andric /// ')' happens to be valid anyway).
27910b57cec5SDimitry Andric ///    int (x)
27920b57cec5SDimitry Andric ///
27930b57cec5SDimitry Andric static bool isValidAfterIdentifierInDeclarator(const Token &T) {
27940b57cec5SDimitry Andric   return T.isOneOf(tok::l_square, tok::l_paren, tok::r_paren, tok::semi,
27950b57cec5SDimitry Andric                    tok::comma, tok::equal, tok::kw_asm, tok::l_brace,
27960b57cec5SDimitry Andric                    tok::colon);
27970b57cec5SDimitry Andric }
27980b57cec5SDimitry Andric 
27990b57cec5SDimitry Andric /// ParseImplicitInt - This method is called when we have an non-typename
28000b57cec5SDimitry Andric /// identifier in a declspec (which normally terminates the decl spec) when
28010b57cec5SDimitry Andric /// the declspec has no type specifier.  In this case, the declspec is either
28020b57cec5SDimitry Andric /// malformed or is "implicit int" (in K&R and C89).
28030b57cec5SDimitry Andric ///
28040b57cec5SDimitry Andric /// This method handles diagnosing this prettily and returns false if the
28050b57cec5SDimitry Andric /// declspec is done being processed.  If it recovers and thinks there may be
28060b57cec5SDimitry Andric /// other pieces of declspec after it, it returns true.
28070b57cec5SDimitry Andric ///
28080b57cec5SDimitry Andric bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
28090b57cec5SDimitry Andric                               const ParsedTemplateInfo &TemplateInfo,
28100b57cec5SDimitry Andric                               AccessSpecifier AS, DeclSpecContext DSC,
281181ad6265SDimitry Andric                               ParsedAttributes &Attrs) {
28120b57cec5SDimitry Andric   assert(Tok.is(tok::identifier) && "should have identifier");
28130b57cec5SDimitry Andric 
28140b57cec5SDimitry Andric   SourceLocation Loc = Tok.getLocation();
28150b57cec5SDimitry Andric   // If we see an identifier that is not a type name, we normally would
28160b57cec5SDimitry Andric   // parse it as the identifier being declared.  However, when a typename
28170b57cec5SDimitry Andric   // is typo'd or the definition is not included, this will incorrectly
28180b57cec5SDimitry Andric   // parse the typename as the identifier name and fall over misparsing
28190b57cec5SDimitry Andric   // later parts of the diagnostic.
28200b57cec5SDimitry Andric   //
28210b57cec5SDimitry Andric   // As such, we try to do some look-ahead in cases where this would
28220b57cec5SDimitry Andric   // otherwise be an "implicit-int" case to see if this is invalid.  For
28230b57cec5SDimitry Andric   // example: "static foo_t x = 4;"  In this case, if we parsed foo_t as
28240b57cec5SDimitry Andric   // an identifier with implicit int, we'd get a parse error because the
28250b57cec5SDimitry Andric   // next token is obviously invalid for a type.  Parse these as a case
28260b57cec5SDimitry Andric   // with an invalid type specifier.
28270b57cec5SDimitry Andric   assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
28280b57cec5SDimitry Andric 
28290b57cec5SDimitry Andric   // Since we know that this either implicit int (which is rare) or an
28300b57cec5SDimitry Andric   // error, do lookahead to try to do better recovery. This never applies
28310b57cec5SDimitry Andric   // within a type specifier. Outside of C++, we allow this even if the
28320b57cec5SDimitry Andric   // language doesn't "officially" support implicit int -- we support
283381ad6265SDimitry Andric   // implicit int as an extension in some language modes.
283481ad6265SDimitry Andric   if (!isTypeSpecifier(DSC) && getLangOpts().isImplicitIntAllowed() &&
28350b57cec5SDimitry Andric       isValidAfterIdentifierInDeclarator(NextToken())) {
28360b57cec5SDimitry Andric     // If this token is valid for implicit int, e.g. "static x = 4", then
28370b57cec5SDimitry Andric     // we just avoid eating the identifier, so it will be parsed as the
28380b57cec5SDimitry Andric     // identifier in the declarator.
28390b57cec5SDimitry Andric     return false;
28400b57cec5SDimitry Andric   }
28410b57cec5SDimitry Andric 
28420b57cec5SDimitry Andric   // Early exit as Sema has a dedicated missing_actual_pipe_type diagnostic
28430b57cec5SDimitry Andric   // for incomplete declarations such as `pipe p`.
28440b57cec5SDimitry Andric   if (getLangOpts().OpenCLCPlusPlus && DS.isTypeSpecPipe())
28450b57cec5SDimitry Andric     return false;
28460b57cec5SDimitry Andric 
28470b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus &&
28480b57cec5SDimitry Andric       DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
28490b57cec5SDimitry Andric     // Don't require a type specifier if we have the 'auto' storage class
28500b57cec5SDimitry Andric     // specifier in C++98 -- we'll promote it to a type specifier.
28510b57cec5SDimitry Andric     if (SS)
28520b57cec5SDimitry Andric       AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
28530b57cec5SDimitry Andric     return false;
28540b57cec5SDimitry Andric   }
28550b57cec5SDimitry Andric 
28560b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus && (!SS || SS->isEmpty()) &&
28570b57cec5SDimitry Andric       getLangOpts().MSVCCompat) {
28580b57cec5SDimitry Andric     // Lookup of an unqualified type name has failed in MSVC compatibility mode.
28590b57cec5SDimitry Andric     // Give Sema a chance to recover if we are in a template with dependent base
28600b57cec5SDimitry Andric     // classes.
28610b57cec5SDimitry Andric     if (ParsedType T = Actions.ActOnMSVCUnknownTypeName(
28620b57cec5SDimitry Andric             *Tok.getIdentifierInfo(), Tok.getLocation(),
28630b57cec5SDimitry Andric             DSC == DeclSpecContext::DSC_template_type_arg)) {
28640b57cec5SDimitry Andric       const char *PrevSpec;
28650b57cec5SDimitry Andric       unsigned DiagID;
28660b57cec5SDimitry Andric       DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
28670b57cec5SDimitry Andric                          Actions.getASTContext().getPrintingPolicy());
28680b57cec5SDimitry Andric       DS.SetRangeEnd(Tok.getLocation());
28690b57cec5SDimitry Andric       ConsumeToken();
28700b57cec5SDimitry Andric       return false;
28710b57cec5SDimitry Andric     }
28720b57cec5SDimitry Andric   }
28730b57cec5SDimitry Andric 
28740b57cec5SDimitry Andric   // Otherwise, if we don't consume this token, we are going to emit an
28750b57cec5SDimitry Andric   // error anyway.  Try to recover from various common problems.  Check
28760b57cec5SDimitry Andric   // to see if this was a reference to a tag name without a tag specified.
28770b57cec5SDimitry Andric   // This is a common problem in C (saying 'foo' instead of 'struct foo').
28780b57cec5SDimitry Andric   //
28790b57cec5SDimitry Andric   // C++ doesn't need this, and isTagName doesn't take SS.
28800b57cec5SDimitry Andric   if (SS == nullptr) {
28810b57cec5SDimitry Andric     const char *TagName = nullptr, *FixitTagName = nullptr;
28820b57cec5SDimitry Andric     tok::TokenKind TagKind = tok::unknown;
28830b57cec5SDimitry Andric 
28840b57cec5SDimitry Andric     switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
28850b57cec5SDimitry Andric       default: break;
28860b57cec5SDimitry Andric       case DeclSpec::TST_enum:
28870b57cec5SDimitry Andric         TagName="enum"  ; FixitTagName = "enum "  ; TagKind=tok::kw_enum ;break;
28880b57cec5SDimitry Andric       case DeclSpec::TST_union:
28890b57cec5SDimitry Andric         TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
28900b57cec5SDimitry Andric       case DeclSpec::TST_struct:
28910b57cec5SDimitry Andric         TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
28920b57cec5SDimitry Andric       case DeclSpec::TST_interface:
28930b57cec5SDimitry Andric         TagName="__interface"; FixitTagName = "__interface ";
28940b57cec5SDimitry Andric         TagKind=tok::kw___interface;break;
28950b57cec5SDimitry Andric       case DeclSpec::TST_class:
28960b57cec5SDimitry Andric         TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
28970b57cec5SDimitry Andric     }
28980b57cec5SDimitry Andric 
28990b57cec5SDimitry Andric     if (TagName) {
29000b57cec5SDimitry Andric       IdentifierInfo *TokenName = Tok.getIdentifierInfo();
29010b57cec5SDimitry Andric       LookupResult R(Actions, TokenName, SourceLocation(),
29020b57cec5SDimitry Andric                      Sema::LookupOrdinaryName);
29030b57cec5SDimitry Andric 
29040b57cec5SDimitry Andric       Diag(Loc, diag::err_use_of_tag_name_without_tag)
29050b57cec5SDimitry Andric         << TokenName << TagName << getLangOpts().CPlusPlus
29060b57cec5SDimitry Andric         << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
29070b57cec5SDimitry Andric 
29080b57cec5SDimitry Andric       if (Actions.LookupParsedName(R, getCurScope(), SS)) {
29090b57cec5SDimitry Andric         for (LookupResult::iterator I = R.begin(), IEnd = R.end();
29100b57cec5SDimitry Andric              I != IEnd; ++I)
29110b57cec5SDimitry Andric           Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
29120b57cec5SDimitry Andric             << TokenName << TagName;
29130b57cec5SDimitry Andric       }
29140b57cec5SDimitry Andric 
29150b57cec5SDimitry Andric       // Parse this as a tag as if the missing tag were present.
29160b57cec5SDimitry Andric       if (TagKind == tok::kw_enum)
29170b57cec5SDimitry Andric         ParseEnumSpecifier(Loc, DS, TemplateInfo, AS,
29180b57cec5SDimitry Andric                            DeclSpecContext::DSC_normal);
29190b57cec5SDimitry Andric       else
29200b57cec5SDimitry Andric         ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
29210b57cec5SDimitry Andric                             /*EnteringContext*/ false,
29220b57cec5SDimitry Andric                             DeclSpecContext::DSC_normal, Attrs);
29230b57cec5SDimitry Andric       return true;
29240b57cec5SDimitry Andric     }
29250b57cec5SDimitry Andric   }
29260b57cec5SDimitry Andric 
29270b57cec5SDimitry Andric   // Determine whether this identifier could plausibly be the name of something
29280b57cec5SDimitry Andric   // being declared (with a missing type).
29290b57cec5SDimitry Andric   if (!isTypeSpecifier(DSC) && (!SS || DSC == DeclSpecContext::DSC_top_level ||
29300b57cec5SDimitry Andric                                 DSC == DeclSpecContext::DSC_class)) {
29310b57cec5SDimitry Andric     // Look ahead to the next token to try to figure out what this declaration
29320b57cec5SDimitry Andric     // was supposed to be.
29330b57cec5SDimitry Andric     switch (NextToken().getKind()) {
29340b57cec5SDimitry Andric     case tok::l_paren: {
29350b57cec5SDimitry Andric       // static x(4); // 'x' is not a type
29360b57cec5SDimitry Andric       // x(int n);    // 'x' is not a type
29370b57cec5SDimitry Andric       // x (*p)[];    // 'x' is a type
29380b57cec5SDimitry Andric       //
29390b57cec5SDimitry Andric       // Since we're in an error case, we can afford to perform a tentative
29400b57cec5SDimitry Andric       // parse to determine which case we're in.
29410b57cec5SDimitry Andric       TentativeParsingAction PA(*this);
29420b57cec5SDimitry Andric       ConsumeToken();
29430b57cec5SDimitry Andric       TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
29440b57cec5SDimitry Andric       PA.Revert();
29450b57cec5SDimitry Andric 
29460b57cec5SDimitry Andric       if (TPR != TPResult::False) {
29470b57cec5SDimitry Andric         // The identifier is followed by a parenthesized declarator.
29480b57cec5SDimitry Andric         // It's supposed to be a type.
29490b57cec5SDimitry Andric         break;
29500b57cec5SDimitry Andric       }
29510b57cec5SDimitry Andric 
29520b57cec5SDimitry Andric       // If we're in a context where we could be declaring a constructor,
29530b57cec5SDimitry Andric       // check whether this is a constructor declaration with a bogus name.
29540b57cec5SDimitry Andric       if (DSC == DeclSpecContext::DSC_class ||
29550b57cec5SDimitry Andric           (DSC == DeclSpecContext::DSC_top_level && SS)) {
29560b57cec5SDimitry Andric         IdentifierInfo *II = Tok.getIdentifierInfo();
29570b57cec5SDimitry Andric         if (Actions.isCurrentClassNameTypo(II, SS)) {
29580b57cec5SDimitry Andric           Diag(Loc, diag::err_constructor_bad_name)
29590b57cec5SDimitry Andric             << Tok.getIdentifierInfo() << II
29600b57cec5SDimitry Andric             << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
29610b57cec5SDimitry Andric           Tok.setIdentifierInfo(II);
29620b57cec5SDimitry Andric         }
29630b57cec5SDimitry Andric       }
29640b57cec5SDimitry Andric       // Fall through.
2965bdd1243dSDimitry Andric       [[fallthrough]];
29660b57cec5SDimitry Andric     }
29670b57cec5SDimitry Andric     case tok::comma:
29680b57cec5SDimitry Andric     case tok::equal:
29690b57cec5SDimitry Andric     case tok::kw_asm:
29700b57cec5SDimitry Andric     case tok::l_brace:
29710b57cec5SDimitry Andric     case tok::l_square:
29720b57cec5SDimitry Andric     case tok::semi:
29730b57cec5SDimitry Andric       // This looks like a variable or function declaration. The type is
29740b57cec5SDimitry Andric       // probably missing. We're done parsing decl-specifiers.
29750b57cec5SDimitry Andric       // But only if we are not in a function prototype scope.
29760b57cec5SDimitry Andric       if (getCurScope()->isFunctionPrototypeScope())
29770b57cec5SDimitry Andric         break;
29780b57cec5SDimitry Andric       if (SS)
29790b57cec5SDimitry Andric         AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
29800b57cec5SDimitry Andric       return false;
29810b57cec5SDimitry Andric 
29820b57cec5SDimitry Andric     default:
29830b57cec5SDimitry Andric       // This is probably supposed to be a type. This includes cases like:
29840b57cec5SDimitry Andric       //   int f(itn);
29855ffd83dbSDimitry Andric       //   struct S { unsigned : 4; };
29860b57cec5SDimitry Andric       break;
29870b57cec5SDimitry Andric     }
29880b57cec5SDimitry Andric   }
29890b57cec5SDimitry Andric 
29900b57cec5SDimitry Andric   // This is almost certainly an invalid type name. Let Sema emit a diagnostic
29910b57cec5SDimitry Andric   // and attempt to recover.
29920b57cec5SDimitry Andric   ParsedType T;
29930b57cec5SDimitry Andric   IdentifierInfo *II = Tok.getIdentifierInfo();
29940b57cec5SDimitry Andric   bool IsTemplateName = getLangOpts().CPlusPlus && NextToken().is(tok::less);
29950b57cec5SDimitry Andric   Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T,
29960b57cec5SDimitry Andric                                   IsTemplateName);
29970b57cec5SDimitry Andric   if (T) {
29980b57cec5SDimitry Andric     // The action has suggested that the type T could be used. Set that as
29990b57cec5SDimitry Andric     // the type in the declaration specifiers, consume the would-be type
30000b57cec5SDimitry Andric     // name token, and we're done.
30010b57cec5SDimitry Andric     const char *PrevSpec;
30020b57cec5SDimitry Andric     unsigned DiagID;
30030b57cec5SDimitry Andric     DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
30040b57cec5SDimitry Andric                        Actions.getASTContext().getPrintingPolicy());
30050b57cec5SDimitry Andric     DS.SetRangeEnd(Tok.getLocation());
30060b57cec5SDimitry Andric     ConsumeToken();
30070b57cec5SDimitry Andric     // There may be other declaration specifiers after this.
30080b57cec5SDimitry Andric     return true;
30090b57cec5SDimitry Andric   } else if (II != Tok.getIdentifierInfo()) {
30100b57cec5SDimitry Andric     // If no type was suggested, the correction is to a keyword
30110b57cec5SDimitry Andric     Tok.setKind(II->getTokenID());
30120b57cec5SDimitry Andric     // There may be other declaration specifiers after this.
30130b57cec5SDimitry Andric     return true;
30140b57cec5SDimitry Andric   }
30150b57cec5SDimitry Andric 
30160b57cec5SDimitry Andric   // Otherwise, the action had no suggestion for us.  Mark this as an error.
30170b57cec5SDimitry Andric   DS.SetTypeSpecError();
30180b57cec5SDimitry Andric   DS.SetRangeEnd(Tok.getLocation());
30190b57cec5SDimitry Andric   ConsumeToken();
30200b57cec5SDimitry Andric 
30210b57cec5SDimitry Andric   // Eat any following template arguments.
30220b57cec5SDimitry Andric   if (IsTemplateName) {
30230b57cec5SDimitry Andric     SourceLocation LAngle, RAngle;
30240b57cec5SDimitry Andric     TemplateArgList Args;
30250b57cec5SDimitry Andric     ParseTemplateIdAfterTemplateName(true, LAngle, Args, RAngle);
30260b57cec5SDimitry Andric   }
30270b57cec5SDimitry Andric 
30280b57cec5SDimitry Andric   // TODO: Could inject an invalid typedef decl in an enclosing scope to
30290b57cec5SDimitry Andric   // avoid rippling error messages on subsequent uses of the same type,
30300b57cec5SDimitry Andric   // could be useful if #include was forgotten.
30310b57cec5SDimitry Andric   return true;
30320b57cec5SDimitry Andric }
30330b57cec5SDimitry Andric 
30340b57cec5SDimitry Andric /// Determine the declaration specifier context from the declarator
30350b57cec5SDimitry Andric /// context.
30360b57cec5SDimitry Andric ///
30370b57cec5SDimitry Andric /// \param Context the declarator context, which is one of the
30380b57cec5SDimitry Andric /// DeclaratorContext enumerator values.
30390b57cec5SDimitry Andric Parser::DeclSpecContext
30400b57cec5SDimitry Andric Parser::getDeclSpecContextFromDeclaratorContext(DeclaratorContext Context) {
3041bdd1243dSDimitry Andric   switch (Context) {
3042bdd1243dSDimitry Andric   case DeclaratorContext::Member:
30430b57cec5SDimitry Andric     return DeclSpecContext::DSC_class;
3044bdd1243dSDimitry Andric   case DeclaratorContext::File:
30450b57cec5SDimitry Andric     return DeclSpecContext::DSC_top_level;
3046bdd1243dSDimitry Andric   case DeclaratorContext::TemplateParam:
30470b57cec5SDimitry Andric     return DeclSpecContext::DSC_template_param;
3048bdd1243dSDimitry Andric   case DeclaratorContext::TemplateArg:
3049bdd1243dSDimitry Andric     return DeclSpecContext::DSC_template_arg;
3050bdd1243dSDimitry Andric   case DeclaratorContext::TemplateTypeArg:
30510b57cec5SDimitry Andric     return DeclSpecContext::DSC_template_type_arg;
3052bdd1243dSDimitry Andric   case DeclaratorContext::TrailingReturn:
3053bdd1243dSDimitry Andric   case DeclaratorContext::TrailingReturnVar:
30540b57cec5SDimitry Andric     return DeclSpecContext::DSC_trailing;
3055bdd1243dSDimitry Andric   case DeclaratorContext::AliasDecl:
3056bdd1243dSDimitry Andric   case DeclaratorContext::AliasTemplate:
30570b57cec5SDimitry Andric     return DeclSpecContext::DSC_alias_declaration;
3058bdd1243dSDimitry Andric   case DeclaratorContext::Association:
305981ad6265SDimitry Andric     return DeclSpecContext::DSC_association;
3060bdd1243dSDimitry Andric   case DeclaratorContext::TypeName:
3061bdd1243dSDimitry Andric     return DeclSpecContext::DSC_type_specifier;
3062bdd1243dSDimitry Andric   case DeclaratorContext::Condition:
3063bdd1243dSDimitry Andric     return DeclSpecContext::DSC_condition;
3064bdd1243dSDimitry Andric   case DeclaratorContext::ConversionId:
3065bdd1243dSDimitry Andric     return DeclSpecContext::DSC_conv_operator;
306606c3fb27SDimitry Andric   case DeclaratorContext::CXXNew:
306706c3fb27SDimitry Andric     return DeclSpecContext::DSC_new;
3068bdd1243dSDimitry Andric   case DeclaratorContext::Prototype:
3069bdd1243dSDimitry Andric   case DeclaratorContext::ObjCResult:
3070bdd1243dSDimitry Andric   case DeclaratorContext::ObjCParameter:
3071bdd1243dSDimitry Andric   case DeclaratorContext::KNRTypeList:
3072bdd1243dSDimitry Andric   case DeclaratorContext::FunctionalCast:
3073bdd1243dSDimitry Andric   case DeclaratorContext::Block:
3074bdd1243dSDimitry Andric   case DeclaratorContext::ForInit:
3075bdd1243dSDimitry Andric   case DeclaratorContext::SelectionInit:
3076bdd1243dSDimitry Andric   case DeclaratorContext::CXXCatch:
3077bdd1243dSDimitry Andric   case DeclaratorContext::ObjCCatch:
3078bdd1243dSDimitry Andric   case DeclaratorContext::BlockLiteral:
3079bdd1243dSDimitry Andric   case DeclaratorContext::LambdaExpr:
3080bdd1243dSDimitry Andric   case DeclaratorContext::LambdaExprParameter:
3081bdd1243dSDimitry Andric   case DeclaratorContext::RequiresExpr:
30820b57cec5SDimitry Andric     return DeclSpecContext::DSC_normal;
30830b57cec5SDimitry Andric   }
30840b57cec5SDimitry Andric 
3085bdd1243dSDimitry Andric   llvm_unreachable("Missing DeclaratorContext case");
3086bdd1243dSDimitry Andric }
3087bdd1243dSDimitry Andric 
30880b57cec5SDimitry Andric /// ParseAlignArgument - Parse the argument to an alignment-specifier.
30890b57cec5SDimitry Andric ///
30900b57cec5SDimitry Andric /// [C11]   type-id
30910b57cec5SDimitry Andric /// [C11]   constant-expression
30920b57cec5SDimitry Andric /// [C++0x] type-id ...[opt]
30930b57cec5SDimitry Andric /// [C++0x] assignment-expression ...[opt]
309406c3fb27SDimitry Andric ExprResult Parser::ParseAlignArgument(StringRef KWName, SourceLocation Start,
309506c3fb27SDimitry Andric                                       SourceLocation &EllipsisLoc, bool &IsType,
309606c3fb27SDimitry Andric                                       ParsedType &TypeResult) {
30970b57cec5SDimitry Andric   ExprResult ER;
30980b57cec5SDimitry Andric   if (isTypeIdInParens()) {
30990b57cec5SDimitry Andric     SourceLocation TypeLoc = Tok.getLocation();
31000b57cec5SDimitry Andric     ParsedType Ty = ParseTypeName().get();
31010b57cec5SDimitry Andric     SourceRange TypeRange(Start, Tok.getLocation());
310206c3fb27SDimitry Andric     if (Actions.ActOnAlignasTypeArgument(KWName, Ty, TypeLoc, TypeRange))
310306c3fb27SDimitry Andric       return ExprError();
310406c3fb27SDimitry Andric     TypeResult = Ty;
310506c3fb27SDimitry Andric     IsType = true;
310606c3fb27SDimitry Andric   } else {
31070b57cec5SDimitry Andric     ER = ParseConstantExpression();
310806c3fb27SDimitry Andric     IsType = false;
310906c3fb27SDimitry Andric   }
31100b57cec5SDimitry Andric 
31110b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus11)
31120b57cec5SDimitry Andric     TryConsumeToken(tok::ellipsis, EllipsisLoc);
31130b57cec5SDimitry Andric 
31140b57cec5SDimitry Andric   return ER;
31150b57cec5SDimitry Andric }
31160b57cec5SDimitry Andric 
31170b57cec5SDimitry Andric /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
31180b57cec5SDimitry Andric /// attribute to Attrs.
31190b57cec5SDimitry Andric ///
31200b57cec5SDimitry Andric /// alignment-specifier:
31210b57cec5SDimitry Andric /// [C11]   '_Alignas' '(' type-id ')'
31220b57cec5SDimitry Andric /// [C11]   '_Alignas' '(' constant-expression ')'
31230b57cec5SDimitry Andric /// [C++11] 'alignas' '(' type-id ...[opt] ')'
31240b57cec5SDimitry Andric /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
31250b57cec5SDimitry Andric void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
31260b57cec5SDimitry Andric                                      SourceLocation *EndLoc) {
31270b57cec5SDimitry Andric   assert(Tok.isOneOf(tok::kw_alignas, tok::kw__Alignas) &&
31280b57cec5SDimitry Andric          "Not an alignment-specifier!");
312906c3fb27SDimitry Andric   Token KWTok = Tok;
313006c3fb27SDimitry Andric   IdentifierInfo *KWName = KWTok.getIdentifierInfo();
313106c3fb27SDimitry Andric   auto Kind = KWTok.getKind();
31320b57cec5SDimitry Andric   SourceLocation KWLoc = ConsumeToken();
31330b57cec5SDimitry Andric 
31340b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
31350b57cec5SDimitry Andric   if (T.expectAndConsume())
31360b57cec5SDimitry Andric     return;
31370b57cec5SDimitry Andric 
313806c3fb27SDimitry Andric   bool IsType;
313906c3fb27SDimitry Andric   ParsedType TypeResult;
31400b57cec5SDimitry Andric   SourceLocation EllipsisLoc;
314106c3fb27SDimitry Andric   ExprResult ArgExpr =
314206c3fb27SDimitry Andric       ParseAlignArgument(PP.getSpelling(KWTok), T.getOpenLocation(),
314306c3fb27SDimitry Andric                          EllipsisLoc, IsType, TypeResult);
31440b57cec5SDimitry Andric   if (ArgExpr.isInvalid()) {
31450b57cec5SDimitry Andric     T.skipToEnd();
31460b57cec5SDimitry Andric     return;
31470b57cec5SDimitry Andric   }
31480b57cec5SDimitry Andric 
31490b57cec5SDimitry Andric   T.consumeClose();
31500b57cec5SDimitry Andric   if (EndLoc)
31510b57cec5SDimitry Andric     *EndLoc = T.getCloseLocation();
31520b57cec5SDimitry Andric 
315306c3fb27SDimitry Andric   if (IsType) {
315406c3fb27SDimitry Andric     Attrs.addNewTypeAttr(KWName, KWLoc, nullptr, KWLoc, TypeResult, Kind,
315506c3fb27SDimitry Andric                          EllipsisLoc);
315606c3fb27SDimitry Andric   } else {
31570b57cec5SDimitry Andric     ArgsVector ArgExprs;
31580b57cec5SDimitry Andric     ArgExprs.push_back(ArgExpr.get());
315906c3fb27SDimitry Andric     Attrs.addNew(KWName, KWLoc, nullptr, KWLoc, ArgExprs.data(), 1, Kind,
316006c3fb27SDimitry Andric                  EllipsisLoc);
316106c3fb27SDimitry Andric   }
31620b57cec5SDimitry Andric }
31630b57cec5SDimitry Andric 
31645ffd83dbSDimitry Andric ExprResult Parser::ParseExtIntegerArgument() {
31650eae32dcSDimitry Andric   assert(Tok.isOneOf(tok::kw__ExtInt, tok::kw__BitInt) &&
31660eae32dcSDimitry Andric          "Not an extended int type");
31675ffd83dbSDimitry Andric   ConsumeToken();
31685ffd83dbSDimitry Andric 
31695ffd83dbSDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
31705ffd83dbSDimitry Andric   if (T.expectAndConsume())
31715ffd83dbSDimitry Andric     return ExprError();
31725ffd83dbSDimitry Andric 
31735ffd83dbSDimitry Andric   ExprResult ER = ParseConstantExpression();
31745ffd83dbSDimitry Andric   if (ER.isInvalid()) {
31755ffd83dbSDimitry Andric     T.skipToEnd();
31765ffd83dbSDimitry Andric     return ExprError();
31775ffd83dbSDimitry Andric   }
31785ffd83dbSDimitry Andric 
31795ffd83dbSDimitry Andric   if(T.consumeClose())
31805ffd83dbSDimitry Andric     return ExprError();
31815ffd83dbSDimitry Andric   return ER;
31825ffd83dbSDimitry Andric }
31835ffd83dbSDimitry Andric 
31840b57cec5SDimitry Andric /// Determine whether we're looking at something that might be a declarator
31850b57cec5SDimitry Andric /// in a simple-declaration. If it can't possibly be a declarator, maybe
31860b57cec5SDimitry Andric /// diagnose a missing semicolon after a prior tag definition in the decl
31870b57cec5SDimitry Andric /// specifier.
31880b57cec5SDimitry Andric ///
31890b57cec5SDimitry Andric /// \return \c true if an error occurred and this can't be any kind of
31900b57cec5SDimitry Andric /// declaration.
31910b57cec5SDimitry Andric bool
31920b57cec5SDimitry Andric Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
31930b57cec5SDimitry Andric                                               DeclSpecContext DSContext,
31940b57cec5SDimitry Andric                                               LateParsedAttrList *LateAttrs) {
31950b57cec5SDimitry Andric   assert(DS.hasTagDefinition() && "shouldn't call this");
31960b57cec5SDimitry Andric 
31970b57cec5SDimitry Andric   bool EnteringContext = (DSContext == DeclSpecContext::DSC_class ||
31980b57cec5SDimitry Andric                           DSContext == DeclSpecContext::DSC_top_level);
31990b57cec5SDimitry Andric 
32000b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus &&
32010b57cec5SDimitry Andric       Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype,
32020b57cec5SDimitry Andric                   tok::annot_template_id) &&
32030b57cec5SDimitry Andric       TryAnnotateCXXScopeToken(EnteringContext)) {
32040b57cec5SDimitry Andric     SkipMalformedDecl();
32050b57cec5SDimitry Andric     return true;
32060b57cec5SDimitry Andric   }
32070b57cec5SDimitry Andric 
32080b57cec5SDimitry Andric   bool HasScope = Tok.is(tok::annot_cxxscope);
32090b57cec5SDimitry Andric   // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
32100b57cec5SDimitry Andric   Token AfterScope = HasScope ? NextToken() : Tok;
32110b57cec5SDimitry Andric 
32120b57cec5SDimitry Andric   // Determine whether the following tokens could possibly be a
32130b57cec5SDimitry Andric   // declarator.
32140b57cec5SDimitry Andric   bool MightBeDeclarator = true;
32150b57cec5SDimitry Andric   if (Tok.isOneOf(tok::kw_typename, tok::annot_typename)) {
32160b57cec5SDimitry Andric     // A declarator-id can't start with 'typename'.
32170b57cec5SDimitry Andric     MightBeDeclarator = false;
32180b57cec5SDimitry Andric   } else if (AfterScope.is(tok::annot_template_id)) {
32190b57cec5SDimitry Andric     // If we have a type expressed as a template-id, this cannot be a
32200b57cec5SDimitry Andric     // declarator-id (such a type cannot be redeclared in a simple-declaration).
32210b57cec5SDimitry Andric     TemplateIdAnnotation *Annot =
32220b57cec5SDimitry Andric         static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
32230b57cec5SDimitry Andric     if (Annot->Kind == TNK_Type_template)
32240b57cec5SDimitry Andric       MightBeDeclarator = false;
32250b57cec5SDimitry Andric   } else if (AfterScope.is(tok::identifier)) {
32260b57cec5SDimitry Andric     const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
32270b57cec5SDimitry Andric 
32280b57cec5SDimitry Andric     // These tokens cannot come after the declarator-id in a
32290b57cec5SDimitry Andric     // simple-declaration, and are likely to come after a type-specifier.
32300b57cec5SDimitry Andric     if (Next.isOneOf(tok::star, tok::amp, tok::ampamp, tok::identifier,
32310b57cec5SDimitry Andric                      tok::annot_cxxscope, tok::coloncolon)) {
32320b57cec5SDimitry Andric       // Missing a semicolon.
32330b57cec5SDimitry Andric       MightBeDeclarator = false;
32340b57cec5SDimitry Andric     } else if (HasScope) {
32350b57cec5SDimitry Andric       // If the declarator-id has a scope specifier, it must redeclare a
32360b57cec5SDimitry Andric       // previously-declared entity. If that's a type (and this is not a
32370b57cec5SDimitry Andric       // typedef), that's an error.
32380b57cec5SDimitry Andric       CXXScopeSpec SS;
32390b57cec5SDimitry Andric       Actions.RestoreNestedNameSpecifierAnnotation(
32400b57cec5SDimitry Andric           Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
32410b57cec5SDimitry Andric       IdentifierInfo *Name = AfterScope.getIdentifierInfo();
32420b57cec5SDimitry Andric       Sema::NameClassification Classification = Actions.ClassifyName(
32430b57cec5SDimitry Andric           getCurScope(), SS, Name, AfterScope.getLocation(), Next,
3244a7dea167SDimitry Andric           /*CCC=*/nullptr);
32450b57cec5SDimitry Andric       switch (Classification.getKind()) {
32460b57cec5SDimitry Andric       case Sema::NC_Error:
32470b57cec5SDimitry Andric         SkipMalformedDecl();
32480b57cec5SDimitry Andric         return true;
32490b57cec5SDimitry Andric 
32500b57cec5SDimitry Andric       case Sema::NC_Keyword:
3251a7dea167SDimitry Andric         llvm_unreachable("typo correction is not possible here");
32520b57cec5SDimitry Andric 
32530b57cec5SDimitry Andric       case Sema::NC_Type:
32540b57cec5SDimitry Andric       case Sema::NC_TypeTemplate:
3255a7dea167SDimitry Andric       case Sema::NC_UndeclaredNonType:
3256a7dea167SDimitry Andric       case Sema::NC_UndeclaredTemplate:
32570b57cec5SDimitry Andric         // Not a previously-declared non-type entity.
32580b57cec5SDimitry Andric         MightBeDeclarator = false;
32590b57cec5SDimitry Andric         break;
32600b57cec5SDimitry Andric 
32610b57cec5SDimitry Andric       case Sema::NC_Unknown:
3262a7dea167SDimitry Andric       case Sema::NC_NonType:
3263a7dea167SDimitry Andric       case Sema::NC_DependentNonType:
3264e8d8bef9SDimitry Andric       case Sema::NC_OverloadSet:
32650b57cec5SDimitry Andric       case Sema::NC_VarTemplate:
32660b57cec5SDimitry Andric       case Sema::NC_FunctionTemplate:
326755e4f9d5SDimitry Andric       case Sema::NC_Concept:
32680b57cec5SDimitry Andric         // Might be a redeclaration of a prior entity.
32690b57cec5SDimitry Andric         break;
32700b57cec5SDimitry Andric       }
32710b57cec5SDimitry Andric     }
32720b57cec5SDimitry Andric   }
32730b57cec5SDimitry Andric 
32740b57cec5SDimitry Andric   if (MightBeDeclarator)
32750b57cec5SDimitry Andric     return false;
32760b57cec5SDimitry Andric 
32770b57cec5SDimitry Andric   const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
32780b57cec5SDimitry Andric   Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getEndLoc()),
32790b57cec5SDimitry Andric        diag::err_expected_after)
32800b57cec5SDimitry Andric       << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi;
32810b57cec5SDimitry Andric 
32820b57cec5SDimitry Andric   // Try to recover from the typo, by dropping the tag definition and parsing
32830b57cec5SDimitry Andric   // the problematic tokens as a type.
32840b57cec5SDimitry Andric   //
32850b57cec5SDimitry Andric   // FIXME: Split the DeclSpec into pieces for the standalone
32860b57cec5SDimitry Andric   // declaration and pieces for the following declaration, instead
32870b57cec5SDimitry Andric   // of assuming that all the other pieces attach to new declaration,
32880b57cec5SDimitry Andric   // and call ParsedFreeStandingDeclSpec as appropriate.
32890b57cec5SDimitry Andric   DS.ClearTypeSpecType();
32900b57cec5SDimitry Andric   ParsedTemplateInfo NotATemplate;
32910b57cec5SDimitry Andric   ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
32920b57cec5SDimitry Andric   return false;
32930b57cec5SDimitry Andric }
32940b57cec5SDimitry Andric 
32950b57cec5SDimitry Andric /// ParseDeclarationSpecifiers
32960b57cec5SDimitry Andric ///       declaration-specifiers: [C99 6.7]
32970b57cec5SDimitry Andric ///         storage-class-specifier declaration-specifiers[opt]
32980b57cec5SDimitry Andric ///         type-specifier declaration-specifiers[opt]
32990b57cec5SDimitry Andric /// [C99]   function-specifier declaration-specifiers[opt]
33000b57cec5SDimitry Andric /// [C11]   alignment-specifier declaration-specifiers[opt]
33010b57cec5SDimitry Andric /// [GNU]   attributes declaration-specifiers[opt]
33020b57cec5SDimitry Andric /// [Clang] '__module_private__' declaration-specifiers[opt]
33030b57cec5SDimitry Andric /// [ObjC1] '__kindof' declaration-specifiers[opt]
33040b57cec5SDimitry Andric ///
33050b57cec5SDimitry Andric ///       storage-class-specifier: [C99 6.7.1]
33060b57cec5SDimitry Andric ///         'typedef'
33070b57cec5SDimitry Andric ///         'extern'
33080b57cec5SDimitry Andric ///         'static'
33090b57cec5SDimitry Andric ///         'auto'
33100b57cec5SDimitry Andric ///         'register'
33110b57cec5SDimitry Andric /// [C++]   'mutable'
33120b57cec5SDimitry Andric /// [C++11] 'thread_local'
33130b57cec5SDimitry Andric /// [C11]   '_Thread_local'
33140b57cec5SDimitry Andric /// [GNU]   '__thread'
33150b57cec5SDimitry Andric ///       function-specifier: [C99 6.7.4]
33160b57cec5SDimitry Andric /// [C99]   'inline'
33170b57cec5SDimitry Andric /// [C++]   'virtual'
33180b57cec5SDimitry Andric /// [C++]   'explicit'
33190b57cec5SDimitry Andric /// [OpenCL] '__kernel'
33200b57cec5SDimitry Andric ///       'friend': [C++ dcl.friend]
33210b57cec5SDimitry Andric ///       'constexpr': [C++0x dcl.constexpr]
3322bdd1243dSDimitry Andric void Parser::ParseDeclarationSpecifiers(
3323bdd1243dSDimitry Andric     DeclSpec &DS, const ParsedTemplateInfo &TemplateInfo, AccessSpecifier AS,
3324bdd1243dSDimitry Andric     DeclSpecContext DSContext, LateParsedAttrList *LateAttrs,
3325bdd1243dSDimitry Andric     ImplicitTypenameContext AllowImplicitTypename) {
33260b57cec5SDimitry Andric   if (DS.getSourceRange().isInvalid()) {
33270b57cec5SDimitry Andric     // Start the range at the current token but make the end of the range
33280b57cec5SDimitry Andric     // invalid.  This will make the entire range invalid unless we successfully
33290b57cec5SDimitry Andric     // consume a token.
33300b57cec5SDimitry Andric     DS.SetRangeStart(Tok.getLocation());
33310b57cec5SDimitry Andric     DS.SetRangeEnd(SourceLocation());
33320b57cec5SDimitry Andric   }
33330b57cec5SDimitry Andric 
3334bdd1243dSDimitry Andric   // If we are in a operator context, convert it back into a type specifier
3335bdd1243dSDimitry Andric   // context for better error handling later on.
3336bdd1243dSDimitry Andric   if (DSContext == DeclSpecContext::DSC_conv_operator) {
3337bdd1243dSDimitry Andric     // No implicit typename here.
3338bdd1243dSDimitry Andric     AllowImplicitTypename = ImplicitTypenameContext::No;
3339bdd1243dSDimitry Andric     DSContext = DeclSpecContext::DSC_type_specifier;
3340bdd1243dSDimitry Andric   }
3341bdd1243dSDimitry Andric 
33420b57cec5SDimitry Andric   bool EnteringContext = (DSContext == DeclSpecContext::DSC_class ||
33430b57cec5SDimitry Andric                           DSContext == DeclSpecContext::DSC_top_level);
33440b57cec5SDimitry Andric   bool AttrsLastTime = false;
334581ad6265SDimitry Andric   ParsedAttributes attrs(AttrFactory);
33460b57cec5SDimitry Andric   // We use Sema's policy to get bool macros right.
33470b57cec5SDimitry Andric   PrintingPolicy Policy = Actions.getPrintingPolicy();
334804eeddc0SDimitry Andric   while (true) {
33490b57cec5SDimitry Andric     bool isInvalid = false;
33500b57cec5SDimitry Andric     bool isStorageClass = false;
33510b57cec5SDimitry Andric     const char *PrevSpec = nullptr;
33520b57cec5SDimitry Andric     unsigned DiagID = 0;
33530b57cec5SDimitry Andric 
33540b57cec5SDimitry Andric     // This value needs to be set to the location of the last token if the last
33550b57cec5SDimitry Andric     // token of the specifier is already consumed.
33560b57cec5SDimitry Andric     SourceLocation ConsumedEnd;
33570b57cec5SDimitry Andric 
33580b57cec5SDimitry Andric     // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
33590b57cec5SDimitry Andric     // implementation for VS2013 uses _Atomic as an identifier for one of the
33600b57cec5SDimitry Andric     // classes in <atomic>.
33610b57cec5SDimitry Andric     //
33620b57cec5SDimitry Andric     // A typedef declaration containing _Atomic<...> is among the places where
33630b57cec5SDimitry Andric     // the class is used.  If we are currently parsing such a declaration, treat
33640b57cec5SDimitry Andric     // the token as an identifier.
33650b57cec5SDimitry Andric     if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) &&
33660b57cec5SDimitry Andric         DS.getStorageClassSpec() == clang::DeclSpec::SCS_typedef &&
33670b57cec5SDimitry Andric         !DS.hasTypeSpecifier() && GetLookAheadToken(1).is(tok::less))
33680b57cec5SDimitry Andric       Tok.setKind(tok::identifier);
33690b57cec5SDimitry Andric 
33700b57cec5SDimitry Andric     SourceLocation Loc = Tok.getLocation();
33710b57cec5SDimitry Andric 
3372fe6060f1SDimitry Andric     // Helper for image types in OpenCL.
3373fe6060f1SDimitry Andric     auto handleOpenCLImageKW = [&] (StringRef Ext, TypeSpecifierType ImageTypeSpec) {
3374fe6060f1SDimitry Andric       // Check if the image type is supported and otherwise turn the keyword into an identifier
3375fe6060f1SDimitry Andric       // because image types from extensions are not reserved identifiers.
3376fe6060f1SDimitry Andric       if (!StringRef(Ext).empty() && !getActions().getOpenCLOptions().isSupported(Ext, getLangOpts())) {
3377fe6060f1SDimitry Andric         Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
3378fe6060f1SDimitry Andric         Tok.setKind(tok::identifier);
3379fe6060f1SDimitry Andric         return false;
3380fe6060f1SDimitry Andric       }
3381fe6060f1SDimitry Andric       isInvalid = DS.SetTypeSpecType(ImageTypeSpec, Loc, PrevSpec, DiagID, Policy);
3382fe6060f1SDimitry Andric       return true;
3383fe6060f1SDimitry Andric     };
3384fe6060f1SDimitry Andric 
3385349cc55cSDimitry Andric     // Turn off usual access checking for template specializations and
3386349cc55cSDimitry Andric     // instantiations.
3387349cc55cSDimitry Andric     bool IsTemplateSpecOrInst =
3388349cc55cSDimitry Andric         (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3389349cc55cSDimitry Andric          TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3390349cc55cSDimitry Andric 
33910b57cec5SDimitry Andric     switch (Tok.getKind()) {
33920b57cec5SDimitry Andric     default:
339306c3fb27SDimitry Andric       if (Tok.isRegularKeywordAttribute())
339406c3fb27SDimitry Andric         goto Attribute;
339506c3fb27SDimitry Andric 
33960b57cec5SDimitry Andric     DoneWithDeclSpec:
33970b57cec5SDimitry Andric       if (!AttrsLastTime)
33980b57cec5SDimitry Andric         ProhibitAttributes(attrs);
33990b57cec5SDimitry Andric       else {
34005f757f3fSDimitry Andric         // Reject C++11 / C23 attributes that aren't type attributes.
340181ad6265SDimitry Andric         for (const ParsedAttr &PA : attrs) {
34025f757f3fSDimitry Andric           if (!PA.isCXX11Attribute() && !PA.isC23Attribute() &&
340306c3fb27SDimitry Andric               !PA.isRegularKeywordAttribute())
340481ad6265SDimitry Andric             continue;
340581ad6265SDimitry Andric           if (PA.getKind() == ParsedAttr::UnknownAttribute)
340681ad6265SDimitry Andric             // We will warn about the unknown attribute elsewhere (in
340781ad6265SDimitry Andric             // SemaDeclAttr.cpp)
340881ad6265SDimitry Andric             continue;
340981ad6265SDimitry Andric           // GCC ignores this attribute when placed on the DeclSpec in [[]]
341081ad6265SDimitry Andric           // syntax, so we do the same.
341181ad6265SDimitry Andric           if (PA.getKind() == ParsedAttr::AT_VectorSize) {
341281ad6265SDimitry Andric             Diag(PA.getLoc(), diag::warn_attribute_ignored) << PA;
341381ad6265SDimitry Andric             PA.setInvalid();
341481ad6265SDimitry Andric             continue;
341581ad6265SDimitry Andric           }
341681ad6265SDimitry Andric           // We reject AT_LifetimeBound and AT_AnyX86NoCfCheck, even though they
341781ad6265SDimitry Andric           // are type attributes, because we historically haven't allowed these
34185f757f3fSDimitry Andric           // to be used as type attributes in C++11 / C23 syntax.
341981ad6265SDimitry Andric           if (PA.isTypeAttr() && PA.getKind() != ParsedAttr::AT_LifetimeBound &&
342081ad6265SDimitry Andric               PA.getKind() != ParsedAttr::AT_AnyX86NoCfCheck)
342181ad6265SDimitry Andric             continue;
342206c3fb27SDimitry Andric           Diag(PA.getLoc(), diag::err_attribute_not_type_attr)
342306c3fb27SDimitry Andric               << PA << PA.isRegularKeywordAttribute();
342481ad6265SDimitry Andric           PA.setInvalid();
342581ad6265SDimitry Andric         }
34260b57cec5SDimitry Andric 
34270b57cec5SDimitry Andric         DS.takeAttributesFrom(attrs);
34280b57cec5SDimitry Andric       }
34290b57cec5SDimitry Andric 
34300b57cec5SDimitry Andric       // If this is not a declaration specifier token, we're done reading decl
34310b57cec5SDimitry Andric       // specifiers.  First verify that DeclSpec's are consistent.
34320b57cec5SDimitry Andric       DS.Finish(Actions, Policy);
34330b57cec5SDimitry Andric       return;
34340b57cec5SDimitry Andric 
34350b57cec5SDimitry Andric     case tok::l_square:
34360b57cec5SDimitry Andric     case tok::kw_alignas:
343706c3fb27SDimitry Andric       if (!isAllowedCXX11AttributeSpecifier())
34380b57cec5SDimitry Andric         goto DoneWithDeclSpec;
34390b57cec5SDimitry Andric 
344006c3fb27SDimitry Andric     Attribute:
34410b57cec5SDimitry Andric       ProhibitAttributes(attrs);
34420b57cec5SDimitry Andric       // FIXME: It would be good to recover by accepting the attributes,
34430b57cec5SDimitry Andric       //        but attempting to do that now would cause serious
34440b57cec5SDimitry Andric       //        madness in terms of diagnostics.
34450b57cec5SDimitry Andric       attrs.clear();
34460b57cec5SDimitry Andric       attrs.Range = SourceRange();
34470b57cec5SDimitry Andric 
34480b57cec5SDimitry Andric       ParseCXX11Attributes(attrs);
34490b57cec5SDimitry Andric       AttrsLastTime = true;
34500b57cec5SDimitry Andric       continue;
34510b57cec5SDimitry Andric 
34520b57cec5SDimitry Andric     case tok::code_completion: {
34530b57cec5SDimitry Andric       Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
34540b57cec5SDimitry Andric       if (DS.hasTypeSpecifier()) {
34550b57cec5SDimitry Andric         bool AllowNonIdentifiers
34560b57cec5SDimitry Andric           = (getCurScope()->getFlags() & (Scope::ControlScope |
34570b57cec5SDimitry Andric                                           Scope::BlockScope |
34580b57cec5SDimitry Andric                                           Scope::TemplateParamScope |
34590b57cec5SDimitry Andric                                           Scope::FunctionPrototypeScope |
34600b57cec5SDimitry Andric                                           Scope::AtCatchScope)) == 0;
34610b57cec5SDimitry Andric         bool AllowNestedNameSpecifiers
34620b57cec5SDimitry Andric           = DSContext == DeclSpecContext::DSC_top_level ||
34630b57cec5SDimitry Andric             (DSContext == DeclSpecContext::DSC_class && DS.isFriendSpecified());
34640b57cec5SDimitry Andric 
3465fe6060f1SDimitry Andric         cutOffParsing();
34660b57cec5SDimitry Andric         Actions.CodeCompleteDeclSpec(getCurScope(), DS,
34670b57cec5SDimitry Andric                                      AllowNonIdentifiers,
34680b57cec5SDimitry Andric                                      AllowNestedNameSpecifiers);
3469fe6060f1SDimitry Andric         return;
34700b57cec5SDimitry Andric       }
34710b57cec5SDimitry Andric 
3472bdd1243dSDimitry Andric       // Class context can appear inside a function/block, so prioritise that.
3473bdd1243dSDimitry Andric       if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
34740b57cec5SDimitry Andric         CCC = DSContext == DeclSpecContext::DSC_class ? Sema::PCC_MemberTemplate
34750b57cec5SDimitry Andric                                                       : Sema::PCC_Template;
34760b57cec5SDimitry Andric       else if (DSContext == DeclSpecContext::DSC_class)
34770b57cec5SDimitry Andric         CCC = Sema::PCC_Class;
3478bdd1243dSDimitry Andric       else if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
3479bdd1243dSDimitry Andric         CCC = Sema::PCC_LocalDeclarationSpecifiers;
34800b57cec5SDimitry Andric       else if (CurParsedObjCImpl)
34810b57cec5SDimitry Andric         CCC = Sema::PCC_ObjCImplementation;
34820b57cec5SDimitry Andric 
3483fe6060f1SDimitry Andric       cutOffParsing();
34840b57cec5SDimitry Andric       Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
3485fe6060f1SDimitry Andric       return;
34860b57cec5SDimitry Andric     }
34870b57cec5SDimitry Andric 
34880b57cec5SDimitry Andric     case tok::coloncolon: // ::foo::bar
34890b57cec5SDimitry Andric       // C++ scope specifier.  Annotate and loop, or bail out on error.
34901db9f3b2SDimitry Andric       if (getLangOpts().CPlusPlus &&
34911db9f3b2SDimitry Andric           TryAnnotateCXXScopeToken(EnteringContext)) {
34920b57cec5SDimitry Andric         if (!DS.hasTypeSpecifier())
34930b57cec5SDimitry Andric           DS.SetTypeSpecError();
34940b57cec5SDimitry Andric         goto DoneWithDeclSpec;
34950b57cec5SDimitry Andric       }
34960b57cec5SDimitry Andric       if (Tok.is(tok::coloncolon)) // ::new or ::delete
34970b57cec5SDimitry Andric         goto DoneWithDeclSpec;
34980b57cec5SDimitry Andric       continue;
34990b57cec5SDimitry Andric 
35000b57cec5SDimitry Andric     case tok::annot_cxxscope: {
35010b57cec5SDimitry Andric       if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
35020b57cec5SDimitry Andric         goto DoneWithDeclSpec;
35030b57cec5SDimitry Andric 
35040b57cec5SDimitry Andric       CXXScopeSpec SS;
350506c3fb27SDimitry Andric       if (TemplateInfo.TemplateParams)
350606c3fb27SDimitry Andric         SS.setTemplateParamLists(*TemplateInfo.TemplateParams);
35070b57cec5SDimitry Andric       Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
35080b57cec5SDimitry Andric                                                    Tok.getAnnotationRange(),
35090b57cec5SDimitry Andric                                                    SS);
35100b57cec5SDimitry Andric 
35110b57cec5SDimitry Andric       // We are looking for a qualified typename.
35120b57cec5SDimitry Andric       Token Next = NextToken();
35135ffd83dbSDimitry Andric 
35145ffd83dbSDimitry Andric       TemplateIdAnnotation *TemplateId = Next.is(tok::annot_template_id)
35155ffd83dbSDimitry Andric                                              ? takeTemplateIdAnnotation(Next)
35165ffd83dbSDimitry Andric                                              : nullptr;
35175ffd83dbSDimitry Andric       if (TemplateId && TemplateId->hasInvalidName()) {
35185ffd83dbSDimitry Andric         // We found something like 'T::U<Args> x', but U is not a template.
35195ffd83dbSDimitry Andric         // Assume it was supposed to be a type.
35205ffd83dbSDimitry Andric         DS.SetTypeSpecError();
35215ffd83dbSDimitry Andric         ConsumeAnnotationToken();
35225ffd83dbSDimitry Andric         break;
35235ffd83dbSDimitry Andric       }
35245ffd83dbSDimitry Andric 
35255ffd83dbSDimitry Andric       if (TemplateId && TemplateId->Kind == TNK_Type_template) {
35260b57cec5SDimitry Andric         // We have a qualified template-id, e.g., N::A<int>
35270b57cec5SDimitry Andric 
35280b57cec5SDimitry Andric         // If this would be a valid constructor declaration with template
35290b57cec5SDimitry Andric         // arguments, we will reject the attempt to form an invalid type-id
35300b57cec5SDimitry Andric         // referring to the injected-class-name when we annotate the token,
35310b57cec5SDimitry Andric         // per C++ [class.qual]p2.
35320b57cec5SDimitry Andric         //
35330b57cec5SDimitry Andric         // To improve diagnostics for this case, parse the declaration as a
35340b57cec5SDimitry Andric         // constructor (and reject the extra template arguments later).
35350b57cec5SDimitry Andric         if ((DSContext == DeclSpecContext::DSC_top_level ||
35360b57cec5SDimitry Andric              DSContext == DeclSpecContext::DSC_class) &&
35370b57cec5SDimitry Andric             TemplateId->Name &&
35380b57cec5SDimitry Andric             Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS) &&
3539bdd1243dSDimitry Andric             isConstructorDeclarator(/*Unqualified=*/false,
3540bdd1243dSDimitry Andric                                     /*DeductionGuide=*/false,
3541bdd1243dSDimitry Andric                                     DS.isFriendSpecified())) {
35420b57cec5SDimitry Andric           // The user meant this to be an out-of-line constructor
35430b57cec5SDimitry Andric           // definition, but template arguments are not allowed
35440b57cec5SDimitry Andric           // there.  Just allow this as a constructor; we'll
35450b57cec5SDimitry Andric           // complain about it later.
35460b57cec5SDimitry Andric           goto DoneWithDeclSpec;
35470b57cec5SDimitry Andric         }
35480b57cec5SDimitry Andric 
35490b57cec5SDimitry Andric         DS.getTypeSpecScope() = SS;
35500b57cec5SDimitry Andric         ConsumeAnnotationToken(); // The C++ scope.
35510b57cec5SDimitry Andric         assert(Tok.is(tok::annot_template_id) &&
35520b57cec5SDimitry Andric                "ParseOptionalCXXScopeSpecifier not working");
3553bdd1243dSDimitry Andric         AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename);
355455e4f9d5SDimitry Andric         continue;
355555e4f9d5SDimitry Andric       }
355655e4f9d5SDimitry Andric 
355706c3fb27SDimitry Andric       if (TemplateId && TemplateId->Kind == TNK_Concept_template) {
355855e4f9d5SDimitry Andric         DS.getTypeSpecScope() = SS;
355906c3fb27SDimitry Andric         // This is probably a qualified placeholder-specifier, e.g., ::C<int>
356006c3fb27SDimitry Andric         // auto ... Consume the scope annotation and continue to consume the
356106c3fb27SDimitry Andric         // template-id as a placeholder-specifier. Let the next iteration
356206c3fb27SDimitry Andric         // diagnose a missing auto.
356355e4f9d5SDimitry Andric         ConsumeAnnotationToken();
35640b57cec5SDimitry Andric         continue;
35650b57cec5SDimitry Andric       }
35660b57cec5SDimitry Andric 
35670b57cec5SDimitry Andric       if (Next.is(tok::annot_typename)) {
35680b57cec5SDimitry Andric         DS.getTypeSpecScope() = SS;
35690b57cec5SDimitry Andric         ConsumeAnnotationToken(); // The C++ scope.
35705ffd83dbSDimitry Andric         TypeResult T = getTypeAnnotation(Tok);
35710b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
35720b57cec5SDimitry Andric                                        Tok.getAnnotationEndLoc(),
35730b57cec5SDimitry Andric                                        PrevSpec, DiagID, T, Policy);
35740b57cec5SDimitry Andric         if (isInvalid)
35750b57cec5SDimitry Andric           break;
35760b57cec5SDimitry Andric         DS.SetRangeEnd(Tok.getAnnotationEndLoc());
35770b57cec5SDimitry Andric         ConsumeAnnotationToken(); // The typename
35780b57cec5SDimitry Andric       }
35790b57cec5SDimitry Andric 
3580bdd1243dSDimitry Andric       if (AllowImplicitTypename == ImplicitTypenameContext::Yes &&
3581bdd1243dSDimitry Andric           Next.is(tok::annot_template_id) &&
3582bdd1243dSDimitry Andric           static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
3583bdd1243dSDimitry Andric                   ->Kind == TNK_Dependent_template_name) {
3584bdd1243dSDimitry Andric         DS.getTypeSpecScope() = SS;
3585bdd1243dSDimitry Andric         ConsumeAnnotationToken(); // The C++ scope.
3586bdd1243dSDimitry Andric         AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename);
3587bdd1243dSDimitry Andric         continue;
3588bdd1243dSDimitry Andric       }
3589bdd1243dSDimitry Andric 
35900b57cec5SDimitry Andric       if (Next.isNot(tok::identifier))
35910b57cec5SDimitry Andric         goto DoneWithDeclSpec;
35920b57cec5SDimitry Andric 
35930b57cec5SDimitry Andric       // Check whether this is a constructor declaration. If we're in a
35940b57cec5SDimitry Andric       // context where the identifier could be a class name, and it has the
35950b57cec5SDimitry Andric       // shape of a constructor declaration, process it as one.
35960b57cec5SDimitry Andric       if ((DSContext == DeclSpecContext::DSC_top_level ||
35970b57cec5SDimitry Andric            DSContext == DeclSpecContext::DSC_class) &&
35980b57cec5SDimitry Andric           Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
35990b57cec5SDimitry Andric                                      &SS) &&
3600bdd1243dSDimitry Andric           isConstructorDeclarator(/*Unqualified=*/false,
3601bdd1243dSDimitry Andric                                   /*DeductionGuide=*/false,
360206c3fb27SDimitry Andric                                   DS.isFriendSpecified(),
360306c3fb27SDimitry Andric                                   &TemplateInfo))
36040b57cec5SDimitry Andric         goto DoneWithDeclSpec;
36050b57cec5SDimitry Andric 
3606349cc55cSDimitry Andric       // C++20 [temp.spec] 13.9/6.
3607349cc55cSDimitry Andric       // This disables the access checking rules for function template explicit
3608349cc55cSDimitry Andric       // instantiation and explicit specialization:
3609349cc55cSDimitry Andric       // - `return type`.
3610349cc55cSDimitry Andric       SuppressAccessChecks SAC(*this, IsTemplateSpecOrInst);
3611349cc55cSDimitry Andric 
3612bdd1243dSDimitry Andric       ParsedType TypeRep = Actions.getTypeName(
3613bdd1243dSDimitry Andric           *Next.getIdentifierInfo(), Next.getLocation(), getCurScope(), &SS,
3614bdd1243dSDimitry Andric           false, false, nullptr,
36150b57cec5SDimitry Andric           /*IsCtorOrDtorName=*/false,
36160b57cec5SDimitry Andric           /*WantNontrivialTypeSourceInfo=*/true,
3617bdd1243dSDimitry Andric           isClassTemplateDeductionContext(DSContext), AllowImplicitTypename);
36180b57cec5SDimitry Andric 
3619349cc55cSDimitry Andric       if (IsTemplateSpecOrInst)
3620349cc55cSDimitry Andric         SAC.done();
3621349cc55cSDimitry Andric 
36220b57cec5SDimitry Andric       // If the referenced identifier is not a type, then this declspec is
36230b57cec5SDimitry Andric       // erroneous: We already checked about that it has no type specifier, and
36240b57cec5SDimitry Andric       // C++ doesn't have implicit int.  Diagnose it as a typo w.r.t. to the
36250b57cec5SDimitry Andric       // typename.
36260b57cec5SDimitry Andric       if (!TypeRep) {
362755e4f9d5SDimitry Andric         if (TryAnnotateTypeConstraint())
362855e4f9d5SDimitry Andric           goto DoneWithDeclSpec;
36295ffd83dbSDimitry Andric         if (Tok.isNot(tok::annot_cxxscope) ||
36305ffd83dbSDimitry Andric             NextToken().isNot(tok::identifier))
3631aec4c088SDimitry Andric           continue;
36320b57cec5SDimitry Andric         // Eat the scope spec so the identifier is current.
36330b57cec5SDimitry Andric         ConsumeAnnotationToken();
363481ad6265SDimitry Andric         ParsedAttributes Attrs(AttrFactory);
36350b57cec5SDimitry Andric         if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
36360b57cec5SDimitry Andric           if (!Attrs.empty()) {
36370b57cec5SDimitry Andric             AttrsLastTime = true;
36380b57cec5SDimitry Andric             attrs.takeAllFrom(Attrs);
36390b57cec5SDimitry Andric           }
36400b57cec5SDimitry Andric           continue;
36410b57cec5SDimitry Andric         }
36420b57cec5SDimitry Andric         goto DoneWithDeclSpec;
36430b57cec5SDimitry Andric       }
36440b57cec5SDimitry Andric 
36450b57cec5SDimitry Andric       DS.getTypeSpecScope() = SS;
36460b57cec5SDimitry Andric       ConsumeAnnotationToken(); // The C++ scope.
36470b57cec5SDimitry Andric 
36480b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
36490b57cec5SDimitry Andric                                      DiagID, TypeRep, Policy);
36500b57cec5SDimitry Andric       if (isInvalid)
36510b57cec5SDimitry Andric         break;
36520b57cec5SDimitry Andric 
36530b57cec5SDimitry Andric       DS.SetRangeEnd(Tok.getLocation());
36540b57cec5SDimitry Andric       ConsumeToken(); // The typename.
36550b57cec5SDimitry Andric 
36560b57cec5SDimitry Andric       continue;
36570b57cec5SDimitry Andric     }
36580b57cec5SDimitry Andric 
36590b57cec5SDimitry Andric     case tok::annot_typename: {
36600b57cec5SDimitry Andric       // If we've previously seen a tag definition, we were almost surely
36610b57cec5SDimitry Andric       // missing a semicolon after it.
36620b57cec5SDimitry Andric       if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
36630b57cec5SDimitry Andric         goto DoneWithDeclSpec;
36640b57cec5SDimitry Andric 
36655ffd83dbSDimitry Andric       TypeResult T = getTypeAnnotation(Tok);
36660b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
36670b57cec5SDimitry Andric                                      DiagID, T, Policy);
36680b57cec5SDimitry Andric       if (isInvalid)
36690b57cec5SDimitry Andric         break;
36700b57cec5SDimitry Andric 
36710b57cec5SDimitry Andric       DS.SetRangeEnd(Tok.getAnnotationEndLoc());
36720b57cec5SDimitry Andric       ConsumeAnnotationToken(); // The typename
36730b57cec5SDimitry Andric 
36740b57cec5SDimitry Andric       continue;
36750b57cec5SDimitry Andric     }
36760b57cec5SDimitry Andric 
36770b57cec5SDimitry Andric     case tok::kw___is_signed:
36780b57cec5SDimitry Andric       // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
36790b57cec5SDimitry Andric       // typically treats it as a trait. If we see __is_signed as it appears
36800b57cec5SDimitry Andric       // in libstdc++, e.g.,
36810b57cec5SDimitry Andric       //
36820b57cec5SDimitry Andric       //   static const bool __is_signed;
36830b57cec5SDimitry Andric       //
36840b57cec5SDimitry Andric       // then treat __is_signed as an identifier rather than as a keyword.
36850b57cec5SDimitry Andric       if (DS.getTypeSpecType() == TST_bool &&
36860b57cec5SDimitry Andric           DS.getTypeQualifiers() == DeclSpec::TQ_const &&
36870b57cec5SDimitry Andric           DS.getStorageClassSpec() == DeclSpec::SCS_static)
36880b57cec5SDimitry Andric         TryKeywordIdentFallback(true);
36890b57cec5SDimitry Andric 
36900b57cec5SDimitry Andric       // We're done with the declaration-specifiers.
36910b57cec5SDimitry Andric       goto DoneWithDeclSpec;
36920b57cec5SDimitry Andric 
36930b57cec5SDimitry Andric       // typedef-name
36940b57cec5SDimitry Andric     case tok::kw___super:
36950b57cec5SDimitry Andric     case tok::kw_decltype:
3696bdd1243dSDimitry Andric     case tok::identifier:
3697bdd1243dSDimitry Andric     ParseIdentifier: {
36980b57cec5SDimitry Andric       // This identifier can only be a typedef name if we haven't already seen
36990b57cec5SDimitry Andric       // a type-specifier.  Without this check we misparse:
37000b57cec5SDimitry Andric       //  typedef int X; struct Y { short X; };  as 'short int'.
37010b57cec5SDimitry Andric       if (DS.hasTypeSpecifier())
37020b57cec5SDimitry Andric         goto DoneWithDeclSpec;
37030b57cec5SDimitry Andric 
37040b57cec5SDimitry Andric       // If the token is an identifier named "__declspec" and Microsoft
37050b57cec5SDimitry Andric       // extensions are not enabled, it is likely that there will be cascading
37060b57cec5SDimitry Andric       // parse errors if this really is a __declspec attribute. Attempt to
37070b57cec5SDimitry Andric       // recognize that scenario and recover gracefully.
37080b57cec5SDimitry Andric       if (!getLangOpts().DeclSpecKeyword && Tok.is(tok::identifier) &&
37090b57cec5SDimitry Andric           Tok.getIdentifierInfo()->getName().equals("__declspec")) {
37100b57cec5SDimitry Andric         Diag(Loc, diag::err_ms_attributes_not_enabled);
37110b57cec5SDimitry Andric 
37120b57cec5SDimitry Andric         // The next token should be an open paren. If it is, eat the entire
37130b57cec5SDimitry Andric         // attribute declaration and continue.
37140b57cec5SDimitry Andric         if (NextToken().is(tok::l_paren)) {
37150b57cec5SDimitry Andric           // Consume the __declspec identifier.
37160b57cec5SDimitry Andric           ConsumeToken();
37170b57cec5SDimitry Andric 
37180b57cec5SDimitry Andric           // Eat the parens and everything between them.
37190b57cec5SDimitry Andric           BalancedDelimiterTracker T(*this, tok::l_paren);
37200b57cec5SDimitry Andric           if (T.consumeOpen()) {
37210b57cec5SDimitry Andric             assert(false && "Not a left paren?");
37220b57cec5SDimitry Andric             return;
37230b57cec5SDimitry Andric           }
37240b57cec5SDimitry Andric           T.skipToEnd();
37250b57cec5SDimitry Andric           continue;
37260b57cec5SDimitry Andric         }
37270b57cec5SDimitry Andric       }
37280b57cec5SDimitry Andric 
37290b57cec5SDimitry Andric       // In C++, check to see if this is a scope specifier like foo::bar::, if
37300b57cec5SDimitry Andric       // so handle it as such.  This is important for ctor parsing.
37310b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus) {
3732349cc55cSDimitry Andric         // C++20 [temp.spec] 13.9/6.
3733349cc55cSDimitry Andric         // This disables the access checking rules for function template
3734349cc55cSDimitry Andric         // explicit instantiation and explicit specialization:
3735349cc55cSDimitry Andric         // - `return type`.
3736349cc55cSDimitry Andric         SuppressAccessChecks SAC(*this, IsTemplateSpecOrInst);
3737349cc55cSDimitry Andric 
3738349cc55cSDimitry Andric         const bool Success = TryAnnotateCXXScopeToken(EnteringContext);
3739349cc55cSDimitry Andric 
3740349cc55cSDimitry Andric         if (IsTemplateSpecOrInst)
3741349cc55cSDimitry Andric           SAC.done();
3742349cc55cSDimitry Andric 
3743349cc55cSDimitry Andric         if (Success) {
3744349cc55cSDimitry Andric           if (IsTemplateSpecOrInst)
3745349cc55cSDimitry Andric             SAC.redelay();
37460b57cec5SDimitry Andric           DS.SetTypeSpecError();
37470b57cec5SDimitry Andric           goto DoneWithDeclSpec;
37480b57cec5SDimitry Andric         }
3749349cc55cSDimitry Andric 
37500b57cec5SDimitry Andric         if (!Tok.is(tok::identifier))
37510b57cec5SDimitry Andric           continue;
37520b57cec5SDimitry Andric       }
37530b57cec5SDimitry Andric 
37540b57cec5SDimitry Andric       // Check for need to substitute AltiVec keyword tokens.
37550b57cec5SDimitry Andric       if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
37560b57cec5SDimitry Andric         break;
37570b57cec5SDimitry Andric 
37580b57cec5SDimitry Andric       // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
37590b57cec5SDimitry Andric       //                allow the use of a typedef name as a type specifier.
37600b57cec5SDimitry Andric       if (DS.isTypeAltiVecVector())
37610b57cec5SDimitry Andric         goto DoneWithDeclSpec;
37620b57cec5SDimitry Andric 
37630b57cec5SDimitry Andric       if (DSContext == DeclSpecContext::DSC_objc_method_result &&
37640b57cec5SDimitry Andric           isObjCInstancetype()) {
37650b57cec5SDimitry Andric         ParsedType TypeRep = Actions.ActOnObjCInstanceType(Loc);
37660b57cec5SDimitry Andric         assert(TypeRep);
37670b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
37680b57cec5SDimitry Andric                                        DiagID, TypeRep, Policy);
37690b57cec5SDimitry Andric         if (isInvalid)
37700b57cec5SDimitry Andric           break;
37710b57cec5SDimitry Andric 
37720b57cec5SDimitry Andric         DS.SetRangeEnd(Loc);
37730b57cec5SDimitry Andric         ConsumeToken();
37740b57cec5SDimitry Andric         continue;
37750b57cec5SDimitry Andric       }
37760b57cec5SDimitry Andric 
37770b57cec5SDimitry Andric       // If we're in a context where the identifier could be a class name,
37780b57cec5SDimitry Andric       // check whether this is a constructor declaration.
37790b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class &&
37800b57cec5SDimitry Andric           Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
3781bdd1243dSDimitry Andric           isConstructorDeclarator(/*Unqualified=*/true,
3782bdd1243dSDimitry Andric                                   /*DeductionGuide=*/false,
3783bdd1243dSDimitry Andric                                   DS.isFriendSpecified()))
37840b57cec5SDimitry Andric         goto DoneWithDeclSpec;
37850b57cec5SDimitry Andric 
37860b57cec5SDimitry Andric       ParsedType TypeRep = Actions.getTypeName(
37870b57cec5SDimitry Andric           *Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), nullptr,
37880b57cec5SDimitry Andric           false, false, nullptr, false, false,
37890b57cec5SDimitry Andric           isClassTemplateDeductionContext(DSContext));
37900b57cec5SDimitry Andric 
37910b57cec5SDimitry Andric       // If this is not a typedef name, don't parse it as part of the declspec,
37920b57cec5SDimitry Andric       // it must be an implicit int or an error.
37930b57cec5SDimitry Andric       if (!TypeRep) {
379455e4f9d5SDimitry Andric         if (TryAnnotateTypeConstraint())
379555e4f9d5SDimitry Andric           goto DoneWithDeclSpec;
37965ffd83dbSDimitry Andric         if (Tok.isNot(tok::identifier))
3797aec4c088SDimitry Andric           continue;
379881ad6265SDimitry Andric         ParsedAttributes Attrs(AttrFactory);
37990b57cec5SDimitry Andric         if (ParseImplicitInt(DS, nullptr, TemplateInfo, AS, DSContext, Attrs)) {
38000b57cec5SDimitry Andric           if (!Attrs.empty()) {
38010b57cec5SDimitry Andric             AttrsLastTime = true;
38020b57cec5SDimitry Andric             attrs.takeAllFrom(Attrs);
38030b57cec5SDimitry Andric           }
38040b57cec5SDimitry Andric           continue;
38050b57cec5SDimitry Andric         }
38060b57cec5SDimitry Andric         goto DoneWithDeclSpec;
38070b57cec5SDimitry Andric       }
38080b57cec5SDimitry Andric 
38090b57cec5SDimitry Andric       // Likewise, if this is a context where the identifier could be a template
38100b57cec5SDimitry Andric       // name, check whether this is a deduction guide declaration.
381106c3fb27SDimitry Andric       CXXScopeSpec SS;
38120b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus17 &&
38130b57cec5SDimitry Andric           (DSContext == DeclSpecContext::DSC_class ||
38140b57cec5SDimitry Andric            DSContext == DeclSpecContext::DSC_top_level) &&
38150b57cec5SDimitry Andric           Actions.isDeductionGuideName(getCurScope(), *Tok.getIdentifierInfo(),
381606c3fb27SDimitry Andric                                        Tok.getLocation(), SS) &&
38170b57cec5SDimitry Andric           isConstructorDeclarator(/*Unqualified*/ true,
38180b57cec5SDimitry Andric                                   /*DeductionGuide*/ true))
38190b57cec5SDimitry Andric         goto DoneWithDeclSpec;
38200b57cec5SDimitry Andric 
38210b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
38220b57cec5SDimitry Andric                                      DiagID, TypeRep, Policy);
38230b57cec5SDimitry Andric       if (isInvalid)
38240b57cec5SDimitry Andric         break;
38250b57cec5SDimitry Andric 
38260b57cec5SDimitry Andric       DS.SetRangeEnd(Tok.getLocation());
38270b57cec5SDimitry Andric       ConsumeToken(); // The identifier
38280b57cec5SDimitry Andric 
38290b57cec5SDimitry Andric       // Objective-C supports type arguments and protocol references
38300b57cec5SDimitry Andric       // following an Objective-C object or object pointer
38310b57cec5SDimitry Andric       // type. Handle either one of them.
38320b57cec5SDimitry Andric       if (Tok.is(tok::less) && getLangOpts().ObjC) {
38330b57cec5SDimitry Andric         SourceLocation NewEndLoc;
38340b57cec5SDimitry Andric         TypeResult NewTypeRep = parseObjCTypeArgsAndProtocolQualifiers(
38350b57cec5SDimitry Andric                                   Loc, TypeRep, /*consumeLastToken=*/true,
38360b57cec5SDimitry Andric                                   NewEndLoc);
38370b57cec5SDimitry Andric         if (NewTypeRep.isUsable()) {
38380b57cec5SDimitry Andric           DS.UpdateTypeRep(NewTypeRep.get());
38390b57cec5SDimitry Andric           DS.SetRangeEnd(NewEndLoc);
38400b57cec5SDimitry Andric         }
38410b57cec5SDimitry Andric       }
38420b57cec5SDimitry Andric 
38430b57cec5SDimitry Andric       // Need to support trailing type qualifiers (e.g. "id<p> const").
38440b57cec5SDimitry Andric       // If a type specifier follows, it will be diagnosed elsewhere.
38450b57cec5SDimitry Andric       continue;
38460b57cec5SDimitry Andric     }
38470b57cec5SDimitry Andric 
384855e4f9d5SDimitry Andric       // type-name or placeholder-specifier
38490b57cec5SDimitry Andric     case tok::annot_template_id: {
38500b57cec5SDimitry Andric       TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
38515ffd83dbSDimitry Andric 
38525ffd83dbSDimitry Andric       if (TemplateId->hasInvalidName()) {
38535ffd83dbSDimitry Andric         DS.SetTypeSpecError();
38545ffd83dbSDimitry Andric         break;
38555ffd83dbSDimitry Andric       }
38565ffd83dbSDimitry Andric 
385755e4f9d5SDimitry Andric       if (TemplateId->Kind == TNK_Concept_template) {
38585ffd83dbSDimitry Andric         // If we've already diagnosed that this type-constraint has invalid
3859bdd1243dSDimitry Andric         // arguments, drop it and just form 'auto' or 'decltype(auto)'.
38605ffd83dbSDimitry Andric         if (TemplateId->hasInvalidArgs())
38615ffd83dbSDimitry Andric           TemplateId = nullptr;
38625ffd83dbSDimitry Andric 
3863bdd1243dSDimitry Andric         // Any of the following tokens are likely the start of the user
3864bdd1243dSDimitry Andric         // forgetting 'auto' or 'decltype(auto)', so diagnose.
3865bdd1243dSDimitry Andric         // Note: if updating this list, please make sure we update
3866bdd1243dSDimitry Andric         // isCXXDeclarationSpecifier's check for IsPlaceholderSpecifier to have
3867bdd1243dSDimitry Andric         // a matching list.
3868bdd1243dSDimitry Andric         if (NextToken().isOneOf(tok::identifier, tok::kw_const,
3869bdd1243dSDimitry Andric                                 tok::kw_volatile, tok::kw_restrict, tok::amp,
3870bdd1243dSDimitry Andric                                 tok::ampamp)) {
387155e4f9d5SDimitry Andric           Diag(Loc, diag::err_placeholder_expected_auto_or_decltype_auto)
387255e4f9d5SDimitry Andric               << FixItHint::CreateInsertion(NextToken().getLocation(), "auto");
387355e4f9d5SDimitry Andric           // Attempt to continue as if 'auto' was placed here.
387455e4f9d5SDimitry Andric           isInvalid = DS.SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID,
387555e4f9d5SDimitry Andric                                          TemplateId, Policy);
387655e4f9d5SDimitry Andric           break;
387755e4f9d5SDimitry Andric         }
387855e4f9d5SDimitry Andric         if (!NextToken().isOneOf(tok::kw_auto, tok::kw_decltype))
387955e4f9d5SDimitry Andric             goto DoneWithDeclSpec;
388006c3fb27SDimitry Andric 
388106c3fb27SDimitry Andric         if (TemplateId && !isInvalid && Actions.CheckTypeConstraint(TemplateId))
388206c3fb27SDimitry Andric             TemplateId = nullptr;
388306c3fb27SDimitry Andric 
388455e4f9d5SDimitry Andric         ConsumeAnnotationToken();
388555e4f9d5SDimitry Andric         SourceLocation AutoLoc = Tok.getLocation();
388655e4f9d5SDimitry Andric         if (TryConsumeToken(tok::kw_decltype)) {
388755e4f9d5SDimitry Andric           BalancedDelimiterTracker Tracker(*this, tok::l_paren);
388855e4f9d5SDimitry Andric           if (Tracker.consumeOpen()) {
388955e4f9d5SDimitry Andric             // Something like `void foo(Iterator decltype i)`
389055e4f9d5SDimitry Andric             Diag(Tok, diag::err_expected) << tok::l_paren;
389155e4f9d5SDimitry Andric           } else {
389255e4f9d5SDimitry Andric             if (!TryConsumeToken(tok::kw_auto)) {
389355e4f9d5SDimitry Andric               // Something like `void foo(Iterator decltype(int) i)`
389455e4f9d5SDimitry Andric               Tracker.skipToEnd();
389555e4f9d5SDimitry Andric               Diag(Tok, diag::err_placeholder_expected_auto_or_decltype_auto)
389655e4f9d5SDimitry Andric                 << FixItHint::CreateReplacement(SourceRange(AutoLoc,
389755e4f9d5SDimitry Andric                                                             Tok.getLocation()),
389855e4f9d5SDimitry Andric                                                 "auto");
389955e4f9d5SDimitry Andric             } else {
390055e4f9d5SDimitry Andric               Tracker.consumeClose();
390155e4f9d5SDimitry Andric             }
390255e4f9d5SDimitry Andric           }
390355e4f9d5SDimitry Andric           ConsumedEnd = Tok.getLocation();
3904bdd1243dSDimitry Andric           DS.setTypeArgumentRange(Tracker.getRange());
390555e4f9d5SDimitry Andric           // Even if something went wrong above, continue as if we've seen
390655e4f9d5SDimitry Andric           // `decltype(auto)`.
390755e4f9d5SDimitry Andric           isInvalid = DS.SetTypeSpecType(TST_decltype_auto, Loc, PrevSpec,
390855e4f9d5SDimitry Andric                                          DiagID, TemplateId, Policy);
390955e4f9d5SDimitry Andric         } else {
391004eeddc0SDimitry Andric           isInvalid = DS.SetTypeSpecType(TST_auto, AutoLoc, PrevSpec, DiagID,
391155e4f9d5SDimitry Andric                                          TemplateId, Policy);
391255e4f9d5SDimitry Andric         }
391355e4f9d5SDimitry Andric         break;
391455e4f9d5SDimitry Andric       }
391555e4f9d5SDimitry Andric 
39160b57cec5SDimitry Andric       if (TemplateId->Kind != TNK_Type_template &&
39170b57cec5SDimitry Andric           TemplateId->Kind != TNK_Undeclared_template) {
39180b57cec5SDimitry Andric         // This template-id does not refer to a type name, so we're
39190b57cec5SDimitry Andric         // done with the type-specifiers.
39200b57cec5SDimitry Andric         goto DoneWithDeclSpec;
39210b57cec5SDimitry Andric       }
39220b57cec5SDimitry Andric 
39230b57cec5SDimitry Andric       // If we're in a context where the template-id could be a
39240b57cec5SDimitry Andric       // constructor name or specialization, check whether this is a
39250b57cec5SDimitry Andric       // constructor declaration.
39260b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class &&
39270b57cec5SDimitry Andric           Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
3928bdd1243dSDimitry Andric           isConstructorDeclarator(/*Unqualified=*/true,
3929bdd1243dSDimitry Andric                                   /*DeductionGuide=*/false,
3930bdd1243dSDimitry Andric                                   DS.isFriendSpecified()))
39310b57cec5SDimitry Andric         goto DoneWithDeclSpec;
39320b57cec5SDimitry Andric 
39330b57cec5SDimitry Andric       // Turn the template-id annotation token into a type annotation
39340b57cec5SDimitry Andric       // token, then try again to parse it as a type-specifier.
393555e4f9d5SDimitry Andric       CXXScopeSpec SS;
3936bdd1243dSDimitry Andric       AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename);
39370b57cec5SDimitry Andric       continue;
39380b57cec5SDimitry Andric     }
39390b57cec5SDimitry Andric 
3940fe6060f1SDimitry Andric     // Attributes support.
39410b57cec5SDimitry Andric     case tok::kw___attribute:
39420b57cec5SDimitry Andric     case tok::kw___declspec:
394381ad6265SDimitry Andric       ParseAttributes(PAKM_GNU | PAKM_Declspec, DS.getAttributes(), LateAttrs);
39440b57cec5SDimitry Andric       continue;
39450b57cec5SDimitry Andric 
39460b57cec5SDimitry Andric     // Microsoft single token adornments.
39470b57cec5SDimitry Andric     case tok::kw___forceinline: {
39480b57cec5SDimitry Andric       isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
39490b57cec5SDimitry Andric       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
39500b57cec5SDimitry Andric       SourceLocation AttrNameLoc = Tok.getLocation();
39510b57cec5SDimitry Andric       DS.getAttributes().addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc,
395206c3fb27SDimitry Andric                                 nullptr, 0, tok::kw___forceinline);
39530b57cec5SDimitry Andric       break;
39540b57cec5SDimitry Andric     }
39550b57cec5SDimitry Andric 
39560b57cec5SDimitry Andric     case tok::kw___unaligned:
39570b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID,
39580b57cec5SDimitry Andric                                  getLangOpts());
39590b57cec5SDimitry Andric       break;
39600b57cec5SDimitry Andric 
39610b57cec5SDimitry Andric     case tok::kw___sptr:
39620b57cec5SDimitry Andric     case tok::kw___uptr:
39630b57cec5SDimitry Andric     case tok::kw___ptr64:
39640b57cec5SDimitry Andric     case tok::kw___ptr32:
39650b57cec5SDimitry Andric     case tok::kw___w64:
39660b57cec5SDimitry Andric     case tok::kw___cdecl:
39670b57cec5SDimitry Andric     case tok::kw___stdcall:
39680b57cec5SDimitry Andric     case tok::kw___fastcall:
39690b57cec5SDimitry Andric     case tok::kw___thiscall:
39700b57cec5SDimitry Andric     case tok::kw___regcall:
39710b57cec5SDimitry Andric     case tok::kw___vectorcall:
39720b57cec5SDimitry Andric       ParseMicrosoftTypeAttributes(DS.getAttributes());
39730b57cec5SDimitry Andric       continue;
39740b57cec5SDimitry Andric 
397506c3fb27SDimitry Andric     case tok::kw___funcref:
397606c3fb27SDimitry Andric       ParseWebAssemblyFuncrefTypeAttribute(DS.getAttributes());
397706c3fb27SDimitry Andric       continue;
397806c3fb27SDimitry Andric 
39790b57cec5SDimitry Andric     // Borland single token adornments.
39800b57cec5SDimitry Andric     case tok::kw___pascal:
39810b57cec5SDimitry Andric       ParseBorlandTypeAttributes(DS.getAttributes());
39820b57cec5SDimitry Andric       continue;
39830b57cec5SDimitry Andric 
39840b57cec5SDimitry Andric     // OpenCL single token adornments.
39850b57cec5SDimitry Andric     case tok::kw___kernel:
39860b57cec5SDimitry Andric       ParseOpenCLKernelAttributes(DS.getAttributes());
39870b57cec5SDimitry Andric       continue;
39880b57cec5SDimitry Andric 
398981ad6265SDimitry Andric     // CUDA/HIP single token adornments.
399081ad6265SDimitry Andric     case tok::kw___noinline__:
399181ad6265SDimitry Andric       ParseCUDAFunctionAttributes(DS.getAttributes());
399281ad6265SDimitry Andric       continue;
399381ad6265SDimitry Andric 
39940b57cec5SDimitry Andric     // Nullability type specifiers.
39950b57cec5SDimitry Andric     case tok::kw__Nonnull:
39960b57cec5SDimitry Andric     case tok::kw__Nullable:
3997e8d8bef9SDimitry Andric     case tok::kw__Nullable_result:
39980b57cec5SDimitry Andric     case tok::kw__Null_unspecified:
39990b57cec5SDimitry Andric       ParseNullabilityTypeSpecifiers(DS.getAttributes());
40000b57cec5SDimitry Andric       continue;
40010b57cec5SDimitry Andric 
40020b57cec5SDimitry Andric     // Objective-C 'kindof' types.
40030b57cec5SDimitry Andric     case tok::kw___kindof:
40040b57cec5SDimitry Andric       DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc,
400506c3fb27SDimitry Andric                                 nullptr, 0, tok::kw___kindof);
40060b57cec5SDimitry Andric       (void)ConsumeToken();
40070b57cec5SDimitry Andric       continue;
40080b57cec5SDimitry Andric 
40090b57cec5SDimitry Andric     // storage-class-specifier
40100b57cec5SDimitry Andric     case tok::kw_typedef:
40110b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
40120b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
40130b57cec5SDimitry Andric       isStorageClass = true;
40140b57cec5SDimitry Andric       break;
40150b57cec5SDimitry Andric     case tok::kw_extern:
40160b57cec5SDimitry Andric       if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
40170b57cec5SDimitry Andric         Diag(Tok, diag::ext_thread_before) << "extern";
40180b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
40190b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
40200b57cec5SDimitry Andric       isStorageClass = true;
40210b57cec5SDimitry Andric       break;
40220b57cec5SDimitry Andric     case tok::kw___private_extern__:
40230b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
40240b57cec5SDimitry Andric                                          Loc, PrevSpec, DiagID, Policy);
40250b57cec5SDimitry Andric       isStorageClass = true;
40260b57cec5SDimitry Andric       break;
40270b57cec5SDimitry Andric     case tok::kw_static:
40280b57cec5SDimitry Andric       if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
40290b57cec5SDimitry Andric         Diag(Tok, diag::ext_thread_before) << "static";
40300b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
40310b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
40320b57cec5SDimitry Andric       isStorageClass = true;
40330b57cec5SDimitry Andric       break;
40340b57cec5SDimitry Andric     case tok::kw_auto:
40355f757f3fSDimitry Andric       if (getLangOpts().CPlusPlus11 || getLangOpts().C23) {
40360b57cec5SDimitry Andric         if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
40370b57cec5SDimitry Andric           isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
40380b57cec5SDimitry Andric                                              PrevSpec, DiagID, Policy);
40395f757f3fSDimitry Andric           if (!isInvalid && !getLangOpts().C23)
40400b57cec5SDimitry Andric             Diag(Tok, diag::ext_auto_storage_class)
40410b57cec5SDimitry Andric               << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
40420b57cec5SDimitry Andric         } else
40430b57cec5SDimitry Andric           isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
40440b57cec5SDimitry Andric                                          DiagID, Policy);
40450b57cec5SDimitry Andric       } else
40460b57cec5SDimitry Andric         isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
40470b57cec5SDimitry Andric                                            PrevSpec, DiagID, Policy);
40480b57cec5SDimitry Andric       isStorageClass = true;
40490b57cec5SDimitry Andric       break;
40500b57cec5SDimitry Andric     case tok::kw___auto_type:
40510b57cec5SDimitry Andric       Diag(Tok, diag::ext_auto_type);
40520b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto_type, Loc, PrevSpec,
40530b57cec5SDimitry Andric                                      DiagID, Policy);
40540b57cec5SDimitry Andric       break;
40550b57cec5SDimitry Andric     case tok::kw_register:
40560b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
40570b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
40580b57cec5SDimitry Andric       isStorageClass = true;
40590b57cec5SDimitry Andric       break;
40600b57cec5SDimitry Andric     case tok::kw_mutable:
40610b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
40620b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
40630b57cec5SDimitry Andric       isStorageClass = true;
40640b57cec5SDimitry Andric       break;
40650b57cec5SDimitry Andric     case tok::kw___thread:
40660b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
40670b57cec5SDimitry Andric                                                PrevSpec, DiagID);
40680b57cec5SDimitry Andric       isStorageClass = true;
40690b57cec5SDimitry Andric       break;
40700b57cec5SDimitry Andric     case tok::kw_thread_local:
40715f757f3fSDimitry Andric       if (getLangOpts().C23)
40725f757f3fSDimitry Andric         Diag(Tok, diag::warn_c23_compat_keyword) << Tok.getName();
4073b121cb00SDimitry Andric       // We map thread_local to _Thread_local in C23 mode so it retains the C
4074b121cb00SDimitry Andric       // semantics rather than getting the C++ semantics.
4075b121cb00SDimitry Andric       // FIXME: diagnostics will show _Thread_local when the user wrote
4076b121cb00SDimitry Andric       // thread_local in source in C23 mode; we need some general way to
4077b121cb00SDimitry Andric       // identify which way the user spelled the keyword in source.
4078b121cb00SDimitry Andric       isInvalid = DS.SetStorageClassSpecThread(
40795f757f3fSDimitry Andric           getLangOpts().C23 ? DeclSpec::TSCS__Thread_local
4080b121cb00SDimitry Andric                             : DeclSpec::TSCS_thread_local,
4081b121cb00SDimitry Andric           Loc, PrevSpec, DiagID);
40820b57cec5SDimitry Andric       isStorageClass = true;
40830b57cec5SDimitry Andric       break;
40840b57cec5SDimitry Andric     case tok::kw__Thread_local:
4085a7dea167SDimitry Andric       if (!getLangOpts().C11)
4086a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
40870b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
40880b57cec5SDimitry Andric                                                Loc, PrevSpec, DiagID);
40890b57cec5SDimitry Andric       isStorageClass = true;
40900b57cec5SDimitry Andric       break;
40910b57cec5SDimitry Andric 
40920b57cec5SDimitry Andric     // function-specifier
40930b57cec5SDimitry Andric     case tok::kw_inline:
40940b57cec5SDimitry Andric       isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
40950b57cec5SDimitry Andric       break;
40960b57cec5SDimitry Andric     case tok::kw_virtual:
40970b57cec5SDimitry Andric       // C++ for OpenCL does not allow virtual function qualifier, to avoid
40980b57cec5SDimitry Andric       // function pointers restricted in OpenCL v2.0 s6.9.a.
4099e8d8bef9SDimitry Andric       if (getLangOpts().OpenCLCPlusPlus &&
4100fe6060f1SDimitry Andric           !getActions().getOpenCLOptions().isAvailableOption(
4101fe6060f1SDimitry Andric               "__cl_clang_function_pointers", getLangOpts())) {
41020b57cec5SDimitry Andric         DiagID = diag::err_openclcxx_virtual_function;
41030b57cec5SDimitry Andric         PrevSpec = Tok.getIdentifierInfo()->getNameStart();
41040b57cec5SDimitry Andric         isInvalid = true;
4105e8d8bef9SDimitry Andric       } else {
41060b57cec5SDimitry Andric         isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
41070b57cec5SDimitry Andric       }
41080b57cec5SDimitry Andric       break;
41090b57cec5SDimitry Andric     case tok::kw_explicit: {
41100b57cec5SDimitry Andric       SourceLocation ExplicitLoc = Loc;
41110b57cec5SDimitry Andric       SourceLocation CloseParenLoc;
41120b57cec5SDimitry Andric       ExplicitSpecifier ExplicitSpec(nullptr, ExplicitSpecKind::ResolvedTrue);
41130b57cec5SDimitry Andric       ConsumedEnd = ExplicitLoc;
41140b57cec5SDimitry Andric       ConsumeToken(); // kw_explicit
41150b57cec5SDimitry Andric       if (Tok.is(tok::l_paren)) {
41165ffd83dbSDimitry Andric         if (getLangOpts().CPlusPlus20 || isExplicitBool() == TPResult::True) {
41175ffd83dbSDimitry Andric           Diag(Tok.getLocation(), getLangOpts().CPlusPlus20
411855e4f9d5SDimitry Andric                                       ? diag::warn_cxx17_compat_explicit_bool
411955e4f9d5SDimitry Andric                                       : diag::ext_explicit_bool);
412055e4f9d5SDimitry Andric 
41210b57cec5SDimitry Andric           ExprResult ExplicitExpr(static_cast<Expr *>(nullptr));
41220b57cec5SDimitry Andric           BalancedDelimiterTracker Tracker(*this, tok::l_paren);
41230b57cec5SDimitry Andric           Tracker.consumeOpen();
41245f757f3fSDimitry Andric 
41255f757f3fSDimitry Andric           EnterExpressionEvaluationContext ConstantEvaluated(
41265f757f3fSDimitry Andric               Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
41275f757f3fSDimitry Andric 
41285f757f3fSDimitry Andric           ExplicitExpr = ParseConstantExpressionInExprEvalContext();
41290b57cec5SDimitry Andric           ConsumedEnd = Tok.getLocation();
41300b57cec5SDimitry Andric           if (ExplicitExpr.isUsable()) {
41310b57cec5SDimitry Andric             CloseParenLoc = Tok.getLocation();
41320b57cec5SDimitry Andric             Tracker.consumeClose();
41330b57cec5SDimitry Andric             ExplicitSpec =
41340b57cec5SDimitry Andric                 Actions.ActOnExplicitBoolSpecifier(ExplicitExpr.get());
41350b57cec5SDimitry Andric           } else
41360b57cec5SDimitry Andric             Tracker.skipToEnd();
413755e4f9d5SDimitry Andric         } else {
41385ffd83dbSDimitry Andric           Diag(Tok.getLocation(), diag::warn_cxx20_compat_explicit_bool);
41390b57cec5SDimitry Andric         }
414055e4f9d5SDimitry Andric       }
41410b57cec5SDimitry Andric       isInvalid = DS.setFunctionSpecExplicit(ExplicitLoc, PrevSpec, DiagID,
41420b57cec5SDimitry Andric                                              ExplicitSpec, CloseParenLoc);
41430b57cec5SDimitry Andric       break;
41440b57cec5SDimitry Andric     }
41450b57cec5SDimitry Andric     case tok::kw__Noreturn:
41460b57cec5SDimitry Andric       if (!getLangOpts().C11)
4147a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
41480b57cec5SDimitry Andric       isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
41490b57cec5SDimitry Andric       break;
41500b57cec5SDimitry Andric 
41510b57cec5SDimitry Andric     // alignment-specifier
41520b57cec5SDimitry Andric     case tok::kw__Alignas:
41530b57cec5SDimitry Andric       if (!getLangOpts().C11)
4154a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
41550b57cec5SDimitry Andric       ParseAlignmentSpecifier(DS.getAttributes());
41560b57cec5SDimitry Andric       continue;
41570b57cec5SDimitry Andric 
41580b57cec5SDimitry Andric     // friend
41590b57cec5SDimitry Andric     case tok::kw_friend:
41600b57cec5SDimitry Andric       if (DSContext == DeclSpecContext::DSC_class)
41610b57cec5SDimitry Andric         isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
41620b57cec5SDimitry Andric       else {
41630b57cec5SDimitry Andric         PrevSpec = ""; // not actually used by the diagnostic
41640b57cec5SDimitry Andric         DiagID = diag::err_friend_invalid_in_context;
41650b57cec5SDimitry Andric         isInvalid = true;
41660b57cec5SDimitry Andric       }
41670b57cec5SDimitry Andric       break;
41680b57cec5SDimitry Andric 
41690b57cec5SDimitry Andric     // Modules
41700b57cec5SDimitry Andric     case tok::kw___module_private__:
41710b57cec5SDimitry Andric       isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
41720b57cec5SDimitry Andric       break;
41730b57cec5SDimitry Andric 
4174a7dea167SDimitry Andric     // constexpr, consteval, constinit specifiers
41750b57cec5SDimitry Andric     case tok::kw_constexpr:
4176e8d8bef9SDimitry Andric       isInvalid = DS.SetConstexprSpec(ConstexprSpecKind::Constexpr, Loc,
4177e8d8bef9SDimitry Andric                                       PrevSpec, DiagID);
41780b57cec5SDimitry Andric       break;
41790b57cec5SDimitry Andric     case tok::kw_consteval:
4180e8d8bef9SDimitry Andric       isInvalid = DS.SetConstexprSpec(ConstexprSpecKind::Consteval, Loc,
4181e8d8bef9SDimitry Andric                                       PrevSpec, DiagID);
41820b57cec5SDimitry Andric       break;
4183a7dea167SDimitry Andric     case tok::kw_constinit:
4184e8d8bef9SDimitry Andric       isInvalid = DS.SetConstexprSpec(ConstexprSpecKind::Constinit, Loc,
4185e8d8bef9SDimitry Andric                                       PrevSpec, DiagID);
4186a7dea167SDimitry Andric       break;
41870b57cec5SDimitry Andric 
41880b57cec5SDimitry Andric     // type-specifier
41890b57cec5SDimitry Andric     case tok::kw_short:
4190e8d8bef9SDimitry Andric       isInvalid = DS.SetTypeSpecWidth(TypeSpecifierWidth::Short, Loc, PrevSpec,
41910b57cec5SDimitry Andric                                       DiagID, Policy);
41920b57cec5SDimitry Andric       break;
41930b57cec5SDimitry Andric     case tok::kw_long:
4194e8d8bef9SDimitry Andric       if (DS.getTypeSpecWidth() != TypeSpecifierWidth::Long)
4195e8d8bef9SDimitry Andric         isInvalid = DS.SetTypeSpecWidth(TypeSpecifierWidth::Long, Loc, PrevSpec,
41960b57cec5SDimitry Andric                                         DiagID, Policy);
41970b57cec5SDimitry Andric       else
4198e8d8bef9SDimitry Andric         isInvalid = DS.SetTypeSpecWidth(TypeSpecifierWidth::LongLong, Loc,
4199e8d8bef9SDimitry Andric                                         PrevSpec, DiagID, Policy);
42000b57cec5SDimitry Andric       break;
42010b57cec5SDimitry Andric     case tok::kw___int64:
4202e8d8bef9SDimitry Andric       isInvalid = DS.SetTypeSpecWidth(TypeSpecifierWidth::LongLong, Loc,
4203e8d8bef9SDimitry Andric                                       PrevSpec, DiagID, Policy);
42040b57cec5SDimitry Andric       break;
42050b57cec5SDimitry Andric     case tok::kw_signed:
4206e8d8bef9SDimitry Andric       isInvalid =
4207e8d8bef9SDimitry Andric           DS.SetTypeSpecSign(TypeSpecifierSign::Signed, Loc, PrevSpec, DiagID);
42080b57cec5SDimitry Andric       break;
42090b57cec5SDimitry Andric     case tok::kw_unsigned:
4210e8d8bef9SDimitry Andric       isInvalid = DS.SetTypeSpecSign(TypeSpecifierSign::Unsigned, Loc, PrevSpec,
42110b57cec5SDimitry Andric                                      DiagID);
42120b57cec5SDimitry Andric       break;
42130b57cec5SDimitry Andric     case tok::kw__Complex:
4214a7dea167SDimitry Andric       if (!getLangOpts().C99)
4215a7dea167SDimitry Andric         Diag(Tok, diag::ext_c99_feature) << Tok.getName();
42160b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
42170b57cec5SDimitry Andric                                         DiagID);
42180b57cec5SDimitry Andric       break;
42190b57cec5SDimitry Andric     case tok::kw__Imaginary:
4220a7dea167SDimitry Andric       if (!getLangOpts().C99)
4221a7dea167SDimitry Andric         Diag(Tok, diag::ext_c99_feature) << Tok.getName();
42220b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
42230b57cec5SDimitry Andric                                         DiagID);
42240b57cec5SDimitry Andric       break;
42250b57cec5SDimitry Andric     case tok::kw_void:
42260b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
42270b57cec5SDimitry Andric                                      DiagID, Policy);
42280b57cec5SDimitry Andric       break;
42290b57cec5SDimitry Andric     case tok::kw_char:
42300b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
42310b57cec5SDimitry Andric                                      DiagID, Policy);
42320b57cec5SDimitry Andric       break;
42330b57cec5SDimitry Andric     case tok::kw_int:
42340b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
42350b57cec5SDimitry Andric                                      DiagID, Policy);
42360b57cec5SDimitry Andric       break;
42370eae32dcSDimitry Andric     case tok::kw__ExtInt:
42380eae32dcSDimitry Andric     case tok::kw__BitInt: {
42390eae32dcSDimitry Andric       DiagnoseBitIntUse(Tok);
42405ffd83dbSDimitry Andric       ExprResult ER = ParseExtIntegerArgument();
42415ffd83dbSDimitry Andric       if (ER.isInvalid())
42425ffd83dbSDimitry Andric         continue;
42430eae32dcSDimitry Andric       isInvalid = DS.SetBitIntType(Loc, ER.get(), PrevSpec, DiagID, Policy);
42445ffd83dbSDimitry Andric       ConsumedEnd = PrevTokLocation;
42455ffd83dbSDimitry Andric       break;
42465ffd83dbSDimitry Andric     }
42470b57cec5SDimitry Andric     case tok::kw___int128:
42480b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
42490b57cec5SDimitry Andric                                      DiagID, Policy);
42500b57cec5SDimitry Andric       break;
42510b57cec5SDimitry Andric     case tok::kw_half:
42520b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
42530b57cec5SDimitry Andric                                      DiagID, Policy);
42540b57cec5SDimitry Andric       break;
42555ffd83dbSDimitry Andric     case tok::kw___bf16:
42565ffd83dbSDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_BFloat16, Loc, PrevSpec,
42575ffd83dbSDimitry Andric                                      DiagID, Policy);
42585ffd83dbSDimitry Andric       break;
42590b57cec5SDimitry Andric     case tok::kw_float:
42600b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
42610b57cec5SDimitry Andric                                      DiagID, Policy);
42620b57cec5SDimitry Andric       break;
42630b57cec5SDimitry Andric     case tok::kw_double:
42640b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
42650b57cec5SDimitry Andric                                      DiagID, Policy);
42660b57cec5SDimitry Andric       break;
42670b57cec5SDimitry Andric     case tok::kw__Float16:
42680b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float16, Loc, PrevSpec,
42690b57cec5SDimitry Andric                                      DiagID, Policy);
42700b57cec5SDimitry Andric       break;
42710b57cec5SDimitry Andric     case tok::kw__Accum:
42725f757f3fSDimitry Andric       assert(getLangOpts().FixedPoint &&
42735f757f3fSDimitry Andric              "This keyword is only used when fixed point types are enabled "
42745f757f3fSDimitry Andric              "with `-ffixed-point`");
42755f757f3fSDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_accum, Loc, PrevSpec, DiagID,
42765f757f3fSDimitry Andric                                      Policy);
42770b57cec5SDimitry Andric       break;
42780b57cec5SDimitry Andric     case tok::kw__Fract:
42795f757f3fSDimitry Andric       assert(getLangOpts().FixedPoint &&
42805f757f3fSDimitry Andric              "This keyword is only used when fixed point types are enabled "
42815f757f3fSDimitry Andric              "with `-ffixed-point`");
42825f757f3fSDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_fract, Loc, PrevSpec, DiagID,
42835f757f3fSDimitry Andric                                      Policy);
42840b57cec5SDimitry Andric       break;
42850b57cec5SDimitry Andric     case tok::kw__Sat:
42865f757f3fSDimitry Andric       assert(getLangOpts().FixedPoint &&
42875f757f3fSDimitry Andric              "This keyword is only used when fixed point types are enabled "
42885f757f3fSDimitry Andric              "with `-ffixed-point`");
42890b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecSat(Loc, PrevSpec, DiagID);
42900b57cec5SDimitry Andric       break;
42910b57cec5SDimitry Andric     case tok::kw___float128:
42920b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float128, Loc, PrevSpec,
42930b57cec5SDimitry Andric                                      DiagID, Policy);
42940b57cec5SDimitry Andric       break;
4295349cc55cSDimitry Andric     case tok::kw___ibm128:
4296349cc55cSDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_ibm128, Loc, PrevSpec,
4297349cc55cSDimitry Andric                                      DiagID, Policy);
4298349cc55cSDimitry Andric       break;
42990b57cec5SDimitry Andric     case tok::kw_wchar_t:
43000b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
43010b57cec5SDimitry Andric                                      DiagID, Policy);
43020b57cec5SDimitry Andric       break;
43030b57cec5SDimitry Andric     case tok::kw_char8_t:
43040b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char8, Loc, PrevSpec,
43050b57cec5SDimitry Andric                                      DiagID, Policy);
43060b57cec5SDimitry Andric       break;
43070b57cec5SDimitry Andric     case tok::kw_char16_t:
43080b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
43090b57cec5SDimitry Andric                                      DiagID, Policy);
43100b57cec5SDimitry Andric       break;
43110b57cec5SDimitry Andric     case tok::kw_char32_t:
43120b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
43130b57cec5SDimitry Andric                                      DiagID, Policy);
43140b57cec5SDimitry Andric       break;
43150b57cec5SDimitry Andric     case tok::kw_bool:
43165f757f3fSDimitry Andric       if (getLangOpts().C23)
43175f757f3fSDimitry Andric         Diag(Tok, diag::warn_c23_compat_keyword) << Tok.getName();
431806c3fb27SDimitry Andric       [[fallthrough]];
43190b57cec5SDimitry Andric     case tok::kw__Bool:
4320a7dea167SDimitry Andric       if (Tok.is(tok::kw__Bool) && !getLangOpts().C99)
4321a7dea167SDimitry Andric         Diag(Tok, diag::ext_c99_feature) << Tok.getName();
4322a7dea167SDimitry Andric 
43230b57cec5SDimitry Andric       if (Tok.is(tok::kw_bool) &&
43240b57cec5SDimitry Andric           DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
43250b57cec5SDimitry Andric           DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
43260b57cec5SDimitry Andric         PrevSpec = ""; // Not used by the diagnostic.
43270b57cec5SDimitry Andric         DiagID = diag::err_bool_redeclaration;
43280b57cec5SDimitry Andric         // For better error recovery.
43290b57cec5SDimitry Andric         Tok.setKind(tok::identifier);
43300b57cec5SDimitry Andric         isInvalid = true;
43310b57cec5SDimitry Andric       } else {
43320b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
43330b57cec5SDimitry Andric                                        DiagID, Policy);
43340b57cec5SDimitry Andric       }
43350b57cec5SDimitry Andric       break;
43360b57cec5SDimitry Andric     case tok::kw__Decimal32:
43370b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
43380b57cec5SDimitry Andric                                      DiagID, Policy);
43390b57cec5SDimitry Andric       break;
43400b57cec5SDimitry Andric     case tok::kw__Decimal64:
43410b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
43420b57cec5SDimitry Andric                                      DiagID, Policy);
43430b57cec5SDimitry Andric       break;
43440b57cec5SDimitry Andric     case tok::kw__Decimal128:
43450b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
43460b57cec5SDimitry Andric                                      DiagID, Policy);
43470b57cec5SDimitry Andric       break;
43480b57cec5SDimitry Andric     case tok::kw___vector:
43490b57cec5SDimitry Andric       isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
43500b57cec5SDimitry Andric       break;
43510b57cec5SDimitry Andric     case tok::kw___pixel:
43520b57cec5SDimitry Andric       isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
43530b57cec5SDimitry Andric       break;
43540b57cec5SDimitry Andric     case tok::kw___bool:
43550b57cec5SDimitry Andric       isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
43560b57cec5SDimitry Andric       break;
43570b57cec5SDimitry Andric     case tok::kw_pipe:
4358349cc55cSDimitry Andric       if (!getLangOpts().OpenCL ||
4359349cc55cSDimitry Andric           getLangOpts().getOpenCLCompatibleVersion() < 200) {
4360fe6060f1SDimitry Andric         // OpenCL 2.0 and later define this keyword. OpenCL 1.2 and earlier
4361fe6060f1SDimitry Andric         // should support the "pipe" word as identifier.
43620b57cec5SDimitry Andric         Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
4363fe6060f1SDimitry Andric         Tok.setKind(tok::identifier);
43640b57cec5SDimitry Andric         goto DoneWithDeclSpec;
43656e75b2fbSDimitry Andric       } else if (!getLangOpts().OpenCLPipes) {
43666e75b2fbSDimitry Andric         DiagID = diag::err_opencl_unknown_type_specifier;
43676e75b2fbSDimitry Andric         PrevSpec = Tok.getIdentifierInfo()->getNameStart();
43686e75b2fbSDimitry Andric         isInvalid = true;
43696e75b2fbSDimitry Andric       } else
43700b57cec5SDimitry Andric         isInvalid = DS.SetTypePipe(true, Loc, PrevSpec, DiagID, Policy);
43710b57cec5SDimitry Andric       break;
4372fe6060f1SDimitry Andric // We only need to enumerate each image type once.
4373fe6060f1SDimitry Andric #define IMAGE_READ_WRITE_TYPE(Type, Id, Ext)
4374fe6060f1SDimitry Andric #define IMAGE_WRITE_TYPE(Type, Id, Ext)
4375fe6060f1SDimitry Andric #define IMAGE_READ_TYPE(ImgType, Id, Ext) \
43760b57cec5SDimitry Andric     case tok::kw_##ImgType##_t: \
4377fe6060f1SDimitry Andric       if (!handleOpenCLImageKW(Ext, DeclSpec::TST_##ImgType##_t)) \
4378fe6060f1SDimitry Andric         goto DoneWithDeclSpec; \
43790b57cec5SDimitry Andric       break;
43800b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def"
43810b57cec5SDimitry Andric     case tok::kw___unknown_anytype:
43820b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
43830b57cec5SDimitry Andric                                      PrevSpec, DiagID, Policy);
43840b57cec5SDimitry Andric       break;
43850b57cec5SDimitry Andric 
43860b57cec5SDimitry Andric     // class-specifier:
43870b57cec5SDimitry Andric     case tok::kw_class:
43880b57cec5SDimitry Andric     case tok::kw_struct:
43890b57cec5SDimitry Andric     case tok::kw___interface:
43900b57cec5SDimitry Andric     case tok::kw_union: {
43910b57cec5SDimitry Andric       tok::TokenKind Kind = Tok.getKind();
43920b57cec5SDimitry Andric       ConsumeToken();
43930b57cec5SDimitry Andric 
43940b57cec5SDimitry Andric       // These are attributes following class specifiers.
43950b57cec5SDimitry Andric       // To produce better diagnostic, we parse them when
43960b57cec5SDimitry Andric       // parsing class specifier.
439781ad6265SDimitry Andric       ParsedAttributes Attributes(AttrFactory);
43980b57cec5SDimitry Andric       ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
43990b57cec5SDimitry Andric                           EnteringContext, DSContext, Attributes);
44000b57cec5SDimitry Andric 
44010b57cec5SDimitry Andric       // If there are attributes following class specifier,
44020b57cec5SDimitry Andric       // take them over and handle them here.
44030b57cec5SDimitry Andric       if (!Attributes.empty()) {
44040b57cec5SDimitry Andric         AttrsLastTime = true;
44050b57cec5SDimitry Andric         attrs.takeAllFrom(Attributes);
44060b57cec5SDimitry Andric       }
44070b57cec5SDimitry Andric       continue;
44080b57cec5SDimitry Andric     }
44090b57cec5SDimitry Andric 
44100b57cec5SDimitry Andric     // enum-specifier:
44110b57cec5SDimitry Andric     case tok::kw_enum:
44120b57cec5SDimitry Andric       ConsumeToken();
44130b57cec5SDimitry Andric       ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
44140b57cec5SDimitry Andric       continue;
44150b57cec5SDimitry Andric 
44160b57cec5SDimitry Andric     // cv-qualifier:
44170b57cec5SDimitry Andric     case tok::kw_const:
44180b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
44190b57cec5SDimitry Andric                                  getLangOpts());
44200b57cec5SDimitry Andric       break;
44210b57cec5SDimitry Andric     case tok::kw_volatile:
44220b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
44230b57cec5SDimitry Andric                                  getLangOpts());
44240b57cec5SDimitry Andric       break;
44250b57cec5SDimitry Andric     case tok::kw_restrict:
44260b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
44270b57cec5SDimitry Andric                                  getLangOpts());
44280b57cec5SDimitry Andric       break;
44290b57cec5SDimitry Andric 
44300b57cec5SDimitry Andric     // C++ typename-specifier:
44310b57cec5SDimitry Andric     case tok::kw_typename:
44320b57cec5SDimitry Andric       if (TryAnnotateTypeOrScopeToken()) {
44330b57cec5SDimitry Andric         DS.SetTypeSpecError();
44340b57cec5SDimitry Andric         goto DoneWithDeclSpec;
44350b57cec5SDimitry Andric       }
44360b57cec5SDimitry Andric       if (!Tok.is(tok::kw_typename))
44370b57cec5SDimitry Andric         continue;
44380b57cec5SDimitry Andric       break;
44390b57cec5SDimitry Andric 
44405f757f3fSDimitry Andric     // C23/GNU typeof support.
44410b57cec5SDimitry Andric     case tok::kw_typeof:
4442bdd1243dSDimitry Andric     case tok::kw_typeof_unqual:
44430b57cec5SDimitry Andric       ParseTypeofSpecifier(DS);
44440b57cec5SDimitry Andric       continue;
44450b57cec5SDimitry Andric 
44460b57cec5SDimitry Andric     case tok::annot_decltype:
44470b57cec5SDimitry Andric       ParseDecltypeSpecifier(DS);
44480b57cec5SDimitry Andric       continue;
44490b57cec5SDimitry Andric 
44500b57cec5SDimitry Andric     case tok::annot_pragma_pack:
44510b57cec5SDimitry Andric       HandlePragmaPack();
44520b57cec5SDimitry Andric       continue;
44530b57cec5SDimitry Andric 
44540b57cec5SDimitry Andric     case tok::annot_pragma_ms_pragma:
44550b57cec5SDimitry Andric       HandlePragmaMSPragma();
44560b57cec5SDimitry Andric       continue;
44570b57cec5SDimitry Andric 
44580b57cec5SDimitry Andric     case tok::annot_pragma_ms_vtordisp:
44590b57cec5SDimitry Andric       HandlePragmaMSVtorDisp();
44600b57cec5SDimitry Andric       continue;
44610b57cec5SDimitry Andric 
44620b57cec5SDimitry Andric     case tok::annot_pragma_ms_pointers_to_members:
44630b57cec5SDimitry Andric       HandlePragmaMSPointersToMembers();
44640b57cec5SDimitry Andric       continue;
44650b57cec5SDimitry Andric 
4466bdd1243dSDimitry Andric #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait:
4467bdd1243dSDimitry Andric #include "clang/Basic/TransformTypeTraits.def"
4468bdd1243dSDimitry Andric       // HACK: libstdc++ already uses '__remove_cv' as an alias template so we
4469bdd1243dSDimitry Andric       // work around this by expecting all transform type traits to be suffixed
4470bdd1243dSDimitry Andric       // with '('. They're an identifier otherwise.
4471bdd1243dSDimitry Andric       if (!MaybeParseTypeTransformTypeSpecifier(DS))
4472bdd1243dSDimitry Andric         goto ParseIdentifier;
44730b57cec5SDimitry Andric       continue;
44740b57cec5SDimitry Andric 
44750b57cec5SDimitry Andric     case tok::kw__Atomic:
44760b57cec5SDimitry Andric       // C11 6.7.2.4/4:
44770b57cec5SDimitry Andric       //   If the _Atomic keyword is immediately followed by a left parenthesis,
44780b57cec5SDimitry Andric       //   it is interpreted as a type specifier (with a type name), not as a
44790b57cec5SDimitry Andric       //   type qualifier.
4480a7dea167SDimitry Andric       if (!getLangOpts().C11)
4481a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
4482a7dea167SDimitry Andric 
44830b57cec5SDimitry Andric       if (NextToken().is(tok::l_paren)) {
44840b57cec5SDimitry Andric         ParseAtomicSpecifier(DS);
44850b57cec5SDimitry Andric         continue;
44860b57cec5SDimitry Andric       }
44870b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
44880b57cec5SDimitry Andric                                  getLangOpts());
44890b57cec5SDimitry Andric       break;
44900b57cec5SDimitry Andric 
44910b57cec5SDimitry Andric     // OpenCL address space qualifiers:
44920b57cec5SDimitry Andric     case tok::kw___generic:
44930b57cec5SDimitry Andric       // generic address space is introduced only in OpenCL v2.0
44940b57cec5SDimitry Andric       // see OpenCL C Spec v2.0 s6.5.5
4495fe6060f1SDimitry Andric       // OpenCL v3.0 introduces __opencl_c_generic_address_space
4496fe6060f1SDimitry Andric       // feature macro to indicate if generic address space is supported
4497fe6060f1SDimitry Andric       if (!Actions.getLangOpts().OpenCLGenericAddressSpace) {
44980b57cec5SDimitry Andric         DiagID = diag::err_opencl_unknown_type_specifier;
44990b57cec5SDimitry Andric         PrevSpec = Tok.getIdentifierInfo()->getNameStart();
45000b57cec5SDimitry Andric         isInvalid = true;
45010b57cec5SDimitry Andric         break;
4502480093f4SDimitry Andric       }
4503bdd1243dSDimitry Andric       [[fallthrough]];
45040b57cec5SDimitry Andric     case tok::kw_private:
4505480093f4SDimitry Andric       // It's fine (but redundant) to check this for __generic on the
4506480093f4SDimitry Andric       // fallthrough path; we only form the __generic token in OpenCL mode.
4507480093f4SDimitry Andric       if (!getLangOpts().OpenCL)
4508480093f4SDimitry Andric         goto DoneWithDeclSpec;
4509bdd1243dSDimitry Andric       [[fallthrough]];
45100b57cec5SDimitry Andric     case tok::kw___private:
45110b57cec5SDimitry Andric     case tok::kw___global:
45120b57cec5SDimitry Andric     case tok::kw___local:
45130b57cec5SDimitry Andric     case tok::kw___constant:
45140b57cec5SDimitry Andric     // OpenCL access qualifiers:
45150b57cec5SDimitry Andric     case tok::kw___read_only:
45160b57cec5SDimitry Andric     case tok::kw___write_only:
45170b57cec5SDimitry Andric     case tok::kw___read_write:
45180b57cec5SDimitry Andric       ParseOpenCLQualifiers(DS.getAttributes());
45190b57cec5SDimitry Andric       break;
45200b57cec5SDimitry Andric 
4521bdd1243dSDimitry Andric     case tok::kw_groupshared:
45225f757f3fSDimitry Andric     case tok::kw_in:
45235f757f3fSDimitry Andric     case tok::kw_inout:
45245f757f3fSDimitry Andric     case tok::kw_out:
4525bdd1243dSDimitry Andric       // NOTE: ParseHLSLQualifiers will consume the qualifier token.
4526bdd1243dSDimitry Andric       ParseHLSLQualifiers(DS.getAttributes());
4527bdd1243dSDimitry Andric       continue;
4528bdd1243dSDimitry Andric 
45290b57cec5SDimitry Andric     case tok::less:
45300b57cec5SDimitry Andric       // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
45310b57cec5SDimitry Andric       // "id<SomeProtocol>".  This is hopelessly old fashioned and dangerous,
45320b57cec5SDimitry Andric       // but we support it.
45330b57cec5SDimitry Andric       if (DS.hasTypeSpecifier() || !getLangOpts().ObjC)
45340b57cec5SDimitry Andric         goto DoneWithDeclSpec;
45350b57cec5SDimitry Andric 
45360b57cec5SDimitry Andric       SourceLocation StartLoc = Tok.getLocation();
45370b57cec5SDimitry Andric       SourceLocation EndLoc;
45380b57cec5SDimitry Andric       TypeResult Type = parseObjCProtocolQualifierType(EndLoc);
45390b57cec5SDimitry Andric       if (Type.isUsable()) {
45400b57cec5SDimitry Andric         if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, StartLoc,
45410b57cec5SDimitry Andric                                PrevSpec, DiagID, Type.get(),
45420b57cec5SDimitry Andric                                Actions.getASTContext().getPrintingPolicy()))
45430b57cec5SDimitry Andric           Diag(StartLoc, DiagID) << PrevSpec;
45440b57cec5SDimitry Andric 
45450b57cec5SDimitry Andric         DS.SetRangeEnd(EndLoc);
45460b57cec5SDimitry Andric       } else {
45470b57cec5SDimitry Andric         DS.SetTypeSpecError();
45480b57cec5SDimitry Andric       }
45490b57cec5SDimitry Andric 
45500b57cec5SDimitry Andric       // Need to support trailing type qualifiers (e.g. "id<p> const").
45510b57cec5SDimitry Andric       // If a type specifier follows, it will be diagnosed elsewhere.
45520b57cec5SDimitry Andric       continue;
45530b57cec5SDimitry Andric     }
45540b57cec5SDimitry Andric 
45550b57cec5SDimitry Andric     DS.SetRangeEnd(ConsumedEnd.isValid() ? ConsumedEnd : Tok.getLocation());
45560b57cec5SDimitry Andric 
45570b57cec5SDimitry Andric     // If the specifier wasn't legal, issue a diagnostic.
45580b57cec5SDimitry Andric     if (isInvalid) {
45590b57cec5SDimitry Andric       assert(PrevSpec && "Method did not return previous specifier!");
45600b57cec5SDimitry Andric       assert(DiagID);
45610b57cec5SDimitry Andric 
45620b57cec5SDimitry Andric       if (DiagID == diag::ext_duplicate_declspec ||
45630b57cec5SDimitry Andric           DiagID == diag::ext_warn_duplicate_declspec ||
45640b57cec5SDimitry Andric           DiagID == diag::err_duplicate_declspec)
45650b57cec5SDimitry Andric         Diag(Loc, DiagID) << PrevSpec
45660b57cec5SDimitry Andric                           << FixItHint::CreateRemoval(
45670b57cec5SDimitry Andric                                  SourceRange(Loc, DS.getEndLoc()));
45680b57cec5SDimitry Andric       else if (DiagID == diag::err_opencl_unknown_type_specifier) {
4569349cc55cSDimitry Andric         Diag(Loc, DiagID) << getLangOpts().getOpenCLVersionString() << PrevSpec
4570349cc55cSDimitry Andric                           << isStorageClass;
45710b57cec5SDimitry Andric       } else
45720b57cec5SDimitry Andric         Diag(Loc, DiagID) << PrevSpec;
45730b57cec5SDimitry Andric     }
45740b57cec5SDimitry Andric 
45750b57cec5SDimitry Andric     if (DiagID != diag::err_bool_redeclaration && ConsumedEnd.isInvalid())
45760b57cec5SDimitry Andric       // After an error the next token can be an annotation token.
45770b57cec5SDimitry Andric       ConsumeAnyToken();
45780b57cec5SDimitry Andric 
45790b57cec5SDimitry Andric     AttrsLastTime = false;
45800b57cec5SDimitry Andric   }
45810b57cec5SDimitry Andric }
45820b57cec5SDimitry Andric 
45830b57cec5SDimitry Andric /// ParseStructDeclaration - Parse a struct declaration without the terminating
45840b57cec5SDimitry Andric /// semicolon.
45850b57cec5SDimitry Andric ///
45860b57cec5SDimitry Andric /// Note that a struct declaration refers to a declaration in a struct,
45870b57cec5SDimitry Andric /// not to the declaration of a struct.
45880b57cec5SDimitry Andric ///
45890b57cec5SDimitry Andric ///       struct-declaration:
45905f757f3fSDimitry Andric /// [C23]   attributes-specifier-seq[opt]
45910b57cec5SDimitry Andric ///           specifier-qualifier-list struct-declarator-list
45920b57cec5SDimitry Andric /// [GNU]   __extension__ struct-declaration
45930b57cec5SDimitry Andric /// [GNU]   specifier-qualifier-list
45940b57cec5SDimitry Andric ///       struct-declarator-list:
45950b57cec5SDimitry Andric ///         struct-declarator
45960b57cec5SDimitry Andric ///         struct-declarator-list ',' struct-declarator
45970b57cec5SDimitry Andric /// [GNU]   struct-declarator-list ',' attributes[opt] struct-declarator
45980b57cec5SDimitry Andric ///       struct-declarator:
45990b57cec5SDimitry Andric ///         declarator
46000b57cec5SDimitry Andric /// [GNU]   declarator attributes[opt]
46010b57cec5SDimitry Andric ///         declarator[opt] ':' constant-expression
46020b57cec5SDimitry Andric /// [GNU]   declarator[opt] ':' constant-expression attributes[opt]
46030b57cec5SDimitry Andric ///
46040b57cec5SDimitry Andric void Parser::ParseStructDeclaration(
46050b57cec5SDimitry Andric     ParsingDeclSpec &DS,
46060b57cec5SDimitry Andric     llvm::function_ref<void(ParsingFieldDeclarator &)> FieldsCallback) {
46070b57cec5SDimitry Andric 
46080b57cec5SDimitry Andric   if (Tok.is(tok::kw___extension__)) {
46090b57cec5SDimitry Andric     // __extension__ silences extension warnings in the subexpression.
46100b57cec5SDimitry Andric     ExtensionRAIIObject O(Diags);  // Use RAII to do this.
46110b57cec5SDimitry Andric     ConsumeToken();
46120b57cec5SDimitry Andric     return ParseStructDeclaration(DS, FieldsCallback);
46130b57cec5SDimitry Andric   }
46140b57cec5SDimitry Andric 
46150b57cec5SDimitry Andric   // Parse leading attributes.
461681ad6265SDimitry Andric   ParsedAttributes Attrs(AttrFactory);
46170b57cec5SDimitry Andric   MaybeParseCXX11Attributes(Attrs);
46180b57cec5SDimitry Andric 
46190b57cec5SDimitry Andric   // Parse the common specifier-qualifiers-list piece.
46200b57cec5SDimitry Andric   ParseSpecifierQualifierList(DS);
46210b57cec5SDimitry Andric 
46220b57cec5SDimitry Andric   // If there are no declarators, this is a free-standing declaration
46230b57cec5SDimitry Andric   // specifier. Let the actions module cope with it.
46240b57cec5SDimitry Andric   if (Tok.is(tok::semi)) {
46255f757f3fSDimitry Andric     // C23 6.7.2.1p9 : "The optional attribute specifier sequence in a
462681ad6265SDimitry Andric     // member declaration appertains to each of the members declared by the
462781ad6265SDimitry Andric     // member declarator list; it shall not appear if the optional member
462881ad6265SDimitry Andric     // declarator list is omitted."
462981ad6265SDimitry Andric     ProhibitAttributes(Attrs);
46300b57cec5SDimitry Andric     RecordDecl *AnonRecord = nullptr;
463181ad6265SDimitry Andric     Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(
463281ad6265SDimitry Andric         getCurScope(), AS_none, DS, ParsedAttributesView::none(), AnonRecord);
46330b57cec5SDimitry Andric     assert(!AnonRecord && "Did not expect anonymous struct or union here");
46340b57cec5SDimitry Andric     DS.complete(TheDecl);
46350b57cec5SDimitry Andric     return;
46360b57cec5SDimitry Andric   }
46370b57cec5SDimitry Andric 
46380b57cec5SDimitry Andric   // Read struct-declarators until we find the semicolon.
46390b57cec5SDimitry Andric   bool FirstDeclarator = true;
46400b57cec5SDimitry Andric   SourceLocation CommaLoc;
464104eeddc0SDimitry Andric   while (true) {
464281ad6265SDimitry Andric     ParsingFieldDeclarator DeclaratorInfo(*this, DS, Attrs);
46430b57cec5SDimitry Andric     DeclaratorInfo.D.setCommaLoc(CommaLoc);
46440b57cec5SDimitry Andric 
46450b57cec5SDimitry Andric     // Attributes are only allowed here on successive declarators.
4646e8d8bef9SDimitry Andric     if (!FirstDeclarator) {
4647e8d8bef9SDimitry Andric       // However, this does not apply for [[]] attributes (which could show up
4648e8d8bef9SDimitry Andric       // before or after the __attribute__ attributes).
4649e8d8bef9SDimitry Andric       DiagnoseAndSkipCXX11Attributes();
46500b57cec5SDimitry Andric       MaybeParseGNUAttributes(DeclaratorInfo.D);
4651e8d8bef9SDimitry Andric       DiagnoseAndSkipCXX11Attributes();
4652e8d8bef9SDimitry Andric     }
46530b57cec5SDimitry Andric 
46540b57cec5SDimitry Andric     /// struct-declarator: declarator
46550b57cec5SDimitry Andric     /// struct-declarator: declarator[opt] ':' constant-expression
46560b57cec5SDimitry Andric     if (Tok.isNot(tok::colon)) {
46570b57cec5SDimitry Andric       // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
46580b57cec5SDimitry Andric       ColonProtectionRAIIObject X(*this);
46590b57cec5SDimitry Andric       ParseDeclarator(DeclaratorInfo.D);
46600b57cec5SDimitry Andric     } else
46610b57cec5SDimitry Andric       DeclaratorInfo.D.SetIdentifier(nullptr, Tok.getLocation());
46620b57cec5SDimitry Andric 
46630b57cec5SDimitry Andric     if (TryConsumeToken(tok::colon)) {
46640b57cec5SDimitry Andric       ExprResult Res(ParseConstantExpression());
46650b57cec5SDimitry Andric       if (Res.isInvalid())
46660b57cec5SDimitry Andric         SkipUntil(tok::semi, StopBeforeMatch);
46670b57cec5SDimitry Andric       else
46680b57cec5SDimitry Andric         DeclaratorInfo.BitfieldSize = Res.get();
46690b57cec5SDimitry Andric     }
46700b57cec5SDimitry Andric 
46710b57cec5SDimitry Andric     // If attributes exist after the declarator, parse them.
46720b57cec5SDimitry Andric     MaybeParseGNUAttributes(DeclaratorInfo.D);
46730b57cec5SDimitry Andric 
46740b57cec5SDimitry Andric     // We're done with this declarator;  invoke the callback.
46750b57cec5SDimitry Andric     FieldsCallback(DeclaratorInfo);
46760b57cec5SDimitry Andric 
46770b57cec5SDimitry Andric     // If we don't have a comma, it is either the end of the list (a ';')
46780b57cec5SDimitry Andric     // or an error, bail out.
46790b57cec5SDimitry Andric     if (!TryConsumeToken(tok::comma, CommaLoc))
46800b57cec5SDimitry Andric       return;
46810b57cec5SDimitry Andric 
46820b57cec5SDimitry Andric     FirstDeclarator = false;
46830b57cec5SDimitry Andric   }
46840b57cec5SDimitry Andric }
46850b57cec5SDimitry Andric 
46860b57cec5SDimitry Andric /// ParseStructUnionBody
46870b57cec5SDimitry Andric ///       struct-contents:
46880b57cec5SDimitry Andric ///         struct-declaration-list
46890b57cec5SDimitry Andric /// [EXT]   empty
4690e8d8bef9SDimitry Andric /// [GNU]   "struct-declaration-list" without terminating ';'
46910b57cec5SDimitry Andric ///       struct-declaration-list:
46920b57cec5SDimitry Andric ///         struct-declaration
46930b57cec5SDimitry Andric ///         struct-declaration-list struct-declaration
46940b57cec5SDimitry Andric /// [OBC]   '@' 'defs' '(' class-name ')'
46950b57cec5SDimitry Andric ///
46960b57cec5SDimitry Andric void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
46975ffd83dbSDimitry Andric                                   DeclSpec::TST TagType, RecordDecl *TagDecl) {
46980b57cec5SDimitry Andric   PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc,
46990b57cec5SDimitry Andric                                       "parsing struct/union body");
47000b57cec5SDimitry Andric   assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
47010b57cec5SDimitry Andric 
47020b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_brace);
47030b57cec5SDimitry Andric   if (T.consumeOpen())
47040b57cec5SDimitry Andric     return;
47050b57cec5SDimitry Andric 
47060b57cec5SDimitry Andric   ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
47070b57cec5SDimitry Andric   Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
47080b57cec5SDimitry Andric 
47090b57cec5SDimitry Andric   // While we still have something to read, read the declarations in the struct.
47100b57cec5SDimitry Andric   while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
47110b57cec5SDimitry Andric          Tok.isNot(tok::eof)) {
47120b57cec5SDimitry Andric     // Each iteration of this loop reads one struct-declaration.
47130b57cec5SDimitry Andric 
47140b57cec5SDimitry Andric     // Check for extraneous top-level semicolon.
47150b57cec5SDimitry Andric     if (Tok.is(tok::semi)) {
47160b57cec5SDimitry Andric       ConsumeExtraSemi(InsideStruct, TagType);
47170b57cec5SDimitry Andric       continue;
47180b57cec5SDimitry Andric     }
47190b57cec5SDimitry Andric 
47200b57cec5SDimitry Andric     // Parse _Static_assert declaration.
4721d409305fSDimitry Andric     if (Tok.isOneOf(tok::kw__Static_assert, tok::kw_static_assert)) {
47220b57cec5SDimitry Andric       SourceLocation DeclEnd;
47230b57cec5SDimitry Andric       ParseStaticAssertDeclaration(DeclEnd);
47240b57cec5SDimitry Andric       continue;
47250b57cec5SDimitry Andric     }
47260b57cec5SDimitry Andric 
47270b57cec5SDimitry Andric     if (Tok.is(tok::annot_pragma_pack)) {
47280b57cec5SDimitry Andric       HandlePragmaPack();
47290b57cec5SDimitry Andric       continue;
47300b57cec5SDimitry Andric     }
47310b57cec5SDimitry Andric 
47320b57cec5SDimitry Andric     if (Tok.is(tok::annot_pragma_align)) {
47330b57cec5SDimitry Andric       HandlePragmaAlign();
47340b57cec5SDimitry Andric       continue;
47350b57cec5SDimitry Andric     }
47360b57cec5SDimitry Andric 
4737fe6060f1SDimitry Andric     if (Tok.isOneOf(tok::annot_pragma_openmp, tok::annot_attr_openmp)) {
47380b57cec5SDimitry Andric       // Result can be ignored, because it must be always empty.
47390b57cec5SDimitry Andric       AccessSpecifier AS = AS_none;
474081ad6265SDimitry Andric       ParsedAttributes Attrs(AttrFactory);
47410b57cec5SDimitry Andric       (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs);
47420b57cec5SDimitry Andric       continue;
47430b57cec5SDimitry Andric     }
47440b57cec5SDimitry Andric 
47455f757f3fSDimitry Andric     if (Tok.is(tok::annot_pragma_openacc)) {
47465f757f3fSDimitry Andric       ParseOpenACCDirectiveDecl();
47475f757f3fSDimitry Andric       continue;
47485f757f3fSDimitry Andric     }
47495f757f3fSDimitry Andric 
4750a7dea167SDimitry Andric     if (tok::isPragmaAnnotation(Tok.getKind())) {
4751a7dea167SDimitry Andric       Diag(Tok.getLocation(), diag::err_pragma_misplaced_in_decl)
4752a7dea167SDimitry Andric           << DeclSpec::getSpecifierName(
4753a7dea167SDimitry Andric                  TagType, Actions.getASTContext().getPrintingPolicy());
4754a7dea167SDimitry Andric       ConsumeAnnotationToken();
4755a7dea167SDimitry Andric       continue;
4756a7dea167SDimitry Andric     }
4757a7dea167SDimitry Andric 
47580b57cec5SDimitry Andric     if (!Tok.is(tok::at)) {
47590b57cec5SDimitry Andric       auto CFieldCallback = [&](ParsingFieldDeclarator &FD) {
47600b57cec5SDimitry Andric         // Install the declarator into the current TagDecl.
47610b57cec5SDimitry Andric         Decl *Field =
47620b57cec5SDimitry Andric             Actions.ActOnField(getCurScope(), TagDecl,
47630b57cec5SDimitry Andric                                FD.D.getDeclSpec().getSourceRange().getBegin(),
47640b57cec5SDimitry Andric                                FD.D, FD.BitfieldSize);
47650b57cec5SDimitry Andric         FD.complete(Field);
47660b57cec5SDimitry Andric       };
47670b57cec5SDimitry Andric 
47680b57cec5SDimitry Andric       // Parse all the comma separated declarators.
47690b57cec5SDimitry Andric       ParsingDeclSpec DS(*this);
47700b57cec5SDimitry Andric       ParseStructDeclaration(DS, CFieldCallback);
47710b57cec5SDimitry Andric     } else { // Handle @defs
47720b57cec5SDimitry Andric       ConsumeToken();
47730b57cec5SDimitry Andric       if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
47740b57cec5SDimitry Andric         Diag(Tok, diag::err_unexpected_at);
47750b57cec5SDimitry Andric         SkipUntil(tok::semi);
47760b57cec5SDimitry Andric         continue;
47770b57cec5SDimitry Andric       }
47780b57cec5SDimitry Andric       ConsumeToken();
47790b57cec5SDimitry Andric       ExpectAndConsume(tok::l_paren);
47800b57cec5SDimitry Andric       if (!Tok.is(tok::identifier)) {
47810b57cec5SDimitry Andric         Diag(Tok, diag::err_expected) << tok::identifier;
47820b57cec5SDimitry Andric         SkipUntil(tok::semi);
47830b57cec5SDimitry Andric         continue;
47840b57cec5SDimitry Andric       }
47850b57cec5SDimitry Andric       SmallVector<Decl *, 16> Fields;
47860b57cec5SDimitry Andric       Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
47870b57cec5SDimitry Andric                         Tok.getIdentifierInfo(), Fields);
47880b57cec5SDimitry Andric       ConsumeToken();
47890b57cec5SDimitry Andric       ExpectAndConsume(tok::r_paren);
47900b57cec5SDimitry Andric     }
47910b57cec5SDimitry Andric 
47920b57cec5SDimitry Andric     if (TryConsumeToken(tok::semi))
47930b57cec5SDimitry Andric       continue;
47940b57cec5SDimitry Andric 
47950b57cec5SDimitry Andric     if (Tok.is(tok::r_brace)) {
47960b57cec5SDimitry Andric       ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
47970b57cec5SDimitry Andric       break;
47980b57cec5SDimitry Andric     }
47990b57cec5SDimitry Andric 
48000b57cec5SDimitry Andric     ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
48010b57cec5SDimitry Andric     // Skip to end of block or statement to avoid ext-warning on extra ';'.
48020b57cec5SDimitry Andric     SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
48030b57cec5SDimitry Andric     // If we stopped at a ';', eat it.
48040b57cec5SDimitry Andric     TryConsumeToken(tok::semi);
48050b57cec5SDimitry Andric   }
48060b57cec5SDimitry Andric 
48070b57cec5SDimitry Andric   T.consumeClose();
48080b57cec5SDimitry Andric 
48090b57cec5SDimitry Andric   ParsedAttributes attrs(AttrFactory);
48100b57cec5SDimitry Andric   // If attributes exist after struct contents, parse them.
48110b57cec5SDimitry Andric   MaybeParseGNUAttributes(attrs);
48120b57cec5SDimitry Andric 
481381ad6265SDimitry Andric   SmallVector<Decl *, 32> FieldDecls(TagDecl->fields());
48145ffd83dbSDimitry Andric 
48150b57cec5SDimitry Andric   Actions.ActOnFields(getCurScope(), RecordLoc, TagDecl, FieldDecls,
48160b57cec5SDimitry Andric                       T.getOpenLocation(), T.getCloseLocation(), attrs);
48170b57cec5SDimitry Andric   StructScope.Exit();
48180b57cec5SDimitry Andric   Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange());
48190b57cec5SDimitry Andric }
48200b57cec5SDimitry Andric 
48210b57cec5SDimitry Andric /// ParseEnumSpecifier
48220b57cec5SDimitry Andric ///       enum-specifier: [C99 6.7.2.2]
48230b57cec5SDimitry Andric ///         'enum' identifier[opt] '{' enumerator-list '}'
48240b57cec5SDimitry Andric ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
48250b57cec5SDimitry Andric /// [GNU]   'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
48260b57cec5SDimitry Andric ///                                                 '}' attributes[opt]
48270b57cec5SDimitry Andric /// [MS]    'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
48280b57cec5SDimitry Andric ///                                                 '}'
48290b57cec5SDimitry Andric ///         'enum' identifier
48300b57cec5SDimitry Andric /// [GNU]   'enum' attributes[opt] identifier
48310b57cec5SDimitry Andric ///
48320b57cec5SDimitry Andric /// [C++11] enum-head '{' enumerator-list[opt] '}'
48330b57cec5SDimitry Andric /// [C++11] enum-head '{' enumerator-list ','  '}'
48340b57cec5SDimitry Andric ///
48350b57cec5SDimitry Andric ///       enum-head: [C++11]
48360b57cec5SDimitry Andric ///         enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
48370b57cec5SDimitry Andric ///         enum-key attribute-specifier-seq[opt] nested-name-specifier
48380b57cec5SDimitry Andric ///             identifier enum-base[opt]
48390b57cec5SDimitry Andric ///
48400b57cec5SDimitry Andric ///       enum-key: [C++11]
48410b57cec5SDimitry Andric ///         'enum'
48420b57cec5SDimitry Andric ///         'enum' 'class'
48430b57cec5SDimitry Andric ///         'enum' 'struct'
48440b57cec5SDimitry Andric ///
48450b57cec5SDimitry Andric ///       enum-base: [C++11]
48460b57cec5SDimitry Andric ///         ':' type-specifier-seq
48470b57cec5SDimitry Andric ///
48480b57cec5SDimitry Andric /// [C++] elaborated-type-specifier:
48495ffd83dbSDimitry Andric /// [C++]   'enum' nested-name-specifier[opt] identifier
48500b57cec5SDimitry Andric ///
48510b57cec5SDimitry Andric void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
48520b57cec5SDimitry Andric                                 const ParsedTemplateInfo &TemplateInfo,
48530b57cec5SDimitry Andric                                 AccessSpecifier AS, DeclSpecContext DSC) {
48540b57cec5SDimitry Andric   // Parse the tag portion of this.
48550b57cec5SDimitry Andric   if (Tok.is(tok::code_completion)) {
48560b57cec5SDimitry Andric     // Code completion for an enum name.
4857fe6060f1SDimitry Andric     cutOffParsing();
48580b57cec5SDimitry Andric     Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
4859bdd1243dSDimitry Andric     DS.SetTypeSpecError(); // Needed by ActOnUsingDeclaration.
4860fe6060f1SDimitry Andric     return;
48610b57cec5SDimitry Andric   }
48620b57cec5SDimitry Andric 
48630b57cec5SDimitry Andric   // If attributes exist after tag, parse them.
486481ad6265SDimitry Andric   ParsedAttributes attrs(AttrFactory);
4865fe6060f1SDimitry Andric   MaybeParseAttributes(PAKM_GNU | PAKM_Declspec | PAKM_CXX11, attrs);
48660b57cec5SDimitry Andric 
48670b57cec5SDimitry Andric   SourceLocation ScopedEnumKWLoc;
48680b57cec5SDimitry Andric   bool IsScopedUsingClassTag = false;
48690b57cec5SDimitry Andric 
48700b57cec5SDimitry Andric   // In C++11, recognize 'enum class' and 'enum struct'.
487181ad6265SDimitry Andric   if (Tok.isOneOf(tok::kw_class, tok::kw_struct) && getLangOpts().CPlusPlus) {
48720b57cec5SDimitry Andric     Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
48730b57cec5SDimitry Andric                                         : diag::ext_scoped_enum);
48740b57cec5SDimitry Andric     IsScopedUsingClassTag = Tok.is(tok::kw_class);
48750b57cec5SDimitry Andric     ScopedEnumKWLoc = ConsumeToken();
48760b57cec5SDimitry Andric 
48770b57cec5SDimitry Andric     // Attributes are not allowed between these keywords.  Diagnose,
48780b57cec5SDimitry Andric     // but then just treat them like they appeared in the right place.
48790b57cec5SDimitry Andric     ProhibitAttributes(attrs);
48800b57cec5SDimitry Andric 
48810b57cec5SDimitry Andric     // They are allowed afterwards, though.
4882fe6060f1SDimitry Andric     MaybeParseAttributes(PAKM_GNU | PAKM_Declspec | PAKM_CXX11, attrs);
48830b57cec5SDimitry Andric   }
48840b57cec5SDimitry Andric 
48850b57cec5SDimitry Andric   // C++11 [temp.explicit]p12:
48860b57cec5SDimitry Andric   //   The usual access controls do not apply to names used to specify
48870b57cec5SDimitry Andric   //   explicit instantiations.
48880b57cec5SDimitry Andric   // We extend this to also cover explicit specializations.  Note that
48890b57cec5SDimitry Andric   // we don't suppress if this turns out to be an elaborated type
48900b57cec5SDimitry Andric   // specifier.
48910b57cec5SDimitry Andric   bool shouldDelayDiagsInTag =
48920b57cec5SDimitry Andric     (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
48930b57cec5SDimitry Andric      TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
48940b57cec5SDimitry Andric   SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
48950b57cec5SDimitry Andric 
48965ffd83dbSDimitry Andric   // Determine whether this declaration is permitted to have an enum-base.
48975ffd83dbSDimitry Andric   AllowDefiningTypeSpec AllowEnumSpecifier =
489881ad6265SDimitry Andric       isDefiningTypeSpecifierContext(DSC, getLangOpts().CPlusPlus);
48995ffd83dbSDimitry Andric   bool CanBeOpaqueEnumDeclaration =
49005ffd83dbSDimitry Andric       DS.isEmpty() && isOpaqueEnumDeclarationContext(DSC);
49015ffd83dbSDimitry Andric   bool CanHaveEnumBase = (getLangOpts().CPlusPlus11 || getLangOpts().ObjC ||
49025ffd83dbSDimitry Andric                           getLangOpts().MicrosoftExt) &&
49035ffd83dbSDimitry Andric                          (AllowEnumSpecifier == AllowDefiningTypeSpec::Yes ||
49045ffd83dbSDimitry Andric                           CanBeOpaqueEnumDeclaration);
49050b57cec5SDimitry Andric 
49060b57cec5SDimitry Andric   CXXScopeSpec &SS = DS.getTypeSpecScope();
49070b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus) {
49085ffd83dbSDimitry Andric     // "enum foo : bar;" is not a potential typo for "enum foo::bar;".
49095ffd83dbSDimitry Andric     ColonProtectionRAIIObject X(*this);
49100b57cec5SDimitry Andric 
49110b57cec5SDimitry Andric     CXXScopeSpec Spec;
49125ffd83dbSDimitry Andric     if (ParseOptionalCXXScopeSpecifier(Spec, /*ObjectType=*/nullptr,
491304eeddc0SDimitry Andric                                        /*ObjectHasErrors=*/false,
49140b57cec5SDimitry Andric                                        /*EnteringContext=*/true))
49150b57cec5SDimitry Andric       return;
49160b57cec5SDimitry Andric 
49170b57cec5SDimitry Andric     if (Spec.isSet() && Tok.isNot(tok::identifier)) {
49180b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::identifier;
4919bdd1243dSDimitry Andric       DS.SetTypeSpecError();
49200b57cec5SDimitry Andric       if (Tok.isNot(tok::l_brace)) {
49210b57cec5SDimitry Andric         // Has no name and is not a definition.
49220b57cec5SDimitry Andric         // Skip the rest of this declarator, up until the comma or semicolon.
49230b57cec5SDimitry Andric         SkipUntil(tok::comma, StopAtSemi);
49240b57cec5SDimitry Andric         return;
49250b57cec5SDimitry Andric       }
49260b57cec5SDimitry Andric     }
49270b57cec5SDimitry Andric 
49280b57cec5SDimitry Andric     SS = Spec;
49290b57cec5SDimitry Andric   }
49300b57cec5SDimitry Andric 
49315ffd83dbSDimitry Andric   // Must have either 'enum name' or 'enum {...}' or (rarely) 'enum : T { ... }'.
49320b57cec5SDimitry Andric   if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
49335ffd83dbSDimitry Andric       Tok.isNot(tok::colon)) {
49340b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
49350b57cec5SDimitry Andric 
4936bdd1243dSDimitry Andric     DS.SetTypeSpecError();
49370b57cec5SDimitry Andric     // Skip the rest of this declarator, up until the comma or semicolon.
49380b57cec5SDimitry Andric     SkipUntil(tok::comma, StopAtSemi);
49390b57cec5SDimitry Andric     return;
49400b57cec5SDimitry Andric   }
49410b57cec5SDimitry Andric 
49420b57cec5SDimitry Andric   // If an identifier is present, consume and remember it.
49430b57cec5SDimitry Andric   IdentifierInfo *Name = nullptr;
49440b57cec5SDimitry Andric   SourceLocation NameLoc;
49450b57cec5SDimitry Andric   if (Tok.is(tok::identifier)) {
49460b57cec5SDimitry Andric     Name = Tok.getIdentifierInfo();
49470b57cec5SDimitry Andric     NameLoc = ConsumeToken();
49480b57cec5SDimitry Andric   }
49490b57cec5SDimitry Andric 
49500b57cec5SDimitry Andric   if (!Name && ScopedEnumKWLoc.isValid()) {
49510b57cec5SDimitry Andric     // C++0x 7.2p2: The optional identifier shall not be omitted in the
49520b57cec5SDimitry Andric     // declaration of a scoped enumeration.
49530b57cec5SDimitry Andric     Diag(Tok, diag::err_scoped_enum_missing_identifier);
49540b57cec5SDimitry Andric     ScopedEnumKWLoc = SourceLocation();
49550b57cec5SDimitry Andric     IsScopedUsingClassTag = false;
49560b57cec5SDimitry Andric   }
49570b57cec5SDimitry Andric 
49580b57cec5SDimitry Andric   // Okay, end the suppression area.  We'll decide whether to emit the
49590b57cec5SDimitry Andric   // diagnostics in a second.
49600b57cec5SDimitry Andric   if (shouldDelayDiagsInTag)
49610b57cec5SDimitry Andric     diagsFromTag.done();
49620b57cec5SDimitry Andric 
49630b57cec5SDimitry Andric   TypeResult BaseType;
49645ffd83dbSDimitry Andric   SourceRange BaseRange;
49655ffd83dbSDimitry Andric 
496681ad6265SDimitry Andric   bool CanBeBitfield =
496781ad6265SDimitry Andric       getCurScope()->isClassScope() && ScopedEnumKWLoc.isInvalid() && Name;
49680b57cec5SDimitry Andric 
49690b57cec5SDimitry Andric   // Parse the fixed underlying type.
49705ffd83dbSDimitry Andric   if (Tok.is(tok::colon)) {
49715ffd83dbSDimitry Andric     // This might be an enum-base or part of some unrelated enclosing context.
49725ffd83dbSDimitry Andric     //
49735ffd83dbSDimitry Andric     // 'enum E : base' is permitted in two circumstances:
49745ffd83dbSDimitry Andric     //
49755ffd83dbSDimitry Andric     // 1) As a defining-type-specifier, when followed by '{'.
49765ffd83dbSDimitry Andric     // 2) As the sole constituent of a complete declaration -- when DS is empty
49775ffd83dbSDimitry Andric     //    and the next token is ';'.
49785ffd83dbSDimitry Andric     //
49795ffd83dbSDimitry Andric     // The restriction to defining-type-specifiers is important to allow parsing
49805ffd83dbSDimitry Andric     //   a ? new enum E : int{}
49815ffd83dbSDimitry Andric     //   _Generic(a, enum E : int{})
49825ffd83dbSDimitry Andric     // properly.
49835ffd83dbSDimitry Andric     //
49845ffd83dbSDimitry Andric     // One additional consideration applies:
49855ffd83dbSDimitry Andric     //
49865ffd83dbSDimitry Andric     // C++ [dcl.enum]p1:
49875ffd83dbSDimitry Andric     //   A ':' following "enum nested-name-specifier[opt] identifier" within
49885ffd83dbSDimitry Andric     //   the decl-specifier-seq of a member-declaration is parsed as part of
49895ffd83dbSDimitry Andric     //   an enum-base.
49905ffd83dbSDimitry Andric     //
49915ffd83dbSDimitry Andric     // Other language modes supporting enumerations with fixed underlying types
49925ffd83dbSDimitry Andric     // do not have clear rules on this, so we disambiguate to determine whether
49935ffd83dbSDimitry Andric     // the tokens form a bit-field width or an enum-base.
49940b57cec5SDimitry Andric 
49955ffd83dbSDimitry Andric     if (CanBeBitfield && !isEnumBase(CanBeOpaqueEnumDeclaration)) {
49965ffd83dbSDimitry Andric       // Outside C++11, do not interpret the tokens as an enum-base if they do
49975ffd83dbSDimitry Andric       // not make sense as one. In C++11, it's an error if this happens.
49985ffd83dbSDimitry Andric       if (getLangOpts().CPlusPlus11)
49995ffd83dbSDimitry Andric         Diag(Tok.getLocation(), diag::err_anonymous_enum_bitfield);
50005ffd83dbSDimitry Andric     } else if (CanHaveEnumBase || !ColonIsSacred) {
50015ffd83dbSDimitry Andric       SourceLocation ColonLoc = ConsumeToken();
50020b57cec5SDimitry Andric 
50035ffd83dbSDimitry Andric       // Parse a type-specifier-seq as a type. We can't just ParseTypeName here,
50045ffd83dbSDimitry Andric       // because under -fms-extensions,
50055ffd83dbSDimitry Andric       //   enum E : int *p;
50065ffd83dbSDimitry Andric       // declares 'enum E : int; E *p;' not 'enum E : int*; E p;'.
50075ffd83dbSDimitry Andric       DeclSpec DS(AttrFactory);
5008bdd1243dSDimitry Andric       // enum-base is not assumed to be a type and therefore requires the
5009bdd1243dSDimitry Andric       // typename keyword [p0634r3].
5010bdd1243dSDimitry Andric       ParseSpecifierQualifierList(DS, ImplicitTypenameContext::No, AS,
5011bdd1243dSDimitry Andric                                   DeclSpecContext::DSC_type_specifier);
501281ad6265SDimitry Andric       Declarator DeclaratorInfo(DS, ParsedAttributesView::none(),
501381ad6265SDimitry Andric                                 DeclaratorContext::TypeName);
50145ffd83dbSDimitry Andric       BaseType = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
50150b57cec5SDimitry Andric 
50165ffd83dbSDimitry Andric       BaseRange = SourceRange(ColonLoc, DeclaratorInfo.getSourceRange().getEnd());
50170b57cec5SDimitry Andric 
50185f757f3fSDimitry Andric       if (!getLangOpts().ObjC && !getLangOpts().C23) {
50190b57cec5SDimitry Andric         if (getLangOpts().CPlusPlus11)
50205ffd83dbSDimitry Andric           Diag(ColonLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type)
50215ffd83dbSDimitry Andric               << BaseRange;
50220b57cec5SDimitry Andric         else if (getLangOpts().CPlusPlus)
50235ffd83dbSDimitry Andric           Diag(ColonLoc, diag::ext_cxx11_enum_fixed_underlying_type)
50245ffd83dbSDimitry Andric               << BaseRange;
50250b57cec5SDimitry Andric         else if (getLangOpts().MicrosoftExt)
50265ffd83dbSDimitry Andric           Diag(ColonLoc, diag::ext_ms_c_enum_fixed_underlying_type)
50275ffd83dbSDimitry Andric               << BaseRange;
50280b57cec5SDimitry Andric         else
50295ffd83dbSDimitry Andric           Diag(ColonLoc, diag::ext_clang_c_enum_fixed_underlying_type)
50305ffd83dbSDimitry Andric               << BaseRange;
50310b57cec5SDimitry Andric       }
50320b57cec5SDimitry Andric     }
50330b57cec5SDimitry Andric   }
50340b57cec5SDimitry Andric 
50350b57cec5SDimitry Andric   // There are four options here.  If we have 'friend enum foo;' then this is a
50360b57cec5SDimitry Andric   // friend declaration, and cannot have an accompanying definition. If we have
50370b57cec5SDimitry Andric   // 'enum foo;', then this is a forward declaration.  If we have
50380b57cec5SDimitry Andric   // 'enum foo {...' then this is a definition. Otherwise we have something
50390b57cec5SDimitry Andric   // like 'enum foo xyz', a reference.
50400b57cec5SDimitry Andric   //
50410b57cec5SDimitry Andric   // This is needed to handle stuff like this right (C99 6.7.2.3p11):
50420b57cec5SDimitry Andric   // enum foo {..};  void bar() { enum foo; }    <- new foo in bar.
50430b57cec5SDimitry Andric   // enum foo {..};  void bar() { enum foo x; }  <- use of old foo.
50440b57cec5SDimitry Andric   //
50450b57cec5SDimitry Andric   Sema::TagUseKind TUK;
50465ffd83dbSDimitry Andric   if (AllowEnumSpecifier == AllowDefiningTypeSpec::No)
50470b57cec5SDimitry Andric     TUK = Sema::TUK_Reference;
50485ffd83dbSDimitry Andric   else if (Tok.is(tok::l_brace)) {
50490b57cec5SDimitry Andric     if (DS.isFriendSpecified()) {
50500b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
50510b57cec5SDimitry Andric         << SourceRange(DS.getFriendSpecLoc());
50520b57cec5SDimitry Andric       ConsumeBrace();
50530b57cec5SDimitry Andric       SkipUntil(tok::r_brace, StopAtSemi);
50545ffd83dbSDimitry Andric       // Discard any other definition-only pieces.
50555ffd83dbSDimitry Andric       attrs.clear();
50565ffd83dbSDimitry Andric       ScopedEnumKWLoc = SourceLocation();
50575ffd83dbSDimitry Andric       IsScopedUsingClassTag = false;
50585ffd83dbSDimitry Andric       BaseType = TypeResult();
50590b57cec5SDimitry Andric       TUK = Sema::TUK_Friend;
50600b57cec5SDimitry Andric     } else {
50610b57cec5SDimitry Andric       TUK = Sema::TUK_Definition;
50620b57cec5SDimitry Andric     }
50630b57cec5SDimitry Andric   } else if (!isTypeSpecifier(DSC) &&
50640b57cec5SDimitry Andric              (Tok.is(tok::semi) ||
50650b57cec5SDimitry Andric               (Tok.isAtStartOfLine() &&
50660b57cec5SDimitry Andric                !isValidAfterTypeSpecifier(CanBeBitfield)))) {
50675ffd83dbSDimitry Andric     // An opaque-enum-declaration is required to be standalone (no preceding or
50685ffd83dbSDimitry Andric     // following tokens in the declaration). Sema enforces this separately by
50695ffd83dbSDimitry Andric     // diagnosing anything else in the DeclSpec.
50700b57cec5SDimitry Andric     TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
50710b57cec5SDimitry Andric     if (Tok.isNot(tok::semi)) {
50720b57cec5SDimitry Andric       // A semicolon was missing after this declaration. Diagnose and recover.
50730b57cec5SDimitry Andric       ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
50740b57cec5SDimitry Andric       PP.EnterToken(Tok, /*IsReinject=*/true);
50750b57cec5SDimitry Andric       Tok.setKind(tok::semi);
50760b57cec5SDimitry Andric     }
50770b57cec5SDimitry Andric   } else {
50780b57cec5SDimitry Andric     TUK = Sema::TUK_Reference;
50790b57cec5SDimitry Andric   }
50800b57cec5SDimitry Andric 
50815ffd83dbSDimitry Andric   bool IsElaboratedTypeSpecifier =
50825ffd83dbSDimitry Andric       TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend;
50835ffd83dbSDimitry Andric 
50845ffd83dbSDimitry Andric   // If this is an elaborated type specifier nested in a larger declaration,
50855ffd83dbSDimitry Andric   // and we delayed diagnostics before, just merge them into the current pool.
50860b57cec5SDimitry Andric   if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
50870b57cec5SDimitry Andric     diagsFromTag.redelay();
50880b57cec5SDimitry Andric   }
50890b57cec5SDimitry Andric 
50900b57cec5SDimitry Andric   MultiTemplateParamsArg TParams;
50910b57cec5SDimitry Andric   if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
50920b57cec5SDimitry Andric       TUK != Sema::TUK_Reference) {
50930b57cec5SDimitry Andric     if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
50940b57cec5SDimitry Andric       // Skip the rest of this declarator, up until the comma or semicolon.
50950b57cec5SDimitry Andric       Diag(Tok, diag::err_enum_template);
50960b57cec5SDimitry Andric       SkipUntil(tok::comma, StopAtSemi);
50970b57cec5SDimitry Andric       return;
50980b57cec5SDimitry Andric     }
50990b57cec5SDimitry Andric 
51000b57cec5SDimitry Andric     if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
51010b57cec5SDimitry Andric       // Enumerations can't be explicitly instantiated.
51020b57cec5SDimitry Andric       DS.SetTypeSpecError();
51030b57cec5SDimitry Andric       Diag(StartLoc, diag::err_explicit_instantiation_enum);
51040b57cec5SDimitry Andric       return;
51050b57cec5SDimitry Andric     }
51060b57cec5SDimitry Andric 
51070b57cec5SDimitry Andric     assert(TemplateInfo.TemplateParams && "no template parameters");
51080b57cec5SDimitry Andric     TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
51090b57cec5SDimitry Andric                                      TemplateInfo.TemplateParams->size());
511006c3fb27SDimitry Andric     SS.setTemplateParamLists(TParams);
51110b57cec5SDimitry Andric   }
51120b57cec5SDimitry Andric 
51130b57cec5SDimitry Andric   if (!Name && TUK != Sema::TUK_Definition) {
51140b57cec5SDimitry Andric     Diag(Tok, diag::err_enumerator_unnamed_no_def);
51150b57cec5SDimitry Andric 
5116bdd1243dSDimitry Andric     DS.SetTypeSpecError();
51170b57cec5SDimitry Andric     // Skip the rest of this declarator, up until the comma or semicolon.
51180b57cec5SDimitry Andric     SkipUntil(tok::comma, StopAtSemi);
51190b57cec5SDimitry Andric     return;
51200b57cec5SDimitry Andric   }
51210b57cec5SDimitry Andric 
51225ffd83dbSDimitry Andric   // An elaborated-type-specifier has a much more constrained grammar:
51235ffd83dbSDimitry Andric   //
51245ffd83dbSDimitry Andric   //   'enum' nested-name-specifier[opt] identifier
51255ffd83dbSDimitry Andric   //
51265ffd83dbSDimitry Andric   // If we parsed any other bits, reject them now.
51275ffd83dbSDimitry Andric   //
51285ffd83dbSDimitry Andric   // MSVC and (for now at least) Objective-C permit a full enum-specifier
51295ffd83dbSDimitry Andric   // or opaque-enum-declaration anywhere.
51305ffd83dbSDimitry Andric   if (IsElaboratedTypeSpecifier && !getLangOpts().MicrosoftExt &&
51315ffd83dbSDimitry Andric       !getLangOpts().ObjC) {
5132fe6060f1SDimitry Andric     ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed,
513306c3fb27SDimitry Andric                             diag::err_keyword_not_allowed,
5134fe6060f1SDimitry Andric                             /*DiagnoseEmptyAttrs=*/true);
51355ffd83dbSDimitry Andric     if (BaseType.isUsable())
51365ffd83dbSDimitry Andric       Diag(BaseRange.getBegin(), diag::ext_enum_base_in_type_specifier)
51375ffd83dbSDimitry Andric           << (AllowEnumSpecifier == AllowDefiningTypeSpec::Yes) << BaseRange;
51385ffd83dbSDimitry Andric     else if (ScopedEnumKWLoc.isValid())
51395ffd83dbSDimitry Andric       Diag(ScopedEnumKWLoc, diag::ext_elaborated_enum_class)
51405ffd83dbSDimitry Andric         << FixItHint::CreateRemoval(ScopedEnumKWLoc) << IsScopedUsingClassTag;
51415ffd83dbSDimitry Andric   }
51425ffd83dbSDimitry Andric 
51430b57cec5SDimitry Andric   stripTypeAttributesOffDeclSpec(attrs, DS, TUK);
51440b57cec5SDimitry Andric 
51450b57cec5SDimitry Andric   Sema::SkipBodyInfo SkipBody;
51460b57cec5SDimitry Andric   if (!Name && TUK == Sema::TUK_Definition && Tok.is(tok::l_brace) &&
51470b57cec5SDimitry Andric       NextToken().is(tok::identifier))
51480b57cec5SDimitry Andric     SkipBody = Actions.shouldSkipAnonEnumBody(getCurScope(),
51490b57cec5SDimitry Andric                                               NextToken().getIdentifierInfo(),
51500b57cec5SDimitry Andric                                               NextToken().getLocation());
51510b57cec5SDimitry Andric 
51520b57cec5SDimitry Andric   bool Owned = false;
51530b57cec5SDimitry Andric   bool IsDependent = false;
51540b57cec5SDimitry Andric   const char *PrevSpec = nullptr;
51550b57cec5SDimitry Andric   unsigned DiagID;
5156bdd1243dSDimitry Andric   Decl *TagDecl =
5157bdd1243dSDimitry Andric       Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, StartLoc, SS,
5158bdd1243dSDimitry Andric                     Name, NameLoc, attrs, AS, DS.getModulePrivateSpecLoc(),
5159bdd1243dSDimitry Andric                     TParams, Owned, IsDependent, ScopedEnumKWLoc,
5160bdd1243dSDimitry Andric                     IsScopedUsingClassTag,
5161bdd1243dSDimitry Andric                     BaseType, DSC == DeclSpecContext::DSC_type_specifier,
51620b57cec5SDimitry Andric                     DSC == DeclSpecContext::DSC_template_param ||
51630b57cec5SDimitry Andric                         DSC == DeclSpecContext::DSC_template_type_arg,
51641ac55f4cSDimitry Andric                     OffsetOfState, &SkipBody).get();
51650b57cec5SDimitry Andric 
51660b57cec5SDimitry Andric   if (SkipBody.ShouldSkip) {
51670b57cec5SDimitry Andric     assert(TUK == Sema::TUK_Definition && "can only skip a definition");
51680b57cec5SDimitry Andric 
51690b57cec5SDimitry Andric     BalancedDelimiterTracker T(*this, tok::l_brace);
51700b57cec5SDimitry Andric     T.consumeOpen();
51710b57cec5SDimitry Andric     T.skipToEnd();
51720b57cec5SDimitry Andric 
51730b57cec5SDimitry Andric     if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
51741ac55f4cSDimitry Andric                            NameLoc.isValid() ? NameLoc : StartLoc,
51751ac55f4cSDimitry Andric                            PrevSpec, DiagID, TagDecl, Owned,
51760b57cec5SDimitry Andric                            Actions.getASTContext().getPrintingPolicy()))
51770b57cec5SDimitry Andric       Diag(StartLoc, DiagID) << PrevSpec;
51780b57cec5SDimitry Andric     return;
51790b57cec5SDimitry Andric   }
51800b57cec5SDimitry Andric 
51810b57cec5SDimitry Andric   if (IsDependent) {
51820b57cec5SDimitry Andric     // This enum has a dependent nested-name-specifier. Handle it as a
51830b57cec5SDimitry Andric     // dependent tag.
51840b57cec5SDimitry Andric     if (!Name) {
51850b57cec5SDimitry Andric       DS.SetTypeSpecError();
51860b57cec5SDimitry Andric       Diag(Tok, diag::err_expected_type_name_after_typename);
51870b57cec5SDimitry Andric       return;
51880b57cec5SDimitry Andric     }
51890b57cec5SDimitry Andric 
51900b57cec5SDimitry Andric     TypeResult Type = Actions.ActOnDependentTag(
51910b57cec5SDimitry Andric         getCurScope(), DeclSpec::TST_enum, TUK, SS, Name, StartLoc, NameLoc);
51920b57cec5SDimitry Andric     if (Type.isInvalid()) {
51930b57cec5SDimitry Andric       DS.SetTypeSpecError();
51940b57cec5SDimitry Andric       return;
51950b57cec5SDimitry Andric     }
51960b57cec5SDimitry Andric 
51970b57cec5SDimitry Andric     if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
51980b57cec5SDimitry Andric                            NameLoc.isValid() ? NameLoc : StartLoc,
51990b57cec5SDimitry Andric                            PrevSpec, DiagID, Type.get(),
52000b57cec5SDimitry Andric                            Actions.getASTContext().getPrintingPolicy()))
52010b57cec5SDimitry Andric       Diag(StartLoc, DiagID) << PrevSpec;
52020b57cec5SDimitry Andric 
52030b57cec5SDimitry Andric     return;
52040b57cec5SDimitry Andric   }
52050b57cec5SDimitry Andric 
52060b57cec5SDimitry Andric   if (!TagDecl) {
52070b57cec5SDimitry Andric     // The action failed to produce an enumeration tag. If this is a
52080b57cec5SDimitry Andric     // definition, consume the entire definition.
52090b57cec5SDimitry Andric     if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
52100b57cec5SDimitry Andric       ConsumeBrace();
52110b57cec5SDimitry Andric       SkipUntil(tok::r_brace, StopAtSemi);
52120b57cec5SDimitry Andric     }
52130b57cec5SDimitry Andric 
52140b57cec5SDimitry Andric     DS.SetTypeSpecError();
52150b57cec5SDimitry Andric     return;
52160b57cec5SDimitry Andric   }
52170b57cec5SDimitry Andric 
52185ffd83dbSDimitry Andric   if (Tok.is(tok::l_brace) && TUK == Sema::TUK_Definition) {
52190b57cec5SDimitry Andric     Decl *D = SkipBody.CheckSameAsPrevious ? SkipBody.New : TagDecl;
52200b57cec5SDimitry Andric     ParseEnumBody(StartLoc, D);
52210b57cec5SDimitry Andric     if (SkipBody.CheckSameAsPrevious &&
522281ad6265SDimitry Andric         !Actions.ActOnDuplicateDefinition(TagDecl, SkipBody)) {
52230b57cec5SDimitry Andric       DS.SetTypeSpecError();
52240b57cec5SDimitry Andric       return;
52250b57cec5SDimitry Andric     }
52260b57cec5SDimitry Andric   }
52270b57cec5SDimitry Andric 
52280b57cec5SDimitry Andric   if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
52291ac55f4cSDimitry Andric                          NameLoc.isValid() ? NameLoc : StartLoc,
52301ac55f4cSDimitry Andric                          PrevSpec, DiagID, TagDecl, Owned,
52310b57cec5SDimitry Andric                          Actions.getASTContext().getPrintingPolicy()))
52320b57cec5SDimitry Andric     Diag(StartLoc, DiagID) << PrevSpec;
52330b57cec5SDimitry Andric }
52340b57cec5SDimitry Andric 
52350b57cec5SDimitry Andric /// ParseEnumBody - Parse a {} enclosed enumerator-list.
52360b57cec5SDimitry Andric ///       enumerator-list:
52370b57cec5SDimitry Andric ///         enumerator
52380b57cec5SDimitry Andric ///         enumerator-list ',' enumerator
52390b57cec5SDimitry Andric ///       enumerator:
52400b57cec5SDimitry Andric ///         enumeration-constant attributes[opt]
52410b57cec5SDimitry Andric ///         enumeration-constant attributes[opt] '=' constant-expression
52420b57cec5SDimitry Andric ///       enumeration-constant:
52430b57cec5SDimitry Andric ///         identifier
52440b57cec5SDimitry Andric ///
52450b57cec5SDimitry Andric void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
52460b57cec5SDimitry Andric   // Enter the scope of the enum body and start the definition.
52470b57cec5SDimitry Andric   ParseScope EnumScope(this, Scope::DeclScope | Scope::EnumScope);
52480b57cec5SDimitry Andric   Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
52490b57cec5SDimitry Andric 
52500b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_brace);
52510b57cec5SDimitry Andric   T.consumeOpen();
52520b57cec5SDimitry Andric 
52530b57cec5SDimitry Andric   // C does not allow an empty enumerator-list, C++ does [dcl.enum].
52540b57cec5SDimitry Andric   if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
52550b57cec5SDimitry Andric     Diag(Tok, diag::err_empty_enum);
52560b57cec5SDimitry Andric 
52570b57cec5SDimitry Andric   SmallVector<Decl *, 32> EnumConstantDecls;
52580b57cec5SDimitry Andric   SmallVector<SuppressAccessChecks, 32> EnumAvailabilityDiags;
52590b57cec5SDimitry Andric 
52600b57cec5SDimitry Andric   Decl *LastEnumConstDecl = nullptr;
52610b57cec5SDimitry Andric 
52620b57cec5SDimitry Andric   // Parse the enumerator-list.
52630b57cec5SDimitry Andric   while (Tok.isNot(tok::r_brace)) {
52640b57cec5SDimitry Andric     // Parse enumerator. If failed, try skipping till the start of the next
52650b57cec5SDimitry Andric     // enumerator definition.
52660b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
52670b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
52680b57cec5SDimitry Andric       if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) &&
52690b57cec5SDimitry Andric           TryConsumeToken(tok::comma))
52700b57cec5SDimitry Andric         continue;
52710b57cec5SDimitry Andric       break;
52720b57cec5SDimitry Andric     }
52730b57cec5SDimitry Andric     IdentifierInfo *Ident = Tok.getIdentifierInfo();
52740b57cec5SDimitry Andric     SourceLocation IdentLoc = ConsumeToken();
52750b57cec5SDimitry Andric 
52760b57cec5SDimitry Andric     // If attributes exist after the enumerator, parse them.
527781ad6265SDimitry Andric     ParsedAttributes attrs(AttrFactory);
52780b57cec5SDimitry Andric     MaybeParseGNUAttributes(attrs);
527906c3fb27SDimitry Andric     if (isAllowedCXX11AttributeSpecifier()) {
52800b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus)
52810b57cec5SDimitry Andric         Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
52820b57cec5SDimitry Andric                                     ? diag::warn_cxx14_compat_ns_enum_attribute
52830b57cec5SDimitry Andric                                     : diag::ext_ns_enum_attribute)
52840b57cec5SDimitry Andric             << 1 /*enumerator*/;
52850b57cec5SDimitry Andric       ParseCXX11Attributes(attrs);
52860b57cec5SDimitry Andric     }
52870b57cec5SDimitry Andric 
52880b57cec5SDimitry Andric     SourceLocation EqualLoc;
52890b57cec5SDimitry Andric     ExprResult AssignedVal;
52900b57cec5SDimitry Andric     EnumAvailabilityDiags.emplace_back(*this);
52910b57cec5SDimitry Andric 
5292a7dea167SDimitry Andric     EnterExpressionEvaluationContext ConstantEvaluated(
5293a7dea167SDimitry Andric         Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
52940b57cec5SDimitry Andric     if (TryConsumeToken(tok::equal, EqualLoc)) {
5295a7dea167SDimitry Andric       AssignedVal = ParseConstantExpressionInExprEvalContext();
52960b57cec5SDimitry Andric       if (AssignedVal.isInvalid())
52970b57cec5SDimitry Andric         SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch);
52980b57cec5SDimitry Andric     }
52990b57cec5SDimitry Andric 
53000b57cec5SDimitry Andric     // Install the enumerator constant into EnumDecl.
53010b57cec5SDimitry Andric     Decl *EnumConstDecl = Actions.ActOnEnumConstant(
53020b57cec5SDimitry Andric         getCurScope(), EnumDecl, LastEnumConstDecl, IdentLoc, Ident, attrs,
53030b57cec5SDimitry Andric         EqualLoc, AssignedVal.get());
53040b57cec5SDimitry Andric     EnumAvailabilityDiags.back().done();
53050b57cec5SDimitry Andric 
53060b57cec5SDimitry Andric     EnumConstantDecls.push_back(EnumConstDecl);
53070b57cec5SDimitry Andric     LastEnumConstDecl = EnumConstDecl;
53080b57cec5SDimitry Andric 
53090b57cec5SDimitry Andric     if (Tok.is(tok::identifier)) {
53100b57cec5SDimitry Andric       // We're missing a comma between enumerators.
53110b57cec5SDimitry Andric       SourceLocation Loc = getEndOfPreviousToken();
53120b57cec5SDimitry Andric       Diag(Loc, diag::err_enumerator_list_missing_comma)
53130b57cec5SDimitry Andric         << FixItHint::CreateInsertion(Loc, ", ");
53140b57cec5SDimitry Andric       continue;
53150b57cec5SDimitry Andric     }
53160b57cec5SDimitry Andric 
53170b57cec5SDimitry Andric     // Emumerator definition must be finished, only comma or r_brace are
53180b57cec5SDimitry Andric     // allowed here.
53190b57cec5SDimitry Andric     SourceLocation CommaLoc;
53200b57cec5SDimitry Andric     if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) {
53210b57cec5SDimitry Andric       if (EqualLoc.isValid())
53220b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace
53230b57cec5SDimitry Andric                                                            << tok::comma;
53240b57cec5SDimitry Andric       else
53250b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator);
53260b57cec5SDimitry Andric       if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) {
53270b57cec5SDimitry Andric         if (TryConsumeToken(tok::comma, CommaLoc))
53280b57cec5SDimitry Andric           continue;
53290b57cec5SDimitry Andric       } else {
53300b57cec5SDimitry Andric         break;
53310b57cec5SDimitry Andric       }
53320b57cec5SDimitry Andric     }
53330b57cec5SDimitry Andric 
53340b57cec5SDimitry Andric     // If comma is followed by r_brace, emit appropriate warning.
53350b57cec5SDimitry Andric     if (Tok.is(tok::r_brace) && CommaLoc.isValid()) {
53360b57cec5SDimitry Andric       if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
53370b57cec5SDimitry Andric         Diag(CommaLoc, getLangOpts().CPlusPlus ?
53380b57cec5SDimitry Andric                diag::ext_enumerator_list_comma_cxx :
53390b57cec5SDimitry Andric                diag::ext_enumerator_list_comma_c)
53400b57cec5SDimitry Andric           << FixItHint::CreateRemoval(CommaLoc);
53410b57cec5SDimitry Andric       else if (getLangOpts().CPlusPlus11)
53420b57cec5SDimitry Andric         Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
53430b57cec5SDimitry Andric           << FixItHint::CreateRemoval(CommaLoc);
53440b57cec5SDimitry Andric       break;
53450b57cec5SDimitry Andric     }
53460b57cec5SDimitry Andric   }
53470b57cec5SDimitry Andric 
53480b57cec5SDimitry Andric   // Eat the }.
53490b57cec5SDimitry Andric   T.consumeClose();
53500b57cec5SDimitry Andric 
53510b57cec5SDimitry Andric   // If attributes exist after the identifier list, parse them.
53520b57cec5SDimitry Andric   ParsedAttributes attrs(AttrFactory);
53530b57cec5SDimitry Andric   MaybeParseGNUAttributes(attrs);
53540b57cec5SDimitry Andric 
53550b57cec5SDimitry Andric   Actions.ActOnEnumBody(StartLoc, T.getRange(), EnumDecl, EnumConstantDecls,
53560b57cec5SDimitry Andric                         getCurScope(), attrs);
53570b57cec5SDimitry Andric 
53580b57cec5SDimitry Andric   // Now handle enum constant availability diagnostics.
53590b57cec5SDimitry Andric   assert(EnumConstantDecls.size() == EnumAvailabilityDiags.size());
53600b57cec5SDimitry Andric   for (size_t i = 0, e = EnumConstantDecls.size(); i != e; ++i) {
53610b57cec5SDimitry Andric     ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
53620b57cec5SDimitry Andric     EnumAvailabilityDiags[i].redelay();
53630b57cec5SDimitry Andric     PD.complete(EnumConstantDecls[i]);
53640b57cec5SDimitry Andric   }
53650b57cec5SDimitry Andric 
53660b57cec5SDimitry Andric   EnumScope.Exit();
53670b57cec5SDimitry Andric   Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, T.getRange());
53680b57cec5SDimitry Andric 
53690b57cec5SDimitry Andric   // The next token must be valid after an enum definition. If not, a ';'
53700b57cec5SDimitry Andric   // was probably forgotten.
537181ad6265SDimitry Andric   bool CanBeBitfield = getCurScope()->isClassScope();
53720b57cec5SDimitry Andric   if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
53730b57cec5SDimitry Andric     ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
53740b57cec5SDimitry Andric     // Push this token back into the preprocessor and change our current token
53750b57cec5SDimitry Andric     // to ';' so that the rest of the code recovers as though there were an
53760b57cec5SDimitry Andric     // ';' after the definition.
53770b57cec5SDimitry Andric     PP.EnterToken(Tok, /*IsReinject=*/true);
53780b57cec5SDimitry Andric     Tok.setKind(tok::semi);
53790b57cec5SDimitry Andric   }
53800b57cec5SDimitry Andric }
53810b57cec5SDimitry Andric 
53820b57cec5SDimitry Andric /// isKnownToBeTypeSpecifier - Return true if we know that the specified token
53830b57cec5SDimitry Andric /// is definitely a type-specifier.  Return false if it isn't part of a type
53840b57cec5SDimitry Andric /// specifier or if we're not sure.
53850b57cec5SDimitry Andric bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
53860b57cec5SDimitry Andric   switch (Tok.getKind()) {
53870b57cec5SDimitry Andric   default: return false;
53880b57cec5SDimitry Andric     // type-specifiers
53890b57cec5SDimitry Andric   case tok::kw_short:
53900b57cec5SDimitry Andric   case tok::kw_long:
53910b57cec5SDimitry Andric   case tok::kw___int64:
53920b57cec5SDimitry Andric   case tok::kw___int128:
53930b57cec5SDimitry Andric   case tok::kw_signed:
53940b57cec5SDimitry Andric   case tok::kw_unsigned:
53950b57cec5SDimitry Andric   case tok::kw__Complex:
53960b57cec5SDimitry Andric   case tok::kw__Imaginary:
53970b57cec5SDimitry Andric   case tok::kw_void:
53980b57cec5SDimitry Andric   case tok::kw_char:
53990b57cec5SDimitry Andric   case tok::kw_wchar_t:
54000b57cec5SDimitry Andric   case tok::kw_char8_t:
54010b57cec5SDimitry Andric   case tok::kw_char16_t:
54020b57cec5SDimitry Andric   case tok::kw_char32_t:
54030b57cec5SDimitry Andric   case tok::kw_int:
54045ffd83dbSDimitry Andric   case tok::kw__ExtInt:
54050eae32dcSDimitry Andric   case tok::kw__BitInt:
54065ffd83dbSDimitry Andric   case tok::kw___bf16:
54070b57cec5SDimitry Andric   case tok::kw_half:
54080b57cec5SDimitry Andric   case tok::kw_float:
54090b57cec5SDimitry Andric   case tok::kw_double:
54100b57cec5SDimitry Andric   case tok::kw__Accum:
54110b57cec5SDimitry Andric   case tok::kw__Fract:
54120b57cec5SDimitry Andric   case tok::kw__Float16:
54130b57cec5SDimitry Andric   case tok::kw___float128:
5414349cc55cSDimitry Andric   case tok::kw___ibm128:
54150b57cec5SDimitry Andric   case tok::kw_bool:
54160b57cec5SDimitry Andric   case tok::kw__Bool:
54170b57cec5SDimitry Andric   case tok::kw__Decimal32:
54180b57cec5SDimitry Andric   case tok::kw__Decimal64:
54190b57cec5SDimitry Andric   case tok::kw__Decimal128:
54200b57cec5SDimitry Andric   case tok::kw___vector:
54210b57cec5SDimitry Andric #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
54220b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def"
54230b57cec5SDimitry Andric 
54240b57cec5SDimitry Andric     // struct-or-union-specifier (C99) or class-specifier (C++)
54250b57cec5SDimitry Andric   case tok::kw_class:
54260b57cec5SDimitry Andric   case tok::kw_struct:
54270b57cec5SDimitry Andric   case tok::kw___interface:
54280b57cec5SDimitry Andric   case tok::kw_union:
54290b57cec5SDimitry Andric     // enum-specifier
54300b57cec5SDimitry Andric   case tok::kw_enum:
54310b57cec5SDimitry Andric 
54320b57cec5SDimitry Andric     // typedef-name
54330b57cec5SDimitry Andric   case tok::annot_typename:
54340b57cec5SDimitry Andric     return true;
54350b57cec5SDimitry Andric   }
54360b57cec5SDimitry Andric }
54370b57cec5SDimitry Andric 
54380b57cec5SDimitry Andric /// isTypeSpecifierQualifier - Return true if the current token could be the
54390b57cec5SDimitry Andric /// start of a specifier-qualifier-list.
54400b57cec5SDimitry Andric bool Parser::isTypeSpecifierQualifier() {
54410b57cec5SDimitry Andric   switch (Tok.getKind()) {
54420b57cec5SDimitry Andric   default: return false;
54430b57cec5SDimitry Andric 
54440b57cec5SDimitry Andric   case tok::identifier:   // foo::bar
54450b57cec5SDimitry Andric     if (TryAltiVecVectorToken())
54460b57cec5SDimitry Andric       return true;
5447bdd1243dSDimitry Andric     [[fallthrough]];
54480b57cec5SDimitry Andric   case tok::kw_typename:  // typename T::type
54490b57cec5SDimitry Andric     // Annotate typenames and C++ scope specifiers.  If we get one, just
54500b57cec5SDimitry Andric     // recurse to handle whatever we get.
54510b57cec5SDimitry Andric     if (TryAnnotateTypeOrScopeToken())
54520b57cec5SDimitry Andric       return true;
54530b57cec5SDimitry Andric     if (Tok.is(tok::identifier))
54540b57cec5SDimitry Andric       return false;
54550b57cec5SDimitry Andric     return isTypeSpecifierQualifier();
54560b57cec5SDimitry Andric 
54570b57cec5SDimitry Andric   case tok::coloncolon:   // ::foo::bar
54580b57cec5SDimitry Andric     if (NextToken().is(tok::kw_new) ||    // ::new
54590b57cec5SDimitry Andric         NextToken().is(tok::kw_delete))   // ::delete
54600b57cec5SDimitry Andric       return false;
54610b57cec5SDimitry Andric 
54620b57cec5SDimitry Andric     if (TryAnnotateTypeOrScopeToken())
54630b57cec5SDimitry Andric       return true;
54640b57cec5SDimitry Andric     return isTypeSpecifierQualifier();
54650b57cec5SDimitry Andric 
54660b57cec5SDimitry Andric     // GNU attributes support.
54670b57cec5SDimitry Andric   case tok::kw___attribute:
54685f757f3fSDimitry Andric     // C23/GNU typeof support.
54690b57cec5SDimitry Andric   case tok::kw_typeof:
5470bdd1243dSDimitry Andric   case tok::kw_typeof_unqual:
54710b57cec5SDimitry Andric 
54720b57cec5SDimitry Andric     // type-specifiers
54730b57cec5SDimitry Andric   case tok::kw_short:
54740b57cec5SDimitry Andric   case tok::kw_long:
54750b57cec5SDimitry Andric   case tok::kw___int64:
54760b57cec5SDimitry Andric   case tok::kw___int128:
54770b57cec5SDimitry Andric   case tok::kw_signed:
54780b57cec5SDimitry Andric   case tok::kw_unsigned:
54790b57cec5SDimitry Andric   case tok::kw__Complex:
54800b57cec5SDimitry Andric   case tok::kw__Imaginary:
54810b57cec5SDimitry Andric   case tok::kw_void:
54820b57cec5SDimitry Andric   case tok::kw_char:
54830b57cec5SDimitry Andric   case tok::kw_wchar_t:
54840b57cec5SDimitry Andric   case tok::kw_char8_t:
54850b57cec5SDimitry Andric   case tok::kw_char16_t:
54860b57cec5SDimitry Andric   case tok::kw_char32_t:
54870b57cec5SDimitry Andric   case tok::kw_int:
54885ffd83dbSDimitry Andric   case tok::kw__ExtInt:
54890eae32dcSDimitry Andric   case tok::kw__BitInt:
54900b57cec5SDimitry Andric   case tok::kw_half:
54915ffd83dbSDimitry Andric   case tok::kw___bf16:
54920b57cec5SDimitry Andric   case tok::kw_float:
54930b57cec5SDimitry Andric   case tok::kw_double:
54940b57cec5SDimitry Andric   case tok::kw__Accum:
54950b57cec5SDimitry Andric   case tok::kw__Fract:
54960b57cec5SDimitry Andric   case tok::kw__Float16:
54970b57cec5SDimitry Andric   case tok::kw___float128:
5498349cc55cSDimitry Andric   case tok::kw___ibm128:
54990b57cec5SDimitry Andric   case tok::kw_bool:
55000b57cec5SDimitry Andric   case tok::kw__Bool:
55010b57cec5SDimitry Andric   case tok::kw__Decimal32:
55020b57cec5SDimitry Andric   case tok::kw__Decimal64:
55030b57cec5SDimitry Andric   case tok::kw__Decimal128:
55040b57cec5SDimitry Andric   case tok::kw___vector:
55050b57cec5SDimitry Andric #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
55060b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def"
55070b57cec5SDimitry Andric 
55080b57cec5SDimitry Andric     // struct-or-union-specifier (C99) or class-specifier (C++)
55090b57cec5SDimitry Andric   case tok::kw_class:
55100b57cec5SDimitry Andric   case tok::kw_struct:
55110b57cec5SDimitry Andric   case tok::kw___interface:
55120b57cec5SDimitry Andric   case tok::kw_union:
55130b57cec5SDimitry Andric     // enum-specifier
55140b57cec5SDimitry Andric   case tok::kw_enum:
55150b57cec5SDimitry Andric 
55160b57cec5SDimitry Andric     // type-qualifier
55170b57cec5SDimitry Andric   case tok::kw_const:
55180b57cec5SDimitry Andric   case tok::kw_volatile:
55190b57cec5SDimitry Andric   case tok::kw_restrict:
55200b57cec5SDimitry Andric   case tok::kw__Sat:
55210b57cec5SDimitry Andric 
55220b57cec5SDimitry Andric     // Debugger support.
55230b57cec5SDimitry Andric   case tok::kw___unknown_anytype:
55240b57cec5SDimitry Andric 
55250b57cec5SDimitry Andric     // typedef-name
55260b57cec5SDimitry Andric   case tok::annot_typename:
55270b57cec5SDimitry Andric     return true;
55280b57cec5SDimitry Andric 
55290b57cec5SDimitry Andric     // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
55300b57cec5SDimitry Andric   case tok::less:
55310b57cec5SDimitry Andric     return getLangOpts().ObjC;
55320b57cec5SDimitry Andric 
55330b57cec5SDimitry Andric   case tok::kw___cdecl:
55340b57cec5SDimitry Andric   case tok::kw___stdcall:
55350b57cec5SDimitry Andric   case tok::kw___fastcall:
55360b57cec5SDimitry Andric   case tok::kw___thiscall:
55370b57cec5SDimitry Andric   case tok::kw___regcall:
55380b57cec5SDimitry Andric   case tok::kw___vectorcall:
55390b57cec5SDimitry Andric   case tok::kw___w64:
55400b57cec5SDimitry Andric   case tok::kw___ptr64:
55410b57cec5SDimitry Andric   case tok::kw___ptr32:
55420b57cec5SDimitry Andric   case tok::kw___pascal:
55430b57cec5SDimitry Andric   case tok::kw___unaligned:
55440b57cec5SDimitry Andric 
55450b57cec5SDimitry Andric   case tok::kw__Nonnull:
55460b57cec5SDimitry Andric   case tok::kw__Nullable:
5547e8d8bef9SDimitry Andric   case tok::kw__Nullable_result:
55480b57cec5SDimitry Andric   case tok::kw__Null_unspecified:
55490b57cec5SDimitry Andric 
55500b57cec5SDimitry Andric   case tok::kw___kindof:
55510b57cec5SDimitry Andric 
55520b57cec5SDimitry Andric   case tok::kw___private:
55530b57cec5SDimitry Andric   case tok::kw___local:
55540b57cec5SDimitry Andric   case tok::kw___global:
55550b57cec5SDimitry Andric   case tok::kw___constant:
55560b57cec5SDimitry Andric   case tok::kw___generic:
55570b57cec5SDimitry Andric   case tok::kw___read_only:
55580b57cec5SDimitry Andric   case tok::kw___read_write:
55590b57cec5SDimitry Andric   case tok::kw___write_only:
556006c3fb27SDimitry Andric   case tok::kw___funcref:
55610b57cec5SDimitry Andric     return true;
55620b57cec5SDimitry Andric 
55630b57cec5SDimitry Andric   case tok::kw_private:
55640b57cec5SDimitry Andric     return getLangOpts().OpenCL;
55650b57cec5SDimitry Andric 
55660b57cec5SDimitry Andric   // C11 _Atomic
55670b57cec5SDimitry Andric   case tok::kw__Atomic:
55680b57cec5SDimitry Andric     return true;
55695f757f3fSDimitry Andric 
55705f757f3fSDimitry Andric   // HLSL type qualifiers
55715f757f3fSDimitry Andric   case tok::kw_groupshared:
55725f757f3fSDimitry Andric   case tok::kw_in:
55735f757f3fSDimitry Andric   case tok::kw_inout:
55745f757f3fSDimitry Andric   case tok::kw_out:
55755f757f3fSDimitry Andric     return getLangOpts().HLSL;
55760b57cec5SDimitry Andric   }
55770b57cec5SDimitry Andric }
55780b57cec5SDimitry Andric 
5579bdd1243dSDimitry Andric Parser::DeclGroupPtrTy Parser::ParseTopLevelStmtDecl() {
5580bdd1243dSDimitry Andric   assert(PP.isIncrementalProcessingEnabled() && "Not in incremental mode");
5581bdd1243dSDimitry Andric 
5582bdd1243dSDimitry Andric   // Parse a top-level-stmt.
5583bdd1243dSDimitry Andric   Parser::StmtVector Stmts;
5584bdd1243dSDimitry Andric   ParsedStmtContext SubStmtCtx = ParsedStmtContext();
558506c3fb27SDimitry Andric   Actions.PushFunctionScope();
5586bdd1243dSDimitry Andric   StmtResult R = ParseStatementOrDeclaration(Stmts, SubStmtCtx);
558706c3fb27SDimitry Andric   Actions.PopFunctionScopeInfo();
5588bdd1243dSDimitry Andric   if (!R.isUsable())
5589bdd1243dSDimitry Andric     return nullptr;
5590bdd1243dSDimitry Andric 
5591bdd1243dSDimitry Andric   SmallVector<Decl *, 2> DeclsInGroup;
5592bdd1243dSDimitry Andric   DeclsInGroup.push_back(Actions.ActOnTopLevelStmtDecl(R.get()));
559306c3fb27SDimitry Andric 
559406c3fb27SDimitry Andric   if (Tok.is(tok::annot_repl_input_end) &&
559506c3fb27SDimitry Andric       Tok.getAnnotationValue() != nullptr) {
559606c3fb27SDimitry Andric     ConsumeAnnotationToken();
559706c3fb27SDimitry Andric     cast<TopLevelStmtDecl>(DeclsInGroup.back())->setSemiMissing();
559806c3fb27SDimitry Andric   }
559906c3fb27SDimitry Andric 
5600bdd1243dSDimitry Andric   // Currently happens for things like  -fms-extensions and use `__if_exists`.
5601bdd1243dSDimitry Andric   for (Stmt *S : Stmts)
5602bdd1243dSDimitry Andric     DeclsInGroup.push_back(Actions.ActOnTopLevelStmtDecl(S));
5603bdd1243dSDimitry Andric 
5604bdd1243dSDimitry Andric   return Actions.BuildDeclaratorGroup(DeclsInGroup);
5605bdd1243dSDimitry Andric }
5606bdd1243dSDimitry Andric 
56070b57cec5SDimitry Andric /// isDeclarationSpecifier() - Return true if the current token is part of a
56080b57cec5SDimitry Andric /// declaration specifier.
56090b57cec5SDimitry Andric ///
5610bdd1243dSDimitry Andric /// \param AllowImplicitTypename whether this is a context where T::type [T
5611bdd1243dSDimitry Andric /// dependent] can appear.
56120b57cec5SDimitry Andric /// \param DisambiguatingWithExpression True to indicate that the purpose of
56130b57cec5SDimitry Andric /// this check is to disambiguate between an expression and a declaration.
5614bdd1243dSDimitry Andric bool Parser::isDeclarationSpecifier(
5615bdd1243dSDimitry Andric     ImplicitTypenameContext AllowImplicitTypename,
5616bdd1243dSDimitry Andric     bool DisambiguatingWithExpression) {
56170b57cec5SDimitry Andric   switch (Tok.getKind()) {
56180b57cec5SDimitry Andric   default: return false;
56190b57cec5SDimitry Andric 
56206e75b2fbSDimitry Andric   // OpenCL 2.0 and later define this keyword.
56210b57cec5SDimitry Andric   case tok::kw_pipe:
5622349cc55cSDimitry Andric     return getLangOpts().OpenCL &&
5623349cc55cSDimitry Andric            getLangOpts().getOpenCLCompatibleVersion() >= 200;
56240b57cec5SDimitry Andric 
56250b57cec5SDimitry Andric   case tok::identifier:   // foo::bar
56260b57cec5SDimitry Andric     // Unfortunate hack to support "Class.factoryMethod" notation.
56270b57cec5SDimitry Andric     if (getLangOpts().ObjC && NextToken().is(tok::period))
56280b57cec5SDimitry Andric       return false;
56290b57cec5SDimitry Andric     if (TryAltiVecVectorToken())
56300b57cec5SDimitry Andric       return true;
5631bdd1243dSDimitry Andric     [[fallthrough]];
56320b57cec5SDimitry Andric   case tok::kw_decltype: // decltype(T())::type
56330b57cec5SDimitry Andric   case tok::kw_typename: // typename T::type
56340b57cec5SDimitry Andric     // Annotate typenames and C++ scope specifiers.  If we get one, just
56350b57cec5SDimitry Andric     // recurse to handle whatever we get.
5636bdd1243dSDimitry Andric     if (TryAnnotateTypeOrScopeToken(AllowImplicitTypename))
56370b57cec5SDimitry Andric       return true;
563813138422SDimitry Andric     if (TryAnnotateTypeConstraint())
563913138422SDimitry Andric       return true;
56400b57cec5SDimitry Andric     if (Tok.is(tok::identifier))
56410b57cec5SDimitry Andric       return false;
56420b57cec5SDimitry Andric 
56430b57cec5SDimitry Andric     // If we're in Objective-C and we have an Objective-C class type followed
56440b57cec5SDimitry Andric     // by an identifier and then either ':' or ']', in a place where an
56450b57cec5SDimitry Andric     // expression is permitted, then this is probably a class message send
56460b57cec5SDimitry Andric     // missing the initial '['. In this case, we won't consider this to be
56470b57cec5SDimitry Andric     // the start of a declaration.
56480b57cec5SDimitry Andric     if (DisambiguatingWithExpression &&
56490b57cec5SDimitry Andric         isStartOfObjCClassMessageMissingOpenBracket())
56500b57cec5SDimitry Andric       return false;
56510b57cec5SDimitry Andric 
5652bdd1243dSDimitry Andric     return isDeclarationSpecifier(AllowImplicitTypename);
56530b57cec5SDimitry Andric 
56540b57cec5SDimitry Andric   case tok::coloncolon:   // ::foo::bar
5655bdd1243dSDimitry Andric     if (!getLangOpts().CPlusPlus)
5656bdd1243dSDimitry Andric       return false;
56570b57cec5SDimitry Andric     if (NextToken().is(tok::kw_new) ||    // ::new
56580b57cec5SDimitry Andric         NextToken().is(tok::kw_delete))   // ::delete
56590b57cec5SDimitry Andric       return false;
56600b57cec5SDimitry Andric 
56610b57cec5SDimitry Andric     // Annotate typenames and C++ scope specifiers.  If we get one, just
56620b57cec5SDimitry Andric     // recurse to handle whatever we get.
56630b57cec5SDimitry Andric     if (TryAnnotateTypeOrScopeToken())
56640b57cec5SDimitry Andric       return true;
5665bdd1243dSDimitry Andric     return isDeclarationSpecifier(ImplicitTypenameContext::No);
56660b57cec5SDimitry Andric 
56670b57cec5SDimitry Andric     // storage-class-specifier
56680b57cec5SDimitry Andric   case tok::kw_typedef:
56690b57cec5SDimitry Andric   case tok::kw_extern:
56700b57cec5SDimitry Andric   case tok::kw___private_extern__:
56710b57cec5SDimitry Andric   case tok::kw_static:
56720b57cec5SDimitry Andric   case tok::kw_auto:
56730b57cec5SDimitry Andric   case tok::kw___auto_type:
56740b57cec5SDimitry Andric   case tok::kw_register:
56750b57cec5SDimitry Andric   case tok::kw___thread:
56760b57cec5SDimitry Andric   case tok::kw_thread_local:
56770b57cec5SDimitry Andric   case tok::kw__Thread_local:
56780b57cec5SDimitry Andric 
56790b57cec5SDimitry Andric     // Modules
56800b57cec5SDimitry Andric   case tok::kw___module_private__:
56810b57cec5SDimitry Andric 
56820b57cec5SDimitry Andric     // Debugger support
56830b57cec5SDimitry Andric   case tok::kw___unknown_anytype:
56840b57cec5SDimitry Andric 
56850b57cec5SDimitry Andric     // type-specifiers
56860b57cec5SDimitry Andric   case tok::kw_short:
56870b57cec5SDimitry Andric   case tok::kw_long:
56880b57cec5SDimitry Andric   case tok::kw___int64:
56890b57cec5SDimitry Andric   case tok::kw___int128:
56900b57cec5SDimitry Andric   case tok::kw_signed:
56910b57cec5SDimitry Andric   case tok::kw_unsigned:
56920b57cec5SDimitry Andric   case tok::kw__Complex:
56930b57cec5SDimitry Andric   case tok::kw__Imaginary:
56940b57cec5SDimitry Andric   case tok::kw_void:
56950b57cec5SDimitry Andric   case tok::kw_char:
56960b57cec5SDimitry Andric   case tok::kw_wchar_t:
56970b57cec5SDimitry Andric   case tok::kw_char8_t:
56980b57cec5SDimitry Andric   case tok::kw_char16_t:
56990b57cec5SDimitry Andric   case tok::kw_char32_t:
57000b57cec5SDimitry Andric 
57010b57cec5SDimitry Andric   case tok::kw_int:
57025ffd83dbSDimitry Andric   case tok::kw__ExtInt:
57030eae32dcSDimitry Andric   case tok::kw__BitInt:
57040b57cec5SDimitry Andric   case tok::kw_half:
57055ffd83dbSDimitry Andric   case tok::kw___bf16:
57060b57cec5SDimitry Andric   case tok::kw_float:
57070b57cec5SDimitry Andric   case tok::kw_double:
57080b57cec5SDimitry Andric   case tok::kw__Accum:
57090b57cec5SDimitry Andric   case tok::kw__Fract:
57100b57cec5SDimitry Andric   case tok::kw__Float16:
57110b57cec5SDimitry Andric   case tok::kw___float128:
5712349cc55cSDimitry Andric   case tok::kw___ibm128:
57130b57cec5SDimitry Andric   case tok::kw_bool:
57140b57cec5SDimitry Andric   case tok::kw__Bool:
57150b57cec5SDimitry Andric   case tok::kw__Decimal32:
57160b57cec5SDimitry Andric   case tok::kw__Decimal64:
57170b57cec5SDimitry Andric   case tok::kw__Decimal128:
57180b57cec5SDimitry Andric   case tok::kw___vector:
57190b57cec5SDimitry Andric 
57200b57cec5SDimitry Andric     // struct-or-union-specifier (C99) or class-specifier (C++)
57210b57cec5SDimitry Andric   case tok::kw_class:
57220b57cec5SDimitry Andric   case tok::kw_struct:
57230b57cec5SDimitry Andric   case tok::kw_union:
57240b57cec5SDimitry Andric   case tok::kw___interface:
57250b57cec5SDimitry Andric     // enum-specifier
57260b57cec5SDimitry Andric   case tok::kw_enum:
57270b57cec5SDimitry Andric 
57280b57cec5SDimitry Andric     // type-qualifier
57290b57cec5SDimitry Andric   case tok::kw_const:
57300b57cec5SDimitry Andric   case tok::kw_volatile:
57310b57cec5SDimitry Andric   case tok::kw_restrict:
57320b57cec5SDimitry Andric   case tok::kw__Sat:
57330b57cec5SDimitry Andric 
57340b57cec5SDimitry Andric     // function-specifier
57350b57cec5SDimitry Andric   case tok::kw_inline:
57360b57cec5SDimitry Andric   case tok::kw_virtual:
57370b57cec5SDimitry Andric   case tok::kw_explicit:
57380b57cec5SDimitry Andric   case tok::kw__Noreturn:
57390b57cec5SDimitry Andric 
57400b57cec5SDimitry Andric     // alignment-specifier
57410b57cec5SDimitry Andric   case tok::kw__Alignas:
57420b57cec5SDimitry Andric 
57430b57cec5SDimitry Andric     // friend keyword.
57440b57cec5SDimitry Andric   case tok::kw_friend:
57450b57cec5SDimitry Andric 
57460b57cec5SDimitry Andric     // static_assert-declaration
5747d409305fSDimitry Andric   case tok::kw_static_assert:
57480b57cec5SDimitry Andric   case tok::kw__Static_assert:
57490b57cec5SDimitry Andric 
57505f757f3fSDimitry Andric     // C23/GNU typeof support.
57510b57cec5SDimitry Andric   case tok::kw_typeof:
5752bdd1243dSDimitry Andric   case tok::kw_typeof_unqual:
57530b57cec5SDimitry Andric 
57540b57cec5SDimitry Andric     // GNU attributes.
57550b57cec5SDimitry Andric   case tok::kw___attribute:
57560b57cec5SDimitry Andric 
57570b57cec5SDimitry Andric     // C++11 decltype and constexpr.
57580b57cec5SDimitry Andric   case tok::annot_decltype:
57590b57cec5SDimitry Andric   case tok::kw_constexpr:
57600b57cec5SDimitry Andric 
5761a7dea167SDimitry Andric     // C++20 consteval and constinit.
57620b57cec5SDimitry Andric   case tok::kw_consteval:
5763a7dea167SDimitry Andric   case tok::kw_constinit:
57640b57cec5SDimitry Andric 
57650b57cec5SDimitry Andric     // C11 _Atomic
57660b57cec5SDimitry Andric   case tok::kw__Atomic:
57670b57cec5SDimitry Andric     return true;
57680b57cec5SDimitry Andric 
57690b57cec5SDimitry Andric     // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
57700b57cec5SDimitry Andric   case tok::less:
57710b57cec5SDimitry Andric     return getLangOpts().ObjC;
57720b57cec5SDimitry Andric 
57730b57cec5SDimitry Andric     // typedef-name
57740b57cec5SDimitry Andric   case tok::annot_typename:
57750b57cec5SDimitry Andric     return !DisambiguatingWithExpression ||
57760b57cec5SDimitry Andric            !isStartOfObjCClassMessageMissingOpenBracket();
57770b57cec5SDimitry Andric 
5778480093f4SDimitry Andric     // placeholder-type-specifier
5779480093f4SDimitry Andric   case tok::annot_template_id: {
57805ffd83dbSDimitry Andric     TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
57815ffd83dbSDimitry Andric     if (TemplateId->hasInvalidName())
57825ffd83dbSDimitry Andric       return true;
57835ffd83dbSDimitry Andric     // FIXME: What about type templates that have only been annotated as
57845ffd83dbSDimitry Andric     // annot_template_id, not as annot_typename?
578513138422SDimitry Andric     return isTypeConstraintAnnotation() &&
5786480093f4SDimitry Andric            (NextToken().is(tok::kw_auto) || NextToken().is(tok::kw_decltype));
5787480093f4SDimitry Andric   }
57885ffd83dbSDimitry Andric 
57895ffd83dbSDimitry Andric   case tok::annot_cxxscope: {
57905ffd83dbSDimitry Andric     TemplateIdAnnotation *TemplateId =
57915ffd83dbSDimitry Andric         NextToken().is(tok::annot_template_id)
57925ffd83dbSDimitry Andric             ? takeTemplateIdAnnotation(NextToken())
57935ffd83dbSDimitry Andric             : nullptr;
57945ffd83dbSDimitry Andric     if (TemplateId && TemplateId->hasInvalidName())
57955ffd83dbSDimitry Andric       return true;
57965ffd83dbSDimitry Andric     // FIXME: What about type templates that have only been annotated as
57975ffd83dbSDimitry Andric     // annot_template_id, not as annot_typename?
579813138422SDimitry Andric     if (NextToken().is(tok::identifier) && TryAnnotateTypeConstraint())
579913138422SDimitry Andric       return true;
580013138422SDimitry Andric     return isTypeConstraintAnnotation() &&
580113138422SDimitry Andric         GetLookAheadToken(2).isOneOf(tok::kw_auto, tok::kw_decltype);
58025ffd83dbSDimitry Andric   }
58035ffd83dbSDimitry Andric 
58040b57cec5SDimitry Andric   case tok::kw___declspec:
58050b57cec5SDimitry Andric   case tok::kw___cdecl:
58060b57cec5SDimitry Andric   case tok::kw___stdcall:
58070b57cec5SDimitry Andric   case tok::kw___fastcall:
58080b57cec5SDimitry Andric   case tok::kw___thiscall:
58090b57cec5SDimitry Andric   case tok::kw___regcall:
58100b57cec5SDimitry Andric   case tok::kw___vectorcall:
58110b57cec5SDimitry Andric   case tok::kw___w64:
58120b57cec5SDimitry Andric   case tok::kw___sptr:
58130b57cec5SDimitry Andric   case tok::kw___uptr:
58140b57cec5SDimitry Andric   case tok::kw___ptr64:
58150b57cec5SDimitry Andric   case tok::kw___ptr32:
58160b57cec5SDimitry Andric   case tok::kw___forceinline:
58170b57cec5SDimitry Andric   case tok::kw___pascal:
58180b57cec5SDimitry Andric   case tok::kw___unaligned:
58190b57cec5SDimitry Andric 
58200b57cec5SDimitry Andric   case tok::kw__Nonnull:
58210b57cec5SDimitry Andric   case tok::kw__Nullable:
5822e8d8bef9SDimitry Andric   case tok::kw__Nullable_result:
58230b57cec5SDimitry Andric   case tok::kw__Null_unspecified:
58240b57cec5SDimitry Andric 
58250b57cec5SDimitry Andric   case tok::kw___kindof:
58260b57cec5SDimitry Andric 
58270b57cec5SDimitry Andric   case tok::kw___private:
58280b57cec5SDimitry Andric   case tok::kw___local:
58290b57cec5SDimitry Andric   case tok::kw___global:
58300b57cec5SDimitry Andric   case tok::kw___constant:
58310b57cec5SDimitry Andric   case tok::kw___generic:
58320b57cec5SDimitry Andric   case tok::kw___read_only:
58330b57cec5SDimitry Andric   case tok::kw___read_write:
58340b57cec5SDimitry Andric   case tok::kw___write_only:
58350b57cec5SDimitry Andric #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
58360b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def"
58370b57cec5SDimitry Andric 
583806c3fb27SDimitry Andric   case tok::kw___funcref:
5839bdd1243dSDimitry Andric   case tok::kw_groupshared:
58400b57cec5SDimitry Andric     return true;
58410b57cec5SDimitry Andric 
58420b57cec5SDimitry Andric   case tok::kw_private:
58430b57cec5SDimitry Andric     return getLangOpts().OpenCL;
58440b57cec5SDimitry Andric   }
58450b57cec5SDimitry Andric }
58460b57cec5SDimitry Andric 
5847bdd1243dSDimitry Andric bool Parser::isConstructorDeclarator(bool IsUnqualified, bool DeductionGuide,
584806c3fb27SDimitry Andric                                      DeclSpec::FriendSpecified IsFriend,
584906c3fb27SDimitry Andric                                      const ParsedTemplateInfo *TemplateInfo) {
58505f757f3fSDimitry Andric   RevertingTentativeParsingAction TPA(*this);
58510b57cec5SDimitry Andric   // Parse the C++ scope specifier.
58520b57cec5SDimitry Andric   CXXScopeSpec SS;
585306c3fb27SDimitry Andric   if (TemplateInfo && TemplateInfo->TemplateParams)
585406c3fb27SDimitry Andric     SS.setTemplateParamLists(*TemplateInfo->TemplateParams);
585506c3fb27SDimitry Andric 
58565ffd83dbSDimitry Andric   if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
585704eeddc0SDimitry Andric                                      /*ObjectHasErrors=*/false,
58580b57cec5SDimitry Andric                                      /*EnteringContext=*/true)) {
58590b57cec5SDimitry Andric     return false;
58600b57cec5SDimitry Andric   }
58610b57cec5SDimitry Andric 
58620b57cec5SDimitry Andric   // Parse the constructor name.
58630b57cec5SDimitry Andric   if (Tok.is(tok::identifier)) {
58640b57cec5SDimitry Andric     // We already know that we have a constructor name; just consume
58650b57cec5SDimitry Andric     // the token.
58660b57cec5SDimitry Andric     ConsumeToken();
58670b57cec5SDimitry Andric   } else if (Tok.is(tok::annot_template_id)) {
58680b57cec5SDimitry Andric     ConsumeAnnotationToken();
58690b57cec5SDimitry Andric   } else {
58700b57cec5SDimitry Andric     return false;
58710b57cec5SDimitry Andric   }
58720b57cec5SDimitry Andric 
58730b57cec5SDimitry Andric   // There may be attributes here, appertaining to the constructor name or type
58740b57cec5SDimitry Andric   // we just stepped past.
58750b57cec5SDimitry Andric   SkipCXX11Attributes();
58760b57cec5SDimitry Andric 
58770b57cec5SDimitry Andric   // Current class name must be followed by a left parenthesis.
58780b57cec5SDimitry Andric   if (Tok.isNot(tok::l_paren)) {
58790b57cec5SDimitry Andric     return false;
58800b57cec5SDimitry Andric   }
58810b57cec5SDimitry Andric   ConsumeParen();
58820b57cec5SDimitry Andric 
58830b57cec5SDimitry Andric   // A right parenthesis, or ellipsis followed by a right parenthesis signals
58840b57cec5SDimitry Andric   // that we have a constructor.
58850b57cec5SDimitry Andric   if (Tok.is(tok::r_paren) ||
58860b57cec5SDimitry Andric       (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
58870b57cec5SDimitry Andric     return true;
58880b57cec5SDimitry Andric   }
58890b57cec5SDimitry Andric 
58900b57cec5SDimitry Andric   // A C++11 attribute here signals that we have a constructor, and is an
58910b57cec5SDimitry Andric   // attribute on the first constructor parameter.
58920b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus11 &&
58930b57cec5SDimitry Andric       isCXX11AttributeSpecifier(/*Disambiguate*/ false,
58940b57cec5SDimitry Andric                                 /*OuterMightBeMessageSend*/ true)) {
58950b57cec5SDimitry Andric     return true;
58960b57cec5SDimitry Andric   }
58970b57cec5SDimitry Andric 
58980b57cec5SDimitry Andric   // If we need to, enter the specified scope.
58990b57cec5SDimitry Andric   DeclaratorScopeObj DeclScopeObj(*this, SS);
59000b57cec5SDimitry Andric   if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
59010b57cec5SDimitry Andric     DeclScopeObj.EnterDeclaratorScope();
59020b57cec5SDimitry Andric 
59030b57cec5SDimitry Andric   // Optionally skip Microsoft attributes.
59040b57cec5SDimitry Andric   ParsedAttributes Attrs(AttrFactory);
59050b57cec5SDimitry Andric   MaybeParseMicrosoftAttributes(Attrs);
59060b57cec5SDimitry Andric 
59070b57cec5SDimitry Andric   // Check whether the next token(s) are part of a declaration
59080b57cec5SDimitry Andric   // specifier, in which case we have the start of a parameter and,
59090b57cec5SDimitry Andric   // therefore, we know that this is a constructor.
5910bdd1243dSDimitry Andric   // Due to an ambiguity with implicit typename, the above is not enough.
5911bdd1243dSDimitry Andric   // Additionally, check to see if we are a friend.
591206c3fb27SDimitry Andric   // If we parsed a scope specifier as well as friend,
591306c3fb27SDimitry Andric   // we might be parsing a friend constructor.
59140b57cec5SDimitry Andric   bool IsConstructor = false;
59155f757f3fSDimitry Andric   ImplicitTypenameContext ITC = IsFriend && !SS.isSet()
591606c3fb27SDimitry Andric                                     ? ImplicitTypenameContext::No
59175f757f3fSDimitry Andric                                     : ImplicitTypenameContext::Yes;
59185f757f3fSDimitry Andric   // Constructors cannot have this parameters, but we support that scenario here
59195f757f3fSDimitry Andric   // to improve diagnostic.
59205f757f3fSDimitry Andric   if (Tok.is(tok::kw_this)) {
59215f757f3fSDimitry Andric     ConsumeToken();
59225f757f3fSDimitry Andric     return isDeclarationSpecifier(ITC);
59235f757f3fSDimitry Andric   }
59245f757f3fSDimitry Andric 
59255f757f3fSDimitry Andric   if (isDeclarationSpecifier(ITC))
59260b57cec5SDimitry Andric     IsConstructor = true;
59270b57cec5SDimitry Andric   else if (Tok.is(tok::identifier) ||
59280b57cec5SDimitry Andric            (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
59290b57cec5SDimitry Andric     // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
59300b57cec5SDimitry Andric     // This might be a parenthesized member name, but is more likely to
59310b57cec5SDimitry Andric     // be a constructor declaration with an invalid argument type. Keep
59320b57cec5SDimitry Andric     // looking.
59330b57cec5SDimitry Andric     if (Tok.is(tok::annot_cxxscope))
59340b57cec5SDimitry Andric       ConsumeAnnotationToken();
59350b57cec5SDimitry Andric     ConsumeToken();
59360b57cec5SDimitry Andric 
59370b57cec5SDimitry Andric     // If this is not a constructor, we must be parsing a declarator,
59380b57cec5SDimitry Andric     // which must have one of the following syntactic forms (see the
59390b57cec5SDimitry Andric     // grammar extract at the start of ParseDirectDeclarator):
59400b57cec5SDimitry Andric     switch (Tok.getKind()) {
59410b57cec5SDimitry Andric     case tok::l_paren:
59420b57cec5SDimitry Andric       // C(X   (   int));
59430b57cec5SDimitry Andric     case tok::l_square:
59440b57cec5SDimitry Andric       // C(X   [   5]);
59450b57cec5SDimitry Andric       // C(X   [   [attribute]]);
59460b57cec5SDimitry Andric     case tok::coloncolon:
59470b57cec5SDimitry Andric       // C(X   ::   Y);
59480b57cec5SDimitry Andric       // C(X   ::   *p);
59490b57cec5SDimitry Andric       // Assume this isn't a constructor, rather than assuming it's a
59500b57cec5SDimitry Andric       // constructor with an unnamed parameter of an ill-formed type.
59510b57cec5SDimitry Andric       break;
59520b57cec5SDimitry Andric 
59530b57cec5SDimitry Andric     case tok::r_paren:
59540b57cec5SDimitry Andric       // C(X   )
59550b57cec5SDimitry Andric 
59560b57cec5SDimitry Andric       // Skip past the right-paren and any following attributes to get to
59570b57cec5SDimitry Andric       // the function body or trailing-return-type.
59580b57cec5SDimitry Andric       ConsumeParen();
59590b57cec5SDimitry Andric       SkipCXX11Attributes();
59600b57cec5SDimitry Andric 
59610b57cec5SDimitry Andric       if (DeductionGuide) {
59620b57cec5SDimitry Andric         // C(X) -> ... is a deduction guide.
59630b57cec5SDimitry Andric         IsConstructor = Tok.is(tok::arrow);
59640b57cec5SDimitry Andric         break;
59650b57cec5SDimitry Andric       }
59660b57cec5SDimitry Andric       if (Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
59670b57cec5SDimitry Andric         // Assume these were meant to be constructors:
59680b57cec5SDimitry Andric         //   C(X)   :    (the name of a bit-field cannot be parenthesized).
59690b57cec5SDimitry Andric         //   C(X)   try  (this is otherwise ill-formed).
59700b57cec5SDimitry Andric         IsConstructor = true;
59710b57cec5SDimitry Andric       }
59720b57cec5SDimitry Andric       if (Tok.is(tok::semi) || Tok.is(tok::l_brace)) {
59730b57cec5SDimitry Andric         // If we have a constructor name within the class definition,
59740b57cec5SDimitry Andric         // assume these were meant to be constructors:
59750b57cec5SDimitry Andric         //   C(X)   {
59760b57cec5SDimitry Andric         //   C(X)   ;
59770b57cec5SDimitry Andric         // ... because otherwise we would be declaring a non-static data
59780b57cec5SDimitry Andric         // member that is ill-formed because it's of the same type as its
59790b57cec5SDimitry Andric         // surrounding class.
59800b57cec5SDimitry Andric         //
59810b57cec5SDimitry Andric         // FIXME: We can actually do this whether or not the name is qualified,
59820b57cec5SDimitry Andric         // because if it is qualified in this context it must be being used as
59830b57cec5SDimitry Andric         // a constructor name.
59840b57cec5SDimitry Andric         // currently, so we're somewhat conservative here.
59850b57cec5SDimitry Andric         IsConstructor = IsUnqualified;
59860b57cec5SDimitry Andric       }
59870b57cec5SDimitry Andric       break;
59880b57cec5SDimitry Andric 
59890b57cec5SDimitry Andric     default:
59900b57cec5SDimitry Andric       IsConstructor = true;
59910b57cec5SDimitry Andric       break;
59920b57cec5SDimitry Andric     }
59930b57cec5SDimitry Andric   }
59940b57cec5SDimitry Andric   return IsConstructor;
59950b57cec5SDimitry Andric }
59960b57cec5SDimitry Andric 
59970b57cec5SDimitry Andric /// ParseTypeQualifierListOpt
59980b57cec5SDimitry Andric ///          type-qualifier-list: [C99 6.7.5]
59990b57cec5SDimitry Andric ///            type-qualifier
60000b57cec5SDimitry Andric /// [vendor]   attributes
60010b57cec5SDimitry Andric ///              [ only if AttrReqs & AR_VendorAttributesParsed ]
60020b57cec5SDimitry Andric ///            type-qualifier-list type-qualifier
60030b57cec5SDimitry Andric /// [vendor]   type-qualifier-list attributes
60040b57cec5SDimitry Andric ///              [ only if AttrReqs & AR_VendorAttributesParsed ]
60050b57cec5SDimitry Andric /// [C++0x]    attribute-specifier[opt] is allowed before cv-qualifier-seq
60060b57cec5SDimitry Andric ///              [ only if AttReqs & AR_CXX11AttributesParsed ]
60070b57cec5SDimitry Andric /// Note: vendor can be GNU, MS, etc and can be explicitly controlled via
60080b57cec5SDimitry Andric /// AttrRequirements bitmask values.
60090b57cec5SDimitry Andric void Parser::ParseTypeQualifierListOpt(
60100b57cec5SDimitry Andric     DeclSpec &DS, unsigned AttrReqs, bool AtomicAllowed,
60110b57cec5SDimitry Andric     bool IdentifierRequired,
6012bdd1243dSDimitry Andric     std::optional<llvm::function_ref<void()>> CodeCompletionHandler) {
601306c3fb27SDimitry Andric   if ((AttrReqs & AR_CXX11AttributesParsed) &&
601406c3fb27SDimitry Andric       isAllowedCXX11AttributeSpecifier()) {
601581ad6265SDimitry Andric     ParsedAttributes Attrs(AttrFactory);
601681ad6265SDimitry Andric     ParseCXX11Attributes(Attrs);
601781ad6265SDimitry Andric     DS.takeAttributesFrom(Attrs);
60180b57cec5SDimitry Andric   }
60190b57cec5SDimitry Andric 
60200b57cec5SDimitry Andric   SourceLocation EndLoc;
60210b57cec5SDimitry Andric 
602204eeddc0SDimitry Andric   while (true) {
60230b57cec5SDimitry Andric     bool isInvalid = false;
60240b57cec5SDimitry Andric     const char *PrevSpec = nullptr;
60250b57cec5SDimitry Andric     unsigned DiagID = 0;
60260b57cec5SDimitry Andric     SourceLocation Loc = Tok.getLocation();
60270b57cec5SDimitry Andric 
60280b57cec5SDimitry Andric     switch (Tok.getKind()) {
60290b57cec5SDimitry Andric     case tok::code_completion:
6030fe6060f1SDimitry Andric       cutOffParsing();
60310b57cec5SDimitry Andric       if (CodeCompletionHandler)
60320b57cec5SDimitry Andric         (*CodeCompletionHandler)();
60330b57cec5SDimitry Andric       else
60340b57cec5SDimitry Andric         Actions.CodeCompleteTypeQualifiers(DS);
6035fe6060f1SDimitry Andric       return;
60360b57cec5SDimitry Andric 
60370b57cec5SDimitry Andric     case tok::kw_const:
60380b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_const   , Loc, PrevSpec, DiagID,
60390b57cec5SDimitry Andric                                  getLangOpts());
60400b57cec5SDimitry Andric       break;
60410b57cec5SDimitry Andric     case tok::kw_volatile:
60420b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
60430b57cec5SDimitry Andric                                  getLangOpts());
60440b57cec5SDimitry Andric       break;
60450b57cec5SDimitry Andric     case tok::kw_restrict:
60460b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
60470b57cec5SDimitry Andric                                  getLangOpts());
60480b57cec5SDimitry Andric       break;
60490b57cec5SDimitry Andric     case tok::kw__Atomic:
60500b57cec5SDimitry Andric       if (!AtomicAllowed)
60510b57cec5SDimitry Andric         goto DoneWithTypeQuals;
6052a7dea167SDimitry Andric       if (!getLangOpts().C11)
6053a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
60540b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
60550b57cec5SDimitry Andric                                  getLangOpts());
60560b57cec5SDimitry Andric       break;
60570b57cec5SDimitry Andric 
60580b57cec5SDimitry Andric     // OpenCL qualifiers:
60590b57cec5SDimitry Andric     case tok::kw_private:
60600b57cec5SDimitry Andric       if (!getLangOpts().OpenCL)
60610b57cec5SDimitry Andric         goto DoneWithTypeQuals;
6062bdd1243dSDimitry Andric       [[fallthrough]];
60630b57cec5SDimitry Andric     case tok::kw___private:
60640b57cec5SDimitry Andric     case tok::kw___global:
60650b57cec5SDimitry Andric     case tok::kw___local:
60660b57cec5SDimitry Andric     case tok::kw___constant:
60670b57cec5SDimitry Andric     case tok::kw___generic:
60680b57cec5SDimitry Andric     case tok::kw___read_only:
60690b57cec5SDimitry Andric     case tok::kw___write_only:
60700b57cec5SDimitry Andric     case tok::kw___read_write:
60710b57cec5SDimitry Andric       ParseOpenCLQualifiers(DS.getAttributes());
60720b57cec5SDimitry Andric       break;
60730b57cec5SDimitry Andric 
6074bdd1243dSDimitry Andric     case tok::kw_groupshared:
60755f757f3fSDimitry Andric     case tok::kw_in:
60765f757f3fSDimitry Andric     case tok::kw_inout:
60775f757f3fSDimitry Andric     case tok::kw_out:
6078bdd1243dSDimitry Andric       // NOTE: ParseHLSLQualifiers will consume the qualifier token.
6079bdd1243dSDimitry Andric       ParseHLSLQualifiers(DS.getAttributes());
6080bdd1243dSDimitry Andric       continue;
6081bdd1243dSDimitry Andric 
60820b57cec5SDimitry Andric     case tok::kw___unaligned:
60830b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID,
60840b57cec5SDimitry Andric                                  getLangOpts());
60850b57cec5SDimitry Andric       break;
60860b57cec5SDimitry Andric     case tok::kw___uptr:
60870b57cec5SDimitry Andric       // GNU libc headers in C mode use '__uptr' as an identifier which conflicts
60880b57cec5SDimitry Andric       // with the MS modifier keyword.
60890b57cec5SDimitry Andric       if ((AttrReqs & AR_DeclspecAttributesParsed) && !getLangOpts().CPlusPlus &&
60900b57cec5SDimitry Andric           IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
60910b57cec5SDimitry Andric         if (TryKeywordIdentFallback(false))
60920b57cec5SDimitry Andric           continue;
60930b57cec5SDimitry Andric       }
6094bdd1243dSDimitry Andric       [[fallthrough]];
60950b57cec5SDimitry Andric     case tok::kw___sptr:
60960b57cec5SDimitry Andric     case tok::kw___w64:
60970b57cec5SDimitry Andric     case tok::kw___ptr64:
60980b57cec5SDimitry Andric     case tok::kw___ptr32:
60990b57cec5SDimitry Andric     case tok::kw___cdecl:
61000b57cec5SDimitry Andric     case tok::kw___stdcall:
61010b57cec5SDimitry Andric     case tok::kw___fastcall:
61020b57cec5SDimitry Andric     case tok::kw___thiscall:
61030b57cec5SDimitry Andric     case tok::kw___regcall:
61040b57cec5SDimitry Andric     case tok::kw___vectorcall:
61050b57cec5SDimitry Andric       if (AttrReqs & AR_DeclspecAttributesParsed) {
61060b57cec5SDimitry Andric         ParseMicrosoftTypeAttributes(DS.getAttributes());
61070b57cec5SDimitry Andric         continue;
61080b57cec5SDimitry Andric       }
61090b57cec5SDimitry Andric       goto DoneWithTypeQuals;
611006c3fb27SDimitry Andric 
611106c3fb27SDimitry Andric     case tok::kw___funcref:
611206c3fb27SDimitry Andric       ParseWebAssemblyFuncrefTypeAttribute(DS.getAttributes());
611306c3fb27SDimitry Andric       continue;
611406c3fb27SDimitry Andric       goto DoneWithTypeQuals;
611506c3fb27SDimitry Andric 
61160b57cec5SDimitry Andric     case tok::kw___pascal:
61170b57cec5SDimitry Andric       if (AttrReqs & AR_VendorAttributesParsed) {
61180b57cec5SDimitry Andric         ParseBorlandTypeAttributes(DS.getAttributes());
61190b57cec5SDimitry Andric         continue;
61200b57cec5SDimitry Andric       }
61210b57cec5SDimitry Andric       goto DoneWithTypeQuals;
61220b57cec5SDimitry Andric 
61230b57cec5SDimitry Andric     // Nullability type specifiers.
61240b57cec5SDimitry Andric     case tok::kw__Nonnull:
61250b57cec5SDimitry Andric     case tok::kw__Nullable:
6126e8d8bef9SDimitry Andric     case tok::kw__Nullable_result:
61270b57cec5SDimitry Andric     case tok::kw__Null_unspecified:
61280b57cec5SDimitry Andric       ParseNullabilityTypeSpecifiers(DS.getAttributes());
61290b57cec5SDimitry Andric       continue;
61300b57cec5SDimitry Andric 
61310b57cec5SDimitry Andric     // Objective-C 'kindof' types.
61320b57cec5SDimitry Andric     case tok::kw___kindof:
61330b57cec5SDimitry Andric       DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc,
613406c3fb27SDimitry Andric                                 nullptr, 0, tok::kw___kindof);
61350b57cec5SDimitry Andric       (void)ConsumeToken();
61360b57cec5SDimitry Andric       continue;
61370b57cec5SDimitry Andric 
61380b57cec5SDimitry Andric     case tok::kw___attribute:
61390b57cec5SDimitry Andric       if (AttrReqs & AR_GNUAttributesParsedAndRejected)
61400b57cec5SDimitry Andric         // When GNU attributes are expressly forbidden, diagnose their usage.
61410b57cec5SDimitry Andric         Diag(Tok, diag::err_attributes_not_allowed);
61420b57cec5SDimitry Andric 
61430b57cec5SDimitry Andric       // Parse the attributes even if they are rejected to ensure that error
61440b57cec5SDimitry Andric       // recovery is graceful.
61450b57cec5SDimitry Andric       if (AttrReqs & AR_GNUAttributesParsed ||
61460b57cec5SDimitry Andric           AttrReqs & AR_GNUAttributesParsedAndRejected) {
61470b57cec5SDimitry Andric         ParseGNUAttributes(DS.getAttributes());
61480b57cec5SDimitry Andric         continue; // do *not* consume the next token!
61490b57cec5SDimitry Andric       }
61500b57cec5SDimitry Andric       // otherwise, FALL THROUGH!
6151bdd1243dSDimitry Andric       [[fallthrough]];
61520b57cec5SDimitry Andric     default:
61530b57cec5SDimitry Andric       DoneWithTypeQuals:
61540b57cec5SDimitry Andric       // If this is not a type-qualifier token, we're done reading type
61550b57cec5SDimitry Andric       // qualifiers.  First verify that DeclSpec's are consistent.
61560b57cec5SDimitry Andric       DS.Finish(Actions, Actions.getASTContext().getPrintingPolicy());
61570b57cec5SDimitry Andric       if (EndLoc.isValid())
61580b57cec5SDimitry Andric         DS.SetRangeEnd(EndLoc);
61590b57cec5SDimitry Andric       return;
61600b57cec5SDimitry Andric     }
61610b57cec5SDimitry Andric 
61620b57cec5SDimitry Andric     // If the specifier combination wasn't legal, issue a diagnostic.
61630b57cec5SDimitry Andric     if (isInvalid) {
61640b57cec5SDimitry Andric       assert(PrevSpec && "Method did not return previous specifier!");
61650b57cec5SDimitry Andric       Diag(Tok, DiagID) << PrevSpec;
61660b57cec5SDimitry Andric     }
61670b57cec5SDimitry Andric     EndLoc = ConsumeToken();
61680b57cec5SDimitry Andric   }
61690b57cec5SDimitry Andric }
61700b57cec5SDimitry Andric 
61710b57cec5SDimitry Andric /// ParseDeclarator - Parse and verify a newly-initialized declarator.
61720b57cec5SDimitry Andric void Parser::ParseDeclarator(Declarator &D) {
61730b57cec5SDimitry Andric   /// This implements the 'declarator' production in the C grammar, then checks
61740b57cec5SDimitry Andric   /// for well-formedness and issues diagnostics.
617581ad6265SDimitry Andric   Actions.runWithSufficientStackSpace(D.getBeginLoc(), [&] {
61760b57cec5SDimitry Andric     ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
617781ad6265SDimitry Andric   });
61780b57cec5SDimitry Andric }
61790b57cec5SDimitry Andric 
61800b57cec5SDimitry Andric static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang,
61810b57cec5SDimitry Andric                                DeclaratorContext TheContext) {
61820b57cec5SDimitry Andric   if (Kind == tok::star || Kind == tok::caret)
61830b57cec5SDimitry Andric     return true;
61840b57cec5SDimitry Andric 
61856e75b2fbSDimitry Andric   // OpenCL 2.0 and later define this keyword.
6186349cc55cSDimitry Andric   if (Kind == tok::kw_pipe && Lang.OpenCL &&
6187349cc55cSDimitry Andric       Lang.getOpenCLCompatibleVersion() >= 200)
61880b57cec5SDimitry Andric     return true;
61890b57cec5SDimitry Andric 
61900b57cec5SDimitry Andric   if (!Lang.CPlusPlus)
61910b57cec5SDimitry Andric     return false;
61920b57cec5SDimitry Andric 
61930b57cec5SDimitry Andric   if (Kind == tok::amp)
61940b57cec5SDimitry Andric     return true;
61950b57cec5SDimitry Andric 
61960b57cec5SDimitry Andric   // We parse rvalue refs in C++03, because otherwise the errors are scary.
61970b57cec5SDimitry Andric   // But we must not parse them in conversion-type-ids and new-type-ids, since
61980b57cec5SDimitry Andric   // those can be legitimately followed by a && operator.
61990b57cec5SDimitry Andric   // (The same thing can in theory happen after a trailing-return-type, but
62000b57cec5SDimitry Andric   // since those are a C++11 feature, there is no rejects-valid issue there.)
62010b57cec5SDimitry Andric   if (Kind == tok::ampamp)
6202e8d8bef9SDimitry Andric     return Lang.CPlusPlus11 || (TheContext != DeclaratorContext::ConversionId &&
6203e8d8bef9SDimitry Andric                                 TheContext != DeclaratorContext::CXXNew);
62040b57cec5SDimitry Andric 
62050b57cec5SDimitry Andric   return false;
62060b57cec5SDimitry Andric }
62070b57cec5SDimitry Andric 
62080b57cec5SDimitry Andric // Indicates whether the given declarator is a pipe declarator.
620981ad6265SDimitry Andric static bool isPipeDeclarator(const Declarator &D) {
62100b57cec5SDimitry Andric   const unsigned NumTypes = D.getNumTypeObjects();
62110b57cec5SDimitry Andric 
62120b57cec5SDimitry Andric   for (unsigned Idx = 0; Idx != NumTypes; ++Idx)
62130b57cec5SDimitry Andric     if (DeclaratorChunk::Pipe == D.getTypeObject(Idx).Kind)
62140b57cec5SDimitry Andric       return true;
62150b57cec5SDimitry Andric 
62160b57cec5SDimitry Andric   return false;
62170b57cec5SDimitry Andric }
62180b57cec5SDimitry Andric 
62190b57cec5SDimitry Andric /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
62200b57cec5SDimitry Andric /// is parsed by the function passed to it. Pass null, and the direct-declarator
62210b57cec5SDimitry Andric /// isn't parsed at all, making this function effectively parse the C++
62220b57cec5SDimitry Andric /// ptr-operator production.
62230b57cec5SDimitry Andric ///
62240b57cec5SDimitry Andric /// If the grammar of this construct is extended, matching changes must also be
62250b57cec5SDimitry Andric /// made to TryParseDeclarator and MightBeDeclarator, and possibly to
62260b57cec5SDimitry Andric /// isConstructorDeclarator.
62270b57cec5SDimitry Andric ///
62280b57cec5SDimitry Andric ///       declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
62290b57cec5SDimitry Andric /// [C]     pointer[opt] direct-declarator
62300b57cec5SDimitry Andric /// [C++]   direct-declarator
62310b57cec5SDimitry Andric /// [C++]   ptr-operator declarator
62320b57cec5SDimitry Andric ///
62330b57cec5SDimitry Andric ///       pointer: [C99 6.7.5]
62340b57cec5SDimitry Andric ///         '*' type-qualifier-list[opt]
62350b57cec5SDimitry Andric ///         '*' type-qualifier-list[opt] pointer
62360b57cec5SDimitry Andric ///
62370b57cec5SDimitry Andric ///       ptr-operator:
62380b57cec5SDimitry Andric ///         '*' cv-qualifier-seq[opt]
62390b57cec5SDimitry Andric ///         '&'
62400b57cec5SDimitry Andric /// [C++0x] '&&'
62410b57cec5SDimitry Andric /// [GNU]   '&' restrict[opt] attributes[opt]
62420b57cec5SDimitry Andric /// [GNU?]  '&&' restrict[opt] attributes[opt]
62430b57cec5SDimitry Andric ///         '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
62440b57cec5SDimitry Andric void Parser::ParseDeclaratorInternal(Declarator &D,
62450b57cec5SDimitry Andric                                      DirectDeclParseFunction DirectDeclParser) {
62460b57cec5SDimitry Andric   if (Diags.hasAllExtensionsSilenced())
62470b57cec5SDimitry Andric     D.setExtension();
62480b57cec5SDimitry Andric 
62490b57cec5SDimitry Andric   // C++ member pointers start with a '::' or a nested-name.
62500b57cec5SDimitry Andric   // Member pointers get special handling, since there's no place for the
62510b57cec5SDimitry Andric   // scope spec in the generic path below.
62520b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus &&
62530b57cec5SDimitry Andric       (Tok.is(tok::coloncolon) || Tok.is(tok::kw_decltype) ||
62540b57cec5SDimitry Andric        (Tok.is(tok::identifier) &&
62550b57cec5SDimitry Andric         (NextToken().is(tok::coloncolon) || NextToken().is(tok::less))) ||
62560b57cec5SDimitry Andric        Tok.is(tok::annot_cxxscope))) {
6257e8d8bef9SDimitry Andric     bool EnteringContext = D.getContext() == DeclaratorContext::File ||
6258e8d8bef9SDimitry Andric                            D.getContext() == DeclaratorContext::Member;
62590b57cec5SDimitry Andric     CXXScopeSpec SS;
626006c3fb27SDimitry Andric     SS.setTemplateParamLists(D.getTemplateParameterLists());
62615ffd83dbSDimitry Andric     ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
626204eeddc0SDimitry Andric                                    /*ObjectHasErrors=*/false, EnteringContext);
62630b57cec5SDimitry Andric 
62640b57cec5SDimitry Andric     if (SS.isNotEmpty()) {
62650b57cec5SDimitry Andric       if (Tok.isNot(tok::star)) {
62660b57cec5SDimitry Andric         // The scope spec really belongs to the direct-declarator.
62670b57cec5SDimitry Andric         if (D.mayHaveIdentifier())
62680b57cec5SDimitry Andric           D.getCXXScopeSpec() = SS;
62690b57cec5SDimitry Andric         else
62700b57cec5SDimitry Andric           AnnotateScopeToken(SS, true);
62710b57cec5SDimitry Andric 
62720b57cec5SDimitry Andric         if (DirectDeclParser)
62730b57cec5SDimitry Andric           (this->*DirectDeclParser)(D);
62740b57cec5SDimitry Andric         return;
62750b57cec5SDimitry Andric       }
62760b57cec5SDimitry Andric 
6277e8d8bef9SDimitry Andric       if (SS.isValid()) {
6278e8d8bef9SDimitry Andric         checkCompoundToken(SS.getEndLoc(), tok::coloncolon,
6279e8d8bef9SDimitry Andric                            CompoundToken::MemberPtr);
6280e8d8bef9SDimitry Andric       }
6281e8d8bef9SDimitry Andric 
62825ffd83dbSDimitry Andric       SourceLocation StarLoc = ConsumeToken();
62835ffd83dbSDimitry Andric       D.SetRangeEnd(StarLoc);
62840b57cec5SDimitry Andric       DeclSpec DS(AttrFactory);
62850b57cec5SDimitry Andric       ParseTypeQualifierListOpt(DS);
62860b57cec5SDimitry Andric       D.ExtendWithDeclSpec(DS);
62870b57cec5SDimitry Andric 
62880b57cec5SDimitry Andric       // Recurse to parse whatever is left.
628981ad6265SDimitry Andric       Actions.runWithSufficientStackSpace(D.getBeginLoc(), [&] {
62900b57cec5SDimitry Andric         ParseDeclaratorInternal(D, DirectDeclParser);
629181ad6265SDimitry Andric       });
62920b57cec5SDimitry Andric 
62930b57cec5SDimitry Andric       // Sema will have to catch (syntactically invalid) pointers into global
62940b57cec5SDimitry Andric       // scope. It has to catch pointers into namespace scope anyway.
62950b57cec5SDimitry Andric       D.AddTypeInfo(DeclaratorChunk::getMemberPointer(
62965ffd83dbSDimitry Andric                         SS, DS.getTypeQualifiers(), StarLoc, DS.getEndLoc()),
62970b57cec5SDimitry Andric                     std::move(DS.getAttributes()),
62980b57cec5SDimitry Andric                     /* Don't replace range end. */ SourceLocation());
62990b57cec5SDimitry Andric       return;
63000b57cec5SDimitry Andric     }
63010b57cec5SDimitry Andric   }
63020b57cec5SDimitry Andric 
63030b57cec5SDimitry Andric   tok::TokenKind Kind = Tok.getKind();
63040b57cec5SDimitry Andric 
630581ad6265SDimitry Andric   if (D.getDeclSpec().isTypeSpecPipe() && !isPipeDeclarator(D)) {
63060b57cec5SDimitry Andric     DeclSpec DS(AttrFactory);
63070b57cec5SDimitry Andric     ParseTypeQualifierListOpt(DS);
63080b57cec5SDimitry Andric 
63090b57cec5SDimitry Andric     D.AddTypeInfo(
63100b57cec5SDimitry Andric         DeclaratorChunk::getPipe(DS.getTypeQualifiers(), DS.getPipeLoc()),
63110b57cec5SDimitry Andric         std::move(DS.getAttributes()), SourceLocation());
63120b57cec5SDimitry Andric   }
63130b57cec5SDimitry Andric 
63140b57cec5SDimitry Andric   // Not a pointer, C++ reference, or block.
63150b57cec5SDimitry Andric   if (!isPtrOperatorToken(Kind, getLangOpts(), D.getContext())) {
63160b57cec5SDimitry Andric     if (DirectDeclParser)
63170b57cec5SDimitry Andric       (this->*DirectDeclParser)(D);
63180b57cec5SDimitry Andric     return;
63190b57cec5SDimitry Andric   }
63200b57cec5SDimitry Andric 
63210b57cec5SDimitry Andric   // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
63220b57cec5SDimitry Andric   // '&&' -> rvalue reference
63230b57cec5SDimitry Andric   SourceLocation Loc = ConsumeToken();  // Eat the *, ^, & or &&.
63240b57cec5SDimitry Andric   D.SetRangeEnd(Loc);
63250b57cec5SDimitry Andric 
63260b57cec5SDimitry Andric   if (Kind == tok::star || Kind == tok::caret) {
63270b57cec5SDimitry Andric     // Is a pointer.
63280b57cec5SDimitry Andric     DeclSpec DS(AttrFactory);
63290b57cec5SDimitry Andric 
63300b57cec5SDimitry Andric     // GNU attributes are not allowed here in a new-type-id, but Declspec and
63310b57cec5SDimitry Andric     // C++11 attributes are allowed.
63320b57cec5SDimitry Andric     unsigned Reqs = AR_CXX11AttributesParsed | AR_DeclspecAttributesParsed |
6333e8d8bef9SDimitry Andric                     ((D.getContext() != DeclaratorContext::CXXNew)
63340b57cec5SDimitry Andric                          ? AR_GNUAttributesParsed
63350b57cec5SDimitry Andric                          : AR_GNUAttributesParsedAndRejected);
63360b57cec5SDimitry Andric     ParseTypeQualifierListOpt(DS, Reqs, true, !D.mayOmitIdentifier());
63370b57cec5SDimitry Andric     D.ExtendWithDeclSpec(DS);
63380b57cec5SDimitry Andric 
63390b57cec5SDimitry Andric     // Recursively parse the declarator.
634081ad6265SDimitry Andric     Actions.runWithSufficientStackSpace(
634181ad6265SDimitry Andric         D.getBeginLoc(), [&] { ParseDeclaratorInternal(D, DirectDeclParser); });
63420b57cec5SDimitry Andric     if (Kind == tok::star)
63430b57cec5SDimitry Andric       // Remember that we parsed a pointer type, and remember the type-quals.
63440b57cec5SDimitry Andric       D.AddTypeInfo(DeclaratorChunk::getPointer(
63450b57cec5SDimitry Andric                         DS.getTypeQualifiers(), Loc, DS.getConstSpecLoc(),
63460b57cec5SDimitry Andric                         DS.getVolatileSpecLoc(), DS.getRestrictSpecLoc(),
63470b57cec5SDimitry Andric                         DS.getAtomicSpecLoc(), DS.getUnalignedSpecLoc()),
63480b57cec5SDimitry Andric                     std::move(DS.getAttributes()), SourceLocation());
63490b57cec5SDimitry Andric     else
63500b57cec5SDimitry Andric       // Remember that we parsed a Block type, and remember the type-quals.
63510b57cec5SDimitry Andric       D.AddTypeInfo(
63520b57cec5SDimitry Andric           DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), Loc),
63530b57cec5SDimitry Andric           std::move(DS.getAttributes()), SourceLocation());
63540b57cec5SDimitry Andric   } else {
63550b57cec5SDimitry Andric     // Is a reference
63560b57cec5SDimitry Andric     DeclSpec DS(AttrFactory);
63570b57cec5SDimitry Andric 
63580b57cec5SDimitry Andric     // Complain about rvalue references in C++03, but then go on and build
63590b57cec5SDimitry Andric     // the declarator.
63600b57cec5SDimitry Andric     if (Kind == tok::ampamp)
63610b57cec5SDimitry Andric       Diag(Loc, getLangOpts().CPlusPlus11 ?
63620b57cec5SDimitry Andric            diag::warn_cxx98_compat_rvalue_reference :
63630b57cec5SDimitry Andric            diag::ext_rvalue_reference);
63640b57cec5SDimitry Andric 
63650b57cec5SDimitry Andric     // GNU-style and C++11 attributes are allowed here, as is restrict.
63660b57cec5SDimitry Andric     ParseTypeQualifierListOpt(DS);
63670b57cec5SDimitry Andric     D.ExtendWithDeclSpec(DS);
63680b57cec5SDimitry Andric 
63690b57cec5SDimitry Andric     // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
63700b57cec5SDimitry Andric     // cv-qualifiers are introduced through the use of a typedef or of a
63710b57cec5SDimitry Andric     // template type argument, in which case the cv-qualifiers are ignored.
63720b57cec5SDimitry Andric     if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
63730b57cec5SDimitry Andric       if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
63740b57cec5SDimitry Andric         Diag(DS.getConstSpecLoc(),
63750b57cec5SDimitry Andric              diag::err_invalid_reference_qualifier_application) << "const";
63760b57cec5SDimitry Andric       if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
63770b57cec5SDimitry Andric         Diag(DS.getVolatileSpecLoc(),
63780b57cec5SDimitry Andric              diag::err_invalid_reference_qualifier_application) << "volatile";
63790b57cec5SDimitry Andric       // 'restrict' is permitted as an extension.
63800b57cec5SDimitry Andric       if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
63810b57cec5SDimitry Andric         Diag(DS.getAtomicSpecLoc(),
63820b57cec5SDimitry Andric              diag::err_invalid_reference_qualifier_application) << "_Atomic";
63830b57cec5SDimitry Andric     }
63840b57cec5SDimitry Andric 
63850b57cec5SDimitry Andric     // Recursively parse the declarator.
638681ad6265SDimitry Andric     Actions.runWithSufficientStackSpace(
638781ad6265SDimitry Andric         D.getBeginLoc(), [&] { ParseDeclaratorInternal(D, DirectDeclParser); });
63880b57cec5SDimitry Andric 
63890b57cec5SDimitry Andric     if (D.getNumTypeObjects() > 0) {
63900b57cec5SDimitry Andric       // C++ [dcl.ref]p4: There shall be no references to references.
63910b57cec5SDimitry Andric       DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
63920b57cec5SDimitry Andric       if (InnerChunk.Kind == DeclaratorChunk::Reference) {
63930b57cec5SDimitry Andric         if (const IdentifierInfo *II = D.getIdentifier())
63940b57cec5SDimitry Andric           Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
63950b57cec5SDimitry Andric            << II;
63960b57cec5SDimitry Andric         else
63970b57cec5SDimitry Andric           Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
63980b57cec5SDimitry Andric             << "type name";
63990b57cec5SDimitry Andric 
64000b57cec5SDimitry Andric         // Once we've complained about the reference-to-reference, we
64010b57cec5SDimitry Andric         // can go ahead and build the (technically ill-formed)
64020b57cec5SDimitry Andric         // declarator: reference collapsing will take care of it.
64030b57cec5SDimitry Andric       }
64040b57cec5SDimitry Andric     }
64050b57cec5SDimitry Andric 
64060b57cec5SDimitry Andric     // Remember that we parsed a reference type.
64070b57cec5SDimitry Andric     D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
64080b57cec5SDimitry Andric                                                 Kind == tok::amp),
64090b57cec5SDimitry Andric                   std::move(DS.getAttributes()), SourceLocation());
64100b57cec5SDimitry Andric   }
64110b57cec5SDimitry Andric }
64120b57cec5SDimitry Andric 
64130b57cec5SDimitry Andric // When correcting from misplaced brackets before the identifier, the location
64140b57cec5SDimitry Andric // is saved inside the declarator so that other diagnostic messages can use
64150b57cec5SDimitry Andric // them.  This extracts and returns that location, or returns the provided
64160b57cec5SDimitry Andric // location if a stored location does not exist.
64170b57cec5SDimitry Andric static SourceLocation getMissingDeclaratorIdLoc(Declarator &D,
64180b57cec5SDimitry Andric                                                 SourceLocation Loc) {
64190b57cec5SDimitry Andric   if (D.getName().StartLocation.isInvalid() &&
64200b57cec5SDimitry Andric       D.getName().EndLocation.isValid())
64210b57cec5SDimitry Andric     return D.getName().EndLocation;
64220b57cec5SDimitry Andric 
64230b57cec5SDimitry Andric   return Loc;
64240b57cec5SDimitry Andric }
64250b57cec5SDimitry Andric 
64260b57cec5SDimitry Andric /// ParseDirectDeclarator
64270b57cec5SDimitry Andric ///       direct-declarator: [C99 6.7.5]
64280b57cec5SDimitry Andric /// [C99]   identifier
64290b57cec5SDimitry Andric ///         '(' declarator ')'
64300b57cec5SDimitry Andric /// [GNU]   '(' attributes declarator ')'
64310b57cec5SDimitry Andric /// [C90]   direct-declarator '[' constant-expression[opt] ']'
64320b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
64330b57cec5SDimitry Andric /// [C99]   direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
64340b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list 'static' assignment-expr ']'
64350b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list[opt] '*' ']'
64360b57cec5SDimitry Andric /// [C++11] direct-declarator '[' constant-expression[opt] ']'
64370b57cec5SDimitry Andric ///                    attribute-specifier-seq[opt]
64380b57cec5SDimitry Andric ///         direct-declarator '(' parameter-type-list ')'
64390b57cec5SDimitry Andric ///         direct-declarator '(' identifier-list[opt] ')'
64400b57cec5SDimitry Andric /// [GNU]   direct-declarator '(' parameter-forward-declarations
64410b57cec5SDimitry Andric ///                    parameter-type-list[opt] ')'
64420b57cec5SDimitry Andric /// [C++]   direct-declarator '(' parameter-declaration-clause ')'
64430b57cec5SDimitry Andric ///                    cv-qualifier-seq[opt] exception-specification[opt]
64440b57cec5SDimitry Andric /// [C++11] direct-declarator '(' parameter-declaration-clause ')'
64450b57cec5SDimitry Andric ///                    attribute-specifier-seq[opt] cv-qualifier-seq[opt]
64460b57cec5SDimitry Andric ///                    ref-qualifier[opt] exception-specification[opt]
64470b57cec5SDimitry Andric /// [C++]   declarator-id
64480b57cec5SDimitry Andric /// [C++11] declarator-id attribute-specifier-seq[opt]
64490b57cec5SDimitry Andric ///
64500b57cec5SDimitry Andric ///       declarator-id: [C++ 8]
64510b57cec5SDimitry Andric ///         '...'[opt] id-expression
64520b57cec5SDimitry Andric ///         '::'[opt] nested-name-specifier[opt] type-name
64530b57cec5SDimitry Andric ///
64540b57cec5SDimitry Andric ///       id-expression: [C++ 5.1]
64550b57cec5SDimitry Andric ///         unqualified-id
64560b57cec5SDimitry Andric ///         qualified-id
64570b57cec5SDimitry Andric ///
64580b57cec5SDimitry Andric ///       unqualified-id: [C++ 5.1]
64590b57cec5SDimitry Andric ///         identifier
64600b57cec5SDimitry Andric ///         operator-function-id
64610b57cec5SDimitry Andric ///         conversion-function-id
64620b57cec5SDimitry Andric ///          '~' class-name
64630b57cec5SDimitry Andric ///         template-id
64640b57cec5SDimitry Andric ///
64650b57cec5SDimitry Andric /// C++17 adds the following, which we also handle here:
64660b57cec5SDimitry Andric ///
64670b57cec5SDimitry Andric ///       simple-declaration:
64680b57cec5SDimitry Andric ///         <decl-spec> '[' identifier-list ']' brace-or-equal-initializer ';'
64690b57cec5SDimitry Andric ///
64700b57cec5SDimitry Andric /// Note, any additional constructs added here may need corresponding changes
64710b57cec5SDimitry Andric /// in isConstructorDeclarator.
64720b57cec5SDimitry Andric void Parser::ParseDirectDeclarator(Declarator &D) {
64730b57cec5SDimitry Andric   DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
64740b57cec5SDimitry Andric 
64750b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
64760b57cec5SDimitry Andric     // This might be a C++17 structured binding.
64770b57cec5SDimitry Andric     if (Tok.is(tok::l_square) && !D.mayOmitIdentifier() &&
64780b57cec5SDimitry Andric         D.getCXXScopeSpec().isEmpty())
64790b57cec5SDimitry Andric       return ParseDecompositionDeclarator(D);
64800b57cec5SDimitry Andric 
64810b57cec5SDimitry Andric     // Don't parse FOO:BAR as if it were a typo for FOO::BAR inside a class, in
64820b57cec5SDimitry Andric     // this context it is a bitfield. Also in range-based for statement colon
64830b57cec5SDimitry Andric     // may delimit for-range-declaration.
64840b57cec5SDimitry Andric     ColonProtectionRAIIObject X(
6485e8d8bef9SDimitry Andric         *this, D.getContext() == DeclaratorContext::Member ||
6486e8d8bef9SDimitry Andric                    (D.getContext() == DeclaratorContext::ForInit &&
64870b57cec5SDimitry Andric                     getLangOpts().CPlusPlus11));
64880b57cec5SDimitry Andric 
64890b57cec5SDimitry Andric     // ParseDeclaratorInternal might already have parsed the scope.
64900b57cec5SDimitry Andric     if (D.getCXXScopeSpec().isEmpty()) {
6491e8d8bef9SDimitry Andric       bool EnteringContext = D.getContext() == DeclaratorContext::File ||
6492e8d8bef9SDimitry Andric                              D.getContext() == DeclaratorContext::Member;
64935ffd83dbSDimitry Andric       ParseOptionalCXXScopeSpecifier(
64945ffd83dbSDimitry Andric           D.getCXXScopeSpec(), /*ObjectType=*/nullptr,
649504eeddc0SDimitry Andric           /*ObjectHasErrors=*/false, EnteringContext);
64960b57cec5SDimitry Andric     }
64970b57cec5SDimitry Andric 
64980b57cec5SDimitry Andric     if (D.getCXXScopeSpec().isValid()) {
64990b57cec5SDimitry Andric       if (Actions.ShouldEnterDeclaratorScope(getCurScope(),
65000b57cec5SDimitry Andric                                              D.getCXXScopeSpec()))
65010b57cec5SDimitry Andric         // Change the declaration context for name lookup, until this function
65020b57cec5SDimitry Andric         // is exited (and the declarator has been parsed).
65030b57cec5SDimitry Andric         DeclScopeObj.EnterDeclaratorScope();
65040b57cec5SDimitry Andric       else if (getObjCDeclContext()) {
65050b57cec5SDimitry Andric         // Ensure that we don't interpret the next token as an identifier when
65060b57cec5SDimitry Andric         // dealing with declarations in an Objective-C container.
65070b57cec5SDimitry Andric         D.SetIdentifier(nullptr, Tok.getLocation());
65080b57cec5SDimitry Andric         D.setInvalidType(true);
65090b57cec5SDimitry Andric         ConsumeToken();
65100b57cec5SDimitry Andric         goto PastIdentifier;
65110b57cec5SDimitry Andric       }
65120b57cec5SDimitry Andric     }
65130b57cec5SDimitry Andric 
65140b57cec5SDimitry Andric     // C++0x [dcl.fct]p14:
65150b57cec5SDimitry Andric     //   There is a syntactic ambiguity when an ellipsis occurs at the end of a
65160b57cec5SDimitry Andric     //   parameter-declaration-clause without a preceding comma. In this case,
65170b57cec5SDimitry Andric     //   the ellipsis is parsed as part of the abstract-declarator if the type
65180b57cec5SDimitry Andric     //   of the parameter either names a template parameter pack that has not
65190b57cec5SDimitry Andric     //   been expanded or contains auto; otherwise, it is parsed as part of the
65200b57cec5SDimitry Andric     //   parameter-declaration-clause.
65210b57cec5SDimitry Andric     if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
6522e8d8bef9SDimitry Andric         !((D.getContext() == DeclaratorContext::Prototype ||
6523e8d8bef9SDimitry Andric            D.getContext() == DeclaratorContext::LambdaExprParameter ||
6524e8d8bef9SDimitry Andric            D.getContext() == DeclaratorContext::BlockLiteral) &&
6525e8d8bef9SDimitry Andric           NextToken().is(tok::r_paren) && !D.hasGroupingParens() &&
65260b57cec5SDimitry Andric           !Actions.containsUnexpandedParameterPacks(D) &&
65270b57cec5SDimitry Andric           D.getDeclSpec().getTypeSpecType() != TST_auto)) {
65280b57cec5SDimitry Andric       SourceLocation EllipsisLoc = ConsumeToken();
65290b57cec5SDimitry Andric       if (isPtrOperatorToken(Tok.getKind(), getLangOpts(), D.getContext())) {
65300b57cec5SDimitry Andric         // The ellipsis was put in the wrong place. Recover, and explain to
65310b57cec5SDimitry Andric         // the user what they should have done.
65320b57cec5SDimitry Andric         ParseDeclarator(D);
65330b57cec5SDimitry Andric         if (EllipsisLoc.isValid())
65340b57cec5SDimitry Andric           DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
65350b57cec5SDimitry Andric         return;
65360b57cec5SDimitry Andric       } else
65370b57cec5SDimitry Andric         D.setEllipsisLoc(EllipsisLoc);
65380b57cec5SDimitry Andric 
65390b57cec5SDimitry Andric       // The ellipsis can't be followed by a parenthesized declarator. We
65400b57cec5SDimitry Andric       // check for that in ParseParenDeclarator, after we have disambiguated
65410b57cec5SDimitry Andric       // the l_paren token.
65420b57cec5SDimitry Andric     }
65430b57cec5SDimitry Andric 
65440b57cec5SDimitry Andric     if (Tok.isOneOf(tok::identifier, tok::kw_operator, tok::annot_template_id,
65450b57cec5SDimitry Andric                     tok::tilde)) {
65460b57cec5SDimitry Andric       // We found something that indicates the start of an unqualified-id.
65470b57cec5SDimitry Andric       // Parse that unqualified-id.
65480b57cec5SDimitry Andric       bool AllowConstructorName;
65490b57cec5SDimitry Andric       bool AllowDeductionGuide;
65500b57cec5SDimitry Andric       if (D.getDeclSpec().hasTypeSpecifier()) {
65510b57cec5SDimitry Andric         AllowConstructorName = false;
65520b57cec5SDimitry Andric         AllowDeductionGuide = false;
65530b57cec5SDimitry Andric       } else if (D.getCXXScopeSpec().isSet()) {
6554e8d8bef9SDimitry Andric         AllowConstructorName = (D.getContext() == DeclaratorContext::File ||
6555e8d8bef9SDimitry Andric                                 D.getContext() == DeclaratorContext::Member);
65560b57cec5SDimitry Andric         AllowDeductionGuide = false;
65570b57cec5SDimitry Andric       } else {
6558e8d8bef9SDimitry Andric         AllowConstructorName = (D.getContext() == DeclaratorContext::Member);
6559e8d8bef9SDimitry Andric         AllowDeductionGuide = (D.getContext() == DeclaratorContext::File ||
6560e8d8bef9SDimitry Andric                                D.getContext() == DeclaratorContext::Member);
65610b57cec5SDimitry Andric       }
65620b57cec5SDimitry Andric 
65630b57cec5SDimitry Andric       bool HadScope = D.getCXXScopeSpec().isValid();
65640b57cec5SDimitry Andric       if (ParseUnqualifiedId(D.getCXXScopeSpec(),
65655ffd83dbSDimitry Andric                              /*ObjectType=*/nullptr,
65665ffd83dbSDimitry Andric                              /*ObjectHadErrors=*/false,
65670b57cec5SDimitry Andric                              /*EnteringContext=*/true,
65680b57cec5SDimitry Andric                              /*AllowDestructorName=*/true, AllowConstructorName,
65695ffd83dbSDimitry Andric                              AllowDeductionGuide, nullptr, D.getName()) ||
65700b57cec5SDimitry Andric           // Once we're past the identifier, if the scope was bad, mark the
65710b57cec5SDimitry Andric           // whole declarator bad.
65720b57cec5SDimitry Andric           D.getCXXScopeSpec().isInvalid()) {
65730b57cec5SDimitry Andric         D.SetIdentifier(nullptr, Tok.getLocation());
65740b57cec5SDimitry Andric         D.setInvalidType(true);
65750b57cec5SDimitry Andric       } else {
65760b57cec5SDimitry Andric         // ParseUnqualifiedId might have parsed a scope specifier during error
65770b57cec5SDimitry Andric         // recovery. If it did so, enter that scope.
65780b57cec5SDimitry Andric         if (!HadScope && D.getCXXScopeSpec().isValid() &&
65790b57cec5SDimitry Andric             Actions.ShouldEnterDeclaratorScope(getCurScope(),
65800b57cec5SDimitry Andric                                                D.getCXXScopeSpec()))
65810b57cec5SDimitry Andric           DeclScopeObj.EnterDeclaratorScope();
65820b57cec5SDimitry Andric 
65830b57cec5SDimitry Andric         // Parsed the unqualified-id; update range information and move along.
65840b57cec5SDimitry Andric         if (D.getSourceRange().getBegin().isInvalid())
65850b57cec5SDimitry Andric           D.SetRangeBegin(D.getName().getSourceRange().getBegin());
65860b57cec5SDimitry Andric         D.SetRangeEnd(D.getName().getSourceRange().getEnd());
65870b57cec5SDimitry Andric       }
65880b57cec5SDimitry Andric       goto PastIdentifier;
65890b57cec5SDimitry Andric     }
65900b57cec5SDimitry Andric 
65910b57cec5SDimitry Andric     if (D.getCXXScopeSpec().isNotEmpty()) {
65920b57cec5SDimitry Andric       // We have a scope specifier but no following unqualified-id.
65930b57cec5SDimitry Andric       Diag(PP.getLocForEndOfToken(D.getCXXScopeSpec().getEndLoc()),
65940b57cec5SDimitry Andric            diag::err_expected_unqualified_id)
65950b57cec5SDimitry Andric           << /*C++*/1;
65960b57cec5SDimitry Andric       D.SetIdentifier(nullptr, Tok.getLocation());
65970b57cec5SDimitry Andric       goto PastIdentifier;
65980b57cec5SDimitry Andric     }
65990b57cec5SDimitry Andric   } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
66000b57cec5SDimitry Andric     assert(!getLangOpts().CPlusPlus &&
66010b57cec5SDimitry Andric            "There's a C++-specific check for tok::identifier above");
66020b57cec5SDimitry Andric     assert(Tok.getIdentifierInfo() && "Not an identifier?");
66030b57cec5SDimitry Andric     D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
66040b57cec5SDimitry Andric     D.SetRangeEnd(Tok.getLocation());
66050b57cec5SDimitry Andric     ConsumeToken();
66060b57cec5SDimitry Andric     goto PastIdentifier;
66070b57cec5SDimitry Andric   } else if (Tok.is(tok::identifier) && !D.mayHaveIdentifier()) {
66080b57cec5SDimitry Andric     // We're not allowed an identifier here, but we got one. Try to figure out
66090b57cec5SDimitry Andric     // if the user was trying to attach a name to the type, or whether the name
66100b57cec5SDimitry Andric     // is some unrelated trailing syntax.
66110b57cec5SDimitry Andric     bool DiagnoseIdentifier = false;
66120b57cec5SDimitry Andric     if (D.hasGroupingParens())
66130b57cec5SDimitry Andric       // An identifier within parens is unlikely to be intended to be anything
66140b57cec5SDimitry Andric       // other than a name being "declared".
66150b57cec5SDimitry Andric       DiagnoseIdentifier = true;
6616e8d8bef9SDimitry Andric     else if (D.getContext() == DeclaratorContext::TemplateArg)
66170b57cec5SDimitry Andric       // T<int N> is an accidental identifier; T<int N indicates a missing '>'.
66180b57cec5SDimitry Andric       DiagnoseIdentifier =
66190b57cec5SDimitry Andric           NextToken().isOneOf(tok::comma, tok::greater, tok::greatergreater);
6620e8d8bef9SDimitry Andric     else if (D.getContext() == DeclaratorContext::AliasDecl ||
6621e8d8bef9SDimitry Andric              D.getContext() == DeclaratorContext::AliasTemplate)
66220b57cec5SDimitry Andric       // The most likely error is that the ';' was forgotten.
66230b57cec5SDimitry Andric       DiagnoseIdentifier = NextToken().isOneOf(tok::comma, tok::semi);
6624e8d8bef9SDimitry Andric     else if ((D.getContext() == DeclaratorContext::TrailingReturn ||
6625e8d8bef9SDimitry Andric               D.getContext() == DeclaratorContext::TrailingReturnVar) &&
66260b57cec5SDimitry Andric              !isCXX11VirtSpecifier(Tok))
66270b57cec5SDimitry Andric       DiagnoseIdentifier = NextToken().isOneOf(
66280b57cec5SDimitry Andric           tok::comma, tok::semi, tok::equal, tok::l_brace, tok::kw_try);
66290b57cec5SDimitry Andric     if (DiagnoseIdentifier) {
66300b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
66310b57cec5SDimitry Andric         << FixItHint::CreateRemoval(Tok.getLocation());
66320b57cec5SDimitry Andric       D.SetIdentifier(nullptr, Tok.getLocation());
66330b57cec5SDimitry Andric       ConsumeToken();
66340b57cec5SDimitry Andric       goto PastIdentifier;
66350b57cec5SDimitry Andric     }
66360b57cec5SDimitry Andric   }
66370b57cec5SDimitry Andric 
66380b57cec5SDimitry Andric   if (Tok.is(tok::l_paren)) {
66390b57cec5SDimitry Andric     // If this might be an abstract-declarator followed by a direct-initializer,
66400b57cec5SDimitry Andric     // check whether this is a valid declarator chunk. If it can't be, assume
66410b57cec5SDimitry Andric     // that it's an initializer instead.
66420b57cec5SDimitry Andric     if (D.mayOmitIdentifier() && D.mayBeFollowedByCXXDirectInit()) {
66430b57cec5SDimitry Andric       RevertingTentativeParsingAction PA(*this);
664406c3fb27SDimitry Andric       if (TryParseDeclarator(true, D.mayHaveIdentifier(), true,
664506c3fb27SDimitry Andric                              D.getDeclSpec().getTypeSpecType() == TST_auto) ==
66460b57cec5SDimitry Andric           TPResult::False) {
66470b57cec5SDimitry Andric         D.SetIdentifier(nullptr, Tok.getLocation());
66480b57cec5SDimitry Andric         goto PastIdentifier;
66490b57cec5SDimitry Andric       }
66500b57cec5SDimitry Andric     }
66510b57cec5SDimitry Andric 
66520b57cec5SDimitry Andric     // direct-declarator: '(' declarator ')'
66530b57cec5SDimitry Andric     // direct-declarator: '(' attributes declarator ')'
66540b57cec5SDimitry Andric     // Example: 'char (*X)'   or 'int (*XX)(void)'
66550b57cec5SDimitry Andric     ParseParenDeclarator(D);
66560b57cec5SDimitry Andric 
66570b57cec5SDimitry Andric     // If the declarator was parenthesized, we entered the declarator
66580b57cec5SDimitry Andric     // scope when parsing the parenthesized declarator, then exited
66590b57cec5SDimitry Andric     // the scope already. Re-enter the scope, if we need to.
66600b57cec5SDimitry Andric     if (D.getCXXScopeSpec().isSet()) {
66610b57cec5SDimitry Andric       // If there was an error parsing parenthesized declarator, declarator
66620b57cec5SDimitry Andric       // scope may have been entered before. Don't do it again.
66630b57cec5SDimitry Andric       if (!D.isInvalidType() &&
66640b57cec5SDimitry Andric           Actions.ShouldEnterDeclaratorScope(getCurScope(),
66650b57cec5SDimitry Andric                                              D.getCXXScopeSpec()))
66660b57cec5SDimitry Andric         // Change the declaration context for name lookup, until this function
66670b57cec5SDimitry Andric         // is exited (and the declarator has been parsed).
66680b57cec5SDimitry Andric         DeclScopeObj.EnterDeclaratorScope();
66690b57cec5SDimitry Andric     }
66700b57cec5SDimitry Andric   } else if (D.mayOmitIdentifier()) {
66710b57cec5SDimitry Andric     // This could be something simple like "int" (in which case the declarator
66720b57cec5SDimitry Andric     // portion is empty), if an abstract-declarator is allowed.
66730b57cec5SDimitry Andric     D.SetIdentifier(nullptr, Tok.getLocation());
66740b57cec5SDimitry Andric 
66750b57cec5SDimitry Andric     // The grammar for abstract-pack-declarator does not allow grouping parens.
66760b57cec5SDimitry Andric     // FIXME: Revisit this once core issue 1488 is resolved.
66770b57cec5SDimitry Andric     if (D.hasEllipsis() && D.hasGroupingParens())
66780b57cec5SDimitry Andric       Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
66790b57cec5SDimitry Andric            diag::ext_abstract_pack_declarator_parens);
66800b57cec5SDimitry Andric   } else {
66810b57cec5SDimitry Andric     if (Tok.getKind() == tok::annot_pragma_parser_crash)
66820b57cec5SDimitry Andric       LLVM_BUILTIN_TRAP;
66830b57cec5SDimitry Andric     if (Tok.is(tok::l_square))
66840b57cec5SDimitry Andric       return ParseMisplacedBracketDeclarator(D);
6685e8d8bef9SDimitry Andric     if (D.getContext() == DeclaratorContext::Member) {
66860b57cec5SDimitry Andric       // Objective-C++: Detect C++ keywords and try to prevent further errors by
66870b57cec5SDimitry Andric       // treating these keyword as valid member names.
66880b57cec5SDimitry Andric       if (getLangOpts().ObjC && getLangOpts().CPlusPlus &&
66895f757f3fSDimitry Andric           !Tok.isAnnotation() && Tok.getIdentifierInfo() &&
66900b57cec5SDimitry Andric           Tok.getIdentifierInfo()->isCPlusPlusKeyword(getLangOpts())) {
66910b57cec5SDimitry Andric         Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
66920b57cec5SDimitry Andric              diag::err_expected_member_name_or_semi_objcxx_keyword)
66930b57cec5SDimitry Andric             << Tok.getIdentifierInfo()
66940b57cec5SDimitry Andric             << (D.getDeclSpec().isEmpty() ? SourceRange()
66950b57cec5SDimitry Andric                                           : D.getDeclSpec().getSourceRange());
66960b57cec5SDimitry Andric         D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
66970b57cec5SDimitry Andric         D.SetRangeEnd(Tok.getLocation());
66980b57cec5SDimitry Andric         ConsumeToken();
66990b57cec5SDimitry Andric         goto PastIdentifier;
67000b57cec5SDimitry Andric       }
67010b57cec5SDimitry Andric       Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
67020b57cec5SDimitry Andric            diag::err_expected_member_name_or_semi)
67030b57cec5SDimitry Andric           << (D.getDeclSpec().isEmpty() ? SourceRange()
67040b57cec5SDimitry Andric                                         : D.getDeclSpec().getSourceRange());
6705972a253aSDimitry Andric     } else {
6706972a253aSDimitry Andric       if (Tok.getKind() == tok::TokenKind::kw_while) {
6707972a253aSDimitry Andric         Diag(Tok, diag::err_while_loop_outside_of_a_function);
67080b57cec5SDimitry Andric       } else if (getLangOpts().CPlusPlus) {
67090b57cec5SDimitry Andric         if (Tok.isOneOf(tok::period, tok::arrow))
67100b57cec5SDimitry Andric           Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
67110b57cec5SDimitry Andric         else {
67120b57cec5SDimitry Andric           SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
67130b57cec5SDimitry Andric           if (Tok.isAtStartOfLine() && Loc.isValid())
67140b57cec5SDimitry Andric             Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
67150b57cec5SDimitry Andric                 << getLangOpts().CPlusPlus;
67160b57cec5SDimitry Andric           else
67170b57cec5SDimitry Andric             Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
67180b57cec5SDimitry Andric                  diag::err_expected_unqualified_id)
67190b57cec5SDimitry Andric                 << getLangOpts().CPlusPlus;
67200b57cec5SDimitry Andric         }
67210b57cec5SDimitry Andric       } else {
67220b57cec5SDimitry Andric         Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
67230b57cec5SDimitry Andric              diag::err_expected_either)
67240b57cec5SDimitry Andric             << tok::identifier << tok::l_paren;
67250b57cec5SDimitry Andric       }
6726972a253aSDimitry Andric     }
67270b57cec5SDimitry Andric     D.SetIdentifier(nullptr, Tok.getLocation());
67280b57cec5SDimitry Andric     D.setInvalidType(true);
67290b57cec5SDimitry Andric   }
67300b57cec5SDimitry Andric 
67310b57cec5SDimitry Andric  PastIdentifier:
67320b57cec5SDimitry Andric   assert(D.isPastIdentifier() &&
67330b57cec5SDimitry Andric          "Haven't past the location of the identifier yet?");
67340b57cec5SDimitry Andric 
67350b57cec5SDimitry Andric   // Don't parse attributes unless we have parsed an unparenthesized name.
67360b57cec5SDimitry Andric   if (D.hasName() && !D.getNumTypeObjects())
67370b57cec5SDimitry Andric     MaybeParseCXX11Attributes(D);
67380b57cec5SDimitry Andric 
673904eeddc0SDimitry Andric   while (true) {
67400b57cec5SDimitry Andric     if (Tok.is(tok::l_paren)) {
674155e4f9d5SDimitry Andric       bool IsFunctionDeclaration = D.isFunctionDeclaratorAFunctionDeclaration();
67420b57cec5SDimitry Andric       // Enter function-declaration scope, limiting any declarators to the
67430b57cec5SDimitry Andric       // function prototype scope, including parameter declarators.
67440b57cec5SDimitry Andric       ParseScope PrototypeScope(this,
67450b57cec5SDimitry Andric                                 Scope::FunctionPrototypeScope|Scope::DeclScope|
674655e4f9d5SDimitry Andric                                 (IsFunctionDeclaration
67470b57cec5SDimitry Andric                                    ? Scope::FunctionDeclarationScope : 0));
67480b57cec5SDimitry Andric 
67490b57cec5SDimitry Andric       // The paren may be part of a C++ direct initializer, eg. "int x(1);".
67500b57cec5SDimitry Andric       // In such a case, check if we actually have a function declarator; if it
67510b57cec5SDimitry Andric       // is not, the declarator has been fully parsed.
67520b57cec5SDimitry Andric       bool IsAmbiguous = false;
67530b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
6754bdd1243dSDimitry Andric         // C++2a [temp.res]p5
6755bdd1243dSDimitry Andric         // A qualified-id is assumed to name a type if
6756bdd1243dSDimitry Andric         //   - [...]
6757bdd1243dSDimitry Andric         //   - it is a decl-specifier of the decl-specifier-seq of a
6758bdd1243dSDimitry Andric         //     - [...]
6759bdd1243dSDimitry Andric         //     - parameter-declaration in a member-declaration [...]
6760bdd1243dSDimitry Andric         //     - parameter-declaration in a declarator of a function or function
6761bdd1243dSDimitry Andric         //       template declaration whose declarator-id is qualified [...]
6762bdd1243dSDimitry Andric         auto AllowImplicitTypename = ImplicitTypenameContext::No;
6763bdd1243dSDimitry Andric         if (D.getCXXScopeSpec().isSet())
6764bdd1243dSDimitry Andric           AllowImplicitTypename =
6765bdd1243dSDimitry Andric               (ImplicitTypenameContext)Actions.isDeclaratorFunctionLike(D);
6766bdd1243dSDimitry Andric         else if (D.getContext() == DeclaratorContext::Member) {
6767bdd1243dSDimitry Andric           AllowImplicitTypename = ImplicitTypenameContext::Yes;
6768bdd1243dSDimitry Andric         }
6769bdd1243dSDimitry Andric 
67700b57cec5SDimitry Andric         // The name of the declarator, if any, is tentatively declared within
67710b57cec5SDimitry Andric         // a possible direct initializer.
67720b57cec5SDimitry Andric         TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
6773bdd1243dSDimitry Andric         bool IsFunctionDecl =
6774bdd1243dSDimitry Andric             isCXXFunctionDeclarator(&IsAmbiguous, AllowImplicitTypename);
67750b57cec5SDimitry Andric         TentativelyDeclaredIdentifiers.pop_back();
67760b57cec5SDimitry Andric         if (!IsFunctionDecl)
67770b57cec5SDimitry Andric           break;
67780b57cec5SDimitry Andric       }
67790b57cec5SDimitry Andric       ParsedAttributes attrs(AttrFactory);
67800b57cec5SDimitry Andric       BalancedDelimiterTracker T(*this, tok::l_paren);
67810b57cec5SDimitry Andric       T.consumeOpen();
678255e4f9d5SDimitry Andric       if (IsFunctionDeclaration)
678355e4f9d5SDimitry Andric         Actions.ActOnStartFunctionDeclarationDeclarator(D,
678455e4f9d5SDimitry Andric                                                         TemplateParameterDepth);
67850b57cec5SDimitry Andric       ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
678655e4f9d5SDimitry Andric       if (IsFunctionDeclaration)
678755e4f9d5SDimitry Andric         Actions.ActOnFinishFunctionDeclarationDeclarator(D);
67880b57cec5SDimitry Andric       PrototypeScope.Exit();
67890b57cec5SDimitry Andric     } else if (Tok.is(tok::l_square)) {
67900b57cec5SDimitry Andric       ParseBracketDeclarator(D);
679106c3fb27SDimitry Andric     } else if (Tok.isRegularKeywordAttribute()) {
679206c3fb27SDimitry Andric       // For consistency with attribute parsing.
679306c3fb27SDimitry Andric       Diag(Tok, diag::err_keyword_not_allowed) << Tok.getIdentifierInfo();
679406c3fb27SDimitry Andric       ConsumeToken();
6795480093f4SDimitry Andric     } else if (Tok.is(tok::kw_requires) && D.hasGroupingParens()) {
6796480093f4SDimitry Andric       // This declarator is declaring a function, but the requires clause is
6797480093f4SDimitry Andric       // in the wrong place:
6798480093f4SDimitry Andric       //   void (f() requires true);
6799480093f4SDimitry Andric       // instead of
6800480093f4SDimitry Andric       //   void f() requires true;
6801480093f4SDimitry Andric       // or
6802480093f4SDimitry Andric       //   void (f()) requires true;
6803480093f4SDimitry Andric       Diag(Tok, diag::err_requires_clause_inside_parens);
6804480093f4SDimitry Andric       ConsumeToken();
6805480093f4SDimitry Andric       ExprResult TrailingRequiresClause = Actions.CorrectDelayedTyposInExpr(
6806480093f4SDimitry Andric          ParseConstraintLogicalOrExpression(/*IsTrailingRequiresClause=*/true));
6807480093f4SDimitry Andric       if (TrailingRequiresClause.isUsable() && D.isFunctionDeclarator() &&
6808480093f4SDimitry Andric           !D.hasTrailingRequiresClause())
6809480093f4SDimitry Andric         // We're already ill-formed if we got here but we'll accept it anyway.
6810480093f4SDimitry Andric         D.setTrailingRequiresClause(TrailingRequiresClause.get());
68110b57cec5SDimitry Andric     } else {
68120b57cec5SDimitry Andric       break;
68130b57cec5SDimitry Andric     }
68140b57cec5SDimitry Andric   }
68150b57cec5SDimitry Andric }
68160b57cec5SDimitry Andric 
68170b57cec5SDimitry Andric void Parser::ParseDecompositionDeclarator(Declarator &D) {
68180b57cec5SDimitry Andric   assert(Tok.is(tok::l_square));
68190b57cec5SDimitry Andric 
68200b57cec5SDimitry Andric   // If this doesn't look like a structured binding, maybe it's a misplaced
68210b57cec5SDimitry Andric   // array declarator.
68220b57cec5SDimitry Andric   // FIXME: Consume the l_square first so we don't need extra lookahead for
68230b57cec5SDimitry Andric   // this.
68240b57cec5SDimitry Andric   if (!(NextToken().is(tok::identifier) &&
68250b57cec5SDimitry Andric         GetLookAheadToken(2).isOneOf(tok::comma, tok::r_square)) &&
68260b57cec5SDimitry Andric       !(NextToken().is(tok::r_square) &&
68270b57cec5SDimitry Andric         GetLookAheadToken(2).isOneOf(tok::equal, tok::l_brace)))
68280b57cec5SDimitry Andric     return ParseMisplacedBracketDeclarator(D);
68290b57cec5SDimitry Andric 
68300b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_square);
68310b57cec5SDimitry Andric   T.consumeOpen();
68320b57cec5SDimitry Andric 
68330b57cec5SDimitry Andric   SmallVector<DecompositionDeclarator::Binding, 32> Bindings;
68340b57cec5SDimitry Andric   while (Tok.isNot(tok::r_square)) {
68350b57cec5SDimitry Andric     if (!Bindings.empty()) {
68360b57cec5SDimitry Andric       if (Tok.is(tok::comma))
68370b57cec5SDimitry Andric         ConsumeToken();
68380b57cec5SDimitry Andric       else {
68390b57cec5SDimitry Andric         if (Tok.is(tok::identifier)) {
68400b57cec5SDimitry Andric           SourceLocation EndLoc = getEndOfPreviousToken();
68410b57cec5SDimitry Andric           Diag(EndLoc, diag::err_expected)
68420b57cec5SDimitry Andric               << tok::comma << FixItHint::CreateInsertion(EndLoc, ",");
68430b57cec5SDimitry Andric         } else {
68440b57cec5SDimitry Andric           Diag(Tok, diag::err_expected_comma_or_rsquare);
68450b57cec5SDimitry Andric         }
68460b57cec5SDimitry Andric 
68470b57cec5SDimitry Andric         SkipUntil(tok::r_square, tok::comma, tok::identifier,
68480b57cec5SDimitry Andric                   StopAtSemi | StopBeforeMatch);
68490b57cec5SDimitry Andric         if (Tok.is(tok::comma))
68500b57cec5SDimitry Andric           ConsumeToken();
68510b57cec5SDimitry Andric         else if (Tok.isNot(tok::identifier))
68520b57cec5SDimitry Andric           break;
68530b57cec5SDimitry Andric       }
68540b57cec5SDimitry Andric     }
68550b57cec5SDimitry Andric 
68560b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
68570b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::identifier;
68580b57cec5SDimitry Andric       break;
68590b57cec5SDimitry Andric     }
68600b57cec5SDimitry Andric 
68610b57cec5SDimitry Andric     Bindings.push_back({Tok.getIdentifierInfo(), Tok.getLocation()});
68620b57cec5SDimitry Andric     ConsumeToken();
68630b57cec5SDimitry Andric   }
68640b57cec5SDimitry Andric 
68650b57cec5SDimitry Andric   if (Tok.isNot(tok::r_square))
68660b57cec5SDimitry Andric     // We've already diagnosed a problem here.
68670b57cec5SDimitry Andric     T.skipToEnd();
68680b57cec5SDimitry Andric   else {
68690b57cec5SDimitry Andric     // C++17 does not allow the identifier-list in a structured binding
68700b57cec5SDimitry Andric     // to be empty.
68710b57cec5SDimitry Andric     if (Bindings.empty())
68720b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::ext_decomp_decl_empty);
68730b57cec5SDimitry Andric 
68740b57cec5SDimitry Andric     T.consumeClose();
68750b57cec5SDimitry Andric   }
68760b57cec5SDimitry Andric 
68770b57cec5SDimitry Andric   return D.setDecompositionBindings(T.getOpenLocation(), Bindings,
68780b57cec5SDimitry Andric                                     T.getCloseLocation());
68790b57cec5SDimitry Andric }
68800b57cec5SDimitry Andric 
68810b57cec5SDimitry Andric /// ParseParenDeclarator - We parsed the declarator D up to a paren.  This is
68820b57cec5SDimitry Andric /// only called before the identifier, so these are most likely just grouping
68830b57cec5SDimitry Andric /// parens for precedence.  If we find that these are actually function
68840b57cec5SDimitry Andric /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
68850b57cec5SDimitry Andric ///
68860b57cec5SDimitry Andric ///       direct-declarator:
68870b57cec5SDimitry Andric ///         '(' declarator ')'
68880b57cec5SDimitry Andric /// [GNU]   '(' attributes declarator ')'
68890b57cec5SDimitry Andric ///         direct-declarator '(' parameter-type-list ')'
68900b57cec5SDimitry Andric ///         direct-declarator '(' identifier-list[opt] ')'
68910b57cec5SDimitry Andric /// [GNU]   direct-declarator '(' parameter-forward-declarations
68920b57cec5SDimitry Andric ///                    parameter-type-list[opt] ')'
68930b57cec5SDimitry Andric ///
68940b57cec5SDimitry Andric void Parser::ParseParenDeclarator(Declarator &D) {
68950b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
68960b57cec5SDimitry Andric   T.consumeOpen();
68970b57cec5SDimitry Andric 
68980b57cec5SDimitry Andric   assert(!D.isPastIdentifier() && "Should be called before passing identifier");
68990b57cec5SDimitry Andric 
69000b57cec5SDimitry Andric   // Eat any attributes before we look at whether this is a grouping or function
69010b57cec5SDimitry Andric   // declarator paren.  If this is a grouping paren, the attribute applies to
69020b57cec5SDimitry Andric   // the type being built up, for example:
69030b57cec5SDimitry Andric   //     int (__attribute__(()) *x)(long y)
69040b57cec5SDimitry Andric   // If this ends up not being a grouping paren, the attribute applies to the
69050b57cec5SDimitry Andric   // first argument, for example:
69060b57cec5SDimitry Andric   //     int (__attribute__(()) int x)
69070b57cec5SDimitry Andric   // In either case, we need to eat any attributes to be able to determine what
69080b57cec5SDimitry Andric   // sort of paren this is.
69090b57cec5SDimitry Andric   //
69100b57cec5SDimitry Andric   ParsedAttributes attrs(AttrFactory);
69110b57cec5SDimitry Andric   bool RequiresArg = false;
69120b57cec5SDimitry Andric   if (Tok.is(tok::kw___attribute)) {
69130b57cec5SDimitry Andric     ParseGNUAttributes(attrs);
69140b57cec5SDimitry Andric 
69150b57cec5SDimitry Andric     // We require that the argument list (if this is a non-grouping paren) be
69160b57cec5SDimitry Andric     // present even if the attribute list was empty.
69170b57cec5SDimitry Andric     RequiresArg = true;
69180b57cec5SDimitry Andric   }
69190b57cec5SDimitry Andric 
69200b57cec5SDimitry Andric   // Eat any Microsoft extensions.
69210b57cec5SDimitry Andric   ParseMicrosoftTypeAttributes(attrs);
69220b57cec5SDimitry Andric 
69230b57cec5SDimitry Andric   // Eat any Borland extensions.
69240b57cec5SDimitry Andric   if  (Tok.is(tok::kw___pascal))
69250b57cec5SDimitry Andric     ParseBorlandTypeAttributes(attrs);
69260b57cec5SDimitry Andric 
69270b57cec5SDimitry Andric   // If we haven't past the identifier yet (or where the identifier would be
69280b57cec5SDimitry Andric   // stored, if this is an abstract declarator), then this is probably just
69290b57cec5SDimitry Andric   // grouping parens. However, if this could be an abstract-declarator, then
69300b57cec5SDimitry Andric   // this could also be the start of function arguments (consider 'void()').
69310b57cec5SDimitry Andric   bool isGrouping;
69320b57cec5SDimitry Andric 
69330b57cec5SDimitry Andric   if (!D.mayOmitIdentifier()) {
69340b57cec5SDimitry Andric     // If this can't be an abstract-declarator, this *must* be a grouping
69350b57cec5SDimitry Andric     // paren, because we haven't seen the identifier yet.
69360b57cec5SDimitry Andric     isGrouping = true;
69370b57cec5SDimitry Andric   } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
69380b57cec5SDimitry Andric              (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
69390b57cec5SDimitry Andric               NextToken().is(tok::r_paren)) || // C++ int(...)
6940bdd1243dSDimitry Andric              isDeclarationSpecifier(
6941bdd1243dSDimitry Andric                  ImplicitTypenameContext::No) || // 'int(int)' is a function.
69420b57cec5SDimitry Andric              isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
69430b57cec5SDimitry Andric     // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
69440b57cec5SDimitry Andric     // considered to be a type, not a K&R identifier-list.
69450b57cec5SDimitry Andric     isGrouping = false;
69460b57cec5SDimitry Andric   } else {
69470b57cec5SDimitry Andric     // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
69480b57cec5SDimitry Andric     isGrouping = true;
69490b57cec5SDimitry Andric   }
69500b57cec5SDimitry Andric 
69510b57cec5SDimitry Andric   // If this is a grouping paren, handle:
69520b57cec5SDimitry Andric   // direct-declarator: '(' declarator ')'
69530b57cec5SDimitry Andric   // direct-declarator: '(' attributes declarator ')'
69540b57cec5SDimitry Andric   if (isGrouping) {
69550b57cec5SDimitry Andric     SourceLocation EllipsisLoc = D.getEllipsisLoc();
69560b57cec5SDimitry Andric     D.setEllipsisLoc(SourceLocation());
69570b57cec5SDimitry Andric 
69580b57cec5SDimitry Andric     bool hadGroupingParens = D.hasGroupingParens();
69590b57cec5SDimitry Andric     D.setGroupingParens(true);
69600b57cec5SDimitry Andric     ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
69610b57cec5SDimitry Andric     // Match the ')'.
69620b57cec5SDimitry Andric     T.consumeClose();
69630b57cec5SDimitry Andric     D.AddTypeInfo(
69640b57cec5SDimitry Andric         DeclaratorChunk::getParen(T.getOpenLocation(), T.getCloseLocation()),
69650b57cec5SDimitry Andric         std::move(attrs), T.getCloseLocation());
69660b57cec5SDimitry Andric 
69670b57cec5SDimitry Andric     D.setGroupingParens(hadGroupingParens);
69680b57cec5SDimitry Andric 
69690b57cec5SDimitry Andric     // An ellipsis cannot be placed outside parentheses.
69700b57cec5SDimitry Andric     if (EllipsisLoc.isValid())
69710b57cec5SDimitry Andric       DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
69720b57cec5SDimitry Andric 
69730b57cec5SDimitry Andric     return;
69740b57cec5SDimitry Andric   }
69750b57cec5SDimitry Andric 
69760b57cec5SDimitry Andric   // Okay, if this wasn't a grouping paren, it must be the start of a function
69770b57cec5SDimitry Andric   // argument list.  Recognize that this declarator will never have an
69780b57cec5SDimitry Andric   // identifier (and remember where it would have been), then call into
69790b57cec5SDimitry Andric   // ParseFunctionDeclarator to handle of argument list.
69800b57cec5SDimitry Andric   D.SetIdentifier(nullptr, Tok.getLocation());
69810b57cec5SDimitry Andric 
69820b57cec5SDimitry Andric   // Enter function-declaration scope, limiting any declarators to the
69830b57cec5SDimitry Andric   // function prototype scope, including parameter declarators.
69840b57cec5SDimitry Andric   ParseScope PrototypeScope(this,
69850b57cec5SDimitry Andric                             Scope::FunctionPrototypeScope | Scope::DeclScope |
69860b57cec5SDimitry Andric                             (D.isFunctionDeclaratorAFunctionDeclaration()
69870b57cec5SDimitry Andric                                ? Scope::FunctionDeclarationScope : 0));
69880b57cec5SDimitry Andric   ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
69890b57cec5SDimitry Andric   PrototypeScope.Exit();
69900b57cec5SDimitry Andric }
69910b57cec5SDimitry Andric 
6992480093f4SDimitry Andric void Parser::InitCXXThisScopeForDeclaratorIfRelevant(
6993480093f4SDimitry Andric     const Declarator &D, const DeclSpec &DS,
6994bdd1243dSDimitry Andric     std::optional<Sema::CXXThisScopeRAII> &ThisScope) {
6995480093f4SDimitry Andric   // C++11 [expr.prim.general]p3:
6996480093f4SDimitry Andric   //   If a declaration declares a member function or member function
6997480093f4SDimitry Andric   //   template of a class X, the expression this is a prvalue of type
6998480093f4SDimitry Andric   //   "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
6999480093f4SDimitry Andric   //   and the end of the function-definition, member-declarator, or
7000480093f4SDimitry Andric   //   declarator.
7001480093f4SDimitry Andric   // FIXME: currently, "static" case isn't handled correctly.
7002e8d8bef9SDimitry Andric   bool IsCXX11MemberFunction =
7003e8d8bef9SDimitry Andric       getLangOpts().CPlusPlus11 &&
7004480093f4SDimitry Andric       D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
7005e8d8bef9SDimitry Andric       (D.getContext() == DeclaratorContext::Member
7006480093f4SDimitry Andric            ? !D.getDeclSpec().isFriendSpecified()
7007e8d8bef9SDimitry Andric            : D.getContext() == DeclaratorContext::File &&
7008480093f4SDimitry Andric                  D.getCXXScopeSpec().isValid() &&
7009480093f4SDimitry Andric                  Actions.CurContext->isRecord());
7010480093f4SDimitry Andric   if (!IsCXX11MemberFunction)
7011480093f4SDimitry Andric     return;
7012480093f4SDimitry Andric 
7013480093f4SDimitry Andric   Qualifiers Q = Qualifiers::fromCVRUMask(DS.getTypeQualifiers());
7014480093f4SDimitry Andric   if (D.getDeclSpec().hasConstexprSpecifier() && !getLangOpts().CPlusPlus14)
7015480093f4SDimitry Andric     Q.addConst();
7016480093f4SDimitry Andric   // FIXME: Collect C++ address spaces.
7017480093f4SDimitry Andric   // If there are multiple different address spaces, the source is invalid.
7018480093f4SDimitry Andric   // Carry on using the first addr space for the qualifiers of 'this'.
7019480093f4SDimitry Andric   // The diagnostic will be given later while creating the function
7020480093f4SDimitry Andric   // prototype for the method.
7021480093f4SDimitry Andric   if (getLangOpts().OpenCLCPlusPlus) {
7022480093f4SDimitry Andric     for (ParsedAttr &attr : DS.getAttributes()) {
7023480093f4SDimitry Andric       LangAS ASIdx = attr.asOpenCLLangAS();
7024480093f4SDimitry Andric       if (ASIdx != LangAS::Default) {
7025480093f4SDimitry Andric         Q.addAddressSpace(ASIdx);
7026480093f4SDimitry Andric         break;
7027480093f4SDimitry Andric       }
7028480093f4SDimitry Andric     }
7029480093f4SDimitry Andric   }
7030480093f4SDimitry Andric   ThisScope.emplace(Actions, dyn_cast<CXXRecordDecl>(Actions.CurContext), Q,
7031480093f4SDimitry Andric                     IsCXX11MemberFunction);
7032480093f4SDimitry Andric }
7033480093f4SDimitry Andric 
70340b57cec5SDimitry Andric /// ParseFunctionDeclarator - We are after the identifier and have parsed the
70350b57cec5SDimitry Andric /// declarator D up to a paren, which indicates that we are parsing function
70360b57cec5SDimitry Andric /// arguments.
70370b57cec5SDimitry Andric ///
703881ad6265SDimitry Andric /// If FirstArgAttrs is non-null, then the caller parsed those attributes
703981ad6265SDimitry Andric /// immediately after the open paren - they will be applied to the DeclSpec
704081ad6265SDimitry Andric /// of the first parameter.
70410b57cec5SDimitry Andric ///
70420b57cec5SDimitry Andric /// If RequiresArg is true, then the first argument of the function is required
70430b57cec5SDimitry Andric /// to be present and required to not be an identifier list.
70440b57cec5SDimitry Andric ///
70450b57cec5SDimitry Andric /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
70460b57cec5SDimitry Andric /// (C++11) ref-qualifier[opt], exception-specification[opt],
7047480093f4SDimitry Andric /// (C++11) attribute-specifier-seq[opt], (C++11) trailing-return-type[opt] and
7048480093f4SDimitry Andric /// (C++2a) the trailing requires-clause.
70490b57cec5SDimitry Andric ///
70500b57cec5SDimitry Andric /// [C++11] exception-specification:
70510b57cec5SDimitry Andric ///           dynamic-exception-specification
70520b57cec5SDimitry Andric ///           noexcept-specification
70530b57cec5SDimitry Andric ///
70540b57cec5SDimitry Andric void Parser::ParseFunctionDeclarator(Declarator &D,
70550b57cec5SDimitry Andric                                      ParsedAttributes &FirstArgAttrs,
70560b57cec5SDimitry Andric                                      BalancedDelimiterTracker &Tracker,
70570b57cec5SDimitry Andric                                      bool IsAmbiguous,
70580b57cec5SDimitry Andric                                      bool RequiresArg) {
70590b57cec5SDimitry Andric   assert(getCurScope()->isFunctionPrototypeScope() &&
70600b57cec5SDimitry Andric          "Should call from a Function scope");
70610b57cec5SDimitry Andric   // lparen is already consumed!
70620b57cec5SDimitry Andric   assert(D.isPastIdentifier() && "Should not call before identifier!");
70630b57cec5SDimitry Andric 
70640b57cec5SDimitry Andric   // This should be true when the function has typed arguments.
70650b57cec5SDimitry Andric   // Otherwise, it is treated as a K&R-style function.
70660b57cec5SDimitry Andric   bool HasProto = false;
70670b57cec5SDimitry Andric   // Build up an array of information about the parsed arguments.
70680b57cec5SDimitry Andric   SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
70690b57cec5SDimitry Andric   // Remember where we see an ellipsis, if any.
70700b57cec5SDimitry Andric   SourceLocation EllipsisLoc;
70710b57cec5SDimitry Andric 
70720b57cec5SDimitry Andric   DeclSpec DS(AttrFactory);
70730b57cec5SDimitry Andric   bool RefQualifierIsLValueRef = true;
70740b57cec5SDimitry Andric   SourceLocation RefQualifierLoc;
70750b57cec5SDimitry Andric   ExceptionSpecificationType ESpecType = EST_None;
70760b57cec5SDimitry Andric   SourceRange ESpecRange;
70770b57cec5SDimitry Andric   SmallVector<ParsedType, 2> DynamicExceptions;
70780b57cec5SDimitry Andric   SmallVector<SourceRange, 2> DynamicExceptionRanges;
70790b57cec5SDimitry Andric   ExprResult NoexceptExpr;
70800b57cec5SDimitry Andric   CachedTokens *ExceptionSpecTokens = nullptr;
708181ad6265SDimitry Andric   ParsedAttributes FnAttrs(AttrFactory);
70820b57cec5SDimitry Andric   TypeResult TrailingReturnType;
7083e8d8bef9SDimitry Andric   SourceLocation TrailingReturnTypeLoc;
70840b57cec5SDimitry Andric 
70850b57cec5SDimitry Andric   /* LocalEndLoc is the end location for the local FunctionTypeLoc.
70860b57cec5SDimitry Andric      EndLoc is the end location for the function declarator.
70870b57cec5SDimitry Andric      They differ for trailing return types. */
70880b57cec5SDimitry Andric   SourceLocation StartLoc, LocalEndLoc, EndLoc;
70890b57cec5SDimitry Andric   SourceLocation LParenLoc, RParenLoc;
70900b57cec5SDimitry Andric   LParenLoc = Tracker.getOpenLocation();
70910b57cec5SDimitry Andric   StartLoc = LParenLoc;
70920b57cec5SDimitry Andric 
70930b57cec5SDimitry Andric   if (isFunctionDeclaratorIdentifierList()) {
70940b57cec5SDimitry Andric     if (RequiresArg)
70950b57cec5SDimitry Andric       Diag(Tok, diag::err_argument_required_after_attribute);
70960b57cec5SDimitry Andric 
70970b57cec5SDimitry Andric     ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
70980b57cec5SDimitry Andric 
70990b57cec5SDimitry Andric     Tracker.consumeClose();
71000b57cec5SDimitry Andric     RParenLoc = Tracker.getCloseLocation();
71010b57cec5SDimitry Andric     LocalEndLoc = RParenLoc;
71020b57cec5SDimitry Andric     EndLoc = RParenLoc;
71030b57cec5SDimitry Andric 
71040b57cec5SDimitry Andric     // If there are attributes following the identifier list, parse them and
71050b57cec5SDimitry Andric     // prohibit them.
71060b57cec5SDimitry Andric     MaybeParseCXX11Attributes(FnAttrs);
71070b57cec5SDimitry Andric     ProhibitAttributes(FnAttrs);
71080b57cec5SDimitry Andric   } else {
71090b57cec5SDimitry Andric     if (Tok.isNot(tok::r_paren))
7110bdd1243dSDimitry Andric       ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
71110b57cec5SDimitry Andric     else if (RequiresArg)
71120b57cec5SDimitry Andric       Diag(Tok, diag::err_argument_required_after_attribute);
71130b57cec5SDimitry Andric 
711481ad6265SDimitry Andric     // OpenCL disallows functions without a prototype, but it doesn't enforce
71155f757f3fSDimitry Andric     // strict prototypes as in C23 because it allows a function definition to
711681ad6265SDimitry Andric     // have an identifier list. See OpenCL 3.0 6.11/g for more details.
711781ad6265SDimitry Andric     HasProto = ParamInfo.size() || getLangOpts().requiresStrictPrototypes() ||
711881ad6265SDimitry Andric                getLangOpts().OpenCL;
71190b57cec5SDimitry Andric 
71200b57cec5SDimitry Andric     // If we have the closing ')', eat it.
71210b57cec5SDimitry Andric     Tracker.consumeClose();
71220b57cec5SDimitry Andric     RParenLoc = Tracker.getCloseLocation();
71230b57cec5SDimitry Andric     LocalEndLoc = RParenLoc;
71240b57cec5SDimitry Andric     EndLoc = RParenLoc;
71250b57cec5SDimitry Andric 
71260b57cec5SDimitry Andric     if (getLangOpts().CPlusPlus) {
71270b57cec5SDimitry Andric       // FIXME: Accept these components in any order, and produce fixits to
71280b57cec5SDimitry Andric       // correct the order if the user gets it wrong. Ideally we should deal
71290b57cec5SDimitry Andric       // with the pure-specifier in the same way.
71300b57cec5SDimitry Andric 
71310b57cec5SDimitry Andric       // Parse cv-qualifier-seq[opt].
71320b57cec5SDimitry Andric       ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed,
71330b57cec5SDimitry Andric                                 /*AtomicAllowed*/ false,
71340b57cec5SDimitry Andric                                 /*IdentifierRequired=*/false,
71350b57cec5SDimitry Andric                                 llvm::function_ref<void()>([&]() {
71360b57cec5SDimitry Andric                                   Actions.CodeCompleteFunctionQualifiers(DS, D);
71370b57cec5SDimitry Andric                                 }));
71380b57cec5SDimitry Andric       if (!DS.getSourceRange().getEnd().isInvalid()) {
71390b57cec5SDimitry Andric         EndLoc = DS.getSourceRange().getEnd();
71400b57cec5SDimitry Andric       }
71410b57cec5SDimitry Andric 
71420b57cec5SDimitry Andric       // Parse ref-qualifier[opt].
71430b57cec5SDimitry Andric       if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc))
71440b57cec5SDimitry Andric         EndLoc = RefQualifierLoc;
71450b57cec5SDimitry Andric 
7146bdd1243dSDimitry Andric       std::optional<Sema::CXXThisScopeRAII> ThisScope;
7147480093f4SDimitry Andric       InitCXXThisScopeForDeclaratorIfRelevant(D, DS, ThisScope);
71480b57cec5SDimitry Andric 
71490b57cec5SDimitry Andric       // Parse exception-specification[opt].
7150e8d8bef9SDimitry Andric       // FIXME: Per [class.mem]p6, all exception-specifications at class scope
7151e8d8bef9SDimitry Andric       // should be delayed, including those for non-members (eg, friend
7152e8d8bef9SDimitry Andric       // declarations). But only applying this to member declarations is
7153e8d8bef9SDimitry Andric       // consistent with what other implementations do.
71540b57cec5SDimitry Andric       bool Delayed = D.isFirstDeclarationOfMember() &&
71550b57cec5SDimitry Andric                      D.isFunctionDeclaratorAFunctionDeclaration();
71560b57cec5SDimitry Andric       if (Delayed && Actions.isLibstdcxxEagerExceptionSpecHack(D) &&
71570b57cec5SDimitry Andric           GetLookAheadToken(0).is(tok::kw_noexcept) &&
71580b57cec5SDimitry Andric           GetLookAheadToken(1).is(tok::l_paren) &&
71590b57cec5SDimitry Andric           GetLookAheadToken(2).is(tok::kw_noexcept) &&
71600b57cec5SDimitry Andric           GetLookAheadToken(3).is(tok::l_paren) &&
71610b57cec5SDimitry Andric           GetLookAheadToken(4).is(tok::identifier) &&
71620b57cec5SDimitry Andric           GetLookAheadToken(4).getIdentifierInfo()->isStr("swap")) {
71630b57cec5SDimitry Andric         // HACK: We've got an exception-specification
71640b57cec5SDimitry Andric         //   noexcept(noexcept(swap(...)))
71650b57cec5SDimitry Andric         // or
71660b57cec5SDimitry Andric         //   noexcept(noexcept(swap(...)) && noexcept(swap(...)))
71670b57cec5SDimitry Andric         // on a 'swap' member function. This is a libstdc++ bug; the lookup
71680b57cec5SDimitry Andric         // for 'swap' will only find the function we're currently declaring,
71690b57cec5SDimitry Andric         // whereas it expects to find a non-member swap through ADL. Turn off
71700b57cec5SDimitry Andric         // delayed parsing to give it a chance to find what it expects.
71710b57cec5SDimitry Andric         Delayed = false;
71720b57cec5SDimitry Andric       }
71730b57cec5SDimitry Andric       ESpecType = tryParseExceptionSpecification(Delayed,
71740b57cec5SDimitry Andric                                                  ESpecRange,
71750b57cec5SDimitry Andric                                                  DynamicExceptions,
71760b57cec5SDimitry Andric                                                  DynamicExceptionRanges,
71770b57cec5SDimitry Andric                                                  NoexceptExpr,
71780b57cec5SDimitry Andric                                                  ExceptionSpecTokens);
71790b57cec5SDimitry Andric       if (ESpecType != EST_None)
71800b57cec5SDimitry Andric         EndLoc = ESpecRange.getEnd();
71810b57cec5SDimitry Andric 
71820b57cec5SDimitry Andric       // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
71830b57cec5SDimitry Andric       // after the exception-specification.
71840b57cec5SDimitry Andric       MaybeParseCXX11Attributes(FnAttrs);
71850b57cec5SDimitry Andric 
71860b57cec5SDimitry Andric       // Parse trailing-return-type[opt].
71870b57cec5SDimitry Andric       LocalEndLoc = EndLoc;
71880b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
71890b57cec5SDimitry Andric         Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
71900b57cec5SDimitry Andric         if (D.getDeclSpec().getTypeSpecType() == TST_auto)
71910b57cec5SDimitry Andric           StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
71920b57cec5SDimitry Andric         LocalEndLoc = Tok.getLocation();
71930b57cec5SDimitry Andric         SourceRange Range;
71940b57cec5SDimitry Andric         TrailingReturnType =
71950b57cec5SDimitry Andric             ParseTrailingReturnType(Range, D.mayBeFollowedByCXXDirectInit());
7196e8d8bef9SDimitry Andric         TrailingReturnTypeLoc = Range.getBegin();
71970b57cec5SDimitry Andric         EndLoc = Range.getEnd();
71980b57cec5SDimitry Andric       }
719906c3fb27SDimitry Andric     } else {
72000b57cec5SDimitry Andric       MaybeParseCXX11Attributes(FnAttrs);
72010b57cec5SDimitry Andric     }
72020b57cec5SDimitry Andric   }
72030b57cec5SDimitry Andric 
72040b57cec5SDimitry Andric   // Collect non-parameter declarations from the prototype if this is a function
72050b57cec5SDimitry Andric   // declaration. They will be moved into the scope of the function. Only do
72060b57cec5SDimitry Andric   // this in C and not C++, where the decls will continue to live in the
72070b57cec5SDimitry Andric   // surrounding context.
72080b57cec5SDimitry Andric   SmallVector<NamedDecl *, 0> DeclsInPrototype;
720981ad6265SDimitry Andric   if (getCurScope()->isFunctionDeclarationScope() && !getLangOpts().CPlusPlus) {
72100b57cec5SDimitry Andric     for (Decl *D : getCurScope()->decls()) {
72110b57cec5SDimitry Andric       NamedDecl *ND = dyn_cast<NamedDecl>(D);
72120b57cec5SDimitry Andric       if (!ND || isa<ParmVarDecl>(ND))
72130b57cec5SDimitry Andric         continue;
72140b57cec5SDimitry Andric       DeclsInPrototype.push_back(ND);
72150b57cec5SDimitry Andric     }
721606c3fb27SDimitry Andric     // Sort DeclsInPrototype based on raw encoding of the source location.
721706c3fb27SDimitry Andric     // Scope::decls() is iterating over a SmallPtrSet so sort the Decls before
721806c3fb27SDimitry Andric     // moving to DeclContext. This provides a stable ordering for traversing
721906c3fb27SDimitry Andric     // Decls in DeclContext, which is important for tasks like ASTWriter for
722006c3fb27SDimitry Andric     // deterministic output.
722106c3fb27SDimitry Andric     llvm::sort(DeclsInPrototype, [](Decl *D1, Decl *D2) {
722206c3fb27SDimitry Andric       return D1->getLocation().getRawEncoding() <
722306c3fb27SDimitry Andric              D2->getLocation().getRawEncoding();
722406c3fb27SDimitry Andric     });
72250b57cec5SDimitry Andric   }
72260b57cec5SDimitry Andric 
72270b57cec5SDimitry Andric   // Remember that we parsed a function type, and remember the attributes.
72280b57cec5SDimitry Andric   D.AddTypeInfo(DeclaratorChunk::getFunction(
72290b57cec5SDimitry Andric                     HasProto, IsAmbiguous, LParenLoc, ParamInfo.data(),
72300b57cec5SDimitry Andric                     ParamInfo.size(), EllipsisLoc, RParenLoc,
72310b57cec5SDimitry Andric                     RefQualifierIsLValueRef, RefQualifierLoc,
72320b57cec5SDimitry Andric                     /*MutableLoc=*/SourceLocation(),
72330b57cec5SDimitry Andric                     ESpecType, ESpecRange, DynamicExceptions.data(),
72340b57cec5SDimitry Andric                     DynamicExceptionRanges.data(), DynamicExceptions.size(),
72350b57cec5SDimitry Andric                     NoexceptExpr.isUsable() ? NoexceptExpr.get() : nullptr,
72360b57cec5SDimitry Andric                     ExceptionSpecTokens, DeclsInPrototype, StartLoc,
7237e8d8bef9SDimitry Andric                     LocalEndLoc, D, TrailingReturnType, TrailingReturnTypeLoc,
7238e8d8bef9SDimitry Andric                     &DS),
72390b57cec5SDimitry Andric                 std::move(FnAttrs), EndLoc);
72400b57cec5SDimitry Andric }
72410b57cec5SDimitry Andric 
72420b57cec5SDimitry Andric /// ParseRefQualifier - Parses a member function ref-qualifier. Returns
72430b57cec5SDimitry Andric /// true if a ref-qualifier is found.
72440b57cec5SDimitry Andric bool Parser::ParseRefQualifier(bool &RefQualifierIsLValueRef,
72450b57cec5SDimitry Andric                                SourceLocation &RefQualifierLoc) {
72460b57cec5SDimitry Andric   if (Tok.isOneOf(tok::amp, tok::ampamp)) {
72470b57cec5SDimitry Andric     Diag(Tok, getLangOpts().CPlusPlus11 ?
72480b57cec5SDimitry Andric          diag::warn_cxx98_compat_ref_qualifier :
72490b57cec5SDimitry Andric          diag::ext_ref_qualifier);
72500b57cec5SDimitry Andric 
72510b57cec5SDimitry Andric     RefQualifierIsLValueRef = Tok.is(tok::amp);
72520b57cec5SDimitry Andric     RefQualifierLoc = ConsumeToken();
72530b57cec5SDimitry Andric     return true;
72540b57cec5SDimitry Andric   }
72550b57cec5SDimitry Andric   return false;
72560b57cec5SDimitry Andric }
72570b57cec5SDimitry Andric 
72580b57cec5SDimitry Andric /// isFunctionDeclaratorIdentifierList - This parameter list may have an
72590b57cec5SDimitry Andric /// identifier list form for a K&R-style function:  void foo(a,b,c)
72600b57cec5SDimitry Andric ///
72610b57cec5SDimitry Andric /// Note that identifier-lists are only allowed for normal declarators, not for
72620b57cec5SDimitry Andric /// abstract-declarators.
72630b57cec5SDimitry Andric bool Parser::isFunctionDeclaratorIdentifierList() {
726481ad6265SDimitry Andric   return !getLangOpts().requiresStrictPrototypes()
72650b57cec5SDimitry Andric          && Tok.is(tok::identifier)
72660b57cec5SDimitry Andric          && !TryAltiVecVectorToken()
72670b57cec5SDimitry Andric          // K&R identifier lists can't have typedefs as identifiers, per C99
72680b57cec5SDimitry Andric          // 6.7.5.3p11.
72690b57cec5SDimitry Andric          && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
72700b57cec5SDimitry Andric          // Identifier lists follow a really simple grammar: the identifiers can
72710b57cec5SDimitry Andric          // be followed *only* by a ", identifier" or ")".  However, K&R
72720b57cec5SDimitry Andric          // identifier lists are really rare in the brave new modern world, and
72730b57cec5SDimitry Andric          // it is very common for someone to typo a type in a non-K&R style
72740b57cec5SDimitry Andric          // list.  If we are presented with something like: "void foo(intptr x,
72750b57cec5SDimitry Andric          // float y)", we don't want to start parsing the function declarator as
72760b57cec5SDimitry Andric          // though it is a K&R style declarator just because intptr is an
72770b57cec5SDimitry Andric          // invalid type.
72780b57cec5SDimitry Andric          //
72790b57cec5SDimitry Andric          // To handle this, we check to see if the token after the first
72800b57cec5SDimitry Andric          // identifier is a "," or ")".  Only then do we parse it as an
72810b57cec5SDimitry Andric          // identifier list.
72820b57cec5SDimitry Andric          && (!Tok.is(tok::eof) &&
72830b57cec5SDimitry Andric              (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)));
72840b57cec5SDimitry Andric }
72850b57cec5SDimitry Andric 
72860b57cec5SDimitry Andric /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
72870b57cec5SDimitry Andric /// we found a K&R-style identifier list instead of a typed parameter list.
72880b57cec5SDimitry Andric ///
72890b57cec5SDimitry Andric /// After returning, ParamInfo will hold the parsed parameters.
72900b57cec5SDimitry Andric ///
72910b57cec5SDimitry Andric ///       identifier-list: [C99 6.7.5]
72920b57cec5SDimitry Andric ///         identifier
72930b57cec5SDimitry Andric ///         identifier-list ',' identifier
72940b57cec5SDimitry Andric ///
72950b57cec5SDimitry Andric void Parser::ParseFunctionDeclaratorIdentifierList(
72960b57cec5SDimitry Andric        Declarator &D,
72970b57cec5SDimitry Andric        SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
72985f757f3fSDimitry Andric   // We should never reach this point in C23 or C++.
729981ad6265SDimitry Andric   assert(!getLangOpts().requiresStrictPrototypes() &&
73005f757f3fSDimitry Andric          "Cannot parse an identifier list in C23 or C++");
730181ad6265SDimitry Andric 
73020b57cec5SDimitry Andric   // If there was no identifier specified for the declarator, either we are in
73030b57cec5SDimitry Andric   // an abstract-declarator, or we are in a parameter declarator which was found
73040b57cec5SDimitry Andric   // to be abstract.  In abstract-declarators, identifier lists are not valid:
73050b57cec5SDimitry Andric   // diagnose this.
73060b57cec5SDimitry Andric   if (!D.getIdentifier())
73070b57cec5SDimitry Andric     Diag(Tok, diag::ext_ident_list_in_param);
73080b57cec5SDimitry Andric 
73090b57cec5SDimitry Andric   // Maintain an efficient lookup of params we have seen so far.
73100b57cec5SDimitry Andric   llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
73110b57cec5SDimitry Andric 
73120b57cec5SDimitry Andric   do {
73130b57cec5SDimitry Andric     // If this isn't an identifier, report the error and skip until ')'.
73140b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
73150b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::identifier;
73160b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
73170b57cec5SDimitry Andric       // Forget we parsed anything.
73180b57cec5SDimitry Andric       ParamInfo.clear();
73190b57cec5SDimitry Andric       return;
73200b57cec5SDimitry Andric     }
73210b57cec5SDimitry Andric 
73220b57cec5SDimitry Andric     IdentifierInfo *ParmII = Tok.getIdentifierInfo();
73230b57cec5SDimitry Andric 
73240b57cec5SDimitry Andric     // Reject 'typedef int y; int test(x, y)', but continue parsing.
73250b57cec5SDimitry Andric     if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
73260b57cec5SDimitry Andric       Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
73270b57cec5SDimitry Andric 
73280b57cec5SDimitry Andric     // Verify that the argument identifier has not already been mentioned.
73290b57cec5SDimitry Andric     if (!ParamsSoFar.insert(ParmII).second) {
73300b57cec5SDimitry Andric       Diag(Tok, diag::err_param_redefinition) << ParmII;
73310b57cec5SDimitry Andric     } else {
73320b57cec5SDimitry Andric       // Remember this identifier in ParamInfo.
73330b57cec5SDimitry Andric       ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
73340b57cec5SDimitry Andric                                                      Tok.getLocation(),
73350b57cec5SDimitry Andric                                                      nullptr));
73360b57cec5SDimitry Andric     }
73370b57cec5SDimitry Andric 
73380b57cec5SDimitry Andric     // Eat the identifier.
73390b57cec5SDimitry Andric     ConsumeToken();
73400b57cec5SDimitry Andric     // The list continues if we see a comma.
73410b57cec5SDimitry Andric   } while (TryConsumeToken(tok::comma));
73420b57cec5SDimitry Andric }
73430b57cec5SDimitry Andric 
73440b57cec5SDimitry Andric /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
73450b57cec5SDimitry Andric /// after the opening parenthesis. This function will not parse a K&R-style
73460b57cec5SDimitry Andric /// identifier list.
73470b57cec5SDimitry Andric ///
734855e4f9d5SDimitry Andric /// DeclContext is the context of the declarator being parsed.  If FirstArgAttrs
734955e4f9d5SDimitry Andric /// is non-null, then the caller parsed those attributes immediately after the
735081ad6265SDimitry Andric /// open paren - they will be applied to the DeclSpec of the first parameter.
73510b57cec5SDimitry Andric ///
73520b57cec5SDimitry Andric /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
73530b57cec5SDimitry Andric /// be the location of the ellipsis, if any was parsed.
73540b57cec5SDimitry Andric ///
73550b57cec5SDimitry Andric ///       parameter-type-list: [C99 6.7.5]
73560b57cec5SDimitry Andric ///         parameter-list
73570b57cec5SDimitry Andric ///         parameter-list ',' '...'
73580b57cec5SDimitry Andric /// [C++]   parameter-list '...'
73590b57cec5SDimitry Andric ///
73600b57cec5SDimitry Andric ///       parameter-list: [C99 6.7.5]
73610b57cec5SDimitry Andric ///         parameter-declaration
73620b57cec5SDimitry Andric ///         parameter-list ',' parameter-declaration
73630b57cec5SDimitry Andric ///
73640b57cec5SDimitry Andric ///       parameter-declaration: [C99 6.7.5]
73650b57cec5SDimitry Andric ///         declaration-specifiers declarator
73660b57cec5SDimitry Andric /// [C++]   declaration-specifiers declarator '=' assignment-expression
73670b57cec5SDimitry Andric /// [C++11]                                       initializer-clause
73680b57cec5SDimitry Andric /// [GNU]   declaration-specifiers declarator attributes
73690b57cec5SDimitry Andric ///         declaration-specifiers abstract-declarator[opt]
73700b57cec5SDimitry Andric /// [C++]   declaration-specifiers abstract-declarator[opt]
73710b57cec5SDimitry Andric ///           '=' assignment-expression
73720b57cec5SDimitry Andric /// [GNU]   declaration-specifiers abstract-declarator[opt] attributes
73730b57cec5SDimitry Andric /// [C++11] attribute-specifier-seq parameter-declaration
73745f757f3fSDimitry Andric /// [C++2b] attribute-specifier-seq 'this' parameter-declaration
73750b57cec5SDimitry Andric ///
73760b57cec5SDimitry Andric void Parser::ParseParameterDeclarationClause(
737781ad6265SDimitry Andric     DeclaratorContext DeclaratorCtx, ParsedAttributes &FirstArgAttrs,
73780b57cec5SDimitry Andric     SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
7379bdd1243dSDimitry Andric     SourceLocation &EllipsisLoc, bool IsACXXFunctionDeclaration) {
7380480093f4SDimitry Andric 
7381480093f4SDimitry Andric   // Avoid exceeding the maximum function scope depth.
7382480093f4SDimitry Andric   // See https://bugs.llvm.org/show_bug.cgi?id=19607
7383480093f4SDimitry Andric   // Note Sema::ActOnParamDeclarator calls ParmVarDecl::setScopeInfo with
7384480093f4SDimitry Andric   // getFunctionPrototypeDepth() - 1.
7385480093f4SDimitry Andric   if (getCurScope()->getFunctionPrototypeDepth() - 1 >
7386480093f4SDimitry Andric       ParmVarDecl::getMaxFunctionScopeDepth()) {
7387480093f4SDimitry Andric     Diag(Tok.getLocation(), diag::err_function_scope_depth_exceeded)
7388480093f4SDimitry Andric         << ParmVarDecl::getMaxFunctionScopeDepth();
7389480093f4SDimitry Andric     cutOffParsing();
7390480093f4SDimitry Andric     return;
7391480093f4SDimitry Andric   }
7392480093f4SDimitry Andric 
7393bdd1243dSDimitry Andric   // C++2a [temp.res]p5
7394bdd1243dSDimitry Andric   // A qualified-id is assumed to name a type if
7395bdd1243dSDimitry Andric   //   - [...]
7396bdd1243dSDimitry Andric   //   - it is a decl-specifier of the decl-specifier-seq of a
7397bdd1243dSDimitry Andric   //     - [...]
7398bdd1243dSDimitry Andric   //     - parameter-declaration in a member-declaration [...]
7399bdd1243dSDimitry Andric   //     - parameter-declaration in a declarator of a function or function
7400bdd1243dSDimitry Andric   //       template declaration whose declarator-id is qualified [...]
7401bdd1243dSDimitry Andric   //     - parameter-declaration in a lambda-declarator [...]
7402bdd1243dSDimitry Andric   auto AllowImplicitTypename = ImplicitTypenameContext::No;
7403bdd1243dSDimitry Andric   if (DeclaratorCtx == DeclaratorContext::Member ||
7404bdd1243dSDimitry Andric       DeclaratorCtx == DeclaratorContext::LambdaExpr ||
7405bdd1243dSDimitry Andric       DeclaratorCtx == DeclaratorContext::RequiresExpr ||
7406bdd1243dSDimitry Andric       IsACXXFunctionDeclaration) {
7407bdd1243dSDimitry Andric     AllowImplicitTypename = ImplicitTypenameContext::Yes;
7408bdd1243dSDimitry Andric   }
7409bdd1243dSDimitry Andric 
74100b57cec5SDimitry Andric   do {
74110b57cec5SDimitry Andric     // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
74120b57cec5SDimitry Andric     // before deciding this was a parameter-declaration-clause.
74130b57cec5SDimitry Andric     if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
74140b57cec5SDimitry Andric       break;
74150b57cec5SDimitry Andric 
74160b57cec5SDimitry Andric     // Parse the declaration-specifiers.
74170b57cec5SDimitry Andric     // Just use the ParsingDeclaration "scope" of the declarator.
74180b57cec5SDimitry Andric     DeclSpec DS(AttrFactory);
74190b57cec5SDimitry Andric 
742081ad6265SDimitry Andric     ParsedAttributes ArgDeclAttrs(AttrFactory);
742181ad6265SDimitry Andric     ParsedAttributes ArgDeclSpecAttrs(AttrFactory);
742281ad6265SDimitry Andric 
742381ad6265SDimitry Andric     if (FirstArgAttrs.Range.isValid()) {
742481ad6265SDimitry Andric       // If the caller parsed attributes for the first argument, add them now.
742581ad6265SDimitry Andric       // Take them so that we only apply the attributes to the first parameter.
742681ad6265SDimitry Andric       // We have already started parsing the decl-specifier sequence, so don't
742781ad6265SDimitry Andric       // parse any parameter-declaration pieces that precede it.
742881ad6265SDimitry Andric       ArgDeclSpecAttrs.takeAllFrom(FirstArgAttrs);
742981ad6265SDimitry Andric     } else {
74300b57cec5SDimitry Andric       // Parse any C++11 attributes.
743181ad6265SDimitry Andric       MaybeParseCXX11Attributes(ArgDeclAttrs);
74320b57cec5SDimitry Andric 
74330b57cec5SDimitry Andric       // Skip any Microsoft attributes before a param.
743481ad6265SDimitry Andric       MaybeParseMicrosoftAttributes(ArgDeclSpecAttrs);
743581ad6265SDimitry Andric     }
74360b57cec5SDimitry Andric 
74370b57cec5SDimitry Andric     SourceLocation DSStart = Tok.getLocation();
74380b57cec5SDimitry Andric 
74395f757f3fSDimitry Andric     // Parse a C++23 Explicit Object Parameter
74405f757f3fSDimitry Andric     // We do that in all language modes to produce a better diagnostic.
74415f757f3fSDimitry Andric     SourceLocation ThisLoc;
74425f757f3fSDimitry Andric     if (getLangOpts().CPlusPlus && Tok.is(tok::kw_this))
74435f757f3fSDimitry Andric       ThisLoc = ConsumeToken();
74445f757f3fSDimitry Andric 
7445bdd1243dSDimitry Andric     ParseDeclarationSpecifiers(DS, /*TemplateInfo=*/ParsedTemplateInfo(),
7446bdd1243dSDimitry Andric                                AS_none, DeclSpecContext::DSC_normal,
7447bdd1243dSDimitry Andric                                /*LateAttrs=*/nullptr, AllowImplicitTypename);
74485f757f3fSDimitry Andric 
744981ad6265SDimitry Andric     DS.takeAttributesFrom(ArgDeclSpecAttrs);
74500b57cec5SDimitry Andric 
74510b57cec5SDimitry Andric     // Parse the declarator.  This is "PrototypeContext" or
74520b57cec5SDimitry Andric     // "LambdaExprParameterContext", because we must accept either
74530b57cec5SDimitry Andric     // 'declarator' or 'abstract-declarator' here.
745481ad6265SDimitry Andric     Declarator ParmDeclarator(DS, ArgDeclAttrs,
745581ad6265SDimitry Andric                               DeclaratorCtx == DeclaratorContext::RequiresExpr
7456e8d8bef9SDimitry Andric                                   ? DeclaratorContext::RequiresExpr
7457e8d8bef9SDimitry Andric                               : DeclaratorCtx == DeclaratorContext::LambdaExpr
7458e8d8bef9SDimitry Andric                                   ? DeclaratorContext::LambdaExprParameter
7459e8d8bef9SDimitry Andric                                   : DeclaratorContext::Prototype);
74600b57cec5SDimitry Andric     ParseDeclarator(ParmDeclarator);
74610b57cec5SDimitry Andric 
74625f757f3fSDimitry Andric     if (ThisLoc.isValid())
74635f757f3fSDimitry Andric       ParmDeclarator.SetRangeBegin(ThisLoc);
74645f757f3fSDimitry Andric 
74650b57cec5SDimitry Andric     // Parse GNU attributes, if present.
74660b57cec5SDimitry Andric     MaybeParseGNUAttributes(ParmDeclarator);
7467bdd1243dSDimitry Andric     if (getLangOpts().HLSL)
746881ad6265SDimitry Andric       MaybeParseHLSLSemantics(DS.getAttributes());
74690b57cec5SDimitry Andric 
7470480093f4SDimitry Andric     if (Tok.is(tok::kw_requires)) {
7471480093f4SDimitry Andric       // User tried to define a requires clause in a parameter declaration,
7472480093f4SDimitry Andric       // which is surely not a function declaration.
7473480093f4SDimitry Andric       // void f(int (*g)(int, int) requires true);
7474480093f4SDimitry Andric       Diag(Tok,
7475480093f4SDimitry Andric            diag::err_requires_clause_on_declarator_not_declaring_a_function);
7476480093f4SDimitry Andric       ConsumeToken();
7477480093f4SDimitry Andric       Actions.CorrectDelayedTyposInExpr(
7478480093f4SDimitry Andric          ParseConstraintLogicalOrExpression(/*IsTrailingRequiresClause=*/true));
7479480093f4SDimitry Andric     }
7480480093f4SDimitry Andric 
74810b57cec5SDimitry Andric     // Remember this parsed parameter in ParamInfo.
74820b57cec5SDimitry Andric     IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
74830b57cec5SDimitry Andric 
74840b57cec5SDimitry Andric     // DefArgToks is used when the parsing of default arguments needs
74850b57cec5SDimitry Andric     // to be delayed.
74860b57cec5SDimitry Andric     std::unique_ptr<CachedTokens> DefArgToks;
74870b57cec5SDimitry Andric 
74880b57cec5SDimitry Andric     // If no parameter was specified, verify that *something* was specified,
74890b57cec5SDimitry Andric     // otherwise we have a missing type and identifier.
74900b57cec5SDimitry Andric     if (DS.isEmpty() && ParmDeclarator.getIdentifier() == nullptr &&
74910b57cec5SDimitry Andric         ParmDeclarator.getNumTypeObjects() == 0) {
74920b57cec5SDimitry Andric       // Completely missing, emit error.
74930b57cec5SDimitry Andric       Diag(DSStart, diag::err_missing_param);
74940b57cec5SDimitry Andric     } else {
74950b57cec5SDimitry Andric       // Otherwise, we have something.  Add it and let semantic analysis try
74960b57cec5SDimitry Andric       // to grok it and add the result to the ParamInfo we are building.
74970b57cec5SDimitry Andric 
74980b57cec5SDimitry Andric       // Last chance to recover from a misplaced ellipsis in an attempted
74990b57cec5SDimitry Andric       // parameter pack declaration.
75000b57cec5SDimitry Andric       if (Tok.is(tok::ellipsis) &&
75010b57cec5SDimitry Andric           (NextToken().isNot(tok::r_paren) ||
75020b57cec5SDimitry Andric            (!ParmDeclarator.getEllipsisLoc().isValid() &&
75030b57cec5SDimitry Andric             !Actions.isUnexpandedParameterPackPermitted())) &&
75040b57cec5SDimitry Andric           Actions.containsUnexpandedParameterPacks(ParmDeclarator))
75050b57cec5SDimitry Andric         DiagnoseMisplacedEllipsisInDeclarator(ConsumeToken(), ParmDeclarator);
75060b57cec5SDimitry Andric 
75075ffd83dbSDimitry Andric       // Now we are at the point where declarator parsing is finished.
75085ffd83dbSDimitry Andric       //
75095ffd83dbSDimitry Andric       // Try to catch keywords in place of the identifier in a declarator, and
75105ffd83dbSDimitry Andric       // in particular the common case where:
75115ffd83dbSDimitry Andric       //   1 identifier comes at the end of the declarator
75125ffd83dbSDimitry Andric       //   2 if the identifier is dropped, the declarator is valid but anonymous
75135ffd83dbSDimitry Andric       //     (no identifier)
75145ffd83dbSDimitry Andric       //   3 declarator parsing succeeds, and then we have a trailing keyword,
75155ffd83dbSDimitry Andric       //     which is never valid in a param list (e.g. missing a ',')
75165ffd83dbSDimitry Andric       // And we can't handle this in ParseDeclarator because in general keywords
75175ffd83dbSDimitry Andric       // may be allowed to follow the declarator. (And in some cases there'd be
75185ffd83dbSDimitry Andric       // better recovery like inserting punctuation). ParseDeclarator is just
75195ffd83dbSDimitry Andric       // treating this as an anonymous parameter, and fortunately at this point
75205ffd83dbSDimitry Andric       // we've already almost done that.
75215ffd83dbSDimitry Andric       //
75225ffd83dbSDimitry Andric       // We care about case 1) where the declarator type should be known, and
75235ffd83dbSDimitry Andric       // the identifier should be null.
75244824e7fdSDimitry Andric       if (!ParmDeclarator.isInvalidType() && !ParmDeclarator.hasName() &&
75254824e7fdSDimitry Andric           Tok.isNot(tok::raw_identifier) && !Tok.isAnnotation() &&
75264824e7fdSDimitry Andric           Tok.getIdentifierInfo() &&
75275ffd83dbSDimitry Andric           Tok.getIdentifierInfo()->isKeyword(getLangOpts())) {
75285ffd83dbSDimitry Andric         Diag(Tok, diag::err_keyword_as_parameter) << PP.getSpelling(Tok);
75295ffd83dbSDimitry Andric         // Consume the keyword.
75305ffd83dbSDimitry Andric         ConsumeToken();
75315ffd83dbSDimitry Andric       }
75320b57cec5SDimitry Andric       // Inform the actions module about the parameter declarator, so it gets
75330b57cec5SDimitry Andric       // added to the current scope.
75345f757f3fSDimitry Andric       Decl *Param =
75355f757f3fSDimitry Andric           Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator, ThisLoc);
75360b57cec5SDimitry Andric       // Parse the default argument, if any. We parse the default
75370b57cec5SDimitry Andric       // arguments in all dialects; the semantic analysis in
75380b57cec5SDimitry Andric       // ActOnParamDefaultArgument will reject the default argument in
75390b57cec5SDimitry Andric       // C.
75400b57cec5SDimitry Andric       if (Tok.is(tok::equal)) {
75410b57cec5SDimitry Andric         SourceLocation EqualLoc = Tok.getLocation();
75420b57cec5SDimitry Andric 
75430b57cec5SDimitry Andric         // Parse the default argument
7544e8d8bef9SDimitry Andric         if (DeclaratorCtx == DeclaratorContext::Member) {
75450b57cec5SDimitry Andric           // If we're inside a class definition, cache the tokens
75460b57cec5SDimitry Andric           // corresponding to the default argument. We'll actually parse
75470b57cec5SDimitry Andric           // them when we see the end of the class definition.
75480b57cec5SDimitry Andric           DefArgToks.reset(new CachedTokens);
75490b57cec5SDimitry Andric 
75500b57cec5SDimitry Andric           SourceLocation ArgStartLoc = NextToken().getLocation();
755106c3fb27SDimitry Andric           ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument);
75520b57cec5SDimitry Andric           Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
75530b57cec5SDimitry Andric                                                     ArgStartLoc);
75540b57cec5SDimitry Andric         } else {
75550b57cec5SDimitry Andric           // Consume the '='.
75560b57cec5SDimitry Andric           ConsumeToken();
75570b57cec5SDimitry Andric 
75580b57cec5SDimitry Andric           // The argument isn't actually potentially evaluated unless it is
75590b57cec5SDimitry Andric           // used.
75600b57cec5SDimitry Andric           EnterExpressionEvaluationContext Eval(
75610b57cec5SDimitry Andric               Actions,
75620b57cec5SDimitry Andric               Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed,
75630b57cec5SDimitry Andric               Param);
75640b57cec5SDimitry Andric 
75650b57cec5SDimitry Andric           ExprResult DefArgResult;
75660b57cec5SDimitry Andric           if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
75670b57cec5SDimitry Andric             Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
75680b57cec5SDimitry Andric             DefArgResult = ParseBraceInitializer();
756981ad6265SDimitry Andric           } else {
757081ad6265SDimitry Andric             if (Tok.is(tok::l_paren) && NextToken().is(tok::l_brace)) {
757181ad6265SDimitry Andric               Diag(Tok, diag::err_stmt_expr_in_default_arg) << 0;
75725f757f3fSDimitry Andric               Actions.ActOnParamDefaultArgumentError(Param, EqualLoc,
75735f757f3fSDimitry Andric                                                      /*DefaultArg=*/nullptr);
757481ad6265SDimitry Andric               // Skip the statement expression and continue parsing
757581ad6265SDimitry Andric               SkipUntil(tok::comma, StopBeforeMatch);
757681ad6265SDimitry Andric               continue;
757781ad6265SDimitry Andric             }
75780b57cec5SDimitry Andric             DefArgResult = ParseAssignmentExpression();
757981ad6265SDimitry Andric           }
75800b57cec5SDimitry Andric           DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
75810b57cec5SDimitry Andric           if (DefArgResult.isInvalid()) {
75825f757f3fSDimitry Andric             Actions.ActOnParamDefaultArgumentError(Param, EqualLoc,
75835f757f3fSDimitry Andric                                                    /*DefaultArg=*/nullptr);
75840b57cec5SDimitry Andric             SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
75850b57cec5SDimitry Andric           } else {
75860b57cec5SDimitry Andric             // Inform the actions module about the default argument
75870b57cec5SDimitry Andric             Actions.ActOnParamDefaultArgument(Param, EqualLoc,
75880b57cec5SDimitry Andric                                               DefArgResult.get());
75890b57cec5SDimitry Andric           }
75900b57cec5SDimitry Andric         }
75910b57cec5SDimitry Andric       }
75920b57cec5SDimitry Andric 
75930b57cec5SDimitry Andric       ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
75940b57cec5SDimitry Andric                                           ParmDeclarator.getIdentifierLoc(),
75950b57cec5SDimitry Andric                                           Param, std::move(DefArgToks)));
75960b57cec5SDimitry Andric     }
75970b57cec5SDimitry Andric 
75980b57cec5SDimitry Andric     if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) {
75990b57cec5SDimitry Andric       if (!getLangOpts().CPlusPlus) {
76000b57cec5SDimitry Andric         // We have ellipsis without a preceding ',', which is ill-formed
76010b57cec5SDimitry Andric         // in C. Complain and provide the fix.
76020b57cec5SDimitry Andric         Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
76030b57cec5SDimitry Andric             << FixItHint::CreateInsertion(EllipsisLoc, ", ");
76040b57cec5SDimitry Andric       } else if (ParmDeclarator.getEllipsisLoc().isValid() ||
76050b57cec5SDimitry Andric                  Actions.containsUnexpandedParameterPacks(ParmDeclarator)) {
76060b57cec5SDimitry Andric         // It looks like this was supposed to be a parameter pack. Warn and
76070b57cec5SDimitry Andric         // point out where the ellipsis should have gone.
76080b57cec5SDimitry Andric         SourceLocation ParmEllipsis = ParmDeclarator.getEllipsisLoc();
76090b57cec5SDimitry Andric         Diag(EllipsisLoc, diag::warn_misplaced_ellipsis_vararg)
76100b57cec5SDimitry Andric           << ParmEllipsis.isValid() << ParmEllipsis;
76110b57cec5SDimitry Andric         if (ParmEllipsis.isValid()) {
76120b57cec5SDimitry Andric           Diag(ParmEllipsis,
76130b57cec5SDimitry Andric                diag::note_misplaced_ellipsis_vararg_existing_ellipsis);
76140b57cec5SDimitry Andric         } else {
76150b57cec5SDimitry Andric           Diag(ParmDeclarator.getIdentifierLoc(),
76160b57cec5SDimitry Andric                diag::note_misplaced_ellipsis_vararg_add_ellipsis)
76170b57cec5SDimitry Andric             << FixItHint::CreateInsertion(ParmDeclarator.getIdentifierLoc(),
76180b57cec5SDimitry Andric                                           "...")
76190b57cec5SDimitry Andric             << !ParmDeclarator.hasName();
76200b57cec5SDimitry Andric         }
76210b57cec5SDimitry Andric         Diag(EllipsisLoc, diag::note_misplaced_ellipsis_vararg_add_comma)
76220b57cec5SDimitry Andric           << FixItHint::CreateInsertion(EllipsisLoc, ", ");
76230b57cec5SDimitry Andric       }
76240b57cec5SDimitry Andric 
76250b57cec5SDimitry Andric       // We can't have any more parameters after an ellipsis.
76260b57cec5SDimitry Andric       break;
76270b57cec5SDimitry Andric     }
76280b57cec5SDimitry Andric 
76290b57cec5SDimitry Andric     // If the next token is a comma, consume it and keep reading arguments.
76300b57cec5SDimitry Andric   } while (TryConsumeToken(tok::comma));
76310b57cec5SDimitry Andric }
76320b57cec5SDimitry Andric 
76330b57cec5SDimitry Andric /// [C90]   direct-declarator '[' constant-expression[opt] ']'
76340b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
76350b57cec5SDimitry Andric /// [C99]   direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
76360b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list 'static' assignment-expr ']'
76370b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list[opt] '*' ']'
76380b57cec5SDimitry Andric /// [C++11] direct-declarator '[' constant-expression[opt] ']'
76390b57cec5SDimitry Andric ///                           attribute-specifier-seq[opt]
76400b57cec5SDimitry Andric void Parser::ParseBracketDeclarator(Declarator &D) {
76410b57cec5SDimitry Andric   if (CheckProhibitedCXX11Attribute())
76420b57cec5SDimitry Andric     return;
76430b57cec5SDimitry Andric 
76440b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_square);
76450b57cec5SDimitry Andric   T.consumeOpen();
76460b57cec5SDimitry Andric 
76470b57cec5SDimitry Andric   // C array syntax has many features, but by-far the most common is [] and [4].
76480b57cec5SDimitry Andric   // This code does a fast path to handle some of the most obvious cases.
76490b57cec5SDimitry Andric   if (Tok.getKind() == tok::r_square) {
76500b57cec5SDimitry Andric     T.consumeClose();
76510b57cec5SDimitry Andric     ParsedAttributes attrs(AttrFactory);
76520b57cec5SDimitry Andric     MaybeParseCXX11Attributes(attrs);
76530b57cec5SDimitry Andric 
76540b57cec5SDimitry Andric     // Remember that we parsed the empty array type.
76550b57cec5SDimitry Andric     D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, nullptr,
76560b57cec5SDimitry Andric                                             T.getOpenLocation(),
76570b57cec5SDimitry Andric                                             T.getCloseLocation()),
76580b57cec5SDimitry Andric                   std::move(attrs), T.getCloseLocation());
76590b57cec5SDimitry Andric     return;
76600b57cec5SDimitry Andric   } else if (Tok.getKind() == tok::numeric_constant &&
76610b57cec5SDimitry Andric              GetLookAheadToken(1).is(tok::r_square)) {
76620b57cec5SDimitry Andric     // [4] is very common.  Parse the numeric constant expression.
76630b57cec5SDimitry Andric     ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
76640b57cec5SDimitry Andric     ConsumeToken();
76650b57cec5SDimitry Andric 
76660b57cec5SDimitry Andric     T.consumeClose();
76670b57cec5SDimitry Andric     ParsedAttributes attrs(AttrFactory);
76680b57cec5SDimitry Andric     MaybeParseCXX11Attributes(attrs);
76690b57cec5SDimitry Andric 
76700b57cec5SDimitry Andric     // Remember that we parsed a array type, and remember its features.
76710b57cec5SDimitry Andric     D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, ExprRes.get(),
76720b57cec5SDimitry Andric                                             T.getOpenLocation(),
76730b57cec5SDimitry Andric                                             T.getCloseLocation()),
76740b57cec5SDimitry Andric                   std::move(attrs), T.getCloseLocation());
76750b57cec5SDimitry Andric     return;
76760b57cec5SDimitry Andric   } else if (Tok.getKind() == tok::code_completion) {
7677fe6060f1SDimitry Andric     cutOffParsing();
76780b57cec5SDimitry Andric     Actions.CodeCompleteBracketDeclarator(getCurScope());
7679fe6060f1SDimitry Andric     return;
76800b57cec5SDimitry Andric   }
76810b57cec5SDimitry Andric 
76820b57cec5SDimitry Andric   // If valid, this location is the position where we read the 'static' keyword.
76830b57cec5SDimitry Andric   SourceLocation StaticLoc;
76840b57cec5SDimitry Andric   TryConsumeToken(tok::kw_static, StaticLoc);
76850b57cec5SDimitry Andric 
76860b57cec5SDimitry Andric   // If there is a type-qualifier-list, read it now.
76870b57cec5SDimitry Andric   // Type qualifiers in an array subscript are a C99 feature.
76880b57cec5SDimitry Andric   DeclSpec DS(AttrFactory);
76890b57cec5SDimitry Andric   ParseTypeQualifierListOpt(DS, AR_CXX11AttributesParsed);
76900b57cec5SDimitry Andric 
76910b57cec5SDimitry Andric   // If we haven't already read 'static', check to see if there is one after the
76920b57cec5SDimitry Andric   // type-qualifier-list.
76930b57cec5SDimitry Andric   if (!StaticLoc.isValid())
76940b57cec5SDimitry Andric     TryConsumeToken(tok::kw_static, StaticLoc);
76950b57cec5SDimitry Andric 
76960b57cec5SDimitry Andric   // Handle "direct-declarator [ type-qual-list[opt] * ]".
76970b57cec5SDimitry Andric   bool isStar = false;
76980b57cec5SDimitry Andric   ExprResult NumElements;
76990b57cec5SDimitry Andric 
77000b57cec5SDimitry Andric   // Handle the case where we have '[*]' as the array size.  However, a leading
77010b57cec5SDimitry Andric   // star could be the start of an expression, for example 'X[*p + 4]'.  Verify
77020b57cec5SDimitry Andric   // the token after the star is a ']'.  Since stars in arrays are
77030b57cec5SDimitry Andric   // infrequent, use of lookahead is not costly here.
77040b57cec5SDimitry Andric   if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
77050b57cec5SDimitry Andric     ConsumeToken();  // Eat the '*'.
77060b57cec5SDimitry Andric 
77070b57cec5SDimitry Andric     if (StaticLoc.isValid()) {
77080b57cec5SDimitry Andric       Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
77090b57cec5SDimitry Andric       StaticLoc = SourceLocation();  // Drop the static.
77100b57cec5SDimitry Andric     }
77110b57cec5SDimitry Andric     isStar = true;
77120b57cec5SDimitry Andric   } else if (Tok.isNot(tok::r_square)) {
77130b57cec5SDimitry Andric     // Note, in C89, this production uses the constant-expr production instead
77140b57cec5SDimitry Andric     // of assignment-expr.  The only difference is that assignment-expr allows
77150b57cec5SDimitry Andric     // things like '=' and '*='.  Sema rejects these in C89 mode because they
77160b57cec5SDimitry Andric     // are not i-c-e's, so we don't need to distinguish between the two here.
77170b57cec5SDimitry Andric 
77180b57cec5SDimitry Andric     // Parse the constant-expression or assignment-expression now (depending
77190b57cec5SDimitry Andric     // on dialect).
77200b57cec5SDimitry Andric     if (getLangOpts().CPlusPlus) {
77215f757f3fSDimitry Andric       NumElements = ParseArrayBoundExpression();
77220b57cec5SDimitry Andric     } else {
77230b57cec5SDimitry Andric       EnterExpressionEvaluationContext Unevaluated(
77240b57cec5SDimitry Andric           Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
77250b57cec5SDimitry Andric       NumElements =
77260b57cec5SDimitry Andric           Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
77270b57cec5SDimitry Andric     }
77280b57cec5SDimitry Andric   } else {
77290b57cec5SDimitry Andric     if (StaticLoc.isValid()) {
77300b57cec5SDimitry Andric       Diag(StaticLoc, diag::err_unspecified_size_with_static);
77310b57cec5SDimitry Andric       StaticLoc = SourceLocation();  // Drop the static.
77320b57cec5SDimitry Andric     }
77330b57cec5SDimitry Andric   }
77340b57cec5SDimitry Andric 
77350b57cec5SDimitry Andric   // If there was an error parsing the assignment-expression, recover.
77360b57cec5SDimitry Andric   if (NumElements.isInvalid()) {
77370b57cec5SDimitry Andric     D.setInvalidType(true);
77380b57cec5SDimitry Andric     // If the expression was invalid, skip it.
77390b57cec5SDimitry Andric     SkipUntil(tok::r_square, StopAtSemi);
77400b57cec5SDimitry Andric     return;
77410b57cec5SDimitry Andric   }
77420b57cec5SDimitry Andric 
77430b57cec5SDimitry Andric   T.consumeClose();
77440b57cec5SDimitry Andric 
77450b57cec5SDimitry Andric   MaybeParseCXX11Attributes(DS.getAttributes());
77460b57cec5SDimitry Andric 
77470b57cec5SDimitry Andric   // Remember that we parsed a array type, and remember its features.
77480b57cec5SDimitry Andric   D.AddTypeInfo(
77490b57cec5SDimitry Andric       DeclaratorChunk::getArray(DS.getTypeQualifiers(), StaticLoc.isValid(),
77500b57cec5SDimitry Andric                                 isStar, NumElements.get(), T.getOpenLocation(),
77510b57cec5SDimitry Andric                                 T.getCloseLocation()),
77520b57cec5SDimitry Andric       std::move(DS.getAttributes()), T.getCloseLocation());
77530b57cec5SDimitry Andric }
77540b57cec5SDimitry Andric 
77550b57cec5SDimitry Andric /// Diagnose brackets before an identifier.
77560b57cec5SDimitry Andric void Parser::ParseMisplacedBracketDeclarator(Declarator &D) {
77570b57cec5SDimitry Andric   assert(Tok.is(tok::l_square) && "Missing opening bracket");
77580b57cec5SDimitry Andric   assert(!D.mayOmitIdentifier() && "Declarator cannot omit identifier");
77590b57cec5SDimitry Andric 
77600b57cec5SDimitry Andric   SourceLocation StartBracketLoc = Tok.getLocation();
776181ad6265SDimitry Andric   Declarator TempDeclarator(D.getDeclSpec(), ParsedAttributesView::none(),
776281ad6265SDimitry Andric                             D.getContext());
77630b57cec5SDimitry Andric 
77640b57cec5SDimitry Andric   while (Tok.is(tok::l_square)) {
77650b57cec5SDimitry Andric     ParseBracketDeclarator(TempDeclarator);
77660b57cec5SDimitry Andric   }
77670b57cec5SDimitry Andric 
77680b57cec5SDimitry Andric   // Stuff the location of the start of the brackets into the Declarator.
77690b57cec5SDimitry Andric   // The diagnostics from ParseDirectDeclarator will make more sense if
77700b57cec5SDimitry Andric   // they use this location instead.
77710b57cec5SDimitry Andric   if (Tok.is(tok::semi))
77720b57cec5SDimitry Andric     D.getName().EndLocation = StartBracketLoc;
77730b57cec5SDimitry Andric 
77740b57cec5SDimitry Andric   SourceLocation SuggestParenLoc = Tok.getLocation();
77750b57cec5SDimitry Andric 
77760b57cec5SDimitry Andric   // Now that the brackets are removed, try parsing the declarator again.
77770b57cec5SDimitry Andric   ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
77780b57cec5SDimitry Andric 
77790b57cec5SDimitry Andric   // Something went wrong parsing the brackets, in which case,
77800b57cec5SDimitry Andric   // ParseBracketDeclarator has emitted an error, and we don't need to emit
77810b57cec5SDimitry Andric   // one here.
77820b57cec5SDimitry Andric   if (TempDeclarator.getNumTypeObjects() == 0)
77830b57cec5SDimitry Andric     return;
77840b57cec5SDimitry Andric 
77850b57cec5SDimitry Andric   // Determine if parens will need to be suggested in the diagnostic.
77860b57cec5SDimitry Andric   bool NeedParens = false;
77870b57cec5SDimitry Andric   if (D.getNumTypeObjects() != 0) {
77880b57cec5SDimitry Andric     switch (D.getTypeObject(D.getNumTypeObjects() - 1).Kind) {
77890b57cec5SDimitry Andric     case DeclaratorChunk::Pointer:
77900b57cec5SDimitry Andric     case DeclaratorChunk::Reference:
77910b57cec5SDimitry Andric     case DeclaratorChunk::BlockPointer:
77920b57cec5SDimitry Andric     case DeclaratorChunk::MemberPointer:
77930b57cec5SDimitry Andric     case DeclaratorChunk::Pipe:
77940b57cec5SDimitry Andric       NeedParens = true;
77950b57cec5SDimitry Andric       break;
77960b57cec5SDimitry Andric     case DeclaratorChunk::Array:
77970b57cec5SDimitry Andric     case DeclaratorChunk::Function:
77980b57cec5SDimitry Andric     case DeclaratorChunk::Paren:
77990b57cec5SDimitry Andric       break;
78000b57cec5SDimitry Andric     }
78010b57cec5SDimitry Andric   }
78020b57cec5SDimitry Andric 
78030b57cec5SDimitry Andric   if (NeedParens) {
78040b57cec5SDimitry Andric     // Create a DeclaratorChunk for the inserted parens.
78050b57cec5SDimitry Andric     SourceLocation EndLoc = PP.getLocForEndOfToken(D.getEndLoc());
78060b57cec5SDimitry Andric     D.AddTypeInfo(DeclaratorChunk::getParen(SuggestParenLoc, EndLoc),
78070b57cec5SDimitry Andric                   SourceLocation());
78080b57cec5SDimitry Andric   }
78090b57cec5SDimitry Andric 
78100b57cec5SDimitry Andric   // Adding back the bracket info to the end of the Declarator.
78110b57cec5SDimitry Andric   for (unsigned i = 0, e = TempDeclarator.getNumTypeObjects(); i < e; ++i) {
78120b57cec5SDimitry Andric     const DeclaratorChunk &Chunk = TempDeclarator.getTypeObject(i);
78130b57cec5SDimitry Andric     D.AddTypeInfo(Chunk, SourceLocation());
78140b57cec5SDimitry Andric   }
78150b57cec5SDimitry Andric 
78160b57cec5SDimitry Andric   // The missing identifier would have been diagnosed in ParseDirectDeclarator.
78170b57cec5SDimitry Andric   // If parentheses are required, always suggest them.
78180b57cec5SDimitry Andric   if (!D.getIdentifier() && !NeedParens)
78190b57cec5SDimitry Andric     return;
78200b57cec5SDimitry Andric 
78210b57cec5SDimitry Andric   SourceLocation EndBracketLoc = TempDeclarator.getEndLoc();
78220b57cec5SDimitry Andric 
78230b57cec5SDimitry Andric   // Generate the move bracket error message.
78240b57cec5SDimitry Andric   SourceRange BracketRange(StartBracketLoc, EndBracketLoc);
78250b57cec5SDimitry Andric   SourceLocation EndLoc = PP.getLocForEndOfToken(D.getEndLoc());
78260b57cec5SDimitry Andric 
78270b57cec5SDimitry Andric   if (NeedParens) {
78280b57cec5SDimitry Andric     Diag(EndLoc, diag::err_brackets_go_after_unqualified_id)
78290b57cec5SDimitry Andric         << getLangOpts().CPlusPlus
78300b57cec5SDimitry Andric         << FixItHint::CreateInsertion(SuggestParenLoc, "(")
78310b57cec5SDimitry Andric         << FixItHint::CreateInsertion(EndLoc, ")")
78320b57cec5SDimitry Andric         << FixItHint::CreateInsertionFromRange(
78330b57cec5SDimitry Andric                EndLoc, CharSourceRange(BracketRange, true))
78340b57cec5SDimitry Andric         << FixItHint::CreateRemoval(BracketRange);
78350b57cec5SDimitry Andric   } else {
78360b57cec5SDimitry Andric     Diag(EndLoc, diag::err_brackets_go_after_unqualified_id)
78370b57cec5SDimitry Andric         << getLangOpts().CPlusPlus
78380b57cec5SDimitry Andric         << FixItHint::CreateInsertionFromRange(
78390b57cec5SDimitry Andric                EndLoc, CharSourceRange(BracketRange, true))
78400b57cec5SDimitry Andric         << FixItHint::CreateRemoval(BracketRange);
78410b57cec5SDimitry Andric   }
78420b57cec5SDimitry Andric }
78430b57cec5SDimitry Andric 
78440b57cec5SDimitry Andric /// [GNU]   typeof-specifier:
78450b57cec5SDimitry Andric ///           typeof ( expressions )
78460b57cec5SDimitry Andric ///           typeof ( type-name )
78470b57cec5SDimitry Andric /// [GNU/C++] typeof unary-expression
78485f757f3fSDimitry Andric /// [C23]   typeof-specifier:
7849bdd1243dSDimitry Andric ///           typeof '(' typeof-specifier-argument ')'
7850bdd1243dSDimitry Andric ///           typeof_unqual '(' typeof-specifier-argument ')'
7851bdd1243dSDimitry Andric ///
7852bdd1243dSDimitry Andric ///         typeof-specifier-argument:
7853bdd1243dSDimitry Andric ///           expression
7854bdd1243dSDimitry Andric ///           type-name
78550b57cec5SDimitry Andric ///
78560b57cec5SDimitry Andric void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
7857bdd1243dSDimitry Andric   assert(Tok.isOneOf(tok::kw_typeof, tok::kw_typeof_unqual) &&
7858bdd1243dSDimitry Andric          "Not a typeof specifier");
7859bdd1243dSDimitry Andric 
7860bdd1243dSDimitry Andric   bool IsUnqual = Tok.is(tok::kw_typeof_unqual);
7861bdd1243dSDimitry Andric   const IdentifierInfo *II = Tok.getIdentifierInfo();
78625f757f3fSDimitry Andric   if (getLangOpts().C23 && !II->getName().starts_with("__"))
78635f757f3fSDimitry Andric     Diag(Tok.getLocation(), diag::warn_c23_compat_keyword) << Tok.getName();
7864bdd1243dSDimitry Andric 
78650b57cec5SDimitry Andric   Token OpTok = Tok;
78660b57cec5SDimitry Andric   SourceLocation StartLoc = ConsumeToken();
7867bdd1243dSDimitry Andric   bool HasParens = Tok.is(tok::l_paren);
78680b57cec5SDimitry Andric 
78690b57cec5SDimitry Andric   EnterExpressionEvaluationContext Unevaluated(
78700b57cec5SDimitry Andric       Actions, Sema::ExpressionEvaluationContext::Unevaluated,
78710b57cec5SDimitry Andric       Sema::ReuseLambdaContextDecl);
78720b57cec5SDimitry Andric 
78730b57cec5SDimitry Andric   bool isCastExpr;
78740b57cec5SDimitry Andric   ParsedType CastTy;
78750b57cec5SDimitry Andric   SourceRange CastRange;
78760b57cec5SDimitry Andric   ExprResult Operand = Actions.CorrectDelayedTyposInExpr(
78770b57cec5SDimitry Andric       ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, CastTy, CastRange));
7878bdd1243dSDimitry Andric   if (HasParens)
7879bdd1243dSDimitry Andric     DS.setTypeArgumentRange(CastRange);
78800b57cec5SDimitry Andric 
78810b57cec5SDimitry Andric   if (CastRange.getEnd().isInvalid())
78820b57cec5SDimitry Andric     // FIXME: Not accurate, the range gets one token more than it should.
78830b57cec5SDimitry Andric     DS.SetRangeEnd(Tok.getLocation());
78840b57cec5SDimitry Andric   else
78850b57cec5SDimitry Andric     DS.SetRangeEnd(CastRange.getEnd());
78860b57cec5SDimitry Andric 
78870b57cec5SDimitry Andric   if (isCastExpr) {
78880b57cec5SDimitry Andric     if (!CastTy) {
78890b57cec5SDimitry Andric       DS.SetTypeSpecError();
78900b57cec5SDimitry Andric       return;
78910b57cec5SDimitry Andric     }
78920b57cec5SDimitry Andric 
78930b57cec5SDimitry Andric     const char *PrevSpec = nullptr;
78940b57cec5SDimitry Andric     unsigned DiagID;
78950b57cec5SDimitry Andric     // Check for duplicate type specifiers (e.g. "int typeof(int)").
7896bdd1243dSDimitry Andric     if (DS.SetTypeSpecType(IsUnqual ? DeclSpec::TST_typeof_unqualType
7897bdd1243dSDimitry Andric                                     : DeclSpec::TST_typeofType,
7898bdd1243dSDimitry Andric                            StartLoc, PrevSpec,
78990b57cec5SDimitry Andric                            DiagID, CastTy,
79000b57cec5SDimitry Andric                            Actions.getASTContext().getPrintingPolicy()))
79010b57cec5SDimitry Andric       Diag(StartLoc, DiagID) << PrevSpec;
79020b57cec5SDimitry Andric     return;
79030b57cec5SDimitry Andric   }
79040b57cec5SDimitry Andric 
79050b57cec5SDimitry Andric   // If we get here, the operand to the typeof was an expression.
79060b57cec5SDimitry Andric   if (Operand.isInvalid()) {
79070b57cec5SDimitry Andric     DS.SetTypeSpecError();
79080b57cec5SDimitry Andric     return;
79090b57cec5SDimitry Andric   }
79100b57cec5SDimitry Andric 
79110b57cec5SDimitry Andric   // We might need to transform the operand if it is potentially evaluated.
79120b57cec5SDimitry Andric   Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
79130b57cec5SDimitry Andric   if (Operand.isInvalid()) {
79140b57cec5SDimitry Andric     DS.SetTypeSpecError();
79150b57cec5SDimitry Andric     return;
79160b57cec5SDimitry Andric   }
79170b57cec5SDimitry Andric 
79180b57cec5SDimitry Andric   const char *PrevSpec = nullptr;
79190b57cec5SDimitry Andric   unsigned DiagID;
79200b57cec5SDimitry Andric   // Check for duplicate type specifiers (e.g. "int typeof(int)").
7921bdd1243dSDimitry Andric   if (DS.SetTypeSpecType(IsUnqual ? DeclSpec::TST_typeof_unqualExpr
7922bdd1243dSDimitry Andric                                   : DeclSpec::TST_typeofExpr,
7923bdd1243dSDimitry Andric                          StartLoc, PrevSpec,
79240b57cec5SDimitry Andric                          DiagID, Operand.get(),
79250b57cec5SDimitry Andric                          Actions.getASTContext().getPrintingPolicy()))
79260b57cec5SDimitry Andric     Diag(StartLoc, DiagID) << PrevSpec;
79270b57cec5SDimitry Andric }
79280b57cec5SDimitry Andric 
79290b57cec5SDimitry Andric /// [C11]   atomic-specifier:
79300b57cec5SDimitry Andric ///           _Atomic ( type-name )
79310b57cec5SDimitry Andric ///
79320b57cec5SDimitry Andric void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
79330b57cec5SDimitry Andric   assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
79340b57cec5SDimitry Andric          "Not an atomic specifier");
79350b57cec5SDimitry Andric 
79360b57cec5SDimitry Andric   SourceLocation StartLoc = ConsumeToken();
79370b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
79380b57cec5SDimitry Andric   if (T.consumeOpen())
79390b57cec5SDimitry Andric     return;
79400b57cec5SDimitry Andric 
79410b57cec5SDimitry Andric   TypeResult Result = ParseTypeName();
79420b57cec5SDimitry Andric   if (Result.isInvalid()) {
79430b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
79440b57cec5SDimitry Andric     return;
79450b57cec5SDimitry Andric   }
79460b57cec5SDimitry Andric 
79470b57cec5SDimitry Andric   // Match the ')'
79480b57cec5SDimitry Andric   T.consumeClose();
79490b57cec5SDimitry Andric 
79500b57cec5SDimitry Andric   if (T.getCloseLocation().isInvalid())
79510b57cec5SDimitry Andric     return;
79520b57cec5SDimitry Andric 
7953bdd1243dSDimitry Andric   DS.setTypeArgumentRange(T.getRange());
79540b57cec5SDimitry Andric   DS.SetRangeEnd(T.getCloseLocation());
79550b57cec5SDimitry Andric 
79560b57cec5SDimitry Andric   const char *PrevSpec = nullptr;
79570b57cec5SDimitry Andric   unsigned DiagID;
79580b57cec5SDimitry Andric   if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
79590b57cec5SDimitry Andric                          DiagID, Result.get(),
79600b57cec5SDimitry Andric                          Actions.getASTContext().getPrintingPolicy()))
79610b57cec5SDimitry Andric     Diag(StartLoc, DiagID) << PrevSpec;
79620b57cec5SDimitry Andric }
79630b57cec5SDimitry Andric 
79640b57cec5SDimitry Andric /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
79650b57cec5SDimitry Andric /// from TryAltiVecVectorToken.
79660b57cec5SDimitry Andric bool Parser::TryAltiVecVectorTokenOutOfLine() {
79670b57cec5SDimitry Andric   Token Next = NextToken();
79680b57cec5SDimitry Andric   switch (Next.getKind()) {
79690b57cec5SDimitry Andric   default: return false;
79700b57cec5SDimitry Andric   case tok::kw_short:
79710b57cec5SDimitry Andric   case tok::kw_long:
79720b57cec5SDimitry Andric   case tok::kw_signed:
79730b57cec5SDimitry Andric   case tok::kw_unsigned:
79740b57cec5SDimitry Andric   case tok::kw_void:
79750b57cec5SDimitry Andric   case tok::kw_char:
79760b57cec5SDimitry Andric   case tok::kw_int:
79770b57cec5SDimitry Andric   case tok::kw_float:
79780b57cec5SDimitry Andric   case tok::kw_double:
79790b57cec5SDimitry Andric   case tok::kw_bool:
7980fe6060f1SDimitry Andric   case tok::kw__Bool:
79810b57cec5SDimitry Andric   case tok::kw___bool:
79820b57cec5SDimitry Andric   case tok::kw___pixel:
79830b57cec5SDimitry Andric     Tok.setKind(tok::kw___vector);
79840b57cec5SDimitry Andric     return true;
79850b57cec5SDimitry Andric   case tok::identifier:
79860b57cec5SDimitry Andric     if (Next.getIdentifierInfo() == Ident_pixel) {
79870b57cec5SDimitry Andric       Tok.setKind(tok::kw___vector);
79880b57cec5SDimitry Andric       return true;
79890b57cec5SDimitry Andric     }
7990fe6060f1SDimitry Andric     if (Next.getIdentifierInfo() == Ident_bool ||
7991fe6060f1SDimitry Andric         Next.getIdentifierInfo() == Ident_Bool) {
79920b57cec5SDimitry Andric       Tok.setKind(tok::kw___vector);
79930b57cec5SDimitry Andric       return true;
79940b57cec5SDimitry Andric     }
79950b57cec5SDimitry Andric     return false;
79960b57cec5SDimitry Andric   }
79970b57cec5SDimitry Andric }
79980b57cec5SDimitry Andric 
79990b57cec5SDimitry Andric bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
80000b57cec5SDimitry Andric                                       const char *&PrevSpec, unsigned &DiagID,
80010b57cec5SDimitry Andric                                       bool &isInvalid) {
80020b57cec5SDimitry Andric   const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
80030b57cec5SDimitry Andric   if (Tok.getIdentifierInfo() == Ident_vector) {
80040b57cec5SDimitry Andric     Token Next = NextToken();
80050b57cec5SDimitry Andric     switch (Next.getKind()) {
80060b57cec5SDimitry Andric     case tok::kw_short:
80070b57cec5SDimitry Andric     case tok::kw_long:
80080b57cec5SDimitry Andric     case tok::kw_signed:
80090b57cec5SDimitry Andric     case tok::kw_unsigned:
80100b57cec5SDimitry Andric     case tok::kw_void:
80110b57cec5SDimitry Andric     case tok::kw_char:
80120b57cec5SDimitry Andric     case tok::kw_int:
80130b57cec5SDimitry Andric     case tok::kw_float:
80140b57cec5SDimitry Andric     case tok::kw_double:
80150b57cec5SDimitry Andric     case tok::kw_bool:
8016fe6060f1SDimitry Andric     case tok::kw__Bool:
80170b57cec5SDimitry Andric     case tok::kw___bool:
80180b57cec5SDimitry Andric     case tok::kw___pixel:
80190b57cec5SDimitry Andric       isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
80200b57cec5SDimitry Andric       return true;
80210b57cec5SDimitry Andric     case tok::identifier:
80220b57cec5SDimitry Andric       if (Next.getIdentifierInfo() == Ident_pixel) {
80230b57cec5SDimitry Andric         isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
80240b57cec5SDimitry Andric         return true;
80250b57cec5SDimitry Andric       }
8026fe6060f1SDimitry Andric       if (Next.getIdentifierInfo() == Ident_bool ||
8027fe6060f1SDimitry Andric           Next.getIdentifierInfo() == Ident_Bool) {
8028fe6060f1SDimitry Andric         isInvalid =
8029fe6060f1SDimitry Andric             DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
80300b57cec5SDimitry Andric         return true;
80310b57cec5SDimitry Andric       }
80320b57cec5SDimitry Andric       break;
80330b57cec5SDimitry Andric     default:
80340b57cec5SDimitry Andric       break;
80350b57cec5SDimitry Andric     }
80360b57cec5SDimitry Andric   } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
80370b57cec5SDimitry Andric              DS.isTypeAltiVecVector()) {
80380b57cec5SDimitry Andric     isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
80390b57cec5SDimitry Andric     return true;
80400b57cec5SDimitry Andric   } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
80410b57cec5SDimitry Andric              DS.isTypeAltiVecVector()) {
80420b57cec5SDimitry Andric     isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
80430b57cec5SDimitry Andric     return true;
80440b57cec5SDimitry Andric   }
80450b57cec5SDimitry Andric   return false;
80460b57cec5SDimitry Andric }
80470eae32dcSDimitry Andric 
80480eae32dcSDimitry Andric void Parser::DiagnoseBitIntUse(const Token &Tok) {
80490eae32dcSDimitry Andric   // If the token is for _ExtInt, diagnose it as being deprecated. Otherwise,
80500eae32dcSDimitry Andric   // the token is about _BitInt and gets (potentially) diagnosed as use of an
80510eae32dcSDimitry Andric   // extension.
80520eae32dcSDimitry Andric   assert(Tok.isOneOf(tok::kw__ExtInt, tok::kw__BitInt) &&
80530eae32dcSDimitry Andric          "expected either an _ExtInt or _BitInt token!");
80540eae32dcSDimitry Andric 
80550eae32dcSDimitry Andric   SourceLocation Loc = Tok.getLocation();
80560eae32dcSDimitry Andric   if (Tok.is(tok::kw__ExtInt)) {
80570eae32dcSDimitry Andric     Diag(Loc, diag::warn_ext_int_deprecated)
80580eae32dcSDimitry Andric         << FixItHint::CreateReplacement(Loc, "_BitInt");
80590eae32dcSDimitry Andric   } else {
80605f757f3fSDimitry Andric     // In C23 mode, diagnose that the use is not compatible with pre-C23 modes.
80610eae32dcSDimitry Andric     // Otherwise, diagnose that the use is a Clang extension.
80625f757f3fSDimitry Andric     if (getLangOpts().C23)
80635f757f3fSDimitry Andric       Diag(Loc, diag::warn_c23_compat_keyword) << Tok.getName();
80640eae32dcSDimitry Andric     else
80650eae32dcSDimitry Andric       Diag(Loc, diag::ext_bit_int) << getLangOpts().CPlusPlus;
80660eae32dcSDimitry Andric   }
80670eae32dcSDimitry Andric }
8068