xref: /freebsd/contrib/llvm-project/clang/lib/Parse/ParseDecl.cpp (revision 06c3fb2749bda94cb5201f81ffdb8fa6c3161b2e)
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"
210b57cec5SDimitry Andric #include "clang/Parse/ParseDiagnostic.h"
2281ad6265SDimitry Andric #include "clang/Parse/Parser.h"
2381ad6265SDimitry Andric #include "clang/Parse/RAIIObjectsForParser.h"
24*06c3fb27SDimitry Andric #include "clang/Sema/EnterExpressionEvaluationContext.h"
250b57cec5SDimitry Andric #include "clang/Sema/Lookup.h"
260b57cec5SDimitry Andric #include "clang/Sema/ParsedTemplate.h"
270b57cec5SDimitry Andric #include "clang/Sema/Scope.h"
28e8d8bef9SDimitry Andric #include "clang/Sema/SemaDiagnostic.h"
290b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h"
300b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
310b57cec5SDimitry Andric #include "llvm/ADT/StringSwitch.h"
32bdd1243dSDimitry Andric #include <optional>
330b57cec5SDimitry Andric 
340b57cec5SDimitry Andric using namespace clang;
350b57cec5SDimitry Andric 
360b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
370b57cec5SDimitry Andric // C99 6.7: Declarations.
380b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
390b57cec5SDimitry Andric 
400b57cec5SDimitry Andric /// ParseTypeName
410b57cec5SDimitry Andric ///       type-name: [C99 6.7.6]
420b57cec5SDimitry Andric ///         specifier-qualifier-list abstract-declarator[opt]
430b57cec5SDimitry Andric ///
440b57cec5SDimitry Andric /// Called type-id in C++.
4581ad6265SDimitry Andric TypeResult Parser::ParseTypeName(SourceRange *Range, DeclaratorContext Context,
4681ad6265SDimitry Andric                                  AccessSpecifier AS, Decl **OwnedType,
470b57cec5SDimitry Andric                                  ParsedAttributes *Attrs) {
480b57cec5SDimitry Andric   DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
490b57cec5SDimitry Andric   if (DSC == DeclSpecContext::DSC_normal)
500b57cec5SDimitry Andric     DSC = DeclSpecContext::DSC_type_specifier;
510b57cec5SDimitry Andric 
520b57cec5SDimitry Andric   // Parse the common declaration-specifiers piece.
530b57cec5SDimitry Andric   DeclSpec DS(AttrFactory);
540b57cec5SDimitry Andric   if (Attrs)
550b57cec5SDimitry Andric     DS.addAttributes(*Attrs);
560b57cec5SDimitry Andric   ParseSpecifierQualifierList(DS, AS, DSC);
570b57cec5SDimitry Andric   if (OwnedType)
580b57cec5SDimitry Andric     *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : nullptr;
590b57cec5SDimitry Andric 
60*06c3fb27SDimitry Andric   // Move declspec attributes to ParsedAttributes
61*06c3fb27SDimitry Andric   if (Attrs) {
62*06c3fb27SDimitry Andric     llvm::SmallVector<ParsedAttr *, 1> ToBeMoved;
63*06c3fb27SDimitry Andric     for (ParsedAttr &AL : DS.getAttributes()) {
64*06c3fb27SDimitry Andric       if (AL.isDeclspecAttribute())
65*06c3fb27SDimitry Andric         ToBeMoved.push_back(&AL);
66*06c3fb27SDimitry Andric     }
67*06c3fb27SDimitry Andric 
68*06c3fb27SDimitry Andric     for (ParsedAttr *AL : ToBeMoved)
69*06c3fb27SDimitry Andric       Attrs->takeOneFrom(DS.getAttributes(), AL);
70*06c3fb27SDimitry Andric   }
71*06c3fb27SDimitry Andric 
720b57cec5SDimitry Andric   // Parse the abstract-declarator, if present.
7381ad6265SDimitry Andric   Declarator DeclaratorInfo(DS, ParsedAttributesView::none(), Context);
740b57cec5SDimitry Andric   ParseDeclarator(DeclaratorInfo);
750b57cec5SDimitry Andric   if (Range)
760b57cec5SDimitry Andric     *Range = DeclaratorInfo.getSourceRange();
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric   if (DeclaratorInfo.isInvalidType())
790b57cec5SDimitry Andric     return true;
800b57cec5SDimitry Andric 
810b57cec5SDimitry Andric   return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
820b57cec5SDimitry Andric }
830b57cec5SDimitry Andric 
840b57cec5SDimitry Andric /// Normalizes an attribute name by dropping prefixed and suffixed __.
850b57cec5SDimitry Andric static StringRef normalizeAttrName(StringRef Name) {
860b57cec5SDimitry Andric   if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
870b57cec5SDimitry Andric     return Name.drop_front(2).drop_back(2);
880b57cec5SDimitry Andric   return Name;
890b57cec5SDimitry Andric }
900b57cec5SDimitry Andric 
910b57cec5SDimitry Andric /// isAttributeLateParsed - Return true if the attribute has arguments that
920b57cec5SDimitry Andric /// require late parsing.
930b57cec5SDimitry Andric static bool isAttributeLateParsed(const IdentifierInfo &II) {
940b57cec5SDimitry Andric #define CLANG_ATTR_LATE_PARSED_LIST
950b57cec5SDimitry Andric     return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
960b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
970b57cec5SDimitry Andric         .Default(false);
980b57cec5SDimitry Andric #undef CLANG_ATTR_LATE_PARSED_LIST
990b57cec5SDimitry Andric }
1000b57cec5SDimitry Andric 
1010b57cec5SDimitry Andric /// Check if the a start and end source location expand to the same macro.
102a7dea167SDimitry Andric static bool FindLocsWithCommonFileID(Preprocessor &PP, SourceLocation StartLoc,
1030b57cec5SDimitry Andric                                      SourceLocation EndLoc) {
1040b57cec5SDimitry Andric   if (!StartLoc.isMacroID() || !EndLoc.isMacroID())
1050b57cec5SDimitry Andric     return false;
1060b57cec5SDimitry Andric 
1070b57cec5SDimitry Andric   SourceManager &SM = PP.getSourceManager();
1080b57cec5SDimitry Andric   if (SM.getFileID(StartLoc) != SM.getFileID(EndLoc))
1090b57cec5SDimitry Andric     return false;
1100b57cec5SDimitry Andric 
1110b57cec5SDimitry Andric   bool AttrStartIsInMacro =
1120b57cec5SDimitry Andric       Lexer::isAtStartOfMacroExpansion(StartLoc, SM, PP.getLangOpts());
1130b57cec5SDimitry Andric   bool AttrEndIsInMacro =
1140b57cec5SDimitry Andric       Lexer::isAtEndOfMacroExpansion(EndLoc, SM, PP.getLangOpts());
1150b57cec5SDimitry Andric   return AttrStartIsInMacro && AttrEndIsInMacro;
1160b57cec5SDimitry Andric }
1170b57cec5SDimitry Andric 
11881ad6265SDimitry Andric void Parser::ParseAttributes(unsigned WhichAttrKinds, ParsedAttributes &Attrs,
119fe6060f1SDimitry Andric                              LateParsedAttrList *LateAttrs) {
120fe6060f1SDimitry Andric   bool MoreToParse;
121fe6060f1SDimitry Andric   do {
122fe6060f1SDimitry Andric     // Assume there's nothing left to parse, but if any attributes are in fact
123fe6060f1SDimitry Andric     // parsed, loop to ensure all specified attribute combinations are parsed.
124fe6060f1SDimitry Andric     MoreToParse = false;
125fe6060f1SDimitry Andric     if (WhichAttrKinds & PAKM_CXX11)
12681ad6265SDimitry Andric       MoreToParse |= MaybeParseCXX11Attributes(Attrs);
127fe6060f1SDimitry Andric     if (WhichAttrKinds & PAKM_GNU)
12881ad6265SDimitry Andric       MoreToParse |= MaybeParseGNUAttributes(Attrs, LateAttrs);
129fe6060f1SDimitry Andric     if (WhichAttrKinds & PAKM_Declspec)
13081ad6265SDimitry Andric       MoreToParse |= MaybeParseMicrosoftDeclSpecs(Attrs);
131fe6060f1SDimitry Andric   } while (MoreToParse);
132fe6060f1SDimitry Andric }
133fe6060f1SDimitry Andric 
1340b57cec5SDimitry Andric /// ParseGNUAttributes - Parse a non-empty attributes list.
1350b57cec5SDimitry Andric ///
1360b57cec5SDimitry Andric /// [GNU] attributes:
1370b57cec5SDimitry Andric ///         attribute
1380b57cec5SDimitry Andric ///         attributes attribute
1390b57cec5SDimitry Andric ///
1400b57cec5SDimitry Andric /// [GNU]  attribute:
1410b57cec5SDimitry Andric ///          '__attribute__' '(' '(' attribute-list ')' ')'
1420b57cec5SDimitry Andric ///
1430b57cec5SDimitry Andric /// [GNU]  attribute-list:
1440b57cec5SDimitry Andric ///          attrib
1450b57cec5SDimitry Andric ///          attribute_list ',' attrib
1460b57cec5SDimitry Andric ///
1470b57cec5SDimitry Andric /// [GNU]  attrib:
1480b57cec5SDimitry Andric ///          empty
1490b57cec5SDimitry Andric ///          attrib-name
1500b57cec5SDimitry Andric ///          attrib-name '(' identifier ')'
1510b57cec5SDimitry Andric ///          attrib-name '(' identifier ',' nonempty-expr-list ')'
1520b57cec5SDimitry Andric ///          attrib-name '(' argument-expression-list [C99 6.5.2] ')'
1530b57cec5SDimitry Andric ///
1540b57cec5SDimitry Andric /// [GNU]  attrib-name:
1550b57cec5SDimitry Andric ///          identifier
1560b57cec5SDimitry Andric ///          typespec
1570b57cec5SDimitry Andric ///          typequal
1580b57cec5SDimitry Andric ///          storageclass
1590b57cec5SDimitry Andric ///
1600b57cec5SDimitry Andric /// Whether an attribute takes an 'identifier' is determined by the
1610b57cec5SDimitry Andric /// attrib-name. GCC's behavior here is not worth imitating:
1620b57cec5SDimitry Andric ///
1630b57cec5SDimitry Andric ///  * In C mode, if the attribute argument list starts with an identifier
1640b57cec5SDimitry Andric ///    followed by a ',' or an ')', and the identifier doesn't resolve to
1650b57cec5SDimitry Andric ///    a type, it is parsed as an identifier. If the attribute actually
1660b57cec5SDimitry Andric ///    wanted an expression, it's out of luck (but it turns out that no
1670b57cec5SDimitry Andric ///    attributes work that way, because C constant expressions are very
1680b57cec5SDimitry Andric ///    limited).
1690b57cec5SDimitry Andric ///  * In C++ mode, if the attribute argument list starts with an identifier,
1700b57cec5SDimitry Andric ///    and the attribute *wants* an identifier, it is parsed as an identifier.
1710b57cec5SDimitry Andric ///    At block scope, any additional tokens between the identifier and the
1720b57cec5SDimitry Andric ///    ',' or ')' are ignored, otherwise they produce a parse error.
1730b57cec5SDimitry Andric ///
1740b57cec5SDimitry Andric /// We follow the C++ model, but don't allow junk after the identifier.
17581ad6265SDimitry Andric void Parser::ParseGNUAttributes(ParsedAttributes &Attrs,
176fe6060f1SDimitry Andric                                 LateParsedAttrList *LateAttrs, Declarator *D) {
1770b57cec5SDimitry Andric   assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
1780b57cec5SDimitry Andric 
17981ad6265SDimitry Andric   SourceLocation StartLoc = Tok.getLocation();
18081ad6265SDimitry Andric   SourceLocation EndLoc = StartLoc;
181fe6060f1SDimitry Andric 
1820b57cec5SDimitry Andric   while (Tok.is(tok::kw___attribute)) {
1830b57cec5SDimitry Andric     SourceLocation AttrTokLoc = ConsumeToken();
184fe6060f1SDimitry Andric     unsigned OldNumAttrs = Attrs.size();
1850b57cec5SDimitry Andric     unsigned OldNumLateAttrs = LateAttrs ? LateAttrs->size() : 0;
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric     if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
1880b57cec5SDimitry Andric                          "attribute")) {
1890b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
1900b57cec5SDimitry Andric       return;
1910b57cec5SDimitry Andric     }
1920b57cec5SDimitry Andric     if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
1930b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
1940b57cec5SDimitry Andric       return;
1950b57cec5SDimitry Andric     }
1960b57cec5SDimitry Andric     // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
1970b57cec5SDimitry Andric     do {
1980b57cec5SDimitry Andric       // Eat preceeding commas to allow __attribute__((,,,foo))
1990b57cec5SDimitry Andric       while (TryConsumeToken(tok::comma))
2000b57cec5SDimitry Andric         ;
2010b57cec5SDimitry Andric 
2020b57cec5SDimitry Andric       // Expect an identifier or declaration specifier (const, int, etc.)
2030b57cec5SDimitry Andric       if (Tok.isAnnotation())
2040b57cec5SDimitry Andric         break;
205349cc55cSDimitry Andric       if (Tok.is(tok::code_completion)) {
206349cc55cSDimitry Andric         cutOffParsing();
207349cc55cSDimitry Andric         Actions.CodeCompleteAttribute(AttributeCommonInfo::Syntax::AS_GNU);
208349cc55cSDimitry Andric         break;
209349cc55cSDimitry Andric       }
2100b57cec5SDimitry Andric       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
2110b57cec5SDimitry Andric       if (!AttrName)
2120b57cec5SDimitry Andric         break;
2130b57cec5SDimitry Andric 
2140b57cec5SDimitry Andric       SourceLocation AttrNameLoc = ConsumeToken();
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric       if (Tok.isNot(tok::l_paren)) {
217fe6060f1SDimitry Andric         Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
218*06c3fb27SDimitry Andric                      ParsedAttr::Form::GNU());
2190b57cec5SDimitry Andric         continue;
2200b57cec5SDimitry Andric       }
2210b57cec5SDimitry Andric 
2220b57cec5SDimitry Andric       // Handle "parameterized" attributes
2230b57cec5SDimitry Andric       if (!LateAttrs || !isAttributeLateParsed(*AttrName)) {
22481ad6265SDimitry Andric         ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, &EndLoc, nullptr,
225*06c3fb27SDimitry Andric                               SourceLocation(), ParsedAttr::Form::GNU(), D);
2260b57cec5SDimitry Andric         continue;
2270b57cec5SDimitry Andric       }
2280b57cec5SDimitry Andric 
2290b57cec5SDimitry Andric       // Handle attributes with arguments that require late parsing.
2300b57cec5SDimitry Andric       LateParsedAttribute *LA =
2310b57cec5SDimitry Andric           new LateParsedAttribute(this, *AttrName, AttrNameLoc);
2320b57cec5SDimitry Andric       LateAttrs->push_back(LA);
2330b57cec5SDimitry Andric 
2340b57cec5SDimitry Andric       // Attributes in a class are parsed at the end of the class, along
2350b57cec5SDimitry Andric       // with other late-parsed declarations.
2360b57cec5SDimitry Andric       if (!ClassStack.empty() && !LateAttrs->parseSoon())
2370b57cec5SDimitry Andric         getCurrentClass().LateParsedDeclarations.push_back(LA);
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric       // Be sure ConsumeAndStoreUntil doesn't see the start l_paren, since it
2400b57cec5SDimitry Andric       // recursively consumes balanced parens.
2410b57cec5SDimitry Andric       LA->Toks.push_back(Tok);
2420b57cec5SDimitry Andric       ConsumeParen();
2430b57cec5SDimitry Andric       // Consume everything up to and including the matching right parens.
2440b57cec5SDimitry Andric       ConsumeAndStoreUntil(tok::r_paren, LA->Toks, /*StopAtSemi=*/true);
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric       Token Eof;
2470b57cec5SDimitry Andric       Eof.startToken();
2480b57cec5SDimitry Andric       Eof.setLocation(Tok.getLocation());
2490b57cec5SDimitry Andric       LA->Toks.push_back(Eof);
2500b57cec5SDimitry Andric     } while (Tok.is(tok::comma));
2510b57cec5SDimitry Andric 
2520b57cec5SDimitry Andric     if (ExpectAndConsume(tok::r_paren))
2530b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
2540b57cec5SDimitry Andric     SourceLocation Loc = Tok.getLocation();
2550b57cec5SDimitry Andric     if (ExpectAndConsume(tok::r_paren))
2560b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
25781ad6265SDimitry Andric     EndLoc = Loc;
2580b57cec5SDimitry Andric 
2590b57cec5SDimitry Andric     // If this was declared in a macro, attach the macro IdentifierInfo to the
2600b57cec5SDimitry Andric     // parsed attribute.
2610b57cec5SDimitry Andric     auto &SM = PP.getSourceManager();
2620b57cec5SDimitry Andric     if (!SM.isWrittenInBuiltinFile(SM.getSpellingLoc(AttrTokLoc)) &&
2630b57cec5SDimitry Andric         FindLocsWithCommonFileID(PP, AttrTokLoc, Loc)) {
2640b57cec5SDimitry Andric       CharSourceRange ExpansionRange = SM.getExpansionRange(AttrTokLoc);
2650b57cec5SDimitry Andric       StringRef FoundName =
2660b57cec5SDimitry Andric           Lexer::getSourceText(ExpansionRange, SM, PP.getLangOpts());
2670b57cec5SDimitry Andric       IdentifierInfo *MacroII = PP.getIdentifierInfo(FoundName);
2680b57cec5SDimitry Andric 
269fe6060f1SDimitry Andric       for (unsigned i = OldNumAttrs; i < Attrs.size(); ++i)
270fe6060f1SDimitry Andric         Attrs[i].setMacroIdentifier(MacroII, ExpansionRange.getBegin());
2710b57cec5SDimitry Andric 
2720b57cec5SDimitry Andric       if (LateAttrs) {
2730b57cec5SDimitry Andric         for (unsigned i = OldNumLateAttrs; i < LateAttrs->size(); ++i)
2740b57cec5SDimitry Andric           (*LateAttrs)[i]->MacroII = MacroII;
2750b57cec5SDimitry Andric       }
2760b57cec5SDimitry Andric     }
2770b57cec5SDimitry Andric   }
278fe6060f1SDimitry Andric 
27981ad6265SDimitry Andric   Attrs.Range = SourceRange(StartLoc, EndLoc);
2800b57cec5SDimitry Andric }
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric /// Determine whether the given attribute has an identifier argument.
2830b57cec5SDimitry Andric static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
2840b57cec5SDimitry Andric #define CLANG_ATTR_IDENTIFIER_ARG_LIST
2850b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
2860b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
2870b57cec5SDimitry Andric            .Default(false);
2880b57cec5SDimitry Andric #undef CLANG_ATTR_IDENTIFIER_ARG_LIST
2890b57cec5SDimitry Andric }
2900b57cec5SDimitry Andric 
2910b57cec5SDimitry Andric /// Determine whether the given attribute has a variadic identifier argument.
2920b57cec5SDimitry Andric static bool attributeHasVariadicIdentifierArg(const IdentifierInfo &II) {
2930b57cec5SDimitry Andric #define CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST
2940b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
2950b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
2960b57cec5SDimitry Andric            .Default(false);
2970b57cec5SDimitry Andric #undef CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST
2980b57cec5SDimitry Andric }
2990b57cec5SDimitry Andric 
3000b57cec5SDimitry Andric /// Determine whether the given attribute treats kw_this as an identifier.
3010b57cec5SDimitry Andric static bool attributeTreatsKeywordThisAsIdentifier(const IdentifierInfo &II) {
3020b57cec5SDimitry Andric #define CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST
3030b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
3040b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
3050b57cec5SDimitry Andric            .Default(false);
3060b57cec5SDimitry Andric #undef CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST
3070b57cec5SDimitry Andric }
3080b57cec5SDimitry Andric 
30981ad6265SDimitry Andric /// Determine if an attribute accepts parameter packs.
31081ad6265SDimitry Andric static bool attributeAcceptsExprPack(const IdentifierInfo &II) {
31181ad6265SDimitry Andric #define CLANG_ATTR_ACCEPTS_EXPR_PACK
31281ad6265SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
31381ad6265SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
31481ad6265SDimitry Andric       .Default(false);
31581ad6265SDimitry Andric #undef CLANG_ATTR_ACCEPTS_EXPR_PACK
31681ad6265SDimitry Andric }
31781ad6265SDimitry Andric 
3180b57cec5SDimitry Andric /// Determine whether the given attribute parses a type argument.
3190b57cec5SDimitry Andric static bool attributeIsTypeArgAttr(const IdentifierInfo &II) {
3200b57cec5SDimitry Andric #define CLANG_ATTR_TYPE_ARG_LIST
3210b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
3220b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
3230b57cec5SDimitry Andric            .Default(false);
3240b57cec5SDimitry Andric #undef CLANG_ATTR_TYPE_ARG_LIST
3250b57cec5SDimitry Andric }
3260b57cec5SDimitry Andric 
3270b57cec5SDimitry Andric /// Determine whether the given attribute requires parsing its arguments
3280b57cec5SDimitry Andric /// in an unevaluated context or not.
3290b57cec5SDimitry Andric static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) {
3300b57cec5SDimitry Andric #define CLANG_ATTR_ARG_CONTEXT_LIST
3310b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
3320b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
3330b57cec5SDimitry Andric            .Default(false);
3340b57cec5SDimitry Andric #undef CLANG_ATTR_ARG_CONTEXT_LIST
3350b57cec5SDimitry Andric }
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric IdentifierLoc *Parser::ParseIdentifierLoc() {
3380b57cec5SDimitry Andric   assert(Tok.is(tok::identifier) && "expected an identifier");
3390b57cec5SDimitry Andric   IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
3400b57cec5SDimitry Andric                                             Tok.getLocation(),
3410b57cec5SDimitry Andric                                             Tok.getIdentifierInfo());
3420b57cec5SDimitry Andric   ConsumeToken();
3430b57cec5SDimitry Andric   return IL;
3440b57cec5SDimitry Andric }
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
3470b57cec5SDimitry Andric                                        SourceLocation AttrNameLoc,
3480b57cec5SDimitry Andric                                        ParsedAttributes &Attrs,
3490b57cec5SDimitry Andric                                        IdentifierInfo *ScopeName,
3500b57cec5SDimitry Andric                                        SourceLocation ScopeLoc,
351*06c3fb27SDimitry Andric                                        ParsedAttr::Form Form) {
3520b57cec5SDimitry Andric   BalancedDelimiterTracker Parens(*this, tok::l_paren);
3530b57cec5SDimitry Andric   Parens.consumeOpen();
3540b57cec5SDimitry Andric 
3550b57cec5SDimitry Andric   TypeResult T;
3560b57cec5SDimitry Andric   if (Tok.isNot(tok::r_paren))
3570b57cec5SDimitry Andric     T = ParseTypeName();
3580b57cec5SDimitry Andric 
3590b57cec5SDimitry Andric   if (Parens.consumeClose())
3600b57cec5SDimitry Andric     return;
3610b57cec5SDimitry Andric 
3620b57cec5SDimitry Andric   if (T.isInvalid())
3630b57cec5SDimitry Andric     return;
3640b57cec5SDimitry Andric 
3650b57cec5SDimitry Andric   if (T.isUsable())
3660b57cec5SDimitry Andric     Attrs.addNewTypeAttr(&AttrName,
3670b57cec5SDimitry Andric                          SourceRange(AttrNameLoc, Parens.getCloseLocation()),
368*06c3fb27SDimitry Andric                          ScopeName, ScopeLoc, T.get(), Form);
3690b57cec5SDimitry Andric   else
3700b57cec5SDimitry Andric     Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()),
371*06c3fb27SDimitry Andric                  ScopeName, ScopeLoc, nullptr, 0, Form);
3720b57cec5SDimitry Andric }
3730b57cec5SDimitry Andric 
3740b57cec5SDimitry Andric unsigned Parser::ParseAttributeArgsCommon(
3750b57cec5SDimitry Andric     IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
3760b57cec5SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
377*06c3fb27SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Form Form) {
3780b57cec5SDimitry Andric   // Ignore the left paren location for now.
3790b57cec5SDimitry Andric   ConsumeParen();
3800b57cec5SDimitry Andric 
3810b57cec5SDimitry Andric   bool ChangeKWThisToIdent = attributeTreatsKeywordThisAsIdentifier(*AttrName);
382a7dea167SDimitry Andric   bool AttributeIsTypeArgAttr = attributeIsTypeArgAttr(*AttrName);
38381ad6265SDimitry Andric   bool AttributeHasVariadicIdentifierArg =
38481ad6265SDimitry Andric       attributeHasVariadicIdentifierArg(*AttrName);
3850b57cec5SDimitry Andric 
3860b57cec5SDimitry Andric   // Interpret "kw_this" as an identifier if the attributed requests it.
3870b57cec5SDimitry Andric   if (ChangeKWThisToIdent && Tok.is(tok::kw_this))
3880b57cec5SDimitry Andric     Tok.setKind(tok::identifier);
3890b57cec5SDimitry Andric 
3900b57cec5SDimitry Andric   ArgsVector ArgExprs;
3910b57cec5SDimitry Andric   if (Tok.is(tok::identifier)) {
3920b57cec5SDimitry Andric     // If this attribute wants an 'identifier' argument, make it so.
39381ad6265SDimitry Andric     bool IsIdentifierArg = AttributeHasVariadicIdentifierArg ||
39481ad6265SDimitry Andric                            attributeHasIdentifierArg(*AttrName);
3950b57cec5SDimitry Andric     ParsedAttr::Kind AttrKind =
396*06c3fb27SDimitry Andric         ParsedAttr::getParsedKind(AttrName, ScopeName, Form.getSyntax());
3970b57cec5SDimitry Andric 
3980b57cec5SDimitry Andric     // If we don't know how to parse this attribute, but this is the only
3990b57cec5SDimitry Andric     // token in this argument, assume it's meant to be an identifier.
4000b57cec5SDimitry Andric     if (AttrKind == ParsedAttr::UnknownAttribute ||
4010b57cec5SDimitry Andric         AttrKind == ParsedAttr::IgnoredAttribute) {
4020b57cec5SDimitry Andric       const Token &Next = NextToken();
4030b57cec5SDimitry Andric       IsIdentifierArg = Next.isOneOf(tok::r_paren, tok::comma);
4040b57cec5SDimitry Andric     }
4050b57cec5SDimitry Andric 
4060b57cec5SDimitry Andric     if (IsIdentifierArg)
4070b57cec5SDimitry Andric       ArgExprs.push_back(ParseIdentifierLoc());
4080b57cec5SDimitry Andric   }
4090b57cec5SDimitry Andric 
410a7dea167SDimitry Andric   ParsedType TheParsedType;
4110b57cec5SDimitry Andric   if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
4120b57cec5SDimitry Andric     // Eat the comma.
4130b57cec5SDimitry Andric     if (!ArgExprs.empty())
4140b57cec5SDimitry Andric       ConsumeToken();
4150b57cec5SDimitry Andric 
416a7dea167SDimitry Andric     if (AttributeIsTypeArgAttr) {
41781ad6265SDimitry Andric       // FIXME: Multiple type arguments are not implemented.
418a7dea167SDimitry Andric       TypeResult T = ParseTypeName();
419a7dea167SDimitry Andric       if (T.isInvalid()) {
420a7dea167SDimitry Andric         SkipUntil(tok::r_paren, StopAtSemi);
421a7dea167SDimitry Andric         return 0;
422a7dea167SDimitry Andric       }
423a7dea167SDimitry Andric       if (T.isUsable())
424a7dea167SDimitry Andric         TheParsedType = T.get();
42581ad6265SDimitry Andric     } else if (AttributeHasVariadicIdentifierArg) {
42681ad6265SDimitry Andric       // Parse variadic identifier arg. This can either consume identifiers or
42781ad6265SDimitry Andric       // expressions. Variadic identifier args do not support parameter packs
42881ad6265SDimitry Andric       // because those are typically used for attributes with enumeration
42981ad6265SDimitry Andric       // arguments, and those enumerations are not something the user could
43081ad6265SDimitry Andric       // express via a pack.
43181ad6265SDimitry Andric       do {
43281ad6265SDimitry Andric         // Interpret "kw_this" as an identifier if the attributed requests it.
43381ad6265SDimitry Andric         if (ChangeKWThisToIdent && Tok.is(tok::kw_this))
43481ad6265SDimitry Andric           Tok.setKind(tok::identifier);
43581ad6265SDimitry Andric 
43681ad6265SDimitry Andric         ExprResult ArgExpr;
43781ad6265SDimitry Andric         if (Tok.is(tok::identifier)) {
4380b57cec5SDimitry Andric           ArgExprs.push_back(ParseIdentifierLoc());
4390b57cec5SDimitry Andric         } else {
4400b57cec5SDimitry Andric           bool Uneval = attributeParsedArgsUnevaluated(*AttrName);
4410b57cec5SDimitry Andric           EnterExpressionEvaluationContext Unevaluated(
4420b57cec5SDimitry Andric               Actions,
4430b57cec5SDimitry Andric               Uneval ? Sema::ExpressionEvaluationContext::Unevaluated
4440b57cec5SDimitry Andric                      : Sema::ExpressionEvaluationContext::ConstantEvaluated);
4450b57cec5SDimitry Andric 
4460b57cec5SDimitry Andric           ExprResult ArgExpr(
4470b57cec5SDimitry Andric               Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()));
44881ad6265SDimitry Andric 
4490b57cec5SDimitry Andric           if (ArgExpr.isInvalid()) {
4500b57cec5SDimitry Andric             SkipUntil(tok::r_paren, StopAtSemi);
4510b57cec5SDimitry Andric             return 0;
4520b57cec5SDimitry Andric           }
4530b57cec5SDimitry Andric           ArgExprs.push_back(ArgExpr.get());
4540b57cec5SDimitry Andric         }
4550b57cec5SDimitry Andric         // Eat the comma, move to the next argument
4560b57cec5SDimitry Andric       } while (TryConsumeToken(tok::comma));
45781ad6265SDimitry Andric     } else {
45881ad6265SDimitry Andric       // General case. Parse all available expressions.
45981ad6265SDimitry Andric       bool Uneval = attributeParsedArgsUnevaluated(*AttrName);
46081ad6265SDimitry Andric       EnterExpressionEvaluationContext Unevaluated(
46181ad6265SDimitry Andric           Actions, Uneval
46281ad6265SDimitry Andric                        ? Sema::ExpressionEvaluationContext::Unevaluated
46381ad6265SDimitry Andric                        : Sema::ExpressionEvaluationContext::ConstantEvaluated);
46481ad6265SDimitry Andric 
46581ad6265SDimitry Andric       ExprVector ParsedExprs;
466bdd1243dSDimitry Andric       if (ParseExpressionList(ParsedExprs, llvm::function_ref<void()>(),
46781ad6265SDimitry Andric                               /*FailImmediatelyOnInvalidExpr=*/true,
46881ad6265SDimitry Andric                               /*EarlyTypoCorrection=*/true)) {
46981ad6265SDimitry Andric         SkipUntil(tok::r_paren, StopAtSemi);
47081ad6265SDimitry Andric         return 0;
47181ad6265SDimitry Andric       }
47281ad6265SDimitry Andric 
47381ad6265SDimitry Andric       // Pack expansion must currently be explicitly supported by an attribute.
47481ad6265SDimitry Andric       for (size_t I = 0; I < ParsedExprs.size(); ++I) {
47581ad6265SDimitry Andric         if (!isa<PackExpansionExpr>(ParsedExprs[I]))
47681ad6265SDimitry Andric           continue;
47781ad6265SDimitry Andric 
47881ad6265SDimitry Andric         if (!attributeAcceptsExprPack(*AttrName)) {
47981ad6265SDimitry Andric           Diag(Tok.getLocation(),
48081ad6265SDimitry Andric                diag::err_attribute_argument_parm_pack_not_supported)
48181ad6265SDimitry Andric               << AttrName;
48281ad6265SDimitry Andric           SkipUntil(tok::r_paren, StopAtSemi);
48381ad6265SDimitry Andric           return 0;
48481ad6265SDimitry Andric         }
48581ad6265SDimitry Andric       }
48681ad6265SDimitry Andric 
48781ad6265SDimitry Andric       ArgExprs.insert(ArgExprs.end(), ParsedExprs.begin(), ParsedExprs.end());
48881ad6265SDimitry Andric     }
4890b57cec5SDimitry Andric   }
4900b57cec5SDimitry Andric 
4910b57cec5SDimitry Andric   SourceLocation RParen = Tok.getLocation();
4920b57cec5SDimitry Andric   if (!ExpectAndConsume(tok::r_paren)) {
4930b57cec5SDimitry Andric     SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
494a7dea167SDimitry Andric 
495a7dea167SDimitry Andric     if (AttributeIsTypeArgAttr && !TheParsedType.get().isNull()) {
496a7dea167SDimitry Andric       Attrs.addNewTypeAttr(AttrName, SourceRange(AttrNameLoc, RParen),
497*06c3fb27SDimitry Andric                            ScopeName, ScopeLoc, TheParsedType, Form);
498a7dea167SDimitry Andric     } else {
4990b57cec5SDimitry Andric       Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
500*06c3fb27SDimitry Andric                    ArgExprs.data(), ArgExprs.size(), Form);
5010b57cec5SDimitry Andric     }
502a7dea167SDimitry Andric   }
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric   if (EndLoc)
5050b57cec5SDimitry Andric     *EndLoc = RParen;
5060b57cec5SDimitry Andric 
507a7dea167SDimitry Andric   return static_cast<unsigned>(ArgExprs.size() + !TheParsedType.get().isNull());
5080b57cec5SDimitry Andric }
5090b57cec5SDimitry Andric 
5100b57cec5SDimitry Andric /// Parse the arguments to a parameterized GNU attribute or
5110b57cec5SDimitry Andric /// a C++11 attribute in "gnu" namespace.
51281ad6265SDimitry Andric void Parser::ParseGNUAttributeArgs(
51381ad6265SDimitry Andric     IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
51481ad6265SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
515*06c3fb27SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Form Form, Declarator *D) {
5160b57cec5SDimitry Andric 
5170b57cec5SDimitry Andric   assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
5180b57cec5SDimitry Andric 
5190b57cec5SDimitry Andric   ParsedAttr::Kind AttrKind =
520*06c3fb27SDimitry Andric       ParsedAttr::getParsedKind(AttrName, ScopeName, Form.getSyntax());
5210b57cec5SDimitry Andric 
5220b57cec5SDimitry Andric   if (AttrKind == ParsedAttr::AT_Availability) {
5230b57cec5SDimitry Andric     ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
524*06c3fb27SDimitry Andric                                ScopeLoc, Form);
5250b57cec5SDimitry Andric     return;
5260b57cec5SDimitry Andric   } else if (AttrKind == ParsedAttr::AT_ExternalSourceSymbol) {
5270b57cec5SDimitry Andric     ParseExternalSourceSymbolAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
528*06c3fb27SDimitry Andric                                        ScopeName, ScopeLoc, Form);
5290b57cec5SDimitry Andric     return;
5300b57cec5SDimitry Andric   } else if (AttrKind == ParsedAttr::AT_ObjCBridgeRelated) {
5310b57cec5SDimitry Andric     ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
532*06c3fb27SDimitry Andric                                     ScopeName, ScopeLoc, Form);
5330b57cec5SDimitry Andric     return;
534e8d8bef9SDimitry Andric   } else if (AttrKind == ParsedAttr::AT_SwiftNewType) {
535e8d8bef9SDimitry Andric     ParseSwiftNewTypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
536*06c3fb27SDimitry Andric                                ScopeLoc, Form);
537e8d8bef9SDimitry Andric     return;
5380b57cec5SDimitry Andric   } else if (AttrKind == ParsedAttr::AT_TypeTagForDatatype) {
5390b57cec5SDimitry Andric     ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
540*06c3fb27SDimitry Andric                                      ScopeName, ScopeLoc, Form);
5410b57cec5SDimitry Andric     return;
5420b57cec5SDimitry Andric   } else if (attributeIsTypeArgAttr(*AttrName)) {
54381ad6265SDimitry Andric     ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, ScopeName,
544*06c3fb27SDimitry Andric                               ScopeLoc, Form);
5450b57cec5SDimitry Andric     return;
5460b57cec5SDimitry Andric   }
5470b57cec5SDimitry Andric 
5480b57cec5SDimitry Andric   // These may refer to the function arguments, but need to be parsed early to
5490b57cec5SDimitry Andric   // participate in determining whether it's a redeclaration.
550bdd1243dSDimitry Andric   std::optional<ParseScope> PrototypeScope;
5510b57cec5SDimitry Andric   if (normalizeAttrName(AttrName->getName()) == "enable_if" &&
5520b57cec5SDimitry Andric       D && D->isFunctionDeclarator()) {
5530b57cec5SDimitry Andric     DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo();
5540b57cec5SDimitry Andric     PrototypeScope.emplace(this, Scope::FunctionPrototypeScope |
5550b57cec5SDimitry Andric                                      Scope::FunctionDeclarationScope |
5560b57cec5SDimitry Andric                                      Scope::DeclScope);
5570b57cec5SDimitry Andric     for (unsigned i = 0; i != FTI.NumParams; ++i) {
5580b57cec5SDimitry Andric       ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param);
5590b57cec5SDimitry Andric       Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param);
5600b57cec5SDimitry Andric     }
5610b57cec5SDimitry Andric   }
5620b57cec5SDimitry Andric 
5630b57cec5SDimitry Andric   ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
564*06c3fb27SDimitry Andric                            ScopeLoc, Form);
5650b57cec5SDimitry Andric }
5660b57cec5SDimitry Andric 
5670b57cec5SDimitry Andric unsigned Parser::ParseClangAttributeArgs(
5680b57cec5SDimitry Andric     IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
5690b57cec5SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
570*06c3fb27SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Form Form) {
5710b57cec5SDimitry Andric   assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric   ParsedAttr::Kind AttrKind =
574*06c3fb27SDimitry Andric       ParsedAttr::getParsedKind(AttrName, ScopeName, Form.getSyntax());
5750b57cec5SDimitry Andric 
5760b57cec5SDimitry Andric   switch (AttrKind) {
5770b57cec5SDimitry Andric   default:
5780b57cec5SDimitry Andric     return ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
579*06c3fb27SDimitry Andric                                     ScopeName, ScopeLoc, Form);
5800b57cec5SDimitry Andric   case ParsedAttr::AT_ExternalSourceSymbol:
5810b57cec5SDimitry Andric     ParseExternalSourceSymbolAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
582*06c3fb27SDimitry Andric                                        ScopeName, ScopeLoc, Form);
5830b57cec5SDimitry Andric     break;
5840b57cec5SDimitry Andric   case ParsedAttr::AT_Availability:
5850b57cec5SDimitry Andric     ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
586*06c3fb27SDimitry Andric                                ScopeLoc, Form);
5870b57cec5SDimitry Andric     break;
5880b57cec5SDimitry Andric   case ParsedAttr::AT_ObjCBridgeRelated:
5890b57cec5SDimitry Andric     ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
590*06c3fb27SDimitry Andric                                     ScopeName, ScopeLoc, Form);
5910b57cec5SDimitry Andric     break;
592e8d8bef9SDimitry Andric   case ParsedAttr::AT_SwiftNewType:
593e8d8bef9SDimitry Andric     ParseSwiftNewTypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
594*06c3fb27SDimitry Andric                                ScopeLoc, Form);
595e8d8bef9SDimitry Andric     break;
5960b57cec5SDimitry Andric   case ParsedAttr::AT_TypeTagForDatatype:
5970b57cec5SDimitry Andric     ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
598*06c3fb27SDimitry Andric                                      ScopeName, ScopeLoc, Form);
5990b57cec5SDimitry Andric     break;
6000b57cec5SDimitry Andric   }
6010b57cec5SDimitry Andric   return !Attrs.empty() ? Attrs.begin()->getNumArgs() : 0;
6020b57cec5SDimitry Andric }
6030b57cec5SDimitry Andric 
6040b57cec5SDimitry Andric bool Parser::ParseMicrosoftDeclSpecArgs(IdentifierInfo *AttrName,
6050b57cec5SDimitry Andric                                         SourceLocation AttrNameLoc,
6060b57cec5SDimitry Andric                                         ParsedAttributes &Attrs) {
60781ad6265SDimitry Andric   unsigned ExistingAttrs = Attrs.size();
60881ad6265SDimitry Andric 
6090b57cec5SDimitry Andric   // If the attribute isn't known, we will not attempt to parse any
6100b57cec5SDimitry Andric   // arguments.
61181ad6265SDimitry Andric   if (!hasAttribute(AttributeCommonInfo::Syntax::AS_Declspec, nullptr, AttrName,
6120b57cec5SDimitry Andric                     getTargetInfo(), getLangOpts())) {
6130b57cec5SDimitry Andric     // Eat the left paren, then skip to the ending right paren.
6140b57cec5SDimitry Andric     ConsumeParen();
6150b57cec5SDimitry Andric     SkipUntil(tok::r_paren);
6160b57cec5SDimitry Andric     return false;
6170b57cec5SDimitry Andric   }
6180b57cec5SDimitry Andric 
6190b57cec5SDimitry Andric   SourceLocation OpenParenLoc = Tok.getLocation();
6200b57cec5SDimitry Andric 
6210b57cec5SDimitry Andric   if (AttrName->getName() == "property") {
6220b57cec5SDimitry Andric     // The property declspec is more complex in that it can take one or two
6230b57cec5SDimitry Andric     // assignment expressions as a parameter, but the lhs of the assignment
6240b57cec5SDimitry Andric     // must be named get or put.
6250b57cec5SDimitry Andric 
6260b57cec5SDimitry Andric     BalancedDelimiterTracker T(*this, tok::l_paren);
6270b57cec5SDimitry Andric     T.expectAndConsume(diag::err_expected_lparen_after,
6280b57cec5SDimitry Andric                        AttrName->getNameStart(), tok::r_paren);
6290b57cec5SDimitry Andric 
6300b57cec5SDimitry Andric     enum AccessorKind {
6310b57cec5SDimitry Andric       AK_Invalid = -1,
6320b57cec5SDimitry Andric       AK_Put = 0,
6330b57cec5SDimitry Andric       AK_Get = 1 // indices into AccessorNames
6340b57cec5SDimitry Andric     };
6350b57cec5SDimitry Andric     IdentifierInfo *AccessorNames[] = {nullptr, nullptr};
6360b57cec5SDimitry Andric     bool HasInvalidAccessor = false;
6370b57cec5SDimitry Andric 
6380b57cec5SDimitry Andric     // Parse the accessor specifications.
6390b57cec5SDimitry Andric     while (true) {
6400b57cec5SDimitry Andric       // Stop if this doesn't look like an accessor spec.
6410b57cec5SDimitry Andric       if (!Tok.is(tok::identifier)) {
6420b57cec5SDimitry Andric         // If the user wrote a completely empty list, use a special diagnostic.
6430b57cec5SDimitry Andric         if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
6440b57cec5SDimitry Andric             AccessorNames[AK_Put] == nullptr &&
6450b57cec5SDimitry Andric             AccessorNames[AK_Get] == nullptr) {
6460b57cec5SDimitry Andric           Diag(AttrNameLoc, diag::err_ms_property_no_getter_or_putter);
6470b57cec5SDimitry Andric           break;
6480b57cec5SDimitry Andric         }
6490b57cec5SDimitry Andric 
6500b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
6510b57cec5SDimitry Andric         break;
6520b57cec5SDimitry Andric       }
6530b57cec5SDimitry Andric 
6540b57cec5SDimitry Andric       AccessorKind Kind;
6550b57cec5SDimitry Andric       SourceLocation KindLoc = Tok.getLocation();
6560b57cec5SDimitry Andric       StringRef KindStr = Tok.getIdentifierInfo()->getName();
6570b57cec5SDimitry Andric       if (KindStr == "get") {
6580b57cec5SDimitry Andric         Kind = AK_Get;
6590b57cec5SDimitry Andric       } else if (KindStr == "put") {
6600b57cec5SDimitry Andric         Kind = AK_Put;
6610b57cec5SDimitry Andric 
6620b57cec5SDimitry Andric         // Recover from the common mistake of using 'set' instead of 'put'.
6630b57cec5SDimitry Andric       } else if (KindStr == "set") {
6640b57cec5SDimitry Andric         Diag(KindLoc, diag::err_ms_property_has_set_accessor)
6650b57cec5SDimitry Andric             << FixItHint::CreateReplacement(KindLoc, "put");
6660b57cec5SDimitry Andric         Kind = AK_Put;
6670b57cec5SDimitry Andric 
6680b57cec5SDimitry Andric         // Handle the mistake of forgetting the accessor kind by skipping
6690b57cec5SDimitry Andric         // this accessor.
6700b57cec5SDimitry Andric       } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
6710b57cec5SDimitry Andric         Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
6720b57cec5SDimitry Andric         ConsumeToken();
6730b57cec5SDimitry Andric         HasInvalidAccessor = true;
6740b57cec5SDimitry Andric         goto next_property_accessor;
6750b57cec5SDimitry Andric 
6760b57cec5SDimitry Andric         // Otherwise, complain about the unknown accessor kind.
6770b57cec5SDimitry Andric       } else {
6780b57cec5SDimitry Andric         Diag(KindLoc, diag::err_ms_property_unknown_accessor);
6790b57cec5SDimitry Andric         HasInvalidAccessor = true;
6800b57cec5SDimitry Andric         Kind = AK_Invalid;
6810b57cec5SDimitry Andric 
6820b57cec5SDimitry Andric         // Try to keep parsing unless it doesn't look like an accessor spec.
6830b57cec5SDimitry Andric         if (!NextToken().is(tok::equal))
6840b57cec5SDimitry Andric           break;
6850b57cec5SDimitry Andric       }
6860b57cec5SDimitry Andric 
6870b57cec5SDimitry Andric       // Consume the identifier.
6880b57cec5SDimitry Andric       ConsumeToken();
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric       // Consume the '='.
6910b57cec5SDimitry Andric       if (!TryConsumeToken(tok::equal)) {
6920b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
6930b57cec5SDimitry Andric             << KindStr;
6940b57cec5SDimitry Andric         break;
6950b57cec5SDimitry Andric       }
6960b57cec5SDimitry Andric 
6970b57cec5SDimitry Andric       // Expect the method name.
6980b57cec5SDimitry Andric       if (!Tok.is(tok::identifier)) {
6990b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
7000b57cec5SDimitry Andric         break;
7010b57cec5SDimitry Andric       }
7020b57cec5SDimitry Andric 
7030b57cec5SDimitry Andric       if (Kind == AK_Invalid) {
7040b57cec5SDimitry Andric         // Just drop invalid accessors.
7050b57cec5SDimitry Andric       } else if (AccessorNames[Kind] != nullptr) {
7060b57cec5SDimitry Andric         // Complain about the repeated accessor, ignore it, and keep parsing.
7070b57cec5SDimitry Andric         Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
7080b57cec5SDimitry Andric       } else {
7090b57cec5SDimitry Andric         AccessorNames[Kind] = Tok.getIdentifierInfo();
7100b57cec5SDimitry Andric       }
7110b57cec5SDimitry Andric       ConsumeToken();
7120b57cec5SDimitry Andric 
7130b57cec5SDimitry Andric     next_property_accessor:
7140b57cec5SDimitry Andric       // Keep processing accessors until we run out.
7150b57cec5SDimitry Andric       if (TryConsumeToken(tok::comma))
7160b57cec5SDimitry Andric         continue;
7170b57cec5SDimitry Andric 
7180b57cec5SDimitry Andric       // If we run into the ')', stop without consuming it.
7190b57cec5SDimitry Andric       if (Tok.is(tok::r_paren))
7200b57cec5SDimitry Andric         break;
7210b57cec5SDimitry Andric 
7220b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
7230b57cec5SDimitry Andric       break;
7240b57cec5SDimitry Andric     }
7250b57cec5SDimitry Andric 
7260b57cec5SDimitry Andric     // Only add the property attribute if it was well-formed.
7270b57cec5SDimitry Andric     if (!HasInvalidAccessor)
7280b57cec5SDimitry Andric       Attrs.addNewPropertyAttr(AttrName, AttrNameLoc, nullptr, SourceLocation(),
7290b57cec5SDimitry Andric                                AccessorNames[AK_Get], AccessorNames[AK_Put],
730*06c3fb27SDimitry Andric                                ParsedAttr::Form::Declspec());
7310b57cec5SDimitry Andric     T.skipToEnd();
7320b57cec5SDimitry Andric     return !HasInvalidAccessor;
7330b57cec5SDimitry Andric   }
7340b57cec5SDimitry Andric 
7350b57cec5SDimitry Andric   unsigned NumArgs =
7360b57cec5SDimitry Andric       ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, nullptr, nullptr,
737*06c3fb27SDimitry Andric                                SourceLocation(), ParsedAttr::Form::Declspec());
7380b57cec5SDimitry Andric 
7390b57cec5SDimitry Andric   // If this attribute's args were parsed, and it was expected to have
7400b57cec5SDimitry Andric   // arguments but none were provided, emit a diagnostic.
74181ad6265SDimitry Andric   if (ExistingAttrs < Attrs.size() && Attrs.back().getMaxArgs() && !NumArgs) {
7420b57cec5SDimitry Andric     Diag(OpenParenLoc, diag::err_attribute_requires_arguments) << AttrName;
7430b57cec5SDimitry Andric     return false;
7440b57cec5SDimitry Andric   }
7450b57cec5SDimitry Andric   return true;
7460b57cec5SDimitry Andric }
7470b57cec5SDimitry Andric 
7480b57cec5SDimitry Andric /// [MS] decl-specifier:
7490b57cec5SDimitry Andric ///             __declspec ( extended-decl-modifier-seq )
7500b57cec5SDimitry Andric ///
7510b57cec5SDimitry Andric /// [MS] extended-decl-modifier-seq:
7520b57cec5SDimitry Andric ///             extended-decl-modifier[opt]
7530b57cec5SDimitry Andric ///             extended-decl-modifier extended-decl-modifier-seq
75481ad6265SDimitry Andric void Parser::ParseMicrosoftDeclSpecs(ParsedAttributes &Attrs) {
7550b57cec5SDimitry Andric   assert(getLangOpts().DeclSpecKeyword && "__declspec keyword is not enabled");
7560b57cec5SDimitry Andric   assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
7570b57cec5SDimitry Andric 
75881ad6265SDimitry Andric   SourceLocation StartLoc = Tok.getLocation();
75981ad6265SDimitry Andric   SourceLocation EndLoc = StartLoc;
76081ad6265SDimitry Andric 
7610b57cec5SDimitry Andric   while (Tok.is(tok::kw___declspec)) {
7620b57cec5SDimitry Andric     ConsumeToken();
7630b57cec5SDimitry Andric     BalancedDelimiterTracker T(*this, tok::l_paren);
7640b57cec5SDimitry Andric     if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
7650b57cec5SDimitry Andric                            tok::r_paren))
7660b57cec5SDimitry Andric       return;
7670b57cec5SDimitry Andric 
7680b57cec5SDimitry Andric     // An empty declspec is perfectly legal and should not warn.  Additionally,
7690b57cec5SDimitry Andric     // you can specify multiple attributes per declspec.
7700b57cec5SDimitry Andric     while (Tok.isNot(tok::r_paren)) {
7710b57cec5SDimitry Andric       // Attribute not present.
7720b57cec5SDimitry Andric       if (TryConsumeToken(tok::comma))
7730b57cec5SDimitry Andric         continue;
7740b57cec5SDimitry Andric 
775349cc55cSDimitry Andric       if (Tok.is(tok::code_completion)) {
776349cc55cSDimitry Andric         cutOffParsing();
777349cc55cSDimitry Andric         Actions.CodeCompleteAttribute(AttributeCommonInfo::AS_Declspec);
778349cc55cSDimitry Andric         return;
779349cc55cSDimitry Andric       }
780349cc55cSDimitry Andric 
7810b57cec5SDimitry Andric       // We expect either a well-known identifier or a generic string.  Anything
7820b57cec5SDimitry Andric       // else is a malformed declspec.
7830b57cec5SDimitry Andric       bool IsString = Tok.getKind() == tok::string_literal;
7840b57cec5SDimitry Andric       if (!IsString && Tok.getKind() != tok::identifier &&
7850b57cec5SDimitry Andric           Tok.getKind() != tok::kw_restrict) {
7860b57cec5SDimitry Andric         Diag(Tok, diag::err_ms_declspec_type);
7870b57cec5SDimitry Andric         T.skipToEnd();
7880b57cec5SDimitry Andric         return;
7890b57cec5SDimitry Andric       }
7900b57cec5SDimitry Andric 
7910b57cec5SDimitry Andric       IdentifierInfo *AttrName;
7920b57cec5SDimitry Andric       SourceLocation AttrNameLoc;
7930b57cec5SDimitry Andric       if (IsString) {
7940b57cec5SDimitry Andric         SmallString<8> StrBuffer;
7950b57cec5SDimitry Andric         bool Invalid = false;
7960b57cec5SDimitry Andric         StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
7970b57cec5SDimitry Andric         if (Invalid) {
7980b57cec5SDimitry Andric           T.skipToEnd();
7990b57cec5SDimitry Andric           return;
8000b57cec5SDimitry Andric         }
8010b57cec5SDimitry Andric         AttrName = PP.getIdentifierInfo(Str);
8020b57cec5SDimitry Andric         AttrNameLoc = ConsumeStringToken();
8030b57cec5SDimitry Andric       } else {
8040b57cec5SDimitry Andric         AttrName = Tok.getIdentifierInfo();
8050b57cec5SDimitry Andric         AttrNameLoc = ConsumeToken();
8060b57cec5SDimitry Andric       }
8070b57cec5SDimitry Andric 
8080b57cec5SDimitry Andric       bool AttrHandled = false;
8090b57cec5SDimitry Andric 
8100b57cec5SDimitry Andric       // Parse attribute arguments.
8110b57cec5SDimitry Andric       if (Tok.is(tok::l_paren))
8120b57cec5SDimitry Andric         AttrHandled = ParseMicrosoftDeclSpecArgs(AttrName, AttrNameLoc, Attrs);
8130b57cec5SDimitry Andric       else if (AttrName->getName() == "property")
8140b57cec5SDimitry Andric         // The property attribute must have an argument list.
8150b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_expected_lparen_after)
8160b57cec5SDimitry Andric             << AttrName->getName();
8170b57cec5SDimitry Andric 
8180b57cec5SDimitry Andric       if (!AttrHandled)
8190b57cec5SDimitry Andric         Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
820*06c3fb27SDimitry Andric                      ParsedAttr::Form::Declspec());
8210b57cec5SDimitry Andric     }
8220b57cec5SDimitry Andric     T.consumeClose();
82381ad6265SDimitry Andric     EndLoc = T.getCloseLocation();
8240b57cec5SDimitry Andric   }
82581ad6265SDimitry Andric 
82681ad6265SDimitry Andric   Attrs.Range = SourceRange(StartLoc, EndLoc);
8270b57cec5SDimitry Andric }
8280b57cec5SDimitry Andric 
8290b57cec5SDimitry Andric void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
8300b57cec5SDimitry Andric   // Treat these like attributes
8310b57cec5SDimitry Andric   while (true) {
832*06c3fb27SDimitry Andric     auto Kind = Tok.getKind();
833*06c3fb27SDimitry Andric     switch (Kind) {
8340b57cec5SDimitry Andric     case tok::kw___fastcall:
8350b57cec5SDimitry Andric     case tok::kw___stdcall:
8360b57cec5SDimitry Andric     case tok::kw___thiscall:
8370b57cec5SDimitry Andric     case tok::kw___regcall:
8380b57cec5SDimitry Andric     case tok::kw___cdecl:
8390b57cec5SDimitry Andric     case tok::kw___vectorcall:
8400b57cec5SDimitry Andric     case tok::kw___ptr64:
8410b57cec5SDimitry Andric     case tok::kw___w64:
8420b57cec5SDimitry Andric     case tok::kw___ptr32:
8430b57cec5SDimitry Andric     case tok::kw___sptr:
8440b57cec5SDimitry Andric     case tok::kw___uptr: {
8450b57cec5SDimitry Andric       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
8460b57cec5SDimitry Andric       SourceLocation AttrNameLoc = ConsumeToken();
8470b57cec5SDimitry Andric       attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
848*06c3fb27SDimitry Andric                    Kind);
8490b57cec5SDimitry Andric       break;
8500b57cec5SDimitry Andric     }
8510b57cec5SDimitry Andric     default:
8520b57cec5SDimitry Andric       return;
8530b57cec5SDimitry Andric     }
8540b57cec5SDimitry Andric   }
8550b57cec5SDimitry Andric }
8560b57cec5SDimitry Andric 
857*06c3fb27SDimitry Andric void Parser::ParseWebAssemblyFuncrefTypeAttribute(ParsedAttributes &attrs) {
858*06c3fb27SDimitry Andric   assert(Tok.is(tok::kw___funcref));
859*06c3fb27SDimitry Andric   SourceLocation StartLoc = Tok.getLocation();
860*06c3fb27SDimitry Andric   if (!getTargetInfo().getTriple().isWasm()) {
861*06c3fb27SDimitry Andric     ConsumeToken();
862*06c3fb27SDimitry Andric     Diag(StartLoc, diag::err_wasm_funcref_not_wasm);
863*06c3fb27SDimitry Andric     return;
864*06c3fb27SDimitry Andric   }
865*06c3fb27SDimitry Andric 
866*06c3fb27SDimitry Andric   IdentifierInfo *AttrName = Tok.getIdentifierInfo();
867*06c3fb27SDimitry Andric   SourceLocation AttrNameLoc = ConsumeToken();
868*06c3fb27SDimitry Andric   attrs.addNew(AttrName, AttrNameLoc, /*ScopeName=*/nullptr,
869*06c3fb27SDimitry Andric                /*ScopeLoc=*/SourceLocation{}, /*Args=*/nullptr, /*numArgs=*/0,
870*06c3fb27SDimitry Andric                tok::kw___funcref);
871*06c3fb27SDimitry Andric }
872*06c3fb27SDimitry Andric 
8730b57cec5SDimitry Andric void Parser::DiagnoseAndSkipExtendedMicrosoftTypeAttributes() {
8740b57cec5SDimitry Andric   SourceLocation StartLoc = Tok.getLocation();
8750b57cec5SDimitry Andric   SourceLocation EndLoc = SkipExtendedMicrosoftTypeAttributes();
8760b57cec5SDimitry Andric 
8770b57cec5SDimitry Andric   if (EndLoc.isValid()) {
8780b57cec5SDimitry Andric     SourceRange Range(StartLoc, EndLoc);
8790b57cec5SDimitry Andric     Diag(StartLoc, diag::warn_microsoft_qualifiers_ignored) << Range;
8800b57cec5SDimitry Andric   }
8810b57cec5SDimitry Andric }
8820b57cec5SDimitry Andric 
8830b57cec5SDimitry Andric SourceLocation Parser::SkipExtendedMicrosoftTypeAttributes() {
8840b57cec5SDimitry Andric   SourceLocation EndLoc;
8850b57cec5SDimitry Andric 
8860b57cec5SDimitry Andric   while (true) {
8870b57cec5SDimitry Andric     switch (Tok.getKind()) {
8880b57cec5SDimitry Andric     case tok::kw_const:
8890b57cec5SDimitry Andric     case tok::kw_volatile:
8900b57cec5SDimitry Andric     case tok::kw___fastcall:
8910b57cec5SDimitry Andric     case tok::kw___stdcall:
8920b57cec5SDimitry Andric     case tok::kw___thiscall:
8930b57cec5SDimitry Andric     case tok::kw___cdecl:
8940b57cec5SDimitry Andric     case tok::kw___vectorcall:
8950b57cec5SDimitry Andric     case tok::kw___ptr32:
8960b57cec5SDimitry Andric     case tok::kw___ptr64:
8970b57cec5SDimitry Andric     case tok::kw___w64:
8980b57cec5SDimitry Andric     case tok::kw___unaligned:
8990b57cec5SDimitry Andric     case tok::kw___sptr:
9000b57cec5SDimitry Andric     case tok::kw___uptr:
9010b57cec5SDimitry Andric       EndLoc = ConsumeToken();
9020b57cec5SDimitry Andric       break;
9030b57cec5SDimitry Andric     default:
9040b57cec5SDimitry Andric       return EndLoc;
9050b57cec5SDimitry Andric     }
9060b57cec5SDimitry Andric   }
9070b57cec5SDimitry Andric }
9080b57cec5SDimitry Andric 
9090b57cec5SDimitry Andric void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
9100b57cec5SDimitry Andric   // Treat these like attributes
9110b57cec5SDimitry Andric   while (Tok.is(tok::kw___pascal)) {
9120b57cec5SDimitry Andric     IdentifierInfo *AttrName = Tok.getIdentifierInfo();
9130b57cec5SDimitry Andric     SourceLocation AttrNameLoc = ConsumeToken();
9140b57cec5SDimitry Andric     attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
915*06c3fb27SDimitry Andric                  tok::kw___pascal);
9160b57cec5SDimitry Andric   }
9170b57cec5SDimitry Andric }
9180b57cec5SDimitry Andric 
9190b57cec5SDimitry Andric void Parser::ParseOpenCLKernelAttributes(ParsedAttributes &attrs) {
9200b57cec5SDimitry Andric   // Treat these like attributes
9210b57cec5SDimitry Andric   while (Tok.is(tok::kw___kernel)) {
9220b57cec5SDimitry Andric     IdentifierInfo *AttrName = Tok.getIdentifierInfo();
9230b57cec5SDimitry Andric     SourceLocation AttrNameLoc = ConsumeToken();
9240b57cec5SDimitry Andric     attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
925*06c3fb27SDimitry Andric                  tok::kw___kernel);
9260b57cec5SDimitry Andric   }
9270b57cec5SDimitry Andric }
9280b57cec5SDimitry Andric 
92981ad6265SDimitry Andric void Parser::ParseCUDAFunctionAttributes(ParsedAttributes &attrs) {
93081ad6265SDimitry Andric   while (Tok.is(tok::kw___noinline__)) {
93181ad6265SDimitry Andric     IdentifierInfo *AttrName = Tok.getIdentifierInfo();
93281ad6265SDimitry Andric     SourceLocation AttrNameLoc = ConsumeToken();
93381ad6265SDimitry Andric     attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
934*06c3fb27SDimitry Andric                  tok::kw___noinline__);
93581ad6265SDimitry Andric   }
93681ad6265SDimitry Andric }
93781ad6265SDimitry Andric 
9380b57cec5SDimitry Andric void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) {
9390b57cec5SDimitry Andric   IdentifierInfo *AttrName = Tok.getIdentifierInfo();
9400b57cec5SDimitry Andric   SourceLocation AttrNameLoc = Tok.getLocation();
9410b57cec5SDimitry Andric   Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
942*06c3fb27SDimitry Andric                Tok.getKind());
9430b57cec5SDimitry Andric }
9440b57cec5SDimitry Andric 
945bdd1243dSDimitry Andric bool Parser::isHLSLQualifier(const Token &Tok) const {
946bdd1243dSDimitry Andric   return Tok.is(tok::kw_groupshared);
947bdd1243dSDimitry Andric }
948bdd1243dSDimitry Andric 
949bdd1243dSDimitry Andric void Parser::ParseHLSLQualifiers(ParsedAttributes &Attrs) {
950bdd1243dSDimitry Andric   IdentifierInfo *AttrName = Tok.getIdentifierInfo();
951*06c3fb27SDimitry Andric   auto Kind = Tok.getKind();
952bdd1243dSDimitry Andric   SourceLocation AttrNameLoc = ConsumeToken();
953*06c3fb27SDimitry Andric   Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0, Kind);
954bdd1243dSDimitry Andric }
955bdd1243dSDimitry Andric 
9560b57cec5SDimitry Andric void Parser::ParseNullabilityTypeSpecifiers(ParsedAttributes &attrs) {
9570b57cec5SDimitry Andric   // Treat these like attributes, even though they're type specifiers.
9580b57cec5SDimitry Andric   while (true) {
959*06c3fb27SDimitry Andric     auto Kind = Tok.getKind();
960*06c3fb27SDimitry Andric     switch (Kind) {
9610b57cec5SDimitry Andric     case tok::kw__Nonnull:
9620b57cec5SDimitry Andric     case tok::kw__Nullable:
963e8d8bef9SDimitry Andric     case tok::kw__Nullable_result:
9640b57cec5SDimitry Andric     case tok::kw__Null_unspecified: {
9650b57cec5SDimitry Andric       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
9660b57cec5SDimitry Andric       SourceLocation AttrNameLoc = ConsumeToken();
9670b57cec5SDimitry Andric       if (!getLangOpts().ObjC)
9680b57cec5SDimitry Andric         Diag(AttrNameLoc, diag::ext_nullability)
9690b57cec5SDimitry Andric           << AttrName;
9700b57cec5SDimitry Andric       attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
971*06c3fb27SDimitry Andric                    Kind);
9720b57cec5SDimitry Andric       break;
9730b57cec5SDimitry Andric     }
9740b57cec5SDimitry Andric     default:
9750b57cec5SDimitry Andric       return;
9760b57cec5SDimitry Andric     }
9770b57cec5SDimitry Andric   }
9780b57cec5SDimitry Andric }
9790b57cec5SDimitry Andric 
9800b57cec5SDimitry Andric static bool VersionNumberSeparator(const char Separator) {
9810b57cec5SDimitry Andric   return (Separator == '.' || Separator == '_');
9820b57cec5SDimitry Andric }
9830b57cec5SDimitry Andric 
9840b57cec5SDimitry Andric /// Parse a version number.
9850b57cec5SDimitry Andric ///
9860b57cec5SDimitry Andric /// version:
9870b57cec5SDimitry Andric ///   simple-integer
9880b57cec5SDimitry Andric ///   simple-integer '.' simple-integer
9890b57cec5SDimitry Andric ///   simple-integer '_' simple-integer
9900b57cec5SDimitry Andric ///   simple-integer '.' simple-integer '.' simple-integer
9910b57cec5SDimitry Andric ///   simple-integer '_' simple-integer '_' simple-integer
9920b57cec5SDimitry Andric VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
9930b57cec5SDimitry Andric   Range = SourceRange(Tok.getLocation(), Tok.getEndLoc());
9940b57cec5SDimitry Andric 
9950b57cec5SDimitry Andric   if (!Tok.is(tok::numeric_constant)) {
9960b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
9970b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
9980b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
9990b57cec5SDimitry Andric     return VersionTuple();
10000b57cec5SDimitry Andric   }
10010b57cec5SDimitry Andric 
10020b57cec5SDimitry Andric   // Parse the major (and possibly minor and subminor) versions, which
10030b57cec5SDimitry Andric   // are stored in the numeric constant. We utilize a quirk of the
10040b57cec5SDimitry Andric   // lexer, which is that it handles something like 1.2.3 as a single
10050b57cec5SDimitry Andric   // numeric constant, rather than two separate tokens.
10060b57cec5SDimitry Andric   SmallString<512> Buffer;
10070b57cec5SDimitry Andric   Buffer.resize(Tok.getLength()+1);
10080b57cec5SDimitry Andric   const char *ThisTokBegin = &Buffer[0];
10090b57cec5SDimitry Andric 
10100b57cec5SDimitry Andric   // Get the spelling of the token, which eliminates trigraphs, etc.
10110b57cec5SDimitry Andric   bool Invalid = false;
10120b57cec5SDimitry Andric   unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
10130b57cec5SDimitry Andric   if (Invalid)
10140b57cec5SDimitry Andric     return VersionTuple();
10150b57cec5SDimitry Andric 
10160b57cec5SDimitry Andric   // Parse the major version.
10170b57cec5SDimitry Andric   unsigned AfterMajor = 0;
10180b57cec5SDimitry Andric   unsigned Major = 0;
10190b57cec5SDimitry Andric   while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
10200b57cec5SDimitry Andric     Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
10210b57cec5SDimitry Andric     ++AfterMajor;
10220b57cec5SDimitry Andric   }
10230b57cec5SDimitry Andric 
10240b57cec5SDimitry Andric   if (AfterMajor == 0) {
10250b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
10260b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
10270b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
10280b57cec5SDimitry Andric     return VersionTuple();
10290b57cec5SDimitry Andric   }
10300b57cec5SDimitry Andric 
10310b57cec5SDimitry Andric   if (AfterMajor == ActualLength) {
10320b57cec5SDimitry Andric     ConsumeToken();
10330b57cec5SDimitry Andric 
10340b57cec5SDimitry Andric     // We only had a single version component.
10350b57cec5SDimitry Andric     if (Major == 0) {
10360b57cec5SDimitry Andric       Diag(Tok, diag::err_zero_version);
10370b57cec5SDimitry Andric       return VersionTuple();
10380b57cec5SDimitry Andric     }
10390b57cec5SDimitry Andric 
10400b57cec5SDimitry Andric     return VersionTuple(Major);
10410b57cec5SDimitry Andric   }
10420b57cec5SDimitry Andric 
10430b57cec5SDimitry Andric   const char AfterMajorSeparator = ThisTokBegin[AfterMajor];
10440b57cec5SDimitry Andric   if (!VersionNumberSeparator(AfterMajorSeparator)
10450b57cec5SDimitry Andric       || (AfterMajor + 1 == ActualLength)) {
10460b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
10470b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
10480b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
10490b57cec5SDimitry Andric     return VersionTuple();
10500b57cec5SDimitry Andric   }
10510b57cec5SDimitry Andric 
10520b57cec5SDimitry Andric   // Parse the minor version.
10530b57cec5SDimitry Andric   unsigned AfterMinor = AfterMajor + 1;
10540b57cec5SDimitry Andric   unsigned Minor = 0;
10550b57cec5SDimitry Andric   while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
10560b57cec5SDimitry Andric     Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
10570b57cec5SDimitry Andric     ++AfterMinor;
10580b57cec5SDimitry Andric   }
10590b57cec5SDimitry Andric 
10600b57cec5SDimitry Andric   if (AfterMinor == ActualLength) {
10610b57cec5SDimitry Andric     ConsumeToken();
10620b57cec5SDimitry Andric 
10630b57cec5SDimitry Andric     // We had major.minor.
10640b57cec5SDimitry Andric     if (Major == 0 && Minor == 0) {
10650b57cec5SDimitry Andric       Diag(Tok, diag::err_zero_version);
10660b57cec5SDimitry Andric       return VersionTuple();
10670b57cec5SDimitry Andric     }
10680b57cec5SDimitry Andric 
10690b57cec5SDimitry Andric     return VersionTuple(Major, Minor);
10700b57cec5SDimitry Andric   }
10710b57cec5SDimitry Andric 
10720b57cec5SDimitry Andric   const char AfterMinorSeparator = ThisTokBegin[AfterMinor];
10730b57cec5SDimitry Andric   // If what follows is not a '.' or '_', we have a problem.
10740b57cec5SDimitry Andric   if (!VersionNumberSeparator(AfterMinorSeparator)) {
10750b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
10760b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
10770b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
10780b57cec5SDimitry Andric     return VersionTuple();
10790b57cec5SDimitry Andric   }
10800b57cec5SDimitry Andric 
10810b57cec5SDimitry Andric   // Warn if separators, be it '.' or '_', do not match.
10820b57cec5SDimitry Andric   if (AfterMajorSeparator != AfterMinorSeparator)
10830b57cec5SDimitry Andric     Diag(Tok, diag::warn_expected_consistent_version_separator);
10840b57cec5SDimitry Andric 
10850b57cec5SDimitry Andric   // Parse the subminor version.
10860b57cec5SDimitry Andric   unsigned AfterSubminor = AfterMinor + 1;
10870b57cec5SDimitry Andric   unsigned Subminor = 0;
10880b57cec5SDimitry Andric   while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
10890b57cec5SDimitry Andric     Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
10900b57cec5SDimitry Andric     ++AfterSubminor;
10910b57cec5SDimitry Andric   }
10920b57cec5SDimitry Andric 
10930b57cec5SDimitry Andric   if (AfterSubminor != ActualLength) {
10940b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
10950b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
10960b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
10970b57cec5SDimitry Andric     return VersionTuple();
10980b57cec5SDimitry Andric   }
10990b57cec5SDimitry Andric   ConsumeToken();
11000b57cec5SDimitry Andric   return VersionTuple(Major, Minor, Subminor);
11010b57cec5SDimitry Andric }
11020b57cec5SDimitry Andric 
11030b57cec5SDimitry Andric /// Parse the contents of the "availability" attribute.
11040b57cec5SDimitry Andric ///
11050b57cec5SDimitry Andric /// availability-attribute:
11060b57cec5SDimitry Andric ///   'availability' '(' platform ',' opt-strict version-arg-list,
11070b57cec5SDimitry Andric ///                      opt-replacement, opt-message')'
11080b57cec5SDimitry Andric ///
11090b57cec5SDimitry Andric /// platform:
11100b57cec5SDimitry Andric ///   identifier
11110b57cec5SDimitry Andric ///
11120b57cec5SDimitry Andric /// opt-strict:
11130b57cec5SDimitry Andric ///   'strict' ','
11140b57cec5SDimitry Andric ///
11150b57cec5SDimitry Andric /// version-arg-list:
11160b57cec5SDimitry Andric ///   version-arg
11170b57cec5SDimitry Andric ///   version-arg ',' version-arg-list
11180b57cec5SDimitry Andric ///
11190b57cec5SDimitry Andric /// version-arg:
11200b57cec5SDimitry Andric ///   'introduced' '=' version
11210b57cec5SDimitry Andric ///   'deprecated' '=' version
11220b57cec5SDimitry Andric ///   'obsoleted' = version
11230b57cec5SDimitry Andric ///   'unavailable'
11240b57cec5SDimitry Andric /// opt-replacement:
11250b57cec5SDimitry Andric ///   'replacement' '=' <string>
11260b57cec5SDimitry Andric /// opt-message:
11270b57cec5SDimitry Andric ///   'message' '=' <string>
1128*06c3fb27SDimitry Andric void Parser::ParseAvailabilityAttribute(
1129*06c3fb27SDimitry Andric     IdentifierInfo &Availability, SourceLocation AvailabilityLoc,
1130*06c3fb27SDimitry Andric     ParsedAttributes &attrs, SourceLocation *endLoc, IdentifierInfo *ScopeName,
1131*06c3fb27SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Form Form) {
11320b57cec5SDimitry Andric   enum { Introduced, Deprecated, Obsoleted, Unknown };
11330b57cec5SDimitry Andric   AvailabilityChange Changes[Unknown];
11340b57cec5SDimitry Andric   ExprResult MessageExpr, ReplacementExpr;
11350b57cec5SDimitry Andric 
11360b57cec5SDimitry Andric   // Opening '('.
11370b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
11380b57cec5SDimitry Andric   if (T.consumeOpen()) {
11390b57cec5SDimitry Andric     Diag(Tok, diag::err_expected) << tok::l_paren;
11400b57cec5SDimitry Andric     return;
11410b57cec5SDimitry Andric   }
11420b57cec5SDimitry Andric 
11430b57cec5SDimitry Andric   // Parse the platform name.
11440b57cec5SDimitry Andric   if (Tok.isNot(tok::identifier)) {
11450b57cec5SDimitry Andric     Diag(Tok, diag::err_availability_expected_platform);
11460b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
11470b57cec5SDimitry Andric     return;
11480b57cec5SDimitry Andric   }
11490b57cec5SDimitry Andric   IdentifierLoc *Platform = ParseIdentifierLoc();
11500b57cec5SDimitry Andric   if (const IdentifierInfo *const Ident = Platform->Ident) {
11510b57cec5SDimitry Andric     // Canonicalize platform name from "macosx" to "macos".
11520b57cec5SDimitry Andric     if (Ident->getName() == "macosx")
11530b57cec5SDimitry Andric       Platform->Ident = PP.getIdentifierInfo("macos");
11540b57cec5SDimitry Andric     // Canonicalize platform name from "macosx_app_extension" to
11550b57cec5SDimitry Andric     // "macos_app_extension".
11560b57cec5SDimitry Andric     else if (Ident->getName() == "macosx_app_extension")
11570b57cec5SDimitry Andric       Platform->Ident = PP.getIdentifierInfo("macos_app_extension");
11580b57cec5SDimitry Andric     else
11590b57cec5SDimitry Andric       Platform->Ident = PP.getIdentifierInfo(
11600b57cec5SDimitry Andric           AvailabilityAttr::canonicalizePlatformName(Ident->getName()));
11610b57cec5SDimitry Andric   }
11620b57cec5SDimitry Andric 
11630b57cec5SDimitry Andric   // Parse the ',' following the platform name.
11640b57cec5SDimitry Andric   if (ExpectAndConsume(tok::comma)) {
11650b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
11660b57cec5SDimitry Andric     return;
11670b57cec5SDimitry Andric   }
11680b57cec5SDimitry Andric 
11690b57cec5SDimitry Andric   // If we haven't grabbed the pointers for the identifiers
11700b57cec5SDimitry Andric   // "introduced", "deprecated", and "obsoleted", do so now.
11710b57cec5SDimitry Andric   if (!Ident_introduced) {
11720b57cec5SDimitry Andric     Ident_introduced = PP.getIdentifierInfo("introduced");
11730b57cec5SDimitry Andric     Ident_deprecated = PP.getIdentifierInfo("deprecated");
11740b57cec5SDimitry Andric     Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
11750b57cec5SDimitry Andric     Ident_unavailable = PP.getIdentifierInfo("unavailable");
11760b57cec5SDimitry Andric     Ident_message = PP.getIdentifierInfo("message");
11770b57cec5SDimitry Andric     Ident_strict = PP.getIdentifierInfo("strict");
11780b57cec5SDimitry Andric     Ident_replacement = PP.getIdentifierInfo("replacement");
11790b57cec5SDimitry Andric   }
11800b57cec5SDimitry Andric 
11810b57cec5SDimitry Andric   // Parse the optional "strict", the optional "replacement" and the set of
11820b57cec5SDimitry Andric   // introductions/deprecations/removals.
11830b57cec5SDimitry Andric   SourceLocation UnavailableLoc, StrictLoc;
11840b57cec5SDimitry Andric   do {
11850b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
11860b57cec5SDimitry Andric       Diag(Tok, diag::err_availability_expected_change);
11870b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
11880b57cec5SDimitry Andric       return;
11890b57cec5SDimitry Andric     }
11900b57cec5SDimitry Andric     IdentifierInfo *Keyword = Tok.getIdentifierInfo();
11910b57cec5SDimitry Andric     SourceLocation KeywordLoc = ConsumeToken();
11920b57cec5SDimitry Andric 
11930b57cec5SDimitry Andric     if (Keyword == Ident_strict) {
11940b57cec5SDimitry Andric       if (StrictLoc.isValid()) {
11950b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_availability_redundant)
11960b57cec5SDimitry Andric           << Keyword << SourceRange(StrictLoc);
11970b57cec5SDimitry Andric       }
11980b57cec5SDimitry Andric       StrictLoc = KeywordLoc;
11990b57cec5SDimitry Andric       continue;
12000b57cec5SDimitry Andric     }
12010b57cec5SDimitry Andric 
12020b57cec5SDimitry Andric     if (Keyword == Ident_unavailable) {
12030b57cec5SDimitry Andric       if (UnavailableLoc.isValid()) {
12040b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_availability_redundant)
12050b57cec5SDimitry Andric           << Keyword << SourceRange(UnavailableLoc);
12060b57cec5SDimitry Andric       }
12070b57cec5SDimitry Andric       UnavailableLoc = KeywordLoc;
12080b57cec5SDimitry Andric       continue;
12090b57cec5SDimitry Andric     }
12100b57cec5SDimitry Andric 
12110b57cec5SDimitry Andric     if (Keyword == Ident_deprecated && Platform->Ident &&
12120b57cec5SDimitry Andric         Platform->Ident->isStr("swift")) {
12130b57cec5SDimitry Andric       // For swift, we deprecate for all versions.
12140b57cec5SDimitry Andric       if (Changes[Deprecated].KeywordLoc.isValid()) {
12150b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_availability_redundant)
12160b57cec5SDimitry Andric           << Keyword
12170b57cec5SDimitry Andric           << SourceRange(Changes[Deprecated].KeywordLoc);
12180b57cec5SDimitry Andric       }
12190b57cec5SDimitry Andric 
12200b57cec5SDimitry Andric       Changes[Deprecated].KeywordLoc = KeywordLoc;
12210b57cec5SDimitry Andric       // Use a fake version here.
12220b57cec5SDimitry Andric       Changes[Deprecated].Version = VersionTuple(1);
12230b57cec5SDimitry Andric       continue;
12240b57cec5SDimitry Andric     }
12250b57cec5SDimitry Andric 
12260b57cec5SDimitry Andric     if (Tok.isNot(tok::equal)) {
12270b57cec5SDimitry Andric       Diag(Tok, diag::err_expected_after) << Keyword << tok::equal;
12280b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
12290b57cec5SDimitry Andric       return;
12300b57cec5SDimitry Andric     }
12310b57cec5SDimitry Andric     ConsumeToken();
12320b57cec5SDimitry Andric     if (Keyword == Ident_message || Keyword == Ident_replacement) {
12330b57cec5SDimitry Andric       if (Tok.isNot(tok::string_literal)) {
12340b57cec5SDimitry Andric         Diag(Tok, diag::err_expected_string_literal)
12350b57cec5SDimitry Andric           << /*Source='availability attribute'*/2;
12360b57cec5SDimitry Andric         SkipUntil(tok::r_paren, StopAtSemi);
12370b57cec5SDimitry Andric         return;
12380b57cec5SDimitry Andric       }
12390b57cec5SDimitry Andric       if (Keyword == Ident_message)
12400b57cec5SDimitry Andric         MessageExpr = ParseStringLiteralExpression();
12410b57cec5SDimitry Andric       else
12420b57cec5SDimitry Andric         ReplacementExpr = ParseStringLiteralExpression();
12430b57cec5SDimitry Andric       // Also reject wide string literals.
12440b57cec5SDimitry Andric       if (StringLiteral *MessageStringLiteral =
12450b57cec5SDimitry Andric               cast_or_null<StringLiteral>(MessageExpr.get())) {
124681ad6265SDimitry Andric         if (!MessageStringLiteral->isOrdinary()) {
12470b57cec5SDimitry Andric           Diag(MessageStringLiteral->getSourceRange().getBegin(),
12480b57cec5SDimitry Andric                diag::err_expected_string_literal)
12490b57cec5SDimitry Andric             << /*Source='availability attribute'*/ 2;
12500b57cec5SDimitry Andric           SkipUntil(tok::r_paren, StopAtSemi);
12510b57cec5SDimitry Andric           return;
12520b57cec5SDimitry Andric         }
12530b57cec5SDimitry Andric       }
12540b57cec5SDimitry Andric       if (Keyword == Ident_message)
12550b57cec5SDimitry Andric         break;
12560b57cec5SDimitry Andric       else
12570b57cec5SDimitry Andric         continue;
12580b57cec5SDimitry Andric     }
12590b57cec5SDimitry Andric 
12600b57cec5SDimitry Andric     // Special handling of 'NA' only when applied to introduced or
12610b57cec5SDimitry Andric     // deprecated.
12620b57cec5SDimitry Andric     if ((Keyword == Ident_introduced || Keyword == Ident_deprecated) &&
12630b57cec5SDimitry Andric         Tok.is(tok::identifier)) {
12640b57cec5SDimitry Andric       IdentifierInfo *NA = Tok.getIdentifierInfo();
12650b57cec5SDimitry Andric       if (NA->getName() == "NA") {
12660b57cec5SDimitry Andric         ConsumeToken();
12670b57cec5SDimitry Andric         if (Keyword == Ident_introduced)
12680b57cec5SDimitry Andric           UnavailableLoc = KeywordLoc;
12690b57cec5SDimitry Andric         continue;
12700b57cec5SDimitry Andric       }
12710b57cec5SDimitry Andric     }
12720b57cec5SDimitry Andric 
12730b57cec5SDimitry Andric     SourceRange VersionRange;
12740b57cec5SDimitry Andric     VersionTuple Version = ParseVersionTuple(VersionRange);
12750b57cec5SDimitry Andric 
12760b57cec5SDimitry Andric     if (Version.empty()) {
12770b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
12780b57cec5SDimitry Andric       return;
12790b57cec5SDimitry Andric     }
12800b57cec5SDimitry Andric 
12810b57cec5SDimitry Andric     unsigned Index;
12820b57cec5SDimitry Andric     if (Keyword == Ident_introduced)
12830b57cec5SDimitry Andric       Index = Introduced;
12840b57cec5SDimitry Andric     else if (Keyword == Ident_deprecated)
12850b57cec5SDimitry Andric       Index = Deprecated;
12860b57cec5SDimitry Andric     else if (Keyword == Ident_obsoleted)
12870b57cec5SDimitry Andric       Index = Obsoleted;
12880b57cec5SDimitry Andric     else
12890b57cec5SDimitry Andric       Index = Unknown;
12900b57cec5SDimitry Andric 
12910b57cec5SDimitry Andric     if (Index < Unknown) {
12920b57cec5SDimitry Andric       if (!Changes[Index].KeywordLoc.isInvalid()) {
12930b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_availability_redundant)
12940b57cec5SDimitry Andric           << Keyword
12950b57cec5SDimitry Andric           << SourceRange(Changes[Index].KeywordLoc,
12960b57cec5SDimitry Andric                          Changes[Index].VersionRange.getEnd());
12970b57cec5SDimitry Andric       }
12980b57cec5SDimitry Andric 
12990b57cec5SDimitry Andric       Changes[Index].KeywordLoc = KeywordLoc;
13000b57cec5SDimitry Andric       Changes[Index].Version = Version;
13010b57cec5SDimitry Andric       Changes[Index].VersionRange = VersionRange;
13020b57cec5SDimitry Andric     } else {
13030b57cec5SDimitry Andric       Diag(KeywordLoc, diag::err_availability_unknown_change)
13040b57cec5SDimitry Andric         << Keyword << VersionRange;
13050b57cec5SDimitry Andric     }
13060b57cec5SDimitry Andric 
13070b57cec5SDimitry Andric   } while (TryConsumeToken(tok::comma));
13080b57cec5SDimitry Andric 
13090b57cec5SDimitry Andric   // Closing ')'.
13100b57cec5SDimitry Andric   if (T.consumeClose())
13110b57cec5SDimitry Andric     return;
13120b57cec5SDimitry Andric 
13130b57cec5SDimitry Andric   if (endLoc)
13140b57cec5SDimitry Andric     *endLoc = T.getCloseLocation();
13150b57cec5SDimitry Andric 
13160b57cec5SDimitry Andric   // The 'unavailable' availability cannot be combined with any other
13170b57cec5SDimitry Andric   // availability changes. Make sure that hasn't happened.
13180b57cec5SDimitry Andric   if (UnavailableLoc.isValid()) {
13190b57cec5SDimitry Andric     bool Complained = false;
13200b57cec5SDimitry Andric     for (unsigned Index = Introduced; Index != Unknown; ++Index) {
13210b57cec5SDimitry Andric       if (Changes[Index].KeywordLoc.isValid()) {
13220b57cec5SDimitry Andric         if (!Complained) {
13230b57cec5SDimitry Andric           Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
13240b57cec5SDimitry Andric             << SourceRange(Changes[Index].KeywordLoc,
13250b57cec5SDimitry Andric                            Changes[Index].VersionRange.getEnd());
13260b57cec5SDimitry Andric           Complained = true;
13270b57cec5SDimitry Andric         }
13280b57cec5SDimitry Andric 
13290b57cec5SDimitry Andric         // Clear out the availability.
13300b57cec5SDimitry Andric         Changes[Index] = AvailabilityChange();
13310b57cec5SDimitry Andric       }
13320b57cec5SDimitry Andric     }
13330b57cec5SDimitry Andric   }
13340b57cec5SDimitry Andric 
13350b57cec5SDimitry Andric   // Record this attribute
13360b57cec5SDimitry Andric   attrs.addNew(&Availability,
1337*06c3fb27SDimitry Andric                SourceRange(AvailabilityLoc, T.getCloseLocation()), ScopeName,
1338*06c3fb27SDimitry Andric                ScopeLoc, Platform, Changes[Introduced], Changes[Deprecated],
1339*06c3fb27SDimitry Andric                Changes[Obsoleted], UnavailableLoc, MessageExpr.get(), Form,
1340*06c3fb27SDimitry Andric                StrictLoc, ReplacementExpr.get());
13410b57cec5SDimitry Andric }
13420b57cec5SDimitry Andric 
13430b57cec5SDimitry Andric /// Parse the contents of the "external_source_symbol" attribute.
13440b57cec5SDimitry Andric ///
13450b57cec5SDimitry Andric /// external-source-symbol-attribute:
13460b57cec5SDimitry Andric ///   'external_source_symbol' '(' keyword-arg-list ')'
13470b57cec5SDimitry Andric ///
13480b57cec5SDimitry Andric /// keyword-arg-list:
13490b57cec5SDimitry Andric ///   keyword-arg
13500b57cec5SDimitry Andric ///   keyword-arg ',' keyword-arg-list
13510b57cec5SDimitry Andric ///
13520b57cec5SDimitry Andric /// keyword-arg:
13530b57cec5SDimitry Andric ///   'language' '=' <string>
13540b57cec5SDimitry Andric ///   'defined_in' '=' <string>
1355*06c3fb27SDimitry Andric ///   'USR' '=' <string>
13560b57cec5SDimitry Andric ///   'generated_declaration'
13570b57cec5SDimitry Andric void Parser::ParseExternalSourceSymbolAttribute(
13580b57cec5SDimitry Andric     IdentifierInfo &ExternalSourceSymbol, SourceLocation Loc,
13590b57cec5SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
1360*06c3fb27SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Form Form) {
13610b57cec5SDimitry Andric   // Opening '('.
13620b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
13630b57cec5SDimitry Andric   if (T.expectAndConsume())
13640b57cec5SDimitry Andric     return;
13650b57cec5SDimitry Andric 
13660b57cec5SDimitry Andric   // Initialize the pointers for the keyword identifiers when required.
13670b57cec5SDimitry Andric   if (!Ident_language) {
13680b57cec5SDimitry Andric     Ident_language = PP.getIdentifierInfo("language");
13690b57cec5SDimitry Andric     Ident_defined_in = PP.getIdentifierInfo("defined_in");
13700b57cec5SDimitry Andric     Ident_generated_declaration = PP.getIdentifierInfo("generated_declaration");
1371*06c3fb27SDimitry Andric     Ident_USR = PP.getIdentifierInfo("USR");
13720b57cec5SDimitry Andric   }
13730b57cec5SDimitry Andric 
13740b57cec5SDimitry Andric   ExprResult Language;
13750b57cec5SDimitry Andric   bool HasLanguage = false;
13760b57cec5SDimitry Andric   ExprResult DefinedInExpr;
13770b57cec5SDimitry Andric   bool HasDefinedIn = false;
13780b57cec5SDimitry Andric   IdentifierLoc *GeneratedDeclaration = nullptr;
1379*06c3fb27SDimitry Andric   ExprResult USR;
1380*06c3fb27SDimitry Andric   bool HasUSR = false;
13810b57cec5SDimitry Andric 
13820b57cec5SDimitry Andric   // Parse the language/defined_in/generated_declaration keywords
13830b57cec5SDimitry Andric   do {
13840b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
13850b57cec5SDimitry Andric       Diag(Tok, diag::err_external_source_symbol_expected_keyword);
13860b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
13870b57cec5SDimitry Andric       return;
13880b57cec5SDimitry Andric     }
13890b57cec5SDimitry Andric 
13900b57cec5SDimitry Andric     SourceLocation KeywordLoc = Tok.getLocation();
13910b57cec5SDimitry Andric     IdentifierInfo *Keyword = Tok.getIdentifierInfo();
13920b57cec5SDimitry Andric     if (Keyword == Ident_generated_declaration) {
13930b57cec5SDimitry Andric       if (GeneratedDeclaration) {
13940b57cec5SDimitry Andric         Diag(Tok, diag::err_external_source_symbol_duplicate_clause) << Keyword;
13950b57cec5SDimitry Andric         SkipUntil(tok::r_paren, StopAtSemi);
13960b57cec5SDimitry Andric         return;
13970b57cec5SDimitry Andric       }
13980b57cec5SDimitry Andric       GeneratedDeclaration = ParseIdentifierLoc();
13990b57cec5SDimitry Andric       continue;
14000b57cec5SDimitry Andric     }
14010b57cec5SDimitry Andric 
1402*06c3fb27SDimitry Andric     if (Keyword != Ident_language && Keyword != Ident_defined_in &&
1403*06c3fb27SDimitry Andric         Keyword != Ident_USR) {
14040b57cec5SDimitry Andric       Diag(Tok, diag::err_external_source_symbol_expected_keyword);
14050b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
14060b57cec5SDimitry Andric       return;
14070b57cec5SDimitry Andric     }
14080b57cec5SDimitry Andric 
14090b57cec5SDimitry Andric     ConsumeToken();
14100b57cec5SDimitry Andric     if (ExpectAndConsume(tok::equal, diag::err_expected_after,
14110b57cec5SDimitry Andric                          Keyword->getName())) {
14120b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
14130b57cec5SDimitry Andric       return;
14140b57cec5SDimitry Andric     }
14150b57cec5SDimitry Andric 
1416*06c3fb27SDimitry Andric     bool HadLanguage = HasLanguage, HadDefinedIn = HasDefinedIn,
1417*06c3fb27SDimitry Andric          HadUSR = HasUSR;
14180b57cec5SDimitry Andric     if (Keyword == Ident_language)
14190b57cec5SDimitry Andric       HasLanguage = true;
1420*06c3fb27SDimitry Andric     else if (Keyword == Ident_USR)
1421*06c3fb27SDimitry Andric       HasUSR = true;
14220b57cec5SDimitry Andric     else
14230b57cec5SDimitry Andric       HasDefinedIn = true;
14240b57cec5SDimitry Andric 
14250b57cec5SDimitry Andric     if (Tok.isNot(tok::string_literal)) {
14260b57cec5SDimitry Andric       Diag(Tok, diag::err_expected_string_literal)
14270b57cec5SDimitry Andric           << /*Source='external_source_symbol attribute'*/ 3
1428*06c3fb27SDimitry Andric           << /*language | source container | USR*/ (
1429*06c3fb27SDimitry Andric                  Keyword == Ident_language
1430*06c3fb27SDimitry Andric                      ? 0
1431*06c3fb27SDimitry Andric                      : (Keyword == Ident_defined_in ? 1 : 2));
14320b57cec5SDimitry Andric       SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
14330b57cec5SDimitry Andric       continue;
14340b57cec5SDimitry Andric     }
14350b57cec5SDimitry Andric     if (Keyword == Ident_language) {
14360b57cec5SDimitry Andric       if (HadLanguage) {
14370b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_external_source_symbol_duplicate_clause)
14380b57cec5SDimitry Andric             << Keyword;
14390b57cec5SDimitry Andric         ParseStringLiteralExpression();
14400b57cec5SDimitry Andric         continue;
14410b57cec5SDimitry Andric       }
14420b57cec5SDimitry Andric       Language = ParseStringLiteralExpression();
1443*06c3fb27SDimitry Andric     } else if (Keyword == Ident_USR) {
1444*06c3fb27SDimitry Andric       if (HadUSR) {
1445*06c3fb27SDimitry Andric         Diag(KeywordLoc, diag::err_external_source_symbol_duplicate_clause)
1446*06c3fb27SDimitry Andric             << Keyword;
1447*06c3fb27SDimitry Andric         ParseStringLiteralExpression();
1448*06c3fb27SDimitry Andric         continue;
1449*06c3fb27SDimitry Andric       }
1450*06c3fb27SDimitry Andric       USR = ParseStringLiteralExpression();
14510b57cec5SDimitry Andric     } else {
14520b57cec5SDimitry Andric       assert(Keyword == Ident_defined_in && "Invalid clause keyword!");
14530b57cec5SDimitry Andric       if (HadDefinedIn) {
14540b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_external_source_symbol_duplicate_clause)
14550b57cec5SDimitry Andric             << Keyword;
14560b57cec5SDimitry Andric         ParseStringLiteralExpression();
14570b57cec5SDimitry Andric         continue;
14580b57cec5SDimitry Andric       }
14590b57cec5SDimitry Andric       DefinedInExpr = ParseStringLiteralExpression();
14600b57cec5SDimitry Andric     }
14610b57cec5SDimitry Andric   } while (TryConsumeToken(tok::comma));
14620b57cec5SDimitry Andric 
14630b57cec5SDimitry Andric   // Closing ')'.
14640b57cec5SDimitry Andric   if (T.consumeClose())
14650b57cec5SDimitry Andric     return;
14660b57cec5SDimitry Andric   if (EndLoc)
14670b57cec5SDimitry Andric     *EndLoc = T.getCloseLocation();
14680b57cec5SDimitry Andric 
1469*06c3fb27SDimitry Andric   ArgsUnion Args[] = {Language.get(), DefinedInExpr.get(), GeneratedDeclaration,
1470*06c3fb27SDimitry Andric                       USR.get()};
14710b57cec5SDimitry Andric   Attrs.addNew(&ExternalSourceSymbol, SourceRange(Loc, T.getCloseLocation()),
1472*06c3fb27SDimitry Andric                ScopeName, ScopeLoc, Args, std::size(Args), Form);
14730b57cec5SDimitry Andric }
14740b57cec5SDimitry Andric 
14750b57cec5SDimitry Andric /// Parse the contents of the "objc_bridge_related" attribute.
14760b57cec5SDimitry Andric /// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')'
14770b57cec5SDimitry Andric /// related_class:
14780b57cec5SDimitry Andric ///     Identifier
14790b57cec5SDimitry Andric ///
14800b57cec5SDimitry Andric /// opt-class_method:
14810b57cec5SDimitry Andric ///     Identifier: | <empty>
14820b57cec5SDimitry Andric ///
14830b57cec5SDimitry Andric /// opt-instance_method:
14840b57cec5SDimitry Andric ///     Identifier | <empty>
14850b57cec5SDimitry Andric ///
148681ad6265SDimitry Andric void Parser::ParseObjCBridgeRelatedAttribute(
148781ad6265SDimitry Andric     IdentifierInfo &ObjCBridgeRelated, SourceLocation ObjCBridgeRelatedLoc,
148881ad6265SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
1489*06c3fb27SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Form Form) {
14900b57cec5SDimitry Andric   // Opening '('.
14910b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
14920b57cec5SDimitry Andric   if (T.consumeOpen()) {
14930b57cec5SDimitry Andric     Diag(Tok, diag::err_expected) << tok::l_paren;
14940b57cec5SDimitry Andric     return;
14950b57cec5SDimitry Andric   }
14960b57cec5SDimitry Andric 
14970b57cec5SDimitry Andric   // Parse the related class name.
14980b57cec5SDimitry Andric   if (Tok.isNot(tok::identifier)) {
14990b57cec5SDimitry Andric     Diag(Tok, diag::err_objcbridge_related_expected_related_class);
15000b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
15010b57cec5SDimitry Andric     return;
15020b57cec5SDimitry Andric   }
15030b57cec5SDimitry Andric   IdentifierLoc *RelatedClass = ParseIdentifierLoc();
15040b57cec5SDimitry Andric   if (ExpectAndConsume(tok::comma)) {
15050b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
15060b57cec5SDimitry Andric     return;
15070b57cec5SDimitry Andric   }
15080b57cec5SDimitry Andric 
15090b57cec5SDimitry Andric   // Parse class method name.  It's non-optional in the sense that a trailing
15100b57cec5SDimitry Andric   // comma is required, but it can be the empty string, and then we record a
15110b57cec5SDimitry Andric   // nullptr.
15120b57cec5SDimitry Andric   IdentifierLoc *ClassMethod = nullptr;
15130b57cec5SDimitry Andric   if (Tok.is(tok::identifier)) {
15140b57cec5SDimitry Andric     ClassMethod = ParseIdentifierLoc();
15150b57cec5SDimitry Andric     if (!TryConsumeToken(tok::colon)) {
15160b57cec5SDimitry Andric       Diag(Tok, diag::err_objcbridge_related_selector_name);
15170b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
15180b57cec5SDimitry Andric       return;
15190b57cec5SDimitry Andric     }
15200b57cec5SDimitry Andric   }
15210b57cec5SDimitry Andric   if (!TryConsumeToken(tok::comma)) {
15220b57cec5SDimitry Andric     if (Tok.is(tok::colon))
15230b57cec5SDimitry Andric       Diag(Tok, diag::err_objcbridge_related_selector_name);
15240b57cec5SDimitry Andric     else
15250b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::comma;
15260b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
15270b57cec5SDimitry Andric     return;
15280b57cec5SDimitry Andric   }
15290b57cec5SDimitry Andric 
15300b57cec5SDimitry Andric   // Parse instance method name.  Also non-optional but empty string is
15310b57cec5SDimitry Andric   // permitted.
15320b57cec5SDimitry Andric   IdentifierLoc *InstanceMethod = nullptr;
15330b57cec5SDimitry Andric   if (Tok.is(tok::identifier))
15340b57cec5SDimitry Andric     InstanceMethod = ParseIdentifierLoc();
15350b57cec5SDimitry Andric   else if (Tok.isNot(tok::r_paren)) {
15360b57cec5SDimitry Andric     Diag(Tok, diag::err_expected) << tok::r_paren;
15370b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
15380b57cec5SDimitry Andric     return;
15390b57cec5SDimitry Andric   }
15400b57cec5SDimitry Andric 
15410b57cec5SDimitry Andric   // Closing ')'.
15420b57cec5SDimitry Andric   if (T.consumeClose())
15430b57cec5SDimitry Andric     return;
15440b57cec5SDimitry Andric 
154581ad6265SDimitry Andric   if (EndLoc)
154681ad6265SDimitry Andric     *EndLoc = T.getCloseLocation();
15470b57cec5SDimitry Andric 
15480b57cec5SDimitry Andric   // Record this attribute
154981ad6265SDimitry Andric   Attrs.addNew(&ObjCBridgeRelated,
15500b57cec5SDimitry Andric                SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()),
155181ad6265SDimitry Andric                ScopeName, ScopeLoc, RelatedClass, ClassMethod, InstanceMethod,
1552*06c3fb27SDimitry Andric                Form);
15530b57cec5SDimitry Andric }
15540b57cec5SDimitry Andric 
1555e8d8bef9SDimitry Andric void Parser::ParseSwiftNewTypeAttribute(
1556e8d8bef9SDimitry Andric     IdentifierInfo &AttrName, SourceLocation AttrNameLoc,
1557e8d8bef9SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
1558*06c3fb27SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Form Form) {
1559e8d8bef9SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
1560e8d8bef9SDimitry Andric 
1561e8d8bef9SDimitry Andric   // Opening '('
1562e8d8bef9SDimitry Andric   if (T.consumeOpen()) {
1563e8d8bef9SDimitry Andric     Diag(Tok, diag::err_expected) << tok::l_paren;
1564e8d8bef9SDimitry Andric     return;
1565e8d8bef9SDimitry Andric   }
1566e8d8bef9SDimitry Andric 
1567e8d8bef9SDimitry Andric   if (Tok.is(tok::r_paren)) {
1568e8d8bef9SDimitry Andric     Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1569e8d8bef9SDimitry Andric     T.consumeClose();
1570e8d8bef9SDimitry Andric     return;
1571e8d8bef9SDimitry Andric   }
1572e8d8bef9SDimitry Andric   if (Tok.isNot(tok::kw_struct) && Tok.isNot(tok::kw_enum)) {
1573e8d8bef9SDimitry Andric     Diag(Tok, diag::warn_attribute_type_not_supported)
1574e8d8bef9SDimitry Andric         << &AttrName << Tok.getIdentifierInfo();
1575e8d8bef9SDimitry Andric     if (!isTokenSpecial())
1576e8d8bef9SDimitry Andric       ConsumeToken();
1577e8d8bef9SDimitry Andric     T.consumeClose();
1578e8d8bef9SDimitry Andric     return;
1579e8d8bef9SDimitry Andric   }
1580e8d8bef9SDimitry Andric 
1581e8d8bef9SDimitry Andric   auto *SwiftType = IdentifierLoc::create(Actions.Context, Tok.getLocation(),
1582e8d8bef9SDimitry Andric                                           Tok.getIdentifierInfo());
1583e8d8bef9SDimitry Andric   ConsumeToken();
1584e8d8bef9SDimitry Andric 
1585e8d8bef9SDimitry Andric   // Closing ')'
1586e8d8bef9SDimitry Andric   if (T.consumeClose())
1587e8d8bef9SDimitry Andric     return;
1588e8d8bef9SDimitry Andric   if (EndLoc)
1589e8d8bef9SDimitry Andric     *EndLoc = T.getCloseLocation();
1590e8d8bef9SDimitry Andric 
1591e8d8bef9SDimitry Andric   ArgsUnion Args[] = {SwiftType};
1592e8d8bef9SDimitry Andric   Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, T.getCloseLocation()),
1593*06c3fb27SDimitry Andric                ScopeName, ScopeLoc, Args, std::size(Args), Form);
1594e8d8bef9SDimitry Andric }
1595e8d8bef9SDimitry Andric 
1596*06c3fb27SDimitry Andric void Parser::ParseTypeTagForDatatypeAttribute(
1597*06c3fb27SDimitry Andric     IdentifierInfo &AttrName, SourceLocation AttrNameLoc,
1598*06c3fb27SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
1599*06c3fb27SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Form Form) {
16000b57cec5SDimitry Andric   assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
16010b57cec5SDimitry Andric 
16020b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
16030b57cec5SDimitry Andric   T.consumeOpen();
16040b57cec5SDimitry Andric 
16050b57cec5SDimitry Andric   if (Tok.isNot(tok::identifier)) {
16060b57cec5SDimitry Andric     Diag(Tok, diag::err_expected) << tok::identifier;
16070b57cec5SDimitry Andric     T.skipToEnd();
16080b57cec5SDimitry Andric     return;
16090b57cec5SDimitry Andric   }
16100b57cec5SDimitry Andric   IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
16110b57cec5SDimitry Andric 
16120b57cec5SDimitry Andric   if (ExpectAndConsume(tok::comma)) {
16130b57cec5SDimitry Andric     T.skipToEnd();
16140b57cec5SDimitry Andric     return;
16150b57cec5SDimitry Andric   }
16160b57cec5SDimitry Andric 
16170b57cec5SDimitry Andric   SourceRange MatchingCTypeRange;
16180b57cec5SDimitry Andric   TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
16190b57cec5SDimitry Andric   if (MatchingCType.isInvalid()) {
16200b57cec5SDimitry Andric     T.skipToEnd();
16210b57cec5SDimitry Andric     return;
16220b57cec5SDimitry Andric   }
16230b57cec5SDimitry Andric 
16240b57cec5SDimitry Andric   bool LayoutCompatible = false;
16250b57cec5SDimitry Andric   bool MustBeNull = false;
16260b57cec5SDimitry Andric   while (TryConsumeToken(tok::comma)) {
16270b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
16280b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::identifier;
16290b57cec5SDimitry Andric       T.skipToEnd();
16300b57cec5SDimitry Andric       return;
16310b57cec5SDimitry Andric     }
16320b57cec5SDimitry Andric     IdentifierInfo *Flag = Tok.getIdentifierInfo();
16330b57cec5SDimitry Andric     if (Flag->isStr("layout_compatible"))
16340b57cec5SDimitry Andric       LayoutCompatible = true;
16350b57cec5SDimitry Andric     else if (Flag->isStr("must_be_null"))
16360b57cec5SDimitry Andric       MustBeNull = true;
16370b57cec5SDimitry Andric     else {
16380b57cec5SDimitry Andric       Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
16390b57cec5SDimitry Andric       T.skipToEnd();
16400b57cec5SDimitry Andric       return;
16410b57cec5SDimitry Andric     }
16420b57cec5SDimitry Andric     ConsumeToken(); // consume flag
16430b57cec5SDimitry Andric   }
16440b57cec5SDimitry Andric 
16450b57cec5SDimitry Andric   if (!T.consumeClose()) {
16460b57cec5SDimitry Andric     Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, ScopeName, ScopeLoc,
16470b57cec5SDimitry Andric                                    ArgumentKind, MatchingCType.get(),
1648*06c3fb27SDimitry Andric                                    LayoutCompatible, MustBeNull, Form);
16490b57cec5SDimitry Andric   }
16500b57cec5SDimitry Andric 
16510b57cec5SDimitry Andric   if (EndLoc)
16520b57cec5SDimitry Andric     *EndLoc = T.getCloseLocation();
16530b57cec5SDimitry Andric }
16540b57cec5SDimitry Andric 
16550b57cec5SDimitry Andric /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
16560b57cec5SDimitry Andric /// of a C++11 attribute-specifier in a location where an attribute is not
16570b57cec5SDimitry Andric /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
16580b57cec5SDimitry Andric /// situation.
16590b57cec5SDimitry Andric ///
16600b57cec5SDimitry Andric /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
16610b57cec5SDimitry Andric /// this doesn't appear to actually be an attribute-specifier, and the caller
16620b57cec5SDimitry Andric /// should try to parse it.
16630b57cec5SDimitry Andric bool Parser::DiagnoseProhibitedCXX11Attribute() {
16640b57cec5SDimitry Andric   assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
16650b57cec5SDimitry Andric 
16660b57cec5SDimitry Andric   switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
16670b57cec5SDimitry Andric   case CAK_NotAttributeSpecifier:
16680b57cec5SDimitry Andric     // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
16690b57cec5SDimitry Andric     return false;
16700b57cec5SDimitry Andric 
16710b57cec5SDimitry Andric   case CAK_InvalidAttributeSpecifier:
16720b57cec5SDimitry Andric     Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
16730b57cec5SDimitry Andric     return false;
16740b57cec5SDimitry Andric 
16750b57cec5SDimitry Andric   case CAK_AttributeSpecifier:
16760b57cec5SDimitry Andric     // Parse and discard the attributes.
16770b57cec5SDimitry Andric     SourceLocation BeginLoc = ConsumeBracket();
16780b57cec5SDimitry Andric     ConsumeBracket();
16790b57cec5SDimitry Andric     SkipUntil(tok::r_square);
16800b57cec5SDimitry Andric     assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
16810b57cec5SDimitry Andric     SourceLocation EndLoc = ConsumeBracket();
16820b57cec5SDimitry Andric     Diag(BeginLoc, diag::err_attributes_not_allowed)
16830b57cec5SDimitry Andric       << SourceRange(BeginLoc, EndLoc);
16840b57cec5SDimitry Andric     return true;
16850b57cec5SDimitry Andric   }
16860b57cec5SDimitry Andric   llvm_unreachable("All cases handled above.");
16870b57cec5SDimitry Andric }
16880b57cec5SDimitry Andric 
16890b57cec5SDimitry Andric /// We have found the opening square brackets of a C++11
16900b57cec5SDimitry Andric /// attribute-specifier in a location where an attribute is not permitted, but
16910b57cec5SDimitry Andric /// we know where the attributes ought to be written. Parse them anyway, and
16920b57cec5SDimitry Andric /// provide a fixit moving them to the right place.
169381ad6265SDimitry Andric void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributes &Attrs,
16940b57cec5SDimitry Andric                                              SourceLocation CorrectLocation) {
16950b57cec5SDimitry Andric   assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1696*06c3fb27SDimitry Andric          Tok.is(tok::kw_alignas) || Tok.isRegularKeywordAttribute());
16970b57cec5SDimitry Andric 
16980b57cec5SDimitry Andric   // Consume the attributes.
1699*06c3fb27SDimitry Andric   auto Keyword =
1700*06c3fb27SDimitry Andric       Tok.isRegularKeywordAttribute() ? Tok.getIdentifierInfo() : nullptr;
17010b57cec5SDimitry Andric   SourceLocation Loc = Tok.getLocation();
17020b57cec5SDimitry Andric   ParseCXX11Attributes(Attrs);
17030b57cec5SDimitry Andric   CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
17040b57cec5SDimitry Andric   // FIXME: use err_attributes_misplaced
1705*06c3fb27SDimitry Andric   (Keyword ? Diag(Loc, diag::err_keyword_not_allowed) << Keyword
1706*06c3fb27SDimitry Andric            : Diag(Loc, diag::err_attributes_not_allowed))
17070b57cec5SDimitry Andric       << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
17080b57cec5SDimitry Andric       << FixItHint::CreateRemoval(AttrRange);
17090b57cec5SDimitry Andric }
17100b57cec5SDimitry Andric 
17110b57cec5SDimitry Andric void Parser::DiagnoseProhibitedAttributes(
1712*06c3fb27SDimitry Andric     const ParsedAttributesView &Attrs, const SourceLocation CorrectLocation) {
1713*06c3fb27SDimitry Andric   auto *FirstAttr = Attrs.empty() ? nullptr : &Attrs.front();
17140b57cec5SDimitry Andric   if (CorrectLocation.isValid()) {
1715*06c3fb27SDimitry Andric     CharSourceRange AttrRange(Attrs.Range, true);
1716*06c3fb27SDimitry Andric     (FirstAttr && FirstAttr->isRegularKeywordAttribute()
1717*06c3fb27SDimitry Andric          ? Diag(CorrectLocation, diag::err_keyword_misplaced) << FirstAttr
1718*06c3fb27SDimitry Andric          : Diag(CorrectLocation, diag::err_attributes_misplaced))
17190b57cec5SDimitry Andric         << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
17200b57cec5SDimitry Andric         << FixItHint::CreateRemoval(AttrRange);
1721*06c3fb27SDimitry Andric   } else {
1722*06c3fb27SDimitry Andric     const SourceRange &Range = Attrs.Range;
1723*06c3fb27SDimitry Andric     (FirstAttr && FirstAttr->isRegularKeywordAttribute()
1724*06c3fb27SDimitry Andric          ? Diag(Range.getBegin(), diag::err_keyword_not_allowed) << FirstAttr
1725*06c3fb27SDimitry Andric          : Diag(Range.getBegin(), diag::err_attributes_not_allowed))
1726*06c3fb27SDimitry Andric         << Range;
1727*06c3fb27SDimitry Andric   }
17280b57cec5SDimitry Andric }
17290b57cec5SDimitry Andric 
1730*06c3fb27SDimitry Andric void Parser::ProhibitCXX11Attributes(ParsedAttributes &Attrs,
1731*06c3fb27SDimitry Andric                                      unsigned AttrDiagID,
1732*06c3fb27SDimitry Andric                                      unsigned KeywordDiagID,
173381ad6265SDimitry Andric                                      bool DiagnoseEmptyAttrs,
173481ad6265SDimitry Andric                                      bool WarnOnUnknownAttrs) {
1735fe6060f1SDimitry Andric 
1736fe6060f1SDimitry Andric   if (DiagnoseEmptyAttrs && Attrs.empty() && Attrs.Range.isValid()) {
1737fe6060f1SDimitry Andric     // An attribute list has been parsed, but it was empty.
1738fe6060f1SDimitry Andric     // This is the case for [[]].
1739fe6060f1SDimitry Andric     const auto &LangOpts = getLangOpts();
1740fe6060f1SDimitry Andric     auto &SM = PP.getSourceManager();
1741fe6060f1SDimitry Andric     Token FirstLSquare;
1742fe6060f1SDimitry Andric     Lexer::getRawToken(Attrs.Range.getBegin(), FirstLSquare, SM, LangOpts);
1743fe6060f1SDimitry Andric 
1744fe6060f1SDimitry Andric     if (FirstLSquare.is(tok::l_square)) {
1745bdd1243dSDimitry Andric       std::optional<Token> SecondLSquare =
1746fe6060f1SDimitry Andric           Lexer::findNextToken(FirstLSquare.getLocation(), SM, LangOpts);
1747fe6060f1SDimitry Andric 
1748fe6060f1SDimitry Andric       if (SecondLSquare && SecondLSquare->is(tok::l_square)) {
1749fe6060f1SDimitry Andric         // The attribute range starts with [[, but is empty. So this must
1750fe6060f1SDimitry Andric         // be [[]], which we are supposed to diagnose because
1751fe6060f1SDimitry Andric         // DiagnoseEmptyAttrs is true.
1752*06c3fb27SDimitry Andric         Diag(Attrs.Range.getBegin(), AttrDiagID) << Attrs.Range;
1753fe6060f1SDimitry Andric         return;
1754fe6060f1SDimitry Andric       }
1755fe6060f1SDimitry Andric     }
1756fe6060f1SDimitry Andric   }
1757fe6060f1SDimitry Andric 
17580b57cec5SDimitry Andric   for (const ParsedAttr &AL : Attrs) {
1759*06c3fb27SDimitry Andric     if (AL.isRegularKeywordAttribute()) {
1760*06c3fb27SDimitry Andric       Diag(AL.getLoc(), KeywordDiagID) << AL;
1761*06c3fb27SDimitry Andric       AL.setInvalid();
1762*06c3fb27SDimitry Andric       continue;
1763*06c3fb27SDimitry Andric     }
17640b57cec5SDimitry Andric     if (!AL.isCXX11Attribute() && !AL.isC2xAttribute())
17650b57cec5SDimitry Andric       continue;
176681ad6265SDimitry Andric     if (AL.getKind() == ParsedAttr::UnknownAttribute) {
176781ad6265SDimitry Andric       if (WarnOnUnknownAttrs)
1768e8d8bef9SDimitry Andric         Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
1769e8d8bef9SDimitry Andric             << AL << AL.getRange();
177081ad6265SDimitry Andric     } else {
1771*06c3fb27SDimitry Andric       Diag(AL.getLoc(), AttrDiagID) << AL;
17720b57cec5SDimitry Andric       AL.setInvalid();
17730b57cec5SDimitry Andric     }
17740b57cec5SDimitry Andric   }
17750b57cec5SDimitry Andric }
17760b57cec5SDimitry Andric 
177781ad6265SDimitry Andric void Parser::DiagnoseCXX11AttributeExtension(ParsedAttributes &Attrs) {
1778fe6060f1SDimitry Andric   for (const ParsedAttr &PA : Attrs) {
1779*06c3fb27SDimitry Andric     if (PA.isCXX11Attribute() || PA.isC2xAttribute() ||
1780*06c3fb27SDimitry Andric         PA.isRegularKeywordAttribute())
1781*06c3fb27SDimitry Andric       Diag(PA.getLoc(), diag::ext_cxx11_attr_placement)
1782*06c3fb27SDimitry Andric           << PA << PA.isRegularKeywordAttribute() << PA.getRange();
1783fe6060f1SDimitry Andric   }
1784fe6060f1SDimitry Andric }
1785fe6060f1SDimitry Andric 
17860b57cec5SDimitry Andric // Usually, `__attribute__((attrib)) class Foo {} var` means that attribute
17870b57cec5SDimitry Andric // applies to var, not the type Foo.
17880b57cec5SDimitry Andric // As an exception to the rule, __declspec(align(...)) before the
17890b57cec5SDimitry Andric // class-key affects the type instead of the variable.
17900b57cec5SDimitry Andric // Also, Microsoft-style [attributes] seem to affect the type instead of the
17910b57cec5SDimitry Andric // variable.
17920b57cec5SDimitry Andric // This function moves attributes that should apply to the type off DS to Attrs.
179381ad6265SDimitry Andric void Parser::stripTypeAttributesOffDeclSpec(ParsedAttributes &Attrs,
17940b57cec5SDimitry Andric                                             DeclSpec &DS,
17950b57cec5SDimitry Andric                                             Sema::TagUseKind TUK) {
17960b57cec5SDimitry Andric   if (TUK == Sema::TUK_Reference)
17970b57cec5SDimitry Andric     return;
17980b57cec5SDimitry Andric 
17990b57cec5SDimitry Andric   llvm::SmallVector<ParsedAttr *, 1> ToBeMoved;
18000b57cec5SDimitry Andric 
18010b57cec5SDimitry Andric   for (ParsedAttr &AL : DS.getAttributes()) {
18020b57cec5SDimitry Andric     if ((AL.getKind() == ParsedAttr::AT_Aligned &&
18030b57cec5SDimitry Andric          AL.isDeclspecAttribute()) ||
18040b57cec5SDimitry Andric         AL.isMicrosoftAttribute())
18050b57cec5SDimitry Andric       ToBeMoved.push_back(&AL);
18060b57cec5SDimitry Andric   }
18070b57cec5SDimitry Andric 
18080b57cec5SDimitry Andric   for (ParsedAttr *AL : ToBeMoved) {
18090b57cec5SDimitry Andric     DS.getAttributes().remove(AL);
18100b57cec5SDimitry Andric     Attrs.addAtEnd(AL);
18110b57cec5SDimitry Andric   }
18120b57cec5SDimitry Andric }
18130b57cec5SDimitry Andric 
18140b57cec5SDimitry Andric /// ParseDeclaration - Parse a full 'declaration', which consists of
18150b57cec5SDimitry Andric /// declaration-specifiers, some number of declarators, and a semicolon.
18160b57cec5SDimitry Andric /// 'Context' should be a DeclaratorContext value.  This returns the
18170b57cec5SDimitry Andric /// location of the semicolon in DeclEnd.
18180b57cec5SDimitry Andric ///
18190b57cec5SDimitry Andric ///       declaration: [C99 6.7]
18200b57cec5SDimitry Andric ///         block-declaration ->
18210b57cec5SDimitry Andric ///           simple-declaration
18220b57cec5SDimitry Andric ///           others                   [FIXME]
18230b57cec5SDimitry Andric /// [C++]   template-declaration
18240b57cec5SDimitry Andric /// [C++]   namespace-definition
18250b57cec5SDimitry Andric /// [C++]   using-directive
18260b57cec5SDimitry Andric /// [C++]   using-declaration
18270b57cec5SDimitry Andric /// [C++11/C11] static_assert-declaration
18280b57cec5SDimitry Andric ///         others... [FIXME]
18290b57cec5SDimitry Andric ///
183081ad6265SDimitry Andric Parser::DeclGroupPtrTy Parser::ParseDeclaration(DeclaratorContext Context,
183181ad6265SDimitry Andric                                                 SourceLocation &DeclEnd,
183281ad6265SDimitry Andric                                                 ParsedAttributes &DeclAttrs,
183381ad6265SDimitry Andric                                                 ParsedAttributes &DeclSpecAttrs,
1834a7dea167SDimitry Andric                                                 SourceLocation *DeclSpecStart) {
18350b57cec5SDimitry Andric   ParenBraceBracketBalancer BalancerRAIIObj(*this);
18360b57cec5SDimitry Andric   // Must temporarily exit the objective-c container scope for
18370b57cec5SDimitry Andric   // parsing c none objective-c decls.
18380b57cec5SDimitry Andric   ObjCDeclContextSwitch ObjCDC(*this);
18390b57cec5SDimitry Andric 
18400b57cec5SDimitry Andric   Decl *SingleDecl = nullptr;
18410b57cec5SDimitry Andric   switch (Tok.getKind()) {
18420b57cec5SDimitry Andric   case tok::kw_template:
18430b57cec5SDimitry Andric   case tok::kw_export:
184481ad6265SDimitry Andric     ProhibitAttributes(DeclAttrs);
184581ad6265SDimitry Andric     ProhibitAttributes(DeclSpecAttrs);
184681ad6265SDimitry Andric     SingleDecl =
184781ad6265SDimitry Andric         ParseDeclarationStartingWithTemplate(Context, DeclEnd, DeclAttrs);
18480b57cec5SDimitry Andric     break;
18490b57cec5SDimitry Andric   case tok::kw_inline:
18500b57cec5SDimitry Andric     // Could be the start of an inline namespace. Allowed as an ext in C++03.
18510b57cec5SDimitry Andric     if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
185281ad6265SDimitry Andric       ProhibitAttributes(DeclAttrs);
185381ad6265SDimitry Andric       ProhibitAttributes(DeclSpecAttrs);
18540b57cec5SDimitry Andric       SourceLocation InlineLoc = ConsumeToken();
18550b57cec5SDimitry Andric       return ParseNamespace(Context, DeclEnd, InlineLoc);
18560b57cec5SDimitry Andric     }
185781ad6265SDimitry Andric     return ParseSimpleDeclaration(Context, DeclEnd, DeclAttrs, DeclSpecAttrs,
185881ad6265SDimitry Andric                                   true, nullptr, DeclSpecStart);
1859bdd1243dSDimitry Andric 
1860bdd1243dSDimitry Andric   case tok::kw_cbuffer:
1861bdd1243dSDimitry Andric   case tok::kw_tbuffer:
1862bdd1243dSDimitry Andric     SingleDecl = ParseHLSLBuffer(DeclEnd);
1863bdd1243dSDimitry Andric     break;
18640b57cec5SDimitry Andric   case tok::kw_namespace:
186581ad6265SDimitry Andric     ProhibitAttributes(DeclAttrs);
186681ad6265SDimitry Andric     ProhibitAttributes(DeclSpecAttrs);
18670b57cec5SDimitry Andric     return ParseNamespace(Context, DeclEnd);
186881ad6265SDimitry Andric   case tok::kw_using: {
186981ad6265SDimitry Andric     ParsedAttributes Attrs(AttrFactory);
187081ad6265SDimitry Andric     takeAndConcatenateAttrs(DeclAttrs, DeclSpecAttrs, Attrs);
18710b57cec5SDimitry Andric     return ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
187281ad6265SDimitry Andric                                             DeclEnd, Attrs);
187381ad6265SDimitry Andric   }
18740b57cec5SDimitry Andric   case tok::kw_static_assert:
18750b57cec5SDimitry Andric   case tok::kw__Static_assert:
187681ad6265SDimitry Andric     ProhibitAttributes(DeclAttrs);
187781ad6265SDimitry Andric     ProhibitAttributes(DeclSpecAttrs);
18780b57cec5SDimitry Andric     SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
18790b57cec5SDimitry Andric     break;
18800b57cec5SDimitry Andric   default:
188181ad6265SDimitry Andric     return ParseSimpleDeclaration(Context, DeclEnd, DeclAttrs, DeclSpecAttrs,
188281ad6265SDimitry Andric                                   true, nullptr, DeclSpecStart);
18830b57cec5SDimitry Andric   }
18840b57cec5SDimitry Andric 
18850b57cec5SDimitry Andric   // This routine returns a DeclGroup, if the thing we parsed only contains a
18860b57cec5SDimitry Andric   // single decl, convert it now.
18870b57cec5SDimitry Andric   return Actions.ConvertDeclToDeclGroup(SingleDecl);
18880b57cec5SDimitry Andric }
18890b57cec5SDimitry Andric 
18900b57cec5SDimitry Andric ///       simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
18910b57cec5SDimitry Andric ///         declaration-specifiers init-declarator-list[opt] ';'
18920b57cec5SDimitry Andric /// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
18930b57cec5SDimitry Andric ///             init-declarator-list ';'
18940b57cec5SDimitry Andric ///[C90/C++]init-declarator-list ';'                             [TODO]
18950b57cec5SDimitry Andric /// [OMP]   threadprivate-directive
18960b57cec5SDimitry Andric /// [OMP]   allocate-directive                                   [TODO]
18970b57cec5SDimitry Andric ///
18980b57cec5SDimitry Andric ///       for-range-declaration: [C++11 6.5p1: stmt.ranged]
18990b57cec5SDimitry Andric ///         attribute-specifier-seq[opt] type-specifier-seq declarator
19000b57cec5SDimitry Andric ///
19010b57cec5SDimitry Andric /// If RequireSemi is false, this does not check for a ';' at the end of the
19020b57cec5SDimitry Andric /// declaration.  If it is true, it checks for and eats it.
19030b57cec5SDimitry Andric ///
19040b57cec5SDimitry Andric /// If FRI is non-null, we might be parsing a for-range-declaration instead
19050b57cec5SDimitry Andric /// of a simple-declaration. If we find that we are, we also parse the
19060b57cec5SDimitry Andric /// for-range-initializer, and place it here.
1907a7dea167SDimitry Andric ///
1908a7dea167SDimitry Andric /// DeclSpecStart is used when decl-specifiers are parsed before parsing
1909a7dea167SDimitry Andric /// the Declaration. The SourceLocation for this Decl is set to
1910a7dea167SDimitry Andric /// DeclSpecStart if DeclSpecStart is non-null.
1911a7dea167SDimitry Andric Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(
1912a7dea167SDimitry Andric     DeclaratorContext Context, SourceLocation &DeclEnd,
191381ad6265SDimitry Andric     ParsedAttributes &DeclAttrs, ParsedAttributes &DeclSpecAttrs,
191481ad6265SDimitry Andric     bool RequireSemi, ForRangeInit *FRI, SourceLocation *DeclSpecStart) {
191581ad6265SDimitry Andric   // Need to retain these for diagnostics before we add them to the DeclSepc.
191681ad6265SDimitry Andric   ParsedAttributesView OriginalDeclSpecAttrs;
191781ad6265SDimitry Andric   OriginalDeclSpecAttrs.addAll(DeclSpecAttrs.begin(), DeclSpecAttrs.end());
191881ad6265SDimitry Andric   OriginalDeclSpecAttrs.Range = DeclSpecAttrs.Range;
191981ad6265SDimitry Andric 
19200b57cec5SDimitry Andric   // Parse the common declaration-specifiers piece.
19210b57cec5SDimitry Andric   ParsingDeclSpec DS(*this);
192281ad6265SDimitry Andric   DS.takeAttributesFrom(DeclSpecAttrs);
19230b57cec5SDimitry Andric 
19240b57cec5SDimitry Andric   DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
19250b57cec5SDimitry Andric   ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
19260b57cec5SDimitry Andric 
19270b57cec5SDimitry Andric   // If we had a free-standing type definition with a missing semicolon, we
19280b57cec5SDimitry Andric   // may get this far before the problem becomes obvious.
19290b57cec5SDimitry Andric   if (DS.hasTagDefinition() &&
19300b57cec5SDimitry Andric       DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
19310b57cec5SDimitry Andric     return nullptr;
19320b57cec5SDimitry Andric 
19330b57cec5SDimitry Andric   // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
19340b57cec5SDimitry Andric   // declaration-specifiers init-declarator-list[opt] ';'
19350b57cec5SDimitry Andric   if (Tok.is(tok::semi)) {
193681ad6265SDimitry Andric     ProhibitAttributes(DeclAttrs);
19370b57cec5SDimitry Andric     DeclEnd = Tok.getLocation();
19380b57cec5SDimitry Andric     if (RequireSemi) ConsumeToken();
19390b57cec5SDimitry Andric     RecordDecl *AnonRecord = nullptr;
194081ad6265SDimitry Andric     Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(
194181ad6265SDimitry Andric         getCurScope(), AS_none, DS, ParsedAttributesView::none(), AnonRecord);
19420b57cec5SDimitry Andric     DS.complete(TheDecl);
19430b57cec5SDimitry Andric     if (AnonRecord) {
19440b57cec5SDimitry Andric       Decl* decls[] = {AnonRecord, TheDecl};
19450b57cec5SDimitry Andric       return Actions.BuildDeclaratorGroup(decls);
19460b57cec5SDimitry Andric     }
19470b57cec5SDimitry Andric     return Actions.ConvertDeclToDeclGroup(TheDecl);
19480b57cec5SDimitry Andric   }
19490b57cec5SDimitry Andric 
1950a7dea167SDimitry Andric   if (DeclSpecStart)
1951a7dea167SDimitry Andric     DS.SetRangeStart(*DeclSpecStart);
1952a7dea167SDimitry Andric 
195381ad6265SDimitry Andric   return ParseDeclGroup(DS, Context, DeclAttrs, &DeclEnd, FRI);
19540b57cec5SDimitry Andric }
19550b57cec5SDimitry Andric 
19560b57cec5SDimitry Andric /// Returns true if this might be the start of a declarator, or a common typo
19570b57cec5SDimitry Andric /// for a declarator.
19580b57cec5SDimitry Andric bool Parser::MightBeDeclarator(DeclaratorContext Context) {
19590b57cec5SDimitry Andric   switch (Tok.getKind()) {
19600b57cec5SDimitry Andric   case tok::annot_cxxscope:
19610b57cec5SDimitry Andric   case tok::annot_template_id:
19620b57cec5SDimitry Andric   case tok::caret:
19630b57cec5SDimitry Andric   case tok::code_completion:
19640b57cec5SDimitry Andric   case tok::coloncolon:
19650b57cec5SDimitry Andric   case tok::ellipsis:
19660b57cec5SDimitry Andric   case tok::kw___attribute:
19670b57cec5SDimitry Andric   case tok::kw_operator:
19680b57cec5SDimitry Andric   case tok::l_paren:
19690b57cec5SDimitry Andric   case tok::star:
19700b57cec5SDimitry Andric     return true;
19710b57cec5SDimitry Andric 
19720b57cec5SDimitry Andric   case tok::amp:
19730b57cec5SDimitry Andric   case tok::ampamp:
19740b57cec5SDimitry Andric     return getLangOpts().CPlusPlus;
19750b57cec5SDimitry Andric 
19760b57cec5SDimitry Andric   case tok::l_square: // Might be an attribute on an unnamed bit-field.
1977e8d8bef9SDimitry Andric     return Context == DeclaratorContext::Member && getLangOpts().CPlusPlus11 &&
1978e8d8bef9SDimitry Andric            NextToken().is(tok::l_square);
19790b57cec5SDimitry Andric 
19800b57cec5SDimitry Andric   case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
1981e8d8bef9SDimitry Andric     return Context == DeclaratorContext::Member || getLangOpts().CPlusPlus;
19820b57cec5SDimitry Andric 
19830b57cec5SDimitry Andric   case tok::identifier:
19840b57cec5SDimitry Andric     switch (NextToken().getKind()) {
19850b57cec5SDimitry Andric     case tok::code_completion:
19860b57cec5SDimitry Andric     case tok::coloncolon:
19870b57cec5SDimitry Andric     case tok::comma:
19880b57cec5SDimitry Andric     case tok::equal:
19890b57cec5SDimitry Andric     case tok::equalequal: // Might be a typo for '='.
19900b57cec5SDimitry Andric     case tok::kw_alignas:
19910b57cec5SDimitry Andric     case tok::kw_asm:
19920b57cec5SDimitry Andric     case tok::kw___attribute:
19930b57cec5SDimitry Andric     case tok::l_brace:
19940b57cec5SDimitry Andric     case tok::l_paren:
19950b57cec5SDimitry Andric     case tok::l_square:
19960b57cec5SDimitry Andric     case tok::less:
19970b57cec5SDimitry Andric     case tok::r_brace:
19980b57cec5SDimitry Andric     case tok::r_paren:
19990b57cec5SDimitry Andric     case tok::r_square:
20000b57cec5SDimitry Andric     case tok::semi:
20010b57cec5SDimitry Andric       return true;
20020b57cec5SDimitry Andric 
20030b57cec5SDimitry Andric     case tok::colon:
20040b57cec5SDimitry Andric       // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
20050b57cec5SDimitry Andric       // and in block scope it's probably a label. Inside a class definition,
20060b57cec5SDimitry Andric       // this is a bit-field.
2007e8d8bef9SDimitry Andric       return Context == DeclaratorContext::Member ||
2008e8d8bef9SDimitry Andric              (getLangOpts().CPlusPlus && Context == DeclaratorContext::File);
20090b57cec5SDimitry Andric 
20100b57cec5SDimitry Andric     case tok::identifier: // Possible virt-specifier.
20110b57cec5SDimitry Andric       return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
20120b57cec5SDimitry Andric 
20130b57cec5SDimitry Andric     default:
2014*06c3fb27SDimitry Andric       return Tok.isRegularKeywordAttribute();
20150b57cec5SDimitry Andric     }
20160b57cec5SDimitry Andric 
20170b57cec5SDimitry Andric   default:
2018*06c3fb27SDimitry Andric     return Tok.isRegularKeywordAttribute();
20190b57cec5SDimitry Andric   }
20200b57cec5SDimitry Andric }
20210b57cec5SDimitry Andric 
20220b57cec5SDimitry Andric /// Skip until we reach something which seems like a sensible place to pick
20230b57cec5SDimitry Andric /// up parsing after a malformed declaration. This will sometimes stop sooner
20240b57cec5SDimitry Andric /// than SkipUntil(tok::r_brace) would, but will never stop later.
20250b57cec5SDimitry Andric void Parser::SkipMalformedDecl() {
20260b57cec5SDimitry Andric   while (true) {
20270b57cec5SDimitry Andric     switch (Tok.getKind()) {
20280b57cec5SDimitry Andric     case tok::l_brace:
20290b57cec5SDimitry Andric       // Skip until matching }, then stop. We've probably skipped over
20300b57cec5SDimitry Andric       // a malformed class or function definition or similar.
20310b57cec5SDimitry Andric       ConsumeBrace();
20320b57cec5SDimitry Andric       SkipUntil(tok::r_brace);
20330b57cec5SDimitry Andric       if (Tok.isOneOf(tok::comma, tok::l_brace, tok::kw_try)) {
20340b57cec5SDimitry Andric         // This declaration isn't over yet. Keep skipping.
20350b57cec5SDimitry Andric         continue;
20360b57cec5SDimitry Andric       }
20370b57cec5SDimitry Andric       TryConsumeToken(tok::semi);
20380b57cec5SDimitry Andric       return;
20390b57cec5SDimitry Andric 
20400b57cec5SDimitry Andric     case tok::l_square:
20410b57cec5SDimitry Andric       ConsumeBracket();
20420b57cec5SDimitry Andric       SkipUntil(tok::r_square);
20430b57cec5SDimitry Andric       continue;
20440b57cec5SDimitry Andric 
20450b57cec5SDimitry Andric     case tok::l_paren:
20460b57cec5SDimitry Andric       ConsumeParen();
20470b57cec5SDimitry Andric       SkipUntil(tok::r_paren);
20480b57cec5SDimitry Andric       continue;
20490b57cec5SDimitry Andric 
20500b57cec5SDimitry Andric     case tok::r_brace:
20510b57cec5SDimitry Andric       return;
20520b57cec5SDimitry Andric 
20530b57cec5SDimitry Andric     case tok::semi:
20540b57cec5SDimitry Andric       ConsumeToken();
20550b57cec5SDimitry Andric       return;
20560b57cec5SDimitry Andric 
20570b57cec5SDimitry Andric     case tok::kw_inline:
20580b57cec5SDimitry Andric       // 'inline namespace' at the start of a line is almost certainly
20590b57cec5SDimitry Andric       // a good place to pick back up parsing, except in an Objective-C
20600b57cec5SDimitry Andric       // @interface context.
20610b57cec5SDimitry Andric       if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
20620b57cec5SDimitry Andric           (!ParsingInObjCContainer || CurParsedObjCImpl))
20630b57cec5SDimitry Andric         return;
20640b57cec5SDimitry Andric       break;
20650b57cec5SDimitry Andric 
20660b57cec5SDimitry Andric     case tok::kw_namespace:
20670b57cec5SDimitry Andric       // 'namespace' at the start of a line is almost certainly a good
20680b57cec5SDimitry Andric       // place to pick back up parsing, except in an Objective-C
20690b57cec5SDimitry Andric       // @interface context.
20700b57cec5SDimitry Andric       if (Tok.isAtStartOfLine() &&
20710b57cec5SDimitry Andric           (!ParsingInObjCContainer || CurParsedObjCImpl))
20720b57cec5SDimitry Andric         return;
20730b57cec5SDimitry Andric       break;
20740b57cec5SDimitry Andric 
20750b57cec5SDimitry Andric     case tok::at:
20760b57cec5SDimitry Andric       // @end is very much like } in Objective-C contexts.
20770b57cec5SDimitry Andric       if (NextToken().isObjCAtKeyword(tok::objc_end) &&
20780b57cec5SDimitry Andric           ParsingInObjCContainer)
20790b57cec5SDimitry Andric         return;
20800b57cec5SDimitry Andric       break;
20810b57cec5SDimitry Andric 
20820b57cec5SDimitry Andric     case tok::minus:
20830b57cec5SDimitry Andric     case tok::plus:
20840b57cec5SDimitry Andric       // - and + probably start new method declarations in Objective-C contexts.
20850b57cec5SDimitry Andric       if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
20860b57cec5SDimitry Andric         return;
20870b57cec5SDimitry Andric       break;
20880b57cec5SDimitry Andric 
20890b57cec5SDimitry Andric     case tok::eof:
20900b57cec5SDimitry Andric     case tok::annot_module_begin:
20910b57cec5SDimitry Andric     case tok::annot_module_end:
20920b57cec5SDimitry Andric     case tok::annot_module_include:
2093*06c3fb27SDimitry Andric     case tok::annot_repl_input_end:
20940b57cec5SDimitry Andric       return;
20950b57cec5SDimitry Andric 
20960b57cec5SDimitry Andric     default:
20970b57cec5SDimitry Andric       break;
20980b57cec5SDimitry Andric     }
20990b57cec5SDimitry Andric 
21000b57cec5SDimitry Andric     ConsumeAnyToken();
21010b57cec5SDimitry Andric   }
21020b57cec5SDimitry Andric }
21030b57cec5SDimitry Andric 
21040b57cec5SDimitry Andric /// ParseDeclGroup - Having concluded that this is either a function
21050b57cec5SDimitry Andric /// definition or a group of object declarations, actually parse the
21060b57cec5SDimitry Andric /// result.
21070b57cec5SDimitry Andric Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
21080b57cec5SDimitry Andric                                               DeclaratorContext Context,
210981ad6265SDimitry Andric                                               ParsedAttributes &Attrs,
21100b57cec5SDimitry Andric                                               SourceLocation *DeclEnd,
21110b57cec5SDimitry Andric                                               ForRangeInit *FRI) {
21120b57cec5SDimitry Andric   // Parse the first declarator.
211381ad6265SDimitry Andric   // Consume all of the attributes from `Attrs` by moving them to our own local
211481ad6265SDimitry Andric   // list. This ensures that we will not attempt to interpret them as statement
211581ad6265SDimitry Andric   // attributes higher up the callchain.
211681ad6265SDimitry Andric   ParsedAttributes LocalAttrs(AttrFactory);
211781ad6265SDimitry Andric   LocalAttrs.takeAllFrom(Attrs);
211881ad6265SDimitry Andric   ParsingDeclarator D(*this, DS, LocalAttrs, Context);
21190b57cec5SDimitry Andric   ParseDeclarator(D);
21200b57cec5SDimitry Andric 
21210b57cec5SDimitry Andric   // Bail out if the first declarator didn't seem well-formed.
21220b57cec5SDimitry Andric   if (!D.hasName() && !D.mayOmitIdentifier()) {
21230b57cec5SDimitry Andric     SkipMalformedDecl();
21240b57cec5SDimitry Andric     return nullptr;
21250b57cec5SDimitry Andric   }
21260b57cec5SDimitry Andric 
2127bdd1243dSDimitry Andric   if (getLangOpts().HLSL)
2128bdd1243dSDimitry Andric     MaybeParseHLSLSemantics(D);
2129bdd1243dSDimitry Andric 
2130480093f4SDimitry Andric   if (Tok.is(tok::kw_requires))
2131480093f4SDimitry Andric     ParseTrailingRequiresClause(D);
2132480093f4SDimitry Andric 
21330b57cec5SDimitry Andric   // Save late-parsed attributes for now; they need to be parsed in the
21340b57cec5SDimitry Andric   // appropriate function scope after the function Decl has been constructed.
21350b57cec5SDimitry Andric   // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
21360b57cec5SDimitry Andric   LateParsedAttrList LateParsedAttrs(true);
21370b57cec5SDimitry Andric   if (D.isFunctionDeclarator()) {
21380b57cec5SDimitry Andric     MaybeParseGNUAttributes(D, &LateParsedAttrs);
21390b57cec5SDimitry Andric 
21400b57cec5SDimitry Andric     // The _Noreturn keyword can't appear here, unlike the GNU noreturn
21410b57cec5SDimitry Andric     // attribute. If we find the keyword here, tell the user to put it
21420b57cec5SDimitry Andric     // at the start instead.
21430b57cec5SDimitry Andric     if (Tok.is(tok::kw__Noreturn)) {
21440b57cec5SDimitry Andric       SourceLocation Loc = ConsumeToken();
21450b57cec5SDimitry Andric       const char *PrevSpec;
21460b57cec5SDimitry Andric       unsigned DiagID;
21470b57cec5SDimitry Andric 
21480b57cec5SDimitry Andric       // We can offer a fixit if it's valid to mark this function as _Noreturn
21490b57cec5SDimitry Andric       // and we don't have any other declarators in this declaration.
21500b57cec5SDimitry Andric       bool Fixit = !DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
21510b57cec5SDimitry Andric       MaybeParseGNUAttributes(D, &LateParsedAttrs);
21520b57cec5SDimitry Andric       Fixit &= Tok.isOneOf(tok::semi, tok::l_brace, tok::kw_try);
21530b57cec5SDimitry Andric 
21540b57cec5SDimitry Andric       Diag(Loc, diag::err_c11_noreturn_misplaced)
21550b57cec5SDimitry Andric           << (Fixit ? FixItHint::CreateRemoval(Loc) : FixItHint())
21560b57cec5SDimitry Andric           << (Fixit ? FixItHint::CreateInsertion(D.getBeginLoc(), "_Noreturn ")
21570b57cec5SDimitry Andric                     : FixItHint());
21580b57cec5SDimitry Andric     }
21590b57cec5SDimitry Andric 
21600b57cec5SDimitry Andric     // Check to see if we have a function *definition* which must have a body.
21615ffd83dbSDimitry Andric     if (Tok.is(tok::equal) && NextToken().is(tok::code_completion)) {
21625ffd83dbSDimitry Andric       cutOffParsing();
2163fe6060f1SDimitry Andric       Actions.CodeCompleteAfterFunctionEquals(D);
21645ffd83dbSDimitry Andric       return nullptr;
21655ffd83dbSDimitry Andric     }
2166349cc55cSDimitry Andric     // We're at the point where the parsing of function declarator is finished.
2167349cc55cSDimitry Andric     //
2168349cc55cSDimitry Andric     // A common error is that users accidently add a virtual specifier
2169349cc55cSDimitry Andric     // (e.g. override) in an out-line method definition.
2170349cc55cSDimitry Andric     // We attempt to recover by stripping all these specifiers coming after
2171349cc55cSDimitry Andric     // the declarator.
2172349cc55cSDimitry Andric     while (auto Specifier = isCXX11VirtSpecifier()) {
2173349cc55cSDimitry Andric       Diag(Tok, diag::err_virt_specifier_outside_class)
2174349cc55cSDimitry Andric           << VirtSpecifiers::getSpecifierName(Specifier)
2175349cc55cSDimitry Andric           << FixItHint::CreateRemoval(Tok.getLocation());
2176349cc55cSDimitry Andric       ConsumeToken();
2177349cc55cSDimitry Andric     }
21780b57cec5SDimitry Andric     // Look at the next token to make sure that this isn't a function
21790b57cec5SDimitry Andric     // declaration.  We have to check this because __attribute__ might be the
21800b57cec5SDimitry Andric     // start of a function definition in GCC-extended K&R C.
21815ffd83dbSDimitry Andric     if (!isDeclarationAfterDeclarator()) {
21820b57cec5SDimitry Andric 
21830b57cec5SDimitry Andric       // Function definitions are only allowed at file scope and in C++ classes.
21840b57cec5SDimitry Andric       // The C++ inline method definition case is handled elsewhere, so we only
21850b57cec5SDimitry Andric       // need to handle the file scope definition case.
2186e8d8bef9SDimitry Andric       if (Context == DeclaratorContext::File) {
21870b57cec5SDimitry Andric         if (isStartOfFunctionDefinition(D)) {
21880b57cec5SDimitry Andric           if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
21890b57cec5SDimitry Andric             Diag(Tok, diag::err_function_declared_typedef);
21900b57cec5SDimitry Andric 
21910b57cec5SDimitry Andric             // Recover by treating the 'typedef' as spurious.
21920b57cec5SDimitry Andric             DS.ClearStorageClassSpecs();
21930b57cec5SDimitry Andric           }
21940b57cec5SDimitry Andric 
21955ffd83dbSDimitry Andric           Decl *TheDecl = ParseFunctionDefinition(D, ParsedTemplateInfo(),
21965ffd83dbSDimitry Andric                                                   &LateParsedAttrs);
21970b57cec5SDimitry Andric           return Actions.ConvertDeclToDeclGroup(TheDecl);
21980b57cec5SDimitry Andric         }
21990b57cec5SDimitry Andric 
2200*06c3fb27SDimitry Andric         if (isDeclarationSpecifier(ImplicitTypenameContext::No) ||
2201*06c3fb27SDimitry Andric             Tok.is(tok::kw_namespace)) {
2202*06c3fb27SDimitry Andric           // If there is an invalid declaration specifier or a namespace
2203*06c3fb27SDimitry Andric           // definition right after the function prototype, then we must be in a
2204*06c3fb27SDimitry Andric           // missing semicolon case where this isn't actually a body.  Just fall
2205*06c3fb27SDimitry Andric           // through into the code that handles it as a prototype, and let the
2206*06c3fb27SDimitry Andric           // top-level code handle the erroneous declspec where it would
2207*06c3fb27SDimitry Andric           // otherwise expect a comma or semicolon. Note that
2208*06c3fb27SDimitry Andric           // isDeclarationSpecifier already covers 'inline namespace', since
2209*06c3fb27SDimitry Andric           // 'inline' can be a declaration specifier.
22100b57cec5SDimitry Andric         } else {
22110b57cec5SDimitry Andric           Diag(Tok, diag::err_expected_fn_body);
22120b57cec5SDimitry Andric           SkipUntil(tok::semi);
22130b57cec5SDimitry Andric           return nullptr;
22140b57cec5SDimitry Andric         }
22150b57cec5SDimitry Andric       } else {
22160b57cec5SDimitry Andric         if (Tok.is(tok::l_brace)) {
22170b57cec5SDimitry Andric           Diag(Tok, diag::err_function_definition_not_allowed);
22180b57cec5SDimitry Andric           SkipMalformedDecl();
22190b57cec5SDimitry Andric           return nullptr;
22200b57cec5SDimitry Andric         }
22210b57cec5SDimitry Andric       }
22220b57cec5SDimitry Andric     }
22235ffd83dbSDimitry Andric   }
22240b57cec5SDimitry Andric 
22250b57cec5SDimitry Andric   if (ParseAsmAttributesAfterDeclarator(D))
22260b57cec5SDimitry Andric     return nullptr;
22270b57cec5SDimitry Andric 
22280b57cec5SDimitry Andric   // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
22290b57cec5SDimitry Andric   // must parse and analyze the for-range-initializer before the declaration is
22300b57cec5SDimitry Andric   // analyzed.
22310b57cec5SDimitry Andric   //
22320b57cec5SDimitry Andric   // Handle the Objective-C for-in loop variable similarly, although we
22330b57cec5SDimitry Andric   // don't need to parse the container in advance.
22340b57cec5SDimitry Andric   if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
22350b57cec5SDimitry Andric     bool IsForRangeLoop = false;
22360b57cec5SDimitry Andric     if (TryConsumeToken(tok::colon, FRI->ColonLoc)) {
22370b57cec5SDimitry Andric       IsForRangeLoop = true;
2238a7dea167SDimitry Andric       if (getLangOpts().OpenMP)
2239a7dea167SDimitry Andric         Actions.startOpenMPCXXRangeFor();
22400b57cec5SDimitry Andric       if (Tok.is(tok::l_brace))
22410b57cec5SDimitry Andric         FRI->RangeExpr = ParseBraceInitializer();
22420b57cec5SDimitry Andric       else
22430b57cec5SDimitry Andric         FRI->RangeExpr = ParseExpression();
22440b57cec5SDimitry Andric     }
22450b57cec5SDimitry Andric 
22460b57cec5SDimitry Andric     Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
22470b57cec5SDimitry Andric     if (IsForRangeLoop) {
22480b57cec5SDimitry Andric       Actions.ActOnCXXForRangeDecl(ThisDecl);
22490b57cec5SDimitry Andric     } else {
22500b57cec5SDimitry Andric       // Obj-C for loop
22510b57cec5SDimitry Andric       if (auto *VD = dyn_cast_or_null<VarDecl>(ThisDecl))
22520b57cec5SDimitry Andric         VD->setObjCForDecl(true);
22530b57cec5SDimitry Andric     }
22540b57cec5SDimitry Andric     Actions.FinalizeDeclaration(ThisDecl);
22550b57cec5SDimitry Andric     D.complete(ThisDecl);
22560b57cec5SDimitry Andric     return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
22570b57cec5SDimitry Andric   }
22580b57cec5SDimitry Andric 
22590b57cec5SDimitry Andric   SmallVector<Decl *, 8> DeclsInGroup;
22600b57cec5SDimitry Andric   Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(
22610b57cec5SDimitry Andric       D, ParsedTemplateInfo(), FRI);
22620b57cec5SDimitry Andric   if (LateParsedAttrs.size() > 0)
22630b57cec5SDimitry Andric     ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
22640b57cec5SDimitry Andric   D.complete(FirstDecl);
22650b57cec5SDimitry Andric   if (FirstDecl)
22660b57cec5SDimitry Andric     DeclsInGroup.push_back(FirstDecl);
22670b57cec5SDimitry Andric 
2268e8d8bef9SDimitry Andric   bool ExpectSemi = Context != DeclaratorContext::ForInit;
22690b57cec5SDimitry Andric 
22700b57cec5SDimitry Andric   // If we don't have a comma, it is either the end of the list (a ';') or an
22710b57cec5SDimitry Andric   // error, bail out.
22720b57cec5SDimitry Andric   SourceLocation CommaLoc;
22730b57cec5SDimitry Andric   while (TryConsumeToken(tok::comma, CommaLoc)) {
22740b57cec5SDimitry Andric     if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
22750b57cec5SDimitry Andric       // This comma was followed by a line-break and something which can't be
22760b57cec5SDimitry Andric       // the start of a declarator. The comma was probably a typo for a
22770b57cec5SDimitry Andric       // semicolon.
22780b57cec5SDimitry Andric       Diag(CommaLoc, diag::err_expected_semi_declaration)
22790b57cec5SDimitry Andric         << FixItHint::CreateReplacement(CommaLoc, ";");
22800b57cec5SDimitry Andric       ExpectSemi = false;
22810b57cec5SDimitry Andric       break;
22820b57cec5SDimitry Andric     }
22830b57cec5SDimitry Andric 
22840b57cec5SDimitry Andric     // Parse the next declarator.
22850b57cec5SDimitry Andric     D.clear();
22860b57cec5SDimitry Andric     D.setCommaLoc(CommaLoc);
22870b57cec5SDimitry Andric 
22880b57cec5SDimitry Andric     // Accept attributes in an init-declarator.  In the first declarator in a
22890b57cec5SDimitry Andric     // declaration, these would be part of the declspec.  In subsequent
22900b57cec5SDimitry Andric     // declarators, they become part of the declarator itself, so that they
22910b57cec5SDimitry Andric     // don't apply to declarators after *this* one.  Examples:
22920b57cec5SDimitry Andric     //    short __attribute__((common)) var;    -> declspec
22930b57cec5SDimitry Andric     //    short var __attribute__((common));    -> declarator
22940b57cec5SDimitry Andric     //    short x, __attribute__((common)) var;    -> declarator
22950b57cec5SDimitry Andric     MaybeParseGNUAttributes(D);
22960b57cec5SDimitry Andric 
22970b57cec5SDimitry Andric     // MSVC parses but ignores qualifiers after the comma as an extension.
22980b57cec5SDimitry Andric     if (getLangOpts().MicrosoftExt)
22990b57cec5SDimitry Andric       DiagnoseAndSkipExtendedMicrosoftTypeAttributes();
23000b57cec5SDimitry Andric 
23010b57cec5SDimitry Andric     ParseDeclarator(D);
2302bdd1243dSDimitry Andric 
2303bdd1243dSDimitry Andric     if (getLangOpts().HLSL)
2304bdd1243dSDimitry Andric       MaybeParseHLSLSemantics(D);
2305bdd1243dSDimitry Andric 
23060b57cec5SDimitry Andric     if (!D.isInvalidType()) {
2307480093f4SDimitry Andric       // C++2a [dcl.decl]p1
2308480093f4SDimitry Andric       //    init-declarator:
2309480093f4SDimitry Andric       //	      declarator initializer[opt]
2310480093f4SDimitry Andric       //        declarator requires-clause
2311480093f4SDimitry Andric       if (Tok.is(tok::kw_requires))
2312480093f4SDimitry Andric         ParseTrailingRequiresClause(D);
23130b57cec5SDimitry Andric       Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
23140b57cec5SDimitry Andric       D.complete(ThisDecl);
23150b57cec5SDimitry Andric       if (ThisDecl)
23160b57cec5SDimitry Andric         DeclsInGroup.push_back(ThisDecl);
23170b57cec5SDimitry Andric     }
23180b57cec5SDimitry Andric   }
23190b57cec5SDimitry Andric 
23200b57cec5SDimitry Andric   if (DeclEnd)
23210b57cec5SDimitry Andric     *DeclEnd = Tok.getLocation();
23220b57cec5SDimitry Andric 
2323e8d8bef9SDimitry Andric   if (ExpectSemi && ExpectAndConsumeSemi(
2324e8d8bef9SDimitry Andric                         Context == DeclaratorContext::File
23250b57cec5SDimitry Andric                             ? diag::err_invalid_token_after_toplevel_declarator
23260b57cec5SDimitry Andric                             : diag::err_expected_semi_declaration)) {
23270b57cec5SDimitry Andric     // Okay, there was no semicolon and one was expected.  If we see a
23280b57cec5SDimitry Andric     // declaration specifier, just assume it was missing and continue parsing.
23290b57cec5SDimitry Andric     // Otherwise things are very confused and we skip to recover.
2330*06c3fb27SDimitry Andric     if (!isDeclarationSpecifier(ImplicitTypenameContext::No))
2331*06c3fb27SDimitry Andric       SkipMalformedDecl();
23320b57cec5SDimitry Andric   }
23330b57cec5SDimitry Andric 
23340b57cec5SDimitry Andric   return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
23350b57cec5SDimitry Andric }
23360b57cec5SDimitry Andric 
23370b57cec5SDimitry Andric /// Parse an optional simple-asm-expr and attributes, and attach them to a
23380b57cec5SDimitry Andric /// declarator. Returns true on an error.
23390b57cec5SDimitry Andric bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
23400b57cec5SDimitry Andric   // If a simple-asm-expr is present, parse it.
23410b57cec5SDimitry Andric   if (Tok.is(tok::kw_asm)) {
23420b57cec5SDimitry Andric     SourceLocation Loc;
2343480093f4SDimitry Andric     ExprResult AsmLabel(ParseSimpleAsm(/*ForAsmLabel*/ true, &Loc));
23440b57cec5SDimitry Andric     if (AsmLabel.isInvalid()) {
23450b57cec5SDimitry Andric       SkipUntil(tok::semi, StopBeforeMatch);
23460b57cec5SDimitry Andric       return true;
23470b57cec5SDimitry Andric     }
23480b57cec5SDimitry Andric 
23490b57cec5SDimitry Andric     D.setAsmLabel(AsmLabel.get());
23500b57cec5SDimitry Andric     D.SetRangeEnd(Loc);
23510b57cec5SDimitry Andric   }
23520b57cec5SDimitry Andric 
23530b57cec5SDimitry Andric   MaybeParseGNUAttributes(D);
23540b57cec5SDimitry Andric   return false;
23550b57cec5SDimitry Andric }
23560b57cec5SDimitry Andric 
23570b57cec5SDimitry Andric /// Parse 'declaration' after parsing 'declaration-specifiers
23580b57cec5SDimitry Andric /// declarator'. This method parses the remainder of the declaration
23590b57cec5SDimitry Andric /// (including any attributes or initializer, among other things) and
23600b57cec5SDimitry Andric /// finalizes the declaration.
23610b57cec5SDimitry Andric ///
23620b57cec5SDimitry Andric ///       init-declarator: [C99 6.7]
23630b57cec5SDimitry Andric ///         declarator
23640b57cec5SDimitry Andric ///         declarator '=' initializer
23650b57cec5SDimitry Andric /// [GNU]   declarator simple-asm-expr[opt] attributes[opt]
23660b57cec5SDimitry Andric /// [GNU]   declarator simple-asm-expr[opt] attributes[opt] '=' initializer
23670b57cec5SDimitry Andric /// [C++]   declarator initializer[opt]
23680b57cec5SDimitry Andric ///
23690b57cec5SDimitry Andric /// [C++] initializer:
23700b57cec5SDimitry Andric /// [C++]   '=' initializer-clause
23710b57cec5SDimitry Andric /// [C++]   '(' expression-list ')'
23720b57cec5SDimitry Andric /// [C++0x] '=' 'default'                                                [TODO]
23730b57cec5SDimitry Andric /// [C++0x] '=' 'delete'
23740b57cec5SDimitry Andric /// [C++0x] braced-init-list
23750b57cec5SDimitry Andric ///
23760b57cec5SDimitry Andric /// According to the standard grammar, =default and =delete are function
23770b57cec5SDimitry Andric /// definitions, but that definitely doesn't fit with the parser here.
23780b57cec5SDimitry Andric ///
23790b57cec5SDimitry Andric Decl *Parser::ParseDeclarationAfterDeclarator(
23800b57cec5SDimitry Andric     Declarator &D, const ParsedTemplateInfo &TemplateInfo) {
23810b57cec5SDimitry Andric   if (ParseAsmAttributesAfterDeclarator(D))
23820b57cec5SDimitry Andric     return nullptr;
23830b57cec5SDimitry Andric 
23840b57cec5SDimitry Andric   return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
23850b57cec5SDimitry Andric }
23860b57cec5SDimitry Andric 
23870b57cec5SDimitry Andric Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(
23880b57cec5SDimitry Andric     Declarator &D, const ParsedTemplateInfo &TemplateInfo, ForRangeInit *FRI) {
23890b57cec5SDimitry Andric   // RAII type used to track whether we're inside an initializer.
23900b57cec5SDimitry Andric   struct InitializerScopeRAII {
23910b57cec5SDimitry Andric     Parser &P;
23920b57cec5SDimitry Andric     Declarator &D;
23930b57cec5SDimitry Andric     Decl *ThisDecl;
23940b57cec5SDimitry Andric 
23950b57cec5SDimitry Andric     InitializerScopeRAII(Parser &P, Declarator &D, Decl *ThisDecl)
23960b57cec5SDimitry Andric         : P(P), D(D), ThisDecl(ThisDecl) {
23970b57cec5SDimitry Andric       if (ThisDecl && P.getLangOpts().CPlusPlus) {
23980b57cec5SDimitry Andric         Scope *S = nullptr;
23990b57cec5SDimitry Andric         if (D.getCXXScopeSpec().isSet()) {
24000b57cec5SDimitry Andric           P.EnterScope(0);
24010b57cec5SDimitry Andric           S = P.getCurScope();
24020b57cec5SDimitry Andric         }
24030b57cec5SDimitry Andric         P.Actions.ActOnCXXEnterDeclInitializer(S, ThisDecl);
24040b57cec5SDimitry Andric       }
24050b57cec5SDimitry Andric     }
24060b57cec5SDimitry Andric     ~InitializerScopeRAII() { pop(); }
24070b57cec5SDimitry Andric     void pop() {
24080b57cec5SDimitry Andric       if (ThisDecl && P.getLangOpts().CPlusPlus) {
24090b57cec5SDimitry Andric         Scope *S = nullptr;
24100b57cec5SDimitry Andric         if (D.getCXXScopeSpec().isSet())
24110b57cec5SDimitry Andric           S = P.getCurScope();
24120b57cec5SDimitry Andric         P.Actions.ActOnCXXExitDeclInitializer(S, ThisDecl);
24130b57cec5SDimitry Andric         if (S)
24140b57cec5SDimitry Andric           P.ExitScope();
24150b57cec5SDimitry Andric       }
24160b57cec5SDimitry Andric       ThisDecl = nullptr;
24170b57cec5SDimitry Andric     }
24180b57cec5SDimitry Andric   };
24190b57cec5SDimitry Andric 
2420e8d8bef9SDimitry Andric   enum class InitKind { Uninitialized, Equal, CXXDirect, CXXBraced };
2421e8d8bef9SDimitry Andric   InitKind TheInitKind;
2422e8d8bef9SDimitry Andric   // If a '==' or '+=' is found, suggest a fixit to '='.
2423e8d8bef9SDimitry Andric   if (isTokenEqualOrEqualTypo())
2424e8d8bef9SDimitry Andric     TheInitKind = InitKind::Equal;
2425e8d8bef9SDimitry Andric   else if (Tok.is(tok::l_paren))
2426e8d8bef9SDimitry Andric     TheInitKind = InitKind::CXXDirect;
2427e8d8bef9SDimitry Andric   else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
2428e8d8bef9SDimitry Andric            (!CurParsedObjCImpl || !D.isFunctionDeclarator()))
2429e8d8bef9SDimitry Andric     TheInitKind = InitKind::CXXBraced;
2430e8d8bef9SDimitry Andric   else
2431e8d8bef9SDimitry Andric     TheInitKind = InitKind::Uninitialized;
2432e8d8bef9SDimitry Andric   if (TheInitKind != InitKind::Uninitialized)
2433e8d8bef9SDimitry Andric     D.setHasInitializer();
2434e8d8bef9SDimitry Andric 
2435e8d8bef9SDimitry Andric   // Inform Sema that we just parsed this declarator.
24360b57cec5SDimitry Andric   Decl *ThisDecl = nullptr;
2437e8d8bef9SDimitry Andric   Decl *OuterDecl = nullptr;
24380b57cec5SDimitry Andric   switch (TemplateInfo.Kind) {
24390b57cec5SDimitry Andric   case ParsedTemplateInfo::NonTemplate:
24400b57cec5SDimitry Andric     ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
24410b57cec5SDimitry Andric     break;
24420b57cec5SDimitry Andric 
24430b57cec5SDimitry Andric   case ParsedTemplateInfo::Template:
24440b57cec5SDimitry Andric   case ParsedTemplateInfo::ExplicitSpecialization: {
24450b57cec5SDimitry Andric     ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
24460b57cec5SDimitry Andric                                                *TemplateInfo.TemplateParams,
24470b57cec5SDimitry Andric                                                D);
2448e8d8bef9SDimitry Andric     if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) {
24490b57cec5SDimitry Andric       // Re-direct this decl to refer to the templated decl so that we can
24500b57cec5SDimitry Andric       // initialize it.
24510b57cec5SDimitry Andric       ThisDecl = VT->getTemplatedDecl();
2452e8d8bef9SDimitry Andric       OuterDecl = VT;
2453e8d8bef9SDimitry Andric     }
24540b57cec5SDimitry Andric     break;
24550b57cec5SDimitry Andric   }
24560b57cec5SDimitry Andric   case ParsedTemplateInfo::ExplicitInstantiation: {
24570b57cec5SDimitry Andric     if (Tok.is(tok::semi)) {
24580b57cec5SDimitry Andric       DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
24590b57cec5SDimitry Andric           getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
24600b57cec5SDimitry Andric       if (ThisRes.isInvalid()) {
24610b57cec5SDimitry Andric         SkipUntil(tok::semi, StopBeforeMatch);
24620b57cec5SDimitry Andric         return nullptr;
24630b57cec5SDimitry Andric       }
24640b57cec5SDimitry Andric       ThisDecl = ThisRes.get();
24650b57cec5SDimitry Andric     } else {
24660b57cec5SDimitry Andric       // FIXME: This check should be for a variable template instantiation only.
24670b57cec5SDimitry Andric 
24680b57cec5SDimitry Andric       // Check that this is a valid instantiation
24690b57cec5SDimitry Andric       if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) {
24700b57cec5SDimitry Andric         // If the declarator-id is not a template-id, issue a diagnostic and
24710b57cec5SDimitry Andric         // recover by ignoring the 'template' keyword.
24720b57cec5SDimitry Andric         Diag(Tok, diag::err_template_defn_explicit_instantiation)
24730b57cec5SDimitry Andric             << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
24740b57cec5SDimitry Andric         ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
24750b57cec5SDimitry Andric       } else {
24760b57cec5SDimitry Andric         SourceLocation LAngleLoc =
24770b57cec5SDimitry Andric             PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
24780b57cec5SDimitry Andric         Diag(D.getIdentifierLoc(),
24790b57cec5SDimitry Andric              diag::err_explicit_instantiation_with_definition)
24800b57cec5SDimitry Andric             << SourceRange(TemplateInfo.TemplateLoc)
24810b57cec5SDimitry Andric             << FixItHint::CreateInsertion(LAngleLoc, "<>");
24820b57cec5SDimitry Andric 
24830b57cec5SDimitry Andric         // Recover as if it were an explicit specialization.
24840b57cec5SDimitry Andric         TemplateParameterLists FakedParamLists;
24850b57cec5SDimitry Andric         FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
2486bdd1243dSDimitry Andric             0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc,
2487bdd1243dSDimitry Andric             std::nullopt, LAngleLoc, nullptr));
24880b57cec5SDimitry Andric 
24890b57cec5SDimitry Andric         ThisDecl =
24900b57cec5SDimitry Andric             Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
24910b57cec5SDimitry Andric       }
24920b57cec5SDimitry Andric     }
24930b57cec5SDimitry Andric     break;
24940b57cec5SDimitry Andric     }
24950b57cec5SDimitry Andric   }
24960b57cec5SDimitry Andric 
2497e8d8bef9SDimitry Andric   switch (TheInitKind) {
24980b57cec5SDimitry Andric   // Parse declarator '=' initializer.
2499e8d8bef9SDimitry Andric   case InitKind::Equal: {
25000b57cec5SDimitry Andric     SourceLocation EqualLoc = ConsumeToken();
25010b57cec5SDimitry Andric 
25020b57cec5SDimitry Andric     if (Tok.is(tok::kw_delete)) {
25030b57cec5SDimitry Andric       if (D.isFunctionDeclarator())
25040b57cec5SDimitry Andric         Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
25050b57cec5SDimitry Andric           << 1 /* delete */;
25060b57cec5SDimitry Andric       else
25070b57cec5SDimitry Andric         Diag(ConsumeToken(), diag::err_deleted_non_function);
25080b57cec5SDimitry Andric     } else if (Tok.is(tok::kw_default)) {
25090b57cec5SDimitry Andric       if (D.isFunctionDeclarator())
25100b57cec5SDimitry Andric         Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
25110b57cec5SDimitry Andric           << 0 /* default */;
25120b57cec5SDimitry Andric       else
2513480093f4SDimitry Andric         Diag(ConsumeToken(), diag::err_default_special_members)
25145ffd83dbSDimitry Andric             << getLangOpts().CPlusPlus20;
25150b57cec5SDimitry Andric     } else {
25160b57cec5SDimitry Andric       InitializerScopeRAII InitScope(*this, D, ThisDecl);
25170b57cec5SDimitry Andric 
25180b57cec5SDimitry Andric       if (Tok.is(tok::code_completion)) {
2519fe6060f1SDimitry Andric         cutOffParsing();
25200b57cec5SDimitry Andric         Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
25210b57cec5SDimitry Andric         Actions.FinalizeDeclaration(ThisDecl);
25220b57cec5SDimitry Andric         return nullptr;
25230b57cec5SDimitry Andric       }
25240b57cec5SDimitry Andric 
25250b57cec5SDimitry Andric       PreferredType.enterVariableInit(Tok.getLocation(), ThisDecl);
25260b57cec5SDimitry Andric       ExprResult Init = ParseInitializer();
25270b57cec5SDimitry Andric 
25280b57cec5SDimitry Andric       // If this is the only decl in (possibly) range based for statement,
25290b57cec5SDimitry Andric       // our best guess is that the user meant ':' instead of '='.
25300b57cec5SDimitry Andric       if (Tok.is(tok::r_paren) && FRI && D.isFirstDeclarator()) {
25310b57cec5SDimitry Andric         Diag(EqualLoc, diag::err_single_decl_assign_in_for_range)
25320b57cec5SDimitry Andric             << FixItHint::CreateReplacement(EqualLoc, ":");
25330b57cec5SDimitry Andric         // We are trying to stop parser from looking for ';' in this for
25340b57cec5SDimitry Andric         // statement, therefore preventing spurious errors to be issued.
25350b57cec5SDimitry Andric         FRI->ColonLoc = EqualLoc;
25360b57cec5SDimitry Andric         Init = ExprError();
25370b57cec5SDimitry Andric         FRI->RangeExpr = Init;
25380b57cec5SDimitry Andric       }
25390b57cec5SDimitry Andric 
25400b57cec5SDimitry Andric       InitScope.pop();
25410b57cec5SDimitry Andric 
25420b57cec5SDimitry Andric       if (Init.isInvalid()) {
25430b57cec5SDimitry Andric         SmallVector<tok::TokenKind, 2> StopTokens;
25440b57cec5SDimitry Andric         StopTokens.push_back(tok::comma);
2545e8d8bef9SDimitry Andric         if (D.getContext() == DeclaratorContext::ForInit ||
2546e8d8bef9SDimitry Andric             D.getContext() == DeclaratorContext::SelectionInit)
25470b57cec5SDimitry Andric           StopTokens.push_back(tok::r_paren);
25480b57cec5SDimitry Andric         SkipUntil(StopTokens, StopAtSemi | StopBeforeMatch);
25490b57cec5SDimitry Andric         Actions.ActOnInitializerError(ThisDecl);
25500b57cec5SDimitry Andric       } else
25510b57cec5SDimitry Andric         Actions.AddInitializerToDecl(ThisDecl, Init.get(),
25520b57cec5SDimitry Andric                                      /*DirectInit=*/false);
25530b57cec5SDimitry Andric     }
2554e8d8bef9SDimitry Andric     break;
2555e8d8bef9SDimitry Andric   }
2556e8d8bef9SDimitry Andric   case InitKind::CXXDirect: {
25570b57cec5SDimitry Andric     // Parse C++ direct initializer: '(' expression-list ')'
25580b57cec5SDimitry Andric     BalancedDelimiterTracker T(*this, tok::l_paren);
25590b57cec5SDimitry Andric     T.consumeOpen();
25600b57cec5SDimitry Andric 
25610b57cec5SDimitry Andric     ExprVector Exprs;
25620b57cec5SDimitry Andric 
25630b57cec5SDimitry Andric     InitializerScopeRAII InitScope(*this, D, ThisDecl);
25640b57cec5SDimitry Andric 
25650b57cec5SDimitry Andric     auto ThisVarDecl = dyn_cast_or_null<VarDecl>(ThisDecl);
25660b57cec5SDimitry Andric     auto RunSignatureHelp = [&]() {
25670b57cec5SDimitry Andric       QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
256804eeddc0SDimitry Andric           ThisVarDecl->getType()->getCanonicalTypeInternal(),
256904eeddc0SDimitry Andric           ThisDecl->getLocation(), Exprs, T.getOpenLocation(),
257004eeddc0SDimitry Andric           /*Braced=*/false);
25710b57cec5SDimitry Andric       CalledSignatureHelp = true;
25720b57cec5SDimitry Andric       return PreferredType;
25730b57cec5SDimitry Andric     };
25740b57cec5SDimitry Andric     auto SetPreferredType = [&] {
25750b57cec5SDimitry Andric       PreferredType.enterFunctionArgument(Tok.getLocation(), RunSignatureHelp);
25760b57cec5SDimitry Andric     };
25770b57cec5SDimitry Andric 
25780b57cec5SDimitry Andric     llvm::function_ref<void()> ExpressionStarts;
25790b57cec5SDimitry Andric     if (ThisVarDecl) {
25800b57cec5SDimitry Andric       // ParseExpressionList can sometimes succeed even when ThisDecl is not
25810b57cec5SDimitry Andric       // VarDecl. This is an error and it is reported in a call to
25820b57cec5SDimitry Andric       // Actions.ActOnInitializerError(). However, we call
25830b57cec5SDimitry Andric       // ProduceConstructorSignatureHelp only on VarDecls.
25840b57cec5SDimitry Andric       ExpressionStarts = SetPreferredType;
25850b57cec5SDimitry Andric     }
2586bdd1243dSDimitry Andric     if (ParseExpressionList(Exprs, ExpressionStarts)) {
25870b57cec5SDimitry Andric       if (ThisVarDecl && PP.isCodeCompletionReached() && !CalledSignatureHelp) {
25880b57cec5SDimitry Andric         Actions.ProduceConstructorSignatureHelp(
258904eeddc0SDimitry Andric             ThisVarDecl->getType()->getCanonicalTypeInternal(),
259004eeddc0SDimitry Andric             ThisDecl->getLocation(), Exprs, T.getOpenLocation(),
259104eeddc0SDimitry Andric             /*Braced=*/false);
25920b57cec5SDimitry Andric         CalledSignatureHelp = true;
25930b57cec5SDimitry Andric       }
25940b57cec5SDimitry Andric       Actions.ActOnInitializerError(ThisDecl);
25950b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
25960b57cec5SDimitry Andric     } else {
25970b57cec5SDimitry Andric       // Match the ')'.
25980b57cec5SDimitry Andric       T.consumeClose();
25990b57cec5SDimitry Andric       InitScope.pop();
26000b57cec5SDimitry Andric 
26010b57cec5SDimitry Andric       ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
26020b57cec5SDimitry Andric                                                           T.getCloseLocation(),
26030b57cec5SDimitry Andric                                                           Exprs);
26040b57cec5SDimitry Andric       Actions.AddInitializerToDecl(ThisDecl, Initializer.get(),
26050b57cec5SDimitry Andric                                    /*DirectInit=*/true);
26060b57cec5SDimitry Andric     }
2607e8d8bef9SDimitry Andric     break;
2608e8d8bef9SDimitry Andric   }
2609e8d8bef9SDimitry Andric   case InitKind::CXXBraced: {
26100b57cec5SDimitry Andric     // Parse C++0x braced-init-list.
26110b57cec5SDimitry Andric     Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
26120b57cec5SDimitry Andric 
26130b57cec5SDimitry Andric     InitializerScopeRAII InitScope(*this, D, ThisDecl);
26140b57cec5SDimitry Andric 
26155ffd83dbSDimitry Andric     PreferredType.enterVariableInit(Tok.getLocation(), ThisDecl);
26160b57cec5SDimitry Andric     ExprResult Init(ParseBraceInitializer());
26170b57cec5SDimitry Andric 
26180b57cec5SDimitry Andric     InitScope.pop();
26190b57cec5SDimitry Andric 
26200b57cec5SDimitry Andric     if (Init.isInvalid()) {
26210b57cec5SDimitry Andric       Actions.ActOnInitializerError(ThisDecl);
26220b57cec5SDimitry Andric     } else
26230b57cec5SDimitry Andric       Actions.AddInitializerToDecl(ThisDecl, Init.get(), /*DirectInit=*/true);
2624e8d8bef9SDimitry Andric     break;
2625e8d8bef9SDimitry Andric   }
2626e8d8bef9SDimitry Andric   case InitKind::Uninitialized: {
26270b57cec5SDimitry Andric     Actions.ActOnUninitializedDecl(ThisDecl);
2628e8d8bef9SDimitry Andric     break;
2629e8d8bef9SDimitry Andric   }
26300b57cec5SDimitry Andric   }
26310b57cec5SDimitry Andric 
26320b57cec5SDimitry Andric   Actions.FinalizeDeclaration(ThisDecl);
2633e8d8bef9SDimitry Andric   return OuterDecl ? OuterDecl : ThisDecl;
26340b57cec5SDimitry Andric }
26350b57cec5SDimitry Andric 
26360b57cec5SDimitry Andric /// ParseSpecifierQualifierList
26370b57cec5SDimitry Andric ///        specifier-qualifier-list:
26380b57cec5SDimitry Andric ///          type-specifier specifier-qualifier-list[opt]
26390b57cec5SDimitry Andric ///          type-qualifier specifier-qualifier-list[opt]
26400b57cec5SDimitry Andric /// [GNU]    attributes     specifier-qualifier-list[opt]
26410b57cec5SDimitry Andric ///
2642bdd1243dSDimitry Andric void Parser::ParseSpecifierQualifierList(
2643bdd1243dSDimitry Andric     DeclSpec &DS, ImplicitTypenameContext AllowImplicitTypename,
2644bdd1243dSDimitry Andric     AccessSpecifier AS, DeclSpecContext DSC) {
26450b57cec5SDimitry Andric   /// specifier-qualifier-list is a subset of declaration-specifiers.  Just
26460b57cec5SDimitry Andric   /// parse declaration-specifiers and complain about extra stuff.
26470b57cec5SDimitry Andric   /// TODO: diagnose attribute-specifiers and alignment-specifiers.
2648bdd1243dSDimitry Andric   ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC, nullptr,
2649bdd1243dSDimitry Andric                              AllowImplicitTypename);
26500b57cec5SDimitry Andric 
26510b57cec5SDimitry Andric   // Validate declspec for type-name.
26520b57cec5SDimitry Andric   unsigned Specs = DS.getParsedSpecifiers();
26530b57cec5SDimitry Andric   if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) {
26540b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_type);
26550b57cec5SDimitry Andric     DS.SetTypeSpecError();
26560b57cec5SDimitry Andric   } else if (Specs == DeclSpec::PQ_None && !DS.hasAttributes()) {
26570b57cec5SDimitry Andric     Diag(Tok, diag::err_typename_requires_specqual);
26580b57cec5SDimitry Andric     if (!DS.hasTypeSpecifier())
26590b57cec5SDimitry Andric       DS.SetTypeSpecError();
26600b57cec5SDimitry Andric   }
26610b57cec5SDimitry Andric 
26620b57cec5SDimitry Andric   // Issue diagnostic and remove storage class if present.
26630b57cec5SDimitry Andric   if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
26640b57cec5SDimitry Andric     if (DS.getStorageClassSpecLoc().isValid())
26650b57cec5SDimitry Andric       Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
26660b57cec5SDimitry Andric     else
26670b57cec5SDimitry Andric       Diag(DS.getThreadStorageClassSpecLoc(),
26680b57cec5SDimitry Andric            diag::err_typename_invalid_storageclass);
26690b57cec5SDimitry Andric     DS.ClearStorageClassSpecs();
26700b57cec5SDimitry Andric   }
26710b57cec5SDimitry Andric 
26720b57cec5SDimitry Andric   // Issue diagnostic and remove function specifier if present.
26730b57cec5SDimitry Andric   if (Specs & DeclSpec::PQ_FunctionSpecifier) {
26740b57cec5SDimitry Andric     if (DS.isInlineSpecified())
26750b57cec5SDimitry Andric       Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
26760b57cec5SDimitry Andric     if (DS.isVirtualSpecified())
26770b57cec5SDimitry Andric       Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
26780b57cec5SDimitry Andric     if (DS.hasExplicitSpecifier())
26790b57cec5SDimitry Andric       Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
2680bdd1243dSDimitry Andric     if (DS.isNoreturnSpecified())
2681bdd1243dSDimitry Andric       Diag(DS.getNoreturnSpecLoc(), diag::err_typename_invalid_functionspec);
26820b57cec5SDimitry Andric     DS.ClearFunctionSpecs();
26830b57cec5SDimitry Andric   }
26840b57cec5SDimitry Andric 
26850b57cec5SDimitry Andric   // Issue diagnostic and remove constexpr specifier if present.
26860b57cec5SDimitry Andric   if (DS.hasConstexprSpecifier() && DSC != DeclSpecContext::DSC_condition) {
26870b57cec5SDimitry Andric     Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr)
2688e8d8bef9SDimitry Andric         << static_cast<int>(DS.getConstexprSpecifier());
26890b57cec5SDimitry Andric     DS.ClearConstexprSpec();
26900b57cec5SDimitry Andric   }
26910b57cec5SDimitry Andric }
26920b57cec5SDimitry Andric 
26930b57cec5SDimitry Andric /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
26940b57cec5SDimitry Andric /// specified token is valid after the identifier in a declarator which
26950b57cec5SDimitry Andric /// immediately follows the declspec.  For example, these things are valid:
26960b57cec5SDimitry Andric ///
26970b57cec5SDimitry Andric ///      int x   [             4];         // direct-declarator
26980b57cec5SDimitry Andric ///      int x   (             int y);     // direct-declarator
26990b57cec5SDimitry Andric ///  int(int x   )                         // direct-declarator
27000b57cec5SDimitry Andric ///      int x   ;                         // simple-declaration
27010b57cec5SDimitry Andric ///      int x   =             17;         // init-declarator-list
27020b57cec5SDimitry Andric ///      int x   ,             y;          // init-declarator-list
27030b57cec5SDimitry Andric ///      int x   __asm__       ("foo");    // init-declarator-list
27040b57cec5SDimitry Andric ///      int x   :             4;          // struct-declarator
27050b57cec5SDimitry Andric ///      int x   {             5};         // C++'0x unified initializers
27060b57cec5SDimitry Andric ///
27070b57cec5SDimitry Andric /// This is not, because 'x' does not immediately follow the declspec (though
27080b57cec5SDimitry Andric /// ')' happens to be valid anyway).
27090b57cec5SDimitry Andric ///    int (x)
27100b57cec5SDimitry Andric ///
27110b57cec5SDimitry Andric static bool isValidAfterIdentifierInDeclarator(const Token &T) {
27120b57cec5SDimitry Andric   return T.isOneOf(tok::l_square, tok::l_paren, tok::r_paren, tok::semi,
27130b57cec5SDimitry Andric                    tok::comma, tok::equal, tok::kw_asm, tok::l_brace,
27140b57cec5SDimitry Andric                    tok::colon);
27150b57cec5SDimitry Andric }
27160b57cec5SDimitry Andric 
27170b57cec5SDimitry Andric /// ParseImplicitInt - This method is called when we have an non-typename
27180b57cec5SDimitry Andric /// identifier in a declspec (which normally terminates the decl spec) when
27190b57cec5SDimitry Andric /// the declspec has no type specifier.  In this case, the declspec is either
27200b57cec5SDimitry Andric /// malformed or is "implicit int" (in K&R and C89).
27210b57cec5SDimitry Andric ///
27220b57cec5SDimitry Andric /// This method handles diagnosing this prettily and returns false if the
27230b57cec5SDimitry Andric /// declspec is done being processed.  If it recovers and thinks there may be
27240b57cec5SDimitry Andric /// other pieces of declspec after it, it returns true.
27250b57cec5SDimitry Andric ///
27260b57cec5SDimitry Andric bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
27270b57cec5SDimitry Andric                               const ParsedTemplateInfo &TemplateInfo,
27280b57cec5SDimitry Andric                               AccessSpecifier AS, DeclSpecContext DSC,
272981ad6265SDimitry Andric                               ParsedAttributes &Attrs) {
27300b57cec5SDimitry Andric   assert(Tok.is(tok::identifier) && "should have identifier");
27310b57cec5SDimitry Andric 
27320b57cec5SDimitry Andric   SourceLocation Loc = Tok.getLocation();
27330b57cec5SDimitry Andric   // If we see an identifier that is not a type name, we normally would
27340b57cec5SDimitry Andric   // parse it as the identifier being declared.  However, when a typename
27350b57cec5SDimitry Andric   // is typo'd or the definition is not included, this will incorrectly
27360b57cec5SDimitry Andric   // parse the typename as the identifier name and fall over misparsing
27370b57cec5SDimitry Andric   // later parts of the diagnostic.
27380b57cec5SDimitry Andric   //
27390b57cec5SDimitry Andric   // As such, we try to do some look-ahead in cases where this would
27400b57cec5SDimitry Andric   // otherwise be an "implicit-int" case to see if this is invalid.  For
27410b57cec5SDimitry Andric   // example: "static foo_t x = 4;"  In this case, if we parsed foo_t as
27420b57cec5SDimitry Andric   // an identifier with implicit int, we'd get a parse error because the
27430b57cec5SDimitry Andric   // next token is obviously invalid for a type.  Parse these as a case
27440b57cec5SDimitry Andric   // with an invalid type specifier.
27450b57cec5SDimitry Andric   assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
27460b57cec5SDimitry Andric 
27470b57cec5SDimitry Andric   // Since we know that this either implicit int (which is rare) or an
27480b57cec5SDimitry Andric   // error, do lookahead to try to do better recovery. This never applies
27490b57cec5SDimitry Andric   // within a type specifier. Outside of C++, we allow this even if the
27500b57cec5SDimitry Andric   // language doesn't "officially" support implicit int -- we support
275181ad6265SDimitry Andric   // implicit int as an extension in some language modes.
275281ad6265SDimitry Andric   if (!isTypeSpecifier(DSC) && getLangOpts().isImplicitIntAllowed() &&
27530b57cec5SDimitry Andric       isValidAfterIdentifierInDeclarator(NextToken())) {
27540b57cec5SDimitry Andric     // If this token is valid for implicit int, e.g. "static x = 4", then
27550b57cec5SDimitry Andric     // we just avoid eating the identifier, so it will be parsed as the
27560b57cec5SDimitry Andric     // identifier in the declarator.
27570b57cec5SDimitry Andric     return false;
27580b57cec5SDimitry Andric   }
27590b57cec5SDimitry Andric 
27600b57cec5SDimitry Andric   // Early exit as Sema has a dedicated missing_actual_pipe_type diagnostic
27610b57cec5SDimitry Andric   // for incomplete declarations such as `pipe p`.
27620b57cec5SDimitry Andric   if (getLangOpts().OpenCLCPlusPlus && DS.isTypeSpecPipe())
27630b57cec5SDimitry Andric     return false;
27640b57cec5SDimitry Andric 
27650b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus &&
27660b57cec5SDimitry Andric       DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
27670b57cec5SDimitry Andric     // Don't require a type specifier if we have the 'auto' storage class
27680b57cec5SDimitry Andric     // specifier in C++98 -- we'll promote it to a type specifier.
27690b57cec5SDimitry Andric     if (SS)
27700b57cec5SDimitry Andric       AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
27710b57cec5SDimitry Andric     return false;
27720b57cec5SDimitry Andric   }
27730b57cec5SDimitry Andric 
27740b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus && (!SS || SS->isEmpty()) &&
27750b57cec5SDimitry Andric       getLangOpts().MSVCCompat) {
27760b57cec5SDimitry Andric     // Lookup of an unqualified type name has failed in MSVC compatibility mode.
27770b57cec5SDimitry Andric     // Give Sema a chance to recover if we are in a template with dependent base
27780b57cec5SDimitry Andric     // classes.
27790b57cec5SDimitry Andric     if (ParsedType T = Actions.ActOnMSVCUnknownTypeName(
27800b57cec5SDimitry Andric             *Tok.getIdentifierInfo(), Tok.getLocation(),
27810b57cec5SDimitry Andric             DSC == DeclSpecContext::DSC_template_type_arg)) {
27820b57cec5SDimitry Andric       const char *PrevSpec;
27830b57cec5SDimitry Andric       unsigned DiagID;
27840b57cec5SDimitry Andric       DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
27850b57cec5SDimitry Andric                          Actions.getASTContext().getPrintingPolicy());
27860b57cec5SDimitry Andric       DS.SetRangeEnd(Tok.getLocation());
27870b57cec5SDimitry Andric       ConsumeToken();
27880b57cec5SDimitry Andric       return false;
27890b57cec5SDimitry Andric     }
27900b57cec5SDimitry Andric   }
27910b57cec5SDimitry Andric 
27920b57cec5SDimitry Andric   // Otherwise, if we don't consume this token, we are going to emit an
27930b57cec5SDimitry Andric   // error anyway.  Try to recover from various common problems.  Check
27940b57cec5SDimitry Andric   // to see if this was a reference to a tag name without a tag specified.
27950b57cec5SDimitry Andric   // This is a common problem in C (saying 'foo' instead of 'struct foo').
27960b57cec5SDimitry Andric   //
27970b57cec5SDimitry Andric   // C++ doesn't need this, and isTagName doesn't take SS.
27980b57cec5SDimitry Andric   if (SS == nullptr) {
27990b57cec5SDimitry Andric     const char *TagName = nullptr, *FixitTagName = nullptr;
28000b57cec5SDimitry Andric     tok::TokenKind TagKind = tok::unknown;
28010b57cec5SDimitry Andric 
28020b57cec5SDimitry Andric     switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
28030b57cec5SDimitry Andric       default: break;
28040b57cec5SDimitry Andric       case DeclSpec::TST_enum:
28050b57cec5SDimitry Andric         TagName="enum"  ; FixitTagName = "enum "  ; TagKind=tok::kw_enum ;break;
28060b57cec5SDimitry Andric       case DeclSpec::TST_union:
28070b57cec5SDimitry Andric         TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
28080b57cec5SDimitry Andric       case DeclSpec::TST_struct:
28090b57cec5SDimitry Andric         TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
28100b57cec5SDimitry Andric       case DeclSpec::TST_interface:
28110b57cec5SDimitry Andric         TagName="__interface"; FixitTagName = "__interface ";
28120b57cec5SDimitry Andric         TagKind=tok::kw___interface;break;
28130b57cec5SDimitry Andric       case DeclSpec::TST_class:
28140b57cec5SDimitry Andric         TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
28150b57cec5SDimitry Andric     }
28160b57cec5SDimitry Andric 
28170b57cec5SDimitry Andric     if (TagName) {
28180b57cec5SDimitry Andric       IdentifierInfo *TokenName = Tok.getIdentifierInfo();
28190b57cec5SDimitry Andric       LookupResult R(Actions, TokenName, SourceLocation(),
28200b57cec5SDimitry Andric                      Sema::LookupOrdinaryName);
28210b57cec5SDimitry Andric 
28220b57cec5SDimitry Andric       Diag(Loc, diag::err_use_of_tag_name_without_tag)
28230b57cec5SDimitry Andric         << TokenName << TagName << getLangOpts().CPlusPlus
28240b57cec5SDimitry Andric         << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
28250b57cec5SDimitry Andric 
28260b57cec5SDimitry Andric       if (Actions.LookupParsedName(R, getCurScope(), SS)) {
28270b57cec5SDimitry Andric         for (LookupResult::iterator I = R.begin(), IEnd = R.end();
28280b57cec5SDimitry Andric              I != IEnd; ++I)
28290b57cec5SDimitry Andric           Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
28300b57cec5SDimitry Andric             << TokenName << TagName;
28310b57cec5SDimitry Andric       }
28320b57cec5SDimitry Andric 
28330b57cec5SDimitry Andric       // Parse this as a tag as if the missing tag were present.
28340b57cec5SDimitry Andric       if (TagKind == tok::kw_enum)
28350b57cec5SDimitry Andric         ParseEnumSpecifier(Loc, DS, TemplateInfo, AS,
28360b57cec5SDimitry Andric                            DeclSpecContext::DSC_normal);
28370b57cec5SDimitry Andric       else
28380b57cec5SDimitry Andric         ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
28390b57cec5SDimitry Andric                             /*EnteringContext*/ false,
28400b57cec5SDimitry Andric                             DeclSpecContext::DSC_normal, Attrs);
28410b57cec5SDimitry Andric       return true;
28420b57cec5SDimitry Andric     }
28430b57cec5SDimitry Andric   }
28440b57cec5SDimitry Andric 
28450b57cec5SDimitry Andric   // Determine whether this identifier could plausibly be the name of something
28460b57cec5SDimitry Andric   // being declared (with a missing type).
28470b57cec5SDimitry Andric   if (!isTypeSpecifier(DSC) && (!SS || DSC == DeclSpecContext::DSC_top_level ||
28480b57cec5SDimitry Andric                                 DSC == DeclSpecContext::DSC_class)) {
28490b57cec5SDimitry Andric     // Look ahead to the next token to try to figure out what this declaration
28500b57cec5SDimitry Andric     // was supposed to be.
28510b57cec5SDimitry Andric     switch (NextToken().getKind()) {
28520b57cec5SDimitry Andric     case tok::l_paren: {
28530b57cec5SDimitry Andric       // static x(4); // 'x' is not a type
28540b57cec5SDimitry Andric       // x(int n);    // 'x' is not a type
28550b57cec5SDimitry Andric       // x (*p)[];    // 'x' is a type
28560b57cec5SDimitry Andric       //
28570b57cec5SDimitry Andric       // Since we're in an error case, we can afford to perform a tentative
28580b57cec5SDimitry Andric       // parse to determine which case we're in.
28590b57cec5SDimitry Andric       TentativeParsingAction PA(*this);
28600b57cec5SDimitry Andric       ConsumeToken();
28610b57cec5SDimitry Andric       TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
28620b57cec5SDimitry Andric       PA.Revert();
28630b57cec5SDimitry Andric 
28640b57cec5SDimitry Andric       if (TPR != TPResult::False) {
28650b57cec5SDimitry Andric         // The identifier is followed by a parenthesized declarator.
28660b57cec5SDimitry Andric         // It's supposed to be a type.
28670b57cec5SDimitry Andric         break;
28680b57cec5SDimitry Andric       }
28690b57cec5SDimitry Andric 
28700b57cec5SDimitry Andric       // If we're in a context where we could be declaring a constructor,
28710b57cec5SDimitry Andric       // check whether this is a constructor declaration with a bogus name.
28720b57cec5SDimitry Andric       if (DSC == DeclSpecContext::DSC_class ||
28730b57cec5SDimitry Andric           (DSC == DeclSpecContext::DSC_top_level && SS)) {
28740b57cec5SDimitry Andric         IdentifierInfo *II = Tok.getIdentifierInfo();
28750b57cec5SDimitry Andric         if (Actions.isCurrentClassNameTypo(II, SS)) {
28760b57cec5SDimitry Andric           Diag(Loc, diag::err_constructor_bad_name)
28770b57cec5SDimitry Andric             << Tok.getIdentifierInfo() << II
28780b57cec5SDimitry Andric             << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
28790b57cec5SDimitry Andric           Tok.setIdentifierInfo(II);
28800b57cec5SDimitry Andric         }
28810b57cec5SDimitry Andric       }
28820b57cec5SDimitry Andric       // Fall through.
2883bdd1243dSDimitry Andric       [[fallthrough]];
28840b57cec5SDimitry Andric     }
28850b57cec5SDimitry Andric     case tok::comma:
28860b57cec5SDimitry Andric     case tok::equal:
28870b57cec5SDimitry Andric     case tok::kw_asm:
28880b57cec5SDimitry Andric     case tok::l_brace:
28890b57cec5SDimitry Andric     case tok::l_square:
28900b57cec5SDimitry Andric     case tok::semi:
28910b57cec5SDimitry Andric       // This looks like a variable or function declaration. The type is
28920b57cec5SDimitry Andric       // probably missing. We're done parsing decl-specifiers.
28930b57cec5SDimitry Andric       // But only if we are not in a function prototype scope.
28940b57cec5SDimitry Andric       if (getCurScope()->isFunctionPrototypeScope())
28950b57cec5SDimitry Andric         break;
28960b57cec5SDimitry Andric       if (SS)
28970b57cec5SDimitry Andric         AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
28980b57cec5SDimitry Andric       return false;
28990b57cec5SDimitry Andric 
29000b57cec5SDimitry Andric     default:
29010b57cec5SDimitry Andric       // This is probably supposed to be a type. This includes cases like:
29020b57cec5SDimitry Andric       //   int f(itn);
29035ffd83dbSDimitry Andric       //   struct S { unsigned : 4; };
29040b57cec5SDimitry Andric       break;
29050b57cec5SDimitry Andric     }
29060b57cec5SDimitry Andric   }
29070b57cec5SDimitry Andric 
29080b57cec5SDimitry Andric   // This is almost certainly an invalid type name. Let Sema emit a diagnostic
29090b57cec5SDimitry Andric   // and attempt to recover.
29100b57cec5SDimitry Andric   ParsedType T;
29110b57cec5SDimitry Andric   IdentifierInfo *II = Tok.getIdentifierInfo();
29120b57cec5SDimitry Andric   bool IsTemplateName = getLangOpts().CPlusPlus && NextToken().is(tok::less);
29130b57cec5SDimitry Andric   Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T,
29140b57cec5SDimitry Andric                                   IsTemplateName);
29150b57cec5SDimitry Andric   if (T) {
29160b57cec5SDimitry Andric     // The action has suggested that the type T could be used. Set that as
29170b57cec5SDimitry Andric     // the type in the declaration specifiers, consume the would-be type
29180b57cec5SDimitry Andric     // name token, and we're done.
29190b57cec5SDimitry Andric     const char *PrevSpec;
29200b57cec5SDimitry Andric     unsigned DiagID;
29210b57cec5SDimitry Andric     DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
29220b57cec5SDimitry Andric                        Actions.getASTContext().getPrintingPolicy());
29230b57cec5SDimitry Andric     DS.SetRangeEnd(Tok.getLocation());
29240b57cec5SDimitry Andric     ConsumeToken();
29250b57cec5SDimitry Andric     // There may be other declaration specifiers after this.
29260b57cec5SDimitry Andric     return true;
29270b57cec5SDimitry Andric   } else if (II != Tok.getIdentifierInfo()) {
29280b57cec5SDimitry Andric     // If no type was suggested, the correction is to a keyword
29290b57cec5SDimitry Andric     Tok.setKind(II->getTokenID());
29300b57cec5SDimitry Andric     // There may be other declaration specifiers after this.
29310b57cec5SDimitry Andric     return true;
29320b57cec5SDimitry Andric   }
29330b57cec5SDimitry Andric 
29340b57cec5SDimitry Andric   // Otherwise, the action had no suggestion for us.  Mark this as an error.
29350b57cec5SDimitry Andric   DS.SetTypeSpecError();
29360b57cec5SDimitry Andric   DS.SetRangeEnd(Tok.getLocation());
29370b57cec5SDimitry Andric   ConsumeToken();
29380b57cec5SDimitry Andric 
29390b57cec5SDimitry Andric   // Eat any following template arguments.
29400b57cec5SDimitry Andric   if (IsTemplateName) {
29410b57cec5SDimitry Andric     SourceLocation LAngle, RAngle;
29420b57cec5SDimitry Andric     TemplateArgList Args;
29430b57cec5SDimitry Andric     ParseTemplateIdAfterTemplateName(true, LAngle, Args, RAngle);
29440b57cec5SDimitry Andric   }
29450b57cec5SDimitry Andric 
29460b57cec5SDimitry Andric   // TODO: Could inject an invalid typedef decl in an enclosing scope to
29470b57cec5SDimitry Andric   // avoid rippling error messages on subsequent uses of the same type,
29480b57cec5SDimitry Andric   // could be useful if #include was forgotten.
29490b57cec5SDimitry Andric   return true;
29500b57cec5SDimitry Andric }
29510b57cec5SDimitry Andric 
29520b57cec5SDimitry Andric /// Determine the declaration specifier context from the declarator
29530b57cec5SDimitry Andric /// context.
29540b57cec5SDimitry Andric ///
29550b57cec5SDimitry Andric /// \param Context the declarator context, which is one of the
29560b57cec5SDimitry Andric /// DeclaratorContext enumerator values.
29570b57cec5SDimitry Andric Parser::DeclSpecContext
29580b57cec5SDimitry Andric Parser::getDeclSpecContextFromDeclaratorContext(DeclaratorContext Context) {
2959bdd1243dSDimitry Andric   switch (Context) {
2960bdd1243dSDimitry Andric   case DeclaratorContext::Member:
29610b57cec5SDimitry Andric     return DeclSpecContext::DSC_class;
2962bdd1243dSDimitry Andric   case DeclaratorContext::File:
29630b57cec5SDimitry Andric     return DeclSpecContext::DSC_top_level;
2964bdd1243dSDimitry Andric   case DeclaratorContext::TemplateParam:
29650b57cec5SDimitry Andric     return DeclSpecContext::DSC_template_param;
2966bdd1243dSDimitry Andric   case DeclaratorContext::TemplateArg:
2967bdd1243dSDimitry Andric     return DeclSpecContext::DSC_template_arg;
2968bdd1243dSDimitry Andric   case DeclaratorContext::TemplateTypeArg:
29690b57cec5SDimitry Andric     return DeclSpecContext::DSC_template_type_arg;
2970bdd1243dSDimitry Andric   case DeclaratorContext::TrailingReturn:
2971bdd1243dSDimitry Andric   case DeclaratorContext::TrailingReturnVar:
29720b57cec5SDimitry Andric     return DeclSpecContext::DSC_trailing;
2973bdd1243dSDimitry Andric   case DeclaratorContext::AliasDecl:
2974bdd1243dSDimitry Andric   case DeclaratorContext::AliasTemplate:
29750b57cec5SDimitry Andric     return DeclSpecContext::DSC_alias_declaration;
2976bdd1243dSDimitry Andric   case DeclaratorContext::Association:
297781ad6265SDimitry Andric     return DeclSpecContext::DSC_association;
2978bdd1243dSDimitry Andric   case DeclaratorContext::TypeName:
2979bdd1243dSDimitry Andric     return DeclSpecContext::DSC_type_specifier;
2980bdd1243dSDimitry Andric   case DeclaratorContext::Condition:
2981bdd1243dSDimitry Andric     return DeclSpecContext::DSC_condition;
2982bdd1243dSDimitry Andric   case DeclaratorContext::ConversionId:
2983bdd1243dSDimitry Andric     return DeclSpecContext::DSC_conv_operator;
2984*06c3fb27SDimitry Andric   case DeclaratorContext::CXXNew:
2985*06c3fb27SDimitry Andric     return DeclSpecContext::DSC_new;
2986bdd1243dSDimitry Andric   case DeclaratorContext::Prototype:
2987bdd1243dSDimitry Andric   case DeclaratorContext::ObjCResult:
2988bdd1243dSDimitry Andric   case DeclaratorContext::ObjCParameter:
2989bdd1243dSDimitry Andric   case DeclaratorContext::KNRTypeList:
2990bdd1243dSDimitry Andric   case DeclaratorContext::FunctionalCast:
2991bdd1243dSDimitry Andric   case DeclaratorContext::Block:
2992bdd1243dSDimitry Andric   case DeclaratorContext::ForInit:
2993bdd1243dSDimitry Andric   case DeclaratorContext::SelectionInit:
2994bdd1243dSDimitry Andric   case DeclaratorContext::CXXCatch:
2995bdd1243dSDimitry Andric   case DeclaratorContext::ObjCCatch:
2996bdd1243dSDimitry Andric   case DeclaratorContext::BlockLiteral:
2997bdd1243dSDimitry Andric   case DeclaratorContext::LambdaExpr:
2998bdd1243dSDimitry Andric   case DeclaratorContext::LambdaExprParameter:
2999bdd1243dSDimitry Andric   case DeclaratorContext::RequiresExpr:
30000b57cec5SDimitry Andric     return DeclSpecContext::DSC_normal;
30010b57cec5SDimitry Andric   }
30020b57cec5SDimitry Andric 
3003bdd1243dSDimitry Andric   llvm_unreachable("Missing DeclaratorContext case");
3004bdd1243dSDimitry Andric }
3005bdd1243dSDimitry Andric 
30060b57cec5SDimitry Andric /// ParseAlignArgument - Parse the argument to an alignment-specifier.
30070b57cec5SDimitry Andric ///
30080b57cec5SDimitry Andric /// [C11]   type-id
30090b57cec5SDimitry Andric /// [C11]   constant-expression
30100b57cec5SDimitry Andric /// [C++0x] type-id ...[opt]
30110b57cec5SDimitry Andric /// [C++0x] assignment-expression ...[opt]
3012*06c3fb27SDimitry Andric ExprResult Parser::ParseAlignArgument(StringRef KWName, SourceLocation Start,
3013*06c3fb27SDimitry Andric                                       SourceLocation &EllipsisLoc, bool &IsType,
3014*06c3fb27SDimitry Andric                                       ParsedType &TypeResult) {
30150b57cec5SDimitry Andric   ExprResult ER;
30160b57cec5SDimitry Andric   if (isTypeIdInParens()) {
30170b57cec5SDimitry Andric     SourceLocation TypeLoc = Tok.getLocation();
30180b57cec5SDimitry Andric     ParsedType Ty = ParseTypeName().get();
30190b57cec5SDimitry Andric     SourceRange TypeRange(Start, Tok.getLocation());
3020*06c3fb27SDimitry Andric     if (Actions.ActOnAlignasTypeArgument(KWName, Ty, TypeLoc, TypeRange))
3021*06c3fb27SDimitry Andric       return ExprError();
3022*06c3fb27SDimitry Andric     TypeResult = Ty;
3023*06c3fb27SDimitry Andric     IsType = true;
3024*06c3fb27SDimitry Andric   } else {
30250b57cec5SDimitry Andric     ER = ParseConstantExpression();
3026*06c3fb27SDimitry Andric     IsType = false;
3027*06c3fb27SDimitry Andric   }
30280b57cec5SDimitry Andric 
30290b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus11)
30300b57cec5SDimitry Andric     TryConsumeToken(tok::ellipsis, EllipsisLoc);
30310b57cec5SDimitry Andric 
30320b57cec5SDimitry Andric   return ER;
30330b57cec5SDimitry Andric }
30340b57cec5SDimitry Andric 
30350b57cec5SDimitry Andric /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
30360b57cec5SDimitry Andric /// attribute to Attrs.
30370b57cec5SDimitry Andric ///
30380b57cec5SDimitry Andric /// alignment-specifier:
30390b57cec5SDimitry Andric /// [C11]   '_Alignas' '(' type-id ')'
30400b57cec5SDimitry Andric /// [C11]   '_Alignas' '(' constant-expression ')'
30410b57cec5SDimitry Andric /// [C++11] 'alignas' '(' type-id ...[opt] ')'
30420b57cec5SDimitry Andric /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
30430b57cec5SDimitry Andric void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
30440b57cec5SDimitry Andric                                      SourceLocation *EndLoc) {
30450b57cec5SDimitry Andric   assert(Tok.isOneOf(tok::kw_alignas, tok::kw__Alignas) &&
30460b57cec5SDimitry Andric          "Not an alignment-specifier!");
3047*06c3fb27SDimitry Andric   Token KWTok = Tok;
3048*06c3fb27SDimitry Andric   IdentifierInfo *KWName = KWTok.getIdentifierInfo();
3049*06c3fb27SDimitry Andric   auto Kind = KWTok.getKind();
30500b57cec5SDimitry Andric   SourceLocation KWLoc = ConsumeToken();
30510b57cec5SDimitry Andric 
30520b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
30530b57cec5SDimitry Andric   if (T.expectAndConsume())
30540b57cec5SDimitry Andric     return;
30550b57cec5SDimitry Andric 
3056*06c3fb27SDimitry Andric   bool IsType;
3057*06c3fb27SDimitry Andric   ParsedType TypeResult;
30580b57cec5SDimitry Andric   SourceLocation EllipsisLoc;
3059*06c3fb27SDimitry Andric   ExprResult ArgExpr =
3060*06c3fb27SDimitry Andric       ParseAlignArgument(PP.getSpelling(KWTok), T.getOpenLocation(),
3061*06c3fb27SDimitry Andric                          EllipsisLoc, IsType, TypeResult);
30620b57cec5SDimitry Andric   if (ArgExpr.isInvalid()) {
30630b57cec5SDimitry Andric     T.skipToEnd();
30640b57cec5SDimitry Andric     return;
30650b57cec5SDimitry Andric   }
30660b57cec5SDimitry Andric 
30670b57cec5SDimitry Andric   T.consumeClose();
30680b57cec5SDimitry Andric   if (EndLoc)
30690b57cec5SDimitry Andric     *EndLoc = T.getCloseLocation();
30700b57cec5SDimitry Andric 
3071*06c3fb27SDimitry Andric   if (IsType) {
3072*06c3fb27SDimitry Andric     Attrs.addNewTypeAttr(KWName, KWLoc, nullptr, KWLoc, TypeResult, Kind,
3073*06c3fb27SDimitry Andric                          EllipsisLoc);
3074*06c3fb27SDimitry Andric   } else {
30750b57cec5SDimitry Andric     ArgsVector ArgExprs;
30760b57cec5SDimitry Andric     ArgExprs.push_back(ArgExpr.get());
3077*06c3fb27SDimitry Andric     Attrs.addNew(KWName, KWLoc, nullptr, KWLoc, ArgExprs.data(), 1, Kind,
3078*06c3fb27SDimitry Andric                  EllipsisLoc);
3079*06c3fb27SDimitry Andric   }
30800b57cec5SDimitry Andric }
30810b57cec5SDimitry Andric 
30825ffd83dbSDimitry Andric ExprResult Parser::ParseExtIntegerArgument() {
30830eae32dcSDimitry Andric   assert(Tok.isOneOf(tok::kw__ExtInt, tok::kw__BitInt) &&
30840eae32dcSDimitry Andric          "Not an extended int type");
30855ffd83dbSDimitry Andric   ConsumeToken();
30865ffd83dbSDimitry Andric 
30875ffd83dbSDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
30885ffd83dbSDimitry Andric   if (T.expectAndConsume())
30895ffd83dbSDimitry Andric     return ExprError();
30905ffd83dbSDimitry Andric 
30915ffd83dbSDimitry Andric   ExprResult ER = ParseConstantExpression();
30925ffd83dbSDimitry Andric   if (ER.isInvalid()) {
30935ffd83dbSDimitry Andric     T.skipToEnd();
30945ffd83dbSDimitry Andric     return ExprError();
30955ffd83dbSDimitry Andric   }
30965ffd83dbSDimitry Andric 
30975ffd83dbSDimitry Andric   if(T.consumeClose())
30985ffd83dbSDimitry Andric     return ExprError();
30995ffd83dbSDimitry Andric   return ER;
31005ffd83dbSDimitry Andric }
31015ffd83dbSDimitry Andric 
31020b57cec5SDimitry Andric /// Determine whether we're looking at something that might be a declarator
31030b57cec5SDimitry Andric /// in a simple-declaration. If it can't possibly be a declarator, maybe
31040b57cec5SDimitry Andric /// diagnose a missing semicolon after a prior tag definition in the decl
31050b57cec5SDimitry Andric /// specifier.
31060b57cec5SDimitry Andric ///
31070b57cec5SDimitry Andric /// \return \c true if an error occurred and this can't be any kind of
31080b57cec5SDimitry Andric /// declaration.
31090b57cec5SDimitry Andric bool
31100b57cec5SDimitry Andric Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
31110b57cec5SDimitry Andric                                               DeclSpecContext DSContext,
31120b57cec5SDimitry Andric                                               LateParsedAttrList *LateAttrs) {
31130b57cec5SDimitry Andric   assert(DS.hasTagDefinition() && "shouldn't call this");
31140b57cec5SDimitry Andric 
31150b57cec5SDimitry Andric   bool EnteringContext = (DSContext == DeclSpecContext::DSC_class ||
31160b57cec5SDimitry Andric                           DSContext == DeclSpecContext::DSC_top_level);
31170b57cec5SDimitry Andric 
31180b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus &&
31190b57cec5SDimitry Andric       Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype,
31200b57cec5SDimitry Andric                   tok::annot_template_id) &&
31210b57cec5SDimitry Andric       TryAnnotateCXXScopeToken(EnteringContext)) {
31220b57cec5SDimitry Andric     SkipMalformedDecl();
31230b57cec5SDimitry Andric     return true;
31240b57cec5SDimitry Andric   }
31250b57cec5SDimitry Andric 
31260b57cec5SDimitry Andric   bool HasScope = Tok.is(tok::annot_cxxscope);
31270b57cec5SDimitry Andric   // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
31280b57cec5SDimitry Andric   Token AfterScope = HasScope ? NextToken() : Tok;
31290b57cec5SDimitry Andric 
31300b57cec5SDimitry Andric   // Determine whether the following tokens could possibly be a
31310b57cec5SDimitry Andric   // declarator.
31320b57cec5SDimitry Andric   bool MightBeDeclarator = true;
31330b57cec5SDimitry Andric   if (Tok.isOneOf(tok::kw_typename, tok::annot_typename)) {
31340b57cec5SDimitry Andric     // A declarator-id can't start with 'typename'.
31350b57cec5SDimitry Andric     MightBeDeclarator = false;
31360b57cec5SDimitry Andric   } else if (AfterScope.is(tok::annot_template_id)) {
31370b57cec5SDimitry Andric     // If we have a type expressed as a template-id, this cannot be a
31380b57cec5SDimitry Andric     // declarator-id (such a type cannot be redeclared in a simple-declaration).
31390b57cec5SDimitry Andric     TemplateIdAnnotation *Annot =
31400b57cec5SDimitry Andric         static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
31410b57cec5SDimitry Andric     if (Annot->Kind == TNK_Type_template)
31420b57cec5SDimitry Andric       MightBeDeclarator = false;
31430b57cec5SDimitry Andric   } else if (AfterScope.is(tok::identifier)) {
31440b57cec5SDimitry Andric     const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
31450b57cec5SDimitry Andric 
31460b57cec5SDimitry Andric     // These tokens cannot come after the declarator-id in a
31470b57cec5SDimitry Andric     // simple-declaration, and are likely to come after a type-specifier.
31480b57cec5SDimitry Andric     if (Next.isOneOf(tok::star, tok::amp, tok::ampamp, tok::identifier,
31490b57cec5SDimitry Andric                      tok::annot_cxxscope, tok::coloncolon)) {
31500b57cec5SDimitry Andric       // Missing a semicolon.
31510b57cec5SDimitry Andric       MightBeDeclarator = false;
31520b57cec5SDimitry Andric     } else if (HasScope) {
31530b57cec5SDimitry Andric       // If the declarator-id has a scope specifier, it must redeclare a
31540b57cec5SDimitry Andric       // previously-declared entity. If that's a type (and this is not a
31550b57cec5SDimitry Andric       // typedef), that's an error.
31560b57cec5SDimitry Andric       CXXScopeSpec SS;
31570b57cec5SDimitry Andric       Actions.RestoreNestedNameSpecifierAnnotation(
31580b57cec5SDimitry Andric           Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
31590b57cec5SDimitry Andric       IdentifierInfo *Name = AfterScope.getIdentifierInfo();
31600b57cec5SDimitry Andric       Sema::NameClassification Classification = Actions.ClassifyName(
31610b57cec5SDimitry Andric           getCurScope(), SS, Name, AfterScope.getLocation(), Next,
3162a7dea167SDimitry Andric           /*CCC=*/nullptr);
31630b57cec5SDimitry Andric       switch (Classification.getKind()) {
31640b57cec5SDimitry Andric       case Sema::NC_Error:
31650b57cec5SDimitry Andric         SkipMalformedDecl();
31660b57cec5SDimitry Andric         return true;
31670b57cec5SDimitry Andric 
31680b57cec5SDimitry Andric       case Sema::NC_Keyword:
3169a7dea167SDimitry Andric         llvm_unreachable("typo correction is not possible here");
31700b57cec5SDimitry Andric 
31710b57cec5SDimitry Andric       case Sema::NC_Type:
31720b57cec5SDimitry Andric       case Sema::NC_TypeTemplate:
3173a7dea167SDimitry Andric       case Sema::NC_UndeclaredNonType:
3174a7dea167SDimitry Andric       case Sema::NC_UndeclaredTemplate:
31750b57cec5SDimitry Andric         // Not a previously-declared non-type entity.
31760b57cec5SDimitry Andric         MightBeDeclarator = false;
31770b57cec5SDimitry Andric         break;
31780b57cec5SDimitry Andric 
31790b57cec5SDimitry Andric       case Sema::NC_Unknown:
3180a7dea167SDimitry Andric       case Sema::NC_NonType:
3181a7dea167SDimitry Andric       case Sema::NC_DependentNonType:
3182e8d8bef9SDimitry Andric       case Sema::NC_OverloadSet:
31830b57cec5SDimitry Andric       case Sema::NC_VarTemplate:
31840b57cec5SDimitry Andric       case Sema::NC_FunctionTemplate:
318555e4f9d5SDimitry Andric       case Sema::NC_Concept:
31860b57cec5SDimitry Andric         // Might be a redeclaration of a prior entity.
31870b57cec5SDimitry Andric         break;
31880b57cec5SDimitry Andric       }
31890b57cec5SDimitry Andric     }
31900b57cec5SDimitry Andric   }
31910b57cec5SDimitry Andric 
31920b57cec5SDimitry Andric   if (MightBeDeclarator)
31930b57cec5SDimitry Andric     return false;
31940b57cec5SDimitry Andric 
31950b57cec5SDimitry Andric   const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
31960b57cec5SDimitry Andric   Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getEndLoc()),
31970b57cec5SDimitry Andric        diag::err_expected_after)
31980b57cec5SDimitry Andric       << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi;
31990b57cec5SDimitry Andric 
32000b57cec5SDimitry Andric   // Try to recover from the typo, by dropping the tag definition and parsing
32010b57cec5SDimitry Andric   // the problematic tokens as a type.
32020b57cec5SDimitry Andric   //
32030b57cec5SDimitry Andric   // FIXME: Split the DeclSpec into pieces for the standalone
32040b57cec5SDimitry Andric   // declaration and pieces for the following declaration, instead
32050b57cec5SDimitry Andric   // of assuming that all the other pieces attach to new declaration,
32060b57cec5SDimitry Andric   // and call ParsedFreeStandingDeclSpec as appropriate.
32070b57cec5SDimitry Andric   DS.ClearTypeSpecType();
32080b57cec5SDimitry Andric   ParsedTemplateInfo NotATemplate;
32090b57cec5SDimitry Andric   ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
32100b57cec5SDimitry Andric   return false;
32110b57cec5SDimitry Andric }
32120b57cec5SDimitry Andric 
32130b57cec5SDimitry Andric // Choose the apprpriate diagnostic error for why fixed point types are
32140b57cec5SDimitry Andric // disabled, set the previous specifier, and mark as invalid.
32150b57cec5SDimitry Andric static void SetupFixedPointError(const LangOptions &LangOpts,
32160b57cec5SDimitry Andric                                  const char *&PrevSpec, unsigned &DiagID,
32170b57cec5SDimitry Andric                                  bool &isInvalid) {
32180b57cec5SDimitry Andric   assert(!LangOpts.FixedPoint);
32190b57cec5SDimitry Andric   DiagID = diag::err_fixed_point_not_enabled;
32200b57cec5SDimitry Andric   PrevSpec = "";  // Not used by diagnostic
32210b57cec5SDimitry Andric   isInvalid = true;
32220b57cec5SDimitry Andric }
32230b57cec5SDimitry Andric 
32240b57cec5SDimitry Andric /// ParseDeclarationSpecifiers
32250b57cec5SDimitry Andric ///       declaration-specifiers: [C99 6.7]
32260b57cec5SDimitry Andric ///         storage-class-specifier declaration-specifiers[opt]
32270b57cec5SDimitry Andric ///         type-specifier declaration-specifiers[opt]
32280b57cec5SDimitry Andric /// [C99]   function-specifier declaration-specifiers[opt]
32290b57cec5SDimitry Andric /// [C11]   alignment-specifier declaration-specifiers[opt]
32300b57cec5SDimitry Andric /// [GNU]   attributes declaration-specifiers[opt]
32310b57cec5SDimitry Andric /// [Clang] '__module_private__' declaration-specifiers[opt]
32320b57cec5SDimitry Andric /// [ObjC1] '__kindof' declaration-specifiers[opt]
32330b57cec5SDimitry Andric ///
32340b57cec5SDimitry Andric ///       storage-class-specifier: [C99 6.7.1]
32350b57cec5SDimitry Andric ///         'typedef'
32360b57cec5SDimitry Andric ///         'extern'
32370b57cec5SDimitry Andric ///         'static'
32380b57cec5SDimitry Andric ///         'auto'
32390b57cec5SDimitry Andric ///         'register'
32400b57cec5SDimitry Andric /// [C++]   'mutable'
32410b57cec5SDimitry Andric /// [C++11] 'thread_local'
32420b57cec5SDimitry Andric /// [C11]   '_Thread_local'
32430b57cec5SDimitry Andric /// [GNU]   '__thread'
32440b57cec5SDimitry Andric ///       function-specifier: [C99 6.7.4]
32450b57cec5SDimitry Andric /// [C99]   'inline'
32460b57cec5SDimitry Andric /// [C++]   'virtual'
32470b57cec5SDimitry Andric /// [C++]   'explicit'
32480b57cec5SDimitry Andric /// [OpenCL] '__kernel'
32490b57cec5SDimitry Andric ///       'friend': [C++ dcl.friend]
32500b57cec5SDimitry Andric ///       'constexpr': [C++0x dcl.constexpr]
3251bdd1243dSDimitry Andric void Parser::ParseDeclarationSpecifiers(
3252bdd1243dSDimitry Andric     DeclSpec &DS, const ParsedTemplateInfo &TemplateInfo, AccessSpecifier AS,
3253bdd1243dSDimitry Andric     DeclSpecContext DSContext, LateParsedAttrList *LateAttrs,
3254bdd1243dSDimitry Andric     ImplicitTypenameContext AllowImplicitTypename) {
32550b57cec5SDimitry Andric   if (DS.getSourceRange().isInvalid()) {
32560b57cec5SDimitry Andric     // Start the range at the current token but make the end of the range
32570b57cec5SDimitry Andric     // invalid.  This will make the entire range invalid unless we successfully
32580b57cec5SDimitry Andric     // consume a token.
32590b57cec5SDimitry Andric     DS.SetRangeStart(Tok.getLocation());
32600b57cec5SDimitry Andric     DS.SetRangeEnd(SourceLocation());
32610b57cec5SDimitry Andric   }
32620b57cec5SDimitry Andric 
3263bdd1243dSDimitry Andric   // If we are in a operator context, convert it back into a type specifier
3264bdd1243dSDimitry Andric   // context for better error handling later on.
3265bdd1243dSDimitry Andric   if (DSContext == DeclSpecContext::DSC_conv_operator) {
3266bdd1243dSDimitry Andric     // No implicit typename here.
3267bdd1243dSDimitry Andric     AllowImplicitTypename = ImplicitTypenameContext::No;
3268bdd1243dSDimitry Andric     DSContext = DeclSpecContext::DSC_type_specifier;
3269bdd1243dSDimitry Andric   }
3270bdd1243dSDimitry Andric 
32710b57cec5SDimitry Andric   bool EnteringContext = (DSContext == DeclSpecContext::DSC_class ||
32720b57cec5SDimitry Andric                           DSContext == DeclSpecContext::DSC_top_level);
32730b57cec5SDimitry Andric   bool AttrsLastTime = false;
327481ad6265SDimitry Andric   ParsedAttributes attrs(AttrFactory);
32750b57cec5SDimitry Andric   // We use Sema's policy to get bool macros right.
32760b57cec5SDimitry Andric   PrintingPolicy Policy = Actions.getPrintingPolicy();
327704eeddc0SDimitry Andric   while (true) {
32780b57cec5SDimitry Andric     bool isInvalid = false;
32790b57cec5SDimitry Andric     bool isStorageClass = false;
32800b57cec5SDimitry Andric     const char *PrevSpec = nullptr;
32810b57cec5SDimitry Andric     unsigned DiagID = 0;
32820b57cec5SDimitry Andric 
32830b57cec5SDimitry Andric     // This value needs to be set to the location of the last token if the last
32840b57cec5SDimitry Andric     // token of the specifier is already consumed.
32850b57cec5SDimitry Andric     SourceLocation ConsumedEnd;
32860b57cec5SDimitry Andric 
32870b57cec5SDimitry Andric     // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
32880b57cec5SDimitry Andric     // implementation for VS2013 uses _Atomic as an identifier for one of the
32890b57cec5SDimitry Andric     // classes in <atomic>.
32900b57cec5SDimitry Andric     //
32910b57cec5SDimitry Andric     // A typedef declaration containing _Atomic<...> is among the places where
32920b57cec5SDimitry Andric     // the class is used.  If we are currently parsing such a declaration, treat
32930b57cec5SDimitry Andric     // the token as an identifier.
32940b57cec5SDimitry Andric     if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) &&
32950b57cec5SDimitry Andric         DS.getStorageClassSpec() == clang::DeclSpec::SCS_typedef &&
32960b57cec5SDimitry Andric         !DS.hasTypeSpecifier() && GetLookAheadToken(1).is(tok::less))
32970b57cec5SDimitry Andric       Tok.setKind(tok::identifier);
32980b57cec5SDimitry Andric 
32990b57cec5SDimitry Andric     SourceLocation Loc = Tok.getLocation();
33000b57cec5SDimitry Andric 
3301fe6060f1SDimitry Andric     // Helper for image types in OpenCL.
3302fe6060f1SDimitry Andric     auto handleOpenCLImageKW = [&] (StringRef Ext, TypeSpecifierType ImageTypeSpec) {
3303fe6060f1SDimitry Andric       // Check if the image type is supported and otherwise turn the keyword into an identifier
3304fe6060f1SDimitry Andric       // because image types from extensions are not reserved identifiers.
3305fe6060f1SDimitry Andric       if (!StringRef(Ext).empty() && !getActions().getOpenCLOptions().isSupported(Ext, getLangOpts())) {
3306fe6060f1SDimitry Andric         Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
3307fe6060f1SDimitry Andric         Tok.setKind(tok::identifier);
3308fe6060f1SDimitry Andric         return false;
3309fe6060f1SDimitry Andric       }
3310fe6060f1SDimitry Andric       isInvalid = DS.SetTypeSpecType(ImageTypeSpec, Loc, PrevSpec, DiagID, Policy);
3311fe6060f1SDimitry Andric       return true;
3312fe6060f1SDimitry Andric     };
3313fe6060f1SDimitry Andric 
3314349cc55cSDimitry Andric     // Turn off usual access checking for template specializations and
3315349cc55cSDimitry Andric     // instantiations.
3316349cc55cSDimitry Andric     bool IsTemplateSpecOrInst =
3317349cc55cSDimitry Andric         (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3318349cc55cSDimitry Andric          TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3319349cc55cSDimitry Andric 
33200b57cec5SDimitry Andric     switch (Tok.getKind()) {
33210b57cec5SDimitry Andric     default:
3322*06c3fb27SDimitry Andric       if (Tok.isRegularKeywordAttribute())
3323*06c3fb27SDimitry Andric         goto Attribute;
3324*06c3fb27SDimitry Andric 
33250b57cec5SDimitry Andric     DoneWithDeclSpec:
33260b57cec5SDimitry Andric       if (!AttrsLastTime)
33270b57cec5SDimitry Andric         ProhibitAttributes(attrs);
33280b57cec5SDimitry Andric       else {
332981ad6265SDimitry Andric         // Reject C++11 / C2x attributes that aren't type attributes.
333081ad6265SDimitry Andric         for (const ParsedAttr &PA : attrs) {
3331*06c3fb27SDimitry Andric           if (!PA.isCXX11Attribute() && !PA.isC2xAttribute() &&
3332*06c3fb27SDimitry Andric               !PA.isRegularKeywordAttribute())
333381ad6265SDimitry Andric             continue;
333481ad6265SDimitry Andric           if (PA.getKind() == ParsedAttr::UnknownAttribute)
333581ad6265SDimitry Andric             // We will warn about the unknown attribute elsewhere (in
333681ad6265SDimitry Andric             // SemaDeclAttr.cpp)
333781ad6265SDimitry Andric             continue;
333881ad6265SDimitry Andric           // GCC ignores this attribute when placed on the DeclSpec in [[]]
333981ad6265SDimitry Andric           // syntax, so we do the same.
334081ad6265SDimitry Andric           if (PA.getKind() == ParsedAttr::AT_VectorSize) {
334181ad6265SDimitry Andric             Diag(PA.getLoc(), diag::warn_attribute_ignored) << PA;
334281ad6265SDimitry Andric             PA.setInvalid();
334381ad6265SDimitry Andric             continue;
334481ad6265SDimitry Andric           }
334581ad6265SDimitry Andric           // We reject AT_LifetimeBound and AT_AnyX86NoCfCheck, even though they
334681ad6265SDimitry Andric           // are type attributes, because we historically haven't allowed these
334781ad6265SDimitry Andric           // to be used as type attributes in C++11 / C2x syntax.
334881ad6265SDimitry Andric           if (PA.isTypeAttr() && PA.getKind() != ParsedAttr::AT_LifetimeBound &&
334981ad6265SDimitry Andric               PA.getKind() != ParsedAttr::AT_AnyX86NoCfCheck)
335081ad6265SDimitry Andric             continue;
3351*06c3fb27SDimitry Andric           Diag(PA.getLoc(), diag::err_attribute_not_type_attr)
3352*06c3fb27SDimitry Andric               << PA << PA.isRegularKeywordAttribute();
335381ad6265SDimitry Andric           PA.setInvalid();
335481ad6265SDimitry Andric         }
33550b57cec5SDimitry Andric 
33560b57cec5SDimitry Andric         DS.takeAttributesFrom(attrs);
33570b57cec5SDimitry Andric       }
33580b57cec5SDimitry Andric 
33590b57cec5SDimitry Andric       // If this is not a declaration specifier token, we're done reading decl
33600b57cec5SDimitry Andric       // specifiers.  First verify that DeclSpec's are consistent.
33610b57cec5SDimitry Andric       DS.Finish(Actions, Policy);
33620b57cec5SDimitry Andric       return;
33630b57cec5SDimitry Andric 
33640b57cec5SDimitry Andric     case tok::l_square:
33650b57cec5SDimitry Andric     case tok::kw_alignas:
3366*06c3fb27SDimitry Andric       if (!isAllowedCXX11AttributeSpecifier())
33670b57cec5SDimitry Andric         goto DoneWithDeclSpec;
33680b57cec5SDimitry Andric 
3369*06c3fb27SDimitry Andric     Attribute:
33700b57cec5SDimitry Andric       ProhibitAttributes(attrs);
33710b57cec5SDimitry Andric       // FIXME: It would be good to recover by accepting the attributes,
33720b57cec5SDimitry Andric       //        but attempting to do that now would cause serious
33730b57cec5SDimitry Andric       //        madness in terms of diagnostics.
33740b57cec5SDimitry Andric       attrs.clear();
33750b57cec5SDimitry Andric       attrs.Range = SourceRange();
33760b57cec5SDimitry Andric 
33770b57cec5SDimitry Andric       ParseCXX11Attributes(attrs);
33780b57cec5SDimitry Andric       AttrsLastTime = true;
33790b57cec5SDimitry Andric       continue;
33800b57cec5SDimitry Andric 
33810b57cec5SDimitry Andric     case tok::code_completion: {
33820b57cec5SDimitry Andric       Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
33830b57cec5SDimitry Andric       if (DS.hasTypeSpecifier()) {
33840b57cec5SDimitry Andric         bool AllowNonIdentifiers
33850b57cec5SDimitry Andric           = (getCurScope()->getFlags() & (Scope::ControlScope |
33860b57cec5SDimitry Andric                                           Scope::BlockScope |
33870b57cec5SDimitry Andric                                           Scope::TemplateParamScope |
33880b57cec5SDimitry Andric                                           Scope::FunctionPrototypeScope |
33890b57cec5SDimitry Andric                                           Scope::AtCatchScope)) == 0;
33900b57cec5SDimitry Andric         bool AllowNestedNameSpecifiers
33910b57cec5SDimitry Andric           = DSContext == DeclSpecContext::DSC_top_level ||
33920b57cec5SDimitry Andric             (DSContext == DeclSpecContext::DSC_class && DS.isFriendSpecified());
33930b57cec5SDimitry Andric 
3394fe6060f1SDimitry Andric         cutOffParsing();
33950b57cec5SDimitry Andric         Actions.CodeCompleteDeclSpec(getCurScope(), DS,
33960b57cec5SDimitry Andric                                      AllowNonIdentifiers,
33970b57cec5SDimitry Andric                                      AllowNestedNameSpecifiers);
3398fe6060f1SDimitry Andric         return;
33990b57cec5SDimitry Andric       }
34000b57cec5SDimitry Andric 
3401bdd1243dSDimitry Andric       // Class context can appear inside a function/block, so prioritise that.
3402bdd1243dSDimitry Andric       if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
34030b57cec5SDimitry Andric         CCC = DSContext == DeclSpecContext::DSC_class ? Sema::PCC_MemberTemplate
34040b57cec5SDimitry Andric                                                       : Sema::PCC_Template;
34050b57cec5SDimitry Andric       else if (DSContext == DeclSpecContext::DSC_class)
34060b57cec5SDimitry Andric         CCC = Sema::PCC_Class;
3407bdd1243dSDimitry Andric       else if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
3408bdd1243dSDimitry Andric         CCC = Sema::PCC_LocalDeclarationSpecifiers;
34090b57cec5SDimitry Andric       else if (CurParsedObjCImpl)
34100b57cec5SDimitry Andric         CCC = Sema::PCC_ObjCImplementation;
34110b57cec5SDimitry Andric 
3412fe6060f1SDimitry Andric       cutOffParsing();
34130b57cec5SDimitry Andric       Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
3414fe6060f1SDimitry Andric       return;
34150b57cec5SDimitry Andric     }
34160b57cec5SDimitry Andric 
34170b57cec5SDimitry Andric     case tok::coloncolon: // ::foo::bar
34180b57cec5SDimitry Andric       // C++ scope specifier.  Annotate and loop, or bail out on error.
34190b57cec5SDimitry Andric       if (TryAnnotateCXXScopeToken(EnteringContext)) {
34200b57cec5SDimitry Andric         if (!DS.hasTypeSpecifier())
34210b57cec5SDimitry Andric           DS.SetTypeSpecError();
34220b57cec5SDimitry Andric         goto DoneWithDeclSpec;
34230b57cec5SDimitry Andric       }
34240b57cec5SDimitry Andric       if (Tok.is(tok::coloncolon)) // ::new or ::delete
34250b57cec5SDimitry Andric         goto DoneWithDeclSpec;
34260b57cec5SDimitry Andric       continue;
34270b57cec5SDimitry Andric 
34280b57cec5SDimitry Andric     case tok::annot_cxxscope: {
34290b57cec5SDimitry Andric       if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
34300b57cec5SDimitry Andric         goto DoneWithDeclSpec;
34310b57cec5SDimitry Andric 
34320b57cec5SDimitry Andric       CXXScopeSpec SS;
3433*06c3fb27SDimitry Andric       if (TemplateInfo.TemplateParams)
3434*06c3fb27SDimitry Andric         SS.setTemplateParamLists(*TemplateInfo.TemplateParams);
34350b57cec5SDimitry Andric       Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
34360b57cec5SDimitry Andric                                                    Tok.getAnnotationRange(),
34370b57cec5SDimitry Andric                                                    SS);
34380b57cec5SDimitry Andric 
34390b57cec5SDimitry Andric       // We are looking for a qualified typename.
34400b57cec5SDimitry Andric       Token Next = NextToken();
34415ffd83dbSDimitry Andric 
34425ffd83dbSDimitry Andric       TemplateIdAnnotation *TemplateId = Next.is(tok::annot_template_id)
34435ffd83dbSDimitry Andric                                              ? takeTemplateIdAnnotation(Next)
34445ffd83dbSDimitry Andric                                              : nullptr;
34455ffd83dbSDimitry Andric       if (TemplateId && TemplateId->hasInvalidName()) {
34465ffd83dbSDimitry Andric         // We found something like 'T::U<Args> x', but U is not a template.
34475ffd83dbSDimitry Andric         // Assume it was supposed to be a type.
34485ffd83dbSDimitry Andric         DS.SetTypeSpecError();
34495ffd83dbSDimitry Andric         ConsumeAnnotationToken();
34505ffd83dbSDimitry Andric         break;
34515ffd83dbSDimitry Andric       }
34525ffd83dbSDimitry Andric 
34535ffd83dbSDimitry Andric       if (TemplateId && TemplateId->Kind == TNK_Type_template) {
34540b57cec5SDimitry Andric         // We have a qualified template-id, e.g., N::A<int>
34550b57cec5SDimitry Andric 
34560b57cec5SDimitry Andric         // If this would be a valid constructor declaration with template
34570b57cec5SDimitry Andric         // arguments, we will reject the attempt to form an invalid type-id
34580b57cec5SDimitry Andric         // referring to the injected-class-name when we annotate the token,
34590b57cec5SDimitry Andric         // per C++ [class.qual]p2.
34600b57cec5SDimitry Andric         //
34610b57cec5SDimitry Andric         // To improve diagnostics for this case, parse the declaration as a
34620b57cec5SDimitry Andric         // constructor (and reject the extra template arguments later).
34630b57cec5SDimitry Andric         if ((DSContext == DeclSpecContext::DSC_top_level ||
34640b57cec5SDimitry Andric              DSContext == DeclSpecContext::DSC_class) &&
34650b57cec5SDimitry Andric             TemplateId->Name &&
34660b57cec5SDimitry Andric             Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS) &&
3467bdd1243dSDimitry Andric             isConstructorDeclarator(/*Unqualified=*/false,
3468bdd1243dSDimitry Andric                                     /*DeductionGuide=*/false,
3469bdd1243dSDimitry Andric                                     DS.isFriendSpecified())) {
34700b57cec5SDimitry Andric           // The user meant this to be an out-of-line constructor
34710b57cec5SDimitry Andric           // definition, but template arguments are not allowed
34720b57cec5SDimitry Andric           // there.  Just allow this as a constructor; we'll
34730b57cec5SDimitry Andric           // complain about it later.
34740b57cec5SDimitry Andric           goto DoneWithDeclSpec;
34750b57cec5SDimitry Andric         }
34760b57cec5SDimitry Andric 
34770b57cec5SDimitry Andric         DS.getTypeSpecScope() = SS;
34780b57cec5SDimitry Andric         ConsumeAnnotationToken(); // The C++ scope.
34790b57cec5SDimitry Andric         assert(Tok.is(tok::annot_template_id) &&
34800b57cec5SDimitry Andric                "ParseOptionalCXXScopeSpecifier not working");
3481bdd1243dSDimitry Andric         AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename);
348255e4f9d5SDimitry Andric         continue;
348355e4f9d5SDimitry Andric       }
348455e4f9d5SDimitry Andric 
3485*06c3fb27SDimitry Andric       if (TemplateId && TemplateId->Kind == TNK_Concept_template) {
348655e4f9d5SDimitry Andric         DS.getTypeSpecScope() = SS;
3487*06c3fb27SDimitry Andric         // This is probably a qualified placeholder-specifier, e.g., ::C<int>
3488*06c3fb27SDimitry Andric         // auto ... Consume the scope annotation and continue to consume the
3489*06c3fb27SDimitry Andric         // template-id as a placeholder-specifier. Let the next iteration
3490*06c3fb27SDimitry Andric         // diagnose a missing auto.
349155e4f9d5SDimitry Andric         ConsumeAnnotationToken();
34920b57cec5SDimitry Andric         continue;
34930b57cec5SDimitry Andric       }
34940b57cec5SDimitry Andric 
34950b57cec5SDimitry Andric       if (Next.is(tok::annot_typename)) {
34960b57cec5SDimitry Andric         DS.getTypeSpecScope() = SS;
34970b57cec5SDimitry Andric         ConsumeAnnotationToken(); // The C++ scope.
34985ffd83dbSDimitry Andric         TypeResult T = getTypeAnnotation(Tok);
34990b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
35000b57cec5SDimitry Andric                                        Tok.getAnnotationEndLoc(),
35010b57cec5SDimitry Andric                                        PrevSpec, DiagID, T, Policy);
35020b57cec5SDimitry Andric         if (isInvalid)
35030b57cec5SDimitry Andric           break;
35040b57cec5SDimitry Andric         DS.SetRangeEnd(Tok.getAnnotationEndLoc());
35050b57cec5SDimitry Andric         ConsumeAnnotationToken(); // The typename
35060b57cec5SDimitry Andric       }
35070b57cec5SDimitry Andric 
3508bdd1243dSDimitry Andric       if (AllowImplicitTypename == ImplicitTypenameContext::Yes &&
3509bdd1243dSDimitry Andric           Next.is(tok::annot_template_id) &&
3510bdd1243dSDimitry Andric           static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
3511bdd1243dSDimitry Andric                   ->Kind == TNK_Dependent_template_name) {
3512bdd1243dSDimitry Andric         DS.getTypeSpecScope() = SS;
3513bdd1243dSDimitry Andric         ConsumeAnnotationToken(); // The C++ scope.
3514bdd1243dSDimitry Andric         AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename);
3515bdd1243dSDimitry Andric         continue;
3516bdd1243dSDimitry Andric       }
3517bdd1243dSDimitry Andric 
35180b57cec5SDimitry Andric       if (Next.isNot(tok::identifier))
35190b57cec5SDimitry Andric         goto DoneWithDeclSpec;
35200b57cec5SDimitry Andric 
35210b57cec5SDimitry Andric       // Check whether this is a constructor declaration. If we're in a
35220b57cec5SDimitry Andric       // context where the identifier could be a class name, and it has the
35230b57cec5SDimitry Andric       // shape of a constructor declaration, process it as one.
35240b57cec5SDimitry Andric       if ((DSContext == DeclSpecContext::DSC_top_level ||
35250b57cec5SDimitry Andric            DSContext == DeclSpecContext::DSC_class) &&
35260b57cec5SDimitry Andric           Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
35270b57cec5SDimitry Andric                                      &SS) &&
3528bdd1243dSDimitry Andric           isConstructorDeclarator(/*Unqualified=*/false,
3529bdd1243dSDimitry Andric                                   /*DeductionGuide=*/false,
3530*06c3fb27SDimitry Andric                                   DS.isFriendSpecified(),
3531*06c3fb27SDimitry Andric                                   &TemplateInfo))
35320b57cec5SDimitry Andric         goto DoneWithDeclSpec;
35330b57cec5SDimitry Andric 
3534349cc55cSDimitry Andric       // C++20 [temp.spec] 13.9/6.
3535349cc55cSDimitry Andric       // This disables the access checking rules for function template explicit
3536349cc55cSDimitry Andric       // instantiation and explicit specialization:
3537349cc55cSDimitry Andric       // - `return type`.
3538349cc55cSDimitry Andric       SuppressAccessChecks SAC(*this, IsTemplateSpecOrInst);
3539349cc55cSDimitry Andric 
3540bdd1243dSDimitry Andric       ParsedType TypeRep = Actions.getTypeName(
3541bdd1243dSDimitry Andric           *Next.getIdentifierInfo(), Next.getLocation(), getCurScope(), &SS,
3542bdd1243dSDimitry Andric           false, false, nullptr,
35430b57cec5SDimitry Andric           /*IsCtorOrDtorName=*/false,
35440b57cec5SDimitry Andric           /*WantNontrivialTypeSourceInfo=*/true,
3545bdd1243dSDimitry Andric           isClassTemplateDeductionContext(DSContext), AllowImplicitTypename);
35460b57cec5SDimitry Andric 
3547349cc55cSDimitry Andric       if (IsTemplateSpecOrInst)
3548349cc55cSDimitry Andric         SAC.done();
3549349cc55cSDimitry Andric 
35500b57cec5SDimitry Andric       // If the referenced identifier is not a type, then this declspec is
35510b57cec5SDimitry Andric       // erroneous: We already checked about that it has no type specifier, and
35520b57cec5SDimitry Andric       // C++ doesn't have implicit int.  Diagnose it as a typo w.r.t. to the
35530b57cec5SDimitry Andric       // typename.
35540b57cec5SDimitry Andric       if (!TypeRep) {
355555e4f9d5SDimitry Andric         if (TryAnnotateTypeConstraint())
355655e4f9d5SDimitry Andric           goto DoneWithDeclSpec;
35575ffd83dbSDimitry Andric         if (Tok.isNot(tok::annot_cxxscope) ||
35585ffd83dbSDimitry Andric             NextToken().isNot(tok::identifier))
3559aec4c088SDimitry Andric           continue;
35600b57cec5SDimitry Andric         // Eat the scope spec so the identifier is current.
35610b57cec5SDimitry Andric         ConsumeAnnotationToken();
356281ad6265SDimitry Andric         ParsedAttributes Attrs(AttrFactory);
35630b57cec5SDimitry Andric         if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
35640b57cec5SDimitry Andric           if (!Attrs.empty()) {
35650b57cec5SDimitry Andric             AttrsLastTime = true;
35660b57cec5SDimitry Andric             attrs.takeAllFrom(Attrs);
35670b57cec5SDimitry Andric           }
35680b57cec5SDimitry Andric           continue;
35690b57cec5SDimitry Andric         }
35700b57cec5SDimitry Andric         goto DoneWithDeclSpec;
35710b57cec5SDimitry Andric       }
35720b57cec5SDimitry Andric 
35730b57cec5SDimitry Andric       DS.getTypeSpecScope() = SS;
35740b57cec5SDimitry Andric       ConsumeAnnotationToken(); // The C++ scope.
35750b57cec5SDimitry Andric 
35760b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
35770b57cec5SDimitry Andric                                      DiagID, TypeRep, Policy);
35780b57cec5SDimitry Andric       if (isInvalid)
35790b57cec5SDimitry Andric         break;
35800b57cec5SDimitry Andric 
35810b57cec5SDimitry Andric       DS.SetRangeEnd(Tok.getLocation());
35820b57cec5SDimitry Andric       ConsumeToken(); // The typename.
35830b57cec5SDimitry Andric 
35840b57cec5SDimitry Andric       continue;
35850b57cec5SDimitry Andric     }
35860b57cec5SDimitry Andric 
35870b57cec5SDimitry Andric     case tok::annot_typename: {
35880b57cec5SDimitry Andric       // If we've previously seen a tag definition, we were almost surely
35890b57cec5SDimitry Andric       // missing a semicolon after it.
35900b57cec5SDimitry Andric       if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
35910b57cec5SDimitry Andric         goto DoneWithDeclSpec;
35920b57cec5SDimitry Andric 
35935ffd83dbSDimitry Andric       TypeResult T = getTypeAnnotation(Tok);
35940b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
35950b57cec5SDimitry Andric                                      DiagID, T, Policy);
35960b57cec5SDimitry Andric       if (isInvalid)
35970b57cec5SDimitry Andric         break;
35980b57cec5SDimitry Andric 
35990b57cec5SDimitry Andric       DS.SetRangeEnd(Tok.getAnnotationEndLoc());
36000b57cec5SDimitry Andric       ConsumeAnnotationToken(); // The typename
36010b57cec5SDimitry Andric 
36020b57cec5SDimitry Andric       continue;
36030b57cec5SDimitry Andric     }
36040b57cec5SDimitry Andric 
36050b57cec5SDimitry Andric     case tok::kw___is_signed:
36060b57cec5SDimitry Andric       // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
36070b57cec5SDimitry Andric       // typically treats it as a trait. If we see __is_signed as it appears
36080b57cec5SDimitry Andric       // in libstdc++, e.g.,
36090b57cec5SDimitry Andric       //
36100b57cec5SDimitry Andric       //   static const bool __is_signed;
36110b57cec5SDimitry Andric       //
36120b57cec5SDimitry Andric       // then treat __is_signed as an identifier rather than as a keyword.
36130b57cec5SDimitry Andric       if (DS.getTypeSpecType() == TST_bool &&
36140b57cec5SDimitry Andric           DS.getTypeQualifiers() == DeclSpec::TQ_const &&
36150b57cec5SDimitry Andric           DS.getStorageClassSpec() == DeclSpec::SCS_static)
36160b57cec5SDimitry Andric         TryKeywordIdentFallback(true);
36170b57cec5SDimitry Andric 
36180b57cec5SDimitry Andric       // We're done with the declaration-specifiers.
36190b57cec5SDimitry Andric       goto DoneWithDeclSpec;
36200b57cec5SDimitry Andric 
36210b57cec5SDimitry Andric       // typedef-name
36220b57cec5SDimitry Andric     case tok::kw___super:
36230b57cec5SDimitry Andric     case tok::kw_decltype:
3624bdd1243dSDimitry Andric     case tok::identifier:
3625bdd1243dSDimitry Andric     ParseIdentifier: {
36260b57cec5SDimitry Andric       // This identifier can only be a typedef name if we haven't already seen
36270b57cec5SDimitry Andric       // a type-specifier.  Without this check we misparse:
36280b57cec5SDimitry Andric       //  typedef int X; struct Y { short X; };  as 'short int'.
36290b57cec5SDimitry Andric       if (DS.hasTypeSpecifier())
36300b57cec5SDimitry Andric         goto DoneWithDeclSpec;
36310b57cec5SDimitry Andric 
36320b57cec5SDimitry Andric       // If the token is an identifier named "__declspec" and Microsoft
36330b57cec5SDimitry Andric       // extensions are not enabled, it is likely that there will be cascading
36340b57cec5SDimitry Andric       // parse errors if this really is a __declspec attribute. Attempt to
36350b57cec5SDimitry Andric       // recognize that scenario and recover gracefully.
36360b57cec5SDimitry Andric       if (!getLangOpts().DeclSpecKeyword && Tok.is(tok::identifier) &&
36370b57cec5SDimitry Andric           Tok.getIdentifierInfo()->getName().equals("__declspec")) {
36380b57cec5SDimitry Andric         Diag(Loc, diag::err_ms_attributes_not_enabled);
36390b57cec5SDimitry Andric 
36400b57cec5SDimitry Andric         // The next token should be an open paren. If it is, eat the entire
36410b57cec5SDimitry Andric         // attribute declaration and continue.
36420b57cec5SDimitry Andric         if (NextToken().is(tok::l_paren)) {
36430b57cec5SDimitry Andric           // Consume the __declspec identifier.
36440b57cec5SDimitry Andric           ConsumeToken();
36450b57cec5SDimitry Andric 
36460b57cec5SDimitry Andric           // Eat the parens and everything between them.
36470b57cec5SDimitry Andric           BalancedDelimiterTracker T(*this, tok::l_paren);
36480b57cec5SDimitry Andric           if (T.consumeOpen()) {
36490b57cec5SDimitry Andric             assert(false && "Not a left paren?");
36500b57cec5SDimitry Andric             return;
36510b57cec5SDimitry Andric           }
36520b57cec5SDimitry Andric           T.skipToEnd();
36530b57cec5SDimitry Andric           continue;
36540b57cec5SDimitry Andric         }
36550b57cec5SDimitry Andric       }
36560b57cec5SDimitry Andric 
36570b57cec5SDimitry Andric       // In C++, check to see if this is a scope specifier like foo::bar::, if
36580b57cec5SDimitry Andric       // so handle it as such.  This is important for ctor parsing.
36590b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus) {
3660349cc55cSDimitry Andric         // C++20 [temp.spec] 13.9/6.
3661349cc55cSDimitry Andric         // This disables the access checking rules for function template
3662349cc55cSDimitry Andric         // explicit instantiation and explicit specialization:
3663349cc55cSDimitry Andric         // - `return type`.
3664349cc55cSDimitry Andric         SuppressAccessChecks SAC(*this, IsTemplateSpecOrInst);
3665349cc55cSDimitry Andric 
3666349cc55cSDimitry Andric         const bool Success = TryAnnotateCXXScopeToken(EnteringContext);
3667349cc55cSDimitry Andric 
3668349cc55cSDimitry Andric         if (IsTemplateSpecOrInst)
3669349cc55cSDimitry Andric           SAC.done();
3670349cc55cSDimitry Andric 
3671349cc55cSDimitry Andric         if (Success) {
3672349cc55cSDimitry Andric           if (IsTemplateSpecOrInst)
3673349cc55cSDimitry Andric             SAC.redelay();
36740b57cec5SDimitry Andric           DS.SetTypeSpecError();
36750b57cec5SDimitry Andric           goto DoneWithDeclSpec;
36760b57cec5SDimitry Andric         }
3677349cc55cSDimitry Andric 
36780b57cec5SDimitry Andric         if (!Tok.is(tok::identifier))
36790b57cec5SDimitry Andric           continue;
36800b57cec5SDimitry Andric       }
36810b57cec5SDimitry Andric 
36820b57cec5SDimitry Andric       // Check for need to substitute AltiVec keyword tokens.
36830b57cec5SDimitry Andric       if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
36840b57cec5SDimitry Andric         break;
36850b57cec5SDimitry Andric 
36860b57cec5SDimitry Andric       // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
36870b57cec5SDimitry Andric       //                allow the use of a typedef name as a type specifier.
36880b57cec5SDimitry Andric       if (DS.isTypeAltiVecVector())
36890b57cec5SDimitry Andric         goto DoneWithDeclSpec;
36900b57cec5SDimitry Andric 
36910b57cec5SDimitry Andric       if (DSContext == DeclSpecContext::DSC_objc_method_result &&
36920b57cec5SDimitry Andric           isObjCInstancetype()) {
36930b57cec5SDimitry Andric         ParsedType TypeRep = Actions.ActOnObjCInstanceType(Loc);
36940b57cec5SDimitry Andric         assert(TypeRep);
36950b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
36960b57cec5SDimitry Andric                                        DiagID, TypeRep, Policy);
36970b57cec5SDimitry Andric         if (isInvalid)
36980b57cec5SDimitry Andric           break;
36990b57cec5SDimitry Andric 
37000b57cec5SDimitry Andric         DS.SetRangeEnd(Loc);
37010b57cec5SDimitry Andric         ConsumeToken();
37020b57cec5SDimitry Andric         continue;
37030b57cec5SDimitry Andric       }
37040b57cec5SDimitry Andric 
37050b57cec5SDimitry Andric       // If we're in a context where the identifier could be a class name,
37060b57cec5SDimitry Andric       // check whether this is a constructor declaration.
37070b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class &&
37080b57cec5SDimitry Andric           Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
3709bdd1243dSDimitry Andric           isConstructorDeclarator(/*Unqualified=*/true,
3710bdd1243dSDimitry Andric                                   /*DeductionGuide=*/false,
3711bdd1243dSDimitry Andric                                   DS.isFriendSpecified()))
37120b57cec5SDimitry Andric         goto DoneWithDeclSpec;
37130b57cec5SDimitry Andric 
37140b57cec5SDimitry Andric       ParsedType TypeRep = Actions.getTypeName(
37150b57cec5SDimitry Andric           *Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), nullptr,
37160b57cec5SDimitry Andric           false, false, nullptr, false, false,
37170b57cec5SDimitry Andric           isClassTemplateDeductionContext(DSContext));
37180b57cec5SDimitry Andric 
37190b57cec5SDimitry Andric       // If this is not a typedef name, don't parse it as part of the declspec,
37200b57cec5SDimitry Andric       // it must be an implicit int or an error.
37210b57cec5SDimitry Andric       if (!TypeRep) {
372255e4f9d5SDimitry Andric         if (TryAnnotateTypeConstraint())
372355e4f9d5SDimitry Andric           goto DoneWithDeclSpec;
37245ffd83dbSDimitry Andric         if (Tok.isNot(tok::identifier))
3725aec4c088SDimitry Andric           continue;
372681ad6265SDimitry Andric         ParsedAttributes Attrs(AttrFactory);
37270b57cec5SDimitry Andric         if (ParseImplicitInt(DS, nullptr, TemplateInfo, AS, DSContext, Attrs)) {
37280b57cec5SDimitry Andric           if (!Attrs.empty()) {
37290b57cec5SDimitry Andric             AttrsLastTime = true;
37300b57cec5SDimitry Andric             attrs.takeAllFrom(Attrs);
37310b57cec5SDimitry Andric           }
37320b57cec5SDimitry Andric           continue;
37330b57cec5SDimitry Andric         }
37340b57cec5SDimitry Andric         goto DoneWithDeclSpec;
37350b57cec5SDimitry Andric       }
37360b57cec5SDimitry Andric 
37370b57cec5SDimitry Andric       // Likewise, if this is a context where the identifier could be a template
37380b57cec5SDimitry Andric       // name, check whether this is a deduction guide declaration.
3739*06c3fb27SDimitry Andric       CXXScopeSpec SS;
37400b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus17 &&
37410b57cec5SDimitry Andric           (DSContext == DeclSpecContext::DSC_class ||
37420b57cec5SDimitry Andric            DSContext == DeclSpecContext::DSC_top_level) &&
37430b57cec5SDimitry Andric           Actions.isDeductionGuideName(getCurScope(), *Tok.getIdentifierInfo(),
3744*06c3fb27SDimitry Andric                                        Tok.getLocation(), SS) &&
37450b57cec5SDimitry Andric           isConstructorDeclarator(/*Unqualified*/ true,
37460b57cec5SDimitry Andric                                   /*DeductionGuide*/ true))
37470b57cec5SDimitry Andric         goto DoneWithDeclSpec;
37480b57cec5SDimitry Andric 
37490b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
37500b57cec5SDimitry Andric                                      DiagID, TypeRep, Policy);
37510b57cec5SDimitry Andric       if (isInvalid)
37520b57cec5SDimitry Andric         break;
37530b57cec5SDimitry Andric 
37540b57cec5SDimitry Andric       DS.SetRangeEnd(Tok.getLocation());
37550b57cec5SDimitry Andric       ConsumeToken(); // The identifier
37560b57cec5SDimitry Andric 
37570b57cec5SDimitry Andric       // Objective-C supports type arguments and protocol references
37580b57cec5SDimitry Andric       // following an Objective-C object or object pointer
37590b57cec5SDimitry Andric       // type. Handle either one of them.
37600b57cec5SDimitry Andric       if (Tok.is(tok::less) && getLangOpts().ObjC) {
37610b57cec5SDimitry Andric         SourceLocation NewEndLoc;
37620b57cec5SDimitry Andric         TypeResult NewTypeRep = parseObjCTypeArgsAndProtocolQualifiers(
37630b57cec5SDimitry Andric                                   Loc, TypeRep, /*consumeLastToken=*/true,
37640b57cec5SDimitry Andric                                   NewEndLoc);
37650b57cec5SDimitry Andric         if (NewTypeRep.isUsable()) {
37660b57cec5SDimitry Andric           DS.UpdateTypeRep(NewTypeRep.get());
37670b57cec5SDimitry Andric           DS.SetRangeEnd(NewEndLoc);
37680b57cec5SDimitry Andric         }
37690b57cec5SDimitry Andric       }
37700b57cec5SDimitry Andric 
37710b57cec5SDimitry Andric       // Need to support trailing type qualifiers (e.g. "id<p> const").
37720b57cec5SDimitry Andric       // If a type specifier follows, it will be diagnosed elsewhere.
37730b57cec5SDimitry Andric       continue;
37740b57cec5SDimitry Andric     }
37750b57cec5SDimitry Andric 
377655e4f9d5SDimitry Andric       // type-name or placeholder-specifier
37770b57cec5SDimitry Andric     case tok::annot_template_id: {
37780b57cec5SDimitry Andric       TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
37795ffd83dbSDimitry Andric 
37805ffd83dbSDimitry Andric       if (TemplateId->hasInvalidName()) {
37815ffd83dbSDimitry Andric         DS.SetTypeSpecError();
37825ffd83dbSDimitry Andric         break;
37835ffd83dbSDimitry Andric       }
37845ffd83dbSDimitry Andric 
378555e4f9d5SDimitry Andric       if (TemplateId->Kind == TNK_Concept_template) {
37865ffd83dbSDimitry Andric         // If we've already diagnosed that this type-constraint has invalid
3787bdd1243dSDimitry Andric         // arguments, drop it and just form 'auto' or 'decltype(auto)'.
37885ffd83dbSDimitry Andric         if (TemplateId->hasInvalidArgs())
37895ffd83dbSDimitry Andric           TemplateId = nullptr;
37905ffd83dbSDimitry Andric 
3791bdd1243dSDimitry Andric         // Any of the following tokens are likely the start of the user
3792bdd1243dSDimitry Andric         // forgetting 'auto' or 'decltype(auto)', so diagnose.
3793bdd1243dSDimitry Andric         // Note: if updating this list, please make sure we update
3794bdd1243dSDimitry Andric         // isCXXDeclarationSpecifier's check for IsPlaceholderSpecifier to have
3795bdd1243dSDimitry Andric         // a matching list.
3796bdd1243dSDimitry Andric         if (NextToken().isOneOf(tok::identifier, tok::kw_const,
3797bdd1243dSDimitry Andric                                 tok::kw_volatile, tok::kw_restrict, tok::amp,
3798bdd1243dSDimitry Andric                                 tok::ampamp)) {
379955e4f9d5SDimitry Andric           Diag(Loc, diag::err_placeholder_expected_auto_or_decltype_auto)
380055e4f9d5SDimitry Andric               << FixItHint::CreateInsertion(NextToken().getLocation(), "auto");
380155e4f9d5SDimitry Andric           // Attempt to continue as if 'auto' was placed here.
380255e4f9d5SDimitry Andric           isInvalid = DS.SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID,
380355e4f9d5SDimitry Andric                                          TemplateId, Policy);
380455e4f9d5SDimitry Andric           break;
380555e4f9d5SDimitry Andric         }
380655e4f9d5SDimitry Andric         if (!NextToken().isOneOf(tok::kw_auto, tok::kw_decltype))
380755e4f9d5SDimitry Andric             goto DoneWithDeclSpec;
3808*06c3fb27SDimitry Andric 
3809*06c3fb27SDimitry Andric         if (TemplateId && !isInvalid && Actions.CheckTypeConstraint(TemplateId))
3810*06c3fb27SDimitry Andric             TemplateId = nullptr;
3811*06c3fb27SDimitry Andric 
381255e4f9d5SDimitry Andric         ConsumeAnnotationToken();
381355e4f9d5SDimitry Andric         SourceLocation AutoLoc = Tok.getLocation();
381455e4f9d5SDimitry Andric         if (TryConsumeToken(tok::kw_decltype)) {
381555e4f9d5SDimitry Andric           BalancedDelimiterTracker Tracker(*this, tok::l_paren);
381655e4f9d5SDimitry Andric           if (Tracker.consumeOpen()) {
381755e4f9d5SDimitry Andric             // Something like `void foo(Iterator decltype i)`
381855e4f9d5SDimitry Andric             Diag(Tok, diag::err_expected) << tok::l_paren;
381955e4f9d5SDimitry Andric           } else {
382055e4f9d5SDimitry Andric             if (!TryConsumeToken(tok::kw_auto)) {
382155e4f9d5SDimitry Andric               // Something like `void foo(Iterator decltype(int) i)`
382255e4f9d5SDimitry Andric               Tracker.skipToEnd();
382355e4f9d5SDimitry Andric               Diag(Tok, diag::err_placeholder_expected_auto_or_decltype_auto)
382455e4f9d5SDimitry Andric                 << FixItHint::CreateReplacement(SourceRange(AutoLoc,
382555e4f9d5SDimitry Andric                                                             Tok.getLocation()),
382655e4f9d5SDimitry Andric                                                 "auto");
382755e4f9d5SDimitry Andric             } else {
382855e4f9d5SDimitry Andric               Tracker.consumeClose();
382955e4f9d5SDimitry Andric             }
383055e4f9d5SDimitry Andric           }
383155e4f9d5SDimitry Andric           ConsumedEnd = Tok.getLocation();
3832bdd1243dSDimitry Andric           DS.setTypeArgumentRange(Tracker.getRange());
383355e4f9d5SDimitry Andric           // Even if something went wrong above, continue as if we've seen
383455e4f9d5SDimitry Andric           // `decltype(auto)`.
383555e4f9d5SDimitry Andric           isInvalid = DS.SetTypeSpecType(TST_decltype_auto, Loc, PrevSpec,
383655e4f9d5SDimitry Andric                                          DiagID, TemplateId, Policy);
383755e4f9d5SDimitry Andric         } else {
383804eeddc0SDimitry Andric           isInvalid = DS.SetTypeSpecType(TST_auto, AutoLoc, PrevSpec, DiagID,
383955e4f9d5SDimitry Andric                                          TemplateId, Policy);
384055e4f9d5SDimitry Andric         }
384155e4f9d5SDimitry Andric         break;
384255e4f9d5SDimitry Andric       }
384355e4f9d5SDimitry Andric 
38440b57cec5SDimitry Andric       if (TemplateId->Kind != TNK_Type_template &&
38450b57cec5SDimitry Andric           TemplateId->Kind != TNK_Undeclared_template) {
38460b57cec5SDimitry Andric         // This template-id does not refer to a type name, so we're
38470b57cec5SDimitry Andric         // done with the type-specifiers.
38480b57cec5SDimitry Andric         goto DoneWithDeclSpec;
38490b57cec5SDimitry Andric       }
38500b57cec5SDimitry Andric 
38510b57cec5SDimitry Andric       // If we're in a context where the template-id could be a
38520b57cec5SDimitry Andric       // constructor name or specialization, check whether this is a
38530b57cec5SDimitry Andric       // constructor declaration.
38540b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class &&
38550b57cec5SDimitry Andric           Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
3856bdd1243dSDimitry Andric           isConstructorDeclarator(/*Unqualified=*/true,
3857bdd1243dSDimitry Andric                                   /*DeductionGuide=*/false,
3858bdd1243dSDimitry Andric                                   DS.isFriendSpecified()))
38590b57cec5SDimitry Andric         goto DoneWithDeclSpec;
38600b57cec5SDimitry Andric 
38610b57cec5SDimitry Andric       // Turn the template-id annotation token into a type annotation
38620b57cec5SDimitry Andric       // token, then try again to parse it as a type-specifier.
386355e4f9d5SDimitry Andric       CXXScopeSpec SS;
3864bdd1243dSDimitry Andric       AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename);
38650b57cec5SDimitry Andric       continue;
38660b57cec5SDimitry Andric     }
38670b57cec5SDimitry Andric 
3868fe6060f1SDimitry Andric     // Attributes support.
38690b57cec5SDimitry Andric     case tok::kw___attribute:
38700b57cec5SDimitry Andric     case tok::kw___declspec:
387181ad6265SDimitry Andric       ParseAttributes(PAKM_GNU | PAKM_Declspec, DS.getAttributes(), LateAttrs);
38720b57cec5SDimitry Andric       continue;
38730b57cec5SDimitry Andric 
38740b57cec5SDimitry Andric     // Microsoft single token adornments.
38750b57cec5SDimitry Andric     case tok::kw___forceinline: {
38760b57cec5SDimitry Andric       isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
38770b57cec5SDimitry Andric       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
38780b57cec5SDimitry Andric       SourceLocation AttrNameLoc = Tok.getLocation();
38790b57cec5SDimitry Andric       DS.getAttributes().addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc,
3880*06c3fb27SDimitry Andric                                 nullptr, 0, tok::kw___forceinline);
38810b57cec5SDimitry Andric       break;
38820b57cec5SDimitry Andric     }
38830b57cec5SDimitry Andric 
38840b57cec5SDimitry Andric     case tok::kw___unaligned:
38850b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID,
38860b57cec5SDimitry Andric                                  getLangOpts());
38870b57cec5SDimitry Andric       break;
38880b57cec5SDimitry Andric 
38890b57cec5SDimitry Andric     case tok::kw___sptr:
38900b57cec5SDimitry Andric     case tok::kw___uptr:
38910b57cec5SDimitry Andric     case tok::kw___ptr64:
38920b57cec5SDimitry Andric     case tok::kw___ptr32:
38930b57cec5SDimitry Andric     case tok::kw___w64:
38940b57cec5SDimitry Andric     case tok::kw___cdecl:
38950b57cec5SDimitry Andric     case tok::kw___stdcall:
38960b57cec5SDimitry Andric     case tok::kw___fastcall:
38970b57cec5SDimitry Andric     case tok::kw___thiscall:
38980b57cec5SDimitry Andric     case tok::kw___regcall:
38990b57cec5SDimitry Andric     case tok::kw___vectorcall:
39000b57cec5SDimitry Andric       ParseMicrosoftTypeAttributes(DS.getAttributes());
39010b57cec5SDimitry Andric       continue;
39020b57cec5SDimitry Andric 
3903*06c3fb27SDimitry Andric     case tok::kw___funcref:
3904*06c3fb27SDimitry Andric       ParseWebAssemblyFuncrefTypeAttribute(DS.getAttributes());
3905*06c3fb27SDimitry Andric       continue;
3906*06c3fb27SDimitry Andric 
39070b57cec5SDimitry Andric     // Borland single token adornments.
39080b57cec5SDimitry Andric     case tok::kw___pascal:
39090b57cec5SDimitry Andric       ParseBorlandTypeAttributes(DS.getAttributes());
39100b57cec5SDimitry Andric       continue;
39110b57cec5SDimitry Andric 
39120b57cec5SDimitry Andric     // OpenCL single token adornments.
39130b57cec5SDimitry Andric     case tok::kw___kernel:
39140b57cec5SDimitry Andric       ParseOpenCLKernelAttributes(DS.getAttributes());
39150b57cec5SDimitry Andric       continue;
39160b57cec5SDimitry Andric 
391781ad6265SDimitry Andric     // CUDA/HIP single token adornments.
391881ad6265SDimitry Andric     case tok::kw___noinline__:
391981ad6265SDimitry Andric       ParseCUDAFunctionAttributes(DS.getAttributes());
392081ad6265SDimitry Andric       continue;
392181ad6265SDimitry Andric 
39220b57cec5SDimitry Andric     // Nullability type specifiers.
39230b57cec5SDimitry Andric     case tok::kw__Nonnull:
39240b57cec5SDimitry Andric     case tok::kw__Nullable:
3925e8d8bef9SDimitry Andric     case tok::kw__Nullable_result:
39260b57cec5SDimitry Andric     case tok::kw__Null_unspecified:
39270b57cec5SDimitry Andric       ParseNullabilityTypeSpecifiers(DS.getAttributes());
39280b57cec5SDimitry Andric       continue;
39290b57cec5SDimitry Andric 
39300b57cec5SDimitry Andric     // Objective-C 'kindof' types.
39310b57cec5SDimitry Andric     case tok::kw___kindof:
39320b57cec5SDimitry Andric       DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc,
3933*06c3fb27SDimitry Andric                                 nullptr, 0, tok::kw___kindof);
39340b57cec5SDimitry Andric       (void)ConsumeToken();
39350b57cec5SDimitry Andric       continue;
39360b57cec5SDimitry Andric 
39370b57cec5SDimitry Andric     // storage-class-specifier
39380b57cec5SDimitry Andric     case tok::kw_typedef:
39390b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
39400b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
39410b57cec5SDimitry Andric       isStorageClass = true;
39420b57cec5SDimitry Andric       break;
39430b57cec5SDimitry Andric     case tok::kw_extern:
39440b57cec5SDimitry Andric       if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
39450b57cec5SDimitry Andric         Diag(Tok, diag::ext_thread_before) << "extern";
39460b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
39470b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
39480b57cec5SDimitry Andric       isStorageClass = true;
39490b57cec5SDimitry Andric       break;
39500b57cec5SDimitry Andric     case tok::kw___private_extern__:
39510b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
39520b57cec5SDimitry Andric                                          Loc, PrevSpec, DiagID, Policy);
39530b57cec5SDimitry Andric       isStorageClass = true;
39540b57cec5SDimitry Andric       break;
39550b57cec5SDimitry Andric     case tok::kw_static:
39560b57cec5SDimitry Andric       if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
39570b57cec5SDimitry Andric         Diag(Tok, diag::ext_thread_before) << "static";
39580b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
39590b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
39600b57cec5SDimitry Andric       isStorageClass = true;
39610b57cec5SDimitry Andric       break;
39620b57cec5SDimitry Andric     case tok::kw_auto:
39630b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus11) {
39640b57cec5SDimitry Andric         if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
39650b57cec5SDimitry Andric           isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
39660b57cec5SDimitry Andric                                              PrevSpec, DiagID, Policy);
39670b57cec5SDimitry Andric           if (!isInvalid)
39680b57cec5SDimitry Andric             Diag(Tok, diag::ext_auto_storage_class)
39690b57cec5SDimitry Andric               << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
39700b57cec5SDimitry Andric         } else
39710b57cec5SDimitry Andric           isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
39720b57cec5SDimitry Andric                                          DiagID, Policy);
39730b57cec5SDimitry Andric       } else
39740b57cec5SDimitry Andric         isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
39750b57cec5SDimitry Andric                                            PrevSpec, DiagID, Policy);
39760b57cec5SDimitry Andric       isStorageClass = true;
39770b57cec5SDimitry Andric       break;
39780b57cec5SDimitry Andric     case tok::kw___auto_type:
39790b57cec5SDimitry Andric       Diag(Tok, diag::ext_auto_type);
39800b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto_type, Loc, PrevSpec,
39810b57cec5SDimitry Andric                                      DiagID, Policy);
39820b57cec5SDimitry Andric       break;
39830b57cec5SDimitry Andric     case tok::kw_register:
39840b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
39850b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
39860b57cec5SDimitry Andric       isStorageClass = true;
39870b57cec5SDimitry Andric       break;
39880b57cec5SDimitry Andric     case tok::kw_mutable:
39890b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
39900b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
39910b57cec5SDimitry Andric       isStorageClass = true;
39920b57cec5SDimitry Andric       break;
39930b57cec5SDimitry Andric     case tok::kw___thread:
39940b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
39950b57cec5SDimitry Andric                                                PrevSpec, DiagID);
39960b57cec5SDimitry Andric       isStorageClass = true;
39970b57cec5SDimitry Andric       break;
39980b57cec5SDimitry Andric     case tok::kw_thread_local:
3999*06c3fb27SDimitry Andric       if (getLangOpts().C2x)
4000*06c3fb27SDimitry Andric         Diag(Tok, diag::warn_c2x_compat_keyword) << Tok.getName();
40010b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
40020b57cec5SDimitry Andric                                                PrevSpec, DiagID);
40030b57cec5SDimitry Andric       isStorageClass = true;
40040b57cec5SDimitry Andric       break;
40050b57cec5SDimitry Andric     case tok::kw__Thread_local:
4006a7dea167SDimitry Andric       if (!getLangOpts().C11)
4007a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
40080b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
40090b57cec5SDimitry Andric                                                Loc, PrevSpec, DiagID);
40100b57cec5SDimitry Andric       isStorageClass = true;
40110b57cec5SDimitry Andric       break;
40120b57cec5SDimitry Andric 
40130b57cec5SDimitry Andric     // function-specifier
40140b57cec5SDimitry Andric     case tok::kw_inline:
40150b57cec5SDimitry Andric       isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
40160b57cec5SDimitry Andric       break;
40170b57cec5SDimitry Andric     case tok::kw_virtual:
40180b57cec5SDimitry Andric       // C++ for OpenCL does not allow virtual function qualifier, to avoid
40190b57cec5SDimitry Andric       // function pointers restricted in OpenCL v2.0 s6.9.a.
4020e8d8bef9SDimitry Andric       if (getLangOpts().OpenCLCPlusPlus &&
4021fe6060f1SDimitry Andric           !getActions().getOpenCLOptions().isAvailableOption(
4022fe6060f1SDimitry Andric               "__cl_clang_function_pointers", getLangOpts())) {
40230b57cec5SDimitry Andric         DiagID = diag::err_openclcxx_virtual_function;
40240b57cec5SDimitry Andric         PrevSpec = Tok.getIdentifierInfo()->getNameStart();
40250b57cec5SDimitry Andric         isInvalid = true;
4026e8d8bef9SDimitry Andric       } else {
40270b57cec5SDimitry Andric         isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
40280b57cec5SDimitry Andric       }
40290b57cec5SDimitry Andric       break;
40300b57cec5SDimitry Andric     case tok::kw_explicit: {
40310b57cec5SDimitry Andric       SourceLocation ExplicitLoc = Loc;
40320b57cec5SDimitry Andric       SourceLocation CloseParenLoc;
40330b57cec5SDimitry Andric       ExplicitSpecifier ExplicitSpec(nullptr, ExplicitSpecKind::ResolvedTrue);
40340b57cec5SDimitry Andric       ConsumedEnd = ExplicitLoc;
40350b57cec5SDimitry Andric       ConsumeToken(); // kw_explicit
40360b57cec5SDimitry Andric       if (Tok.is(tok::l_paren)) {
40375ffd83dbSDimitry Andric         if (getLangOpts().CPlusPlus20 || isExplicitBool() == TPResult::True) {
40385ffd83dbSDimitry Andric           Diag(Tok.getLocation(), getLangOpts().CPlusPlus20
403955e4f9d5SDimitry Andric                                       ? diag::warn_cxx17_compat_explicit_bool
404055e4f9d5SDimitry Andric                                       : diag::ext_explicit_bool);
404155e4f9d5SDimitry Andric 
40420b57cec5SDimitry Andric           ExprResult ExplicitExpr(static_cast<Expr *>(nullptr));
40430b57cec5SDimitry Andric           BalancedDelimiterTracker Tracker(*this, tok::l_paren);
40440b57cec5SDimitry Andric           Tracker.consumeOpen();
40450b57cec5SDimitry Andric           ExplicitExpr = ParseConstantExpression();
40460b57cec5SDimitry Andric           ConsumedEnd = Tok.getLocation();
40470b57cec5SDimitry Andric           if (ExplicitExpr.isUsable()) {
40480b57cec5SDimitry Andric             CloseParenLoc = Tok.getLocation();
40490b57cec5SDimitry Andric             Tracker.consumeClose();
40500b57cec5SDimitry Andric             ExplicitSpec =
40510b57cec5SDimitry Andric                 Actions.ActOnExplicitBoolSpecifier(ExplicitExpr.get());
40520b57cec5SDimitry Andric           } else
40530b57cec5SDimitry Andric             Tracker.skipToEnd();
405455e4f9d5SDimitry Andric         } else {
40555ffd83dbSDimitry Andric           Diag(Tok.getLocation(), diag::warn_cxx20_compat_explicit_bool);
40560b57cec5SDimitry Andric         }
405755e4f9d5SDimitry Andric       }
40580b57cec5SDimitry Andric       isInvalid = DS.setFunctionSpecExplicit(ExplicitLoc, PrevSpec, DiagID,
40590b57cec5SDimitry Andric                                              ExplicitSpec, CloseParenLoc);
40600b57cec5SDimitry Andric       break;
40610b57cec5SDimitry Andric     }
40620b57cec5SDimitry Andric     case tok::kw__Noreturn:
40630b57cec5SDimitry Andric       if (!getLangOpts().C11)
4064a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
40650b57cec5SDimitry Andric       isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
40660b57cec5SDimitry Andric       break;
40670b57cec5SDimitry Andric 
40680b57cec5SDimitry Andric     // alignment-specifier
40690b57cec5SDimitry Andric     case tok::kw__Alignas:
40700b57cec5SDimitry Andric       if (!getLangOpts().C11)
4071a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
40720b57cec5SDimitry Andric       ParseAlignmentSpecifier(DS.getAttributes());
40730b57cec5SDimitry Andric       continue;
40740b57cec5SDimitry Andric 
40750b57cec5SDimitry Andric     // friend
40760b57cec5SDimitry Andric     case tok::kw_friend:
40770b57cec5SDimitry Andric       if (DSContext == DeclSpecContext::DSC_class)
40780b57cec5SDimitry Andric         isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
40790b57cec5SDimitry Andric       else {
40800b57cec5SDimitry Andric         PrevSpec = ""; // not actually used by the diagnostic
40810b57cec5SDimitry Andric         DiagID = diag::err_friend_invalid_in_context;
40820b57cec5SDimitry Andric         isInvalid = true;
40830b57cec5SDimitry Andric       }
40840b57cec5SDimitry Andric       break;
40850b57cec5SDimitry Andric 
40860b57cec5SDimitry Andric     // Modules
40870b57cec5SDimitry Andric     case tok::kw___module_private__:
40880b57cec5SDimitry Andric       isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
40890b57cec5SDimitry Andric       break;
40900b57cec5SDimitry Andric 
4091a7dea167SDimitry Andric     // constexpr, consteval, constinit specifiers
40920b57cec5SDimitry Andric     case tok::kw_constexpr:
4093e8d8bef9SDimitry Andric       isInvalid = DS.SetConstexprSpec(ConstexprSpecKind::Constexpr, Loc,
4094e8d8bef9SDimitry Andric                                       PrevSpec, DiagID);
40950b57cec5SDimitry Andric       break;
40960b57cec5SDimitry Andric     case tok::kw_consteval:
4097e8d8bef9SDimitry Andric       isInvalid = DS.SetConstexprSpec(ConstexprSpecKind::Consteval, Loc,
4098e8d8bef9SDimitry Andric                                       PrevSpec, DiagID);
40990b57cec5SDimitry Andric       break;
4100a7dea167SDimitry Andric     case tok::kw_constinit:
4101e8d8bef9SDimitry Andric       isInvalid = DS.SetConstexprSpec(ConstexprSpecKind::Constinit, Loc,
4102e8d8bef9SDimitry Andric                                       PrevSpec, DiagID);
4103a7dea167SDimitry Andric       break;
41040b57cec5SDimitry Andric 
41050b57cec5SDimitry Andric     // type-specifier
41060b57cec5SDimitry Andric     case tok::kw_short:
4107e8d8bef9SDimitry Andric       isInvalid = DS.SetTypeSpecWidth(TypeSpecifierWidth::Short, Loc, PrevSpec,
41080b57cec5SDimitry Andric                                       DiagID, Policy);
41090b57cec5SDimitry Andric       break;
41100b57cec5SDimitry Andric     case tok::kw_long:
4111e8d8bef9SDimitry Andric       if (DS.getTypeSpecWidth() != TypeSpecifierWidth::Long)
4112e8d8bef9SDimitry Andric         isInvalid = DS.SetTypeSpecWidth(TypeSpecifierWidth::Long, Loc, PrevSpec,
41130b57cec5SDimitry Andric                                         DiagID, Policy);
41140b57cec5SDimitry Andric       else
4115e8d8bef9SDimitry Andric         isInvalid = DS.SetTypeSpecWidth(TypeSpecifierWidth::LongLong, Loc,
4116e8d8bef9SDimitry Andric                                         PrevSpec, DiagID, Policy);
41170b57cec5SDimitry Andric       break;
41180b57cec5SDimitry Andric     case tok::kw___int64:
4119e8d8bef9SDimitry Andric       isInvalid = DS.SetTypeSpecWidth(TypeSpecifierWidth::LongLong, Loc,
4120e8d8bef9SDimitry Andric                                       PrevSpec, DiagID, Policy);
41210b57cec5SDimitry Andric       break;
41220b57cec5SDimitry Andric     case tok::kw_signed:
4123e8d8bef9SDimitry Andric       isInvalid =
4124e8d8bef9SDimitry Andric           DS.SetTypeSpecSign(TypeSpecifierSign::Signed, Loc, PrevSpec, DiagID);
41250b57cec5SDimitry Andric       break;
41260b57cec5SDimitry Andric     case tok::kw_unsigned:
4127e8d8bef9SDimitry Andric       isInvalid = DS.SetTypeSpecSign(TypeSpecifierSign::Unsigned, Loc, PrevSpec,
41280b57cec5SDimitry Andric                                      DiagID);
41290b57cec5SDimitry Andric       break;
41300b57cec5SDimitry Andric     case tok::kw__Complex:
4131a7dea167SDimitry Andric       if (!getLangOpts().C99)
4132a7dea167SDimitry Andric         Diag(Tok, diag::ext_c99_feature) << Tok.getName();
41330b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
41340b57cec5SDimitry Andric                                         DiagID);
41350b57cec5SDimitry Andric       break;
41360b57cec5SDimitry Andric     case tok::kw__Imaginary:
4137a7dea167SDimitry Andric       if (!getLangOpts().C99)
4138a7dea167SDimitry Andric         Diag(Tok, diag::ext_c99_feature) << Tok.getName();
41390b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
41400b57cec5SDimitry Andric                                         DiagID);
41410b57cec5SDimitry Andric       break;
41420b57cec5SDimitry Andric     case tok::kw_void:
41430b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
41440b57cec5SDimitry Andric                                      DiagID, Policy);
41450b57cec5SDimitry Andric       break;
41460b57cec5SDimitry Andric     case tok::kw_char:
41470b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
41480b57cec5SDimitry Andric                                      DiagID, Policy);
41490b57cec5SDimitry Andric       break;
41500b57cec5SDimitry Andric     case tok::kw_int:
41510b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
41520b57cec5SDimitry Andric                                      DiagID, Policy);
41530b57cec5SDimitry Andric       break;
41540eae32dcSDimitry Andric     case tok::kw__ExtInt:
41550eae32dcSDimitry Andric     case tok::kw__BitInt: {
41560eae32dcSDimitry Andric       DiagnoseBitIntUse(Tok);
41575ffd83dbSDimitry Andric       ExprResult ER = ParseExtIntegerArgument();
41585ffd83dbSDimitry Andric       if (ER.isInvalid())
41595ffd83dbSDimitry Andric         continue;
41600eae32dcSDimitry Andric       isInvalid = DS.SetBitIntType(Loc, ER.get(), PrevSpec, DiagID, Policy);
41615ffd83dbSDimitry Andric       ConsumedEnd = PrevTokLocation;
41625ffd83dbSDimitry Andric       break;
41635ffd83dbSDimitry Andric     }
41640b57cec5SDimitry Andric     case tok::kw___int128:
41650b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
41660b57cec5SDimitry Andric                                      DiagID, Policy);
41670b57cec5SDimitry Andric       break;
41680b57cec5SDimitry Andric     case tok::kw_half:
41690b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
41700b57cec5SDimitry Andric                                      DiagID, Policy);
41710b57cec5SDimitry Andric       break;
41725ffd83dbSDimitry Andric     case tok::kw___bf16:
41735ffd83dbSDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_BFloat16, Loc, PrevSpec,
41745ffd83dbSDimitry Andric                                      DiagID, Policy);
41755ffd83dbSDimitry Andric       break;
41760b57cec5SDimitry Andric     case tok::kw_float:
41770b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
41780b57cec5SDimitry Andric                                      DiagID, Policy);
41790b57cec5SDimitry Andric       break;
41800b57cec5SDimitry Andric     case tok::kw_double:
41810b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
41820b57cec5SDimitry Andric                                      DiagID, Policy);
41830b57cec5SDimitry Andric       break;
41840b57cec5SDimitry Andric     case tok::kw__Float16:
41850b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float16, Loc, PrevSpec,
41860b57cec5SDimitry Andric                                      DiagID, Policy);
41870b57cec5SDimitry Andric       break;
41880b57cec5SDimitry Andric     case tok::kw__Accum:
41890b57cec5SDimitry Andric       if (!getLangOpts().FixedPoint) {
41900b57cec5SDimitry Andric         SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
41910b57cec5SDimitry Andric       } else {
41920b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_accum, Loc, PrevSpec,
41930b57cec5SDimitry Andric                                        DiagID, Policy);
41940b57cec5SDimitry Andric       }
41950b57cec5SDimitry Andric       break;
41960b57cec5SDimitry Andric     case tok::kw__Fract:
41970b57cec5SDimitry Andric       if (!getLangOpts().FixedPoint) {
41980b57cec5SDimitry Andric         SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
41990b57cec5SDimitry Andric       } else {
42000b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_fract, Loc, PrevSpec,
42010b57cec5SDimitry Andric                                        DiagID, Policy);
42020b57cec5SDimitry Andric       }
42030b57cec5SDimitry Andric       break;
42040b57cec5SDimitry Andric     case tok::kw__Sat:
42050b57cec5SDimitry Andric       if (!getLangOpts().FixedPoint) {
42060b57cec5SDimitry Andric         SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
42070b57cec5SDimitry Andric       } else {
42080b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecSat(Loc, PrevSpec, DiagID);
42090b57cec5SDimitry Andric       }
42100b57cec5SDimitry Andric       break;
42110b57cec5SDimitry Andric     case tok::kw___float128:
42120b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float128, Loc, PrevSpec,
42130b57cec5SDimitry Andric                                      DiagID, Policy);
42140b57cec5SDimitry Andric       break;
4215349cc55cSDimitry Andric     case tok::kw___ibm128:
4216349cc55cSDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_ibm128, Loc, PrevSpec,
4217349cc55cSDimitry Andric                                      DiagID, Policy);
4218349cc55cSDimitry Andric       break;
42190b57cec5SDimitry Andric     case tok::kw_wchar_t:
42200b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
42210b57cec5SDimitry Andric                                      DiagID, Policy);
42220b57cec5SDimitry Andric       break;
42230b57cec5SDimitry Andric     case tok::kw_char8_t:
42240b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char8, Loc, PrevSpec,
42250b57cec5SDimitry Andric                                      DiagID, Policy);
42260b57cec5SDimitry Andric       break;
42270b57cec5SDimitry Andric     case tok::kw_char16_t:
42280b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
42290b57cec5SDimitry Andric                                      DiagID, Policy);
42300b57cec5SDimitry Andric       break;
42310b57cec5SDimitry Andric     case tok::kw_char32_t:
42320b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
42330b57cec5SDimitry Andric                                      DiagID, Policy);
42340b57cec5SDimitry Andric       break;
42350b57cec5SDimitry Andric     case tok::kw_bool:
4236*06c3fb27SDimitry Andric       if (getLangOpts().C2x)
4237*06c3fb27SDimitry Andric         Diag(Tok, diag::warn_c2x_compat_keyword) << Tok.getName();
4238*06c3fb27SDimitry Andric       [[fallthrough]];
42390b57cec5SDimitry Andric     case tok::kw__Bool:
4240a7dea167SDimitry Andric       if (Tok.is(tok::kw__Bool) && !getLangOpts().C99)
4241a7dea167SDimitry Andric         Diag(Tok, diag::ext_c99_feature) << Tok.getName();
4242a7dea167SDimitry Andric 
42430b57cec5SDimitry Andric       if (Tok.is(tok::kw_bool) &&
42440b57cec5SDimitry Andric           DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
42450b57cec5SDimitry Andric           DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
42460b57cec5SDimitry Andric         PrevSpec = ""; // Not used by the diagnostic.
42470b57cec5SDimitry Andric         DiagID = diag::err_bool_redeclaration;
42480b57cec5SDimitry Andric         // For better error recovery.
42490b57cec5SDimitry Andric         Tok.setKind(tok::identifier);
42500b57cec5SDimitry Andric         isInvalid = true;
42510b57cec5SDimitry Andric       } else {
42520b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
42530b57cec5SDimitry Andric                                        DiagID, Policy);
42540b57cec5SDimitry Andric       }
42550b57cec5SDimitry Andric       break;
42560b57cec5SDimitry Andric     case tok::kw__Decimal32:
42570b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
42580b57cec5SDimitry Andric                                      DiagID, Policy);
42590b57cec5SDimitry Andric       break;
42600b57cec5SDimitry Andric     case tok::kw__Decimal64:
42610b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
42620b57cec5SDimitry Andric                                      DiagID, Policy);
42630b57cec5SDimitry Andric       break;
42640b57cec5SDimitry Andric     case tok::kw__Decimal128:
42650b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
42660b57cec5SDimitry Andric                                      DiagID, Policy);
42670b57cec5SDimitry Andric       break;
42680b57cec5SDimitry Andric     case tok::kw___vector:
42690b57cec5SDimitry Andric       isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
42700b57cec5SDimitry Andric       break;
42710b57cec5SDimitry Andric     case tok::kw___pixel:
42720b57cec5SDimitry Andric       isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
42730b57cec5SDimitry Andric       break;
42740b57cec5SDimitry Andric     case tok::kw___bool:
42750b57cec5SDimitry Andric       isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
42760b57cec5SDimitry Andric       break;
42770b57cec5SDimitry Andric     case tok::kw_pipe:
4278349cc55cSDimitry Andric       if (!getLangOpts().OpenCL ||
4279349cc55cSDimitry Andric           getLangOpts().getOpenCLCompatibleVersion() < 200) {
4280fe6060f1SDimitry Andric         // OpenCL 2.0 and later define this keyword. OpenCL 1.2 and earlier
4281fe6060f1SDimitry Andric         // should support the "pipe" word as identifier.
42820b57cec5SDimitry Andric         Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
4283fe6060f1SDimitry Andric         Tok.setKind(tok::identifier);
42840b57cec5SDimitry Andric         goto DoneWithDeclSpec;
42856e75b2fbSDimitry Andric       } else if (!getLangOpts().OpenCLPipes) {
42866e75b2fbSDimitry Andric         DiagID = diag::err_opencl_unknown_type_specifier;
42876e75b2fbSDimitry Andric         PrevSpec = Tok.getIdentifierInfo()->getNameStart();
42886e75b2fbSDimitry Andric         isInvalid = true;
42896e75b2fbSDimitry Andric       } else
42900b57cec5SDimitry Andric         isInvalid = DS.SetTypePipe(true, Loc, PrevSpec, DiagID, Policy);
42910b57cec5SDimitry Andric       break;
4292fe6060f1SDimitry Andric // We only need to enumerate each image type once.
4293fe6060f1SDimitry Andric #define IMAGE_READ_WRITE_TYPE(Type, Id, Ext)
4294fe6060f1SDimitry Andric #define IMAGE_WRITE_TYPE(Type, Id, Ext)
4295fe6060f1SDimitry Andric #define IMAGE_READ_TYPE(ImgType, Id, Ext) \
42960b57cec5SDimitry Andric     case tok::kw_##ImgType##_t: \
4297fe6060f1SDimitry Andric       if (!handleOpenCLImageKW(Ext, DeclSpec::TST_##ImgType##_t)) \
4298fe6060f1SDimitry Andric         goto DoneWithDeclSpec; \
42990b57cec5SDimitry Andric       break;
43000b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def"
43010b57cec5SDimitry Andric     case tok::kw___unknown_anytype:
43020b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
43030b57cec5SDimitry Andric                                      PrevSpec, DiagID, Policy);
43040b57cec5SDimitry Andric       break;
43050b57cec5SDimitry Andric 
43060b57cec5SDimitry Andric     // class-specifier:
43070b57cec5SDimitry Andric     case tok::kw_class:
43080b57cec5SDimitry Andric     case tok::kw_struct:
43090b57cec5SDimitry Andric     case tok::kw___interface:
43100b57cec5SDimitry Andric     case tok::kw_union: {
43110b57cec5SDimitry Andric       tok::TokenKind Kind = Tok.getKind();
43120b57cec5SDimitry Andric       ConsumeToken();
43130b57cec5SDimitry Andric 
43140b57cec5SDimitry Andric       // These are attributes following class specifiers.
43150b57cec5SDimitry Andric       // To produce better diagnostic, we parse them when
43160b57cec5SDimitry Andric       // parsing class specifier.
431781ad6265SDimitry Andric       ParsedAttributes Attributes(AttrFactory);
43180b57cec5SDimitry Andric       ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
43190b57cec5SDimitry Andric                           EnteringContext, DSContext, Attributes);
43200b57cec5SDimitry Andric 
43210b57cec5SDimitry Andric       // If there are attributes following class specifier,
43220b57cec5SDimitry Andric       // take them over and handle them here.
43230b57cec5SDimitry Andric       if (!Attributes.empty()) {
43240b57cec5SDimitry Andric         AttrsLastTime = true;
43250b57cec5SDimitry Andric         attrs.takeAllFrom(Attributes);
43260b57cec5SDimitry Andric       }
43270b57cec5SDimitry Andric       continue;
43280b57cec5SDimitry Andric     }
43290b57cec5SDimitry Andric 
43300b57cec5SDimitry Andric     // enum-specifier:
43310b57cec5SDimitry Andric     case tok::kw_enum:
43320b57cec5SDimitry Andric       ConsumeToken();
43330b57cec5SDimitry Andric       ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
43340b57cec5SDimitry Andric       continue;
43350b57cec5SDimitry Andric 
43360b57cec5SDimitry Andric     // cv-qualifier:
43370b57cec5SDimitry Andric     case tok::kw_const:
43380b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
43390b57cec5SDimitry Andric                                  getLangOpts());
43400b57cec5SDimitry Andric       break;
43410b57cec5SDimitry Andric     case tok::kw_volatile:
43420b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
43430b57cec5SDimitry Andric                                  getLangOpts());
43440b57cec5SDimitry Andric       break;
43450b57cec5SDimitry Andric     case tok::kw_restrict:
43460b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
43470b57cec5SDimitry Andric                                  getLangOpts());
43480b57cec5SDimitry Andric       break;
43490b57cec5SDimitry Andric 
43500b57cec5SDimitry Andric     // C++ typename-specifier:
43510b57cec5SDimitry Andric     case tok::kw_typename:
43520b57cec5SDimitry Andric       if (TryAnnotateTypeOrScopeToken()) {
43530b57cec5SDimitry Andric         DS.SetTypeSpecError();
43540b57cec5SDimitry Andric         goto DoneWithDeclSpec;
43550b57cec5SDimitry Andric       }
43560b57cec5SDimitry Andric       if (!Tok.is(tok::kw_typename))
43570b57cec5SDimitry Andric         continue;
43580b57cec5SDimitry Andric       break;
43590b57cec5SDimitry Andric 
4360bdd1243dSDimitry Andric     // C2x/GNU typeof support.
43610b57cec5SDimitry Andric     case tok::kw_typeof:
4362bdd1243dSDimitry Andric     case tok::kw_typeof_unqual:
43630b57cec5SDimitry Andric       ParseTypeofSpecifier(DS);
43640b57cec5SDimitry Andric       continue;
43650b57cec5SDimitry Andric 
43660b57cec5SDimitry Andric     case tok::annot_decltype:
43670b57cec5SDimitry Andric       ParseDecltypeSpecifier(DS);
43680b57cec5SDimitry Andric       continue;
43690b57cec5SDimitry Andric 
43700b57cec5SDimitry Andric     case tok::annot_pragma_pack:
43710b57cec5SDimitry Andric       HandlePragmaPack();
43720b57cec5SDimitry Andric       continue;
43730b57cec5SDimitry Andric 
43740b57cec5SDimitry Andric     case tok::annot_pragma_ms_pragma:
43750b57cec5SDimitry Andric       HandlePragmaMSPragma();
43760b57cec5SDimitry Andric       continue;
43770b57cec5SDimitry Andric 
43780b57cec5SDimitry Andric     case tok::annot_pragma_ms_vtordisp:
43790b57cec5SDimitry Andric       HandlePragmaMSVtorDisp();
43800b57cec5SDimitry Andric       continue;
43810b57cec5SDimitry Andric 
43820b57cec5SDimitry Andric     case tok::annot_pragma_ms_pointers_to_members:
43830b57cec5SDimitry Andric       HandlePragmaMSPointersToMembers();
43840b57cec5SDimitry Andric       continue;
43850b57cec5SDimitry Andric 
4386bdd1243dSDimitry Andric #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait:
4387bdd1243dSDimitry Andric #include "clang/Basic/TransformTypeTraits.def"
4388bdd1243dSDimitry Andric       // HACK: libstdc++ already uses '__remove_cv' as an alias template so we
4389bdd1243dSDimitry Andric       // work around this by expecting all transform type traits to be suffixed
4390bdd1243dSDimitry Andric       // with '('. They're an identifier otherwise.
4391bdd1243dSDimitry Andric       if (!MaybeParseTypeTransformTypeSpecifier(DS))
4392bdd1243dSDimitry Andric         goto ParseIdentifier;
43930b57cec5SDimitry Andric       continue;
43940b57cec5SDimitry Andric 
43950b57cec5SDimitry Andric     case tok::kw__Atomic:
43960b57cec5SDimitry Andric       // C11 6.7.2.4/4:
43970b57cec5SDimitry Andric       //   If the _Atomic keyword is immediately followed by a left parenthesis,
43980b57cec5SDimitry Andric       //   it is interpreted as a type specifier (with a type name), not as a
43990b57cec5SDimitry Andric       //   type qualifier.
4400a7dea167SDimitry Andric       if (!getLangOpts().C11)
4401a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
4402a7dea167SDimitry Andric 
44030b57cec5SDimitry Andric       if (NextToken().is(tok::l_paren)) {
44040b57cec5SDimitry Andric         ParseAtomicSpecifier(DS);
44050b57cec5SDimitry Andric         continue;
44060b57cec5SDimitry Andric       }
44070b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
44080b57cec5SDimitry Andric                                  getLangOpts());
44090b57cec5SDimitry Andric       break;
44100b57cec5SDimitry Andric 
44110b57cec5SDimitry Andric     // OpenCL address space qualifiers:
44120b57cec5SDimitry Andric     case tok::kw___generic:
44130b57cec5SDimitry Andric       // generic address space is introduced only in OpenCL v2.0
44140b57cec5SDimitry Andric       // see OpenCL C Spec v2.0 s6.5.5
4415fe6060f1SDimitry Andric       // OpenCL v3.0 introduces __opencl_c_generic_address_space
4416fe6060f1SDimitry Andric       // feature macro to indicate if generic address space is supported
4417fe6060f1SDimitry Andric       if (!Actions.getLangOpts().OpenCLGenericAddressSpace) {
44180b57cec5SDimitry Andric         DiagID = diag::err_opencl_unknown_type_specifier;
44190b57cec5SDimitry Andric         PrevSpec = Tok.getIdentifierInfo()->getNameStart();
44200b57cec5SDimitry Andric         isInvalid = true;
44210b57cec5SDimitry Andric         break;
4422480093f4SDimitry Andric       }
4423bdd1243dSDimitry Andric       [[fallthrough]];
44240b57cec5SDimitry Andric     case tok::kw_private:
4425480093f4SDimitry Andric       // It's fine (but redundant) to check this for __generic on the
4426480093f4SDimitry Andric       // fallthrough path; we only form the __generic token in OpenCL mode.
4427480093f4SDimitry Andric       if (!getLangOpts().OpenCL)
4428480093f4SDimitry Andric         goto DoneWithDeclSpec;
4429bdd1243dSDimitry Andric       [[fallthrough]];
44300b57cec5SDimitry Andric     case tok::kw___private:
44310b57cec5SDimitry Andric     case tok::kw___global:
44320b57cec5SDimitry Andric     case tok::kw___local:
44330b57cec5SDimitry Andric     case tok::kw___constant:
44340b57cec5SDimitry Andric     // OpenCL access qualifiers:
44350b57cec5SDimitry Andric     case tok::kw___read_only:
44360b57cec5SDimitry Andric     case tok::kw___write_only:
44370b57cec5SDimitry Andric     case tok::kw___read_write:
44380b57cec5SDimitry Andric       ParseOpenCLQualifiers(DS.getAttributes());
44390b57cec5SDimitry Andric       break;
44400b57cec5SDimitry Andric 
4441bdd1243dSDimitry Andric     case tok::kw_groupshared:
4442bdd1243dSDimitry Andric       // NOTE: ParseHLSLQualifiers will consume the qualifier token.
4443bdd1243dSDimitry Andric       ParseHLSLQualifiers(DS.getAttributes());
4444bdd1243dSDimitry Andric       continue;
4445bdd1243dSDimitry Andric 
44460b57cec5SDimitry Andric     case tok::less:
44470b57cec5SDimitry Andric       // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
44480b57cec5SDimitry Andric       // "id<SomeProtocol>".  This is hopelessly old fashioned and dangerous,
44490b57cec5SDimitry Andric       // but we support it.
44500b57cec5SDimitry Andric       if (DS.hasTypeSpecifier() || !getLangOpts().ObjC)
44510b57cec5SDimitry Andric         goto DoneWithDeclSpec;
44520b57cec5SDimitry Andric 
44530b57cec5SDimitry Andric       SourceLocation StartLoc = Tok.getLocation();
44540b57cec5SDimitry Andric       SourceLocation EndLoc;
44550b57cec5SDimitry Andric       TypeResult Type = parseObjCProtocolQualifierType(EndLoc);
44560b57cec5SDimitry Andric       if (Type.isUsable()) {
44570b57cec5SDimitry Andric         if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, StartLoc,
44580b57cec5SDimitry Andric                                PrevSpec, DiagID, Type.get(),
44590b57cec5SDimitry Andric                                Actions.getASTContext().getPrintingPolicy()))
44600b57cec5SDimitry Andric           Diag(StartLoc, DiagID) << PrevSpec;
44610b57cec5SDimitry Andric 
44620b57cec5SDimitry Andric         DS.SetRangeEnd(EndLoc);
44630b57cec5SDimitry Andric       } else {
44640b57cec5SDimitry Andric         DS.SetTypeSpecError();
44650b57cec5SDimitry Andric       }
44660b57cec5SDimitry Andric 
44670b57cec5SDimitry Andric       // Need to support trailing type qualifiers (e.g. "id<p> const").
44680b57cec5SDimitry Andric       // If a type specifier follows, it will be diagnosed elsewhere.
44690b57cec5SDimitry Andric       continue;
44700b57cec5SDimitry Andric     }
44710b57cec5SDimitry Andric 
44720b57cec5SDimitry Andric     DS.SetRangeEnd(ConsumedEnd.isValid() ? ConsumedEnd : Tok.getLocation());
44730b57cec5SDimitry Andric 
44740b57cec5SDimitry Andric     // If the specifier wasn't legal, issue a diagnostic.
44750b57cec5SDimitry Andric     if (isInvalid) {
44760b57cec5SDimitry Andric       assert(PrevSpec && "Method did not return previous specifier!");
44770b57cec5SDimitry Andric       assert(DiagID);
44780b57cec5SDimitry Andric 
44790b57cec5SDimitry Andric       if (DiagID == diag::ext_duplicate_declspec ||
44800b57cec5SDimitry Andric           DiagID == diag::ext_warn_duplicate_declspec ||
44810b57cec5SDimitry Andric           DiagID == diag::err_duplicate_declspec)
44820b57cec5SDimitry Andric         Diag(Loc, DiagID) << PrevSpec
44830b57cec5SDimitry Andric                           << FixItHint::CreateRemoval(
44840b57cec5SDimitry Andric                                  SourceRange(Loc, DS.getEndLoc()));
44850b57cec5SDimitry Andric       else if (DiagID == diag::err_opencl_unknown_type_specifier) {
4486349cc55cSDimitry Andric         Diag(Loc, DiagID) << getLangOpts().getOpenCLVersionString() << PrevSpec
4487349cc55cSDimitry Andric                           << isStorageClass;
44880b57cec5SDimitry Andric       } else
44890b57cec5SDimitry Andric         Diag(Loc, DiagID) << PrevSpec;
44900b57cec5SDimitry Andric     }
44910b57cec5SDimitry Andric 
44920b57cec5SDimitry Andric     if (DiagID != diag::err_bool_redeclaration && ConsumedEnd.isInvalid())
44930b57cec5SDimitry Andric       // After an error the next token can be an annotation token.
44940b57cec5SDimitry Andric       ConsumeAnyToken();
44950b57cec5SDimitry Andric 
44960b57cec5SDimitry Andric     AttrsLastTime = false;
44970b57cec5SDimitry Andric   }
44980b57cec5SDimitry Andric }
44990b57cec5SDimitry Andric 
45000b57cec5SDimitry Andric /// ParseStructDeclaration - Parse a struct declaration without the terminating
45010b57cec5SDimitry Andric /// semicolon.
45020b57cec5SDimitry Andric ///
45030b57cec5SDimitry Andric /// Note that a struct declaration refers to a declaration in a struct,
45040b57cec5SDimitry Andric /// not to the declaration of a struct.
45050b57cec5SDimitry Andric ///
45060b57cec5SDimitry Andric ///       struct-declaration:
45070b57cec5SDimitry Andric /// [C2x]   attributes-specifier-seq[opt]
45080b57cec5SDimitry Andric ///           specifier-qualifier-list struct-declarator-list
45090b57cec5SDimitry Andric /// [GNU]   __extension__ struct-declaration
45100b57cec5SDimitry Andric /// [GNU]   specifier-qualifier-list
45110b57cec5SDimitry Andric ///       struct-declarator-list:
45120b57cec5SDimitry Andric ///         struct-declarator
45130b57cec5SDimitry Andric ///         struct-declarator-list ',' struct-declarator
45140b57cec5SDimitry Andric /// [GNU]   struct-declarator-list ',' attributes[opt] struct-declarator
45150b57cec5SDimitry Andric ///       struct-declarator:
45160b57cec5SDimitry Andric ///         declarator
45170b57cec5SDimitry Andric /// [GNU]   declarator attributes[opt]
45180b57cec5SDimitry Andric ///         declarator[opt] ':' constant-expression
45190b57cec5SDimitry Andric /// [GNU]   declarator[opt] ':' constant-expression attributes[opt]
45200b57cec5SDimitry Andric ///
45210b57cec5SDimitry Andric void Parser::ParseStructDeclaration(
45220b57cec5SDimitry Andric     ParsingDeclSpec &DS,
45230b57cec5SDimitry Andric     llvm::function_ref<void(ParsingFieldDeclarator &)> FieldsCallback) {
45240b57cec5SDimitry Andric 
45250b57cec5SDimitry Andric   if (Tok.is(tok::kw___extension__)) {
45260b57cec5SDimitry Andric     // __extension__ silences extension warnings in the subexpression.
45270b57cec5SDimitry Andric     ExtensionRAIIObject O(Diags);  // Use RAII to do this.
45280b57cec5SDimitry Andric     ConsumeToken();
45290b57cec5SDimitry Andric     return ParseStructDeclaration(DS, FieldsCallback);
45300b57cec5SDimitry Andric   }
45310b57cec5SDimitry Andric 
45320b57cec5SDimitry Andric   // Parse leading attributes.
453381ad6265SDimitry Andric   ParsedAttributes Attrs(AttrFactory);
45340b57cec5SDimitry Andric   MaybeParseCXX11Attributes(Attrs);
45350b57cec5SDimitry Andric 
45360b57cec5SDimitry Andric   // Parse the common specifier-qualifiers-list piece.
45370b57cec5SDimitry Andric   ParseSpecifierQualifierList(DS);
45380b57cec5SDimitry Andric 
45390b57cec5SDimitry Andric   // If there are no declarators, this is a free-standing declaration
45400b57cec5SDimitry Andric   // specifier. Let the actions module cope with it.
45410b57cec5SDimitry Andric   if (Tok.is(tok::semi)) {
454281ad6265SDimitry Andric     // C2x 6.7.2.1p9 : "The optional attribute specifier sequence in a
454381ad6265SDimitry Andric     // member declaration appertains to each of the members declared by the
454481ad6265SDimitry Andric     // member declarator list; it shall not appear if the optional member
454581ad6265SDimitry Andric     // declarator list is omitted."
454681ad6265SDimitry Andric     ProhibitAttributes(Attrs);
45470b57cec5SDimitry Andric     RecordDecl *AnonRecord = nullptr;
454881ad6265SDimitry Andric     Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(
454981ad6265SDimitry Andric         getCurScope(), AS_none, DS, ParsedAttributesView::none(), AnonRecord);
45500b57cec5SDimitry Andric     assert(!AnonRecord && "Did not expect anonymous struct or union here");
45510b57cec5SDimitry Andric     DS.complete(TheDecl);
45520b57cec5SDimitry Andric     return;
45530b57cec5SDimitry Andric   }
45540b57cec5SDimitry Andric 
45550b57cec5SDimitry Andric   // Read struct-declarators until we find the semicolon.
45560b57cec5SDimitry Andric   bool FirstDeclarator = true;
45570b57cec5SDimitry Andric   SourceLocation CommaLoc;
455804eeddc0SDimitry Andric   while (true) {
455981ad6265SDimitry Andric     ParsingFieldDeclarator DeclaratorInfo(*this, DS, Attrs);
45600b57cec5SDimitry Andric     DeclaratorInfo.D.setCommaLoc(CommaLoc);
45610b57cec5SDimitry Andric 
45620b57cec5SDimitry Andric     // Attributes are only allowed here on successive declarators.
4563e8d8bef9SDimitry Andric     if (!FirstDeclarator) {
4564e8d8bef9SDimitry Andric       // However, this does not apply for [[]] attributes (which could show up
4565e8d8bef9SDimitry Andric       // before or after the __attribute__ attributes).
4566e8d8bef9SDimitry Andric       DiagnoseAndSkipCXX11Attributes();
45670b57cec5SDimitry Andric       MaybeParseGNUAttributes(DeclaratorInfo.D);
4568e8d8bef9SDimitry Andric       DiagnoseAndSkipCXX11Attributes();
4569e8d8bef9SDimitry Andric     }
45700b57cec5SDimitry Andric 
45710b57cec5SDimitry Andric     /// struct-declarator: declarator
45720b57cec5SDimitry Andric     /// struct-declarator: declarator[opt] ':' constant-expression
45730b57cec5SDimitry Andric     if (Tok.isNot(tok::colon)) {
45740b57cec5SDimitry Andric       // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
45750b57cec5SDimitry Andric       ColonProtectionRAIIObject X(*this);
45760b57cec5SDimitry Andric       ParseDeclarator(DeclaratorInfo.D);
45770b57cec5SDimitry Andric     } else
45780b57cec5SDimitry Andric       DeclaratorInfo.D.SetIdentifier(nullptr, Tok.getLocation());
45790b57cec5SDimitry Andric 
45800b57cec5SDimitry Andric     if (TryConsumeToken(tok::colon)) {
45810b57cec5SDimitry Andric       ExprResult Res(ParseConstantExpression());
45820b57cec5SDimitry Andric       if (Res.isInvalid())
45830b57cec5SDimitry Andric         SkipUntil(tok::semi, StopBeforeMatch);
45840b57cec5SDimitry Andric       else
45850b57cec5SDimitry Andric         DeclaratorInfo.BitfieldSize = Res.get();
45860b57cec5SDimitry Andric     }
45870b57cec5SDimitry Andric 
45880b57cec5SDimitry Andric     // If attributes exist after the declarator, parse them.
45890b57cec5SDimitry Andric     MaybeParseGNUAttributes(DeclaratorInfo.D);
45900b57cec5SDimitry Andric 
45910b57cec5SDimitry Andric     // We're done with this declarator;  invoke the callback.
45920b57cec5SDimitry Andric     FieldsCallback(DeclaratorInfo);
45930b57cec5SDimitry Andric 
45940b57cec5SDimitry Andric     // If we don't have a comma, it is either the end of the list (a ';')
45950b57cec5SDimitry Andric     // or an error, bail out.
45960b57cec5SDimitry Andric     if (!TryConsumeToken(tok::comma, CommaLoc))
45970b57cec5SDimitry Andric       return;
45980b57cec5SDimitry Andric 
45990b57cec5SDimitry Andric     FirstDeclarator = false;
46000b57cec5SDimitry Andric   }
46010b57cec5SDimitry Andric }
46020b57cec5SDimitry Andric 
46030b57cec5SDimitry Andric /// ParseStructUnionBody
46040b57cec5SDimitry Andric ///       struct-contents:
46050b57cec5SDimitry Andric ///         struct-declaration-list
46060b57cec5SDimitry Andric /// [EXT]   empty
4607e8d8bef9SDimitry Andric /// [GNU]   "struct-declaration-list" without terminating ';'
46080b57cec5SDimitry Andric ///       struct-declaration-list:
46090b57cec5SDimitry Andric ///         struct-declaration
46100b57cec5SDimitry Andric ///         struct-declaration-list struct-declaration
46110b57cec5SDimitry Andric /// [OBC]   '@' 'defs' '(' class-name ')'
46120b57cec5SDimitry Andric ///
46130b57cec5SDimitry Andric void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
46145ffd83dbSDimitry Andric                                   DeclSpec::TST TagType, RecordDecl *TagDecl) {
46150b57cec5SDimitry Andric   PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc,
46160b57cec5SDimitry Andric                                       "parsing struct/union body");
46170b57cec5SDimitry Andric   assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
46180b57cec5SDimitry Andric 
46190b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_brace);
46200b57cec5SDimitry Andric   if (T.consumeOpen())
46210b57cec5SDimitry Andric     return;
46220b57cec5SDimitry Andric 
46230b57cec5SDimitry Andric   ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
46240b57cec5SDimitry Andric   Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
46250b57cec5SDimitry Andric 
46260b57cec5SDimitry Andric   // While we still have something to read, read the declarations in the struct.
46270b57cec5SDimitry Andric   while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
46280b57cec5SDimitry Andric          Tok.isNot(tok::eof)) {
46290b57cec5SDimitry Andric     // Each iteration of this loop reads one struct-declaration.
46300b57cec5SDimitry Andric 
46310b57cec5SDimitry Andric     // Check for extraneous top-level semicolon.
46320b57cec5SDimitry Andric     if (Tok.is(tok::semi)) {
46330b57cec5SDimitry Andric       ConsumeExtraSemi(InsideStruct, TagType);
46340b57cec5SDimitry Andric       continue;
46350b57cec5SDimitry Andric     }
46360b57cec5SDimitry Andric 
46370b57cec5SDimitry Andric     // Parse _Static_assert declaration.
4638d409305fSDimitry Andric     if (Tok.isOneOf(tok::kw__Static_assert, tok::kw_static_assert)) {
46390b57cec5SDimitry Andric       SourceLocation DeclEnd;
46400b57cec5SDimitry Andric       ParseStaticAssertDeclaration(DeclEnd);
46410b57cec5SDimitry Andric       continue;
46420b57cec5SDimitry Andric     }
46430b57cec5SDimitry Andric 
46440b57cec5SDimitry Andric     if (Tok.is(tok::annot_pragma_pack)) {
46450b57cec5SDimitry Andric       HandlePragmaPack();
46460b57cec5SDimitry Andric       continue;
46470b57cec5SDimitry Andric     }
46480b57cec5SDimitry Andric 
46490b57cec5SDimitry Andric     if (Tok.is(tok::annot_pragma_align)) {
46500b57cec5SDimitry Andric       HandlePragmaAlign();
46510b57cec5SDimitry Andric       continue;
46520b57cec5SDimitry Andric     }
46530b57cec5SDimitry Andric 
4654fe6060f1SDimitry Andric     if (Tok.isOneOf(tok::annot_pragma_openmp, tok::annot_attr_openmp)) {
46550b57cec5SDimitry Andric       // Result can be ignored, because it must be always empty.
46560b57cec5SDimitry Andric       AccessSpecifier AS = AS_none;
465781ad6265SDimitry Andric       ParsedAttributes Attrs(AttrFactory);
46580b57cec5SDimitry Andric       (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs);
46590b57cec5SDimitry Andric       continue;
46600b57cec5SDimitry Andric     }
46610b57cec5SDimitry Andric 
4662a7dea167SDimitry Andric     if (tok::isPragmaAnnotation(Tok.getKind())) {
4663a7dea167SDimitry Andric       Diag(Tok.getLocation(), diag::err_pragma_misplaced_in_decl)
4664a7dea167SDimitry Andric           << DeclSpec::getSpecifierName(
4665a7dea167SDimitry Andric                  TagType, Actions.getASTContext().getPrintingPolicy());
4666a7dea167SDimitry Andric       ConsumeAnnotationToken();
4667a7dea167SDimitry Andric       continue;
4668a7dea167SDimitry Andric     }
4669a7dea167SDimitry Andric 
46700b57cec5SDimitry Andric     if (!Tok.is(tok::at)) {
46710b57cec5SDimitry Andric       auto CFieldCallback = [&](ParsingFieldDeclarator &FD) {
46720b57cec5SDimitry Andric         // Install the declarator into the current TagDecl.
46730b57cec5SDimitry Andric         Decl *Field =
46740b57cec5SDimitry Andric             Actions.ActOnField(getCurScope(), TagDecl,
46750b57cec5SDimitry Andric                                FD.D.getDeclSpec().getSourceRange().getBegin(),
46760b57cec5SDimitry Andric                                FD.D, FD.BitfieldSize);
46770b57cec5SDimitry Andric         FD.complete(Field);
46780b57cec5SDimitry Andric       };
46790b57cec5SDimitry Andric 
46800b57cec5SDimitry Andric       // Parse all the comma separated declarators.
46810b57cec5SDimitry Andric       ParsingDeclSpec DS(*this);
46820b57cec5SDimitry Andric       ParseStructDeclaration(DS, CFieldCallback);
46830b57cec5SDimitry Andric     } else { // Handle @defs
46840b57cec5SDimitry Andric       ConsumeToken();
46850b57cec5SDimitry Andric       if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
46860b57cec5SDimitry Andric         Diag(Tok, diag::err_unexpected_at);
46870b57cec5SDimitry Andric         SkipUntil(tok::semi);
46880b57cec5SDimitry Andric         continue;
46890b57cec5SDimitry Andric       }
46900b57cec5SDimitry Andric       ConsumeToken();
46910b57cec5SDimitry Andric       ExpectAndConsume(tok::l_paren);
46920b57cec5SDimitry Andric       if (!Tok.is(tok::identifier)) {
46930b57cec5SDimitry Andric         Diag(Tok, diag::err_expected) << tok::identifier;
46940b57cec5SDimitry Andric         SkipUntil(tok::semi);
46950b57cec5SDimitry Andric         continue;
46960b57cec5SDimitry Andric       }
46970b57cec5SDimitry Andric       SmallVector<Decl *, 16> Fields;
46980b57cec5SDimitry Andric       Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
46990b57cec5SDimitry Andric                         Tok.getIdentifierInfo(), Fields);
47000b57cec5SDimitry Andric       ConsumeToken();
47010b57cec5SDimitry Andric       ExpectAndConsume(tok::r_paren);
47020b57cec5SDimitry Andric     }
47030b57cec5SDimitry Andric 
47040b57cec5SDimitry Andric     if (TryConsumeToken(tok::semi))
47050b57cec5SDimitry Andric       continue;
47060b57cec5SDimitry Andric 
47070b57cec5SDimitry Andric     if (Tok.is(tok::r_brace)) {
47080b57cec5SDimitry Andric       ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
47090b57cec5SDimitry Andric       break;
47100b57cec5SDimitry Andric     }
47110b57cec5SDimitry Andric 
47120b57cec5SDimitry Andric     ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
47130b57cec5SDimitry Andric     // Skip to end of block or statement to avoid ext-warning on extra ';'.
47140b57cec5SDimitry Andric     SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
47150b57cec5SDimitry Andric     // If we stopped at a ';', eat it.
47160b57cec5SDimitry Andric     TryConsumeToken(tok::semi);
47170b57cec5SDimitry Andric   }
47180b57cec5SDimitry Andric 
47190b57cec5SDimitry Andric   T.consumeClose();
47200b57cec5SDimitry Andric 
47210b57cec5SDimitry Andric   ParsedAttributes attrs(AttrFactory);
47220b57cec5SDimitry Andric   // If attributes exist after struct contents, parse them.
47230b57cec5SDimitry Andric   MaybeParseGNUAttributes(attrs);
47240b57cec5SDimitry Andric 
472581ad6265SDimitry Andric   SmallVector<Decl *, 32> FieldDecls(TagDecl->fields());
47265ffd83dbSDimitry Andric 
47270b57cec5SDimitry Andric   Actions.ActOnFields(getCurScope(), RecordLoc, TagDecl, FieldDecls,
47280b57cec5SDimitry Andric                       T.getOpenLocation(), T.getCloseLocation(), attrs);
47290b57cec5SDimitry Andric   StructScope.Exit();
47300b57cec5SDimitry Andric   Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange());
47310b57cec5SDimitry Andric }
47320b57cec5SDimitry Andric 
47330b57cec5SDimitry Andric /// ParseEnumSpecifier
47340b57cec5SDimitry Andric ///       enum-specifier: [C99 6.7.2.2]
47350b57cec5SDimitry Andric ///         'enum' identifier[opt] '{' enumerator-list '}'
47360b57cec5SDimitry Andric ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
47370b57cec5SDimitry Andric /// [GNU]   'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
47380b57cec5SDimitry Andric ///                                                 '}' attributes[opt]
47390b57cec5SDimitry Andric /// [MS]    'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
47400b57cec5SDimitry Andric ///                                                 '}'
47410b57cec5SDimitry Andric ///         'enum' identifier
47420b57cec5SDimitry Andric /// [GNU]   'enum' attributes[opt] identifier
47430b57cec5SDimitry Andric ///
47440b57cec5SDimitry Andric /// [C++11] enum-head '{' enumerator-list[opt] '}'
47450b57cec5SDimitry Andric /// [C++11] enum-head '{' enumerator-list ','  '}'
47460b57cec5SDimitry Andric ///
47470b57cec5SDimitry Andric ///       enum-head: [C++11]
47480b57cec5SDimitry Andric ///         enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
47490b57cec5SDimitry Andric ///         enum-key attribute-specifier-seq[opt] nested-name-specifier
47500b57cec5SDimitry Andric ///             identifier enum-base[opt]
47510b57cec5SDimitry Andric ///
47520b57cec5SDimitry Andric ///       enum-key: [C++11]
47530b57cec5SDimitry Andric ///         'enum'
47540b57cec5SDimitry Andric ///         'enum' 'class'
47550b57cec5SDimitry Andric ///         'enum' 'struct'
47560b57cec5SDimitry Andric ///
47570b57cec5SDimitry Andric ///       enum-base: [C++11]
47580b57cec5SDimitry Andric ///         ':' type-specifier-seq
47590b57cec5SDimitry Andric ///
47600b57cec5SDimitry Andric /// [C++] elaborated-type-specifier:
47615ffd83dbSDimitry Andric /// [C++]   'enum' nested-name-specifier[opt] identifier
47620b57cec5SDimitry Andric ///
47630b57cec5SDimitry Andric void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
47640b57cec5SDimitry Andric                                 const ParsedTemplateInfo &TemplateInfo,
47650b57cec5SDimitry Andric                                 AccessSpecifier AS, DeclSpecContext DSC) {
47660b57cec5SDimitry Andric   // Parse the tag portion of this.
47670b57cec5SDimitry Andric   if (Tok.is(tok::code_completion)) {
47680b57cec5SDimitry Andric     // Code completion for an enum name.
4769fe6060f1SDimitry Andric     cutOffParsing();
47700b57cec5SDimitry Andric     Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
4771bdd1243dSDimitry Andric     DS.SetTypeSpecError(); // Needed by ActOnUsingDeclaration.
4772fe6060f1SDimitry Andric     return;
47730b57cec5SDimitry Andric   }
47740b57cec5SDimitry Andric 
47750b57cec5SDimitry Andric   // If attributes exist after tag, parse them.
477681ad6265SDimitry Andric   ParsedAttributes attrs(AttrFactory);
4777fe6060f1SDimitry Andric   MaybeParseAttributes(PAKM_GNU | PAKM_Declspec | PAKM_CXX11, attrs);
47780b57cec5SDimitry Andric 
47790b57cec5SDimitry Andric   SourceLocation ScopedEnumKWLoc;
47800b57cec5SDimitry Andric   bool IsScopedUsingClassTag = false;
47810b57cec5SDimitry Andric 
47820b57cec5SDimitry Andric   // In C++11, recognize 'enum class' and 'enum struct'.
478381ad6265SDimitry Andric   if (Tok.isOneOf(tok::kw_class, tok::kw_struct) && getLangOpts().CPlusPlus) {
47840b57cec5SDimitry Andric     Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
47850b57cec5SDimitry Andric                                         : diag::ext_scoped_enum);
47860b57cec5SDimitry Andric     IsScopedUsingClassTag = Tok.is(tok::kw_class);
47870b57cec5SDimitry Andric     ScopedEnumKWLoc = ConsumeToken();
47880b57cec5SDimitry Andric 
47890b57cec5SDimitry Andric     // Attributes are not allowed between these keywords.  Diagnose,
47900b57cec5SDimitry Andric     // but then just treat them like they appeared in the right place.
47910b57cec5SDimitry Andric     ProhibitAttributes(attrs);
47920b57cec5SDimitry Andric 
47930b57cec5SDimitry Andric     // They are allowed afterwards, though.
4794fe6060f1SDimitry Andric     MaybeParseAttributes(PAKM_GNU | PAKM_Declspec | PAKM_CXX11, attrs);
47950b57cec5SDimitry Andric   }
47960b57cec5SDimitry Andric 
47970b57cec5SDimitry Andric   // C++11 [temp.explicit]p12:
47980b57cec5SDimitry Andric   //   The usual access controls do not apply to names used to specify
47990b57cec5SDimitry Andric   //   explicit instantiations.
48000b57cec5SDimitry Andric   // We extend this to also cover explicit specializations.  Note that
48010b57cec5SDimitry Andric   // we don't suppress if this turns out to be an elaborated type
48020b57cec5SDimitry Andric   // specifier.
48030b57cec5SDimitry Andric   bool shouldDelayDiagsInTag =
48040b57cec5SDimitry Andric     (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
48050b57cec5SDimitry Andric      TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
48060b57cec5SDimitry Andric   SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
48070b57cec5SDimitry Andric 
48085ffd83dbSDimitry Andric   // Determine whether this declaration is permitted to have an enum-base.
48095ffd83dbSDimitry Andric   AllowDefiningTypeSpec AllowEnumSpecifier =
481081ad6265SDimitry Andric       isDefiningTypeSpecifierContext(DSC, getLangOpts().CPlusPlus);
48115ffd83dbSDimitry Andric   bool CanBeOpaqueEnumDeclaration =
48125ffd83dbSDimitry Andric       DS.isEmpty() && isOpaqueEnumDeclarationContext(DSC);
48135ffd83dbSDimitry Andric   bool CanHaveEnumBase = (getLangOpts().CPlusPlus11 || getLangOpts().ObjC ||
48145ffd83dbSDimitry Andric                           getLangOpts().MicrosoftExt) &&
48155ffd83dbSDimitry Andric                          (AllowEnumSpecifier == AllowDefiningTypeSpec::Yes ||
48165ffd83dbSDimitry Andric                           CanBeOpaqueEnumDeclaration);
48170b57cec5SDimitry Andric 
48180b57cec5SDimitry Andric   CXXScopeSpec &SS = DS.getTypeSpecScope();
48190b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus) {
48205ffd83dbSDimitry Andric     // "enum foo : bar;" is not a potential typo for "enum foo::bar;".
48215ffd83dbSDimitry Andric     ColonProtectionRAIIObject X(*this);
48220b57cec5SDimitry Andric 
48230b57cec5SDimitry Andric     CXXScopeSpec Spec;
48245ffd83dbSDimitry Andric     if (ParseOptionalCXXScopeSpecifier(Spec, /*ObjectType=*/nullptr,
482504eeddc0SDimitry Andric                                        /*ObjectHasErrors=*/false,
48260b57cec5SDimitry Andric                                        /*EnteringContext=*/true))
48270b57cec5SDimitry Andric       return;
48280b57cec5SDimitry Andric 
48290b57cec5SDimitry Andric     if (Spec.isSet() && Tok.isNot(tok::identifier)) {
48300b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::identifier;
4831bdd1243dSDimitry Andric       DS.SetTypeSpecError();
48320b57cec5SDimitry Andric       if (Tok.isNot(tok::l_brace)) {
48330b57cec5SDimitry Andric         // Has no name and is not a definition.
48340b57cec5SDimitry Andric         // Skip the rest of this declarator, up until the comma or semicolon.
48350b57cec5SDimitry Andric         SkipUntil(tok::comma, StopAtSemi);
48360b57cec5SDimitry Andric         return;
48370b57cec5SDimitry Andric       }
48380b57cec5SDimitry Andric     }
48390b57cec5SDimitry Andric 
48400b57cec5SDimitry Andric     SS = Spec;
48410b57cec5SDimitry Andric   }
48420b57cec5SDimitry Andric 
48435ffd83dbSDimitry Andric   // Must have either 'enum name' or 'enum {...}' or (rarely) 'enum : T { ... }'.
48440b57cec5SDimitry Andric   if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
48455ffd83dbSDimitry Andric       Tok.isNot(tok::colon)) {
48460b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
48470b57cec5SDimitry Andric 
4848bdd1243dSDimitry Andric     DS.SetTypeSpecError();
48490b57cec5SDimitry Andric     // Skip the rest of this declarator, up until the comma or semicolon.
48500b57cec5SDimitry Andric     SkipUntil(tok::comma, StopAtSemi);
48510b57cec5SDimitry Andric     return;
48520b57cec5SDimitry Andric   }
48530b57cec5SDimitry Andric 
48540b57cec5SDimitry Andric   // If an identifier is present, consume and remember it.
48550b57cec5SDimitry Andric   IdentifierInfo *Name = nullptr;
48560b57cec5SDimitry Andric   SourceLocation NameLoc;
48570b57cec5SDimitry Andric   if (Tok.is(tok::identifier)) {
48580b57cec5SDimitry Andric     Name = Tok.getIdentifierInfo();
48590b57cec5SDimitry Andric     NameLoc = ConsumeToken();
48600b57cec5SDimitry Andric   }
48610b57cec5SDimitry Andric 
48620b57cec5SDimitry Andric   if (!Name && ScopedEnumKWLoc.isValid()) {
48630b57cec5SDimitry Andric     // C++0x 7.2p2: The optional identifier shall not be omitted in the
48640b57cec5SDimitry Andric     // declaration of a scoped enumeration.
48650b57cec5SDimitry Andric     Diag(Tok, diag::err_scoped_enum_missing_identifier);
48660b57cec5SDimitry Andric     ScopedEnumKWLoc = SourceLocation();
48670b57cec5SDimitry Andric     IsScopedUsingClassTag = false;
48680b57cec5SDimitry Andric   }
48690b57cec5SDimitry Andric 
48700b57cec5SDimitry Andric   // Okay, end the suppression area.  We'll decide whether to emit the
48710b57cec5SDimitry Andric   // diagnostics in a second.
48720b57cec5SDimitry Andric   if (shouldDelayDiagsInTag)
48730b57cec5SDimitry Andric     diagsFromTag.done();
48740b57cec5SDimitry Andric 
48750b57cec5SDimitry Andric   TypeResult BaseType;
48765ffd83dbSDimitry Andric   SourceRange BaseRange;
48775ffd83dbSDimitry Andric 
487881ad6265SDimitry Andric   bool CanBeBitfield =
487981ad6265SDimitry Andric       getCurScope()->isClassScope() && ScopedEnumKWLoc.isInvalid() && Name;
48800b57cec5SDimitry Andric 
48810b57cec5SDimitry Andric   // Parse the fixed underlying type.
48825ffd83dbSDimitry Andric   if (Tok.is(tok::colon)) {
48835ffd83dbSDimitry Andric     // This might be an enum-base or part of some unrelated enclosing context.
48845ffd83dbSDimitry Andric     //
48855ffd83dbSDimitry Andric     // 'enum E : base' is permitted in two circumstances:
48865ffd83dbSDimitry Andric     //
48875ffd83dbSDimitry Andric     // 1) As a defining-type-specifier, when followed by '{'.
48885ffd83dbSDimitry Andric     // 2) As the sole constituent of a complete declaration -- when DS is empty
48895ffd83dbSDimitry Andric     //    and the next token is ';'.
48905ffd83dbSDimitry Andric     //
48915ffd83dbSDimitry Andric     // The restriction to defining-type-specifiers is important to allow parsing
48925ffd83dbSDimitry Andric     //   a ? new enum E : int{}
48935ffd83dbSDimitry Andric     //   _Generic(a, enum E : int{})
48945ffd83dbSDimitry Andric     // properly.
48955ffd83dbSDimitry Andric     //
48965ffd83dbSDimitry Andric     // One additional consideration applies:
48975ffd83dbSDimitry Andric     //
48985ffd83dbSDimitry Andric     // C++ [dcl.enum]p1:
48995ffd83dbSDimitry Andric     //   A ':' following "enum nested-name-specifier[opt] identifier" within
49005ffd83dbSDimitry Andric     //   the decl-specifier-seq of a member-declaration is parsed as part of
49015ffd83dbSDimitry Andric     //   an enum-base.
49025ffd83dbSDimitry Andric     //
49035ffd83dbSDimitry Andric     // Other language modes supporting enumerations with fixed underlying types
49045ffd83dbSDimitry Andric     // do not have clear rules on this, so we disambiguate to determine whether
49055ffd83dbSDimitry Andric     // the tokens form a bit-field width or an enum-base.
49060b57cec5SDimitry Andric 
49075ffd83dbSDimitry Andric     if (CanBeBitfield && !isEnumBase(CanBeOpaqueEnumDeclaration)) {
49085ffd83dbSDimitry Andric       // Outside C++11, do not interpret the tokens as an enum-base if they do
49095ffd83dbSDimitry Andric       // not make sense as one. In C++11, it's an error if this happens.
49105ffd83dbSDimitry Andric       if (getLangOpts().CPlusPlus11)
49115ffd83dbSDimitry Andric         Diag(Tok.getLocation(), diag::err_anonymous_enum_bitfield);
49125ffd83dbSDimitry Andric     } else if (CanHaveEnumBase || !ColonIsSacred) {
49135ffd83dbSDimitry Andric       SourceLocation ColonLoc = ConsumeToken();
49140b57cec5SDimitry Andric 
49155ffd83dbSDimitry Andric       // Parse a type-specifier-seq as a type. We can't just ParseTypeName here,
49165ffd83dbSDimitry Andric       // because under -fms-extensions,
49175ffd83dbSDimitry Andric       //   enum E : int *p;
49185ffd83dbSDimitry Andric       // declares 'enum E : int; E *p;' not 'enum E : int*; E p;'.
49195ffd83dbSDimitry Andric       DeclSpec DS(AttrFactory);
4920bdd1243dSDimitry Andric       // enum-base is not assumed to be a type and therefore requires the
4921bdd1243dSDimitry Andric       // typename keyword [p0634r3].
4922bdd1243dSDimitry Andric       ParseSpecifierQualifierList(DS, ImplicitTypenameContext::No, AS,
4923bdd1243dSDimitry Andric                                   DeclSpecContext::DSC_type_specifier);
492481ad6265SDimitry Andric       Declarator DeclaratorInfo(DS, ParsedAttributesView::none(),
492581ad6265SDimitry Andric                                 DeclaratorContext::TypeName);
49265ffd83dbSDimitry Andric       BaseType = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
49270b57cec5SDimitry Andric 
49285ffd83dbSDimitry Andric       BaseRange = SourceRange(ColonLoc, DeclaratorInfo.getSourceRange().getEnd());
49290b57cec5SDimitry Andric 
49300b57cec5SDimitry Andric       if (!getLangOpts().ObjC) {
49310b57cec5SDimitry Andric         if (getLangOpts().CPlusPlus11)
49325ffd83dbSDimitry Andric           Diag(ColonLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type)
49335ffd83dbSDimitry Andric               << BaseRange;
49340b57cec5SDimitry Andric         else if (getLangOpts().CPlusPlus)
49355ffd83dbSDimitry Andric           Diag(ColonLoc, diag::ext_cxx11_enum_fixed_underlying_type)
49365ffd83dbSDimitry Andric               << BaseRange;
49370b57cec5SDimitry Andric         else if (getLangOpts().MicrosoftExt)
49385ffd83dbSDimitry Andric           Diag(ColonLoc, diag::ext_ms_c_enum_fixed_underlying_type)
49395ffd83dbSDimitry Andric               << BaseRange;
49400b57cec5SDimitry Andric         else
49415ffd83dbSDimitry Andric           Diag(ColonLoc, diag::ext_clang_c_enum_fixed_underlying_type)
49425ffd83dbSDimitry Andric               << BaseRange;
49430b57cec5SDimitry Andric       }
49440b57cec5SDimitry Andric     }
49450b57cec5SDimitry Andric   }
49460b57cec5SDimitry Andric 
49470b57cec5SDimitry Andric   // There are four options here.  If we have 'friend enum foo;' then this is a
49480b57cec5SDimitry Andric   // friend declaration, and cannot have an accompanying definition. If we have
49490b57cec5SDimitry Andric   // 'enum foo;', then this is a forward declaration.  If we have
49500b57cec5SDimitry Andric   // 'enum foo {...' then this is a definition. Otherwise we have something
49510b57cec5SDimitry Andric   // like 'enum foo xyz', a reference.
49520b57cec5SDimitry Andric   //
49530b57cec5SDimitry Andric   // This is needed to handle stuff like this right (C99 6.7.2.3p11):
49540b57cec5SDimitry Andric   // enum foo {..};  void bar() { enum foo; }    <- new foo in bar.
49550b57cec5SDimitry Andric   // enum foo {..};  void bar() { enum foo x; }  <- use of old foo.
49560b57cec5SDimitry Andric   //
49570b57cec5SDimitry Andric   Sema::TagUseKind TUK;
49585ffd83dbSDimitry Andric   if (AllowEnumSpecifier == AllowDefiningTypeSpec::No)
49590b57cec5SDimitry Andric     TUK = Sema::TUK_Reference;
49605ffd83dbSDimitry Andric   else if (Tok.is(tok::l_brace)) {
49610b57cec5SDimitry Andric     if (DS.isFriendSpecified()) {
49620b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
49630b57cec5SDimitry Andric         << SourceRange(DS.getFriendSpecLoc());
49640b57cec5SDimitry Andric       ConsumeBrace();
49650b57cec5SDimitry Andric       SkipUntil(tok::r_brace, StopAtSemi);
49665ffd83dbSDimitry Andric       // Discard any other definition-only pieces.
49675ffd83dbSDimitry Andric       attrs.clear();
49685ffd83dbSDimitry Andric       ScopedEnumKWLoc = SourceLocation();
49695ffd83dbSDimitry Andric       IsScopedUsingClassTag = false;
49705ffd83dbSDimitry Andric       BaseType = TypeResult();
49710b57cec5SDimitry Andric       TUK = Sema::TUK_Friend;
49720b57cec5SDimitry Andric     } else {
49730b57cec5SDimitry Andric       TUK = Sema::TUK_Definition;
49740b57cec5SDimitry Andric     }
49750b57cec5SDimitry Andric   } else if (!isTypeSpecifier(DSC) &&
49760b57cec5SDimitry Andric              (Tok.is(tok::semi) ||
49770b57cec5SDimitry Andric               (Tok.isAtStartOfLine() &&
49780b57cec5SDimitry Andric                !isValidAfterTypeSpecifier(CanBeBitfield)))) {
49795ffd83dbSDimitry Andric     // An opaque-enum-declaration is required to be standalone (no preceding or
49805ffd83dbSDimitry Andric     // following tokens in the declaration). Sema enforces this separately by
49815ffd83dbSDimitry Andric     // diagnosing anything else in the DeclSpec.
49820b57cec5SDimitry Andric     TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
49830b57cec5SDimitry Andric     if (Tok.isNot(tok::semi)) {
49840b57cec5SDimitry Andric       // A semicolon was missing after this declaration. Diagnose and recover.
49850b57cec5SDimitry Andric       ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
49860b57cec5SDimitry Andric       PP.EnterToken(Tok, /*IsReinject=*/true);
49870b57cec5SDimitry Andric       Tok.setKind(tok::semi);
49880b57cec5SDimitry Andric     }
49890b57cec5SDimitry Andric   } else {
49900b57cec5SDimitry Andric     TUK = Sema::TUK_Reference;
49910b57cec5SDimitry Andric   }
49920b57cec5SDimitry Andric 
49935ffd83dbSDimitry Andric   bool IsElaboratedTypeSpecifier =
49945ffd83dbSDimitry Andric       TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend;
49955ffd83dbSDimitry Andric 
49965ffd83dbSDimitry Andric   // If this is an elaborated type specifier nested in a larger declaration,
49975ffd83dbSDimitry Andric   // and we delayed diagnostics before, just merge them into the current pool.
49980b57cec5SDimitry Andric   if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
49990b57cec5SDimitry Andric     diagsFromTag.redelay();
50000b57cec5SDimitry Andric   }
50010b57cec5SDimitry Andric 
50020b57cec5SDimitry Andric   MultiTemplateParamsArg TParams;
50030b57cec5SDimitry Andric   if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
50040b57cec5SDimitry Andric       TUK != Sema::TUK_Reference) {
50050b57cec5SDimitry Andric     if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
50060b57cec5SDimitry Andric       // Skip the rest of this declarator, up until the comma or semicolon.
50070b57cec5SDimitry Andric       Diag(Tok, diag::err_enum_template);
50080b57cec5SDimitry Andric       SkipUntil(tok::comma, StopAtSemi);
50090b57cec5SDimitry Andric       return;
50100b57cec5SDimitry Andric     }
50110b57cec5SDimitry Andric 
50120b57cec5SDimitry Andric     if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
50130b57cec5SDimitry Andric       // Enumerations can't be explicitly instantiated.
50140b57cec5SDimitry Andric       DS.SetTypeSpecError();
50150b57cec5SDimitry Andric       Diag(StartLoc, diag::err_explicit_instantiation_enum);
50160b57cec5SDimitry Andric       return;
50170b57cec5SDimitry Andric     }
50180b57cec5SDimitry Andric 
50190b57cec5SDimitry Andric     assert(TemplateInfo.TemplateParams && "no template parameters");
50200b57cec5SDimitry Andric     TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
50210b57cec5SDimitry Andric                                      TemplateInfo.TemplateParams->size());
5022*06c3fb27SDimitry Andric     SS.setTemplateParamLists(TParams);
50230b57cec5SDimitry Andric   }
50240b57cec5SDimitry Andric 
50250b57cec5SDimitry Andric   if (!Name && TUK != Sema::TUK_Definition) {
50260b57cec5SDimitry Andric     Diag(Tok, diag::err_enumerator_unnamed_no_def);
50270b57cec5SDimitry Andric 
5028bdd1243dSDimitry Andric     DS.SetTypeSpecError();
50290b57cec5SDimitry Andric     // Skip the rest of this declarator, up until the comma or semicolon.
50300b57cec5SDimitry Andric     SkipUntil(tok::comma, StopAtSemi);
50310b57cec5SDimitry Andric     return;
50320b57cec5SDimitry Andric   }
50330b57cec5SDimitry Andric 
50345ffd83dbSDimitry Andric   // An elaborated-type-specifier has a much more constrained grammar:
50355ffd83dbSDimitry Andric   //
50365ffd83dbSDimitry Andric   //   'enum' nested-name-specifier[opt] identifier
50375ffd83dbSDimitry Andric   //
50385ffd83dbSDimitry Andric   // If we parsed any other bits, reject them now.
50395ffd83dbSDimitry Andric   //
50405ffd83dbSDimitry Andric   // MSVC and (for now at least) Objective-C permit a full enum-specifier
50415ffd83dbSDimitry Andric   // or opaque-enum-declaration anywhere.
50425ffd83dbSDimitry Andric   if (IsElaboratedTypeSpecifier && !getLangOpts().MicrosoftExt &&
50435ffd83dbSDimitry Andric       !getLangOpts().ObjC) {
5044fe6060f1SDimitry Andric     ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed,
5045*06c3fb27SDimitry Andric                             diag::err_keyword_not_allowed,
5046fe6060f1SDimitry Andric                             /*DiagnoseEmptyAttrs=*/true);
50475ffd83dbSDimitry Andric     if (BaseType.isUsable())
50485ffd83dbSDimitry Andric       Diag(BaseRange.getBegin(), diag::ext_enum_base_in_type_specifier)
50495ffd83dbSDimitry Andric           << (AllowEnumSpecifier == AllowDefiningTypeSpec::Yes) << BaseRange;
50505ffd83dbSDimitry Andric     else if (ScopedEnumKWLoc.isValid())
50515ffd83dbSDimitry Andric       Diag(ScopedEnumKWLoc, diag::ext_elaborated_enum_class)
50525ffd83dbSDimitry Andric         << FixItHint::CreateRemoval(ScopedEnumKWLoc) << IsScopedUsingClassTag;
50535ffd83dbSDimitry Andric   }
50545ffd83dbSDimitry Andric 
50550b57cec5SDimitry Andric   stripTypeAttributesOffDeclSpec(attrs, DS, TUK);
50560b57cec5SDimitry Andric 
50570b57cec5SDimitry Andric   Sema::SkipBodyInfo SkipBody;
50580b57cec5SDimitry Andric   if (!Name && TUK == Sema::TUK_Definition && Tok.is(tok::l_brace) &&
50590b57cec5SDimitry Andric       NextToken().is(tok::identifier))
50600b57cec5SDimitry Andric     SkipBody = Actions.shouldSkipAnonEnumBody(getCurScope(),
50610b57cec5SDimitry Andric                                               NextToken().getIdentifierInfo(),
50620b57cec5SDimitry Andric                                               NextToken().getLocation());
50630b57cec5SDimitry Andric 
50640b57cec5SDimitry Andric   bool Owned = false;
50650b57cec5SDimitry Andric   bool IsDependent = false;
50660b57cec5SDimitry Andric   const char *PrevSpec = nullptr;
50670b57cec5SDimitry Andric   unsigned DiagID;
5068bdd1243dSDimitry Andric   Decl *TagDecl =
5069bdd1243dSDimitry Andric       Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, StartLoc, SS,
5070bdd1243dSDimitry Andric                     Name, NameLoc, attrs, AS, DS.getModulePrivateSpecLoc(),
5071bdd1243dSDimitry Andric                     TParams, Owned, IsDependent, ScopedEnumKWLoc,
5072bdd1243dSDimitry Andric                     IsScopedUsingClassTag,
5073bdd1243dSDimitry Andric                     BaseType, DSC == DeclSpecContext::DSC_type_specifier,
50740b57cec5SDimitry Andric                     DSC == DeclSpecContext::DSC_template_param ||
50750b57cec5SDimitry Andric                         DSC == DeclSpecContext::DSC_template_type_arg,
50761ac55f4cSDimitry Andric                     OffsetOfState, &SkipBody).get();
50770b57cec5SDimitry Andric 
50780b57cec5SDimitry Andric   if (SkipBody.ShouldSkip) {
50790b57cec5SDimitry Andric     assert(TUK == Sema::TUK_Definition && "can only skip a definition");
50800b57cec5SDimitry Andric 
50810b57cec5SDimitry Andric     BalancedDelimiterTracker T(*this, tok::l_brace);
50820b57cec5SDimitry Andric     T.consumeOpen();
50830b57cec5SDimitry Andric     T.skipToEnd();
50840b57cec5SDimitry Andric 
50850b57cec5SDimitry Andric     if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
50861ac55f4cSDimitry Andric                            NameLoc.isValid() ? NameLoc : StartLoc,
50871ac55f4cSDimitry Andric                            PrevSpec, DiagID, TagDecl, Owned,
50880b57cec5SDimitry Andric                            Actions.getASTContext().getPrintingPolicy()))
50890b57cec5SDimitry Andric       Diag(StartLoc, DiagID) << PrevSpec;
50900b57cec5SDimitry Andric     return;
50910b57cec5SDimitry Andric   }
50920b57cec5SDimitry Andric 
50930b57cec5SDimitry Andric   if (IsDependent) {
50940b57cec5SDimitry Andric     // This enum has a dependent nested-name-specifier. Handle it as a
50950b57cec5SDimitry Andric     // dependent tag.
50960b57cec5SDimitry Andric     if (!Name) {
50970b57cec5SDimitry Andric       DS.SetTypeSpecError();
50980b57cec5SDimitry Andric       Diag(Tok, diag::err_expected_type_name_after_typename);
50990b57cec5SDimitry Andric       return;
51000b57cec5SDimitry Andric     }
51010b57cec5SDimitry Andric 
51020b57cec5SDimitry Andric     TypeResult Type = Actions.ActOnDependentTag(
51030b57cec5SDimitry Andric         getCurScope(), DeclSpec::TST_enum, TUK, SS, Name, StartLoc, NameLoc);
51040b57cec5SDimitry Andric     if (Type.isInvalid()) {
51050b57cec5SDimitry Andric       DS.SetTypeSpecError();
51060b57cec5SDimitry Andric       return;
51070b57cec5SDimitry Andric     }
51080b57cec5SDimitry Andric 
51090b57cec5SDimitry Andric     if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
51100b57cec5SDimitry Andric                            NameLoc.isValid() ? NameLoc : StartLoc,
51110b57cec5SDimitry Andric                            PrevSpec, DiagID, Type.get(),
51120b57cec5SDimitry Andric                            Actions.getASTContext().getPrintingPolicy()))
51130b57cec5SDimitry Andric       Diag(StartLoc, DiagID) << PrevSpec;
51140b57cec5SDimitry Andric 
51150b57cec5SDimitry Andric     return;
51160b57cec5SDimitry Andric   }
51170b57cec5SDimitry Andric 
51180b57cec5SDimitry Andric   if (!TagDecl) {
51190b57cec5SDimitry Andric     // The action failed to produce an enumeration tag. If this is a
51200b57cec5SDimitry Andric     // definition, consume the entire definition.
51210b57cec5SDimitry Andric     if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
51220b57cec5SDimitry Andric       ConsumeBrace();
51230b57cec5SDimitry Andric       SkipUntil(tok::r_brace, StopAtSemi);
51240b57cec5SDimitry Andric     }
51250b57cec5SDimitry Andric 
51260b57cec5SDimitry Andric     DS.SetTypeSpecError();
51270b57cec5SDimitry Andric     return;
51280b57cec5SDimitry Andric   }
51290b57cec5SDimitry Andric 
51305ffd83dbSDimitry Andric   if (Tok.is(tok::l_brace) && TUK == Sema::TUK_Definition) {
51310b57cec5SDimitry Andric     Decl *D = SkipBody.CheckSameAsPrevious ? SkipBody.New : TagDecl;
51320b57cec5SDimitry Andric     ParseEnumBody(StartLoc, D);
51330b57cec5SDimitry Andric     if (SkipBody.CheckSameAsPrevious &&
513481ad6265SDimitry Andric         !Actions.ActOnDuplicateDefinition(TagDecl, SkipBody)) {
51350b57cec5SDimitry Andric       DS.SetTypeSpecError();
51360b57cec5SDimitry Andric       return;
51370b57cec5SDimitry Andric     }
51380b57cec5SDimitry Andric   }
51390b57cec5SDimitry Andric 
51400b57cec5SDimitry Andric   if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
51411ac55f4cSDimitry Andric                          NameLoc.isValid() ? NameLoc : StartLoc,
51421ac55f4cSDimitry Andric                          PrevSpec, DiagID, TagDecl, Owned,
51430b57cec5SDimitry Andric                          Actions.getASTContext().getPrintingPolicy()))
51440b57cec5SDimitry Andric     Diag(StartLoc, DiagID) << PrevSpec;
51450b57cec5SDimitry Andric }
51460b57cec5SDimitry Andric 
51470b57cec5SDimitry Andric /// ParseEnumBody - Parse a {} enclosed enumerator-list.
51480b57cec5SDimitry Andric ///       enumerator-list:
51490b57cec5SDimitry Andric ///         enumerator
51500b57cec5SDimitry Andric ///         enumerator-list ',' enumerator
51510b57cec5SDimitry Andric ///       enumerator:
51520b57cec5SDimitry Andric ///         enumeration-constant attributes[opt]
51530b57cec5SDimitry Andric ///         enumeration-constant attributes[opt] '=' constant-expression
51540b57cec5SDimitry Andric ///       enumeration-constant:
51550b57cec5SDimitry Andric ///         identifier
51560b57cec5SDimitry Andric ///
51570b57cec5SDimitry Andric void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
51580b57cec5SDimitry Andric   // Enter the scope of the enum body and start the definition.
51590b57cec5SDimitry Andric   ParseScope EnumScope(this, Scope::DeclScope | Scope::EnumScope);
51600b57cec5SDimitry Andric   Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
51610b57cec5SDimitry Andric 
51620b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_brace);
51630b57cec5SDimitry Andric   T.consumeOpen();
51640b57cec5SDimitry Andric 
51650b57cec5SDimitry Andric   // C does not allow an empty enumerator-list, C++ does [dcl.enum].
51660b57cec5SDimitry Andric   if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
51670b57cec5SDimitry Andric     Diag(Tok, diag::err_empty_enum);
51680b57cec5SDimitry Andric 
51690b57cec5SDimitry Andric   SmallVector<Decl *, 32> EnumConstantDecls;
51700b57cec5SDimitry Andric   SmallVector<SuppressAccessChecks, 32> EnumAvailabilityDiags;
51710b57cec5SDimitry Andric 
51720b57cec5SDimitry Andric   Decl *LastEnumConstDecl = nullptr;
51730b57cec5SDimitry Andric 
51740b57cec5SDimitry Andric   // Parse the enumerator-list.
51750b57cec5SDimitry Andric   while (Tok.isNot(tok::r_brace)) {
51760b57cec5SDimitry Andric     // Parse enumerator. If failed, try skipping till the start of the next
51770b57cec5SDimitry Andric     // enumerator definition.
51780b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
51790b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
51800b57cec5SDimitry Andric       if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) &&
51810b57cec5SDimitry Andric           TryConsumeToken(tok::comma))
51820b57cec5SDimitry Andric         continue;
51830b57cec5SDimitry Andric       break;
51840b57cec5SDimitry Andric     }
51850b57cec5SDimitry Andric     IdentifierInfo *Ident = Tok.getIdentifierInfo();
51860b57cec5SDimitry Andric     SourceLocation IdentLoc = ConsumeToken();
51870b57cec5SDimitry Andric 
51880b57cec5SDimitry Andric     // If attributes exist after the enumerator, parse them.
518981ad6265SDimitry Andric     ParsedAttributes attrs(AttrFactory);
51900b57cec5SDimitry Andric     MaybeParseGNUAttributes(attrs);
5191*06c3fb27SDimitry Andric     if (isAllowedCXX11AttributeSpecifier()) {
51920b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus)
51930b57cec5SDimitry Andric         Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
51940b57cec5SDimitry Andric                                     ? diag::warn_cxx14_compat_ns_enum_attribute
51950b57cec5SDimitry Andric                                     : diag::ext_ns_enum_attribute)
51960b57cec5SDimitry Andric             << 1 /*enumerator*/;
51970b57cec5SDimitry Andric       ParseCXX11Attributes(attrs);
51980b57cec5SDimitry Andric     }
51990b57cec5SDimitry Andric 
52000b57cec5SDimitry Andric     SourceLocation EqualLoc;
52010b57cec5SDimitry Andric     ExprResult AssignedVal;
52020b57cec5SDimitry Andric     EnumAvailabilityDiags.emplace_back(*this);
52030b57cec5SDimitry Andric 
5204a7dea167SDimitry Andric     EnterExpressionEvaluationContext ConstantEvaluated(
5205a7dea167SDimitry Andric         Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
52060b57cec5SDimitry Andric     if (TryConsumeToken(tok::equal, EqualLoc)) {
5207a7dea167SDimitry Andric       AssignedVal = ParseConstantExpressionInExprEvalContext();
52080b57cec5SDimitry Andric       if (AssignedVal.isInvalid())
52090b57cec5SDimitry Andric         SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch);
52100b57cec5SDimitry Andric     }
52110b57cec5SDimitry Andric 
52120b57cec5SDimitry Andric     // Install the enumerator constant into EnumDecl.
52130b57cec5SDimitry Andric     Decl *EnumConstDecl = Actions.ActOnEnumConstant(
52140b57cec5SDimitry Andric         getCurScope(), EnumDecl, LastEnumConstDecl, IdentLoc, Ident, attrs,
52150b57cec5SDimitry Andric         EqualLoc, AssignedVal.get());
52160b57cec5SDimitry Andric     EnumAvailabilityDiags.back().done();
52170b57cec5SDimitry Andric 
52180b57cec5SDimitry Andric     EnumConstantDecls.push_back(EnumConstDecl);
52190b57cec5SDimitry Andric     LastEnumConstDecl = EnumConstDecl;
52200b57cec5SDimitry Andric 
52210b57cec5SDimitry Andric     if (Tok.is(tok::identifier)) {
52220b57cec5SDimitry Andric       // We're missing a comma between enumerators.
52230b57cec5SDimitry Andric       SourceLocation Loc = getEndOfPreviousToken();
52240b57cec5SDimitry Andric       Diag(Loc, diag::err_enumerator_list_missing_comma)
52250b57cec5SDimitry Andric         << FixItHint::CreateInsertion(Loc, ", ");
52260b57cec5SDimitry Andric       continue;
52270b57cec5SDimitry Andric     }
52280b57cec5SDimitry Andric 
52290b57cec5SDimitry Andric     // Emumerator definition must be finished, only comma or r_brace are
52300b57cec5SDimitry Andric     // allowed here.
52310b57cec5SDimitry Andric     SourceLocation CommaLoc;
52320b57cec5SDimitry Andric     if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) {
52330b57cec5SDimitry Andric       if (EqualLoc.isValid())
52340b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace
52350b57cec5SDimitry Andric                                                            << tok::comma;
52360b57cec5SDimitry Andric       else
52370b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator);
52380b57cec5SDimitry Andric       if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) {
52390b57cec5SDimitry Andric         if (TryConsumeToken(tok::comma, CommaLoc))
52400b57cec5SDimitry Andric           continue;
52410b57cec5SDimitry Andric       } else {
52420b57cec5SDimitry Andric         break;
52430b57cec5SDimitry Andric       }
52440b57cec5SDimitry Andric     }
52450b57cec5SDimitry Andric 
52460b57cec5SDimitry Andric     // If comma is followed by r_brace, emit appropriate warning.
52470b57cec5SDimitry Andric     if (Tok.is(tok::r_brace) && CommaLoc.isValid()) {
52480b57cec5SDimitry Andric       if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
52490b57cec5SDimitry Andric         Diag(CommaLoc, getLangOpts().CPlusPlus ?
52500b57cec5SDimitry Andric                diag::ext_enumerator_list_comma_cxx :
52510b57cec5SDimitry Andric                diag::ext_enumerator_list_comma_c)
52520b57cec5SDimitry Andric           << FixItHint::CreateRemoval(CommaLoc);
52530b57cec5SDimitry Andric       else if (getLangOpts().CPlusPlus11)
52540b57cec5SDimitry Andric         Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
52550b57cec5SDimitry Andric           << FixItHint::CreateRemoval(CommaLoc);
52560b57cec5SDimitry Andric       break;
52570b57cec5SDimitry Andric     }
52580b57cec5SDimitry Andric   }
52590b57cec5SDimitry Andric 
52600b57cec5SDimitry Andric   // Eat the }.
52610b57cec5SDimitry Andric   T.consumeClose();
52620b57cec5SDimitry Andric 
52630b57cec5SDimitry Andric   // If attributes exist after the identifier list, parse them.
52640b57cec5SDimitry Andric   ParsedAttributes attrs(AttrFactory);
52650b57cec5SDimitry Andric   MaybeParseGNUAttributes(attrs);
52660b57cec5SDimitry Andric 
52670b57cec5SDimitry Andric   Actions.ActOnEnumBody(StartLoc, T.getRange(), EnumDecl, EnumConstantDecls,
52680b57cec5SDimitry Andric                         getCurScope(), attrs);
52690b57cec5SDimitry Andric 
52700b57cec5SDimitry Andric   // Now handle enum constant availability diagnostics.
52710b57cec5SDimitry Andric   assert(EnumConstantDecls.size() == EnumAvailabilityDiags.size());
52720b57cec5SDimitry Andric   for (size_t i = 0, e = EnumConstantDecls.size(); i != e; ++i) {
52730b57cec5SDimitry Andric     ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
52740b57cec5SDimitry Andric     EnumAvailabilityDiags[i].redelay();
52750b57cec5SDimitry Andric     PD.complete(EnumConstantDecls[i]);
52760b57cec5SDimitry Andric   }
52770b57cec5SDimitry Andric 
52780b57cec5SDimitry Andric   EnumScope.Exit();
52790b57cec5SDimitry Andric   Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, T.getRange());
52800b57cec5SDimitry Andric 
52810b57cec5SDimitry Andric   // The next token must be valid after an enum definition. If not, a ';'
52820b57cec5SDimitry Andric   // was probably forgotten.
528381ad6265SDimitry Andric   bool CanBeBitfield = getCurScope()->isClassScope();
52840b57cec5SDimitry Andric   if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
52850b57cec5SDimitry Andric     ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
52860b57cec5SDimitry Andric     // Push this token back into the preprocessor and change our current token
52870b57cec5SDimitry Andric     // to ';' so that the rest of the code recovers as though there were an
52880b57cec5SDimitry Andric     // ';' after the definition.
52890b57cec5SDimitry Andric     PP.EnterToken(Tok, /*IsReinject=*/true);
52900b57cec5SDimitry Andric     Tok.setKind(tok::semi);
52910b57cec5SDimitry Andric   }
52920b57cec5SDimitry Andric }
52930b57cec5SDimitry Andric 
52940b57cec5SDimitry Andric /// isKnownToBeTypeSpecifier - Return true if we know that the specified token
52950b57cec5SDimitry Andric /// is definitely a type-specifier.  Return false if it isn't part of a type
52960b57cec5SDimitry Andric /// specifier or if we're not sure.
52970b57cec5SDimitry Andric bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
52980b57cec5SDimitry Andric   switch (Tok.getKind()) {
52990b57cec5SDimitry Andric   default: return false;
53000b57cec5SDimitry Andric     // type-specifiers
53010b57cec5SDimitry Andric   case tok::kw_short:
53020b57cec5SDimitry Andric   case tok::kw_long:
53030b57cec5SDimitry Andric   case tok::kw___int64:
53040b57cec5SDimitry Andric   case tok::kw___int128:
53050b57cec5SDimitry Andric   case tok::kw_signed:
53060b57cec5SDimitry Andric   case tok::kw_unsigned:
53070b57cec5SDimitry Andric   case tok::kw__Complex:
53080b57cec5SDimitry Andric   case tok::kw__Imaginary:
53090b57cec5SDimitry Andric   case tok::kw_void:
53100b57cec5SDimitry Andric   case tok::kw_char:
53110b57cec5SDimitry Andric   case tok::kw_wchar_t:
53120b57cec5SDimitry Andric   case tok::kw_char8_t:
53130b57cec5SDimitry Andric   case tok::kw_char16_t:
53140b57cec5SDimitry Andric   case tok::kw_char32_t:
53150b57cec5SDimitry Andric   case tok::kw_int:
53165ffd83dbSDimitry Andric   case tok::kw__ExtInt:
53170eae32dcSDimitry Andric   case tok::kw__BitInt:
53185ffd83dbSDimitry Andric   case tok::kw___bf16:
53190b57cec5SDimitry Andric   case tok::kw_half:
53200b57cec5SDimitry Andric   case tok::kw_float:
53210b57cec5SDimitry Andric   case tok::kw_double:
53220b57cec5SDimitry Andric   case tok::kw__Accum:
53230b57cec5SDimitry Andric   case tok::kw__Fract:
53240b57cec5SDimitry Andric   case tok::kw__Float16:
53250b57cec5SDimitry Andric   case tok::kw___float128:
5326349cc55cSDimitry Andric   case tok::kw___ibm128:
53270b57cec5SDimitry Andric   case tok::kw_bool:
53280b57cec5SDimitry Andric   case tok::kw__Bool:
53290b57cec5SDimitry Andric   case tok::kw__Decimal32:
53300b57cec5SDimitry Andric   case tok::kw__Decimal64:
53310b57cec5SDimitry Andric   case tok::kw__Decimal128:
53320b57cec5SDimitry Andric   case tok::kw___vector:
53330b57cec5SDimitry Andric #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
53340b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def"
53350b57cec5SDimitry Andric 
53360b57cec5SDimitry Andric     // struct-or-union-specifier (C99) or class-specifier (C++)
53370b57cec5SDimitry Andric   case tok::kw_class:
53380b57cec5SDimitry Andric   case tok::kw_struct:
53390b57cec5SDimitry Andric   case tok::kw___interface:
53400b57cec5SDimitry Andric   case tok::kw_union:
53410b57cec5SDimitry Andric     // enum-specifier
53420b57cec5SDimitry Andric   case tok::kw_enum:
53430b57cec5SDimitry Andric 
53440b57cec5SDimitry Andric     // typedef-name
53450b57cec5SDimitry Andric   case tok::annot_typename:
53460b57cec5SDimitry Andric     return true;
53470b57cec5SDimitry Andric   }
53480b57cec5SDimitry Andric }
53490b57cec5SDimitry Andric 
53500b57cec5SDimitry Andric /// isTypeSpecifierQualifier - Return true if the current token could be the
53510b57cec5SDimitry Andric /// start of a specifier-qualifier-list.
53520b57cec5SDimitry Andric bool Parser::isTypeSpecifierQualifier() {
53530b57cec5SDimitry Andric   switch (Tok.getKind()) {
53540b57cec5SDimitry Andric   default: return false;
53550b57cec5SDimitry Andric 
53560b57cec5SDimitry Andric   case tok::identifier:   // foo::bar
53570b57cec5SDimitry Andric     if (TryAltiVecVectorToken())
53580b57cec5SDimitry Andric       return true;
5359bdd1243dSDimitry Andric     [[fallthrough]];
53600b57cec5SDimitry Andric   case tok::kw_typename:  // typename T::type
53610b57cec5SDimitry Andric     // Annotate typenames and C++ scope specifiers.  If we get one, just
53620b57cec5SDimitry Andric     // recurse to handle whatever we get.
53630b57cec5SDimitry Andric     if (TryAnnotateTypeOrScopeToken())
53640b57cec5SDimitry Andric       return true;
53650b57cec5SDimitry Andric     if (Tok.is(tok::identifier))
53660b57cec5SDimitry Andric       return false;
53670b57cec5SDimitry Andric     return isTypeSpecifierQualifier();
53680b57cec5SDimitry Andric 
53690b57cec5SDimitry Andric   case tok::coloncolon:   // ::foo::bar
53700b57cec5SDimitry Andric     if (NextToken().is(tok::kw_new) ||    // ::new
53710b57cec5SDimitry Andric         NextToken().is(tok::kw_delete))   // ::delete
53720b57cec5SDimitry Andric       return false;
53730b57cec5SDimitry Andric 
53740b57cec5SDimitry Andric     if (TryAnnotateTypeOrScopeToken())
53750b57cec5SDimitry Andric       return true;
53760b57cec5SDimitry Andric     return isTypeSpecifierQualifier();
53770b57cec5SDimitry Andric 
53780b57cec5SDimitry Andric     // GNU attributes support.
53790b57cec5SDimitry Andric   case tok::kw___attribute:
5380bdd1243dSDimitry Andric     // C2x/GNU typeof support.
53810b57cec5SDimitry Andric   case tok::kw_typeof:
5382bdd1243dSDimitry Andric   case tok::kw_typeof_unqual:
53830b57cec5SDimitry Andric 
53840b57cec5SDimitry Andric     // type-specifiers
53850b57cec5SDimitry Andric   case tok::kw_short:
53860b57cec5SDimitry Andric   case tok::kw_long:
53870b57cec5SDimitry Andric   case tok::kw___int64:
53880b57cec5SDimitry Andric   case tok::kw___int128:
53890b57cec5SDimitry Andric   case tok::kw_signed:
53900b57cec5SDimitry Andric   case tok::kw_unsigned:
53910b57cec5SDimitry Andric   case tok::kw__Complex:
53920b57cec5SDimitry Andric   case tok::kw__Imaginary:
53930b57cec5SDimitry Andric   case tok::kw_void:
53940b57cec5SDimitry Andric   case tok::kw_char:
53950b57cec5SDimitry Andric   case tok::kw_wchar_t:
53960b57cec5SDimitry Andric   case tok::kw_char8_t:
53970b57cec5SDimitry Andric   case tok::kw_char16_t:
53980b57cec5SDimitry Andric   case tok::kw_char32_t:
53990b57cec5SDimitry Andric   case tok::kw_int:
54005ffd83dbSDimitry Andric   case tok::kw__ExtInt:
54010eae32dcSDimitry Andric   case tok::kw__BitInt:
54020b57cec5SDimitry Andric   case tok::kw_half:
54035ffd83dbSDimitry Andric   case tok::kw___bf16:
54040b57cec5SDimitry Andric   case tok::kw_float:
54050b57cec5SDimitry Andric   case tok::kw_double:
54060b57cec5SDimitry Andric   case tok::kw__Accum:
54070b57cec5SDimitry Andric   case tok::kw__Fract:
54080b57cec5SDimitry Andric   case tok::kw__Float16:
54090b57cec5SDimitry Andric   case tok::kw___float128:
5410349cc55cSDimitry Andric   case tok::kw___ibm128:
54110b57cec5SDimitry Andric   case tok::kw_bool:
54120b57cec5SDimitry Andric   case tok::kw__Bool:
54130b57cec5SDimitry Andric   case tok::kw__Decimal32:
54140b57cec5SDimitry Andric   case tok::kw__Decimal64:
54150b57cec5SDimitry Andric   case tok::kw__Decimal128:
54160b57cec5SDimitry Andric   case tok::kw___vector:
54170b57cec5SDimitry Andric #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
54180b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def"
54190b57cec5SDimitry Andric 
54200b57cec5SDimitry Andric     // struct-or-union-specifier (C99) or class-specifier (C++)
54210b57cec5SDimitry Andric   case tok::kw_class:
54220b57cec5SDimitry Andric   case tok::kw_struct:
54230b57cec5SDimitry Andric   case tok::kw___interface:
54240b57cec5SDimitry Andric   case tok::kw_union:
54250b57cec5SDimitry Andric     // enum-specifier
54260b57cec5SDimitry Andric   case tok::kw_enum:
54270b57cec5SDimitry Andric 
54280b57cec5SDimitry Andric     // type-qualifier
54290b57cec5SDimitry Andric   case tok::kw_const:
54300b57cec5SDimitry Andric   case tok::kw_volatile:
54310b57cec5SDimitry Andric   case tok::kw_restrict:
54320b57cec5SDimitry Andric   case tok::kw__Sat:
54330b57cec5SDimitry Andric 
54340b57cec5SDimitry Andric     // Debugger support.
54350b57cec5SDimitry Andric   case tok::kw___unknown_anytype:
54360b57cec5SDimitry Andric 
54370b57cec5SDimitry Andric     // typedef-name
54380b57cec5SDimitry Andric   case tok::annot_typename:
54390b57cec5SDimitry Andric     return true;
54400b57cec5SDimitry Andric 
54410b57cec5SDimitry Andric     // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
54420b57cec5SDimitry Andric   case tok::less:
54430b57cec5SDimitry Andric     return getLangOpts().ObjC;
54440b57cec5SDimitry Andric 
54450b57cec5SDimitry Andric   case tok::kw___cdecl:
54460b57cec5SDimitry Andric   case tok::kw___stdcall:
54470b57cec5SDimitry Andric   case tok::kw___fastcall:
54480b57cec5SDimitry Andric   case tok::kw___thiscall:
54490b57cec5SDimitry Andric   case tok::kw___regcall:
54500b57cec5SDimitry Andric   case tok::kw___vectorcall:
54510b57cec5SDimitry Andric   case tok::kw___w64:
54520b57cec5SDimitry Andric   case tok::kw___ptr64:
54530b57cec5SDimitry Andric   case tok::kw___ptr32:
54540b57cec5SDimitry Andric   case tok::kw___pascal:
54550b57cec5SDimitry Andric   case tok::kw___unaligned:
54560b57cec5SDimitry Andric 
54570b57cec5SDimitry Andric   case tok::kw__Nonnull:
54580b57cec5SDimitry Andric   case tok::kw__Nullable:
5459e8d8bef9SDimitry Andric   case tok::kw__Nullable_result:
54600b57cec5SDimitry Andric   case tok::kw__Null_unspecified:
54610b57cec5SDimitry Andric 
54620b57cec5SDimitry Andric   case tok::kw___kindof:
54630b57cec5SDimitry Andric 
54640b57cec5SDimitry Andric   case tok::kw___private:
54650b57cec5SDimitry Andric   case tok::kw___local:
54660b57cec5SDimitry Andric   case tok::kw___global:
54670b57cec5SDimitry Andric   case tok::kw___constant:
54680b57cec5SDimitry Andric   case tok::kw___generic:
54690b57cec5SDimitry Andric   case tok::kw___read_only:
54700b57cec5SDimitry Andric   case tok::kw___read_write:
54710b57cec5SDimitry Andric   case tok::kw___write_only:
5472*06c3fb27SDimitry Andric   case tok::kw___funcref:
5473bdd1243dSDimitry Andric   case tok::kw_groupshared:
54740b57cec5SDimitry Andric     return true;
54750b57cec5SDimitry Andric 
54760b57cec5SDimitry Andric   case tok::kw_private:
54770b57cec5SDimitry Andric     return getLangOpts().OpenCL;
54780b57cec5SDimitry Andric 
54790b57cec5SDimitry Andric   // C11 _Atomic
54800b57cec5SDimitry Andric   case tok::kw__Atomic:
54810b57cec5SDimitry Andric     return true;
54820b57cec5SDimitry Andric   }
54830b57cec5SDimitry Andric }
54840b57cec5SDimitry Andric 
5485bdd1243dSDimitry Andric Parser::DeclGroupPtrTy Parser::ParseTopLevelStmtDecl() {
5486bdd1243dSDimitry Andric   assert(PP.isIncrementalProcessingEnabled() && "Not in incremental mode");
5487bdd1243dSDimitry Andric 
5488bdd1243dSDimitry Andric   // Parse a top-level-stmt.
5489bdd1243dSDimitry Andric   Parser::StmtVector Stmts;
5490bdd1243dSDimitry Andric   ParsedStmtContext SubStmtCtx = ParsedStmtContext();
5491*06c3fb27SDimitry Andric   Actions.PushFunctionScope();
5492bdd1243dSDimitry Andric   StmtResult R = ParseStatementOrDeclaration(Stmts, SubStmtCtx);
5493*06c3fb27SDimitry Andric   Actions.PopFunctionScopeInfo();
5494bdd1243dSDimitry Andric   if (!R.isUsable())
5495bdd1243dSDimitry Andric     return nullptr;
5496bdd1243dSDimitry Andric 
5497bdd1243dSDimitry Andric   SmallVector<Decl *, 2> DeclsInGroup;
5498bdd1243dSDimitry Andric   DeclsInGroup.push_back(Actions.ActOnTopLevelStmtDecl(R.get()));
5499*06c3fb27SDimitry Andric 
5500*06c3fb27SDimitry Andric   if (Tok.is(tok::annot_repl_input_end) &&
5501*06c3fb27SDimitry Andric       Tok.getAnnotationValue() != nullptr) {
5502*06c3fb27SDimitry Andric     ConsumeAnnotationToken();
5503*06c3fb27SDimitry Andric     cast<TopLevelStmtDecl>(DeclsInGroup.back())->setSemiMissing();
5504*06c3fb27SDimitry Andric   }
5505*06c3fb27SDimitry Andric 
5506bdd1243dSDimitry Andric   // Currently happens for things like  -fms-extensions and use `__if_exists`.
5507bdd1243dSDimitry Andric   for (Stmt *S : Stmts)
5508bdd1243dSDimitry Andric     DeclsInGroup.push_back(Actions.ActOnTopLevelStmtDecl(S));
5509bdd1243dSDimitry Andric 
5510bdd1243dSDimitry Andric   return Actions.BuildDeclaratorGroup(DeclsInGroup);
5511bdd1243dSDimitry Andric }
5512bdd1243dSDimitry Andric 
55130b57cec5SDimitry Andric /// isDeclarationSpecifier() - Return true if the current token is part of a
55140b57cec5SDimitry Andric /// declaration specifier.
55150b57cec5SDimitry Andric ///
5516bdd1243dSDimitry Andric /// \param AllowImplicitTypename whether this is a context where T::type [T
5517bdd1243dSDimitry Andric /// dependent] can appear.
55180b57cec5SDimitry Andric /// \param DisambiguatingWithExpression True to indicate that the purpose of
55190b57cec5SDimitry Andric /// this check is to disambiguate between an expression and a declaration.
5520bdd1243dSDimitry Andric bool Parser::isDeclarationSpecifier(
5521bdd1243dSDimitry Andric     ImplicitTypenameContext AllowImplicitTypename,
5522bdd1243dSDimitry Andric     bool DisambiguatingWithExpression) {
55230b57cec5SDimitry Andric   switch (Tok.getKind()) {
55240b57cec5SDimitry Andric   default: return false;
55250b57cec5SDimitry Andric 
55266e75b2fbSDimitry Andric   // OpenCL 2.0 and later define this keyword.
55270b57cec5SDimitry Andric   case tok::kw_pipe:
5528349cc55cSDimitry Andric     return getLangOpts().OpenCL &&
5529349cc55cSDimitry Andric            getLangOpts().getOpenCLCompatibleVersion() >= 200;
55300b57cec5SDimitry Andric 
55310b57cec5SDimitry Andric   case tok::identifier:   // foo::bar
55320b57cec5SDimitry Andric     // Unfortunate hack to support "Class.factoryMethod" notation.
55330b57cec5SDimitry Andric     if (getLangOpts().ObjC && NextToken().is(tok::period))
55340b57cec5SDimitry Andric       return false;
55350b57cec5SDimitry Andric     if (TryAltiVecVectorToken())
55360b57cec5SDimitry Andric       return true;
5537bdd1243dSDimitry Andric     [[fallthrough]];
55380b57cec5SDimitry Andric   case tok::kw_decltype: // decltype(T())::type
55390b57cec5SDimitry Andric   case tok::kw_typename: // typename T::type
55400b57cec5SDimitry Andric     // Annotate typenames and C++ scope specifiers.  If we get one, just
55410b57cec5SDimitry Andric     // recurse to handle whatever we get.
5542bdd1243dSDimitry Andric     if (TryAnnotateTypeOrScopeToken(AllowImplicitTypename))
55430b57cec5SDimitry Andric       return true;
554413138422SDimitry Andric     if (TryAnnotateTypeConstraint())
554513138422SDimitry Andric       return true;
55460b57cec5SDimitry Andric     if (Tok.is(tok::identifier))
55470b57cec5SDimitry Andric       return false;
55480b57cec5SDimitry Andric 
55490b57cec5SDimitry Andric     // If we're in Objective-C and we have an Objective-C class type followed
55500b57cec5SDimitry Andric     // by an identifier and then either ':' or ']', in a place where an
55510b57cec5SDimitry Andric     // expression is permitted, then this is probably a class message send
55520b57cec5SDimitry Andric     // missing the initial '['. In this case, we won't consider this to be
55530b57cec5SDimitry Andric     // the start of a declaration.
55540b57cec5SDimitry Andric     if (DisambiguatingWithExpression &&
55550b57cec5SDimitry Andric         isStartOfObjCClassMessageMissingOpenBracket())
55560b57cec5SDimitry Andric       return false;
55570b57cec5SDimitry Andric 
5558bdd1243dSDimitry Andric     return isDeclarationSpecifier(AllowImplicitTypename);
55590b57cec5SDimitry Andric 
55600b57cec5SDimitry Andric   case tok::coloncolon:   // ::foo::bar
5561bdd1243dSDimitry Andric     if (!getLangOpts().CPlusPlus)
5562bdd1243dSDimitry Andric       return false;
55630b57cec5SDimitry Andric     if (NextToken().is(tok::kw_new) ||    // ::new
55640b57cec5SDimitry Andric         NextToken().is(tok::kw_delete))   // ::delete
55650b57cec5SDimitry Andric       return false;
55660b57cec5SDimitry Andric 
55670b57cec5SDimitry Andric     // Annotate typenames and C++ scope specifiers.  If we get one, just
55680b57cec5SDimitry Andric     // recurse to handle whatever we get.
55690b57cec5SDimitry Andric     if (TryAnnotateTypeOrScopeToken())
55700b57cec5SDimitry Andric       return true;
5571bdd1243dSDimitry Andric     return isDeclarationSpecifier(ImplicitTypenameContext::No);
55720b57cec5SDimitry Andric 
55730b57cec5SDimitry Andric     // storage-class-specifier
55740b57cec5SDimitry Andric   case tok::kw_typedef:
55750b57cec5SDimitry Andric   case tok::kw_extern:
55760b57cec5SDimitry Andric   case tok::kw___private_extern__:
55770b57cec5SDimitry Andric   case tok::kw_static:
55780b57cec5SDimitry Andric   case tok::kw_auto:
55790b57cec5SDimitry Andric   case tok::kw___auto_type:
55800b57cec5SDimitry Andric   case tok::kw_register:
55810b57cec5SDimitry Andric   case tok::kw___thread:
55820b57cec5SDimitry Andric   case tok::kw_thread_local:
55830b57cec5SDimitry Andric   case tok::kw__Thread_local:
55840b57cec5SDimitry Andric 
55850b57cec5SDimitry Andric     // Modules
55860b57cec5SDimitry Andric   case tok::kw___module_private__:
55870b57cec5SDimitry Andric 
55880b57cec5SDimitry Andric     // Debugger support
55890b57cec5SDimitry Andric   case tok::kw___unknown_anytype:
55900b57cec5SDimitry Andric 
55910b57cec5SDimitry Andric     // type-specifiers
55920b57cec5SDimitry Andric   case tok::kw_short:
55930b57cec5SDimitry Andric   case tok::kw_long:
55940b57cec5SDimitry Andric   case tok::kw___int64:
55950b57cec5SDimitry Andric   case tok::kw___int128:
55960b57cec5SDimitry Andric   case tok::kw_signed:
55970b57cec5SDimitry Andric   case tok::kw_unsigned:
55980b57cec5SDimitry Andric   case tok::kw__Complex:
55990b57cec5SDimitry Andric   case tok::kw__Imaginary:
56000b57cec5SDimitry Andric   case tok::kw_void:
56010b57cec5SDimitry Andric   case tok::kw_char:
56020b57cec5SDimitry Andric   case tok::kw_wchar_t:
56030b57cec5SDimitry Andric   case tok::kw_char8_t:
56040b57cec5SDimitry Andric   case tok::kw_char16_t:
56050b57cec5SDimitry Andric   case tok::kw_char32_t:
56060b57cec5SDimitry Andric 
56070b57cec5SDimitry Andric   case tok::kw_int:
56085ffd83dbSDimitry Andric   case tok::kw__ExtInt:
56090eae32dcSDimitry Andric   case tok::kw__BitInt:
56100b57cec5SDimitry Andric   case tok::kw_half:
56115ffd83dbSDimitry Andric   case tok::kw___bf16:
56120b57cec5SDimitry Andric   case tok::kw_float:
56130b57cec5SDimitry Andric   case tok::kw_double:
56140b57cec5SDimitry Andric   case tok::kw__Accum:
56150b57cec5SDimitry Andric   case tok::kw__Fract:
56160b57cec5SDimitry Andric   case tok::kw__Float16:
56170b57cec5SDimitry Andric   case tok::kw___float128:
5618349cc55cSDimitry Andric   case tok::kw___ibm128:
56190b57cec5SDimitry Andric   case tok::kw_bool:
56200b57cec5SDimitry Andric   case tok::kw__Bool:
56210b57cec5SDimitry Andric   case tok::kw__Decimal32:
56220b57cec5SDimitry Andric   case tok::kw__Decimal64:
56230b57cec5SDimitry Andric   case tok::kw__Decimal128:
56240b57cec5SDimitry Andric   case tok::kw___vector:
56250b57cec5SDimitry Andric 
56260b57cec5SDimitry Andric     // struct-or-union-specifier (C99) or class-specifier (C++)
56270b57cec5SDimitry Andric   case tok::kw_class:
56280b57cec5SDimitry Andric   case tok::kw_struct:
56290b57cec5SDimitry Andric   case tok::kw_union:
56300b57cec5SDimitry Andric   case tok::kw___interface:
56310b57cec5SDimitry Andric     // enum-specifier
56320b57cec5SDimitry Andric   case tok::kw_enum:
56330b57cec5SDimitry Andric 
56340b57cec5SDimitry Andric     // type-qualifier
56350b57cec5SDimitry Andric   case tok::kw_const:
56360b57cec5SDimitry Andric   case tok::kw_volatile:
56370b57cec5SDimitry Andric   case tok::kw_restrict:
56380b57cec5SDimitry Andric   case tok::kw__Sat:
56390b57cec5SDimitry Andric 
56400b57cec5SDimitry Andric     // function-specifier
56410b57cec5SDimitry Andric   case tok::kw_inline:
56420b57cec5SDimitry Andric   case tok::kw_virtual:
56430b57cec5SDimitry Andric   case tok::kw_explicit:
56440b57cec5SDimitry Andric   case tok::kw__Noreturn:
56450b57cec5SDimitry Andric 
56460b57cec5SDimitry Andric     // alignment-specifier
56470b57cec5SDimitry Andric   case tok::kw__Alignas:
56480b57cec5SDimitry Andric 
56490b57cec5SDimitry Andric     // friend keyword.
56500b57cec5SDimitry Andric   case tok::kw_friend:
56510b57cec5SDimitry Andric 
56520b57cec5SDimitry Andric     // static_assert-declaration
5653d409305fSDimitry Andric   case tok::kw_static_assert:
56540b57cec5SDimitry Andric   case tok::kw__Static_assert:
56550b57cec5SDimitry Andric 
5656bdd1243dSDimitry Andric     // C2x/GNU typeof support.
56570b57cec5SDimitry Andric   case tok::kw_typeof:
5658bdd1243dSDimitry Andric   case tok::kw_typeof_unqual:
56590b57cec5SDimitry Andric 
56600b57cec5SDimitry Andric     // GNU attributes.
56610b57cec5SDimitry Andric   case tok::kw___attribute:
56620b57cec5SDimitry Andric 
56630b57cec5SDimitry Andric     // C++11 decltype and constexpr.
56640b57cec5SDimitry Andric   case tok::annot_decltype:
56650b57cec5SDimitry Andric   case tok::kw_constexpr:
56660b57cec5SDimitry Andric 
5667a7dea167SDimitry Andric     // C++20 consteval and constinit.
56680b57cec5SDimitry Andric   case tok::kw_consteval:
5669a7dea167SDimitry Andric   case tok::kw_constinit:
56700b57cec5SDimitry Andric 
56710b57cec5SDimitry Andric     // C11 _Atomic
56720b57cec5SDimitry Andric   case tok::kw__Atomic:
56730b57cec5SDimitry Andric     return true;
56740b57cec5SDimitry Andric 
56750b57cec5SDimitry Andric     // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
56760b57cec5SDimitry Andric   case tok::less:
56770b57cec5SDimitry Andric     return getLangOpts().ObjC;
56780b57cec5SDimitry Andric 
56790b57cec5SDimitry Andric     // typedef-name
56800b57cec5SDimitry Andric   case tok::annot_typename:
56810b57cec5SDimitry Andric     return !DisambiguatingWithExpression ||
56820b57cec5SDimitry Andric            !isStartOfObjCClassMessageMissingOpenBracket();
56830b57cec5SDimitry Andric 
5684480093f4SDimitry Andric     // placeholder-type-specifier
5685480093f4SDimitry Andric   case tok::annot_template_id: {
56865ffd83dbSDimitry Andric     TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
56875ffd83dbSDimitry Andric     if (TemplateId->hasInvalidName())
56885ffd83dbSDimitry Andric       return true;
56895ffd83dbSDimitry Andric     // FIXME: What about type templates that have only been annotated as
56905ffd83dbSDimitry Andric     // annot_template_id, not as annot_typename?
569113138422SDimitry Andric     return isTypeConstraintAnnotation() &&
5692480093f4SDimitry Andric            (NextToken().is(tok::kw_auto) || NextToken().is(tok::kw_decltype));
5693480093f4SDimitry Andric   }
56945ffd83dbSDimitry Andric 
56955ffd83dbSDimitry Andric   case tok::annot_cxxscope: {
56965ffd83dbSDimitry Andric     TemplateIdAnnotation *TemplateId =
56975ffd83dbSDimitry Andric         NextToken().is(tok::annot_template_id)
56985ffd83dbSDimitry Andric             ? takeTemplateIdAnnotation(NextToken())
56995ffd83dbSDimitry Andric             : nullptr;
57005ffd83dbSDimitry Andric     if (TemplateId && TemplateId->hasInvalidName())
57015ffd83dbSDimitry Andric       return true;
57025ffd83dbSDimitry Andric     // FIXME: What about type templates that have only been annotated as
57035ffd83dbSDimitry Andric     // annot_template_id, not as annot_typename?
570413138422SDimitry Andric     if (NextToken().is(tok::identifier) && TryAnnotateTypeConstraint())
570513138422SDimitry Andric       return true;
570613138422SDimitry Andric     return isTypeConstraintAnnotation() &&
570713138422SDimitry Andric         GetLookAheadToken(2).isOneOf(tok::kw_auto, tok::kw_decltype);
57085ffd83dbSDimitry Andric   }
57095ffd83dbSDimitry Andric 
57100b57cec5SDimitry Andric   case tok::kw___declspec:
57110b57cec5SDimitry Andric   case tok::kw___cdecl:
57120b57cec5SDimitry Andric   case tok::kw___stdcall:
57130b57cec5SDimitry Andric   case tok::kw___fastcall:
57140b57cec5SDimitry Andric   case tok::kw___thiscall:
57150b57cec5SDimitry Andric   case tok::kw___regcall:
57160b57cec5SDimitry Andric   case tok::kw___vectorcall:
57170b57cec5SDimitry Andric   case tok::kw___w64:
57180b57cec5SDimitry Andric   case tok::kw___sptr:
57190b57cec5SDimitry Andric   case tok::kw___uptr:
57200b57cec5SDimitry Andric   case tok::kw___ptr64:
57210b57cec5SDimitry Andric   case tok::kw___ptr32:
57220b57cec5SDimitry Andric   case tok::kw___forceinline:
57230b57cec5SDimitry Andric   case tok::kw___pascal:
57240b57cec5SDimitry Andric   case tok::kw___unaligned:
57250b57cec5SDimitry Andric 
57260b57cec5SDimitry Andric   case tok::kw__Nonnull:
57270b57cec5SDimitry Andric   case tok::kw__Nullable:
5728e8d8bef9SDimitry Andric   case tok::kw__Nullable_result:
57290b57cec5SDimitry Andric   case tok::kw__Null_unspecified:
57300b57cec5SDimitry Andric 
57310b57cec5SDimitry Andric   case tok::kw___kindof:
57320b57cec5SDimitry Andric 
57330b57cec5SDimitry Andric   case tok::kw___private:
57340b57cec5SDimitry Andric   case tok::kw___local:
57350b57cec5SDimitry Andric   case tok::kw___global:
57360b57cec5SDimitry Andric   case tok::kw___constant:
57370b57cec5SDimitry Andric   case tok::kw___generic:
57380b57cec5SDimitry Andric   case tok::kw___read_only:
57390b57cec5SDimitry Andric   case tok::kw___read_write:
57400b57cec5SDimitry Andric   case tok::kw___write_only:
57410b57cec5SDimitry Andric #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
57420b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def"
57430b57cec5SDimitry Andric 
5744*06c3fb27SDimitry Andric   case tok::kw___funcref:
5745bdd1243dSDimitry Andric   case tok::kw_groupshared:
57460b57cec5SDimitry Andric     return true;
57470b57cec5SDimitry Andric 
57480b57cec5SDimitry Andric   case tok::kw_private:
57490b57cec5SDimitry Andric     return getLangOpts().OpenCL;
57500b57cec5SDimitry Andric   }
57510b57cec5SDimitry Andric }
57520b57cec5SDimitry Andric 
5753bdd1243dSDimitry Andric bool Parser::isConstructorDeclarator(bool IsUnqualified, bool DeductionGuide,
5754*06c3fb27SDimitry Andric                                      DeclSpec::FriendSpecified IsFriend,
5755*06c3fb27SDimitry Andric                                      const ParsedTemplateInfo *TemplateInfo) {
57560b57cec5SDimitry Andric   TentativeParsingAction TPA(*this);
57570b57cec5SDimitry Andric 
57580b57cec5SDimitry Andric   // Parse the C++ scope specifier.
57590b57cec5SDimitry Andric   CXXScopeSpec SS;
5760*06c3fb27SDimitry Andric   if (TemplateInfo && TemplateInfo->TemplateParams)
5761*06c3fb27SDimitry Andric     SS.setTemplateParamLists(*TemplateInfo->TemplateParams);
5762*06c3fb27SDimitry Andric 
57635ffd83dbSDimitry Andric   if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
576404eeddc0SDimitry Andric                                      /*ObjectHasErrors=*/false,
57650b57cec5SDimitry Andric                                      /*EnteringContext=*/true)) {
57660b57cec5SDimitry Andric     TPA.Revert();
57670b57cec5SDimitry Andric     return false;
57680b57cec5SDimitry Andric   }
57690b57cec5SDimitry Andric 
57700b57cec5SDimitry Andric   // Parse the constructor name.
57710b57cec5SDimitry Andric   if (Tok.is(tok::identifier)) {
57720b57cec5SDimitry Andric     // We already know that we have a constructor name; just consume
57730b57cec5SDimitry Andric     // the token.
57740b57cec5SDimitry Andric     ConsumeToken();
57750b57cec5SDimitry Andric   } else if (Tok.is(tok::annot_template_id)) {
57760b57cec5SDimitry Andric     ConsumeAnnotationToken();
57770b57cec5SDimitry Andric   } else {
57780b57cec5SDimitry Andric     TPA.Revert();
57790b57cec5SDimitry Andric     return false;
57800b57cec5SDimitry Andric   }
57810b57cec5SDimitry Andric 
57820b57cec5SDimitry Andric   // There may be attributes here, appertaining to the constructor name or type
57830b57cec5SDimitry Andric   // we just stepped past.
57840b57cec5SDimitry Andric   SkipCXX11Attributes();
57850b57cec5SDimitry Andric 
57860b57cec5SDimitry Andric   // Current class name must be followed by a left parenthesis.
57870b57cec5SDimitry Andric   if (Tok.isNot(tok::l_paren)) {
57880b57cec5SDimitry Andric     TPA.Revert();
57890b57cec5SDimitry Andric     return false;
57900b57cec5SDimitry Andric   }
57910b57cec5SDimitry Andric   ConsumeParen();
57920b57cec5SDimitry Andric 
57930b57cec5SDimitry Andric   // A right parenthesis, or ellipsis followed by a right parenthesis signals
57940b57cec5SDimitry Andric   // that we have a constructor.
57950b57cec5SDimitry Andric   if (Tok.is(tok::r_paren) ||
57960b57cec5SDimitry Andric       (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
57970b57cec5SDimitry Andric     TPA.Revert();
57980b57cec5SDimitry Andric     return true;
57990b57cec5SDimitry Andric   }
58000b57cec5SDimitry Andric 
58010b57cec5SDimitry Andric   // A C++11 attribute here signals that we have a constructor, and is an
58020b57cec5SDimitry Andric   // attribute on the first constructor parameter.
58030b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus11 &&
58040b57cec5SDimitry Andric       isCXX11AttributeSpecifier(/*Disambiguate*/ false,
58050b57cec5SDimitry Andric                                 /*OuterMightBeMessageSend*/ true)) {
58060b57cec5SDimitry Andric     TPA.Revert();
58070b57cec5SDimitry Andric     return true;
58080b57cec5SDimitry Andric   }
58090b57cec5SDimitry Andric 
58100b57cec5SDimitry Andric   // If we need to, enter the specified scope.
58110b57cec5SDimitry Andric   DeclaratorScopeObj DeclScopeObj(*this, SS);
58120b57cec5SDimitry Andric   if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
58130b57cec5SDimitry Andric     DeclScopeObj.EnterDeclaratorScope();
58140b57cec5SDimitry Andric 
58150b57cec5SDimitry Andric   // Optionally skip Microsoft attributes.
58160b57cec5SDimitry Andric   ParsedAttributes Attrs(AttrFactory);
58170b57cec5SDimitry Andric   MaybeParseMicrosoftAttributes(Attrs);
58180b57cec5SDimitry Andric 
58190b57cec5SDimitry Andric   // Check whether the next token(s) are part of a declaration
58200b57cec5SDimitry Andric   // specifier, in which case we have the start of a parameter and,
58210b57cec5SDimitry Andric   // therefore, we know that this is a constructor.
5822bdd1243dSDimitry Andric   // Due to an ambiguity with implicit typename, the above is not enough.
5823bdd1243dSDimitry Andric   // Additionally, check to see if we are a friend.
5824*06c3fb27SDimitry Andric   // If we parsed a scope specifier as well as friend,
5825*06c3fb27SDimitry Andric   // we might be parsing a friend constructor.
58260b57cec5SDimitry Andric   bool IsConstructor = false;
5827*06c3fb27SDimitry Andric   if (isDeclarationSpecifier(IsFriend && !SS.isSet()
5828*06c3fb27SDimitry Andric                                  ? ImplicitTypenameContext::No
5829bdd1243dSDimitry Andric                                  : ImplicitTypenameContext::Yes))
58300b57cec5SDimitry Andric     IsConstructor = true;
58310b57cec5SDimitry Andric   else if (Tok.is(tok::identifier) ||
58320b57cec5SDimitry Andric            (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
58330b57cec5SDimitry Andric     // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
58340b57cec5SDimitry Andric     // This might be a parenthesized member name, but is more likely to
58350b57cec5SDimitry Andric     // be a constructor declaration with an invalid argument type. Keep
58360b57cec5SDimitry Andric     // looking.
58370b57cec5SDimitry Andric     if (Tok.is(tok::annot_cxxscope))
58380b57cec5SDimitry Andric       ConsumeAnnotationToken();
58390b57cec5SDimitry Andric     ConsumeToken();
58400b57cec5SDimitry Andric 
58410b57cec5SDimitry Andric     // If this is not a constructor, we must be parsing a declarator,
58420b57cec5SDimitry Andric     // which must have one of the following syntactic forms (see the
58430b57cec5SDimitry Andric     // grammar extract at the start of ParseDirectDeclarator):
58440b57cec5SDimitry Andric     switch (Tok.getKind()) {
58450b57cec5SDimitry Andric     case tok::l_paren:
58460b57cec5SDimitry Andric       // C(X   (   int));
58470b57cec5SDimitry Andric     case tok::l_square:
58480b57cec5SDimitry Andric       // C(X   [   5]);
58490b57cec5SDimitry Andric       // C(X   [   [attribute]]);
58500b57cec5SDimitry Andric     case tok::coloncolon:
58510b57cec5SDimitry Andric       // C(X   ::   Y);
58520b57cec5SDimitry Andric       // C(X   ::   *p);
58530b57cec5SDimitry Andric       // Assume this isn't a constructor, rather than assuming it's a
58540b57cec5SDimitry Andric       // constructor with an unnamed parameter of an ill-formed type.
58550b57cec5SDimitry Andric       break;
58560b57cec5SDimitry Andric 
58570b57cec5SDimitry Andric     case tok::r_paren:
58580b57cec5SDimitry Andric       // C(X   )
58590b57cec5SDimitry Andric 
58600b57cec5SDimitry Andric       // Skip past the right-paren and any following attributes to get to
58610b57cec5SDimitry Andric       // the function body or trailing-return-type.
58620b57cec5SDimitry Andric       ConsumeParen();
58630b57cec5SDimitry Andric       SkipCXX11Attributes();
58640b57cec5SDimitry Andric 
58650b57cec5SDimitry Andric       if (DeductionGuide) {
58660b57cec5SDimitry Andric         // C(X) -> ... is a deduction guide.
58670b57cec5SDimitry Andric         IsConstructor = Tok.is(tok::arrow);
58680b57cec5SDimitry Andric         break;
58690b57cec5SDimitry Andric       }
58700b57cec5SDimitry Andric       if (Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
58710b57cec5SDimitry Andric         // Assume these were meant to be constructors:
58720b57cec5SDimitry Andric         //   C(X)   :    (the name of a bit-field cannot be parenthesized).
58730b57cec5SDimitry Andric         //   C(X)   try  (this is otherwise ill-formed).
58740b57cec5SDimitry Andric         IsConstructor = true;
58750b57cec5SDimitry Andric       }
58760b57cec5SDimitry Andric       if (Tok.is(tok::semi) || Tok.is(tok::l_brace)) {
58770b57cec5SDimitry Andric         // If we have a constructor name within the class definition,
58780b57cec5SDimitry Andric         // assume these were meant to be constructors:
58790b57cec5SDimitry Andric         //   C(X)   {
58800b57cec5SDimitry Andric         //   C(X)   ;
58810b57cec5SDimitry Andric         // ... because otherwise we would be declaring a non-static data
58820b57cec5SDimitry Andric         // member that is ill-formed because it's of the same type as its
58830b57cec5SDimitry Andric         // surrounding class.
58840b57cec5SDimitry Andric         //
58850b57cec5SDimitry Andric         // FIXME: We can actually do this whether or not the name is qualified,
58860b57cec5SDimitry Andric         // because if it is qualified in this context it must be being used as
58870b57cec5SDimitry Andric         // a constructor name.
58880b57cec5SDimitry Andric         // currently, so we're somewhat conservative here.
58890b57cec5SDimitry Andric         IsConstructor = IsUnqualified;
58900b57cec5SDimitry Andric       }
58910b57cec5SDimitry Andric       break;
58920b57cec5SDimitry Andric 
58930b57cec5SDimitry Andric     default:
58940b57cec5SDimitry Andric       IsConstructor = true;
58950b57cec5SDimitry Andric       break;
58960b57cec5SDimitry Andric     }
58970b57cec5SDimitry Andric   }
58980b57cec5SDimitry Andric 
58990b57cec5SDimitry Andric   TPA.Revert();
59000b57cec5SDimitry Andric   return IsConstructor;
59010b57cec5SDimitry Andric }
59020b57cec5SDimitry Andric 
59030b57cec5SDimitry Andric /// ParseTypeQualifierListOpt
59040b57cec5SDimitry Andric ///          type-qualifier-list: [C99 6.7.5]
59050b57cec5SDimitry Andric ///            type-qualifier
59060b57cec5SDimitry Andric /// [vendor]   attributes
59070b57cec5SDimitry Andric ///              [ only if AttrReqs & AR_VendorAttributesParsed ]
59080b57cec5SDimitry Andric ///            type-qualifier-list type-qualifier
59090b57cec5SDimitry Andric /// [vendor]   type-qualifier-list attributes
59100b57cec5SDimitry Andric ///              [ only if AttrReqs & AR_VendorAttributesParsed ]
59110b57cec5SDimitry Andric /// [C++0x]    attribute-specifier[opt] is allowed before cv-qualifier-seq
59120b57cec5SDimitry Andric ///              [ only if AttReqs & AR_CXX11AttributesParsed ]
59130b57cec5SDimitry Andric /// Note: vendor can be GNU, MS, etc and can be explicitly controlled via
59140b57cec5SDimitry Andric /// AttrRequirements bitmask values.
59150b57cec5SDimitry Andric void Parser::ParseTypeQualifierListOpt(
59160b57cec5SDimitry Andric     DeclSpec &DS, unsigned AttrReqs, bool AtomicAllowed,
59170b57cec5SDimitry Andric     bool IdentifierRequired,
5918bdd1243dSDimitry Andric     std::optional<llvm::function_ref<void()>> CodeCompletionHandler) {
5919*06c3fb27SDimitry Andric   if ((AttrReqs & AR_CXX11AttributesParsed) &&
5920*06c3fb27SDimitry Andric       isAllowedCXX11AttributeSpecifier()) {
592181ad6265SDimitry Andric     ParsedAttributes Attrs(AttrFactory);
592281ad6265SDimitry Andric     ParseCXX11Attributes(Attrs);
592381ad6265SDimitry Andric     DS.takeAttributesFrom(Attrs);
59240b57cec5SDimitry Andric   }
59250b57cec5SDimitry Andric 
59260b57cec5SDimitry Andric   SourceLocation EndLoc;
59270b57cec5SDimitry Andric 
592804eeddc0SDimitry Andric   while (true) {
59290b57cec5SDimitry Andric     bool isInvalid = false;
59300b57cec5SDimitry Andric     const char *PrevSpec = nullptr;
59310b57cec5SDimitry Andric     unsigned DiagID = 0;
59320b57cec5SDimitry Andric     SourceLocation Loc = Tok.getLocation();
59330b57cec5SDimitry Andric 
59340b57cec5SDimitry Andric     switch (Tok.getKind()) {
59350b57cec5SDimitry Andric     case tok::code_completion:
5936fe6060f1SDimitry Andric       cutOffParsing();
59370b57cec5SDimitry Andric       if (CodeCompletionHandler)
59380b57cec5SDimitry Andric         (*CodeCompletionHandler)();
59390b57cec5SDimitry Andric       else
59400b57cec5SDimitry Andric         Actions.CodeCompleteTypeQualifiers(DS);
5941fe6060f1SDimitry Andric       return;
59420b57cec5SDimitry Andric 
59430b57cec5SDimitry Andric     case tok::kw_const:
59440b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_const   , Loc, PrevSpec, DiagID,
59450b57cec5SDimitry Andric                                  getLangOpts());
59460b57cec5SDimitry Andric       break;
59470b57cec5SDimitry Andric     case tok::kw_volatile:
59480b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
59490b57cec5SDimitry Andric                                  getLangOpts());
59500b57cec5SDimitry Andric       break;
59510b57cec5SDimitry Andric     case tok::kw_restrict:
59520b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
59530b57cec5SDimitry Andric                                  getLangOpts());
59540b57cec5SDimitry Andric       break;
59550b57cec5SDimitry Andric     case tok::kw__Atomic:
59560b57cec5SDimitry Andric       if (!AtomicAllowed)
59570b57cec5SDimitry Andric         goto DoneWithTypeQuals;
5958a7dea167SDimitry Andric       if (!getLangOpts().C11)
5959a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
59600b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
59610b57cec5SDimitry Andric                                  getLangOpts());
59620b57cec5SDimitry Andric       break;
59630b57cec5SDimitry Andric 
59640b57cec5SDimitry Andric     // OpenCL qualifiers:
59650b57cec5SDimitry Andric     case tok::kw_private:
59660b57cec5SDimitry Andric       if (!getLangOpts().OpenCL)
59670b57cec5SDimitry Andric         goto DoneWithTypeQuals;
5968bdd1243dSDimitry Andric       [[fallthrough]];
59690b57cec5SDimitry Andric     case tok::kw___private:
59700b57cec5SDimitry Andric     case tok::kw___global:
59710b57cec5SDimitry Andric     case tok::kw___local:
59720b57cec5SDimitry Andric     case tok::kw___constant:
59730b57cec5SDimitry Andric     case tok::kw___generic:
59740b57cec5SDimitry Andric     case tok::kw___read_only:
59750b57cec5SDimitry Andric     case tok::kw___write_only:
59760b57cec5SDimitry Andric     case tok::kw___read_write:
59770b57cec5SDimitry Andric       ParseOpenCLQualifiers(DS.getAttributes());
59780b57cec5SDimitry Andric       break;
59790b57cec5SDimitry Andric 
5980bdd1243dSDimitry Andric     case tok::kw_groupshared:
5981bdd1243dSDimitry Andric       // NOTE: ParseHLSLQualifiers will consume the qualifier token.
5982bdd1243dSDimitry Andric       ParseHLSLQualifiers(DS.getAttributes());
5983bdd1243dSDimitry Andric       continue;
5984bdd1243dSDimitry Andric 
59850b57cec5SDimitry Andric     case tok::kw___unaligned:
59860b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID,
59870b57cec5SDimitry Andric                                  getLangOpts());
59880b57cec5SDimitry Andric       break;
59890b57cec5SDimitry Andric     case tok::kw___uptr:
59900b57cec5SDimitry Andric       // GNU libc headers in C mode use '__uptr' as an identifier which conflicts
59910b57cec5SDimitry Andric       // with the MS modifier keyword.
59920b57cec5SDimitry Andric       if ((AttrReqs & AR_DeclspecAttributesParsed) && !getLangOpts().CPlusPlus &&
59930b57cec5SDimitry Andric           IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
59940b57cec5SDimitry Andric         if (TryKeywordIdentFallback(false))
59950b57cec5SDimitry Andric           continue;
59960b57cec5SDimitry Andric       }
5997bdd1243dSDimitry Andric       [[fallthrough]];
59980b57cec5SDimitry Andric     case tok::kw___sptr:
59990b57cec5SDimitry Andric     case tok::kw___w64:
60000b57cec5SDimitry Andric     case tok::kw___ptr64:
60010b57cec5SDimitry Andric     case tok::kw___ptr32:
60020b57cec5SDimitry Andric     case tok::kw___cdecl:
60030b57cec5SDimitry Andric     case tok::kw___stdcall:
60040b57cec5SDimitry Andric     case tok::kw___fastcall:
60050b57cec5SDimitry Andric     case tok::kw___thiscall:
60060b57cec5SDimitry Andric     case tok::kw___regcall:
60070b57cec5SDimitry Andric     case tok::kw___vectorcall:
60080b57cec5SDimitry Andric       if (AttrReqs & AR_DeclspecAttributesParsed) {
60090b57cec5SDimitry Andric         ParseMicrosoftTypeAttributes(DS.getAttributes());
60100b57cec5SDimitry Andric         continue;
60110b57cec5SDimitry Andric       }
60120b57cec5SDimitry Andric       goto DoneWithTypeQuals;
6013*06c3fb27SDimitry Andric 
6014*06c3fb27SDimitry Andric     case tok::kw___funcref:
6015*06c3fb27SDimitry Andric       ParseWebAssemblyFuncrefTypeAttribute(DS.getAttributes());
6016*06c3fb27SDimitry Andric       continue;
6017*06c3fb27SDimitry Andric       goto DoneWithTypeQuals;
6018*06c3fb27SDimitry Andric 
60190b57cec5SDimitry Andric     case tok::kw___pascal:
60200b57cec5SDimitry Andric       if (AttrReqs & AR_VendorAttributesParsed) {
60210b57cec5SDimitry Andric         ParseBorlandTypeAttributes(DS.getAttributes());
60220b57cec5SDimitry Andric         continue;
60230b57cec5SDimitry Andric       }
60240b57cec5SDimitry Andric       goto DoneWithTypeQuals;
60250b57cec5SDimitry Andric 
60260b57cec5SDimitry Andric     // Nullability type specifiers.
60270b57cec5SDimitry Andric     case tok::kw__Nonnull:
60280b57cec5SDimitry Andric     case tok::kw__Nullable:
6029e8d8bef9SDimitry Andric     case tok::kw__Nullable_result:
60300b57cec5SDimitry Andric     case tok::kw__Null_unspecified:
60310b57cec5SDimitry Andric       ParseNullabilityTypeSpecifiers(DS.getAttributes());
60320b57cec5SDimitry Andric       continue;
60330b57cec5SDimitry Andric 
60340b57cec5SDimitry Andric     // Objective-C 'kindof' types.
60350b57cec5SDimitry Andric     case tok::kw___kindof:
60360b57cec5SDimitry Andric       DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc,
6037*06c3fb27SDimitry Andric                                 nullptr, 0, tok::kw___kindof);
60380b57cec5SDimitry Andric       (void)ConsumeToken();
60390b57cec5SDimitry Andric       continue;
60400b57cec5SDimitry Andric 
60410b57cec5SDimitry Andric     case tok::kw___attribute:
60420b57cec5SDimitry Andric       if (AttrReqs & AR_GNUAttributesParsedAndRejected)
60430b57cec5SDimitry Andric         // When GNU attributes are expressly forbidden, diagnose their usage.
60440b57cec5SDimitry Andric         Diag(Tok, diag::err_attributes_not_allowed);
60450b57cec5SDimitry Andric 
60460b57cec5SDimitry Andric       // Parse the attributes even if they are rejected to ensure that error
60470b57cec5SDimitry Andric       // recovery is graceful.
60480b57cec5SDimitry Andric       if (AttrReqs & AR_GNUAttributesParsed ||
60490b57cec5SDimitry Andric           AttrReqs & AR_GNUAttributesParsedAndRejected) {
60500b57cec5SDimitry Andric         ParseGNUAttributes(DS.getAttributes());
60510b57cec5SDimitry Andric         continue; // do *not* consume the next token!
60520b57cec5SDimitry Andric       }
60530b57cec5SDimitry Andric       // otherwise, FALL THROUGH!
6054bdd1243dSDimitry Andric       [[fallthrough]];
60550b57cec5SDimitry Andric     default:
60560b57cec5SDimitry Andric       DoneWithTypeQuals:
60570b57cec5SDimitry Andric       // If this is not a type-qualifier token, we're done reading type
60580b57cec5SDimitry Andric       // qualifiers.  First verify that DeclSpec's are consistent.
60590b57cec5SDimitry Andric       DS.Finish(Actions, Actions.getASTContext().getPrintingPolicy());
60600b57cec5SDimitry Andric       if (EndLoc.isValid())
60610b57cec5SDimitry Andric         DS.SetRangeEnd(EndLoc);
60620b57cec5SDimitry Andric       return;
60630b57cec5SDimitry Andric     }
60640b57cec5SDimitry Andric 
60650b57cec5SDimitry Andric     // If the specifier combination wasn't legal, issue a diagnostic.
60660b57cec5SDimitry Andric     if (isInvalid) {
60670b57cec5SDimitry Andric       assert(PrevSpec && "Method did not return previous specifier!");
60680b57cec5SDimitry Andric       Diag(Tok, DiagID) << PrevSpec;
60690b57cec5SDimitry Andric     }
60700b57cec5SDimitry Andric     EndLoc = ConsumeToken();
60710b57cec5SDimitry Andric   }
60720b57cec5SDimitry Andric }
60730b57cec5SDimitry Andric 
60740b57cec5SDimitry Andric /// ParseDeclarator - Parse and verify a newly-initialized declarator.
60750b57cec5SDimitry Andric void Parser::ParseDeclarator(Declarator &D) {
60760b57cec5SDimitry Andric   /// This implements the 'declarator' production in the C grammar, then checks
60770b57cec5SDimitry Andric   /// for well-formedness and issues diagnostics.
607881ad6265SDimitry Andric   Actions.runWithSufficientStackSpace(D.getBeginLoc(), [&] {
60790b57cec5SDimitry Andric     ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
608081ad6265SDimitry Andric   });
60810b57cec5SDimitry Andric }
60820b57cec5SDimitry Andric 
60830b57cec5SDimitry Andric static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang,
60840b57cec5SDimitry Andric                                DeclaratorContext TheContext) {
60850b57cec5SDimitry Andric   if (Kind == tok::star || Kind == tok::caret)
60860b57cec5SDimitry Andric     return true;
60870b57cec5SDimitry Andric 
60886e75b2fbSDimitry Andric   // OpenCL 2.0 and later define this keyword.
6089349cc55cSDimitry Andric   if (Kind == tok::kw_pipe && Lang.OpenCL &&
6090349cc55cSDimitry Andric       Lang.getOpenCLCompatibleVersion() >= 200)
60910b57cec5SDimitry Andric     return true;
60920b57cec5SDimitry Andric 
60930b57cec5SDimitry Andric   if (!Lang.CPlusPlus)
60940b57cec5SDimitry Andric     return false;
60950b57cec5SDimitry Andric 
60960b57cec5SDimitry Andric   if (Kind == tok::amp)
60970b57cec5SDimitry Andric     return true;
60980b57cec5SDimitry Andric 
60990b57cec5SDimitry Andric   // We parse rvalue refs in C++03, because otherwise the errors are scary.
61000b57cec5SDimitry Andric   // But we must not parse them in conversion-type-ids and new-type-ids, since
61010b57cec5SDimitry Andric   // those can be legitimately followed by a && operator.
61020b57cec5SDimitry Andric   // (The same thing can in theory happen after a trailing-return-type, but
61030b57cec5SDimitry Andric   // since those are a C++11 feature, there is no rejects-valid issue there.)
61040b57cec5SDimitry Andric   if (Kind == tok::ampamp)
6105e8d8bef9SDimitry Andric     return Lang.CPlusPlus11 || (TheContext != DeclaratorContext::ConversionId &&
6106e8d8bef9SDimitry Andric                                 TheContext != DeclaratorContext::CXXNew);
61070b57cec5SDimitry Andric 
61080b57cec5SDimitry Andric   return false;
61090b57cec5SDimitry Andric }
61100b57cec5SDimitry Andric 
61110b57cec5SDimitry Andric // Indicates whether the given declarator is a pipe declarator.
611281ad6265SDimitry Andric static bool isPipeDeclarator(const Declarator &D) {
61130b57cec5SDimitry Andric   const unsigned NumTypes = D.getNumTypeObjects();
61140b57cec5SDimitry Andric 
61150b57cec5SDimitry Andric   for (unsigned Idx = 0; Idx != NumTypes; ++Idx)
61160b57cec5SDimitry Andric     if (DeclaratorChunk::Pipe == D.getTypeObject(Idx).Kind)
61170b57cec5SDimitry Andric       return true;
61180b57cec5SDimitry Andric 
61190b57cec5SDimitry Andric   return false;
61200b57cec5SDimitry Andric }
61210b57cec5SDimitry Andric 
61220b57cec5SDimitry Andric /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
61230b57cec5SDimitry Andric /// is parsed by the function passed to it. Pass null, and the direct-declarator
61240b57cec5SDimitry Andric /// isn't parsed at all, making this function effectively parse the C++
61250b57cec5SDimitry Andric /// ptr-operator production.
61260b57cec5SDimitry Andric ///
61270b57cec5SDimitry Andric /// If the grammar of this construct is extended, matching changes must also be
61280b57cec5SDimitry Andric /// made to TryParseDeclarator and MightBeDeclarator, and possibly to
61290b57cec5SDimitry Andric /// isConstructorDeclarator.
61300b57cec5SDimitry Andric ///
61310b57cec5SDimitry Andric ///       declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
61320b57cec5SDimitry Andric /// [C]     pointer[opt] direct-declarator
61330b57cec5SDimitry Andric /// [C++]   direct-declarator
61340b57cec5SDimitry Andric /// [C++]   ptr-operator declarator
61350b57cec5SDimitry Andric ///
61360b57cec5SDimitry Andric ///       pointer: [C99 6.7.5]
61370b57cec5SDimitry Andric ///         '*' type-qualifier-list[opt]
61380b57cec5SDimitry Andric ///         '*' type-qualifier-list[opt] pointer
61390b57cec5SDimitry Andric ///
61400b57cec5SDimitry Andric ///       ptr-operator:
61410b57cec5SDimitry Andric ///         '*' cv-qualifier-seq[opt]
61420b57cec5SDimitry Andric ///         '&'
61430b57cec5SDimitry Andric /// [C++0x] '&&'
61440b57cec5SDimitry Andric /// [GNU]   '&' restrict[opt] attributes[opt]
61450b57cec5SDimitry Andric /// [GNU?]  '&&' restrict[opt] attributes[opt]
61460b57cec5SDimitry Andric ///         '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
61470b57cec5SDimitry Andric void Parser::ParseDeclaratorInternal(Declarator &D,
61480b57cec5SDimitry Andric                                      DirectDeclParseFunction DirectDeclParser) {
61490b57cec5SDimitry Andric   if (Diags.hasAllExtensionsSilenced())
61500b57cec5SDimitry Andric     D.setExtension();
61510b57cec5SDimitry Andric 
61520b57cec5SDimitry Andric   // C++ member pointers start with a '::' or a nested-name.
61530b57cec5SDimitry Andric   // Member pointers get special handling, since there's no place for the
61540b57cec5SDimitry Andric   // scope spec in the generic path below.
61550b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus &&
61560b57cec5SDimitry Andric       (Tok.is(tok::coloncolon) || Tok.is(tok::kw_decltype) ||
61570b57cec5SDimitry Andric        (Tok.is(tok::identifier) &&
61580b57cec5SDimitry Andric         (NextToken().is(tok::coloncolon) || NextToken().is(tok::less))) ||
61590b57cec5SDimitry Andric        Tok.is(tok::annot_cxxscope))) {
6160e8d8bef9SDimitry Andric     bool EnteringContext = D.getContext() == DeclaratorContext::File ||
6161e8d8bef9SDimitry Andric                            D.getContext() == DeclaratorContext::Member;
61620b57cec5SDimitry Andric     CXXScopeSpec SS;
6163*06c3fb27SDimitry Andric     SS.setTemplateParamLists(D.getTemplateParameterLists());
61645ffd83dbSDimitry Andric     ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
616504eeddc0SDimitry Andric                                    /*ObjectHasErrors=*/false, EnteringContext);
61660b57cec5SDimitry Andric 
61670b57cec5SDimitry Andric     if (SS.isNotEmpty()) {
61680b57cec5SDimitry Andric       if (Tok.isNot(tok::star)) {
61690b57cec5SDimitry Andric         // The scope spec really belongs to the direct-declarator.
61700b57cec5SDimitry Andric         if (D.mayHaveIdentifier())
61710b57cec5SDimitry Andric           D.getCXXScopeSpec() = SS;
61720b57cec5SDimitry Andric         else
61730b57cec5SDimitry Andric           AnnotateScopeToken(SS, true);
61740b57cec5SDimitry Andric 
61750b57cec5SDimitry Andric         if (DirectDeclParser)
61760b57cec5SDimitry Andric           (this->*DirectDeclParser)(D);
61770b57cec5SDimitry Andric         return;
61780b57cec5SDimitry Andric       }
61790b57cec5SDimitry Andric 
6180e8d8bef9SDimitry Andric       if (SS.isValid()) {
6181e8d8bef9SDimitry Andric         checkCompoundToken(SS.getEndLoc(), tok::coloncolon,
6182e8d8bef9SDimitry Andric                            CompoundToken::MemberPtr);
6183e8d8bef9SDimitry Andric       }
6184e8d8bef9SDimitry Andric 
61855ffd83dbSDimitry Andric       SourceLocation StarLoc = ConsumeToken();
61865ffd83dbSDimitry Andric       D.SetRangeEnd(StarLoc);
61870b57cec5SDimitry Andric       DeclSpec DS(AttrFactory);
61880b57cec5SDimitry Andric       ParseTypeQualifierListOpt(DS);
61890b57cec5SDimitry Andric       D.ExtendWithDeclSpec(DS);
61900b57cec5SDimitry Andric 
61910b57cec5SDimitry Andric       // Recurse to parse whatever is left.
619281ad6265SDimitry Andric       Actions.runWithSufficientStackSpace(D.getBeginLoc(), [&] {
61930b57cec5SDimitry Andric         ParseDeclaratorInternal(D, DirectDeclParser);
619481ad6265SDimitry Andric       });
61950b57cec5SDimitry Andric 
61960b57cec5SDimitry Andric       // Sema will have to catch (syntactically invalid) pointers into global
61970b57cec5SDimitry Andric       // scope. It has to catch pointers into namespace scope anyway.
61980b57cec5SDimitry Andric       D.AddTypeInfo(DeclaratorChunk::getMemberPointer(
61995ffd83dbSDimitry Andric                         SS, DS.getTypeQualifiers(), StarLoc, DS.getEndLoc()),
62000b57cec5SDimitry Andric                     std::move(DS.getAttributes()),
62010b57cec5SDimitry Andric                     /* Don't replace range end. */ SourceLocation());
62020b57cec5SDimitry Andric       return;
62030b57cec5SDimitry Andric     }
62040b57cec5SDimitry Andric   }
62050b57cec5SDimitry Andric 
62060b57cec5SDimitry Andric   tok::TokenKind Kind = Tok.getKind();
62070b57cec5SDimitry Andric 
620881ad6265SDimitry Andric   if (D.getDeclSpec().isTypeSpecPipe() && !isPipeDeclarator(D)) {
62090b57cec5SDimitry Andric     DeclSpec DS(AttrFactory);
62100b57cec5SDimitry Andric     ParseTypeQualifierListOpt(DS);
62110b57cec5SDimitry Andric 
62120b57cec5SDimitry Andric     D.AddTypeInfo(
62130b57cec5SDimitry Andric         DeclaratorChunk::getPipe(DS.getTypeQualifiers(), DS.getPipeLoc()),
62140b57cec5SDimitry Andric         std::move(DS.getAttributes()), SourceLocation());
62150b57cec5SDimitry Andric   }
62160b57cec5SDimitry Andric 
62170b57cec5SDimitry Andric   // Not a pointer, C++ reference, or block.
62180b57cec5SDimitry Andric   if (!isPtrOperatorToken(Kind, getLangOpts(), D.getContext())) {
62190b57cec5SDimitry Andric     if (DirectDeclParser)
62200b57cec5SDimitry Andric       (this->*DirectDeclParser)(D);
62210b57cec5SDimitry Andric     return;
62220b57cec5SDimitry Andric   }
62230b57cec5SDimitry Andric 
62240b57cec5SDimitry Andric   // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
62250b57cec5SDimitry Andric   // '&&' -> rvalue reference
62260b57cec5SDimitry Andric   SourceLocation Loc = ConsumeToken();  // Eat the *, ^, & or &&.
62270b57cec5SDimitry Andric   D.SetRangeEnd(Loc);
62280b57cec5SDimitry Andric 
62290b57cec5SDimitry Andric   if (Kind == tok::star || Kind == tok::caret) {
62300b57cec5SDimitry Andric     // Is a pointer.
62310b57cec5SDimitry Andric     DeclSpec DS(AttrFactory);
62320b57cec5SDimitry Andric 
62330b57cec5SDimitry Andric     // GNU attributes are not allowed here in a new-type-id, but Declspec and
62340b57cec5SDimitry Andric     // C++11 attributes are allowed.
62350b57cec5SDimitry Andric     unsigned Reqs = AR_CXX11AttributesParsed | AR_DeclspecAttributesParsed |
6236e8d8bef9SDimitry Andric                     ((D.getContext() != DeclaratorContext::CXXNew)
62370b57cec5SDimitry Andric                          ? AR_GNUAttributesParsed
62380b57cec5SDimitry Andric                          : AR_GNUAttributesParsedAndRejected);
62390b57cec5SDimitry Andric     ParseTypeQualifierListOpt(DS, Reqs, true, !D.mayOmitIdentifier());
62400b57cec5SDimitry Andric     D.ExtendWithDeclSpec(DS);
62410b57cec5SDimitry Andric 
62420b57cec5SDimitry Andric     // Recursively parse the declarator.
624381ad6265SDimitry Andric     Actions.runWithSufficientStackSpace(
624481ad6265SDimitry Andric         D.getBeginLoc(), [&] { ParseDeclaratorInternal(D, DirectDeclParser); });
62450b57cec5SDimitry Andric     if (Kind == tok::star)
62460b57cec5SDimitry Andric       // Remember that we parsed a pointer type, and remember the type-quals.
62470b57cec5SDimitry Andric       D.AddTypeInfo(DeclaratorChunk::getPointer(
62480b57cec5SDimitry Andric                         DS.getTypeQualifiers(), Loc, DS.getConstSpecLoc(),
62490b57cec5SDimitry Andric                         DS.getVolatileSpecLoc(), DS.getRestrictSpecLoc(),
62500b57cec5SDimitry Andric                         DS.getAtomicSpecLoc(), DS.getUnalignedSpecLoc()),
62510b57cec5SDimitry Andric                     std::move(DS.getAttributes()), SourceLocation());
62520b57cec5SDimitry Andric     else
62530b57cec5SDimitry Andric       // Remember that we parsed a Block type, and remember the type-quals.
62540b57cec5SDimitry Andric       D.AddTypeInfo(
62550b57cec5SDimitry Andric           DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), Loc),
62560b57cec5SDimitry Andric           std::move(DS.getAttributes()), SourceLocation());
62570b57cec5SDimitry Andric   } else {
62580b57cec5SDimitry Andric     // Is a reference
62590b57cec5SDimitry Andric     DeclSpec DS(AttrFactory);
62600b57cec5SDimitry Andric 
62610b57cec5SDimitry Andric     // Complain about rvalue references in C++03, but then go on and build
62620b57cec5SDimitry Andric     // the declarator.
62630b57cec5SDimitry Andric     if (Kind == tok::ampamp)
62640b57cec5SDimitry Andric       Diag(Loc, getLangOpts().CPlusPlus11 ?
62650b57cec5SDimitry Andric            diag::warn_cxx98_compat_rvalue_reference :
62660b57cec5SDimitry Andric            diag::ext_rvalue_reference);
62670b57cec5SDimitry Andric 
62680b57cec5SDimitry Andric     // GNU-style and C++11 attributes are allowed here, as is restrict.
62690b57cec5SDimitry Andric     ParseTypeQualifierListOpt(DS);
62700b57cec5SDimitry Andric     D.ExtendWithDeclSpec(DS);
62710b57cec5SDimitry Andric 
62720b57cec5SDimitry Andric     // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
62730b57cec5SDimitry Andric     // cv-qualifiers are introduced through the use of a typedef or of a
62740b57cec5SDimitry Andric     // template type argument, in which case the cv-qualifiers are ignored.
62750b57cec5SDimitry Andric     if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
62760b57cec5SDimitry Andric       if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
62770b57cec5SDimitry Andric         Diag(DS.getConstSpecLoc(),
62780b57cec5SDimitry Andric              diag::err_invalid_reference_qualifier_application) << "const";
62790b57cec5SDimitry Andric       if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
62800b57cec5SDimitry Andric         Diag(DS.getVolatileSpecLoc(),
62810b57cec5SDimitry Andric              diag::err_invalid_reference_qualifier_application) << "volatile";
62820b57cec5SDimitry Andric       // 'restrict' is permitted as an extension.
62830b57cec5SDimitry Andric       if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
62840b57cec5SDimitry Andric         Diag(DS.getAtomicSpecLoc(),
62850b57cec5SDimitry Andric              diag::err_invalid_reference_qualifier_application) << "_Atomic";
62860b57cec5SDimitry Andric     }
62870b57cec5SDimitry Andric 
62880b57cec5SDimitry Andric     // Recursively parse the declarator.
628981ad6265SDimitry Andric     Actions.runWithSufficientStackSpace(
629081ad6265SDimitry Andric         D.getBeginLoc(), [&] { ParseDeclaratorInternal(D, DirectDeclParser); });
62910b57cec5SDimitry Andric 
62920b57cec5SDimitry Andric     if (D.getNumTypeObjects() > 0) {
62930b57cec5SDimitry Andric       // C++ [dcl.ref]p4: There shall be no references to references.
62940b57cec5SDimitry Andric       DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
62950b57cec5SDimitry Andric       if (InnerChunk.Kind == DeclaratorChunk::Reference) {
62960b57cec5SDimitry Andric         if (const IdentifierInfo *II = D.getIdentifier())
62970b57cec5SDimitry Andric           Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
62980b57cec5SDimitry Andric            << II;
62990b57cec5SDimitry Andric         else
63000b57cec5SDimitry Andric           Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
63010b57cec5SDimitry Andric             << "type name";
63020b57cec5SDimitry Andric 
63030b57cec5SDimitry Andric         // Once we've complained about the reference-to-reference, we
63040b57cec5SDimitry Andric         // can go ahead and build the (technically ill-formed)
63050b57cec5SDimitry Andric         // declarator: reference collapsing will take care of it.
63060b57cec5SDimitry Andric       }
63070b57cec5SDimitry Andric     }
63080b57cec5SDimitry Andric 
63090b57cec5SDimitry Andric     // Remember that we parsed a reference type.
63100b57cec5SDimitry Andric     D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
63110b57cec5SDimitry Andric                                                 Kind == tok::amp),
63120b57cec5SDimitry Andric                   std::move(DS.getAttributes()), SourceLocation());
63130b57cec5SDimitry Andric   }
63140b57cec5SDimitry Andric }
63150b57cec5SDimitry Andric 
63160b57cec5SDimitry Andric // When correcting from misplaced brackets before the identifier, the location
63170b57cec5SDimitry Andric // is saved inside the declarator so that other diagnostic messages can use
63180b57cec5SDimitry Andric // them.  This extracts and returns that location, or returns the provided
63190b57cec5SDimitry Andric // location if a stored location does not exist.
63200b57cec5SDimitry Andric static SourceLocation getMissingDeclaratorIdLoc(Declarator &D,
63210b57cec5SDimitry Andric                                                 SourceLocation Loc) {
63220b57cec5SDimitry Andric   if (D.getName().StartLocation.isInvalid() &&
63230b57cec5SDimitry Andric       D.getName().EndLocation.isValid())
63240b57cec5SDimitry Andric     return D.getName().EndLocation;
63250b57cec5SDimitry Andric 
63260b57cec5SDimitry Andric   return Loc;
63270b57cec5SDimitry Andric }
63280b57cec5SDimitry Andric 
63290b57cec5SDimitry Andric /// ParseDirectDeclarator
63300b57cec5SDimitry Andric ///       direct-declarator: [C99 6.7.5]
63310b57cec5SDimitry Andric /// [C99]   identifier
63320b57cec5SDimitry Andric ///         '(' declarator ')'
63330b57cec5SDimitry Andric /// [GNU]   '(' attributes declarator ')'
63340b57cec5SDimitry Andric /// [C90]   direct-declarator '[' constant-expression[opt] ']'
63350b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
63360b57cec5SDimitry Andric /// [C99]   direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
63370b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list 'static' assignment-expr ']'
63380b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list[opt] '*' ']'
63390b57cec5SDimitry Andric /// [C++11] direct-declarator '[' constant-expression[opt] ']'
63400b57cec5SDimitry Andric ///                    attribute-specifier-seq[opt]
63410b57cec5SDimitry Andric ///         direct-declarator '(' parameter-type-list ')'
63420b57cec5SDimitry Andric ///         direct-declarator '(' identifier-list[opt] ')'
63430b57cec5SDimitry Andric /// [GNU]   direct-declarator '(' parameter-forward-declarations
63440b57cec5SDimitry Andric ///                    parameter-type-list[opt] ')'
63450b57cec5SDimitry Andric /// [C++]   direct-declarator '(' parameter-declaration-clause ')'
63460b57cec5SDimitry Andric ///                    cv-qualifier-seq[opt] exception-specification[opt]
63470b57cec5SDimitry Andric /// [C++11] direct-declarator '(' parameter-declaration-clause ')'
63480b57cec5SDimitry Andric ///                    attribute-specifier-seq[opt] cv-qualifier-seq[opt]
63490b57cec5SDimitry Andric ///                    ref-qualifier[opt] exception-specification[opt]
63500b57cec5SDimitry Andric /// [C++]   declarator-id
63510b57cec5SDimitry Andric /// [C++11] declarator-id attribute-specifier-seq[opt]
63520b57cec5SDimitry Andric ///
63530b57cec5SDimitry Andric ///       declarator-id: [C++ 8]
63540b57cec5SDimitry Andric ///         '...'[opt] id-expression
63550b57cec5SDimitry Andric ///         '::'[opt] nested-name-specifier[opt] type-name
63560b57cec5SDimitry Andric ///
63570b57cec5SDimitry Andric ///       id-expression: [C++ 5.1]
63580b57cec5SDimitry Andric ///         unqualified-id
63590b57cec5SDimitry Andric ///         qualified-id
63600b57cec5SDimitry Andric ///
63610b57cec5SDimitry Andric ///       unqualified-id: [C++ 5.1]
63620b57cec5SDimitry Andric ///         identifier
63630b57cec5SDimitry Andric ///         operator-function-id
63640b57cec5SDimitry Andric ///         conversion-function-id
63650b57cec5SDimitry Andric ///          '~' class-name
63660b57cec5SDimitry Andric ///         template-id
63670b57cec5SDimitry Andric ///
63680b57cec5SDimitry Andric /// C++17 adds the following, which we also handle here:
63690b57cec5SDimitry Andric ///
63700b57cec5SDimitry Andric ///       simple-declaration:
63710b57cec5SDimitry Andric ///         <decl-spec> '[' identifier-list ']' brace-or-equal-initializer ';'
63720b57cec5SDimitry Andric ///
63730b57cec5SDimitry Andric /// Note, any additional constructs added here may need corresponding changes
63740b57cec5SDimitry Andric /// in isConstructorDeclarator.
63750b57cec5SDimitry Andric void Parser::ParseDirectDeclarator(Declarator &D) {
63760b57cec5SDimitry Andric   DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
63770b57cec5SDimitry Andric 
63780b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
63790b57cec5SDimitry Andric     // This might be a C++17 structured binding.
63800b57cec5SDimitry Andric     if (Tok.is(tok::l_square) && !D.mayOmitIdentifier() &&
63810b57cec5SDimitry Andric         D.getCXXScopeSpec().isEmpty())
63820b57cec5SDimitry Andric       return ParseDecompositionDeclarator(D);
63830b57cec5SDimitry Andric 
63840b57cec5SDimitry Andric     // Don't parse FOO:BAR as if it were a typo for FOO::BAR inside a class, in
63850b57cec5SDimitry Andric     // this context it is a bitfield. Also in range-based for statement colon
63860b57cec5SDimitry Andric     // may delimit for-range-declaration.
63870b57cec5SDimitry Andric     ColonProtectionRAIIObject X(
6388e8d8bef9SDimitry Andric         *this, D.getContext() == DeclaratorContext::Member ||
6389e8d8bef9SDimitry Andric                    (D.getContext() == DeclaratorContext::ForInit &&
63900b57cec5SDimitry Andric                     getLangOpts().CPlusPlus11));
63910b57cec5SDimitry Andric 
63920b57cec5SDimitry Andric     // ParseDeclaratorInternal might already have parsed the scope.
63930b57cec5SDimitry Andric     if (D.getCXXScopeSpec().isEmpty()) {
6394e8d8bef9SDimitry Andric       bool EnteringContext = D.getContext() == DeclaratorContext::File ||
6395e8d8bef9SDimitry Andric                              D.getContext() == DeclaratorContext::Member;
63965ffd83dbSDimitry Andric       ParseOptionalCXXScopeSpecifier(
63975ffd83dbSDimitry Andric           D.getCXXScopeSpec(), /*ObjectType=*/nullptr,
639804eeddc0SDimitry Andric           /*ObjectHasErrors=*/false, EnteringContext);
63990b57cec5SDimitry Andric     }
64000b57cec5SDimitry Andric 
64010b57cec5SDimitry Andric     if (D.getCXXScopeSpec().isValid()) {
64020b57cec5SDimitry Andric       if (Actions.ShouldEnterDeclaratorScope(getCurScope(),
64030b57cec5SDimitry Andric                                              D.getCXXScopeSpec()))
64040b57cec5SDimitry Andric         // Change the declaration context for name lookup, until this function
64050b57cec5SDimitry Andric         // is exited (and the declarator has been parsed).
64060b57cec5SDimitry Andric         DeclScopeObj.EnterDeclaratorScope();
64070b57cec5SDimitry Andric       else if (getObjCDeclContext()) {
64080b57cec5SDimitry Andric         // Ensure that we don't interpret the next token as an identifier when
64090b57cec5SDimitry Andric         // dealing with declarations in an Objective-C container.
64100b57cec5SDimitry Andric         D.SetIdentifier(nullptr, Tok.getLocation());
64110b57cec5SDimitry Andric         D.setInvalidType(true);
64120b57cec5SDimitry Andric         ConsumeToken();
64130b57cec5SDimitry Andric         goto PastIdentifier;
64140b57cec5SDimitry Andric       }
64150b57cec5SDimitry Andric     }
64160b57cec5SDimitry Andric 
64170b57cec5SDimitry Andric     // C++0x [dcl.fct]p14:
64180b57cec5SDimitry Andric     //   There is a syntactic ambiguity when an ellipsis occurs at the end of a
64190b57cec5SDimitry Andric     //   parameter-declaration-clause without a preceding comma. In this case,
64200b57cec5SDimitry Andric     //   the ellipsis is parsed as part of the abstract-declarator if the type
64210b57cec5SDimitry Andric     //   of the parameter either names a template parameter pack that has not
64220b57cec5SDimitry Andric     //   been expanded or contains auto; otherwise, it is parsed as part of the
64230b57cec5SDimitry Andric     //   parameter-declaration-clause.
64240b57cec5SDimitry Andric     if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
6425e8d8bef9SDimitry Andric         !((D.getContext() == DeclaratorContext::Prototype ||
6426e8d8bef9SDimitry Andric            D.getContext() == DeclaratorContext::LambdaExprParameter ||
6427e8d8bef9SDimitry Andric            D.getContext() == DeclaratorContext::BlockLiteral) &&
6428e8d8bef9SDimitry Andric           NextToken().is(tok::r_paren) && !D.hasGroupingParens() &&
64290b57cec5SDimitry Andric           !Actions.containsUnexpandedParameterPacks(D) &&
64300b57cec5SDimitry Andric           D.getDeclSpec().getTypeSpecType() != TST_auto)) {
64310b57cec5SDimitry Andric       SourceLocation EllipsisLoc = ConsumeToken();
64320b57cec5SDimitry Andric       if (isPtrOperatorToken(Tok.getKind(), getLangOpts(), D.getContext())) {
64330b57cec5SDimitry Andric         // The ellipsis was put in the wrong place. Recover, and explain to
64340b57cec5SDimitry Andric         // the user what they should have done.
64350b57cec5SDimitry Andric         ParseDeclarator(D);
64360b57cec5SDimitry Andric         if (EllipsisLoc.isValid())
64370b57cec5SDimitry Andric           DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
64380b57cec5SDimitry Andric         return;
64390b57cec5SDimitry Andric       } else
64400b57cec5SDimitry Andric         D.setEllipsisLoc(EllipsisLoc);
64410b57cec5SDimitry Andric 
64420b57cec5SDimitry Andric       // The ellipsis can't be followed by a parenthesized declarator. We
64430b57cec5SDimitry Andric       // check for that in ParseParenDeclarator, after we have disambiguated
64440b57cec5SDimitry Andric       // the l_paren token.
64450b57cec5SDimitry Andric     }
64460b57cec5SDimitry Andric 
64470b57cec5SDimitry Andric     if (Tok.isOneOf(tok::identifier, tok::kw_operator, tok::annot_template_id,
64480b57cec5SDimitry Andric                     tok::tilde)) {
64490b57cec5SDimitry Andric       // We found something that indicates the start of an unqualified-id.
64500b57cec5SDimitry Andric       // Parse that unqualified-id.
64510b57cec5SDimitry Andric       bool AllowConstructorName;
64520b57cec5SDimitry Andric       bool AllowDeductionGuide;
64530b57cec5SDimitry Andric       if (D.getDeclSpec().hasTypeSpecifier()) {
64540b57cec5SDimitry Andric         AllowConstructorName = false;
64550b57cec5SDimitry Andric         AllowDeductionGuide = false;
64560b57cec5SDimitry Andric       } else if (D.getCXXScopeSpec().isSet()) {
6457e8d8bef9SDimitry Andric         AllowConstructorName = (D.getContext() == DeclaratorContext::File ||
6458e8d8bef9SDimitry Andric                                 D.getContext() == DeclaratorContext::Member);
64590b57cec5SDimitry Andric         AllowDeductionGuide = false;
64600b57cec5SDimitry Andric       } else {
6461e8d8bef9SDimitry Andric         AllowConstructorName = (D.getContext() == DeclaratorContext::Member);
6462e8d8bef9SDimitry Andric         AllowDeductionGuide = (D.getContext() == DeclaratorContext::File ||
6463e8d8bef9SDimitry Andric                                D.getContext() == DeclaratorContext::Member);
64640b57cec5SDimitry Andric       }
64650b57cec5SDimitry Andric 
64660b57cec5SDimitry Andric       bool HadScope = D.getCXXScopeSpec().isValid();
64670b57cec5SDimitry Andric       if (ParseUnqualifiedId(D.getCXXScopeSpec(),
64685ffd83dbSDimitry Andric                              /*ObjectType=*/nullptr,
64695ffd83dbSDimitry Andric                              /*ObjectHadErrors=*/false,
64700b57cec5SDimitry Andric                              /*EnteringContext=*/true,
64710b57cec5SDimitry Andric                              /*AllowDestructorName=*/true, AllowConstructorName,
64725ffd83dbSDimitry Andric                              AllowDeductionGuide, nullptr, D.getName()) ||
64730b57cec5SDimitry Andric           // Once we're past the identifier, if the scope was bad, mark the
64740b57cec5SDimitry Andric           // whole declarator bad.
64750b57cec5SDimitry Andric           D.getCXXScopeSpec().isInvalid()) {
64760b57cec5SDimitry Andric         D.SetIdentifier(nullptr, Tok.getLocation());
64770b57cec5SDimitry Andric         D.setInvalidType(true);
64780b57cec5SDimitry Andric       } else {
64790b57cec5SDimitry Andric         // ParseUnqualifiedId might have parsed a scope specifier during error
64800b57cec5SDimitry Andric         // recovery. If it did so, enter that scope.
64810b57cec5SDimitry Andric         if (!HadScope && D.getCXXScopeSpec().isValid() &&
64820b57cec5SDimitry Andric             Actions.ShouldEnterDeclaratorScope(getCurScope(),
64830b57cec5SDimitry Andric                                                D.getCXXScopeSpec()))
64840b57cec5SDimitry Andric           DeclScopeObj.EnterDeclaratorScope();
64850b57cec5SDimitry Andric 
64860b57cec5SDimitry Andric         // Parsed the unqualified-id; update range information and move along.
64870b57cec5SDimitry Andric         if (D.getSourceRange().getBegin().isInvalid())
64880b57cec5SDimitry Andric           D.SetRangeBegin(D.getName().getSourceRange().getBegin());
64890b57cec5SDimitry Andric         D.SetRangeEnd(D.getName().getSourceRange().getEnd());
64900b57cec5SDimitry Andric       }
64910b57cec5SDimitry Andric       goto PastIdentifier;
64920b57cec5SDimitry Andric     }
64930b57cec5SDimitry Andric 
64940b57cec5SDimitry Andric     if (D.getCXXScopeSpec().isNotEmpty()) {
64950b57cec5SDimitry Andric       // We have a scope specifier but no following unqualified-id.
64960b57cec5SDimitry Andric       Diag(PP.getLocForEndOfToken(D.getCXXScopeSpec().getEndLoc()),
64970b57cec5SDimitry Andric            diag::err_expected_unqualified_id)
64980b57cec5SDimitry Andric           << /*C++*/1;
64990b57cec5SDimitry Andric       D.SetIdentifier(nullptr, Tok.getLocation());
65000b57cec5SDimitry Andric       goto PastIdentifier;
65010b57cec5SDimitry Andric     }
65020b57cec5SDimitry Andric   } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
65030b57cec5SDimitry Andric     assert(!getLangOpts().CPlusPlus &&
65040b57cec5SDimitry Andric            "There's a C++-specific check for tok::identifier above");
65050b57cec5SDimitry Andric     assert(Tok.getIdentifierInfo() && "Not an identifier?");
65060b57cec5SDimitry Andric     D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
65070b57cec5SDimitry Andric     D.SetRangeEnd(Tok.getLocation());
65080b57cec5SDimitry Andric     ConsumeToken();
65090b57cec5SDimitry Andric     goto PastIdentifier;
65100b57cec5SDimitry Andric   } else if (Tok.is(tok::identifier) && !D.mayHaveIdentifier()) {
65110b57cec5SDimitry Andric     // We're not allowed an identifier here, but we got one. Try to figure out
65120b57cec5SDimitry Andric     // if the user was trying to attach a name to the type, or whether the name
65130b57cec5SDimitry Andric     // is some unrelated trailing syntax.
65140b57cec5SDimitry Andric     bool DiagnoseIdentifier = false;
65150b57cec5SDimitry Andric     if (D.hasGroupingParens())
65160b57cec5SDimitry Andric       // An identifier within parens is unlikely to be intended to be anything
65170b57cec5SDimitry Andric       // other than a name being "declared".
65180b57cec5SDimitry Andric       DiagnoseIdentifier = true;
6519e8d8bef9SDimitry Andric     else if (D.getContext() == DeclaratorContext::TemplateArg)
65200b57cec5SDimitry Andric       // T<int N> is an accidental identifier; T<int N indicates a missing '>'.
65210b57cec5SDimitry Andric       DiagnoseIdentifier =
65220b57cec5SDimitry Andric           NextToken().isOneOf(tok::comma, tok::greater, tok::greatergreater);
6523e8d8bef9SDimitry Andric     else if (D.getContext() == DeclaratorContext::AliasDecl ||
6524e8d8bef9SDimitry Andric              D.getContext() == DeclaratorContext::AliasTemplate)
65250b57cec5SDimitry Andric       // The most likely error is that the ';' was forgotten.
65260b57cec5SDimitry Andric       DiagnoseIdentifier = NextToken().isOneOf(tok::comma, tok::semi);
6527e8d8bef9SDimitry Andric     else if ((D.getContext() == DeclaratorContext::TrailingReturn ||
6528e8d8bef9SDimitry Andric               D.getContext() == DeclaratorContext::TrailingReturnVar) &&
65290b57cec5SDimitry Andric              !isCXX11VirtSpecifier(Tok))
65300b57cec5SDimitry Andric       DiagnoseIdentifier = NextToken().isOneOf(
65310b57cec5SDimitry Andric           tok::comma, tok::semi, tok::equal, tok::l_brace, tok::kw_try);
65320b57cec5SDimitry Andric     if (DiagnoseIdentifier) {
65330b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
65340b57cec5SDimitry Andric         << FixItHint::CreateRemoval(Tok.getLocation());
65350b57cec5SDimitry Andric       D.SetIdentifier(nullptr, Tok.getLocation());
65360b57cec5SDimitry Andric       ConsumeToken();
65370b57cec5SDimitry Andric       goto PastIdentifier;
65380b57cec5SDimitry Andric     }
65390b57cec5SDimitry Andric   }
65400b57cec5SDimitry Andric 
65410b57cec5SDimitry Andric   if (Tok.is(tok::l_paren)) {
65420b57cec5SDimitry Andric     // If this might be an abstract-declarator followed by a direct-initializer,
65430b57cec5SDimitry Andric     // check whether this is a valid declarator chunk. If it can't be, assume
65440b57cec5SDimitry Andric     // that it's an initializer instead.
65450b57cec5SDimitry Andric     if (D.mayOmitIdentifier() && D.mayBeFollowedByCXXDirectInit()) {
65460b57cec5SDimitry Andric       RevertingTentativeParsingAction PA(*this);
6547*06c3fb27SDimitry Andric       if (TryParseDeclarator(true, D.mayHaveIdentifier(), true,
6548*06c3fb27SDimitry Andric                              D.getDeclSpec().getTypeSpecType() == TST_auto) ==
65490b57cec5SDimitry Andric           TPResult::False) {
65500b57cec5SDimitry Andric         D.SetIdentifier(nullptr, Tok.getLocation());
65510b57cec5SDimitry Andric         goto PastIdentifier;
65520b57cec5SDimitry Andric       }
65530b57cec5SDimitry Andric     }
65540b57cec5SDimitry Andric 
65550b57cec5SDimitry Andric     // direct-declarator: '(' declarator ')'
65560b57cec5SDimitry Andric     // direct-declarator: '(' attributes declarator ')'
65570b57cec5SDimitry Andric     // Example: 'char (*X)'   or 'int (*XX)(void)'
65580b57cec5SDimitry Andric     ParseParenDeclarator(D);
65590b57cec5SDimitry Andric 
65600b57cec5SDimitry Andric     // If the declarator was parenthesized, we entered the declarator
65610b57cec5SDimitry Andric     // scope when parsing the parenthesized declarator, then exited
65620b57cec5SDimitry Andric     // the scope already. Re-enter the scope, if we need to.
65630b57cec5SDimitry Andric     if (D.getCXXScopeSpec().isSet()) {
65640b57cec5SDimitry Andric       // If there was an error parsing parenthesized declarator, declarator
65650b57cec5SDimitry Andric       // scope may have been entered before. Don't do it again.
65660b57cec5SDimitry Andric       if (!D.isInvalidType() &&
65670b57cec5SDimitry Andric           Actions.ShouldEnterDeclaratorScope(getCurScope(),
65680b57cec5SDimitry Andric                                              D.getCXXScopeSpec()))
65690b57cec5SDimitry Andric         // Change the declaration context for name lookup, until this function
65700b57cec5SDimitry Andric         // is exited (and the declarator has been parsed).
65710b57cec5SDimitry Andric         DeclScopeObj.EnterDeclaratorScope();
65720b57cec5SDimitry Andric     }
65730b57cec5SDimitry Andric   } else if (D.mayOmitIdentifier()) {
65740b57cec5SDimitry Andric     // This could be something simple like "int" (in which case the declarator
65750b57cec5SDimitry Andric     // portion is empty), if an abstract-declarator is allowed.
65760b57cec5SDimitry Andric     D.SetIdentifier(nullptr, Tok.getLocation());
65770b57cec5SDimitry Andric 
65780b57cec5SDimitry Andric     // The grammar for abstract-pack-declarator does not allow grouping parens.
65790b57cec5SDimitry Andric     // FIXME: Revisit this once core issue 1488 is resolved.
65800b57cec5SDimitry Andric     if (D.hasEllipsis() && D.hasGroupingParens())
65810b57cec5SDimitry Andric       Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
65820b57cec5SDimitry Andric            diag::ext_abstract_pack_declarator_parens);
65830b57cec5SDimitry Andric   } else {
65840b57cec5SDimitry Andric     if (Tok.getKind() == tok::annot_pragma_parser_crash)
65850b57cec5SDimitry Andric       LLVM_BUILTIN_TRAP;
65860b57cec5SDimitry Andric     if (Tok.is(tok::l_square))
65870b57cec5SDimitry Andric       return ParseMisplacedBracketDeclarator(D);
6588e8d8bef9SDimitry Andric     if (D.getContext() == DeclaratorContext::Member) {
65890b57cec5SDimitry Andric       // Objective-C++: Detect C++ keywords and try to prevent further errors by
65900b57cec5SDimitry Andric       // treating these keyword as valid member names.
65910b57cec5SDimitry Andric       if (getLangOpts().ObjC && getLangOpts().CPlusPlus &&
65920b57cec5SDimitry Andric           Tok.getIdentifierInfo() &&
65930b57cec5SDimitry Andric           Tok.getIdentifierInfo()->isCPlusPlusKeyword(getLangOpts())) {
65940b57cec5SDimitry Andric         Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
65950b57cec5SDimitry Andric              diag::err_expected_member_name_or_semi_objcxx_keyword)
65960b57cec5SDimitry Andric             << Tok.getIdentifierInfo()
65970b57cec5SDimitry Andric             << (D.getDeclSpec().isEmpty() ? SourceRange()
65980b57cec5SDimitry Andric                                           : D.getDeclSpec().getSourceRange());
65990b57cec5SDimitry Andric         D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
66000b57cec5SDimitry Andric         D.SetRangeEnd(Tok.getLocation());
66010b57cec5SDimitry Andric         ConsumeToken();
66020b57cec5SDimitry Andric         goto PastIdentifier;
66030b57cec5SDimitry Andric       }
66040b57cec5SDimitry Andric       Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
66050b57cec5SDimitry Andric            diag::err_expected_member_name_or_semi)
66060b57cec5SDimitry Andric           << (D.getDeclSpec().isEmpty() ? SourceRange()
66070b57cec5SDimitry Andric                                         : D.getDeclSpec().getSourceRange());
6608972a253aSDimitry Andric     } else {
6609972a253aSDimitry Andric       if (Tok.getKind() == tok::TokenKind::kw_while) {
6610972a253aSDimitry Andric         Diag(Tok, diag::err_while_loop_outside_of_a_function);
66110b57cec5SDimitry Andric       } else if (getLangOpts().CPlusPlus) {
66120b57cec5SDimitry Andric         if (Tok.isOneOf(tok::period, tok::arrow))
66130b57cec5SDimitry Andric           Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
66140b57cec5SDimitry Andric         else {
66150b57cec5SDimitry Andric           SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
66160b57cec5SDimitry Andric           if (Tok.isAtStartOfLine() && Loc.isValid())
66170b57cec5SDimitry Andric             Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
66180b57cec5SDimitry Andric                 << getLangOpts().CPlusPlus;
66190b57cec5SDimitry Andric           else
66200b57cec5SDimitry Andric             Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
66210b57cec5SDimitry Andric                  diag::err_expected_unqualified_id)
66220b57cec5SDimitry Andric                 << getLangOpts().CPlusPlus;
66230b57cec5SDimitry Andric         }
66240b57cec5SDimitry Andric       } else {
66250b57cec5SDimitry Andric         Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
66260b57cec5SDimitry Andric              diag::err_expected_either)
66270b57cec5SDimitry Andric             << tok::identifier << tok::l_paren;
66280b57cec5SDimitry Andric       }
6629972a253aSDimitry Andric     }
66300b57cec5SDimitry Andric     D.SetIdentifier(nullptr, Tok.getLocation());
66310b57cec5SDimitry Andric     D.setInvalidType(true);
66320b57cec5SDimitry Andric   }
66330b57cec5SDimitry Andric 
66340b57cec5SDimitry Andric  PastIdentifier:
66350b57cec5SDimitry Andric   assert(D.isPastIdentifier() &&
66360b57cec5SDimitry Andric          "Haven't past the location of the identifier yet?");
66370b57cec5SDimitry Andric 
66380b57cec5SDimitry Andric   // Don't parse attributes unless we have parsed an unparenthesized name.
66390b57cec5SDimitry Andric   if (D.hasName() && !D.getNumTypeObjects())
66400b57cec5SDimitry Andric     MaybeParseCXX11Attributes(D);
66410b57cec5SDimitry Andric 
664204eeddc0SDimitry Andric   while (true) {
66430b57cec5SDimitry Andric     if (Tok.is(tok::l_paren)) {
664455e4f9d5SDimitry Andric       bool IsFunctionDeclaration = D.isFunctionDeclaratorAFunctionDeclaration();
66450b57cec5SDimitry Andric       // Enter function-declaration scope, limiting any declarators to the
66460b57cec5SDimitry Andric       // function prototype scope, including parameter declarators.
66470b57cec5SDimitry Andric       ParseScope PrototypeScope(this,
66480b57cec5SDimitry Andric                                 Scope::FunctionPrototypeScope|Scope::DeclScope|
664955e4f9d5SDimitry Andric                                 (IsFunctionDeclaration
66500b57cec5SDimitry Andric                                    ? Scope::FunctionDeclarationScope : 0));
66510b57cec5SDimitry Andric 
66520b57cec5SDimitry Andric       // The paren may be part of a C++ direct initializer, eg. "int x(1);".
66530b57cec5SDimitry Andric       // In such a case, check if we actually have a function declarator; if it
66540b57cec5SDimitry Andric       // is not, the declarator has been fully parsed.
66550b57cec5SDimitry Andric       bool IsAmbiguous = false;
66560b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
6657bdd1243dSDimitry Andric         // C++2a [temp.res]p5
6658bdd1243dSDimitry Andric         // A qualified-id is assumed to name a type if
6659bdd1243dSDimitry Andric         //   - [...]
6660bdd1243dSDimitry Andric         //   - it is a decl-specifier of the decl-specifier-seq of a
6661bdd1243dSDimitry Andric         //     - [...]
6662bdd1243dSDimitry Andric         //     - parameter-declaration in a member-declaration [...]
6663bdd1243dSDimitry Andric         //     - parameter-declaration in a declarator of a function or function
6664bdd1243dSDimitry Andric         //       template declaration whose declarator-id is qualified [...]
6665bdd1243dSDimitry Andric         auto AllowImplicitTypename = ImplicitTypenameContext::No;
6666bdd1243dSDimitry Andric         if (D.getCXXScopeSpec().isSet())
6667bdd1243dSDimitry Andric           AllowImplicitTypename =
6668bdd1243dSDimitry Andric               (ImplicitTypenameContext)Actions.isDeclaratorFunctionLike(D);
6669bdd1243dSDimitry Andric         else if (D.getContext() == DeclaratorContext::Member) {
6670bdd1243dSDimitry Andric           AllowImplicitTypename = ImplicitTypenameContext::Yes;
6671bdd1243dSDimitry Andric         }
6672bdd1243dSDimitry Andric 
66730b57cec5SDimitry Andric         // The name of the declarator, if any, is tentatively declared within
66740b57cec5SDimitry Andric         // a possible direct initializer.
66750b57cec5SDimitry Andric         TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
6676bdd1243dSDimitry Andric         bool IsFunctionDecl =
6677bdd1243dSDimitry Andric             isCXXFunctionDeclarator(&IsAmbiguous, AllowImplicitTypename);
66780b57cec5SDimitry Andric         TentativelyDeclaredIdentifiers.pop_back();
66790b57cec5SDimitry Andric         if (!IsFunctionDecl)
66800b57cec5SDimitry Andric           break;
66810b57cec5SDimitry Andric       }
66820b57cec5SDimitry Andric       ParsedAttributes attrs(AttrFactory);
66830b57cec5SDimitry Andric       BalancedDelimiterTracker T(*this, tok::l_paren);
66840b57cec5SDimitry Andric       T.consumeOpen();
668555e4f9d5SDimitry Andric       if (IsFunctionDeclaration)
668655e4f9d5SDimitry Andric         Actions.ActOnStartFunctionDeclarationDeclarator(D,
668755e4f9d5SDimitry Andric                                                         TemplateParameterDepth);
66880b57cec5SDimitry Andric       ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
668955e4f9d5SDimitry Andric       if (IsFunctionDeclaration)
669055e4f9d5SDimitry Andric         Actions.ActOnFinishFunctionDeclarationDeclarator(D);
66910b57cec5SDimitry Andric       PrototypeScope.Exit();
66920b57cec5SDimitry Andric     } else if (Tok.is(tok::l_square)) {
66930b57cec5SDimitry Andric       ParseBracketDeclarator(D);
6694*06c3fb27SDimitry Andric     } else if (Tok.isRegularKeywordAttribute()) {
6695*06c3fb27SDimitry Andric       // For consistency with attribute parsing.
6696*06c3fb27SDimitry Andric       Diag(Tok, diag::err_keyword_not_allowed) << Tok.getIdentifierInfo();
6697*06c3fb27SDimitry Andric       ConsumeToken();
6698480093f4SDimitry Andric     } else if (Tok.is(tok::kw_requires) && D.hasGroupingParens()) {
6699480093f4SDimitry Andric       // This declarator is declaring a function, but the requires clause is
6700480093f4SDimitry Andric       // in the wrong place:
6701480093f4SDimitry Andric       //   void (f() requires true);
6702480093f4SDimitry Andric       // instead of
6703480093f4SDimitry Andric       //   void f() requires true;
6704480093f4SDimitry Andric       // or
6705480093f4SDimitry Andric       //   void (f()) requires true;
6706480093f4SDimitry Andric       Diag(Tok, diag::err_requires_clause_inside_parens);
6707480093f4SDimitry Andric       ConsumeToken();
6708480093f4SDimitry Andric       ExprResult TrailingRequiresClause = Actions.CorrectDelayedTyposInExpr(
6709480093f4SDimitry Andric          ParseConstraintLogicalOrExpression(/*IsTrailingRequiresClause=*/true));
6710480093f4SDimitry Andric       if (TrailingRequiresClause.isUsable() && D.isFunctionDeclarator() &&
6711480093f4SDimitry Andric           !D.hasTrailingRequiresClause())
6712480093f4SDimitry Andric         // We're already ill-formed if we got here but we'll accept it anyway.
6713480093f4SDimitry Andric         D.setTrailingRequiresClause(TrailingRequiresClause.get());
67140b57cec5SDimitry Andric     } else {
67150b57cec5SDimitry Andric       break;
67160b57cec5SDimitry Andric     }
67170b57cec5SDimitry Andric   }
67180b57cec5SDimitry Andric }
67190b57cec5SDimitry Andric 
67200b57cec5SDimitry Andric void Parser::ParseDecompositionDeclarator(Declarator &D) {
67210b57cec5SDimitry Andric   assert(Tok.is(tok::l_square));
67220b57cec5SDimitry Andric 
67230b57cec5SDimitry Andric   // If this doesn't look like a structured binding, maybe it's a misplaced
67240b57cec5SDimitry Andric   // array declarator.
67250b57cec5SDimitry Andric   // FIXME: Consume the l_square first so we don't need extra lookahead for
67260b57cec5SDimitry Andric   // this.
67270b57cec5SDimitry Andric   if (!(NextToken().is(tok::identifier) &&
67280b57cec5SDimitry Andric         GetLookAheadToken(2).isOneOf(tok::comma, tok::r_square)) &&
67290b57cec5SDimitry Andric       !(NextToken().is(tok::r_square) &&
67300b57cec5SDimitry Andric         GetLookAheadToken(2).isOneOf(tok::equal, tok::l_brace)))
67310b57cec5SDimitry Andric     return ParseMisplacedBracketDeclarator(D);
67320b57cec5SDimitry Andric 
67330b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_square);
67340b57cec5SDimitry Andric   T.consumeOpen();
67350b57cec5SDimitry Andric 
67360b57cec5SDimitry Andric   SmallVector<DecompositionDeclarator::Binding, 32> Bindings;
67370b57cec5SDimitry Andric   while (Tok.isNot(tok::r_square)) {
67380b57cec5SDimitry Andric     if (!Bindings.empty()) {
67390b57cec5SDimitry Andric       if (Tok.is(tok::comma))
67400b57cec5SDimitry Andric         ConsumeToken();
67410b57cec5SDimitry Andric       else {
67420b57cec5SDimitry Andric         if (Tok.is(tok::identifier)) {
67430b57cec5SDimitry Andric           SourceLocation EndLoc = getEndOfPreviousToken();
67440b57cec5SDimitry Andric           Diag(EndLoc, diag::err_expected)
67450b57cec5SDimitry Andric               << tok::comma << FixItHint::CreateInsertion(EndLoc, ",");
67460b57cec5SDimitry Andric         } else {
67470b57cec5SDimitry Andric           Diag(Tok, diag::err_expected_comma_or_rsquare);
67480b57cec5SDimitry Andric         }
67490b57cec5SDimitry Andric 
67500b57cec5SDimitry Andric         SkipUntil(tok::r_square, tok::comma, tok::identifier,
67510b57cec5SDimitry Andric                   StopAtSemi | StopBeforeMatch);
67520b57cec5SDimitry Andric         if (Tok.is(tok::comma))
67530b57cec5SDimitry Andric           ConsumeToken();
67540b57cec5SDimitry Andric         else if (Tok.isNot(tok::identifier))
67550b57cec5SDimitry Andric           break;
67560b57cec5SDimitry Andric       }
67570b57cec5SDimitry Andric     }
67580b57cec5SDimitry Andric 
67590b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
67600b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::identifier;
67610b57cec5SDimitry Andric       break;
67620b57cec5SDimitry Andric     }
67630b57cec5SDimitry Andric 
67640b57cec5SDimitry Andric     Bindings.push_back({Tok.getIdentifierInfo(), Tok.getLocation()});
67650b57cec5SDimitry Andric     ConsumeToken();
67660b57cec5SDimitry Andric   }
67670b57cec5SDimitry Andric 
67680b57cec5SDimitry Andric   if (Tok.isNot(tok::r_square))
67690b57cec5SDimitry Andric     // We've already diagnosed a problem here.
67700b57cec5SDimitry Andric     T.skipToEnd();
67710b57cec5SDimitry Andric   else {
67720b57cec5SDimitry Andric     // C++17 does not allow the identifier-list in a structured binding
67730b57cec5SDimitry Andric     // to be empty.
67740b57cec5SDimitry Andric     if (Bindings.empty())
67750b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::ext_decomp_decl_empty);
67760b57cec5SDimitry Andric 
67770b57cec5SDimitry Andric     T.consumeClose();
67780b57cec5SDimitry Andric   }
67790b57cec5SDimitry Andric 
67800b57cec5SDimitry Andric   return D.setDecompositionBindings(T.getOpenLocation(), Bindings,
67810b57cec5SDimitry Andric                                     T.getCloseLocation());
67820b57cec5SDimitry Andric }
67830b57cec5SDimitry Andric 
67840b57cec5SDimitry Andric /// ParseParenDeclarator - We parsed the declarator D up to a paren.  This is
67850b57cec5SDimitry Andric /// only called before the identifier, so these are most likely just grouping
67860b57cec5SDimitry Andric /// parens for precedence.  If we find that these are actually function
67870b57cec5SDimitry Andric /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
67880b57cec5SDimitry Andric ///
67890b57cec5SDimitry Andric ///       direct-declarator:
67900b57cec5SDimitry Andric ///         '(' declarator ')'
67910b57cec5SDimitry Andric /// [GNU]   '(' attributes declarator ')'
67920b57cec5SDimitry Andric ///         direct-declarator '(' parameter-type-list ')'
67930b57cec5SDimitry Andric ///         direct-declarator '(' identifier-list[opt] ')'
67940b57cec5SDimitry Andric /// [GNU]   direct-declarator '(' parameter-forward-declarations
67950b57cec5SDimitry Andric ///                    parameter-type-list[opt] ')'
67960b57cec5SDimitry Andric ///
67970b57cec5SDimitry Andric void Parser::ParseParenDeclarator(Declarator &D) {
67980b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
67990b57cec5SDimitry Andric   T.consumeOpen();
68000b57cec5SDimitry Andric 
68010b57cec5SDimitry Andric   assert(!D.isPastIdentifier() && "Should be called before passing identifier");
68020b57cec5SDimitry Andric 
68030b57cec5SDimitry Andric   // Eat any attributes before we look at whether this is a grouping or function
68040b57cec5SDimitry Andric   // declarator paren.  If this is a grouping paren, the attribute applies to
68050b57cec5SDimitry Andric   // the type being built up, for example:
68060b57cec5SDimitry Andric   //     int (__attribute__(()) *x)(long y)
68070b57cec5SDimitry Andric   // If this ends up not being a grouping paren, the attribute applies to the
68080b57cec5SDimitry Andric   // first argument, for example:
68090b57cec5SDimitry Andric   //     int (__attribute__(()) int x)
68100b57cec5SDimitry Andric   // In either case, we need to eat any attributes to be able to determine what
68110b57cec5SDimitry Andric   // sort of paren this is.
68120b57cec5SDimitry Andric   //
68130b57cec5SDimitry Andric   ParsedAttributes attrs(AttrFactory);
68140b57cec5SDimitry Andric   bool RequiresArg = false;
68150b57cec5SDimitry Andric   if (Tok.is(tok::kw___attribute)) {
68160b57cec5SDimitry Andric     ParseGNUAttributes(attrs);
68170b57cec5SDimitry Andric 
68180b57cec5SDimitry Andric     // We require that the argument list (if this is a non-grouping paren) be
68190b57cec5SDimitry Andric     // present even if the attribute list was empty.
68200b57cec5SDimitry Andric     RequiresArg = true;
68210b57cec5SDimitry Andric   }
68220b57cec5SDimitry Andric 
68230b57cec5SDimitry Andric   // Eat any Microsoft extensions.
68240b57cec5SDimitry Andric   ParseMicrosoftTypeAttributes(attrs);
68250b57cec5SDimitry Andric 
68260b57cec5SDimitry Andric   // Eat any Borland extensions.
68270b57cec5SDimitry Andric   if  (Tok.is(tok::kw___pascal))
68280b57cec5SDimitry Andric     ParseBorlandTypeAttributes(attrs);
68290b57cec5SDimitry Andric 
68300b57cec5SDimitry Andric   // If we haven't past the identifier yet (or where the identifier would be
68310b57cec5SDimitry Andric   // stored, if this is an abstract declarator), then this is probably just
68320b57cec5SDimitry Andric   // grouping parens. However, if this could be an abstract-declarator, then
68330b57cec5SDimitry Andric   // this could also be the start of function arguments (consider 'void()').
68340b57cec5SDimitry Andric   bool isGrouping;
68350b57cec5SDimitry Andric 
68360b57cec5SDimitry Andric   if (!D.mayOmitIdentifier()) {
68370b57cec5SDimitry Andric     // If this can't be an abstract-declarator, this *must* be a grouping
68380b57cec5SDimitry Andric     // paren, because we haven't seen the identifier yet.
68390b57cec5SDimitry Andric     isGrouping = true;
68400b57cec5SDimitry Andric   } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
68410b57cec5SDimitry Andric              (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
68420b57cec5SDimitry Andric               NextToken().is(tok::r_paren)) || // C++ int(...)
6843bdd1243dSDimitry Andric              isDeclarationSpecifier(
6844bdd1243dSDimitry Andric                  ImplicitTypenameContext::No) || // 'int(int)' is a function.
68450b57cec5SDimitry Andric              isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
68460b57cec5SDimitry Andric     // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
68470b57cec5SDimitry Andric     // considered to be a type, not a K&R identifier-list.
68480b57cec5SDimitry Andric     isGrouping = false;
68490b57cec5SDimitry Andric   } else {
68500b57cec5SDimitry Andric     // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
68510b57cec5SDimitry Andric     isGrouping = true;
68520b57cec5SDimitry Andric   }
68530b57cec5SDimitry Andric 
68540b57cec5SDimitry Andric   // If this is a grouping paren, handle:
68550b57cec5SDimitry Andric   // direct-declarator: '(' declarator ')'
68560b57cec5SDimitry Andric   // direct-declarator: '(' attributes declarator ')'
68570b57cec5SDimitry Andric   if (isGrouping) {
68580b57cec5SDimitry Andric     SourceLocation EllipsisLoc = D.getEllipsisLoc();
68590b57cec5SDimitry Andric     D.setEllipsisLoc(SourceLocation());
68600b57cec5SDimitry Andric 
68610b57cec5SDimitry Andric     bool hadGroupingParens = D.hasGroupingParens();
68620b57cec5SDimitry Andric     D.setGroupingParens(true);
68630b57cec5SDimitry Andric     ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
68640b57cec5SDimitry Andric     // Match the ')'.
68650b57cec5SDimitry Andric     T.consumeClose();
68660b57cec5SDimitry Andric     D.AddTypeInfo(
68670b57cec5SDimitry Andric         DeclaratorChunk::getParen(T.getOpenLocation(), T.getCloseLocation()),
68680b57cec5SDimitry Andric         std::move(attrs), T.getCloseLocation());
68690b57cec5SDimitry Andric 
68700b57cec5SDimitry Andric     D.setGroupingParens(hadGroupingParens);
68710b57cec5SDimitry Andric 
68720b57cec5SDimitry Andric     // An ellipsis cannot be placed outside parentheses.
68730b57cec5SDimitry Andric     if (EllipsisLoc.isValid())
68740b57cec5SDimitry Andric       DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
68750b57cec5SDimitry Andric 
68760b57cec5SDimitry Andric     return;
68770b57cec5SDimitry Andric   }
68780b57cec5SDimitry Andric 
68790b57cec5SDimitry Andric   // Okay, if this wasn't a grouping paren, it must be the start of a function
68800b57cec5SDimitry Andric   // argument list.  Recognize that this declarator will never have an
68810b57cec5SDimitry Andric   // identifier (and remember where it would have been), then call into
68820b57cec5SDimitry Andric   // ParseFunctionDeclarator to handle of argument list.
68830b57cec5SDimitry Andric   D.SetIdentifier(nullptr, Tok.getLocation());
68840b57cec5SDimitry Andric 
68850b57cec5SDimitry Andric   // Enter function-declaration scope, limiting any declarators to the
68860b57cec5SDimitry Andric   // function prototype scope, including parameter declarators.
68870b57cec5SDimitry Andric   ParseScope PrototypeScope(this,
68880b57cec5SDimitry Andric                             Scope::FunctionPrototypeScope | Scope::DeclScope |
68890b57cec5SDimitry Andric                             (D.isFunctionDeclaratorAFunctionDeclaration()
68900b57cec5SDimitry Andric                                ? Scope::FunctionDeclarationScope : 0));
68910b57cec5SDimitry Andric   ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
68920b57cec5SDimitry Andric   PrototypeScope.Exit();
68930b57cec5SDimitry Andric }
68940b57cec5SDimitry Andric 
6895480093f4SDimitry Andric void Parser::InitCXXThisScopeForDeclaratorIfRelevant(
6896480093f4SDimitry Andric     const Declarator &D, const DeclSpec &DS,
6897bdd1243dSDimitry Andric     std::optional<Sema::CXXThisScopeRAII> &ThisScope) {
6898480093f4SDimitry Andric   // C++11 [expr.prim.general]p3:
6899480093f4SDimitry Andric   //   If a declaration declares a member function or member function
6900480093f4SDimitry Andric   //   template of a class X, the expression this is a prvalue of type
6901480093f4SDimitry Andric   //   "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
6902480093f4SDimitry Andric   //   and the end of the function-definition, member-declarator, or
6903480093f4SDimitry Andric   //   declarator.
6904480093f4SDimitry Andric   // FIXME: currently, "static" case isn't handled correctly.
6905e8d8bef9SDimitry Andric   bool IsCXX11MemberFunction =
6906e8d8bef9SDimitry Andric       getLangOpts().CPlusPlus11 &&
6907480093f4SDimitry Andric       D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
6908e8d8bef9SDimitry Andric       (D.getContext() == DeclaratorContext::Member
6909480093f4SDimitry Andric            ? !D.getDeclSpec().isFriendSpecified()
6910e8d8bef9SDimitry Andric            : D.getContext() == DeclaratorContext::File &&
6911480093f4SDimitry Andric                  D.getCXXScopeSpec().isValid() &&
6912480093f4SDimitry Andric                  Actions.CurContext->isRecord());
6913480093f4SDimitry Andric   if (!IsCXX11MemberFunction)
6914480093f4SDimitry Andric     return;
6915480093f4SDimitry Andric 
6916480093f4SDimitry Andric   Qualifiers Q = Qualifiers::fromCVRUMask(DS.getTypeQualifiers());
6917480093f4SDimitry Andric   if (D.getDeclSpec().hasConstexprSpecifier() && !getLangOpts().CPlusPlus14)
6918480093f4SDimitry Andric     Q.addConst();
6919480093f4SDimitry Andric   // FIXME: Collect C++ address spaces.
6920480093f4SDimitry Andric   // If there are multiple different address spaces, the source is invalid.
6921480093f4SDimitry Andric   // Carry on using the first addr space for the qualifiers of 'this'.
6922480093f4SDimitry Andric   // The diagnostic will be given later while creating the function
6923480093f4SDimitry Andric   // prototype for the method.
6924480093f4SDimitry Andric   if (getLangOpts().OpenCLCPlusPlus) {
6925480093f4SDimitry Andric     for (ParsedAttr &attr : DS.getAttributes()) {
6926480093f4SDimitry Andric       LangAS ASIdx = attr.asOpenCLLangAS();
6927480093f4SDimitry Andric       if (ASIdx != LangAS::Default) {
6928480093f4SDimitry Andric         Q.addAddressSpace(ASIdx);
6929480093f4SDimitry Andric         break;
6930480093f4SDimitry Andric       }
6931480093f4SDimitry Andric     }
6932480093f4SDimitry Andric   }
6933480093f4SDimitry Andric   ThisScope.emplace(Actions, dyn_cast<CXXRecordDecl>(Actions.CurContext), Q,
6934480093f4SDimitry Andric                     IsCXX11MemberFunction);
6935480093f4SDimitry Andric }
6936480093f4SDimitry Andric 
69370b57cec5SDimitry Andric /// ParseFunctionDeclarator - We are after the identifier and have parsed the
69380b57cec5SDimitry Andric /// declarator D up to a paren, which indicates that we are parsing function
69390b57cec5SDimitry Andric /// arguments.
69400b57cec5SDimitry Andric ///
694181ad6265SDimitry Andric /// If FirstArgAttrs is non-null, then the caller parsed those attributes
694281ad6265SDimitry Andric /// immediately after the open paren - they will be applied to the DeclSpec
694381ad6265SDimitry Andric /// of the first parameter.
69440b57cec5SDimitry Andric ///
69450b57cec5SDimitry Andric /// If RequiresArg is true, then the first argument of the function is required
69460b57cec5SDimitry Andric /// to be present and required to not be an identifier list.
69470b57cec5SDimitry Andric ///
69480b57cec5SDimitry Andric /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
69490b57cec5SDimitry Andric /// (C++11) ref-qualifier[opt], exception-specification[opt],
6950480093f4SDimitry Andric /// (C++11) attribute-specifier-seq[opt], (C++11) trailing-return-type[opt] and
6951480093f4SDimitry Andric /// (C++2a) the trailing requires-clause.
69520b57cec5SDimitry Andric ///
69530b57cec5SDimitry Andric /// [C++11] exception-specification:
69540b57cec5SDimitry Andric ///           dynamic-exception-specification
69550b57cec5SDimitry Andric ///           noexcept-specification
69560b57cec5SDimitry Andric ///
69570b57cec5SDimitry Andric void Parser::ParseFunctionDeclarator(Declarator &D,
69580b57cec5SDimitry Andric                                      ParsedAttributes &FirstArgAttrs,
69590b57cec5SDimitry Andric                                      BalancedDelimiterTracker &Tracker,
69600b57cec5SDimitry Andric                                      bool IsAmbiguous,
69610b57cec5SDimitry Andric                                      bool RequiresArg) {
69620b57cec5SDimitry Andric   assert(getCurScope()->isFunctionPrototypeScope() &&
69630b57cec5SDimitry Andric          "Should call from a Function scope");
69640b57cec5SDimitry Andric   // lparen is already consumed!
69650b57cec5SDimitry Andric   assert(D.isPastIdentifier() && "Should not call before identifier!");
69660b57cec5SDimitry Andric 
69670b57cec5SDimitry Andric   // This should be true when the function has typed arguments.
69680b57cec5SDimitry Andric   // Otherwise, it is treated as a K&R-style function.
69690b57cec5SDimitry Andric   bool HasProto = false;
69700b57cec5SDimitry Andric   // Build up an array of information about the parsed arguments.
69710b57cec5SDimitry Andric   SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
69720b57cec5SDimitry Andric   // Remember where we see an ellipsis, if any.
69730b57cec5SDimitry Andric   SourceLocation EllipsisLoc;
69740b57cec5SDimitry Andric 
69750b57cec5SDimitry Andric   DeclSpec DS(AttrFactory);
69760b57cec5SDimitry Andric   bool RefQualifierIsLValueRef = true;
69770b57cec5SDimitry Andric   SourceLocation RefQualifierLoc;
69780b57cec5SDimitry Andric   ExceptionSpecificationType ESpecType = EST_None;
69790b57cec5SDimitry Andric   SourceRange ESpecRange;
69800b57cec5SDimitry Andric   SmallVector<ParsedType, 2> DynamicExceptions;
69810b57cec5SDimitry Andric   SmallVector<SourceRange, 2> DynamicExceptionRanges;
69820b57cec5SDimitry Andric   ExprResult NoexceptExpr;
69830b57cec5SDimitry Andric   CachedTokens *ExceptionSpecTokens = nullptr;
698481ad6265SDimitry Andric   ParsedAttributes FnAttrs(AttrFactory);
69850b57cec5SDimitry Andric   TypeResult TrailingReturnType;
6986e8d8bef9SDimitry Andric   SourceLocation TrailingReturnTypeLoc;
69870b57cec5SDimitry Andric 
69880b57cec5SDimitry Andric   /* LocalEndLoc is the end location for the local FunctionTypeLoc.
69890b57cec5SDimitry Andric      EndLoc is the end location for the function declarator.
69900b57cec5SDimitry Andric      They differ for trailing return types. */
69910b57cec5SDimitry Andric   SourceLocation StartLoc, LocalEndLoc, EndLoc;
69920b57cec5SDimitry Andric   SourceLocation LParenLoc, RParenLoc;
69930b57cec5SDimitry Andric   LParenLoc = Tracker.getOpenLocation();
69940b57cec5SDimitry Andric   StartLoc = LParenLoc;
69950b57cec5SDimitry Andric 
69960b57cec5SDimitry Andric   if (isFunctionDeclaratorIdentifierList()) {
69970b57cec5SDimitry Andric     if (RequiresArg)
69980b57cec5SDimitry Andric       Diag(Tok, diag::err_argument_required_after_attribute);
69990b57cec5SDimitry Andric 
70000b57cec5SDimitry Andric     ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
70010b57cec5SDimitry Andric 
70020b57cec5SDimitry Andric     Tracker.consumeClose();
70030b57cec5SDimitry Andric     RParenLoc = Tracker.getCloseLocation();
70040b57cec5SDimitry Andric     LocalEndLoc = RParenLoc;
70050b57cec5SDimitry Andric     EndLoc = RParenLoc;
70060b57cec5SDimitry Andric 
70070b57cec5SDimitry Andric     // If there are attributes following the identifier list, parse them and
70080b57cec5SDimitry Andric     // prohibit them.
70090b57cec5SDimitry Andric     MaybeParseCXX11Attributes(FnAttrs);
70100b57cec5SDimitry Andric     ProhibitAttributes(FnAttrs);
70110b57cec5SDimitry Andric   } else {
70120b57cec5SDimitry Andric     if (Tok.isNot(tok::r_paren))
7013bdd1243dSDimitry Andric       ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
70140b57cec5SDimitry Andric     else if (RequiresArg)
70150b57cec5SDimitry Andric       Diag(Tok, diag::err_argument_required_after_attribute);
70160b57cec5SDimitry Andric 
701781ad6265SDimitry Andric     // OpenCL disallows functions without a prototype, but it doesn't enforce
701881ad6265SDimitry Andric     // strict prototypes as in C2x because it allows a function definition to
701981ad6265SDimitry Andric     // have an identifier list. See OpenCL 3.0 6.11/g for more details.
702081ad6265SDimitry Andric     HasProto = ParamInfo.size() || getLangOpts().requiresStrictPrototypes() ||
702181ad6265SDimitry Andric                getLangOpts().OpenCL;
70220b57cec5SDimitry Andric 
70230b57cec5SDimitry Andric     // If we have the closing ')', eat it.
70240b57cec5SDimitry Andric     Tracker.consumeClose();
70250b57cec5SDimitry Andric     RParenLoc = Tracker.getCloseLocation();
70260b57cec5SDimitry Andric     LocalEndLoc = RParenLoc;
70270b57cec5SDimitry Andric     EndLoc = RParenLoc;
70280b57cec5SDimitry Andric 
70290b57cec5SDimitry Andric     if (getLangOpts().CPlusPlus) {
70300b57cec5SDimitry Andric       // FIXME: Accept these components in any order, and produce fixits to
70310b57cec5SDimitry Andric       // correct the order if the user gets it wrong. Ideally we should deal
70320b57cec5SDimitry Andric       // with the pure-specifier in the same way.
70330b57cec5SDimitry Andric 
70340b57cec5SDimitry Andric       // Parse cv-qualifier-seq[opt].
70350b57cec5SDimitry Andric       ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed,
70360b57cec5SDimitry Andric                                 /*AtomicAllowed*/ false,
70370b57cec5SDimitry Andric                                 /*IdentifierRequired=*/false,
70380b57cec5SDimitry Andric                                 llvm::function_ref<void()>([&]() {
70390b57cec5SDimitry Andric                                   Actions.CodeCompleteFunctionQualifiers(DS, D);
70400b57cec5SDimitry Andric                                 }));
70410b57cec5SDimitry Andric       if (!DS.getSourceRange().getEnd().isInvalid()) {
70420b57cec5SDimitry Andric         EndLoc = DS.getSourceRange().getEnd();
70430b57cec5SDimitry Andric       }
70440b57cec5SDimitry Andric 
70450b57cec5SDimitry Andric       // Parse ref-qualifier[opt].
70460b57cec5SDimitry Andric       if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc))
70470b57cec5SDimitry Andric         EndLoc = RefQualifierLoc;
70480b57cec5SDimitry Andric 
7049bdd1243dSDimitry Andric       std::optional<Sema::CXXThisScopeRAII> ThisScope;
7050480093f4SDimitry Andric       InitCXXThisScopeForDeclaratorIfRelevant(D, DS, ThisScope);
70510b57cec5SDimitry Andric 
70520b57cec5SDimitry Andric       // Parse exception-specification[opt].
7053e8d8bef9SDimitry Andric       // FIXME: Per [class.mem]p6, all exception-specifications at class scope
7054e8d8bef9SDimitry Andric       // should be delayed, including those for non-members (eg, friend
7055e8d8bef9SDimitry Andric       // declarations). But only applying this to member declarations is
7056e8d8bef9SDimitry Andric       // consistent with what other implementations do.
70570b57cec5SDimitry Andric       bool Delayed = D.isFirstDeclarationOfMember() &&
70580b57cec5SDimitry Andric                      D.isFunctionDeclaratorAFunctionDeclaration();
70590b57cec5SDimitry Andric       if (Delayed && Actions.isLibstdcxxEagerExceptionSpecHack(D) &&
70600b57cec5SDimitry Andric           GetLookAheadToken(0).is(tok::kw_noexcept) &&
70610b57cec5SDimitry Andric           GetLookAheadToken(1).is(tok::l_paren) &&
70620b57cec5SDimitry Andric           GetLookAheadToken(2).is(tok::kw_noexcept) &&
70630b57cec5SDimitry Andric           GetLookAheadToken(3).is(tok::l_paren) &&
70640b57cec5SDimitry Andric           GetLookAheadToken(4).is(tok::identifier) &&
70650b57cec5SDimitry Andric           GetLookAheadToken(4).getIdentifierInfo()->isStr("swap")) {
70660b57cec5SDimitry Andric         // HACK: We've got an exception-specification
70670b57cec5SDimitry Andric         //   noexcept(noexcept(swap(...)))
70680b57cec5SDimitry Andric         // or
70690b57cec5SDimitry Andric         //   noexcept(noexcept(swap(...)) && noexcept(swap(...)))
70700b57cec5SDimitry Andric         // on a 'swap' member function. This is a libstdc++ bug; the lookup
70710b57cec5SDimitry Andric         // for 'swap' will only find the function we're currently declaring,
70720b57cec5SDimitry Andric         // whereas it expects to find a non-member swap through ADL. Turn off
70730b57cec5SDimitry Andric         // delayed parsing to give it a chance to find what it expects.
70740b57cec5SDimitry Andric         Delayed = false;
70750b57cec5SDimitry Andric       }
70760b57cec5SDimitry Andric       ESpecType = tryParseExceptionSpecification(Delayed,
70770b57cec5SDimitry Andric                                                  ESpecRange,
70780b57cec5SDimitry Andric                                                  DynamicExceptions,
70790b57cec5SDimitry Andric                                                  DynamicExceptionRanges,
70800b57cec5SDimitry Andric                                                  NoexceptExpr,
70810b57cec5SDimitry Andric                                                  ExceptionSpecTokens);
70820b57cec5SDimitry Andric       if (ESpecType != EST_None)
70830b57cec5SDimitry Andric         EndLoc = ESpecRange.getEnd();
70840b57cec5SDimitry Andric 
70850b57cec5SDimitry Andric       // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
70860b57cec5SDimitry Andric       // after the exception-specification.
70870b57cec5SDimitry Andric       MaybeParseCXX11Attributes(FnAttrs);
70880b57cec5SDimitry Andric 
70890b57cec5SDimitry Andric       // Parse trailing-return-type[opt].
70900b57cec5SDimitry Andric       LocalEndLoc = EndLoc;
70910b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
70920b57cec5SDimitry Andric         Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
70930b57cec5SDimitry Andric         if (D.getDeclSpec().getTypeSpecType() == TST_auto)
70940b57cec5SDimitry Andric           StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
70950b57cec5SDimitry Andric         LocalEndLoc = Tok.getLocation();
70960b57cec5SDimitry Andric         SourceRange Range;
70970b57cec5SDimitry Andric         TrailingReturnType =
70980b57cec5SDimitry Andric             ParseTrailingReturnType(Range, D.mayBeFollowedByCXXDirectInit());
7099e8d8bef9SDimitry Andric         TrailingReturnTypeLoc = Range.getBegin();
71000b57cec5SDimitry Andric         EndLoc = Range.getEnd();
71010b57cec5SDimitry Andric       }
7102*06c3fb27SDimitry Andric     } else {
71030b57cec5SDimitry Andric       MaybeParseCXX11Attributes(FnAttrs);
71040b57cec5SDimitry Andric     }
71050b57cec5SDimitry Andric   }
71060b57cec5SDimitry Andric 
71070b57cec5SDimitry Andric   // Collect non-parameter declarations from the prototype if this is a function
71080b57cec5SDimitry Andric   // declaration. They will be moved into the scope of the function. Only do
71090b57cec5SDimitry Andric   // this in C and not C++, where the decls will continue to live in the
71100b57cec5SDimitry Andric   // surrounding context.
71110b57cec5SDimitry Andric   SmallVector<NamedDecl *, 0> DeclsInPrototype;
711281ad6265SDimitry Andric   if (getCurScope()->isFunctionDeclarationScope() && !getLangOpts().CPlusPlus) {
71130b57cec5SDimitry Andric     for (Decl *D : getCurScope()->decls()) {
71140b57cec5SDimitry Andric       NamedDecl *ND = dyn_cast<NamedDecl>(D);
71150b57cec5SDimitry Andric       if (!ND || isa<ParmVarDecl>(ND))
71160b57cec5SDimitry Andric         continue;
71170b57cec5SDimitry Andric       DeclsInPrototype.push_back(ND);
71180b57cec5SDimitry Andric     }
7119*06c3fb27SDimitry Andric     // Sort DeclsInPrototype based on raw encoding of the source location.
7120*06c3fb27SDimitry Andric     // Scope::decls() is iterating over a SmallPtrSet so sort the Decls before
7121*06c3fb27SDimitry Andric     // moving to DeclContext. This provides a stable ordering for traversing
7122*06c3fb27SDimitry Andric     // Decls in DeclContext, which is important for tasks like ASTWriter for
7123*06c3fb27SDimitry Andric     // deterministic output.
7124*06c3fb27SDimitry Andric     llvm::sort(DeclsInPrototype, [](Decl *D1, Decl *D2) {
7125*06c3fb27SDimitry Andric       return D1->getLocation().getRawEncoding() <
7126*06c3fb27SDimitry Andric              D2->getLocation().getRawEncoding();
7127*06c3fb27SDimitry Andric     });
71280b57cec5SDimitry Andric   }
71290b57cec5SDimitry Andric 
71300b57cec5SDimitry Andric   // Remember that we parsed a function type, and remember the attributes.
71310b57cec5SDimitry Andric   D.AddTypeInfo(DeclaratorChunk::getFunction(
71320b57cec5SDimitry Andric                     HasProto, IsAmbiguous, LParenLoc, ParamInfo.data(),
71330b57cec5SDimitry Andric                     ParamInfo.size(), EllipsisLoc, RParenLoc,
71340b57cec5SDimitry Andric                     RefQualifierIsLValueRef, RefQualifierLoc,
71350b57cec5SDimitry Andric                     /*MutableLoc=*/SourceLocation(),
71360b57cec5SDimitry Andric                     ESpecType, ESpecRange, DynamicExceptions.data(),
71370b57cec5SDimitry Andric                     DynamicExceptionRanges.data(), DynamicExceptions.size(),
71380b57cec5SDimitry Andric                     NoexceptExpr.isUsable() ? NoexceptExpr.get() : nullptr,
71390b57cec5SDimitry Andric                     ExceptionSpecTokens, DeclsInPrototype, StartLoc,
7140e8d8bef9SDimitry Andric                     LocalEndLoc, D, TrailingReturnType, TrailingReturnTypeLoc,
7141e8d8bef9SDimitry Andric                     &DS),
71420b57cec5SDimitry Andric                 std::move(FnAttrs), EndLoc);
71430b57cec5SDimitry Andric }
71440b57cec5SDimitry Andric 
71450b57cec5SDimitry Andric /// ParseRefQualifier - Parses a member function ref-qualifier. Returns
71460b57cec5SDimitry Andric /// true if a ref-qualifier is found.
71470b57cec5SDimitry Andric bool Parser::ParseRefQualifier(bool &RefQualifierIsLValueRef,
71480b57cec5SDimitry Andric                                SourceLocation &RefQualifierLoc) {
71490b57cec5SDimitry Andric   if (Tok.isOneOf(tok::amp, tok::ampamp)) {
71500b57cec5SDimitry Andric     Diag(Tok, getLangOpts().CPlusPlus11 ?
71510b57cec5SDimitry Andric          diag::warn_cxx98_compat_ref_qualifier :
71520b57cec5SDimitry Andric          diag::ext_ref_qualifier);
71530b57cec5SDimitry Andric 
71540b57cec5SDimitry Andric     RefQualifierIsLValueRef = Tok.is(tok::amp);
71550b57cec5SDimitry Andric     RefQualifierLoc = ConsumeToken();
71560b57cec5SDimitry Andric     return true;
71570b57cec5SDimitry Andric   }
71580b57cec5SDimitry Andric   return false;
71590b57cec5SDimitry Andric }
71600b57cec5SDimitry Andric 
71610b57cec5SDimitry Andric /// isFunctionDeclaratorIdentifierList - This parameter list may have an
71620b57cec5SDimitry Andric /// identifier list form for a K&R-style function:  void foo(a,b,c)
71630b57cec5SDimitry Andric ///
71640b57cec5SDimitry Andric /// Note that identifier-lists are only allowed for normal declarators, not for
71650b57cec5SDimitry Andric /// abstract-declarators.
71660b57cec5SDimitry Andric bool Parser::isFunctionDeclaratorIdentifierList() {
716781ad6265SDimitry Andric   return !getLangOpts().requiresStrictPrototypes()
71680b57cec5SDimitry Andric          && Tok.is(tok::identifier)
71690b57cec5SDimitry Andric          && !TryAltiVecVectorToken()
71700b57cec5SDimitry Andric          // K&R identifier lists can't have typedefs as identifiers, per C99
71710b57cec5SDimitry Andric          // 6.7.5.3p11.
71720b57cec5SDimitry Andric          && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
71730b57cec5SDimitry Andric          // Identifier lists follow a really simple grammar: the identifiers can
71740b57cec5SDimitry Andric          // be followed *only* by a ", identifier" or ")".  However, K&R
71750b57cec5SDimitry Andric          // identifier lists are really rare in the brave new modern world, and
71760b57cec5SDimitry Andric          // it is very common for someone to typo a type in a non-K&R style
71770b57cec5SDimitry Andric          // list.  If we are presented with something like: "void foo(intptr x,
71780b57cec5SDimitry Andric          // float y)", we don't want to start parsing the function declarator as
71790b57cec5SDimitry Andric          // though it is a K&R style declarator just because intptr is an
71800b57cec5SDimitry Andric          // invalid type.
71810b57cec5SDimitry Andric          //
71820b57cec5SDimitry Andric          // To handle this, we check to see if the token after the first
71830b57cec5SDimitry Andric          // identifier is a "," or ")".  Only then do we parse it as an
71840b57cec5SDimitry Andric          // identifier list.
71850b57cec5SDimitry Andric          && (!Tok.is(tok::eof) &&
71860b57cec5SDimitry Andric              (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)));
71870b57cec5SDimitry Andric }
71880b57cec5SDimitry Andric 
71890b57cec5SDimitry Andric /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
71900b57cec5SDimitry Andric /// we found a K&R-style identifier list instead of a typed parameter list.
71910b57cec5SDimitry Andric ///
71920b57cec5SDimitry Andric /// After returning, ParamInfo will hold the parsed parameters.
71930b57cec5SDimitry Andric ///
71940b57cec5SDimitry Andric ///       identifier-list: [C99 6.7.5]
71950b57cec5SDimitry Andric ///         identifier
71960b57cec5SDimitry Andric ///         identifier-list ',' identifier
71970b57cec5SDimitry Andric ///
71980b57cec5SDimitry Andric void Parser::ParseFunctionDeclaratorIdentifierList(
71990b57cec5SDimitry Andric        Declarator &D,
72000b57cec5SDimitry Andric        SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
720181ad6265SDimitry Andric   // We should never reach this point in C2x or C++.
720281ad6265SDimitry Andric   assert(!getLangOpts().requiresStrictPrototypes() &&
720381ad6265SDimitry Andric          "Cannot parse an identifier list in C2x or C++");
720481ad6265SDimitry Andric 
72050b57cec5SDimitry Andric   // If there was no identifier specified for the declarator, either we are in
72060b57cec5SDimitry Andric   // an abstract-declarator, or we are in a parameter declarator which was found
72070b57cec5SDimitry Andric   // to be abstract.  In abstract-declarators, identifier lists are not valid:
72080b57cec5SDimitry Andric   // diagnose this.
72090b57cec5SDimitry Andric   if (!D.getIdentifier())
72100b57cec5SDimitry Andric     Diag(Tok, diag::ext_ident_list_in_param);
72110b57cec5SDimitry Andric 
72120b57cec5SDimitry Andric   // Maintain an efficient lookup of params we have seen so far.
72130b57cec5SDimitry Andric   llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
72140b57cec5SDimitry Andric 
72150b57cec5SDimitry Andric   do {
72160b57cec5SDimitry Andric     // If this isn't an identifier, report the error and skip until ')'.
72170b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
72180b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::identifier;
72190b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
72200b57cec5SDimitry Andric       // Forget we parsed anything.
72210b57cec5SDimitry Andric       ParamInfo.clear();
72220b57cec5SDimitry Andric       return;
72230b57cec5SDimitry Andric     }
72240b57cec5SDimitry Andric 
72250b57cec5SDimitry Andric     IdentifierInfo *ParmII = Tok.getIdentifierInfo();
72260b57cec5SDimitry Andric 
72270b57cec5SDimitry Andric     // Reject 'typedef int y; int test(x, y)', but continue parsing.
72280b57cec5SDimitry Andric     if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
72290b57cec5SDimitry Andric       Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
72300b57cec5SDimitry Andric 
72310b57cec5SDimitry Andric     // Verify that the argument identifier has not already been mentioned.
72320b57cec5SDimitry Andric     if (!ParamsSoFar.insert(ParmII).second) {
72330b57cec5SDimitry Andric       Diag(Tok, diag::err_param_redefinition) << ParmII;
72340b57cec5SDimitry Andric     } else {
72350b57cec5SDimitry Andric       // Remember this identifier in ParamInfo.
72360b57cec5SDimitry Andric       ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
72370b57cec5SDimitry Andric                                                      Tok.getLocation(),
72380b57cec5SDimitry Andric                                                      nullptr));
72390b57cec5SDimitry Andric     }
72400b57cec5SDimitry Andric 
72410b57cec5SDimitry Andric     // Eat the identifier.
72420b57cec5SDimitry Andric     ConsumeToken();
72430b57cec5SDimitry Andric     // The list continues if we see a comma.
72440b57cec5SDimitry Andric   } while (TryConsumeToken(tok::comma));
72450b57cec5SDimitry Andric }
72460b57cec5SDimitry Andric 
72470b57cec5SDimitry Andric /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
72480b57cec5SDimitry Andric /// after the opening parenthesis. This function will not parse a K&R-style
72490b57cec5SDimitry Andric /// identifier list.
72500b57cec5SDimitry Andric ///
725155e4f9d5SDimitry Andric /// DeclContext is the context of the declarator being parsed.  If FirstArgAttrs
725255e4f9d5SDimitry Andric /// is non-null, then the caller parsed those attributes immediately after the
725381ad6265SDimitry Andric /// open paren - they will be applied to the DeclSpec of the first parameter.
72540b57cec5SDimitry Andric ///
72550b57cec5SDimitry Andric /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
72560b57cec5SDimitry Andric /// be the location of the ellipsis, if any was parsed.
72570b57cec5SDimitry Andric ///
72580b57cec5SDimitry Andric ///       parameter-type-list: [C99 6.7.5]
72590b57cec5SDimitry Andric ///         parameter-list
72600b57cec5SDimitry Andric ///         parameter-list ',' '...'
72610b57cec5SDimitry Andric /// [C++]   parameter-list '...'
72620b57cec5SDimitry Andric ///
72630b57cec5SDimitry Andric ///       parameter-list: [C99 6.7.5]
72640b57cec5SDimitry Andric ///         parameter-declaration
72650b57cec5SDimitry Andric ///         parameter-list ',' parameter-declaration
72660b57cec5SDimitry Andric ///
72670b57cec5SDimitry Andric ///       parameter-declaration: [C99 6.7.5]
72680b57cec5SDimitry Andric ///         declaration-specifiers declarator
72690b57cec5SDimitry Andric /// [C++]   declaration-specifiers declarator '=' assignment-expression
72700b57cec5SDimitry Andric /// [C++11]                                       initializer-clause
72710b57cec5SDimitry Andric /// [GNU]   declaration-specifiers declarator attributes
72720b57cec5SDimitry Andric ///         declaration-specifiers abstract-declarator[opt]
72730b57cec5SDimitry Andric /// [C++]   declaration-specifiers abstract-declarator[opt]
72740b57cec5SDimitry Andric ///           '=' assignment-expression
72750b57cec5SDimitry Andric /// [GNU]   declaration-specifiers abstract-declarator[opt] attributes
72760b57cec5SDimitry Andric /// [C++11] attribute-specifier-seq parameter-declaration
72770b57cec5SDimitry Andric ///
72780b57cec5SDimitry Andric void Parser::ParseParameterDeclarationClause(
727981ad6265SDimitry Andric     DeclaratorContext DeclaratorCtx, ParsedAttributes &FirstArgAttrs,
72800b57cec5SDimitry Andric     SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
7281bdd1243dSDimitry Andric     SourceLocation &EllipsisLoc, bool IsACXXFunctionDeclaration) {
7282480093f4SDimitry Andric 
7283480093f4SDimitry Andric   // Avoid exceeding the maximum function scope depth.
7284480093f4SDimitry Andric   // See https://bugs.llvm.org/show_bug.cgi?id=19607
7285480093f4SDimitry Andric   // Note Sema::ActOnParamDeclarator calls ParmVarDecl::setScopeInfo with
7286480093f4SDimitry Andric   // getFunctionPrototypeDepth() - 1.
7287480093f4SDimitry Andric   if (getCurScope()->getFunctionPrototypeDepth() - 1 >
7288480093f4SDimitry Andric       ParmVarDecl::getMaxFunctionScopeDepth()) {
7289480093f4SDimitry Andric     Diag(Tok.getLocation(), diag::err_function_scope_depth_exceeded)
7290480093f4SDimitry Andric         << ParmVarDecl::getMaxFunctionScopeDepth();
7291480093f4SDimitry Andric     cutOffParsing();
7292480093f4SDimitry Andric     return;
7293480093f4SDimitry Andric   }
7294480093f4SDimitry Andric 
7295bdd1243dSDimitry Andric   // C++2a [temp.res]p5
7296bdd1243dSDimitry Andric   // A qualified-id is assumed to name a type if
7297bdd1243dSDimitry Andric   //   - [...]
7298bdd1243dSDimitry Andric   //   - it is a decl-specifier of the decl-specifier-seq of a
7299bdd1243dSDimitry Andric   //     - [...]
7300bdd1243dSDimitry Andric   //     - parameter-declaration in a member-declaration [...]
7301bdd1243dSDimitry Andric   //     - parameter-declaration in a declarator of a function or function
7302bdd1243dSDimitry Andric   //       template declaration whose declarator-id is qualified [...]
7303bdd1243dSDimitry Andric   //     - parameter-declaration in a lambda-declarator [...]
7304bdd1243dSDimitry Andric   auto AllowImplicitTypename = ImplicitTypenameContext::No;
7305bdd1243dSDimitry Andric   if (DeclaratorCtx == DeclaratorContext::Member ||
7306bdd1243dSDimitry Andric       DeclaratorCtx == DeclaratorContext::LambdaExpr ||
7307bdd1243dSDimitry Andric       DeclaratorCtx == DeclaratorContext::RequiresExpr ||
7308bdd1243dSDimitry Andric       IsACXXFunctionDeclaration) {
7309bdd1243dSDimitry Andric     AllowImplicitTypename = ImplicitTypenameContext::Yes;
7310bdd1243dSDimitry Andric   }
7311bdd1243dSDimitry Andric 
73120b57cec5SDimitry Andric   do {
73130b57cec5SDimitry Andric     // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
73140b57cec5SDimitry Andric     // before deciding this was a parameter-declaration-clause.
73150b57cec5SDimitry Andric     if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
73160b57cec5SDimitry Andric       break;
73170b57cec5SDimitry Andric 
73180b57cec5SDimitry Andric     // Parse the declaration-specifiers.
73190b57cec5SDimitry Andric     // Just use the ParsingDeclaration "scope" of the declarator.
73200b57cec5SDimitry Andric     DeclSpec DS(AttrFactory);
73210b57cec5SDimitry Andric 
732281ad6265SDimitry Andric     ParsedAttributes ArgDeclAttrs(AttrFactory);
732381ad6265SDimitry Andric     ParsedAttributes ArgDeclSpecAttrs(AttrFactory);
732481ad6265SDimitry Andric 
732581ad6265SDimitry Andric     if (FirstArgAttrs.Range.isValid()) {
732681ad6265SDimitry Andric       // If the caller parsed attributes for the first argument, add them now.
732781ad6265SDimitry Andric       // Take them so that we only apply the attributes to the first parameter.
732881ad6265SDimitry Andric       // We have already started parsing the decl-specifier sequence, so don't
732981ad6265SDimitry Andric       // parse any parameter-declaration pieces that precede it.
733081ad6265SDimitry Andric       ArgDeclSpecAttrs.takeAllFrom(FirstArgAttrs);
733181ad6265SDimitry Andric     } else {
73320b57cec5SDimitry Andric       // Parse any C++11 attributes.
733381ad6265SDimitry Andric       MaybeParseCXX11Attributes(ArgDeclAttrs);
73340b57cec5SDimitry Andric 
73350b57cec5SDimitry Andric       // Skip any Microsoft attributes before a param.
733681ad6265SDimitry Andric       MaybeParseMicrosoftAttributes(ArgDeclSpecAttrs);
733781ad6265SDimitry Andric     }
73380b57cec5SDimitry Andric 
73390b57cec5SDimitry Andric     SourceLocation DSStart = Tok.getLocation();
73400b57cec5SDimitry Andric 
7341bdd1243dSDimitry Andric     ParseDeclarationSpecifiers(DS, /*TemplateInfo=*/ParsedTemplateInfo(),
7342bdd1243dSDimitry Andric                                AS_none, DeclSpecContext::DSC_normal,
7343bdd1243dSDimitry Andric                                /*LateAttrs=*/nullptr, AllowImplicitTypename);
734481ad6265SDimitry Andric     DS.takeAttributesFrom(ArgDeclSpecAttrs);
73450b57cec5SDimitry Andric 
73460b57cec5SDimitry Andric     // Parse the declarator.  This is "PrototypeContext" or
73470b57cec5SDimitry Andric     // "LambdaExprParameterContext", because we must accept either
73480b57cec5SDimitry Andric     // 'declarator' or 'abstract-declarator' here.
734981ad6265SDimitry Andric     Declarator ParmDeclarator(DS, ArgDeclAttrs,
735081ad6265SDimitry Andric                               DeclaratorCtx == DeclaratorContext::RequiresExpr
7351e8d8bef9SDimitry Andric                                   ? DeclaratorContext::RequiresExpr
7352e8d8bef9SDimitry Andric                               : DeclaratorCtx == DeclaratorContext::LambdaExpr
7353e8d8bef9SDimitry Andric                                   ? DeclaratorContext::LambdaExprParameter
7354e8d8bef9SDimitry Andric                                   : DeclaratorContext::Prototype);
73550b57cec5SDimitry Andric     ParseDeclarator(ParmDeclarator);
73560b57cec5SDimitry Andric 
73570b57cec5SDimitry Andric     // Parse GNU attributes, if present.
73580b57cec5SDimitry Andric     MaybeParseGNUAttributes(ParmDeclarator);
7359bdd1243dSDimitry Andric     if (getLangOpts().HLSL)
736081ad6265SDimitry Andric       MaybeParseHLSLSemantics(DS.getAttributes());
73610b57cec5SDimitry Andric 
7362480093f4SDimitry Andric     if (Tok.is(tok::kw_requires)) {
7363480093f4SDimitry Andric       // User tried to define a requires clause in a parameter declaration,
7364480093f4SDimitry Andric       // which is surely not a function declaration.
7365480093f4SDimitry Andric       // void f(int (*g)(int, int) requires true);
7366480093f4SDimitry Andric       Diag(Tok,
7367480093f4SDimitry Andric            diag::err_requires_clause_on_declarator_not_declaring_a_function);
7368480093f4SDimitry Andric       ConsumeToken();
7369480093f4SDimitry Andric       Actions.CorrectDelayedTyposInExpr(
7370480093f4SDimitry Andric          ParseConstraintLogicalOrExpression(/*IsTrailingRequiresClause=*/true));
7371480093f4SDimitry Andric     }
7372480093f4SDimitry Andric 
73730b57cec5SDimitry Andric     // Remember this parsed parameter in ParamInfo.
73740b57cec5SDimitry Andric     IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
73750b57cec5SDimitry Andric 
73760b57cec5SDimitry Andric     // DefArgToks is used when the parsing of default arguments needs
73770b57cec5SDimitry Andric     // to be delayed.
73780b57cec5SDimitry Andric     std::unique_ptr<CachedTokens> DefArgToks;
73790b57cec5SDimitry Andric 
73800b57cec5SDimitry Andric     // If no parameter was specified, verify that *something* was specified,
73810b57cec5SDimitry Andric     // otherwise we have a missing type and identifier.
73820b57cec5SDimitry Andric     if (DS.isEmpty() && ParmDeclarator.getIdentifier() == nullptr &&
73830b57cec5SDimitry Andric         ParmDeclarator.getNumTypeObjects() == 0) {
73840b57cec5SDimitry Andric       // Completely missing, emit error.
73850b57cec5SDimitry Andric       Diag(DSStart, diag::err_missing_param);
73860b57cec5SDimitry Andric     } else {
73870b57cec5SDimitry Andric       // Otherwise, we have something.  Add it and let semantic analysis try
73880b57cec5SDimitry Andric       // to grok it and add the result to the ParamInfo we are building.
73890b57cec5SDimitry Andric 
73900b57cec5SDimitry Andric       // Last chance to recover from a misplaced ellipsis in an attempted
73910b57cec5SDimitry Andric       // parameter pack declaration.
73920b57cec5SDimitry Andric       if (Tok.is(tok::ellipsis) &&
73930b57cec5SDimitry Andric           (NextToken().isNot(tok::r_paren) ||
73940b57cec5SDimitry Andric            (!ParmDeclarator.getEllipsisLoc().isValid() &&
73950b57cec5SDimitry Andric             !Actions.isUnexpandedParameterPackPermitted())) &&
73960b57cec5SDimitry Andric           Actions.containsUnexpandedParameterPacks(ParmDeclarator))
73970b57cec5SDimitry Andric         DiagnoseMisplacedEllipsisInDeclarator(ConsumeToken(), ParmDeclarator);
73980b57cec5SDimitry Andric 
73995ffd83dbSDimitry Andric       // Now we are at the point where declarator parsing is finished.
74005ffd83dbSDimitry Andric       //
74015ffd83dbSDimitry Andric       // Try to catch keywords in place of the identifier in a declarator, and
74025ffd83dbSDimitry Andric       // in particular the common case where:
74035ffd83dbSDimitry Andric       //   1 identifier comes at the end of the declarator
74045ffd83dbSDimitry Andric       //   2 if the identifier is dropped, the declarator is valid but anonymous
74055ffd83dbSDimitry Andric       //     (no identifier)
74065ffd83dbSDimitry Andric       //   3 declarator parsing succeeds, and then we have a trailing keyword,
74075ffd83dbSDimitry Andric       //     which is never valid in a param list (e.g. missing a ',')
74085ffd83dbSDimitry Andric       // And we can't handle this in ParseDeclarator because in general keywords
74095ffd83dbSDimitry Andric       // may be allowed to follow the declarator. (And in some cases there'd be
74105ffd83dbSDimitry Andric       // better recovery like inserting punctuation). ParseDeclarator is just
74115ffd83dbSDimitry Andric       // treating this as an anonymous parameter, and fortunately at this point
74125ffd83dbSDimitry Andric       // we've already almost done that.
74135ffd83dbSDimitry Andric       //
74145ffd83dbSDimitry Andric       // We care about case 1) where the declarator type should be known, and
74155ffd83dbSDimitry Andric       // the identifier should be null.
74164824e7fdSDimitry Andric       if (!ParmDeclarator.isInvalidType() && !ParmDeclarator.hasName() &&
74174824e7fdSDimitry Andric           Tok.isNot(tok::raw_identifier) && !Tok.isAnnotation() &&
74184824e7fdSDimitry Andric           Tok.getIdentifierInfo() &&
74195ffd83dbSDimitry Andric           Tok.getIdentifierInfo()->isKeyword(getLangOpts())) {
74205ffd83dbSDimitry Andric         Diag(Tok, diag::err_keyword_as_parameter) << PP.getSpelling(Tok);
74215ffd83dbSDimitry Andric         // Consume the keyword.
74225ffd83dbSDimitry Andric         ConsumeToken();
74235ffd83dbSDimitry Andric       }
74240b57cec5SDimitry Andric       // Inform the actions module about the parameter declarator, so it gets
74250b57cec5SDimitry Andric       // added to the current scope.
74260b57cec5SDimitry Andric       Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
74270b57cec5SDimitry Andric       // Parse the default argument, if any. We parse the default
74280b57cec5SDimitry Andric       // arguments in all dialects; the semantic analysis in
74290b57cec5SDimitry Andric       // ActOnParamDefaultArgument will reject the default argument in
74300b57cec5SDimitry Andric       // C.
74310b57cec5SDimitry Andric       if (Tok.is(tok::equal)) {
74320b57cec5SDimitry Andric         SourceLocation EqualLoc = Tok.getLocation();
74330b57cec5SDimitry Andric 
74340b57cec5SDimitry Andric         // Parse the default argument
7435e8d8bef9SDimitry Andric         if (DeclaratorCtx == DeclaratorContext::Member) {
74360b57cec5SDimitry Andric           // If we're inside a class definition, cache the tokens
74370b57cec5SDimitry Andric           // corresponding to the default argument. We'll actually parse
74380b57cec5SDimitry Andric           // them when we see the end of the class definition.
74390b57cec5SDimitry Andric           DefArgToks.reset(new CachedTokens);
74400b57cec5SDimitry Andric 
74410b57cec5SDimitry Andric           SourceLocation ArgStartLoc = NextToken().getLocation();
7442*06c3fb27SDimitry Andric           ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument);
74430b57cec5SDimitry Andric           Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
74440b57cec5SDimitry Andric                                                     ArgStartLoc);
74450b57cec5SDimitry Andric         } else {
74460b57cec5SDimitry Andric           // Consume the '='.
74470b57cec5SDimitry Andric           ConsumeToken();
74480b57cec5SDimitry Andric 
74490b57cec5SDimitry Andric           // The argument isn't actually potentially evaluated unless it is
74500b57cec5SDimitry Andric           // used.
74510b57cec5SDimitry Andric           EnterExpressionEvaluationContext Eval(
74520b57cec5SDimitry Andric               Actions,
74530b57cec5SDimitry Andric               Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed,
74540b57cec5SDimitry Andric               Param);
74550b57cec5SDimitry Andric 
74560b57cec5SDimitry Andric           ExprResult DefArgResult;
74570b57cec5SDimitry Andric           if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
74580b57cec5SDimitry Andric             Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
74590b57cec5SDimitry Andric             DefArgResult = ParseBraceInitializer();
746081ad6265SDimitry Andric           } else {
746181ad6265SDimitry Andric             if (Tok.is(tok::l_paren) && NextToken().is(tok::l_brace)) {
746281ad6265SDimitry Andric               Diag(Tok, diag::err_stmt_expr_in_default_arg) << 0;
746381ad6265SDimitry Andric               Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
746481ad6265SDimitry Andric               // Skip the statement expression and continue parsing
746581ad6265SDimitry Andric               SkipUntil(tok::comma, StopBeforeMatch);
746681ad6265SDimitry Andric               continue;
746781ad6265SDimitry Andric             }
74680b57cec5SDimitry Andric             DefArgResult = ParseAssignmentExpression();
746981ad6265SDimitry Andric           }
74700b57cec5SDimitry Andric           DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
74710b57cec5SDimitry Andric           if (DefArgResult.isInvalid()) {
74720b57cec5SDimitry Andric             Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
74730b57cec5SDimitry Andric             SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
74740b57cec5SDimitry Andric           } else {
74750b57cec5SDimitry Andric             // Inform the actions module about the default argument
74760b57cec5SDimitry Andric             Actions.ActOnParamDefaultArgument(Param, EqualLoc,
74770b57cec5SDimitry Andric                                               DefArgResult.get());
74780b57cec5SDimitry Andric           }
74790b57cec5SDimitry Andric         }
74800b57cec5SDimitry Andric       }
74810b57cec5SDimitry Andric 
74820b57cec5SDimitry Andric       ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
74830b57cec5SDimitry Andric                                           ParmDeclarator.getIdentifierLoc(),
74840b57cec5SDimitry Andric                                           Param, std::move(DefArgToks)));
74850b57cec5SDimitry Andric     }
74860b57cec5SDimitry Andric 
74870b57cec5SDimitry Andric     if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) {
74880b57cec5SDimitry Andric       if (!getLangOpts().CPlusPlus) {
74890b57cec5SDimitry Andric         // We have ellipsis without a preceding ',', which is ill-formed
74900b57cec5SDimitry Andric         // in C. Complain and provide the fix.
74910b57cec5SDimitry Andric         Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
74920b57cec5SDimitry Andric             << FixItHint::CreateInsertion(EllipsisLoc, ", ");
74930b57cec5SDimitry Andric       } else if (ParmDeclarator.getEllipsisLoc().isValid() ||
74940b57cec5SDimitry Andric                  Actions.containsUnexpandedParameterPacks(ParmDeclarator)) {
74950b57cec5SDimitry Andric         // It looks like this was supposed to be a parameter pack. Warn and
74960b57cec5SDimitry Andric         // point out where the ellipsis should have gone.
74970b57cec5SDimitry Andric         SourceLocation ParmEllipsis = ParmDeclarator.getEllipsisLoc();
74980b57cec5SDimitry Andric         Diag(EllipsisLoc, diag::warn_misplaced_ellipsis_vararg)
74990b57cec5SDimitry Andric           << ParmEllipsis.isValid() << ParmEllipsis;
75000b57cec5SDimitry Andric         if (ParmEllipsis.isValid()) {
75010b57cec5SDimitry Andric           Diag(ParmEllipsis,
75020b57cec5SDimitry Andric                diag::note_misplaced_ellipsis_vararg_existing_ellipsis);
75030b57cec5SDimitry Andric         } else {
75040b57cec5SDimitry Andric           Diag(ParmDeclarator.getIdentifierLoc(),
75050b57cec5SDimitry Andric                diag::note_misplaced_ellipsis_vararg_add_ellipsis)
75060b57cec5SDimitry Andric             << FixItHint::CreateInsertion(ParmDeclarator.getIdentifierLoc(),
75070b57cec5SDimitry Andric                                           "...")
75080b57cec5SDimitry Andric             << !ParmDeclarator.hasName();
75090b57cec5SDimitry Andric         }
75100b57cec5SDimitry Andric         Diag(EllipsisLoc, diag::note_misplaced_ellipsis_vararg_add_comma)
75110b57cec5SDimitry Andric           << FixItHint::CreateInsertion(EllipsisLoc, ", ");
75120b57cec5SDimitry Andric       }
75130b57cec5SDimitry Andric 
75140b57cec5SDimitry Andric       // We can't have any more parameters after an ellipsis.
75150b57cec5SDimitry Andric       break;
75160b57cec5SDimitry Andric     }
75170b57cec5SDimitry Andric 
75180b57cec5SDimitry Andric     // If the next token is a comma, consume it and keep reading arguments.
75190b57cec5SDimitry Andric   } while (TryConsumeToken(tok::comma));
75200b57cec5SDimitry Andric }
75210b57cec5SDimitry Andric 
75220b57cec5SDimitry Andric /// [C90]   direct-declarator '[' constant-expression[opt] ']'
75230b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
75240b57cec5SDimitry Andric /// [C99]   direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
75250b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list 'static' assignment-expr ']'
75260b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list[opt] '*' ']'
75270b57cec5SDimitry Andric /// [C++11] direct-declarator '[' constant-expression[opt] ']'
75280b57cec5SDimitry Andric ///                           attribute-specifier-seq[opt]
75290b57cec5SDimitry Andric void Parser::ParseBracketDeclarator(Declarator &D) {
75300b57cec5SDimitry Andric   if (CheckProhibitedCXX11Attribute())
75310b57cec5SDimitry Andric     return;
75320b57cec5SDimitry Andric 
75330b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_square);
75340b57cec5SDimitry Andric   T.consumeOpen();
75350b57cec5SDimitry Andric 
75360b57cec5SDimitry Andric   // C array syntax has many features, but by-far the most common is [] and [4].
75370b57cec5SDimitry Andric   // This code does a fast path to handle some of the most obvious cases.
75380b57cec5SDimitry Andric   if (Tok.getKind() == tok::r_square) {
75390b57cec5SDimitry Andric     T.consumeClose();
75400b57cec5SDimitry Andric     ParsedAttributes attrs(AttrFactory);
75410b57cec5SDimitry Andric     MaybeParseCXX11Attributes(attrs);
75420b57cec5SDimitry Andric 
75430b57cec5SDimitry Andric     // Remember that we parsed the empty array type.
75440b57cec5SDimitry Andric     D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, nullptr,
75450b57cec5SDimitry Andric                                             T.getOpenLocation(),
75460b57cec5SDimitry Andric                                             T.getCloseLocation()),
75470b57cec5SDimitry Andric                   std::move(attrs), T.getCloseLocation());
75480b57cec5SDimitry Andric     return;
75490b57cec5SDimitry Andric   } else if (Tok.getKind() == tok::numeric_constant &&
75500b57cec5SDimitry Andric              GetLookAheadToken(1).is(tok::r_square)) {
75510b57cec5SDimitry Andric     // [4] is very common.  Parse the numeric constant expression.
75520b57cec5SDimitry Andric     ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
75530b57cec5SDimitry Andric     ConsumeToken();
75540b57cec5SDimitry Andric 
75550b57cec5SDimitry Andric     T.consumeClose();
75560b57cec5SDimitry Andric     ParsedAttributes attrs(AttrFactory);
75570b57cec5SDimitry Andric     MaybeParseCXX11Attributes(attrs);
75580b57cec5SDimitry Andric 
75590b57cec5SDimitry Andric     // Remember that we parsed a array type, and remember its features.
75600b57cec5SDimitry Andric     D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, ExprRes.get(),
75610b57cec5SDimitry Andric                                             T.getOpenLocation(),
75620b57cec5SDimitry Andric                                             T.getCloseLocation()),
75630b57cec5SDimitry Andric                   std::move(attrs), T.getCloseLocation());
75640b57cec5SDimitry Andric     return;
75650b57cec5SDimitry Andric   } else if (Tok.getKind() == tok::code_completion) {
7566fe6060f1SDimitry Andric     cutOffParsing();
75670b57cec5SDimitry Andric     Actions.CodeCompleteBracketDeclarator(getCurScope());
7568fe6060f1SDimitry Andric     return;
75690b57cec5SDimitry Andric   }
75700b57cec5SDimitry Andric 
75710b57cec5SDimitry Andric   // If valid, this location is the position where we read the 'static' keyword.
75720b57cec5SDimitry Andric   SourceLocation StaticLoc;
75730b57cec5SDimitry Andric   TryConsumeToken(tok::kw_static, StaticLoc);
75740b57cec5SDimitry Andric 
75750b57cec5SDimitry Andric   // If there is a type-qualifier-list, read it now.
75760b57cec5SDimitry Andric   // Type qualifiers in an array subscript are a C99 feature.
75770b57cec5SDimitry Andric   DeclSpec DS(AttrFactory);
75780b57cec5SDimitry Andric   ParseTypeQualifierListOpt(DS, AR_CXX11AttributesParsed);
75790b57cec5SDimitry Andric 
75800b57cec5SDimitry Andric   // If we haven't already read 'static', check to see if there is one after the
75810b57cec5SDimitry Andric   // type-qualifier-list.
75820b57cec5SDimitry Andric   if (!StaticLoc.isValid())
75830b57cec5SDimitry Andric     TryConsumeToken(tok::kw_static, StaticLoc);
75840b57cec5SDimitry Andric 
75850b57cec5SDimitry Andric   // Handle "direct-declarator [ type-qual-list[opt] * ]".
75860b57cec5SDimitry Andric   bool isStar = false;
75870b57cec5SDimitry Andric   ExprResult NumElements;
75880b57cec5SDimitry Andric 
75890b57cec5SDimitry Andric   // Handle the case where we have '[*]' as the array size.  However, a leading
75900b57cec5SDimitry Andric   // star could be the start of an expression, for example 'X[*p + 4]'.  Verify
75910b57cec5SDimitry Andric   // the token after the star is a ']'.  Since stars in arrays are
75920b57cec5SDimitry Andric   // infrequent, use of lookahead is not costly here.
75930b57cec5SDimitry Andric   if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
75940b57cec5SDimitry Andric     ConsumeToken();  // Eat the '*'.
75950b57cec5SDimitry Andric 
75960b57cec5SDimitry Andric     if (StaticLoc.isValid()) {
75970b57cec5SDimitry Andric       Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
75980b57cec5SDimitry Andric       StaticLoc = SourceLocation();  // Drop the static.
75990b57cec5SDimitry Andric     }
76000b57cec5SDimitry Andric     isStar = true;
76010b57cec5SDimitry Andric   } else if (Tok.isNot(tok::r_square)) {
76020b57cec5SDimitry Andric     // Note, in C89, this production uses the constant-expr production instead
76030b57cec5SDimitry Andric     // of assignment-expr.  The only difference is that assignment-expr allows
76040b57cec5SDimitry Andric     // things like '=' and '*='.  Sema rejects these in C89 mode because they
76050b57cec5SDimitry Andric     // are not i-c-e's, so we don't need to distinguish between the two here.
76060b57cec5SDimitry Andric 
76070b57cec5SDimitry Andric     // Parse the constant-expression or assignment-expression now (depending
76080b57cec5SDimitry Andric     // on dialect).
76090b57cec5SDimitry Andric     if (getLangOpts().CPlusPlus) {
76100b57cec5SDimitry Andric       NumElements = ParseConstantExpression();
76110b57cec5SDimitry Andric     } else {
76120b57cec5SDimitry Andric       EnterExpressionEvaluationContext Unevaluated(
76130b57cec5SDimitry Andric           Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
76140b57cec5SDimitry Andric       NumElements =
76150b57cec5SDimitry Andric           Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
76160b57cec5SDimitry Andric     }
76170b57cec5SDimitry Andric   } else {
76180b57cec5SDimitry Andric     if (StaticLoc.isValid()) {
76190b57cec5SDimitry Andric       Diag(StaticLoc, diag::err_unspecified_size_with_static);
76200b57cec5SDimitry Andric       StaticLoc = SourceLocation();  // Drop the static.
76210b57cec5SDimitry Andric     }
76220b57cec5SDimitry Andric   }
76230b57cec5SDimitry Andric 
76240b57cec5SDimitry Andric   // If there was an error parsing the assignment-expression, recover.
76250b57cec5SDimitry Andric   if (NumElements.isInvalid()) {
76260b57cec5SDimitry Andric     D.setInvalidType(true);
76270b57cec5SDimitry Andric     // If the expression was invalid, skip it.
76280b57cec5SDimitry Andric     SkipUntil(tok::r_square, StopAtSemi);
76290b57cec5SDimitry Andric     return;
76300b57cec5SDimitry Andric   }
76310b57cec5SDimitry Andric 
76320b57cec5SDimitry Andric   T.consumeClose();
76330b57cec5SDimitry Andric 
76340b57cec5SDimitry Andric   MaybeParseCXX11Attributes(DS.getAttributes());
76350b57cec5SDimitry Andric 
76360b57cec5SDimitry Andric   // Remember that we parsed a array type, and remember its features.
76370b57cec5SDimitry Andric   D.AddTypeInfo(
76380b57cec5SDimitry Andric       DeclaratorChunk::getArray(DS.getTypeQualifiers(), StaticLoc.isValid(),
76390b57cec5SDimitry Andric                                 isStar, NumElements.get(), T.getOpenLocation(),
76400b57cec5SDimitry Andric                                 T.getCloseLocation()),
76410b57cec5SDimitry Andric       std::move(DS.getAttributes()), T.getCloseLocation());
76420b57cec5SDimitry Andric }
76430b57cec5SDimitry Andric 
76440b57cec5SDimitry Andric /// Diagnose brackets before an identifier.
76450b57cec5SDimitry Andric void Parser::ParseMisplacedBracketDeclarator(Declarator &D) {
76460b57cec5SDimitry Andric   assert(Tok.is(tok::l_square) && "Missing opening bracket");
76470b57cec5SDimitry Andric   assert(!D.mayOmitIdentifier() && "Declarator cannot omit identifier");
76480b57cec5SDimitry Andric 
76490b57cec5SDimitry Andric   SourceLocation StartBracketLoc = Tok.getLocation();
765081ad6265SDimitry Andric   Declarator TempDeclarator(D.getDeclSpec(), ParsedAttributesView::none(),
765181ad6265SDimitry Andric                             D.getContext());
76520b57cec5SDimitry Andric 
76530b57cec5SDimitry Andric   while (Tok.is(tok::l_square)) {
76540b57cec5SDimitry Andric     ParseBracketDeclarator(TempDeclarator);
76550b57cec5SDimitry Andric   }
76560b57cec5SDimitry Andric 
76570b57cec5SDimitry Andric   // Stuff the location of the start of the brackets into the Declarator.
76580b57cec5SDimitry Andric   // The diagnostics from ParseDirectDeclarator will make more sense if
76590b57cec5SDimitry Andric   // they use this location instead.
76600b57cec5SDimitry Andric   if (Tok.is(tok::semi))
76610b57cec5SDimitry Andric     D.getName().EndLocation = StartBracketLoc;
76620b57cec5SDimitry Andric 
76630b57cec5SDimitry Andric   SourceLocation SuggestParenLoc = Tok.getLocation();
76640b57cec5SDimitry Andric 
76650b57cec5SDimitry Andric   // Now that the brackets are removed, try parsing the declarator again.
76660b57cec5SDimitry Andric   ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
76670b57cec5SDimitry Andric 
76680b57cec5SDimitry Andric   // Something went wrong parsing the brackets, in which case,
76690b57cec5SDimitry Andric   // ParseBracketDeclarator has emitted an error, and we don't need to emit
76700b57cec5SDimitry Andric   // one here.
76710b57cec5SDimitry Andric   if (TempDeclarator.getNumTypeObjects() == 0)
76720b57cec5SDimitry Andric     return;
76730b57cec5SDimitry Andric 
76740b57cec5SDimitry Andric   // Determine if parens will need to be suggested in the diagnostic.
76750b57cec5SDimitry Andric   bool NeedParens = false;
76760b57cec5SDimitry Andric   if (D.getNumTypeObjects() != 0) {
76770b57cec5SDimitry Andric     switch (D.getTypeObject(D.getNumTypeObjects() - 1).Kind) {
76780b57cec5SDimitry Andric     case DeclaratorChunk::Pointer:
76790b57cec5SDimitry Andric     case DeclaratorChunk::Reference:
76800b57cec5SDimitry Andric     case DeclaratorChunk::BlockPointer:
76810b57cec5SDimitry Andric     case DeclaratorChunk::MemberPointer:
76820b57cec5SDimitry Andric     case DeclaratorChunk::Pipe:
76830b57cec5SDimitry Andric       NeedParens = true;
76840b57cec5SDimitry Andric       break;
76850b57cec5SDimitry Andric     case DeclaratorChunk::Array:
76860b57cec5SDimitry Andric     case DeclaratorChunk::Function:
76870b57cec5SDimitry Andric     case DeclaratorChunk::Paren:
76880b57cec5SDimitry Andric       break;
76890b57cec5SDimitry Andric     }
76900b57cec5SDimitry Andric   }
76910b57cec5SDimitry Andric 
76920b57cec5SDimitry Andric   if (NeedParens) {
76930b57cec5SDimitry Andric     // Create a DeclaratorChunk for the inserted parens.
76940b57cec5SDimitry Andric     SourceLocation EndLoc = PP.getLocForEndOfToken(D.getEndLoc());
76950b57cec5SDimitry Andric     D.AddTypeInfo(DeclaratorChunk::getParen(SuggestParenLoc, EndLoc),
76960b57cec5SDimitry Andric                   SourceLocation());
76970b57cec5SDimitry Andric   }
76980b57cec5SDimitry Andric 
76990b57cec5SDimitry Andric   // Adding back the bracket info to the end of the Declarator.
77000b57cec5SDimitry Andric   for (unsigned i = 0, e = TempDeclarator.getNumTypeObjects(); i < e; ++i) {
77010b57cec5SDimitry Andric     const DeclaratorChunk &Chunk = TempDeclarator.getTypeObject(i);
77020b57cec5SDimitry Andric     D.AddTypeInfo(Chunk, SourceLocation());
77030b57cec5SDimitry Andric   }
77040b57cec5SDimitry Andric 
77050b57cec5SDimitry Andric   // The missing identifier would have been diagnosed in ParseDirectDeclarator.
77060b57cec5SDimitry Andric   // If parentheses are required, always suggest them.
77070b57cec5SDimitry Andric   if (!D.getIdentifier() && !NeedParens)
77080b57cec5SDimitry Andric     return;
77090b57cec5SDimitry Andric 
77100b57cec5SDimitry Andric   SourceLocation EndBracketLoc = TempDeclarator.getEndLoc();
77110b57cec5SDimitry Andric 
77120b57cec5SDimitry Andric   // Generate the move bracket error message.
77130b57cec5SDimitry Andric   SourceRange BracketRange(StartBracketLoc, EndBracketLoc);
77140b57cec5SDimitry Andric   SourceLocation EndLoc = PP.getLocForEndOfToken(D.getEndLoc());
77150b57cec5SDimitry Andric 
77160b57cec5SDimitry Andric   if (NeedParens) {
77170b57cec5SDimitry Andric     Diag(EndLoc, diag::err_brackets_go_after_unqualified_id)
77180b57cec5SDimitry Andric         << getLangOpts().CPlusPlus
77190b57cec5SDimitry Andric         << FixItHint::CreateInsertion(SuggestParenLoc, "(")
77200b57cec5SDimitry Andric         << FixItHint::CreateInsertion(EndLoc, ")")
77210b57cec5SDimitry Andric         << FixItHint::CreateInsertionFromRange(
77220b57cec5SDimitry Andric                EndLoc, CharSourceRange(BracketRange, true))
77230b57cec5SDimitry Andric         << FixItHint::CreateRemoval(BracketRange);
77240b57cec5SDimitry Andric   } else {
77250b57cec5SDimitry Andric     Diag(EndLoc, diag::err_brackets_go_after_unqualified_id)
77260b57cec5SDimitry Andric         << getLangOpts().CPlusPlus
77270b57cec5SDimitry Andric         << FixItHint::CreateInsertionFromRange(
77280b57cec5SDimitry Andric                EndLoc, CharSourceRange(BracketRange, true))
77290b57cec5SDimitry Andric         << FixItHint::CreateRemoval(BracketRange);
77300b57cec5SDimitry Andric   }
77310b57cec5SDimitry Andric }
77320b57cec5SDimitry Andric 
77330b57cec5SDimitry Andric /// [GNU]   typeof-specifier:
77340b57cec5SDimitry Andric ///           typeof ( expressions )
77350b57cec5SDimitry Andric ///           typeof ( type-name )
77360b57cec5SDimitry Andric /// [GNU/C++] typeof unary-expression
7737bdd1243dSDimitry Andric /// [C2x]   typeof-specifier:
7738bdd1243dSDimitry Andric ///           typeof '(' typeof-specifier-argument ')'
7739bdd1243dSDimitry Andric ///           typeof_unqual '(' typeof-specifier-argument ')'
7740bdd1243dSDimitry Andric ///
7741bdd1243dSDimitry Andric ///         typeof-specifier-argument:
7742bdd1243dSDimitry Andric ///           expression
7743bdd1243dSDimitry Andric ///           type-name
77440b57cec5SDimitry Andric ///
77450b57cec5SDimitry Andric void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
7746bdd1243dSDimitry Andric   assert(Tok.isOneOf(tok::kw_typeof, tok::kw_typeof_unqual) &&
7747bdd1243dSDimitry Andric          "Not a typeof specifier");
7748bdd1243dSDimitry Andric 
7749bdd1243dSDimitry Andric   bool IsUnqual = Tok.is(tok::kw_typeof_unqual);
7750bdd1243dSDimitry Andric   const IdentifierInfo *II = Tok.getIdentifierInfo();
7751bdd1243dSDimitry Andric   if (getLangOpts().C2x && !II->getName().startswith("__"))
7752*06c3fb27SDimitry Andric     Diag(Tok.getLocation(), diag::warn_c2x_compat_keyword) << Tok.getName();
7753bdd1243dSDimitry Andric 
77540b57cec5SDimitry Andric   Token OpTok = Tok;
77550b57cec5SDimitry Andric   SourceLocation StartLoc = ConsumeToken();
7756bdd1243dSDimitry Andric   bool HasParens = Tok.is(tok::l_paren);
77570b57cec5SDimitry Andric 
77580b57cec5SDimitry Andric   EnterExpressionEvaluationContext Unevaluated(
77590b57cec5SDimitry Andric       Actions, Sema::ExpressionEvaluationContext::Unevaluated,
77600b57cec5SDimitry Andric       Sema::ReuseLambdaContextDecl);
77610b57cec5SDimitry Andric 
77620b57cec5SDimitry Andric   bool isCastExpr;
77630b57cec5SDimitry Andric   ParsedType CastTy;
77640b57cec5SDimitry Andric   SourceRange CastRange;
77650b57cec5SDimitry Andric   ExprResult Operand = Actions.CorrectDelayedTyposInExpr(
77660b57cec5SDimitry Andric       ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, CastTy, CastRange));
7767bdd1243dSDimitry Andric   if (HasParens)
7768bdd1243dSDimitry Andric     DS.setTypeArgumentRange(CastRange);
77690b57cec5SDimitry Andric 
77700b57cec5SDimitry Andric   if (CastRange.getEnd().isInvalid())
77710b57cec5SDimitry Andric     // FIXME: Not accurate, the range gets one token more than it should.
77720b57cec5SDimitry Andric     DS.SetRangeEnd(Tok.getLocation());
77730b57cec5SDimitry Andric   else
77740b57cec5SDimitry Andric     DS.SetRangeEnd(CastRange.getEnd());
77750b57cec5SDimitry Andric 
77760b57cec5SDimitry Andric   if (isCastExpr) {
77770b57cec5SDimitry Andric     if (!CastTy) {
77780b57cec5SDimitry Andric       DS.SetTypeSpecError();
77790b57cec5SDimitry Andric       return;
77800b57cec5SDimitry Andric     }
77810b57cec5SDimitry Andric 
77820b57cec5SDimitry Andric     const char *PrevSpec = nullptr;
77830b57cec5SDimitry Andric     unsigned DiagID;
77840b57cec5SDimitry Andric     // Check for duplicate type specifiers (e.g. "int typeof(int)").
7785bdd1243dSDimitry Andric     if (DS.SetTypeSpecType(IsUnqual ? DeclSpec::TST_typeof_unqualType
7786bdd1243dSDimitry Andric                                     : DeclSpec::TST_typeofType,
7787bdd1243dSDimitry Andric                            StartLoc, PrevSpec,
77880b57cec5SDimitry Andric                            DiagID, CastTy,
77890b57cec5SDimitry Andric                            Actions.getASTContext().getPrintingPolicy()))
77900b57cec5SDimitry Andric       Diag(StartLoc, DiagID) << PrevSpec;
77910b57cec5SDimitry Andric     return;
77920b57cec5SDimitry Andric   }
77930b57cec5SDimitry Andric 
77940b57cec5SDimitry Andric   // If we get here, the operand to the typeof was an expression.
77950b57cec5SDimitry Andric   if (Operand.isInvalid()) {
77960b57cec5SDimitry Andric     DS.SetTypeSpecError();
77970b57cec5SDimitry Andric     return;
77980b57cec5SDimitry Andric   }
77990b57cec5SDimitry Andric 
78000b57cec5SDimitry Andric   // We might need to transform the operand if it is potentially evaluated.
78010b57cec5SDimitry Andric   Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
78020b57cec5SDimitry Andric   if (Operand.isInvalid()) {
78030b57cec5SDimitry Andric     DS.SetTypeSpecError();
78040b57cec5SDimitry Andric     return;
78050b57cec5SDimitry Andric   }
78060b57cec5SDimitry Andric 
78070b57cec5SDimitry Andric   const char *PrevSpec = nullptr;
78080b57cec5SDimitry Andric   unsigned DiagID;
78090b57cec5SDimitry Andric   // Check for duplicate type specifiers (e.g. "int typeof(int)").
7810bdd1243dSDimitry Andric   if (DS.SetTypeSpecType(IsUnqual ? DeclSpec::TST_typeof_unqualExpr
7811bdd1243dSDimitry Andric                                   : DeclSpec::TST_typeofExpr,
7812bdd1243dSDimitry Andric                          StartLoc, PrevSpec,
78130b57cec5SDimitry Andric                          DiagID, Operand.get(),
78140b57cec5SDimitry Andric                          Actions.getASTContext().getPrintingPolicy()))
78150b57cec5SDimitry Andric     Diag(StartLoc, DiagID) << PrevSpec;
78160b57cec5SDimitry Andric }
78170b57cec5SDimitry Andric 
78180b57cec5SDimitry Andric /// [C11]   atomic-specifier:
78190b57cec5SDimitry Andric ///           _Atomic ( type-name )
78200b57cec5SDimitry Andric ///
78210b57cec5SDimitry Andric void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
78220b57cec5SDimitry Andric   assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
78230b57cec5SDimitry Andric          "Not an atomic specifier");
78240b57cec5SDimitry Andric 
78250b57cec5SDimitry Andric   SourceLocation StartLoc = ConsumeToken();
78260b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
78270b57cec5SDimitry Andric   if (T.consumeOpen())
78280b57cec5SDimitry Andric     return;
78290b57cec5SDimitry Andric 
78300b57cec5SDimitry Andric   TypeResult Result = ParseTypeName();
78310b57cec5SDimitry Andric   if (Result.isInvalid()) {
78320b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
78330b57cec5SDimitry Andric     return;
78340b57cec5SDimitry Andric   }
78350b57cec5SDimitry Andric 
78360b57cec5SDimitry Andric   // Match the ')'
78370b57cec5SDimitry Andric   T.consumeClose();
78380b57cec5SDimitry Andric 
78390b57cec5SDimitry Andric   if (T.getCloseLocation().isInvalid())
78400b57cec5SDimitry Andric     return;
78410b57cec5SDimitry Andric 
7842bdd1243dSDimitry Andric   DS.setTypeArgumentRange(T.getRange());
78430b57cec5SDimitry Andric   DS.SetRangeEnd(T.getCloseLocation());
78440b57cec5SDimitry Andric 
78450b57cec5SDimitry Andric   const char *PrevSpec = nullptr;
78460b57cec5SDimitry Andric   unsigned DiagID;
78470b57cec5SDimitry Andric   if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
78480b57cec5SDimitry Andric                          DiagID, Result.get(),
78490b57cec5SDimitry Andric                          Actions.getASTContext().getPrintingPolicy()))
78500b57cec5SDimitry Andric     Diag(StartLoc, DiagID) << PrevSpec;
78510b57cec5SDimitry Andric }
78520b57cec5SDimitry Andric 
78530b57cec5SDimitry Andric /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
78540b57cec5SDimitry Andric /// from TryAltiVecVectorToken.
78550b57cec5SDimitry Andric bool Parser::TryAltiVecVectorTokenOutOfLine() {
78560b57cec5SDimitry Andric   Token Next = NextToken();
78570b57cec5SDimitry Andric   switch (Next.getKind()) {
78580b57cec5SDimitry Andric   default: return false;
78590b57cec5SDimitry Andric   case tok::kw_short:
78600b57cec5SDimitry Andric   case tok::kw_long:
78610b57cec5SDimitry Andric   case tok::kw_signed:
78620b57cec5SDimitry Andric   case tok::kw_unsigned:
78630b57cec5SDimitry Andric   case tok::kw_void:
78640b57cec5SDimitry Andric   case tok::kw_char:
78650b57cec5SDimitry Andric   case tok::kw_int:
78660b57cec5SDimitry Andric   case tok::kw_float:
78670b57cec5SDimitry Andric   case tok::kw_double:
78680b57cec5SDimitry Andric   case tok::kw_bool:
7869fe6060f1SDimitry Andric   case tok::kw__Bool:
78700b57cec5SDimitry Andric   case tok::kw___bool:
78710b57cec5SDimitry Andric   case tok::kw___pixel:
78720b57cec5SDimitry Andric     Tok.setKind(tok::kw___vector);
78730b57cec5SDimitry Andric     return true;
78740b57cec5SDimitry Andric   case tok::identifier:
78750b57cec5SDimitry Andric     if (Next.getIdentifierInfo() == Ident_pixel) {
78760b57cec5SDimitry Andric       Tok.setKind(tok::kw___vector);
78770b57cec5SDimitry Andric       return true;
78780b57cec5SDimitry Andric     }
7879fe6060f1SDimitry Andric     if (Next.getIdentifierInfo() == Ident_bool ||
7880fe6060f1SDimitry Andric         Next.getIdentifierInfo() == Ident_Bool) {
78810b57cec5SDimitry Andric       Tok.setKind(tok::kw___vector);
78820b57cec5SDimitry Andric       return true;
78830b57cec5SDimitry Andric     }
78840b57cec5SDimitry Andric     return false;
78850b57cec5SDimitry Andric   }
78860b57cec5SDimitry Andric }
78870b57cec5SDimitry Andric 
78880b57cec5SDimitry Andric bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
78890b57cec5SDimitry Andric                                       const char *&PrevSpec, unsigned &DiagID,
78900b57cec5SDimitry Andric                                       bool &isInvalid) {
78910b57cec5SDimitry Andric   const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
78920b57cec5SDimitry Andric   if (Tok.getIdentifierInfo() == Ident_vector) {
78930b57cec5SDimitry Andric     Token Next = NextToken();
78940b57cec5SDimitry Andric     switch (Next.getKind()) {
78950b57cec5SDimitry Andric     case tok::kw_short:
78960b57cec5SDimitry Andric     case tok::kw_long:
78970b57cec5SDimitry Andric     case tok::kw_signed:
78980b57cec5SDimitry Andric     case tok::kw_unsigned:
78990b57cec5SDimitry Andric     case tok::kw_void:
79000b57cec5SDimitry Andric     case tok::kw_char:
79010b57cec5SDimitry Andric     case tok::kw_int:
79020b57cec5SDimitry Andric     case tok::kw_float:
79030b57cec5SDimitry Andric     case tok::kw_double:
79040b57cec5SDimitry Andric     case tok::kw_bool:
7905fe6060f1SDimitry Andric     case tok::kw__Bool:
79060b57cec5SDimitry Andric     case tok::kw___bool:
79070b57cec5SDimitry Andric     case tok::kw___pixel:
79080b57cec5SDimitry Andric       isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
79090b57cec5SDimitry Andric       return true;
79100b57cec5SDimitry Andric     case tok::identifier:
79110b57cec5SDimitry Andric       if (Next.getIdentifierInfo() == Ident_pixel) {
79120b57cec5SDimitry Andric         isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
79130b57cec5SDimitry Andric         return true;
79140b57cec5SDimitry Andric       }
7915fe6060f1SDimitry Andric       if (Next.getIdentifierInfo() == Ident_bool ||
7916fe6060f1SDimitry Andric           Next.getIdentifierInfo() == Ident_Bool) {
7917fe6060f1SDimitry Andric         isInvalid =
7918fe6060f1SDimitry Andric             DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
79190b57cec5SDimitry Andric         return true;
79200b57cec5SDimitry Andric       }
79210b57cec5SDimitry Andric       break;
79220b57cec5SDimitry Andric     default:
79230b57cec5SDimitry Andric       break;
79240b57cec5SDimitry Andric     }
79250b57cec5SDimitry Andric   } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
79260b57cec5SDimitry Andric              DS.isTypeAltiVecVector()) {
79270b57cec5SDimitry Andric     isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
79280b57cec5SDimitry Andric     return true;
79290b57cec5SDimitry Andric   } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
79300b57cec5SDimitry Andric              DS.isTypeAltiVecVector()) {
79310b57cec5SDimitry Andric     isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
79320b57cec5SDimitry Andric     return true;
79330b57cec5SDimitry Andric   }
79340b57cec5SDimitry Andric   return false;
79350b57cec5SDimitry Andric }
79360eae32dcSDimitry Andric 
79370eae32dcSDimitry Andric void Parser::DiagnoseBitIntUse(const Token &Tok) {
79380eae32dcSDimitry Andric   // If the token is for _ExtInt, diagnose it as being deprecated. Otherwise,
79390eae32dcSDimitry Andric   // the token is about _BitInt and gets (potentially) diagnosed as use of an
79400eae32dcSDimitry Andric   // extension.
79410eae32dcSDimitry Andric   assert(Tok.isOneOf(tok::kw__ExtInt, tok::kw__BitInt) &&
79420eae32dcSDimitry Andric          "expected either an _ExtInt or _BitInt token!");
79430eae32dcSDimitry Andric 
79440eae32dcSDimitry Andric   SourceLocation Loc = Tok.getLocation();
79450eae32dcSDimitry Andric   if (Tok.is(tok::kw__ExtInt)) {
79460eae32dcSDimitry Andric     Diag(Loc, diag::warn_ext_int_deprecated)
79470eae32dcSDimitry Andric         << FixItHint::CreateReplacement(Loc, "_BitInt");
79480eae32dcSDimitry Andric   } else {
79490eae32dcSDimitry Andric     // In C2x mode, diagnose that the use is not compatible with pre-C2x modes.
79500eae32dcSDimitry Andric     // Otherwise, diagnose that the use is a Clang extension.
79510eae32dcSDimitry Andric     if (getLangOpts().C2x)
7952*06c3fb27SDimitry Andric       Diag(Loc, diag::warn_c2x_compat_keyword) << Tok.getName();
79530eae32dcSDimitry Andric     else
79540eae32dcSDimitry Andric       Diag(Loc, diag::ext_bit_int) << getLangOpts().CPlusPlus;
79550eae32dcSDimitry Andric   }
79560eae32dcSDimitry Andric }
7957