xref: /freebsd/contrib/llvm-project/clang/lib/Parse/ParseDecl.cpp (revision bdd1243df58e60e85101c09001d9812a789b6bc4)
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"
240b57cec5SDimitry Andric #include "clang/Sema/Lookup.h"
250b57cec5SDimitry Andric #include "clang/Sema/ParsedTemplate.h"
260b57cec5SDimitry Andric #include "clang/Sema/Scope.h"
27e8d8bef9SDimitry Andric #include "clang/Sema/SemaDiagnostic.h"
280b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h"
290b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
300b57cec5SDimitry Andric #include "llvm/ADT/StringSwitch.h"
31*bdd1243dSDimitry Andric #include <optional>
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric using namespace clang;
340b57cec5SDimitry Andric 
350b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
360b57cec5SDimitry Andric // C99 6.7: Declarations.
370b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
380b57cec5SDimitry Andric 
390b57cec5SDimitry Andric /// ParseTypeName
400b57cec5SDimitry Andric ///       type-name: [C99 6.7.6]
410b57cec5SDimitry Andric ///         specifier-qualifier-list abstract-declarator[opt]
420b57cec5SDimitry Andric ///
430b57cec5SDimitry Andric /// Called type-id in C++.
4481ad6265SDimitry Andric TypeResult Parser::ParseTypeName(SourceRange *Range, DeclaratorContext Context,
4581ad6265SDimitry Andric                                  AccessSpecifier AS, Decl **OwnedType,
460b57cec5SDimitry Andric                                  ParsedAttributes *Attrs) {
470b57cec5SDimitry Andric   DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
480b57cec5SDimitry Andric   if (DSC == DeclSpecContext::DSC_normal)
490b57cec5SDimitry Andric     DSC = DeclSpecContext::DSC_type_specifier;
500b57cec5SDimitry Andric 
510b57cec5SDimitry Andric   // Parse the common declaration-specifiers piece.
520b57cec5SDimitry Andric   DeclSpec DS(AttrFactory);
530b57cec5SDimitry Andric   if (Attrs)
540b57cec5SDimitry Andric     DS.addAttributes(*Attrs);
550b57cec5SDimitry Andric   ParseSpecifierQualifierList(DS, AS, DSC);
560b57cec5SDimitry Andric   if (OwnedType)
570b57cec5SDimitry Andric     *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : nullptr;
580b57cec5SDimitry Andric 
590b57cec5SDimitry Andric   // Parse the abstract-declarator, if present.
6081ad6265SDimitry Andric   Declarator DeclaratorInfo(DS, ParsedAttributesView::none(), Context);
610b57cec5SDimitry Andric   ParseDeclarator(DeclaratorInfo);
620b57cec5SDimitry Andric   if (Range)
630b57cec5SDimitry Andric     *Range = DeclaratorInfo.getSourceRange();
640b57cec5SDimitry Andric 
650b57cec5SDimitry Andric   if (DeclaratorInfo.isInvalidType())
660b57cec5SDimitry Andric     return true;
670b57cec5SDimitry Andric 
680b57cec5SDimitry Andric   return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
690b57cec5SDimitry Andric }
700b57cec5SDimitry Andric 
710b57cec5SDimitry Andric /// Normalizes an attribute name by dropping prefixed and suffixed __.
720b57cec5SDimitry Andric static StringRef normalizeAttrName(StringRef Name) {
730b57cec5SDimitry Andric   if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
740b57cec5SDimitry Andric     return Name.drop_front(2).drop_back(2);
750b57cec5SDimitry Andric   return Name;
760b57cec5SDimitry Andric }
770b57cec5SDimitry Andric 
780b57cec5SDimitry Andric /// isAttributeLateParsed - Return true if the attribute has arguments that
790b57cec5SDimitry Andric /// require late parsing.
800b57cec5SDimitry Andric static bool isAttributeLateParsed(const IdentifierInfo &II) {
810b57cec5SDimitry Andric #define CLANG_ATTR_LATE_PARSED_LIST
820b57cec5SDimitry Andric     return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
830b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
840b57cec5SDimitry Andric         .Default(false);
850b57cec5SDimitry Andric #undef CLANG_ATTR_LATE_PARSED_LIST
860b57cec5SDimitry Andric }
870b57cec5SDimitry Andric 
880b57cec5SDimitry Andric /// Check if the a start and end source location expand to the same macro.
89a7dea167SDimitry Andric static bool FindLocsWithCommonFileID(Preprocessor &PP, SourceLocation StartLoc,
900b57cec5SDimitry Andric                                      SourceLocation EndLoc) {
910b57cec5SDimitry Andric   if (!StartLoc.isMacroID() || !EndLoc.isMacroID())
920b57cec5SDimitry Andric     return false;
930b57cec5SDimitry Andric 
940b57cec5SDimitry Andric   SourceManager &SM = PP.getSourceManager();
950b57cec5SDimitry Andric   if (SM.getFileID(StartLoc) != SM.getFileID(EndLoc))
960b57cec5SDimitry Andric     return false;
970b57cec5SDimitry Andric 
980b57cec5SDimitry Andric   bool AttrStartIsInMacro =
990b57cec5SDimitry Andric       Lexer::isAtStartOfMacroExpansion(StartLoc, SM, PP.getLangOpts());
1000b57cec5SDimitry Andric   bool AttrEndIsInMacro =
1010b57cec5SDimitry Andric       Lexer::isAtEndOfMacroExpansion(EndLoc, SM, PP.getLangOpts());
1020b57cec5SDimitry Andric   return AttrStartIsInMacro && AttrEndIsInMacro;
1030b57cec5SDimitry Andric }
1040b57cec5SDimitry Andric 
10581ad6265SDimitry Andric void Parser::ParseAttributes(unsigned WhichAttrKinds, ParsedAttributes &Attrs,
106fe6060f1SDimitry Andric                              LateParsedAttrList *LateAttrs) {
107fe6060f1SDimitry Andric   bool MoreToParse;
108fe6060f1SDimitry Andric   do {
109fe6060f1SDimitry Andric     // Assume there's nothing left to parse, but if any attributes are in fact
110fe6060f1SDimitry Andric     // parsed, loop to ensure all specified attribute combinations are parsed.
111fe6060f1SDimitry Andric     MoreToParse = false;
112fe6060f1SDimitry Andric     if (WhichAttrKinds & PAKM_CXX11)
11381ad6265SDimitry Andric       MoreToParse |= MaybeParseCXX11Attributes(Attrs);
114fe6060f1SDimitry Andric     if (WhichAttrKinds & PAKM_GNU)
11581ad6265SDimitry Andric       MoreToParse |= MaybeParseGNUAttributes(Attrs, LateAttrs);
116fe6060f1SDimitry Andric     if (WhichAttrKinds & PAKM_Declspec)
11781ad6265SDimitry Andric       MoreToParse |= MaybeParseMicrosoftDeclSpecs(Attrs);
118fe6060f1SDimitry Andric   } while (MoreToParse);
119fe6060f1SDimitry Andric }
120fe6060f1SDimitry Andric 
1210b57cec5SDimitry Andric /// ParseGNUAttributes - Parse a non-empty attributes list.
1220b57cec5SDimitry Andric ///
1230b57cec5SDimitry Andric /// [GNU] attributes:
1240b57cec5SDimitry Andric ///         attribute
1250b57cec5SDimitry Andric ///         attributes attribute
1260b57cec5SDimitry Andric ///
1270b57cec5SDimitry Andric /// [GNU]  attribute:
1280b57cec5SDimitry Andric ///          '__attribute__' '(' '(' attribute-list ')' ')'
1290b57cec5SDimitry Andric ///
1300b57cec5SDimitry Andric /// [GNU]  attribute-list:
1310b57cec5SDimitry Andric ///          attrib
1320b57cec5SDimitry Andric ///          attribute_list ',' attrib
1330b57cec5SDimitry Andric ///
1340b57cec5SDimitry Andric /// [GNU]  attrib:
1350b57cec5SDimitry Andric ///          empty
1360b57cec5SDimitry Andric ///          attrib-name
1370b57cec5SDimitry Andric ///          attrib-name '(' identifier ')'
1380b57cec5SDimitry Andric ///          attrib-name '(' identifier ',' nonempty-expr-list ')'
1390b57cec5SDimitry Andric ///          attrib-name '(' argument-expression-list [C99 6.5.2] ')'
1400b57cec5SDimitry Andric ///
1410b57cec5SDimitry Andric /// [GNU]  attrib-name:
1420b57cec5SDimitry Andric ///          identifier
1430b57cec5SDimitry Andric ///          typespec
1440b57cec5SDimitry Andric ///          typequal
1450b57cec5SDimitry Andric ///          storageclass
1460b57cec5SDimitry Andric ///
1470b57cec5SDimitry Andric /// Whether an attribute takes an 'identifier' is determined by the
1480b57cec5SDimitry Andric /// attrib-name. GCC's behavior here is not worth imitating:
1490b57cec5SDimitry Andric ///
1500b57cec5SDimitry Andric ///  * In C mode, if the attribute argument list starts with an identifier
1510b57cec5SDimitry Andric ///    followed by a ',' or an ')', and the identifier doesn't resolve to
1520b57cec5SDimitry Andric ///    a type, it is parsed as an identifier. If the attribute actually
1530b57cec5SDimitry Andric ///    wanted an expression, it's out of luck (but it turns out that no
1540b57cec5SDimitry Andric ///    attributes work that way, because C constant expressions are very
1550b57cec5SDimitry Andric ///    limited).
1560b57cec5SDimitry Andric ///  * In C++ mode, if the attribute argument list starts with an identifier,
1570b57cec5SDimitry Andric ///    and the attribute *wants* an identifier, it is parsed as an identifier.
1580b57cec5SDimitry Andric ///    At block scope, any additional tokens between the identifier and the
1590b57cec5SDimitry Andric ///    ',' or ')' are ignored, otherwise they produce a parse error.
1600b57cec5SDimitry Andric ///
1610b57cec5SDimitry Andric /// We follow the C++ model, but don't allow junk after the identifier.
16281ad6265SDimitry Andric void Parser::ParseGNUAttributes(ParsedAttributes &Attrs,
163fe6060f1SDimitry Andric                                 LateParsedAttrList *LateAttrs, Declarator *D) {
1640b57cec5SDimitry Andric   assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
1650b57cec5SDimitry Andric 
16681ad6265SDimitry Andric   SourceLocation StartLoc = Tok.getLocation();
16781ad6265SDimitry Andric   SourceLocation EndLoc = StartLoc;
168fe6060f1SDimitry Andric 
1690b57cec5SDimitry Andric   while (Tok.is(tok::kw___attribute)) {
1700b57cec5SDimitry Andric     SourceLocation AttrTokLoc = ConsumeToken();
171fe6060f1SDimitry Andric     unsigned OldNumAttrs = Attrs.size();
1720b57cec5SDimitry Andric     unsigned OldNumLateAttrs = LateAttrs ? LateAttrs->size() : 0;
1730b57cec5SDimitry Andric 
1740b57cec5SDimitry Andric     if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
1750b57cec5SDimitry Andric                          "attribute")) {
1760b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
1770b57cec5SDimitry Andric       return;
1780b57cec5SDimitry Andric     }
1790b57cec5SDimitry Andric     if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
1800b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
1810b57cec5SDimitry Andric       return;
1820b57cec5SDimitry Andric     }
1830b57cec5SDimitry Andric     // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
1840b57cec5SDimitry Andric     do {
1850b57cec5SDimitry Andric       // Eat preceeding commas to allow __attribute__((,,,foo))
1860b57cec5SDimitry Andric       while (TryConsumeToken(tok::comma))
1870b57cec5SDimitry Andric         ;
1880b57cec5SDimitry Andric 
1890b57cec5SDimitry Andric       // Expect an identifier or declaration specifier (const, int, etc.)
1900b57cec5SDimitry Andric       if (Tok.isAnnotation())
1910b57cec5SDimitry Andric         break;
192349cc55cSDimitry Andric       if (Tok.is(tok::code_completion)) {
193349cc55cSDimitry Andric         cutOffParsing();
194349cc55cSDimitry Andric         Actions.CodeCompleteAttribute(AttributeCommonInfo::Syntax::AS_GNU);
195349cc55cSDimitry Andric         break;
196349cc55cSDimitry Andric       }
1970b57cec5SDimitry Andric       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1980b57cec5SDimitry Andric       if (!AttrName)
1990b57cec5SDimitry Andric         break;
2000b57cec5SDimitry Andric 
2010b57cec5SDimitry Andric       SourceLocation AttrNameLoc = ConsumeToken();
2020b57cec5SDimitry Andric 
2030b57cec5SDimitry Andric       if (Tok.isNot(tok::l_paren)) {
204fe6060f1SDimitry Andric         Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
2050b57cec5SDimitry Andric                      ParsedAttr::AS_GNU);
2060b57cec5SDimitry Andric         continue;
2070b57cec5SDimitry Andric       }
2080b57cec5SDimitry Andric 
2090b57cec5SDimitry Andric       // Handle "parameterized" attributes
2100b57cec5SDimitry Andric       if (!LateAttrs || !isAttributeLateParsed(*AttrName)) {
21181ad6265SDimitry Andric         ParseGNUAttributeArgs(AttrName, AttrNameLoc, Attrs, &EndLoc, nullptr,
2120b57cec5SDimitry Andric                               SourceLocation(), ParsedAttr::AS_GNU, D);
2130b57cec5SDimitry Andric         continue;
2140b57cec5SDimitry Andric       }
2150b57cec5SDimitry Andric 
2160b57cec5SDimitry Andric       // Handle attributes with arguments that require late parsing.
2170b57cec5SDimitry Andric       LateParsedAttribute *LA =
2180b57cec5SDimitry Andric           new LateParsedAttribute(this, *AttrName, AttrNameLoc);
2190b57cec5SDimitry Andric       LateAttrs->push_back(LA);
2200b57cec5SDimitry Andric 
2210b57cec5SDimitry Andric       // Attributes in a class are parsed at the end of the class, along
2220b57cec5SDimitry Andric       // with other late-parsed declarations.
2230b57cec5SDimitry Andric       if (!ClassStack.empty() && !LateAttrs->parseSoon())
2240b57cec5SDimitry Andric         getCurrentClass().LateParsedDeclarations.push_back(LA);
2250b57cec5SDimitry Andric 
2260b57cec5SDimitry Andric       // Be sure ConsumeAndStoreUntil doesn't see the start l_paren, since it
2270b57cec5SDimitry Andric       // recursively consumes balanced parens.
2280b57cec5SDimitry Andric       LA->Toks.push_back(Tok);
2290b57cec5SDimitry Andric       ConsumeParen();
2300b57cec5SDimitry Andric       // Consume everything up to and including the matching right parens.
2310b57cec5SDimitry Andric       ConsumeAndStoreUntil(tok::r_paren, LA->Toks, /*StopAtSemi=*/true);
2320b57cec5SDimitry Andric 
2330b57cec5SDimitry Andric       Token Eof;
2340b57cec5SDimitry Andric       Eof.startToken();
2350b57cec5SDimitry Andric       Eof.setLocation(Tok.getLocation());
2360b57cec5SDimitry Andric       LA->Toks.push_back(Eof);
2370b57cec5SDimitry Andric     } while (Tok.is(tok::comma));
2380b57cec5SDimitry Andric 
2390b57cec5SDimitry Andric     if (ExpectAndConsume(tok::r_paren))
2400b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
2410b57cec5SDimitry Andric     SourceLocation Loc = Tok.getLocation();
2420b57cec5SDimitry Andric     if (ExpectAndConsume(tok::r_paren))
2430b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
24481ad6265SDimitry Andric     EndLoc = Loc;
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric     // If this was declared in a macro, attach the macro IdentifierInfo to the
2470b57cec5SDimitry Andric     // parsed attribute.
2480b57cec5SDimitry Andric     auto &SM = PP.getSourceManager();
2490b57cec5SDimitry Andric     if (!SM.isWrittenInBuiltinFile(SM.getSpellingLoc(AttrTokLoc)) &&
2500b57cec5SDimitry Andric         FindLocsWithCommonFileID(PP, AttrTokLoc, Loc)) {
2510b57cec5SDimitry Andric       CharSourceRange ExpansionRange = SM.getExpansionRange(AttrTokLoc);
2520b57cec5SDimitry Andric       StringRef FoundName =
2530b57cec5SDimitry Andric           Lexer::getSourceText(ExpansionRange, SM, PP.getLangOpts());
2540b57cec5SDimitry Andric       IdentifierInfo *MacroII = PP.getIdentifierInfo(FoundName);
2550b57cec5SDimitry Andric 
256fe6060f1SDimitry Andric       for (unsigned i = OldNumAttrs; i < Attrs.size(); ++i)
257fe6060f1SDimitry Andric         Attrs[i].setMacroIdentifier(MacroII, ExpansionRange.getBegin());
2580b57cec5SDimitry Andric 
2590b57cec5SDimitry Andric       if (LateAttrs) {
2600b57cec5SDimitry Andric         for (unsigned i = OldNumLateAttrs; i < LateAttrs->size(); ++i)
2610b57cec5SDimitry Andric           (*LateAttrs)[i]->MacroII = MacroII;
2620b57cec5SDimitry Andric       }
2630b57cec5SDimitry Andric     }
2640b57cec5SDimitry Andric   }
265fe6060f1SDimitry Andric 
26681ad6265SDimitry Andric   Attrs.Range = SourceRange(StartLoc, EndLoc);
2670b57cec5SDimitry Andric }
2680b57cec5SDimitry Andric 
2690b57cec5SDimitry Andric /// Determine whether the given attribute has an identifier argument.
2700b57cec5SDimitry Andric static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
2710b57cec5SDimitry Andric #define CLANG_ATTR_IDENTIFIER_ARG_LIST
2720b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
2730b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
2740b57cec5SDimitry Andric            .Default(false);
2750b57cec5SDimitry Andric #undef CLANG_ATTR_IDENTIFIER_ARG_LIST
2760b57cec5SDimitry Andric }
2770b57cec5SDimitry Andric 
2780b57cec5SDimitry Andric /// Determine whether the given attribute has a variadic identifier argument.
2790b57cec5SDimitry Andric static bool attributeHasVariadicIdentifierArg(const IdentifierInfo &II) {
2800b57cec5SDimitry Andric #define CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST
2810b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
2820b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
2830b57cec5SDimitry Andric            .Default(false);
2840b57cec5SDimitry Andric #undef CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST
2850b57cec5SDimitry Andric }
2860b57cec5SDimitry Andric 
2870b57cec5SDimitry Andric /// Determine whether the given attribute treats kw_this as an identifier.
2880b57cec5SDimitry Andric static bool attributeTreatsKeywordThisAsIdentifier(const IdentifierInfo &II) {
2890b57cec5SDimitry Andric #define CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST
2900b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
2910b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
2920b57cec5SDimitry Andric            .Default(false);
2930b57cec5SDimitry Andric #undef CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST
2940b57cec5SDimitry Andric }
2950b57cec5SDimitry Andric 
29681ad6265SDimitry Andric /// Determine if an attribute accepts parameter packs.
29781ad6265SDimitry Andric static bool attributeAcceptsExprPack(const IdentifierInfo &II) {
29881ad6265SDimitry Andric #define CLANG_ATTR_ACCEPTS_EXPR_PACK
29981ad6265SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
30081ad6265SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
30181ad6265SDimitry Andric       .Default(false);
30281ad6265SDimitry Andric #undef CLANG_ATTR_ACCEPTS_EXPR_PACK
30381ad6265SDimitry Andric }
30481ad6265SDimitry Andric 
3050b57cec5SDimitry Andric /// Determine whether the given attribute parses a type argument.
3060b57cec5SDimitry Andric static bool attributeIsTypeArgAttr(const IdentifierInfo &II) {
3070b57cec5SDimitry Andric #define CLANG_ATTR_TYPE_ARG_LIST
3080b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
3090b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
3100b57cec5SDimitry Andric            .Default(false);
3110b57cec5SDimitry Andric #undef CLANG_ATTR_TYPE_ARG_LIST
3120b57cec5SDimitry Andric }
3130b57cec5SDimitry Andric 
3140b57cec5SDimitry Andric /// Determine whether the given attribute requires parsing its arguments
3150b57cec5SDimitry Andric /// in an unevaluated context or not.
3160b57cec5SDimitry Andric static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) {
3170b57cec5SDimitry Andric #define CLANG_ATTR_ARG_CONTEXT_LIST
3180b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
3190b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
3200b57cec5SDimitry Andric            .Default(false);
3210b57cec5SDimitry Andric #undef CLANG_ATTR_ARG_CONTEXT_LIST
3220b57cec5SDimitry Andric }
3230b57cec5SDimitry Andric 
3240b57cec5SDimitry Andric IdentifierLoc *Parser::ParseIdentifierLoc() {
3250b57cec5SDimitry Andric   assert(Tok.is(tok::identifier) && "expected an identifier");
3260b57cec5SDimitry Andric   IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
3270b57cec5SDimitry Andric                                             Tok.getLocation(),
3280b57cec5SDimitry Andric                                             Tok.getIdentifierInfo());
3290b57cec5SDimitry Andric   ConsumeToken();
3300b57cec5SDimitry Andric   return IL;
3310b57cec5SDimitry Andric }
3320b57cec5SDimitry Andric 
3330b57cec5SDimitry Andric void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
3340b57cec5SDimitry Andric                                        SourceLocation AttrNameLoc,
3350b57cec5SDimitry Andric                                        ParsedAttributes &Attrs,
3360b57cec5SDimitry Andric                                        IdentifierInfo *ScopeName,
3370b57cec5SDimitry Andric                                        SourceLocation ScopeLoc,
3380b57cec5SDimitry Andric                                        ParsedAttr::Syntax Syntax) {
3390b57cec5SDimitry Andric   BalancedDelimiterTracker Parens(*this, tok::l_paren);
3400b57cec5SDimitry Andric   Parens.consumeOpen();
3410b57cec5SDimitry Andric 
3420b57cec5SDimitry Andric   TypeResult T;
3430b57cec5SDimitry Andric   if (Tok.isNot(tok::r_paren))
3440b57cec5SDimitry Andric     T = ParseTypeName();
3450b57cec5SDimitry Andric 
3460b57cec5SDimitry Andric   if (Parens.consumeClose())
3470b57cec5SDimitry Andric     return;
3480b57cec5SDimitry Andric 
3490b57cec5SDimitry Andric   if (T.isInvalid())
3500b57cec5SDimitry Andric     return;
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric   if (T.isUsable())
3530b57cec5SDimitry Andric     Attrs.addNewTypeAttr(&AttrName,
3540b57cec5SDimitry Andric                          SourceRange(AttrNameLoc, Parens.getCloseLocation()),
3550b57cec5SDimitry Andric                          ScopeName, ScopeLoc, T.get(), Syntax);
3560b57cec5SDimitry Andric   else
3570b57cec5SDimitry Andric     Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()),
3580b57cec5SDimitry Andric                  ScopeName, ScopeLoc, nullptr, 0, Syntax);
3590b57cec5SDimitry Andric }
3600b57cec5SDimitry Andric 
3610b57cec5SDimitry Andric unsigned Parser::ParseAttributeArgsCommon(
3620b57cec5SDimitry Andric     IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
3630b57cec5SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
3640b57cec5SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax) {
3650b57cec5SDimitry Andric   // Ignore the left paren location for now.
3660b57cec5SDimitry Andric   ConsumeParen();
3670b57cec5SDimitry Andric 
3680b57cec5SDimitry Andric   bool ChangeKWThisToIdent = attributeTreatsKeywordThisAsIdentifier(*AttrName);
369a7dea167SDimitry Andric   bool AttributeIsTypeArgAttr = attributeIsTypeArgAttr(*AttrName);
37081ad6265SDimitry Andric   bool AttributeHasVariadicIdentifierArg =
37181ad6265SDimitry Andric       attributeHasVariadicIdentifierArg(*AttrName);
3720b57cec5SDimitry Andric 
3730b57cec5SDimitry Andric   // Interpret "kw_this" as an identifier if the attributed requests it.
3740b57cec5SDimitry Andric   if (ChangeKWThisToIdent && Tok.is(tok::kw_this))
3750b57cec5SDimitry Andric     Tok.setKind(tok::identifier);
3760b57cec5SDimitry Andric 
3770b57cec5SDimitry Andric   ArgsVector ArgExprs;
3780b57cec5SDimitry Andric   if (Tok.is(tok::identifier)) {
3790b57cec5SDimitry Andric     // If this attribute wants an 'identifier' argument, make it so.
38081ad6265SDimitry Andric     bool IsIdentifierArg = AttributeHasVariadicIdentifierArg ||
38181ad6265SDimitry Andric                            attributeHasIdentifierArg(*AttrName);
3820b57cec5SDimitry Andric     ParsedAttr::Kind AttrKind =
383a7dea167SDimitry Andric         ParsedAttr::getParsedKind(AttrName, ScopeName, Syntax);
3840b57cec5SDimitry Andric 
3850b57cec5SDimitry Andric     // If we don't know how to parse this attribute, but this is the only
3860b57cec5SDimitry Andric     // token in this argument, assume it's meant to be an identifier.
3870b57cec5SDimitry Andric     if (AttrKind == ParsedAttr::UnknownAttribute ||
3880b57cec5SDimitry Andric         AttrKind == ParsedAttr::IgnoredAttribute) {
3890b57cec5SDimitry Andric       const Token &Next = NextToken();
3900b57cec5SDimitry Andric       IsIdentifierArg = Next.isOneOf(tok::r_paren, tok::comma);
3910b57cec5SDimitry Andric     }
3920b57cec5SDimitry Andric 
3930b57cec5SDimitry Andric     if (IsIdentifierArg)
3940b57cec5SDimitry Andric       ArgExprs.push_back(ParseIdentifierLoc());
3950b57cec5SDimitry Andric   }
3960b57cec5SDimitry Andric 
397a7dea167SDimitry Andric   ParsedType TheParsedType;
3980b57cec5SDimitry Andric   if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
3990b57cec5SDimitry Andric     // Eat the comma.
4000b57cec5SDimitry Andric     if (!ArgExprs.empty())
4010b57cec5SDimitry Andric       ConsumeToken();
4020b57cec5SDimitry Andric 
403a7dea167SDimitry Andric     if (AttributeIsTypeArgAttr) {
40481ad6265SDimitry Andric       // FIXME: Multiple type arguments are not implemented.
405a7dea167SDimitry Andric       TypeResult T = ParseTypeName();
406a7dea167SDimitry Andric       if (T.isInvalid()) {
407a7dea167SDimitry Andric         SkipUntil(tok::r_paren, StopAtSemi);
408a7dea167SDimitry Andric         return 0;
409a7dea167SDimitry Andric       }
410a7dea167SDimitry Andric       if (T.isUsable())
411a7dea167SDimitry Andric         TheParsedType = T.get();
41281ad6265SDimitry Andric     } else if (AttributeHasVariadicIdentifierArg) {
41381ad6265SDimitry Andric       // Parse variadic identifier arg. This can either consume identifiers or
41481ad6265SDimitry Andric       // expressions. Variadic identifier args do not support parameter packs
41581ad6265SDimitry Andric       // because those are typically used for attributes with enumeration
41681ad6265SDimitry Andric       // arguments, and those enumerations are not something the user could
41781ad6265SDimitry Andric       // express via a pack.
41881ad6265SDimitry Andric       do {
41981ad6265SDimitry Andric         // Interpret "kw_this" as an identifier if the attributed requests it.
42081ad6265SDimitry Andric         if (ChangeKWThisToIdent && Tok.is(tok::kw_this))
42181ad6265SDimitry Andric           Tok.setKind(tok::identifier);
42281ad6265SDimitry Andric 
42381ad6265SDimitry Andric         ExprResult ArgExpr;
42481ad6265SDimitry Andric         if (Tok.is(tok::identifier)) {
4250b57cec5SDimitry Andric           ArgExprs.push_back(ParseIdentifierLoc());
4260b57cec5SDimitry Andric         } else {
4270b57cec5SDimitry Andric           bool Uneval = attributeParsedArgsUnevaluated(*AttrName);
4280b57cec5SDimitry Andric           EnterExpressionEvaluationContext Unevaluated(
4290b57cec5SDimitry Andric               Actions,
4300b57cec5SDimitry Andric               Uneval ? Sema::ExpressionEvaluationContext::Unevaluated
4310b57cec5SDimitry Andric                      : Sema::ExpressionEvaluationContext::ConstantEvaluated);
4320b57cec5SDimitry Andric 
4330b57cec5SDimitry Andric           ExprResult ArgExpr(
4340b57cec5SDimitry Andric               Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()));
43581ad6265SDimitry Andric 
4360b57cec5SDimitry Andric           if (ArgExpr.isInvalid()) {
4370b57cec5SDimitry Andric             SkipUntil(tok::r_paren, StopAtSemi);
4380b57cec5SDimitry Andric             return 0;
4390b57cec5SDimitry Andric           }
4400b57cec5SDimitry Andric           ArgExprs.push_back(ArgExpr.get());
4410b57cec5SDimitry Andric         }
4420b57cec5SDimitry Andric         // Eat the comma, move to the next argument
4430b57cec5SDimitry Andric       } while (TryConsumeToken(tok::comma));
44481ad6265SDimitry Andric     } else {
44581ad6265SDimitry Andric       // General case. Parse all available expressions.
44681ad6265SDimitry Andric       bool Uneval = attributeParsedArgsUnevaluated(*AttrName);
44781ad6265SDimitry Andric       EnterExpressionEvaluationContext Unevaluated(
44881ad6265SDimitry Andric           Actions, Uneval
44981ad6265SDimitry Andric                        ? Sema::ExpressionEvaluationContext::Unevaluated
45081ad6265SDimitry Andric                        : Sema::ExpressionEvaluationContext::ConstantEvaluated);
45181ad6265SDimitry Andric 
45281ad6265SDimitry Andric       ExprVector ParsedExprs;
453*bdd1243dSDimitry Andric       if (ParseExpressionList(ParsedExprs, llvm::function_ref<void()>(),
45481ad6265SDimitry Andric                               /*FailImmediatelyOnInvalidExpr=*/true,
45581ad6265SDimitry Andric                               /*EarlyTypoCorrection=*/true)) {
45681ad6265SDimitry Andric         SkipUntil(tok::r_paren, StopAtSemi);
45781ad6265SDimitry Andric         return 0;
45881ad6265SDimitry Andric       }
45981ad6265SDimitry Andric 
46081ad6265SDimitry Andric       // Pack expansion must currently be explicitly supported by an attribute.
46181ad6265SDimitry Andric       for (size_t I = 0; I < ParsedExprs.size(); ++I) {
46281ad6265SDimitry Andric         if (!isa<PackExpansionExpr>(ParsedExprs[I]))
46381ad6265SDimitry Andric           continue;
46481ad6265SDimitry Andric 
46581ad6265SDimitry Andric         if (!attributeAcceptsExprPack(*AttrName)) {
46681ad6265SDimitry Andric           Diag(Tok.getLocation(),
46781ad6265SDimitry Andric                diag::err_attribute_argument_parm_pack_not_supported)
46881ad6265SDimitry Andric               << AttrName;
46981ad6265SDimitry Andric           SkipUntil(tok::r_paren, StopAtSemi);
47081ad6265SDimitry Andric           return 0;
47181ad6265SDimitry Andric         }
47281ad6265SDimitry Andric       }
47381ad6265SDimitry Andric 
47481ad6265SDimitry Andric       ArgExprs.insert(ArgExprs.end(), ParsedExprs.begin(), ParsedExprs.end());
47581ad6265SDimitry Andric     }
4760b57cec5SDimitry Andric   }
4770b57cec5SDimitry Andric 
4780b57cec5SDimitry Andric   SourceLocation RParen = Tok.getLocation();
4790b57cec5SDimitry Andric   if (!ExpectAndConsume(tok::r_paren)) {
4800b57cec5SDimitry Andric     SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
481a7dea167SDimitry Andric 
482a7dea167SDimitry Andric     if (AttributeIsTypeArgAttr && !TheParsedType.get().isNull()) {
483a7dea167SDimitry Andric       Attrs.addNewTypeAttr(AttrName, SourceRange(AttrNameLoc, RParen),
484a7dea167SDimitry Andric                            ScopeName, ScopeLoc, TheParsedType, Syntax);
485a7dea167SDimitry Andric     } else {
4860b57cec5SDimitry Andric       Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
4870b57cec5SDimitry Andric                    ArgExprs.data(), ArgExprs.size(), Syntax);
4880b57cec5SDimitry Andric     }
489a7dea167SDimitry Andric   }
4900b57cec5SDimitry Andric 
4910b57cec5SDimitry Andric   if (EndLoc)
4920b57cec5SDimitry Andric     *EndLoc = RParen;
4930b57cec5SDimitry Andric 
494a7dea167SDimitry Andric   return static_cast<unsigned>(ArgExprs.size() + !TheParsedType.get().isNull());
4950b57cec5SDimitry Andric }
4960b57cec5SDimitry Andric 
4970b57cec5SDimitry Andric /// Parse the arguments to a parameterized GNU attribute or
4980b57cec5SDimitry Andric /// a C++11 attribute in "gnu" namespace.
49981ad6265SDimitry Andric void Parser::ParseGNUAttributeArgs(
50081ad6265SDimitry Andric     IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
50181ad6265SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
50281ad6265SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax, Declarator *D) {
5030b57cec5SDimitry Andric 
5040b57cec5SDimitry Andric   assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
5050b57cec5SDimitry Andric 
5060b57cec5SDimitry Andric   ParsedAttr::Kind AttrKind =
507a7dea167SDimitry Andric       ParsedAttr::getParsedKind(AttrName, ScopeName, Syntax);
5080b57cec5SDimitry Andric 
5090b57cec5SDimitry Andric   if (AttrKind == ParsedAttr::AT_Availability) {
5100b57cec5SDimitry Andric     ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
5110b57cec5SDimitry Andric                                ScopeLoc, Syntax);
5120b57cec5SDimitry Andric     return;
5130b57cec5SDimitry Andric   } else if (AttrKind == ParsedAttr::AT_ExternalSourceSymbol) {
5140b57cec5SDimitry Andric     ParseExternalSourceSymbolAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
5150b57cec5SDimitry Andric                                        ScopeName, ScopeLoc, Syntax);
5160b57cec5SDimitry Andric     return;
5170b57cec5SDimitry Andric   } else if (AttrKind == ParsedAttr::AT_ObjCBridgeRelated) {
5180b57cec5SDimitry Andric     ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
5190b57cec5SDimitry Andric                                     ScopeName, ScopeLoc, Syntax);
5200b57cec5SDimitry Andric     return;
521e8d8bef9SDimitry Andric   } else if (AttrKind == ParsedAttr::AT_SwiftNewType) {
522e8d8bef9SDimitry Andric     ParseSwiftNewTypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
523e8d8bef9SDimitry Andric                                ScopeLoc, Syntax);
524e8d8bef9SDimitry Andric     return;
5250b57cec5SDimitry Andric   } else if (AttrKind == ParsedAttr::AT_TypeTagForDatatype) {
5260b57cec5SDimitry Andric     ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
5270b57cec5SDimitry Andric                                      ScopeName, ScopeLoc, Syntax);
5280b57cec5SDimitry Andric     return;
5290b57cec5SDimitry Andric   } else if (attributeIsTypeArgAttr(*AttrName)) {
53081ad6265SDimitry Andric     ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, ScopeName,
5310b57cec5SDimitry Andric                               ScopeLoc, Syntax);
5320b57cec5SDimitry Andric     return;
5330b57cec5SDimitry Andric   }
5340b57cec5SDimitry Andric 
5350b57cec5SDimitry Andric   // These may refer to the function arguments, but need to be parsed early to
5360b57cec5SDimitry Andric   // participate in determining whether it's a redeclaration.
537*bdd1243dSDimitry Andric   std::optional<ParseScope> PrototypeScope;
5380b57cec5SDimitry Andric   if (normalizeAttrName(AttrName->getName()) == "enable_if" &&
5390b57cec5SDimitry Andric       D && D->isFunctionDeclarator()) {
5400b57cec5SDimitry Andric     DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo();
5410b57cec5SDimitry Andric     PrototypeScope.emplace(this, Scope::FunctionPrototypeScope |
5420b57cec5SDimitry Andric                                      Scope::FunctionDeclarationScope |
5430b57cec5SDimitry Andric                                      Scope::DeclScope);
5440b57cec5SDimitry Andric     for (unsigned i = 0; i != FTI.NumParams; ++i) {
5450b57cec5SDimitry Andric       ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param);
5460b57cec5SDimitry Andric       Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param);
5470b57cec5SDimitry Andric     }
5480b57cec5SDimitry Andric   }
5490b57cec5SDimitry Andric 
5500b57cec5SDimitry Andric   ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
5510b57cec5SDimitry Andric                            ScopeLoc, Syntax);
5520b57cec5SDimitry Andric }
5530b57cec5SDimitry Andric 
5540b57cec5SDimitry Andric unsigned Parser::ParseClangAttributeArgs(
5550b57cec5SDimitry Andric     IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
5560b57cec5SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
5570b57cec5SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax) {
5580b57cec5SDimitry Andric   assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
5590b57cec5SDimitry Andric 
5600b57cec5SDimitry Andric   ParsedAttr::Kind AttrKind =
561a7dea167SDimitry Andric       ParsedAttr::getParsedKind(AttrName, ScopeName, Syntax);
5620b57cec5SDimitry Andric 
5630b57cec5SDimitry Andric   switch (AttrKind) {
5640b57cec5SDimitry Andric   default:
5650b57cec5SDimitry Andric     return ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
5660b57cec5SDimitry Andric                                     ScopeName, ScopeLoc, Syntax);
5670b57cec5SDimitry Andric   case ParsedAttr::AT_ExternalSourceSymbol:
5680b57cec5SDimitry Andric     ParseExternalSourceSymbolAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
5690b57cec5SDimitry Andric                                        ScopeName, ScopeLoc, Syntax);
5700b57cec5SDimitry Andric     break;
5710b57cec5SDimitry Andric   case ParsedAttr::AT_Availability:
5720b57cec5SDimitry Andric     ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
5730b57cec5SDimitry Andric                                ScopeLoc, Syntax);
5740b57cec5SDimitry Andric     break;
5750b57cec5SDimitry Andric   case ParsedAttr::AT_ObjCBridgeRelated:
5760b57cec5SDimitry Andric     ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
5770b57cec5SDimitry Andric                                     ScopeName, ScopeLoc, Syntax);
5780b57cec5SDimitry Andric     break;
579e8d8bef9SDimitry Andric   case ParsedAttr::AT_SwiftNewType:
580e8d8bef9SDimitry Andric     ParseSwiftNewTypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
581e8d8bef9SDimitry Andric                                ScopeLoc, Syntax);
582e8d8bef9SDimitry Andric     break;
5830b57cec5SDimitry Andric   case ParsedAttr::AT_TypeTagForDatatype:
5840b57cec5SDimitry Andric     ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
5850b57cec5SDimitry Andric                                      ScopeName, ScopeLoc, Syntax);
5860b57cec5SDimitry Andric     break;
5870b57cec5SDimitry Andric   }
5880b57cec5SDimitry Andric   return !Attrs.empty() ? Attrs.begin()->getNumArgs() : 0;
5890b57cec5SDimitry Andric }
5900b57cec5SDimitry Andric 
5910b57cec5SDimitry Andric bool Parser::ParseMicrosoftDeclSpecArgs(IdentifierInfo *AttrName,
5920b57cec5SDimitry Andric                                         SourceLocation AttrNameLoc,
5930b57cec5SDimitry Andric                                         ParsedAttributes &Attrs) {
59481ad6265SDimitry Andric   unsigned ExistingAttrs = Attrs.size();
59581ad6265SDimitry Andric 
5960b57cec5SDimitry Andric   // If the attribute isn't known, we will not attempt to parse any
5970b57cec5SDimitry Andric   // arguments.
59881ad6265SDimitry Andric   if (!hasAttribute(AttributeCommonInfo::Syntax::AS_Declspec, nullptr, AttrName,
5990b57cec5SDimitry Andric                     getTargetInfo(), getLangOpts())) {
6000b57cec5SDimitry Andric     // Eat the left paren, then skip to the ending right paren.
6010b57cec5SDimitry Andric     ConsumeParen();
6020b57cec5SDimitry Andric     SkipUntil(tok::r_paren);
6030b57cec5SDimitry Andric     return false;
6040b57cec5SDimitry Andric   }
6050b57cec5SDimitry Andric 
6060b57cec5SDimitry Andric   SourceLocation OpenParenLoc = Tok.getLocation();
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric   if (AttrName->getName() == "property") {
6090b57cec5SDimitry Andric     // The property declspec is more complex in that it can take one or two
6100b57cec5SDimitry Andric     // assignment expressions as a parameter, but the lhs of the assignment
6110b57cec5SDimitry Andric     // must be named get or put.
6120b57cec5SDimitry Andric 
6130b57cec5SDimitry Andric     BalancedDelimiterTracker T(*this, tok::l_paren);
6140b57cec5SDimitry Andric     T.expectAndConsume(diag::err_expected_lparen_after,
6150b57cec5SDimitry Andric                        AttrName->getNameStart(), tok::r_paren);
6160b57cec5SDimitry Andric 
6170b57cec5SDimitry Andric     enum AccessorKind {
6180b57cec5SDimitry Andric       AK_Invalid = -1,
6190b57cec5SDimitry Andric       AK_Put = 0,
6200b57cec5SDimitry Andric       AK_Get = 1 // indices into AccessorNames
6210b57cec5SDimitry Andric     };
6220b57cec5SDimitry Andric     IdentifierInfo *AccessorNames[] = {nullptr, nullptr};
6230b57cec5SDimitry Andric     bool HasInvalidAccessor = false;
6240b57cec5SDimitry Andric 
6250b57cec5SDimitry Andric     // Parse the accessor specifications.
6260b57cec5SDimitry Andric     while (true) {
6270b57cec5SDimitry Andric       // Stop if this doesn't look like an accessor spec.
6280b57cec5SDimitry Andric       if (!Tok.is(tok::identifier)) {
6290b57cec5SDimitry Andric         // If the user wrote a completely empty list, use a special diagnostic.
6300b57cec5SDimitry Andric         if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
6310b57cec5SDimitry Andric             AccessorNames[AK_Put] == nullptr &&
6320b57cec5SDimitry Andric             AccessorNames[AK_Get] == nullptr) {
6330b57cec5SDimitry Andric           Diag(AttrNameLoc, diag::err_ms_property_no_getter_or_putter);
6340b57cec5SDimitry Andric           break;
6350b57cec5SDimitry Andric         }
6360b57cec5SDimitry Andric 
6370b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
6380b57cec5SDimitry Andric         break;
6390b57cec5SDimitry Andric       }
6400b57cec5SDimitry Andric 
6410b57cec5SDimitry Andric       AccessorKind Kind;
6420b57cec5SDimitry Andric       SourceLocation KindLoc = Tok.getLocation();
6430b57cec5SDimitry Andric       StringRef KindStr = Tok.getIdentifierInfo()->getName();
6440b57cec5SDimitry Andric       if (KindStr == "get") {
6450b57cec5SDimitry Andric         Kind = AK_Get;
6460b57cec5SDimitry Andric       } else if (KindStr == "put") {
6470b57cec5SDimitry Andric         Kind = AK_Put;
6480b57cec5SDimitry Andric 
6490b57cec5SDimitry Andric         // Recover from the common mistake of using 'set' instead of 'put'.
6500b57cec5SDimitry Andric       } else if (KindStr == "set") {
6510b57cec5SDimitry Andric         Diag(KindLoc, diag::err_ms_property_has_set_accessor)
6520b57cec5SDimitry Andric             << FixItHint::CreateReplacement(KindLoc, "put");
6530b57cec5SDimitry Andric         Kind = AK_Put;
6540b57cec5SDimitry Andric 
6550b57cec5SDimitry Andric         // Handle the mistake of forgetting the accessor kind by skipping
6560b57cec5SDimitry Andric         // this accessor.
6570b57cec5SDimitry Andric       } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
6580b57cec5SDimitry Andric         Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
6590b57cec5SDimitry Andric         ConsumeToken();
6600b57cec5SDimitry Andric         HasInvalidAccessor = true;
6610b57cec5SDimitry Andric         goto next_property_accessor;
6620b57cec5SDimitry Andric 
6630b57cec5SDimitry Andric         // Otherwise, complain about the unknown accessor kind.
6640b57cec5SDimitry Andric       } else {
6650b57cec5SDimitry Andric         Diag(KindLoc, diag::err_ms_property_unknown_accessor);
6660b57cec5SDimitry Andric         HasInvalidAccessor = true;
6670b57cec5SDimitry Andric         Kind = AK_Invalid;
6680b57cec5SDimitry Andric 
6690b57cec5SDimitry Andric         // Try to keep parsing unless it doesn't look like an accessor spec.
6700b57cec5SDimitry Andric         if (!NextToken().is(tok::equal))
6710b57cec5SDimitry Andric           break;
6720b57cec5SDimitry Andric       }
6730b57cec5SDimitry Andric 
6740b57cec5SDimitry Andric       // Consume the identifier.
6750b57cec5SDimitry Andric       ConsumeToken();
6760b57cec5SDimitry Andric 
6770b57cec5SDimitry Andric       // Consume the '='.
6780b57cec5SDimitry Andric       if (!TryConsumeToken(tok::equal)) {
6790b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
6800b57cec5SDimitry Andric             << KindStr;
6810b57cec5SDimitry Andric         break;
6820b57cec5SDimitry Andric       }
6830b57cec5SDimitry Andric 
6840b57cec5SDimitry Andric       // Expect the method name.
6850b57cec5SDimitry Andric       if (!Tok.is(tok::identifier)) {
6860b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
6870b57cec5SDimitry Andric         break;
6880b57cec5SDimitry Andric       }
6890b57cec5SDimitry Andric 
6900b57cec5SDimitry Andric       if (Kind == AK_Invalid) {
6910b57cec5SDimitry Andric         // Just drop invalid accessors.
6920b57cec5SDimitry Andric       } else if (AccessorNames[Kind] != nullptr) {
6930b57cec5SDimitry Andric         // Complain about the repeated accessor, ignore it, and keep parsing.
6940b57cec5SDimitry Andric         Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
6950b57cec5SDimitry Andric       } else {
6960b57cec5SDimitry Andric         AccessorNames[Kind] = Tok.getIdentifierInfo();
6970b57cec5SDimitry Andric       }
6980b57cec5SDimitry Andric       ConsumeToken();
6990b57cec5SDimitry Andric 
7000b57cec5SDimitry Andric     next_property_accessor:
7010b57cec5SDimitry Andric       // Keep processing accessors until we run out.
7020b57cec5SDimitry Andric       if (TryConsumeToken(tok::comma))
7030b57cec5SDimitry Andric         continue;
7040b57cec5SDimitry Andric 
7050b57cec5SDimitry Andric       // If we run into the ')', stop without consuming it.
7060b57cec5SDimitry Andric       if (Tok.is(tok::r_paren))
7070b57cec5SDimitry Andric         break;
7080b57cec5SDimitry Andric 
7090b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
7100b57cec5SDimitry Andric       break;
7110b57cec5SDimitry Andric     }
7120b57cec5SDimitry Andric 
7130b57cec5SDimitry Andric     // Only add the property attribute if it was well-formed.
7140b57cec5SDimitry Andric     if (!HasInvalidAccessor)
7150b57cec5SDimitry Andric       Attrs.addNewPropertyAttr(AttrName, AttrNameLoc, nullptr, SourceLocation(),
7160b57cec5SDimitry Andric                                AccessorNames[AK_Get], AccessorNames[AK_Put],
7170b57cec5SDimitry Andric                                ParsedAttr::AS_Declspec);
7180b57cec5SDimitry Andric     T.skipToEnd();
7190b57cec5SDimitry Andric     return !HasInvalidAccessor;
7200b57cec5SDimitry Andric   }
7210b57cec5SDimitry Andric 
7220b57cec5SDimitry Andric   unsigned NumArgs =
7230b57cec5SDimitry Andric       ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, nullptr, nullptr,
7240b57cec5SDimitry Andric                                SourceLocation(), ParsedAttr::AS_Declspec);
7250b57cec5SDimitry Andric 
7260b57cec5SDimitry Andric   // If this attribute's args were parsed, and it was expected to have
7270b57cec5SDimitry Andric   // arguments but none were provided, emit a diagnostic.
72881ad6265SDimitry Andric   if (ExistingAttrs < Attrs.size() && Attrs.back().getMaxArgs() && !NumArgs) {
7290b57cec5SDimitry Andric     Diag(OpenParenLoc, diag::err_attribute_requires_arguments) << AttrName;
7300b57cec5SDimitry Andric     return false;
7310b57cec5SDimitry Andric   }
7320b57cec5SDimitry Andric   return true;
7330b57cec5SDimitry Andric }
7340b57cec5SDimitry Andric 
7350b57cec5SDimitry Andric /// [MS] decl-specifier:
7360b57cec5SDimitry Andric ///             __declspec ( extended-decl-modifier-seq )
7370b57cec5SDimitry Andric ///
7380b57cec5SDimitry Andric /// [MS] extended-decl-modifier-seq:
7390b57cec5SDimitry Andric ///             extended-decl-modifier[opt]
7400b57cec5SDimitry Andric ///             extended-decl-modifier extended-decl-modifier-seq
74181ad6265SDimitry Andric void Parser::ParseMicrosoftDeclSpecs(ParsedAttributes &Attrs) {
7420b57cec5SDimitry Andric   assert(getLangOpts().DeclSpecKeyword && "__declspec keyword is not enabled");
7430b57cec5SDimitry Andric   assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
7440b57cec5SDimitry Andric 
74581ad6265SDimitry Andric   SourceLocation StartLoc = Tok.getLocation();
74681ad6265SDimitry Andric   SourceLocation EndLoc = StartLoc;
74781ad6265SDimitry Andric 
7480b57cec5SDimitry Andric   while (Tok.is(tok::kw___declspec)) {
7490b57cec5SDimitry Andric     ConsumeToken();
7500b57cec5SDimitry Andric     BalancedDelimiterTracker T(*this, tok::l_paren);
7510b57cec5SDimitry Andric     if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
7520b57cec5SDimitry Andric                            tok::r_paren))
7530b57cec5SDimitry Andric       return;
7540b57cec5SDimitry Andric 
7550b57cec5SDimitry Andric     // An empty declspec is perfectly legal and should not warn.  Additionally,
7560b57cec5SDimitry Andric     // you can specify multiple attributes per declspec.
7570b57cec5SDimitry Andric     while (Tok.isNot(tok::r_paren)) {
7580b57cec5SDimitry Andric       // Attribute not present.
7590b57cec5SDimitry Andric       if (TryConsumeToken(tok::comma))
7600b57cec5SDimitry Andric         continue;
7610b57cec5SDimitry Andric 
762349cc55cSDimitry Andric       if (Tok.is(tok::code_completion)) {
763349cc55cSDimitry Andric         cutOffParsing();
764349cc55cSDimitry Andric         Actions.CodeCompleteAttribute(AttributeCommonInfo::AS_Declspec);
765349cc55cSDimitry Andric         return;
766349cc55cSDimitry Andric       }
767349cc55cSDimitry Andric 
7680b57cec5SDimitry Andric       // We expect either a well-known identifier or a generic string.  Anything
7690b57cec5SDimitry Andric       // else is a malformed declspec.
7700b57cec5SDimitry Andric       bool IsString = Tok.getKind() == tok::string_literal;
7710b57cec5SDimitry Andric       if (!IsString && Tok.getKind() != tok::identifier &&
7720b57cec5SDimitry Andric           Tok.getKind() != tok::kw_restrict) {
7730b57cec5SDimitry Andric         Diag(Tok, diag::err_ms_declspec_type);
7740b57cec5SDimitry Andric         T.skipToEnd();
7750b57cec5SDimitry Andric         return;
7760b57cec5SDimitry Andric       }
7770b57cec5SDimitry Andric 
7780b57cec5SDimitry Andric       IdentifierInfo *AttrName;
7790b57cec5SDimitry Andric       SourceLocation AttrNameLoc;
7800b57cec5SDimitry Andric       if (IsString) {
7810b57cec5SDimitry Andric         SmallString<8> StrBuffer;
7820b57cec5SDimitry Andric         bool Invalid = false;
7830b57cec5SDimitry Andric         StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
7840b57cec5SDimitry Andric         if (Invalid) {
7850b57cec5SDimitry Andric           T.skipToEnd();
7860b57cec5SDimitry Andric           return;
7870b57cec5SDimitry Andric         }
7880b57cec5SDimitry Andric         AttrName = PP.getIdentifierInfo(Str);
7890b57cec5SDimitry Andric         AttrNameLoc = ConsumeStringToken();
7900b57cec5SDimitry Andric       } else {
7910b57cec5SDimitry Andric         AttrName = Tok.getIdentifierInfo();
7920b57cec5SDimitry Andric         AttrNameLoc = ConsumeToken();
7930b57cec5SDimitry Andric       }
7940b57cec5SDimitry Andric 
7950b57cec5SDimitry Andric       bool AttrHandled = false;
7960b57cec5SDimitry Andric 
7970b57cec5SDimitry Andric       // Parse attribute arguments.
7980b57cec5SDimitry Andric       if (Tok.is(tok::l_paren))
7990b57cec5SDimitry Andric         AttrHandled = ParseMicrosoftDeclSpecArgs(AttrName, AttrNameLoc, Attrs);
8000b57cec5SDimitry Andric       else if (AttrName->getName() == "property")
8010b57cec5SDimitry Andric         // The property attribute must have an argument list.
8020b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_expected_lparen_after)
8030b57cec5SDimitry Andric             << AttrName->getName();
8040b57cec5SDimitry Andric 
8050b57cec5SDimitry Andric       if (!AttrHandled)
8060b57cec5SDimitry Andric         Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
8070b57cec5SDimitry Andric                      ParsedAttr::AS_Declspec);
8080b57cec5SDimitry Andric     }
8090b57cec5SDimitry Andric     T.consumeClose();
81081ad6265SDimitry Andric     EndLoc = T.getCloseLocation();
8110b57cec5SDimitry Andric   }
81281ad6265SDimitry Andric 
81381ad6265SDimitry Andric   Attrs.Range = SourceRange(StartLoc, EndLoc);
8140b57cec5SDimitry Andric }
8150b57cec5SDimitry Andric 
8160b57cec5SDimitry Andric void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
8170b57cec5SDimitry Andric   // Treat these like attributes
8180b57cec5SDimitry Andric   while (true) {
8190b57cec5SDimitry Andric     switch (Tok.getKind()) {
8200b57cec5SDimitry Andric     case tok::kw___fastcall:
8210b57cec5SDimitry Andric     case tok::kw___stdcall:
8220b57cec5SDimitry Andric     case tok::kw___thiscall:
8230b57cec5SDimitry Andric     case tok::kw___regcall:
8240b57cec5SDimitry Andric     case tok::kw___cdecl:
8250b57cec5SDimitry Andric     case tok::kw___vectorcall:
8260b57cec5SDimitry Andric     case tok::kw___ptr64:
8270b57cec5SDimitry Andric     case tok::kw___w64:
8280b57cec5SDimitry Andric     case tok::kw___ptr32:
8290b57cec5SDimitry Andric     case tok::kw___sptr:
8300b57cec5SDimitry Andric     case tok::kw___uptr: {
8310b57cec5SDimitry Andric       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
8320b57cec5SDimitry Andric       SourceLocation AttrNameLoc = ConsumeToken();
8330b57cec5SDimitry Andric       attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
8340b57cec5SDimitry Andric                    ParsedAttr::AS_Keyword);
8350b57cec5SDimitry Andric       break;
8360b57cec5SDimitry Andric     }
8370b57cec5SDimitry Andric     default:
8380b57cec5SDimitry Andric       return;
8390b57cec5SDimitry Andric     }
8400b57cec5SDimitry Andric   }
8410b57cec5SDimitry Andric }
8420b57cec5SDimitry Andric 
8430b57cec5SDimitry Andric void Parser::DiagnoseAndSkipExtendedMicrosoftTypeAttributes() {
8440b57cec5SDimitry Andric   SourceLocation StartLoc = Tok.getLocation();
8450b57cec5SDimitry Andric   SourceLocation EndLoc = SkipExtendedMicrosoftTypeAttributes();
8460b57cec5SDimitry Andric 
8470b57cec5SDimitry Andric   if (EndLoc.isValid()) {
8480b57cec5SDimitry Andric     SourceRange Range(StartLoc, EndLoc);
8490b57cec5SDimitry Andric     Diag(StartLoc, diag::warn_microsoft_qualifiers_ignored) << Range;
8500b57cec5SDimitry Andric   }
8510b57cec5SDimitry Andric }
8520b57cec5SDimitry Andric 
8530b57cec5SDimitry Andric SourceLocation Parser::SkipExtendedMicrosoftTypeAttributes() {
8540b57cec5SDimitry Andric   SourceLocation EndLoc;
8550b57cec5SDimitry Andric 
8560b57cec5SDimitry Andric   while (true) {
8570b57cec5SDimitry Andric     switch (Tok.getKind()) {
8580b57cec5SDimitry Andric     case tok::kw_const:
8590b57cec5SDimitry Andric     case tok::kw_volatile:
8600b57cec5SDimitry Andric     case tok::kw___fastcall:
8610b57cec5SDimitry Andric     case tok::kw___stdcall:
8620b57cec5SDimitry Andric     case tok::kw___thiscall:
8630b57cec5SDimitry Andric     case tok::kw___cdecl:
8640b57cec5SDimitry Andric     case tok::kw___vectorcall:
8650b57cec5SDimitry Andric     case tok::kw___ptr32:
8660b57cec5SDimitry Andric     case tok::kw___ptr64:
8670b57cec5SDimitry Andric     case tok::kw___w64:
8680b57cec5SDimitry Andric     case tok::kw___unaligned:
8690b57cec5SDimitry Andric     case tok::kw___sptr:
8700b57cec5SDimitry Andric     case tok::kw___uptr:
8710b57cec5SDimitry Andric       EndLoc = ConsumeToken();
8720b57cec5SDimitry Andric       break;
8730b57cec5SDimitry Andric     default:
8740b57cec5SDimitry Andric       return EndLoc;
8750b57cec5SDimitry Andric     }
8760b57cec5SDimitry Andric   }
8770b57cec5SDimitry Andric }
8780b57cec5SDimitry Andric 
8790b57cec5SDimitry Andric void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
8800b57cec5SDimitry Andric   // Treat these like attributes
8810b57cec5SDimitry Andric   while (Tok.is(tok::kw___pascal)) {
8820b57cec5SDimitry Andric     IdentifierInfo *AttrName = Tok.getIdentifierInfo();
8830b57cec5SDimitry Andric     SourceLocation AttrNameLoc = ConsumeToken();
8840b57cec5SDimitry Andric     attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
8850b57cec5SDimitry Andric                  ParsedAttr::AS_Keyword);
8860b57cec5SDimitry Andric   }
8870b57cec5SDimitry Andric }
8880b57cec5SDimitry Andric 
8890b57cec5SDimitry Andric void Parser::ParseOpenCLKernelAttributes(ParsedAttributes &attrs) {
8900b57cec5SDimitry Andric   // Treat these like attributes
8910b57cec5SDimitry Andric   while (Tok.is(tok::kw___kernel)) {
8920b57cec5SDimitry Andric     IdentifierInfo *AttrName = Tok.getIdentifierInfo();
8930b57cec5SDimitry Andric     SourceLocation AttrNameLoc = ConsumeToken();
8940b57cec5SDimitry Andric     attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
8950b57cec5SDimitry Andric                  ParsedAttr::AS_Keyword);
8960b57cec5SDimitry Andric   }
8970b57cec5SDimitry Andric }
8980b57cec5SDimitry Andric 
89981ad6265SDimitry Andric void Parser::ParseCUDAFunctionAttributes(ParsedAttributes &attrs) {
90081ad6265SDimitry Andric   while (Tok.is(tok::kw___noinline__)) {
90181ad6265SDimitry Andric     IdentifierInfo *AttrName = Tok.getIdentifierInfo();
90281ad6265SDimitry Andric     SourceLocation AttrNameLoc = ConsumeToken();
90381ad6265SDimitry Andric     attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
90481ad6265SDimitry Andric                  ParsedAttr::AS_Keyword);
90581ad6265SDimitry Andric   }
90681ad6265SDimitry Andric }
90781ad6265SDimitry Andric 
9080b57cec5SDimitry Andric void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) {
9090b57cec5SDimitry Andric   IdentifierInfo *AttrName = Tok.getIdentifierInfo();
9100b57cec5SDimitry Andric   SourceLocation AttrNameLoc = Tok.getLocation();
9110b57cec5SDimitry Andric   Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
9120b57cec5SDimitry Andric                ParsedAttr::AS_Keyword);
9130b57cec5SDimitry Andric }
9140b57cec5SDimitry Andric 
915*bdd1243dSDimitry Andric bool Parser::isHLSLQualifier(const Token &Tok) const {
916*bdd1243dSDimitry Andric   return Tok.is(tok::kw_groupshared);
917*bdd1243dSDimitry Andric }
918*bdd1243dSDimitry Andric 
919*bdd1243dSDimitry Andric void Parser::ParseHLSLQualifiers(ParsedAttributes &Attrs) {
920*bdd1243dSDimitry Andric   IdentifierInfo *AttrName = Tok.getIdentifierInfo();
921*bdd1243dSDimitry Andric   SourceLocation AttrNameLoc = ConsumeToken();
922*bdd1243dSDimitry Andric   Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
923*bdd1243dSDimitry Andric                ParsedAttr::AS_Keyword);
924*bdd1243dSDimitry Andric }
925*bdd1243dSDimitry Andric 
9260b57cec5SDimitry Andric void Parser::ParseNullabilityTypeSpecifiers(ParsedAttributes &attrs) {
9270b57cec5SDimitry Andric   // Treat these like attributes, even though they're type specifiers.
9280b57cec5SDimitry Andric   while (true) {
9290b57cec5SDimitry Andric     switch (Tok.getKind()) {
9300b57cec5SDimitry Andric     case tok::kw__Nonnull:
9310b57cec5SDimitry Andric     case tok::kw__Nullable:
932e8d8bef9SDimitry Andric     case tok::kw__Nullable_result:
9330b57cec5SDimitry Andric     case tok::kw__Null_unspecified: {
9340b57cec5SDimitry Andric       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
9350b57cec5SDimitry Andric       SourceLocation AttrNameLoc = ConsumeToken();
9360b57cec5SDimitry Andric       if (!getLangOpts().ObjC)
9370b57cec5SDimitry Andric         Diag(AttrNameLoc, diag::ext_nullability)
9380b57cec5SDimitry Andric           << AttrName;
9390b57cec5SDimitry Andric       attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
9400b57cec5SDimitry Andric                    ParsedAttr::AS_Keyword);
9410b57cec5SDimitry Andric       break;
9420b57cec5SDimitry Andric     }
9430b57cec5SDimitry Andric     default:
9440b57cec5SDimitry Andric       return;
9450b57cec5SDimitry Andric     }
9460b57cec5SDimitry Andric   }
9470b57cec5SDimitry Andric }
9480b57cec5SDimitry Andric 
9490b57cec5SDimitry Andric static bool VersionNumberSeparator(const char Separator) {
9500b57cec5SDimitry Andric   return (Separator == '.' || Separator == '_');
9510b57cec5SDimitry Andric }
9520b57cec5SDimitry Andric 
9530b57cec5SDimitry Andric /// Parse a version number.
9540b57cec5SDimitry Andric ///
9550b57cec5SDimitry Andric /// version:
9560b57cec5SDimitry Andric ///   simple-integer
9570b57cec5SDimitry Andric ///   simple-integer '.' simple-integer
9580b57cec5SDimitry Andric ///   simple-integer '_' simple-integer
9590b57cec5SDimitry Andric ///   simple-integer '.' simple-integer '.' simple-integer
9600b57cec5SDimitry Andric ///   simple-integer '_' simple-integer '_' simple-integer
9610b57cec5SDimitry Andric VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
9620b57cec5SDimitry Andric   Range = SourceRange(Tok.getLocation(), Tok.getEndLoc());
9630b57cec5SDimitry Andric 
9640b57cec5SDimitry Andric   if (!Tok.is(tok::numeric_constant)) {
9650b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
9660b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
9670b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
9680b57cec5SDimitry Andric     return VersionTuple();
9690b57cec5SDimitry Andric   }
9700b57cec5SDimitry Andric 
9710b57cec5SDimitry Andric   // Parse the major (and possibly minor and subminor) versions, which
9720b57cec5SDimitry Andric   // are stored in the numeric constant. We utilize a quirk of the
9730b57cec5SDimitry Andric   // lexer, which is that it handles something like 1.2.3 as a single
9740b57cec5SDimitry Andric   // numeric constant, rather than two separate tokens.
9750b57cec5SDimitry Andric   SmallString<512> Buffer;
9760b57cec5SDimitry Andric   Buffer.resize(Tok.getLength()+1);
9770b57cec5SDimitry Andric   const char *ThisTokBegin = &Buffer[0];
9780b57cec5SDimitry Andric 
9790b57cec5SDimitry Andric   // Get the spelling of the token, which eliminates trigraphs, etc.
9800b57cec5SDimitry Andric   bool Invalid = false;
9810b57cec5SDimitry Andric   unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
9820b57cec5SDimitry Andric   if (Invalid)
9830b57cec5SDimitry Andric     return VersionTuple();
9840b57cec5SDimitry Andric 
9850b57cec5SDimitry Andric   // Parse the major version.
9860b57cec5SDimitry Andric   unsigned AfterMajor = 0;
9870b57cec5SDimitry Andric   unsigned Major = 0;
9880b57cec5SDimitry Andric   while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
9890b57cec5SDimitry Andric     Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
9900b57cec5SDimitry Andric     ++AfterMajor;
9910b57cec5SDimitry Andric   }
9920b57cec5SDimitry Andric 
9930b57cec5SDimitry Andric   if (AfterMajor == 0) {
9940b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
9950b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
9960b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
9970b57cec5SDimitry Andric     return VersionTuple();
9980b57cec5SDimitry Andric   }
9990b57cec5SDimitry Andric 
10000b57cec5SDimitry Andric   if (AfterMajor == ActualLength) {
10010b57cec5SDimitry Andric     ConsumeToken();
10020b57cec5SDimitry Andric 
10030b57cec5SDimitry Andric     // We only had a single version component.
10040b57cec5SDimitry Andric     if (Major == 0) {
10050b57cec5SDimitry Andric       Diag(Tok, diag::err_zero_version);
10060b57cec5SDimitry Andric       return VersionTuple();
10070b57cec5SDimitry Andric     }
10080b57cec5SDimitry Andric 
10090b57cec5SDimitry Andric     return VersionTuple(Major);
10100b57cec5SDimitry Andric   }
10110b57cec5SDimitry Andric 
10120b57cec5SDimitry Andric   const char AfterMajorSeparator = ThisTokBegin[AfterMajor];
10130b57cec5SDimitry Andric   if (!VersionNumberSeparator(AfterMajorSeparator)
10140b57cec5SDimitry Andric       || (AfterMajor + 1 == ActualLength)) {
10150b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
10160b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
10170b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
10180b57cec5SDimitry Andric     return VersionTuple();
10190b57cec5SDimitry Andric   }
10200b57cec5SDimitry Andric 
10210b57cec5SDimitry Andric   // Parse the minor version.
10220b57cec5SDimitry Andric   unsigned AfterMinor = AfterMajor + 1;
10230b57cec5SDimitry Andric   unsigned Minor = 0;
10240b57cec5SDimitry Andric   while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
10250b57cec5SDimitry Andric     Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
10260b57cec5SDimitry Andric     ++AfterMinor;
10270b57cec5SDimitry Andric   }
10280b57cec5SDimitry Andric 
10290b57cec5SDimitry Andric   if (AfterMinor == ActualLength) {
10300b57cec5SDimitry Andric     ConsumeToken();
10310b57cec5SDimitry Andric 
10320b57cec5SDimitry Andric     // We had major.minor.
10330b57cec5SDimitry Andric     if (Major == 0 && Minor == 0) {
10340b57cec5SDimitry Andric       Diag(Tok, diag::err_zero_version);
10350b57cec5SDimitry Andric       return VersionTuple();
10360b57cec5SDimitry Andric     }
10370b57cec5SDimitry Andric 
10380b57cec5SDimitry Andric     return VersionTuple(Major, Minor);
10390b57cec5SDimitry Andric   }
10400b57cec5SDimitry Andric 
10410b57cec5SDimitry Andric   const char AfterMinorSeparator = ThisTokBegin[AfterMinor];
10420b57cec5SDimitry Andric   // If what follows is not a '.' or '_', we have a problem.
10430b57cec5SDimitry Andric   if (!VersionNumberSeparator(AfterMinorSeparator)) {
10440b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
10450b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
10460b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
10470b57cec5SDimitry Andric     return VersionTuple();
10480b57cec5SDimitry Andric   }
10490b57cec5SDimitry Andric 
10500b57cec5SDimitry Andric   // Warn if separators, be it '.' or '_', do not match.
10510b57cec5SDimitry Andric   if (AfterMajorSeparator != AfterMinorSeparator)
10520b57cec5SDimitry Andric     Diag(Tok, diag::warn_expected_consistent_version_separator);
10530b57cec5SDimitry Andric 
10540b57cec5SDimitry Andric   // Parse the subminor version.
10550b57cec5SDimitry Andric   unsigned AfterSubminor = AfterMinor + 1;
10560b57cec5SDimitry Andric   unsigned Subminor = 0;
10570b57cec5SDimitry Andric   while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
10580b57cec5SDimitry Andric     Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
10590b57cec5SDimitry Andric     ++AfterSubminor;
10600b57cec5SDimitry Andric   }
10610b57cec5SDimitry Andric 
10620b57cec5SDimitry Andric   if (AfterSubminor != ActualLength) {
10630b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
10640b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
10650b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
10660b57cec5SDimitry Andric     return VersionTuple();
10670b57cec5SDimitry Andric   }
10680b57cec5SDimitry Andric   ConsumeToken();
10690b57cec5SDimitry Andric   return VersionTuple(Major, Minor, Subminor);
10700b57cec5SDimitry Andric }
10710b57cec5SDimitry Andric 
10720b57cec5SDimitry Andric /// Parse the contents of the "availability" attribute.
10730b57cec5SDimitry Andric ///
10740b57cec5SDimitry Andric /// availability-attribute:
10750b57cec5SDimitry Andric ///   'availability' '(' platform ',' opt-strict version-arg-list,
10760b57cec5SDimitry Andric ///                      opt-replacement, opt-message')'
10770b57cec5SDimitry Andric ///
10780b57cec5SDimitry Andric /// platform:
10790b57cec5SDimitry Andric ///   identifier
10800b57cec5SDimitry Andric ///
10810b57cec5SDimitry Andric /// opt-strict:
10820b57cec5SDimitry Andric ///   'strict' ','
10830b57cec5SDimitry Andric ///
10840b57cec5SDimitry Andric /// version-arg-list:
10850b57cec5SDimitry Andric ///   version-arg
10860b57cec5SDimitry Andric ///   version-arg ',' version-arg-list
10870b57cec5SDimitry Andric ///
10880b57cec5SDimitry Andric /// version-arg:
10890b57cec5SDimitry Andric ///   'introduced' '=' version
10900b57cec5SDimitry Andric ///   'deprecated' '=' version
10910b57cec5SDimitry Andric ///   'obsoleted' = version
10920b57cec5SDimitry Andric ///   'unavailable'
10930b57cec5SDimitry Andric /// opt-replacement:
10940b57cec5SDimitry Andric ///   'replacement' '=' <string>
10950b57cec5SDimitry Andric /// opt-message:
10960b57cec5SDimitry Andric ///   'message' '=' <string>
10970b57cec5SDimitry Andric void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
10980b57cec5SDimitry Andric                                         SourceLocation AvailabilityLoc,
10990b57cec5SDimitry Andric                                         ParsedAttributes &attrs,
11000b57cec5SDimitry Andric                                         SourceLocation *endLoc,
11010b57cec5SDimitry Andric                                         IdentifierInfo *ScopeName,
11020b57cec5SDimitry Andric                                         SourceLocation ScopeLoc,
11030b57cec5SDimitry Andric                                         ParsedAttr::Syntax Syntax) {
11040b57cec5SDimitry Andric   enum { Introduced, Deprecated, Obsoleted, Unknown };
11050b57cec5SDimitry Andric   AvailabilityChange Changes[Unknown];
11060b57cec5SDimitry Andric   ExprResult MessageExpr, ReplacementExpr;
11070b57cec5SDimitry Andric 
11080b57cec5SDimitry Andric   // Opening '('.
11090b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
11100b57cec5SDimitry Andric   if (T.consumeOpen()) {
11110b57cec5SDimitry Andric     Diag(Tok, diag::err_expected) << tok::l_paren;
11120b57cec5SDimitry Andric     return;
11130b57cec5SDimitry Andric   }
11140b57cec5SDimitry Andric 
11150b57cec5SDimitry Andric   // Parse the platform name.
11160b57cec5SDimitry Andric   if (Tok.isNot(tok::identifier)) {
11170b57cec5SDimitry Andric     Diag(Tok, diag::err_availability_expected_platform);
11180b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
11190b57cec5SDimitry Andric     return;
11200b57cec5SDimitry Andric   }
11210b57cec5SDimitry Andric   IdentifierLoc *Platform = ParseIdentifierLoc();
11220b57cec5SDimitry Andric   if (const IdentifierInfo *const Ident = Platform->Ident) {
11230b57cec5SDimitry Andric     // Canonicalize platform name from "macosx" to "macos".
11240b57cec5SDimitry Andric     if (Ident->getName() == "macosx")
11250b57cec5SDimitry Andric       Platform->Ident = PP.getIdentifierInfo("macos");
11260b57cec5SDimitry Andric     // Canonicalize platform name from "macosx_app_extension" to
11270b57cec5SDimitry Andric     // "macos_app_extension".
11280b57cec5SDimitry Andric     else if (Ident->getName() == "macosx_app_extension")
11290b57cec5SDimitry Andric       Platform->Ident = PP.getIdentifierInfo("macos_app_extension");
11300b57cec5SDimitry Andric     else
11310b57cec5SDimitry Andric       Platform->Ident = PP.getIdentifierInfo(
11320b57cec5SDimitry Andric           AvailabilityAttr::canonicalizePlatformName(Ident->getName()));
11330b57cec5SDimitry Andric   }
11340b57cec5SDimitry Andric 
11350b57cec5SDimitry Andric   // Parse the ',' following the platform name.
11360b57cec5SDimitry Andric   if (ExpectAndConsume(tok::comma)) {
11370b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
11380b57cec5SDimitry Andric     return;
11390b57cec5SDimitry Andric   }
11400b57cec5SDimitry Andric 
11410b57cec5SDimitry Andric   // If we haven't grabbed the pointers for the identifiers
11420b57cec5SDimitry Andric   // "introduced", "deprecated", and "obsoleted", do so now.
11430b57cec5SDimitry Andric   if (!Ident_introduced) {
11440b57cec5SDimitry Andric     Ident_introduced = PP.getIdentifierInfo("introduced");
11450b57cec5SDimitry Andric     Ident_deprecated = PP.getIdentifierInfo("deprecated");
11460b57cec5SDimitry Andric     Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
11470b57cec5SDimitry Andric     Ident_unavailable = PP.getIdentifierInfo("unavailable");
11480b57cec5SDimitry Andric     Ident_message = PP.getIdentifierInfo("message");
11490b57cec5SDimitry Andric     Ident_strict = PP.getIdentifierInfo("strict");
11500b57cec5SDimitry Andric     Ident_replacement = PP.getIdentifierInfo("replacement");
11510b57cec5SDimitry Andric   }
11520b57cec5SDimitry Andric 
11530b57cec5SDimitry Andric   // Parse the optional "strict", the optional "replacement" and the set of
11540b57cec5SDimitry Andric   // introductions/deprecations/removals.
11550b57cec5SDimitry Andric   SourceLocation UnavailableLoc, StrictLoc;
11560b57cec5SDimitry Andric   do {
11570b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
11580b57cec5SDimitry Andric       Diag(Tok, diag::err_availability_expected_change);
11590b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
11600b57cec5SDimitry Andric       return;
11610b57cec5SDimitry Andric     }
11620b57cec5SDimitry Andric     IdentifierInfo *Keyword = Tok.getIdentifierInfo();
11630b57cec5SDimitry Andric     SourceLocation KeywordLoc = ConsumeToken();
11640b57cec5SDimitry Andric 
11650b57cec5SDimitry Andric     if (Keyword == Ident_strict) {
11660b57cec5SDimitry Andric       if (StrictLoc.isValid()) {
11670b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_availability_redundant)
11680b57cec5SDimitry Andric           << Keyword << SourceRange(StrictLoc);
11690b57cec5SDimitry Andric       }
11700b57cec5SDimitry Andric       StrictLoc = KeywordLoc;
11710b57cec5SDimitry Andric       continue;
11720b57cec5SDimitry Andric     }
11730b57cec5SDimitry Andric 
11740b57cec5SDimitry Andric     if (Keyword == Ident_unavailable) {
11750b57cec5SDimitry Andric       if (UnavailableLoc.isValid()) {
11760b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_availability_redundant)
11770b57cec5SDimitry Andric           << Keyword << SourceRange(UnavailableLoc);
11780b57cec5SDimitry Andric       }
11790b57cec5SDimitry Andric       UnavailableLoc = KeywordLoc;
11800b57cec5SDimitry Andric       continue;
11810b57cec5SDimitry Andric     }
11820b57cec5SDimitry Andric 
11830b57cec5SDimitry Andric     if (Keyword == Ident_deprecated && Platform->Ident &&
11840b57cec5SDimitry Andric         Platform->Ident->isStr("swift")) {
11850b57cec5SDimitry Andric       // For swift, we deprecate for all versions.
11860b57cec5SDimitry Andric       if (Changes[Deprecated].KeywordLoc.isValid()) {
11870b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_availability_redundant)
11880b57cec5SDimitry Andric           << Keyword
11890b57cec5SDimitry Andric           << SourceRange(Changes[Deprecated].KeywordLoc);
11900b57cec5SDimitry Andric       }
11910b57cec5SDimitry Andric 
11920b57cec5SDimitry Andric       Changes[Deprecated].KeywordLoc = KeywordLoc;
11930b57cec5SDimitry Andric       // Use a fake version here.
11940b57cec5SDimitry Andric       Changes[Deprecated].Version = VersionTuple(1);
11950b57cec5SDimitry Andric       continue;
11960b57cec5SDimitry Andric     }
11970b57cec5SDimitry Andric 
11980b57cec5SDimitry Andric     if (Tok.isNot(tok::equal)) {
11990b57cec5SDimitry Andric       Diag(Tok, diag::err_expected_after) << Keyword << tok::equal;
12000b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
12010b57cec5SDimitry Andric       return;
12020b57cec5SDimitry Andric     }
12030b57cec5SDimitry Andric     ConsumeToken();
12040b57cec5SDimitry Andric     if (Keyword == Ident_message || Keyword == Ident_replacement) {
12050b57cec5SDimitry Andric       if (Tok.isNot(tok::string_literal)) {
12060b57cec5SDimitry Andric         Diag(Tok, diag::err_expected_string_literal)
12070b57cec5SDimitry Andric           << /*Source='availability attribute'*/2;
12080b57cec5SDimitry Andric         SkipUntil(tok::r_paren, StopAtSemi);
12090b57cec5SDimitry Andric         return;
12100b57cec5SDimitry Andric       }
12110b57cec5SDimitry Andric       if (Keyword == Ident_message)
12120b57cec5SDimitry Andric         MessageExpr = ParseStringLiteralExpression();
12130b57cec5SDimitry Andric       else
12140b57cec5SDimitry Andric         ReplacementExpr = ParseStringLiteralExpression();
12150b57cec5SDimitry Andric       // Also reject wide string literals.
12160b57cec5SDimitry Andric       if (StringLiteral *MessageStringLiteral =
12170b57cec5SDimitry Andric               cast_or_null<StringLiteral>(MessageExpr.get())) {
121881ad6265SDimitry Andric         if (!MessageStringLiteral->isOrdinary()) {
12190b57cec5SDimitry Andric           Diag(MessageStringLiteral->getSourceRange().getBegin(),
12200b57cec5SDimitry Andric                diag::err_expected_string_literal)
12210b57cec5SDimitry Andric             << /*Source='availability attribute'*/ 2;
12220b57cec5SDimitry Andric           SkipUntil(tok::r_paren, StopAtSemi);
12230b57cec5SDimitry Andric           return;
12240b57cec5SDimitry Andric         }
12250b57cec5SDimitry Andric       }
12260b57cec5SDimitry Andric       if (Keyword == Ident_message)
12270b57cec5SDimitry Andric         break;
12280b57cec5SDimitry Andric       else
12290b57cec5SDimitry Andric         continue;
12300b57cec5SDimitry Andric     }
12310b57cec5SDimitry Andric 
12320b57cec5SDimitry Andric     // Special handling of 'NA' only when applied to introduced or
12330b57cec5SDimitry Andric     // deprecated.
12340b57cec5SDimitry Andric     if ((Keyword == Ident_introduced || Keyword == Ident_deprecated) &&
12350b57cec5SDimitry Andric         Tok.is(tok::identifier)) {
12360b57cec5SDimitry Andric       IdentifierInfo *NA = Tok.getIdentifierInfo();
12370b57cec5SDimitry Andric       if (NA->getName() == "NA") {
12380b57cec5SDimitry Andric         ConsumeToken();
12390b57cec5SDimitry Andric         if (Keyword == Ident_introduced)
12400b57cec5SDimitry Andric           UnavailableLoc = KeywordLoc;
12410b57cec5SDimitry Andric         continue;
12420b57cec5SDimitry Andric       }
12430b57cec5SDimitry Andric     }
12440b57cec5SDimitry Andric 
12450b57cec5SDimitry Andric     SourceRange VersionRange;
12460b57cec5SDimitry Andric     VersionTuple Version = ParseVersionTuple(VersionRange);
12470b57cec5SDimitry Andric 
12480b57cec5SDimitry Andric     if (Version.empty()) {
12490b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
12500b57cec5SDimitry Andric       return;
12510b57cec5SDimitry Andric     }
12520b57cec5SDimitry Andric 
12530b57cec5SDimitry Andric     unsigned Index;
12540b57cec5SDimitry Andric     if (Keyword == Ident_introduced)
12550b57cec5SDimitry Andric       Index = Introduced;
12560b57cec5SDimitry Andric     else if (Keyword == Ident_deprecated)
12570b57cec5SDimitry Andric       Index = Deprecated;
12580b57cec5SDimitry Andric     else if (Keyword == Ident_obsoleted)
12590b57cec5SDimitry Andric       Index = Obsoleted;
12600b57cec5SDimitry Andric     else
12610b57cec5SDimitry Andric       Index = Unknown;
12620b57cec5SDimitry Andric 
12630b57cec5SDimitry Andric     if (Index < Unknown) {
12640b57cec5SDimitry Andric       if (!Changes[Index].KeywordLoc.isInvalid()) {
12650b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_availability_redundant)
12660b57cec5SDimitry Andric           << Keyword
12670b57cec5SDimitry Andric           << SourceRange(Changes[Index].KeywordLoc,
12680b57cec5SDimitry Andric                          Changes[Index].VersionRange.getEnd());
12690b57cec5SDimitry Andric       }
12700b57cec5SDimitry Andric 
12710b57cec5SDimitry Andric       Changes[Index].KeywordLoc = KeywordLoc;
12720b57cec5SDimitry Andric       Changes[Index].Version = Version;
12730b57cec5SDimitry Andric       Changes[Index].VersionRange = VersionRange;
12740b57cec5SDimitry Andric     } else {
12750b57cec5SDimitry Andric       Diag(KeywordLoc, diag::err_availability_unknown_change)
12760b57cec5SDimitry Andric         << Keyword << VersionRange;
12770b57cec5SDimitry Andric     }
12780b57cec5SDimitry Andric 
12790b57cec5SDimitry Andric   } while (TryConsumeToken(tok::comma));
12800b57cec5SDimitry Andric 
12810b57cec5SDimitry Andric   // Closing ')'.
12820b57cec5SDimitry Andric   if (T.consumeClose())
12830b57cec5SDimitry Andric     return;
12840b57cec5SDimitry Andric 
12850b57cec5SDimitry Andric   if (endLoc)
12860b57cec5SDimitry Andric     *endLoc = T.getCloseLocation();
12870b57cec5SDimitry Andric 
12880b57cec5SDimitry Andric   // The 'unavailable' availability cannot be combined with any other
12890b57cec5SDimitry Andric   // availability changes. Make sure that hasn't happened.
12900b57cec5SDimitry Andric   if (UnavailableLoc.isValid()) {
12910b57cec5SDimitry Andric     bool Complained = false;
12920b57cec5SDimitry Andric     for (unsigned Index = Introduced; Index != Unknown; ++Index) {
12930b57cec5SDimitry Andric       if (Changes[Index].KeywordLoc.isValid()) {
12940b57cec5SDimitry Andric         if (!Complained) {
12950b57cec5SDimitry Andric           Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
12960b57cec5SDimitry Andric             << SourceRange(Changes[Index].KeywordLoc,
12970b57cec5SDimitry Andric                            Changes[Index].VersionRange.getEnd());
12980b57cec5SDimitry Andric           Complained = true;
12990b57cec5SDimitry Andric         }
13000b57cec5SDimitry Andric 
13010b57cec5SDimitry Andric         // Clear out the availability.
13020b57cec5SDimitry Andric         Changes[Index] = AvailabilityChange();
13030b57cec5SDimitry Andric       }
13040b57cec5SDimitry Andric     }
13050b57cec5SDimitry Andric   }
13060b57cec5SDimitry Andric 
13070b57cec5SDimitry Andric   // Record this attribute
13080b57cec5SDimitry Andric   attrs.addNew(&Availability,
13090b57cec5SDimitry Andric                SourceRange(AvailabilityLoc, T.getCloseLocation()),
13100b57cec5SDimitry Andric                ScopeName, ScopeLoc,
13110b57cec5SDimitry Andric                Platform,
13120b57cec5SDimitry Andric                Changes[Introduced],
13130b57cec5SDimitry Andric                Changes[Deprecated],
13140b57cec5SDimitry Andric                Changes[Obsoleted],
13150b57cec5SDimitry Andric                UnavailableLoc, MessageExpr.get(),
13160b57cec5SDimitry Andric                Syntax, StrictLoc, ReplacementExpr.get());
13170b57cec5SDimitry Andric }
13180b57cec5SDimitry Andric 
13190b57cec5SDimitry Andric /// Parse the contents of the "external_source_symbol" attribute.
13200b57cec5SDimitry Andric ///
13210b57cec5SDimitry Andric /// external-source-symbol-attribute:
13220b57cec5SDimitry Andric ///   'external_source_symbol' '(' keyword-arg-list ')'
13230b57cec5SDimitry Andric ///
13240b57cec5SDimitry Andric /// keyword-arg-list:
13250b57cec5SDimitry Andric ///   keyword-arg
13260b57cec5SDimitry Andric ///   keyword-arg ',' keyword-arg-list
13270b57cec5SDimitry Andric ///
13280b57cec5SDimitry Andric /// keyword-arg:
13290b57cec5SDimitry Andric ///   'language' '=' <string>
13300b57cec5SDimitry Andric ///   'defined_in' '=' <string>
13310b57cec5SDimitry Andric ///   'generated_declaration'
13320b57cec5SDimitry Andric void Parser::ParseExternalSourceSymbolAttribute(
13330b57cec5SDimitry Andric     IdentifierInfo &ExternalSourceSymbol, SourceLocation Loc,
13340b57cec5SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
13350b57cec5SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax) {
13360b57cec5SDimitry Andric   // Opening '('.
13370b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
13380b57cec5SDimitry Andric   if (T.expectAndConsume())
13390b57cec5SDimitry Andric     return;
13400b57cec5SDimitry Andric 
13410b57cec5SDimitry Andric   // Initialize the pointers for the keyword identifiers when required.
13420b57cec5SDimitry Andric   if (!Ident_language) {
13430b57cec5SDimitry Andric     Ident_language = PP.getIdentifierInfo("language");
13440b57cec5SDimitry Andric     Ident_defined_in = PP.getIdentifierInfo("defined_in");
13450b57cec5SDimitry Andric     Ident_generated_declaration = PP.getIdentifierInfo("generated_declaration");
13460b57cec5SDimitry Andric   }
13470b57cec5SDimitry Andric 
13480b57cec5SDimitry Andric   ExprResult Language;
13490b57cec5SDimitry Andric   bool HasLanguage = false;
13500b57cec5SDimitry Andric   ExprResult DefinedInExpr;
13510b57cec5SDimitry Andric   bool HasDefinedIn = false;
13520b57cec5SDimitry Andric   IdentifierLoc *GeneratedDeclaration = nullptr;
13530b57cec5SDimitry Andric 
13540b57cec5SDimitry Andric   // Parse the language/defined_in/generated_declaration keywords
13550b57cec5SDimitry Andric   do {
13560b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
13570b57cec5SDimitry Andric       Diag(Tok, diag::err_external_source_symbol_expected_keyword);
13580b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
13590b57cec5SDimitry Andric       return;
13600b57cec5SDimitry Andric     }
13610b57cec5SDimitry Andric 
13620b57cec5SDimitry Andric     SourceLocation KeywordLoc = Tok.getLocation();
13630b57cec5SDimitry Andric     IdentifierInfo *Keyword = Tok.getIdentifierInfo();
13640b57cec5SDimitry Andric     if (Keyword == Ident_generated_declaration) {
13650b57cec5SDimitry Andric       if (GeneratedDeclaration) {
13660b57cec5SDimitry Andric         Diag(Tok, diag::err_external_source_symbol_duplicate_clause) << Keyword;
13670b57cec5SDimitry Andric         SkipUntil(tok::r_paren, StopAtSemi);
13680b57cec5SDimitry Andric         return;
13690b57cec5SDimitry Andric       }
13700b57cec5SDimitry Andric       GeneratedDeclaration = ParseIdentifierLoc();
13710b57cec5SDimitry Andric       continue;
13720b57cec5SDimitry Andric     }
13730b57cec5SDimitry Andric 
13740b57cec5SDimitry Andric     if (Keyword != Ident_language && Keyword != Ident_defined_in) {
13750b57cec5SDimitry Andric       Diag(Tok, diag::err_external_source_symbol_expected_keyword);
13760b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
13770b57cec5SDimitry Andric       return;
13780b57cec5SDimitry Andric     }
13790b57cec5SDimitry Andric 
13800b57cec5SDimitry Andric     ConsumeToken();
13810b57cec5SDimitry Andric     if (ExpectAndConsume(tok::equal, diag::err_expected_after,
13820b57cec5SDimitry Andric                          Keyword->getName())) {
13830b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
13840b57cec5SDimitry Andric       return;
13850b57cec5SDimitry Andric     }
13860b57cec5SDimitry Andric 
13870b57cec5SDimitry Andric     bool HadLanguage = HasLanguage, HadDefinedIn = HasDefinedIn;
13880b57cec5SDimitry Andric     if (Keyword == Ident_language)
13890b57cec5SDimitry Andric       HasLanguage = true;
13900b57cec5SDimitry Andric     else
13910b57cec5SDimitry Andric       HasDefinedIn = true;
13920b57cec5SDimitry Andric 
13930b57cec5SDimitry Andric     if (Tok.isNot(tok::string_literal)) {
13940b57cec5SDimitry Andric       Diag(Tok, diag::err_expected_string_literal)
13950b57cec5SDimitry Andric           << /*Source='external_source_symbol attribute'*/ 3
13960b57cec5SDimitry Andric           << /*language | source container*/ (Keyword != Ident_language);
13970b57cec5SDimitry Andric       SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
13980b57cec5SDimitry Andric       continue;
13990b57cec5SDimitry Andric     }
14000b57cec5SDimitry Andric     if (Keyword == Ident_language) {
14010b57cec5SDimitry Andric       if (HadLanguage) {
14020b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_external_source_symbol_duplicate_clause)
14030b57cec5SDimitry Andric             << Keyword;
14040b57cec5SDimitry Andric         ParseStringLiteralExpression();
14050b57cec5SDimitry Andric         continue;
14060b57cec5SDimitry Andric       }
14070b57cec5SDimitry Andric       Language = ParseStringLiteralExpression();
14080b57cec5SDimitry Andric     } else {
14090b57cec5SDimitry Andric       assert(Keyword == Ident_defined_in && "Invalid clause keyword!");
14100b57cec5SDimitry Andric       if (HadDefinedIn) {
14110b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_external_source_symbol_duplicate_clause)
14120b57cec5SDimitry Andric             << Keyword;
14130b57cec5SDimitry Andric         ParseStringLiteralExpression();
14140b57cec5SDimitry Andric         continue;
14150b57cec5SDimitry Andric       }
14160b57cec5SDimitry Andric       DefinedInExpr = ParseStringLiteralExpression();
14170b57cec5SDimitry Andric     }
14180b57cec5SDimitry Andric   } while (TryConsumeToken(tok::comma));
14190b57cec5SDimitry Andric 
14200b57cec5SDimitry Andric   // Closing ')'.
14210b57cec5SDimitry Andric   if (T.consumeClose())
14220b57cec5SDimitry Andric     return;
14230b57cec5SDimitry Andric   if (EndLoc)
14240b57cec5SDimitry Andric     *EndLoc = T.getCloseLocation();
14250b57cec5SDimitry Andric 
14260b57cec5SDimitry Andric   ArgsUnion Args[] = {Language.get(), DefinedInExpr.get(),
14270b57cec5SDimitry Andric                       GeneratedDeclaration};
14280b57cec5SDimitry Andric   Attrs.addNew(&ExternalSourceSymbol, SourceRange(Loc, T.getCloseLocation()),
1429*bdd1243dSDimitry Andric                ScopeName, ScopeLoc, Args, std::size(Args), Syntax);
14300b57cec5SDimitry Andric }
14310b57cec5SDimitry Andric 
14320b57cec5SDimitry Andric /// Parse the contents of the "objc_bridge_related" attribute.
14330b57cec5SDimitry Andric /// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')'
14340b57cec5SDimitry Andric /// related_class:
14350b57cec5SDimitry Andric ///     Identifier
14360b57cec5SDimitry Andric ///
14370b57cec5SDimitry Andric /// opt-class_method:
14380b57cec5SDimitry Andric ///     Identifier: | <empty>
14390b57cec5SDimitry Andric ///
14400b57cec5SDimitry Andric /// opt-instance_method:
14410b57cec5SDimitry Andric ///     Identifier | <empty>
14420b57cec5SDimitry Andric ///
144381ad6265SDimitry Andric void Parser::ParseObjCBridgeRelatedAttribute(
144481ad6265SDimitry Andric     IdentifierInfo &ObjCBridgeRelated, SourceLocation ObjCBridgeRelatedLoc,
144581ad6265SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
144681ad6265SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax) {
14470b57cec5SDimitry Andric   // Opening '('.
14480b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
14490b57cec5SDimitry Andric   if (T.consumeOpen()) {
14500b57cec5SDimitry Andric     Diag(Tok, diag::err_expected) << tok::l_paren;
14510b57cec5SDimitry Andric     return;
14520b57cec5SDimitry Andric   }
14530b57cec5SDimitry Andric 
14540b57cec5SDimitry Andric   // Parse the related class name.
14550b57cec5SDimitry Andric   if (Tok.isNot(tok::identifier)) {
14560b57cec5SDimitry Andric     Diag(Tok, diag::err_objcbridge_related_expected_related_class);
14570b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
14580b57cec5SDimitry Andric     return;
14590b57cec5SDimitry Andric   }
14600b57cec5SDimitry Andric   IdentifierLoc *RelatedClass = ParseIdentifierLoc();
14610b57cec5SDimitry Andric   if (ExpectAndConsume(tok::comma)) {
14620b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
14630b57cec5SDimitry Andric     return;
14640b57cec5SDimitry Andric   }
14650b57cec5SDimitry Andric 
14660b57cec5SDimitry Andric   // Parse class method name.  It's non-optional in the sense that a trailing
14670b57cec5SDimitry Andric   // comma is required, but it can be the empty string, and then we record a
14680b57cec5SDimitry Andric   // nullptr.
14690b57cec5SDimitry Andric   IdentifierLoc *ClassMethod = nullptr;
14700b57cec5SDimitry Andric   if (Tok.is(tok::identifier)) {
14710b57cec5SDimitry Andric     ClassMethod = ParseIdentifierLoc();
14720b57cec5SDimitry Andric     if (!TryConsumeToken(tok::colon)) {
14730b57cec5SDimitry Andric       Diag(Tok, diag::err_objcbridge_related_selector_name);
14740b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
14750b57cec5SDimitry Andric       return;
14760b57cec5SDimitry Andric     }
14770b57cec5SDimitry Andric   }
14780b57cec5SDimitry Andric   if (!TryConsumeToken(tok::comma)) {
14790b57cec5SDimitry Andric     if (Tok.is(tok::colon))
14800b57cec5SDimitry Andric       Diag(Tok, diag::err_objcbridge_related_selector_name);
14810b57cec5SDimitry Andric     else
14820b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::comma;
14830b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
14840b57cec5SDimitry Andric     return;
14850b57cec5SDimitry Andric   }
14860b57cec5SDimitry Andric 
14870b57cec5SDimitry Andric   // Parse instance method name.  Also non-optional but empty string is
14880b57cec5SDimitry Andric   // permitted.
14890b57cec5SDimitry Andric   IdentifierLoc *InstanceMethod = nullptr;
14900b57cec5SDimitry Andric   if (Tok.is(tok::identifier))
14910b57cec5SDimitry Andric     InstanceMethod = ParseIdentifierLoc();
14920b57cec5SDimitry Andric   else if (Tok.isNot(tok::r_paren)) {
14930b57cec5SDimitry Andric     Diag(Tok, diag::err_expected) << tok::r_paren;
14940b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
14950b57cec5SDimitry Andric     return;
14960b57cec5SDimitry Andric   }
14970b57cec5SDimitry Andric 
14980b57cec5SDimitry Andric   // Closing ')'.
14990b57cec5SDimitry Andric   if (T.consumeClose())
15000b57cec5SDimitry Andric     return;
15010b57cec5SDimitry Andric 
150281ad6265SDimitry Andric   if (EndLoc)
150381ad6265SDimitry Andric     *EndLoc = T.getCloseLocation();
15040b57cec5SDimitry Andric 
15050b57cec5SDimitry Andric   // Record this attribute
150681ad6265SDimitry Andric   Attrs.addNew(&ObjCBridgeRelated,
15070b57cec5SDimitry Andric                SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()),
150881ad6265SDimitry Andric                ScopeName, ScopeLoc, RelatedClass, ClassMethod, InstanceMethod,
15090b57cec5SDimitry Andric                Syntax);
15100b57cec5SDimitry Andric }
15110b57cec5SDimitry Andric 
1512e8d8bef9SDimitry Andric void Parser::ParseSwiftNewTypeAttribute(
1513e8d8bef9SDimitry Andric     IdentifierInfo &AttrName, SourceLocation AttrNameLoc,
1514e8d8bef9SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
1515e8d8bef9SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax) {
1516e8d8bef9SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
1517e8d8bef9SDimitry Andric 
1518e8d8bef9SDimitry Andric   // Opening '('
1519e8d8bef9SDimitry Andric   if (T.consumeOpen()) {
1520e8d8bef9SDimitry Andric     Diag(Tok, diag::err_expected) << tok::l_paren;
1521e8d8bef9SDimitry Andric     return;
1522e8d8bef9SDimitry Andric   }
1523e8d8bef9SDimitry Andric 
1524e8d8bef9SDimitry Andric   if (Tok.is(tok::r_paren)) {
1525e8d8bef9SDimitry Andric     Diag(Tok.getLocation(), diag::err_argument_required_after_attribute);
1526e8d8bef9SDimitry Andric     T.consumeClose();
1527e8d8bef9SDimitry Andric     return;
1528e8d8bef9SDimitry Andric   }
1529e8d8bef9SDimitry Andric   if (Tok.isNot(tok::kw_struct) && Tok.isNot(tok::kw_enum)) {
1530e8d8bef9SDimitry Andric     Diag(Tok, diag::warn_attribute_type_not_supported)
1531e8d8bef9SDimitry Andric         << &AttrName << Tok.getIdentifierInfo();
1532e8d8bef9SDimitry Andric     if (!isTokenSpecial())
1533e8d8bef9SDimitry Andric       ConsumeToken();
1534e8d8bef9SDimitry Andric     T.consumeClose();
1535e8d8bef9SDimitry Andric     return;
1536e8d8bef9SDimitry Andric   }
1537e8d8bef9SDimitry Andric 
1538e8d8bef9SDimitry Andric   auto *SwiftType = IdentifierLoc::create(Actions.Context, Tok.getLocation(),
1539e8d8bef9SDimitry Andric                                           Tok.getIdentifierInfo());
1540e8d8bef9SDimitry Andric   ConsumeToken();
1541e8d8bef9SDimitry Andric 
1542e8d8bef9SDimitry Andric   // Closing ')'
1543e8d8bef9SDimitry Andric   if (T.consumeClose())
1544e8d8bef9SDimitry Andric     return;
1545e8d8bef9SDimitry Andric   if (EndLoc)
1546e8d8bef9SDimitry Andric     *EndLoc = T.getCloseLocation();
1547e8d8bef9SDimitry Andric 
1548e8d8bef9SDimitry Andric   ArgsUnion Args[] = {SwiftType};
1549e8d8bef9SDimitry Andric   Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, T.getCloseLocation()),
1550*bdd1243dSDimitry Andric                ScopeName, ScopeLoc, Args, std::size(Args), Syntax);
1551e8d8bef9SDimitry Andric }
1552e8d8bef9SDimitry Andric 
15530b57cec5SDimitry Andric void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
15540b57cec5SDimitry Andric                                               SourceLocation AttrNameLoc,
15550b57cec5SDimitry Andric                                               ParsedAttributes &Attrs,
15560b57cec5SDimitry Andric                                               SourceLocation *EndLoc,
15570b57cec5SDimitry Andric                                               IdentifierInfo *ScopeName,
15580b57cec5SDimitry Andric                                               SourceLocation ScopeLoc,
15590b57cec5SDimitry Andric                                               ParsedAttr::Syntax Syntax) {
15600b57cec5SDimitry Andric   assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
15610b57cec5SDimitry Andric 
15620b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
15630b57cec5SDimitry Andric   T.consumeOpen();
15640b57cec5SDimitry Andric 
15650b57cec5SDimitry Andric   if (Tok.isNot(tok::identifier)) {
15660b57cec5SDimitry Andric     Diag(Tok, diag::err_expected) << tok::identifier;
15670b57cec5SDimitry Andric     T.skipToEnd();
15680b57cec5SDimitry Andric     return;
15690b57cec5SDimitry Andric   }
15700b57cec5SDimitry Andric   IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
15710b57cec5SDimitry Andric 
15720b57cec5SDimitry Andric   if (ExpectAndConsume(tok::comma)) {
15730b57cec5SDimitry Andric     T.skipToEnd();
15740b57cec5SDimitry Andric     return;
15750b57cec5SDimitry Andric   }
15760b57cec5SDimitry Andric 
15770b57cec5SDimitry Andric   SourceRange MatchingCTypeRange;
15780b57cec5SDimitry Andric   TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
15790b57cec5SDimitry Andric   if (MatchingCType.isInvalid()) {
15800b57cec5SDimitry Andric     T.skipToEnd();
15810b57cec5SDimitry Andric     return;
15820b57cec5SDimitry Andric   }
15830b57cec5SDimitry Andric 
15840b57cec5SDimitry Andric   bool LayoutCompatible = false;
15850b57cec5SDimitry Andric   bool MustBeNull = false;
15860b57cec5SDimitry Andric   while (TryConsumeToken(tok::comma)) {
15870b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
15880b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::identifier;
15890b57cec5SDimitry Andric       T.skipToEnd();
15900b57cec5SDimitry Andric       return;
15910b57cec5SDimitry Andric     }
15920b57cec5SDimitry Andric     IdentifierInfo *Flag = Tok.getIdentifierInfo();
15930b57cec5SDimitry Andric     if (Flag->isStr("layout_compatible"))
15940b57cec5SDimitry Andric       LayoutCompatible = true;
15950b57cec5SDimitry Andric     else if (Flag->isStr("must_be_null"))
15960b57cec5SDimitry Andric       MustBeNull = true;
15970b57cec5SDimitry Andric     else {
15980b57cec5SDimitry Andric       Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
15990b57cec5SDimitry Andric       T.skipToEnd();
16000b57cec5SDimitry Andric       return;
16010b57cec5SDimitry Andric     }
16020b57cec5SDimitry Andric     ConsumeToken(); // consume flag
16030b57cec5SDimitry Andric   }
16040b57cec5SDimitry Andric 
16050b57cec5SDimitry Andric   if (!T.consumeClose()) {
16060b57cec5SDimitry Andric     Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, ScopeName, ScopeLoc,
16070b57cec5SDimitry Andric                                    ArgumentKind, MatchingCType.get(),
16080b57cec5SDimitry Andric                                    LayoutCompatible, MustBeNull, Syntax);
16090b57cec5SDimitry Andric   }
16100b57cec5SDimitry Andric 
16110b57cec5SDimitry Andric   if (EndLoc)
16120b57cec5SDimitry Andric     *EndLoc = T.getCloseLocation();
16130b57cec5SDimitry Andric }
16140b57cec5SDimitry Andric 
16150b57cec5SDimitry Andric /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
16160b57cec5SDimitry Andric /// of a C++11 attribute-specifier in a location where an attribute is not
16170b57cec5SDimitry Andric /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
16180b57cec5SDimitry Andric /// situation.
16190b57cec5SDimitry Andric ///
16200b57cec5SDimitry Andric /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
16210b57cec5SDimitry Andric /// this doesn't appear to actually be an attribute-specifier, and the caller
16220b57cec5SDimitry Andric /// should try to parse it.
16230b57cec5SDimitry Andric bool Parser::DiagnoseProhibitedCXX11Attribute() {
16240b57cec5SDimitry Andric   assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
16250b57cec5SDimitry Andric 
16260b57cec5SDimitry Andric   switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
16270b57cec5SDimitry Andric   case CAK_NotAttributeSpecifier:
16280b57cec5SDimitry Andric     // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
16290b57cec5SDimitry Andric     return false;
16300b57cec5SDimitry Andric 
16310b57cec5SDimitry Andric   case CAK_InvalidAttributeSpecifier:
16320b57cec5SDimitry Andric     Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
16330b57cec5SDimitry Andric     return false;
16340b57cec5SDimitry Andric 
16350b57cec5SDimitry Andric   case CAK_AttributeSpecifier:
16360b57cec5SDimitry Andric     // Parse and discard the attributes.
16370b57cec5SDimitry Andric     SourceLocation BeginLoc = ConsumeBracket();
16380b57cec5SDimitry Andric     ConsumeBracket();
16390b57cec5SDimitry Andric     SkipUntil(tok::r_square);
16400b57cec5SDimitry Andric     assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
16410b57cec5SDimitry Andric     SourceLocation EndLoc = ConsumeBracket();
16420b57cec5SDimitry Andric     Diag(BeginLoc, diag::err_attributes_not_allowed)
16430b57cec5SDimitry Andric       << SourceRange(BeginLoc, EndLoc);
16440b57cec5SDimitry Andric     return true;
16450b57cec5SDimitry Andric   }
16460b57cec5SDimitry Andric   llvm_unreachable("All cases handled above.");
16470b57cec5SDimitry Andric }
16480b57cec5SDimitry Andric 
16490b57cec5SDimitry Andric /// We have found the opening square brackets of a C++11
16500b57cec5SDimitry Andric /// attribute-specifier in a location where an attribute is not permitted, but
16510b57cec5SDimitry Andric /// we know where the attributes ought to be written. Parse them anyway, and
16520b57cec5SDimitry Andric /// provide a fixit moving them to the right place.
165381ad6265SDimitry Andric void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributes &Attrs,
16540b57cec5SDimitry Andric                                              SourceLocation CorrectLocation) {
16550b57cec5SDimitry Andric   assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
16560b57cec5SDimitry Andric          Tok.is(tok::kw_alignas));
16570b57cec5SDimitry Andric 
16580b57cec5SDimitry Andric   // Consume the attributes.
16590b57cec5SDimitry Andric   SourceLocation Loc = Tok.getLocation();
16600b57cec5SDimitry Andric   ParseCXX11Attributes(Attrs);
16610b57cec5SDimitry Andric   CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
16620b57cec5SDimitry Andric   // FIXME: use err_attributes_misplaced
16630b57cec5SDimitry Andric   Diag(Loc, diag::err_attributes_not_allowed)
16640b57cec5SDimitry Andric     << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
16650b57cec5SDimitry Andric     << FixItHint::CreateRemoval(AttrRange);
16660b57cec5SDimitry Andric }
16670b57cec5SDimitry Andric 
16680b57cec5SDimitry Andric void Parser::DiagnoseProhibitedAttributes(
16690b57cec5SDimitry Andric     const SourceRange &Range, const SourceLocation CorrectLocation) {
16700b57cec5SDimitry Andric   if (CorrectLocation.isValid()) {
16710b57cec5SDimitry Andric     CharSourceRange AttrRange(Range, true);
16720b57cec5SDimitry Andric     Diag(CorrectLocation, diag::err_attributes_misplaced)
16730b57cec5SDimitry Andric         << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
16740b57cec5SDimitry Andric         << FixItHint::CreateRemoval(AttrRange);
16750b57cec5SDimitry Andric   } else
16760b57cec5SDimitry Andric     Diag(Range.getBegin(), diag::err_attributes_not_allowed) << Range;
16770b57cec5SDimitry Andric }
16780b57cec5SDimitry Andric 
167981ad6265SDimitry Andric void Parser::ProhibitCXX11Attributes(ParsedAttributes &Attrs, unsigned DiagID,
168081ad6265SDimitry Andric                                      bool DiagnoseEmptyAttrs,
168181ad6265SDimitry Andric                                      bool WarnOnUnknownAttrs) {
1682fe6060f1SDimitry Andric 
1683fe6060f1SDimitry Andric   if (DiagnoseEmptyAttrs && Attrs.empty() && Attrs.Range.isValid()) {
1684fe6060f1SDimitry Andric     // An attribute list has been parsed, but it was empty.
1685fe6060f1SDimitry Andric     // This is the case for [[]].
1686fe6060f1SDimitry Andric     const auto &LangOpts = getLangOpts();
1687fe6060f1SDimitry Andric     auto &SM = PP.getSourceManager();
1688fe6060f1SDimitry Andric     Token FirstLSquare;
1689fe6060f1SDimitry Andric     Lexer::getRawToken(Attrs.Range.getBegin(), FirstLSquare, SM, LangOpts);
1690fe6060f1SDimitry Andric 
1691fe6060f1SDimitry Andric     if (FirstLSquare.is(tok::l_square)) {
1692*bdd1243dSDimitry Andric       std::optional<Token> SecondLSquare =
1693fe6060f1SDimitry Andric           Lexer::findNextToken(FirstLSquare.getLocation(), SM, LangOpts);
1694fe6060f1SDimitry Andric 
1695fe6060f1SDimitry Andric       if (SecondLSquare && SecondLSquare->is(tok::l_square)) {
1696fe6060f1SDimitry Andric         // The attribute range starts with [[, but is empty. So this must
1697fe6060f1SDimitry Andric         // be [[]], which we are supposed to diagnose because
1698fe6060f1SDimitry Andric         // DiagnoseEmptyAttrs is true.
1699fe6060f1SDimitry Andric         Diag(Attrs.Range.getBegin(), DiagID) << Attrs.Range;
1700fe6060f1SDimitry Andric         return;
1701fe6060f1SDimitry Andric       }
1702fe6060f1SDimitry Andric     }
1703fe6060f1SDimitry Andric   }
1704fe6060f1SDimitry Andric 
17050b57cec5SDimitry Andric   for (const ParsedAttr &AL : Attrs) {
17060b57cec5SDimitry Andric     if (!AL.isCXX11Attribute() && !AL.isC2xAttribute())
17070b57cec5SDimitry Andric       continue;
170881ad6265SDimitry Andric     if (AL.getKind() == ParsedAttr::UnknownAttribute) {
170981ad6265SDimitry Andric       if (WarnOnUnknownAttrs)
1710e8d8bef9SDimitry Andric         Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored)
1711e8d8bef9SDimitry Andric             << AL << AL.getRange();
171281ad6265SDimitry Andric     } else {
1713a7dea167SDimitry Andric       Diag(AL.getLoc(), DiagID) << AL;
17140b57cec5SDimitry Andric       AL.setInvalid();
17150b57cec5SDimitry Andric     }
17160b57cec5SDimitry Andric   }
17170b57cec5SDimitry Andric }
17180b57cec5SDimitry Andric 
171981ad6265SDimitry Andric void Parser::DiagnoseCXX11AttributeExtension(ParsedAttributes &Attrs) {
1720fe6060f1SDimitry Andric   for (const ParsedAttr &PA : Attrs) {
1721fe6060f1SDimitry Andric     if (PA.isCXX11Attribute() || PA.isC2xAttribute())
1722fe6060f1SDimitry Andric       Diag(PA.getLoc(), diag::ext_cxx11_attr_placement) << PA << PA.getRange();
1723fe6060f1SDimitry Andric   }
1724fe6060f1SDimitry Andric }
1725fe6060f1SDimitry Andric 
17260b57cec5SDimitry Andric // Usually, `__attribute__((attrib)) class Foo {} var` means that attribute
17270b57cec5SDimitry Andric // applies to var, not the type Foo.
17280b57cec5SDimitry Andric // As an exception to the rule, __declspec(align(...)) before the
17290b57cec5SDimitry Andric // class-key affects the type instead of the variable.
17300b57cec5SDimitry Andric // Also, Microsoft-style [attributes] seem to affect the type instead of the
17310b57cec5SDimitry Andric // variable.
17320b57cec5SDimitry Andric // This function moves attributes that should apply to the type off DS to Attrs.
173381ad6265SDimitry Andric void Parser::stripTypeAttributesOffDeclSpec(ParsedAttributes &Attrs,
17340b57cec5SDimitry Andric                                             DeclSpec &DS,
17350b57cec5SDimitry Andric                                             Sema::TagUseKind TUK) {
17360b57cec5SDimitry Andric   if (TUK == Sema::TUK_Reference)
17370b57cec5SDimitry Andric     return;
17380b57cec5SDimitry Andric 
17390b57cec5SDimitry Andric   llvm::SmallVector<ParsedAttr *, 1> ToBeMoved;
17400b57cec5SDimitry Andric 
17410b57cec5SDimitry Andric   for (ParsedAttr &AL : DS.getAttributes()) {
17420b57cec5SDimitry Andric     if ((AL.getKind() == ParsedAttr::AT_Aligned &&
17430b57cec5SDimitry Andric          AL.isDeclspecAttribute()) ||
17440b57cec5SDimitry Andric         AL.isMicrosoftAttribute())
17450b57cec5SDimitry Andric       ToBeMoved.push_back(&AL);
17460b57cec5SDimitry Andric   }
17470b57cec5SDimitry Andric 
17480b57cec5SDimitry Andric   for (ParsedAttr *AL : ToBeMoved) {
17490b57cec5SDimitry Andric     DS.getAttributes().remove(AL);
17500b57cec5SDimitry Andric     Attrs.addAtEnd(AL);
17510b57cec5SDimitry Andric   }
17520b57cec5SDimitry Andric }
17530b57cec5SDimitry Andric 
17540b57cec5SDimitry Andric /// ParseDeclaration - Parse a full 'declaration', which consists of
17550b57cec5SDimitry Andric /// declaration-specifiers, some number of declarators, and a semicolon.
17560b57cec5SDimitry Andric /// 'Context' should be a DeclaratorContext value.  This returns the
17570b57cec5SDimitry Andric /// location of the semicolon in DeclEnd.
17580b57cec5SDimitry Andric ///
17590b57cec5SDimitry Andric ///       declaration: [C99 6.7]
17600b57cec5SDimitry Andric ///         block-declaration ->
17610b57cec5SDimitry Andric ///           simple-declaration
17620b57cec5SDimitry Andric ///           others                   [FIXME]
17630b57cec5SDimitry Andric /// [C++]   template-declaration
17640b57cec5SDimitry Andric /// [C++]   namespace-definition
17650b57cec5SDimitry Andric /// [C++]   using-directive
17660b57cec5SDimitry Andric /// [C++]   using-declaration
17670b57cec5SDimitry Andric /// [C++11/C11] static_assert-declaration
17680b57cec5SDimitry Andric ///         others... [FIXME]
17690b57cec5SDimitry Andric ///
177081ad6265SDimitry Andric Parser::DeclGroupPtrTy Parser::ParseDeclaration(DeclaratorContext Context,
177181ad6265SDimitry Andric                                                 SourceLocation &DeclEnd,
177281ad6265SDimitry Andric                                                 ParsedAttributes &DeclAttrs,
177381ad6265SDimitry Andric                                                 ParsedAttributes &DeclSpecAttrs,
1774a7dea167SDimitry Andric                                                 SourceLocation *DeclSpecStart) {
17750b57cec5SDimitry Andric   ParenBraceBracketBalancer BalancerRAIIObj(*this);
17760b57cec5SDimitry Andric   // Must temporarily exit the objective-c container scope for
17770b57cec5SDimitry Andric   // parsing c none objective-c decls.
17780b57cec5SDimitry Andric   ObjCDeclContextSwitch ObjCDC(*this);
17790b57cec5SDimitry Andric 
17800b57cec5SDimitry Andric   Decl *SingleDecl = nullptr;
17810b57cec5SDimitry Andric   switch (Tok.getKind()) {
17820b57cec5SDimitry Andric   case tok::kw_template:
17830b57cec5SDimitry Andric   case tok::kw_export:
178481ad6265SDimitry Andric     ProhibitAttributes(DeclAttrs);
178581ad6265SDimitry Andric     ProhibitAttributes(DeclSpecAttrs);
178681ad6265SDimitry Andric     SingleDecl =
178781ad6265SDimitry Andric         ParseDeclarationStartingWithTemplate(Context, DeclEnd, DeclAttrs);
17880b57cec5SDimitry Andric     break;
17890b57cec5SDimitry Andric   case tok::kw_inline:
17900b57cec5SDimitry Andric     // Could be the start of an inline namespace. Allowed as an ext in C++03.
17910b57cec5SDimitry Andric     if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
179281ad6265SDimitry Andric       ProhibitAttributes(DeclAttrs);
179381ad6265SDimitry Andric       ProhibitAttributes(DeclSpecAttrs);
17940b57cec5SDimitry Andric       SourceLocation InlineLoc = ConsumeToken();
17950b57cec5SDimitry Andric       return ParseNamespace(Context, DeclEnd, InlineLoc);
17960b57cec5SDimitry Andric     }
179781ad6265SDimitry Andric     return ParseSimpleDeclaration(Context, DeclEnd, DeclAttrs, DeclSpecAttrs,
179881ad6265SDimitry Andric                                   true, nullptr, DeclSpecStart);
1799*bdd1243dSDimitry Andric 
1800*bdd1243dSDimitry Andric   case tok::kw_cbuffer:
1801*bdd1243dSDimitry Andric   case tok::kw_tbuffer:
1802*bdd1243dSDimitry Andric     SingleDecl = ParseHLSLBuffer(DeclEnd);
1803*bdd1243dSDimitry Andric     break;
18040b57cec5SDimitry Andric   case tok::kw_namespace:
180581ad6265SDimitry Andric     ProhibitAttributes(DeclAttrs);
180681ad6265SDimitry Andric     ProhibitAttributes(DeclSpecAttrs);
18070b57cec5SDimitry Andric     return ParseNamespace(Context, DeclEnd);
180881ad6265SDimitry Andric   case tok::kw_using: {
180981ad6265SDimitry Andric     ParsedAttributes Attrs(AttrFactory);
181081ad6265SDimitry Andric     takeAndConcatenateAttrs(DeclAttrs, DeclSpecAttrs, Attrs);
18110b57cec5SDimitry Andric     return ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
181281ad6265SDimitry Andric                                             DeclEnd, Attrs);
181381ad6265SDimitry Andric   }
18140b57cec5SDimitry Andric   case tok::kw_static_assert:
18150b57cec5SDimitry Andric   case tok::kw__Static_assert:
181681ad6265SDimitry Andric     ProhibitAttributes(DeclAttrs);
181781ad6265SDimitry Andric     ProhibitAttributes(DeclSpecAttrs);
18180b57cec5SDimitry Andric     SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
18190b57cec5SDimitry Andric     break;
18200b57cec5SDimitry Andric   default:
182181ad6265SDimitry Andric     return ParseSimpleDeclaration(Context, DeclEnd, DeclAttrs, DeclSpecAttrs,
182281ad6265SDimitry Andric                                   true, nullptr, DeclSpecStart);
18230b57cec5SDimitry Andric   }
18240b57cec5SDimitry Andric 
18250b57cec5SDimitry Andric   // This routine returns a DeclGroup, if the thing we parsed only contains a
18260b57cec5SDimitry Andric   // single decl, convert it now.
18270b57cec5SDimitry Andric   return Actions.ConvertDeclToDeclGroup(SingleDecl);
18280b57cec5SDimitry Andric }
18290b57cec5SDimitry Andric 
18300b57cec5SDimitry Andric ///       simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
18310b57cec5SDimitry Andric ///         declaration-specifiers init-declarator-list[opt] ';'
18320b57cec5SDimitry Andric /// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
18330b57cec5SDimitry Andric ///             init-declarator-list ';'
18340b57cec5SDimitry Andric ///[C90/C++]init-declarator-list ';'                             [TODO]
18350b57cec5SDimitry Andric /// [OMP]   threadprivate-directive
18360b57cec5SDimitry Andric /// [OMP]   allocate-directive                                   [TODO]
18370b57cec5SDimitry Andric ///
18380b57cec5SDimitry Andric ///       for-range-declaration: [C++11 6.5p1: stmt.ranged]
18390b57cec5SDimitry Andric ///         attribute-specifier-seq[opt] type-specifier-seq declarator
18400b57cec5SDimitry Andric ///
18410b57cec5SDimitry Andric /// If RequireSemi is false, this does not check for a ';' at the end of the
18420b57cec5SDimitry Andric /// declaration.  If it is true, it checks for and eats it.
18430b57cec5SDimitry Andric ///
18440b57cec5SDimitry Andric /// If FRI is non-null, we might be parsing a for-range-declaration instead
18450b57cec5SDimitry Andric /// of a simple-declaration. If we find that we are, we also parse the
18460b57cec5SDimitry Andric /// for-range-initializer, and place it here.
1847a7dea167SDimitry Andric ///
1848a7dea167SDimitry Andric /// DeclSpecStart is used when decl-specifiers are parsed before parsing
1849a7dea167SDimitry Andric /// the Declaration. The SourceLocation for this Decl is set to
1850a7dea167SDimitry Andric /// DeclSpecStart if DeclSpecStart is non-null.
1851a7dea167SDimitry Andric Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(
1852a7dea167SDimitry Andric     DeclaratorContext Context, SourceLocation &DeclEnd,
185381ad6265SDimitry Andric     ParsedAttributes &DeclAttrs, ParsedAttributes &DeclSpecAttrs,
185481ad6265SDimitry Andric     bool RequireSemi, ForRangeInit *FRI, SourceLocation *DeclSpecStart) {
185581ad6265SDimitry Andric   // Need to retain these for diagnostics before we add them to the DeclSepc.
185681ad6265SDimitry Andric   ParsedAttributesView OriginalDeclSpecAttrs;
185781ad6265SDimitry Andric   OriginalDeclSpecAttrs.addAll(DeclSpecAttrs.begin(), DeclSpecAttrs.end());
185881ad6265SDimitry Andric   OriginalDeclSpecAttrs.Range = DeclSpecAttrs.Range;
185981ad6265SDimitry Andric 
18600b57cec5SDimitry Andric   // Parse the common declaration-specifiers piece.
18610b57cec5SDimitry Andric   ParsingDeclSpec DS(*this);
186281ad6265SDimitry Andric   DS.takeAttributesFrom(DeclSpecAttrs);
18630b57cec5SDimitry Andric 
18640b57cec5SDimitry Andric   DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
18650b57cec5SDimitry Andric   ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
18660b57cec5SDimitry Andric 
18670b57cec5SDimitry Andric   // If we had a free-standing type definition with a missing semicolon, we
18680b57cec5SDimitry Andric   // may get this far before the problem becomes obvious.
18690b57cec5SDimitry Andric   if (DS.hasTagDefinition() &&
18700b57cec5SDimitry Andric       DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
18710b57cec5SDimitry Andric     return nullptr;
18720b57cec5SDimitry Andric 
18730b57cec5SDimitry Andric   // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
18740b57cec5SDimitry Andric   // declaration-specifiers init-declarator-list[opt] ';'
18750b57cec5SDimitry Andric   if (Tok.is(tok::semi)) {
187681ad6265SDimitry Andric     ProhibitAttributes(DeclAttrs);
18770b57cec5SDimitry Andric     DeclEnd = Tok.getLocation();
18780b57cec5SDimitry Andric     if (RequireSemi) ConsumeToken();
18790b57cec5SDimitry Andric     RecordDecl *AnonRecord = nullptr;
188081ad6265SDimitry Andric     Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(
188181ad6265SDimitry Andric         getCurScope(), AS_none, DS, ParsedAttributesView::none(), AnonRecord);
18820b57cec5SDimitry Andric     DS.complete(TheDecl);
18830b57cec5SDimitry Andric     if (AnonRecord) {
18840b57cec5SDimitry Andric       Decl* decls[] = {AnonRecord, TheDecl};
18850b57cec5SDimitry Andric       return Actions.BuildDeclaratorGroup(decls);
18860b57cec5SDimitry Andric     }
18870b57cec5SDimitry Andric     return Actions.ConvertDeclToDeclGroup(TheDecl);
18880b57cec5SDimitry Andric   }
18890b57cec5SDimitry Andric 
1890a7dea167SDimitry Andric   if (DeclSpecStart)
1891a7dea167SDimitry Andric     DS.SetRangeStart(*DeclSpecStart);
1892a7dea167SDimitry Andric 
189381ad6265SDimitry Andric   return ParseDeclGroup(DS, Context, DeclAttrs, &DeclEnd, FRI);
18940b57cec5SDimitry Andric }
18950b57cec5SDimitry Andric 
18960b57cec5SDimitry Andric /// Returns true if this might be the start of a declarator, or a common typo
18970b57cec5SDimitry Andric /// for a declarator.
18980b57cec5SDimitry Andric bool Parser::MightBeDeclarator(DeclaratorContext Context) {
18990b57cec5SDimitry Andric   switch (Tok.getKind()) {
19000b57cec5SDimitry Andric   case tok::annot_cxxscope:
19010b57cec5SDimitry Andric   case tok::annot_template_id:
19020b57cec5SDimitry Andric   case tok::caret:
19030b57cec5SDimitry Andric   case tok::code_completion:
19040b57cec5SDimitry Andric   case tok::coloncolon:
19050b57cec5SDimitry Andric   case tok::ellipsis:
19060b57cec5SDimitry Andric   case tok::kw___attribute:
19070b57cec5SDimitry Andric   case tok::kw_operator:
19080b57cec5SDimitry Andric   case tok::l_paren:
19090b57cec5SDimitry Andric   case tok::star:
19100b57cec5SDimitry Andric     return true;
19110b57cec5SDimitry Andric 
19120b57cec5SDimitry Andric   case tok::amp:
19130b57cec5SDimitry Andric   case tok::ampamp:
19140b57cec5SDimitry Andric     return getLangOpts().CPlusPlus;
19150b57cec5SDimitry Andric 
19160b57cec5SDimitry Andric   case tok::l_square: // Might be an attribute on an unnamed bit-field.
1917e8d8bef9SDimitry Andric     return Context == DeclaratorContext::Member && getLangOpts().CPlusPlus11 &&
1918e8d8bef9SDimitry Andric            NextToken().is(tok::l_square);
19190b57cec5SDimitry Andric 
19200b57cec5SDimitry Andric   case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
1921e8d8bef9SDimitry Andric     return Context == DeclaratorContext::Member || getLangOpts().CPlusPlus;
19220b57cec5SDimitry Andric 
19230b57cec5SDimitry Andric   case tok::identifier:
19240b57cec5SDimitry Andric     switch (NextToken().getKind()) {
19250b57cec5SDimitry Andric     case tok::code_completion:
19260b57cec5SDimitry Andric     case tok::coloncolon:
19270b57cec5SDimitry Andric     case tok::comma:
19280b57cec5SDimitry Andric     case tok::equal:
19290b57cec5SDimitry Andric     case tok::equalequal: // Might be a typo for '='.
19300b57cec5SDimitry Andric     case tok::kw_alignas:
19310b57cec5SDimitry Andric     case tok::kw_asm:
19320b57cec5SDimitry Andric     case tok::kw___attribute:
19330b57cec5SDimitry Andric     case tok::l_brace:
19340b57cec5SDimitry Andric     case tok::l_paren:
19350b57cec5SDimitry Andric     case tok::l_square:
19360b57cec5SDimitry Andric     case tok::less:
19370b57cec5SDimitry Andric     case tok::r_brace:
19380b57cec5SDimitry Andric     case tok::r_paren:
19390b57cec5SDimitry Andric     case tok::r_square:
19400b57cec5SDimitry Andric     case tok::semi:
19410b57cec5SDimitry Andric       return true;
19420b57cec5SDimitry Andric 
19430b57cec5SDimitry Andric     case tok::colon:
19440b57cec5SDimitry Andric       // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
19450b57cec5SDimitry Andric       // and in block scope it's probably a label. Inside a class definition,
19460b57cec5SDimitry Andric       // this is a bit-field.
1947e8d8bef9SDimitry Andric       return Context == DeclaratorContext::Member ||
1948e8d8bef9SDimitry Andric              (getLangOpts().CPlusPlus && Context == DeclaratorContext::File);
19490b57cec5SDimitry Andric 
19500b57cec5SDimitry Andric     case tok::identifier: // Possible virt-specifier.
19510b57cec5SDimitry Andric       return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
19520b57cec5SDimitry Andric 
19530b57cec5SDimitry Andric     default:
19540b57cec5SDimitry Andric       return false;
19550b57cec5SDimitry Andric     }
19560b57cec5SDimitry Andric 
19570b57cec5SDimitry Andric   default:
19580b57cec5SDimitry Andric     return false;
19590b57cec5SDimitry Andric   }
19600b57cec5SDimitry Andric }
19610b57cec5SDimitry Andric 
19620b57cec5SDimitry Andric /// Skip until we reach something which seems like a sensible place to pick
19630b57cec5SDimitry Andric /// up parsing after a malformed declaration. This will sometimes stop sooner
19640b57cec5SDimitry Andric /// than SkipUntil(tok::r_brace) would, but will never stop later.
19650b57cec5SDimitry Andric void Parser::SkipMalformedDecl() {
19660b57cec5SDimitry Andric   while (true) {
19670b57cec5SDimitry Andric     switch (Tok.getKind()) {
19680b57cec5SDimitry Andric     case tok::l_brace:
19690b57cec5SDimitry Andric       // Skip until matching }, then stop. We've probably skipped over
19700b57cec5SDimitry Andric       // a malformed class or function definition or similar.
19710b57cec5SDimitry Andric       ConsumeBrace();
19720b57cec5SDimitry Andric       SkipUntil(tok::r_brace);
19730b57cec5SDimitry Andric       if (Tok.isOneOf(tok::comma, tok::l_brace, tok::kw_try)) {
19740b57cec5SDimitry Andric         // This declaration isn't over yet. Keep skipping.
19750b57cec5SDimitry Andric         continue;
19760b57cec5SDimitry Andric       }
19770b57cec5SDimitry Andric       TryConsumeToken(tok::semi);
19780b57cec5SDimitry Andric       return;
19790b57cec5SDimitry Andric 
19800b57cec5SDimitry Andric     case tok::l_square:
19810b57cec5SDimitry Andric       ConsumeBracket();
19820b57cec5SDimitry Andric       SkipUntil(tok::r_square);
19830b57cec5SDimitry Andric       continue;
19840b57cec5SDimitry Andric 
19850b57cec5SDimitry Andric     case tok::l_paren:
19860b57cec5SDimitry Andric       ConsumeParen();
19870b57cec5SDimitry Andric       SkipUntil(tok::r_paren);
19880b57cec5SDimitry Andric       continue;
19890b57cec5SDimitry Andric 
19900b57cec5SDimitry Andric     case tok::r_brace:
19910b57cec5SDimitry Andric       return;
19920b57cec5SDimitry Andric 
19930b57cec5SDimitry Andric     case tok::semi:
19940b57cec5SDimitry Andric       ConsumeToken();
19950b57cec5SDimitry Andric       return;
19960b57cec5SDimitry Andric 
19970b57cec5SDimitry Andric     case tok::kw_inline:
19980b57cec5SDimitry Andric       // 'inline namespace' at the start of a line is almost certainly
19990b57cec5SDimitry Andric       // a good place to pick back up parsing, except in an Objective-C
20000b57cec5SDimitry Andric       // @interface context.
20010b57cec5SDimitry Andric       if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
20020b57cec5SDimitry Andric           (!ParsingInObjCContainer || CurParsedObjCImpl))
20030b57cec5SDimitry Andric         return;
20040b57cec5SDimitry Andric       break;
20050b57cec5SDimitry Andric 
20060b57cec5SDimitry Andric     case tok::kw_namespace:
20070b57cec5SDimitry Andric       // 'namespace' at the start of a line is almost certainly a good
20080b57cec5SDimitry Andric       // place to pick back up parsing, except in an Objective-C
20090b57cec5SDimitry Andric       // @interface context.
20100b57cec5SDimitry Andric       if (Tok.isAtStartOfLine() &&
20110b57cec5SDimitry Andric           (!ParsingInObjCContainer || CurParsedObjCImpl))
20120b57cec5SDimitry Andric         return;
20130b57cec5SDimitry Andric       break;
20140b57cec5SDimitry Andric 
20150b57cec5SDimitry Andric     case tok::at:
20160b57cec5SDimitry Andric       // @end is very much like } in Objective-C contexts.
20170b57cec5SDimitry Andric       if (NextToken().isObjCAtKeyword(tok::objc_end) &&
20180b57cec5SDimitry Andric           ParsingInObjCContainer)
20190b57cec5SDimitry Andric         return;
20200b57cec5SDimitry Andric       break;
20210b57cec5SDimitry Andric 
20220b57cec5SDimitry Andric     case tok::minus:
20230b57cec5SDimitry Andric     case tok::plus:
20240b57cec5SDimitry Andric       // - and + probably start new method declarations in Objective-C contexts.
20250b57cec5SDimitry Andric       if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
20260b57cec5SDimitry Andric         return;
20270b57cec5SDimitry Andric       break;
20280b57cec5SDimitry Andric 
20290b57cec5SDimitry Andric     case tok::eof:
20300b57cec5SDimitry Andric     case tok::annot_module_begin:
20310b57cec5SDimitry Andric     case tok::annot_module_end:
20320b57cec5SDimitry Andric     case tok::annot_module_include:
20330b57cec5SDimitry Andric       return;
20340b57cec5SDimitry Andric 
20350b57cec5SDimitry Andric     default:
20360b57cec5SDimitry Andric       break;
20370b57cec5SDimitry Andric     }
20380b57cec5SDimitry Andric 
20390b57cec5SDimitry Andric     ConsumeAnyToken();
20400b57cec5SDimitry Andric   }
20410b57cec5SDimitry Andric }
20420b57cec5SDimitry Andric 
20430b57cec5SDimitry Andric /// ParseDeclGroup - Having concluded that this is either a function
20440b57cec5SDimitry Andric /// definition or a group of object declarations, actually parse the
20450b57cec5SDimitry Andric /// result.
20460b57cec5SDimitry Andric Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
20470b57cec5SDimitry Andric                                               DeclaratorContext Context,
204881ad6265SDimitry Andric                                               ParsedAttributes &Attrs,
20490b57cec5SDimitry Andric                                               SourceLocation *DeclEnd,
20500b57cec5SDimitry Andric                                               ForRangeInit *FRI) {
20510b57cec5SDimitry Andric   // Parse the first declarator.
205281ad6265SDimitry Andric   // Consume all of the attributes from `Attrs` by moving them to our own local
205381ad6265SDimitry Andric   // list. This ensures that we will not attempt to interpret them as statement
205481ad6265SDimitry Andric   // attributes higher up the callchain.
205581ad6265SDimitry Andric   ParsedAttributes LocalAttrs(AttrFactory);
205681ad6265SDimitry Andric   LocalAttrs.takeAllFrom(Attrs);
205781ad6265SDimitry Andric   ParsingDeclarator D(*this, DS, LocalAttrs, Context);
20580b57cec5SDimitry Andric   ParseDeclarator(D);
20590b57cec5SDimitry Andric 
20600b57cec5SDimitry Andric   // Bail out if the first declarator didn't seem well-formed.
20610b57cec5SDimitry Andric   if (!D.hasName() && !D.mayOmitIdentifier()) {
20620b57cec5SDimitry Andric     SkipMalformedDecl();
20630b57cec5SDimitry Andric     return nullptr;
20640b57cec5SDimitry Andric   }
20650b57cec5SDimitry Andric 
2066*bdd1243dSDimitry Andric   if (getLangOpts().HLSL)
2067*bdd1243dSDimitry Andric     MaybeParseHLSLSemantics(D);
2068*bdd1243dSDimitry Andric 
2069480093f4SDimitry Andric   if (Tok.is(tok::kw_requires))
2070480093f4SDimitry Andric     ParseTrailingRequiresClause(D);
2071480093f4SDimitry Andric 
20720b57cec5SDimitry Andric   // Save late-parsed attributes for now; they need to be parsed in the
20730b57cec5SDimitry Andric   // appropriate function scope after the function Decl has been constructed.
20740b57cec5SDimitry Andric   // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
20750b57cec5SDimitry Andric   LateParsedAttrList LateParsedAttrs(true);
20760b57cec5SDimitry Andric   if (D.isFunctionDeclarator()) {
20770b57cec5SDimitry Andric     MaybeParseGNUAttributes(D, &LateParsedAttrs);
20780b57cec5SDimitry Andric 
20790b57cec5SDimitry Andric     // The _Noreturn keyword can't appear here, unlike the GNU noreturn
20800b57cec5SDimitry Andric     // attribute. If we find the keyword here, tell the user to put it
20810b57cec5SDimitry Andric     // at the start instead.
20820b57cec5SDimitry Andric     if (Tok.is(tok::kw__Noreturn)) {
20830b57cec5SDimitry Andric       SourceLocation Loc = ConsumeToken();
20840b57cec5SDimitry Andric       const char *PrevSpec;
20850b57cec5SDimitry Andric       unsigned DiagID;
20860b57cec5SDimitry Andric 
20870b57cec5SDimitry Andric       // We can offer a fixit if it's valid to mark this function as _Noreturn
20880b57cec5SDimitry Andric       // and we don't have any other declarators in this declaration.
20890b57cec5SDimitry Andric       bool Fixit = !DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
20900b57cec5SDimitry Andric       MaybeParseGNUAttributes(D, &LateParsedAttrs);
20910b57cec5SDimitry Andric       Fixit &= Tok.isOneOf(tok::semi, tok::l_brace, tok::kw_try);
20920b57cec5SDimitry Andric 
20930b57cec5SDimitry Andric       Diag(Loc, diag::err_c11_noreturn_misplaced)
20940b57cec5SDimitry Andric           << (Fixit ? FixItHint::CreateRemoval(Loc) : FixItHint())
20950b57cec5SDimitry Andric           << (Fixit ? FixItHint::CreateInsertion(D.getBeginLoc(), "_Noreturn ")
20960b57cec5SDimitry Andric                     : FixItHint());
20970b57cec5SDimitry Andric     }
20980b57cec5SDimitry Andric 
20990b57cec5SDimitry Andric     // Check to see if we have a function *definition* which must have a body.
21005ffd83dbSDimitry Andric     if (Tok.is(tok::equal) && NextToken().is(tok::code_completion)) {
21015ffd83dbSDimitry Andric       cutOffParsing();
2102fe6060f1SDimitry Andric       Actions.CodeCompleteAfterFunctionEquals(D);
21035ffd83dbSDimitry Andric       return nullptr;
21045ffd83dbSDimitry Andric     }
2105349cc55cSDimitry Andric     // We're at the point where the parsing of function declarator is finished.
2106349cc55cSDimitry Andric     //
2107349cc55cSDimitry Andric     // A common error is that users accidently add a virtual specifier
2108349cc55cSDimitry Andric     // (e.g. override) in an out-line method definition.
2109349cc55cSDimitry Andric     // We attempt to recover by stripping all these specifiers coming after
2110349cc55cSDimitry Andric     // the declarator.
2111349cc55cSDimitry Andric     while (auto Specifier = isCXX11VirtSpecifier()) {
2112349cc55cSDimitry Andric       Diag(Tok, diag::err_virt_specifier_outside_class)
2113349cc55cSDimitry Andric           << VirtSpecifiers::getSpecifierName(Specifier)
2114349cc55cSDimitry Andric           << FixItHint::CreateRemoval(Tok.getLocation());
2115349cc55cSDimitry Andric       ConsumeToken();
2116349cc55cSDimitry Andric     }
21170b57cec5SDimitry Andric     // Look at the next token to make sure that this isn't a function
21180b57cec5SDimitry Andric     // declaration.  We have to check this because __attribute__ might be the
21190b57cec5SDimitry Andric     // start of a function definition in GCC-extended K&R C.
21205ffd83dbSDimitry Andric     if (!isDeclarationAfterDeclarator()) {
21210b57cec5SDimitry Andric 
21220b57cec5SDimitry Andric       // Function definitions are only allowed at file scope and in C++ classes.
21230b57cec5SDimitry Andric       // The C++ inline method definition case is handled elsewhere, so we only
21240b57cec5SDimitry Andric       // need to handle the file scope definition case.
2125e8d8bef9SDimitry Andric       if (Context == DeclaratorContext::File) {
21260b57cec5SDimitry Andric         if (isStartOfFunctionDefinition(D)) {
21270b57cec5SDimitry Andric           if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
21280b57cec5SDimitry Andric             Diag(Tok, diag::err_function_declared_typedef);
21290b57cec5SDimitry Andric 
21300b57cec5SDimitry Andric             // Recover by treating the 'typedef' as spurious.
21310b57cec5SDimitry Andric             DS.ClearStorageClassSpecs();
21320b57cec5SDimitry Andric           }
21330b57cec5SDimitry Andric 
21345ffd83dbSDimitry Andric           Decl *TheDecl = ParseFunctionDefinition(D, ParsedTemplateInfo(),
21355ffd83dbSDimitry Andric                                                   &LateParsedAttrs);
21360b57cec5SDimitry Andric           return Actions.ConvertDeclToDeclGroup(TheDecl);
21370b57cec5SDimitry Andric         }
21380b57cec5SDimitry Andric 
2139*bdd1243dSDimitry Andric         if (isDeclarationSpecifier(ImplicitTypenameContext::No)) {
21400b57cec5SDimitry Andric           // If there is an invalid declaration specifier right after the
21410b57cec5SDimitry Andric           // function prototype, then we must be in a missing semicolon case
21420b57cec5SDimitry Andric           // where this isn't actually a body.  Just fall through into the code
21430b57cec5SDimitry Andric           // that handles it as a prototype, and let the top-level code handle
21440b57cec5SDimitry Andric           // the erroneous declspec where it would otherwise expect a comma or
21450b57cec5SDimitry Andric           // semicolon.
21460b57cec5SDimitry Andric         } else {
21470b57cec5SDimitry Andric           Diag(Tok, diag::err_expected_fn_body);
21480b57cec5SDimitry Andric           SkipUntil(tok::semi);
21490b57cec5SDimitry Andric           return nullptr;
21500b57cec5SDimitry Andric         }
21510b57cec5SDimitry Andric       } else {
21520b57cec5SDimitry Andric         if (Tok.is(tok::l_brace)) {
21530b57cec5SDimitry Andric           Diag(Tok, diag::err_function_definition_not_allowed);
21540b57cec5SDimitry Andric           SkipMalformedDecl();
21550b57cec5SDimitry Andric           return nullptr;
21560b57cec5SDimitry Andric         }
21570b57cec5SDimitry Andric       }
21580b57cec5SDimitry Andric     }
21595ffd83dbSDimitry Andric   }
21600b57cec5SDimitry Andric 
21610b57cec5SDimitry Andric   if (ParseAsmAttributesAfterDeclarator(D))
21620b57cec5SDimitry Andric     return nullptr;
21630b57cec5SDimitry Andric 
21640b57cec5SDimitry Andric   // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
21650b57cec5SDimitry Andric   // must parse and analyze the for-range-initializer before the declaration is
21660b57cec5SDimitry Andric   // analyzed.
21670b57cec5SDimitry Andric   //
21680b57cec5SDimitry Andric   // Handle the Objective-C for-in loop variable similarly, although we
21690b57cec5SDimitry Andric   // don't need to parse the container in advance.
21700b57cec5SDimitry Andric   if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
21710b57cec5SDimitry Andric     bool IsForRangeLoop = false;
21720b57cec5SDimitry Andric     if (TryConsumeToken(tok::colon, FRI->ColonLoc)) {
21730b57cec5SDimitry Andric       IsForRangeLoop = true;
2174a7dea167SDimitry Andric       if (getLangOpts().OpenMP)
2175a7dea167SDimitry Andric         Actions.startOpenMPCXXRangeFor();
21760b57cec5SDimitry Andric       if (Tok.is(tok::l_brace))
21770b57cec5SDimitry Andric         FRI->RangeExpr = ParseBraceInitializer();
21780b57cec5SDimitry Andric       else
21790b57cec5SDimitry Andric         FRI->RangeExpr = ParseExpression();
21800b57cec5SDimitry Andric     }
21810b57cec5SDimitry Andric 
21820b57cec5SDimitry Andric     Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
21830b57cec5SDimitry Andric     if (IsForRangeLoop) {
21840b57cec5SDimitry Andric       Actions.ActOnCXXForRangeDecl(ThisDecl);
21850b57cec5SDimitry Andric     } else {
21860b57cec5SDimitry Andric       // Obj-C for loop
21870b57cec5SDimitry Andric       if (auto *VD = dyn_cast_or_null<VarDecl>(ThisDecl))
21880b57cec5SDimitry Andric         VD->setObjCForDecl(true);
21890b57cec5SDimitry Andric     }
21900b57cec5SDimitry Andric     Actions.FinalizeDeclaration(ThisDecl);
21910b57cec5SDimitry Andric     D.complete(ThisDecl);
21920b57cec5SDimitry Andric     return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
21930b57cec5SDimitry Andric   }
21940b57cec5SDimitry Andric 
21950b57cec5SDimitry Andric   SmallVector<Decl *, 8> DeclsInGroup;
21960b57cec5SDimitry Andric   Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(
21970b57cec5SDimitry Andric       D, ParsedTemplateInfo(), FRI);
21980b57cec5SDimitry Andric   if (LateParsedAttrs.size() > 0)
21990b57cec5SDimitry Andric     ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
22000b57cec5SDimitry Andric   D.complete(FirstDecl);
22010b57cec5SDimitry Andric   if (FirstDecl)
22020b57cec5SDimitry Andric     DeclsInGroup.push_back(FirstDecl);
22030b57cec5SDimitry Andric 
2204e8d8bef9SDimitry Andric   bool ExpectSemi = Context != DeclaratorContext::ForInit;
22050b57cec5SDimitry Andric 
22060b57cec5SDimitry Andric   // If we don't have a comma, it is either the end of the list (a ';') or an
22070b57cec5SDimitry Andric   // error, bail out.
22080b57cec5SDimitry Andric   SourceLocation CommaLoc;
22090b57cec5SDimitry Andric   while (TryConsumeToken(tok::comma, CommaLoc)) {
22100b57cec5SDimitry Andric     if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
22110b57cec5SDimitry Andric       // This comma was followed by a line-break and something which can't be
22120b57cec5SDimitry Andric       // the start of a declarator. The comma was probably a typo for a
22130b57cec5SDimitry Andric       // semicolon.
22140b57cec5SDimitry Andric       Diag(CommaLoc, diag::err_expected_semi_declaration)
22150b57cec5SDimitry Andric         << FixItHint::CreateReplacement(CommaLoc, ";");
22160b57cec5SDimitry Andric       ExpectSemi = false;
22170b57cec5SDimitry Andric       break;
22180b57cec5SDimitry Andric     }
22190b57cec5SDimitry Andric 
22200b57cec5SDimitry Andric     // Parse the next declarator.
22210b57cec5SDimitry Andric     D.clear();
22220b57cec5SDimitry Andric     D.setCommaLoc(CommaLoc);
22230b57cec5SDimitry Andric 
22240b57cec5SDimitry Andric     // Accept attributes in an init-declarator.  In the first declarator in a
22250b57cec5SDimitry Andric     // declaration, these would be part of the declspec.  In subsequent
22260b57cec5SDimitry Andric     // declarators, they become part of the declarator itself, so that they
22270b57cec5SDimitry Andric     // don't apply to declarators after *this* one.  Examples:
22280b57cec5SDimitry Andric     //    short __attribute__((common)) var;    -> declspec
22290b57cec5SDimitry Andric     //    short var __attribute__((common));    -> declarator
22300b57cec5SDimitry Andric     //    short x, __attribute__((common)) var;    -> declarator
22310b57cec5SDimitry Andric     MaybeParseGNUAttributes(D);
22320b57cec5SDimitry Andric 
22330b57cec5SDimitry Andric     // MSVC parses but ignores qualifiers after the comma as an extension.
22340b57cec5SDimitry Andric     if (getLangOpts().MicrosoftExt)
22350b57cec5SDimitry Andric       DiagnoseAndSkipExtendedMicrosoftTypeAttributes();
22360b57cec5SDimitry Andric 
22370b57cec5SDimitry Andric     ParseDeclarator(D);
2238*bdd1243dSDimitry Andric 
2239*bdd1243dSDimitry Andric     if (getLangOpts().HLSL)
2240*bdd1243dSDimitry Andric       MaybeParseHLSLSemantics(D);
2241*bdd1243dSDimitry Andric 
22420b57cec5SDimitry Andric     if (!D.isInvalidType()) {
2243480093f4SDimitry Andric       // C++2a [dcl.decl]p1
2244480093f4SDimitry Andric       //    init-declarator:
2245480093f4SDimitry Andric       //	      declarator initializer[opt]
2246480093f4SDimitry Andric       //        declarator requires-clause
2247480093f4SDimitry Andric       if (Tok.is(tok::kw_requires))
2248480093f4SDimitry Andric         ParseTrailingRequiresClause(D);
22490b57cec5SDimitry Andric       Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
22500b57cec5SDimitry Andric       D.complete(ThisDecl);
22510b57cec5SDimitry Andric       if (ThisDecl)
22520b57cec5SDimitry Andric         DeclsInGroup.push_back(ThisDecl);
22530b57cec5SDimitry Andric     }
22540b57cec5SDimitry Andric   }
22550b57cec5SDimitry Andric 
22560b57cec5SDimitry Andric   if (DeclEnd)
22570b57cec5SDimitry Andric     *DeclEnd = Tok.getLocation();
22580b57cec5SDimitry Andric 
2259e8d8bef9SDimitry Andric   if (ExpectSemi && ExpectAndConsumeSemi(
2260e8d8bef9SDimitry Andric                         Context == DeclaratorContext::File
22610b57cec5SDimitry Andric                             ? diag::err_invalid_token_after_toplevel_declarator
22620b57cec5SDimitry Andric                             : diag::err_expected_semi_declaration)) {
22630b57cec5SDimitry Andric     // Okay, there was no semicolon and one was expected.  If we see a
22640b57cec5SDimitry Andric     // declaration specifier, just assume it was missing and continue parsing.
22650b57cec5SDimitry Andric     // Otherwise things are very confused and we skip to recover.
2266*bdd1243dSDimitry Andric     if (!isDeclarationSpecifier(ImplicitTypenameContext::No)) {
22670b57cec5SDimitry Andric       SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
22680b57cec5SDimitry Andric       TryConsumeToken(tok::semi);
22690b57cec5SDimitry Andric     }
22700b57cec5SDimitry Andric   }
22710b57cec5SDimitry Andric 
22720b57cec5SDimitry Andric   return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
22730b57cec5SDimitry Andric }
22740b57cec5SDimitry Andric 
22750b57cec5SDimitry Andric /// Parse an optional simple-asm-expr and attributes, and attach them to a
22760b57cec5SDimitry Andric /// declarator. Returns true on an error.
22770b57cec5SDimitry Andric bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
22780b57cec5SDimitry Andric   // If a simple-asm-expr is present, parse it.
22790b57cec5SDimitry Andric   if (Tok.is(tok::kw_asm)) {
22800b57cec5SDimitry Andric     SourceLocation Loc;
2281480093f4SDimitry Andric     ExprResult AsmLabel(ParseSimpleAsm(/*ForAsmLabel*/ true, &Loc));
22820b57cec5SDimitry Andric     if (AsmLabel.isInvalid()) {
22830b57cec5SDimitry Andric       SkipUntil(tok::semi, StopBeforeMatch);
22840b57cec5SDimitry Andric       return true;
22850b57cec5SDimitry Andric     }
22860b57cec5SDimitry Andric 
22870b57cec5SDimitry Andric     D.setAsmLabel(AsmLabel.get());
22880b57cec5SDimitry Andric     D.SetRangeEnd(Loc);
22890b57cec5SDimitry Andric   }
22900b57cec5SDimitry Andric 
22910b57cec5SDimitry Andric   MaybeParseGNUAttributes(D);
22920b57cec5SDimitry Andric   return false;
22930b57cec5SDimitry Andric }
22940b57cec5SDimitry Andric 
22950b57cec5SDimitry Andric /// Parse 'declaration' after parsing 'declaration-specifiers
22960b57cec5SDimitry Andric /// declarator'. This method parses the remainder of the declaration
22970b57cec5SDimitry Andric /// (including any attributes or initializer, among other things) and
22980b57cec5SDimitry Andric /// finalizes the declaration.
22990b57cec5SDimitry Andric ///
23000b57cec5SDimitry Andric ///       init-declarator: [C99 6.7]
23010b57cec5SDimitry Andric ///         declarator
23020b57cec5SDimitry Andric ///         declarator '=' initializer
23030b57cec5SDimitry Andric /// [GNU]   declarator simple-asm-expr[opt] attributes[opt]
23040b57cec5SDimitry Andric /// [GNU]   declarator simple-asm-expr[opt] attributes[opt] '=' initializer
23050b57cec5SDimitry Andric /// [C++]   declarator initializer[opt]
23060b57cec5SDimitry Andric ///
23070b57cec5SDimitry Andric /// [C++] initializer:
23080b57cec5SDimitry Andric /// [C++]   '=' initializer-clause
23090b57cec5SDimitry Andric /// [C++]   '(' expression-list ')'
23100b57cec5SDimitry Andric /// [C++0x] '=' 'default'                                                [TODO]
23110b57cec5SDimitry Andric /// [C++0x] '=' 'delete'
23120b57cec5SDimitry Andric /// [C++0x] braced-init-list
23130b57cec5SDimitry Andric ///
23140b57cec5SDimitry Andric /// According to the standard grammar, =default and =delete are function
23150b57cec5SDimitry Andric /// definitions, but that definitely doesn't fit with the parser here.
23160b57cec5SDimitry Andric ///
23170b57cec5SDimitry Andric Decl *Parser::ParseDeclarationAfterDeclarator(
23180b57cec5SDimitry Andric     Declarator &D, const ParsedTemplateInfo &TemplateInfo) {
23190b57cec5SDimitry Andric   if (ParseAsmAttributesAfterDeclarator(D))
23200b57cec5SDimitry Andric     return nullptr;
23210b57cec5SDimitry Andric 
23220b57cec5SDimitry Andric   return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
23230b57cec5SDimitry Andric }
23240b57cec5SDimitry Andric 
23250b57cec5SDimitry Andric Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(
23260b57cec5SDimitry Andric     Declarator &D, const ParsedTemplateInfo &TemplateInfo, ForRangeInit *FRI) {
23270b57cec5SDimitry Andric   // RAII type used to track whether we're inside an initializer.
23280b57cec5SDimitry Andric   struct InitializerScopeRAII {
23290b57cec5SDimitry Andric     Parser &P;
23300b57cec5SDimitry Andric     Declarator &D;
23310b57cec5SDimitry Andric     Decl *ThisDecl;
23320b57cec5SDimitry Andric 
23330b57cec5SDimitry Andric     InitializerScopeRAII(Parser &P, Declarator &D, Decl *ThisDecl)
23340b57cec5SDimitry Andric         : P(P), D(D), ThisDecl(ThisDecl) {
23350b57cec5SDimitry Andric       if (ThisDecl && P.getLangOpts().CPlusPlus) {
23360b57cec5SDimitry Andric         Scope *S = nullptr;
23370b57cec5SDimitry Andric         if (D.getCXXScopeSpec().isSet()) {
23380b57cec5SDimitry Andric           P.EnterScope(0);
23390b57cec5SDimitry Andric           S = P.getCurScope();
23400b57cec5SDimitry Andric         }
23410b57cec5SDimitry Andric         P.Actions.ActOnCXXEnterDeclInitializer(S, ThisDecl);
23420b57cec5SDimitry Andric       }
23430b57cec5SDimitry Andric     }
23440b57cec5SDimitry Andric     ~InitializerScopeRAII() { pop(); }
23450b57cec5SDimitry Andric     void pop() {
23460b57cec5SDimitry Andric       if (ThisDecl && P.getLangOpts().CPlusPlus) {
23470b57cec5SDimitry Andric         Scope *S = nullptr;
23480b57cec5SDimitry Andric         if (D.getCXXScopeSpec().isSet())
23490b57cec5SDimitry Andric           S = P.getCurScope();
23500b57cec5SDimitry Andric         P.Actions.ActOnCXXExitDeclInitializer(S, ThisDecl);
23510b57cec5SDimitry Andric         if (S)
23520b57cec5SDimitry Andric           P.ExitScope();
23530b57cec5SDimitry Andric       }
23540b57cec5SDimitry Andric       ThisDecl = nullptr;
23550b57cec5SDimitry Andric     }
23560b57cec5SDimitry Andric   };
23570b57cec5SDimitry Andric 
2358e8d8bef9SDimitry Andric   enum class InitKind { Uninitialized, Equal, CXXDirect, CXXBraced };
2359e8d8bef9SDimitry Andric   InitKind TheInitKind;
2360e8d8bef9SDimitry Andric   // If a '==' or '+=' is found, suggest a fixit to '='.
2361e8d8bef9SDimitry Andric   if (isTokenEqualOrEqualTypo())
2362e8d8bef9SDimitry Andric     TheInitKind = InitKind::Equal;
2363e8d8bef9SDimitry Andric   else if (Tok.is(tok::l_paren))
2364e8d8bef9SDimitry Andric     TheInitKind = InitKind::CXXDirect;
2365e8d8bef9SDimitry Andric   else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
2366e8d8bef9SDimitry Andric            (!CurParsedObjCImpl || !D.isFunctionDeclarator()))
2367e8d8bef9SDimitry Andric     TheInitKind = InitKind::CXXBraced;
2368e8d8bef9SDimitry Andric   else
2369e8d8bef9SDimitry Andric     TheInitKind = InitKind::Uninitialized;
2370e8d8bef9SDimitry Andric   if (TheInitKind != InitKind::Uninitialized)
2371e8d8bef9SDimitry Andric     D.setHasInitializer();
2372e8d8bef9SDimitry Andric 
2373e8d8bef9SDimitry Andric   // Inform Sema that we just parsed this declarator.
23740b57cec5SDimitry Andric   Decl *ThisDecl = nullptr;
2375e8d8bef9SDimitry Andric   Decl *OuterDecl = nullptr;
23760b57cec5SDimitry Andric   switch (TemplateInfo.Kind) {
23770b57cec5SDimitry Andric   case ParsedTemplateInfo::NonTemplate:
23780b57cec5SDimitry Andric     ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
23790b57cec5SDimitry Andric     break;
23800b57cec5SDimitry Andric 
23810b57cec5SDimitry Andric   case ParsedTemplateInfo::Template:
23820b57cec5SDimitry Andric   case ParsedTemplateInfo::ExplicitSpecialization: {
23830b57cec5SDimitry Andric     ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
23840b57cec5SDimitry Andric                                                *TemplateInfo.TemplateParams,
23850b57cec5SDimitry Andric                                                D);
2386e8d8bef9SDimitry Andric     if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl)) {
23870b57cec5SDimitry Andric       // Re-direct this decl to refer to the templated decl so that we can
23880b57cec5SDimitry Andric       // initialize it.
23890b57cec5SDimitry Andric       ThisDecl = VT->getTemplatedDecl();
2390e8d8bef9SDimitry Andric       OuterDecl = VT;
2391e8d8bef9SDimitry Andric     }
23920b57cec5SDimitry Andric     break;
23930b57cec5SDimitry Andric   }
23940b57cec5SDimitry Andric   case ParsedTemplateInfo::ExplicitInstantiation: {
23950b57cec5SDimitry Andric     if (Tok.is(tok::semi)) {
23960b57cec5SDimitry Andric       DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
23970b57cec5SDimitry Andric           getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
23980b57cec5SDimitry Andric       if (ThisRes.isInvalid()) {
23990b57cec5SDimitry Andric         SkipUntil(tok::semi, StopBeforeMatch);
24000b57cec5SDimitry Andric         return nullptr;
24010b57cec5SDimitry Andric       }
24020b57cec5SDimitry Andric       ThisDecl = ThisRes.get();
24030b57cec5SDimitry Andric     } else {
24040b57cec5SDimitry Andric       // FIXME: This check should be for a variable template instantiation only.
24050b57cec5SDimitry Andric 
24060b57cec5SDimitry Andric       // Check that this is a valid instantiation
24070b57cec5SDimitry Andric       if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) {
24080b57cec5SDimitry Andric         // If the declarator-id is not a template-id, issue a diagnostic and
24090b57cec5SDimitry Andric         // recover by ignoring the 'template' keyword.
24100b57cec5SDimitry Andric         Diag(Tok, diag::err_template_defn_explicit_instantiation)
24110b57cec5SDimitry Andric             << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
24120b57cec5SDimitry Andric         ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
24130b57cec5SDimitry Andric       } else {
24140b57cec5SDimitry Andric         SourceLocation LAngleLoc =
24150b57cec5SDimitry Andric             PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
24160b57cec5SDimitry Andric         Diag(D.getIdentifierLoc(),
24170b57cec5SDimitry Andric              diag::err_explicit_instantiation_with_definition)
24180b57cec5SDimitry Andric             << SourceRange(TemplateInfo.TemplateLoc)
24190b57cec5SDimitry Andric             << FixItHint::CreateInsertion(LAngleLoc, "<>");
24200b57cec5SDimitry Andric 
24210b57cec5SDimitry Andric         // Recover as if it were an explicit specialization.
24220b57cec5SDimitry Andric         TemplateParameterLists FakedParamLists;
24230b57cec5SDimitry Andric         FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
2424*bdd1243dSDimitry Andric             0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc,
2425*bdd1243dSDimitry Andric             std::nullopt, LAngleLoc, nullptr));
24260b57cec5SDimitry Andric 
24270b57cec5SDimitry Andric         ThisDecl =
24280b57cec5SDimitry Andric             Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
24290b57cec5SDimitry Andric       }
24300b57cec5SDimitry Andric     }
24310b57cec5SDimitry Andric     break;
24320b57cec5SDimitry Andric     }
24330b57cec5SDimitry Andric   }
24340b57cec5SDimitry Andric 
2435e8d8bef9SDimitry Andric   switch (TheInitKind) {
24360b57cec5SDimitry Andric   // Parse declarator '=' initializer.
2437e8d8bef9SDimitry Andric   case InitKind::Equal: {
24380b57cec5SDimitry Andric     SourceLocation EqualLoc = ConsumeToken();
24390b57cec5SDimitry Andric 
24400b57cec5SDimitry Andric     if (Tok.is(tok::kw_delete)) {
24410b57cec5SDimitry Andric       if (D.isFunctionDeclarator())
24420b57cec5SDimitry Andric         Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
24430b57cec5SDimitry Andric           << 1 /* delete */;
24440b57cec5SDimitry Andric       else
24450b57cec5SDimitry Andric         Diag(ConsumeToken(), diag::err_deleted_non_function);
24460b57cec5SDimitry Andric     } else if (Tok.is(tok::kw_default)) {
24470b57cec5SDimitry Andric       if (D.isFunctionDeclarator())
24480b57cec5SDimitry Andric         Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
24490b57cec5SDimitry Andric           << 0 /* default */;
24500b57cec5SDimitry Andric       else
2451480093f4SDimitry Andric         Diag(ConsumeToken(), diag::err_default_special_members)
24525ffd83dbSDimitry Andric             << getLangOpts().CPlusPlus20;
24530b57cec5SDimitry Andric     } else {
24540b57cec5SDimitry Andric       InitializerScopeRAII InitScope(*this, D, ThisDecl);
24550b57cec5SDimitry Andric 
24560b57cec5SDimitry Andric       if (Tok.is(tok::code_completion)) {
2457fe6060f1SDimitry Andric         cutOffParsing();
24580b57cec5SDimitry Andric         Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
24590b57cec5SDimitry Andric         Actions.FinalizeDeclaration(ThisDecl);
24600b57cec5SDimitry Andric         return nullptr;
24610b57cec5SDimitry Andric       }
24620b57cec5SDimitry Andric 
24630b57cec5SDimitry Andric       PreferredType.enterVariableInit(Tok.getLocation(), ThisDecl);
24640b57cec5SDimitry Andric       ExprResult Init = ParseInitializer();
24650b57cec5SDimitry Andric 
24660b57cec5SDimitry Andric       // If this is the only decl in (possibly) range based for statement,
24670b57cec5SDimitry Andric       // our best guess is that the user meant ':' instead of '='.
24680b57cec5SDimitry Andric       if (Tok.is(tok::r_paren) && FRI && D.isFirstDeclarator()) {
24690b57cec5SDimitry Andric         Diag(EqualLoc, diag::err_single_decl_assign_in_for_range)
24700b57cec5SDimitry Andric             << FixItHint::CreateReplacement(EqualLoc, ":");
24710b57cec5SDimitry Andric         // We are trying to stop parser from looking for ';' in this for
24720b57cec5SDimitry Andric         // statement, therefore preventing spurious errors to be issued.
24730b57cec5SDimitry Andric         FRI->ColonLoc = EqualLoc;
24740b57cec5SDimitry Andric         Init = ExprError();
24750b57cec5SDimitry Andric         FRI->RangeExpr = Init;
24760b57cec5SDimitry Andric       }
24770b57cec5SDimitry Andric 
24780b57cec5SDimitry Andric       InitScope.pop();
24790b57cec5SDimitry Andric 
24800b57cec5SDimitry Andric       if (Init.isInvalid()) {
24810b57cec5SDimitry Andric         SmallVector<tok::TokenKind, 2> StopTokens;
24820b57cec5SDimitry Andric         StopTokens.push_back(tok::comma);
2483e8d8bef9SDimitry Andric         if (D.getContext() == DeclaratorContext::ForInit ||
2484e8d8bef9SDimitry Andric             D.getContext() == DeclaratorContext::SelectionInit)
24850b57cec5SDimitry Andric           StopTokens.push_back(tok::r_paren);
24860b57cec5SDimitry Andric         SkipUntil(StopTokens, StopAtSemi | StopBeforeMatch);
24870b57cec5SDimitry Andric         Actions.ActOnInitializerError(ThisDecl);
24880b57cec5SDimitry Andric       } else
24890b57cec5SDimitry Andric         Actions.AddInitializerToDecl(ThisDecl, Init.get(),
24900b57cec5SDimitry Andric                                      /*DirectInit=*/false);
24910b57cec5SDimitry Andric     }
2492e8d8bef9SDimitry Andric     break;
2493e8d8bef9SDimitry Andric   }
2494e8d8bef9SDimitry Andric   case InitKind::CXXDirect: {
24950b57cec5SDimitry Andric     // Parse C++ direct initializer: '(' expression-list ')'
24960b57cec5SDimitry Andric     BalancedDelimiterTracker T(*this, tok::l_paren);
24970b57cec5SDimitry Andric     T.consumeOpen();
24980b57cec5SDimitry Andric 
24990b57cec5SDimitry Andric     ExprVector Exprs;
25000b57cec5SDimitry Andric 
25010b57cec5SDimitry Andric     InitializerScopeRAII InitScope(*this, D, ThisDecl);
25020b57cec5SDimitry Andric 
25030b57cec5SDimitry Andric     auto ThisVarDecl = dyn_cast_or_null<VarDecl>(ThisDecl);
25040b57cec5SDimitry Andric     auto RunSignatureHelp = [&]() {
25050b57cec5SDimitry Andric       QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
250604eeddc0SDimitry Andric           ThisVarDecl->getType()->getCanonicalTypeInternal(),
250704eeddc0SDimitry Andric           ThisDecl->getLocation(), Exprs, T.getOpenLocation(),
250804eeddc0SDimitry Andric           /*Braced=*/false);
25090b57cec5SDimitry Andric       CalledSignatureHelp = true;
25100b57cec5SDimitry Andric       return PreferredType;
25110b57cec5SDimitry Andric     };
25120b57cec5SDimitry Andric     auto SetPreferredType = [&] {
25130b57cec5SDimitry Andric       PreferredType.enterFunctionArgument(Tok.getLocation(), RunSignatureHelp);
25140b57cec5SDimitry Andric     };
25150b57cec5SDimitry Andric 
25160b57cec5SDimitry Andric     llvm::function_ref<void()> ExpressionStarts;
25170b57cec5SDimitry Andric     if (ThisVarDecl) {
25180b57cec5SDimitry Andric       // ParseExpressionList can sometimes succeed even when ThisDecl is not
25190b57cec5SDimitry Andric       // VarDecl. This is an error and it is reported in a call to
25200b57cec5SDimitry Andric       // Actions.ActOnInitializerError(). However, we call
25210b57cec5SDimitry Andric       // ProduceConstructorSignatureHelp only on VarDecls.
25220b57cec5SDimitry Andric       ExpressionStarts = SetPreferredType;
25230b57cec5SDimitry Andric     }
2524*bdd1243dSDimitry Andric     if (ParseExpressionList(Exprs, ExpressionStarts)) {
25250b57cec5SDimitry Andric       if (ThisVarDecl && PP.isCodeCompletionReached() && !CalledSignatureHelp) {
25260b57cec5SDimitry Andric         Actions.ProduceConstructorSignatureHelp(
252704eeddc0SDimitry Andric             ThisVarDecl->getType()->getCanonicalTypeInternal(),
252804eeddc0SDimitry Andric             ThisDecl->getLocation(), Exprs, T.getOpenLocation(),
252904eeddc0SDimitry Andric             /*Braced=*/false);
25300b57cec5SDimitry Andric         CalledSignatureHelp = true;
25310b57cec5SDimitry Andric       }
25320b57cec5SDimitry Andric       Actions.ActOnInitializerError(ThisDecl);
25330b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
25340b57cec5SDimitry Andric     } else {
25350b57cec5SDimitry Andric       // Match the ')'.
25360b57cec5SDimitry Andric       T.consumeClose();
25370b57cec5SDimitry Andric       InitScope.pop();
25380b57cec5SDimitry Andric 
25390b57cec5SDimitry Andric       ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
25400b57cec5SDimitry Andric                                                           T.getCloseLocation(),
25410b57cec5SDimitry Andric                                                           Exprs);
25420b57cec5SDimitry Andric       Actions.AddInitializerToDecl(ThisDecl, Initializer.get(),
25430b57cec5SDimitry Andric                                    /*DirectInit=*/true);
25440b57cec5SDimitry Andric     }
2545e8d8bef9SDimitry Andric     break;
2546e8d8bef9SDimitry Andric   }
2547e8d8bef9SDimitry Andric   case InitKind::CXXBraced: {
25480b57cec5SDimitry Andric     // Parse C++0x braced-init-list.
25490b57cec5SDimitry Andric     Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
25500b57cec5SDimitry Andric 
25510b57cec5SDimitry Andric     InitializerScopeRAII InitScope(*this, D, ThisDecl);
25520b57cec5SDimitry Andric 
25535ffd83dbSDimitry Andric     PreferredType.enterVariableInit(Tok.getLocation(), ThisDecl);
25540b57cec5SDimitry Andric     ExprResult Init(ParseBraceInitializer());
25550b57cec5SDimitry Andric 
25560b57cec5SDimitry Andric     InitScope.pop();
25570b57cec5SDimitry Andric 
25580b57cec5SDimitry Andric     if (Init.isInvalid()) {
25590b57cec5SDimitry Andric       Actions.ActOnInitializerError(ThisDecl);
25600b57cec5SDimitry Andric     } else
25610b57cec5SDimitry Andric       Actions.AddInitializerToDecl(ThisDecl, Init.get(), /*DirectInit=*/true);
2562e8d8bef9SDimitry Andric     break;
2563e8d8bef9SDimitry Andric   }
2564e8d8bef9SDimitry Andric   case InitKind::Uninitialized: {
25650b57cec5SDimitry Andric     Actions.ActOnUninitializedDecl(ThisDecl);
2566e8d8bef9SDimitry Andric     break;
2567e8d8bef9SDimitry Andric   }
25680b57cec5SDimitry Andric   }
25690b57cec5SDimitry Andric 
25700b57cec5SDimitry Andric   Actions.FinalizeDeclaration(ThisDecl);
2571e8d8bef9SDimitry Andric   return OuterDecl ? OuterDecl : ThisDecl;
25720b57cec5SDimitry Andric }
25730b57cec5SDimitry Andric 
25740b57cec5SDimitry Andric /// ParseSpecifierQualifierList
25750b57cec5SDimitry Andric ///        specifier-qualifier-list:
25760b57cec5SDimitry Andric ///          type-specifier specifier-qualifier-list[opt]
25770b57cec5SDimitry Andric ///          type-qualifier specifier-qualifier-list[opt]
25780b57cec5SDimitry Andric /// [GNU]    attributes     specifier-qualifier-list[opt]
25790b57cec5SDimitry Andric ///
2580*bdd1243dSDimitry Andric void Parser::ParseSpecifierQualifierList(
2581*bdd1243dSDimitry Andric     DeclSpec &DS, ImplicitTypenameContext AllowImplicitTypename,
2582*bdd1243dSDimitry Andric     AccessSpecifier AS, DeclSpecContext DSC) {
25830b57cec5SDimitry Andric   /// specifier-qualifier-list is a subset of declaration-specifiers.  Just
25840b57cec5SDimitry Andric   /// parse declaration-specifiers and complain about extra stuff.
25850b57cec5SDimitry Andric   /// TODO: diagnose attribute-specifiers and alignment-specifiers.
2586*bdd1243dSDimitry Andric   ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC, nullptr,
2587*bdd1243dSDimitry Andric                              AllowImplicitTypename);
25880b57cec5SDimitry Andric 
25890b57cec5SDimitry Andric   // Validate declspec for type-name.
25900b57cec5SDimitry Andric   unsigned Specs = DS.getParsedSpecifiers();
25910b57cec5SDimitry Andric   if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) {
25920b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_type);
25930b57cec5SDimitry Andric     DS.SetTypeSpecError();
25940b57cec5SDimitry Andric   } else if (Specs == DeclSpec::PQ_None && !DS.hasAttributes()) {
25950b57cec5SDimitry Andric     Diag(Tok, diag::err_typename_requires_specqual);
25960b57cec5SDimitry Andric     if (!DS.hasTypeSpecifier())
25970b57cec5SDimitry Andric       DS.SetTypeSpecError();
25980b57cec5SDimitry Andric   }
25990b57cec5SDimitry Andric 
26000b57cec5SDimitry Andric   // Issue diagnostic and remove storage class if present.
26010b57cec5SDimitry Andric   if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
26020b57cec5SDimitry Andric     if (DS.getStorageClassSpecLoc().isValid())
26030b57cec5SDimitry Andric       Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
26040b57cec5SDimitry Andric     else
26050b57cec5SDimitry Andric       Diag(DS.getThreadStorageClassSpecLoc(),
26060b57cec5SDimitry Andric            diag::err_typename_invalid_storageclass);
26070b57cec5SDimitry Andric     DS.ClearStorageClassSpecs();
26080b57cec5SDimitry Andric   }
26090b57cec5SDimitry Andric 
26100b57cec5SDimitry Andric   // Issue diagnostic and remove function specifier if present.
26110b57cec5SDimitry Andric   if (Specs & DeclSpec::PQ_FunctionSpecifier) {
26120b57cec5SDimitry Andric     if (DS.isInlineSpecified())
26130b57cec5SDimitry Andric       Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
26140b57cec5SDimitry Andric     if (DS.isVirtualSpecified())
26150b57cec5SDimitry Andric       Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
26160b57cec5SDimitry Andric     if (DS.hasExplicitSpecifier())
26170b57cec5SDimitry Andric       Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
2618*bdd1243dSDimitry Andric     if (DS.isNoreturnSpecified())
2619*bdd1243dSDimitry Andric       Diag(DS.getNoreturnSpecLoc(), diag::err_typename_invalid_functionspec);
26200b57cec5SDimitry Andric     DS.ClearFunctionSpecs();
26210b57cec5SDimitry Andric   }
26220b57cec5SDimitry Andric 
26230b57cec5SDimitry Andric   // Issue diagnostic and remove constexpr specifier if present.
26240b57cec5SDimitry Andric   if (DS.hasConstexprSpecifier() && DSC != DeclSpecContext::DSC_condition) {
26250b57cec5SDimitry Andric     Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr)
2626e8d8bef9SDimitry Andric         << static_cast<int>(DS.getConstexprSpecifier());
26270b57cec5SDimitry Andric     DS.ClearConstexprSpec();
26280b57cec5SDimitry Andric   }
26290b57cec5SDimitry Andric }
26300b57cec5SDimitry Andric 
26310b57cec5SDimitry Andric /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
26320b57cec5SDimitry Andric /// specified token is valid after the identifier in a declarator which
26330b57cec5SDimitry Andric /// immediately follows the declspec.  For example, these things are valid:
26340b57cec5SDimitry Andric ///
26350b57cec5SDimitry Andric ///      int x   [             4];         // direct-declarator
26360b57cec5SDimitry Andric ///      int x   (             int y);     // direct-declarator
26370b57cec5SDimitry Andric ///  int(int x   )                         // direct-declarator
26380b57cec5SDimitry Andric ///      int x   ;                         // simple-declaration
26390b57cec5SDimitry Andric ///      int x   =             17;         // init-declarator-list
26400b57cec5SDimitry Andric ///      int x   ,             y;          // init-declarator-list
26410b57cec5SDimitry Andric ///      int x   __asm__       ("foo");    // init-declarator-list
26420b57cec5SDimitry Andric ///      int x   :             4;          // struct-declarator
26430b57cec5SDimitry Andric ///      int x   {             5};         // C++'0x unified initializers
26440b57cec5SDimitry Andric ///
26450b57cec5SDimitry Andric /// This is not, because 'x' does not immediately follow the declspec (though
26460b57cec5SDimitry Andric /// ')' happens to be valid anyway).
26470b57cec5SDimitry Andric ///    int (x)
26480b57cec5SDimitry Andric ///
26490b57cec5SDimitry Andric static bool isValidAfterIdentifierInDeclarator(const Token &T) {
26500b57cec5SDimitry Andric   return T.isOneOf(tok::l_square, tok::l_paren, tok::r_paren, tok::semi,
26510b57cec5SDimitry Andric                    tok::comma, tok::equal, tok::kw_asm, tok::l_brace,
26520b57cec5SDimitry Andric                    tok::colon);
26530b57cec5SDimitry Andric }
26540b57cec5SDimitry Andric 
26550b57cec5SDimitry Andric /// ParseImplicitInt - This method is called when we have an non-typename
26560b57cec5SDimitry Andric /// identifier in a declspec (which normally terminates the decl spec) when
26570b57cec5SDimitry Andric /// the declspec has no type specifier.  In this case, the declspec is either
26580b57cec5SDimitry Andric /// malformed or is "implicit int" (in K&R and C89).
26590b57cec5SDimitry Andric ///
26600b57cec5SDimitry Andric /// This method handles diagnosing this prettily and returns false if the
26610b57cec5SDimitry Andric /// declspec is done being processed.  If it recovers and thinks there may be
26620b57cec5SDimitry Andric /// other pieces of declspec after it, it returns true.
26630b57cec5SDimitry Andric ///
26640b57cec5SDimitry Andric bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
26650b57cec5SDimitry Andric                               const ParsedTemplateInfo &TemplateInfo,
26660b57cec5SDimitry Andric                               AccessSpecifier AS, DeclSpecContext DSC,
266781ad6265SDimitry Andric                               ParsedAttributes &Attrs) {
26680b57cec5SDimitry Andric   assert(Tok.is(tok::identifier) && "should have identifier");
26690b57cec5SDimitry Andric 
26700b57cec5SDimitry Andric   SourceLocation Loc = Tok.getLocation();
26710b57cec5SDimitry Andric   // If we see an identifier that is not a type name, we normally would
26720b57cec5SDimitry Andric   // parse it as the identifier being declared.  However, when a typename
26730b57cec5SDimitry Andric   // is typo'd or the definition is not included, this will incorrectly
26740b57cec5SDimitry Andric   // parse the typename as the identifier name and fall over misparsing
26750b57cec5SDimitry Andric   // later parts of the diagnostic.
26760b57cec5SDimitry Andric   //
26770b57cec5SDimitry Andric   // As such, we try to do some look-ahead in cases where this would
26780b57cec5SDimitry Andric   // otherwise be an "implicit-int" case to see if this is invalid.  For
26790b57cec5SDimitry Andric   // example: "static foo_t x = 4;"  In this case, if we parsed foo_t as
26800b57cec5SDimitry Andric   // an identifier with implicit int, we'd get a parse error because the
26810b57cec5SDimitry Andric   // next token is obviously invalid for a type.  Parse these as a case
26820b57cec5SDimitry Andric   // with an invalid type specifier.
26830b57cec5SDimitry Andric   assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
26840b57cec5SDimitry Andric 
26850b57cec5SDimitry Andric   // Since we know that this either implicit int (which is rare) or an
26860b57cec5SDimitry Andric   // error, do lookahead to try to do better recovery. This never applies
26870b57cec5SDimitry Andric   // within a type specifier. Outside of C++, we allow this even if the
26880b57cec5SDimitry Andric   // language doesn't "officially" support implicit int -- we support
268981ad6265SDimitry Andric   // implicit int as an extension in some language modes.
269081ad6265SDimitry Andric   if (!isTypeSpecifier(DSC) && getLangOpts().isImplicitIntAllowed() &&
26910b57cec5SDimitry Andric       isValidAfterIdentifierInDeclarator(NextToken())) {
26920b57cec5SDimitry Andric     // If this token is valid for implicit int, e.g. "static x = 4", then
26930b57cec5SDimitry Andric     // we just avoid eating the identifier, so it will be parsed as the
26940b57cec5SDimitry Andric     // identifier in the declarator.
26950b57cec5SDimitry Andric     return false;
26960b57cec5SDimitry Andric   }
26970b57cec5SDimitry Andric 
26980b57cec5SDimitry Andric   // Early exit as Sema has a dedicated missing_actual_pipe_type diagnostic
26990b57cec5SDimitry Andric   // for incomplete declarations such as `pipe p`.
27000b57cec5SDimitry Andric   if (getLangOpts().OpenCLCPlusPlus && DS.isTypeSpecPipe())
27010b57cec5SDimitry Andric     return false;
27020b57cec5SDimitry Andric 
27030b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus &&
27040b57cec5SDimitry Andric       DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
27050b57cec5SDimitry Andric     // Don't require a type specifier if we have the 'auto' storage class
27060b57cec5SDimitry Andric     // specifier in C++98 -- we'll promote it to a type specifier.
27070b57cec5SDimitry Andric     if (SS)
27080b57cec5SDimitry Andric       AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
27090b57cec5SDimitry Andric     return false;
27100b57cec5SDimitry Andric   }
27110b57cec5SDimitry Andric 
27120b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus && (!SS || SS->isEmpty()) &&
27130b57cec5SDimitry Andric       getLangOpts().MSVCCompat) {
27140b57cec5SDimitry Andric     // Lookup of an unqualified type name has failed in MSVC compatibility mode.
27150b57cec5SDimitry Andric     // Give Sema a chance to recover if we are in a template with dependent base
27160b57cec5SDimitry Andric     // classes.
27170b57cec5SDimitry Andric     if (ParsedType T = Actions.ActOnMSVCUnknownTypeName(
27180b57cec5SDimitry Andric             *Tok.getIdentifierInfo(), Tok.getLocation(),
27190b57cec5SDimitry Andric             DSC == DeclSpecContext::DSC_template_type_arg)) {
27200b57cec5SDimitry Andric       const char *PrevSpec;
27210b57cec5SDimitry Andric       unsigned DiagID;
27220b57cec5SDimitry Andric       DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
27230b57cec5SDimitry Andric                          Actions.getASTContext().getPrintingPolicy());
27240b57cec5SDimitry Andric       DS.SetRangeEnd(Tok.getLocation());
27250b57cec5SDimitry Andric       ConsumeToken();
27260b57cec5SDimitry Andric       return false;
27270b57cec5SDimitry Andric     }
27280b57cec5SDimitry Andric   }
27290b57cec5SDimitry Andric 
27300b57cec5SDimitry Andric   // Otherwise, if we don't consume this token, we are going to emit an
27310b57cec5SDimitry Andric   // error anyway.  Try to recover from various common problems.  Check
27320b57cec5SDimitry Andric   // to see if this was a reference to a tag name without a tag specified.
27330b57cec5SDimitry Andric   // This is a common problem in C (saying 'foo' instead of 'struct foo').
27340b57cec5SDimitry Andric   //
27350b57cec5SDimitry Andric   // C++ doesn't need this, and isTagName doesn't take SS.
27360b57cec5SDimitry Andric   if (SS == nullptr) {
27370b57cec5SDimitry Andric     const char *TagName = nullptr, *FixitTagName = nullptr;
27380b57cec5SDimitry Andric     tok::TokenKind TagKind = tok::unknown;
27390b57cec5SDimitry Andric 
27400b57cec5SDimitry Andric     switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
27410b57cec5SDimitry Andric       default: break;
27420b57cec5SDimitry Andric       case DeclSpec::TST_enum:
27430b57cec5SDimitry Andric         TagName="enum"  ; FixitTagName = "enum "  ; TagKind=tok::kw_enum ;break;
27440b57cec5SDimitry Andric       case DeclSpec::TST_union:
27450b57cec5SDimitry Andric         TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
27460b57cec5SDimitry Andric       case DeclSpec::TST_struct:
27470b57cec5SDimitry Andric         TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
27480b57cec5SDimitry Andric       case DeclSpec::TST_interface:
27490b57cec5SDimitry Andric         TagName="__interface"; FixitTagName = "__interface ";
27500b57cec5SDimitry Andric         TagKind=tok::kw___interface;break;
27510b57cec5SDimitry Andric       case DeclSpec::TST_class:
27520b57cec5SDimitry Andric         TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
27530b57cec5SDimitry Andric     }
27540b57cec5SDimitry Andric 
27550b57cec5SDimitry Andric     if (TagName) {
27560b57cec5SDimitry Andric       IdentifierInfo *TokenName = Tok.getIdentifierInfo();
27570b57cec5SDimitry Andric       LookupResult R(Actions, TokenName, SourceLocation(),
27580b57cec5SDimitry Andric                      Sema::LookupOrdinaryName);
27590b57cec5SDimitry Andric 
27600b57cec5SDimitry Andric       Diag(Loc, diag::err_use_of_tag_name_without_tag)
27610b57cec5SDimitry Andric         << TokenName << TagName << getLangOpts().CPlusPlus
27620b57cec5SDimitry Andric         << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
27630b57cec5SDimitry Andric 
27640b57cec5SDimitry Andric       if (Actions.LookupParsedName(R, getCurScope(), SS)) {
27650b57cec5SDimitry Andric         for (LookupResult::iterator I = R.begin(), IEnd = R.end();
27660b57cec5SDimitry Andric              I != IEnd; ++I)
27670b57cec5SDimitry Andric           Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
27680b57cec5SDimitry Andric             << TokenName << TagName;
27690b57cec5SDimitry Andric       }
27700b57cec5SDimitry Andric 
27710b57cec5SDimitry Andric       // Parse this as a tag as if the missing tag were present.
27720b57cec5SDimitry Andric       if (TagKind == tok::kw_enum)
27730b57cec5SDimitry Andric         ParseEnumSpecifier(Loc, DS, TemplateInfo, AS,
27740b57cec5SDimitry Andric                            DeclSpecContext::DSC_normal);
27750b57cec5SDimitry Andric       else
27760b57cec5SDimitry Andric         ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
27770b57cec5SDimitry Andric                             /*EnteringContext*/ false,
27780b57cec5SDimitry Andric                             DeclSpecContext::DSC_normal, Attrs);
27790b57cec5SDimitry Andric       return true;
27800b57cec5SDimitry Andric     }
27810b57cec5SDimitry Andric   }
27820b57cec5SDimitry Andric 
27830b57cec5SDimitry Andric   // Determine whether this identifier could plausibly be the name of something
27840b57cec5SDimitry Andric   // being declared (with a missing type).
27850b57cec5SDimitry Andric   if (!isTypeSpecifier(DSC) && (!SS || DSC == DeclSpecContext::DSC_top_level ||
27860b57cec5SDimitry Andric                                 DSC == DeclSpecContext::DSC_class)) {
27870b57cec5SDimitry Andric     // Look ahead to the next token to try to figure out what this declaration
27880b57cec5SDimitry Andric     // was supposed to be.
27890b57cec5SDimitry Andric     switch (NextToken().getKind()) {
27900b57cec5SDimitry Andric     case tok::l_paren: {
27910b57cec5SDimitry Andric       // static x(4); // 'x' is not a type
27920b57cec5SDimitry Andric       // x(int n);    // 'x' is not a type
27930b57cec5SDimitry Andric       // x (*p)[];    // 'x' is a type
27940b57cec5SDimitry Andric       //
27950b57cec5SDimitry Andric       // Since we're in an error case, we can afford to perform a tentative
27960b57cec5SDimitry Andric       // parse to determine which case we're in.
27970b57cec5SDimitry Andric       TentativeParsingAction PA(*this);
27980b57cec5SDimitry Andric       ConsumeToken();
27990b57cec5SDimitry Andric       TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
28000b57cec5SDimitry Andric       PA.Revert();
28010b57cec5SDimitry Andric 
28020b57cec5SDimitry Andric       if (TPR != TPResult::False) {
28030b57cec5SDimitry Andric         // The identifier is followed by a parenthesized declarator.
28040b57cec5SDimitry Andric         // It's supposed to be a type.
28050b57cec5SDimitry Andric         break;
28060b57cec5SDimitry Andric       }
28070b57cec5SDimitry Andric 
28080b57cec5SDimitry Andric       // If we're in a context where we could be declaring a constructor,
28090b57cec5SDimitry Andric       // check whether this is a constructor declaration with a bogus name.
28100b57cec5SDimitry Andric       if (DSC == DeclSpecContext::DSC_class ||
28110b57cec5SDimitry Andric           (DSC == DeclSpecContext::DSC_top_level && SS)) {
28120b57cec5SDimitry Andric         IdentifierInfo *II = Tok.getIdentifierInfo();
28130b57cec5SDimitry Andric         if (Actions.isCurrentClassNameTypo(II, SS)) {
28140b57cec5SDimitry Andric           Diag(Loc, diag::err_constructor_bad_name)
28150b57cec5SDimitry Andric             << Tok.getIdentifierInfo() << II
28160b57cec5SDimitry Andric             << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
28170b57cec5SDimitry Andric           Tok.setIdentifierInfo(II);
28180b57cec5SDimitry Andric         }
28190b57cec5SDimitry Andric       }
28200b57cec5SDimitry Andric       // Fall through.
2821*bdd1243dSDimitry Andric       [[fallthrough]];
28220b57cec5SDimitry Andric     }
28230b57cec5SDimitry Andric     case tok::comma:
28240b57cec5SDimitry Andric     case tok::equal:
28250b57cec5SDimitry Andric     case tok::kw_asm:
28260b57cec5SDimitry Andric     case tok::l_brace:
28270b57cec5SDimitry Andric     case tok::l_square:
28280b57cec5SDimitry Andric     case tok::semi:
28290b57cec5SDimitry Andric       // This looks like a variable or function declaration. The type is
28300b57cec5SDimitry Andric       // probably missing. We're done parsing decl-specifiers.
28310b57cec5SDimitry Andric       // But only if we are not in a function prototype scope.
28320b57cec5SDimitry Andric       if (getCurScope()->isFunctionPrototypeScope())
28330b57cec5SDimitry Andric         break;
28340b57cec5SDimitry Andric       if (SS)
28350b57cec5SDimitry Andric         AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
28360b57cec5SDimitry Andric       return false;
28370b57cec5SDimitry Andric 
28380b57cec5SDimitry Andric     default:
28390b57cec5SDimitry Andric       // This is probably supposed to be a type. This includes cases like:
28400b57cec5SDimitry Andric       //   int f(itn);
28415ffd83dbSDimitry Andric       //   struct S { unsigned : 4; };
28420b57cec5SDimitry Andric       break;
28430b57cec5SDimitry Andric     }
28440b57cec5SDimitry Andric   }
28450b57cec5SDimitry Andric 
28460b57cec5SDimitry Andric   // This is almost certainly an invalid type name. Let Sema emit a diagnostic
28470b57cec5SDimitry Andric   // and attempt to recover.
28480b57cec5SDimitry Andric   ParsedType T;
28490b57cec5SDimitry Andric   IdentifierInfo *II = Tok.getIdentifierInfo();
28500b57cec5SDimitry Andric   bool IsTemplateName = getLangOpts().CPlusPlus && NextToken().is(tok::less);
28510b57cec5SDimitry Andric   Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T,
28520b57cec5SDimitry Andric                                   IsTemplateName);
28530b57cec5SDimitry Andric   if (T) {
28540b57cec5SDimitry Andric     // The action has suggested that the type T could be used. Set that as
28550b57cec5SDimitry Andric     // the type in the declaration specifiers, consume the would-be type
28560b57cec5SDimitry Andric     // name token, and we're done.
28570b57cec5SDimitry Andric     const char *PrevSpec;
28580b57cec5SDimitry Andric     unsigned DiagID;
28590b57cec5SDimitry Andric     DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
28600b57cec5SDimitry Andric                        Actions.getASTContext().getPrintingPolicy());
28610b57cec5SDimitry Andric     DS.SetRangeEnd(Tok.getLocation());
28620b57cec5SDimitry Andric     ConsumeToken();
28630b57cec5SDimitry Andric     // There may be other declaration specifiers after this.
28640b57cec5SDimitry Andric     return true;
28650b57cec5SDimitry Andric   } else if (II != Tok.getIdentifierInfo()) {
28660b57cec5SDimitry Andric     // If no type was suggested, the correction is to a keyword
28670b57cec5SDimitry Andric     Tok.setKind(II->getTokenID());
28680b57cec5SDimitry Andric     // There may be other declaration specifiers after this.
28690b57cec5SDimitry Andric     return true;
28700b57cec5SDimitry Andric   }
28710b57cec5SDimitry Andric 
28720b57cec5SDimitry Andric   // Otherwise, the action had no suggestion for us.  Mark this as an error.
28730b57cec5SDimitry Andric   DS.SetTypeSpecError();
28740b57cec5SDimitry Andric   DS.SetRangeEnd(Tok.getLocation());
28750b57cec5SDimitry Andric   ConsumeToken();
28760b57cec5SDimitry Andric 
28770b57cec5SDimitry Andric   // Eat any following template arguments.
28780b57cec5SDimitry Andric   if (IsTemplateName) {
28790b57cec5SDimitry Andric     SourceLocation LAngle, RAngle;
28800b57cec5SDimitry Andric     TemplateArgList Args;
28810b57cec5SDimitry Andric     ParseTemplateIdAfterTemplateName(true, LAngle, Args, RAngle);
28820b57cec5SDimitry Andric   }
28830b57cec5SDimitry Andric 
28840b57cec5SDimitry Andric   // TODO: Could inject an invalid typedef decl in an enclosing scope to
28850b57cec5SDimitry Andric   // avoid rippling error messages on subsequent uses of the same type,
28860b57cec5SDimitry Andric   // could be useful if #include was forgotten.
28870b57cec5SDimitry Andric   return true;
28880b57cec5SDimitry Andric }
28890b57cec5SDimitry Andric 
28900b57cec5SDimitry Andric /// Determine the declaration specifier context from the declarator
28910b57cec5SDimitry Andric /// context.
28920b57cec5SDimitry Andric ///
28930b57cec5SDimitry Andric /// \param Context the declarator context, which is one of the
28940b57cec5SDimitry Andric /// DeclaratorContext enumerator values.
28950b57cec5SDimitry Andric Parser::DeclSpecContext
28960b57cec5SDimitry Andric Parser::getDeclSpecContextFromDeclaratorContext(DeclaratorContext Context) {
2897*bdd1243dSDimitry Andric   switch (Context) {
2898*bdd1243dSDimitry Andric   case DeclaratorContext::Member:
28990b57cec5SDimitry Andric     return DeclSpecContext::DSC_class;
2900*bdd1243dSDimitry Andric   case DeclaratorContext::File:
29010b57cec5SDimitry Andric     return DeclSpecContext::DSC_top_level;
2902*bdd1243dSDimitry Andric   case DeclaratorContext::TemplateParam:
29030b57cec5SDimitry Andric     return DeclSpecContext::DSC_template_param;
2904*bdd1243dSDimitry Andric   case DeclaratorContext::TemplateArg:
2905*bdd1243dSDimitry Andric     return DeclSpecContext::DSC_template_arg;
2906*bdd1243dSDimitry Andric   case DeclaratorContext::TemplateTypeArg:
29070b57cec5SDimitry Andric     return DeclSpecContext::DSC_template_type_arg;
2908*bdd1243dSDimitry Andric   case DeclaratorContext::TrailingReturn:
2909*bdd1243dSDimitry Andric   case DeclaratorContext::TrailingReturnVar:
29100b57cec5SDimitry Andric     return DeclSpecContext::DSC_trailing;
2911*bdd1243dSDimitry Andric   case DeclaratorContext::AliasDecl:
2912*bdd1243dSDimitry Andric   case DeclaratorContext::AliasTemplate:
29130b57cec5SDimitry Andric     return DeclSpecContext::DSC_alias_declaration;
2914*bdd1243dSDimitry Andric   case DeclaratorContext::Association:
291581ad6265SDimitry Andric     return DeclSpecContext::DSC_association;
2916*bdd1243dSDimitry Andric   case DeclaratorContext::TypeName:
2917*bdd1243dSDimitry Andric     return DeclSpecContext::DSC_type_specifier;
2918*bdd1243dSDimitry Andric   case DeclaratorContext::Condition:
2919*bdd1243dSDimitry Andric     return DeclSpecContext::DSC_condition;
2920*bdd1243dSDimitry Andric   case DeclaratorContext::ConversionId:
2921*bdd1243dSDimitry Andric     return DeclSpecContext::DSC_conv_operator;
2922*bdd1243dSDimitry Andric   case DeclaratorContext::Prototype:
2923*bdd1243dSDimitry Andric   case DeclaratorContext::ObjCResult:
2924*bdd1243dSDimitry Andric   case DeclaratorContext::ObjCParameter:
2925*bdd1243dSDimitry Andric   case DeclaratorContext::KNRTypeList:
2926*bdd1243dSDimitry Andric   case DeclaratorContext::FunctionalCast:
2927*bdd1243dSDimitry Andric   case DeclaratorContext::Block:
2928*bdd1243dSDimitry Andric   case DeclaratorContext::ForInit:
2929*bdd1243dSDimitry Andric   case DeclaratorContext::SelectionInit:
2930*bdd1243dSDimitry Andric   case DeclaratorContext::CXXNew:
2931*bdd1243dSDimitry Andric   case DeclaratorContext::CXXCatch:
2932*bdd1243dSDimitry Andric   case DeclaratorContext::ObjCCatch:
2933*bdd1243dSDimitry Andric   case DeclaratorContext::BlockLiteral:
2934*bdd1243dSDimitry Andric   case DeclaratorContext::LambdaExpr:
2935*bdd1243dSDimitry Andric   case DeclaratorContext::LambdaExprParameter:
2936*bdd1243dSDimitry Andric   case DeclaratorContext::RequiresExpr:
29370b57cec5SDimitry Andric     return DeclSpecContext::DSC_normal;
29380b57cec5SDimitry Andric   }
29390b57cec5SDimitry Andric 
2940*bdd1243dSDimitry Andric   llvm_unreachable("Missing DeclaratorContext case");
2941*bdd1243dSDimitry Andric }
2942*bdd1243dSDimitry Andric 
29430b57cec5SDimitry Andric /// ParseAlignArgument - Parse the argument to an alignment-specifier.
29440b57cec5SDimitry Andric ///
29450b57cec5SDimitry Andric /// FIXME: Simply returns an alignof() expression if the argument is a
29460b57cec5SDimitry Andric /// type. Ideally, the type should be propagated directly into Sema.
29470b57cec5SDimitry Andric ///
29480b57cec5SDimitry Andric /// [C11]   type-id
29490b57cec5SDimitry Andric /// [C11]   constant-expression
29500b57cec5SDimitry Andric /// [C++0x] type-id ...[opt]
29510b57cec5SDimitry Andric /// [C++0x] assignment-expression ...[opt]
29520b57cec5SDimitry Andric ExprResult Parser::ParseAlignArgument(SourceLocation Start,
29530b57cec5SDimitry Andric                                       SourceLocation &EllipsisLoc) {
29540b57cec5SDimitry Andric   ExprResult ER;
29550b57cec5SDimitry Andric   if (isTypeIdInParens()) {
29560b57cec5SDimitry Andric     SourceLocation TypeLoc = Tok.getLocation();
29570b57cec5SDimitry Andric     ParsedType Ty = ParseTypeName().get();
29580b57cec5SDimitry Andric     SourceRange TypeRange(Start, Tok.getLocation());
29590b57cec5SDimitry Andric     ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
29600b57cec5SDimitry Andric                                                Ty.getAsOpaquePtr(), TypeRange);
29610b57cec5SDimitry Andric   } else
29620b57cec5SDimitry Andric     ER = ParseConstantExpression();
29630b57cec5SDimitry Andric 
29640b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus11)
29650b57cec5SDimitry Andric     TryConsumeToken(tok::ellipsis, EllipsisLoc);
29660b57cec5SDimitry Andric 
29670b57cec5SDimitry Andric   return ER;
29680b57cec5SDimitry Andric }
29690b57cec5SDimitry Andric 
29700b57cec5SDimitry Andric /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
29710b57cec5SDimitry Andric /// attribute to Attrs.
29720b57cec5SDimitry Andric ///
29730b57cec5SDimitry Andric /// alignment-specifier:
29740b57cec5SDimitry Andric /// [C11]   '_Alignas' '(' type-id ')'
29750b57cec5SDimitry Andric /// [C11]   '_Alignas' '(' constant-expression ')'
29760b57cec5SDimitry Andric /// [C++11] 'alignas' '(' type-id ...[opt] ')'
29770b57cec5SDimitry Andric /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
29780b57cec5SDimitry Andric void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
29790b57cec5SDimitry Andric                                      SourceLocation *EndLoc) {
29800b57cec5SDimitry Andric   assert(Tok.isOneOf(tok::kw_alignas, tok::kw__Alignas) &&
29810b57cec5SDimitry Andric          "Not an alignment-specifier!");
29820b57cec5SDimitry Andric 
29830b57cec5SDimitry Andric   IdentifierInfo *KWName = Tok.getIdentifierInfo();
29840b57cec5SDimitry Andric   SourceLocation KWLoc = ConsumeToken();
29850b57cec5SDimitry Andric 
29860b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
29870b57cec5SDimitry Andric   if (T.expectAndConsume())
29880b57cec5SDimitry Andric     return;
29890b57cec5SDimitry Andric 
29900b57cec5SDimitry Andric   SourceLocation EllipsisLoc;
29910b57cec5SDimitry Andric   ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
29920b57cec5SDimitry Andric   if (ArgExpr.isInvalid()) {
29930b57cec5SDimitry Andric     T.skipToEnd();
29940b57cec5SDimitry Andric     return;
29950b57cec5SDimitry Andric   }
29960b57cec5SDimitry Andric 
29970b57cec5SDimitry Andric   T.consumeClose();
29980b57cec5SDimitry Andric   if (EndLoc)
29990b57cec5SDimitry Andric     *EndLoc = T.getCloseLocation();
30000b57cec5SDimitry Andric 
30010b57cec5SDimitry Andric   ArgsVector ArgExprs;
30020b57cec5SDimitry Andric   ArgExprs.push_back(ArgExpr.get());
30030b57cec5SDimitry Andric   Attrs.addNew(KWName, KWLoc, nullptr, KWLoc, ArgExprs.data(), 1,
30040b57cec5SDimitry Andric                ParsedAttr::AS_Keyword, EllipsisLoc);
30050b57cec5SDimitry Andric }
30060b57cec5SDimitry Andric 
30075ffd83dbSDimitry Andric ExprResult Parser::ParseExtIntegerArgument() {
30080eae32dcSDimitry Andric   assert(Tok.isOneOf(tok::kw__ExtInt, tok::kw__BitInt) &&
30090eae32dcSDimitry Andric          "Not an extended int type");
30105ffd83dbSDimitry Andric   ConsumeToken();
30115ffd83dbSDimitry Andric 
30125ffd83dbSDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
30135ffd83dbSDimitry Andric   if (T.expectAndConsume())
30145ffd83dbSDimitry Andric     return ExprError();
30155ffd83dbSDimitry Andric 
30165ffd83dbSDimitry Andric   ExprResult ER = ParseConstantExpression();
30175ffd83dbSDimitry Andric   if (ER.isInvalid()) {
30185ffd83dbSDimitry Andric     T.skipToEnd();
30195ffd83dbSDimitry Andric     return ExprError();
30205ffd83dbSDimitry Andric   }
30215ffd83dbSDimitry Andric 
30225ffd83dbSDimitry Andric   if(T.consumeClose())
30235ffd83dbSDimitry Andric     return ExprError();
30245ffd83dbSDimitry Andric   return ER;
30255ffd83dbSDimitry Andric }
30265ffd83dbSDimitry Andric 
30270b57cec5SDimitry Andric /// Determine whether we're looking at something that might be a declarator
30280b57cec5SDimitry Andric /// in a simple-declaration. If it can't possibly be a declarator, maybe
30290b57cec5SDimitry Andric /// diagnose a missing semicolon after a prior tag definition in the decl
30300b57cec5SDimitry Andric /// specifier.
30310b57cec5SDimitry Andric ///
30320b57cec5SDimitry Andric /// \return \c true if an error occurred and this can't be any kind of
30330b57cec5SDimitry Andric /// declaration.
30340b57cec5SDimitry Andric bool
30350b57cec5SDimitry Andric Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
30360b57cec5SDimitry Andric                                               DeclSpecContext DSContext,
30370b57cec5SDimitry Andric                                               LateParsedAttrList *LateAttrs) {
30380b57cec5SDimitry Andric   assert(DS.hasTagDefinition() && "shouldn't call this");
30390b57cec5SDimitry Andric 
30400b57cec5SDimitry Andric   bool EnteringContext = (DSContext == DeclSpecContext::DSC_class ||
30410b57cec5SDimitry Andric                           DSContext == DeclSpecContext::DSC_top_level);
30420b57cec5SDimitry Andric 
30430b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus &&
30440b57cec5SDimitry Andric       Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype,
30450b57cec5SDimitry Andric                   tok::annot_template_id) &&
30460b57cec5SDimitry Andric       TryAnnotateCXXScopeToken(EnteringContext)) {
30470b57cec5SDimitry Andric     SkipMalformedDecl();
30480b57cec5SDimitry Andric     return true;
30490b57cec5SDimitry Andric   }
30500b57cec5SDimitry Andric 
30510b57cec5SDimitry Andric   bool HasScope = Tok.is(tok::annot_cxxscope);
30520b57cec5SDimitry Andric   // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
30530b57cec5SDimitry Andric   Token AfterScope = HasScope ? NextToken() : Tok;
30540b57cec5SDimitry Andric 
30550b57cec5SDimitry Andric   // Determine whether the following tokens could possibly be a
30560b57cec5SDimitry Andric   // declarator.
30570b57cec5SDimitry Andric   bool MightBeDeclarator = true;
30580b57cec5SDimitry Andric   if (Tok.isOneOf(tok::kw_typename, tok::annot_typename)) {
30590b57cec5SDimitry Andric     // A declarator-id can't start with 'typename'.
30600b57cec5SDimitry Andric     MightBeDeclarator = false;
30610b57cec5SDimitry Andric   } else if (AfterScope.is(tok::annot_template_id)) {
30620b57cec5SDimitry Andric     // If we have a type expressed as a template-id, this cannot be a
30630b57cec5SDimitry Andric     // declarator-id (such a type cannot be redeclared in a simple-declaration).
30640b57cec5SDimitry Andric     TemplateIdAnnotation *Annot =
30650b57cec5SDimitry Andric         static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
30660b57cec5SDimitry Andric     if (Annot->Kind == TNK_Type_template)
30670b57cec5SDimitry Andric       MightBeDeclarator = false;
30680b57cec5SDimitry Andric   } else if (AfterScope.is(tok::identifier)) {
30690b57cec5SDimitry Andric     const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
30700b57cec5SDimitry Andric 
30710b57cec5SDimitry Andric     // These tokens cannot come after the declarator-id in a
30720b57cec5SDimitry Andric     // simple-declaration, and are likely to come after a type-specifier.
30730b57cec5SDimitry Andric     if (Next.isOneOf(tok::star, tok::amp, tok::ampamp, tok::identifier,
30740b57cec5SDimitry Andric                      tok::annot_cxxscope, tok::coloncolon)) {
30750b57cec5SDimitry Andric       // Missing a semicolon.
30760b57cec5SDimitry Andric       MightBeDeclarator = false;
30770b57cec5SDimitry Andric     } else if (HasScope) {
30780b57cec5SDimitry Andric       // If the declarator-id has a scope specifier, it must redeclare a
30790b57cec5SDimitry Andric       // previously-declared entity. If that's a type (and this is not a
30800b57cec5SDimitry Andric       // typedef), that's an error.
30810b57cec5SDimitry Andric       CXXScopeSpec SS;
30820b57cec5SDimitry Andric       Actions.RestoreNestedNameSpecifierAnnotation(
30830b57cec5SDimitry Andric           Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
30840b57cec5SDimitry Andric       IdentifierInfo *Name = AfterScope.getIdentifierInfo();
30850b57cec5SDimitry Andric       Sema::NameClassification Classification = Actions.ClassifyName(
30860b57cec5SDimitry Andric           getCurScope(), SS, Name, AfterScope.getLocation(), Next,
3087a7dea167SDimitry Andric           /*CCC=*/nullptr);
30880b57cec5SDimitry Andric       switch (Classification.getKind()) {
30890b57cec5SDimitry Andric       case Sema::NC_Error:
30900b57cec5SDimitry Andric         SkipMalformedDecl();
30910b57cec5SDimitry Andric         return true;
30920b57cec5SDimitry Andric 
30930b57cec5SDimitry Andric       case Sema::NC_Keyword:
3094a7dea167SDimitry Andric         llvm_unreachable("typo correction is not possible here");
30950b57cec5SDimitry Andric 
30960b57cec5SDimitry Andric       case Sema::NC_Type:
30970b57cec5SDimitry Andric       case Sema::NC_TypeTemplate:
3098a7dea167SDimitry Andric       case Sema::NC_UndeclaredNonType:
3099a7dea167SDimitry Andric       case Sema::NC_UndeclaredTemplate:
31000b57cec5SDimitry Andric         // Not a previously-declared non-type entity.
31010b57cec5SDimitry Andric         MightBeDeclarator = false;
31020b57cec5SDimitry Andric         break;
31030b57cec5SDimitry Andric 
31040b57cec5SDimitry Andric       case Sema::NC_Unknown:
3105a7dea167SDimitry Andric       case Sema::NC_NonType:
3106a7dea167SDimitry Andric       case Sema::NC_DependentNonType:
3107e8d8bef9SDimitry Andric       case Sema::NC_OverloadSet:
31080b57cec5SDimitry Andric       case Sema::NC_VarTemplate:
31090b57cec5SDimitry Andric       case Sema::NC_FunctionTemplate:
311055e4f9d5SDimitry Andric       case Sema::NC_Concept:
31110b57cec5SDimitry Andric         // Might be a redeclaration of a prior entity.
31120b57cec5SDimitry Andric         break;
31130b57cec5SDimitry Andric       }
31140b57cec5SDimitry Andric     }
31150b57cec5SDimitry Andric   }
31160b57cec5SDimitry Andric 
31170b57cec5SDimitry Andric   if (MightBeDeclarator)
31180b57cec5SDimitry Andric     return false;
31190b57cec5SDimitry Andric 
31200b57cec5SDimitry Andric   const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
31210b57cec5SDimitry Andric   Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getEndLoc()),
31220b57cec5SDimitry Andric        diag::err_expected_after)
31230b57cec5SDimitry Andric       << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi;
31240b57cec5SDimitry Andric 
31250b57cec5SDimitry Andric   // Try to recover from the typo, by dropping the tag definition and parsing
31260b57cec5SDimitry Andric   // the problematic tokens as a type.
31270b57cec5SDimitry Andric   //
31280b57cec5SDimitry Andric   // FIXME: Split the DeclSpec into pieces for the standalone
31290b57cec5SDimitry Andric   // declaration and pieces for the following declaration, instead
31300b57cec5SDimitry Andric   // of assuming that all the other pieces attach to new declaration,
31310b57cec5SDimitry Andric   // and call ParsedFreeStandingDeclSpec as appropriate.
31320b57cec5SDimitry Andric   DS.ClearTypeSpecType();
31330b57cec5SDimitry Andric   ParsedTemplateInfo NotATemplate;
31340b57cec5SDimitry Andric   ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
31350b57cec5SDimitry Andric   return false;
31360b57cec5SDimitry Andric }
31370b57cec5SDimitry Andric 
31380b57cec5SDimitry Andric // Choose the apprpriate diagnostic error for why fixed point types are
31390b57cec5SDimitry Andric // disabled, set the previous specifier, and mark as invalid.
31400b57cec5SDimitry Andric static void SetupFixedPointError(const LangOptions &LangOpts,
31410b57cec5SDimitry Andric                                  const char *&PrevSpec, unsigned &DiagID,
31420b57cec5SDimitry Andric                                  bool &isInvalid) {
31430b57cec5SDimitry Andric   assert(!LangOpts.FixedPoint);
31440b57cec5SDimitry Andric   DiagID = diag::err_fixed_point_not_enabled;
31450b57cec5SDimitry Andric   PrevSpec = "";  // Not used by diagnostic
31460b57cec5SDimitry Andric   isInvalid = true;
31470b57cec5SDimitry Andric }
31480b57cec5SDimitry Andric 
31490b57cec5SDimitry Andric /// ParseDeclarationSpecifiers
31500b57cec5SDimitry Andric ///       declaration-specifiers: [C99 6.7]
31510b57cec5SDimitry Andric ///         storage-class-specifier declaration-specifiers[opt]
31520b57cec5SDimitry Andric ///         type-specifier declaration-specifiers[opt]
31530b57cec5SDimitry Andric /// [C99]   function-specifier declaration-specifiers[opt]
31540b57cec5SDimitry Andric /// [C11]   alignment-specifier declaration-specifiers[opt]
31550b57cec5SDimitry Andric /// [GNU]   attributes declaration-specifiers[opt]
31560b57cec5SDimitry Andric /// [Clang] '__module_private__' declaration-specifiers[opt]
31570b57cec5SDimitry Andric /// [ObjC1] '__kindof' declaration-specifiers[opt]
31580b57cec5SDimitry Andric ///
31590b57cec5SDimitry Andric ///       storage-class-specifier: [C99 6.7.1]
31600b57cec5SDimitry Andric ///         'typedef'
31610b57cec5SDimitry Andric ///         'extern'
31620b57cec5SDimitry Andric ///         'static'
31630b57cec5SDimitry Andric ///         'auto'
31640b57cec5SDimitry Andric ///         'register'
31650b57cec5SDimitry Andric /// [C++]   'mutable'
31660b57cec5SDimitry Andric /// [C++11] 'thread_local'
31670b57cec5SDimitry Andric /// [C11]   '_Thread_local'
31680b57cec5SDimitry Andric /// [GNU]   '__thread'
31690b57cec5SDimitry Andric ///       function-specifier: [C99 6.7.4]
31700b57cec5SDimitry Andric /// [C99]   'inline'
31710b57cec5SDimitry Andric /// [C++]   'virtual'
31720b57cec5SDimitry Andric /// [C++]   'explicit'
31730b57cec5SDimitry Andric /// [OpenCL] '__kernel'
31740b57cec5SDimitry Andric ///       'friend': [C++ dcl.friend]
31750b57cec5SDimitry Andric ///       'constexpr': [C++0x dcl.constexpr]
3176*bdd1243dSDimitry Andric void Parser::ParseDeclarationSpecifiers(
3177*bdd1243dSDimitry Andric     DeclSpec &DS, const ParsedTemplateInfo &TemplateInfo, AccessSpecifier AS,
3178*bdd1243dSDimitry Andric     DeclSpecContext DSContext, LateParsedAttrList *LateAttrs,
3179*bdd1243dSDimitry Andric     ImplicitTypenameContext AllowImplicitTypename) {
31800b57cec5SDimitry Andric   if (DS.getSourceRange().isInvalid()) {
31810b57cec5SDimitry Andric     // Start the range at the current token but make the end of the range
31820b57cec5SDimitry Andric     // invalid.  This will make the entire range invalid unless we successfully
31830b57cec5SDimitry Andric     // consume a token.
31840b57cec5SDimitry Andric     DS.SetRangeStart(Tok.getLocation());
31850b57cec5SDimitry Andric     DS.SetRangeEnd(SourceLocation());
31860b57cec5SDimitry Andric   }
31870b57cec5SDimitry Andric 
3188*bdd1243dSDimitry Andric   // If we are in a operator context, convert it back into a type specifier
3189*bdd1243dSDimitry Andric   // context for better error handling later on.
3190*bdd1243dSDimitry Andric   if (DSContext == DeclSpecContext::DSC_conv_operator) {
3191*bdd1243dSDimitry Andric     // No implicit typename here.
3192*bdd1243dSDimitry Andric     AllowImplicitTypename = ImplicitTypenameContext::No;
3193*bdd1243dSDimitry Andric     DSContext = DeclSpecContext::DSC_type_specifier;
3194*bdd1243dSDimitry Andric   }
3195*bdd1243dSDimitry Andric 
31960b57cec5SDimitry Andric   bool EnteringContext = (DSContext == DeclSpecContext::DSC_class ||
31970b57cec5SDimitry Andric                           DSContext == DeclSpecContext::DSC_top_level);
31980b57cec5SDimitry Andric   bool AttrsLastTime = false;
319981ad6265SDimitry Andric   ParsedAttributes attrs(AttrFactory);
32000b57cec5SDimitry Andric   // We use Sema's policy to get bool macros right.
32010b57cec5SDimitry Andric   PrintingPolicy Policy = Actions.getPrintingPolicy();
320204eeddc0SDimitry Andric   while (true) {
32030b57cec5SDimitry Andric     bool isInvalid = false;
32040b57cec5SDimitry Andric     bool isStorageClass = false;
32050b57cec5SDimitry Andric     const char *PrevSpec = nullptr;
32060b57cec5SDimitry Andric     unsigned DiagID = 0;
32070b57cec5SDimitry Andric 
32080b57cec5SDimitry Andric     // This value needs to be set to the location of the last token if the last
32090b57cec5SDimitry Andric     // token of the specifier is already consumed.
32100b57cec5SDimitry Andric     SourceLocation ConsumedEnd;
32110b57cec5SDimitry Andric 
32120b57cec5SDimitry Andric     // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
32130b57cec5SDimitry Andric     // implementation for VS2013 uses _Atomic as an identifier for one of the
32140b57cec5SDimitry Andric     // classes in <atomic>.
32150b57cec5SDimitry Andric     //
32160b57cec5SDimitry Andric     // A typedef declaration containing _Atomic<...> is among the places where
32170b57cec5SDimitry Andric     // the class is used.  If we are currently parsing such a declaration, treat
32180b57cec5SDimitry Andric     // the token as an identifier.
32190b57cec5SDimitry Andric     if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) &&
32200b57cec5SDimitry Andric         DS.getStorageClassSpec() == clang::DeclSpec::SCS_typedef &&
32210b57cec5SDimitry Andric         !DS.hasTypeSpecifier() && GetLookAheadToken(1).is(tok::less))
32220b57cec5SDimitry Andric       Tok.setKind(tok::identifier);
32230b57cec5SDimitry Andric 
32240b57cec5SDimitry Andric     SourceLocation Loc = Tok.getLocation();
32250b57cec5SDimitry Andric 
3226fe6060f1SDimitry Andric     // Helper for image types in OpenCL.
3227fe6060f1SDimitry Andric     auto handleOpenCLImageKW = [&] (StringRef Ext, TypeSpecifierType ImageTypeSpec) {
3228fe6060f1SDimitry Andric       // Check if the image type is supported and otherwise turn the keyword into an identifier
3229fe6060f1SDimitry Andric       // because image types from extensions are not reserved identifiers.
3230fe6060f1SDimitry Andric       if (!StringRef(Ext).empty() && !getActions().getOpenCLOptions().isSupported(Ext, getLangOpts())) {
3231fe6060f1SDimitry Andric         Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
3232fe6060f1SDimitry Andric         Tok.setKind(tok::identifier);
3233fe6060f1SDimitry Andric         return false;
3234fe6060f1SDimitry Andric       }
3235fe6060f1SDimitry Andric       isInvalid = DS.SetTypeSpecType(ImageTypeSpec, Loc, PrevSpec, DiagID, Policy);
3236fe6060f1SDimitry Andric       return true;
3237fe6060f1SDimitry Andric     };
3238fe6060f1SDimitry Andric 
3239349cc55cSDimitry Andric     // Turn off usual access checking for template specializations and
3240349cc55cSDimitry Andric     // instantiations.
3241349cc55cSDimitry Andric     bool IsTemplateSpecOrInst =
3242349cc55cSDimitry Andric         (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3243349cc55cSDimitry Andric          TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3244349cc55cSDimitry Andric 
32450b57cec5SDimitry Andric     switch (Tok.getKind()) {
32460b57cec5SDimitry Andric     default:
32470b57cec5SDimitry Andric     DoneWithDeclSpec:
32480b57cec5SDimitry Andric       if (!AttrsLastTime)
32490b57cec5SDimitry Andric         ProhibitAttributes(attrs);
32500b57cec5SDimitry Andric       else {
325181ad6265SDimitry Andric         // Reject C++11 / C2x attributes that aren't type attributes.
325281ad6265SDimitry Andric         for (const ParsedAttr &PA : attrs) {
325381ad6265SDimitry Andric           if (!PA.isCXX11Attribute() && !PA.isC2xAttribute())
325481ad6265SDimitry Andric             continue;
325581ad6265SDimitry Andric           if (PA.getKind() == ParsedAttr::UnknownAttribute)
325681ad6265SDimitry Andric             // We will warn about the unknown attribute elsewhere (in
325781ad6265SDimitry Andric             // SemaDeclAttr.cpp)
325881ad6265SDimitry Andric             continue;
325981ad6265SDimitry Andric           // GCC ignores this attribute when placed on the DeclSpec in [[]]
326081ad6265SDimitry Andric           // syntax, so we do the same.
326181ad6265SDimitry Andric           if (PA.getKind() == ParsedAttr::AT_VectorSize) {
326281ad6265SDimitry Andric             Diag(PA.getLoc(), diag::warn_attribute_ignored) << PA;
326381ad6265SDimitry Andric             PA.setInvalid();
326481ad6265SDimitry Andric             continue;
326581ad6265SDimitry Andric           }
326681ad6265SDimitry Andric           // We reject AT_LifetimeBound and AT_AnyX86NoCfCheck, even though they
326781ad6265SDimitry Andric           // are type attributes, because we historically haven't allowed these
326881ad6265SDimitry Andric           // to be used as type attributes in C++11 / C2x syntax.
326981ad6265SDimitry Andric           if (PA.isTypeAttr() && PA.getKind() != ParsedAttr::AT_LifetimeBound &&
327081ad6265SDimitry Andric               PA.getKind() != ParsedAttr::AT_AnyX86NoCfCheck)
327181ad6265SDimitry Andric             continue;
327281ad6265SDimitry Andric           Diag(PA.getLoc(), diag::err_attribute_not_type_attr) << PA;
327381ad6265SDimitry Andric           PA.setInvalid();
327481ad6265SDimitry Andric         }
32750b57cec5SDimitry Andric 
32760b57cec5SDimitry Andric         DS.takeAttributesFrom(attrs);
32770b57cec5SDimitry Andric       }
32780b57cec5SDimitry Andric 
32790b57cec5SDimitry Andric       // If this is not a declaration specifier token, we're done reading decl
32800b57cec5SDimitry Andric       // specifiers.  First verify that DeclSpec's are consistent.
32810b57cec5SDimitry Andric       DS.Finish(Actions, Policy);
32820b57cec5SDimitry Andric       return;
32830b57cec5SDimitry Andric 
32840b57cec5SDimitry Andric     case tok::l_square:
32850b57cec5SDimitry Andric     case tok::kw_alignas:
32860b57cec5SDimitry Andric       if (!standardAttributesAllowed() || !isCXX11AttributeSpecifier())
32870b57cec5SDimitry Andric         goto DoneWithDeclSpec;
32880b57cec5SDimitry Andric 
32890b57cec5SDimitry Andric       ProhibitAttributes(attrs);
32900b57cec5SDimitry Andric       // FIXME: It would be good to recover by accepting the attributes,
32910b57cec5SDimitry Andric       //        but attempting to do that now would cause serious
32920b57cec5SDimitry Andric       //        madness in terms of diagnostics.
32930b57cec5SDimitry Andric       attrs.clear();
32940b57cec5SDimitry Andric       attrs.Range = SourceRange();
32950b57cec5SDimitry Andric 
32960b57cec5SDimitry Andric       ParseCXX11Attributes(attrs);
32970b57cec5SDimitry Andric       AttrsLastTime = true;
32980b57cec5SDimitry Andric       continue;
32990b57cec5SDimitry Andric 
33000b57cec5SDimitry Andric     case tok::code_completion: {
33010b57cec5SDimitry Andric       Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
33020b57cec5SDimitry Andric       if (DS.hasTypeSpecifier()) {
33030b57cec5SDimitry Andric         bool AllowNonIdentifiers
33040b57cec5SDimitry Andric           = (getCurScope()->getFlags() & (Scope::ControlScope |
33050b57cec5SDimitry Andric                                           Scope::BlockScope |
33060b57cec5SDimitry Andric                                           Scope::TemplateParamScope |
33070b57cec5SDimitry Andric                                           Scope::FunctionPrototypeScope |
33080b57cec5SDimitry Andric                                           Scope::AtCatchScope)) == 0;
33090b57cec5SDimitry Andric         bool AllowNestedNameSpecifiers
33100b57cec5SDimitry Andric           = DSContext == DeclSpecContext::DSC_top_level ||
33110b57cec5SDimitry Andric             (DSContext == DeclSpecContext::DSC_class && DS.isFriendSpecified());
33120b57cec5SDimitry Andric 
3313fe6060f1SDimitry Andric         cutOffParsing();
33140b57cec5SDimitry Andric         Actions.CodeCompleteDeclSpec(getCurScope(), DS,
33150b57cec5SDimitry Andric                                      AllowNonIdentifiers,
33160b57cec5SDimitry Andric                                      AllowNestedNameSpecifiers);
3317fe6060f1SDimitry Andric         return;
33180b57cec5SDimitry Andric       }
33190b57cec5SDimitry Andric 
3320*bdd1243dSDimitry Andric       // Class context can appear inside a function/block, so prioritise that.
3321*bdd1243dSDimitry Andric       if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
33220b57cec5SDimitry Andric         CCC = DSContext == DeclSpecContext::DSC_class ? Sema::PCC_MemberTemplate
33230b57cec5SDimitry Andric                                                       : Sema::PCC_Template;
33240b57cec5SDimitry Andric       else if (DSContext == DeclSpecContext::DSC_class)
33250b57cec5SDimitry Andric         CCC = Sema::PCC_Class;
3326*bdd1243dSDimitry Andric       else if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
3327*bdd1243dSDimitry Andric         CCC = Sema::PCC_LocalDeclarationSpecifiers;
33280b57cec5SDimitry Andric       else if (CurParsedObjCImpl)
33290b57cec5SDimitry Andric         CCC = Sema::PCC_ObjCImplementation;
33300b57cec5SDimitry Andric 
3331fe6060f1SDimitry Andric       cutOffParsing();
33320b57cec5SDimitry Andric       Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
3333fe6060f1SDimitry Andric       return;
33340b57cec5SDimitry Andric     }
33350b57cec5SDimitry Andric 
33360b57cec5SDimitry Andric     case tok::coloncolon: // ::foo::bar
33370b57cec5SDimitry Andric       // C++ scope specifier.  Annotate and loop, or bail out on error.
33380b57cec5SDimitry Andric       if (TryAnnotateCXXScopeToken(EnteringContext)) {
33390b57cec5SDimitry Andric         if (!DS.hasTypeSpecifier())
33400b57cec5SDimitry Andric           DS.SetTypeSpecError();
33410b57cec5SDimitry Andric         goto DoneWithDeclSpec;
33420b57cec5SDimitry Andric       }
33430b57cec5SDimitry Andric       if (Tok.is(tok::coloncolon)) // ::new or ::delete
33440b57cec5SDimitry Andric         goto DoneWithDeclSpec;
33450b57cec5SDimitry Andric       continue;
33460b57cec5SDimitry Andric 
33470b57cec5SDimitry Andric     case tok::annot_cxxscope: {
33480b57cec5SDimitry Andric       if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
33490b57cec5SDimitry Andric         goto DoneWithDeclSpec;
33500b57cec5SDimitry Andric 
33510b57cec5SDimitry Andric       CXXScopeSpec SS;
33520b57cec5SDimitry Andric       Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
33530b57cec5SDimitry Andric                                                    Tok.getAnnotationRange(),
33540b57cec5SDimitry Andric                                                    SS);
33550b57cec5SDimitry Andric 
33560b57cec5SDimitry Andric       // We are looking for a qualified typename.
33570b57cec5SDimitry Andric       Token Next = NextToken();
33585ffd83dbSDimitry Andric 
33595ffd83dbSDimitry Andric       TemplateIdAnnotation *TemplateId = Next.is(tok::annot_template_id)
33605ffd83dbSDimitry Andric                                              ? takeTemplateIdAnnotation(Next)
33615ffd83dbSDimitry Andric                                              : nullptr;
33625ffd83dbSDimitry Andric       if (TemplateId && TemplateId->hasInvalidName()) {
33635ffd83dbSDimitry Andric         // We found something like 'T::U<Args> x', but U is not a template.
33645ffd83dbSDimitry Andric         // Assume it was supposed to be a type.
33655ffd83dbSDimitry Andric         DS.SetTypeSpecError();
33665ffd83dbSDimitry Andric         ConsumeAnnotationToken();
33675ffd83dbSDimitry Andric         break;
33685ffd83dbSDimitry Andric       }
33695ffd83dbSDimitry Andric 
33705ffd83dbSDimitry Andric       if (TemplateId && TemplateId->Kind == TNK_Type_template) {
33710b57cec5SDimitry Andric         // We have a qualified template-id, e.g., N::A<int>
33720b57cec5SDimitry Andric 
33730b57cec5SDimitry Andric         // If this would be a valid constructor declaration with template
33740b57cec5SDimitry Andric         // arguments, we will reject the attempt to form an invalid type-id
33750b57cec5SDimitry Andric         // referring to the injected-class-name when we annotate the token,
33760b57cec5SDimitry Andric         // per C++ [class.qual]p2.
33770b57cec5SDimitry Andric         //
33780b57cec5SDimitry Andric         // To improve diagnostics for this case, parse the declaration as a
33790b57cec5SDimitry Andric         // constructor (and reject the extra template arguments later).
33800b57cec5SDimitry Andric         if ((DSContext == DeclSpecContext::DSC_top_level ||
33810b57cec5SDimitry Andric              DSContext == DeclSpecContext::DSC_class) &&
33820b57cec5SDimitry Andric             TemplateId->Name &&
33830b57cec5SDimitry Andric             Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS) &&
3384*bdd1243dSDimitry Andric             isConstructorDeclarator(/*Unqualified=*/false,
3385*bdd1243dSDimitry Andric                                     /*DeductionGuide=*/false,
3386*bdd1243dSDimitry Andric                                     DS.isFriendSpecified())) {
33870b57cec5SDimitry Andric           // The user meant this to be an out-of-line constructor
33880b57cec5SDimitry Andric           // definition, but template arguments are not allowed
33890b57cec5SDimitry Andric           // there.  Just allow this as a constructor; we'll
33900b57cec5SDimitry Andric           // complain about it later.
33910b57cec5SDimitry Andric           goto DoneWithDeclSpec;
33920b57cec5SDimitry Andric         }
33930b57cec5SDimitry Andric 
33940b57cec5SDimitry Andric         DS.getTypeSpecScope() = SS;
33950b57cec5SDimitry Andric         ConsumeAnnotationToken(); // The C++ scope.
33960b57cec5SDimitry Andric         assert(Tok.is(tok::annot_template_id) &&
33970b57cec5SDimitry Andric                "ParseOptionalCXXScopeSpecifier not working");
3398*bdd1243dSDimitry Andric         AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename);
339955e4f9d5SDimitry Andric         continue;
340055e4f9d5SDimitry Andric       }
340155e4f9d5SDimitry Andric 
34025ffd83dbSDimitry Andric       if (TemplateId && TemplateId->Kind == TNK_Concept_template &&
340355e4f9d5SDimitry Andric           GetLookAheadToken(2).isOneOf(tok::kw_auto, tok::kw_decltype)) {
340455e4f9d5SDimitry Andric         DS.getTypeSpecScope() = SS;
340555e4f9d5SDimitry Andric         // This is a qualified placeholder-specifier, e.g., ::C<int> auto ...
340655e4f9d5SDimitry Andric         // Consume the scope annotation and continue to consume the template-id
340755e4f9d5SDimitry Andric         // as a placeholder-specifier.
340855e4f9d5SDimitry Andric         ConsumeAnnotationToken();
34090b57cec5SDimitry Andric         continue;
34100b57cec5SDimitry Andric       }
34110b57cec5SDimitry Andric 
34120b57cec5SDimitry Andric       if (Next.is(tok::annot_typename)) {
34130b57cec5SDimitry Andric         DS.getTypeSpecScope() = SS;
34140b57cec5SDimitry Andric         ConsumeAnnotationToken(); // The C++ scope.
34155ffd83dbSDimitry Andric         TypeResult T = getTypeAnnotation(Tok);
34160b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
34170b57cec5SDimitry Andric                                        Tok.getAnnotationEndLoc(),
34180b57cec5SDimitry Andric                                        PrevSpec, DiagID, T, Policy);
34190b57cec5SDimitry Andric         if (isInvalid)
34200b57cec5SDimitry Andric           break;
34210b57cec5SDimitry Andric         DS.SetRangeEnd(Tok.getAnnotationEndLoc());
34220b57cec5SDimitry Andric         ConsumeAnnotationToken(); // The typename
34230b57cec5SDimitry Andric       }
34240b57cec5SDimitry Andric 
3425*bdd1243dSDimitry Andric       if (AllowImplicitTypename == ImplicitTypenameContext::Yes &&
3426*bdd1243dSDimitry Andric           Next.is(tok::annot_template_id) &&
3427*bdd1243dSDimitry Andric           static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
3428*bdd1243dSDimitry Andric                   ->Kind == TNK_Dependent_template_name) {
3429*bdd1243dSDimitry Andric         DS.getTypeSpecScope() = SS;
3430*bdd1243dSDimitry Andric         ConsumeAnnotationToken(); // The C++ scope.
3431*bdd1243dSDimitry Andric         AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename);
3432*bdd1243dSDimitry Andric         continue;
3433*bdd1243dSDimitry Andric       }
3434*bdd1243dSDimitry Andric 
34350b57cec5SDimitry Andric       if (Next.isNot(tok::identifier))
34360b57cec5SDimitry Andric         goto DoneWithDeclSpec;
34370b57cec5SDimitry Andric 
34380b57cec5SDimitry Andric       // Check whether this is a constructor declaration. If we're in a
34390b57cec5SDimitry Andric       // context where the identifier could be a class name, and it has the
34400b57cec5SDimitry Andric       // shape of a constructor declaration, process it as one.
34410b57cec5SDimitry Andric       if ((DSContext == DeclSpecContext::DSC_top_level ||
34420b57cec5SDimitry Andric            DSContext == DeclSpecContext::DSC_class) &&
34430b57cec5SDimitry Andric           Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
34440b57cec5SDimitry Andric                                      &SS) &&
3445*bdd1243dSDimitry Andric           isConstructorDeclarator(/*Unqualified=*/false,
3446*bdd1243dSDimitry Andric                                   /*DeductionGuide=*/false,
3447*bdd1243dSDimitry Andric                                   DS.isFriendSpecified()))
34480b57cec5SDimitry Andric         goto DoneWithDeclSpec;
34490b57cec5SDimitry Andric 
3450349cc55cSDimitry Andric       // C++20 [temp.spec] 13.9/6.
3451349cc55cSDimitry Andric       // This disables the access checking rules for function template explicit
3452349cc55cSDimitry Andric       // instantiation and explicit specialization:
3453349cc55cSDimitry Andric       // - `return type`.
3454349cc55cSDimitry Andric       SuppressAccessChecks SAC(*this, IsTemplateSpecOrInst);
3455349cc55cSDimitry Andric 
3456*bdd1243dSDimitry Andric       ParsedType TypeRep = Actions.getTypeName(
3457*bdd1243dSDimitry Andric           *Next.getIdentifierInfo(), Next.getLocation(), getCurScope(), &SS,
3458*bdd1243dSDimitry Andric           false, false, nullptr,
34590b57cec5SDimitry Andric           /*IsCtorOrDtorName=*/false,
34600b57cec5SDimitry Andric           /*WantNontrivialTypeSourceInfo=*/true,
3461*bdd1243dSDimitry Andric           isClassTemplateDeductionContext(DSContext), AllowImplicitTypename);
34620b57cec5SDimitry Andric 
3463349cc55cSDimitry Andric       if (IsTemplateSpecOrInst)
3464349cc55cSDimitry Andric         SAC.done();
3465349cc55cSDimitry Andric 
34660b57cec5SDimitry Andric       // If the referenced identifier is not a type, then this declspec is
34670b57cec5SDimitry Andric       // erroneous: We already checked about that it has no type specifier, and
34680b57cec5SDimitry Andric       // C++ doesn't have implicit int.  Diagnose it as a typo w.r.t. to the
34690b57cec5SDimitry Andric       // typename.
34700b57cec5SDimitry Andric       if (!TypeRep) {
347155e4f9d5SDimitry Andric         if (TryAnnotateTypeConstraint())
347255e4f9d5SDimitry Andric           goto DoneWithDeclSpec;
34735ffd83dbSDimitry Andric         if (Tok.isNot(tok::annot_cxxscope) ||
34745ffd83dbSDimitry Andric             NextToken().isNot(tok::identifier))
3475aec4c088SDimitry Andric           continue;
34760b57cec5SDimitry Andric         // Eat the scope spec so the identifier is current.
34770b57cec5SDimitry Andric         ConsumeAnnotationToken();
347881ad6265SDimitry Andric         ParsedAttributes Attrs(AttrFactory);
34790b57cec5SDimitry Andric         if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
34800b57cec5SDimitry Andric           if (!Attrs.empty()) {
34810b57cec5SDimitry Andric             AttrsLastTime = true;
34820b57cec5SDimitry Andric             attrs.takeAllFrom(Attrs);
34830b57cec5SDimitry Andric           }
34840b57cec5SDimitry Andric           continue;
34850b57cec5SDimitry Andric         }
34860b57cec5SDimitry Andric         goto DoneWithDeclSpec;
34870b57cec5SDimitry Andric       }
34880b57cec5SDimitry Andric 
34890b57cec5SDimitry Andric       DS.getTypeSpecScope() = SS;
34900b57cec5SDimitry Andric       ConsumeAnnotationToken(); // The C++ scope.
34910b57cec5SDimitry Andric 
34920b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
34930b57cec5SDimitry Andric                                      DiagID, TypeRep, Policy);
34940b57cec5SDimitry Andric       if (isInvalid)
34950b57cec5SDimitry Andric         break;
34960b57cec5SDimitry Andric 
34970b57cec5SDimitry Andric       DS.SetRangeEnd(Tok.getLocation());
34980b57cec5SDimitry Andric       ConsumeToken(); // The typename.
34990b57cec5SDimitry Andric 
35000b57cec5SDimitry Andric       continue;
35010b57cec5SDimitry Andric     }
35020b57cec5SDimitry Andric 
35030b57cec5SDimitry Andric     case tok::annot_typename: {
35040b57cec5SDimitry Andric       // If we've previously seen a tag definition, we were almost surely
35050b57cec5SDimitry Andric       // missing a semicolon after it.
35060b57cec5SDimitry Andric       if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
35070b57cec5SDimitry Andric         goto DoneWithDeclSpec;
35080b57cec5SDimitry Andric 
35095ffd83dbSDimitry Andric       TypeResult T = getTypeAnnotation(Tok);
35100b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
35110b57cec5SDimitry Andric                                      DiagID, T, Policy);
35120b57cec5SDimitry Andric       if (isInvalid)
35130b57cec5SDimitry Andric         break;
35140b57cec5SDimitry Andric 
35150b57cec5SDimitry Andric       DS.SetRangeEnd(Tok.getAnnotationEndLoc());
35160b57cec5SDimitry Andric       ConsumeAnnotationToken(); // The typename
35170b57cec5SDimitry Andric 
35180b57cec5SDimitry Andric       continue;
35190b57cec5SDimitry Andric     }
35200b57cec5SDimitry Andric 
35210b57cec5SDimitry Andric     case tok::kw___is_signed:
35220b57cec5SDimitry Andric       // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
35230b57cec5SDimitry Andric       // typically treats it as a trait. If we see __is_signed as it appears
35240b57cec5SDimitry Andric       // in libstdc++, e.g.,
35250b57cec5SDimitry Andric       //
35260b57cec5SDimitry Andric       //   static const bool __is_signed;
35270b57cec5SDimitry Andric       //
35280b57cec5SDimitry Andric       // then treat __is_signed as an identifier rather than as a keyword.
35290b57cec5SDimitry Andric       if (DS.getTypeSpecType() == TST_bool &&
35300b57cec5SDimitry Andric           DS.getTypeQualifiers() == DeclSpec::TQ_const &&
35310b57cec5SDimitry Andric           DS.getStorageClassSpec() == DeclSpec::SCS_static)
35320b57cec5SDimitry Andric         TryKeywordIdentFallback(true);
35330b57cec5SDimitry Andric 
35340b57cec5SDimitry Andric       // We're done with the declaration-specifiers.
35350b57cec5SDimitry Andric       goto DoneWithDeclSpec;
35360b57cec5SDimitry Andric 
35370b57cec5SDimitry Andric       // typedef-name
35380b57cec5SDimitry Andric     case tok::kw___super:
35390b57cec5SDimitry Andric     case tok::kw_decltype:
3540*bdd1243dSDimitry Andric     case tok::identifier:
3541*bdd1243dSDimitry Andric     ParseIdentifier: {
35420b57cec5SDimitry Andric       // This identifier can only be a typedef name if we haven't already seen
35430b57cec5SDimitry Andric       // a type-specifier.  Without this check we misparse:
35440b57cec5SDimitry Andric       //  typedef int X; struct Y { short X; };  as 'short int'.
35450b57cec5SDimitry Andric       if (DS.hasTypeSpecifier())
35460b57cec5SDimitry Andric         goto DoneWithDeclSpec;
35470b57cec5SDimitry Andric 
35480b57cec5SDimitry Andric       // If the token is an identifier named "__declspec" and Microsoft
35490b57cec5SDimitry Andric       // extensions are not enabled, it is likely that there will be cascading
35500b57cec5SDimitry Andric       // parse errors if this really is a __declspec attribute. Attempt to
35510b57cec5SDimitry Andric       // recognize that scenario and recover gracefully.
35520b57cec5SDimitry Andric       if (!getLangOpts().DeclSpecKeyword && Tok.is(tok::identifier) &&
35530b57cec5SDimitry Andric           Tok.getIdentifierInfo()->getName().equals("__declspec")) {
35540b57cec5SDimitry Andric         Diag(Loc, diag::err_ms_attributes_not_enabled);
35550b57cec5SDimitry Andric 
35560b57cec5SDimitry Andric         // The next token should be an open paren. If it is, eat the entire
35570b57cec5SDimitry Andric         // attribute declaration and continue.
35580b57cec5SDimitry Andric         if (NextToken().is(tok::l_paren)) {
35590b57cec5SDimitry Andric           // Consume the __declspec identifier.
35600b57cec5SDimitry Andric           ConsumeToken();
35610b57cec5SDimitry Andric 
35620b57cec5SDimitry Andric           // Eat the parens and everything between them.
35630b57cec5SDimitry Andric           BalancedDelimiterTracker T(*this, tok::l_paren);
35640b57cec5SDimitry Andric           if (T.consumeOpen()) {
35650b57cec5SDimitry Andric             assert(false && "Not a left paren?");
35660b57cec5SDimitry Andric             return;
35670b57cec5SDimitry Andric           }
35680b57cec5SDimitry Andric           T.skipToEnd();
35690b57cec5SDimitry Andric           continue;
35700b57cec5SDimitry Andric         }
35710b57cec5SDimitry Andric       }
35720b57cec5SDimitry Andric 
35730b57cec5SDimitry Andric       // In C++, check to see if this is a scope specifier like foo::bar::, if
35740b57cec5SDimitry Andric       // so handle it as such.  This is important for ctor parsing.
35750b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus) {
3576349cc55cSDimitry Andric         // C++20 [temp.spec] 13.9/6.
3577349cc55cSDimitry Andric         // This disables the access checking rules for function template
3578349cc55cSDimitry Andric         // explicit instantiation and explicit specialization:
3579349cc55cSDimitry Andric         // - `return type`.
3580349cc55cSDimitry Andric         SuppressAccessChecks SAC(*this, IsTemplateSpecOrInst);
3581349cc55cSDimitry Andric 
3582349cc55cSDimitry Andric         const bool Success = TryAnnotateCXXScopeToken(EnteringContext);
3583349cc55cSDimitry Andric 
3584349cc55cSDimitry Andric         if (IsTemplateSpecOrInst)
3585349cc55cSDimitry Andric           SAC.done();
3586349cc55cSDimitry Andric 
3587349cc55cSDimitry Andric         if (Success) {
3588349cc55cSDimitry Andric           if (IsTemplateSpecOrInst)
3589349cc55cSDimitry Andric             SAC.redelay();
35900b57cec5SDimitry Andric           DS.SetTypeSpecError();
35910b57cec5SDimitry Andric           goto DoneWithDeclSpec;
35920b57cec5SDimitry Andric         }
3593349cc55cSDimitry Andric 
35940b57cec5SDimitry Andric         if (!Tok.is(tok::identifier))
35950b57cec5SDimitry Andric           continue;
35960b57cec5SDimitry Andric       }
35970b57cec5SDimitry Andric 
35980b57cec5SDimitry Andric       // Check for need to substitute AltiVec keyword tokens.
35990b57cec5SDimitry Andric       if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
36000b57cec5SDimitry Andric         break;
36010b57cec5SDimitry Andric 
36020b57cec5SDimitry Andric       // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
36030b57cec5SDimitry Andric       //                allow the use of a typedef name as a type specifier.
36040b57cec5SDimitry Andric       if (DS.isTypeAltiVecVector())
36050b57cec5SDimitry Andric         goto DoneWithDeclSpec;
36060b57cec5SDimitry Andric 
36070b57cec5SDimitry Andric       if (DSContext == DeclSpecContext::DSC_objc_method_result &&
36080b57cec5SDimitry Andric           isObjCInstancetype()) {
36090b57cec5SDimitry Andric         ParsedType TypeRep = Actions.ActOnObjCInstanceType(Loc);
36100b57cec5SDimitry Andric         assert(TypeRep);
36110b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
36120b57cec5SDimitry Andric                                        DiagID, TypeRep, Policy);
36130b57cec5SDimitry Andric         if (isInvalid)
36140b57cec5SDimitry Andric           break;
36150b57cec5SDimitry Andric 
36160b57cec5SDimitry Andric         DS.SetRangeEnd(Loc);
36170b57cec5SDimitry Andric         ConsumeToken();
36180b57cec5SDimitry Andric         continue;
36190b57cec5SDimitry Andric       }
36200b57cec5SDimitry Andric 
36210b57cec5SDimitry Andric       // If we're in a context where the identifier could be a class name,
36220b57cec5SDimitry Andric       // check whether this is a constructor declaration.
36230b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class &&
36240b57cec5SDimitry Andric           Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
3625*bdd1243dSDimitry Andric           isConstructorDeclarator(/*Unqualified=*/true,
3626*bdd1243dSDimitry Andric                                   /*DeductionGuide=*/false,
3627*bdd1243dSDimitry Andric                                   DS.isFriendSpecified()))
36280b57cec5SDimitry Andric         goto DoneWithDeclSpec;
36290b57cec5SDimitry Andric 
36300b57cec5SDimitry Andric       ParsedType TypeRep = Actions.getTypeName(
36310b57cec5SDimitry Andric           *Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), nullptr,
36320b57cec5SDimitry Andric           false, false, nullptr, false, false,
36330b57cec5SDimitry Andric           isClassTemplateDeductionContext(DSContext));
36340b57cec5SDimitry Andric 
36350b57cec5SDimitry Andric       // If this is not a typedef name, don't parse it as part of the declspec,
36360b57cec5SDimitry Andric       // it must be an implicit int or an error.
36370b57cec5SDimitry Andric       if (!TypeRep) {
363855e4f9d5SDimitry Andric         if (TryAnnotateTypeConstraint())
363955e4f9d5SDimitry Andric           goto DoneWithDeclSpec;
36405ffd83dbSDimitry Andric         if (Tok.isNot(tok::identifier))
3641aec4c088SDimitry Andric           continue;
364281ad6265SDimitry Andric         ParsedAttributes Attrs(AttrFactory);
36430b57cec5SDimitry Andric         if (ParseImplicitInt(DS, nullptr, TemplateInfo, AS, DSContext, Attrs)) {
36440b57cec5SDimitry Andric           if (!Attrs.empty()) {
36450b57cec5SDimitry Andric             AttrsLastTime = true;
36460b57cec5SDimitry Andric             attrs.takeAllFrom(Attrs);
36470b57cec5SDimitry Andric           }
36480b57cec5SDimitry Andric           continue;
36490b57cec5SDimitry Andric         }
36500b57cec5SDimitry Andric         goto DoneWithDeclSpec;
36510b57cec5SDimitry Andric       }
36520b57cec5SDimitry Andric 
36530b57cec5SDimitry Andric       // Likewise, if this is a context where the identifier could be a template
36540b57cec5SDimitry Andric       // name, check whether this is a deduction guide declaration.
36550b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus17 &&
36560b57cec5SDimitry Andric           (DSContext == DeclSpecContext::DSC_class ||
36570b57cec5SDimitry Andric            DSContext == DeclSpecContext::DSC_top_level) &&
36580b57cec5SDimitry Andric           Actions.isDeductionGuideName(getCurScope(), *Tok.getIdentifierInfo(),
36590b57cec5SDimitry Andric                                        Tok.getLocation()) &&
36600b57cec5SDimitry Andric           isConstructorDeclarator(/*Unqualified*/ true,
36610b57cec5SDimitry Andric                                   /*DeductionGuide*/ true))
36620b57cec5SDimitry Andric         goto DoneWithDeclSpec;
36630b57cec5SDimitry Andric 
36640b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
36650b57cec5SDimitry Andric                                      DiagID, TypeRep, Policy);
36660b57cec5SDimitry Andric       if (isInvalid)
36670b57cec5SDimitry Andric         break;
36680b57cec5SDimitry Andric 
36690b57cec5SDimitry Andric       DS.SetRangeEnd(Tok.getLocation());
36700b57cec5SDimitry Andric       ConsumeToken(); // The identifier
36710b57cec5SDimitry Andric 
36720b57cec5SDimitry Andric       // Objective-C supports type arguments and protocol references
36730b57cec5SDimitry Andric       // following an Objective-C object or object pointer
36740b57cec5SDimitry Andric       // type. Handle either one of them.
36750b57cec5SDimitry Andric       if (Tok.is(tok::less) && getLangOpts().ObjC) {
36760b57cec5SDimitry Andric         SourceLocation NewEndLoc;
36770b57cec5SDimitry Andric         TypeResult NewTypeRep = parseObjCTypeArgsAndProtocolQualifiers(
36780b57cec5SDimitry Andric                                   Loc, TypeRep, /*consumeLastToken=*/true,
36790b57cec5SDimitry Andric                                   NewEndLoc);
36800b57cec5SDimitry Andric         if (NewTypeRep.isUsable()) {
36810b57cec5SDimitry Andric           DS.UpdateTypeRep(NewTypeRep.get());
36820b57cec5SDimitry Andric           DS.SetRangeEnd(NewEndLoc);
36830b57cec5SDimitry Andric         }
36840b57cec5SDimitry Andric       }
36850b57cec5SDimitry Andric 
36860b57cec5SDimitry Andric       // Need to support trailing type qualifiers (e.g. "id<p> const").
36870b57cec5SDimitry Andric       // If a type specifier follows, it will be diagnosed elsewhere.
36880b57cec5SDimitry Andric       continue;
36890b57cec5SDimitry Andric     }
36900b57cec5SDimitry Andric 
369155e4f9d5SDimitry Andric       // type-name or placeholder-specifier
36920b57cec5SDimitry Andric     case tok::annot_template_id: {
36930b57cec5SDimitry Andric       TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
36945ffd83dbSDimitry Andric 
36955ffd83dbSDimitry Andric       if (TemplateId->hasInvalidName()) {
36965ffd83dbSDimitry Andric         DS.SetTypeSpecError();
36975ffd83dbSDimitry Andric         break;
36985ffd83dbSDimitry Andric       }
36995ffd83dbSDimitry Andric 
370055e4f9d5SDimitry Andric       if (TemplateId->Kind == TNK_Concept_template) {
37015ffd83dbSDimitry Andric         // If we've already diagnosed that this type-constraint has invalid
3702*bdd1243dSDimitry Andric         // arguments, drop it and just form 'auto' or 'decltype(auto)'.
37035ffd83dbSDimitry Andric         if (TemplateId->hasInvalidArgs())
37045ffd83dbSDimitry Andric           TemplateId = nullptr;
37055ffd83dbSDimitry Andric 
3706*bdd1243dSDimitry Andric         // Any of the following tokens are likely the start of the user
3707*bdd1243dSDimitry Andric         // forgetting 'auto' or 'decltype(auto)', so diagnose.
3708*bdd1243dSDimitry Andric         // Note: if updating this list, please make sure we update
3709*bdd1243dSDimitry Andric         // isCXXDeclarationSpecifier's check for IsPlaceholderSpecifier to have
3710*bdd1243dSDimitry Andric         // a matching list.
3711*bdd1243dSDimitry Andric         if (NextToken().isOneOf(tok::identifier, tok::kw_const,
3712*bdd1243dSDimitry Andric                                 tok::kw_volatile, tok::kw_restrict, tok::amp,
3713*bdd1243dSDimitry Andric                                 tok::ampamp)) {
371455e4f9d5SDimitry Andric           Diag(Loc, diag::err_placeholder_expected_auto_or_decltype_auto)
371555e4f9d5SDimitry Andric               << FixItHint::CreateInsertion(NextToken().getLocation(), "auto");
371655e4f9d5SDimitry Andric           // Attempt to continue as if 'auto' was placed here.
371755e4f9d5SDimitry Andric           isInvalid = DS.SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID,
371855e4f9d5SDimitry Andric                                          TemplateId, Policy);
371955e4f9d5SDimitry Andric           break;
372055e4f9d5SDimitry Andric         }
372155e4f9d5SDimitry Andric         if (!NextToken().isOneOf(tok::kw_auto, tok::kw_decltype))
372255e4f9d5SDimitry Andric             goto DoneWithDeclSpec;
372355e4f9d5SDimitry Andric         ConsumeAnnotationToken();
372455e4f9d5SDimitry Andric         SourceLocation AutoLoc = Tok.getLocation();
372555e4f9d5SDimitry Andric         if (TryConsumeToken(tok::kw_decltype)) {
372655e4f9d5SDimitry Andric           BalancedDelimiterTracker Tracker(*this, tok::l_paren);
372755e4f9d5SDimitry Andric           if (Tracker.consumeOpen()) {
372855e4f9d5SDimitry Andric             // Something like `void foo(Iterator decltype i)`
372955e4f9d5SDimitry Andric             Diag(Tok, diag::err_expected) << tok::l_paren;
373055e4f9d5SDimitry Andric           } else {
373155e4f9d5SDimitry Andric             if (!TryConsumeToken(tok::kw_auto)) {
373255e4f9d5SDimitry Andric               // Something like `void foo(Iterator decltype(int) i)`
373355e4f9d5SDimitry Andric               Tracker.skipToEnd();
373455e4f9d5SDimitry Andric               Diag(Tok, diag::err_placeholder_expected_auto_or_decltype_auto)
373555e4f9d5SDimitry Andric                 << FixItHint::CreateReplacement(SourceRange(AutoLoc,
373655e4f9d5SDimitry Andric                                                             Tok.getLocation()),
373755e4f9d5SDimitry Andric                                                 "auto");
373855e4f9d5SDimitry Andric             } else {
373955e4f9d5SDimitry Andric               Tracker.consumeClose();
374055e4f9d5SDimitry Andric             }
374155e4f9d5SDimitry Andric           }
374255e4f9d5SDimitry Andric           ConsumedEnd = Tok.getLocation();
3743*bdd1243dSDimitry Andric           DS.setTypeArgumentRange(Tracker.getRange());
374455e4f9d5SDimitry Andric           // Even if something went wrong above, continue as if we've seen
374555e4f9d5SDimitry Andric           // `decltype(auto)`.
374655e4f9d5SDimitry Andric           isInvalid = DS.SetTypeSpecType(TST_decltype_auto, Loc, PrevSpec,
374755e4f9d5SDimitry Andric                                          DiagID, TemplateId, Policy);
374855e4f9d5SDimitry Andric         } else {
374904eeddc0SDimitry Andric           isInvalid = DS.SetTypeSpecType(TST_auto, AutoLoc, PrevSpec, DiagID,
375055e4f9d5SDimitry Andric                                          TemplateId, Policy);
375155e4f9d5SDimitry Andric         }
375255e4f9d5SDimitry Andric         break;
375355e4f9d5SDimitry Andric       }
375455e4f9d5SDimitry Andric 
37550b57cec5SDimitry Andric       if (TemplateId->Kind != TNK_Type_template &&
37560b57cec5SDimitry Andric           TemplateId->Kind != TNK_Undeclared_template) {
37570b57cec5SDimitry Andric         // This template-id does not refer to a type name, so we're
37580b57cec5SDimitry Andric         // done with the type-specifiers.
37590b57cec5SDimitry Andric         goto DoneWithDeclSpec;
37600b57cec5SDimitry Andric       }
37610b57cec5SDimitry Andric 
37620b57cec5SDimitry Andric       // If we're in a context where the template-id could be a
37630b57cec5SDimitry Andric       // constructor name or specialization, check whether this is a
37640b57cec5SDimitry Andric       // constructor declaration.
37650b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class &&
37660b57cec5SDimitry Andric           Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
3767*bdd1243dSDimitry Andric           isConstructorDeclarator(/*Unqualified=*/true,
3768*bdd1243dSDimitry Andric                                   /*DeductionGuide=*/false,
3769*bdd1243dSDimitry Andric                                   DS.isFriendSpecified()))
37700b57cec5SDimitry Andric         goto DoneWithDeclSpec;
37710b57cec5SDimitry Andric 
37720b57cec5SDimitry Andric       // Turn the template-id annotation token into a type annotation
37730b57cec5SDimitry Andric       // token, then try again to parse it as a type-specifier.
377455e4f9d5SDimitry Andric       CXXScopeSpec SS;
3775*bdd1243dSDimitry Andric       AnnotateTemplateIdTokenAsType(SS, AllowImplicitTypename);
37760b57cec5SDimitry Andric       continue;
37770b57cec5SDimitry Andric     }
37780b57cec5SDimitry Andric 
3779fe6060f1SDimitry Andric     // Attributes support.
37800b57cec5SDimitry Andric     case tok::kw___attribute:
37810b57cec5SDimitry Andric     case tok::kw___declspec:
378281ad6265SDimitry Andric       ParseAttributes(PAKM_GNU | PAKM_Declspec, DS.getAttributes(), LateAttrs);
37830b57cec5SDimitry Andric       continue;
37840b57cec5SDimitry Andric 
37850b57cec5SDimitry Andric     // Microsoft single token adornments.
37860b57cec5SDimitry Andric     case tok::kw___forceinline: {
37870b57cec5SDimitry Andric       isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
37880b57cec5SDimitry Andric       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
37890b57cec5SDimitry Andric       SourceLocation AttrNameLoc = Tok.getLocation();
37900b57cec5SDimitry Andric       DS.getAttributes().addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc,
37910b57cec5SDimitry Andric                                 nullptr, 0, ParsedAttr::AS_Keyword);
37920b57cec5SDimitry Andric       break;
37930b57cec5SDimitry Andric     }
37940b57cec5SDimitry Andric 
37950b57cec5SDimitry Andric     case tok::kw___unaligned:
37960b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID,
37970b57cec5SDimitry Andric                                  getLangOpts());
37980b57cec5SDimitry Andric       break;
37990b57cec5SDimitry Andric 
38000b57cec5SDimitry Andric     case tok::kw___sptr:
38010b57cec5SDimitry Andric     case tok::kw___uptr:
38020b57cec5SDimitry Andric     case tok::kw___ptr64:
38030b57cec5SDimitry Andric     case tok::kw___ptr32:
38040b57cec5SDimitry Andric     case tok::kw___w64:
38050b57cec5SDimitry Andric     case tok::kw___cdecl:
38060b57cec5SDimitry Andric     case tok::kw___stdcall:
38070b57cec5SDimitry Andric     case tok::kw___fastcall:
38080b57cec5SDimitry Andric     case tok::kw___thiscall:
38090b57cec5SDimitry Andric     case tok::kw___regcall:
38100b57cec5SDimitry Andric     case tok::kw___vectorcall:
38110b57cec5SDimitry Andric       ParseMicrosoftTypeAttributes(DS.getAttributes());
38120b57cec5SDimitry Andric       continue;
38130b57cec5SDimitry Andric 
38140b57cec5SDimitry Andric     // Borland single token adornments.
38150b57cec5SDimitry Andric     case tok::kw___pascal:
38160b57cec5SDimitry Andric       ParseBorlandTypeAttributes(DS.getAttributes());
38170b57cec5SDimitry Andric       continue;
38180b57cec5SDimitry Andric 
38190b57cec5SDimitry Andric     // OpenCL single token adornments.
38200b57cec5SDimitry Andric     case tok::kw___kernel:
38210b57cec5SDimitry Andric       ParseOpenCLKernelAttributes(DS.getAttributes());
38220b57cec5SDimitry Andric       continue;
38230b57cec5SDimitry Andric 
382481ad6265SDimitry Andric     // CUDA/HIP single token adornments.
382581ad6265SDimitry Andric     case tok::kw___noinline__:
382681ad6265SDimitry Andric       ParseCUDAFunctionAttributes(DS.getAttributes());
382781ad6265SDimitry Andric       continue;
382881ad6265SDimitry Andric 
38290b57cec5SDimitry Andric     // Nullability type specifiers.
38300b57cec5SDimitry Andric     case tok::kw__Nonnull:
38310b57cec5SDimitry Andric     case tok::kw__Nullable:
3832e8d8bef9SDimitry Andric     case tok::kw__Nullable_result:
38330b57cec5SDimitry Andric     case tok::kw__Null_unspecified:
38340b57cec5SDimitry Andric       ParseNullabilityTypeSpecifiers(DS.getAttributes());
38350b57cec5SDimitry Andric       continue;
38360b57cec5SDimitry Andric 
38370b57cec5SDimitry Andric     // Objective-C 'kindof' types.
38380b57cec5SDimitry Andric     case tok::kw___kindof:
38390b57cec5SDimitry Andric       DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc,
38400b57cec5SDimitry Andric                                 nullptr, 0, ParsedAttr::AS_Keyword);
38410b57cec5SDimitry Andric       (void)ConsumeToken();
38420b57cec5SDimitry Andric       continue;
38430b57cec5SDimitry Andric 
38440b57cec5SDimitry Andric     // storage-class-specifier
38450b57cec5SDimitry Andric     case tok::kw_typedef:
38460b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
38470b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
38480b57cec5SDimitry Andric       isStorageClass = true;
38490b57cec5SDimitry Andric       break;
38500b57cec5SDimitry Andric     case tok::kw_extern:
38510b57cec5SDimitry Andric       if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
38520b57cec5SDimitry Andric         Diag(Tok, diag::ext_thread_before) << "extern";
38530b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
38540b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
38550b57cec5SDimitry Andric       isStorageClass = true;
38560b57cec5SDimitry Andric       break;
38570b57cec5SDimitry Andric     case tok::kw___private_extern__:
38580b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
38590b57cec5SDimitry Andric                                          Loc, PrevSpec, DiagID, Policy);
38600b57cec5SDimitry Andric       isStorageClass = true;
38610b57cec5SDimitry Andric       break;
38620b57cec5SDimitry Andric     case tok::kw_static:
38630b57cec5SDimitry Andric       if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
38640b57cec5SDimitry Andric         Diag(Tok, diag::ext_thread_before) << "static";
38650b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
38660b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
38670b57cec5SDimitry Andric       isStorageClass = true;
38680b57cec5SDimitry Andric       break;
38690b57cec5SDimitry Andric     case tok::kw_auto:
38700b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus11) {
38710b57cec5SDimitry Andric         if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
38720b57cec5SDimitry Andric           isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
38730b57cec5SDimitry Andric                                              PrevSpec, DiagID, Policy);
38740b57cec5SDimitry Andric           if (!isInvalid)
38750b57cec5SDimitry Andric             Diag(Tok, diag::ext_auto_storage_class)
38760b57cec5SDimitry Andric               << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
38770b57cec5SDimitry Andric         } else
38780b57cec5SDimitry Andric           isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
38790b57cec5SDimitry Andric                                          DiagID, Policy);
38800b57cec5SDimitry Andric       } else
38810b57cec5SDimitry Andric         isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
38820b57cec5SDimitry Andric                                            PrevSpec, DiagID, Policy);
38830b57cec5SDimitry Andric       isStorageClass = true;
38840b57cec5SDimitry Andric       break;
38850b57cec5SDimitry Andric     case tok::kw___auto_type:
38860b57cec5SDimitry Andric       Diag(Tok, diag::ext_auto_type);
38870b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto_type, Loc, PrevSpec,
38880b57cec5SDimitry Andric                                      DiagID, Policy);
38890b57cec5SDimitry Andric       break;
38900b57cec5SDimitry Andric     case tok::kw_register:
38910b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
38920b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
38930b57cec5SDimitry Andric       isStorageClass = true;
38940b57cec5SDimitry Andric       break;
38950b57cec5SDimitry Andric     case tok::kw_mutable:
38960b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
38970b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
38980b57cec5SDimitry Andric       isStorageClass = true;
38990b57cec5SDimitry Andric       break;
39000b57cec5SDimitry Andric     case tok::kw___thread:
39010b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
39020b57cec5SDimitry Andric                                                PrevSpec, DiagID);
39030b57cec5SDimitry Andric       isStorageClass = true;
39040b57cec5SDimitry Andric       break;
39050b57cec5SDimitry Andric     case tok::kw_thread_local:
39060b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
39070b57cec5SDimitry Andric                                                PrevSpec, DiagID);
39080b57cec5SDimitry Andric       isStorageClass = true;
39090b57cec5SDimitry Andric       break;
39100b57cec5SDimitry Andric     case tok::kw__Thread_local:
3911a7dea167SDimitry Andric       if (!getLangOpts().C11)
3912a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
39130b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
39140b57cec5SDimitry Andric                                                Loc, PrevSpec, DiagID);
39150b57cec5SDimitry Andric       isStorageClass = true;
39160b57cec5SDimitry Andric       break;
39170b57cec5SDimitry Andric 
39180b57cec5SDimitry Andric     // function-specifier
39190b57cec5SDimitry Andric     case tok::kw_inline:
39200b57cec5SDimitry Andric       isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
39210b57cec5SDimitry Andric       break;
39220b57cec5SDimitry Andric     case tok::kw_virtual:
39230b57cec5SDimitry Andric       // C++ for OpenCL does not allow virtual function qualifier, to avoid
39240b57cec5SDimitry Andric       // function pointers restricted in OpenCL v2.0 s6.9.a.
3925e8d8bef9SDimitry Andric       if (getLangOpts().OpenCLCPlusPlus &&
3926fe6060f1SDimitry Andric           !getActions().getOpenCLOptions().isAvailableOption(
3927fe6060f1SDimitry Andric               "__cl_clang_function_pointers", getLangOpts())) {
39280b57cec5SDimitry Andric         DiagID = diag::err_openclcxx_virtual_function;
39290b57cec5SDimitry Andric         PrevSpec = Tok.getIdentifierInfo()->getNameStart();
39300b57cec5SDimitry Andric         isInvalid = true;
3931e8d8bef9SDimitry Andric       } else {
39320b57cec5SDimitry Andric         isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
39330b57cec5SDimitry Andric       }
39340b57cec5SDimitry Andric       break;
39350b57cec5SDimitry Andric     case tok::kw_explicit: {
39360b57cec5SDimitry Andric       SourceLocation ExplicitLoc = Loc;
39370b57cec5SDimitry Andric       SourceLocation CloseParenLoc;
39380b57cec5SDimitry Andric       ExplicitSpecifier ExplicitSpec(nullptr, ExplicitSpecKind::ResolvedTrue);
39390b57cec5SDimitry Andric       ConsumedEnd = ExplicitLoc;
39400b57cec5SDimitry Andric       ConsumeToken(); // kw_explicit
39410b57cec5SDimitry Andric       if (Tok.is(tok::l_paren)) {
39425ffd83dbSDimitry Andric         if (getLangOpts().CPlusPlus20 || isExplicitBool() == TPResult::True) {
39435ffd83dbSDimitry Andric           Diag(Tok.getLocation(), getLangOpts().CPlusPlus20
394455e4f9d5SDimitry Andric                                       ? diag::warn_cxx17_compat_explicit_bool
394555e4f9d5SDimitry Andric                                       : diag::ext_explicit_bool);
394655e4f9d5SDimitry Andric 
39470b57cec5SDimitry Andric           ExprResult ExplicitExpr(static_cast<Expr *>(nullptr));
39480b57cec5SDimitry Andric           BalancedDelimiterTracker Tracker(*this, tok::l_paren);
39490b57cec5SDimitry Andric           Tracker.consumeOpen();
39500b57cec5SDimitry Andric           ExplicitExpr = ParseConstantExpression();
39510b57cec5SDimitry Andric           ConsumedEnd = Tok.getLocation();
39520b57cec5SDimitry Andric           if (ExplicitExpr.isUsable()) {
39530b57cec5SDimitry Andric             CloseParenLoc = Tok.getLocation();
39540b57cec5SDimitry Andric             Tracker.consumeClose();
39550b57cec5SDimitry Andric             ExplicitSpec =
39560b57cec5SDimitry Andric                 Actions.ActOnExplicitBoolSpecifier(ExplicitExpr.get());
39570b57cec5SDimitry Andric           } else
39580b57cec5SDimitry Andric             Tracker.skipToEnd();
395955e4f9d5SDimitry Andric         } else {
39605ffd83dbSDimitry Andric           Diag(Tok.getLocation(), diag::warn_cxx20_compat_explicit_bool);
39610b57cec5SDimitry Andric         }
396255e4f9d5SDimitry Andric       }
39630b57cec5SDimitry Andric       isInvalid = DS.setFunctionSpecExplicit(ExplicitLoc, PrevSpec, DiagID,
39640b57cec5SDimitry Andric                                              ExplicitSpec, CloseParenLoc);
39650b57cec5SDimitry Andric       break;
39660b57cec5SDimitry Andric     }
39670b57cec5SDimitry Andric     case tok::kw__Noreturn:
39680b57cec5SDimitry Andric       if (!getLangOpts().C11)
3969a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
39700b57cec5SDimitry Andric       isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
39710b57cec5SDimitry Andric       break;
39720b57cec5SDimitry Andric 
39730b57cec5SDimitry Andric     // alignment-specifier
39740b57cec5SDimitry Andric     case tok::kw__Alignas:
39750b57cec5SDimitry Andric       if (!getLangOpts().C11)
3976a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
39770b57cec5SDimitry Andric       ParseAlignmentSpecifier(DS.getAttributes());
39780b57cec5SDimitry Andric       continue;
39790b57cec5SDimitry Andric 
39800b57cec5SDimitry Andric     // friend
39810b57cec5SDimitry Andric     case tok::kw_friend:
39820b57cec5SDimitry Andric       if (DSContext == DeclSpecContext::DSC_class)
39830b57cec5SDimitry Andric         isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
39840b57cec5SDimitry Andric       else {
39850b57cec5SDimitry Andric         PrevSpec = ""; // not actually used by the diagnostic
39860b57cec5SDimitry Andric         DiagID = diag::err_friend_invalid_in_context;
39870b57cec5SDimitry Andric         isInvalid = true;
39880b57cec5SDimitry Andric       }
39890b57cec5SDimitry Andric       break;
39900b57cec5SDimitry Andric 
39910b57cec5SDimitry Andric     // Modules
39920b57cec5SDimitry Andric     case tok::kw___module_private__:
39930b57cec5SDimitry Andric       isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
39940b57cec5SDimitry Andric       break;
39950b57cec5SDimitry Andric 
3996a7dea167SDimitry Andric     // constexpr, consteval, constinit specifiers
39970b57cec5SDimitry Andric     case tok::kw_constexpr:
3998e8d8bef9SDimitry Andric       isInvalid = DS.SetConstexprSpec(ConstexprSpecKind::Constexpr, Loc,
3999e8d8bef9SDimitry Andric                                       PrevSpec, DiagID);
40000b57cec5SDimitry Andric       break;
40010b57cec5SDimitry Andric     case tok::kw_consteval:
4002e8d8bef9SDimitry Andric       isInvalid = DS.SetConstexprSpec(ConstexprSpecKind::Consteval, Loc,
4003e8d8bef9SDimitry Andric                                       PrevSpec, DiagID);
40040b57cec5SDimitry Andric       break;
4005a7dea167SDimitry Andric     case tok::kw_constinit:
4006e8d8bef9SDimitry Andric       isInvalid = DS.SetConstexprSpec(ConstexprSpecKind::Constinit, Loc,
4007e8d8bef9SDimitry Andric                                       PrevSpec, DiagID);
4008a7dea167SDimitry Andric       break;
40090b57cec5SDimitry Andric 
40100b57cec5SDimitry Andric     // type-specifier
40110b57cec5SDimitry Andric     case tok::kw_short:
4012e8d8bef9SDimitry Andric       isInvalid = DS.SetTypeSpecWidth(TypeSpecifierWidth::Short, Loc, PrevSpec,
40130b57cec5SDimitry Andric                                       DiagID, Policy);
40140b57cec5SDimitry Andric       break;
40150b57cec5SDimitry Andric     case tok::kw_long:
4016e8d8bef9SDimitry Andric       if (DS.getTypeSpecWidth() != TypeSpecifierWidth::Long)
4017e8d8bef9SDimitry Andric         isInvalid = DS.SetTypeSpecWidth(TypeSpecifierWidth::Long, Loc, PrevSpec,
40180b57cec5SDimitry Andric                                         DiagID, Policy);
40190b57cec5SDimitry Andric       else
4020e8d8bef9SDimitry Andric         isInvalid = DS.SetTypeSpecWidth(TypeSpecifierWidth::LongLong, Loc,
4021e8d8bef9SDimitry Andric                                         PrevSpec, DiagID, Policy);
40220b57cec5SDimitry Andric       break;
40230b57cec5SDimitry Andric     case tok::kw___int64:
4024e8d8bef9SDimitry Andric       isInvalid = DS.SetTypeSpecWidth(TypeSpecifierWidth::LongLong, Loc,
4025e8d8bef9SDimitry Andric                                       PrevSpec, DiagID, Policy);
40260b57cec5SDimitry Andric       break;
40270b57cec5SDimitry Andric     case tok::kw_signed:
4028e8d8bef9SDimitry Andric       isInvalid =
4029e8d8bef9SDimitry Andric           DS.SetTypeSpecSign(TypeSpecifierSign::Signed, Loc, PrevSpec, DiagID);
40300b57cec5SDimitry Andric       break;
40310b57cec5SDimitry Andric     case tok::kw_unsigned:
4032e8d8bef9SDimitry Andric       isInvalid = DS.SetTypeSpecSign(TypeSpecifierSign::Unsigned, Loc, PrevSpec,
40330b57cec5SDimitry Andric                                      DiagID);
40340b57cec5SDimitry Andric       break;
40350b57cec5SDimitry Andric     case tok::kw__Complex:
4036a7dea167SDimitry Andric       if (!getLangOpts().C99)
4037a7dea167SDimitry Andric         Diag(Tok, diag::ext_c99_feature) << Tok.getName();
40380b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
40390b57cec5SDimitry Andric                                         DiagID);
40400b57cec5SDimitry Andric       break;
40410b57cec5SDimitry Andric     case tok::kw__Imaginary:
4042a7dea167SDimitry Andric       if (!getLangOpts().C99)
4043a7dea167SDimitry Andric         Diag(Tok, diag::ext_c99_feature) << Tok.getName();
40440b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
40450b57cec5SDimitry Andric                                         DiagID);
40460b57cec5SDimitry Andric       break;
40470b57cec5SDimitry Andric     case tok::kw_void:
40480b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
40490b57cec5SDimitry Andric                                      DiagID, Policy);
40500b57cec5SDimitry Andric       break;
40510b57cec5SDimitry Andric     case tok::kw_char:
40520b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
40530b57cec5SDimitry Andric                                      DiagID, Policy);
40540b57cec5SDimitry Andric       break;
40550b57cec5SDimitry Andric     case tok::kw_int:
40560b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
40570b57cec5SDimitry Andric                                      DiagID, Policy);
40580b57cec5SDimitry Andric       break;
40590eae32dcSDimitry Andric     case tok::kw__ExtInt:
40600eae32dcSDimitry Andric     case tok::kw__BitInt: {
40610eae32dcSDimitry Andric       DiagnoseBitIntUse(Tok);
40625ffd83dbSDimitry Andric       ExprResult ER = ParseExtIntegerArgument();
40635ffd83dbSDimitry Andric       if (ER.isInvalid())
40645ffd83dbSDimitry Andric         continue;
40650eae32dcSDimitry Andric       isInvalid = DS.SetBitIntType(Loc, ER.get(), PrevSpec, DiagID, Policy);
40665ffd83dbSDimitry Andric       ConsumedEnd = PrevTokLocation;
40675ffd83dbSDimitry Andric       break;
40685ffd83dbSDimitry Andric     }
40690b57cec5SDimitry Andric     case tok::kw___int128:
40700b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
40710b57cec5SDimitry Andric                                      DiagID, Policy);
40720b57cec5SDimitry Andric       break;
40730b57cec5SDimitry Andric     case tok::kw_half:
40740b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
40750b57cec5SDimitry Andric                                      DiagID, Policy);
40760b57cec5SDimitry Andric       break;
40775ffd83dbSDimitry Andric     case tok::kw___bf16:
40785ffd83dbSDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_BFloat16, Loc, PrevSpec,
40795ffd83dbSDimitry Andric                                      DiagID, Policy);
40805ffd83dbSDimitry Andric       break;
40810b57cec5SDimitry Andric     case tok::kw_float:
40820b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
40830b57cec5SDimitry Andric                                      DiagID, Policy);
40840b57cec5SDimitry Andric       break;
40850b57cec5SDimitry Andric     case tok::kw_double:
40860b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
40870b57cec5SDimitry Andric                                      DiagID, Policy);
40880b57cec5SDimitry Andric       break;
40890b57cec5SDimitry Andric     case tok::kw__Float16:
40900b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float16, Loc, PrevSpec,
40910b57cec5SDimitry Andric                                      DiagID, Policy);
40920b57cec5SDimitry Andric       break;
40930b57cec5SDimitry Andric     case tok::kw__Accum:
40940b57cec5SDimitry Andric       if (!getLangOpts().FixedPoint) {
40950b57cec5SDimitry Andric         SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
40960b57cec5SDimitry Andric       } else {
40970b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_accum, Loc, PrevSpec,
40980b57cec5SDimitry Andric                                        DiagID, Policy);
40990b57cec5SDimitry Andric       }
41000b57cec5SDimitry Andric       break;
41010b57cec5SDimitry Andric     case tok::kw__Fract:
41020b57cec5SDimitry Andric       if (!getLangOpts().FixedPoint) {
41030b57cec5SDimitry Andric         SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
41040b57cec5SDimitry Andric       } else {
41050b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_fract, Loc, PrevSpec,
41060b57cec5SDimitry Andric                                        DiagID, Policy);
41070b57cec5SDimitry Andric       }
41080b57cec5SDimitry Andric       break;
41090b57cec5SDimitry Andric     case tok::kw__Sat:
41100b57cec5SDimitry Andric       if (!getLangOpts().FixedPoint) {
41110b57cec5SDimitry Andric         SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
41120b57cec5SDimitry Andric       } else {
41130b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecSat(Loc, PrevSpec, DiagID);
41140b57cec5SDimitry Andric       }
41150b57cec5SDimitry Andric       break;
41160b57cec5SDimitry Andric     case tok::kw___float128:
41170b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float128, Loc, PrevSpec,
41180b57cec5SDimitry Andric                                      DiagID, Policy);
41190b57cec5SDimitry Andric       break;
4120349cc55cSDimitry Andric     case tok::kw___ibm128:
4121349cc55cSDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_ibm128, Loc, PrevSpec,
4122349cc55cSDimitry Andric                                      DiagID, Policy);
4123349cc55cSDimitry Andric       break;
41240b57cec5SDimitry Andric     case tok::kw_wchar_t:
41250b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
41260b57cec5SDimitry Andric                                      DiagID, Policy);
41270b57cec5SDimitry Andric       break;
41280b57cec5SDimitry Andric     case tok::kw_char8_t:
41290b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char8, Loc, PrevSpec,
41300b57cec5SDimitry Andric                                      DiagID, Policy);
41310b57cec5SDimitry Andric       break;
41320b57cec5SDimitry Andric     case tok::kw_char16_t:
41330b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
41340b57cec5SDimitry Andric                                      DiagID, Policy);
41350b57cec5SDimitry Andric       break;
41360b57cec5SDimitry Andric     case tok::kw_char32_t:
41370b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
41380b57cec5SDimitry Andric                                      DiagID, Policy);
41390b57cec5SDimitry Andric       break;
41400b57cec5SDimitry Andric     case tok::kw_bool:
41410b57cec5SDimitry Andric     case tok::kw__Bool:
4142a7dea167SDimitry Andric       if (Tok.is(tok::kw__Bool) && !getLangOpts().C99)
4143a7dea167SDimitry Andric         Diag(Tok, diag::ext_c99_feature) << Tok.getName();
4144a7dea167SDimitry Andric 
41450b57cec5SDimitry Andric       if (Tok.is(tok::kw_bool) &&
41460b57cec5SDimitry Andric           DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
41470b57cec5SDimitry Andric           DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
41480b57cec5SDimitry Andric         PrevSpec = ""; // Not used by the diagnostic.
41490b57cec5SDimitry Andric         DiagID = diag::err_bool_redeclaration;
41500b57cec5SDimitry Andric         // For better error recovery.
41510b57cec5SDimitry Andric         Tok.setKind(tok::identifier);
41520b57cec5SDimitry Andric         isInvalid = true;
41530b57cec5SDimitry Andric       } else {
41540b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
41550b57cec5SDimitry Andric                                        DiagID, Policy);
41560b57cec5SDimitry Andric       }
41570b57cec5SDimitry Andric       break;
41580b57cec5SDimitry Andric     case tok::kw__Decimal32:
41590b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
41600b57cec5SDimitry Andric                                      DiagID, Policy);
41610b57cec5SDimitry Andric       break;
41620b57cec5SDimitry Andric     case tok::kw__Decimal64:
41630b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
41640b57cec5SDimitry Andric                                      DiagID, Policy);
41650b57cec5SDimitry Andric       break;
41660b57cec5SDimitry Andric     case tok::kw__Decimal128:
41670b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
41680b57cec5SDimitry Andric                                      DiagID, Policy);
41690b57cec5SDimitry Andric       break;
41700b57cec5SDimitry Andric     case tok::kw___vector:
41710b57cec5SDimitry Andric       isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
41720b57cec5SDimitry Andric       break;
41730b57cec5SDimitry Andric     case tok::kw___pixel:
41740b57cec5SDimitry Andric       isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
41750b57cec5SDimitry Andric       break;
41760b57cec5SDimitry Andric     case tok::kw___bool:
41770b57cec5SDimitry Andric       isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
41780b57cec5SDimitry Andric       break;
41790b57cec5SDimitry Andric     case tok::kw_pipe:
4180349cc55cSDimitry Andric       if (!getLangOpts().OpenCL ||
4181349cc55cSDimitry Andric           getLangOpts().getOpenCLCompatibleVersion() < 200) {
4182fe6060f1SDimitry Andric         // OpenCL 2.0 and later define this keyword. OpenCL 1.2 and earlier
4183fe6060f1SDimitry Andric         // should support the "pipe" word as identifier.
41840b57cec5SDimitry Andric         Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
4185fe6060f1SDimitry Andric         Tok.setKind(tok::identifier);
41860b57cec5SDimitry Andric         goto DoneWithDeclSpec;
41876e75b2fbSDimitry Andric       } else if (!getLangOpts().OpenCLPipes) {
41886e75b2fbSDimitry Andric         DiagID = diag::err_opencl_unknown_type_specifier;
41896e75b2fbSDimitry Andric         PrevSpec = Tok.getIdentifierInfo()->getNameStart();
41906e75b2fbSDimitry Andric         isInvalid = true;
41916e75b2fbSDimitry Andric       } else
41920b57cec5SDimitry Andric         isInvalid = DS.SetTypePipe(true, Loc, PrevSpec, DiagID, Policy);
41930b57cec5SDimitry Andric       break;
4194fe6060f1SDimitry Andric // We only need to enumerate each image type once.
4195fe6060f1SDimitry Andric #define IMAGE_READ_WRITE_TYPE(Type, Id, Ext)
4196fe6060f1SDimitry Andric #define IMAGE_WRITE_TYPE(Type, Id, Ext)
4197fe6060f1SDimitry Andric #define IMAGE_READ_TYPE(ImgType, Id, Ext) \
41980b57cec5SDimitry Andric     case tok::kw_##ImgType##_t: \
4199fe6060f1SDimitry Andric       if (!handleOpenCLImageKW(Ext, DeclSpec::TST_##ImgType##_t)) \
4200fe6060f1SDimitry Andric         goto DoneWithDeclSpec; \
42010b57cec5SDimitry Andric       break;
42020b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def"
42030b57cec5SDimitry Andric     case tok::kw___unknown_anytype:
42040b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
42050b57cec5SDimitry Andric                                      PrevSpec, DiagID, Policy);
42060b57cec5SDimitry Andric       break;
42070b57cec5SDimitry Andric 
42080b57cec5SDimitry Andric     // class-specifier:
42090b57cec5SDimitry Andric     case tok::kw_class:
42100b57cec5SDimitry Andric     case tok::kw_struct:
42110b57cec5SDimitry Andric     case tok::kw___interface:
42120b57cec5SDimitry Andric     case tok::kw_union: {
42130b57cec5SDimitry Andric       tok::TokenKind Kind = Tok.getKind();
42140b57cec5SDimitry Andric       ConsumeToken();
42150b57cec5SDimitry Andric 
42160b57cec5SDimitry Andric       // These are attributes following class specifiers.
42170b57cec5SDimitry Andric       // To produce better diagnostic, we parse them when
42180b57cec5SDimitry Andric       // parsing class specifier.
421981ad6265SDimitry Andric       ParsedAttributes Attributes(AttrFactory);
42200b57cec5SDimitry Andric       ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
42210b57cec5SDimitry Andric                           EnteringContext, DSContext, Attributes);
42220b57cec5SDimitry Andric 
42230b57cec5SDimitry Andric       // If there are attributes following class specifier,
42240b57cec5SDimitry Andric       // take them over and handle them here.
42250b57cec5SDimitry Andric       if (!Attributes.empty()) {
42260b57cec5SDimitry Andric         AttrsLastTime = true;
42270b57cec5SDimitry Andric         attrs.takeAllFrom(Attributes);
42280b57cec5SDimitry Andric       }
42290b57cec5SDimitry Andric       continue;
42300b57cec5SDimitry Andric     }
42310b57cec5SDimitry Andric 
42320b57cec5SDimitry Andric     // enum-specifier:
42330b57cec5SDimitry Andric     case tok::kw_enum:
42340b57cec5SDimitry Andric       ConsumeToken();
42350b57cec5SDimitry Andric       ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
42360b57cec5SDimitry Andric       continue;
42370b57cec5SDimitry Andric 
42380b57cec5SDimitry Andric     // cv-qualifier:
42390b57cec5SDimitry Andric     case tok::kw_const:
42400b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
42410b57cec5SDimitry Andric                                  getLangOpts());
42420b57cec5SDimitry Andric       break;
42430b57cec5SDimitry Andric     case tok::kw_volatile:
42440b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
42450b57cec5SDimitry Andric                                  getLangOpts());
42460b57cec5SDimitry Andric       break;
42470b57cec5SDimitry Andric     case tok::kw_restrict:
42480b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
42490b57cec5SDimitry Andric                                  getLangOpts());
42500b57cec5SDimitry Andric       break;
42510b57cec5SDimitry Andric 
42520b57cec5SDimitry Andric     // C++ typename-specifier:
42530b57cec5SDimitry Andric     case tok::kw_typename:
42540b57cec5SDimitry Andric       if (TryAnnotateTypeOrScopeToken()) {
42550b57cec5SDimitry Andric         DS.SetTypeSpecError();
42560b57cec5SDimitry Andric         goto DoneWithDeclSpec;
42570b57cec5SDimitry Andric       }
42580b57cec5SDimitry Andric       if (!Tok.is(tok::kw_typename))
42590b57cec5SDimitry Andric         continue;
42600b57cec5SDimitry Andric       break;
42610b57cec5SDimitry Andric 
4262*bdd1243dSDimitry Andric     // C2x/GNU typeof support.
42630b57cec5SDimitry Andric     case tok::kw_typeof:
4264*bdd1243dSDimitry Andric     case tok::kw_typeof_unqual:
42650b57cec5SDimitry Andric       ParseTypeofSpecifier(DS);
42660b57cec5SDimitry Andric       continue;
42670b57cec5SDimitry Andric 
42680b57cec5SDimitry Andric     case tok::annot_decltype:
42690b57cec5SDimitry Andric       ParseDecltypeSpecifier(DS);
42700b57cec5SDimitry Andric       continue;
42710b57cec5SDimitry Andric 
42720b57cec5SDimitry Andric     case tok::annot_pragma_pack:
42730b57cec5SDimitry Andric       HandlePragmaPack();
42740b57cec5SDimitry Andric       continue;
42750b57cec5SDimitry Andric 
42760b57cec5SDimitry Andric     case tok::annot_pragma_ms_pragma:
42770b57cec5SDimitry Andric       HandlePragmaMSPragma();
42780b57cec5SDimitry Andric       continue;
42790b57cec5SDimitry Andric 
42800b57cec5SDimitry Andric     case tok::annot_pragma_ms_vtordisp:
42810b57cec5SDimitry Andric       HandlePragmaMSVtorDisp();
42820b57cec5SDimitry Andric       continue;
42830b57cec5SDimitry Andric 
42840b57cec5SDimitry Andric     case tok::annot_pragma_ms_pointers_to_members:
42850b57cec5SDimitry Andric       HandlePragmaMSPointersToMembers();
42860b57cec5SDimitry Andric       continue;
42870b57cec5SDimitry Andric 
4288*bdd1243dSDimitry Andric #define TRANSFORM_TYPE_TRAIT_DEF(_, Trait) case tok::kw___##Trait:
4289*bdd1243dSDimitry Andric #include "clang/Basic/TransformTypeTraits.def"
4290*bdd1243dSDimitry Andric       // HACK: libstdc++ already uses '__remove_cv' as an alias template so we
4291*bdd1243dSDimitry Andric       // work around this by expecting all transform type traits to be suffixed
4292*bdd1243dSDimitry Andric       // with '('. They're an identifier otherwise.
4293*bdd1243dSDimitry Andric       if (!MaybeParseTypeTransformTypeSpecifier(DS))
4294*bdd1243dSDimitry Andric         goto ParseIdentifier;
42950b57cec5SDimitry Andric       continue;
42960b57cec5SDimitry Andric 
42970b57cec5SDimitry Andric     case tok::kw__Atomic:
42980b57cec5SDimitry Andric       // C11 6.7.2.4/4:
42990b57cec5SDimitry Andric       //   If the _Atomic keyword is immediately followed by a left parenthesis,
43000b57cec5SDimitry Andric       //   it is interpreted as a type specifier (with a type name), not as a
43010b57cec5SDimitry Andric       //   type qualifier.
4302a7dea167SDimitry Andric       if (!getLangOpts().C11)
4303a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
4304a7dea167SDimitry Andric 
43050b57cec5SDimitry Andric       if (NextToken().is(tok::l_paren)) {
43060b57cec5SDimitry Andric         ParseAtomicSpecifier(DS);
43070b57cec5SDimitry Andric         continue;
43080b57cec5SDimitry Andric       }
43090b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
43100b57cec5SDimitry Andric                                  getLangOpts());
43110b57cec5SDimitry Andric       break;
43120b57cec5SDimitry Andric 
43130b57cec5SDimitry Andric     // OpenCL address space qualifiers:
43140b57cec5SDimitry Andric     case tok::kw___generic:
43150b57cec5SDimitry Andric       // generic address space is introduced only in OpenCL v2.0
43160b57cec5SDimitry Andric       // see OpenCL C Spec v2.0 s6.5.5
4317fe6060f1SDimitry Andric       // OpenCL v3.0 introduces __opencl_c_generic_address_space
4318fe6060f1SDimitry Andric       // feature macro to indicate if generic address space is supported
4319fe6060f1SDimitry Andric       if (!Actions.getLangOpts().OpenCLGenericAddressSpace) {
43200b57cec5SDimitry Andric         DiagID = diag::err_opencl_unknown_type_specifier;
43210b57cec5SDimitry Andric         PrevSpec = Tok.getIdentifierInfo()->getNameStart();
43220b57cec5SDimitry Andric         isInvalid = true;
43230b57cec5SDimitry Andric         break;
4324480093f4SDimitry Andric       }
4325*bdd1243dSDimitry Andric       [[fallthrough]];
43260b57cec5SDimitry Andric     case tok::kw_private:
4327480093f4SDimitry Andric       // It's fine (but redundant) to check this for __generic on the
4328480093f4SDimitry Andric       // fallthrough path; we only form the __generic token in OpenCL mode.
4329480093f4SDimitry Andric       if (!getLangOpts().OpenCL)
4330480093f4SDimitry Andric         goto DoneWithDeclSpec;
4331*bdd1243dSDimitry Andric       [[fallthrough]];
43320b57cec5SDimitry Andric     case tok::kw___private:
43330b57cec5SDimitry Andric     case tok::kw___global:
43340b57cec5SDimitry Andric     case tok::kw___local:
43350b57cec5SDimitry Andric     case tok::kw___constant:
43360b57cec5SDimitry Andric     // OpenCL access qualifiers:
43370b57cec5SDimitry Andric     case tok::kw___read_only:
43380b57cec5SDimitry Andric     case tok::kw___write_only:
43390b57cec5SDimitry Andric     case tok::kw___read_write:
43400b57cec5SDimitry Andric       ParseOpenCLQualifiers(DS.getAttributes());
43410b57cec5SDimitry Andric       break;
43420b57cec5SDimitry Andric 
4343*bdd1243dSDimitry Andric     case tok::kw_groupshared:
4344*bdd1243dSDimitry Andric       // NOTE: ParseHLSLQualifiers will consume the qualifier token.
4345*bdd1243dSDimitry Andric       ParseHLSLQualifiers(DS.getAttributes());
4346*bdd1243dSDimitry Andric       continue;
4347*bdd1243dSDimitry Andric 
43480b57cec5SDimitry Andric     case tok::less:
43490b57cec5SDimitry Andric       // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
43500b57cec5SDimitry Andric       // "id<SomeProtocol>".  This is hopelessly old fashioned and dangerous,
43510b57cec5SDimitry Andric       // but we support it.
43520b57cec5SDimitry Andric       if (DS.hasTypeSpecifier() || !getLangOpts().ObjC)
43530b57cec5SDimitry Andric         goto DoneWithDeclSpec;
43540b57cec5SDimitry Andric 
43550b57cec5SDimitry Andric       SourceLocation StartLoc = Tok.getLocation();
43560b57cec5SDimitry Andric       SourceLocation EndLoc;
43570b57cec5SDimitry Andric       TypeResult Type = parseObjCProtocolQualifierType(EndLoc);
43580b57cec5SDimitry Andric       if (Type.isUsable()) {
43590b57cec5SDimitry Andric         if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, StartLoc,
43600b57cec5SDimitry Andric                                PrevSpec, DiagID, Type.get(),
43610b57cec5SDimitry Andric                                Actions.getASTContext().getPrintingPolicy()))
43620b57cec5SDimitry Andric           Diag(StartLoc, DiagID) << PrevSpec;
43630b57cec5SDimitry Andric 
43640b57cec5SDimitry Andric         DS.SetRangeEnd(EndLoc);
43650b57cec5SDimitry Andric       } else {
43660b57cec5SDimitry Andric         DS.SetTypeSpecError();
43670b57cec5SDimitry Andric       }
43680b57cec5SDimitry Andric 
43690b57cec5SDimitry Andric       // Need to support trailing type qualifiers (e.g. "id<p> const").
43700b57cec5SDimitry Andric       // If a type specifier follows, it will be diagnosed elsewhere.
43710b57cec5SDimitry Andric       continue;
43720b57cec5SDimitry Andric     }
43730b57cec5SDimitry Andric 
43740b57cec5SDimitry Andric     DS.SetRangeEnd(ConsumedEnd.isValid() ? ConsumedEnd : Tok.getLocation());
43750b57cec5SDimitry Andric 
43760b57cec5SDimitry Andric     // If the specifier wasn't legal, issue a diagnostic.
43770b57cec5SDimitry Andric     if (isInvalid) {
43780b57cec5SDimitry Andric       assert(PrevSpec && "Method did not return previous specifier!");
43790b57cec5SDimitry Andric       assert(DiagID);
43800b57cec5SDimitry Andric 
43810b57cec5SDimitry Andric       if (DiagID == diag::ext_duplicate_declspec ||
43820b57cec5SDimitry Andric           DiagID == diag::ext_warn_duplicate_declspec ||
43830b57cec5SDimitry Andric           DiagID == diag::err_duplicate_declspec)
43840b57cec5SDimitry Andric         Diag(Loc, DiagID) << PrevSpec
43850b57cec5SDimitry Andric                           << FixItHint::CreateRemoval(
43860b57cec5SDimitry Andric                                  SourceRange(Loc, DS.getEndLoc()));
43870b57cec5SDimitry Andric       else if (DiagID == diag::err_opencl_unknown_type_specifier) {
4388349cc55cSDimitry Andric         Diag(Loc, DiagID) << getLangOpts().getOpenCLVersionString() << PrevSpec
4389349cc55cSDimitry Andric                           << isStorageClass;
43900b57cec5SDimitry Andric       } else
43910b57cec5SDimitry Andric         Diag(Loc, DiagID) << PrevSpec;
43920b57cec5SDimitry Andric     }
43930b57cec5SDimitry Andric 
43940b57cec5SDimitry Andric     if (DiagID != diag::err_bool_redeclaration && ConsumedEnd.isInvalid())
43950b57cec5SDimitry Andric       // After an error the next token can be an annotation token.
43960b57cec5SDimitry Andric       ConsumeAnyToken();
43970b57cec5SDimitry Andric 
43980b57cec5SDimitry Andric     AttrsLastTime = false;
43990b57cec5SDimitry Andric   }
44000b57cec5SDimitry Andric }
44010b57cec5SDimitry Andric 
44020b57cec5SDimitry Andric /// ParseStructDeclaration - Parse a struct declaration without the terminating
44030b57cec5SDimitry Andric /// semicolon.
44040b57cec5SDimitry Andric ///
44050b57cec5SDimitry Andric /// Note that a struct declaration refers to a declaration in a struct,
44060b57cec5SDimitry Andric /// not to the declaration of a struct.
44070b57cec5SDimitry Andric ///
44080b57cec5SDimitry Andric ///       struct-declaration:
44090b57cec5SDimitry Andric /// [C2x]   attributes-specifier-seq[opt]
44100b57cec5SDimitry Andric ///           specifier-qualifier-list struct-declarator-list
44110b57cec5SDimitry Andric /// [GNU]   __extension__ struct-declaration
44120b57cec5SDimitry Andric /// [GNU]   specifier-qualifier-list
44130b57cec5SDimitry Andric ///       struct-declarator-list:
44140b57cec5SDimitry Andric ///         struct-declarator
44150b57cec5SDimitry Andric ///         struct-declarator-list ',' struct-declarator
44160b57cec5SDimitry Andric /// [GNU]   struct-declarator-list ',' attributes[opt] struct-declarator
44170b57cec5SDimitry Andric ///       struct-declarator:
44180b57cec5SDimitry Andric ///         declarator
44190b57cec5SDimitry Andric /// [GNU]   declarator attributes[opt]
44200b57cec5SDimitry Andric ///         declarator[opt] ':' constant-expression
44210b57cec5SDimitry Andric /// [GNU]   declarator[opt] ':' constant-expression attributes[opt]
44220b57cec5SDimitry Andric ///
44230b57cec5SDimitry Andric void Parser::ParseStructDeclaration(
44240b57cec5SDimitry Andric     ParsingDeclSpec &DS,
44250b57cec5SDimitry Andric     llvm::function_ref<void(ParsingFieldDeclarator &)> FieldsCallback) {
44260b57cec5SDimitry Andric 
44270b57cec5SDimitry Andric   if (Tok.is(tok::kw___extension__)) {
44280b57cec5SDimitry Andric     // __extension__ silences extension warnings in the subexpression.
44290b57cec5SDimitry Andric     ExtensionRAIIObject O(Diags);  // Use RAII to do this.
44300b57cec5SDimitry Andric     ConsumeToken();
44310b57cec5SDimitry Andric     return ParseStructDeclaration(DS, FieldsCallback);
44320b57cec5SDimitry Andric   }
44330b57cec5SDimitry Andric 
44340b57cec5SDimitry Andric   // Parse leading attributes.
443581ad6265SDimitry Andric   ParsedAttributes Attrs(AttrFactory);
44360b57cec5SDimitry Andric   MaybeParseCXX11Attributes(Attrs);
44370b57cec5SDimitry Andric 
44380b57cec5SDimitry Andric   // Parse the common specifier-qualifiers-list piece.
44390b57cec5SDimitry Andric   ParseSpecifierQualifierList(DS);
44400b57cec5SDimitry Andric 
44410b57cec5SDimitry Andric   // If there are no declarators, this is a free-standing declaration
44420b57cec5SDimitry Andric   // specifier. Let the actions module cope with it.
44430b57cec5SDimitry Andric   if (Tok.is(tok::semi)) {
444481ad6265SDimitry Andric     // C2x 6.7.2.1p9 : "The optional attribute specifier sequence in a
444581ad6265SDimitry Andric     // member declaration appertains to each of the members declared by the
444681ad6265SDimitry Andric     // member declarator list; it shall not appear if the optional member
444781ad6265SDimitry Andric     // declarator list is omitted."
444881ad6265SDimitry Andric     ProhibitAttributes(Attrs);
44490b57cec5SDimitry Andric     RecordDecl *AnonRecord = nullptr;
445081ad6265SDimitry Andric     Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(
445181ad6265SDimitry Andric         getCurScope(), AS_none, DS, ParsedAttributesView::none(), AnonRecord);
44520b57cec5SDimitry Andric     assert(!AnonRecord && "Did not expect anonymous struct or union here");
44530b57cec5SDimitry Andric     DS.complete(TheDecl);
44540b57cec5SDimitry Andric     return;
44550b57cec5SDimitry Andric   }
44560b57cec5SDimitry Andric 
44570b57cec5SDimitry Andric   // Read struct-declarators until we find the semicolon.
44580b57cec5SDimitry Andric   bool FirstDeclarator = true;
44590b57cec5SDimitry Andric   SourceLocation CommaLoc;
446004eeddc0SDimitry Andric   while (true) {
446181ad6265SDimitry Andric     ParsingFieldDeclarator DeclaratorInfo(*this, DS, Attrs);
44620b57cec5SDimitry Andric     DeclaratorInfo.D.setCommaLoc(CommaLoc);
44630b57cec5SDimitry Andric 
44640b57cec5SDimitry Andric     // Attributes are only allowed here on successive declarators.
4465e8d8bef9SDimitry Andric     if (!FirstDeclarator) {
4466e8d8bef9SDimitry Andric       // However, this does not apply for [[]] attributes (which could show up
4467e8d8bef9SDimitry Andric       // before or after the __attribute__ attributes).
4468e8d8bef9SDimitry Andric       DiagnoseAndSkipCXX11Attributes();
44690b57cec5SDimitry Andric       MaybeParseGNUAttributes(DeclaratorInfo.D);
4470e8d8bef9SDimitry Andric       DiagnoseAndSkipCXX11Attributes();
4471e8d8bef9SDimitry Andric     }
44720b57cec5SDimitry Andric 
44730b57cec5SDimitry Andric     /// struct-declarator: declarator
44740b57cec5SDimitry Andric     /// struct-declarator: declarator[opt] ':' constant-expression
44750b57cec5SDimitry Andric     if (Tok.isNot(tok::colon)) {
44760b57cec5SDimitry Andric       // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
44770b57cec5SDimitry Andric       ColonProtectionRAIIObject X(*this);
44780b57cec5SDimitry Andric       ParseDeclarator(DeclaratorInfo.D);
44790b57cec5SDimitry Andric     } else
44800b57cec5SDimitry Andric       DeclaratorInfo.D.SetIdentifier(nullptr, Tok.getLocation());
44810b57cec5SDimitry Andric 
44820b57cec5SDimitry Andric     if (TryConsumeToken(tok::colon)) {
44830b57cec5SDimitry Andric       ExprResult Res(ParseConstantExpression());
44840b57cec5SDimitry Andric       if (Res.isInvalid())
44850b57cec5SDimitry Andric         SkipUntil(tok::semi, StopBeforeMatch);
44860b57cec5SDimitry Andric       else
44870b57cec5SDimitry Andric         DeclaratorInfo.BitfieldSize = Res.get();
44880b57cec5SDimitry Andric     }
44890b57cec5SDimitry Andric 
44900b57cec5SDimitry Andric     // If attributes exist after the declarator, parse them.
44910b57cec5SDimitry Andric     MaybeParseGNUAttributes(DeclaratorInfo.D);
44920b57cec5SDimitry Andric 
44930b57cec5SDimitry Andric     // We're done with this declarator;  invoke the callback.
44940b57cec5SDimitry Andric     FieldsCallback(DeclaratorInfo);
44950b57cec5SDimitry Andric 
44960b57cec5SDimitry Andric     // If we don't have a comma, it is either the end of the list (a ';')
44970b57cec5SDimitry Andric     // or an error, bail out.
44980b57cec5SDimitry Andric     if (!TryConsumeToken(tok::comma, CommaLoc))
44990b57cec5SDimitry Andric       return;
45000b57cec5SDimitry Andric 
45010b57cec5SDimitry Andric     FirstDeclarator = false;
45020b57cec5SDimitry Andric   }
45030b57cec5SDimitry Andric }
45040b57cec5SDimitry Andric 
45050b57cec5SDimitry Andric /// ParseStructUnionBody
45060b57cec5SDimitry Andric ///       struct-contents:
45070b57cec5SDimitry Andric ///         struct-declaration-list
45080b57cec5SDimitry Andric /// [EXT]   empty
4509e8d8bef9SDimitry Andric /// [GNU]   "struct-declaration-list" without terminating ';'
45100b57cec5SDimitry Andric ///       struct-declaration-list:
45110b57cec5SDimitry Andric ///         struct-declaration
45120b57cec5SDimitry Andric ///         struct-declaration-list struct-declaration
45130b57cec5SDimitry Andric /// [OBC]   '@' 'defs' '(' class-name ')'
45140b57cec5SDimitry Andric ///
45150b57cec5SDimitry Andric void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
45165ffd83dbSDimitry Andric                                   DeclSpec::TST TagType, RecordDecl *TagDecl) {
45170b57cec5SDimitry Andric   PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc,
45180b57cec5SDimitry Andric                                       "parsing struct/union body");
45190b57cec5SDimitry Andric   assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
45200b57cec5SDimitry Andric 
45210b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_brace);
45220b57cec5SDimitry Andric   if (T.consumeOpen())
45230b57cec5SDimitry Andric     return;
45240b57cec5SDimitry Andric 
45250b57cec5SDimitry Andric   ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
45260b57cec5SDimitry Andric   Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
45270b57cec5SDimitry Andric 
45280b57cec5SDimitry Andric   // While we still have something to read, read the declarations in the struct.
45290b57cec5SDimitry Andric   while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
45300b57cec5SDimitry Andric          Tok.isNot(tok::eof)) {
45310b57cec5SDimitry Andric     // Each iteration of this loop reads one struct-declaration.
45320b57cec5SDimitry Andric 
45330b57cec5SDimitry Andric     // Check for extraneous top-level semicolon.
45340b57cec5SDimitry Andric     if (Tok.is(tok::semi)) {
45350b57cec5SDimitry Andric       ConsumeExtraSemi(InsideStruct, TagType);
45360b57cec5SDimitry Andric       continue;
45370b57cec5SDimitry Andric     }
45380b57cec5SDimitry Andric 
45390b57cec5SDimitry Andric     // Parse _Static_assert declaration.
4540d409305fSDimitry Andric     if (Tok.isOneOf(tok::kw__Static_assert, tok::kw_static_assert)) {
45410b57cec5SDimitry Andric       SourceLocation DeclEnd;
45420b57cec5SDimitry Andric       ParseStaticAssertDeclaration(DeclEnd);
45430b57cec5SDimitry Andric       continue;
45440b57cec5SDimitry Andric     }
45450b57cec5SDimitry Andric 
45460b57cec5SDimitry Andric     if (Tok.is(tok::annot_pragma_pack)) {
45470b57cec5SDimitry Andric       HandlePragmaPack();
45480b57cec5SDimitry Andric       continue;
45490b57cec5SDimitry Andric     }
45500b57cec5SDimitry Andric 
45510b57cec5SDimitry Andric     if (Tok.is(tok::annot_pragma_align)) {
45520b57cec5SDimitry Andric       HandlePragmaAlign();
45530b57cec5SDimitry Andric       continue;
45540b57cec5SDimitry Andric     }
45550b57cec5SDimitry Andric 
4556fe6060f1SDimitry Andric     if (Tok.isOneOf(tok::annot_pragma_openmp, tok::annot_attr_openmp)) {
45570b57cec5SDimitry Andric       // Result can be ignored, because it must be always empty.
45580b57cec5SDimitry Andric       AccessSpecifier AS = AS_none;
455981ad6265SDimitry Andric       ParsedAttributes Attrs(AttrFactory);
45600b57cec5SDimitry Andric       (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs);
45610b57cec5SDimitry Andric       continue;
45620b57cec5SDimitry Andric     }
45630b57cec5SDimitry Andric 
4564a7dea167SDimitry Andric     if (tok::isPragmaAnnotation(Tok.getKind())) {
4565a7dea167SDimitry Andric       Diag(Tok.getLocation(), diag::err_pragma_misplaced_in_decl)
4566a7dea167SDimitry Andric           << DeclSpec::getSpecifierName(
4567a7dea167SDimitry Andric                  TagType, Actions.getASTContext().getPrintingPolicy());
4568a7dea167SDimitry Andric       ConsumeAnnotationToken();
4569a7dea167SDimitry Andric       continue;
4570a7dea167SDimitry Andric     }
4571a7dea167SDimitry Andric 
45720b57cec5SDimitry Andric     if (!Tok.is(tok::at)) {
45730b57cec5SDimitry Andric       auto CFieldCallback = [&](ParsingFieldDeclarator &FD) {
45740b57cec5SDimitry Andric         // Install the declarator into the current TagDecl.
45750b57cec5SDimitry Andric         Decl *Field =
45760b57cec5SDimitry Andric             Actions.ActOnField(getCurScope(), TagDecl,
45770b57cec5SDimitry Andric                                FD.D.getDeclSpec().getSourceRange().getBegin(),
45780b57cec5SDimitry Andric                                FD.D, FD.BitfieldSize);
45790b57cec5SDimitry Andric         FD.complete(Field);
45800b57cec5SDimitry Andric       };
45810b57cec5SDimitry Andric 
45820b57cec5SDimitry Andric       // Parse all the comma separated declarators.
45830b57cec5SDimitry Andric       ParsingDeclSpec DS(*this);
45840b57cec5SDimitry Andric       ParseStructDeclaration(DS, CFieldCallback);
45850b57cec5SDimitry Andric     } else { // Handle @defs
45860b57cec5SDimitry Andric       ConsumeToken();
45870b57cec5SDimitry Andric       if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
45880b57cec5SDimitry Andric         Diag(Tok, diag::err_unexpected_at);
45890b57cec5SDimitry Andric         SkipUntil(tok::semi);
45900b57cec5SDimitry Andric         continue;
45910b57cec5SDimitry Andric       }
45920b57cec5SDimitry Andric       ConsumeToken();
45930b57cec5SDimitry Andric       ExpectAndConsume(tok::l_paren);
45940b57cec5SDimitry Andric       if (!Tok.is(tok::identifier)) {
45950b57cec5SDimitry Andric         Diag(Tok, diag::err_expected) << tok::identifier;
45960b57cec5SDimitry Andric         SkipUntil(tok::semi);
45970b57cec5SDimitry Andric         continue;
45980b57cec5SDimitry Andric       }
45990b57cec5SDimitry Andric       SmallVector<Decl *, 16> Fields;
46000b57cec5SDimitry Andric       Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
46010b57cec5SDimitry Andric                         Tok.getIdentifierInfo(), Fields);
46020b57cec5SDimitry Andric       ConsumeToken();
46030b57cec5SDimitry Andric       ExpectAndConsume(tok::r_paren);
46040b57cec5SDimitry Andric     }
46050b57cec5SDimitry Andric 
46060b57cec5SDimitry Andric     if (TryConsumeToken(tok::semi))
46070b57cec5SDimitry Andric       continue;
46080b57cec5SDimitry Andric 
46090b57cec5SDimitry Andric     if (Tok.is(tok::r_brace)) {
46100b57cec5SDimitry Andric       ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
46110b57cec5SDimitry Andric       break;
46120b57cec5SDimitry Andric     }
46130b57cec5SDimitry Andric 
46140b57cec5SDimitry Andric     ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
46150b57cec5SDimitry Andric     // Skip to end of block or statement to avoid ext-warning on extra ';'.
46160b57cec5SDimitry Andric     SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
46170b57cec5SDimitry Andric     // If we stopped at a ';', eat it.
46180b57cec5SDimitry Andric     TryConsumeToken(tok::semi);
46190b57cec5SDimitry Andric   }
46200b57cec5SDimitry Andric 
46210b57cec5SDimitry Andric   T.consumeClose();
46220b57cec5SDimitry Andric 
46230b57cec5SDimitry Andric   ParsedAttributes attrs(AttrFactory);
46240b57cec5SDimitry Andric   // If attributes exist after struct contents, parse them.
46250b57cec5SDimitry Andric   MaybeParseGNUAttributes(attrs);
46260b57cec5SDimitry Andric 
462781ad6265SDimitry Andric   SmallVector<Decl *, 32> FieldDecls(TagDecl->fields());
46285ffd83dbSDimitry Andric 
46290b57cec5SDimitry Andric   Actions.ActOnFields(getCurScope(), RecordLoc, TagDecl, FieldDecls,
46300b57cec5SDimitry Andric                       T.getOpenLocation(), T.getCloseLocation(), attrs);
46310b57cec5SDimitry Andric   StructScope.Exit();
46320b57cec5SDimitry Andric   Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange());
46330b57cec5SDimitry Andric }
46340b57cec5SDimitry Andric 
46350b57cec5SDimitry Andric /// ParseEnumSpecifier
46360b57cec5SDimitry Andric ///       enum-specifier: [C99 6.7.2.2]
46370b57cec5SDimitry Andric ///         'enum' identifier[opt] '{' enumerator-list '}'
46380b57cec5SDimitry Andric ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
46390b57cec5SDimitry Andric /// [GNU]   'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
46400b57cec5SDimitry Andric ///                                                 '}' attributes[opt]
46410b57cec5SDimitry Andric /// [MS]    'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
46420b57cec5SDimitry Andric ///                                                 '}'
46430b57cec5SDimitry Andric ///         'enum' identifier
46440b57cec5SDimitry Andric /// [GNU]   'enum' attributes[opt] identifier
46450b57cec5SDimitry Andric ///
46460b57cec5SDimitry Andric /// [C++11] enum-head '{' enumerator-list[opt] '}'
46470b57cec5SDimitry Andric /// [C++11] enum-head '{' enumerator-list ','  '}'
46480b57cec5SDimitry Andric ///
46490b57cec5SDimitry Andric ///       enum-head: [C++11]
46500b57cec5SDimitry Andric ///         enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
46510b57cec5SDimitry Andric ///         enum-key attribute-specifier-seq[opt] nested-name-specifier
46520b57cec5SDimitry Andric ///             identifier enum-base[opt]
46530b57cec5SDimitry Andric ///
46540b57cec5SDimitry Andric ///       enum-key: [C++11]
46550b57cec5SDimitry Andric ///         'enum'
46560b57cec5SDimitry Andric ///         'enum' 'class'
46570b57cec5SDimitry Andric ///         'enum' 'struct'
46580b57cec5SDimitry Andric ///
46590b57cec5SDimitry Andric ///       enum-base: [C++11]
46600b57cec5SDimitry Andric ///         ':' type-specifier-seq
46610b57cec5SDimitry Andric ///
46620b57cec5SDimitry Andric /// [C++] elaborated-type-specifier:
46635ffd83dbSDimitry Andric /// [C++]   'enum' nested-name-specifier[opt] identifier
46640b57cec5SDimitry Andric ///
46650b57cec5SDimitry Andric void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
46660b57cec5SDimitry Andric                                 const ParsedTemplateInfo &TemplateInfo,
46670b57cec5SDimitry Andric                                 AccessSpecifier AS, DeclSpecContext DSC) {
46680b57cec5SDimitry Andric   // Parse the tag portion of this.
46690b57cec5SDimitry Andric   if (Tok.is(tok::code_completion)) {
46700b57cec5SDimitry Andric     // Code completion for an enum name.
4671fe6060f1SDimitry Andric     cutOffParsing();
46720b57cec5SDimitry Andric     Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
4673*bdd1243dSDimitry Andric     DS.SetTypeSpecError(); // Needed by ActOnUsingDeclaration.
4674fe6060f1SDimitry Andric     return;
46750b57cec5SDimitry Andric   }
46760b57cec5SDimitry Andric 
46770b57cec5SDimitry Andric   // If attributes exist after tag, parse them.
467881ad6265SDimitry Andric   ParsedAttributes attrs(AttrFactory);
4679fe6060f1SDimitry Andric   MaybeParseAttributes(PAKM_GNU | PAKM_Declspec | PAKM_CXX11, attrs);
46800b57cec5SDimitry Andric 
46810b57cec5SDimitry Andric   SourceLocation ScopedEnumKWLoc;
46820b57cec5SDimitry Andric   bool IsScopedUsingClassTag = false;
46830b57cec5SDimitry Andric 
46840b57cec5SDimitry Andric   // In C++11, recognize 'enum class' and 'enum struct'.
468581ad6265SDimitry Andric   if (Tok.isOneOf(tok::kw_class, tok::kw_struct) && getLangOpts().CPlusPlus) {
46860b57cec5SDimitry Andric     Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
46870b57cec5SDimitry Andric                                         : diag::ext_scoped_enum);
46880b57cec5SDimitry Andric     IsScopedUsingClassTag = Tok.is(tok::kw_class);
46890b57cec5SDimitry Andric     ScopedEnumKWLoc = ConsumeToken();
46900b57cec5SDimitry Andric 
46910b57cec5SDimitry Andric     // Attributes are not allowed between these keywords.  Diagnose,
46920b57cec5SDimitry Andric     // but then just treat them like they appeared in the right place.
46930b57cec5SDimitry Andric     ProhibitAttributes(attrs);
46940b57cec5SDimitry Andric 
46950b57cec5SDimitry Andric     // They are allowed afterwards, though.
4696fe6060f1SDimitry Andric     MaybeParseAttributes(PAKM_GNU | PAKM_Declspec | PAKM_CXX11, attrs);
46970b57cec5SDimitry Andric   }
46980b57cec5SDimitry Andric 
46990b57cec5SDimitry Andric   // C++11 [temp.explicit]p12:
47000b57cec5SDimitry Andric   //   The usual access controls do not apply to names used to specify
47010b57cec5SDimitry Andric   //   explicit instantiations.
47020b57cec5SDimitry Andric   // We extend this to also cover explicit specializations.  Note that
47030b57cec5SDimitry Andric   // we don't suppress if this turns out to be an elaborated type
47040b57cec5SDimitry Andric   // specifier.
47050b57cec5SDimitry Andric   bool shouldDelayDiagsInTag =
47060b57cec5SDimitry Andric     (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
47070b57cec5SDimitry Andric      TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
47080b57cec5SDimitry Andric   SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
47090b57cec5SDimitry Andric 
47105ffd83dbSDimitry Andric   // Determine whether this declaration is permitted to have an enum-base.
47115ffd83dbSDimitry Andric   AllowDefiningTypeSpec AllowEnumSpecifier =
471281ad6265SDimitry Andric       isDefiningTypeSpecifierContext(DSC, getLangOpts().CPlusPlus);
47135ffd83dbSDimitry Andric   bool CanBeOpaqueEnumDeclaration =
47145ffd83dbSDimitry Andric       DS.isEmpty() && isOpaqueEnumDeclarationContext(DSC);
47155ffd83dbSDimitry Andric   bool CanHaveEnumBase = (getLangOpts().CPlusPlus11 || getLangOpts().ObjC ||
47165ffd83dbSDimitry Andric                           getLangOpts().MicrosoftExt) &&
47175ffd83dbSDimitry Andric                          (AllowEnumSpecifier == AllowDefiningTypeSpec::Yes ||
47185ffd83dbSDimitry Andric                           CanBeOpaqueEnumDeclaration);
47190b57cec5SDimitry Andric 
47200b57cec5SDimitry Andric   CXXScopeSpec &SS = DS.getTypeSpecScope();
47210b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus) {
47225ffd83dbSDimitry Andric     // "enum foo : bar;" is not a potential typo for "enum foo::bar;".
47235ffd83dbSDimitry Andric     ColonProtectionRAIIObject X(*this);
47240b57cec5SDimitry Andric 
47250b57cec5SDimitry Andric     CXXScopeSpec Spec;
47265ffd83dbSDimitry Andric     if (ParseOptionalCXXScopeSpecifier(Spec, /*ObjectType=*/nullptr,
472704eeddc0SDimitry Andric                                        /*ObjectHasErrors=*/false,
47280b57cec5SDimitry Andric                                        /*EnteringContext=*/true))
47290b57cec5SDimitry Andric       return;
47300b57cec5SDimitry Andric 
47310b57cec5SDimitry Andric     if (Spec.isSet() && Tok.isNot(tok::identifier)) {
47320b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::identifier;
4733*bdd1243dSDimitry Andric       DS.SetTypeSpecError();
47340b57cec5SDimitry Andric       if (Tok.isNot(tok::l_brace)) {
47350b57cec5SDimitry Andric         // Has no name and is not a definition.
47360b57cec5SDimitry Andric         // Skip the rest of this declarator, up until the comma or semicolon.
47370b57cec5SDimitry Andric         SkipUntil(tok::comma, StopAtSemi);
47380b57cec5SDimitry Andric         return;
47390b57cec5SDimitry Andric       }
47400b57cec5SDimitry Andric     }
47410b57cec5SDimitry Andric 
47420b57cec5SDimitry Andric     SS = Spec;
47430b57cec5SDimitry Andric   }
47440b57cec5SDimitry Andric 
47455ffd83dbSDimitry Andric   // Must have either 'enum name' or 'enum {...}' or (rarely) 'enum : T { ... }'.
47460b57cec5SDimitry Andric   if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
47475ffd83dbSDimitry Andric       Tok.isNot(tok::colon)) {
47480b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
47490b57cec5SDimitry Andric 
4750*bdd1243dSDimitry Andric     DS.SetTypeSpecError();
47510b57cec5SDimitry Andric     // Skip the rest of this declarator, up until the comma or semicolon.
47520b57cec5SDimitry Andric     SkipUntil(tok::comma, StopAtSemi);
47530b57cec5SDimitry Andric     return;
47540b57cec5SDimitry Andric   }
47550b57cec5SDimitry Andric 
47560b57cec5SDimitry Andric   // If an identifier is present, consume and remember it.
47570b57cec5SDimitry Andric   IdentifierInfo *Name = nullptr;
47580b57cec5SDimitry Andric   SourceLocation NameLoc;
47590b57cec5SDimitry Andric   if (Tok.is(tok::identifier)) {
47600b57cec5SDimitry Andric     Name = Tok.getIdentifierInfo();
47610b57cec5SDimitry Andric     NameLoc = ConsumeToken();
47620b57cec5SDimitry Andric   }
47630b57cec5SDimitry Andric 
47640b57cec5SDimitry Andric   if (!Name && ScopedEnumKWLoc.isValid()) {
47650b57cec5SDimitry Andric     // C++0x 7.2p2: The optional identifier shall not be omitted in the
47660b57cec5SDimitry Andric     // declaration of a scoped enumeration.
47670b57cec5SDimitry Andric     Diag(Tok, diag::err_scoped_enum_missing_identifier);
47680b57cec5SDimitry Andric     ScopedEnumKWLoc = SourceLocation();
47690b57cec5SDimitry Andric     IsScopedUsingClassTag = false;
47700b57cec5SDimitry Andric   }
47710b57cec5SDimitry Andric 
47720b57cec5SDimitry Andric   // Okay, end the suppression area.  We'll decide whether to emit the
47730b57cec5SDimitry Andric   // diagnostics in a second.
47740b57cec5SDimitry Andric   if (shouldDelayDiagsInTag)
47750b57cec5SDimitry Andric     diagsFromTag.done();
47760b57cec5SDimitry Andric 
47770b57cec5SDimitry Andric   TypeResult BaseType;
47785ffd83dbSDimitry Andric   SourceRange BaseRange;
47795ffd83dbSDimitry Andric 
478081ad6265SDimitry Andric   bool CanBeBitfield =
478181ad6265SDimitry Andric       getCurScope()->isClassScope() && ScopedEnumKWLoc.isInvalid() && Name;
47820b57cec5SDimitry Andric 
47830b57cec5SDimitry Andric   // Parse the fixed underlying type.
47845ffd83dbSDimitry Andric   if (Tok.is(tok::colon)) {
47855ffd83dbSDimitry Andric     // This might be an enum-base or part of some unrelated enclosing context.
47865ffd83dbSDimitry Andric     //
47875ffd83dbSDimitry Andric     // 'enum E : base' is permitted in two circumstances:
47885ffd83dbSDimitry Andric     //
47895ffd83dbSDimitry Andric     // 1) As a defining-type-specifier, when followed by '{'.
47905ffd83dbSDimitry Andric     // 2) As the sole constituent of a complete declaration -- when DS is empty
47915ffd83dbSDimitry Andric     //    and the next token is ';'.
47925ffd83dbSDimitry Andric     //
47935ffd83dbSDimitry Andric     // The restriction to defining-type-specifiers is important to allow parsing
47945ffd83dbSDimitry Andric     //   a ? new enum E : int{}
47955ffd83dbSDimitry Andric     //   _Generic(a, enum E : int{})
47965ffd83dbSDimitry Andric     // properly.
47975ffd83dbSDimitry Andric     //
47985ffd83dbSDimitry Andric     // One additional consideration applies:
47995ffd83dbSDimitry Andric     //
48005ffd83dbSDimitry Andric     // C++ [dcl.enum]p1:
48015ffd83dbSDimitry Andric     //   A ':' following "enum nested-name-specifier[opt] identifier" within
48025ffd83dbSDimitry Andric     //   the decl-specifier-seq of a member-declaration is parsed as part of
48035ffd83dbSDimitry Andric     //   an enum-base.
48045ffd83dbSDimitry Andric     //
48055ffd83dbSDimitry Andric     // Other language modes supporting enumerations with fixed underlying types
48065ffd83dbSDimitry Andric     // do not have clear rules on this, so we disambiguate to determine whether
48075ffd83dbSDimitry Andric     // the tokens form a bit-field width or an enum-base.
48080b57cec5SDimitry Andric 
48095ffd83dbSDimitry Andric     if (CanBeBitfield && !isEnumBase(CanBeOpaqueEnumDeclaration)) {
48105ffd83dbSDimitry Andric       // Outside C++11, do not interpret the tokens as an enum-base if they do
48115ffd83dbSDimitry Andric       // not make sense as one. In C++11, it's an error if this happens.
48125ffd83dbSDimitry Andric       if (getLangOpts().CPlusPlus11)
48135ffd83dbSDimitry Andric         Diag(Tok.getLocation(), diag::err_anonymous_enum_bitfield);
48145ffd83dbSDimitry Andric     } else if (CanHaveEnumBase || !ColonIsSacred) {
48155ffd83dbSDimitry Andric       SourceLocation ColonLoc = ConsumeToken();
48160b57cec5SDimitry Andric 
48175ffd83dbSDimitry Andric       // Parse a type-specifier-seq as a type. We can't just ParseTypeName here,
48185ffd83dbSDimitry Andric       // because under -fms-extensions,
48195ffd83dbSDimitry Andric       //   enum E : int *p;
48205ffd83dbSDimitry Andric       // declares 'enum E : int; E *p;' not 'enum E : int*; E p;'.
48215ffd83dbSDimitry Andric       DeclSpec DS(AttrFactory);
4822*bdd1243dSDimitry Andric       // enum-base is not assumed to be a type and therefore requires the
4823*bdd1243dSDimitry Andric       // typename keyword [p0634r3].
4824*bdd1243dSDimitry Andric       ParseSpecifierQualifierList(DS, ImplicitTypenameContext::No, AS,
4825*bdd1243dSDimitry Andric                                   DeclSpecContext::DSC_type_specifier);
482681ad6265SDimitry Andric       Declarator DeclaratorInfo(DS, ParsedAttributesView::none(),
482781ad6265SDimitry Andric                                 DeclaratorContext::TypeName);
48285ffd83dbSDimitry Andric       BaseType = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
48290b57cec5SDimitry Andric 
48305ffd83dbSDimitry Andric       BaseRange = SourceRange(ColonLoc, DeclaratorInfo.getSourceRange().getEnd());
48310b57cec5SDimitry Andric 
48320b57cec5SDimitry Andric       if (!getLangOpts().ObjC) {
48330b57cec5SDimitry Andric         if (getLangOpts().CPlusPlus11)
48345ffd83dbSDimitry Andric           Diag(ColonLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type)
48355ffd83dbSDimitry Andric               << BaseRange;
48360b57cec5SDimitry Andric         else if (getLangOpts().CPlusPlus)
48375ffd83dbSDimitry Andric           Diag(ColonLoc, diag::ext_cxx11_enum_fixed_underlying_type)
48385ffd83dbSDimitry Andric               << BaseRange;
48390b57cec5SDimitry Andric         else if (getLangOpts().MicrosoftExt)
48405ffd83dbSDimitry Andric           Diag(ColonLoc, diag::ext_ms_c_enum_fixed_underlying_type)
48415ffd83dbSDimitry Andric               << BaseRange;
48420b57cec5SDimitry Andric         else
48435ffd83dbSDimitry Andric           Diag(ColonLoc, diag::ext_clang_c_enum_fixed_underlying_type)
48445ffd83dbSDimitry Andric               << BaseRange;
48450b57cec5SDimitry Andric       }
48460b57cec5SDimitry Andric     }
48470b57cec5SDimitry Andric   }
48480b57cec5SDimitry Andric 
48490b57cec5SDimitry Andric   // There are four options here.  If we have 'friend enum foo;' then this is a
48500b57cec5SDimitry Andric   // friend declaration, and cannot have an accompanying definition. If we have
48510b57cec5SDimitry Andric   // 'enum foo;', then this is a forward declaration.  If we have
48520b57cec5SDimitry Andric   // 'enum foo {...' then this is a definition. Otherwise we have something
48530b57cec5SDimitry Andric   // like 'enum foo xyz', a reference.
48540b57cec5SDimitry Andric   //
48550b57cec5SDimitry Andric   // This is needed to handle stuff like this right (C99 6.7.2.3p11):
48560b57cec5SDimitry Andric   // enum foo {..};  void bar() { enum foo; }    <- new foo in bar.
48570b57cec5SDimitry Andric   // enum foo {..};  void bar() { enum foo x; }  <- use of old foo.
48580b57cec5SDimitry Andric   //
48590b57cec5SDimitry Andric   Sema::TagUseKind TUK;
48605ffd83dbSDimitry Andric   if (AllowEnumSpecifier == AllowDefiningTypeSpec::No)
48610b57cec5SDimitry Andric     TUK = Sema::TUK_Reference;
48625ffd83dbSDimitry Andric   else if (Tok.is(tok::l_brace)) {
48630b57cec5SDimitry Andric     if (DS.isFriendSpecified()) {
48640b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
48650b57cec5SDimitry Andric         << SourceRange(DS.getFriendSpecLoc());
48660b57cec5SDimitry Andric       ConsumeBrace();
48670b57cec5SDimitry Andric       SkipUntil(tok::r_brace, StopAtSemi);
48685ffd83dbSDimitry Andric       // Discard any other definition-only pieces.
48695ffd83dbSDimitry Andric       attrs.clear();
48705ffd83dbSDimitry Andric       ScopedEnumKWLoc = SourceLocation();
48715ffd83dbSDimitry Andric       IsScopedUsingClassTag = false;
48725ffd83dbSDimitry Andric       BaseType = TypeResult();
48730b57cec5SDimitry Andric       TUK = Sema::TUK_Friend;
48740b57cec5SDimitry Andric     } else {
48750b57cec5SDimitry Andric       TUK = Sema::TUK_Definition;
48760b57cec5SDimitry Andric     }
48770b57cec5SDimitry Andric   } else if (!isTypeSpecifier(DSC) &&
48780b57cec5SDimitry Andric              (Tok.is(tok::semi) ||
48790b57cec5SDimitry Andric               (Tok.isAtStartOfLine() &&
48800b57cec5SDimitry Andric                !isValidAfterTypeSpecifier(CanBeBitfield)))) {
48815ffd83dbSDimitry Andric     // An opaque-enum-declaration is required to be standalone (no preceding or
48825ffd83dbSDimitry Andric     // following tokens in the declaration). Sema enforces this separately by
48835ffd83dbSDimitry Andric     // diagnosing anything else in the DeclSpec.
48840b57cec5SDimitry Andric     TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
48850b57cec5SDimitry Andric     if (Tok.isNot(tok::semi)) {
48860b57cec5SDimitry Andric       // A semicolon was missing after this declaration. Diagnose and recover.
48870b57cec5SDimitry Andric       ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
48880b57cec5SDimitry Andric       PP.EnterToken(Tok, /*IsReinject=*/true);
48890b57cec5SDimitry Andric       Tok.setKind(tok::semi);
48900b57cec5SDimitry Andric     }
48910b57cec5SDimitry Andric   } else {
48920b57cec5SDimitry Andric     TUK = Sema::TUK_Reference;
48930b57cec5SDimitry Andric   }
48940b57cec5SDimitry Andric 
48955ffd83dbSDimitry Andric   bool IsElaboratedTypeSpecifier =
48965ffd83dbSDimitry Andric       TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend;
48975ffd83dbSDimitry Andric 
48985ffd83dbSDimitry Andric   // If this is an elaborated type specifier nested in a larger declaration,
48995ffd83dbSDimitry Andric   // and we delayed diagnostics before, just merge them into the current pool.
49000b57cec5SDimitry Andric   if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
49010b57cec5SDimitry Andric     diagsFromTag.redelay();
49020b57cec5SDimitry Andric   }
49030b57cec5SDimitry Andric 
49040b57cec5SDimitry Andric   MultiTemplateParamsArg TParams;
49050b57cec5SDimitry Andric   if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
49060b57cec5SDimitry Andric       TUK != Sema::TUK_Reference) {
49070b57cec5SDimitry Andric     if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
49080b57cec5SDimitry Andric       // Skip the rest of this declarator, up until the comma or semicolon.
49090b57cec5SDimitry Andric       Diag(Tok, diag::err_enum_template);
49100b57cec5SDimitry Andric       SkipUntil(tok::comma, StopAtSemi);
49110b57cec5SDimitry Andric       return;
49120b57cec5SDimitry Andric     }
49130b57cec5SDimitry Andric 
49140b57cec5SDimitry Andric     if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
49150b57cec5SDimitry Andric       // Enumerations can't be explicitly instantiated.
49160b57cec5SDimitry Andric       DS.SetTypeSpecError();
49170b57cec5SDimitry Andric       Diag(StartLoc, diag::err_explicit_instantiation_enum);
49180b57cec5SDimitry Andric       return;
49190b57cec5SDimitry Andric     }
49200b57cec5SDimitry Andric 
49210b57cec5SDimitry Andric     assert(TemplateInfo.TemplateParams && "no template parameters");
49220b57cec5SDimitry Andric     TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
49230b57cec5SDimitry Andric                                      TemplateInfo.TemplateParams->size());
49240b57cec5SDimitry Andric   }
49250b57cec5SDimitry Andric 
49260b57cec5SDimitry Andric   if (!Name && TUK != Sema::TUK_Definition) {
49270b57cec5SDimitry Andric     Diag(Tok, diag::err_enumerator_unnamed_no_def);
49280b57cec5SDimitry Andric 
4929*bdd1243dSDimitry Andric     DS.SetTypeSpecError();
49300b57cec5SDimitry Andric     // Skip the rest of this declarator, up until the comma or semicolon.
49310b57cec5SDimitry Andric     SkipUntil(tok::comma, StopAtSemi);
49320b57cec5SDimitry Andric     return;
49330b57cec5SDimitry Andric   }
49340b57cec5SDimitry Andric 
49355ffd83dbSDimitry Andric   // An elaborated-type-specifier has a much more constrained grammar:
49365ffd83dbSDimitry Andric   //
49375ffd83dbSDimitry Andric   //   'enum' nested-name-specifier[opt] identifier
49385ffd83dbSDimitry Andric   //
49395ffd83dbSDimitry Andric   // If we parsed any other bits, reject them now.
49405ffd83dbSDimitry Andric   //
49415ffd83dbSDimitry Andric   // MSVC and (for now at least) Objective-C permit a full enum-specifier
49425ffd83dbSDimitry Andric   // or opaque-enum-declaration anywhere.
49435ffd83dbSDimitry Andric   if (IsElaboratedTypeSpecifier && !getLangOpts().MicrosoftExt &&
49445ffd83dbSDimitry Andric       !getLangOpts().ObjC) {
4945fe6060f1SDimitry Andric     ProhibitCXX11Attributes(attrs, diag::err_attributes_not_allowed,
4946fe6060f1SDimitry Andric                             /*DiagnoseEmptyAttrs=*/true);
49475ffd83dbSDimitry Andric     if (BaseType.isUsable())
49485ffd83dbSDimitry Andric       Diag(BaseRange.getBegin(), diag::ext_enum_base_in_type_specifier)
49495ffd83dbSDimitry Andric           << (AllowEnumSpecifier == AllowDefiningTypeSpec::Yes) << BaseRange;
49505ffd83dbSDimitry Andric     else if (ScopedEnumKWLoc.isValid())
49515ffd83dbSDimitry Andric       Diag(ScopedEnumKWLoc, diag::ext_elaborated_enum_class)
49525ffd83dbSDimitry Andric         << FixItHint::CreateRemoval(ScopedEnumKWLoc) << IsScopedUsingClassTag;
49535ffd83dbSDimitry Andric   }
49545ffd83dbSDimitry Andric 
49550b57cec5SDimitry Andric   stripTypeAttributesOffDeclSpec(attrs, DS, TUK);
49560b57cec5SDimitry Andric 
49570b57cec5SDimitry Andric   Sema::SkipBodyInfo SkipBody;
49580b57cec5SDimitry Andric   if (!Name && TUK == Sema::TUK_Definition && Tok.is(tok::l_brace) &&
49590b57cec5SDimitry Andric       NextToken().is(tok::identifier))
49600b57cec5SDimitry Andric     SkipBody = Actions.shouldSkipAnonEnumBody(getCurScope(),
49610b57cec5SDimitry Andric                                               NextToken().getIdentifierInfo(),
49620b57cec5SDimitry Andric                                               NextToken().getLocation());
49630b57cec5SDimitry Andric 
49640b57cec5SDimitry Andric   bool Owned = false;
49650b57cec5SDimitry Andric   bool IsDependent = false;
49660b57cec5SDimitry Andric   const char *PrevSpec = nullptr;
49670b57cec5SDimitry Andric   unsigned DiagID;
4968*bdd1243dSDimitry Andric   UsingShadowDecl* FoundUsing = nullptr;
4969*bdd1243dSDimitry Andric   Decl *TagDecl =
4970*bdd1243dSDimitry Andric       Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK, StartLoc, SS,
4971*bdd1243dSDimitry Andric                     Name, NameLoc, attrs, AS, DS.getModulePrivateSpecLoc(),
4972*bdd1243dSDimitry Andric                     TParams, Owned, IsDependent, ScopedEnumKWLoc,
4973*bdd1243dSDimitry Andric                     IsScopedUsingClassTag,
4974*bdd1243dSDimitry Andric                     BaseType, DSC == DeclSpecContext::DSC_type_specifier,
49750b57cec5SDimitry Andric                     DSC == DeclSpecContext::DSC_template_param ||
49760b57cec5SDimitry Andric                         DSC == DeclSpecContext::DSC_template_type_arg,
4977*bdd1243dSDimitry Andric                     OffsetOfState, FoundUsing, &SkipBody).get();
49780b57cec5SDimitry Andric 
49790b57cec5SDimitry Andric   if (SkipBody.ShouldSkip) {
49800b57cec5SDimitry Andric     assert(TUK == Sema::TUK_Definition && "can only skip a definition");
49810b57cec5SDimitry Andric 
49820b57cec5SDimitry Andric     BalancedDelimiterTracker T(*this, tok::l_brace);
49830b57cec5SDimitry Andric     T.consumeOpen();
49840b57cec5SDimitry Andric     T.skipToEnd();
49850b57cec5SDimitry Andric 
49860b57cec5SDimitry Andric     if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
4987*bdd1243dSDimitry Andric                            NameLoc.isValid() ? NameLoc : StartLoc, PrevSpec,
4988*bdd1243dSDimitry Andric                            DiagID, FoundUsing ? FoundUsing : TagDecl, Owned,
49890b57cec5SDimitry Andric                            Actions.getASTContext().getPrintingPolicy()))
49900b57cec5SDimitry Andric       Diag(StartLoc, DiagID) << PrevSpec;
49910b57cec5SDimitry Andric     return;
49920b57cec5SDimitry Andric   }
49930b57cec5SDimitry Andric 
49940b57cec5SDimitry Andric   if (IsDependent) {
49950b57cec5SDimitry Andric     // This enum has a dependent nested-name-specifier. Handle it as a
49960b57cec5SDimitry Andric     // dependent tag.
49970b57cec5SDimitry Andric     if (!Name) {
49980b57cec5SDimitry Andric       DS.SetTypeSpecError();
49990b57cec5SDimitry Andric       Diag(Tok, diag::err_expected_type_name_after_typename);
50000b57cec5SDimitry Andric       return;
50010b57cec5SDimitry Andric     }
50020b57cec5SDimitry Andric 
50030b57cec5SDimitry Andric     TypeResult Type = Actions.ActOnDependentTag(
50040b57cec5SDimitry Andric         getCurScope(), DeclSpec::TST_enum, TUK, SS, Name, StartLoc, NameLoc);
50050b57cec5SDimitry Andric     if (Type.isInvalid()) {
50060b57cec5SDimitry Andric       DS.SetTypeSpecError();
50070b57cec5SDimitry Andric       return;
50080b57cec5SDimitry Andric     }
50090b57cec5SDimitry Andric 
50100b57cec5SDimitry Andric     if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
50110b57cec5SDimitry Andric                            NameLoc.isValid() ? NameLoc : StartLoc,
50120b57cec5SDimitry Andric                            PrevSpec, DiagID, Type.get(),
50130b57cec5SDimitry Andric                            Actions.getASTContext().getPrintingPolicy()))
50140b57cec5SDimitry Andric       Diag(StartLoc, DiagID) << PrevSpec;
50150b57cec5SDimitry Andric 
50160b57cec5SDimitry Andric     return;
50170b57cec5SDimitry Andric   }
50180b57cec5SDimitry Andric 
50190b57cec5SDimitry Andric   if (!TagDecl) {
50200b57cec5SDimitry Andric     // The action failed to produce an enumeration tag. If this is a
50210b57cec5SDimitry Andric     // definition, consume the entire definition.
50220b57cec5SDimitry Andric     if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
50230b57cec5SDimitry Andric       ConsumeBrace();
50240b57cec5SDimitry Andric       SkipUntil(tok::r_brace, StopAtSemi);
50250b57cec5SDimitry Andric     }
50260b57cec5SDimitry Andric 
50270b57cec5SDimitry Andric     DS.SetTypeSpecError();
50280b57cec5SDimitry Andric     return;
50290b57cec5SDimitry Andric   }
50300b57cec5SDimitry Andric 
50315ffd83dbSDimitry Andric   if (Tok.is(tok::l_brace) && TUK == Sema::TUK_Definition) {
50320b57cec5SDimitry Andric     Decl *D = SkipBody.CheckSameAsPrevious ? SkipBody.New : TagDecl;
50330b57cec5SDimitry Andric     ParseEnumBody(StartLoc, D);
50340b57cec5SDimitry Andric     if (SkipBody.CheckSameAsPrevious &&
503581ad6265SDimitry Andric         !Actions.ActOnDuplicateDefinition(TagDecl, SkipBody)) {
50360b57cec5SDimitry Andric       DS.SetTypeSpecError();
50370b57cec5SDimitry Andric       return;
50380b57cec5SDimitry Andric     }
50390b57cec5SDimitry Andric   }
50400b57cec5SDimitry Andric 
50410b57cec5SDimitry Andric   if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
5042*bdd1243dSDimitry Andric                          NameLoc.isValid() ? NameLoc : StartLoc, PrevSpec,
5043*bdd1243dSDimitry Andric                          DiagID, FoundUsing ? FoundUsing : TagDecl, Owned,
50440b57cec5SDimitry Andric                          Actions.getASTContext().getPrintingPolicy()))
50450b57cec5SDimitry Andric     Diag(StartLoc, DiagID) << PrevSpec;
50460b57cec5SDimitry Andric }
50470b57cec5SDimitry Andric 
50480b57cec5SDimitry Andric /// ParseEnumBody - Parse a {} enclosed enumerator-list.
50490b57cec5SDimitry Andric ///       enumerator-list:
50500b57cec5SDimitry Andric ///         enumerator
50510b57cec5SDimitry Andric ///         enumerator-list ',' enumerator
50520b57cec5SDimitry Andric ///       enumerator:
50530b57cec5SDimitry Andric ///         enumeration-constant attributes[opt]
50540b57cec5SDimitry Andric ///         enumeration-constant attributes[opt] '=' constant-expression
50550b57cec5SDimitry Andric ///       enumeration-constant:
50560b57cec5SDimitry Andric ///         identifier
50570b57cec5SDimitry Andric ///
50580b57cec5SDimitry Andric void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
50590b57cec5SDimitry Andric   // Enter the scope of the enum body and start the definition.
50600b57cec5SDimitry Andric   ParseScope EnumScope(this, Scope::DeclScope | Scope::EnumScope);
50610b57cec5SDimitry Andric   Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
50620b57cec5SDimitry Andric 
50630b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_brace);
50640b57cec5SDimitry Andric   T.consumeOpen();
50650b57cec5SDimitry Andric 
50660b57cec5SDimitry Andric   // C does not allow an empty enumerator-list, C++ does [dcl.enum].
50670b57cec5SDimitry Andric   if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
50680b57cec5SDimitry Andric     Diag(Tok, diag::err_empty_enum);
50690b57cec5SDimitry Andric 
50700b57cec5SDimitry Andric   SmallVector<Decl *, 32> EnumConstantDecls;
50710b57cec5SDimitry Andric   SmallVector<SuppressAccessChecks, 32> EnumAvailabilityDiags;
50720b57cec5SDimitry Andric 
50730b57cec5SDimitry Andric   Decl *LastEnumConstDecl = nullptr;
50740b57cec5SDimitry Andric 
50750b57cec5SDimitry Andric   // Parse the enumerator-list.
50760b57cec5SDimitry Andric   while (Tok.isNot(tok::r_brace)) {
50770b57cec5SDimitry Andric     // Parse enumerator. If failed, try skipping till the start of the next
50780b57cec5SDimitry Andric     // enumerator definition.
50790b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
50800b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
50810b57cec5SDimitry Andric       if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) &&
50820b57cec5SDimitry Andric           TryConsumeToken(tok::comma))
50830b57cec5SDimitry Andric         continue;
50840b57cec5SDimitry Andric       break;
50850b57cec5SDimitry Andric     }
50860b57cec5SDimitry Andric     IdentifierInfo *Ident = Tok.getIdentifierInfo();
50870b57cec5SDimitry Andric     SourceLocation IdentLoc = ConsumeToken();
50880b57cec5SDimitry Andric 
50890b57cec5SDimitry Andric     // If attributes exist after the enumerator, parse them.
509081ad6265SDimitry Andric     ParsedAttributes attrs(AttrFactory);
50910b57cec5SDimitry Andric     MaybeParseGNUAttributes(attrs);
50920b57cec5SDimitry Andric     if (standardAttributesAllowed() && isCXX11AttributeSpecifier()) {
50930b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus)
50940b57cec5SDimitry Andric         Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
50950b57cec5SDimitry Andric                                     ? diag::warn_cxx14_compat_ns_enum_attribute
50960b57cec5SDimitry Andric                                     : diag::ext_ns_enum_attribute)
50970b57cec5SDimitry Andric             << 1 /*enumerator*/;
50980b57cec5SDimitry Andric       ParseCXX11Attributes(attrs);
50990b57cec5SDimitry Andric     }
51000b57cec5SDimitry Andric 
51010b57cec5SDimitry Andric     SourceLocation EqualLoc;
51020b57cec5SDimitry Andric     ExprResult AssignedVal;
51030b57cec5SDimitry Andric     EnumAvailabilityDiags.emplace_back(*this);
51040b57cec5SDimitry Andric 
5105a7dea167SDimitry Andric     EnterExpressionEvaluationContext ConstantEvaluated(
5106a7dea167SDimitry Andric         Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
51070b57cec5SDimitry Andric     if (TryConsumeToken(tok::equal, EqualLoc)) {
5108a7dea167SDimitry Andric       AssignedVal = ParseConstantExpressionInExprEvalContext();
51090b57cec5SDimitry Andric       if (AssignedVal.isInvalid())
51100b57cec5SDimitry Andric         SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch);
51110b57cec5SDimitry Andric     }
51120b57cec5SDimitry Andric 
51130b57cec5SDimitry Andric     // Install the enumerator constant into EnumDecl.
51140b57cec5SDimitry Andric     Decl *EnumConstDecl = Actions.ActOnEnumConstant(
51150b57cec5SDimitry Andric         getCurScope(), EnumDecl, LastEnumConstDecl, IdentLoc, Ident, attrs,
51160b57cec5SDimitry Andric         EqualLoc, AssignedVal.get());
51170b57cec5SDimitry Andric     EnumAvailabilityDiags.back().done();
51180b57cec5SDimitry Andric 
51190b57cec5SDimitry Andric     EnumConstantDecls.push_back(EnumConstDecl);
51200b57cec5SDimitry Andric     LastEnumConstDecl = EnumConstDecl;
51210b57cec5SDimitry Andric 
51220b57cec5SDimitry Andric     if (Tok.is(tok::identifier)) {
51230b57cec5SDimitry Andric       // We're missing a comma between enumerators.
51240b57cec5SDimitry Andric       SourceLocation Loc = getEndOfPreviousToken();
51250b57cec5SDimitry Andric       Diag(Loc, diag::err_enumerator_list_missing_comma)
51260b57cec5SDimitry Andric         << FixItHint::CreateInsertion(Loc, ", ");
51270b57cec5SDimitry Andric       continue;
51280b57cec5SDimitry Andric     }
51290b57cec5SDimitry Andric 
51300b57cec5SDimitry Andric     // Emumerator definition must be finished, only comma or r_brace are
51310b57cec5SDimitry Andric     // allowed here.
51320b57cec5SDimitry Andric     SourceLocation CommaLoc;
51330b57cec5SDimitry Andric     if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) {
51340b57cec5SDimitry Andric       if (EqualLoc.isValid())
51350b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace
51360b57cec5SDimitry Andric                                                            << tok::comma;
51370b57cec5SDimitry Andric       else
51380b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator);
51390b57cec5SDimitry Andric       if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) {
51400b57cec5SDimitry Andric         if (TryConsumeToken(tok::comma, CommaLoc))
51410b57cec5SDimitry Andric           continue;
51420b57cec5SDimitry Andric       } else {
51430b57cec5SDimitry Andric         break;
51440b57cec5SDimitry Andric       }
51450b57cec5SDimitry Andric     }
51460b57cec5SDimitry Andric 
51470b57cec5SDimitry Andric     // If comma is followed by r_brace, emit appropriate warning.
51480b57cec5SDimitry Andric     if (Tok.is(tok::r_brace) && CommaLoc.isValid()) {
51490b57cec5SDimitry Andric       if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
51500b57cec5SDimitry Andric         Diag(CommaLoc, getLangOpts().CPlusPlus ?
51510b57cec5SDimitry Andric                diag::ext_enumerator_list_comma_cxx :
51520b57cec5SDimitry Andric                diag::ext_enumerator_list_comma_c)
51530b57cec5SDimitry Andric           << FixItHint::CreateRemoval(CommaLoc);
51540b57cec5SDimitry Andric       else if (getLangOpts().CPlusPlus11)
51550b57cec5SDimitry Andric         Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
51560b57cec5SDimitry Andric           << FixItHint::CreateRemoval(CommaLoc);
51570b57cec5SDimitry Andric       break;
51580b57cec5SDimitry Andric     }
51590b57cec5SDimitry Andric   }
51600b57cec5SDimitry Andric 
51610b57cec5SDimitry Andric   // Eat the }.
51620b57cec5SDimitry Andric   T.consumeClose();
51630b57cec5SDimitry Andric 
51640b57cec5SDimitry Andric   // If attributes exist after the identifier list, parse them.
51650b57cec5SDimitry Andric   ParsedAttributes attrs(AttrFactory);
51660b57cec5SDimitry Andric   MaybeParseGNUAttributes(attrs);
51670b57cec5SDimitry Andric 
51680b57cec5SDimitry Andric   Actions.ActOnEnumBody(StartLoc, T.getRange(), EnumDecl, EnumConstantDecls,
51690b57cec5SDimitry Andric                         getCurScope(), attrs);
51700b57cec5SDimitry Andric 
51710b57cec5SDimitry Andric   // Now handle enum constant availability diagnostics.
51720b57cec5SDimitry Andric   assert(EnumConstantDecls.size() == EnumAvailabilityDiags.size());
51730b57cec5SDimitry Andric   for (size_t i = 0, e = EnumConstantDecls.size(); i != e; ++i) {
51740b57cec5SDimitry Andric     ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
51750b57cec5SDimitry Andric     EnumAvailabilityDiags[i].redelay();
51760b57cec5SDimitry Andric     PD.complete(EnumConstantDecls[i]);
51770b57cec5SDimitry Andric   }
51780b57cec5SDimitry Andric 
51790b57cec5SDimitry Andric   EnumScope.Exit();
51800b57cec5SDimitry Andric   Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, T.getRange());
51810b57cec5SDimitry Andric 
51820b57cec5SDimitry Andric   // The next token must be valid after an enum definition. If not, a ';'
51830b57cec5SDimitry Andric   // was probably forgotten.
518481ad6265SDimitry Andric   bool CanBeBitfield = getCurScope()->isClassScope();
51850b57cec5SDimitry Andric   if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
51860b57cec5SDimitry Andric     ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
51870b57cec5SDimitry Andric     // Push this token back into the preprocessor and change our current token
51880b57cec5SDimitry Andric     // to ';' so that the rest of the code recovers as though there were an
51890b57cec5SDimitry Andric     // ';' after the definition.
51900b57cec5SDimitry Andric     PP.EnterToken(Tok, /*IsReinject=*/true);
51910b57cec5SDimitry Andric     Tok.setKind(tok::semi);
51920b57cec5SDimitry Andric   }
51930b57cec5SDimitry Andric }
51940b57cec5SDimitry Andric 
51950b57cec5SDimitry Andric /// isKnownToBeTypeSpecifier - Return true if we know that the specified token
51960b57cec5SDimitry Andric /// is definitely a type-specifier.  Return false if it isn't part of a type
51970b57cec5SDimitry Andric /// specifier or if we're not sure.
51980b57cec5SDimitry Andric bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
51990b57cec5SDimitry Andric   switch (Tok.getKind()) {
52000b57cec5SDimitry Andric   default: return false;
52010b57cec5SDimitry Andric     // type-specifiers
52020b57cec5SDimitry Andric   case tok::kw_short:
52030b57cec5SDimitry Andric   case tok::kw_long:
52040b57cec5SDimitry Andric   case tok::kw___int64:
52050b57cec5SDimitry Andric   case tok::kw___int128:
52060b57cec5SDimitry Andric   case tok::kw_signed:
52070b57cec5SDimitry Andric   case tok::kw_unsigned:
52080b57cec5SDimitry Andric   case tok::kw__Complex:
52090b57cec5SDimitry Andric   case tok::kw__Imaginary:
52100b57cec5SDimitry Andric   case tok::kw_void:
52110b57cec5SDimitry Andric   case tok::kw_char:
52120b57cec5SDimitry Andric   case tok::kw_wchar_t:
52130b57cec5SDimitry Andric   case tok::kw_char8_t:
52140b57cec5SDimitry Andric   case tok::kw_char16_t:
52150b57cec5SDimitry Andric   case tok::kw_char32_t:
52160b57cec5SDimitry Andric   case tok::kw_int:
52175ffd83dbSDimitry Andric   case tok::kw__ExtInt:
52180eae32dcSDimitry Andric   case tok::kw__BitInt:
52195ffd83dbSDimitry Andric   case tok::kw___bf16:
52200b57cec5SDimitry Andric   case tok::kw_half:
52210b57cec5SDimitry Andric   case tok::kw_float:
52220b57cec5SDimitry Andric   case tok::kw_double:
52230b57cec5SDimitry Andric   case tok::kw__Accum:
52240b57cec5SDimitry Andric   case tok::kw__Fract:
52250b57cec5SDimitry Andric   case tok::kw__Float16:
52260b57cec5SDimitry Andric   case tok::kw___float128:
5227349cc55cSDimitry Andric   case tok::kw___ibm128:
52280b57cec5SDimitry Andric   case tok::kw_bool:
52290b57cec5SDimitry Andric   case tok::kw__Bool:
52300b57cec5SDimitry Andric   case tok::kw__Decimal32:
52310b57cec5SDimitry Andric   case tok::kw__Decimal64:
52320b57cec5SDimitry Andric   case tok::kw__Decimal128:
52330b57cec5SDimitry Andric   case tok::kw___vector:
52340b57cec5SDimitry Andric #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
52350b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def"
52360b57cec5SDimitry Andric 
52370b57cec5SDimitry Andric     // struct-or-union-specifier (C99) or class-specifier (C++)
52380b57cec5SDimitry Andric   case tok::kw_class:
52390b57cec5SDimitry Andric   case tok::kw_struct:
52400b57cec5SDimitry Andric   case tok::kw___interface:
52410b57cec5SDimitry Andric   case tok::kw_union:
52420b57cec5SDimitry Andric     // enum-specifier
52430b57cec5SDimitry Andric   case tok::kw_enum:
52440b57cec5SDimitry Andric 
52450b57cec5SDimitry Andric     // typedef-name
52460b57cec5SDimitry Andric   case tok::annot_typename:
52470b57cec5SDimitry Andric     return true;
52480b57cec5SDimitry Andric   }
52490b57cec5SDimitry Andric }
52500b57cec5SDimitry Andric 
52510b57cec5SDimitry Andric /// isTypeSpecifierQualifier - Return true if the current token could be the
52520b57cec5SDimitry Andric /// start of a specifier-qualifier-list.
52530b57cec5SDimitry Andric bool Parser::isTypeSpecifierQualifier() {
52540b57cec5SDimitry Andric   switch (Tok.getKind()) {
52550b57cec5SDimitry Andric   default: return false;
52560b57cec5SDimitry Andric 
52570b57cec5SDimitry Andric   case tok::identifier:   // foo::bar
52580b57cec5SDimitry Andric     if (TryAltiVecVectorToken())
52590b57cec5SDimitry Andric       return true;
5260*bdd1243dSDimitry Andric     [[fallthrough]];
52610b57cec5SDimitry Andric   case tok::kw_typename:  // typename T::type
52620b57cec5SDimitry Andric     // Annotate typenames and C++ scope specifiers.  If we get one, just
52630b57cec5SDimitry Andric     // recurse to handle whatever we get.
52640b57cec5SDimitry Andric     if (TryAnnotateTypeOrScopeToken())
52650b57cec5SDimitry Andric       return true;
52660b57cec5SDimitry Andric     if (Tok.is(tok::identifier))
52670b57cec5SDimitry Andric       return false;
52680b57cec5SDimitry Andric     return isTypeSpecifierQualifier();
52690b57cec5SDimitry Andric 
52700b57cec5SDimitry Andric   case tok::coloncolon:   // ::foo::bar
52710b57cec5SDimitry Andric     if (NextToken().is(tok::kw_new) ||    // ::new
52720b57cec5SDimitry Andric         NextToken().is(tok::kw_delete))   // ::delete
52730b57cec5SDimitry Andric       return false;
52740b57cec5SDimitry Andric 
52750b57cec5SDimitry Andric     if (TryAnnotateTypeOrScopeToken())
52760b57cec5SDimitry Andric       return true;
52770b57cec5SDimitry Andric     return isTypeSpecifierQualifier();
52780b57cec5SDimitry Andric 
52790b57cec5SDimitry Andric     // GNU attributes support.
52800b57cec5SDimitry Andric   case tok::kw___attribute:
5281*bdd1243dSDimitry Andric     // C2x/GNU typeof support.
52820b57cec5SDimitry Andric   case tok::kw_typeof:
5283*bdd1243dSDimitry Andric   case tok::kw_typeof_unqual:
52840b57cec5SDimitry Andric 
52850b57cec5SDimitry Andric     // type-specifiers
52860b57cec5SDimitry Andric   case tok::kw_short:
52870b57cec5SDimitry Andric   case tok::kw_long:
52880b57cec5SDimitry Andric   case tok::kw___int64:
52890b57cec5SDimitry Andric   case tok::kw___int128:
52900b57cec5SDimitry Andric   case tok::kw_signed:
52910b57cec5SDimitry Andric   case tok::kw_unsigned:
52920b57cec5SDimitry Andric   case tok::kw__Complex:
52930b57cec5SDimitry Andric   case tok::kw__Imaginary:
52940b57cec5SDimitry Andric   case tok::kw_void:
52950b57cec5SDimitry Andric   case tok::kw_char:
52960b57cec5SDimitry Andric   case tok::kw_wchar_t:
52970b57cec5SDimitry Andric   case tok::kw_char8_t:
52980b57cec5SDimitry Andric   case tok::kw_char16_t:
52990b57cec5SDimitry Andric   case tok::kw_char32_t:
53000b57cec5SDimitry Andric   case tok::kw_int:
53015ffd83dbSDimitry Andric   case tok::kw__ExtInt:
53020eae32dcSDimitry Andric   case tok::kw__BitInt:
53030b57cec5SDimitry Andric   case tok::kw_half:
53045ffd83dbSDimitry Andric   case tok::kw___bf16:
53050b57cec5SDimitry Andric   case tok::kw_float:
53060b57cec5SDimitry Andric   case tok::kw_double:
53070b57cec5SDimitry Andric   case tok::kw__Accum:
53080b57cec5SDimitry Andric   case tok::kw__Fract:
53090b57cec5SDimitry Andric   case tok::kw__Float16:
53100b57cec5SDimitry Andric   case tok::kw___float128:
5311349cc55cSDimitry Andric   case tok::kw___ibm128:
53120b57cec5SDimitry Andric   case tok::kw_bool:
53130b57cec5SDimitry Andric   case tok::kw__Bool:
53140b57cec5SDimitry Andric   case tok::kw__Decimal32:
53150b57cec5SDimitry Andric   case tok::kw__Decimal64:
53160b57cec5SDimitry Andric   case tok::kw__Decimal128:
53170b57cec5SDimitry Andric   case tok::kw___vector:
53180b57cec5SDimitry Andric #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
53190b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def"
53200b57cec5SDimitry Andric 
53210b57cec5SDimitry Andric     // struct-or-union-specifier (C99) or class-specifier (C++)
53220b57cec5SDimitry Andric   case tok::kw_class:
53230b57cec5SDimitry Andric   case tok::kw_struct:
53240b57cec5SDimitry Andric   case tok::kw___interface:
53250b57cec5SDimitry Andric   case tok::kw_union:
53260b57cec5SDimitry Andric     // enum-specifier
53270b57cec5SDimitry Andric   case tok::kw_enum:
53280b57cec5SDimitry Andric 
53290b57cec5SDimitry Andric     // type-qualifier
53300b57cec5SDimitry Andric   case tok::kw_const:
53310b57cec5SDimitry Andric   case tok::kw_volatile:
53320b57cec5SDimitry Andric   case tok::kw_restrict:
53330b57cec5SDimitry Andric   case tok::kw__Sat:
53340b57cec5SDimitry Andric 
53350b57cec5SDimitry Andric     // Debugger support.
53360b57cec5SDimitry Andric   case tok::kw___unknown_anytype:
53370b57cec5SDimitry Andric 
53380b57cec5SDimitry Andric     // typedef-name
53390b57cec5SDimitry Andric   case tok::annot_typename:
53400b57cec5SDimitry Andric     return true;
53410b57cec5SDimitry Andric 
53420b57cec5SDimitry Andric     // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
53430b57cec5SDimitry Andric   case tok::less:
53440b57cec5SDimitry Andric     return getLangOpts().ObjC;
53450b57cec5SDimitry Andric 
53460b57cec5SDimitry Andric   case tok::kw___cdecl:
53470b57cec5SDimitry Andric   case tok::kw___stdcall:
53480b57cec5SDimitry Andric   case tok::kw___fastcall:
53490b57cec5SDimitry Andric   case tok::kw___thiscall:
53500b57cec5SDimitry Andric   case tok::kw___regcall:
53510b57cec5SDimitry Andric   case tok::kw___vectorcall:
53520b57cec5SDimitry Andric   case tok::kw___w64:
53530b57cec5SDimitry Andric   case tok::kw___ptr64:
53540b57cec5SDimitry Andric   case tok::kw___ptr32:
53550b57cec5SDimitry Andric   case tok::kw___pascal:
53560b57cec5SDimitry Andric   case tok::kw___unaligned:
53570b57cec5SDimitry Andric 
53580b57cec5SDimitry Andric   case tok::kw__Nonnull:
53590b57cec5SDimitry Andric   case tok::kw__Nullable:
5360e8d8bef9SDimitry Andric   case tok::kw__Nullable_result:
53610b57cec5SDimitry Andric   case tok::kw__Null_unspecified:
53620b57cec5SDimitry Andric 
53630b57cec5SDimitry Andric   case tok::kw___kindof:
53640b57cec5SDimitry Andric 
53650b57cec5SDimitry Andric   case tok::kw___private:
53660b57cec5SDimitry Andric   case tok::kw___local:
53670b57cec5SDimitry Andric   case tok::kw___global:
53680b57cec5SDimitry Andric   case tok::kw___constant:
53690b57cec5SDimitry Andric   case tok::kw___generic:
53700b57cec5SDimitry Andric   case tok::kw___read_only:
53710b57cec5SDimitry Andric   case tok::kw___read_write:
53720b57cec5SDimitry Andric   case tok::kw___write_only:
5373*bdd1243dSDimitry Andric 
5374*bdd1243dSDimitry Andric   case tok::kw_groupshared:
53750b57cec5SDimitry Andric     return true;
53760b57cec5SDimitry Andric 
53770b57cec5SDimitry Andric   case tok::kw_private:
53780b57cec5SDimitry Andric     return getLangOpts().OpenCL;
53790b57cec5SDimitry Andric 
53800b57cec5SDimitry Andric   // C11 _Atomic
53810b57cec5SDimitry Andric   case tok::kw__Atomic:
53820b57cec5SDimitry Andric     return true;
53830b57cec5SDimitry Andric   }
53840b57cec5SDimitry Andric }
53850b57cec5SDimitry Andric 
5386*bdd1243dSDimitry Andric Parser::DeclGroupPtrTy Parser::ParseTopLevelStmtDecl() {
5387*bdd1243dSDimitry Andric   assert(PP.isIncrementalProcessingEnabled() && "Not in incremental mode");
5388*bdd1243dSDimitry Andric 
5389*bdd1243dSDimitry Andric   // Parse a top-level-stmt.
5390*bdd1243dSDimitry Andric   Parser::StmtVector Stmts;
5391*bdd1243dSDimitry Andric   ParsedStmtContext SubStmtCtx = ParsedStmtContext();
5392*bdd1243dSDimitry Andric   StmtResult R = ParseStatementOrDeclaration(Stmts, SubStmtCtx);
5393*bdd1243dSDimitry Andric   if (!R.isUsable())
5394*bdd1243dSDimitry Andric     return nullptr;
5395*bdd1243dSDimitry Andric 
5396*bdd1243dSDimitry Andric   SmallVector<Decl *, 2> DeclsInGroup;
5397*bdd1243dSDimitry Andric   DeclsInGroup.push_back(Actions.ActOnTopLevelStmtDecl(R.get()));
5398*bdd1243dSDimitry Andric   // Currently happens for things like  -fms-extensions and use `__if_exists`.
5399*bdd1243dSDimitry Andric   for (Stmt *S : Stmts)
5400*bdd1243dSDimitry Andric     DeclsInGroup.push_back(Actions.ActOnTopLevelStmtDecl(S));
5401*bdd1243dSDimitry Andric 
5402*bdd1243dSDimitry Andric   return Actions.BuildDeclaratorGroup(DeclsInGroup);
5403*bdd1243dSDimitry Andric }
5404*bdd1243dSDimitry Andric 
54050b57cec5SDimitry Andric /// isDeclarationSpecifier() - Return true if the current token is part of a
54060b57cec5SDimitry Andric /// declaration specifier.
54070b57cec5SDimitry Andric ///
5408*bdd1243dSDimitry Andric /// \param AllowImplicitTypename whether this is a context where T::type [T
5409*bdd1243dSDimitry Andric /// dependent] can appear.
54100b57cec5SDimitry Andric /// \param DisambiguatingWithExpression True to indicate that the purpose of
54110b57cec5SDimitry Andric /// this check is to disambiguate between an expression and a declaration.
5412*bdd1243dSDimitry Andric bool Parser::isDeclarationSpecifier(
5413*bdd1243dSDimitry Andric     ImplicitTypenameContext AllowImplicitTypename,
5414*bdd1243dSDimitry Andric     bool DisambiguatingWithExpression) {
54150b57cec5SDimitry Andric   switch (Tok.getKind()) {
54160b57cec5SDimitry Andric   default: return false;
54170b57cec5SDimitry Andric 
54186e75b2fbSDimitry Andric   // OpenCL 2.0 and later define this keyword.
54190b57cec5SDimitry Andric   case tok::kw_pipe:
5420349cc55cSDimitry Andric     return getLangOpts().OpenCL &&
5421349cc55cSDimitry Andric            getLangOpts().getOpenCLCompatibleVersion() >= 200;
54220b57cec5SDimitry Andric 
54230b57cec5SDimitry Andric   case tok::identifier:   // foo::bar
54240b57cec5SDimitry Andric     // Unfortunate hack to support "Class.factoryMethod" notation.
54250b57cec5SDimitry Andric     if (getLangOpts().ObjC && NextToken().is(tok::period))
54260b57cec5SDimitry Andric       return false;
54270b57cec5SDimitry Andric     if (TryAltiVecVectorToken())
54280b57cec5SDimitry Andric       return true;
5429*bdd1243dSDimitry Andric     [[fallthrough]];
54300b57cec5SDimitry Andric   case tok::kw_decltype: // decltype(T())::type
54310b57cec5SDimitry Andric   case tok::kw_typename: // typename T::type
54320b57cec5SDimitry Andric     // Annotate typenames and C++ scope specifiers.  If we get one, just
54330b57cec5SDimitry Andric     // recurse to handle whatever we get.
5434*bdd1243dSDimitry Andric     if (TryAnnotateTypeOrScopeToken(AllowImplicitTypename))
54350b57cec5SDimitry Andric       return true;
543613138422SDimitry Andric     if (TryAnnotateTypeConstraint())
543713138422SDimitry Andric       return true;
54380b57cec5SDimitry Andric     if (Tok.is(tok::identifier))
54390b57cec5SDimitry Andric       return false;
54400b57cec5SDimitry Andric 
54410b57cec5SDimitry Andric     // If we're in Objective-C and we have an Objective-C class type followed
54420b57cec5SDimitry Andric     // by an identifier and then either ':' or ']', in a place where an
54430b57cec5SDimitry Andric     // expression is permitted, then this is probably a class message send
54440b57cec5SDimitry Andric     // missing the initial '['. In this case, we won't consider this to be
54450b57cec5SDimitry Andric     // the start of a declaration.
54460b57cec5SDimitry Andric     if (DisambiguatingWithExpression &&
54470b57cec5SDimitry Andric         isStartOfObjCClassMessageMissingOpenBracket())
54480b57cec5SDimitry Andric       return false;
54490b57cec5SDimitry Andric 
5450*bdd1243dSDimitry Andric     return isDeclarationSpecifier(AllowImplicitTypename);
54510b57cec5SDimitry Andric 
54520b57cec5SDimitry Andric   case tok::coloncolon:   // ::foo::bar
5453*bdd1243dSDimitry Andric     if (!getLangOpts().CPlusPlus)
5454*bdd1243dSDimitry Andric       return false;
54550b57cec5SDimitry Andric     if (NextToken().is(tok::kw_new) ||    // ::new
54560b57cec5SDimitry Andric         NextToken().is(tok::kw_delete))   // ::delete
54570b57cec5SDimitry Andric       return false;
54580b57cec5SDimitry Andric 
54590b57cec5SDimitry Andric     // Annotate typenames and C++ scope specifiers.  If we get one, just
54600b57cec5SDimitry Andric     // recurse to handle whatever we get.
54610b57cec5SDimitry Andric     if (TryAnnotateTypeOrScopeToken())
54620b57cec5SDimitry Andric       return true;
5463*bdd1243dSDimitry Andric     return isDeclarationSpecifier(ImplicitTypenameContext::No);
54640b57cec5SDimitry Andric 
54650b57cec5SDimitry Andric     // storage-class-specifier
54660b57cec5SDimitry Andric   case tok::kw_typedef:
54670b57cec5SDimitry Andric   case tok::kw_extern:
54680b57cec5SDimitry Andric   case tok::kw___private_extern__:
54690b57cec5SDimitry Andric   case tok::kw_static:
54700b57cec5SDimitry Andric   case tok::kw_auto:
54710b57cec5SDimitry Andric   case tok::kw___auto_type:
54720b57cec5SDimitry Andric   case tok::kw_register:
54730b57cec5SDimitry Andric   case tok::kw___thread:
54740b57cec5SDimitry Andric   case tok::kw_thread_local:
54750b57cec5SDimitry Andric   case tok::kw__Thread_local:
54760b57cec5SDimitry Andric 
54770b57cec5SDimitry Andric     // Modules
54780b57cec5SDimitry Andric   case tok::kw___module_private__:
54790b57cec5SDimitry Andric 
54800b57cec5SDimitry Andric     // Debugger support
54810b57cec5SDimitry Andric   case tok::kw___unknown_anytype:
54820b57cec5SDimitry Andric 
54830b57cec5SDimitry Andric     // type-specifiers
54840b57cec5SDimitry Andric   case tok::kw_short:
54850b57cec5SDimitry Andric   case tok::kw_long:
54860b57cec5SDimitry Andric   case tok::kw___int64:
54870b57cec5SDimitry Andric   case tok::kw___int128:
54880b57cec5SDimitry Andric   case tok::kw_signed:
54890b57cec5SDimitry Andric   case tok::kw_unsigned:
54900b57cec5SDimitry Andric   case tok::kw__Complex:
54910b57cec5SDimitry Andric   case tok::kw__Imaginary:
54920b57cec5SDimitry Andric   case tok::kw_void:
54930b57cec5SDimitry Andric   case tok::kw_char:
54940b57cec5SDimitry Andric   case tok::kw_wchar_t:
54950b57cec5SDimitry Andric   case tok::kw_char8_t:
54960b57cec5SDimitry Andric   case tok::kw_char16_t:
54970b57cec5SDimitry Andric   case tok::kw_char32_t:
54980b57cec5SDimitry Andric 
54990b57cec5SDimitry Andric   case tok::kw_int:
55005ffd83dbSDimitry Andric   case tok::kw__ExtInt:
55010eae32dcSDimitry Andric   case tok::kw__BitInt:
55020b57cec5SDimitry Andric   case tok::kw_half:
55035ffd83dbSDimitry Andric   case tok::kw___bf16:
55040b57cec5SDimitry Andric   case tok::kw_float:
55050b57cec5SDimitry Andric   case tok::kw_double:
55060b57cec5SDimitry Andric   case tok::kw__Accum:
55070b57cec5SDimitry Andric   case tok::kw__Fract:
55080b57cec5SDimitry Andric   case tok::kw__Float16:
55090b57cec5SDimitry Andric   case tok::kw___float128:
5510349cc55cSDimitry Andric   case tok::kw___ibm128:
55110b57cec5SDimitry Andric   case tok::kw_bool:
55120b57cec5SDimitry Andric   case tok::kw__Bool:
55130b57cec5SDimitry Andric   case tok::kw__Decimal32:
55140b57cec5SDimitry Andric   case tok::kw__Decimal64:
55150b57cec5SDimitry Andric   case tok::kw__Decimal128:
55160b57cec5SDimitry Andric   case tok::kw___vector:
55170b57cec5SDimitry Andric 
55180b57cec5SDimitry Andric     // struct-or-union-specifier (C99) or class-specifier (C++)
55190b57cec5SDimitry Andric   case tok::kw_class:
55200b57cec5SDimitry Andric   case tok::kw_struct:
55210b57cec5SDimitry Andric   case tok::kw_union:
55220b57cec5SDimitry Andric   case tok::kw___interface:
55230b57cec5SDimitry Andric     // enum-specifier
55240b57cec5SDimitry Andric   case tok::kw_enum:
55250b57cec5SDimitry Andric 
55260b57cec5SDimitry Andric     // type-qualifier
55270b57cec5SDimitry Andric   case tok::kw_const:
55280b57cec5SDimitry Andric   case tok::kw_volatile:
55290b57cec5SDimitry Andric   case tok::kw_restrict:
55300b57cec5SDimitry Andric   case tok::kw__Sat:
55310b57cec5SDimitry Andric 
55320b57cec5SDimitry Andric     // function-specifier
55330b57cec5SDimitry Andric   case tok::kw_inline:
55340b57cec5SDimitry Andric   case tok::kw_virtual:
55350b57cec5SDimitry Andric   case tok::kw_explicit:
55360b57cec5SDimitry Andric   case tok::kw__Noreturn:
55370b57cec5SDimitry Andric 
55380b57cec5SDimitry Andric     // alignment-specifier
55390b57cec5SDimitry Andric   case tok::kw__Alignas:
55400b57cec5SDimitry Andric 
55410b57cec5SDimitry Andric     // friend keyword.
55420b57cec5SDimitry Andric   case tok::kw_friend:
55430b57cec5SDimitry Andric 
55440b57cec5SDimitry Andric     // static_assert-declaration
5545d409305fSDimitry Andric   case tok::kw_static_assert:
55460b57cec5SDimitry Andric   case tok::kw__Static_assert:
55470b57cec5SDimitry Andric 
5548*bdd1243dSDimitry Andric     // C2x/GNU typeof support.
55490b57cec5SDimitry Andric   case tok::kw_typeof:
5550*bdd1243dSDimitry Andric   case tok::kw_typeof_unqual:
55510b57cec5SDimitry Andric 
55520b57cec5SDimitry Andric     // GNU attributes.
55530b57cec5SDimitry Andric   case tok::kw___attribute:
55540b57cec5SDimitry Andric 
55550b57cec5SDimitry Andric     // C++11 decltype and constexpr.
55560b57cec5SDimitry Andric   case tok::annot_decltype:
55570b57cec5SDimitry Andric   case tok::kw_constexpr:
55580b57cec5SDimitry Andric 
5559a7dea167SDimitry Andric     // C++20 consteval and constinit.
55600b57cec5SDimitry Andric   case tok::kw_consteval:
5561a7dea167SDimitry Andric   case tok::kw_constinit:
55620b57cec5SDimitry Andric 
55630b57cec5SDimitry Andric     // C11 _Atomic
55640b57cec5SDimitry Andric   case tok::kw__Atomic:
55650b57cec5SDimitry Andric     return true;
55660b57cec5SDimitry Andric 
55670b57cec5SDimitry Andric     // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
55680b57cec5SDimitry Andric   case tok::less:
55690b57cec5SDimitry Andric     return getLangOpts().ObjC;
55700b57cec5SDimitry Andric 
55710b57cec5SDimitry Andric     // typedef-name
55720b57cec5SDimitry Andric   case tok::annot_typename:
55730b57cec5SDimitry Andric     return !DisambiguatingWithExpression ||
55740b57cec5SDimitry Andric            !isStartOfObjCClassMessageMissingOpenBracket();
55750b57cec5SDimitry Andric 
5576480093f4SDimitry Andric     // placeholder-type-specifier
5577480093f4SDimitry Andric   case tok::annot_template_id: {
55785ffd83dbSDimitry Andric     TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
55795ffd83dbSDimitry Andric     if (TemplateId->hasInvalidName())
55805ffd83dbSDimitry Andric       return true;
55815ffd83dbSDimitry Andric     // FIXME: What about type templates that have only been annotated as
55825ffd83dbSDimitry Andric     // annot_template_id, not as annot_typename?
558313138422SDimitry Andric     return isTypeConstraintAnnotation() &&
5584480093f4SDimitry Andric            (NextToken().is(tok::kw_auto) || NextToken().is(tok::kw_decltype));
5585480093f4SDimitry Andric   }
55865ffd83dbSDimitry Andric 
55875ffd83dbSDimitry Andric   case tok::annot_cxxscope: {
55885ffd83dbSDimitry Andric     TemplateIdAnnotation *TemplateId =
55895ffd83dbSDimitry Andric         NextToken().is(tok::annot_template_id)
55905ffd83dbSDimitry Andric             ? takeTemplateIdAnnotation(NextToken())
55915ffd83dbSDimitry Andric             : nullptr;
55925ffd83dbSDimitry Andric     if (TemplateId && TemplateId->hasInvalidName())
55935ffd83dbSDimitry Andric       return true;
55945ffd83dbSDimitry Andric     // FIXME: What about type templates that have only been annotated as
55955ffd83dbSDimitry Andric     // annot_template_id, not as annot_typename?
559613138422SDimitry Andric     if (NextToken().is(tok::identifier) && TryAnnotateTypeConstraint())
559713138422SDimitry Andric       return true;
559813138422SDimitry Andric     return isTypeConstraintAnnotation() &&
559913138422SDimitry Andric         GetLookAheadToken(2).isOneOf(tok::kw_auto, tok::kw_decltype);
56005ffd83dbSDimitry Andric   }
56015ffd83dbSDimitry Andric 
56020b57cec5SDimitry Andric   case tok::kw___declspec:
56030b57cec5SDimitry Andric   case tok::kw___cdecl:
56040b57cec5SDimitry Andric   case tok::kw___stdcall:
56050b57cec5SDimitry Andric   case tok::kw___fastcall:
56060b57cec5SDimitry Andric   case tok::kw___thiscall:
56070b57cec5SDimitry Andric   case tok::kw___regcall:
56080b57cec5SDimitry Andric   case tok::kw___vectorcall:
56090b57cec5SDimitry Andric   case tok::kw___w64:
56100b57cec5SDimitry Andric   case tok::kw___sptr:
56110b57cec5SDimitry Andric   case tok::kw___uptr:
56120b57cec5SDimitry Andric   case tok::kw___ptr64:
56130b57cec5SDimitry Andric   case tok::kw___ptr32:
56140b57cec5SDimitry Andric   case tok::kw___forceinline:
56150b57cec5SDimitry Andric   case tok::kw___pascal:
56160b57cec5SDimitry Andric   case tok::kw___unaligned:
56170b57cec5SDimitry Andric 
56180b57cec5SDimitry Andric   case tok::kw__Nonnull:
56190b57cec5SDimitry Andric   case tok::kw__Nullable:
5620e8d8bef9SDimitry Andric   case tok::kw__Nullable_result:
56210b57cec5SDimitry Andric   case tok::kw__Null_unspecified:
56220b57cec5SDimitry Andric 
56230b57cec5SDimitry Andric   case tok::kw___kindof:
56240b57cec5SDimitry Andric 
56250b57cec5SDimitry Andric   case tok::kw___private:
56260b57cec5SDimitry Andric   case tok::kw___local:
56270b57cec5SDimitry Andric   case tok::kw___global:
56280b57cec5SDimitry Andric   case tok::kw___constant:
56290b57cec5SDimitry Andric   case tok::kw___generic:
56300b57cec5SDimitry Andric   case tok::kw___read_only:
56310b57cec5SDimitry Andric   case tok::kw___read_write:
56320b57cec5SDimitry Andric   case tok::kw___write_only:
56330b57cec5SDimitry Andric #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
56340b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def"
56350b57cec5SDimitry Andric 
5636*bdd1243dSDimitry Andric   case tok::kw_groupshared:
56370b57cec5SDimitry Andric     return true;
56380b57cec5SDimitry Andric 
56390b57cec5SDimitry Andric   case tok::kw_private:
56400b57cec5SDimitry Andric     return getLangOpts().OpenCL;
56410b57cec5SDimitry Andric   }
56420b57cec5SDimitry Andric }
56430b57cec5SDimitry Andric 
5644*bdd1243dSDimitry Andric bool Parser::isConstructorDeclarator(bool IsUnqualified, bool DeductionGuide,
5645*bdd1243dSDimitry Andric                                      DeclSpec::FriendSpecified IsFriend) {
56460b57cec5SDimitry Andric   TentativeParsingAction TPA(*this);
56470b57cec5SDimitry Andric 
56480b57cec5SDimitry Andric   // Parse the C++ scope specifier.
56490b57cec5SDimitry Andric   CXXScopeSpec SS;
56505ffd83dbSDimitry Andric   if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
565104eeddc0SDimitry Andric                                      /*ObjectHasErrors=*/false,
56520b57cec5SDimitry Andric                                      /*EnteringContext=*/true)) {
56530b57cec5SDimitry Andric     TPA.Revert();
56540b57cec5SDimitry Andric     return false;
56550b57cec5SDimitry Andric   }
56560b57cec5SDimitry Andric 
56570b57cec5SDimitry Andric   // Parse the constructor name.
56580b57cec5SDimitry Andric   if (Tok.is(tok::identifier)) {
56590b57cec5SDimitry Andric     // We already know that we have a constructor name; just consume
56600b57cec5SDimitry Andric     // the token.
56610b57cec5SDimitry Andric     ConsumeToken();
56620b57cec5SDimitry Andric   } else if (Tok.is(tok::annot_template_id)) {
56630b57cec5SDimitry Andric     ConsumeAnnotationToken();
56640b57cec5SDimitry Andric   } else {
56650b57cec5SDimitry Andric     TPA.Revert();
56660b57cec5SDimitry Andric     return false;
56670b57cec5SDimitry Andric   }
56680b57cec5SDimitry Andric 
56690b57cec5SDimitry Andric   // There may be attributes here, appertaining to the constructor name or type
56700b57cec5SDimitry Andric   // we just stepped past.
56710b57cec5SDimitry Andric   SkipCXX11Attributes();
56720b57cec5SDimitry Andric 
56730b57cec5SDimitry Andric   // Current class name must be followed by a left parenthesis.
56740b57cec5SDimitry Andric   if (Tok.isNot(tok::l_paren)) {
56750b57cec5SDimitry Andric     TPA.Revert();
56760b57cec5SDimitry Andric     return false;
56770b57cec5SDimitry Andric   }
56780b57cec5SDimitry Andric   ConsumeParen();
56790b57cec5SDimitry Andric 
56800b57cec5SDimitry Andric   // A right parenthesis, or ellipsis followed by a right parenthesis signals
56810b57cec5SDimitry Andric   // that we have a constructor.
56820b57cec5SDimitry Andric   if (Tok.is(tok::r_paren) ||
56830b57cec5SDimitry Andric       (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
56840b57cec5SDimitry Andric     TPA.Revert();
56850b57cec5SDimitry Andric     return true;
56860b57cec5SDimitry Andric   }
56870b57cec5SDimitry Andric 
56880b57cec5SDimitry Andric   // A C++11 attribute here signals that we have a constructor, and is an
56890b57cec5SDimitry Andric   // attribute on the first constructor parameter.
56900b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus11 &&
56910b57cec5SDimitry Andric       isCXX11AttributeSpecifier(/*Disambiguate*/ false,
56920b57cec5SDimitry Andric                                 /*OuterMightBeMessageSend*/ true)) {
56930b57cec5SDimitry Andric     TPA.Revert();
56940b57cec5SDimitry Andric     return true;
56950b57cec5SDimitry Andric   }
56960b57cec5SDimitry Andric 
56970b57cec5SDimitry Andric   // If we need to, enter the specified scope.
56980b57cec5SDimitry Andric   DeclaratorScopeObj DeclScopeObj(*this, SS);
56990b57cec5SDimitry Andric   if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
57000b57cec5SDimitry Andric     DeclScopeObj.EnterDeclaratorScope();
57010b57cec5SDimitry Andric 
57020b57cec5SDimitry Andric   // Optionally skip Microsoft attributes.
57030b57cec5SDimitry Andric   ParsedAttributes Attrs(AttrFactory);
57040b57cec5SDimitry Andric   MaybeParseMicrosoftAttributes(Attrs);
57050b57cec5SDimitry Andric 
57060b57cec5SDimitry Andric   // Check whether the next token(s) are part of a declaration
57070b57cec5SDimitry Andric   // specifier, in which case we have the start of a parameter and,
57080b57cec5SDimitry Andric   // therefore, we know that this is a constructor.
5709*bdd1243dSDimitry Andric   // Due to an ambiguity with implicit typename, the above is not enough.
5710*bdd1243dSDimitry Andric   // Additionally, check to see if we are a friend.
57110b57cec5SDimitry Andric   bool IsConstructor = false;
5712*bdd1243dSDimitry Andric   if (isDeclarationSpecifier(IsFriend ? ImplicitTypenameContext::No
5713*bdd1243dSDimitry Andric                                       : ImplicitTypenameContext::Yes))
57140b57cec5SDimitry Andric     IsConstructor = true;
57150b57cec5SDimitry Andric   else if (Tok.is(tok::identifier) ||
57160b57cec5SDimitry Andric            (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
57170b57cec5SDimitry Andric     // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
57180b57cec5SDimitry Andric     // This might be a parenthesized member name, but is more likely to
57190b57cec5SDimitry Andric     // be a constructor declaration with an invalid argument type. Keep
57200b57cec5SDimitry Andric     // looking.
57210b57cec5SDimitry Andric     if (Tok.is(tok::annot_cxxscope))
57220b57cec5SDimitry Andric       ConsumeAnnotationToken();
57230b57cec5SDimitry Andric     ConsumeToken();
57240b57cec5SDimitry Andric 
57250b57cec5SDimitry Andric     // If this is not a constructor, we must be parsing a declarator,
57260b57cec5SDimitry Andric     // which must have one of the following syntactic forms (see the
57270b57cec5SDimitry Andric     // grammar extract at the start of ParseDirectDeclarator):
57280b57cec5SDimitry Andric     switch (Tok.getKind()) {
57290b57cec5SDimitry Andric     case tok::l_paren:
57300b57cec5SDimitry Andric       // C(X   (   int));
57310b57cec5SDimitry Andric     case tok::l_square:
57320b57cec5SDimitry Andric       // C(X   [   5]);
57330b57cec5SDimitry Andric       // C(X   [   [attribute]]);
57340b57cec5SDimitry Andric     case tok::coloncolon:
57350b57cec5SDimitry Andric       // C(X   ::   Y);
57360b57cec5SDimitry Andric       // C(X   ::   *p);
57370b57cec5SDimitry Andric       // Assume this isn't a constructor, rather than assuming it's a
57380b57cec5SDimitry Andric       // constructor with an unnamed parameter of an ill-formed type.
57390b57cec5SDimitry Andric       break;
57400b57cec5SDimitry Andric 
57410b57cec5SDimitry Andric     case tok::r_paren:
57420b57cec5SDimitry Andric       // C(X   )
57430b57cec5SDimitry Andric 
57440b57cec5SDimitry Andric       // Skip past the right-paren and any following attributes to get to
57450b57cec5SDimitry Andric       // the function body or trailing-return-type.
57460b57cec5SDimitry Andric       ConsumeParen();
57470b57cec5SDimitry Andric       SkipCXX11Attributes();
57480b57cec5SDimitry Andric 
57490b57cec5SDimitry Andric       if (DeductionGuide) {
57500b57cec5SDimitry Andric         // C(X) -> ... is a deduction guide.
57510b57cec5SDimitry Andric         IsConstructor = Tok.is(tok::arrow);
57520b57cec5SDimitry Andric         break;
57530b57cec5SDimitry Andric       }
57540b57cec5SDimitry Andric       if (Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
57550b57cec5SDimitry Andric         // Assume these were meant to be constructors:
57560b57cec5SDimitry Andric         //   C(X)   :    (the name of a bit-field cannot be parenthesized).
57570b57cec5SDimitry Andric         //   C(X)   try  (this is otherwise ill-formed).
57580b57cec5SDimitry Andric         IsConstructor = true;
57590b57cec5SDimitry Andric       }
57600b57cec5SDimitry Andric       if (Tok.is(tok::semi) || Tok.is(tok::l_brace)) {
57610b57cec5SDimitry Andric         // If we have a constructor name within the class definition,
57620b57cec5SDimitry Andric         // assume these were meant to be constructors:
57630b57cec5SDimitry Andric         //   C(X)   {
57640b57cec5SDimitry Andric         //   C(X)   ;
57650b57cec5SDimitry Andric         // ... because otherwise we would be declaring a non-static data
57660b57cec5SDimitry Andric         // member that is ill-formed because it's of the same type as its
57670b57cec5SDimitry Andric         // surrounding class.
57680b57cec5SDimitry Andric         //
57690b57cec5SDimitry Andric         // FIXME: We can actually do this whether or not the name is qualified,
57700b57cec5SDimitry Andric         // because if it is qualified in this context it must be being used as
57710b57cec5SDimitry Andric         // a constructor name.
57720b57cec5SDimitry Andric         // currently, so we're somewhat conservative here.
57730b57cec5SDimitry Andric         IsConstructor = IsUnqualified;
57740b57cec5SDimitry Andric       }
57750b57cec5SDimitry Andric       break;
57760b57cec5SDimitry Andric 
57770b57cec5SDimitry Andric     default:
57780b57cec5SDimitry Andric       IsConstructor = true;
57790b57cec5SDimitry Andric       break;
57800b57cec5SDimitry Andric     }
57810b57cec5SDimitry Andric   }
57820b57cec5SDimitry Andric 
57830b57cec5SDimitry Andric   TPA.Revert();
57840b57cec5SDimitry Andric   return IsConstructor;
57850b57cec5SDimitry Andric }
57860b57cec5SDimitry Andric 
57870b57cec5SDimitry Andric /// ParseTypeQualifierListOpt
57880b57cec5SDimitry Andric ///          type-qualifier-list: [C99 6.7.5]
57890b57cec5SDimitry Andric ///            type-qualifier
57900b57cec5SDimitry Andric /// [vendor]   attributes
57910b57cec5SDimitry Andric ///              [ only if AttrReqs & AR_VendorAttributesParsed ]
57920b57cec5SDimitry Andric ///            type-qualifier-list type-qualifier
57930b57cec5SDimitry Andric /// [vendor]   type-qualifier-list attributes
57940b57cec5SDimitry Andric ///              [ only if AttrReqs & AR_VendorAttributesParsed ]
57950b57cec5SDimitry Andric /// [C++0x]    attribute-specifier[opt] is allowed before cv-qualifier-seq
57960b57cec5SDimitry Andric ///              [ only if AttReqs & AR_CXX11AttributesParsed ]
57970b57cec5SDimitry Andric /// Note: vendor can be GNU, MS, etc and can be explicitly controlled via
57980b57cec5SDimitry Andric /// AttrRequirements bitmask values.
57990b57cec5SDimitry Andric void Parser::ParseTypeQualifierListOpt(
58000b57cec5SDimitry Andric     DeclSpec &DS, unsigned AttrReqs, bool AtomicAllowed,
58010b57cec5SDimitry Andric     bool IdentifierRequired,
5802*bdd1243dSDimitry Andric     std::optional<llvm::function_ref<void()>> CodeCompletionHandler) {
58030b57cec5SDimitry Andric   if (standardAttributesAllowed() && (AttrReqs & AR_CXX11AttributesParsed) &&
58040b57cec5SDimitry Andric       isCXX11AttributeSpecifier()) {
580581ad6265SDimitry Andric     ParsedAttributes Attrs(AttrFactory);
580681ad6265SDimitry Andric     ParseCXX11Attributes(Attrs);
580781ad6265SDimitry Andric     DS.takeAttributesFrom(Attrs);
58080b57cec5SDimitry Andric   }
58090b57cec5SDimitry Andric 
58100b57cec5SDimitry Andric   SourceLocation EndLoc;
58110b57cec5SDimitry Andric 
581204eeddc0SDimitry Andric   while (true) {
58130b57cec5SDimitry Andric     bool isInvalid = false;
58140b57cec5SDimitry Andric     const char *PrevSpec = nullptr;
58150b57cec5SDimitry Andric     unsigned DiagID = 0;
58160b57cec5SDimitry Andric     SourceLocation Loc = Tok.getLocation();
58170b57cec5SDimitry Andric 
58180b57cec5SDimitry Andric     switch (Tok.getKind()) {
58190b57cec5SDimitry Andric     case tok::code_completion:
5820fe6060f1SDimitry Andric       cutOffParsing();
58210b57cec5SDimitry Andric       if (CodeCompletionHandler)
58220b57cec5SDimitry Andric         (*CodeCompletionHandler)();
58230b57cec5SDimitry Andric       else
58240b57cec5SDimitry Andric         Actions.CodeCompleteTypeQualifiers(DS);
5825fe6060f1SDimitry Andric       return;
58260b57cec5SDimitry Andric 
58270b57cec5SDimitry Andric     case tok::kw_const:
58280b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_const   , Loc, PrevSpec, DiagID,
58290b57cec5SDimitry Andric                                  getLangOpts());
58300b57cec5SDimitry Andric       break;
58310b57cec5SDimitry Andric     case tok::kw_volatile:
58320b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
58330b57cec5SDimitry Andric                                  getLangOpts());
58340b57cec5SDimitry Andric       break;
58350b57cec5SDimitry Andric     case tok::kw_restrict:
58360b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
58370b57cec5SDimitry Andric                                  getLangOpts());
58380b57cec5SDimitry Andric       break;
58390b57cec5SDimitry Andric     case tok::kw__Atomic:
58400b57cec5SDimitry Andric       if (!AtomicAllowed)
58410b57cec5SDimitry Andric         goto DoneWithTypeQuals;
5842a7dea167SDimitry Andric       if (!getLangOpts().C11)
5843a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
58440b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
58450b57cec5SDimitry Andric                                  getLangOpts());
58460b57cec5SDimitry Andric       break;
58470b57cec5SDimitry Andric 
58480b57cec5SDimitry Andric     // OpenCL qualifiers:
58490b57cec5SDimitry Andric     case tok::kw_private:
58500b57cec5SDimitry Andric       if (!getLangOpts().OpenCL)
58510b57cec5SDimitry Andric         goto DoneWithTypeQuals;
5852*bdd1243dSDimitry Andric       [[fallthrough]];
58530b57cec5SDimitry Andric     case tok::kw___private:
58540b57cec5SDimitry Andric     case tok::kw___global:
58550b57cec5SDimitry Andric     case tok::kw___local:
58560b57cec5SDimitry Andric     case tok::kw___constant:
58570b57cec5SDimitry Andric     case tok::kw___generic:
58580b57cec5SDimitry Andric     case tok::kw___read_only:
58590b57cec5SDimitry Andric     case tok::kw___write_only:
58600b57cec5SDimitry Andric     case tok::kw___read_write:
58610b57cec5SDimitry Andric       ParseOpenCLQualifiers(DS.getAttributes());
58620b57cec5SDimitry Andric       break;
58630b57cec5SDimitry Andric 
5864*bdd1243dSDimitry Andric     case tok::kw_groupshared:
5865*bdd1243dSDimitry Andric       // NOTE: ParseHLSLQualifiers will consume the qualifier token.
5866*bdd1243dSDimitry Andric       ParseHLSLQualifiers(DS.getAttributes());
5867*bdd1243dSDimitry Andric       continue;
5868*bdd1243dSDimitry Andric 
58690b57cec5SDimitry Andric     case tok::kw___unaligned:
58700b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID,
58710b57cec5SDimitry Andric                                  getLangOpts());
58720b57cec5SDimitry Andric       break;
58730b57cec5SDimitry Andric     case tok::kw___uptr:
58740b57cec5SDimitry Andric       // GNU libc headers in C mode use '__uptr' as an identifier which conflicts
58750b57cec5SDimitry Andric       // with the MS modifier keyword.
58760b57cec5SDimitry Andric       if ((AttrReqs & AR_DeclspecAttributesParsed) && !getLangOpts().CPlusPlus &&
58770b57cec5SDimitry Andric           IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
58780b57cec5SDimitry Andric         if (TryKeywordIdentFallback(false))
58790b57cec5SDimitry Andric           continue;
58800b57cec5SDimitry Andric       }
5881*bdd1243dSDimitry Andric       [[fallthrough]];
58820b57cec5SDimitry Andric     case tok::kw___sptr:
58830b57cec5SDimitry Andric     case tok::kw___w64:
58840b57cec5SDimitry Andric     case tok::kw___ptr64:
58850b57cec5SDimitry Andric     case tok::kw___ptr32:
58860b57cec5SDimitry Andric     case tok::kw___cdecl:
58870b57cec5SDimitry Andric     case tok::kw___stdcall:
58880b57cec5SDimitry Andric     case tok::kw___fastcall:
58890b57cec5SDimitry Andric     case tok::kw___thiscall:
58900b57cec5SDimitry Andric     case tok::kw___regcall:
58910b57cec5SDimitry Andric     case tok::kw___vectorcall:
58920b57cec5SDimitry Andric       if (AttrReqs & AR_DeclspecAttributesParsed) {
58930b57cec5SDimitry Andric         ParseMicrosoftTypeAttributes(DS.getAttributes());
58940b57cec5SDimitry Andric         continue;
58950b57cec5SDimitry Andric       }
58960b57cec5SDimitry Andric       goto DoneWithTypeQuals;
58970b57cec5SDimitry Andric     case tok::kw___pascal:
58980b57cec5SDimitry Andric       if (AttrReqs & AR_VendorAttributesParsed) {
58990b57cec5SDimitry Andric         ParseBorlandTypeAttributes(DS.getAttributes());
59000b57cec5SDimitry Andric         continue;
59010b57cec5SDimitry Andric       }
59020b57cec5SDimitry Andric       goto DoneWithTypeQuals;
59030b57cec5SDimitry Andric 
59040b57cec5SDimitry Andric     // Nullability type specifiers.
59050b57cec5SDimitry Andric     case tok::kw__Nonnull:
59060b57cec5SDimitry Andric     case tok::kw__Nullable:
5907e8d8bef9SDimitry Andric     case tok::kw__Nullable_result:
59080b57cec5SDimitry Andric     case tok::kw__Null_unspecified:
59090b57cec5SDimitry Andric       ParseNullabilityTypeSpecifiers(DS.getAttributes());
59100b57cec5SDimitry Andric       continue;
59110b57cec5SDimitry Andric 
59120b57cec5SDimitry Andric     // Objective-C 'kindof' types.
59130b57cec5SDimitry Andric     case tok::kw___kindof:
59140b57cec5SDimitry Andric       DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc,
59150b57cec5SDimitry Andric                                 nullptr, 0, ParsedAttr::AS_Keyword);
59160b57cec5SDimitry Andric       (void)ConsumeToken();
59170b57cec5SDimitry Andric       continue;
59180b57cec5SDimitry Andric 
59190b57cec5SDimitry Andric     case tok::kw___attribute:
59200b57cec5SDimitry Andric       if (AttrReqs & AR_GNUAttributesParsedAndRejected)
59210b57cec5SDimitry Andric         // When GNU attributes are expressly forbidden, diagnose their usage.
59220b57cec5SDimitry Andric         Diag(Tok, diag::err_attributes_not_allowed);
59230b57cec5SDimitry Andric 
59240b57cec5SDimitry Andric       // Parse the attributes even if they are rejected to ensure that error
59250b57cec5SDimitry Andric       // recovery is graceful.
59260b57cec5SDimitry Andric       if (AttrReqs & AR_GNUAttributesParsed ||
59270b57cec5SDimitry Andric           AttrReqs & AR_GNUAttributesParsedAndRejected) {
59280b57cec5SDimitry Andric         ParseGNUAttributes(DS.getAttributes());
59290b57cec5SDimitry Andric         continue; // do *not* consume the next token!
59300b57cec5SDimitry Andric       }
59310b57cec5SDimitry Andric       // otherwise, FALL THROUGH!
5932*bdd1243dSDimitry Andric       [[fallthrough]];
59330b57cec5SDimitry Andric     default:
59340b57cec5SDimitry Andric       DoneWithTypeQuals:
59350b57cec5SDimitry Andric       // If this is not a type-qualifier token, we're done reading type
59360b57cec5SDimitry Andric       // qualifiers.  First verify that DeclSpec's are consistent.
59370b57cec5SDimitry Andric       DS.Finish(Actions, Actions.getASTContext().getPrintingPolicy());
59380b57cec5SDimitry Andric       if (EndLoc.isValid())
59390b57cec5SDimitry Andric         DS.SetRangeEnd(EndLoc);
59400b57cec5SDimitry Andric       return;
59410b57cec5SDimitry Andric     }
59420b57cec5SDimitry Andric 
59430b57cec5SDimitry Andric     // If the specifier combination wasn't legal, issue a diagnostic.
59440b57cec5SDimitry Andric     if (isInvalid) {
59450b57cec5SDimitry Andric       assert(PrevSpec && "Method did not return previous specifier!");
59460b57cec5SDimitry Andric       Diag(Tok, DiagID) << PrevSpec;
59470b57cec5SDimitry Andric     }
59480b57cec5SDimitry Andric     EndLoc = ConsumeToken();
59490b57cec5SDimitry Andric   }
59500b57cec5SDimitry Andric }
59510b57cec5SDimitry Andric 
59520b57cec5SDimitry Andric /// ParseDeclarator - Parse and verify a newly-initialized declarator.
59530b57cec5SDimitry Andric void Parser::ParseDeclarator(Declarator &D) {
59540b57cec5SDimitry Andric   /// This implements the 'declarator' production in the C grammar, then checks
59550b57cec5SDimitry Andric   /// for well-formedness and issues diagnostics.
595681ad6265SDimitry Andric   Actions.runWithSufficientStackSpace(D.getBeginLoc(), [&] {
59570b57cec5SDimitry Andric     ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
595881ad6265SDimitry Andric   });
59590b57cec5SDimitry Andric }
59600b57cec5SDimitry Andric 
59610b57cec5SDimitry Andric static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang,
59620b57cec5SDimitry Andric                                DeclaratorContext TheContext) {
59630b57cec5SDimitry Andric   if (Kind == tok::star || Kind == tok::caret)
59640b57cec5SDimitry Andric     return true;
59650b57cec5SDimitry Andric 
59666e75b2fbSDimitry Andric   // OpenCL 2.0 and later define this keyword.
5967349cc55cSDimitry Andric   if (Kind == tok::kw_pipe && Lang.OpenCL &&
5968349cc55cSDimitry Andric       Lang.getOpenCLCompatibleVersion() >= 200)
59690b57cec5SDimitry Andric     return true;
59700b57cec5SDimitry Andric 
59710b57cec5SDimitry Andric   if (!Lang.CPlusPlus)
59720b57cec5SDimitry Andric     return false;
59730b57cec5SDimitry Andric 
59740b57cec5SDimitry Andric   if (Kind == tok::amp)
59750b57cec5SDimitry Andric     return true;
59760b57cec5SDimitry Andric 
59770b57cec5SDimitry Andric   // We parse rvalue refs in C++03, because otherwise the errors are scary.
59780b57cec5SDimitry Andric   // But we must not parse them in conversion-type-ids and new-type-ids, since
59790b57cec5SDimitry Andric   // those can be legitimately followed by a && operator.
59800b57cec5SDimitry Andric   // (The same thing can in theory happen after a trailing-return-type, but
59810b57cec5SDimitry Andric   // since those are a C++11 feature, there is no rejects-valid issue there.)
59820b57cec5SDimitry Andric   if (Kind == tok::ampamp)
5983e8d8bef9SDimitry Andric     return Lang.CPlusPlus11 || (TheContext != DeclaratorContext::ConversionId &&
5984e8d8bef9SDimitry Andric                                 TheContext != DeclaratorContext::CXXNew);
59850b57cec5SDimitry Andric 
59860b57cec5SDimitry Andric   return false;
59870b57cec5SDimitry Andric }
59880b57cec5SDimitry Andric 
59890b57cec5SDimitry Andric // Indicates whether the given declarator is a pipe declarator.
599081ad6265SDimitry Andric static bool isPipeDeclarator(const Declarator &D) {
59910b57cec5SDimitry Andric   const unsigned NumTypes = D.getNumTypeObjects();
59920b57cec5SDimitry Andric 
59930b57cec5SDimitry Andric   for (unsigned Idx = 0; Idx != NumTypes; ++Idx)
59940b57cec5SDimitry Andric     if (DeclaratorChunk::Pipe == D.getTypeObject(Idx).Kind)
59950b57cec5SDimitry Andric       return true;
59960b57cec5SDimitry Andric 
59970b57cec5SDimitry Andric   return false;
59980b57cec5SDimitry Andric }
59990b57cec5SDimitry Andric 
60000b57cec5SDimitry Andric /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
60010b57cec5SDimitry Andric /// is parsed by the function passed to it. Pass null, and the direct-declarator
60020b57cec5SDimitry Andric /// isn't parsed at all, making this function effectively parse the C++
60030b57cec5SDimitry Andric /// ptr-operator production.
60040b57cec5SDimitry Andric ///
60050b57cec5SDimitry Andric /// If the grammar of this construct is extended, matching changes must also be
60060b57cec5SDimitry Andric /// made to TryParseDeclarator and MightBeDeclarator, and possibly to
60070b57cec5SDimitry Andric /// isConstructorDeclarator.
60080b57cec5SDimitry Andric ///
60090b57cec5SDimitry Andric ///       declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
60100b57cec5SDimitry Andric /// [C]     pointer[opt] direct-declarator
60110b57cec5SDimitry Andric /// [C++]   direct-declarator
60120b57cec5SDimitry Andric /// [C++]   ptr-operator declarator
60130b57cec5SDimitry Andric ///
60140b57cec5SDimitry Andric ///       pointer: [C99 6.7.5]
60150b57cec5SDimitry Andric ///         '*' type-qualifier-list[opt]
60160b57cec5SDimitry Andric ///         '*' type-qualifier-list[opt] pointer
60170b57cec5SDimitry Andric ///
60180b57cec5SDimitry Andric ///       ptr-operator:
60190b57cec5SDimitry Andric ///         '*' cv-qualifier-seq[opt]
60200b57cec5SDimitry Andric ///         '&'
60210b57cec5SDimitry Andric /// [C++0x] '&&'
60220b57cec5SDimitry Andric /// [GNU]   '&' restrict[opt] attributes[opt]
60230b57cec5SDimitry Andric /// [GNU?]  '&&' restrict[opt] attributes[opt]
60240b57cec5SDimitry Andric ///         '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
60250b57cec5SDimitry Andric void Parser::ParseDeclaratorInternal(Declarator &D,
60260b57cec5SDimitry Andric                                      DirectDeclParseFunction DirectDeclParser) {
60270b57cec5SDimitry Andric   if (Diags.hasAllExtensionsSilenced())
60280b57cec5SDimitry Andric     D.setExtension();
60290b57cec5SDimitry Andric 
60300b57cec5SDimitry Andric   // C++ member pointers start with a '::' or a nested-name.
60310b57cec5SDimitry Andric   // Member pointers get special handling, since there's no place for the
60320b57cec5SDimitry Andric   // scope spec in the generic path below.
60330b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus &&
60340b57cec5SDimitry Andric       (Tok.is(tok::coloncolon) || Tok.is(tok::kw_decltype) ||
60350b57cec5SDimitry Andric        (Tok.is(tok::identifier) &&
60360b57cec5SDimitry Andric         (NextToken().is(tok::coloncolon) || NextToken().is(tok::less))) ||
60370b57cec5SDimitry Andric        Tok.is(tok::annot_cxxscope))) {
6038e8d8bef9SDimitry Andric     bool EnteringContext = D.getContext() == DeclaratorContext::File ||
6039e8d8bef9SDimitry Andric                            D.getContext() == DeclaratorContext::Member;
60400b57cec5SDimitry Andric     CXXScopeSpec SS;
60415ffd83dbSDimitry Andric     ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
604204eeddc0SDimitry Andric                                    /*ObjectHasErrors=*/false, EnteringContext);
60430b57cec5SDimitry Andric 
60440b57cec5SDimitry Andric     if (SS.isNotEmpty()) {
60450b57cec5SDimitry Andric       if (Tok.isNot(tok::star)) {
60460b57cec5SDimitry Andric         // The scope spec really belongs to the direct-declarator.
60470b57cec5SDimitry Andric         if (D.mayHaveIdentifier())
60480b57cec5SDimitry Andric           D.getCXXScopeSpec() = SS;
60490b57cec5SDimitry Andric         else
60500b57cec5SDimitry Andric           AnnotateScopeToken(SS, true);
60510b57cec5SDimitry Andric 
60520b57cec5SDimitry Andric         if (DirectDeclParser)
60530b57cec5SDimitry Andric           (this->*DirectDeclParser)(D);
60540b57cec5SDimitry Andric         return;
60550b57cec5SDimitry Andric       }
60560b57cec5SDimitry Andric 
6057e8d8bef9SDimitry Andric       if (SS.isValid()) {
6058e8d8bef9SDimitry Andric         checkCompoundToken(SS.getEndLoc(), tok::coloncolon,
6059e8d8bef9SDimitry Andric                            CompoundToken::MemberPtr);
6060e8d8bef9SDimitry Andric       }
6061e8d8bef9SDimitry Andric 
60625ffd83dbSDimitry Andric       SourceLocation StarLoc = ConsumeToken();
60635ffd83dbSDimitry Andric       D.SetRangeEnd(StarLoc);
60640b57cec5SDimitry Andric       DeclSpec DS(AttrFactory);
60650b57cec5SDimitry Andric       ParseTypeQualifierListOpt(DS);
60660b57cec5SDimitry Andric       D.ExtendWithDeclSpec(DS);
60670b57cec5SDimitry Andric 
60680b57cec5SDimitry Andric       // Recurse to parse whatever is left.
606981ad6265SDimitry Andric       Actions.runWithSufficientStackSpace(D.getBeginLoc(), [&] {
60700b57cec5SDimitry Andric         ParseDeclaratorInternal(D, DirectDeclParser);
607181ad6265SDimitry Andric       });
60720b57cec5SDimitry Andric 
60730b57cec5SDimitry Andric       // Sema will have to catch (syntactically invalid) pointers into global
60740b57cec5SDimitry Andric       // scope. It has to catch pointers into namespace scope anyway.
60750b57cec5SDimitry Andric       D.AddTypeInfo(DeclaratorChunk::getMemberPointer(
60765ffd83dbSDimitry Andric                         SS, DS.getTypeQualifiers(), StarLoc, DS.getEndLoc()),
60770b57cec5SDimitry Andric                     std::move(DS.getAttributes()),
60780b57cec5SDimitry Andric                     /* Don't replace range end. */ SourceLocation());
60790b57cec5SDimitry Andric       return;
60800b57cec5SDimitry Andric     }
60810b57cec5SDimitry Andric   }
60820b57cec5SDimitry Andric 
60830b57cec5SDimitry Andric   tok::TokenKind Kind = Tok.getKind();
60840b57cec5SDimitry Andric 
608581ad6265SDimitry Andric   if (D.getDeclSpec().isTypeSpecPipe() && !isPipeDeclarator(D)) {
60860b57cec5SDimitry Andric     DeclSpec DS(AttrFactory);
60870b57cec5SDimitry Andric     ParseTypeQualifierListOpt(DS);
60880b57cec5SDimitry Andric 
60890b57cec5SDimitry Andric     D.AddTypeInfo(
60900b57cec5SDimitry Andric         DeclaratorChunk::getPipe(DS.getTypeQualifiers(), DS.getPipeLoc()),
60910b57cec5SDimitry Andric         std::move(DS.getAttributes()), SourceLocation());
60920b57cec5SDimitry Andric   }
60930b57cec5SDimitry Andric 
60940b57cec5SDimitry Andric   // Not a pointer, C++ reference, or block.
60950b57cec5SDimitry Andric   if (!isPtrOperatorToken(Kind, getLangOpts(), D.getContext())) {
60960b57cec5SDimitry Andric     if (DirectDeclParser)
60970b57cec5SDimitry Andric       (this->*DirectDeclParser)(D);
60980b57cec5SDimitry Andric     return;
60990b57cec5SDimitry Andric   }
61000b57cec5SDimitry Andric 
61010b57cec5SDimitry Andric   // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
61020b57cec5SDimitry Andric   // '&&' -> rvalue reference
61030b57cec5SDimitry Andric   SourceLocation Loc = ConsumeToken();  // Eat the *, ^, & or &&.
61040b57cec5SDimitry Andric   D.SetRangeEnd(Loc);
61050b57cec5SDimitry Andric 
61060b57cec5SDimitry Andric   if (Kind == tok::star || Kind == tok::caret) {
61070b57cec5SDimitry Andric     // Is a pointer.
61080b57cec5SDimitry Andric     DeclSpec DS(AttrFactory);
61090b57cec5SDimitry Andric 
61100b57cec5SDimitry Andric     // GNU attributes are not allowed here in a new-type-id, but Declspec and
61110b57cec5SDimitry Andric     // C++11 attributes are allowed.
61120b57cec5SDimitry Andric     unsigned Reqs = AR_CXX11AttributesParsed | AR_DeclspecAttributesParsed |
6113e8d8bef9SDimitry Andric                     ((D.getContext() != DeclaratorContext::CXXNew)
61140b57cec5SDimitry Andric                          ? AR_GNUAttributesParsed
61150b57cec5SDimitry Andric                          : AR_GNUAttributesParsedAndRejected);
61160b57cec5SDimitry Andric     ParseTypeQualifierListOpt(DS, Reqs, true, !D.mayOmitIdentifier());
61170b57cec5SDimitry Andric     D.ExtendWithDeclSpec(DS);
61180b57cec5SDimitry Andric 
61190b57cec5SDimitry Andric     // Recursively parse the declarator.
612081ad6265SDimitry Andric     Actions.runWithSufficientStackSpace(
612181ad6265SDimitry Andric         D.getBeginLoc(), [&] { ParseDeclaratorInternal(D, DirectDeclParser); });
61220b57cec5SDimitry Andric     if (Kind == tok::star)
61230b57cec5SDimitry Andric       // Remember that we parsed a pointer type, and remember the type-quals.
61240b57cec5SDimitry Andric       D.AddTypeInfo(DeclaratorChunk::getPointer(
61250b57cec5SDimitry Andric                         DS.getTypeQualifiers(), Loc, DS.getConstSpecLoc(),
61260b57cec5SDimitry Andric                         DS.getVolatileSpecLoc(), DS.getRestrictSpecLoc(),
61270b57cec5SDimitry Andric                         DS.getAtomicSpecLoc(), DS.getUnalignedSpecLoc()),
61280b57cec5SDimitry Andric                     std::move(DS.getAttributes()), SourceLocation());
61290b57cec5SDimitry Andric     else
61300b57cec5SDimitry Andric       // Remember that we parsed a Block type, and remember the type-quals.
61310b57cec5SDimitry Andric       D.AddTypeInfo(
61320b57cec5SDimitry Andric           DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), Loc),
61330b57cec5SDimitry Andric           std::move(DS.getAttributes()), SourceLocation());
61340b57cec5SDimitry Andric   } else {
61350b57cec5SDimitry Andric     // Is a reference
61360b57cec5SDimitry Andric     DeclSpec DS(AttrFactory);
61370b57cec5SDimitry Andric 
61380b57cec5SDimitry Andric     // Complain about rvalue references in C++03, but then go on and build
61390b57cec5SDimitry Andric     // the declarator.
61400b57cec5SDimitry Andric     if (Kind == tok::ampamp)
61410b57cec5SDimitry Andric       Diag(Loc, getLangOpts().CPlusPlus11 ?
61420b57cec5SDimitry Andric            diag::warn_cxx98_compat_rvalue_reference :
61430b57cec5SDimitry Andric            diag::ext_rvalue_reference);
61440b57cec5SDimitry Andric 
61450b57cec5SDimitry Andric     // GNU-style and C++11 attributes are allowed here, as is restrict.
61460b57cec5SDimitry Andric     ParseTypeQualifierListOpt(DS);
61470b57cec5SDimitry Andric     D.ExtendWithDeclSpec(DS);
61480b57cec5SDimitry Andric 
61490b57cec5SDimitry Andric     // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
61500b57cec5SDimitry Andric     // cv-qualifiers are introduced through the use of a typedef or of a
61510b57cec5SDimitry Andric     // template type argument, in which case the cv-qualifiers are ignored.
61520b57cec5SDimitry Andric     if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
61530b57cec5SDimitry Andric       if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
61540b57cec5SDimitry Andric         Diag(DS.getConstSpecLoc(),
61550b57cec5SDimitry Andric              diag::err_invalid_reference_qualifier_application) << "const";
61560b57cec5SDimitry Andric       if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
61570b57cec5SDimitry Andric         Diag(DS.getVolatileSpecLoc(),
61580b57cec5SDimitry Andric              diag::err_invalid_reference_qualifier_application) << "volatile";
61590b57cec5SDimitry Andric       // 'restrict' is permitted as an extension.
61600b57cec5SDimitry Andric       if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
61610b57cec5SDimitry Andric         Diag(DS.getAtomicSpecLoc(),
61620b57cec5SDimitry Andric              diag::err_invalid_reference_qualifier_application) << "_Atomic";
61630b57cec5SDimitry Andric     }
61640b57cec5SDimitry Andric 
61650b57cec5SDimitry Andric     // Recursively parse the declarator.
616681ad6265SDimitry Andric     Actions.runWithSufficientStackSpace(
616781ad6265SDimitry Andric         D.getBeginLoc(), [&] { ParseDeclaratorInternal(D, DirectDeclParser); });
61680b57cec5SDimitry Andric 
61690b57cec5SDimitry Andric     if (D.getNumTypeObjects() > 0) {
61700b57cec5SDimitry Andric       // C++ [dcl.ref]p4: There shall be no references to references.
61710b57cec5SDimitry Andric       DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
61720b57cec5SDimitry Andric       if (InnerChunk.Kind == DeclaratorChunk::Reference) {
61730b57cec5SDimitry Andric         if (const IdentifierInfo *II = D.getIdentifier())
61740b57cec5SDimitry Andric           Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
61750b57cec5SDimitry Andric            << II;
61760b57cec5SDimitry Andric         else
61770b57cec5SDimitry Andric           Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
61780b57cec5SDimitry Andric             << "type name";
61790b57cec5SDimitry Andric 
61800b57cec5SDimitry Andric         // Once we've complained about the reference-to-reference, we
61810b57cec5SDimitry Andric         // can go ahead and build the (technically ill-formed)
61820b57cec5SDimitry Andric         // declarator: reference collapsing will take care of it.
61830b57cec5SDimitry Andric       }
61840b57cec5SDimitry Andric     }
61850b57cec5SDimitry Andric 
61860b57cec5SDimitry Andric     // Remember that we parsed a reference type.
61870b57cec5SDimitry Andric     D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
61880b57cec5SDimitry Andric                                                 Kind == tok::amp),
61890b57cec5SDimitry Andric                   std::move(DS.getAttributes()), SourceLocation());
61900b57cec5SDimitry Andric   }
61910b57cec5SDimitry Andric }
61920b57cec5SDimitry Andric 
61930b57cec5SDimitry Andric // When correcting from misplaced brackets before the identifier, the location
61940b57cec5SDimitry Andric // is saved inside the declarator so that other diagnostic messages can use
61950b57cec5SDimitry Andric // them.  This extracts and returns that location, or returns the provided
61960b57cec5SDimitry Andric // location if a stored location does not exist.
61970b57cec5SDimitry Andric static SourceLocation getMissingDeclaratorIdLoc(Declarator &D,
61980b57cec5SDimitry Andric                                                 SourceLocation Loc) {
61990b57cec5SDimitry Andric   if (D.getName().StartLocation.isInvalid() &&
62000b57cec5SDimitry Andric       D.getName().EndLocation.isValid())
62010b57cec5SDimitry Andric     return D.getName().EndLocation;
62020b57cec5SDimitry Andric 
62030b57cec5SDimitry Andric   return Loc;
62040b57cec5SDimitry Andric }
62050b57cec5SDimitry Andric 
62060b57cec5SDimitry Andric /// ParseDirectDeclarator
62070b57cec5SDimitry Andric ///       direct-declarator: [C99 6.7.5]
62080b57cec5SDimitry Andric /// [C99]   identifier
62090b57cec5SDimitry Andric ///         '(' declarator ')'
62100b57cec5SDimitry Andric /// [GNU]   '(' attributes declarator ')'
62110b57cec5SDimitry Andric /// [C90]   direct-declarator '[' constant-expression[opt] ']'
62120b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
62130b57cec5SDimitry Andric /// [C99]   direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
62140b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list 'static' assignment-expr ']'
62150b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list[opt] '*' ']'
62160b57cec5SDimitry Andric /// [C++11] direct-declarator '[' constant-expression[opt] ']'
62170b57cec5SDimitry Andric ///                    attribute-specifier-seq[opt]
62180b57cec5SDimitry Andric ///         direct-declarator '(' parameter-type-list ')'
62190b57cec5SDimitry Andric ///         direct-declarator '(' identifier-list[opt] ')'
62200b57cec5SDimitry Andric /// [GNU]   direct-declarator '(' parameter-forward-declarations
62210b57cec5SDimitry Andric ///                    parameter-type-list[opt] ')'
62220b57cec5SDimitry Andric /// [C++]   direct-declarator '(' parameter-declaration-clause ')'
62230b57cec5SDimitry Andric ///                    cv-qualifier-seq[opt] exception-specification[opt]
62240b57cec5SDimitry Andric /// [C++11] direct-declarator '(' parameter-declaration-clause ')'
62250b57cec5SDimitry Andric ///                    attribute-specifier-seq[opt] cv-qualifier-seq[opt]
62260b57cec5SDimitry Andric ///                    ref-qualifier[opt] exception-specification[opt]
62270b57cec5SDimitry Andric /// [C++]   declarator-id
62280b57cec5SDimitry Andric /// [C++11] declarator-id attribute-specifier-seq[opt]
62290b57cec5SDimitry Andric ///
62300b57cec5SDimitry Andric ///       declarator-id: [C++ 8]
62310b57cec5SDimitry Andric ///         '...'[opt] id-expression
62320b57cec5SDimitry Andric ///         '::'[opt] nested-name-specifier[opt] type-name
62330b57cec5SDimitry Andric ///
62340b57cec5SDimitry Andric ///       id-expression: [C++ 5.1]
62350b57cec5SDimitry Andric ///         unqualified-id
62360b57cec5SDimitry Andric ///         qualified-id
62370b57cec5SDimitry Andric ///
62380b57cec5SDimitry Andric ///       unqualified-id: [C++ 5.1]
62390b57cec5SDimitry Andric ///         identifier
62400b57cec5SDimitry Andric ///         operator-function-id
62410b57cec5SDimitry Andric ///         conversion-function-id
62420b57cec5SDimitry Andric ///          '~' class-name
62430b57cec5SDimitry Andric ///         template-id
62440b57cec5SDimitry Andric ///
62450b57cec5SDimitry Andric /// C++17 adds the following, which we also handle here:
62460b57cec5SDimitry Andric ///
62470b57cec5SDimitry Andric ///       simple-declaration:
62480b57cec5SDimitry Andric ///         <decl-spec> '[' identifier-list ']' brace-or-equal-initializer ';'
62490b57cec5SDimitry Andric ///
62500b57cec5SDimitry Andric /// Note, any additional constructs added here may need corresponding changes
62510b57cec5SDimitry Andric /// in isConstructorDeclarator.
62520b57cec5SDimitry Andric void Parser::ParseDirectDeclarator(Declarator &D) {
62530b57cec5SDimitry Andric   DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
62540b57cec5SDimitry Andric 
62550b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
62560b57cec5SDimitry Andric     // This might be a C++17 structured binding.
62570b57cec5SDimitry Andric     if (Tok.is(tok::l_square) && !D.mayOmitIdentifier() &&
62580b57cec5SDimitry Andric         D.getCXXScopeSpec().isEmpty())
62590b57cec5SDimitry Andric       return ParseDecompositionDeclarator(D);
62600b57cec5SDimitry Andric 
62610b57cec5SDimitry Andric     // Don't parse FOO:BAR as if it were a typo for FOO::BAR inside a class, in
62620b57cec5SDimitry Andric     // this context it is a bitfield. Also in range-based for statement colon
62630b57cec5SDimitry Andric     // may delimit for-range-declaration.
62640b57cec5SDimitry Andric     ColonProtectionRAIIObject X(
6265e8d8bef9SDimitry Andric         *this, D.getContext() == DeclaratorContext::Member ||
6266e8d8bef9SDimitry Andric                    (D.getContext() == DeclaratorContext::ForInit &&
62670b57cec5SDimitry Andric                     getLangOpts().CPlusPlus11));
62680b57cec5SDimitry Andric 
62690b57cec5SDimitry Andric     // ParseDeclaratorInternal might already have parsed the scope.
62700b57cec5SDimitry Andric     if (D.getCXXScopeSpec().isEmpty()) {
6271e8d8bef9SDimitry Andric       bool EnteringContext = D.getContext() == DeclaratorContext::File ||
6272e8d8bef9SDimitry Andric                              D.getContext() == DeclaratorContext::Member;
62735ffd83dbSDimitry Andric       ParseOptionalCXXScopeSpecifier(
62745ffd83dbSDimitry Andric           D.getCXXScopeSpec(), /*ObjectType=*/nullptr,
627504eeddc0SDimitry Andric           /*ObjectHasErrors=*/false, EnteringContext);
62760b57cec5SDimitry Andric     }
62770b57cec5SDimitry Andric 
62780b57cec5SDimitry Andric     if (D.getCXXScopeSpec().isValid()) {
62790b57cec5SDimitry Andric       if (Actions.ShouldEnterDeclaratorScope(getCurScope(),
62800b57cec5SDimitry Andric                                              D.getCXXScopeSpec()))
62810b57cec5SDimitry Andric         // Change the declaration context for name lookup, until this function
62820b57cec5SDimitry Andric         // is exited (and the declarator has been parsed).
62830b57cec5SDimitry Andric         DeclScopeObj.EnterDeclaratorScope();
62840b57cec5SDimitry Andric       else if (getObjCDeclContext()) {
62850b57cec5SDimitry Andric         // Ensure that we don't interpret the next token as an identifier when
62860b57cec5SDimitry Andric         // dealing with declarations in an Objective-C container.
62870b57cec5SDimitry Andric         D.SetIdentifier(nullptr, Tok.getLocation());
62880b57cec5SDimitry Andric         D.setInvalidType(true);
62890b57cec5SDimitry Andric         ConsumeToken();
62900b57cec5SDimitry Andric         goto PastIdentifier;
62910b57cec5SDimitry Andric       }
62920b57cec5SDimitry Andric     }
62930b57cec5SDimitry Andric 
62940b57cec5SDimitry Andric     // C++0x [dcl.fct]p14:
62950b57cec5SDimitry Andric     //   There is a syntactic ambiguity when an ellipsis occurs at the end of a
62960b57cec5SDimitry Andric     //   parameter-declaration-clause without a preceding comma. In this case,
62970b57cec5SDimitry Andric     //   the ellipsis is parsed as part of the abstract-declarator if the type
62980b57cec5SDimitry Andric     //   of the parameter either names a template parameter pack that has not
62990b57cec5SDimitry Andric     //   been expanded or contains auto; otherwise, it is parsed as part of the
63000b57cec5SDimitry Andric     //   parameter-declaration-clause.
63010b57cec5SDimitry Andric     if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
6302e8d8bef9SDimitry Andric         !((D.getContext() == DeclaratorContext::Prototype ||
6303e8d8bef9SDimitry Andric            D.getContext() == DeclaratorContext::LambdaExprParameter ||
6304e8d8bef9SDimitry Andric            D.getContext() == DeclaratorContext::BlockLiteral) &&
6305e8d8bef9SDimitry Andric           NextToken().is(tok::r_paren) && !D.hasGroupingParens() &&
63060b57cec5SDimitry Andric           !Actions.containsUnexpandedParameterPacks(D) &&
63070b57cec5SDimitry Andric           D.getDeclSpec().getTypeSpecType() != TST_auto)) {
63080b57cec5SDimitry Andric       SourceLocation EllipsisLoc = ConsumeToken();
63090b57cec5SDimitry Andric       if (isPtrOperatorToken(Tok.getKind(), getLangOpts(), D.getContext())) {
63100b57cec5SDimitry Andric         // The ellipsis was put in the wrong place. Recover, and explain to
63110b57cec5SDimitry Andric         // the user what they should have done.
63120b57cec5SDimitry Andric         ParseDeclarator(D);
63130b57cec5SDimitry Andric         if (EllipsisLoc.isValid())
63140b57cec5SDimitry Andric           DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
63150b57cec5SDimitry Andric         return;
63160b57cec5SDimitry Andric       } else
63170b57cec5SDimitry Andric         D.setEllipsisLoc(EllipsisLoc);
63180b57cec5SDimitry Andric 
63190b57cec5SDimitry Andric       // The ellipsis can't be followed by a parenthesized declarator. We
63200b57cec5SDimitry Andric       // check for that in ParseParenDeclarator, after we have disambiguated
63210b57cec5SDimitry Andric       // the l_paren token.
63220b57cec5SDimitry Andric     }
63230b57cec5SDimitry Andric 
63240b57cec5SDimitry Andric     if (Tok.isOneOf(tok::identifier, tok::kw_operator, tok::annot_template_id,
63250b57cec5SDimitry Andric                     tok::tilde)) {
63260b57cec5SDimitry Andric       // We found something that indicates the start of an unqualified-id.
63270b57cec5SDimitry Andric       // Parse that unqualified-id.
63280b57cec5SDimitry Andric       bool AllowConstructorName;
63290b57cec5SDimitry Andric       bool AllowDeductionGuide;
63300b57cec5SDimitry Andric       if (D.getDeclSpec().hasTypeSpecifier()) {
63310b57cec5SDimitry Andric         AllowConstructorName = false;
63320b57cec5SDimitry Andric         AllowDeductionGuide = false;
63330b57cec5SDimitry Andric       } else if (D.getCXXScopeSpec().isSet()) {
6334e8d8bef9SDimitry Andric         AllowConstructorName = (D.getContext() == DeclaratorContext::File ||
6335e8d8bef9SDimitry Andric                                 D.getContext() == DeclaratorContext::Member);
63360b57cec5SDimitry Andric         AllowDeductionGuide = false;
63370b57cec5SDimitry Andric       } else {
6338e8d8bef9SDimitry Andric         AllowConstructorName = (D.getContext() == DeclaratorContext::Member);
6339e8d8bef9SDimitry Andric         AllowDeductionGuide = (D.getContext() == DeclaratorContext::File ||
6340e8d8bef9SDimitry Andric                                D.getContext() == DeclaratorContext::Member);
63410b57cec5SDimitry Andric       }
63420b57cec5SDimitry Andric 
63430b57cec5SDimitry Andric       bool HadScope = D.getCXXScopeSpec().isValid();
63440b57cec5SDimitry Andric       if (ParseUnqualifiedId(D.getCXXScopeSpec(),
63455ffd83dbSDimitry Andric                              /*ObjectType=*/nullptr,
63465ffd83dbSDimitry Andric                              /*ObjectHadErrors=*/false,
63470b57cec5SDimitry Andric                              /*EnteringContext=*/true,
63480b57cec5SDimitry Andric                              /*AllowDestructorName=*/true, AllowConstructorName,
63495ffd83dbSDimitry Andric                              AllowDeductionGuide, nullptr, D.getName()) ||
63500b57cec5SDimitry Andric           // Once we're past the identifier, if the scope was bad, mark the
63510b57cec5SDimitry Andric           // whole declarator bad.
63520b57cec5SDimitry Andric           D.getCXXScopeSpec().isInvalid()) {
63530b57cec5SDimitry Andric         D.SetIdentifier(nullptr, Tok.getLocation());
63540b57cec5SDimitry Andric         D.setInvalidType(true);
63550b57cec5SDimitry Andric       } else {
63560b57cec5SDimitry Andric         // ParseUnqualifiedId might have parsed a scope specifier during error
63570b57cec5SDimitry Andric         // recovery. If it did so, enter that scope.
63580b57cec5SDimitry Andric         if (!HadScope && D.getCXXScopeSpec().isValid() &&
63590b57cec5SDimitry Andric             Actions.ShouldEnterDeclaratorScope(getCurScope(),
63600b57cec5SDimitry Andric                                                D.getCXXScopeSpec()))
63610b57cec5SDimitry Andric           DeclScopeObj.EnterDeclaratorScope();
63620b57cec5SDimitry Andric 
63630b57cec5SDimitry Andric         // Parsed the unqualified-id; update range information and move along.
63640b57cec5SDimitry Andric         if (D.getSourceRange().getBegin().isInvalid())
63650b57cec5SDimitry Andric           D.SetRangeBegin(D.getName().getSourceRange().getBegin());
63660b57cec5SDimitry Andric         D.SetRangeEnd(D.getName().getSourceRange().getEnd());
63670b57cec5SDimitry Andric       }
63680b57cec5SDimitry Andric       goto PastIdentifier;
63690b57cec5SDimitry Andric     }
63700b57cec5SDimitry Andric 
63710b57cec5SDimitry Andric     if (D.getCXXScopeSpec().isNotEmpty()) {
63720b57cec5SDimitry Andric       // We have a scope specifier but no following unqualified-id.
63730b57cec5SDimitry Andric       Diag(PP.getLocForEndOfToken(D.getCXXScopeSpec().getEndLoc()),
63740b57cec5SDimitry Andric            diag::err_expected_unqualified_id)
63750b57cec5SDimitry Andric           << /*C++*/1;
63760b57cec5SDimitry Andric       D.SetIdentifier(nullptr, Tok.getLocation());
63770b57cec5SDimitry Andric       goto PastIdentifier;
63780b57cec5SDimitry Andric     }
63790b57cec5SDimitry Andric   } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
63800b57cec5SDimitry Andric     assert(!getLangOpts().CPlusPlus &&
63810b57cec5SDimitry Andric            "There's a C++-specific check for tok::identifier above");
63820b57cec5SDimitry Andric     assert(Tok.getIdentifierInfo() && "Not an identifier?");
63830b57cec5SDimitry Andric     D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
63840b57cec5SDimitry Andric     D.SetRangeEnd(Tok.getLocation());
63850b57cec5SDimitry Andric     ConsumeToken();
63860b57cec5SDimitry Andric     goto PastIdentifier;
63870b57cec5SDimitry Andric   } else if (Tok.is(tok::identifier) && !D.mayHaveIdentifier()) {
63880b57cec5SDimitry Andric     // We're not allowed an identifier here, but we got one. Try to figure out
63890b57cec5SDimitry Andric     // if the user was trying to attach a name to the type, or whether the name
63900b57cec5SDimitry Andric     // is some unrelated trailing syntax.
63910b57cec5SDimitry Andric     bool DiagnoseIdentifier = false;
63920b57cec5SDimitry Andric     if (D.hasGroupingParens())
63930b57cec5SDimitry Andric       // An identifier within parens is unlikely to be intended to be anything
63940b57cec5SDimitry Andric       // other than a name being "declared".
63950b57cec5SDimitry Andric       DiagnoseIdentifier = true;
6396e8d8bef9SDimitry Andric     else if (D.getContext() == DeclaratorContext::TemplateArg)
63970b57cec5SDimitry Andric       // T<int N> is an accidental identifier; T<int N indicates a missing '>'.
63980b57cec5SDimitry Andric       DiagnoseIdentifier =
63990b57cec5SDimitry Andric           NextToken().isOneOf(tok::comma, tok::greater, tok::greatergreater);
6400e8d8bef9SDimitry Andric     else if (D.getContext() == DeclaratorContext::AliasDecl ||
6401e8d8bef9SDimitry Andric              D.getContext() == DeclaratorContext::AliasTemplate)
64020b57cec5SDimitry Andric       // The most likely error is that the ';' was forgotten.
64030b57cec5SDimitry Andric       DiagnoseIdentifier = NextToken().isOneOf(tok::comma, tok::semi);
6404e8d8bef9SDimitry Andric     else if ((D.getContext() == DeclaratorContext::TrailingReturn ||
6405e8d8bef9SDimitry Andric               D.getContext() == DeclaratorContext::TrailingReturnVar) &&
64060b57cec5SDimitry Andric              !isCXX11VirtSpecifier(Tok))
64070b57cec5SDimitry Andric       DiagnoseIdentifier = NextToken().isOneOf(
64080b57cec5SDimitry Andric           tok::comma, tok::semi, tok::equal, tok::l_brace, tok::kw_try);
64090b57cec5SDimitry Andric     if (DiagnoseIdentifier) {
64100b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
64110b57cec5SDimitry Andric         << FixItHint::CreateRemoval(Tok.getLocation());
64120b57cec5SDimitry Andric       D.SetIdentifier(nullptr, Tok.getLocation());
64130b57cec5SDimitry Andric       ConsumeToken();
64140b57cec5SDimitry Andric       goto PastIdentifier;
64150b57cec5SDimitry Andric     }
64160b57cec5SDimitry Andric   }
64170b57cec5SDimitry Andric 
64180b57cec5SDimitry Andric   if (Tok.is(tok::l_paren)) {
64190b57cec5SDimitry Andric     // If this might be an abstract-declarator followed by a direct-initializer,
64200b57cec5SDimitry Andric     // check whether this is a valid declarator chunk. If it can't be, assume
64210b57cec5SDimitry Andric     // that it's an initializer instead.
64220b57cec5SDimitry Andric     if (D.mayOmitIdentifier() && D.mayBeFollowedByCXXDirectInit()) {
64230b57cec5SDimitry Andric       RevertingTentativeParsingAction PA(*this);
64240b57cec5SDimitry Andric       if (TryParseDeclarator(true, D.mayHaveIdentifier(), true) ==
64250b57cec5SDimitry Andric               TPResult::False) {
64260b57cec5SDimitry Andric         D.SetIdentifier(nullptr, Tok.getLocation());
64270b57cec5SDimitry Andric         goto PastIdentifier;
64280b57cec5SDimitry Andric       }
64290b57cec5SDimitry Andric     }
64300b57cec5SDimitry Andric 
64310b57cec5SDimitry Andric     // direct-declarator: '(' declarator ')'
64320b57cec5SDimitry Andric     // direct-declarator: '(' attributes declarator ')'
64330b57cec5SDimitry Andric     // Example: 'char (*X)'   or 'int (*XX)(void)'
64340b57cec5SDimitry Andric     ParseParenDeclarator(D);
64350b57cec5SDimitry Andric 
64360b57cec5SDimitry Andric     // If the declarator was parenthesized, we entered the declarator
64370b57cec5SDimitry Andric     // scope when parsing the parenthesized declarator, then exited
64380b57cec5SDimitry Andric     // the scope already. Re-enter the scope, if we need to.
64390b57cec5SDimitry Andric     if (D.getCXXScopeSpec().isSet()) {
64400b57cec5SDimitry Andric       // If there was an error parsing parenthesized declarator, declarator
64410b57cec5SDimitry Andric       // scope may have been entered before. Don't do it again.
64420b57cec5SDimitry Andric       if (!D.isInvalidType() &&
64430b57cec5SDimitry Andric           Actions.ShouldEnterDeclaratorScope(getCurScope(),
64440b57cec5SDimitry Andric                                              D.getCXXScopeSpec()))
64450b57cec5SDimitry Andric         // Change the declaration context for name lookup, until this function
64460b57cec5SDimitry Andric         // is exited (and the declarator has been parsed).
64470b57cec5SDimitry Andric         DeclScopeObj.EnterDeclaratorScope();
64480b57cec5SDimitry Andric     }
64490b57cec5SDimitry Andric   } else if (D.mayOmitIdentifier()) {
64500b57cec5SDimitry Andric     // This could be something simple like "int" (in which case the declarator
64510b57cec5SDimitry Andric     // portion is empty), if an abstract-declarator is allowed.
64520b57cec5SDimitry Andric     D.SetIdentifier(nullptr, Tok.getLocation());
64530b57cec5SDimitry Andric 
64540b57cec5SDimitry Andric     // The grammar for abstract-pack-declarator does not allow grouping parens.
64550b57cec5SDimitry Andric     // FIXME: Revisit this once core issue 1488 is resolved.
64560b57cec5SDimitry Andric     if (D.hasEllipsis() && D.hasGroupingParens())
64570b57cec5SDimitry Andric       Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
64580b57cec5SDimitry Andric            diag::ext_abstract_pack_declarator_parens);
64590b57cec5SDimitry Andric   } else {
64600b57cec5SDimitry Andric     if (Tok.getKind() == tok::annot_pragma_parser_crash)
64610b57cec5SDimitry Andric       LLVM_BUILTIN_TRAP;
64620b57cec5SDimitry Andric     if (Tok.is(tok::l_square))
64630b57cec5SDimitry Andric       return ParseMisplacedBracketDeclarator(D);
6464e8d8bef9SDimitry Andric     if (D.getContext() == DeclaratorContext::Member) {
64650b57cec5SDimitry Andric       // Objective-C++: Detect C++ keywords and try to prevent further errors by
64660b57cec5SDimitry Andric       // treating these keyword as valid member names.
64670b57cec5SDimitry Andric       if (getLangOpts().ObjC && getLangOpts().CPlusPlus &&
64680b57cec5SDimitry Andric           Tok.getIdentifierInfo() &&
64690b57cec5SDimitry Andric           Tok.getIdentifierInfo()->isCPlusPlusKeyword(getLangOpts())) {
64700b57cec5SDimitry Andric         Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
64710b57cec5SDimitry Andric              diag::err_expected_member_name_or_semi_objcxx_keyword)
64720b57cec5SDimitry Andric             << Tok.getIdentifierInfo()
64730b57cec5SDimitry Andric             << (D.getDeclSpec().isEmpty() ? SourceRange()
64740b57cec5SDimitry Andric                                           : D.getDeclSpec().getSourceRange());
64750b57cec5SDimitry Andric         D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
64760b57cec5SDimitry Andric         D.SetRangeEnd(Tok.getLocation());
64770b57cec5SDimitry Andric         ConsumeToken();
64780b57cec5SDimitry Andric         goto PastIdentifier;
64790b57cec5SDimitry Andric       }
64800b57cec5SDimitry Andric       Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
64810b57cec5SDimitry Andric            diag::err_expected_member_name_or_semi)
64820b57cec5SDimitry Andric           << (D.getDeclSpec().isEmpty() ? SourceRange()
64830b57cec5SDimitry Andric                                         : D.getDeclSpec().getSourceRange());
6484972a253aSDimitry Andric     } else {
6485972a253aSDimitry Andric       if (Tok.getKind() == tok::TokenKind::kw_while) {
6486972a253aSDimitry Andric         Diag(Tok, diag::err_while_loop_outside_of_a_function);
64870b57cec5SDimitry Andric       } else if (getLangOpts().CPlusPlus) {
64880b57cec5SDimitry Andric         if (Tok.isOneOf(tok::period, tok::arrow))
64890b57cec5SDimitry Andric           Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
64900b57cec5SDimitry Andric         else {
64910b57cec5SDimitry Andric           SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
64920b57cec5SDimitry Andric           if (Tok.isAtStartOfLine() && Loc.isValid())
64930b57cec5SDimitry Andric             Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
64940b57cec5SDimitry Andric                 << getLangOpts().CPlusPlus;
64950b57cec5SDimitry Andric           else
64960b57cec5SDimitry Andric             Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
64970b57cec5SDimitry Andric                  diag::err_expected_unqualified_id)
64980b57cec5SDimitry Andric                 << getLangOpts().CPlusPlus;
64990b57cec5SDimitry Andric         }
65000b57cec5SDimitry Andric       } else {
65010b57cec5SDimitry Andric         Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
65020b57cec5SDimitry Andric              diag::err_expected_either)
65030b57cec5SDimitry Andric             << tok::identifier << tok::l_paren;
65040b57cec5SDimitry Andric       }
6505972a253aSDimitry Andric     }
65060b57cec5SDimitry Andric     D.SetIdentifier(nullptr, Tok.getLocation());
65070b57cec5SDimitry Andric     D.setInvalidType(true);
65080b57cec5SDimitry Andric   }
65090b57cec5SDimitry Andric 
65100b57cec5SDimitry Andric  PastIdentifier:
65110b57cec5SDimitry Andric   assert(D.isPastIdentifier() &&
65120b57cec5SDimitry Andric          "Haven't past the location of the identifier yet?");
65130b57cec5SDimitry Andric 
65140b57cec5SDimitry Andric   // Don't parse attributes unless we have parsed an unparenthesized name.
65150b57cec5SDimitry Andric   if (D.hasName() && !D.getNumTypeObjects())
65160b57cec5SDimitry Andric     MaybeParseCXX11Attributes(D);
65170b57cec5SDimitry Andric 
651804eeddc0SDimitry Andric   while (true) {
65190b57cec5SDimitry Andric     if (Tok.is(tok::l_paren)) {
652055e4f9d5SDimitry Andric       bool IsFunctionDeclaration = D.isFunctionDeclaratorAFunctionDeclaration();
65210b57cec5SDimitry Andric       // Enter function-declaration scope, limiting any declarators to the
65220b57cec5SDimitry Andric       // function prototype scope, including parameter declarators.
65230b57cec5SDimitry Andric       ParseScope PrototypeScope(this,
65240b57cec5SDimitry Andric                                 Scope::FunctionPrototypeScope|Scope::DeclScope|
652555e4f9d5SDimitry Andric                                 (IsFunctionDeclaration
65260b57cec5SDimitry Andric                                    ? Scope::FunctionDeclarationScope : 0));
65270b57cec5SDimitry Andric 
65280b57cec5SDimitry Andric       // The paren may be part of a C++ direct initializer, eg. "int x(1);".
65290b57cec5SDimitry Andric       // In such a case, check if we actually have a function declarator; if it
65300b57cec5SDimitry Andric       // is not, the declarator has been fully parsed.
65310b57cec5SDimitry Andric       bool IsAmbiguous = false;
65320b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
6533*bdd1243dSDimitry Andric         // C++2a [temp.res]p5
6534*bdd1243dSDimitry Andric         // A qualified-id is assumed to name a type if
6535*bdd1243dSDimitry Andric         //   - [...]
6536*bdd1243dSDimitry Andric         //   - it is a decl-specifier of the decl-specifier-seq of a
6537*bdd1243dSDimitry Andric         //     - [...]
6538*bdd1243dSDimitry Andric         //     - parameter-declaration in a member-declaration [...]
6539*bdd1243dSDimitry Andric         //     - parameter-declaration in a declarator of a function or function
6540*bdd1243dSDimitry Andric         //       template declaration whose declarator-id is qualified [...]
6541*bdd1243dSDimitry Andric         auto AllowImplicitTypename = ImplicitTypenameContext::No;
6542*bdd1243dSDimitry Andric         if (D.getCXXScopeSpec().isSet())
6543*bdd1243dSDimitry Andric           AllowImplicitTypename =
6544*bdd1243dSDimitry Andric               (ImplicitTypenameContext)Actions.isDeclaratorFunctionLike(D);
6545*bdd1243dSDimitry Andric         else if (D.getContext() == DeclaratorContext::Member) {
6546*bdd1243dSDimitry Andric           AllowImplicitTypename = ImplicitTypenameContext::Yes;
6547*bdd1243dSDimitry Andric         }
6548*bdd1243dSDimitry Andric 
65490b57cec5SDimitry Andric         // The name of the declarator, if any, is tentatively declared within
65500b57cec5SDimitry Andric         // a possible direct initializer.
65510b57cec5SDimitry Andric         TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
6552*bdd1243dSDimitry Andric         bool IsFunctionDecl =
6553*bdd1243dSDimitry Andric             isCXXFunctionDeclarator(&IsAmbiguous, AllowImplicitTypename);
65540b57cec5SDimitry Andric         TentativelyDeclaredIdentifiers.pop_back();
65550b57cec5SDimitry Andric         if (!IsFunctionDecl)
65560b57cec5SDimitry Andric           break;
65570b57cec5SDimitry Andric       }
65580b57cec5SDimitry Andric       ParsedAttributes attrs(AttrFactory);
65590b57cec5SDimitry Andric       BalancedDelimiterTracker T(*this, tok::l_paren);
65600b57cec5SDimitry Andric       T.consumeOpen();
656155e4f9d5SDimitry Andric       if (IsFunctionDeclaration)
656255e4f9d5SDimitry Andric         Actions.ActOnStartFunctionDeclarationDeclarator(D,
656355e4f9d5SDimitry Andric                                                         TemplateParameterDepth);
65640b57cec5SDimitry Andric       ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
656555e4f9d5SDimitry Andric       if (IsFunctionDeclaration)
656655e4f9d5SDimitry Andric         Actions.ActOnFinishFunctionDeclarationDeclarator(D);
65670b57cec5SDimitry Andric       PrototypeScope.Exit();
65680b57cec5SDimitry Andric     } else if (Tok.is(tok::l_square)) {
65690b57cec5SDimitry Andric       ParseBracketDeclarator(D);
6570480093f4SDimitry Andric     } else if (Tok.is(tok::kw_requires) && D.hasGroupingParens()) {
6571480093f4SDimitry Andric       // This declarator is declaring a function, but the requires clause is
6572480093f4SDimitry Andric       // in the wrong place:
6573480093f4SDimitry Andric       //   void (f() requires true);
6574480093f4SDimitry Andric       // instead of
6575480093f4SDimitry Andric       //   void f() requires true;
6576480093f4SDimitry Andric       // or
6577480093f4SDimitry Andric       //   void (f()) requires true;
6578480093f4SDimitry Andric       Diag(Tok, diag::err_requires_clause_inside_parens);
6579480093f4SDimitry Andric       ConsumeToken();
6580480093f4SDimitry Andric       ExprResult TrailingRequiresClause = Actions.CorrectDelayedTyposInExpr(
6581480093f4SDimitry Andric          ParseConstraintLogicalOrExpression(/*IsTrailingRequiresClause=*/true));
6582480093f4SDimitry Andric       if (TrailingRequiresClause.isUsable() && D.isFunctionDeclarator() &&
6583480093f4SDimitry Andric           !D.hasTrailingRequiresClause())
6584480093f4SDimitry Andric         // We're already ill-formed if we got here but we'll accept it anyway.
6585480093f4SDimitry Andric         D.setTrailingRequiresClause(TrailingRequiresClause.get());
65860b57cec5SDimitry Andric     } else {
65870b57cec5SDimitry Andric       break;
65880b57cec5SDimitry Andric     }
65890b57cec5SDimitry Andric   }
65900b57cec5SDimitry Andric }
65910b57cec5SDimitry Andric 
65920b57cec5SDimitry Andric void Parser::ParseDecompositionDeclarator(Declarator &D) {
65930b57cec5SDimitry Andric   assert(Tok.is(tok::l_square));
65940b57cec5SDimitry Andric 
65950b57cec5SDimitry Andric   // If this doesn't look like a structured binding, maybe it's a misplaced
65960b57cec5SDimitry Andric   // array declarator.
65970b57cec5SDimitry Andric   // FIXME: Consume the l_square first so we don't need extra lookahead for
65980b57cec5SDimitry Andric   // this.
65990b57cec5SDimitry Andric   if (!(NextToken().is(tok::identifier) &&
66000b57cec5SDimitry Andric         GetLookAheadToken(2).isOneOf(tok::comma, tok::r_square)) &&
66010b57cec5SDimitry Andric       !(NextToken().is(tok::r_square) &&
66020b57cec5SDimitry Andric         GetLookAheadToken(2).isOneOf(tok::equal, tok::l_brace)))
66030b57cec5SDimitry Andric     return ParseMisplacedBracketDeclarator(D);
66040b57cec5SDimitry Andric 
66050b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_square);
66060b57cec5SDimitry Andric   T.consumeOpen();
66070b57cec5SDimitry Andric 
66080b57cec5SDimitry Andric   SmallVector<DecompositionDeclarator::Binding, 32> Bindings;
66090b57cec5SDimitry Andric   while (Tok.isNot(tok::r_square)) {
66100b57cec5SDimitry Andric     if (!Bindings.empty()) {
66110b57cec5SDimitry Andric       if (Tok.is(tok::comma))
66120b57cec5SDimitry Andric         ConsumeToken();
66130b57cec5SDimitry Andric       else {
66140b57cec5SDimitry Andric         if (Tok.is(tok::identifier)) {
66150b57cec5SDimitry Andric           SourceLocation EndLoc = getEndOfPreviousToken();
66160b57cec5SDimitry Andric           Diag(EndLoc, diag::err_expected)
66170b57cec5SDimitry Andric               << tok::comma << FixItHint::CreateInsertion(EndLoc, ",");
66180b57cec5SDimitry Andric         } else {
66190b57cec5SDimitry Andric           Diag(Tok, diag::err_expected_comma_or_rsquare);
66200b57cec5SDimitry Andric         }
66210b57cec5SDimitry Andric 
66220b57cec5SDimitry Andric         SkipUntil(tok::r_square, tok::comma, tok::identifier,
66230b57cec5SDimitry Andric                   StopAtSemi | StopBeforeMatch);
66240b57cec5SDimitry Andric         if (Tok.is(tok::comma))
66250b57cec5SDimitry Andric           ConsumeToken();
66260b57cec5SDimitry Andric         else if (Tok.isNot(tok::identifier))
66270b57cec5SDimitry Andric           break;
66280b57cec5SDimitry Andric       }
66290b57cec5SDimitry Andric     }
66300b57cec5SDimitry Andric 
66310b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
66320b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::identifier;
66330b57cec5SDimitry Andric       break;
66340b57cec5SDimitry Andric     }
66350b57cec5SDimitry Andric 
66360b57cec5SDimitry Andric     Bindings.push_back({Tok.getIdentifierInfo(), Tok.getLocation()});
66370b57cec5SDimitry Andric     ConsumeToken();
66380b57cec5SDimitry Andric   }
66390b57cec5SDimitry Andric 
66400b57cec5SDimitry Andric   if (Tok.isNot(tok::r_square))
66410b57cec5SDimitry Andric     // We've already diagnosed a problem here.
66420b57cec5SDimitry Andric     T.skipToEnd();
66430b57cec5SDimitry Andric   else {
66440b57cec5SDimitry Andric     // C++17 does not allow the identifier-list in a structured binding
66450b57cec5SDimitry Andric     // to be empty.
66460b57cec5SDimitry Andric     if (Bindings.empty())
66470b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::ext_decomp_decl_empty);
66480b57cec5SDimitry Andric 
66490b57cec5SDimitry Andric     T.consumeClose();
66500b57cec5SDimitry Andric   }
66510b57cec5SDimitry Andric 
66520b57cec5SDimitry Andric   return D.setDecompositionBindings(T.getOpenLocation(), Bindings,
66530b57cec5SDimitry Andric                                     T.getCloseLocation());
66540b57cec5SDimitry Andric }
66550b57cec5SDimitry Andric 
66560b57cec5SDimitry Andric /// ParseParenDeclarator - We parsed the declarator D up to a paren.  This is
66570b57cec5SDimitry Andric /// only called before the identifier, so these are most likely just grouping
66580b57cec5SDimitry Andric /// parens for precedence.  If we find that these are actually function
66590b57cec5SDimitry Andric /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
66600b57cec5SDimitry Andric ///
66610b57cec5SDimitry Andric ///       direct-declarator:
66620b57cec5SDimitry Andric ///         '(' declarator ')'
66630b57cec5SDimitry Andric /// [GNU]   '(' attributes declarator ')'
66640b57cec5SDimitry Andric ///         direct-declarator '(' parameter-type-list ')'
66650b57cec5SDimitry Andric ///         direct-declarator '(' identifier-list[opt] ')'
66660b57cec5SDimitry Andric /// [GNU]   direct-declarator '(' parameter-forward-declarations
66670b57cec5SDimitry Andric ///                    parameter-type-list[opt] ')'
66680b57cec5SDimitry Andric ///
66690b57cec5SDimitry Andric void Parser::ParseParenDeclarator(Declarator &D) {
66700b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
66710b57cec5SDimitry Andric   T.consumeOpen();
66720b57cec5SDimitry Andric 
66730b57cec5SDimitry Andric   assert(!D.isPastIdentifier() && "Should be called before passing identifier");
66740b57cec5SDimitry Andric 
66750b57cec5SDimitry Andric   // Eat any attributes before we look at whether this is a grouping or function
66760b57cec5SDimitry Andric   // declarator paren.  If this is a grouping paren, the attribute applies to
66770b57cec5SDimitry Andric   // the type being built up, for example:
66780b57cec5SDimitry Andric   //     int (__attribute__(()) *x)(long y)
66790b57cec5SDimitry Andric   // If this ends up not being a grouping paren, the attribute applies to the
66800b57cec5SDimitry Andric   // first argument, for example:
66810b57cec5SDimitry Andric   //     int (__attribute__(()) int x)
66820b57cec5SDimitry Andric   // In either case, we need to eat any attributes to be able to determine what
66830b57cec5SDimitry Andric   // sort of paren this is.
66840b57cec5SDimitry Andric   //
66850b57cec5SDimitry Andric   ParsedAttributes attrs(AttrFactory);
66860b57cec5SDimitry Andric   bool RequiresArg = false;
66870b57cec5SDimitry Andric   if (Tok.is(tok::kw___attribute)) {
66880b57cec5SDimitry Andric     ParseGNUAttributes(attrs);
66890b57cec5SDimitry Andric 
66900b57cec5SDimitry Andric     // We require that the argument list (if this is a non-grouping paren) be
66910b57cec5SDimitry Andric     // present even if the attribute list was empty.
66920b57cec5SDimitry Andric     RequiresArg = true;
66930b57cec5SDimitry Andric   }
66940b57cec5SDimitry Andric 
66950b57cec5SDimitry Andric   // Eat any Microsoft extensions.
66960b57cec5SDimitry Andric   ParseMicrosoftTypeAttributes(attrs);
66970b57cec5SDimitry Andric 
66980b57cec5SDimitry Andric   // Eat any Borland extensions.
66990b57cec5SDimitry Andric   if  (Tok.is(tok::kw___pascal))
67000b57cec5SDimitry Andric     ParseBorlandTypeAttributes(attrs);
67010b57cec5SDimitry Andric 
67020b57cec5SDimitry Andric   // If we haven't past the identifier yet (or where the identifier would be
67030b57cec5SDimitry Andric   // stored, if this is an abstract declarator), then this is probably just
67040b57cec5SDimitry Andric   // grouping parens. However, if this could be an abstract-declarator, then
67050b57cec5SDimitry Andric   // this could also be the start of function arguments (consider 'void()').
67060b57cec5SDimitry Andric   bool isGrouping;
67070b57cec5SDimitry Andric 
67080b57cec5SDimitry Andric   if (!D.mayOmitIdentifier()) {
67090b57cec5SDimitry Andric     // If this can't be an abstract-declarator, this *must* be a grouping
67100b57cec5SDimitry Andric     // paren, because we haven't seen the identifier yet.
67110b57cec5SDimitry Andric     isGrouping = true;
67120b57cec5SDimitry Andric   } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
67130b57cec5SDimitry Andric              (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
67140b57cec5SDimitry Andric               NextToken().is(tok::r_paren)) || // C++ int(...)
6715*bdd1243dSDimitry Andric              isDeclarationSpecifier(
6716*bdd1243dSDimitry Andric                  ImplicitTypenameContext::No) || // 'int(int)' is a function.
67170b57cec5SDimitry Andric              isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
67180b57cec5SDimitry Andric     // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
67190b57cec5SDimitry Andric     // considered to be a type, not a K&R identifier-list.
67200b57cec5SDimitry Andric     isGrouping = false;
67210b57cec5SDimitry Andric   } else {
67220b57cec5SDimitry Andric     // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
67230b57cec5SDimitry Andric     isGrouping = true;
67240b57cec5SDimitry Andric   }
67250b57cec5SDimitry Andric 
67260b57cec5SDimitry Andric   // If this is a grouping paren, handle:
67270b57cec5SDimitry Andric   // direct-declarator: '(' declarator ')'
67280b57cec5SDimitry Andric   // direct-declarator: '(' attributes declarator ')'
67290b57cec5SDimitry Andric   if (isGrouping) {
67300b57cec5SDimitry Andric     SourceLocation EllipsisLoc = D.getEllipsisLoc();
67310b57cec5SDimitry Andric     D.setEllipsisLoc(SourceLocation());
67320b57cec5SDimitry Andric 
67330b57cec5SDimitry Andric     bool hadGroupingParens = D.hasGroupingParens();
67340b57cec5SDimitry Andric     D.setGroupingParens(true);
67350b57cec5SDimitry Andric     ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
67360b57cec5SDimitry Andric     // Match the ')'.
67370b57cec5SDimitry Andric     T.consumeClose();
67380b57cec5SDimitry Andric     D.AddTypeInfo(
67390b57cec5SDimitry Andric         DeclaratorChunk::getParen(T.getOpenLocation(), T.getCloseLocation()),
67400b57cec5SDimitry Andric         std::move(attrs), T.getCloseLocation());
67410b57cec5SDimitry Andric 
67420b57cec5SDimitry Andric     D.setGroupingParens(hadGroupingParens);
67430b57cec5SDimitry Andric 
67440b57cec5SDimitry Andric     // An ellipsis cannot be placed outside parentheses.
67450b57cec5SDimitry Andric     if (EllipsisLoc.isValid())
67460b57cec5SDimitry Andric       DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
67470b57cec5SDimitry Andric 
67480b57cec5SDimitry Andric     return;
67490b57cec5SDimitry Andric   }
67500b57cec5SDimitry Andric 
67510b57cec5SDimitry Andric   // Okay, if this wasn't a grouping paren, it must be the start of a function
67520b57cec5SDimitry Andric   // argument list.  Recognize that this declarator will never have an
67530b57cec5SDimitry Andric   // identifier (and remember where it would have been), then call into
67540b57cec5SDimitry Andric   // ParseFunctionDeclarator to handle of argument list.
67550b57cec5SDimitry Andric   D.SetIdentifier(nullptr, Tok.getLocation());
67560b57cec5SDimitry Andric 
67570b57cec5SDimitry Andric   // Enter function-declaration scope, limiting any declarators to the
67580b57cec5SDimitry Andric   // function prototype scope, including parameter declarators.
67590b57cec5SDimitry Andric   ParseScope PrototypeScope(this,
67600b57cec5SDimitry Andric                             Scope::FunctionPrototypeScope | Scope::DeclScope |
67610b57cec5SDimitry Andric                             (D.isFunctionDeclaratorAFunctionDeclaration()
67620b57cec5SDimitry Andric                                ? Scope::FunctionDeclarationScope : 0));
67630b57cec5SDimitry Andric   ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
67640b57cec5SDimitry Andric   PrototypeScope.Exit();
67650b57cec5SDimitry Andric }
67660b57cec5SDimitry Andric 
6767480093f4SDimitry Andric void Parser::InitCXXThisScopeForDeclaratorIfRelevant(
6768480093f4SDimitry Andric     const Declarator &D, const DeclSpec &DS,
6769*bdd1243dSDimitry Andric     std::optional<Sema::CXXThisScopeRAII> &ThisScope) {
6770480093f4SDimitry Andric   // C++11 [expr.prim.general]p3:
6771480093f4SDimitry Andric   //   If a declaration declares a member function or member function
6772480093f4SDimitry Andric   //   template of a class X, the expression this is a prvalue of type
6773480093f4SDimitry Andric   //   "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
6774480093f4SDimitry Andric   //   and the end of the function-definition, member-declarator, or
6775480093f4SDimitry Andric   //   declarator.
6776480093f4SDimitry Andric   // FIXME: currently, "static" case isn't handled correctly.
6777e8d8bef9SDimitry Andric   bool IsCXX11MemberFunction =
6778e8d8bef9SDimitry Andric       getLangOpts().CPlusPlus11 &&
6779480093f4SDimitry Andric       D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
6780e8d8bef9SDimitry Andric       (D.getContext() == DeclaratorContext::Member
6781480093f4SDimitry Andric            ? !D.getDeclSpec().isFriendSpecified()
6782e8d8bef9SDimitry Andric            : D.getContext() == DeclaratorContext::File &&
6783480093f4SDimitry Andric                  D.getCXXScopeSpec().isValid() &&
6784480093f4SDimitry Andric                  Actions.CurContext->isRecord());
6785480093f4SDimitry Andric   if (!IsCXX11MemberFunction)
6786480093f4SDimitry Andric     return;
6787480093f4SDimitry Andric 
6788480093f4SDimitry Andric   Qualifiers Q = Qualifiers::fromCVRUMask(DS.getTypeQualifiers());
6789480093f4SDimitry Andric   if (D.getDeclSpec().hasConstexprSpecifier() && !getLangOpts().CPlusPlus14)
6790480093f4SDimitry Andric     Q.addConst();
6791480093f4SDimitry Andric   // FIXME: Collect C++ address spaces.
6792480093f4SDimitry Andric   // If there are multiple different address spaces, the source is invalid.
6793480093f4SDimitry Andric   // Carry on using the first addr space for the qualifiers of 'this'.
6794480093f4SDimitry Andric   // The diagnostic will be given later while creating the function
6795480093f4SDimitry Andric   // prototype for the method.
6796480093f4SDimitry Andric   if (getLangOpts().OpenCLCPlusPlus) {
6797480093f4SDimitry Andric     for (ParsedAttr &attr : DS.getAttributes()) {
6798480093f4SDimitry Andric       LangAS ASIdx = attr.asOpenCLLangAS();
6799480093f4SDimitry Andric       if (ASIdx != LangAS::Default) {
6800480093f4SDimitry Andric         Q.addAddressSpace(ASIdx);
6801480093f4SDimitry Andric         break;
6802480093f4SDimitry Andric       }
6803480093f4SDimitry Andric     }
6804480093f4SDimitry Andric   }
6805480093f4SDimitry Andric   ThisScope.emplace(Actions, dyn_cast<CXXRecordDecl>(Actions.CurContext), Q,
6806480093f4SDimitry Andric                     IsCXX11MemberFunction);
6807480093f4SDimitry Andric }
6808480093f4SDimitry Andric 
68090b57cec5SDimitry Andric /// ParseFunctionDeclarator - We are after the identifier and have parsed the
68100b57cec5SDimitry Andric /// declarator D up to a paren, which indicates that we are parsing function
68110b57cec5SDimitry Andric /// arguments.
68120b57cec5SDimitry Andric ///
681381ad6265SDimitry Andric /// If FirstArgAttrs is non-null, then the caller parsed those attributes
681481ad6265SDimitry Andric /// immediately after the open paren - they will be applied to the DeclSpec
681581ad6265SDimitry Andric /// of the first parameter.
68160b57cec5SDimitry Andric ///
68170b57cec5SDimitry Andric /// If RequiresArg is true, then the first argument of the function is required
68180b57cec5SDimitry Andric /// to be present and required to not be an identifier list.
68190b57cec5SDimitry Andric ///
68200b57cec5SDimitry Andric /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
68210b57cec5SDimitry Andric /// (C++11) ref-qualifier[opt], exception-specification[opt],
6822480093f4SDimitry Andric /// (C++11) attribute-specifier-seq[opt], (C++11) trailing-return-type[opt] and
6823480093f4SDimitry Andric /// (C++2a) the trailing requires-clause.
68240b57cec5SDimitry Andric ///
68250b57cec5SDimitry Andric /// [C++11] exception-specification:
68260b57cec5SDimitry Andric ///           dynamic-exception-specification
68270b57cec5SDimitry Andric ///           noexcept-specification
68280b57cec5SDimitry Andric ///
68290b57cec5SDimitry Andric void Parser::ParseFunctionDeclarator(Declarator &D,
68300b57cec5SDimitry Andric                                      ParsedAttributes &FirstArgAttrs,
68310b57cec5SDimitry Andric                                      BalancedDelimiterTracker &Tracker,
68320b57cec5SDimitry Andric                                      bool IsAmbiguous,
68330b57cec5SDimitry Andric                                      bool RequiresArg) {
68340b57cec5SDimitry Andric   assert(getCurScope()->isFunctionPrototypeScope() &&
68350b57cec5SDimitry Andric          "Should call from a Function scope");
68360b57cec5SDimitry Andric   // lparen is already consumed!
68370b57cec5SDimitry Andric   assert(D.isPastIdentifier() && "Should not call before identifier!");
68380b57cec5SDimitry Andric 
68390b57cec5SDimitry Andric   // This should be true when the function has typed arguments.
68400b57cec5SDimitry Andric   // Otherwise, it is treated as a K&R-style function.
68410b57cec5SDimitry Andric   bool HasProto = false;
68420b57cec5SDimitry Andric   // Build up an array of information about the parsed arguments.
68430b57cec5SDimitry Andric   SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
68440b57cec5SDimitry Andric   // Remember where we see an ellipsis, if any.
68450b57cec5SDimitry Andric   SourceLocation EllipsisLoc;
68460b57cec5SDimitry Andric 
68470b57cec5SDimitry Andric   DeclSpec DS(AttrFactory);
68480b57cec5SDimitry Andric   bool RefQualifierIsLValueRef = true;
68490b57cec5SDimitry Andric   SourceLocation RefQualifierLoc;
68500b57cec5SDimitry Andric   ExceptionSpecificationType ESpecType = EST_None;
68510b57cec5SDimitry Andric   SourceRange ESpecRange;
68520b57cec5SDimitry Andric   SmallVector<ParsedType, 2> DynamicExceptions;
68530b57cec5SDimitry Andric   SmallVector<SourceRange, 2> DynamicExceptionRanges;
68540b57cec5SDimitry Andric   ExprResult NoexceptExpr;
68550b57cec5SDimitry Andric   CachedTokens *ExceptionSpecTokens = nullptr;
685681ad6265SDimitry Andric   ParsedAttributes FnAttrs(AttrFactory);
68570b57cec5SDimitry Andric   TypeResult TrailingReturnType;
6858e8d8bef9SDimitry Andric   SourceLocation TrailingReturnTypeLoc;
68590b57cec5SDimitry Andric 
68600b57cec5SDimitry Andric   /* LocalEndLoc is the end location for the local FunctionTypeLoc.
68610b57cec5SDimitry Andric      EndLoc is the end location for the function declarator.
68620b57cec5SDimitry Andric      They differ for trailing return types. */
68630b57cec5SDimitry Andric   SourceLocation StartLoc, LocalEndLoc, EndLoc;
68640b57cec5SDimitry Andric   SourceLocation LParenLoc, RParenLoc;
68650b57cec5SDimitry Andric   LParenLoc = Tracker.getOpenLocation();
68660b57cec5SDimitry Andric   StartLoc = LParenLoc;
68670b57cec5SDimitry Andric 
68680b57cec5SDimitry Andric   if (isFunctionDeclaratorIdentifierList()) {
68690b57cec5SDimitry Andric     if (RequiresArg)
68700b57cec5SDimitry Andric       Diag(Tok, diag::err_argument_required_after_attribute);
68710b57cec5SDimitry Andric 
68720b57cec5SDimitry Andric     ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
68730b57cec5SDimitry Andric 
68740b57cec5SDimitry Andric     Tracker.consumeClose();
68750b57cec5SDimitry Andric     RParenLoc = Tracker.getCloseLocation();
68760b57cec5SDimitry Andric     LocalEndLoc = RParenLoc;
68770b57cec5SDimitry Andric     EndLoc = RParenLoc;
68780b57cec5SDimitry Andric 
68790b57cec5SDimitry Andric     // If there are attributes following the identifier list, parse them and
68800b57cec5SDimitry Andric     // prohibit them.
68810b57cec5SDimitry Andric     MaybeParseCXX11Attributes(FnAttrs);
68820b57cec5SDimitry Andric     ProhibitAttributes(FnAttrs);
68830b57cec5SDimitry Andric   } else {
68840b57cec5SDimitry Andric     if (Tok.isNot(tok::r_paren))
6885*bdd1243dSDimitry Andric       ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
68860b57cec5SDimitry Andric     else if (RequiresArg)
68870b57cec5SDimitry Andric       Diag(Tok, diag::err_argument_required_after_attribute);
68880b57cec5SDimitry Andric 
688981ad6265SDimitry Andric     // OpenCL disallows functions without a prototype, but it doesn't enforce
689081ad6265SDimitry Andric     // strict prototypes as in C2x because it allows a function definition to
689181ad6265SDimitry Andric     // have an identifier list. See OpenCL 3.0 6.11/g for more details.
689281ad6265SDimitry Andric     HasProto = ParamInfo.size() || getLangOpts().requiresStrictPrototypes() ||
689381ad6265SDimitry Andric                getLangOpts().OpenCL;
68940b57cec5SDimitry Andric 
68950b57cec5SDimitry Andric     // If we have the closing ')', eat it.
68960b57cec5SDimitry Andric     Tracker.consumeClose();
68970b57cec5SDimitry Andric     RParenLoc = Tracker.getCloseLocation();
68980b57cec5SDimitry Andric     LocalEndLoc = RParenLoc;
68990b57cec5SDimitry Andric     EndLoc = RParenLoc;
69000b57cec5SDimitry Andric 
69010b57cec5SDimitry Andric     if (getLangOpts().CPlusPlus) {
69020b57cec5SDimitry Andric       // FIXME: Accept these components in any order, and produce fixits to
69030b57cec5SDimitry Andric       // correct the order if the user gets it wrong. Ideally we should deal
69040b57cec5SDimitry Andric       // with the pure-specifier in the same way.
69050b57cec5SDimitry Andric 
69060b57cec5SDimitry Andric       // Parse cv-qualifier-seq[opt].
69070b57cec5SDimitry Andric       ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed,
69080b57cec5SDimitry Andric                                 /*AtomicAllowed*/ false,
69090b57cec5SDimitry Andric                                 /*IdentifierRequired=*/false,
69100b57cec5SDimitry Andric                                 llvm::function_ref<void()>([&]() {
69110b57cec5SDimitry Andric                                   Actions.CodeCompleteFunctionQualifiers(DS, D);
69120b57cec5SDimitry Andric                                 }));
69130b57cec5SDimitry Andric       if (!DS.getSourceRange().getEnd().isInvalid()) {
69140b57cec5SDimitry Andric         EndLoc = DS.getSourceRange().getEnd();
69150b57cec5SDimitry Andric       }
69160b57cec5SDimitry Andric 
69170b57cec5SDimitry Andric       // Parse ref-qualifier[opt].
69180b57cec5SDimitry Andric       if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc))
69190b57cec5SDimitry Andric         EndLoc = RefQualifierLoc;
69200b57cec5SDimitry Andric 
6921*bdd1243dSDimitry Andric       std::optional<Sema::CXXThisScopeRAII> ThisScope;
6922480093f4SDimitry Andric       InitCXXThisScopeForDeclaratorIfRelevant(D, DS, ThisScope);
69230b57cec5SDimitry Andric 
69240b57cec5SDimitry Andric       // Parse exception-specification[opt].
6925e8d8bef9SDimitry Andric       // FIXME: Per [class.mem]p6, all exception-specifications at class scope
6926e8d8bef9SDimitry Andric       // should be delayed, including those for non-members (eg, friend
6927e8d8bef9SDimitry Andric       // declarations). But only applying this to member declarations is
6928e8d8bef9SDimitry Andric       // consistent with what other implementations do.
69290b57cec5SDimitry Andric       bool Delayed = D.isFirstDeclarationOfMember() &&
69300b57cec5SDimitry Andric                      D.isFunctionDeclaratorAFunctionDeclaration();
69310b57cec5SDimitry Andric       if (Delayed && Actions.isLibstdcxxEagerExceptionSpecHack(D) &&
69320b57cec5SDimitry Andric           GetLookAheadToken(0).is(tok::kw_noexcept) &&
69330b57cec5SDimitry Andric           GetLookAheadToken(1).is(tok::l_paren) &&
69340b57cec5SDimitry Andric           GetLookAheadToken(2).is(tok::kw_noexcept) &&
69350b57cec5SDimitry Andric           GetLookAheadToken(3).is(tok::l_paren) &&
69360b57cec5SDimitry Andric           GetLookAheadToken(4).is(tok::identifier) &&
69370b57cec5SDimitry Andric           GetLookAheadToken(4).getIdentifierInfo()->isStr("swap")) {
69380b57cec5SDimitry Andric         // HACK: We've got an exception-specification
69390b57cec5SDimitry Andric         //   noexcept(noexcept(swap(...)))
69400b57cec5SDimitry Andric         // or
69410b57cec5SDimitry Andric         //   noexcept(noexcept(swap(...)) && noexcept(swap(...)))
69420b57cec5SDimitry Andric         // on a 'swap' member function. This is a libstdc++ bug; the lookup
69430b57cec5SDimitry Andric         // for 'swap' will only find the function we're currently declaring,
69440b57cec5SDimitry Andric         // whereas it expects to find a non-member swap through ADL. Turn off
69450b57cec5SDimitry Andric         // delayed parsing to give it a chance to find what it expects.
69460b57cec5SDimitry Andric         Delayed = false;
69470b57cec5SDimitry Andric       }
69480b57cec5SDimitry Andric       ESpecType = tryParseExceptionSpecification(Delayed,
69490b57cec5SDimitry Andric                                                  ESpecRange,
69500b57cec5SDimitry Andric                                                  DynamicExceptions,
69510b57cec5SDimitry Andric                                                  DynamicExceptionRanges,
69520b57cec5SDimitry Andric                                                  NoexceptExpr,
69530b57cec5SDimitry Andric                                                  ExceptionSpecTokens);
69540b57cec5SDimitry Andric       if (ESpecType != EST_None)
69550b57cec5SDimitry Andric         EndLoc = ESpecRange.getEnd();
69560b57cec5SDimitry Andric 
69570b57cec5SDimitry Andric       // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
69580b57cec5SDimitry Andric       // after the exception-specification.
69590b57cec5SDimitry Andric       MaybeParseCXX11Attributes(FnAttrs);
69600b57cec5SDimitry Andric 
69610b57cec5SDimitry Andric       // Parse trailing-return-type[opt].
69620b57cec5SDimitry Andric       LocalEndLoc = EndLoc;
69630b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
69640b57cec5SDimitry Andric         Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
69650b57cec5SDimitry Andric         if (D.getDeclSpec().getTypeSpecType() == TST_auto)
69660b57cec5SDimitry Andric           StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
69670b57cec5SDimitry Andric         LocalEndLoc = Tok.getLocation();
69680b57cec5SDimitry Andric         SourceRange Range;
69690b57cec5SDimitry Andric         TrailingReturnType =
69700b57cec5SDimitry Andric             ParseTrailingReturnType(Range, D.mayBeFollowedByCXXDirectInit());
6971e8d8bef9SDimitry Andric         TrailingReturnTypeLoc = Range.getBegin();
69720b57cec5SDimitry Andric         EndLoc = Range.getEnd();
69730b57cec5SDimitry Andric       }
69740b57cec5SDimitry Andric     } else if (standardAttributesAllowed()) {
69750b57cec5SDimitry Andric       MaybeParseCXX11Attributes(FnAttrs);
69760b57cec5SDimitry Andric     }
69770b57cec5SDimitry Andric   }
69780b57cec5SDimitry Andric 
69790b57cec5SDimitry Andric   // Collect non-parameter declarations from the prototype if this is a function
69800b57cec5SDimitry Andric   // declaration. They will be moved into the scope of the function. Only do
69810b57cec5SDimitry Andric   // this in C and not C++, where the decls will continue to live in the
69820b57cec5SDimitry Andric   // surrounding context.
69830b57cec5SDimitry Andric   SmallVector<NamedDecl *, 0> DeclsInPrototype;
698481ad6265SDimitry Andric   if (getCurScope()->isFunctionDeclarationScope() && !getLangOpts().CPlusPlus) {
69850b57cec5SDimitry Andric     for (Decl *D : getCurScope()->decls()) {
69860b57cec5SDimitry Andric       NamedDecl *ND = dyn_cast<NamedDecl>(D);
69870b57cec5SDimitry Andric       if (!ND || isa<ParmVarDecl>(ND))
69880b57cec5SDimitry Andric         continue;
69890b57cec5SDimitry Andric       DeclsInPrototype.push_back(ND);
69900b57cec5SDimitry Andric     }
69910b57cec5SDimitry Andric   }
69920b57cec5SDimitry Andric 
69930b57cec5SDimitry Andric   // Remember that we parsed a function type, and remember the attributes.
69940b57cec5SDimitry Andric   D.AddTypeInfo(DeclaratorChunk::getFunction(
69950b57cec5SDimitry Andric                     HasProto, IsAmbiguous, LParenLoc, ParamInfo.data(),
69960b57cec5SDimitry Andric                     ParamInfo.size(), EllipsisLoc, RParenLoc,
69970b57cec5SDimitry Andric                     RefQualifierIsLValueRef, RefQualifierLoc,
69980b57cec5SDimitry Andric                     /*MutableLoc=*/SourceLocation(),
69990b57cec5SDimitry Andric                     ESpecType, ESpecRange, DynamicExceptions.data(),
70000b57cec5SDimitry Andric                     DynamicExceptionRanges.data(), DynamicExceptions.size(),
70010b57cec5SDimitry Andric                     NoexceptExpr.isUsable() ? NoexceptExpr.get() : nullptr,
70020b57cec5SDimitry Andric                     ExceptionSpecTokens, DeclsInPrototype, StartLoc,
7003e8d8bef9SDimitry Andric                     LocalEndLoc, D, TrailingReturnType, TrailingReturnTypeLoc,
7004e8d8bef9SDimitry Andric                     &DS),
70050b57cec5SDimitry Andric                 std::move(FnAttrs), EndLoc);
70060b57cec5SDimitry Andric }
70070b57cec5SDimitry Andric 
70080b57cec5SDimitry Andric /// ParseRefQualifier - Parses a member function ref-qualifier. Returns
70090b57cec5SDimitry Andric /// true if a ref-qualifier is found.
70100b57cec5SDimitry Andric bool Parser::ParseRefQualifier(bool &RefQualifierIsLValueRef,
70110b57cec5SDimitry Andric                                SourceLocation &RefQualifierLoc) {
70120b57cec5SDimitry Andric   if (Tok.isOneOf(tok::amp, tok::ampamp)) {
70130b57cec5SDimitry Andric     Diag(Tok, getLangOpts().CPlusPlus11 ?
70140b57cec5SDimitry Andric          diag::warn_cxx98_compat_ref_qualifier :
70150b57cec5SDimitry Andric          diag::ext_ref_qualifier);
70160b57cec5SDimitry Andric 
70170b57cec5SDimitry Andric     RefQualifierIsLValueRef = Tok.is(tok::amp);
70180b57cec5SDimitry Andric     RefQualifierLoc = ConsumeToken();
70190b57cec5SDimitry Andric     return true;
70200b57cec5SDimitry Andric   }
70210b57cec5SDimitry Andric   return false;
70220b57cec5SDimitry Andric }
70230b57cec5SDimitry Andric 
70240b57cec5SDimitry Andric /// isFunctionDeclaratorIdentifierList - This parameter list may have an
70250b57cec5SDimitry Andric /// identifier list form for a K&R-style function:  void foo(a,b,c)
70260b57cec5SDimitry Andric ///
70270b57cec5SDimitry Andric /// Note that identifier-lists are only allowed for normal declarators, not for
70280b57cec5SDimitry Andric /// abstract-declarators.
70290b57cec5SDimitry Andric bool Parser::isFunctionDeclaratorIdentifierList() {
703081ad6265SDimitry Andric   return !getLangOpts().requiresStrictPrototypes()
70310b57cec5SDimitry Andric          && Tok.is(tok::identifier)
70320b57cec5SDimitry Andric          && !TryAltiVecVectorToken()
70330b57cec5SDimitry Andric          // K&R identifier lists can't have typedefs as identifiers, per C99
70340b57cec5SDimitry Andric          // 6.7.5.3p11.
70350b57cec5SDimitry Andric          && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
70360b57cec5SDimitry Andric          // Identifier lists follow a really simple grammar: the identifiers can
70370b57cec5SDimitry Andric          // be followed *only* by a ", identifier" or ")".  However, K&R
70380b57cec5SDimitry Andric          // identifier lists are really rare in the brave new modern world, and
70390b57cec5SDimitry Andric          // it is very common for someone to typo a type in a non-K&R style
70400b57cec5SDimitry Andric          // list.  If we are presented with something like: "void foo(intptr x,
70410b57cec5SDimitry Andric          // float y)", we don't want to start parsing the function declarator as
70420b57cec5SDimitry Andric          // though it is a K&R style declarator just because intptr is an
70430b57cec5SDimitry Andric          // invalid type.
70440b57cec5SDimitry Andric          //
70450b57cec5SDimitry Andric          // To handle this, we check to see if the token after the first
70460b57cec5SDimitry Andric          // identifier is a "," or ")".  Only then do we parse it as an
70470b57cec5SDimitry Andric          // identifier list.
70480b57cec5SDimitry Andric          && (!Tok.is(tok::eof) &&
70490b57cec5SDimitry Andric              (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)));
70500b57cec5SDimitry Andric }
70510b57cec5SDimitry Andric 
70520b57cec5SDimitry Andric /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
70530b57cec5SDimitry Andric /// we found a K&R-style identifier list instead of a typed parameter list.
70540b57cec5SDimitry Andric ///
70550b57cec5SDimitry Andric /// After returning, ParamInfo will hold the parsed parameters.
70560b57cec5SDimitry Andric ///
70570b57cec5SDimitry Andric ///       identifier-list: [C99 6.7.5]
70580b57cec5SDimitry Andric ///         identifier
70590b57cec5SDimitry Andric ///         identifier-list ',' identifier
70600b57cec5SDimitry Andric ///
70610b57cec5SDimitry Andric void Parser::ParseFunctionDeclaratorIdentifierList(
70620b57cec5SDimitry Andric        Declarator &D,
70630b57cec5SDimitry Andric        SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
706481ad6265SDimitry Andric   // We should never reach this point in C2x or C++.
706581ad6265SDimitry Andric   assert(!getLangOpts().requiresStrictPrototypes() &&
706681ad6265SDimitry Andric          "Cannot parse an identifier list in C2x or C++");
706781ad6265SDimitry Andric 
70680b57cec5SDimitry Andric   // If there was no identifier specified for the declarator, either we are in
70690b57cec5SDimitry Andric   // an abstract-declarator, or we are in a parameter declarator which was found
70700b57cec5SDimitry Andric   // to be abstract.  In abstract-declarators, identifier lists are not valid:
70710b57cec5SDimitry Andric   // diagnose this.
70720b57cec5SDimitry Andric   if (!D.getIdentifier())
70730b57cec5SDimitry Andric     Diag(Tok, diag::ext_ident_list_in_param);
70740b57cec5SDimitry Andric 
70750b57cec5SDimitry Andric   // Maintain an efficient lookup of params we have seen so far.
70760b57cec5SDimitry Andric   llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
70770b57cec5SDimitry Andric 
70780b57cec5SDimitry Andric   do {
70790b57cec5SDimitry Andric     // If this isn't an identifier, report the error and skip until ')'.
70800b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
70810b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::identifier;
70820b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
70830b57cec5SDimitry Andric       // Forget we parsed anything.
70840b57cec5SDimitry Andric       ParamInfo.clear();
70850b57cec5SDimitry Andric       return;
70860b57cec5SDimitry Andric     }
70870b57cec5SDimitry Andric 
70880b57cec5SDimitry Andric     IdentifierInfo *ParmII = Tok.getIdentifierInfo();
70890b57cec5SDimitry Andric 
70900b57cec5SDimitry Andric     // Reject 'typedef int y; int test(x, y)', but continue parsing.
70910b57cec5SDimitry Andric     if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
70920b57cec5SDimitry Andric       Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
70930b57cec5SDimitry Andric 
70940b57cec5SDimitry Andric     // Verify that the argument identifier has not already been mentioned.
70950b57cec5SDimitry Andric     if (!ParamsSoFar.insert(ParmII).second) {
70960b57cec5SDimitry Andric       Diag(Tok, diag::err_param_redefinition) << ParmII;
70970b57cec5SDimitry Andric     } else {
70980b57cec5SDimitry Andric       // Remember this identifier in ParamInfo.
70990b57cec5SDimitry Andric       ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
71000b57cec5SDimitry Andric                                                      Tok.getLocation(),
71010b57cec5SDimitry Andric                                                      nullptr));
71020b57cec5SDimitry Andric     }
71030b57cec5SDimitry Andric 
71040b57cec5SDimitry Andric     // Eat the identifier.
71050b57cec5SDimitry Andric     ConsumeToken();
71060b57cec5SDimitry Andric     // The list continues if we see a comma.
71070b57cec5SDimitry Andric   } while (TryConsumeToken(tok::comma));
71080b57cec5SDimitry Andric }
71090b57cec5SDimitry Andric 
71100b57cec5SDimitry Andric /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
71110b57cec5SDimitry Andric /// after the opening parenthesis. This function will not parse a K&R-style
71120b57cec5SDimitry Andric /// identifier list.
71130b57cec5SDimitry Andric ///
711455e4f9d5SDimitry Andric /// DeclContext is the context of the declarator being parsed.  If FirstArgAttrs
711555e4f9d5SDimitry Andric /// is non-null, then the caller parsed those attributes immediately after the
711681ad6265SDimitry Andric /// open paren - they will be applied to the DeclSpec of the first parameter.
71170b57cec5SDimitry Andric ///
71180b57cec5SDimitry Andric /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
71190b57cec5SDimitry Andric /// be the location of the ellipsis, if any was parsed.
71200b57cec5SDimitry Andric ///
71210b57cec5SDimitry Andric ///       parameter-type-list: [C99 6.7.5]
71220b57cec5SDimitry Andric ///         parameter-list
71230b57cec5SDimitry Andric ///         parameter-list ',' '...'
71240b57cec5SDimitry Andric /// [C++]   parameter-list '...'
71250b57cec5SDimitry Andric ///
71260b57cec5SDimitry Andric ///       parameter-list: [C99 6.7.5]
71270b57cec5SDimitry Andric ///         parameter-declaration
71280b57cec5SDimitry Andric ///         parameter-list ',' parameter-declaration
71290b57cec5SDimitry Andric ///
71300b57cec5SDimitry Andric ///       parameter-declaration: [C99 6.7.5]
71310b57cec5SDimitry Andric ///         declaration-specifiers declarator
71320b57cec5SDimitry Andric /// [C++]   declaration-specifiers declarator '=' assignment-expression
71330b57cec5SDimitry Andric /// [C++11]                                       initializer-clause
71340b57cec5SDimitry Andric /// [GNU]   declaration-specifiers declarator attributes
71350b57cec5SDimitry Andric ///         declaration-specifiers abstract-declarator[opt]
71360b57cec5SDimitry Andric /// [C++]   declaration-specifiers abstract-declarator[opt]
71370b57cec5SDimitry Andric ///           '=' assignment-expression
71380b57cec5SDimitry Andric /// [GNU]   declaration-specifiers abstract-declarator[opt] attributes
71390b57cec5SDimitry Andric /// [C++11] attribute-specifier-seq parameter-declaration
71400b57cec5SDimitry Andric ///
71410b57cec5SDimitry Andric void Parser::ParseParameterDeclarationClause(
714281ad6265SDimitry Andric     DeclaratorContext DeclaratorCtx, ParsedAttributes &FirstArgAttrs,
71430b57cec5SDimitry Andric     SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
7144*bdd1243dSDimitry Andric     SourceLocation &EllipsisLoc, bool IsACXXFunctionDeclaration) {
7145480093f4SDimitry Andric 
7146480093f4SDimitry Andric   // Avoid exceeding the maximum function scope depth.
7147480093f4SDimitry Andric   // See https://bugs.llvm.org/show_bug.cgi?id=19607
7148480093f4SDimitry Andric   // Note Sema::ActOnParamDeclarator calls ParmVarDecl::setScopeInfo with
7149480093f4SDimitry Andric   // getFunctionPrototypeDepth() - 1.
7150480093f4SDimitry Andric   if (getCurScope()->getFunctionPrototypeDepth() - 1 >
7151480093f4SDimitry Andric       ParmVarDecl::getMaxFunctionScopeDepth()) {
7152480093f4SDimitry Andric     Diag(Tok.getLocation(), diag::err_function_scope_depth_exceeded)
7153480093f4SDimitry Andric         << ParmVarDecl::getMaxFunctionScopeDepth();
7154480093f4SDimitry Andric     cutOffParsing();
7155480093f4SDimitry Andric     return;
7156480093f4SDimitry Andric   }
7157480093f4SDimitry Andric 
7158*bdd1243dSDimitry Andric   // C++2a [temp.res]p5
7159*bdd1243dSDimitry Andric   // A qualified-id is assumed to name a type if
7160*bdd1243dSDimitry Andric   //   - [...]
7161*bdd1243dSDimitry Andric   //   - it is a decl-specifier of the decl-specifier-seq of a
7162*bdd1243dSDimitry Andric   //     - [...]
7163*bdd1243dSDimitry Andric   //     - parameter-declaration in a member-declaration [...]
7164*bdd1243dSDimitry Andric   //     - parameter-declaration in a declarator of a function or function
7165*bdd1243dSDimitry Andric   //       template declaration whose declarator-id is qualified [...]
7166*bdd1243dSDimitry Andric   //     - parameter-declaration in a lambda-declarator [...]
7167*bdd1243dSDimitry Andric   auto AllowImplicitTypename = ImplicitTypenameContext::No;
7168*bdd1243dSDimitry Andric   if (DeclaratorCtx == DeclaratorContext::Member ||
7169*bdd1243dSDimitry Andric       DeclaratorCtx == DeclaratorContext::LambdaExpr ||
7170*bdd1243dSDimitry Andric       DeclaratorCtx == DeclaratorContext::RequiresExpr ||
7171*bdd1243dSDimitry Andric       IsACXXFunctionDeclaration) {
7172*bdd1243dSDimitry Andric     AllowImplicitTypename = ImplicitTypenameContext::Yes;
7173*bdd1243dSDimitry Andric   }
7174*bdd1243dSDimitry Andric 
71750b57cec5SDimitry Andric   do {
71760b57cec5SDimitry Andric     // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
71770b57cec5SDimitry Andric     // before deciding this was a parameter-declaration-clause.
71780b57cec5SDimitry Andric     if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
71790b57cec5SDimitry Andric       break;
71800b57cec5SDimitry Andric 
71810b57cec5SDimitry Andric     // Parse the declaration-specifiers.
71820b57cec5SDimitry Andric     // Just use the ParsingDeclaration "scope" of the declarator.
71830b57cec5SDimitry Andric     DeclSpec DS(AttrFactory);
71840b57cec5SDimitry Andric 
718581ad6265SDimitry Andric     ParsedAttributes ArgDeclAttrs(AttrFactory);
718681ad6265SDimitry Andric     ParsedAttributes ArgDeclSpecAttrs(AttrFactory);
718781ad6265SDimitry Andric 
718881ad6265SDimitry Andric     if (FirstArgAttrs.Range.isValid()) {
718981ad6265SDimitry Andric       // If the caller parsed attributes for the first argument, add them now.
719081ad6265SDimitry Andric       // Take them so that we only apply the attributes to the first parameter.
719181ad6265SDimitry Andric       // We have already started parsing the decl-specifier sequence, so don't
719281ad6265SDimitry Andric       // parse any parameter-declaration pieces that precede it.
719381ad6265SDimitry Andric       ArgDeclSpecAttrs.takeAllFrom(FirstArgAttrs);
719481ad6265SDimitry Andric     } else {
71950b57cec5SDimitry Andric       // Parse any C++11 attributes.
719681ad6265SDimitry Andric       MaybeParseCXX11Attributes(ArgDeclAttrs);
71970b57cec5SDimitry Andric 
71980b57cec5SDimitry Andric       // Skip any Microsoft attributes before a param.
719981ad6265SDimitry Andric       MaybeParseMicrosoftAttributes(ArgDeclSpecAttrs);
720081ad6265SDimitry Andric     }
72010b57cec5SDimitry Andric 
72020b57cec5SDimitry Andric     SourceLocation DSStart = Tok.getLocation();
72030b57cec5SDimitry Andric 
7204*bdd1243dSDimitry Andric     ParseDeclarationSpecifiers(DS, /*TemplateInfo=*/ParsedTemplateInfo(),
7205*bdd1243dSDimitry Andric                                AS_none, DeclSpecContext::DSC_normal,
7206*bdd1243dSDimitry Andric                                /*LateAttrs=*/nullptr, AllowImplicitTypename);
720781ad6265SDimitry Andric     DS.takeAttributesFrom(ArgDeclSpecAttrs);
72080b57cec5SDimitry Andric 
72090b57cec5SDimitry Andric     // Parse the declarator.  This is "PrototypeContext" or
72100b57cec5SDimitry Andric     // "LambdaExprParameterContext", because we must accept either
72110b57cec5SDimitry Andric     // 'declarator' or 'abstract-declarator' here.
721281ad6265SDimitry Andric     Declarator ParmDeclarator(DS, ArgDeclAttrs,
721381ad6265SDimitry Andric                               DeclaratorCtx == DeclaratorContext::RequiresExpr
7214e8d8bef9SDimitry Andric                                   ? DeclaratorContext::RequiresExpr
7215e8d8bef9SDimitry Andric                               : DeclaratorCtx == DeclaratorContext::LambdaExpr
7216e8d8bef9SDimitry Andric                                   ? DeclaratorContext::LambdaExprParameter
7217e8d8bef9SDimitry Andric                                   : DeclaratorContext::Prototype);
72180b57cec5SDimitry Andric     ParseDeclarator(ParmDeclarator);
72190b57cec5SDimitry Andric 
72200b57cec5SDimitry Andric     // Parse GNU attributes, if present.
72210b57cec5SDimitry Andric     MaybeParseGNUAttributes(ParmDeclarator);
7222*bdd1243dSDimitry Andric     if (getLangOpts().HLSL)
722381ad6265SDimitry Andric       MaybeParseHLSLSemantics(DS.getAttributes());
72240b57cec5SDimitry Andric 
7225480093f4SDimitry Andric     if (Tok.is(tok::kw_requires)) {
7226480093f4SDimitry Andric       // User tried to define a requires clause in a parameter declaration,
7227480093f4SDimitry Andric       // which is surely not a function declaration.
7228480093f4SDimitry Andric       // void f(int (*g)(int, int) requires true);
7229480093f4SDimitry Andric       Diag(Tok,
7230480093f4SDimitry Andric            diag::err_requires_clause_on_declarator_not_declaring_a_function);
7231480093f4SDimitry Andric       ConsumeToken();
7232480093f4SDimitry Andric       Actions.CorrectDelayedTyposInExpr(
7233480093f4SDimitry Andric          ParseConstraintLogicalOrExpression(/*IsTrailingRequiresClause=*/true));
7234480093f4SDimitry Andric     }
7235480093f4SDimitry Andric 
72360b57cec5SDimitry Andric     // Remember this parsed parameter in ParamInfo.
72370b57cec5SDimitry Andric     IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
72380b57cec5SDimitry Andric 
72390b57cec5SDimitry Andric     // DefArgToks is used when the parsing of default arguments needs
72400b57cec5SDimitry Andric     // to be delayed.
72410b57cec5SDimitry Andric     std::unique_ptr<CachedTokens> DefArgToks;
72420b57cec5SDimitry Andric 
72430b57cec5SDimitry Andric     // If no parameter was specified, verify that *something* was specified,
72440b57cec5SDimitry Andric     // otherwise we have a missing type and identifier.
72450b57cec5SDimitry Andric     if (DS.isEmpty() && ParmDeclarator.getIdentifier() == nullptr &&
72460b57cec5SDimitry Andric         ParmDeclarator.getNumTypeObjects() == 0) {
72470b57cec5SDimitry Andric       // Completely missing, emit error.
72480b57cec5SDimitry Andric       Diag(DSStart, diag::err_missing_param);
72490b57cec5SDimitry Andric     } else {
72500b57cec5SDimitry Andric       // Otherwise, we have something.  Add it and let semantic analysis try
72510b57cec5SDimitry Andric       // to grok it and add the result to the ParamInfo we are building.
72520b57cec5SDimitry Andric 
72530b57cec5SDimitry Andric       // Last chance to recover from a misplaced ellipsis in an attempted
72540b57cec5SDimitry Andric       // parameter pack declaration.
72550b57cec5SDimitry Andric       if (Tok.is(tok::ellipsis) &&
72560b57cec5SDimitry Andric           (NextToken().isNot(tok::r_paren) ||
72570b57cec5SDimitry Andric            (!ParmDeclarator.getEllipsisLoc().isValid() &&
72580b57cec5SDimitry Andric             !Actions.isUnexpandedParameterPackPermitted())) &&
72590b57cec5SDimitry Andric           Actions.containsUnexpandedParameterPacks(ParmDeclarator))
72600b57cec5SDimitry Andric         DiagnoseMisplacedEllipsisInDeclarator(ConsumeToken(), ParmDeclarator);
72610b57cec5SDimitry Andric 
72625ffd83dbSDimitry Andric       // Now we are at the point where declarator parsing is finished.
72635ffd83dbSDimitry Andric       //
72645ffd83dbSDimitry Andric       // Try to catch keywords in place of the identifier in a declarator, and
72655ffd83dbSDimitry Andric       // in particular the common case where:
72665ffd83dbSDimitry Andric       //   1 identifier comes at the end of the declarator
72675ffd83dbSDimitry Andric       //   2 if the identifier is dropped, the declarator is valid but anonymous
72685ffd83dbSDimitry Andric       //     (no identifier)
72695ffd83dbSDimitry Andric       //   3 declarator parsing succeeds, and then we have a trailing keyword,
72705ffd83dbSDimitry Andric       //     which is never valid in a param list (e.g. missing a ',')
72715ffd83dbSDimitry Andric       // And we can't handle this in ParseDeclarator because in general keywords
72725ffd83dbSDimitry Andric       // may be allowed to follow the declarator. (And in some cases there'd be
72735ffd83dbSDimitry Andric       // better recovery like inserting punctuation). ParseDeclarator is just
72745ffd83dbSDimitry Andric       // treating this as an anonymous parameter, and fortunately at this point
72755ffd83dbSDimitry Andric       // we've already almost done that.
72765ffd83dbSDimitry Andric       //
72775ffd83dbSDimitry Andric       // We care about case 1) where the declarator type should be known, and
72785ffd83dbSDimitry Andric       // the identifier should be null.
72794824e7fdSDimitry Andric       if (!ParmDeclarator.isInvalidType() && !ParmDeclarator.hasName() &&
72804824e7fdSDimitry Andric           Tok.isNot(tok::raw_identifier) && !Tok.isAnnotation() &&
72814824e7fdSDimitry Andric           Tok.getIdentifierInfo() &&
72825ffd83dbSDimitry Andric           Tok.getIdentifierInfo()->isKeyword(getLangOpts())) {
72835ffd83dbSDimitry Andric         Diag(Tok, diag::err_keyword_as_parameter) << PP.getSpelling(Tok);
72845ffd83dbSDimitry Andric         // Consume the keyword.
72855ffd83dbSDimitry Andric         ConsumeToken();
72865ffd83dbSDimitry Andric       }
72870b57cec5SDimitry Andric       // Inform the actions module about the parameter declarator, so it gets
72880b57cec5SDimitry Andric       // added to the current scope.
72890b57cec5SDimitry Andric       Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
72900b57cec5SDimitry Andric       // Parse the default argument, if any. We parse the default
72910b57cec5SDimitry Andric       // arguments in all dialects; the semantic analysis in
72920b57cec5SDimitry Andric       // ActOnParamDefaultArgument will reject the default argument in
72930b57cec5SDimitry Andric       // C.
72940b57cec5SDimitry Andric       if (Tok.is(tok::equal)) {
72950b57cec5SDimitry Andric         SourceLocation EqualLoc = Tok.getLocation();
72960b57cec5SDimitry Andric 
72970b57cec5SDimitry Andric         // Parse the default argument
7298e8d8bef9SDimitry Andric         if (DeclaratorCtx == DeclaratorContext::Member) {
72990b57cec5SDimitry Andric           // If we're inside a class definition, cache the tokens
73000b57cec5SDimitry Andric           // corresponding to the default argument. We'll actually parse
73010b57cec5SDimitry Andric           // them when we see the end of the class definition.
73020b57cec5SDimitry Andric           DefArgToks.reset(new CachedTokens);
73030b57cec5SDimitry Andric 
73040b57cec5SDimitry Andric           SourceLocation ArgStartLoc = NextToken().getLocation();
73050b57cec5SDimitry Andric           if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
73060b57cec5SDimitry Andric             DefArgToks.reset();
73070b57cec5SDimitry Andric             Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
73080b57cec5SDimitry Andric           } else {
73090b57cec5SDimitry Andric             Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
73100b57cec5SDimitry Andric                                                       ArgStartLoc);
73110b57cec5SDimitry Andric           }
73120b57cec5SDimitry Andric         } else {
73130b57cec5SDimitry Andric           // Consume the '='.
73140b57cec5SDimitry Andric           ConsumeToken();
73150b57cec5SDimitry Andric 
73160b57cec5SDimitry Andric           // The argument isn't actually potentially evaluated unless it is
73170b57cec5SDimitry Andric           // used.
73180b57cec5SDimitry Andric           EnterExpressionEvaluationContext Eval(
73190b57cec5SDimitry Andric               Actions,
73200b57cec5SDimitry Andric               Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed,
73210b57cec5SDimitry Andric               Param);
73220b57cec5SDimitry Andric 
73230b57cec5SDimitry Andric           ExprResult DefArgResult;
73240b57cec5SDimitry Andric           if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
73250b57cec5SDimitry Andric             Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
73260b57cec5SDimitry Andric             DefArgResult = ParseBraceInitializer();
732781ad6265SDimitry Andric           } else {
732881ad6265SDimitry Andric             if (Tok.is(tok::l_paren) && NextToken().is(tok::l_brace)) {
732981ad6265SDimitry Andric               Diag(Tok, diag::err_stmt_expr_in_default_arg) << 0;
733081ad6265SDimitry Andric               Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
733181ad6265SDimitry Andric               // Skip the statement expression and continue parsing
733281ad6265SDimitry Andric               SkipUntil(tok::comma, StopBeforeMatch);
733381ad6265SDimitry Andric               continue;
733481ad6265SDimitry Andric             }
73350b57cec5SDimitry Andric             DefArgResult = ParseAssignmentExpression();
733681ad6265SDimitry Andric           }
73370b57cec5SDimitry Andric           DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
73380b57cec5SDimitry Andric           if (DefArgResult.isInvalid()) {
73390b57cec5SDimitry Andric             Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
73400b57cec5SDimitry Andric             SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
73410b57cec5SDimitry Andric           } else {
73420b57cec5SDimitry Andric             // Inform the actions module about the default argument
73430b57cec5SDimitry Andric             Actions.ActOnParamDefaultArgument(Param, EqualLoc,
73440b57cec5SDimitry Andric                                               DefArgResult.get());
73450b57cec5SDimitry Andric           }
73460b57cec5SDimitry Andric         }
73470b57cec5SDimitry Andric       }
73480b57cec5SDimitry Andric 
73490b57cec5SDimitry Andric       ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
73500b57cec5SDimitry Andric                                           ParmDeclarator.getIdentifierLoc(),
73510b57cec5SDimitry Andric                                           Param, std::move(DefArgToks)));
73520b57cec5SDimitry Andric     }
73530b57cec5SDimitry Andric 
73540b57cec5SDimitry Andric     if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) {
73550b57cec5SDimitry Andric       if (!getLangOpts().CPlusPlus) {
73560b57cec5SDimitry Andric         // We have ellipsis without a preceding ',', which is ill-formed
73570b57cec5SDimitry Andric         // in C. Complain and provide the fix.
73580b57cec5SDimitry Andric         Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
73590b57cec5SDimitry Andric             << FixItHint::CreateInsertion(EllipsisLoc, ", ");
73600b57cec5SDimitry Andric       } else if (ParmDeclarator.getEllipsisLoc().isValid() ||
73610b57cec5SDimitry Andric                  Actions.containsUnexpandedParameterPacks(ParmDeclarator)) {
73620b57cec5SDimitry Andric         // It looks like this was supposed to be a parameter pack. Warn and
73630b57cec5SDimitry Andric         // point out where the ellipsis should have gone.
73640b57cec5SDimitry Andric         SourceLocation ParmEllipsis = ParmDeclarator.getEllipsisLoc();
73650b57cec5SDimitry Andric         Diag(EllipsisLoc, diag::warn_misplaced_ellipsis_vararg)
73660b57cec5SDimitry Andric           << ParmEllipsis.isValid() << ParmEllipsis;
73670b57cec5SDimitry Andric         if (ParmEllipsis.isValid()) {
73680b57cec5SDimitry Andric           Diag(ParmEllipsis,
73690b57cec5SDimitry Andric                diag::note_misplaced_ellipsis_vararg_existing_ellipsis);
73700b57cec5SDimitry Andric         } else {
73710b57cec5SDimitry Andric           Diag(ParmDeclarator.getIdentifierLoc(),
73720b57cec5SDimitry Andric                diag::note_misplaced_ellipsis_vararg_add_ellipsis)
73730b57cec5SDimitry Andric             << FixItHint::CreateInsertion(ParmDeclarator.getIdentifierLoc(),
73740b57cec5SDimitry Andric                                           "...")
73750b57cec5SDimitry Andric             << !ParmDeclarator.hasName();
73760b57cec5SDimitry Andric         }
73770b57cec5SDimitry Andric         Diag(EllipsisLoc, diag::note_misplaced_ellipsis_vararg_add_comma)
73780b57cec5SDimitry Andric           << FixItHint::CreateInsertion(EllipsisLoc, ", ");
73790b57cec5SDimitry Andric       }
73800b57cec5SDimitry Andric 
73810b57cec5SDimitry Andric       // We can't have any more parameters after an ellipsis.
73820b57cec5SDimitry Andric       break;
73830b57cec5SDimitry Andric     }
73840b57cec5SDimitry Andric 
73850b57cec5SDimitry Andric     // If the next token is a comma, consume it and keep reading arguments.
73860b57cec5SDimitry Andric   } while (TryConsumeToken(tok::comma));
73870b57cec5SDimitry Andric }
73880b57cec5SDimitry Andric 
73890b57cec5SDimitry Andric /// [C90]   direct-declarator '[' constant-expression[opt] ']'
73900b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
73910b57cec5SDimitry Andric /// [C99]   direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
73920b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list 'static' assignment-expr ']'
73930b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list[opt] '*' ']'
73940b57cec5SDimitry Andric /// [C++11] direct-declarator '[' constant-expression[opt] ']'
73950b57cec5SDimitry Andric ///                           attribute-specifier-seq[opt]
73960b57cec5SDimitry Andric void Parser::ParseBracketDeclarator(Declarator &D) {
73970b57cec5SDimitry Andric   if (CheckProhibitedCXX11Attribute())
73980b57cec5SDimitry Andric     return;
73990b57cec5SDimitry Andric 
74000b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_square);
74010b57cec5SDimitry Andric   T.consumeOpen();
74020b57cec5SDimitry Andric 
74030b57cec5SDimitry Andric   // C array syntax has many features, but by-far the most common is [] and [4].
74040b57cec5SDimitry Andric   // This code does a fast path to handle some of the most obvious cases.
74050b57cec5SDimitry Andric   if (Tok.getKind() == tok::r_square) {
74060b57cec5SDimitry Andric     T.consumeClose();
74070b57cec5SDimitry Andric     ParsedAttributes attrs(AttrFactory);
74080b57cec5SDimitry Andric     MaybeParseCXX11Attributes(attrs);
74090b57cec5SDimitry Andric 
74100b57cec5SDimitry Andric     // Remember that we parsed the empty array type.
74110b57cec5SDimitry Andric     D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, nullptr,
74120b57cec5SDimitry Andric                                             T.getOpenLocation(),
74130b57cec5SDimitry Andric                                             T.getCloseLocation()),
74140b57cec5SDimitry Andric                   std::move(attrs), T.getCloseLocation());
74150b57cec5SDimitry Andric     return;
74160b57cec5SDimitry Andric   } else if (Tok.getKind() == tok::numeric_constant &&
74170b57cec5SDimitry Andric              GetLookAheadToken(1).is(tok::r_square)) {
74180b57cec5SDimitry Andric     // [4] is very common.  Parse the numeric constant expression.
74190b57cec5SDimitry Andric     ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
74200b57cec5SDimitry Andric     ConsumeToken();
74210b57cec5SDimitry Andric 
74220b57cec5SDimitry Andric     T.consumeClose();
74230b57cec5SDimitry Andric     ParsedAttributes attrs(AttrFactory);
74240b57cec5SDimitry Andric     MaybeParseCXX11Attributes(attrs);
74250b57cec5SDimitry Andric 
74260b57cec5SDimitry Andric     // Remember that we parsed a array type, and remember its features.
74270b57cec5SDimitry Andric     D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, ExprRes.get(),
74280b57cec5SDimitry Andric                                             T.getOpenLocation(),
74290b57cec5SDimitry Andric                                             T.getCloseLocation()),
74300b57cec5SDimitry Andric                   std::move(attrs), T.getCloseLocation());
74310b57cec5SDimitry Andric     return;
74320b57cec5SDimitry Andric   } else if (Tok.getKind() == tok::code_completion) {
7433fe6060f1SDimitry Andric     cutOffParsing();
74340b57cec5SDimitry Andric     Actions.CodeCompleteBracketDeclarator(getCurScope());
7435fe6060f1SDimitry Andric     return;
74360b57cec5SDimitry Andric   }
74370b57cec5SDimitry Andric 
74380b57cec5SDimitry Andric   // If valid, this location is the position where we read the 'static' keyword.
74390b57cec5SDimitry Andric   SourceLocation StaticLoc;
74400b57cec5SDimitry Andric   TryConsumeToken(tok::kw_static, StaticLoc);
74410b57cec5SDimitry Andric 
74420b57cec5SDimitry Andric   // If there is a type-qualifier-list, read it now.
74430b57cec5SDimitry Andric   // Type qualifiers in an array subscript are a C99 feature.
74440b57cec5SDimitry Andric   DeclSpec DS(AttrFactory);
74450b57cec5SDimitry Andric   ParseTypeQualifierListOpt(DS, AR_CXX11AttributesParsed);
74460b57cec5SDimitry Andric 
74470b57cec5SDimitry Andric   // If we haven't already read 'static', check to see if there is one after the
74480b57cec5SDimitry Andric   // type-qualifier-list.
74490b57cec5SDimitry Andric   if (!StaticLoc.isValid())
74500b57cec5SDimitry Andric     TryConsumeToken(tok::kw_static, StaticLoc);
74510b57cec5SDimitry Andric 
74520b57cec5SDimitry Andric   // Handle "direct-declarator [ type-qual-list[opt] * ]".
74530b57cec5SDimitry Andric   bool isStar = false;
74540b57cec5SDimitry Andric   ExprResult NumElements;
74550b57cec5SDimitry Andric 
74560b57cec5SDimitry Andric   // Handle the case where we have '[*]' as the array size.  However, a leading
74570b57cec5SDimitry Andric   // star could be the start of an expression, for example 'X[*p + 4]'.  Verify
74580b57cec5SDimitry Andric   // the token after the star is a ']'.  Since stars in arrays are
74590b57cec5SDimitry Andric   // infrequent, use of lookahead is not costly here.
74600b57cec5SDimitry Andric   if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
74610b57cec5SDimitry Andric     ConsumeToken();  // Eat the '*'.
74620b57cec5SDimitry Andric 
74630b57cec5SDimitry Andric     if (StaticLoc.isValid()) {
74640b57cec5SDimitry Andric       Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
74650b57cec5SDimitry Andric       StaticLoc = SourceLocation();  // Drop the static.
74660b57cec5SDimitry Andric     }
74670b57cec5SDimitry Andric     isStar = true;
74680b57cec5SDimitry Andric   } else if (Tok.isNot(tok::r_square)) {
74690b57cec5SDimitry Andric     // Note, in C89, this production uses the constant-expr production instead
74700b57cec5SDimitry Andric     // of assignment-expr.  The only difference is that assignment-expr allows
74710b57cec5SDimitry Andric     // things like '=' and '*='.  Sema rejects these in C89 mode because they
74720b57cec5SDimitry Andric     // are not i-c-e's, so we don't need to distinguish between the two here.
74730b57cec5SDimitry Andric 
74740b57cec5SDimitry Andric     // Parse the constant-expression or assignment-expression now (depending
74750b57cec5SDimitry Andric     // on dialect).
74760b57cec5SDimitry Andric     if (getLangOpts().CPlusPlus) {
74770b57cec5SDimitry Andric       NumElements = ParseConstantExpression();
74780b57cec5SDimitry Andric     } else {
74790b57cec5SDimitry Andric       EnterExpressionEvaluationContext Unevaluated(
74800b57cec5SDimitry Andric           Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
74810b57cec5SDimitry Andric       NumElements =
74820b57cec5SDimitry Andric           Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
74830b57cec5SDimitry Andric     }
74840b57cec5SDimitry Andric   } else {
74850b57cec5SDimitry Andric     if (StaticLoc.isValid()) {
74860b57cec5SDimitry Andric       Diag(StaticLoc, diag::err_unspecified_size_with_static);
74870b57cec5SDimitry Andric       StaticLoc = SourceLocation();  // Drop the static.
74880b57cec5SDimitry Andric     }
74890b57cec5SDimitry Andric   }
74900b57cec5SDimitry Andric 
74910b57cec5SDimitry Andric   // If there was an error parsing the assignment-expression, recover.
74920b57cec5SDimitry Andric   if (NumElements.isInvalid()) {
74930b57cec5SDimitry Andric     D.setInvalidType(true);
74940b57cec5SDimitry Andric     // If the expression was invalid, skip it.
74950b57cec5SDimitry Andric     SkipUntil(tok::r_square, StopAtSemi);
74960b57cec5SDimitry Andric     return;
74970b57cec5SDimitry Andric   }
74980b57cec5SDimitry Andric 
74990b57cec5SDimitry Andric   T.consumeClose();
75000b57cec5SDimitry Andric 
75010b57cec5SDimitry Andric   MaybeParseCXX11Attributes(DS.getAttributes());
75020b57cec5SDimitry Andric 
75030b57cec5SDimitry Andric   // Remember that we parsed a array type, and remember its features.
75040b57cec5SDimitry Andric   D.AddTypeInfo(
75050b57cec5SDimitry Andric       DeclaratorChunk::getArray(DS.getTypeQualifiers(), StaticLoc.isValid(),
75060b57cec5SDimitry Andric                                 isStar, NumElements.get(), T.getOpenLocation(),
75070b57cec5SDimitry Andric                                 T.getCloseLocation()),
75080b57cec5SDimitry Andric       std::move(DS.getAttributes()), T.getCloseLocation());
75090b57cec5SDimitry Andric }
75100b57cec5SDimitry Andric 
75110b57cec5SDimitry Andric /// Diagnose brackets before an identifier.
75120b57cec5SDimitry Andric void Parser::ParseMisplacedBracketDeclarator(Declarator &D) {
75130b57cec5SDimitry Andric   assert(Tok.is(tok::l_square) && "Missing opening bracket");
75140b57cec5SDimitry Andric   assert(!D.mayOmitIdentifier() && "Declarator cannot omit identifier");
75150b57cec5SDimitry Andric 
75160b57cec5SDimitry Andric   SourceLocation StartBracketLoc = Tok.getLocation();
751781ad6265SDimitry Andric   Declarator TempDeclarator(D.getDeclSpec(), ParsedAttributesView::none(),
751881ad6265SDimitry Andric                             D.getContext());
75190b57cec5SDimitry Andric 
75200b57cec5SDimitry Andric   while (Tok.is(tok::l_square)) {
75210b57cec5SDimitry Andric     ParseBracketDeclarator(TempDeclarator);
75220b57cec5SDimitry Andric   }
75230b57cec5SDimitry Andric 
75240b57cec5SDimitry Andric   // Stuff the location of the start of the brackets into the Declarator.
75250b57cec5SDimitry Andric   // The diagnostics from ParseDirectDeclarator will make more sense if
75260b57cec5SDimitry Andric   // they use this location instead.
75270b57cec5SDimitry Andric   if (Tok.is(tok::semi))
75280b57cec5SDimitry Andric     D.getName().EndLocation = StartBracketLoc;
75290b57cec5SDimitry Andric 
75300b57cec5SDimitry Andric   SourceLocation SuggestParenLoc = Tok.getLocation();
75310b57cec5SDimitry Andric 
75320b57cec5SDimitry Andric   // Now that the brackets are removed, try parsing the declarator again.
75330b57cec5SDimitry Andric   ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
75340b57cec5SDimitry Andric 
75350b57cec5SDimitry Andric   // Something went wrong parsing the brackets, in which case,
75360b57cec5SDimitry Andric   // ParseBracketDeclarator has emitted an error, and we don't need to emit
75370b57cec5SDimitry Andric   // one here.
75380b57cec5SDimitry Andric   if (TempDeclarator.getNumTypeObjects() == 0)
75390b57cec5SDimitry Andric     return;
75400b57cec5SDimitry Andric 
75410b57cec5SDimitry Andric   // Determine if parens will need to be suggested in the diagnostic.
75420b57cec5SDimitry Andric   bool NeedParens = false;
75430b57cec5SDimitry Andric   if (D.getNumTypeObjects() != 0) {
75440b57cec5SDimitry Andric     switch (D.getTypeObject(D.getNumTypeObjects() - 1).Kind) {
75450b57cec5SDimitry Andric     case DeclaratorChunk::Pointer:
75460b57cec5SDimitry Andric     case DeclaratorChunk::Reference:
75470b57cec5SDimitry Andric     case DeclaratorChunk::BlockPointer:
75480b57cec5SDimitry Andric     case DeclaratorChunk::MemberPointer:
75490b57cec5SDimitry Andric     case DeclaratorChunk::Pipe:
75500b57cec5SDimitry Andric       NeedParens = true;
75510b57cec5SDimitry Andric       break;
75520b57cec5SDimitry Andric     case DeclaratorChunk::Array:
75530b57cec5SDimitry Andric     case DeclaratorChunk::Function:
75540b57cec5SDimitry Andric     case DeclaratorChunk::Paren:
75550b57cec5SDimitry Andric       break;
75560b57cec5SDimitry Andric     }
75570b57cec5SDimitry Andric   }
75580b57cec5SDimitry Andric 
75590b57cec5SDimitry Andric   if (NeedParens) {
75600b57cec5SDimitry Andric     // Create a DeclaratorChunk for the inserted parens.
75610b57cec5SDimitry Andric     SourceLocation EndLoc = PP.getLocForEndOfToken(D.getEndLoc());
75620b57cec5SDimitry Andric     D.AddTypeInfo(DeclaratorChunk::getParen(SuggestParenLoc, EndLoc),
75630b57cec5SDimitry Andric                   SourceLocation());
75640b57cec5SDimitry Andric   }
75650b57cec5SDimitry Andric 
75660b57cec5SDimitry Andric   // Adding back the bracket info to the end of the Declarator.
75670b57cec5SDimitry Andric   for (unsigned i = 0, e = TempDeclarator.getNumTypeObjects(); i < e; ++i) {
75680b57cec5SDimitry Andric     const DeclaratorChunk &Chunk = TempDeclarator.getTypeObject(i);
75690b57cec5SDimitry Andric     D.AddTypeInfo(Chunk, SourceLocation());
75700b57cec5SDimitry Andric   }
75710b57cec5SDimitry Andric 
75720b57cec5SDimitry Andric   // The missing identifier would have been diagnosed in ParseDirectDeclarator.
75730b57cec5SDimitry Andric   // If parentheses are required, always suggest them.
75740b57cec5SDimitry Andric   if (!D.getIdentifier() && !NeedParens)
75750b57cec5SDimitry Andric     return;
75760b57cec5SDimitry Andric 
75770b57cec5SDimitry Andric   SourceLocation EndBracketLoc = TempDeclarator.getEndLoc();
75780b57cec5SDimitry Andric 
75790b57cec5SDimitry Andric   // Generate the move bracket error message.
75800b57cec5SDimitry Andric   SourceRange BracketRange(StartBracketLoc, EndBracketLoc);
75810b57cec5SDimitry Andric   SourceLocation EndLoc = PP.getLocForEndOfToken(D.getEndLoc());
75820b57cec5SDimitry Andric 
75830b57cec5SDimitry Andric   if (NeedParens) {
75840b57cec5SDimitry Andric     Diag(EndLoc, diag::err_brackets_go_after_unqualified_id)
75850b57cec5SDimitry Andric         << getLangOpts().CPlusPlus
75860b57cec5SDimitry Andric         << FixItHint::CreateInsertion(SuggestParenLoc, "(")
75870b57cec5SDimitry Andric         << FixItHint::CreateInsertion(EndLoc, ")")
75880b57cec5SDimitry Andric         << FixItHint::CreateInsertionFromRange(
75890b57cec5SDimitry Andric                EndLoc, CharSourceRange(BracketRange, true))
75900b57cec5SDimitry Andric         << FixItHint::CreateRemoval(BracketRange);
75910b57cec5SDimitry Andric   } else {
75920b57cec5SDimitry Andric     Diag(EndLoc, diag::err_brackets_go_after_unqualified_id)
75930b57cec5SDimitry Andric         << getLangOpts().CPlusPlus
75940b57cec5SDimitry Andric         << FixItHint::CreateInsertionFromRange(
75950b57cec5SDimitry Andric                EndLoc, CharSourceRange(BracketRange, true))
75960b57cec5SDimitry Andric         << FixItHint::CreateRemoval(BracketRange);
75970b57cec5SDimitry Andric   }
75980b57cec5SDimitry Andric }
75990b57cec5SDimitry Andric 
76000b57cec5SDimitry Andric /// [GNU]   typeof-specifier:
76010b57cec5SDimitry Andric ///           typeof ( expressions )
76020b57cec5SDimitry Andric ///           typeof ( type-name )
76030b57cec5SDimitry Andric /// [GNU/C++] typeof unary-expression
7604*bdd1243dSDimitry Andric /// [C2x]   typeof-specifier:
7605*bdd1243dSDimitry Andric ///           typeof '(' typeof-specifier-argument ')'
7606*bdd1243dSDimitry Andric ///           typeof_unqual '(' typeof-specifier-argument ')'
7607*bdd1243dSDimitry Andric ///
7608*bdd1243dSDimitry Andric ///         typeof-specifier-argument:
7609*bdd1243dSDimitry Andric ///           expression
7610*bdd1243dSDimitry Andric ///           type-name
76110b57cec5SDimitry Andric ///
76120b57cec5SDimitry Andric void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
7613*bdd1243dSDimitry Andric   assert(Tok.isOneOf(tok::kw_typeof, tok::kw_typeof_unqual) &&
7614*bdd1243dSDimitry Andric          "Not a typeof specifier");
7615*bdd1243dSDimitry Andric 
7616*bdd1243dSDimitry Andric   bool IsUnqual = Tok.is(tok::kw_typeof_unqual);
7617*bdd1243dSDimitry Andric   const IdentifierInfo *II = Tok.getIdentifierInfo();
7618*bdd1243dSDimitry Andric   if (getLangOpts().C2x && !II->getName().startswith("__"))
7619*bdd1243dSDimitry Andric     Diag(Tok.getLocation(), diag::warn_c2x_compat_typeof_type_specifier)
7620*bdd1243dSDimitry Andric         << IsUnqual;
7621*bdd1243dSDimitry Andric 
76220b57cec5SDimitry Andric   Token OpTok = Tok;
76230b57cec5SDimitry Andric   SourceLocation StartLoc = ConsumeToken();
7624*bdd1243dSDimitry Andric   bool HasParens = Tok.is(tok::l_paren);
76250b57cec5SDimitry Andric 
76260b57cec5SDimitry Andric   EnterExpressionEvaluationContext Unevaluated(
76270b57cec5SDimitry Andric       Actions, Sema::ExpressionEvaluationContext::Unevaluated,
76280b57cec5SDimitry Andric       Sema::ReuseLambdaContextDecl);
76290b57cec5SDimitry Andric 
76300b57cec5SDimitry Andric   bool isCastExpr;
76310b57cec5SDimitry Andric   ParsedType CastTy;
76320b57cec5SDimitry Andric   SourceRange CastRange;
76330b57cec5SDimitry Andric   ExprResult Operand = Actions.CorrectDelayedTyposInExpr(
76340b57cec5SDimitry Andric       ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, CastTy, CastRange));
7635*bdd1243dSDimitry Andric   if (HasParens)
7636*bdd1243dSDimitry Andric     DS.setTypeArgumentRange(CastRange);
76370b57cec5SDimitry Andric 
76380b57cec5SDimitry Andric   if (CastRange.getEnd().isInvalid())
76390b57cec5SDimitry Andric     // FIXME: Not accurate, the range gets one token more than it should.
76400b57cec5SDimitry Andric     DS.SetRangeEnd(Tok.getLocation());
76410b57cec5SDimitry Andric   else
76420b57cec5SDimitry Andric     DS.SetRangeEnd(CastRange.getEnd());
76430b57cec5SDimitry Andric 
76440b57cec5SDimitry Andric   if (isCastExpr) {
76450b57cec5SDimitry Andric     if (!CastTy) {
76460b57cec5SDimitry Andric       DS.SetTypeSpecError();
76470b57cec5SDimitry Andric       return;
76480b57cec5SDimitry Andric     }
76490b57cec5SDimitry Andric 
76500b57cec5SDimitry Andric     const char *PrevSpec = nullptr;
76510b57cec5SDimitry Andric     unsigned DiagID;
76520b57cec5SDimitry Andric     // Check for duplicate type specifiers (e.g. "int typeof(int)").
7653*bdd1243dSDimitry Andric     if (DS.SetTypeSpecType(IsUnqual ? DeclSpec::TST_typeof_unqualType
7654*bdd1243dSDimitry Andric                                     : DeclSpec::TST_typeofType,
7655*bdd1243dSDimitry Andric                            StartLoc, PrevSpec,
76560b57cec5SDimitry Andric                            DiagID, CastTy,
76570b57cec5SDimitry Andric                            Actions.getASTContext().getPrintingPolicy()))
76580b57cec5SDimitry Andric       Diag(StartLoc, DiagID) << PrevSpec;
76590b57cec5SDimitry Andric     return;
76600b57cec5SDimitry Andric   }
76610b57cec5SDimitry Andric 
76620b57cec5SDimitry Andric   // If we get here, the operand to the typeof was an expression.
76630b57cec5SDimitry Andric   if (Operand.isInvalid()) {
76640b57cec5SDimitry Andric     DS.SetTypeSpecError();
76650b57cec5SDimitry Andric     return;
76660b57cec5SDimitry Andric   }
76670b57cec5SDimitry Andric 
76680b57cec5SDimitry Andric   // We might need to transform the operand if it is potentially evaluated.
76690b57cec5SDimitry Andric   Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
76700b57cec5SDimitry Andric   if (Operand.isInvalid()) {
76710b57cec5SDimitry Andric     DS.SetTypeSpecError();
76720b57cec5SDimitry Andric     return;
76730b57cec5SDimitry Andric   }
76740b57cec5SDimitry Andric 
76750b57cec5SDimitry Andric   const char *PrevSpec = nullptr;
76760b57cec5SDimitry Andric   unsigned DiagID;
76770b57cec5SDimitry Andric   // Check for duplicate type specifiers (e.g. "int typeof(int)").
7678*bdd1243dSDimitry Andric   if (DS.SetTypeSpecType(IsUnqual ? DeclSpec::TST_typeof_unqualExpr
7679*bdd1243dSDimitry Andric                                   : DeclSpec::TST_typeofExpr,
7680*bdd1243dSDimitry Andric                          StartLoc, PrevSpec,
76810b57cec5SDimitry Andric                          DiagID, Operand.get(),
76820b57cec5SDimitry Andric                          Actions.getASTContext().getPrintingPolicy()))
76830b57cec5SDimitry Andric     Diag(StartLoc, DiagID) << PrevSpec;
76840b57cec5SDimitry Andric }
76850b57cec5SDimitry Andric 
76860b57cec5SDimitry Andric /// [C11]   atomic-specifier:
76870b57cec5SDimitry Andric ///           _Atomic ( type-name )
76880b57cec5SDimitry Andric ///
76890b57cec5SDimitry Andric void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
76900b57cec5SDimitry Andric   assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
76910b57cec5SDimitry Andric          "Not an atomic specifier");
76920b57cec5SDimitry Andric 
76930b57cec5SDimitry Andric   SourceLocation StartLoc = ConsumeToken();
76940b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
76950b57cec5SDimitry Andric   if (T.consumeOpen())
76960b57cec5SDimitry Andric     return;
76970b57cec5SDimitry Andric 
76980b57cec5SDimitry Andric   TypeResult Result = ParseTypeName();
76990b57cec5SDimitry Andric   if (Result.isInvalid()) {
77000b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
77010b57cec5SDimitry Andric     return;
77020b57cec5SDimitry Andric   }
77030b57cec5SDimitry Andric 
77040b57cec5SDimitry Andric   // Match the ')'
77050b57cec5SDimitry Andric   T.consumeClose();
77060b57cec5SDimitry Andric 
77070b57cec5SDimitry Andric   if (T.getCloseLocation().isInvalid())
77080b57cec5SDimitry Andric     return;
77090b57cec5SDimitry Andric 
7710*bdd1243dSDimitry Andric   DS.setTypeArgumentRange(T.getRange());
77110b57cec5SDimitry Andric   DS.SetRangeEnd(T.getCloseLocation());
77120b57cec5SDimitry Andric 
77130b57cec5SDimitry Andric   const char *PrevSpec = nullptr;
77140b57cec5SDimitry Andric   unsigned DiagID;
77150b57cec5SDimitry Andric   if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
77160b57cec5SDimitry Andric                          DiagID, Result.get(),
77170b57cec5SDimitry Andric                          Actions.getASTContext().getPrintingPolicy()))
77180b57cec5SDimitry Andric     Diag(StartLoc, DiagID) << PrevSpec;
77190b57cec5SDimitry Andric }
77200b57cec5SDimitry Andric 
77210b57cec5SDimitry Andric /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
77220b57cec5SDimitry Andric /// from TryAltiVecVectorToken.
77230b57cec5SDimitry Andric bool Parser::TryAltiVecVectorTokenOutOfLine() {
77240b57cec5SDimitry Andric   Token Next = NextToken();
77250b57cec5SDimitry Andric   switch (Next.getKind()) {
77260b57cec5SDimitry Andric   default: return false;
77270b57cec5SDimitry Andric   case tok::kw_short:
77280b57cec5SDimitry Andric   case tok::kw_long:
77290b57cec5SDimitry Andric   case tok::kw_signed:
77300b57cec5SDimitry Andric   case tok::kw_unsigned:
77310b57cec5SDimitry Andric   case tok::kw_void:
77320b57cec5SDimitry Andric   case tok::kw_char:
77330b57cec5SDimitry Andric   case tok::kw_int:
77340b57cec5SDimitry Andric   case tok::kw_float:
77350b57cec5SDimitry Andric   case tok::kw_double:
77360b57cec5SDimitry Andric   case tok::kw_bool:
7737fe6060f1SDimitry Andric   case tok::kw__Bool:
77380b57cec5SDimitry Andric   case tok::kw___bool:
77390b57cec5SDimitry Andric   case tok::kw___pixel:
77400b57cec5SDimitry Andric     Tok.setKind(tok::kw___vector);
77410b57cec5SDimitry Andric     return true;
77420b57cec5SDimitry Andric   case tok::identifier:
77430b57cec5SDimitry Andric     if (Next.getIdentifierInfo() == Ident_pixel) {
77440b57cec5SDimitry Andric       Tok.setKind(tok::kw___vector);
77450b57cec5SDimitry Andric       return true;
77460b57cec5SDimitry Andric     }
7747fe6060f1SDimitry Andric     if (Next.getIdentifierInfo() == Ident_bool ||
7748fe6060f1SDimitry Andric         Next.getIdentifierInfo() == Ident_Bool) {
77490b57cec5SDimitry Andric       Tok.setKind(tok::kw___vector);
77500b57cec5SDimitry Andric       return true;
77510b57cec5SDimitry Andric     }
77520b57cec5SDimitry Andric     return false;
77530b57cec5SDimitry Andric   }
77540b57cec5SDimitry Andric }
77550b57cec5SDimitry Andric 
77560b57cec5SDimitry Andric bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
77570b57cec5SDimitry Andric                                       const char *&PrevSpec, unsigned &DiagID,
77580b57cec5SDimitry Andric                                       bool &isInvalid) {
77590b57cec5SDimitry Andric   const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
77600b57cec5SDimitry Andric   if (Tok.getIdentifierInfo() == Ident_vector) {
77610b57cec5SDimitry Andric     Token Next = NextToken();
77620b57cec5SDimitry Andric     switch (Next.getKind()) {
77630b57cec5SDimitry Andric     case tok::kw_short:
77640b57cec5SDimitry Andric     case tok::kw_long:
77650b57cec5SDimitry Andric     case tok::kw_signed:
77660b57cec5SDimitry Andric     case tok::kw_unsigned:
77670b57cec5SDimitry Andric     case tok::kw_void:
77680b57cec5SDimitry Andric     case tok::kw_char:
77690b57cec5SDimitry Andric     case tok::kw_int:
77700b57cec5SDimitry Andric     case tok::kw_float:
77710b57cec5SDimitry Andric     case tok::kw_double:
77720b57cec5SDimitry Andric     case tok::kw_bool:
7773fe6060f1SDimitry Andric     case tok::kw__Bool:
77740b57cec5SDimitry Andric     case tok::kw___bool:
77750b57cec5SDimitry Andric     case tok::kw___pixel:
77760b57cec5SDimitry Andric       isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
77770b57cec5SDimitry Andric       return true;
77780b57cec5SDimitry Andric     case tok::identifier:
77790b57cec5SDimitry Andric       if (Next.getIdentifierInfo() == Ident_pixel) {
77800b57cec5SDimitry Andric         isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
77810b57cec5SDimitry Andric         return true;
77820b57cec5SDimitry Andric       }
7783fe6060f1SDimitry Andric       if (Next.getIdentifierInfo() == Ident_bool ||
7784fe6060f1SDimitry Andric           Next.getIdentifierInfo() == Ident_Bool) {
7785fe6060f1SDimitry Andric         isInvalid =
7786fe6060f1SDimitry Andric             DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
77870b57cec5SDimitry Andric         return true;
77880b57cec5SDimitry Andric       }
77890b57cec5SDimitry Andric       break;
77900b57cec5SDimitry Andric     default:
77910b57cec5SDimitry Andric       break;
77920b57cec5SDimitry Andric     }
77930b57cec5SDimitry Andric   } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
77940b57cec5SDimitry Andric              DS.isTypeAltiVecVector()) {
77950b57cec5SDimitry Andric     isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
77960b57cec5SDimitry Andric     return true;
77970b57cec5SDimitry Andric   } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
77980b57cec5SDimitry Andric              DS.isTypeAltiVecVector()) {
77990b57cec5SDimitry Andric     isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
78000b57cec5SDimitry Andric     return true;
78010b57cec5SDimitry Andric   }
78020b57cec5SDimitry Andric   return false;
78030b57cec5SDimitry Andric }
78040eae32dcSDimitry Andric 
78050eae32dcSDimitry Andric void Parser::DiagnoseBitIntUse(const Token &Tok) {
78060eae32dcSDimitry Andric   // If the token is for _ExtInt, diagnose it as being deprecated. Otherwise,
78070eae32dcSDimitry Andric   // the token is about _BitInt and gets (potentially) diagnosed as use of an
78080eae32dcSDimitry Andric   // extension.
78090eae32dcSDimitry Andric   assert(Tok.isOneOf(tok::kw__ExtInt, tok::kw__BitInt) &&
78100eae32dcSDimitry Andric          "expected either an _ExtInt or _BitInt token!");
78110eae32dcSDimitry Andric 
78120eae32dcSDimitry Andric   SourceLocation Loc = Tok.getLocation();
78130eae32dcSDimitry Andric   if (Tok.is(tok::kw__ExtInt)) {
78140eae32dcSDimitry Andric     Diag(Loc, diag::warn_ext_int_deprecated)
78150eae32dcSDimitry Andric         << FixItHint::CreateReplacement(Loc, "_BitInt");
78160eae32dcSDimitry Andric   } else {
78170eae32dcSDimitry Andric     // In C2x mode, diagnose that the use is not compatible with pre-C2x modes.
78180eae32dcSDimitry Andric     // Otherwise, diagnose that the use is a Clang extension.
78190eae32dcSDimitry Andric     if (getLangOpts().C2x)
78200eae32dcSDimitry Andric       Diag(Loc, diag::warn_c17_compat_bit_int);
78210eae32dcSDimitry Andric     else
78220eae32dcSDimitry Andric       Diag(Loc, diag::ext_bit_int) << getLangOpts().CPlusPlus;
78230eae32dcSDimitry Andric   }
78240eae32dcSDimitry Andric }
7825