xref: /freebsd/contrib/llvm-project/clang/lib/Parse/ParseDecl.cpp (revision 5ffd83dbcc34f10e07f6d3e968ae6365869615f4)
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/Parse/Parser.h"
140b57cec5SDimitry Andric #include "clang/Parse/RAIIObjectsForParser.h"
150b57cec5SDimitry Andric #include "clang/AST/ASTContext.h"
160b57cec5SDimitry Andric #include "clang/AST/DeclTemplate.h"
170b57cec5SDimitry Andric #include "clang/AST/PrettyDeclStackTrace.h"
180b57cec5SDimitry Andric #include "clang/Basic/AddressSpaces.h"
190b57cec5SDimitry Andric #include "clang/Basic/Attributes.h"
200b57cec5SDimitry Andric #include "clang/Basic/CharInfo.h"
210b57cec5SDimitry Andric #include "clang/Basic/TargetInfo.h"
220b57cec5SDimitry Andric #include "clang/Parse/ParseDiagnostic.h"
230b57cec5SDimitry Andric #include "clang/Sema/Lookup.h"
240b57cec5SDimitry Andric #include "clang/Sema/ParsedTemplate.h"
250b57cec5SDimitry Andric #include "clang/Sema/Scope.h"
260b57cec5SDimitry Andric #include "llvm/ADT/Optional.h"
270b57cec5SDimitry Andric #include "llvm/ADT/SmallSet.h"
280b57cec5SDimitry Andric #include "llvm/ADT/SmallString.h"
290b57cec5SDimitry Andric #include "llvm/ADT/StringSwitch.h"
300b57cec5SDimitry Andric 
310b57cec5SDimitry Andric using namespace clang;
320b57cec5SDimitry Andric 
330b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
340b57cec5SDimitry Andric // C99 6.7: Declarations.
350b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
360b57cec5SDimitry Andric 
370b57cec5SDimitry Andric /// ParseTypeName
380b57cec5SDimitry Andric ///       type-name: [C99 6.7.6]
390b57cec5SDimitry Andric ///         specifier-qualifier-list abstract-declarator[opt]
400b57cec5SDimitry Andric ///
410b57cec5SDimitry Andric /// Called type-id in C++.
420b57cec5SDimitry Andric TypeResult Parser::ParseTypeName(SourceRange *Range,
430b57cec5SDimitry Andric                                  DeclaratorContext Context,
440b57cec5SDimitry Andric                                  AccessSpecifier AS,
450b57cec5SDimitry Andric                                  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.
600b57cec5SDimitry Andric   Declarator DeclaratorInfo(DS, 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 
1050b57cec5SDimitry Andric /// ParseGNUAttributes - Parse a non-empty attributes list.
1060b57cec5SDimitry Andric ///
1070b57cec5SDimitry Andric /// [GNU] attributes:
1080b57cec5SDimitry Andric ///         attribute
1090b57cec5SDimitry Andric ///         attributes attribute
1100b57cec5SDimitry Andric ///
1110b57cec5SDimitry Andric /// [GNU]  attribute:
1120b57cec5SDimitry Andric ///          '__attribute__' '(' '(' attribute-list ')' ')'
1130b57cec5SDimitry Andric ///
1140b57cec5SDimitry Andric /// [GNU]  attribute-list:
1150b57cec5SDimitry Andric ///          attrib
1160b57cec5SDimitry Andric ///          attribute_list ',' attrib
1170b57cec5SDimitry Andric ///
1180b57cec5SDimitry Andric /// [GNU]  attrib:
1190b57cec5SDimitry Andric ///          empty
1200b57cec5SDimitry Andric ///          attrib-name
1210b57cec5SDimitry Andric ///          attrib-name '(' identifier ')'
1220b57cec5SDimitry Andric ///          attrib-name '(' identifier ',' nonempty-expr-list ')'
1230b57cec5SDimitry Andric ///          attrib-name '(' argument-expression-list [C99 6.5.2] ')'
1240b57cec5SDimitry Andric ///
1250b57cec5SDimitry Andric /// [GNU]  attrib-name:
1260b57cec5SDimitry Andric ///          identifier
1270b57cec5SDimitry Andric ///          typespec
1280b57cec5SDimitry Andric ///          typequal
1290b57cec5SDimitry Andric ///          storageclass
1300b57cec5SDimitry Andric ///
1310b57cec5SDimitry Andric /// Whether an attribute takes an 'identifier' is determined by the
1320b57cec5SDimitry Andric /// attrib-name. GCC's behavior here is not worth imitating:
1330b57cec5SDimitry Andric ///
1340b57cec5SDimitry Andric ///  * In C mode, if the attribute argument list starts with an identifier
1350b57cec5SDimitry Andric ///    followed by a ',' or an ')', and the identifier doesn't resolve to
1360b57cec5SDimitry Andric ///    a type, it is parsed as an identifier. If the attribute actually
1370b57cec5SDimitry Andric ///    wanted an expression, it's out of luck (but it turns out that no
1380b57cec5SDimitry Andric ///    attributes work that way, because C constant expressions are very
1390b57cec5SDimitry Andric ///    limited).
1400b57cec5SDimitry Andric ///  * In C++ mode, if the attribute argument list starts with an identifier,
1410b57cec5SDimitry Andric ///    and the attribute *wants* an identifier, it is parsed as an identifier.
1420b57cec5SDimitry Andric ///    At block scope, any additional tokens between the identifier and the
1430b57cec5SDimitry Andric ///    ',' or ')' are ignored, otherwise they produce a parse error.
1440b57cec5SDimitry Andric ///
1450b57cec5SDimitry Andric /// We follow the C++ model, but don't allow junk after the identifier.
1460b57cec5SDimitry Andric void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
1470b57cec5SDimitry Andric                                 SourceLocation *endLoc,
1480b57cec5SDimitry Andric                                 LateParsedAttrList *LateAttrs,
1490b57cec5SDimitry Andric                                 Declarator *D) {
1500b57cec5SDimitry Andric   assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
1510b57cec5SDimitry Andric 
1520b57cec5SDimitry Andric   while (Tok.is(tok::kw___attribute)) {
1530b57cec5SDimitry Andric     SourceLocation AttrTokLoc = ConsumeToken();
1540b57cec5SDimitry Andric     unsigned OldNumAttrs = attrs.size();
1550b57cec5SDimitry Andric     unsigned OldNumLateAttrs = LateAttrs ? LateAttrs->size() : 0;
1560b57cec5SDimitry Andric 
1570b57cec5SDimitry Andric     if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
1580b57cec5SDimitry Andric                          "attribute")) {
1590b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
1600b57cec5SDimitry Andric       return;
1610b57cec5SDimitry Andric     }
1620b57cec5SDimitry Andric     if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
1630b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
1640b57cec5SDimitry Andric       return;
1650b57cec5SDimitry Andric     }
1660b57cec5SDimitry Andric     // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
1670b57cec5SDimitry Andric     do {
1680b57cec5SDimitry Andric       // Eat preceeding commas to allow __attribute__((,,,foo))
1690b57cec5SDimitry Andric       while (TryConsumeToken(tok::comma))
1700b57cec5SDimitry Andric         ;
1710b57cec5SDimitry Andric 
1720b57cec5SDimitry Andric       // Expect an identifier or declaration specifier (const, int, etc.)
1730b57cec5SDimitry Andric       if (Tok.isAnnotation())
1740b57cec5SDimitry Andric         break;
1750b57cec5SDimitry Andric       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
1760b57cec5SDimitry Andric       if (!AttrName)
1770b57cec5SDimitry Andric         break;
1780b57cec5SDimitry Andric 
1790b57cec5SDimitry Andric       SourceLocation AttrNameLoc = ConsumeToken();
1800b57cec5SDimitry Andric 
1810b57cec5SDimitry Andric       if (Tok.isNot(tok::l_paren)) {
1820b57cec5SDimitry Andric         attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
1830b57cec5SDimitry Andric                      ParsedAttr::AS_GNU);
1840b57cec5SDimitry Andric         continue;
1850b57cec5SDimitry Andric       }
1860b57cec5SDimitry Andric 
1870b57cec5SDimitry Andric       // Handle "parameterized" attributes
1880b57cec5SDimitry Andric       if (!LateAttrs || !isAttributeLateParsed(*AttrName)) {
1890b57cec5SDimitry Andric         ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, nullptr,
1900b57cec5SDimitry Andric                               SourceLocation(), ParsedAttr::AS_GNU, D);
1910b57cec5SDimitry Andric         continue;
1920b57cec5SDimitry Andric       }
1930b57cec5SDimitry Andric 
1940b57cec5SDimitry Andric       // Handle attributes with arguments that require late parsing.
1950b57cec5SDimitry Andric       LateParsedAttribute *LA =
1960b57cec5SDimitry Andric           new LateParsedAttribute(this, *AttrName, AttrNameLoc);
1970b57cec5SDimitry Andric       LateAttrs->push_back(LA);
1980b57cec5SDimitry Andric 
1990b57cec5SDimitry Andric       // Attributes in a class are parsed at the end of the class, along
2000b57cec5SDimitry Andric       // with other late-parsed declarations.
2010b57cec5SDimitry Andric       if (!ClassStack.empty() && !LateAttrs->parseSoon())
2020b57cec5SDimitry Andric         getCurrentClass().LateParsedDeclarations.push_back(LA);
2030b57cec5SDimitry Andric 
2040b57cec5SDimitry Andric       // Be sure ConsumeAndStoreUntil doesn't see the start l_paren, since it
2050b57cec5SDimitry Andric       // recursively consumes balanced parens.
2060b57cec5SDimitry Andric       LA->Toks.push_back(Tok);
2070b57cec5SDimitry Andric       ConsumeParen();
2080b57cec5SDimitry Andric       // Consume everything up to and including the matching right parens.
2090b57cec5SDimitry Andric       ConsumeAndStoreUntil(tok::r_paren, LA->Toks, /*StopAtSemi=*/true);
2100b57cec5SDimitry Andric 
2110b57cec5SDimitry Andric       Token Eof;
2120b57cec5SDimitry Andric       Eof.startToken();
2130b57cec5SDimitry Andric       Eof.setLocation(Tok.getLocation());
2140b57cec5SDimitry Andric       LA->Toks.push_back(Eof);
2150b57cec5SDimitry Andric     } while (Tok.is(tok::comma));
2160b57cec5SDimitry Andric 
2170b57cec5SDimitry Andric     if (ExpectAndConsume(tok::r_paren))
2180b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
2190b57cec5SDimitry Andric     SourceLocation Loc = Tok.getLocation();
2200b57cec5SDimitry Andric     if (ExpectAndConsume(tok::r_paren))
2210b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
2220b57cec5SDimitry Andric     if (endLoc)
2230b57cec5SDimitry Andric       *endLoc = Loc;
2240b57cec5SDimitry Andric 
2250b57cec5SDimitry Andric     // If this was declared in a macro, attach the macro IdentifierInfo to the
2260b57cec5SDimitry Andric     // parsed attribute.
2270b57cec5SDimitry Andric     auto &SM = PP.getSourceManager();
2280b57cec5SDimitry Andric     if (!SM.isWrittenInBuiltinFile(SM.getSpellingLoc(AttrTokLoc)) &&
2290b57cec5SDimitry Andric         FindLocsWithCommonFileID(PP, AttrTokLoc, Loc)) {
2300b57cec5SDimitry Andric       CharSourceRange ExpansionRange = SM.getExpansionRange(AttrTokLoc);
2310b57cec5SDimitry Andric       StringRef FoundName =
2320b57cec5SDimitry Andric           Lexer::getSourceText(ExpansionRange, SM, PP.getLangOpts());
2330b57cec5SDimitry Andric       IdentifierInfo *MacroII = PP.getIdentifierInfo(FoundName);
2340b57cec5SDimitry Andric 
2350b57cec5SDimitry Andric       for (unsigned i = OldNumAttrs; i < attrs.size(); ++i)
2360b57cec5SDimitry Andric         attrs[i].setMacroIdentifier(MacroII, ExpansionRange.getBegin());
2370b57cec5SDimitry Andric 
2380b57cec5SDimitry Andric       if (LateAttrs) {
2390b57cec5SDimitry Andric         for (unsigned i = OldNumLateAttrs; i < LateAttrs->size(); ++i)
2400b57cec5SDimitry Andric           (*LateAttrs)[i]->MacroII = MacroII;
2410b57cec5SDimitry Andric       }
2420b57cec5SDimitry Andric     }
2430b57cec5SDimitry Andric   }
2440b57cec5SDimitry Andric }
2450b57cec5SDimitry Andric 
2460b57cec5SDimitry Andric /// Determine whether the given attribute has an identifier argument.
2470b57cec5SDimitry Andric static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
2480b57cec5SDimitry Andric #define CLANG_ATTR_IDENTIFIER_ARG_LIST
2490b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
2500b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
2510b57cec5SDimitry Andric            .Default(false);
2520b57cec5SDimitry Andric #undef CLANG_ATTR_IDENTIFIER_ARG_LIST
2530b57cec5SDimitry Andric }
2540b57cec5SDimitry Andric 
2550b57cec5SDimitry Andric /// Determine whether the given attribute has a variadic identifier argument.
2560b57cec5SDimitry Andric static bool attributeHasVariadicIdentifierArg(const IdentifierInfo &II) {
2570b57cec5SDimitry Andric #define CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST
2580b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
2590b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
2600b57cec5SDimitry Andric            .Default(false);
2610b57cec5SDimitry Andric #undef CLANG_ATTR_VARIADIC_IDENTIFIER_ARG_LIST
2620b57cec5SDimitry Andric }
2630b57cec5SDimitry Andric 
2640b57cec5SDimitry Andric /// Determine whether the given attribute treats kw_this as an identifier.
2650b57cec5SDimitry Andric static bool attributeTreatsKeywordThisAsIdentifier(const IdentifierInfo &II) {
2660b57cec5SDimitry Andric #define CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST
2670b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
2680b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
2690b57cec5SDimitry Andric            .Default(false);
2700b57cec5SDimitry Andric #undef CLANG_ATTR_THIS_ISA_IDENTIFIER_ARG_LIST
2710b57cec5SDimitry Andric }
2720b57cec5SDimitry Andric 
2730b57cec5SDimitry Andric /// Determine whether the given attribute parses a type argument.
2740b57cec5SDimitry Andric static bool attributeIsTypeArgAttr(const IdentifierInfo &II) {
2750b57cec5SDimitry Andric #define CLANG_ATTR_TYPE_ARG_LIST
2760b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
2770b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
2780b57cec5SDimitry Andric            .Default(false);
2790b57cec5SDimitry Andric #undef CLANG_ATTR_TYPE_ARG_LIST
2800b57cec5SDimitry Andric }
2810b57cec5SDimitry Andric 
2820b57cec5SDimitry Andric /// Determine whether the given attribute requires parsing its arguments
2830b57cec5SDimitry Andric /// in an unevaluated context or not.
2840b57cec5SDimitry Andric static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) {
2850b57cec5SDimitry Andric #define CLANG_ATTR_ARG_CONTEXT_LIST
2860b57cec5SDimitry Andric   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
2870b57cec5SDimitry Andric #include "clang/Parse/AttrParserStringSwitches.inc"
2880b57cec5SDimitry Andric            .Default(false);
2890b57cec5SDimitry Andric #undef CLANG_ATTR_ARG_CONTEXT_LIST
2900b57cec5SDimitry Andric }
2910b57cec5SDimitry Andric 
2920b57cec5SDimitry Andric IdentifierLoc *Parser::ParseIdentifierLoc() {
2930b57cec5SDimitry Andric   assert(Tok.is(tok::identifier) && "expected an identifier");
2940b57cec5SDimitry Andric   IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
2950b57cec5SDimitry Andric                                             Tok.getLocation(),
2960b57cec5SDimitry Andric                                             Tok.getIdentifierInfo());
2970b57cec5SDimitry Andric   ConsumeToken();
2980b57cec5SDimitry Andric   return IL;
2990b57cec5SDimitry Andric }
3000b57cec5SDimitry Andric 
3010b57cec5SDimitry Andric void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
3020b57cec5SDimitry Andric                                        SourceLocation AttrNameLoc,
3030b57cec5SDimitry Andric                                        ParsedAttributes &Attrs,
3040b57cec5SDimitry Andric                                        SourceLocation *EndLoc,
3050b57cec5SDimitry Andric                                        IdentifierInfo *ScopeName,
3060b57cec5SDimitry Andric                                        SourceLocation ScopeLoc,
3070b57cec5SDimitry Andric                                        ParsedAttr::Syntax Syntax) {
3080b57cec5SDimitry Andric   BalancedDelimiterTracker Parens(*this, tok::l_paren);
3090b57cec5SDimitry Andric   Parens.consumeOpen();
3100b57cec5SDimitry Andric 
3110b57cec5SDimitry Andric   TypeResult T;
3120b57cec5SDimitry Andric   if (Tok.isNot(tok::r_paren))
3130b57cec5SDimitry Andric     T = ParseTypeName();
3140b57cec5SDimitry Andric 
3150b57cec5SDimitry Andric   if (Parens.consumeClose())
3160b57cec5SDimitry Andric     return;
3170b57cec5SDimitry Andric 
3180b57cec5SDimitry Andric   if (T.isInvalid())
3190b57cec5SDimitry Andric     return;
3200b57cec5SDimitry Andric 
3210b57cec5SDimitry Andric   if (T.isUsable())
3220b57cec5SDimitry Andric     Attrs.addNewTypeAttr(&AttrName,
3230b57cec5SDimitry Andric                          SourceRange(AttrNameLoc, Parens.getCloseLocation()),
3240b57cec5SDimitry Andric                          ScopeName, ScopeLoc, T.get(), Syntax);
3250b57cec5SDimitry Andric   else
3260b57cec5SDimitry Andric     Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()),
3270b57cec5SDimitry Andric                  ScopeName, ScopeLoc, nullptr, 0, Syntax);
3280b57cec5SDimitry Andric }
3290b57cec5SDimitry Andric 
3300b57cec5SDimitry Andric unsigned Parser::ParseAttributeArgsCommon(
3310b57cec5SDimitry Andric     IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
3320b57cec5SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
3330b57cec5SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax) {
3340b57cec5SDimitry Andric   // Ignore the left paren location for now.
3350b57cec5SDimitry Andric   ConsumeParen();
3360b57cec5SDimitry Andric 
3370b57cec5SDimitry Andric   bool ChangeKWThisToIdent = attributeTreatsKeywordThisAsIdentifier(*AttrName);
338a7dea167SDimitry Andric   bool AttributeIsTypeArgAttr = attributeIsTypeArgAttr(*AttrName);
3390b57cec5SDimitry Andric 
3400b57cec5SDimitry Andric   // Interpret "kw_this" as an identifier if the attributed requests it.
3410b57cec5SDimitry Andric   if (ChangeKWThisToIdent && Tok.is(tok::kw_this))
3420b57cec5SDimitry Andric     Tok.setKind(tok::identifier);
3430b57cec5SDimitry Andric 
3440b57cec5SDimitry Andric   ArgsVector ArgExprs;
3450b57cec5SDimitry Andric   if (Tok.is(tok::identifier)) {
3460b57cec5SDimitry Andric     // If this attribute wants an 'identifier' argument, make it so.
3470b57cec5SDimitry Andric     bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName) ||
3480b57cec5SDimitry Andric                            attributeHasVariadicIdentifierArg(*AttrName);
3490b57cec5SDimitry Andric     ParsedAttr::Kind AttrKind =
350a7dea167SDimitry Andric         ParsedAttr::getParsedKind(AttrName, ScopeName, Syntax);
3510b57cec5SDimitry Andric 
3520b57cec5SDimitry Andric     // If we don't know how to parse this attribute, but this is the only
3530b57cec5SDimitry Andric     // token in this argument, assume it's meant to be an identifier.
3540b57cec5SDimitry Andric     if (AttrKind == ParsedAttr::UnknownAttribute ||
3550b57cec5SDimitry Andric         AttrKind == ParsedAttr::IgnoredAttribute) {
3560b57cec5SDimitry Andric       const Token &Next = NextToken();
3570b57cec5SDimitry Andric       IsIdentifierArg = Next.isOneOf(tok::r_paren, tok::comma);
3580b57cec5SDimitry Andric     }
3590b57cec5SDimitry Andric 
3600b57cec5SDimitry Andric     if (IsIdentifierArg)
3610b57cec5SDimitry Andric       ArgExprs.push_back(ParseIdentifierLoc());
3620b57cec5SDimitry Andric   }
3630b57cec5SDimitry Andric 
364a7dea167SDimitry Andric   ParsedType TheParsedType;
3650b57cec5SDimitry Andric   if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
3660b57cec5SDimitry Andric     // Eat the comma.
3670b57cec5SDimitry Andric     if (!ArgExprs.empty())
3680b57cec5SDimitry Andric       ConsumeToken();
3690b57cec5SDimitry Andric 
3700b57cec5SDimitry Andric     // Parse the non-empty comma-separated list of expressions.
3710b57cec5SDimitry Andric     do {
3720b57cec5SDimitry Andric       // Interpret "kw_this" as an identifier if the attributed requests it.
3730b57cec5SDimitry Andric       if (ChangeKWThisToIdent && Tok.is(tok::kw_this))
3740b57cec5SDimitry Andric         Tok.setKind(tok::identifier);
3750b57cec5SDimitry Andric 
3760b57cec5SDimitry Andric       ExprResult ArgExpr;
377a7dea167SDimitry Andric       if (AttributeIsTypeArgAttr) {
378a7dea167SDimitry Andric         TypeResult T = ParseTypeName();
379a7dea167SDimitry Andric         if (T.isInvalid()) {
380a7dea167SDimitry Andric           SkipUntil(tok::r_paren, StopAtSemi);
381a7dea167SDimitry Andric           return 0;
382a7dea167SDimitry Andric         }
383a7dea167SDimitry Andric         if (T.isUsable())
384a7dea167SDimitry Andric           TheParsedType = T.get();
385a7dea167SDimitry Andric         break; // FIXME: Multiple type arguments are not implemented.
386a7dea167SDimitry Andric       } else if (Tok.is(tok::identifier) &&
3870b57cec5SDimitry Andric                  attributeHasVariadicIdentifierArg(*AttrName)) {
3880b57cec5SDimitry Andric         ArgExprs.push_back(ParseIdentifierLoc());
3890b57cec5SDimitry Andric       } else {
3900b57cec5SDimitry Andric         bool Uneval = attributeParsedArgsUnevaluated(*AttrName);
3910b57cec5SDimitry Andric         EnterExpressionEvaluationContext Unevaluated(
3920b57cec5SDimitry Andric             Actions,
3930b57cec5SDimitry Andric             Uneval ? Sema::ExpressionEvaluationContext::Unevaluated
3940b57cec5SDimitry Andric                    : Sema::ExpressionEvaluationContext::ConstantEvaluated);
3950b57cec5SDimitry Andric 
3960b57cec5SDimitry Andric         ExprResult ArgExpr(
3970b57cec5SDimitry Andric             Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()));
3980b57cec5SDimitry Andric         if (ArgExpr.isInvalid()) {
3990b57cec5SDimitry Andric           SkipUntil(tok::r_paren, StopAtSemi);
4000b57cec5SDimitry Andric           return 0;
4010b57cec5SDimitry Andric         }
4020b57cec5SDimitry Andric         ArgExprs.push_back(ArgExpr.get());
4030b57cec5SDimitry Andric       }
4040b57cec5SDimitry Andric       // Eat the comma, move to the next argument
4050b57cec5SDimitry Andric     } while (TryConsumeToken(tok::comma));
4060b57cec5SDimitry Andric   }
4070b57cec5SDimitry Andric 
4080b57cec5SDimitry Andric   SourceLocation RParen = Tok.getLocation();
4090b57cec5SDimitry Andric   if (!ExpectAndConsume(tok::r_paren)) {
4100b57cec5SDimitry Andric     SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
411a7dea167SDimitry Andric 
412a7dea167SDimitry Andric     if (AttributeIsTypeArgAttr && !TheParsedType.get().isNull()) {
413a7dea167SDimitry Andric       Attrs.addNewTypeAttr(AttrName, SourceRange(AttrNameLoc, RParen),
414a7dea167SDimitry Andric                            ScopeName, ScopeLoc, TheParsedType, Syntax);
415a7dea167SDimitry Andric     } else {
4160b57cec5SDimitry Andric       Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
4170b57cec5SDimitry Andric                    ArgExprs.data(), ArgExprs.size(), Syntax);
4180b57cec5SDimitry Andric     }
419a7dea167SDimitry Andric   }
4200b57cec5SDimitry Andric 
4210b57cec5SDimitry Andric   if (EndLoc)
4220b57cec5SDimitry Andric     *EndLoc = RParen;
4230b57cec5SDimitry Andric 
424a7dea167SDimitry Andric   return static_cast<unsigned>(ArgExprs.size() + !TheParsedType.get().isNull());
4250b57cec5SDimitry Andric }
4260b57cec5SDimitry Andric 
4270b57cec5SDimitry Andric /// Parse the arguments to a parameterized GNU attribute or
4280b57cec5SDimitry Andric /// a C++11 attribute in "gnu" namespace.
4290b57cec5SDimitry Andric void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
4300b57cec5SDimitry Andric                                    SourceLocation AttrNameLoc,
4310b57cec5SDimitry Andric                                    ParsedAttributes &Attrs,
4320b57cec5SDimitry Andric                                    SourceLocation *EndLoc,
4330b57cec5SDimitry Andric                                    IdentifierInfo *ScopeName,
4340b57cec5SDimitry Andric                                    SourceLocation ScopeLoc,
4350b57cec5SDimitry Andric                                    ParsedAttr::Syntax Syntax,
4360b57cec5SDimitry Andric                                    Declarator *D) {
4370b57cec5SDimitry Andric 
4380b57cec5SDimitry Andric   assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
4390b57cec5SDimitry Andric 
4400b57cec5SDimitry Andric   ParsedAttr::Kind AttrKind =
441a7dea167SDimitry Andric       ParsedAttr::getParsedKind(AttrName, ScopeName, Syntax);
4420b57cec5SDimitry Andric 
4430b57cec5SDimitry Andric   if (AttrKind == ParsedAttr::AT_Availability) {
4440b57cec5SDimitry Andric     ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
4450b57cec5SDimitry Andric                                ScopeLoc, Syntax);
4460b57cec5SDimitry Andric     return;
4470b57cec5SDimitry Andric   } else if (AttrKind == ParsedAttr::AT_ExternalSourceSymbol) {
4480b57cec5SDimitry Andric     ParseExternalSourceSymbolAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
4490b57cec5SDimitry Andric                                        ScopeName, ScopeLoc, Syntax);
4500b57cec5SDimitry Andric     return;
4510b57cec5SDimitry Andric   } else if (AttrKind == ParsedAttr::AT_ObjCBridgeRelated) {
4520b57cec5SDimitry Andric     ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
4530b57cec5SDimitry Andric                                     ScopeName, ScopeLoc, Syntax);
4540b57cec5SDimitry Andric     return;
4550b57cec5SDimitry Andric   } else if (AttrKind == ParsedAttr::AT_TypeTagForDatatype) {
4560b57cec5SDimitry Andric     ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
4570b57cec5SDimitry Andric                                      ScopeName, ScopeLoc, Syntax);
4580b57cec5SDimitry Andric     return;
4590b57cec5SDimitry Andric   } else if (attributeIsTypeArgAttr(*AttrName)) {
4600b57cec5SDimitry Andric     ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
4610b57cec5SDimitry Andric                               ScopeLoc, Syntax);
4620b57cec5SDimitry Andric     return;
4630b57cec5SDimitry Andric   }
4640b57cec5SDimitry Andric 
4650b57cec5SDimitry Andric   // These may refer to the function arguments, but need to be parsed early to
4660b57cec5SDimitry Andric   // participate in determining whether it's a redeclaration.
4670b57cec5SDimitry Andric   llvm::Optional<ParseScope> PrototypeScope;
4680b57cec5SDimitry Andric   if (normalizeAttrName(AttrName->getName()) == "enable_if" &&
4690b57cec5SDimitry Andric       D && D->isFunctionDeclarator()) {
4700b57cec5SDimitry Andric     DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo();
4710b57cec5SDimitry Andric     PrototypeScope.emplace(this, Scope::FunctionPrototypeScope |
4720b57cec5SDimitry Andric                                      Scope::FunctionDeclarationScope |
4730b57cec5SDimitry Andric                                      Scope::DeclScope);
4740b57cec5SDimitry Andric     for (unsigned i = 0; i != FTI.NumParams; ++i) {
4750b57cec5SDimitry Andric       ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param);
4760b57cec5SDimitry Andric       Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param);
4770b57cec5SDimitry Andric     }
4780b57cec5SDimitry Andric   }
4790b57cec5SDimitry Andric 
4800b57cec5SDimitry Andric   ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
4810b57cec5SDimitry Andric                            ScopeLoc, Syntax);
4820b57cec5SDimitry Andric }
4830b57cec5SDimitry Andric 
4840b57cec5SDimitry Andric unsigned Parser::ParseClangAttributeArgs(
4850b57cec5SDimitry Andric     IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
4860b57cec5SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
4870b57cec5SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax) {
4880b57cec5SDimitry Andric   assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
4890b57cec5SDimitry Andric 
4900b57cec5SDimitry Andric   ParsedAttr::Kind AttrKind =
491a7dea167SDimitry Andric       ParsedAttr::getParsedKind(AttrName, ScopeName, Syntax);
4920b57cec5SDimitry Andric 
4930b57cec5SDimitry Andric   switch (AttrKind) {
4940b57cec5SDimitry Andric   default:
4950b57cec5SDimitry Andric     return ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc,
4960b57cec5SDimitry Andric                                     ScopeName, ScopeLoc, Syntax);
4970b57cec5SDimitry Andric   case ParsedAttr::AT_ExternalSourceSymbol:
4980b57cec5SDimitry Andric     ParseExternalSourceSymbolAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
4990b57cec5SDimitry Andric                                        ScopeName, ScopeLoc, Syntax);
5000b57cec5SDimitry Andric     break;
5010b57cec5SDimitry Andric   case ParsedAttr::AT_Availability:
5020b57cec5SDimitry Andric     ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
5030b57cec5SDimitry Andric                                ScopeLoc, Syntax);
5040b57cec5SDimitry Andric     break;
5050b57cec5SDimitry Andric   case ParsedAttr::AT_ObjCBridgeRelated:
5060b57cec5SDimitry Andric     ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
5070b57cec5SDimitry Andric                                     ScopeName, ScopeLoc, Syntax);
5080b57cec5SDimitry Andric     break;
5090b57cec5SDimitry Andric   case ParsedAttr::AT_TypeTagForDatatype:
5100b57cec5SDimitry Andric     ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
5110b57cec5SDimitry Andric                                      ScopeName, ScopeLoc, Syntax);
5120b57cec5SDimitry Andric     break;
5130b57cec5SDimitry Andric   }
5140b57cec5SDimitry Andric   return !Attrs.empty() ? Attrs.begin()->getNumArgs() : 0;
5150b57cec5SDimitry Andric }
5160b57cec5SDimitry Andric 
5170b57cec5SDimitry Andric bool Parser::ParseMicrosoftDeclSpecArgs(IdentifierInfo *AttrName,
5180b57cec5SDimitry Andric                                         SourceLocation AttrNameLoc,
5190b57cec5SDimitry Andric                                         ParsedAttributes &Attrs) {
5200b57cec5SDimitry Andric   // If the attribute isn't known, we will not attempt to parse any
5210b57cec5SDimitry Andric   // arguments.
5220b57cec5SDimitry Andric   if (!hasAttribute(AttrSyntax::Declspec, nullptr, AttrName,
5230b57cec5SDimitry Andric                     getTargetInfo(), getLangOpts())) {
5240b57cec5SDimitry Andric     // Eat the left paren, then skip to the ending right paren.
5250b57cec5SDimitry Andric     ConsumeParen();
5260b57cec5SDimitry Andric     SkipUntil(tok::r_paren);
5270b57cec5SDimitry Andric     return false;
5280b57cec5SDimitry Andric   }
5290b57cec5SDimitry Andric 
5300b57cec5SDimitry Andric   SourceLocation OpenParenLoc = Tok.getLocation();
5310b57cec5SDimitry Andric 
5320b57cec5SDimitry Andric   if (AttrName->getName() == "property") {
5330b57cec5SDimitry Andric     // The property declspec is more complex in that it can take one or two
5340b57cec5SDimitry Andric     // assignment expressions as a parameter, but the lhs of the assignment
5350b57cec5SDimitry Andric     // must be named get or put.
5360b57cec5SDimitry Andric 
5370b57cec5SDimitry Andric     BalancedDelimiterTracker T(*this, tok::l_paren);
5380b57cec5SDimitry Andric     T.expectAndConsume(diag::err_expected_lparen_after,
5390b57cec5SDimitry Andric                        AttrName->getNameStart(), tok::r_paren);
5400b57cec5SDimitry Andric 
5410b57cec5SDimitry Andric     enum AccessorKind {
5420b57cec5SDimitry Andric       AK_Invalid = -1,
5430b57cec5SDimitry Andric       AK_Put = 0,
5440b57cec5SDimitry Andric       AK_Get = 1 // indices into AccessorNames
5450b57cec5SDimitry Andric     };
5460b57cec5SDimitry Andric     IdentifierInfo *AccessorNames[] = {nullptr, nullptr};
5470b57cec5SDimitry Andric     bool HasInvalidAccessor = false;
5480b57cec5SDimitry Andric 
5490b57cec5SDimitry Andric     // Parse the accessor specifications.
5500b57cec5SDimitry Andric     while (true) {
5510b57cec5SDimitry Andric       // Stop if this doesn't look like an accessor spec.
5520b57cec5SDimitry Andric       if (!Tok.is(tok::identifier)) {
5530b57cec5SDimitry Andric         // If the user wrote a completely empty list, use a special diagnostic.
5540b57cec5SDimitry Andric         if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
5550b57cec5SDimitry Andric             AccessorNames[AK_Put] == nullptr &&
5560b57cec5SDimitry Andric             AccessorNames[AK_Get] == nullptr) {
5570b57cec5SDimitry Andric           Diag(AttrNameLoc, diag::err_ms_property_no_getter_or_putter);
5580b57cec5SDimitry Andric           break;
5590b57cec5SDimitry Andric         }
5600b57cec5SDimitry Andric 
5610b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
5620b57cec5SDimitry Andric         break;
5630b57cec5SDimitry Andric       }
5640b57cec5SDimitry Andric 
5650b57cec5SDimitry Andric       AccessorKind Kind;
5660b57cec5SDimitry Andric       SourceLocation KindLoc = Tok.getLocation();
5670b57cec5SDimitry Andric       StringRef KindStr = Tok.getIdentifierInfo()->getName();
5680b57cec5SDimitry Andric       if (KindStr == "get") {
5690b57cec5SDimitry Andric         Kind = AK_Get;
5700b57cec5SDimitry Andric       } else if (KindStr == "put") {
5710b57cec5SDimitry Andric         Kind = AK_Put;
5720b57cec5SDimitry Andric 
5730b57cec5SDimitry Andric         // Recover from the common mistake of using 'set' instead of 'put'.
5740b57cec5SDimitry Andric       } else if (KindStr == "set") {
5750b57cec5SDimitry Andric         Diag(KindLoc, diag::err_ms_property_has_set_accessor)
5760b57cec5SDimitry Andric             << FixItHint::CreateReplacement(KindLoc, "put");
5770b57cec5SDimitry Andric         Kind = AK_Put;
5780b57cec5SDimitry Andric 
5790b57cec5SDimitry Andric         // Handle the mistake of forgetting the accessor kind by skipping
5800b57cec5SDimitry Andric         // this accessor.
5810b57cec5SDimitry Andric       } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
5820b57cec5SDimitry Andric         Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
5830b57cec5SDimitry Andric         ConsumeToken();
5840b57cec5SDimitry Andric         HasInvalidAccessor = true;
5850b57cec5SDimitry Andric         goto next_property_accessor;
5860b57cec5SDimitry Andric 
5870b57cec5SDimitry Andric         // Otherwise, complain about the unknown accessor kind.
5880b57cec5SDimitry Andric       } else {
5890b57cec5SDimitry Andric         Diag(KindLoc, diag::err_ms_property_unknown_accessor);
5900b57cec5SDimitry Andric         HasInvalidAccessor = true;
5910b57cec5SDimitry Andric         Kind = AK_Invalid;
5920b57cec5SDimitry Andric 
5930b57cec5SDimitry Andric         // Try to keep parsing unless it doesn't look like an accessor spec.
5940b57cec5SDimitry Andric         if (!NextToken().is(tok::equal))
5950b57cec5SDimitry Andric           break;
5960b57cec5SDimitry Andric       }
5970b57cec5SDimitry Andric 
5980b57cec5SDimitry Andric       // Consume the identifier.
5990b57cec5SDimitry Andric       ConsumeToken();
6000b57cec5SDimitry Andric 
6010b57cec5SDimitry Andric       // Consume the '='.
6020b57cec5SDimitry Andric       if (!TryConsumeToken(tok::equal)) {
6030b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
6040b57cec5SDimitry Andric             << KindStr;
6050b57cec5SDimitry Andric         break;
6060b57cec5SDimitry Andric       }
6070b57cec5SDimitry Andric 
6080b57cec5SDimitry Andric       // Expect the method name.
6090b57cec5SDimitry Andric       if (!Tok.is(tok::identifier)) {
6100b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
6110b57cec5SDimitry Andric         break;
6120b57cec5SDimitry Andric       }
6130b57cec5SDimitry Andric 
6140b57cec5SDimitry Andric       if (Kind == AK_Invalid) {
6150b57cec5SDimitry Andric         // Just drop invalid accessors.
6160b57cec5SDimitry Andric       } else if (AccessorNames[Kind] != nullptr) {
6170b57cec5SDimitry Andric         // Complain about the repeated accessor, ignore it, and keep parsing.
6180b57cec5SDimitry Andric         Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
6190b57cec5SDimitry Andric       } else {
6200b57cec5SDimitry Andric         AccessorNames[Kind] = Tok.getIdentifierInfo();
6210b57cec5SDimitry Andric       }
6220b57cec5SDimitry Andric       ConsumeToken();
6230b57cec5SDimitry Andric 
6240b57cec5SDimitry Andric     next_property_accessor:
6250b57cec5SDimitry Andric       // Keep processing accessors until we run out.
6260b57cec5SDimitry Andric       if (TryConsumeToken(tok::comma))
6270b57cec5SDimitry Andric         continue;
6280b57cec5SDimitry Andric 
6290b57cec5SDimitry Andric       // If we run into the ')', stop without consuming it.
6300b57cec5SDimitry Andric       if (Tok.is(tok::r_paren))
6310b57cec5SDimitry Andric         break;
6320b57cec5SDimitry Andric 
6330b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
6340b57cec5SDimitry Andric       break;
6350b57cec5SDimitry Andric     }
6360b57cec5SDimitry Andric 
6370b57cec5SDimitry Andric     // Only add the property attribute if it was well-formed.
6380b57cec5SDimitry Andric     if (!HasInvalidAccessor)
6390b57cec5SDimitry Andric       Attrs.addNewPropertyAttr(AttrName, AttrNameLoc, nullptr, SourceLocation(),
6400b57cec5SDimitry Andric                                AccessorNames[AK_Get], AccessorNames[AK_Put],
6410b57cec5SDimitry Andric                                ParsedAttr::AS_Declspec);
6420b57cec5SDimitry Andric     T.skipToEnd();
6430b57cec5SDimitry Andric     return !HasInvalidAccessor;
6440b57cec5SDimitry Andric   }
6450b57cec5SDimitry Andric 
6460b57cec5SDimitry Andric   unsigned NumArgs =
6470b57cec5SDimitry Andric       ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, nullptr, nullptr,
6480b57cec5SDimitry Andric                                SourceLocation(), ParsedAttr::AS_Declspec);
6490b57cec5SDimitry Andric 
6500b57cec5SDimitry Andric   // If this attribute's args were parsed, and it was expected to have
6510b57cec5SDimitry Andric   // arguments but none were provided, emit a diagnostic.
6520b57cec5SDimitry Andric   if (!Attrs.empty() && Attrs.begin()->getMaxArgs() && !NumArgs) {
6530b57cec5SDimitry Andric     Diag(OpenParenLoc, diag::err_attribute_requires_arguments) << AttrName;
6540b57cec5SDimitry Andric     return false;
6550b57cec5SDimitry Andric   }
6560b57cec5SDimitry Andric   return true;
6570b57cec5SDimitry Andric }
6580b57cec5SDimitry Andric 
6590b57cec5SDimitry Andric /// [MS] decl-specifier:
6600b57cec5SDimitry Andric ///             __declspec ( extended-decl-modifier-seq )
6610b57cec5SDimitry Andric ///
6620b57cec5SDimitry Andric /// [MS] extended-decl-modifier-seq:
6630b57cec5SDimitry Andric ///             extended-decl-modifier[opt]
6640b57cec5SDimitry Andric ///             extended-decl-modifier extended-decl-modifier-seq
6650b57cec5SDimitry Andric void Parser::ParseMicrosoftDeclSpecs(ParsedAttributes &Attrs,
6660b57cec5SDimitry Andric                                      SourceLocation *End) {
6670b57cec5SDimitry Andric   assert(getLangOpts().DeclSpecKeyword && "__declspec keyword is not enabled");
6680b57cec5SDimitry Andric   assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
6690b57cec5SDimitry Andric 
6700b57cec5SDimitry Andric   while (Tok.is(tok::kw___declspec)) {
6710b57cec5SDimitry Andric     ConsumeToken();
6720b57cec5SDimitry Andric     BalancedDelimiterTracker T(*this, tok::l_paren);
6730b57cec5SDimitry Andric     if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
6740b57cec5SDimitry Andric                            tok::r_paren))
6750b57cec5SDimitry Andric       return;
6760b57cec5SDimitry Andric 
6770b57cec5SDimitry Andric     // An empty declspec is perfectly legal and should not warn.  Additionally,
6780b57cec5SDimitry Andric     // you can specify multiple attributes per declspec.
6790b57cec5SDimitry Andric     while (Tok.isNot(tok::r_paren)) {
6800b57cec5SDimitry Andric       // Attribute not present.
6810b57cec5SDimitry Andric       if (TryConsumeToken(tok::comma))
6820b57cec5SDimitry Andric         continue;
6830b57cec5SDimitry Andric 
6840b57cec5SDimitry Andric       // We expect either a well-known identifier or a generic string.  Anything
6850b57cec5SDimitry Andric       // else is a malformed declspec.
6860b57cec5SDimitry Andric       bool IsString = Tok.getKind() == tok::string_literal;
6870b57cec5SDimitry Andric       if (!IsString && Tok.getKind() != tok::identifier &&
6880b57cec5SDimitry Andric           Tok.getKind() != tok::kw_restrict) {
6890b57cec5SDimitry Andric         Diag(Tok, diag::err_ms_declspec_type);
6900b57cec5SDimitry Andric         T.skipToEnd();
6910b57cec5SDimitry Andric         return;
6920b57cec5SDimitry Andric       }
6930b57cec5SDimitry Andric 
6940b57cec5SDimitry Andric       IdentifierInfo *AttrName;
6950b57cec5SDimitry Andric       SourceLocation AttrNameLoc;
6960b57cec5SDimitry Andric       if (IsString) {
6970b57cec5SDimitry Andric         SmallString<8> StrBuffer;
6980b57cec5SDimitry Andric         bool Invalid = false;
6990b57cec5SDimitry Andric         StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
7000b57cec5SDimitry Andric         if (Invalid) {
7010b57cec5SDimitry Andric           T.skipToEnd();
7020b57cec5SDimitry Andric           return;
7030b57cec5SDimitry Andric         }
7040b57cec5SDimitry Andric         AttrName = PP.getIdentifierInfo(Str);
7050b57cec5SDimitry Andric         AttrNameLoc = ConsumeStringToken();
7060b57cec5SDimitry Andric       } else {
7070b57cec5SDimitry Andric         AttrName = Tok.getIdentifierInfo();
7080b57cec5SDimitry Andric         AttrNameLoc = ConsumeToken();
7090b57cec5SDimitry Andric       }
7100b57cec5SDimitry Andric 
7110b57cec5SDimitry Andric       bool AttrHandled = false;
7120b57cec5SDimitry Andric 
7130b57cec5SDimitry Andric       // Parse attribute arguments.
7140b57cec5SDimitry Andric       if (Tok.is(tok::l_paren))
7150b57cec5SDimitry Andric         AttrHandled = ParseMicrosoftDeclSpecArgs(AttrName, AttrNameLoc, Attrs);
7160b57cec5SDimitry Andric       else if (AttrName->getName() == "property")
7170b57cec5SDimitry Andric         // The property attribute must have an argument list.
7180b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_expected_lparen_after)
7190b57cec5SDimitry Andric             << AttrName->getName();
7200b57cec5SDimitry Andric 
7210b57cec5SDimitry Andric       if (!AttrHandled)
7220b57cec5SDimitry Andric         Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
7230b57cec5SDimitry Andric                      ParsedAttr::AS_Declspec);
7240b57cec5SDimitry Andric     }
7250b57cec5SDimitry Andric     T.consumeClose();
7260b57cec5SDimitry Andric     if (End)
7270b57cec5SDimitry Andric       *End = T.getCloseLocation();
7280b57cec5SDimitry Andric   }
7290b57cec5SDimitry Andric }
7300b57cec5SDimitry Andric 
7310b57cec5SDimitry Andric void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
7320b57cec5SDimitry Andric   // Treat these like attributes
7330b57cec5SDimitry Andric   while (true) {
7340b57cec5SDimitry Andric     switch (Tok.getKind()) {
7350b57cec5SDimitry Andric     case tok::kw___fastcall:
7360b57cec5SDimitry Andric     case tok::kw___stdcall:
7370b57cec5SDimitry Andric     case tok::kw___thiscall:
7380b57cec5SDimitry Andric     case tok::kw___regcall:
7390b57cec5SDimitry Andric     case tok::kw___cdecl:
7400b57cec5SDimitry Andric     case tok::kw___vectorcall:
7410b57cec5SDimitry Andric     case tok::kw___ptr64:
7420b57cec5SDimitry Andric     case tok::kw___w64:
7430b57cec5SDimitry Andric     case tok::kw___ptr32:
7440b57cec5SDimitry Andric     case tok::kw___sptr:
7450b57cec5SDimitry Andric     case tok::kw___uptr: {
7460b57cec5SDimitry Andric       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
7470b57cec5SDimitry Andric       SourceLocation AttrNameLoc = ConsumeToken();
7480b57cec5SDimitry Andric       attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
7490b57cec5SDimitry Andric                    ParsedAttr::AS_Keyword);
7500b57cec5SDimitry Andric       break;
7510b57cec5SDimitry Andric     }
7520b57cec5SDimitry Andric     default:
7530b57cec5SDimitry Andric       return;
7540b57cec5SDimitry Andric     }
7550b57cec5SDimitry Andric   }
7560b57cec5SDimitry Andric }
7570b57cec5SDimitry Andric 
7580b57cec5SDimitry Andric void Parser::DiagnoseAndSkipExtendedMicrosoftTypeAttributes() {
7590b57cec5SDimitry Andric   SourceLocation StartLoc = Tok.getLocation();
7600b57cec5SDimitry Andric   SourceLocation EndLoc = SkipExtendedMicrosoftTypeAttributes();
7610b57cec5SDimitry Andric 
7620b57cec5SDimitry Andric   if (EndLoc.isValid()) {
7630b57cec5SDimitry Andric     SourceRange Range(StartLoc, EndLoc);
7640b57cec5SDimitry Andric     Diag(StartLoc, diag::warn_microsoft_qualifiers_ignored) << Range;
7650b57cec5SDimitry Andric   }
7660b57cec5SDimitry Andric }
7670b57cec5SDimitry Andric 
7680b57cec5SDimitry Andric SourceLocation Parser::SkipExtendedMicrosoftTypeAttributes() {
7690b57cec5SDimitry Andric   SourceLocation EndLoc;
7700b57cec5SDimitry Andric 
7710b57cec5SDimitry Andric   while (true) {
7720b57cec5SDimitry Andric     switch (Tok.getKind()) {
7730b57cec5SDimitry Andric     case tok::kw_const:
7740b57cec5SDimitry Andric     case tok::kw_volatile:
7750b57cec5SDimitry Andric     case tok::kw___fastcall:
7760b57cec5SDimitry Andric     case tok::kw___stdcall:
7770b57cec5SDimitry Andric     case tok::kw___thiscall:
7780b57cec5SDimitry Andric     case tok::kw___cdecl:
7790b57cec5SDimitry Andric     case tok::kw___vectorcall:
7800b57cec5SDimitry Andric     case tok::kw___ptr32:
7810b57cec5SDimitry Andric     case tok::kw___ptr64:
7820b57cec5SDimitry Andric     case tok::kw___w64:
7830b57cec5SDimitry Andric     case tok::kw___unaligned:
7840b57cec5SDimitry Andric     case tok::kw___sptr:
7850b57cec5SDimitry Andric     case tok::kw___uptr:
7860b57cec5SDimitry Andric       EndLoc = ConsumeToken();
7870b57cec5SDimitry Andric       break;
7880b57cec5SDimitry Andric     default:
7890b57cec5SDimitry Andric       return EndLoc;
7900b57cec5SDimitry Andric     }
7910b57cec5SDimitry Andric   }
7920b57cec5SDimitry Andric }
7930b57cec5SDimitry Andric 
7940b57cec5SDimitry Andric void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
7950b57cec5SDimitry Andric   // Treat these like attributes
7960b57cec5SDimitry Andric   while (Tok.is(tok::kw___pascal)) {
7970b57cec5SDimitry Andric     IdentifierInfo *AttrName = Tok.getIdentifierInfo();
7980b57cec5SDimitry Andric     SourceLocation AttrNameLoc = ConsumeToken();
7990b57cec5SDimitry Andric     attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
8000b57cec5SDimitry Andric                  ParsedAttr::AS_Keyword);
8010b57cec5SDimitry Andric   }
8020b57cec5SDimitry Andric }
8030b57cec5SDimitry Andric 
8040b57cec5SDimitry Andric void Parser::ParseOpenCLKernelAttributes(ParsedAttributes &attrs) {
8050b57cec5SDimitry Andric   // Treat these like attributes
8060b57cec5SDimitry Andric   while (Tok.is(tok::kw___kernel)) {
8070b57cec5SDimitry Andric     IdentifierInfo *AttrName = Tok.getIdentifierInfo();
8080b57cec5SDimitry Andric     SourceLocation AttrNameLoc = ConsumeToken();
8090b57cec5SDimitry Andric     attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
8100b57cec5SDimitry Andric                  ParsedAttr::AS_Keyword);
8110b57cec5SDimitry Andric   }
8120b57cec5SDimitry Andric }
8130b57cec5SDimitry Andric 
8140b57cec5SDimitry Andric void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) {
8150b57cec5SDimitry Andric   IdentifierInfo *AttrName = Tok.getIdentifierInfo();
8160b57cec5SDimitry Andric   SourceLocation AttrNameLoc = Tok.getLocation();
8170b57cec5SDimitry Andric   Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
8180b57cec5SDimitry Andric                ParsedAttr::AS_Keyword);
8190b57cec5SDimitry Andric }
8200b57cec5SDimitry Andric 
8210b57cec5SDimitry Andric void Parser::ParseNullabilityTypeSpecifiers(ParsedAttributes &attrs) {
8220b57cec5SDimitry Andric   // Treat these like attributes, even though they're type specifiers.
8230b57cec5SDimitry Andric   while (true) {
8240b57cec5SDimitry Andric     switch (Tok.getKind()) {
8250b57cec5SDimitry Andric     case tok::kw__Nonnull:
8260b57cec5SDimitry Andric     case tok::kw__Nullable:
8270b57cec5SDimitry Andric     case tok::kw__Null_unspecified: {
8280b57cec5SDimitry Andric       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
8290b57cec5SDimitry Andric       SourceLocation AttrNameLoc = ConsumeToken();
8300b57cec5SDimitry Andric       if (!getLangOpts().ObjC)
8310b57cec5SDimitry Andric         Diag(AttrNameLoc, diag::ext_nullability)
8320b57cec5SDimitry Andric           << AttrName;
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 static bool VersionNumberSeparator(const char Separator) {
8440b57cec5SDimitry Andric   return (Separator == '.' || Separator == '_');
8450b57cec5SDimitry Andric }
8460b57cec5SDimitry Andric 
8470b57cec5SDimitry Andric /// Parse a version number.
8480b57cec5SDimitry Andric ///
8490b57cec5SDimitry Andric /// version:
8500b57cec5SDimitry Andric ///   simple-integer
8510b57cec5SDimitry Andric ///   simple-integer '.' simple-integer
8520b57cec5SDimitry Andric ///   simple-integer '_' simple-integer
8530b57cec5SDimitry Andric ///   simple-integer '.' simple-integer '.' simple-integer
8540b57cec5SDimitry Andric ///   simple-integer '_' simple-integer '_' simple-integer
8550b57cec5SDimitry Andric VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
8560b57cec5SDimitry Andric   Range = SourceRange(Tok.getLocation(), Tok.getEndLoc());
8570b57cec5SDimitry Andric 
8580b57cec5SDimitry Andric   if (!Tok.is(tok::numeric_constant)) {
8590b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
8600b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
8610b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
8620b57cec5SDimitry Andric     return VersionTuple();
8630b57cec5SDimitry Andric   }
8640b57cec5SDimitry Andric 
8650b57cec5SDimitry Andric   // Parse the major (and possibly minor and subminor) versions, which
8660b57cec5SDimitry Andric   // are stored in the numeric constant. We utilize a quirk of the
8670b57cec5SDimitry Andric   // lexer, which is that it handles something like 1.2.3 as a single
8680b57cec5SDimitry Andric   // numeric constant, rather than two separate tokens.
8690b57cec5SDimitry Andric   SmallString<512> Buffer;
8700b57cec5SDimitry Andric   Buffer.resize(Tok.getLength()+1);
8710b57cec5SDimitry Andric   const char *ThisTokBegin = &Buffer[0];
8720b57cec5SDimitry Andric 
8730b57cec5SDimitry Andric   // Get the spelling of the token, which eliminates trigraphs, etc.
8740b57cec5SDimitry Andric   bool Invalid = false;
8750b57cec5SDimitry Andric   unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
8760b57cec5SDimitry Andric   if (Invalid)
8770b57cec5SDimitry Andric     return VersionTuple();
8780b57cec5SDimitry Andric 
8790b57cec5SDimitry Andric   // Parse the major version.
8800b57cec5SDimitry Andric   unsigned AfterMajor = 0;
8810b57cec5SDimitry Andric   unsigned Major = 0;
8820b57cec5SDimitry Andric   while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
8830b57cec5SDimitry Andric     Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
8840b57cec5SDimitry Andric     ++AfterMajor;
8850b57cec5SDimitry Andric   }
8860b57cec5SDimitry Andric 
8870b57cec5SDimitry Andric   if (AfterMajor == 0) {
8880b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
8890b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
8900b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
8910b57cec5SDimitry Andric     return VersionTuple();
8920b57cec5SDimitry Andric   }
8930b57cec5SDimitry Andric 
8940b57cec5SDimitry Andric   if (AfterMajor == ActualLength) {
8950b57cec5SDimitry Andric     ConsumeToken();
8960b57cec5SDimitry Andric 
8970b57cec5SDimitry Andric     // We only had a single version component.
8980b57cec5SDimitry Andric     if (Major == 0) {
8990b57cec5SDimitry Andric       Diag(Tok, diag::err_zero_version);
9000b57cec5SDimitry Andric       return VersionTuple();
9010b57cec5SDimitry Andric     }
9020b57cec5SDimitry Andric 
9030b57cec5SDimitry Andric     return VersionTuple(Major);
9040b57cec5SDimitry Andric   }
9050b57cec5SDimitry Andric 
9060b57cec5SDimitry Andric   const char AfterMajorSeparator = ThisTokBegin[AfterMajor];
9070b57cec5SDimitry Andric   if (!VersionNumberSeparator(AfterMajorSeparator)
9080b57cec5SDimitry Andric       || (AfterMajor + 1 == ActualLength)) {
9090b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
9100b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
9110b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
9120b57cec5SDimitry Andric     return VersionTuple();
9130b57cec5SDimitry Andric   }
9140b57cec5SDimitry Andric 
9150b57cec5SDimitry Andric   // Parse the minor version.
9160b57cec5SDimitry Andric   unsigned AfterMinor = AfterMajor + 1;
9170b57cec5SDimitry Andric   unsigned Minor = 0;
9180b57cec5SDimitry Andric   while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
9190b57cec5SDimitry Andric     Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
9200b57cec5SDimitry Andric     ++AfterMinor;
9210b57cec5SDimitry Andric   }
9220b57cec5SDimitry Andric 
9230b57cec5SDimitry Andric   if (AfterMinor == ActualLength) {
9240b57cec5SDimitry Andric     ConsumeToken();
9250b57cec5SDimitry Andric 
9260b57cec5SDimitry Andric     // We had major.minor.
9270b57cec5SDimitry Andric     if (Major == 0 && Minor == 0) {
9280b57cec5SDimitry Andric       Diag(Tok, diag::err_zero_version);
9290b57cec5SDimitry Andric       return VersionTuple();
9300b57cec5SDimitry Andric     }
9310b57cec5SDimitry Andric 
9320b57cec5SDimitry Andric     return VersionTuple(Major, Minor);
9330b57cec5SDimitry Andric   }
9340b57cec5SDimitry Andric 
9350b57cec5SDimitry Andric   const char AfterMinorSeparator = ThisTokBegin[AfterMinor];
9360b57cec5SDimitry Andric   // If what follows is not a '.' or '_', we have a problem.
9370b57cec5SDimitry Andric   if (!VersionNumberSeparator(AfterMinorSeparator)) {
9380b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
9390b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
9400b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
9410b57cec5SDimitry Andric     return VersionTuple();
9420b57cec5SDimitry Andric   }
9430b57cec5SDimitry Andric 
9440b57cec5SDimitry Andric   // Warn if separators, be it '.' or '_', do not match.
9450b57cec5SDimitry Andric   if (AfterMajorSeparator != AfterMinorSeparator)
9460b57cec5SDimitry Andric     Diag(Tok, diag::warn_expected_consistent_version_separator);
9470b57cec5SDimitry Andric 
9480b57cec5SDimitry Andric   // Parse the subminor version.
9490b57cec5SDimitry Andric   unsigned AfterSubminor = AfterMinor + 1;
9500b57cec5SDimitry Andric   unsigned Subminor = 0;
9510b57cec5SDimitry Andric   while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
9520b57cec5SDimitry Andric     Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
9530b57cec5SDimitry Andric     ++AfterSubminor;
9540b57cec5SDimitry Andric   }
9550b57cec5SDimitry Andric 
9560b57cec5SDimitry Andric   if (AfterSubminor != ActualLength) {
9570b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_version);
9580b57cec5SDimitry Andric     SkipUntil(tok::comma, tok::r_paren,
9590b57cec5SDimitry Andric               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
9600b57cec5SDimitry Andric     return VersionTuple();
9610b57cec5SDimitry Andric   }
9620b57cec5SDimitry Andric   ConsumeToken();
9630b57cec5SDimitry Andric   return VersionTuple(Major, Minor, Subminor);
9640b57cec5SDimitry Andric }
9650b57cec5SDimitry Andric 
9660b57cec5SDimitry Andric /// Parse the contents of the "availability" attribute.
9670b57cec5SDimitry Andric ///
9680b57cec5SDimitry Andric /// availability-attribute:
9690b57cec5SDimitry Andric ///   'availability' '(' platform ',' opt-strict version-arg-list,
9700b57cec5SDimitry Andric ///                      opt-replacement, opt-message')'
9710b57cec5SDimitry Andric ///
9720b57cec5SDimitry Andric /// platform:
9730b57cec5SDimitry Andric ///   identifier
9740b57cec5SDimitry Andric ///
9750b57cec5SDimitry Andric /// opt-strict:
9760b57cec5SDimitry Andric ///   'strict' ','
9770b57cec5SDimitry Andric ///
9780b57cec5SDimitry Andric /// version-arg-list:
9790b57cec5SDimitry Andric ///   version-arg
9800b57cec5SDimitry Andric ///   version-arg ',' version-arg-list
9810b57cec5SDimitry Andric ///
9820b57cec5SDimitry Andric /// version-arg:
9830b57cec5SDimitry Andric ///   'introduced' '=' version
9840b57cec5SDimitry Andric ///   'deprecated' '=' version
9850b57cec5SDimitry Andric ///   'obsoleted' = version
9860b57cec5SDimitry Andric ///   'unavailable'
9870b57cec5SDimitry Andric /// opt-replacement:
9880b57cec5SDimitry Andric ///   'replacement' '=' <string>
9890b57cec5SDimitry Andric /// opt-message:
9900b57cec5SDimitry Andric ///   'message' '=' <string>
9910b57cec5SDimitry Andric void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
9920b57cec5SDimitry Andric                                         SourceLocation AvailabilityLoc,
9930b57cec5SDimitry Andric                                         ParsedAttributes &attrs,
9940b57cec5SDimitry Andric                                         SourceLocation *endLoc,
9950b57cec5SDimitry Andric                                         IdentifierInfo *ScopeName,
9960b57cec5SDimitry Andric                                         SourceLocation ScopeLoc,
9970b57cec5SDimitry Andric                                         ParsedAttr::Syntax Syntax) {
9980b57cec5SDimitry Andric   enum { Introduced, Deprecated, Obsoleted, Unknown };
9990b57cec5SDimitry Andric   AvailabilityChange Changes[Unknown];
10000b57cec5SDimitry Andric   ExprResult MessageExpr, ReplacementExpr;
10010b57cec5SDimitry Andric 
10020b57cec5SDimitry Andric   // Opening '('.
10030b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
10040b57cec5SDimitry Andric   if (T.consumeOpen()) {
10050b57cec5SDimitry Andric     Diag(Tok, diag::err_expected) << tok::l_paren;
10060b57cec5SDimitry Andric     return;
10070b57cec5SDimitry Andric   }
10080b57cec5SDimitry Andric 
10090b57cec5SDimitry Andric   // Parse the platform name.
10100b57cec5SDimitry Andric   if (Tok.isNot(tok::identifier)) {
10110b57cec5SDimitry Andric     Diag(Tok, diag::err_availability_expected_platform);
10120b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
10130b57cec5SDimitry Andric     return;
10140b57cec5SDimitry Andric   }
10150b57cec5SDimitry Andric   IdentifierLoc *Platform = ParseIdentifierLoc();
10160b57cec5SDimitry Andric   if (const IdentifierInfo *const Ident = Platform->Ident) {
10170b57cec5SDimitry Andric     // Canonicalize platform name from "macosx" to "macos".
10180b57cec5SDimitry Andric     if (Ident->getName() == "macosx")
10190b57cec5SDimitry Andric       Platform->Ident = PP.getIdentifierInfo("macos");
10200b57cec5SDimitry Andric     // Canonicalize platform name from "macosx_app_extension" to
10210b57cec5SDimitry Andric     // "macos_app_extension".
10220b57cec5SDimitry Andric     else if (Ident->getName() == "macosx_app_extension")
10230b57cec5SDimitry Andric       Platform->Ident = PP.getIdentifierInfo("macos_app_extension");
10240b57cec5SDimitry Andric     else
10250b57cec5SDimitry Andric       Platform->Ident = PP.getIdentifierInfo(
10260b57cec5SDimitry Andric           AvailabilityAttr::canonicalizePlatformName(Ident->getName()));
10270b57cec5SDimitry Andric   }
10280b57cec5SDimitry Andric 
10290b57cec5SDimitry Andric   // Parse the ',' following the platform name.
10300b57cec5SDimitry Andric   if (ExpectAndConsume(tok::comma)) {
10310b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
10320b57cec5SDimitry Andric     return;
10330b57cec5SDimitry Andric   }
10340b57cec5SDimitry Andric 
10350b57cec5SDimitry Andric   // If we haven't grabbed the pointers for the identifiers
10360b57cec5SDimitry Andric   // "introduced", "deprecated", and "obsoleted", do so now.
10370b57cec5SDimitry Andric   if (!Ident_introduced) {
10380b57cec5SDimitry Andric     Ident_introduced = PP.getIdentifierInfo("introduced");
10390b57cec5SDimitry Andric     Ident_deprecated = PP.getIdentifierInfo("deprecated");
10400b57cec5SDimitry Andric     Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
10410b57cec5SDimitry Andric     Ident_unavailable = PP.getIdentifierInfo("unavailable");
10420b57cec5SDimitry Andric     Ident_message = PP.getIdentifierInfo("message");
10430b57cec5SDimitry Andric     Ident_strict = PP.getIdentifierInfo("strict");
10440b57cec5SDimitry Andric     Ident_replacement = PP.getIdentifierInfo("replacement");
10450b57cec5SDimitry Andric   }
10460b57cec5SDimitry Andric 
10470b57cec5SDimitry Andric   // Parse the optional "strict", the optional "replacement" and the set of
10480b57cec5SDimitry Andric   // introductions/deprecations/removals.
10490b57cec5SDimitry Andric   SourceLocation UnavailableLoc, StrictLoc;
10500b57cec5SDimitry Andric   do {
10510b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
10520b57cec5SDimitry Andric       Diag(Tok, diag::err_availability_expected_change);
10530b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
10540b57cec5SDimitry Andric       return;
10550b57cec5SDimitry Andric     }
10560b57cec5SDimitry Andric     IdentifierInfo *Keyword = Tok.getIdentifierInfo();
10570b57cec5SDimitry Andric     SourceLocation KeywordLoc = ConsumeToken();
10580b57cec5SDimitry Andric 
10590b57cec5SDimitry Andric     if (Keyword == Ident_strict) {
10600b57cec5SDimitry Andric       if (StrictLoc.isValid()) {
10610b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_availability_redundant)
10620b57cec5SDimitry Andric           << Keyword << SourceRange(StrictLoc);
10630b57cec5SDimitry Andric       }
10640b57cec5SDimitry Andric       StrictLoc = KeywordLoc;
10650b57cec5SDimitry Andric       continue;
10660b57cec5SDimitry Andric     }
10670b57cec5SDimitry Andric 
10680b57cec5SDimitry Andric     if (Keyword == Ident_unavailable) {
10690b57cec5SDimitry Andric       if (UnavailableLoc.isValid()) {
10700b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_availability_redundant)
10710b57cec5SDimitry Andric           << Keyword << SourceRange(UnavailableLoc);
10720b57cec5SDimitry Andric       }
10730b57cec5SDimitry Andric       UnavailableLoc = KeywordLoc;
10740b57cec5SDimitry Andric       continue;
10750b57cec5SDimitry Andric     }
10760b57cec5SDimitry Andric 
10770b57cec5SDimitry Andric     if (Keyword == Ident_deprecated && Platform->Ident &&
10780b57cec5SDimitry Andric         Platform->Ident->isStr("swift")) {
10790b57cec5SDimitry Andric       // For swift, we deprecate for all versions.
10800b57cec5SDimitry Andric       if (Changes[Deprecated].KeywordLoc.isValid()) {
10810b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_availability_redundant)
10820b57cec5SDimitry Andric           << Keyword
10830b57cec5SDimitry Andric           << SourceRange(Changes[Deprecated].KeywordLoc);
10840b57cec5SDimitry Andric       }
10850b57cec5SDimitry Andric 
10860b57cec5SDimitry Andric       Changes[Deprecated].KeywordLoc = KeywordLoc;
10870b57cec5SDimitry Andric       // Use a fake version here.
10880b57cec5SDimitry Andric       Changes[Deprecated].Version = VersionTuple(1);
10890b57cec5SDimitry Andric       continue;
10900b57cec5SDimitry Andric     }
10910b57cec5SDimitry Andric 
10920b57cec5SDimitry Andric     if (Tok.isNot(tok::equal)) {
10930b57cec5SDimitry Andric       Diag(Tok, diag::err_expected_after) << Keyword << tok::equal;
10940b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
10950b57cec5SDimitry Andric       return;
10960b57cec5SDimitry Andric     }
10970b57cec5SDimitry Andric     ConsumeToken();
10980b57cec5SDimitry Andric     if (Keyword == Ident_message || Keyword == Ident_replacement) {
10990b57cec5SDimitry Andric       if (Tok.isNot(tok::string_literal)) {
11000b57cec5SDimitry Andric         Diag(Tok, diag::err_expected_string_literal)
11010b57cec5SDimitry Andric           << /*Source='availability attribute'*/2;
11020b57cec5SDimitry Andric         SkipUntil(tok::r_paren, StopAtSemi);
11030b57cec5SDimitry Andric         return;
11040b57cec5SDimitry Andric       }
11050b57cec5SDimitry Andric       if (Keyword == Ident_message)
11060b57cec5SDimitry Andric         MessageExpr = ParseStringLiteralExpression();
11070b57cec5SDimitry Andric       else
11080b57cec5SDimitry Andric         ReplacementExpr = ParseStringLiteralExpression();
11090b57cec5SDimitry Andric       // Also reject wide string literals.
11100b57cec5SDimitry Andric       if (StringLiteral *MessageStringLiteral =
11110b57cec5SDimitry Andric               cast_or_null<StringLiteral>(MessageExpr.get())) {
11120b57cec5SDimitry Andric         if (MessageStringLiteral->getCharByteWidth() != 1) {
11130b57cec5SDimitry Andric           Diag(MessageStringLiteral->getSourceRange().getBegin(),
11140b57cec5SDimitry Andric                diag::err_expected_string_literal)
11150b57cec5SDimitry Andric             << /*Source='availability attribute'*/ 2;
11160b57cec5SDimitry Andric           SkipUntil(tok::r_paren, StopAtSemi);
11170b57cec5SDimitry Andric           return;
11180b57cec5SDimitry Andric         }
11190b57cec5SDimitry Andric       }
11200b57cec5SDimitry Andric       if (Keyword == Ident_message)
11210b57cec5SDimitry Andric         break;
11220b57cec5SDimitry Andric       else
11230b57cec5SDimitry Andric         continue;
11240b57cec5SDimitry Andric     }
11250b57cec5SDimitry Andric 
11260b57cec5SDimitry Andric     // Special handling of 'NA' only when applied to introduced or
11270b57cec5SDimitry Andric     // deprecated.
11280b57cec5SDimitry Andric     if ((Keyword == Ident_introduced || Keyword == Ident_deprecated) &&
11290b57cec5SDimitry Andric         Tok.is(tok::identifier)) {
11300b57cec5SDimitry Andric       IdentifierInfo *NA = Tok.getIdentifierInfo();
11310b57cec5SDimitry Andric       if (NA->getName() == "NA") {
11320b57cec5SDimitry Andric         ConsumeToken();
11330b57cec5SDimitry Andric         if (Keyword == Ident_introduced)
11340b57cec5SDimitry Andric           UnavailableLoc = KeywordLoc;
11350b57cec5SDimitry Andric         continue;
11360b57cec5SDimitry Andric       }
11370b57cec5SDimitry Andric     }
11380b57cec5SDimitry Andric 
11390b57cec5SDimitry Andric     SourceRange VersionRange;
11400b57cec5SDimitry Andric     VersionTuple Version = ParseVersionTuple(VersionRange);
11410b57cec5SDimitry Andric 
11420b57cec5SDimitry Andric     if (Version.empty()) {
11430b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
11440b57cec5SDimitry Andric       return;
11450b57cec5SDimitry Andric     }
11460b57cec5SDimitry Andric 
11470b57cec5SDimitry Andric     unsigned Index;
11480b57cec5SDimitry Andric     if (Keyword == Ident_introduced)
11490b57cec5SDimitry Andric       Index = Introduced;
11500b57cec5SDimitry Andric     else if (Keyword == Ident_deprecated)
11510b57cec5SDimitry Andric       Index = Deprecated;
11520b57cec5SDimitry Andric     else if (Keyword == Ident_obsoleted)
11530b57cec5SDimitry Andric       Index = Obsoleted;
11540b57cec5SDimitry Andric     else
11550b57cec5SDimitry Andric       Index = Unknown;
11560b57cec5SDimitry Andric 
11570b57cec5SDimitry Andric     if (Index < Unknown) {
11580b57cec5SDimitry Andric       if (!Changes[Index].KeywordLoc.isInvalid()) {
11590b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_availability_redundant)
11600b57cec5SDimitry Andric           << Keyword
11610b57cec5SDimitry Andric           << SourceRange(Changes[Index].KeywordLoc,
11620b57cec5SDimitry Andric                          Changes[Index].VersionRange.getEnd());
11630b57cec5SDimitry Andric       }
11640b57cec5SDimitry Andric 
11650b57cec5SDimitry Andric       Changes[Index].KeywordLoc = KeywordLoc;
11660b57cec5SDimitry Andric       Changes[Index].Version = Version;
11670b57cec5SDimitry Andric       Changes[Index].VersionRange = VersionRange;
11680b57cec5SDimitry Andric     } else {
11690b57cec5SDimitry Andric       Diag(KeywordLoc, diag::err_availability_unknown_change)
11700b57cec5SDimitry Andric         << Keyword << VersionRange;
11710b57cec5SDimitry Andric     }
11720b57cec5SDimitry Andric 
11730b57cec5SDimitry Andric   } while (TryConsumeToken(tok::comma));
11740b57cec5SDimitry Andric 
11750b57cec5SDimitry Andric   // Closing ')'.
11760b57cec5SDimitry Andric   if (T.consumeClose())
11770b57cec5SDimitry Andric     return;
11780b57cec5SDimitry Andric 
11790b57cec5SDimitry Andric   if (endLoc)
11800b57cec5SDimitry Andric     *endLoc = T.getCloseLocation();
11810b57cec5SDimitry Andric 
11820b57cec5SDimitry Andric   // The 'unavailable' availability cannot be combined with any other
11830b57cec5SDimitry Andric   // availability changes. Make sure that hasn't happened.
11840b57cec5SDimitry Andric   if (UnavailableLoc.isValid()) {
11850b57cec5SDimitry Andric     bool Complained = false;
11860b57cec5SDimitry Andric     for (unsigned Index = Introduced; Index != Unknown; ++Index) {
11870b57cec5SDimitry Andric       if (Changes[Index].KeywordLoc.isValid()) {
11880b57cec5SDimitry Andric         if (!Complained) {
11890b57cec5SDimitry Andric           Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
11900b57cec5SDimitry Andric             << SourceRange(Changes[Index].KeywordLoc,
11910b57cec5SDimitry Andric                            Changes[Index].VersionRange.getEnd());
11920b57cec5SDimitry Andric           Complained = true;
11930b57cec5SDimitry Andric         }
11940b57cec5SDimitry Andric 
11950b57cec5SDimitry Andric         // Clear out the availability.
11960b57cec5SDimitry Andric         Changes[Index] = AvailabilityChange();
11970b57cec5SDimitry Andric       }
11980b57cec5SDimitry Andric     }
11990b57cec5SDimitry Andric   }
12000b57cec5SDimitry Andric 
12010b57cec5SDimitry Andric   // Record this attribute
12020b57cec5SDimitry Andric   attrs.addNew(&Availability,
12030b57cec5SDimitry Andric                SourceRange(AvailabilityLoc, T.getCloseLocation()),
12040b57cec5SDimitry Andric                ScopeName, ScopeLoc,
12050b57cec5SDimitry Andric                Platform,
12060b57cec5SDimitry Andric                Changes[Introduced],
12070b57cec5SDimitry Andric                Changes[Deprecated],
12080b57cec5SDimitry Andric                Changes[Obsoleted],
12090b57cec5SDimitry Andric                UnavailableLoc, MessageExpr.get(),
12100b57cec5SDimitry Andric                Syntax, StrictLoc, ReplacementExpr.get());
12110b57cec5SDimitry Andric }
12120b57cec5SDimitry Andric 
12130b57cec5SDimitry Andric /// Parse the contents of the "external_source_symbol" attribute.
12140b57cec5SDimitry Andric ///
12150b57cec5SDimitry Andric /// external-source-symbol-attribute:
12160b57cec5SDimitry Andric ///   'external_source_symbol' '(' keyword-arg-list ')'
12170b57cec5SDimitry Andric ///
12180b57cec5SDimitry Andric /// keyword-arg-list:
12190b57cec5SDimitry Andric ///   keyword-arg
12200b57cec5SDimitry Andric ///   keyword-arg ',' keyword-arg-list
12210b57cec5SDimitry Andric ///
12220b57cec5SDimitry Andric /// keyword-arg:
12230b57cec5SDimitry Andric ///   'language' '=' <string>
12240b57cec5SDimitry Andric ///   'defined_in' '=' <string>
12250b57cec5SDimitry Andric ///   'generated_declaration'
12260b57cec5SDimitry Andric void Parser::ParseExternalSourceSymbolAttribute(
12270b57cec5SDimitry Andric     IdentifierInfo &ExternalSourceSymbol, SourceLocation Loc,
12280b57cec5SDimitry Andric     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
12290b57cec5SDimitry Andric     SourceLocation ScopeLoc, ParsedAttr::Syntax Syntax) {
12300b57cec5SDimitry Andric   // Opening '('.
12310b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
12320b57cec5SDimitry Andric   if (T.expectAndConsume())
12330b57cec5SDimitry Andric     return;
12340b57cec5SDimitry Andric 
12350b57cec5SDimitry Andric   // Initialize the pointers for the keyword identifiers when required.
12360b57cec5SDimitry Andric   if (!Ident_language) {
12370b57cec5SDimitry Andric     Ident_language = PP.getIdentifierInfo("language");
12380b57cec5SDimitry Andric     Ident_defined_in = PP.getIdentifierInfo("defined_in");
12390b57cec5SDimitry Andric     Ident_generated_declaration = PP.getIdentifierInfo("generated_declaration");
12400b57cec5SDimitry Andric   }
12410b57cec5SDimitry Andric 
12420b57cec5SDimitry Andric   ExprResult Language;
12430b57cec5SDimitry Andric   bool HasLanguage = false;
12440b57cec5SDimitry Andric   ExprResult DefinedInExpr;
12450b57cec5SDimitry Andric   bool HasDefinedIn = false;
12460b57cec5SDimitry Andric   IdentifierLoc *GeneratedDeclaration = nullptr;
12470b57cec5SDimitry Andric 
12480b57cec5SDimitry Andric   // Parse the language/defined_in/generated_declaration keywords
12490b57cec5SDimitry Andric   do {
12500b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
12510b57cec5SDimitry Andric       Diag(Tok, diag::err_external_source_symbol_expected_keyword);
12520b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
12530b57cec5SDimitry Andric       return;
12540b57cec5SDimitry Andric     }
12550b57cec5SDimitry Andric 
12560b57cec5SDimitry Andric     SourceLocation KeywordLoc = Tok.getLocation();
12570b57cec5SDimitry Andric     IdentifierInfo *Keyword = Tok.getIdentifierInfo();
12580b57cec5SDimitry Andric     if (Keyword == Ident_generated_declaration) {
12590b57cec5SDimitry Andric       if (GeneratedDeclaration) {
12600b57cec5SDimitry Andric         Diag(Tok, diag::err_external_source_symbol_duplicate_clause) << Keyword;
12610b57cec5SDimitry Andric         SkipUntil(tok::r_paren, StopAtSemi);
12620b57cec5SDimitry Andric         return;
12630b57cec5SDimitry Andric       }
12640b57cec5SDimitry Andric       GeneratedDeclaration = ParseIdentifierLoc();
12650b57cec5SDimitry Andric       continue;
12660b57cec5SDimitry Andric     }
12670b57cec5SDimitry Andric 
12680b57cec5SDimitry Andric     if (Keyword != Ident_language && Keyword != Ident_defined_in) {
12690b57cec5SDimitry Andric       Diag(Tok, diag::err_external_source_symbol_expected_keyword);
12700b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
12710b57cec5SDimitry Andric       return;
12720b57cec5SDimitry Andric     }
12730b57cec5SDimitry Andric 
12740b57cec5SDimitry Andric     ConsumeToken();
12750b57cec5SDimitry Andric     if (ExpectAndConsume(tok::equal, diag::err_expected_after,
12760b57cec5SDimitry Andric                          Keyword->getName())) {
12770b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
12780b57cec5SDimitry Andric       return;
12790b57cec5SDimitry Andric     }
12800b57cec5SDimitry Andric 
12810b57cec5SDimitry Andric     bool HadLanguage = HasLanguage, HadDefinedIn = HasDefinedIn;
12820b57cec5SDimitry Andric     if (Keyword == Ident_language)
12830b57cec5SDimitry Andric       HasLanguage = true;
12840b57cec5SDimitry Andric     else
12850b57cec5SDimitry Andric       HasDefinedIn = true;
12860b57cec5SDimitry Andric 
12870b57cec5SDimitry Andric     if (Tok.isNot(tok::string_literal)) {
12880b57cec5SDimitry Andric       Diag(Tok, diag::err_expected_string_literal)
12890b57cec5SDimitry Andric           << /*Source='external_source_symbol attribute'*/ 3
12900b57cec5SDimitry Andric           << /*language | source container*/ (Keyword != Ident_language);
12910b57cec5SDimitry Andric       SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
12920b57cec5SDimitry Andric       continue;
12930b57cec5SDimitry Andric     }
12940b57cec5SDimitry Andric     if (Keyword == Ident_language) {
12950b57cec5SDimitry Andric       if (HadLanguage) {
12960b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_external_source_symbol_duplicate_clause)
12970b57cec5SDimitry Andric             << Keyword;
12980b57cec5SDimitry Andric         ParseStringLiteralExpression();
12990b57cec5SDimitry Andric         continue;
13000b57cec5SDimitry Andric       }
13010b57cec5SDimitry Andric       Language = ParseStringLiteralExpression();
13020b57cec5SDimitry Andric     } else {
13030b57cec5SDimitry Andric       assert(Keyword == Ident_defined_in && "Invalid clause keyword!");
13040b57cec5SDimitry Andric       if (HadDefinedIn) {
13050b57cec5SDimitry Andric         Diag(KeywordLoc, diag::err_external_source_symbol_duplicate_clause)
13060b57cec5SDimitry Andric             << Keyword;
13070b57cec5SDimitry Andric         ParseStringLiteralExpression();
13080b57cec5SDimitry Andric         continue;
13090b57cec5SDimitry Andric       }
13100b57cec5SDimitry Andric       DefinedInExpr = ParseStringLiteralExpression();
13110b57cec5SDimitry Andric     }
13120b57cec5SDimitry Andric   } while (TryConsumeToken(tok::comma));
13130b57cec5SDimitry Andric 
13140b57cec5SDimitry Andric   // Closing ')'.
13150b57cec5SDimitry Andric   if (T.consumeClose())
13160b57cec5SDimitry Andric     return;
13170b57cec5SDimitry Andric   if (EndLoc)
13180b57cec5SDimitry Andric     *EndLoc = T.getCloseLocation();
13190b57cec5SDimitry Andric 
13200b57cec5SDimitry Andric   ArgsUnion Args[] = {Language.get(), DefinedInExpr.get(),
13210b57cec5SDimitry Andric                       GeneratedDeclaration};
13220b57cec5SDimitry Andric   Attrs.addNew(&ExternalSourceSymbol, SourceRange(Loc, T.getCloseLocation()),
13230b57cec5SDimitry Andric                ScopeName, ScopeLoc, Args, llvm::array_lengthof(Args), Syntax);
13240b57cec5SDimitry Andric }
13250b57cec5SDimitry Andric 
13260b57cec5SDimitry Andric /// Parse the contents of the "objc_bridge_related" attribute.
13270b57cec5SDimitry Andric /// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')'
13280b57cec5SDimitry Andric /// related_class:
13290b57cec5SDimitry Andric ///     Identifier
13300b57cec5SDimitry Andric ///
13310b57cec5SDimitry Andric /// opt-class_method:
13320b57cec5SDimitry Andric ///     Identifier: | <empty>
13330b57cec5SDimitry Andric ///
13340b57cec5SDimitry Andric /// opt-instance_method:
13350b57cec5SDimitry Andric ///     Identifier | <empty>
13360b57cec5SDimitry Andric ///
13370b57cec5SDimitry Andric void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated,
13380b57cec5SDimitry Andric                                 SourceLocation ObjCBridgeRelatedLoc,
13390b57cec5SDimitry Andric                                 ParsedAttributes &attrs,
13400b57cec5SDimitry Andric                                 SourceLocation *endLoc,
13410b57cec5SDimitry Andric                                 IdentifierInfo *ScopeName,
13420b57cec5SDimitry Andric                                 SourceLocation ScopeLoc,
13430b57cec5SDimitry Andric                                 ParsedAttr::Syntax Syntax) {
13440b57cec5SDimitry Andric   // Opening '('.
13450b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
13460b57cec5SDimitry Andric   if (T.consumeOpen()) {
13470b57cec5SDimitry Andric     Diag(Tok, diag::err_expected) << tok::l_paren;
13480b57cec5SDimitry Andric     return;
13490b57cec5SDimitry Andric   }
13500b57cec5SDimitry Andric 
13510b57cec5SDimitry Andric   // Parse the related class name.
13520b57cec5SDimitry Andric   if (Tok.isNot(tok::identifier)) {
13530b57cec5SDimitry Andric     Diag(Tok, diag::err_objcbridge_related_expected_related_class);
13540b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
13550b57cec5SDimitry Andric     return;
13560b57cec5SDimitry Andric   }
13570b57cec5SDimitry Andric   IdentifierLoc *RelatedClass = ParseIdentifierLoc();
13580b57cec5SDimitry Andric   if (ExpectAndConsume(tok::comma)) {
13590b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
13600b57cec5SDimitry Andric     return;
13610b57cec5SDimitry Andric   }
13620b57cec5SDimitry Andric 
13630b57cec5SDimitry Andric   // Parse class method name.  It's non-optional in the sense that a trailing
13640b57cec5SDimitry Andric   // comma is required, but it can be the empty string, and then we record a
13650b57cec5SDimitry Andric   // nullptr.
13660b57cec5SDimitry Andric   IdentifierLoc *ClassMethod = nullptr;
13670b57cec5SDimitry Andric   if (Tok.is(tok::identifier)) {
13680b57cec5SDimitry Andric     ClassMethod = ParseIdentifierLoc();
13690b57cec5SDimitry Andric     if (!TryConsumeToken(tok::colon)) {
13700b57cec5SDimitry Andric       Diag(Tok, diag::err_objcbridge_related_selector_name);
13710b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
13720b57cec5SDimitry Andric       return;
13730b57cec5SDimitry Andric     }
13740b57cec5SDimitry Andric   }
13750b57cec5SDimitry Andric   if (!TryConsumeToken(tok::comma)) {
13760b57cec5SDimitry Andric     if (Tok.is(tok::colon))
13770b57cec5SDimitry Andric       Diag(Tok, diag::err_objcbridge_related_selector_name);
13780b57cec5SDimitry Andric     else
13790b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::comma;
13800b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
13810b57cec5SDimitry Andric     return;
13820b57cec5SDimitry Andric   }
13830b57cec5SDimitry Andric 
13840b57cec5SDimitry Andric   // Parse instance method name.  Also non-optional but empty string is
13850b57cec5SDimitry Andric   // permitted.
13860b57cec5SDimitry Andric   IdentifierLoc *InstanceMethod = nullptr;
13870b57cec5SDimitry Andric   if (Tok.is(tok::identifier))
13880b57cec5SDimitry Andric     InstanceMethod = ParseIdentifierLoc();
13890b57cec5SDimitry Andric   else if (Tok.isNot(tok::r_paren)) {
13900b57cec5SDimitry Andric     Diag(Tok, diag::err_expected) << tok::r_paren;
13910b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
13920b57cec5SDimitry Andric     return;
13930b57cec5SDimitry Andric   }
13940b57cec5SDimitry Andric 
13950b57cec5SDimitry Andric   // Closing ')'.
13960b57cec5SDimitry Andric   if (T.consumeClose())
13970b57cec5SDimitry Andric     return;
13980b57cec5SDimitry Andric 
13990b57cec5SDimitry Andric   if (endLoc)
14000b57cec5SDimitry Andric     *endLoc = T.getCloseLocation();
14010b57cec5SDimitry Andric 
14020b57cec5SDimitry Andric   // Record this attribute
14030b57cec5SDimitry Andric   attrs.addNew(&ObjCBridgeRelated,
14040b57cec5SDimitry Andric                SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()),
14050b57cec5SDimitry Andric                ScopeName, ScopeLoc,
14060b57cec5SDimitry Andric                RelatedClass,
14070b57cec5SDimitry Andric                ClassMethod,
14080b57cec5SDimitry Andric                InstanceMethod,
14090b57cec5SDimitry Andric                Syntax);
14100b57cec5SDimitry Andric }
14110b57cec5SDimitry Andric 
14120b57cec5SDimitry Andric void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
14130b57cec5SDimitry Andric                                               SourceLocation AttrNameLoc,
14140b57cec5SDimitry Andric                                               ParsedAttributes &Attrs,
14150b57cec5SDimitry Andric                                               SourceLocation *EndLoc,
14160b57cec5SDimitry Andric                                               IdentifierInfo *ScopeName,
14170b57cec5SDimitry Andric                                               SourceLocation ScopeLoc,
14180b57cec5SDimitry Andric                                               ParsedAttr::Syntax Syntax) {
14190b57cec5SDimitry Andric   assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
14200b57cec5SDimitry Andric 
14210b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
14220b57cec5SDimitry Andric   T.consumeOpen();
14230b57cec5SDimitry Andric 
14240b57cec5SDimitry Andric   if (Tok.isNot(tok::identifier)) {
14250b57cec5SDimitry Andric     Diag(Tok, diag::err_expected) << tok::identifier;
14260b57cec5SDimitry Andric     T.skipToEnd();
14270b57cec5SDimitry Andric     return;
14280b57cec5SDimitry Andric   }
14290b57cec5SDimitry Andric   IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
14300b57cec5SDimitry Andric 
14310b57cec5SDimitry Andric   if (ExpectAndConsume(tok::comma)) {
14320b57cec5SDimitry Andric     T.skipToEnd();
14330b57cec5SDimitry Andric     return;
14340b57cec5SDimitry Andric   }
14350b57cec5SDimitry Andric 
14360b57cec5SDimitry Andric   SourceRange MatchingCTypeRange;
14370b57cec5SDimitry Andric   TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
14380b57cec5SDimitry Andric   if (MatchingCType.isInvalid()) {
14390b57cec5SDimitry Andric     T.skipToEnd();
14400b57cec5SDimitry Andric     return;
14410b57cec5SDimitry Andric   }
14420b57cec5SDimitry Andric 
14430b57cec5SDimitry Andric   bool LayoutCompatible = false;
14440b57cec5SDimitry Andric   bool MustBeNull = false;
14450b57cec5SDimitry Andric   while (TryConsumeToken(tok::comma)) {
14460b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
14470b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::identifier;
14480b57cec5SDimitry Andric       T.skipToEnd();
14490b57cec5SDimitry Andric       return;
14500b57cec5SDimitry Andric     }
14510b57cec5SDimitry Andric     IdentifierInfo *Flag = Tok.getIdentifierInfo();
14520b57cec5SDimitry Andric     if (Flag->isStr("layout_compatible"))
14530b57cec5SDimitry Andric       LayoutCompatible = true;
14540b57cec5SDimitry Andric     else if (Flag->isStr("must_be_null"))
14550b57cec5SDimitry Andric       MustBeNull = true;
14560b57cec5SDimitry Andric     else {
14570b57cec5SDimitry Andric       Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
14580b57cec5SDimitry Andric       T.skipToEnd();
14590b57cec5SDimitry Andric       return;
14600b57cec5SDimitry Andric     }
14610b57cec5SDimitry Andric     ConsumeToken(); // consume flag
14620b57cec5SDimitry Andric   }
14630b57cec5SDimitry Andric 
14640b57cec5SDimitry Andric   if (!T.consumeClose()) {
14650b57cec5SDimitry Andric     Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, ScopeName, ScopeLoc,
14660b57cec5SDimitry Andric                                    ArgumentKind, MatchingCType.get(),
14670b57cec5SDimitry Andric                                    LayoutCompatible, MustBeNull, Syntax);
14680b57cec5SDimitry Andric   }
14690b57cec5SDimitry Andric 
14700b57cec5SDimitry Andric   if (EndLoc)
14710b57cec5SDimitry Andric     *EndLoc = T.getCloseLocation();
14720b57cec5SDimitry Andric }
14730b57cec5SDimitry Andric 
14740b57cec5SDimitry Andric /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
14750b57cec5SDimitry Andric /// of a C++11 attribute-specifier in a location where an attribute is not
14760b57cec5SDimitry Andric /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
14770b57cec5SDimitry Andric /// situation.
14780b57cec5SDimitry Andric ///
14790b57cec5SDimitry Andric /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
14800b57cec5SDimitry Andric /// this doesn't appear to actually be an attribute-specifier, and the caller
14810b57cec5SDimitry Andric /// should try to parse it.
14820b57cec5SDimitry Andric bool Parser::DiagnoseProhibitedCXX11Attribute() {
14830b57cec5SDimitry Andric   assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
14840b57cec5SDimitry Andric 
14850b57cec5SDimitry Andric   switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
14860b57cec5SDimitry Andric   case CAK_NotAttributeSpecifier:
14870b57cec5SDimitry Andric     // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
14880b57cec5SDimitry Andric     return false;
14890b57cec5SDimitry Andric 
14900b57cec5SDimitry Andric   case CAK_InvalidAttributeSpecifier:
14910b57cec5SDimitry Andric     Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
14920b57cec5SDimitry Andric     return false;
14930b57cec5SDimitry Andric 
14940b57cec5SDimitry Andric   case CAK_AttributeSpecifier:
14950b57cec5SDimitry Andric     // Parse and discard the attributes.
14960b57cec5SDimitry Andric     SourceLocation BeginLoc = ConsumeBracket();
14970b57cec5SDimitry Andric     ConsumeBracket();
14980b57cec5SDimitry Andric     SkipUntil(tok::r_square);
14990b57cec5SDimitry Andric     assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
15000b57cec5SDimitry Andric     SourceLocation EndLoc = ConsumeBracket();
15010b57cec5SDimitry Andric     Diag(BeginLoc, diag::err_attributes_not_allowed)
15020b57cec5SDimitry Andric       << SourceRange(BeginLoc, EndLoc);
15030b57cec5SDimitry Andric     return true;
15040b57cec5SDimitry Andric   }
15050b57cec5SDimitry Andric   llvm_unreachable("All cases handled above.");
15060b57cec5SDimitry Andric }
15070b57cec5SDimitry Andric 
15080b57cec5SDimitry Andric /// We have found the opening square brackets of a C++11
15090b57cec5SDimitry Andric /// attribute-specifier in a location where an attribute is not permitted, but
15100b57cec5SDimitry Andric /// we know where the attributes ought to be written. Parse them anyway, and
15110b57cec5SDimitry Andric /// provide a fixit moving them to the right place.
15120b57cec5SDimitry Andric void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
15130b57cec5SDimitry Andric                                              SourceLocation CorrectLocation) {
15140b57cec5SDimitry Andric   assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
15150b57cec5SDimitry Andric          Tok.is(tok::kw_alignas));
15160b57cec5SDimitry Andric 
15170b57cec5SDimitry Andric   // Consume the attributes.
15180b57cec5SDimitry Andric   SourceLocation Loc = Tok.getLocation();
15190b57cec5SDimitry Andric   ParseCXX11Attributes(Attrs);
15200b57cec5SDimitry Andric   CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
15210b57cec5SDimitry Andric   // FIXME: use err_attributes_misplaced
15220b57cec5SDimitry Andric   Diag(Loc, diag::err_attributes_not_allowed)
15230b57cec5SDimitry Andric     << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
15240b57cec5SDimitry Andric     << FixItHint::CreateRemoval(AttrRange);
15250b57cec5SDimitry Andric }
15260b57cec5SDimitry Andric 
15270b57cec5SDimitry Andric void Parser::DiagnoseProhibitedAttributes(
15280b57cec5SDimitry Andric     const SourceRange &Range, const SourceLocation CorrectLocation) {
15290b57cec5SDimitry Andric   if (CorrectLocation.isValid()) {
15300b57cec5SDimitry Andric     CharSourceRange AttrRange(Range, true);
15310b57cec5SDimitry Andric     Diag(CorrectLocation, diag::err_attributes_misplaced)
15320b57cec5SDimitry Andric         << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
15330b57cec5SDimitry Andric         << FixItHint::CreateRemoval(AttrRange);
15340b57cec5SDimitry Andric   } else
15350b57cec5SDimitry Andric     Diag(Range.getBegin(), diag::err_attributes_not_allowed) << Range;
15360b57cec5SDimitry Andric }
15370b57cec5SDimitry Andric 
15380b57cec5SDimitry Andric void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &Attrs,
15390b57cec5SDimitry Andric                                      unsigned DiagID) {
15400b57cec5SDimitry Andric   for (const ParsedAttr &AL : Attrs) {
15410b57cec5SDimitry Andric     if (!AL.isCXX11Attribute() && !AL.isC2xAttribute())
15420b57cec5SDimitry Andric       continue;
15430b57cec5SDimitry Andric     if (AL.getKind() == ParsedAttr::UnknownAttribute)
1544a7dea167SDimitry Andric       Diag(AL.getLoc(), diag::warn_unknown_attribute_ignored) << AL;
15450b57cec5SDimitry Andric     else {
1546a7dea167SDimitry Andric       Diag(AL.getLoc(), DiagID) << AL;
15470b57cec5SDimitry Andric       AL.setInvalid();
15480b57cec5SDimitry Andric     }
15490b57cec5SDimitry Andric   }
15500b57cec5SDimitry Andric }
15510b57cec5SDimitry Andric 
15520b57cec5SDimitry Andric // Usually, `__attribute__((attrib)) class Foo {} var` means that attribute
15530b57cec5SDimitry Andric // applies to var, not the type Foo.
15540b57cec5SDimitry Andric // As an exception to the rule, __declspec(align(...)) before the
15550b57cec5SDimitry Andric // class-key affects the type instead of the variable.
15560b57cec5SDimitry Andric // Also, Microsoft-style [attributes] seem to affect the type instead of the
15570b57cec5SDimitry Andric // variable.
15580b57cec5SDimitry Andric // This function moves attributes that should apply to the type off DS to Attrs.
15590b57cec5SDimitry Andric void Parser::stripTypeAttributesOffDeclSpec(ParsedAttributesWithRange &Attrs,
15600b57cec5SDimitry Andric                                             DeclSpec &DS,
15610b57cec5SDimitry Andric                                             Sema::TagUseKind TUK) {
15620b57cec5SDimitry Andric   if (TUK == Sema::TUK_Reference)
15630b57cec5SDimitry Andric     return;
15640b57cec5SDimitry Andric 
15650b57cec5SDimitry Andric   llvm::SmallVector<ParsedAttr *, 1> ToBeMoved;
15660b57cec5SDimitry Andric 
15670b57cec5SDimitry Andric   for (ParsedAttr &AL : DS.getAttributes()) {
15680b57cec5SDimitry Andric     if ((AL.getKind() == ParsedAttr::AT_Aligned &&
15690b57cec5SDimitry Andric          AL.isDeclspecAttribute()) ||
15700b57cec5SDimitry Andric         AL.isMicrosoftAttribute())
15710b57cec5SDimitry Andric       ToBeMoved.push_back(&AL);
15720b57cec5SDimitry Andric   }
15730b57cec5SDimitry Andric 
15740b57cec5SDimitry Andric   for (ParsedAttr *AL : ToBeMoved) {
15750b57cec5SDimitry Andric     DS.getAttributes().remove(AL);
15760b57cec5SDimitry Andric     Attrs.addAtEnd(AL);
15770b57cec5SDimitry Andric   }
15780b57cec5SDimitry Andric }
15790b57cec5SDimitry Andric 
15800b57cec5SDimitry Andric /// ParseDeclaration - Parse a full 'declaration', which consists of
15810b57cec5SDimitry Andric /// declaration-specifiers, some number of declarators, and a semicolon.
15820b57cec5SDimitry Andric /// 'Context' should be a DeclaratorContext value.  This returns the
15830b57cec5SDimitry Andric /// location of the semicolon in DeclEnd.
15840b57cec5SDimitry Andric ///
15850b57cec5SDimitry Andric ///       declaration: [C99 6.7]
15860b57cec5SDimitry Andric ///         block-declaration ->
15870b57cec5SDimitry Andric ///           simple-declaration
15880b57cec5SDimitry Andric ///           others                   [FIXME]
15890b57cec5SDimitry Andric /// [C++]   template-declaration
15900b57cec5SDimitry Andric /// [C++]   namespace-definition
15910b57cec5SDimitry Andric /// [C++]   using-directive
15920b57cec5SDimitry Andric /// [C++]   using-declaration
15930b57cec5SDimitry Andric /// [C++11/C11] static_assert-declaration
15940b57cec5SDimitry Andric ///         others... [FIXME]
15950b57cec5SDimitry Andric ///
1596a7dea167SDimitry Andric Parser::DeclGroupPtrTy
1597a7dea167SDimitry Andric Parser::ParseDeclaration(DeclaratorContext Context, SourceLocation &DeclEnd,
1598a7dea167SDimitry Andric                          ParsedAttributesWithRange &attrs,
1599a7dea167SDimitry Andric                          SourceLocation *DeclSpecStart) {
16000b57cec5SDimitry Andric   ParenBraceBracketBalancer BalancerRAIIObj(*this);
16010b57cec5SDimitry Andric   // Must temporarily exit the objective-c container scope for
16020b57cec5SDimitry Andric   // parsing c none objective-c decls.
16030b57cec5SDimitry Andric   ObjCDeclContextSwitch ObjCDC(*this);
16040b57cec5SDimitry Andric 
16050b57cec5SDimitry Andric   Decl *SingleDecl = nullptr;
16060b57cec5SDimitry Andric   switch (Tok.getKind()) {
16070b57cec5SDimitry Andric   case tok::kw_template:
16080b57cec5SDimitry Andric   case tok::kw_export:
16090b57cec5SDimitry Andric     ProhibitAttributes(attrs);
16100b57cec5SDimitry Andric     SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd, attrs);
16110b57cec5SDimitry Andric     break;
16120b57cec5SDimitry Andric   case tok::kw_inline:
16130b57cec5SDimitry Andric     // Could be the start of an inline namespace. Allowed as an ext in C++03.
16140b57cec5SDimitry Andric     if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
16150b57cec5SDimitry Andric       ProhibitAttributes(attrs);
16160b57cec5SDimitry Andric       SourceLocation InlineLoc = ConsumeToken();
16170b57cec5SDimitry Andric       return ParseNamespace(Context, DeclEnd, InlineLoc);
16180b57cec5SDimitry Andric     }
1619a7dea167SDimitry Andric     return ParseSimpleDeclaration(Context, DeclEnd, attrs, true, nullptr,
1620a7dea167SDimitry Andric                                   DeclSpecStart);
16210b57cec5SDimitry Andric   case tok::kw_namespace:
16220b57cec5SDimitry Andric     ProhibitAttributes(attrs);
16230b57cec5SDimitry Andric     return ParseNamespace(Context, DeclEnd);
16240b57cec5SDimitry Andric   case tok::kw_using:
16250b57cec5SDimitry Andric     return ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
16260b57cec5SDimitry Andric                                             DeclEnd, attrs);
16270b57cec5SDimitry Andric   case tok::kw_static_assert:
16280b57cec5SDimitry Andric   case tok::kw__Static_assert:
16290b57cec5SDimitry Andric     ProhibitAttributes(attrs);
16300b57cec5SDimitry Andric     SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
16310b57cec5SDimitry Andric     break;
16320b57cec5SDimitry Andric   default:
1633a7dea167SDimitry Andric     return ParseSimpleDeclaration(Context, DeclEnd, attrs, true, nullptr,
1634a7dea167SDimitry Andric                                   DeclSpecStart);
16350b57cec5SDimitry Andric   }
16360b57cec5SDimitry Andric 
16370b57cec5SDimitry Andric   // This routine returns a DeclGroup, if the thing we parsed only contains a
16380b57cec5SDimitry Andric   // single decl, convert it now.
16390b57cec5SDimitry Andric   return Actions.ConvertDeclToDeclGroup(SingleDecl);
16400b57cec5SDimitry Andric }
16410b57cec5SDimitry Andric 
16420b57cec5SDimitry Andric ///       simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
16430b57cec5SDimitry Andric ///         declaration-specifiers init-declarator-list[opt] ';'
16440b57cec5SDimitry Andric /// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
16450b57cec5SDimitry Andric ///             init-declarator-list ';'
16460b57cec5SDimitry Andric ///[C90/C++]init-declarator-list ';'                             [TODO]
16470b57cec5SDimitry Andric /// [OMP]   threadprivate-directive
16480b57cec5SDimitry Andric /// [OMP]   allocate-directive                                   [TODO]
16490b57cec5SDimitry Andric ///
16500b57cec5SDimitry Andric ///       for-range-declaration: [C++11 6.5p1: stmt.ranged]
16510b57cec5SDimitry Andric ///         attribute-specifier-seq[opt] type-specifier-seq declarator
16520b57cec5SDimitry Andric ///
16530b57cec5SDimitry Andric /// If RequireSemi is false, this does not check for a ';' at the end of the
16540b57cec5SDimitry Andric /// declaration.  If it is true, it checks for and eats it.
16550b57cec5SDimitry Andric ///
16560b57cec5SDimitry Andric /// If FRI is non-null, we might be parsing a for-range-declaration instead
16570b57cec5SDimitry Andric /// of a simple-declaration. If we find that we are, we also parse the
16580b57cec5SDimitry Andric /// for-range-initializer, and place it here.
1659a7dea167SDimitry Andric ///
1660a7dea167SDimitry Andric /// DeclSpecStart is used when decl-specifiers are parsed before parsing
1661a7dea167SDimitry Andric /// the Declaration. The SourceLocation for this Decl is set to
1662a7dea167SDimitry Andric /// DeclSpecStart if DeclSpecStart is non-null.
1663a7dea167SDimitry Andric Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(
1664a7dea167SDimitry Andric     DeclaratorContext Context, SourceLocation &DeclEnd,
1665a7dea167SDimitry Andric     ParsedAttributesWithRange &Attrs, bool RequireSemi, ForRangeInit *FRI,
1666a7dea167SDimitry Andric     SourceLocation *DeclSpecStart) {
16670b57cec5SDimitry Andric   // Parse the common declaration-specifiers piece.
16680b57cec5SDimitry Andric   ParsingDeclSpec DS(*this);
16690b57cec5SDimitry Andric 
16700b57cec5SDimitry Andric   DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
16710b57cec5SDimitry Andric   ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
16720b57cec5SDimitry Andric 
16730b57cec5SDimitry Andric   // If we had a free-standing type definition with a missing semicolon, we
16740b57cec5SDimitry Andric   // may get this far before the problem becomes obvious.
16750b57cec5SDimitry Andric   if (DS.hasTagDefinition() &&
16760b57cec5SDimitry Andric       DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
16770b57cec5SDimitry Andric     return nullptr;
16780b57cec5SDimitry Andric 
16790b57cec5SDimitry Andric   // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
16800b57cec5SDimitry Andric   // declaration-specifiers init-declarator-list[opt] ';'
16810b57cec5SDimitry Andric   if (Tok.is(tok::semi)) {
16820b57cec5SDimitry Andric     ProhibitAttributes(Attrs);
16830b57cec5SDimitry Andric     DeclEnd = Tok.getLocation();
16840b57cec5SDimitry Andric     if (RequireSemi) ConsumeToken();
16850b57cec5SDimitry Andric     RecordDecl *AnonRecord = nullptr;
16860b57cec5SDimitry Andric     Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
16870b57cec5SDimitry Andric                                                        DS, AnonRecord);
16880b57cec5SDimitry Andric     DS.complete(TheDecl);
16890b57cec5SDimitry Andric     if (AnonRecord) {
16900b57cec5SDimitry Andric       Decl* decls[] = {AnonRecord, TheDecl};
16910b57cec5SDimitry Andric       return Actions.BuildDeclaratorGroup(decls);
16920b57cec5SDimitry Andric     }
16930b57cec5SDimitry Andric     return Actions.ConvertDeclToDeclGroup(TheDecl);
16940b57cec5SDimitry Andric   }
16950b57cec5SDimitry Andric 
1696a7dea167SDimitry Andric   if (DeclSpecStart)
1697a7dea167SDimitry Andric     DS.SetRangeStart(*DeclSpecStart);
1698a7dea167SDimitry Andric 
16990b57cec5SDimitry Andric   DS.takeAttributesFrom(Attrs);
17000b57cec5SDimitry Andric   return ParseDeclGroup(DS, Context, &DeclEnd, FRI);
17010b57cec5SDimitry Andric }
17020b57cec5SDimitry Andric 
17030b57cec5SDimitry Andric /// Returns true if this might be the start of a declarator, or a common typo
17040b57cec5SDimitry Andric /// for a declarator.
17050b57cec5SDimitry Andric bool Parser::MightBeDeclarator(DeclaratorContext Context) {
17060b57cec5SDimitry Andric   switch (Tok.getKind()) {
17070b57cec5SDimitry Andric   case tok::annot_cxxscope:
17080b57cec5SDimitry Andric   case tok::annot_template_id:
17090b57cec5SDimitry Andric   case tok::caret:
17100b57cec5SDimitry Andric   case tok::code_completion:
17110b57cec5SDimitry Andric   case tok::coloncolon:
17120b57cec5SDimitry Andric   case tok::ellipsis:
17130b57cec5SDimitry Andric   case tok::kw___attribute:
17140b57cec5SDimitry Andric   case tok::kw_operator:
17150b57cec5SDimitry Andric   case tok::l_paren:
17160b57cec5SDimitry Andric   case tok::star:
17170b57cec5SDimitry Andric     return true;
17180b57cec5SDimitry Andric 
17190b57cec5SDimitry Andric   case tok::amp:
17200b57cec5SDimitry Andric   case tok::ampamp:
17210b57cec5SDimitry Andric     return getLangOpts().CPlusPlus;
17220b57cec5SDimitry Andric 
17230b57cec5SDimitry Andric   case tok::l_square: // Might be an attribute on an unnamed bit-field.
17240b57cec5SDimitry Andric     return Context == DeclaratorContext::MemberContext &&
17250b57cec5SDimitry Andric            getLangOpts().CPlusPlus11 && NextToken().is(tok::l_square);
17260b57cec5SDimitry Andric 
17270b57cec5SDimitry Andric   case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
17280b57cec5SDimitry Andric     return Context == DeclaratorContext::MemberContext ||
17290b57cec5SDimitry Andric            getLangOpts().CPlusPlus;
17300b57cec5SDimitry Andric 
17310b57cec5SDimitry Andric   case tok::identifier:
17320b57cec5SDimitry Andric     switch (NextToken().getKind()) {
17330b57cec5SDimitry Andric     case tok::code_completion:
17340b57cec5SDimitry Andric     case tok::coloncolon:
17350b57cec5SDimitry Andric     case tok::comma:
17360b57cec5SDimitry Andric     case tok::equal:
17370b57cec5SDimitry Andric     case tok::equalequal: // Might be a typo for '='.
17380b57cec5SDimitry Andric     case tok::kw_alignas:
17390b57cec5SDimitry Andric     case tok::kw_asm:
17400b57cec5SDimitry Andric     case tok::kw___attribute:
17410b57cec5SDimitry Andric     case tok::l_brace:
17420b57cec5SDimitry Andric     case tok::l_paren:
17430b57cec5SDimitry Andric     case tok::l_square:
17440b57cec5SDimitry Andric     case tok::less:
17450b57cec5SDimitry Andric     case tok::r_brace:
17460b57cec5SDimitry Andric     case tok::r_paren:
17470b57cec5SDimitry Andric     case tok::r_square:
17480b57cec5SDimitry Andric     case tok::semi:
17490b57cec5SDimitry Andric       return true;
17500b57cec5SDimitry Andric 
17510b57cec5SDimitry Andric     case tok::colon:
17520b57cec5SDimitry Andric       // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
17530b57cec5SDimitry Andric       // and in block scope it's probably a label. Inside a class definition,
17540b57cec5SDimitry Andric       // this is a bit-field.
17550b57cec5SDimitry Andric       return Context == DeclaratorContext::MemberContext ||
17560b57cec5SDimitry Andric              (getLangOpts().CPlusPlus &&
17570b57cec5SDimitry Andric               Context == DeclaratorContext::FileContext);
17580b57cec5SDimitry Andric 
17590b57cec5SDimitry Andric     case tok::identifier: // Possible virt-specifier.
17600b57cec5SDimitry Andric       return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
17610b57cec5SDimitry Andric 
17620b57cec5SDimitry Andric     default:
17630b57cec5SDimitry Andric       return false;
17640b57cec5SDimitry Andric     }
17650b57cec5SDimitry Andric 
17660b57cec5SDimitry Andric   default:
17670b57cec5SDimitry Andric     return false;
17680b57cec5SDimitry Andric   }
17690b57cec5SDimitry Andric }
17700b57cec5SDimitry Andric 
17710b57cec5SDimitry Andric /// Skip until we reach something which seems like a sensible place to pick
17720b57cec5SDimitry Andric /// up parsing after a malformed declaration. This will sometimes stop sooner
17730b57cec5SDimitry Andric /// than SkipUntil(tok::r_brace) would, but will never stop later.
17740b57cec5SDimitry Andric void Parser::SkipMalformedDecl() {
17750b57cec5SDimitry Andric   while (true) {
17760b57cec5SDimitry Andric     switch (Tok.getKind()) {
17770b57cec5SDimitry Andric     case tok::l_brace:
17780b57cec5SDimitry Andric       // Skip until matching }, then stop. We've probably skipped over
17790b57cec5SDimitry Andric       // a malformed class or function definition or similar.
17800b57cec5SDimitry Andric       ConsumeBrace();
17810b57cec5SDimitry Andric       SkipUntil(tok::r_brace);
17820b57cec5SDimitry Andric       if (Tok.isOneOf(tok::comma, tok::l_brace, tok::kw_try)) {
17830b57cec5SDimitry Andric         // This declaration isn't over yet. Keep skipping.
17840b57cec5SDimitry Andric         continue;
17850b57cec5SDimitry Andric       }
17860b57cec5SDimitry Andric       TryConsumeToken(tok::semi);
17870b57cec5SDimitry Andric       return;
17880b57cec5SDimitry Andric 
17890b57cec5SDimitry Andric     case tok::l_square:
17900b57cec5SDimitry Andric       ConsumeBracket();
17910b57cec5SDimitry Andric       SkipUntil(tok::r_square);
17920b57cec5SDimitry Andric       continue;
17930b57cec5SDimitry Andric 
17940b57cec5SDimitry Andric     case tok::l_paren:
17950b57cec5SDimitry Andric       ConsumeParen();
17960b57cec5SDimitry Andric       SkipUntil(tok::r_paren);
17970b57cec5SDimitry Andric       continue;
17980b57cec5SDimitry Andric 
17990b57cec5SDimitry Andric     case tok::r_brace:
18000b57cec5SDimitry Andric       return;
18010b57cec5SDimitry Andric 
18020b57cec5SDimitry Andric     case tok::semi:
18030b57cec5SDimitry Andric       ConsumeToken();
18040b57cec5SDimitry Andric       return;
18050b57cec5SDimitry Andric 
18060b57cec5SDimitry Andric     case tok::kw_inline:
18070b57cec5SDimitry Andric       // 'inline namespace' at the start of a line is almost certainly
18080b57cec5SDimitry Andric       // a good place to pick back up parsing, except in an Objective-C
18090b57cec5SDimitry Andric       // @interface context.
18100b57cec5SDimitry Andric       if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
18110b57cec5SDimitry Andric           (!ParsingInObjCContainer || CurParsedObjCImpl))
18120b57cec5SDimitry Andric         return;
18130b57cec5SDimitry Andric       break;
18140b57cec5SDimitry Andric 
18150b57cec5SDimitry Andric     case tok::kw_namespace:
18160b57cec5SDimitry Andric       // 'namespace' at the start of a line is almost certainly a good
18170b57cec5SDimitry Andric       // place to pick back up parsing, except in an Objective-C
18180b57cec5SDimitry Andric       // @interface context.
18190b57cec5SDimitry Andric       if (Tok.isAtStartOfLine() &&
18200b57cec5SDimitry Andric           (!ParsingInObjCContainer || CurParsedObjCImpl))
18210b57cec5SDimitry Andric         return;
18220b57cec5SDimitry Andric       break;
18230b57cec5SDimitry Andric 
18240b57cec5SDimitry Andric     case tok::at:
18250b57cec5SDimitry Andric       // @end is very much like } in Objective-C contexts.
18260b57cec5SDimitry Andric       if (NextToken().isObjCAtKeyword(tok::objc_end) &&
18270b57cec5SDimitry Andric           ParsingInObjCContainer)
18280b57cec5SDimitry Andric         return;
18290b57cec5SDimitry Andric       break;
18300b57cec5SDimitry Andric 
18310b57cec5SDimitry Andric     case tok::minus:
18320b57cec5SDimitry Andric     case tok::plus:
18330b57cec5SDimitry Andric       // - and + probably start new method declarations in Objective-C contexts.
18340b57cec5SDimitry Andric       if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
18350b57cec5SDimitry Andric         return;
18360b57cec5SDimitry Andric       break;
18370b57cec5SDimitry Andric 
18380b57cec5SDimitry Andric     case tok::eof:
18390b57cec5SDimitry Andric     case tok::annot_module_begin:
18400b57cec5SDimitry Andric     case tok::annot_module_end:
18410b57cec5SDimitry Andric     case tok::annot_module_include:
18420b57cec5SDimitry Andric       return;
18430b57cec5SDimitry Andric 
18440b57cec5SDimitry Andric     default:
18450b57cec5SDimitry Andric       break;
18460b57cec5SDimitry Andric     }
18470b57cec5SDimitry Andric 
18480b57cec5SDimitry Andric     ConsumeAnyToken();
18490b57cec5SDimitry Andric   }
18500b57cec5SDimitry Andric }
18510b57cec5SDimitry Andric 
18520b57cec5SDimitry Andric /// ParseDeclGroup - Having concluded that this is either a function
18530b57cec5SDimitry Andric /// definition or a group of object declarations, actually parse the
18540b57cec5SDimitry Andric /// result.
18550b57cec5SDimitry Andric Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
18560b57cec5SDimitry Andric                                               DeclaratorContext Context,
18570b57cec5SDimitry Andric                                               SourceLocation *DeclEnd,
18580b57cec5SDimitry Andric                                               ForRangeInit *FRI) {
18590b57cec5SDimitry Andric   // Parse the first declarator.
18600b57cec5SDimitry Andric   ParsingDeclarator D(*this, DS, Context);
18610b57cec5SDimitry Andric   ParseDeclarator(D);
18620b57cec5SDimitry Andric 
18630b57cec5SDimitry Andric   // Bail out if the first declarator didn't seem well-formed.
18640b57cec5SDimitry Andric   if (!D.hasName() && !D.mayOmitIdentifier()) {
18650b57cec5SDimitry Andric     SkipMalformedDecl();
18660b57cec5SDimitry Andric     return nullptr;
18670b57cec5SDimitry Andric   }
18680b57cec5SDimitry Andric 
1869480093f4SDimitry Andric   if (Tok.is(tok::kw_requires))
1870480093f4SDimitry Andric     ParseTrailingRequiresClause(D);
1871480093f4SDimitry Andric 
18720b57cec5SDimitry Andric   // Save late-parsed attributes for now; they need to be parsed in the
18730b57cec5SDimitry Andric   // appropriate function scope after the function Decl has been constructed.
18740b57cec5SDimitry Andric   // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
18750b57cec5SDimitry Andric   LateParsedAttrList LateParsedAttrs(true);
18760b57cec5SDimitry Andric   if (D.isFunctionDeclarator()) {
18770b57cec5SDimitry Andric     MaybeParseGNUAttributes(D, &LateParsedAttrs);
18780b57cec5SDimitry Andric 
18790b57cec5SDimitry Andric     // The _Noreturn keyword can't appear here, unlike the GNU noreturn
18800b57cec5SDimitry Andric     // attribute. If we find the keyword here, tell the user to put it
18810b57cec5SDimitry Andric     // at the start instead.
18820b57cec5SDimitry Andric     if (Tok.is(tok::kw__Noreturn)) {
18830b57cec5SDimitry Andric       SourceLocation Loc = ConsumeToken();
18840b57cec5SDimitry Andric       const char *PrevSpec;
18850b57cec5SDimitry Andric       unsigned DiagID;
18860b57cec5SDimitry Andric 
18870b57cec5SDimitry Andric       // We can offer a fixit if it's valid to mark this function as _Noreturn
18880b57cec5SDimitry Andric       // and we don't have any other declarators in this declaration.
18890b57cec5SDimitry Andric       bool Fixit = !DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
18900b57cec5SDimitry Andric       MaybeParseGNUAttributes(D, &LateParsedAttrs);
18910b57cec5SDimitry Andric       Fixit &= Tok.isOneOf(tok::semi, tok::l_brace, tok::kw_try);
18920b57cec5SDimitry Andric 
18930b57cec5SDimitry Andric       Diag(Loc, diag::err_c11_noreturn_misplaced)
18940b57cec5SDimitry Andric           << (Fixit ? FixItHint::CreateRemoval(Loc) : FixItHint())
18950b57cec5SDimitry Andric           << (Fixit ? FixItHint::CreateInsertion(D.getBeginLoc(), "_Noreturn ")
18960b57cec5SDimitry Andric                     : FixItHint());
18970b57cec5SDimitry Andric     }
18980b57cec5SDimitry Andric   }
18990b57cec5SDimitry Andric 
19000b57cec5SDimitry Andric   // Check to see if we have a function *definition* which must have a body.
1901*5ffd83dbSDimitry Andric   if (D.isFunctionDeclarator()) {
1902*5ffd83dbSDimitry Andric     if (Tok.is(tok::equal) && NextToken().is(tok::code_completion)) {
1903*5ffd83dbSDimitry Andric       Actions.CodeCompleteAfterFunctionEquals(D);
1904*5ffd83dbSDimitry Andric       cutOffParsing();
1905*5ffd83dbSDimitry Andric       return nullptr;
1906*5ffd83dbSDimitry Andric     }
19070b57cec5SDimitry Andric     // Look at the next token to make sure that this isn't a function
19080b57cec5SDimitry Andric     // declaration.  We have to check this because __attribute__ might be the
19090b57cec5SDimitry Andric     // start of a function definition in GCC-extended K&R C.
1910*5ffd83dbSDimitry Andric     if (!isDeclarationAfterDeclarator()) {
19110b57cec5SDimitry Andric 
19120b57cec5SDimitry Andric       // Function definitions are only allowed at file scope and in C++ classes.
19130b57cec5SDimitry Andric       // The C++ inline method definition case is handled elsewhere, so we only
19140b57cec5SDimitry Andric       // need to handle the file scope definition case.
19150b57cec5SDimitry Andric       if (Context == DeclaratorContext::FileContext) {
19160b57cec5SDimitry Andric         if (isStartOfFunctionDefinition(D)) {
19170b57cec5SDimitry Andric           if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
19180b57cec5SDimitry Andric             Diag(Tok, diag::err_function_declared_typedef);
19190b57cec5SDimitry Andric 
19200b57cec5SDimitry Andric             // Recover by treating the 'typedef' as spurious.
19210b57cec5SDimitry Andric             DS.ClearStorageClassSpecs();
19220b57cec5SDimitry Andric           }
19230b57cec5SDimitry Andric 
1924*5ffd83dbSDimitry Andric           Decl *TheDecl = ParseFunctionDefinition(D, ParsedTemplateInfo(),
1925*5ffd83dbSDimitry Andric                                                   &LateParsedAttrs);
19260b57cec5SDimitry Andric           return Actions.ConvertDeclToDeclGroup(TheDecl);
19270b57cec5SDimitry Andric         }
19280b57cec5SDimitry Andric 
19290b57cec5SDimitry Andric         if (isDeclarationSpecifier()) {
19300b57cec5SDimitry Andric           // If there is an invalid declaration specifier right after the
19310b57cec5SDimitry Andric           // function prototype, then we must be in a missing semicolon case
19320b57cec5SDimitry Andric           // where this isn't actually a body.  Just fall through into the code
19330b57cec5SDimitry Andric           // that handles it as a prototype, and let the top-level code handle
19340b57cec5SDimitry Andric           // the erroneous declspec where it would otherwise expect a comma or
19350b57cec5SDimitry Andric           // semicolon.
19360b57cec5SDimitry Andric         } else {
19370b57cec5SDimitry Andric           Diag(Tok, diag::err_expected_fn_body);
19380b57cec5SDimitry Andric           SkipUntil(tok::semi);
19390b57cec5SDimitry Andric           return nullptr;
19400b57cec5SDimitry Andric         }
19410b57cec5SDimitry Andric       } else {
19420b57cec5SDimitry Andric         if (Tok.is(tok::l_brace)) {
19430b57cec5SDimitry Andric           Diag(Tok, diag::err_function_definition_not_allowed);
19440b57cec5SDimitry Andric           SkipMalformedDecl();
19450b57cec5SDimitry Andric           return nullptr;
19460b57cec5SDimitry Andric         }
19470b57cec5SDimitry Andric       }
19480b57cec5SDimitry Andric     }
1949*5ffd83dbSDimitry Andric   }
19500b57cec5SDimitry Andric 
19510b57cec5SDimitry Andric   if (ParseAsmAttributesAfterDeclarator(D))
19520b57cec5SDimitry Andric     return nullptr;
19530b57cec5SDimitry Andric 
19540b57cec5SDimitry Andric   // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
19550b57cec5SDimitry Andric   // must parse and analyze the for-range-initializer before the declaration is
19560b57cec5SDimitry Andric   // analyzed.
19570b57cec5SDimitry Andric   //
19580b57cec5SDimitry Andric   // Handle the Objective-C for-in loop variable similarly, although we
19590b57cec5SDimitry Andric   // don't need to parse the container in advance.
19600b57cec5SDimitry Andric   if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
19610b57cec5SDimitry Andric     bool IsForRangeLoop = false;
19620b57cec5SDimitry Andric     if (TryConsumeToken(tok::colon, FRI->ColonLoc)) {
19630b57cec5SDimitry Andric       IsForRangeLoop = true;
1964a7dea167SDimitry Andric       if (getLangOpts().OpenMP)
1965a7dea167SDimitry Andric         Actions.startOpenMPCXXRangeFor();
19660b57cec5SDimitry Andric       if (Tok.is(tok::l_brace))
19670b57cec5SDimitry Andric         FRI->RangeExpr = ParseBraceInitializer();
19680b57cec5SDimitry Andric       else
19690b57cec5SDimitry Andric         FRI->RangeExpr = ParseExpression();
19700b57cec5SDimitry Andric     }
19710b57cec5SDimitry Andric 
19720b57cec5SDimitry Andric     Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
19730b57cec5SDimitry Andric     if (IsForRangeLoop) {
19740b57cec5SDimitry Andric       Actions.ActOnCXXForRangeDecl(ThisDecl);
19750b57cec5SDimitry Andric     } else {
19760b57cec5SDimitry Andric       // Obj-C for loop
19770b57cec5SDimitry Andric       if (auto *VD = dyn_cast_or_null<VarDecl>(ThisDecl))
19780b57cec5SDimitry Andric         VD->setObjCForDecl(true);
19790b57cec5SDimitry Andric     }
19800b57cec5SDimitry Andric     Actions.FinalizeDeclaration(ThisDecl);
19810b57cec5SDimitry Andric     D.complete(ThisDecl);
19820b57cec5SDimitry Andric     return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
19830b57cec5SDimitry Andric   }
19840b57cec5SDimitry Andric 
19850b57cec5SDimitry Andric   SmallVector<Decl *, 8> DeclsInGroup;
19860b57cec5SDimitry Andric   Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(
19870b57cec5SDimitry Andric       D, ParsedTemplateInfo(), FRI);
19880b57cec5SDimitry Andric   if (LateParsedAttrs.size() > 0)
19890b57cec5SDimitry Andric     ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
19900b57cec5SDimitry Andric   D.complete(FirstDecl);
19910b57cec5SDimitry Andric   if (FirstDecl)
19920b57cec5SDimitry Andric     DeclsInGroup.push_back(FirstDecl);
19930b57cec5SDimitry Andric 
19940b57cec5SDimitry Andric   bool ExpectSemi = Context != DeclaratorContext::ForContext;
19950b57cec5SDimitry Andric 
19960b57cec5SDimitry Andric   // If we don't have a comma, it is either the end of the list (a ';') or an
19970b57cec5SDimitry Andric   // error, bail out.
19980b57cec5SDimitry Andric   SourceLocation CommaLoc;
19990b57cec5SDimitry Andric   while (TryConsumeToken(tok::comma, CommaLoc)) {
20000b57cec5SDimitry Andric     if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
20010b57cec5SDimitry Andric       // This comma was followed by a line-break and something which can't be
20020b57cec5SDimitry Andric       // the start of a declarator. The comma was probably a typo for a
20030b57cec5SDimitry Andric       // semicolon.
20040b57cec5SDimitry Andric       Diag(CommaLoc, diag::err_expected_semi_declaration)
20050b57cec5SDimitry Andric         << FixItHint::CreateReplacement(CommaLoc, ";");
20060b57cec5SDimitry Andric       ExpectSemi = false;
20070b57cec5SDimitry Andric       break;
20080b57cec5SDimitry Andric     }
20090b57cec5SDimitry Andric 
20100b57cec5SDimitry Andric     // Parse the next declarator.
20110b57cec5SDimitry Andric     D.clear();
20120b57cec5SDimitry Andric     D.setCommaLoc(CommaLoc);
20130b57cec5SDimitry Andric 
20140b57cec5SDimitry Andric     // Accept attributes in an init-declarator.  In the first declarator in a
20150b57cec5SDimitry Andric     // declaration, these would be part of the declspec.  In subsequent
20160b57cec5SDimitry Andric     // declarators, they become part of the declarator itself, so that they
20170b57cec5SDimitry Andric     // don't apply to declarators after *this* one.  Examples:
20180b57cec5SDimitry Andric     //    short __attribute__((common)) var;    -> declspec
20190b57cec5SDimitry Andric     //    short var __attribute__((common));    -> declarator
20200b57cec5SDimitry Andric     //    short x, __attribute__((common)) var;    -> declarator
20210b57cec5SDimitry Andric     MaybeParseGNUAttributes(D);
20220b57cec5SDimitry Andric 
20230b57cec5SDimitry Andric     // MSVC parses but ignores qualifiers after the comma as an extension.
20240b57cec5SDimitry Andric     if (getLangOpts().MicrosoftExt)
20250b57cec5SDimitry Andric       DiagnoseAndSkipExtendedMicrosoftTypeAttributes();
20260b57cec5SDimitry Andric 
20270b57cec5SDimitry Andric     ParseDeclarator(D);
20280b57cec5SDimitry Andric     if (!D.isInvalidType()) {
2029480093f4SDimitry Andric       // C++2a [dcl.decl]p1
2030480093f4SDimitry Andric       //    init-declarator:
2031480093f4SDimitry Andric       //	      declarator initializer[opt]
2032480093f4SDimitry Andric       //        declarator requires-clause
2033480093f4SDimitry Andric       if (Tok.is(tok::kw_requires))
2034480093f4SDimitry Andric         ParseTrailingRequiresClause(D);
20350b57cec5SDimitry Andric       Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
20360b57cec5SDimitry Andric       D.complete(ThisDecl);
20370b57cec5SDimitry Andric       if (ThisDecl)
20380b57cec5SDimitry Andric         DeclsInGroup.push_back(ThisDecl);
20390b57cec5SDimitry Andric     }
20400b57cec5SDimitry Andric   }
20410b57cec5SDimitry Andric 
20420b57cec5SDimitry Andric   if (DeclEnd)
20430b57cec5SDimitry Andric     *DeclEnd = Tok.getLocation();
20440b57cec5SDimitry Andric 
20450b57cec5SDimitry Andric   if (ExpectSemi &&
20460b57cec5SDimitry Andric       ExpectAndConsumeSemi(Context == DeclaratorContext::FileContext
20470b57cec5SDimitry Andric                            ? diag::err_invalid_token_after_toplevel_declarator
20480b57cec5SDimitry Andric                            : diag::err_expected_semi_declaration)) {
20490b57cec5SDimitry Andric     // Okay, there was no semicolon and one was expected.  If we see a
20500b57cec5SDimitry Andric     // declaration specifier, just assume it was missing and continue parsing.
20510b57cec5SDimitry Andric     // Otherwise things are very confused and we skip to recover.
20520b57cec5SDimitry Andric     if (!isDeclarationSpecifier()) {
20530b57cec5SDimitry Andric       SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
20540b57cec5SDimitry Andric       TryConsumeToken(tok::semi);
20550b57cec5SDimitry Andric     }
20560b57cec5SDimitry Andric   }
20570b57cec5SDimitry Andric 
20580b57cec5SDimitry Andric   return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
20590b57cec5SDimitry Andric }
20600b57cec5SDimitry Andric 
20610b57cec5SDimitry Andric /// Parse an optional simple-asm-expr and attributes, and attach them to a
20620b57cec5SDimitry Andric /// declarator. Returns true on an error.
20630b57cec5SDimitry Andric bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
20640b57cec5SDimitry Andric   // If a simple-asm-expr is present, parse it.
20650b57cec5SDimitry Andric   if (Tok.is(tok::kw_asm)) {
20660b57cec5SDimitry Andric     SourceLocation Loc;
2067480093f4SDimitry Andric     ExprResult AsmLabel(ParseSimpleAsm(/*ForAsmLabel*/ true, &Loc));
20680b57cec5SDimitry Andric     if (AsmLabel.isInvalid()) {
20690b57cec5SDimitry Andric       SkipUntil(tok::semi, StopBeforeMatch);
20700b57cec5SDimitry Andric       return true;
20710b57cec5SDimitry Andric     }
20720b57cec5SDimitry Andric 
20730b57cec5SDimitry Andric     D.setAsmLabel(AsmLabel.get());
20740b57cec5SDimitry Andric     D.SetRangeEnd(Loc);
20750b57cec5SDimitry Andric   }
20760b57cec5SDimitry Andric 
20770b57cec5SDimitry Andric   MaybeParseGNUAttributes(D);
20780b57cec5SDimitry Andric   return false;
20790b57cec5SDimitry Andric }
20800b57cec5SDimitry Andric 
20810b57cec5SDimitry Andric /// Parse 'declaration' after parsing 'declaration-specifiers
20820b57cec5SDimitry Andric /// declarator'. This method parses the remainder of the declaration
20830b57cec5SDimitry Andric /// (including any attributes or initializer, among other things) and
20840b57cec5SDimitry Andric /// finalizes the declaration.
20850b57cec5SDimitry Andric ///
20860b57cec5SDimitry Andric ///       init-declarator: [C99 6.7]
20870b57cec5SDimitry Andric ///         declarator
20880b57cec5SDimitry Andric ///         declarator '=' initializer
20890b57cec5SDimitry Andric /// [GNU]   declarator simple-asm-expr[opt] attributes[opt]
20900b57cec5SDimitry Andric /// [GNU]   declarator simple-asm-expr[opt] attributes[opt] '=' initializer
20910b57cec5SDimitry Andric /// [C++]   declarator initializer[opt]
20920b57cec5SDimitry Andric ///
20930b57cec5SDimitry Andric /// [C++] initializer:
20940b57cec5SDimitry Andric /// [C++]   '=' initializer-clause
20950b57cec5SDimitry Andric /// [C++]   '(' expression-list ')'
20960b57cec5SDimitry Andric /// [C++0x] '=' 'default'                                                [TODO]
20970b57cec5SDimitry Andric /// [C++0x] '=' 'delete'
20980b57cec5SDimitry Andric /// [C++0x] braced-init-list
20990b57cec5SDimitry Andric ///
21000b57cec5SDimitry Andric /// According to the standard grammar, =default and =delete are function
21010b57cec5SDimitry Andric /// definitions, but that definitely doesn't fit with the parser here.
21020b57cec5SDimitry Andric ///
21030b57cec5SDimitry Andric Decl *Parser::ParseDeclarationAfterDeclarator(
21040b57cec5SDimitry Andric     Declarator &D, const ParsedTemplateInfo &TemplateInfo) {
21050b57cec5SDimitry Andric   if (ParseAsmAttributesAfterDeclarator(D))
21060b57cec5SDimitry Andric     return nullptr;
21070b57cec5SDimitry Andric 
21080b57cec5SDimitry Andric   return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
21090b57cec5SDimitry Andric }
21100b57cec5SDimitry Andric 
21110b57cec5SDimitry Andric Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(
21120b57cec5SDimitry Andric     Declarator &D, const ParsedTemplateInfo &TemplateInfo, ForRangeInit *FRI) {
21130b57cec5SDimitry Andric   // RAII type used to track whether we're inside an initializer.
21140b57cec5SDimitry Andric   struct InitializerScopeRAII {
21150b57cec5SDimitry Andric     Parser &P;
21160b57cec5SDimitry Andric     Declarator &D;
21170b57cec5SDimitry Andric     Decl *ThisDecl;
21180b57cec5SDimitry Andric 
21190b57cec5SDimitry Andric     InitializerScopeRAII(Parser &P, Declarator &D, Decl *ThisDecl)
21200b57cec5SDimitry Andric         : P(P), D(D), ThisDecl(ThisDecl) {
21210b57cec5SDimitry Andric       if (ThisDecl && P.getLangOpts().CPlusPlus) {
21220b57cec5SDimitry Andric         Scope *S = nullptr;
21230b57cec5SDimitry Andric         if (D.getCXXScopeSpec().isSet()) {
21240b57cec5SDimitry Andric           P.EnterScope(0);
21250b57cec5SDimitry Andric           S = P.getCurScope();
21260b57cec5SDimitry Andric         }
21270b57cec5SDimitry Andric         P.Actions.ActOnCXXEnterDeclInitializer(S, ThisDecl);
21280b57cec5SDimitry Andric       }
21290b57cec5SDimitry Andric     }
21300b57cec5SDimitry Andric     ~InitializerScopeRAII() { pop(); }
21310b57cec5SDimitry Andric     void pop() {
21320b57cec5SDimitry Andric       if (ThisDecl && P.getLangOpts().CPlusPlus) {
21330b57cec5SDimitry Andric         Scope *S = nullptr;
21340b57cec5SDimitry Andric         if (D.getCXXScopeSpec().isSet())
21350b57cec5SDimitry Andric           S = P.getCurScope();
21360b57cec5SDimitry Andric         P.Actions.ActOnCXXExitDeclInitializer(S, ThisDecl);
21370b57cec5SDimitry Andric         if (S)
21380b57cec5SDimitry Andric           P.ExitScope();
21390b57cec5SDimitry Andric       }
21400b57cec5SDimitry Andric       ThisDecl = nullptr;
21410b57cec5SDimitry Andric     }
21420b57cec5SDimitry Andric   };
21430b57cec5SDimitry Andric 
21440b57cec5SDimitry Andric   // Inform the current actions module that we just parsed this declarator.
21450b57cec5SDimitry Andric   Decl *ThisDecl = nullptr;
21460b57cec5SDimitry Andric   switch (TemplateInfo.Kind) {
21470b57cec5SDimitry Andric   case ParsedTemplateInfo::NonTemplate:
21480b57cec5SDimitry Andric     ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
21490b57cec5SDimitry Andric     break;
21500b57cec5SDimitry Andric 
21510b57cec5SDimitry Andric   case ParsedTemplateInfo::Template:
21520b57cec5SDimitry Andric   case ParsedTemplateInfo::ExplicitSpecialization: {
21530b57cec5SDimitry Andric     ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
21540b57cec5SDimitry Andric                                                *TemplateInfo.TemplateParams,
21550b57cec5SDimitry Andric                                                D);
21560b57cec5SDimitry Andric     if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
21570b57cec5SDimitry Andric       // Re-direct this decl to refer to the templated decl so that we can
21580b57cec5SDimitry Andric       // initialize it.
21590b57cec5SDimitry Andric       ThisDecl = VT->getTemplatedDecl();
21600b57cec5SDimitry Andric     break;
21610b57cec5SDimitry Andric   }
21620b57cec5SDimitry Andric   case ParsedTemplateInfo::ExplicitInstantiation: {
21630b57cec5SDimitry Andric     if (Tok.is(tok::semi)) {
21640b57cec5SDimitry Andric       DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
21650b57cec5SDimitry Andric           getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
21660b57cec5SDimitry Andric       if (ThisRes.isInvalid()) {
21670b57cec5SDimitry Andric         SkipUntil(tok::semi, StopBeforeMatch);
21680b57cec5SDimitry Andric         return nullptr;
21690b57cec5SDimitry Andric       }
21700b57cec5SDimitry Andric       ThisDecl = ThisRes.get();
21710b57cec5SDimitry Andric     } else {
21720b57cec5SDimitry Andric       // FIXME: This check should be for a variable template instantiation only.
21730b57cec5SDimitry Andric 
21740b57cec5SDimitry Andric       // Check that this is a valid instantiation
21750b57cec5SDimitry Andric       if (D.getName().getKind() != UnqualifiedIdKind::IK_TemplateId) {
21760b57cec5SDimitry Andric         // If the declarator-id is not a template-id, issue a diagnostic and
21770b57cec5SDimitry Andric         // recover by ignoring the 'template' keyword.
21780b57cec5SDimitry Andric         Diag(Tok, diag::err_template_defn_explicit_instantiation)
21790b57cec5SDimitry Andric             << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
21800b57cec5SDimitry Andric         ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
21810b57cec5SDimitry Andric       } else {
21820b57cec5SDimitry Andric         SourceLocation LAngleLoc =
21830b57cec5SDimitry Andric             PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
21840b57cec5SDimitry Andric         Diag(D.getIdentifierLoc(),
21850b57cec5SDimitry Andric              diag::err_explicit_instantiation_with_definition)
21860b57cec5SDimitry Andric             << SourceRange(TemplateInfo.TemplateLoc)
21870b57cec5SDimitry Andric             << FixItHint::CreateInsertion(LAngleLoc, "<>");
21880b57cec5SDimitry Andric 
21890b57cec5SDimitry Andric         // Recover as if it were an explicit specialization.
21900b57cec5SDimitry Andric         TemplateParameterLists FakedParamLists;
21910b57cec5SDimitry Andric         FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
21920b57cec5SDimitry Andric             0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None,
21930b57cec5SDimitry Andric             LAngleLoc, nullptr));
21940b57cec5SDimitry Andric 
21950b57cec5SDimitry Andric         ThisDecl =
21960b57cec5SDimitry Andric             Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
21970b57cec5SDimitry Andric       }
21980b57cec5SDimitry Andric     }
21990b57cec5SDimitry Andric     break;
22000b57cec5SDimitry Andric     }
22010b57cec5SDimitry Andric   }
22020b57cec5SDimitry Andric 
22030b57cec5SDimitry Andric   // Parse declarator '=' initializer.
22040b57cec5SDimitry Andric   // If a '==' or '+=' is found, suggest a fixit to '='.
22050b57cec5SDimitry Andric   if (isTokenEqualOrEqualTypo()) {
22060b57cec5SDimitry Andric     SourceLocation EqualLoc = ConsumeToken();
22070b57cec5SDimitry Andric 
22080b57cec5SDimitry Andric     if (Tok.is(tok::kw_delete)) {
22090b57cec5SDimitry Andric       if (D.isFunctionDeclarator())
22100b57cec5SDimitry Andric         Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
22110b57cec5SDimitry Andric           << 1 /* delete */;
22120b57cec5SDimitry Andric       else
22130b57cec5SDimitry Andric         Diag(ConsumeToken(), diag::err_deleted_non_function);
22140b57cec5SDimitry Andric     } else if (Tok.is(tok::kw_default)) {
22150b57cec5SDimitry Andric       if (D.isFunctionDeclarator())
22160b57cec5SDimitry Andric         Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
22170b57cec5SDimitry Andric           << 0 /* default */;
22180b57cec5SDimitry Andric       else
2219480093f4SDimitry Andric         Diag(ConsumeToken(), diag::err_default_special_members)
2220*5ffd83dbSDimitry Andric             << getLangOpts().CPlusPlus20;
22210b57cec5SDimitry Andric     } else {
22220b57cec5SDimitry Andric       InitializerScopeRAII InitScope(*this, D, ThisDecl);
22230b57cec5SDimitry Andric 
22240b57cec5SDimitry Andric       if (Tok.is(tok::code_completion)) {
22250b57cec5SDimitry Andric         Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
22260b57cec5SDimitry Andric         Actions.FinalizeDeclaration(ThisDecl);
22270b57cec5SDimitry Andric         cutOffParsing();
22280b57cec5SDimitry Andric         return nullptr;
22290b57cec5SDimitry Andric       }
22300b57cec5SDimitry Andric 
22310b57cec5SDimitry Andric       PreferredType.enterVariableInit(Tok.getLocation(), ThisDecl);
22320b57cec5SDimitry Andric       ExprResult Init = ParseInitializer();
22330b57cec5SDimitry Andric 
22340b57cec5SDimitry Andric       // If this is the only decl in (possibly) range based for statement,
22350b57cec5SDimitry Andric       // our best guess is that the user meant ':' instead of '='.
22360b57cec5SDimitry Andric       if (Tok.is(tok::r_paren) && FRI && D.isFirstDeclarator()) {
22370b57cec5SDimitry Andric         Diag(EqualLoc, diag::err_single_decl_assign_in_for_range)
22380b57cec5SDimitry Andric             << FixItHint::CreateReplacement(EqualLoc, ":");
22390b57cec5SDimitry Andric         // We are trying to stop parser from looking for ';' in this for
22400b57cec5SDimitry Andric         // statement, therefore preventing spurious errors to be issued.
22410b57cec5SDimitry Andric         FRI->ColonLoc = EqualLoc;
22420b57cec5SDimitry Andric         Init = ExprError();
22430b57cec5SDimitry Andric         FRI->RangeExpr = Init;
22440b57cec5SDimitry Andric       }
22450b57cec5SDimitry Andric 
22460b57cec5SDimitry Andric       InitScope.pop();
22470b57cec5SDimitry Andric 
22480b57cec5SDimitry Andric       if (Init.isInvalid()) {
22490b57cec5SDimitry Andric         SmallVector<tok::TokenKind, 2> StopTokens;
22500b57cec5SDimitry Andric         StopTokens.push_back(tok::comma);
22510b57cec5SDimitry Andric         if (D.getContext() == DeclaratorContext::ForContext ||
22520b57cec5SDimitry Andric             D.getContext() == DeclaratorContext::InitStmtContext)
22530b57cec5SDimitry Andric           StopTokens.push_back(tok::r_paren);
22540b57cec5SDimitry Andric         SkipUntil(StopTokens, StopAtSemi | StopBeforeMatch);
22550b57cec5SDimitry Andric         Actions.ActOnInitializerError(ThisDecl);
22560b57cec5SDimitry Andric       } else
22570b57cec5SDimitry Andric         Actions.AddInitializerToDecl(ThisDecl, Init.get(),
22580b57cec5SDimitry Andric                                      /*DirectInit=*/false);
22590b57cec5SDimitry Andric     }
22600b57cec5SDimitry Andric   } else if (Tok.is(tok::l_paren)) {
22610b57cec5SDimitry Andric     // Parse C++ direct initializer: '(' expression-list ')'
22620b57cec5SDimitry Andric     BalancedDelimiterTracker T(*this, tok::l_paren);
22630b57cec5SDimitry Andric     T.consumeOpen();
22640b57cec5SDimitry Andric 
22650b57cec5SDimitry Andric     ExprVector Exprs;
22660b57cec5SDimitry Andric     CommaLocsTy CommaLocs;
22670b57cec5SDimitry Andric 
22680b57cec5SDimitry Andric     InitializerScopeRAII InitScope(*this, D, ThisDecl);
22690b57cec5SDimitry Andric 
22700b57cec5SDimitry Andric     auto ThisVarDecl = dyn_cast_or_null<VarDecl>(ThisDecl);
22710b57cec5SDimitry Andric     auto RunSignatureHelp = [&]() {
22720b57cec5SDimitry Andric       QualType PreferredType = Actions.ProduceConstructorSignatureHelp(
22730b57cec5SDimitry Andric           getCurScope(), ThisVarDecl->getType()->getCanonicalTypeInternal(),
22740b57cec5SDimitry Andric           ThisDecl->getLocation(), Exprs, T.getOpenLocation());
22750b57cec5SDimitry Andric       CalledSignatureHelp = true;
22760b57cec5SDimitry Andric       return PreferredType;
22770b57cec5SDimitry Andric     };
22780b57cec5SDimitry Andric     auto SetPreferredType = [&] {
22790b57cec5SDimitry Andric       PreferredType.enterFunctionArgument(Tok.getLocation(), RunSignatureHelp);
22800b57cec5SDimitry Andric     };
22810b57cec5SDimitry Andric 
22820b57cec5SDimitry Andric     llvm::function_ref<void()> ExpressionStarts;
22830b57cec5SDimitry Andric     if (ThisVarDecl) {
22840b57cec5SDimitry Andric       // ParseExpressionList can sometimes succeed even when ThisDecl is not
22850b57cec5SDimitry Andric       // VarDecl. This is an error and it is reported in a call to
22860b57cec5SDimitry Andric       // Actions.ActOnInitializerError(). However, we call
22870b57cec5SDimitry Andric       // ProduceConstructorSignatureHelp only on VarDecls.
22880b57cec5SDimitry Andric       ExpressionStarts = SetPreferredType;
22890b57cec5SDimitry Andric     }
22900b57cec5SDimitry Andric     if (ParseExpressionList(Exprs, CommaLocs, ExpressionStarts)) {
22910b57cec5SDimitry Andric       if (ThisVarDecl && PP.isCodeCompletionReached() && !CalledSignatureHelp) {
22920b57cec5SDimitry Andric         Actions.ProduceConstructorSignatureHelp(
22930b57cec5SDimitry Andric             getCurScope(), ThisVarDecl->getType()->getCanonicalTypeInternal(),
22940b57cec5SDimitry Andric             ThisDecl->getLocation(), Exprs, T.getOpenLocation());
22950b57cec5SDimitry Andric         CalledSignatureHelp = true;
22960b57cec5SDimitry Andric       }
22970b57cec5SDimitry Andric       Actions.ActOnInitializerError(ThisDecl);
22980b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi);
22990b57cec5SDimitry Andric     } else {
23000b57cec5SDimitry Andric       // Match the ')'.
23010b57cec5SDimitry Andric       T.consumeClose();
23020b57cec5SDimitry Andric 
23030b57cec5SDimitry Andric       assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
23040b57cec5SDimitry Andric              "Unexpected number of commas!");
23050b57cec5SDimitry Andric 
23060b57cec5SDimitry Andric       InitScope.pop();
23070b57cec5SDimitry Andric 
23080b57cec5SDimitry Andric       ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
23090b57cec5SDimitry Andric                                                           T.getCloseLocation(),
23100b57cec5SDimitry Andric                                                           Exprs);
23110b57cec5SDimitry Andric       Actions.AddInitializerToDecl(ThisDecl, Initializer.get(),
23120b57cec5SDimitry Andric                                    /*DirectInit=*/true);
23130b57cec5SDimitry Andric     }
23140b57cec5SDimitry Andric   } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
23150b57cec5SDimitry Andric              (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
23160b57cec5SDimitry Andric     // Parse C++0x braced-init-list.
23170b57cec5SDimitry Andric     Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
23180b57cec5SDimitry Andric 
23190b57cec5SDimitry Andric     InitializerScopeRAII InitScope(*this, D, ThisDecl);
23200b57cec5SDimitry Andric 
2321*5ffd83dbSDimitry Andric     PreferredType.enterVariableInit(Tok.getLocation(), ThisDecl);
23220b57cec5SDimitry Andric     ExprResult Init(ParseBraceInitializer());
23230b57cec5SDimitry Andric 
23240b57cec5SDimitry Andric     InitScope.pop();
23250b57cec5SDimitry Andric 
23260b57cec5SDimitry Andric     if (Init.isInvalid()) {
23270b57cec5SDimitry Andric       Actions.ActOnInitializerError(ThisDecl);
23280b57cec5SDimitry Andric     } else
23290b57cec5SDimitry Andric       Actions.AddInitializerToDecl(ThisDecl, Init.get(), /*DirectInit=*/true);
23300b57cec5SDimitry Andric 
23310b57cec5SDimitry Andric   } else {
23320b57cec5SDimitry Andric     Actions.ActOnUninitializedDecl(ThisDecl);
23330b57cec5SDimitry Andric   }
23340b57cec5SDimitry Andric 
23350b57cec5SDimitry Andric   Actions.FinalizeDeclaration(ThisDecl);
23360b57cec5SDimitry Andric 
23370b57cec5SDimitry Andric   return ThisDecl;
23380b57cec5SDimitry Andric }
23390b57cec5SDimitry Andric 
23400b57cec5SDimitry Andric /// ParseSpecifierQualifierList
23410b57cec5SDimitry Andric ///        specifier-qualifier-list:
23420b57cec5SDimitry Andric ///          type-specifier specifier-qualifier-list[opt]
23430b57cec5SDimitry Andric ///          type-qualifier specifier-qualifier-list[opt]
23440b57cec5SDimitry Andric /// [GNU]    attributes     specifier-qualifier-list[opt]
23450b57cec5SDimitry Andric ///
23460b57cec5SDimitry Andric void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
23470b57cec5SDimitry Andric                                          DeclSpecContext DSC) {
23480b57cec5SDimitry Andric   /// specifier-qualifier-list is a subset of declaration-specifiers.  Just
23490b57cec5SDimitry Andric   /// parse declaration-specifiers and complain about extra stuff.
23500b57cec5SDimitry Andric   /// TODO: diagnose attribute-specifiers and alignment-specifiers.
23510b57cec5SDimitry Andric   ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
23520b57cec5SDimitry Andric 
23530b57cec5SDimitry Andric   // Validate declspec for type-name.
23540b57cec5SDimitry Andric   unsigned Specs = DS.getParsedSpecifiers();
23550b57cec5SDimitry Andric   if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) {
23560b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_type);
23570b57cec5SDimitry Andric     DS.SetTypeSpecError();
23580b57cec5SDimitry Andric   } else if (Specs == DeclSpec::PQ_None && !DS.hasAttributes()) {
23590b57cec5SDimitry Andric     Diag(Tok, diag::err_typename_requires_specqual);
23600b57cec5SDimitry Andric     if (!DS.hasTypeSpecifier())
23610b57cec5SDimitry Andric       DS.SetTypeSpecError();
23620b57cec5SDimitry Andric   }
23630b57cec5SDimitry Andric 
23640b57cec5SDimitry Andric   // Issue diagnostic and remove storage class if present.
23650b57cec5SDimitry Andric   if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
23660b57cec5SDimitry Andric     if (DS.getStorageClassSpecLoc().isValid())
23670b57cec5SDimitry Andric       Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
23680b57cec5SDimitry Andric     else
23690b57cec5SDimitry Andric       Diag(DS.getThreadStorageClassSpecLoc(),
23700b57cec5SDimitry Andric            diag::err_typename_invalid_storageclass);
23710b57cec5SDimitry Andric     DS.ClearStorageClassSpecs();
23720b57cec5SDimitry Andric   }
23730b57cec5SDimitry Andric 
23740b57cec5SDimitry Andric   // Issue diagnostic and remove function specifier if present.
23750b57cec5SDimitry Andric   if (Specs & DeclSpec::PQ_FunctionSpecifier) {
23760b57cec5SDimitry Andric     if (DS.isInlineSpecified())
23770b57cec5SDimitry Andric       Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
23780b57cec5SDimitry Andric     if (DS.isVirtualSpecified())
23790b57cec5SDimitry Andric       Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
23800b57cec5SDimitry Andric     if (DS.hasExplicitSpecifier())
23810b57cec5SDimitry Andric       Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
23820b57cec5SDimitry Andric     DS.ClearFunctionSpecs();
23830b57cec5SDimitry Andric   }
23840b57cec5SDimitry Andric 
23850b57cec5SDimitry Andric   // Issue diagnostic and remove constexpr specifier if present.
23860b57cec5SDimitry Andric   if (DS.hasConstexprSpecifier() && DSC != DeclSpecContext::DSC_condition) {
23870b57cec5SDimitry Andric     Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr)
2388a7dea167SDimitry Andric         << DS.getConstexprSpecifier();
23890b57cec5SDimitry Andric     DS.ClearConstexprSpec();
23900b57cec5SDimitry Andric   }
23910b57cec5SDimitry Andric }
23920b57cec5SDimitry Andric 
23930b57cec5SDimitry Andric /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
23940b57cec5SDimitry Andric /// specified token is valid after the identifier in a declarator which
23950b57cec5SDimitry Andric /// immediately follows the declspec.  For example, these things are valid:
23960b57cec5SDimitry Andric ///
23970b57cec5SDimitry Andric ///      int x   [             4];         // direct-declarator
23980b57cec5SDimitry Andric ///      int x   (             int y);     // direct-declarator
23990b57cec5SDimitry Andric ///  int(int x   )                         // direct-declarator
24000b57cec5SDimitry Andric ///      int x   ;                         // simple-declaration
24010b57cec5SDimitry Andric ///      int x   =             17;         // init-declarator-list
24020b57cec5SDimitry Andric ///      int x   ,             y;          // init-declarator-list
24030b57cec5SDimitry Andric ///      int x   __asm__       ("foo");    // init-declarator-list
24040b57cec5SDimitry Andric ///      int x   :             4;          // struct-declarator
24050b57cec5SDimitry Andric ///      int x   {             5};         // C++'0x unified initializers
24060b57cec5SDimitry Andric ///
24070b57cec5SDimitry Andric /// This is not, because 'x' does not immediately follow the declspec (though
24080b57cec5SDimitry Andric /// ')' happens to be valid anyway).
24090b57cec5SDimitry Andric ///    int (x)
24100b57cec5SDimitry Andric ///
24110b57cec5SDimitry Andric static bool isValidAfterIdentifierInDeclarator(const Token &T) {
24120b57cec5SDimitry Andric   return T.isOneOf(tok::l_square, tok::l_paren, tok::r_paren, tok::semi,
24130b57cec5SDimitry Andric                    tok::comma, tok::equal, tok::kw_asm, tok::l_brace,
24140b57cec5SDimitry Andric                    tok::colon);
24150b57cec5SDimitry Andric }
24160b57cec5SDimitry Andric 
24170b57cec5SDimitry Andric /// ParseImplicitInt - This method is called when we have an non-typename
24180b57cec5SDimitry Andric /// identifier in a declspec (which normally terminates the decl spec) when
24190b57cec5SDimitry Andric /// the declspec has no type specifier.  In this case, the declspec is either
24200b57cec5SDimitry Andric /// malformed or is "implicit int" (in K&R and C89).
24210b57cec5SDimitry Andric ///
24220b57cec5SDimitry Andric /// This method handles diagnosing this prettily and returns false if the
24230b57cec5SDimitry Andric /// declspec is done being processed.  If it recovers and thinks there may be
24240b57cec5SDimitry Andric /// other pieces of declspec after it, it returns true.
24250b57cec5SDimitry Andric ///
24260b57cec5SDimitry Andric bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
24270b57cec5SDimitry Andric                               const ParsedTemplateInfo &TemplateInfo,
24280b57cec5SDimitry Andric                               AccessSpecifier AS, DeclSpecContext DSC,
24290b57cec5SDimitry Andric                               ParsedAttributesWithRange &Attrs) {
24300b57cec5SDimitry Andric   assert(Tok.is(tok::identifier) && "should have identifier");
24310b57cec5SDimitry Andric 
24320b57cec5SDimitry Andric   SourceLocation Loc = Tok.getLocation();
24330b57cec5SDimitry Andric   // If we see an identifier that is not a type name, we normally would
24340b57cec5SDimitry Andric   // parse it as the identifier being declared.  However, when a typename
24350b57cec5SDimitry Andric   // is typo'd or the definition is not included, this will incorrectly
24360b57cec5SDimitry Andric   // parse the typename as the identifier name and fall over misparsing
24370b57cec5SDimitry Andric   // later parts of the diagnostic.
24380b57cec5SDimitry Andric   //
24390b57cec5SDimitry Andric   // As such, we try to do some look-ahead in cases where this would
24400b57cec5SDimitry Andric   // otherwise be an "implicit-int" case to see if this is invalid.  For
24410b57cec5SDimitry Andric   // example: "static foo_t x = 4;"  In this case, if we parsed foo_t as
24420b57cec5SDimitry Andric   // an identifier with implicit int, we'd get a parse error because the
24430b57cec5SDimitry Andric   // next token is obviously invalid for a type.  Parse these as a case
24440b57cec5SDimitry Andric   // with an invalid type specifier.
24450b57cec5SDimitry Andric   assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
24460b57cec5SDimitry Andric 
24470b57cec5SDimitry Andric   // Since we know that this either implicit int (which is rare) or an
24480b57cec5SDimitry Andric   // error, do lookahead to try to do better recovery. This never applies
24490b57cec5SDimitry Andric   // within a type specifier. Outside of C++, we allow this even if the
24500b57cec5SDimitry Andric   // language doesn't "officially" support implicit int -- we support
24510b57cec5SDimitry Andric   // implicit int as an extension in C99 and C11.
24520b57cec5SDimitry Andric   if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus &&
24530b57cec5SDimitry Andric       isValidAfterIdentifierInDeclarator(NextToken())) {
24540b57cec5SDimitry Andric     // If this token is valid for implicit int, e.g. "static x = 4", then
24550b57cec5SDimitry Andric     // we just avoid eating the identifier, so it will be parsed as the
24560b57cec5SDimitry Andric     // identifier in the declarator.
24570b57cec5SDimitry Andric     return false;
24580b57cec5SDimitry Andric   }
24590b57cec5SDimitry Andric 
24600b57cec5SDimitry Andric   // Early exit as Sema has a dedicated missing_actual_pipe_type diagnostic
24610b57cec5SDimitry Andric   // for incomplete declarations such as `pipe p`.
24620b57cec5SDimitry Andric   if (getLangOpts().OpenCLCPlusPlus && DS.isTypeSpecPipe())
24630b57cec5SDimitry Andric     return false;
24640b57cec5SDimitry Andric 
24650b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus &&
24660b57cec5SDimitry Andric       DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
24670b57cec5SDimitry Andric     // Don't require a type specifier if we have the 'auto' storage class
24680b57cec5SDimitry Andric     // specifier in C++98 -- we'll promote it to a type specifier.
24690b57cec5SDimitry Andric     if (SS)
24700b57cec5SDimitry Andric       AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
24710b57cec5SDimitry Andric     return false;
24720b57cec5SDimitry Andric   }
24730b57cec5SDimitry Andric 
24740b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus && (!SS || SS->isEmpty()) &&
24750b57cec5SDimitry Andric       getLangOpts().MSVCCompat) {
24760b57cec5SDimitry Andric     // Lookup of an unqualified type name has failed in MSVC compatibility mode.
24770b57cec5SDimitry Andric     // Give Sema a chance to recover if we are in a template with dependent base
24780b57cec5SDimitry Andric     // classes.
24790b57cec5SDimitry Andric     if (ParsedType T = Actions.ActOnMSVCUnknownTypeName(
24800b57cec5SDimitry Andric             *Tok.getIdentifierInfo(), Tok.getLocation(),
24810b57cec5SDimitry Andric             DSC == DeclSpecContext::DSC_template_type_arg)) {
24820b57cec5SDimitry Andric       const char *PrevSpec;
24830b57cec5SDimitry Andric       unsigned DiagID;
24840b57cec5SDimitry Andric       DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
24850b57cec5SDimitry Andric                          Actions.getASTContext().getPrintingPolicy());
24860b57cec5SDimitry Andric       DS.SetRangeEnd(Tok.getLocation());
24870b57cec5SDimitry Andric       ConsumeToken();
24880b57cec5SDimitry Andric       return false;
24890b57cec5SDimitry Andric     }
24900b57cec5SDimitry Andric   }
24910b57cec5SDimitry Andric 
24920b57cec5SDimitry Andric   // Otherwise, if we don't consume this token, we are going to emit an
24930b57cec5SDimitry Andric   // error anyway.  Try to recover from various common problems.  Check
24940b57cec5SDimitry Andric   // to see if this was a reference to a tag name without a tag specified.
24950b57cec5SDimitry Andric   // This is a common problem in C (saying 'foo' instead of 'struct foo').
24960b57cec5SDimitry Andric   //
24970b57cec5SDimitry Andric   // C++ doesn't need this, and isTagName doesn't take SS.
24980b57cec5SDimitry Andric   if (SS == nullptr) {
24990b57cec5SDimitry Andric     const char *TagName = nullptr, *FixitTagName = nullptr;
25000b57cec5SDimitry Andric     tok::TokenKind TagKind = tok::unknown;
25010b57cec5SDimitry Andric 
25020b57cec5SDimitry Andric     switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
25030b57cec5SDimitry Andric       default: break;
25040b57cec5SDimitry Andric       case DeclSpec::TST_enum:
25050b57cec5SDimitry Andric         TagName="enum"  ; FixitTagName = "enum "  ; TagKind=tok::kw_enum ;break;
25060b57cec5SDimitry Andric       case DeclSpec::TST_union:
25070b57cec5SDimitry Andric         TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
25080b57cec5SDimitry Andric       case DeclSpec::TST_struct:
25090b57cec5SDimitry Andric         TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
25100b57cec5SDimitry Andric       case DeclSpec::TST_interface:
25110b57cec5SDimitry Andric         TagName="__interface"; FixitTagName = "__interface ";
25120b57cec5SDimitry Andric         TagKind=tok::kw___interface;break;
25130b57cec5SDimitry Andric       case DeclSpec::TST_class:
25140b57cec5SDimitry Andric         TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
25150b57cec5SDimitry Andric     }
25160b57cec5SDimitry Andric 
25170b57cec5SDimitry Andric     if (TagName) {
25180b57cec5SDimitry Andric       IdentifierInfo *TokenName = Tok.getIdentifierInfo();
25190b57cec5SDimitry Andric       LookupResult R(Actions, TokenName, SourceLocation(),
25200b57cec5SDimitry Andric                      Sema::LookupOrdinaryName);
25210b57cec5SDimitry Andric 
25220b57cec5SDimitry Andric       Diag(Loc, diag::err_use_of_tag_name_without_tag)
25230b57cec5SDimitry Andric         << TokenName << TagName << getLangOpts().CPlusPlus
25240b57cec5SDimitry Andric         << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
25250b57cec5SDimitry Andric 
25260b57cec5SDimitry Andric       if (Actions.LookupParsedName(R, getCurScope(), SS)) {
25270b57cec5SDimitry Andric         for (LookupResult::iterator I = R.begin(), IEnd = R.end();
25280b57cec5SDimitry Andric              I != IEnd; ++I)
25290b57cec5SDimitry Andric           Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
25300b57cec5SDimitry Andric             << TokenName << TagName;
25310b57cec5SDimitry Andric       }
25320b57cec5SDimitry Andric 
25330b57cec5SDimitry Andric       // Parse this as a tag as if the missing tag were present.
25340b57cec5SDimitry Andric       if (TagKind == tok::kw_enum)
25350b57cec5SDimitry Andric         ParseEnumSpecifier(Loc, DS, TemplateInfo, AS,
25360b57cec5SDimitry Andric                            DeclSpecContext::DSC_normal);
25370b57cec5SDimitry Andric       else
25380b57cec5SDimitry Andric         ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
25390b57cec5SDimitry Andric                             /*EnteringContext*/ false,
25400b57cec5SDimitry Andric                             DeclSpecContext::DSC_normal, Attrs);
25410b57cec5SDimitry Andric       return true;
25420b57cec5SDimitry Andric     }
25430b57cec5SDimitry Andric   }
25440b57cec5SDimitry Andric 
25450b57cec5SDimitry Andric   // Determine whether this identifier could plausibly be the name of something
25460b57cec5SDimitry Andric   // being declared (with a missing type).
25470b57cec5SDimitry Andric   if (!isTypeSpecifier(DSC) && (!SS || DSC == DeclSpecContext::DSC_top_level ||
25480b57cec5SDimitry Andric                                 DSC == DeclSpecContext::DSC_class)) {
25490b57cec5SDimitry Andric     // Look ahead to the next token to try to figure out what this declaration
25500b57cec5SDimitry Andric     // was supposed to be.
25510b57cec5SDimitry Andric     switch (NextToken().getKind()) {
25520b57cec5SDimitry Andric     case tok::l_paren: {
25530b57cec5SDimitry Andric       // static x(4); // 'x' is not a type
25540b57cec5SDimitry Andric       // x(int n);    // 'x' is not a type
25550b57cec5SDimitry Andric       // x (*p)[];    // 'x' is a type
25560b57cec5SDimitry Andric       //
25570b57cec5SDimitry Andric       // Since we're in an error case, we can afford to perform a tentative
25580b57cec5SDimitry Andric       // parse to determine which case we're in.
25590b57cec5SDimitry Andric       TentativeParsingAction PA(*this);
25600b57cec5SDimitry Andric       ConsumeToken();
25610b57cec5SDimitry Andric       TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
25620b57cec5SDimitry Andric       PA.Revert();
25630b57cec5SDimitry Andric 
25640b57cec5SDimitry Andric       if (TPR != TPResult::False) {
25650b57cec5SDimitry Andric         // The identifier is followed by a parenthesized declarator.
25660b57cec5SDimitry Andric         // It's supposed to be a type.
25670b57cec5SDimitry Andric         break;
25680b57cec5SDimitry Andric       }
25690b57cec5SDimitry Andric 
25700b57cec5SDimitry Andric       // If we're in a context where we could be declaring a constructor,
25710b57cec5SDimitry Andric       // check whether this is a constructor declaration with a bogus name.
25720b57cec5SDimitry Andric       if (DSC == DeclSpecContext::DSC_class ||
25730b57cec5SDimitry Andric           (DSC == DeclSpecContext::DSC_top_level && SS)) {
25740b57cec5SDimitry Andric         IdentifierInfo *II = Tok.getIdentifierInfo();
25750b57cec5SDimitry Andric         if (Actions.isCurrentClassNameTypo(II, SS)) {
25760b57cec5SDimitry Andric           Diag(Loc, diag::err_constructor_bad_name)
25770b57cec5SDimitry Andric             << Tok.getIdentifierInfo() << II
25780b57cec5SDimitry Andric             << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
25790b57cec5SDimitry Andric           Tok.setIdentifierInfo(II);
25800b57cec5SDimitry Andric         }
25810b57cec5SDimitry Andric       }
25820b57cec5SDimitry Andric       // Fall through.
25830b57cec5SDimitry Andric       LLVM_FALLTHROUGH;
25840b57cec5SDimitry Andric     }
25850b57cec5SDimitry Andric     case tok::comma:
25860b57cec5SDimitry Andric     case tok::equal:
25870b57cec5SDimitry Andric     case tok::kw_asm:
25880b57cec5SDimitry Andric     case tok::l_brace:
25890b57cec5SDimitry Andric     case tok::l_square:
25900b57cec5SDimitry Andric     case tok::semi:
25910b57cec5SDimitry Andric       // This looks like a variable or function declaration. The type is
25920b57cec5SDimitry Andric       // probably missing. We're done parsing decl-specifiers.
25930b57cec5SDimitry Andric       // But only if we are not in a function prototype scope.
25940b57cec5SDimitry Andric       if (getCurScope()->isFunctionPrototypeScope())
25950b57cec5SDimitry Andric         break;
25960b57cec5SDimitry Andric       if (SS)
25970b57cec5SDimitry Andric         AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
25980b57cec5SDimitry Andric       return false;
25990b57cec5SDimitry Andric 
26000b57cec5SDimitry Andric     default:
26010b57cec5SDimitry Andric       // This is probably supposed to be a type. This includes cases like:
26020b57cec5SDimitry Andric       //   int f(itn);
2603*5ffd83dbSDimitry Andric       //   struct S { unsigned : 4; };
26040b57cec5SDimitry Andric       break;
26050b57cec5SDimitry Andric     }
26060b57cec5SDimitry Andric   }
26070b57cec5SDimitry Andric 
26080b57cec5SDimitry Andric   // This is almost certainly an invalid type name. Let Sema emit a diagnostic
26090b57cec5SDimitry Andric   // and attempt to recover.
26100b57cec5SDimitry Andric   ParsedType T;
26110b57cec5SDimitry Andric   IdentifierInfo *II = Tok.getIdentifierInfo();
26120b57cec5SDimitry Andric   bool IsTemplateName = getLangOpts().CPlusPlus && NextToken().is(tok::less);
26130b57cec5SDimitry Andric   Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T,
26140b57cec5SDimitry Andric                                   IsTemplateName);
26150b57cec5SDimitry Andric   if (T) {
26160b57cec5SDimitry Andric     // The action has suggested that the type T could be used. Set that as
26170b57cec5SDimitry Andric     // the type in the declaration specifiers, consume the would-be type
26180b57cec5SDimitry Andric     // name token, and we're done.
26190b57cec5SDimitry Andric     const char *PrevSpec;
26200b57cec5SDimitry Andric     unsigned DiagID;
26210b57cec5SDimitry Andric     DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
26220b57cec5SDimitry Andric                        Actions.getASTContext().getPrintingPolicy());
26230b57cec5SDimitry Andric     DS.SetRangeEnd(Tok.getLocation());
26240b57cec5SDimitry Andric     ConsumeToken();
26250b57cec5SDimitry Andric     // There may be other declaration specifiers after this.
26260b57cec5SDimitry Andric     return true;
26270b57cec5SDimitry Andric   } else if (II != Tok.getIdentifierInfo()) {
26280b57cec5SDimitry Andric     // If no type was suggested, the correction is to a keyword
26290b57cec5SDimitry Andric     Tok.setKind(II->getTokenID());
26300b57cec5SDimitry Andric     // There may be other declaration specifiers after this.
26310b57cec5SDimitry Andric     return true;
26320b57cec5SDimitry Andric   }
26330b57cec5SDimitry Andric 
26340b57cec5SDimitry Andric   // Otherwise, the action had no suggestion for us.  Mark this as an error.
26350b57cec5SDimitry Andric   DS.SetTypeSpecError();
26360b57cec5SDimitry Andric   DS.SetRangeEnd(Tok.getLocation());
26370b57cec5SDimitry Andric   ConsumeToken();
26380b57cec5SDimitry Andric 
26390b57cec5SDimitry Andric   // Eat any following template arguments.
26400b57cec5SDimitry Andric   if (IsTemplateName) {
26410b57cec5SDimitry Andric     SourceLocation LAngle, RAngle;
26420b57cec5SDimitry Andric     TemplateArgList Args;
26430b57cec5SDimitry Andric     ParseTemplateIdAfterTemplateName(true, LAngle, Args, RAngle);
26440b57cec5SDimitry Andric   }
26450b57cec5SDimitry Andric 
26460b57cec5SDimitry Andric   // TODO: Could inject an invalid typedef decl in an enclosing scope to
26470b57cec5SDimitry Andric   // avoid rippling error messages on subsequent uses of the same type,
26480b57cec5SDimitry Andric   // could be useful if #include was forgotten.
26490b57cec5SDimitry Andric   return true;
26500b57cec5SDimitry Andric }
26510b57cec5SDimitry Andric 
26520b57cec5SDimitry Andric /// Determine the declaration specifier context from the declarator
26530b57cec5SDimitry Andric /// context.
26540b57cec5SDimitry Andric ///
26550b57cec5SDimitry Andric /// \param Context the declarator context, which is one of the
26560b57cec5SDimitry Andric /// DeclaratorContext enumerator values.
26570b57cec5SDimitry Andric Parser::DeclSpecContext
26580b57cec5SDimitry Andric Parser::getDeclSpecContextFromDeclaratorContext(DeclaratorContext Context) {
26590b57cec5SDimitry Andric   if (Context == DeclaratorContext::MemberContext)
26600b57cec5SDimitry Andric     return DeclSpecContext::DSC_class;
26610b57cec5SDimitry Andric   if (Context == DeclaratorContext::FileContext)
26620b57cec5SDimitry Andric     return DeclSpecContext::DSC_top_level;
26630b57cec5SDimitry Andric   if (Context == DeclaratorContext::TemplateParamContext)
26640b57cec5SDimitry Andric     return DeclSpecContext::DSC_template_param;
26650b57cec5SDimitry Andric   if (Context == DeclaratorContext::TemplateArgContext ||
26660b57cec5SDimitry Andric       Context == DeclaratorContext::TemplateTypeArgContext)
26670b57cec5SDimitry Andric     return DeclSpecContext::DSC_template_type_arg;
26680b57cec5SDimitry Andric   if (Context == DeclaratorContext::TrailingReturnContext ||
26690b57cec5SDimitry Andric       Context == DeclaratorContext::TrailingReturnVarContext)
26700b57cec5SDimitry Andric     return DeclSpecContext::DSC_trailing;
26710b57cec5SDimitry Andric   if (Context == DeclaratorContext::AliasDeclContext ||
26720b57cec5SDimitry Andric       Context == DeclaratorContext::AliasTemplateContext)
26730b57cec5SDimitry Andric     return DeclSpecContext::DSC_alias_declaration;
26740b57cec5SDimitry Andric   return DeclSpecContext::DSC_normal;
26750b57cec5SDimitry Andric }
26760b57cec5SDimitry Andric 
26770b57cec5SDimitry Andric /// ParseAlignArgument - Parse the argument to an alignment-specifier.
26780b57cec5SDimitry Andric ///
26790b57cec5SDimitry Andric /// FIXME: Simply returns an alignof() expression if the argument is a
26800b57cec5SDimitry Andric /// type. Ideally, the type should be propagated directly into Sema.
26810b57cec5SDimitry Andric ///
26820b57cec5SDimitry Andric /// [C11]   type-id
26830b57cec5SDimitry Andric /// [C11]   constant-expression
26840b57cec5SDimitry Andric /// [C++0x] type-id ...[opt]
26850b57cec5SDimitry Andric /// [C++0x] assignment-expression ...[opt]
26860b57cec5SDimitry Andric ExprResult Parser::ParseAlignArgument(SourceLocation Start,
26870b57cec5SDimitry Andric                                       SourceLocation &EllipsisLoc) {
26880b57cec5SDimitry Andric   ExprResult ER;
26890b57cec5SDimitry Andric   if (isTypeIdInParens()) {
26900b57cec5SDimitry Andric     SourceLocation TypeLoc = Tok.getLocation();
26910b57cec5SDimitry Andric     ParsedType Ty = ParseTypeName().get();
26920b57cec5SDimitry Andric     SourceRange TypeRange(Start, Tok.getLocation());
26930b57cec5SDimitry Andric     ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
26940b57cec5SDimitry Andric                                                Ty.getAsOpaquePtr(), TypeRange);
26950b57cec5SDimitry Andric   } else
26960b57cec5SDimitry Andric     ER = ParseConstantExpression();
26970b57cec5SDimitry Andric 
26980b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus11)
26990b57cec5SDimitry Andric     TryConsumeToken(tok::ellipsis, EllipsisLoc);
27000b57cec5SDimitry Andric 
27010b57cec5SDimitry Andric   return ER;
27020b57cec5SDimitry Andric }
27030b57cec5SDimitry Andric 
27040b57cec5SDimitry Andric /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
27050b57cec5SDimitry Andric /// attribute to Attrs.
27060b57cec5SDimitry Andric ///
27070b57cec5SDimitry Andric /// alignment-specifier:
27080b57cec5SDimitry Andric /// [C11]   '_Alignas' '(' type-id ')'
27090b57cec5SDimitry Andric /// [C11]   '_Alignas' '(' constant-expression ')'
27100b57cec5SDimitry Andric /// [C++11] 'alignas' '(' type-id ...[opt] ')'
27110b57cec5SDimitry Andric /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
27120b57cec5SDimitry Andric void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
27130b57cec5SDimitry Andric                                      SourceLocation *EndLoc) {
27140b57cec5SDimitry Andric   assert(Tok.isOneOf(tok::kw_alignas, tok::kw__Alignas) &&
27150b57cec5SDimitry Andric          "Not an alignment-specifier!");
27160b57cec5SDimitry Andric 
27170b57cec5SDimitry Andric   IdentifierInfo *KWName = Tok.getIdentifierInfo();
27180b57cec5SDimitry Andric   SourceLocation KWLoc = ConsumeToken();
27190b57cec5SDimitry Andric 
27200b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
27210b57cec5SDimitry Andric   if (T.expectAndConsume())
27220b57cec5SDimitry Andric     return;
27230b57cec5SDimitry Andric 
27240b57cec5SDimitry Andric   SourceLocation EllipsisLoc;
27250b57cec5SDimitry Andric   ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
27260b57cec5SDimitry Andric   if (ArgExpr.isInvalid()) {
27270b57cec5SDimitry Andric     T.skipToEnd();
27280b57cec5SDimitry Andric     return;
27290b57cec5SDimitry Andric   }
27300b57cec5SDimitry Andric 
27310b57cec5SDimitry Andric   T.consumeClose();
27320b57cec5SDimitry Andric   if (EndLoc)
27330b57cec5SDimitry Andric     *EndLoc = T.getCloseLocation();
27340b57cec5SDimitry Andric 
27350b57cec5SDimitry Andric   ArgsVector ArgExprs;
27360b57cec5SDimitry Andric   ArgExprs.push_back(ArgExpr.get());
27370b57cec5SDimitry Andric   Attrs.addNew(KWName, KWLoc, nullptr, KWLoc, ArgExprs.data(), 1,
27380b57cec5SDimitry Andric                ParsedAttr::AS_Keyword, EllipsisLoc);
27390b57cec5SDimitry Andric }
27400b57cec5SDimitry Andric 
2741*5ffd83dbSDimitry Andric ExprResult Parser::ParseExtIntegerArgument() {
2742*5ffd83dbSDimitry Andric   assert(Tok.is(tok::kw__ExtInt) && "Not an extended int type");
2743*5ffd83dbSDimitry Andric   ConsumeToken();
2744*5ffd83dbSDimitry Andric 
2745*5ffd83dbSDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
2746*5ffd83dbSDimitry Andric   if (T.expectAndConsume())
2747*5ffd83dbSDimitry Andric     return ExprError();
2748*5ffd83dbSDimitry Andric 
2749*5ffd83dbSDimitry Andric   ExprResult ER = ParseConstantExpression();
2750*5ffd83dbSDimitry Andric   if (ER.isInvalid()) {
2751*5ffd83dbSDimitry Andric     T.skipToEnd();
2752*5ffd83dbSDimitry Andric     return ExprError();
2753*5ffd83dbSDimitry Andric   }
2754*5ffd83dbSDimitry Andric 
2755*5ffd83dbSDimitry Andric   if(T.consumeClose())
2756*5ffd83dbSDimitry Andric     return ExprError();
2757*5ffd83dbSDimitry Andric   return ER;
2758*5ffd83dbSDimitry Andric }
2759*5ffd83dbSDimitry Andric 
27600b57cec5SDimitry Andric /// Determine whether we're looking at something that might be a declarator
27610b57cec5SDimitry Andric /// in a simple-declaration. If it can't possibly be a declarator, maybe
27620b57cec5SDimitry Andric /// diagnose a missing semicolon after a prior tag definition in the decl
27630b57cec5SDimitry Andric /// specifier.
27640b57cec5SDimitry Andric ///
27650b57cec5SDimitry Andric /// \return \c true if an error occurred and this can't be any kind of
27660b57cec5SDimitry Andric /// declaration.
27670b57cec5SDimitry Andric bool
27680b57cec5SDimitry Andric Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
27690b57cec5SDimitry Andric                                               DeclSpecContext DSContext,
27700b57cec5SDimitry Andric                                               LateParsedAttrList *LateAttrs) {
27710b57cec5SDimitry Andric   assert(DS.hasTagDefinition() && "shouldn't call this");
27720b57cec5SDimitry Andric 
27730b57cec5SDimitry Andric   bool EnteringContext = (DSContext == DeclSpecContext::DSC_class ||
27740b57cec5SDimitry Andric                           DSContext == DeclSpecContext::DSC_top_level);
27750b57cec5SDimitry Andric 
27760b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus &&
27770b57cec5SDimitry Andric       Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype,
27780b57cec5SDimitry Andric                   tok::annot_template_id) &&
27790b57cec5SDimitry Andric       TryAnnotateCXXScopeToken(EnteringContext)) {
27800b57cec5SDimitry Andric     SkipMalformedDecl();
27810b57cec5SDimitry Andric     return true;
27820b57cec5SDimitry Andric   }
27830b57cec5SDimitry Andric 
27840b57cec5SDimitry Andric   bool HasScope = Tok.is(tok::annot_cxxscope);
27850b57cec5SDimitry Andric   // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
27860b57cec5SDimitry Andric   Token AfterScope = HasScope ? NextToken() : Tok;
27870b57cec5SDimitry Andric 
27880b57cec5SDimitry Andric   // Determine whether the following tokens could possibly be a
27890b57cec5SDimitry Andric   // declarator.
27900b57cec5SDimitry Andric   bool MightBeDeclarator = true;
27910b57cec5SDimitry Andric   if (Tok.isOneOf(tok::kw_typename, tok::annot_typename)) {
27920b57cec5SDimitry Andric     // A declarator-id can't start with 'typename'.
27930b57cec5SDimitry Andric     MightBeDeclarator = false;
27940b57cec5SDimitry Andric   } else if (AfterScope.is(tok::annot_template_id)) {
27950b57cec5SDimitry Andric     // If we have a type expressed as a template-id, this cannot be a
27960b57cec5SDimitry Andric     // declarator-id (such a type cannot be redeclared in a simple-declaration).
27970b57cec5SDimitry Andric     TemplateIdAnnotation *Annot =
27980b57cec5SDimitry Andric         static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
27990b57cec5SDimitry Andric     if (Annot->Kind == TNK_Type_template)
28000b57cec5SDimitry Andric       MightBeDeclarator = false;
28010b57cec5SDimitry Andric   } else if (AfterScope.is(tok::identifier)) {
28020b57cec5SDimitry Andric     const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
28030b57cec5SDimitry Andric 
28040b57cec5SDimitry Andric     // These tokens cannot come after the declarator-id in a
28050b57cec5SDimitry Andric     // simple-declaration, and are likely to come after a type-specifier.
28060b57cec5SDimitry Andric     if (Next.isOneOf(tok::star, tok::amp, tok::ampamp, tok::identifier,
28070b57cec5SDimitry Andric                      tok::annot_cxxscope, tok::coloncolon)) {
28080b57cec5SDimitry Andric       // Missing a semicolon.
28090b57cec5SDimitry Andric       MightBeDeclarator = false;
28100b57cec5SDimitry Andric     } else if (HasScope) {
28110b57cec5SDimitry Andric       // If the declarator-id has a scope specifier, it must redeclare a
28120b57cec5SDimitry Andric       // previously-declared entity. If that's a type (and this is not a
28130b57cec5SDimitry Andric       // typedef), that's an error.
28140b57cec5SDimitry Andric       CXXScopeSpec SS;
28150b57cec5SDimitry Andric       Actions.RestoreNestedNameSpecifierAnnotation(
28160b57cec5SDimitry Andric           Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
28170b57cec5SDimitry Andric       IdentifierInfo *Name = AfterScope.getIdentifierInfo();
28180b57cec5SDimitry Andric       Sema::NameClassification Classification = Actions.ClassifyName(
28190b57cec5SDimitry Andric           getCurScope(), SS, Name, AfterScope.getLocation(), Next,
2820a7dea167SDimitry Andric           /*CCC=*/nullptr);
28210b57cec5SDimitry Andric       switch (Classification.getKind()) {
28220b57cec5SDimitry Andric       case Sema::NC_Error:
28230b57cec5SDimitry Andric         SkipMalformedDecl();
28240b57cec5SDimitry Andric         return true;
28250b57cec5SDimitry Andric 
28260b57cec5SDimitry Andric       case Sema::NC_Keyword:
2827a7dea167SDimitry Andric         llvm_unreachable("typo correction is not possible here");
28280b57cec5SDimitry Andric 
28290b57cec5SDimitry Andric       case Sema::NC_Type:
28300b57cec5SDimitry Andric       case Sema::NC_TypeTemplate:
2831a7dea167SDimitry Andric       case Sema::NC_UndeclaredNonType:
2832a7dea167SDimitry Andric       case Sema::NC_UndeclaredTemplate:
28330b57cec5SDimitry Andric         // Not a previously-declared non-type entity.
28340b57cec5SDimitry Andric         MightBeDeclarator = false;
28350b57cec5SDimitry Andric         break;
28360b57cec5SDimitry Andric 
28370b57cec5SDimitry Andric       case Sema::NC_Unknown:
2838a7dea167SDimitry Andric       case Sema::NC_NonType:
2839a7dea167SDimitry Andric       case Sema::NC_DependentNonType:
2840a7dea167SDimitry Andric       case Sema::NC_ContextIndependentExpr:
28410b57cec5SDimitry Andric       case Sema::NC_VarTemplate:
28420b57cec5SDimitry Andric       case Sema::NC_FunctionTemplate:
284355e4f9d5SDimitry Andric       case Sema::NC_Concept:
28440b57cec5SDimitry Andric         // Might be a redeclaration of a prior entity.
28450b57cec5SDimitry Andric         break;
28460b57cec5SDimitry Andric       }
28470b57cec5SDimitry Andric     }
28480b57cec5SDimitry Andric   }
28490b57cec5SDimitry Andric 
28500b57cec5SDimitry Andric   if (MightBeDeclarator)
28510b57cec5SDimitry Andric     return false;
28520b57cec5SDimitry Andric 
28530b57cec5SDimitry Andric   const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
28540b57cec5SDimitry Andric   Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getEndLoc()),
28550b57cec5SDimitry Andric        diag::err_expected_after)
28560b57cec5SDimitry Andric       << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi;
28570b57cec5SDimitry Andric 
28580b57cec5SDimitry Andric   // Try to recover from the typo, by dropping the tag definition and parsing
28590b57cec5SDimitry Andric   // the problematic tokens as a type.
28600b57cec5SDimitry Andric   //
28610b57cec5SDimitry Andric   // FIXME: Split the DeclSpec into pieces for the standalone
28620b57cec5SDimitry Andric   // declaration and pieces for the following declaration, instead
28630b57cec5SDimitry Andric   // of assuming that all the other pieces attach to new declaration,
28640b57cec5SDimitry Andric   // and call ParsedFreeStandingDeclSpec as appropriate.
28650b57cec5SDimitry Andric   DS.ClearTypeSpecType();
28660b57cec5SDimitry Andric   ParsedTemplateInfo NotATemplate;
28670b57cec5SDimitry Andric   ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
28680b57cec5SDimitry Andric   return false;
28690b57cec5SDimitry Andric }
28700b57cec5SDimitry Andric 
28710b57cec5SDimitry Andric // Choose the apprpriate diagnostic error for why fixed point types are
28720b57cec5SDimitry Andric // disabled, set the previous specifier, and mark as invalid.
28730b57cec5SDimitry Andric static void SetupFixedPointError(const LangOptions &LangOpts,
28740b57cec5SDimitry Andric                                  const char *&PrevSpec, unsigned &DiagID,
28750b57cec5SDimitry Andric                                  bool &isInvalid) {
28760b57cec5SDimitry Andric   assert(!LangOpts.FixedPoint);
28770b57cec5SDimitry Andric   DiagID = diag::err_fixed_point_not_enabled;
28780b57cec5SDimitry Andric   PrevSpec = "";  // Not used by diagnostic
28790b57cec5SDimitry Andric   isInvalid = true;
28800b57cec5SDimitry Andric }
28810b57cec5SDimitry Andric 
28820b57cec5SDimitry Andric /// ParseDeclarationSpecifiers
28830b57cec5SDimitry Andric ///       declaration-specifiers: [C99 6.7]
28840b57cec5SDimitry Andric ///         storage-class-specifier declaration-specifiers[opt]
28850b57cec5SDimitry Andric ///         type-specifier declaration-specifiers[opt]
28860b57cec5SDimitry Andric /// [C99]   function-specifier declaration-specifiers[opt]
28870b57cec5SDimitry Andric /// [C11]   alignment-specifier declaration-specifiers[opt]
28880b57cec5SDimitry Andric /// [GNU]   attributes declaration-specifiers[opt]
28890b57cec5SDimitry Andric /// [Clang] '__module_private__' declaration-specifiers[opt]
28900b57cec5SDimitry Andric /// [ObjC1] '__kindof' declaration-specifiers[opt]
28910b57cec5SDimitry Andric ///
28920b57cec5SDimitry Andric ///       storage-class-specifier: [C99 6.7.1]
28930b57cec5SDimitry Andric ///         'typedef'
28940b57cec5SDimitry Andric ///         'extern'
28950b57cec5SDimitry Andric ///         'static'
28960b57cec5SDimitry Andric ///         'auto'
28970b57cec5SDimitry Andric ///         'register'
28980b57cec5SDimitry Andric /// [C++]   'mutable'
28990b57cec5SDimitry Andric /// [C++11] 'thread_local'
29000b57cec5SDimitry Andric /// [C11]   '_Thread_local'
29010b57cec5SDimitry Andric /// [GNU]   '__thread'
29020b57cec5SDimitry Andric ///       function-specifier: [C99 6.7.4]
29030b57cec5SDimitry Andric /// [C99]   'inline'
29040b57cec5SDimitry Andric /// [C++]   'virtual'
29050b57cec5SDimitry Andric /// [C++]   'explicit'
29060b57cec5SDimitry Andric /// [OpenCL] '__kernel'
29070b57cec5SDimitry Andric ///       'friend': [C++ dcl.friend]
29080b57cec5SDimitry Andric ///       'constexpr': [C++0x dcl.constexpr]
29090b57cec5SDimitry Andric void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
29100b57cec5SDimitry Andric                                         const ParsedTemplateInfo &TemplateInfo,
29110b57cec5SDimitry Andric                                         AccessSpecifier AS,
29120b57cec5SDimitry Andric                                         DeclSpecContext DSContext,
29130b57cec5SDimitry Andric                                         LateParsedAttrList *LateAttrs) {
29140b57cec5SDimitry Andric   if (DS.getSourceRange().isInvalid()) {
29150b57cec5SDimitry Andric     // Start the range at the current token but make the end of the range
29160b57cec5SDimitry Andric     // invalid.  This will make the entire range invalid unless we successfully
29170b57cec5SDimitry Andric     // consume a token.
29180b57cec5SDimitry Andric     DS.SetRangeStart(Tok.getLocation());
29190b57cec5SDimitry Andric     DS.SetRangeEnd(SourceLocation());
29200b57cec5SDimitry Andric   }
29210b57cec5SDimitry Andric 
29220b57cec5SDimitry Andric   bool EnteringContext = (DSContext == DeclSpecContext::DSC_class ||
29230b57cec5SDimitry Andric                           DSContext == DeclSpecContext::DSC_top_level);
29240b57cec5SDimitry Andric   bool AttrsLastTime = false;
29250b57cec5SDimitry Andric   ParsedAttributesWithRange attrs(AttrFactory);
29260b57cec5SDimitry Andric   // We use Sema's policy to get bool macros right.
29270b57cec5SDimitry Andric   PrintingPolicy Policy = Actions.getPrintingPolicy();
29280b57cec5SDimitry Andric   while (1) {
29290b57cec5SDimitry Andric     bool isInvalid = false;
29300b57cec5SDimitry Andric     bool isStorageClass = false;
29310b57cec5SDimitry Andric     const char *PrevSpec = nullptr;
29320b57cec5SDimitry Andric     unsigned DiagID = 0;
29330b57cec5SDimitry Andric 
29340b57cec5SDimitry Andric     // This value needs to be set to the location of the last token if the last
29350b57cec5SDimitry Andric     // token of the specifier is already consumed.
29360b57cec5SDimitry Andric     SourceLocation ConsumedEnd;
29370b57cec5SDimitry Andric 
29380b57cec5SDimitry Andric     // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
29390b57cec5SDimitry Andric     // implementation for VS2013 uses _Atomic as an identifier for one of the
29400b57cec5SDimitry Andric     // classes in <atomic>.
29410b57cec5SDimitry Andric     //
29420b57cec5SDimitry Andric     // A typedef declaration containing _Atomic<...> is among the places where
29430b57cec5SDimitry Andric     // the class is used.  If we are currently parsing such a declaration, treat
29440b57cec5SDimitry Andric     // the token as an identifier.
29450b57cec5SDimitry Andric     if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) &&
29460b57cec5SDimitry Andric         DS.getStorageClassSpec() == clang::DeclSpec::SCS_typedef &&
29470b57cec5SDimitry Andric         !DS.hasTypeSpecifier() && GetLookAheadToken(1).is(tok::less))
29480b57cec5SDimitry Andric       Tok.setKind(tok::identifier);
29490b57cec5SDimitry Andric 
29500b57cec5SDimitry Andric     SourceLocation Loc = Tok.getLocation();
29510b57cec5SDimitry Andric 
29520b57cec5SDimitry Andric     switch (Tok.getKind()) {
29530b57cec5SDimitry Andric     default:
29540b57cec5SDimitry Andric     DoneWithDeclSpec:
29550b57cec5SDimitry Andric       if (!AttrsLastTime)
29560b57cec5SDimitry Andric         ProhibitAttributes(attrs);
29570b57cec5SDimitry Andric       else {
29580b57cec5SDimitry Andric         // Reject C++11 attributes that appertain to decl specifiers as
29590b57cec5SDimitry Andric         // we don't support any C++11 attributes that appertain to decl
29600b57cec5SDimitry Andric         // specifiers. This also conforms to what g++ 4.8 is doing.
29610b57cec5SDimitry Andric         ProhibitCXX11Attributes(attrs, diag::err_attribute_not_type_attr);
29620b57cec5SDimitry Andric 
29630b57cec5SDimitry Andric         DS.takeAttributesFrom(attrs);
29640b57cec5SDimitry Andric       }
29650b57cec5SDimitry Andric 
29660b57cec5SDimitry Andric       // If this is not a declaration specifier token, we're done reading decl
29670b57cec5SDimitry Andric       // specifiers.  First verify that DeclSpec's are consistent.
29680b57cec5SDimitry Andric       DS.Finish(Actions, Policy);
29690b57cec5SDimitry Andric       return;
29700b57cec5SDimitry Andric 
29710b57cec5SDimitry Andric     case tok::l_square:
29720b57cec5SDimitry Andric     case tok::kw_alignas:
29730b57cec5SDimitry Andric       if (!standardAttributesAllowed() || !isCXX11AttributeSpecifier())
29740b57cec5SDimitry Andric         goto DoneWithDeclSpec;
29750b57cec5SDimitry Andric 
29760b57cec5SDimitry Andric       ProhibitAttributes(attrs);
29770b57cec5SDimitry Andric       // FIXME: It would be good to recover by accepting the attributes,
29780b57cec5SDimitry Andric       //        but attempting to do that now would cause serious
29790b57cec5SDimitry Andric       //        madness in terms of diagnostics.
29800b57cec5SDimitry Andric       attrs.clear();
29810b57cec5SDimitry Andric       attrs.Range = SourceRange();
29820b57cec5SDimitry Andric 
29830b57cec5SDimitry Andric       ParseCXX11Attributes(attrs);
29840b57cec5SDimitry Andric       AttrsLastTime = true;
29850b57cec5SDimitry Andric       continue;
29860b57cec5SDimitry Andric 
29870b57cec5SDimitry Andric     case tok::code_completion: {
29880b57cec5SDimitry Andric       Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
29890b57cec5SDimitry Andric       if (DS.hasTypeSpecifier()) {
29900b57cec5SDimitry Andric         bool AllowNonIdentifiers
29910b57cec5SDimitry Andric           = (getCurScope()->getFlags() & (Scope::ControlScope |
29920b57cec5SDimitry Andric                                           Scope::BlockScope |
29930b57cec5SDimitry Andric                                           Scope::TemplateParamScope |
29940b57cec5SDimitry Andric                                           Scope::FunctionPrototypeScope |
29950b57cec5SDimitry Andric                                           Scope::AtCatchScope)) == 0;
29960b57cec5SDimitry Andric         bool AllowNestedNameSpecifiers
29970b57cec5SDimitry Andric           = DSContext == DeclSpecContext::DSC_top_level ||
29980b57cec5SDimitry Andric             (DSContext == DeclSpecContext::DSC_class && DS.isFriendSpecified());
29990b57cec5SDimitry Andric 
30000b57cec5SDimitry Andric         Actions.CodeCompleteDeclSpec(getCurScope(), DS,
30010b57cec5SDimitry Andric                                      AllowNonIdentifiers,
30020b57cec5SDimitry Andric                                      AllowNestedNameSpecifiers);
30030b57cec5SDimitry Andric         return cutOffParsing();
30040b57cec5SDimitry Andric       }
30050b57cec5SDimitry Andric 
30060b57cec5SDimitry Andric       if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
30070b57cec5SDimitry Andric         CCC = Sema::PCC_LocalDeclarationSpecifiers;
30080b57cec5SDimitry Andric       else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
30090b57cec5SDimitry Andric         CCC = DSContext == DeclSpecContext::DSC_class ? Sema::PCC_MemberTemplate
30100b57cec5SDimitry Andric                                                       : Sema::PCC_Template;
30110b57cec5SDimitry Andric       else if (DSContext == DeclSpecContext::DSC_class)
30120b57cec5SDimitry Andric         CCC = Sema::PCC_Class;
30130b57cec5SDimitry Andric       else if (CurParsedObjCImpl)
30140b57cec5SDimitry Andric         CCC = Sema::PCC_ObjCImplementation;
30150b57cec5SDimitry Andric 
30160b57cec5SDimitry Andric       Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
30170b57cec5SDimitry Andric       return cutOffParsing();
30180b57cec5SDimitry Andric     }
30190b57cec5SDimitry Andric 
30200b57cec5SDimitry Andric     case tok::coloncolon: // ::foo::bar
30210b57cec5SDimitry Andric       // C++ scope specifier.  Annotate and loop, or bail out on error.
30220b57cec5SDimitry Andric       if (TryAnnotateCXXScopeToken(EnteringContext)) {
30230b57cec5SDimitry Andric         if (!DS.hasTypeSpecifier())
30240b57cec5SDimitry Andric           DS.SetTypeSpecError();
30250b57cec5SDimitry Andric         goto DoneWithDeclSpec;
30260b57cec5SDimitry Andric       }
30270b57cec5SDimitry Andric       if (Tok.is(tok::coloncolon)) // ::new or ::delete
30280b57cec5SDimitry Andric         goto DoneWithDeclSpec;
30290b57cec5SDimitry Andric       continue;
30300b57cec5SDimitry Andric 
30310b57cec5SDimitry Andric     case tok::annot_cxxscope: {
30320b57cec5SDimitry Andric       if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
30330b57cec5SDimitry Andric         goto DoneWithDeclSpec;
30340b57cec5SDimitry Andric 
30350b57cec5SDimitry Andric       CXXScopeSpec SS;
30360b57cec5SDimitry Andric       Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
30370b57cec5SDimitry Andric                                                    Tok.getAnnotationRange(),
30380b57cec5SDimitry Andric                                                    SS);
30390b57cec5SDimitry Andric 
30400b57cec5SDimitry Andric       // We are looking for a qualified typename.
30410b57cec5SDimitry Andric       Token Next = NextToken();
3042*5ffd83dbSDimitry Andric 
3043*5ffd83dbSDimitry Andric       TemplateIdAnnotation *TemplateId = Next.is(tok::annot_template_id)
3044*5ffd83dbSDimitry Andric                                              ? takeTemplateIdAnnotation(Next)
3045*5ffd83dbSDimitry Andric                                              : nullptr;
3046*5ffd83dbSDimitry Andric       if (TemplateId && TemplateId->hasInvalidName()) {
3047*5ffd83dbSDimitry Andric         // We found something like 'T::U<Args> x', but U is not a template.
3048*5ffd83dbSDimitry Andric         // Assume it was supposed to be a type.
3049*5ffd83dbSDimitry Andric         DS.SetTypeSpecError();
3050*5ffd83dbSDimitry Andric         ConsumeAnnotationToken();
3051*5ffd83dbSDimitry Andric         break;
3052*5ffd83dbSDimitry Andric       }
3053*5ffd83dbSDimitry Andric 
3054*5ffd83dbSDimitry Andric       if (TemplateId && TemplateId->Kind == TNK_Type_template) {
30550b57cec5SDimitry Andric         // We have a qualified template-id, e.g., N::A<int>
30560b57cec5SDimitry Andric 
30570b57cec5SDimitry Andric         // If this would be a valid constructor declaration with template
30580b57cec5SDimitry Andric         // arguments, we will reject the attempt to form an invalid type-id
30590b57cec5SDimitry Andric         // referring to the injected-class-name when we annotate the token,
30600b57cec5SDimitry Andric         // per C++ [class.qual]p2.
30610b57cec5SDimitry Andric         //
30620b57cec5SDimitry Andric         // To improve diagnostics for this case, parse the declaration as a
30630b57cec5SDimitry Andric         // constructor (and reject the extra template arguments later).
30640b57cec5SDimitry Andric         if ((DSContext == DeclSpecContext::DSC_top_level ||
30650b57cec5SDimitry Andric              DSContext == DeclSpecContext::DSC_class) &&
30660b57cec5SDimitry Andric             TemplateId->Name &&
30670b57cec5SDimitry Andric             Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS) &&
306855e4f9d5SDimitry Andric             isConstructorDeclarator(/*Unqualified=*/false)) {
30690b57cec5SDimitry Andric           // The user meant this to be an out-of-line constructor
30700b57cec5SDimitry Andric           // definition, but template arguments are not allowed
30710b57cec5SDimitry Andric           // there.  Just allow this as a constructor; we'll
30720b57cec5SDimitry Andric           // complain about it later.
30730b57cec5SDimitry Andric           goto DoneWithDeclSpec;
30740b57cec5SDimitry Andric         }
30750b57cec5SDimitry Andric 
30760b57cec5SDimitry Andric         DS.getTypeSpecScope() = SS;
30770b57cec5SDimitry Andric         ConsumeAnnotationToken(); // The C++ scope.
30780b57cec5SDimitry Andric         assert(Tok.is(tok::annot_template_id) &&
30790b57cec5SDimitry Andric                "ParseOptionalCXXScopeSpecifier not working");
308055e4f9d5SDimitry Andric         AnnotateTemplateIdTokenAsType(SS);
308155e4f9d5SDimitry Andric         continue;
308255e4f9d5SDimitry Andric       }
308355e4f9d5SDimitry Andric 
3084*5ffd83dbSDimitry Andric       if (TemplateId && TemplateId->Kind == TNK_Concept_template &&
308555e4f9d5SDimitry Andric           GetLookAheadToken(2).isOneOf(tok::kw_auto, tok::kw_decltype)) {
308655e4f9d5SDimitry Andric         DS.getTypeSpecScope() = SS;
308755e4f9d5SDimitry Andric         // This is a qualified placeholder-specifier, e.g., ::C<int> auto ...
308855e4f9d5SDimitry Andric         // Consume the scope annotation and continue to consume the template-id
308955e4f9d5SDimitry Andric         // as a placeholder-specifier.
309055e4f9d5SDimitry Andric         ConsumeAnnotationToken();
30910b57cec5SDimitry Andric         continue;
30920b57cec5SDimitry Andric       }
30930b57cec5SDimitry Andric 
30940b57cec5SDimitry Andric       if (Next.is(tok::annot_typename)) {
30950b57cec5SDimitry Andric         DS.getTypeSpecScope() = SS;
30960b57cec5SDimitry Andric         ConsumeAnnotationToken(); // The C++ scope.
3097*5ffd83dbSDimitry Andric         TypeResult T = getTypeAnnotation(Tok);
30980b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
30990b57cec5SDimitry Andric                                        Tok.getAnnotationEndLoc(),
31000b57cec5SDimitry Andric                                        PrevSpec, DiagID, T, Policy);
31010b57cec5SDimitry Andric         if (isInvalid)
31020b57cec5SDimitry Andric           break;
31030b57cec5SDimitry Andric         DS.SetRangeEnd(Tok.getAnnotationEndLoc());
31040b57cec5SDimitry Andric         ConsumeAnnotationToken(); // The typename
31050b57cec5SDimitry Andric       }
31060b57cec5SDimitry Andric 
31070b57cec5SDimitry Andric       if (Next.isNot(tok::identifier))
31080b57cec5SDimitry Andric         goto DoneWithDeclSpec;
31090b57cec5SDimitry Andric 
31100b57cec5SDimitry Andric       // Check whether this is a constructor declaration. If we're in a
31110b57cec5SDimitry Andric       // context where the identifier could be a class name, and it has the
31120b57cec5SDimitry Andric       // shape of a constructor declaration, process it as one.
31130b57cec5SDimitry Andric       if ((DSContext == DeclSpecContext::DSC_top_level ||
31140b57cec5SDimitry Andric            DSContext == DeclSpecContext::DSC_class) &&
31150b57cec5SDimitry Andric           Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
31160b57cec5SDimitry Andric                                      &SS) &&
31170b57cec5SDimitry Andric           isConstructorDeclarator(/*Unqualified*/ false))
31180b57cec5SDimitry Andric         goto DoneWithDeclSpec;
31190b57cec5SDimitry Andric 
31200b57cec5SDimitry Andric       ParsedType TypeRep =
31210b57cec5SDimitry Andric           Actions.getTypeName(*Next.getIdentifierInfo(), Next.getLocation(),
31220b57cec5SDimitry Andric                               getCurScope(), &SS, false, false, nullptr,
31230b57cec5SDimitry Andric                               /*IsCtorOrDtorName=*/false,
31240b57cec5SDimitry Andric                               /*WantNontrivialTypeSourceInfo=*/true,
31250b57cec5SDimitry Andric                               isClassTemplateDeductionContext(DSContext));
31260b57cec5SDimitry Andric 
31270b57cec5SDimitry Andric       // If the referenced identifier is not a type, then this declspec is
31280b57cec5SDimitry Andric       // erroneous: We already checked about that it has no type specifier, and
31290b57cec5SDimitry Andric       // C++ doesn't have implicit int.  Diagnose it as a typo w.r.t. to the
31300b57cec5SDimitry Andric       // typename.
31310b57cec5SDimitry Andric       if (!TypeRep) {
313255e4f9d5SDimitry Andric         if (TryAnnotateTypeConstraint())
313355e4f9d5SDimitry Andric           goto DoneWithDeclSpec;
3134*5ffd83dbSDimitry Andric         if (Tok.isNot(tok::annot_cxxscope) ||
3135*5ffd83dbSDimitry Andric             NextToken().isNot(tok::identifier))
3136aec4c088SDimitry Andric           continue;
31370b57cec5SDimitry Andric         // Eat the scope spec so the identifier is current.
31380b57cec5SDimitry Andric         ConsumeAnnotationToken();
31390b57cec5SDimitry Andric         ParsedAttributesWithRange Attrs(AttrFactory);
31400b57cec5SDimitry Andric         if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
31410b57cec5SDimitry Andric           if (!Attrs.empty()) {
31420b57cec5SDimitry Andric             AttrsLastTime = true;
31430b57cec5SDimitry Andric             attrs.takeAllFrom(Attrs);
31440b57cec5SDimitry Andric           }
31450b57cec5SDimitry Andric           continue;
31460b57cec5SDimitry Andric         }
31470b57cec5SDimitry Andric         goto DoneWithDeclSpec;
31480b57cec5SDimitry Andric       }
31490b57cec5SDimitry Andric 
31500b57cec5SDimitry Andric       DS.getTypeSpecScope() = SS;
31510b57cec5SDimitry Andric       ConsumeAnnotationToken(); // The C++ scope.
31520b57cec5SDimitry Andric 
31530b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
31540b57cec5SDimitry Andric                                      DiagID, TypeRep, Policy);
31550b57cec5SDimitry Andric       if (isInvalid)
31560b57cec5SDimitry Andric         break;
31570b57cec5SDimitry Andric 
31580b57cec5SDimitry Andric       DS.SetRangeEnd(Tok.getLocation());
31590b57cec5SDimitry Andric       ConsumeToken(); // The typename.
31600b57cec5SDimitry Andric 
31610b57cec5SDimitry Andric       continue;
31620b57cec5SDimitry Andric     }
31630b57cec5SDimitry Andric 
31640b57cec5SDimitry Andric     case tok::annot_typename: {
31650b57cec5SDimitry Andric       // If we've previously seen a tag definition, we were almost surely
31660b57cec5SDimitry Andric       // missing a semicolon after it.
31670b57cec5SDimitry Andric       if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
31680b57cec5SDimitry Andric         goto DoneWithDeclSpec;
31690b57cec5SDimitry Andric 
3170*5ffd83dbSDimitry Andric       TypeResult T = getTypeAnnotation(Tok);
31710b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
31720b57cec5SDimitry Andric                                      DiagID, T, Policy);
31730b57cec5SDimitry Andric       if (isInvalid)
31740b57cec5SDimitry Andric         break;
31750b57cec5SDimitry Andric 
31760b57cec5SDimitry Andric       DS.SetRangeEnd(Tok.getAnnotationEndLoc());
31770b57cec5SDimitry Andric       ConsumeAnnotationToken(); // The typename
31780b57cec5SDimitry Andric 
31790b57cec5SDimitry Andric       continue;
31800b57cec5SDimitry Andric     }
31810b57cec5SDimitry Andric 
31820b57cec5SDimitry Andric     case tok::kw___is_signed:
31830b57cec5SDimitry Andric       // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
31840b57cec5SDimitry Andric       // typically treats it as a trait. If we see __is_signed as it appears
31850b57cec5SDimitry Andric       // in libstdc++, e.g.,
31860b57cec5SDimitry Andric       //
31870b57cec5SDimitry Andric       //   static const bool __is_signed;
31880b57cec5SDimitry Andric       //
31890b57cec5SDimitry Andric       // then treat __is_signed as an identifier rather than as a keyword.
31900b57cec5SDimitry Andric       if (DS.getTypeSpecType() == TST_bool &&
31910b57cec5SDimitry Andric           DS.getTypeQualifiers() == DeclSpec::TQ_const &&
31920b57cec5SDimitry Andric           DS.getStorageClassSpec() == DeclSpec::SCS_static)
31930b57cec5SDimitry Andric         TryKeywordIdentFallback(true);
31940b57cec5SDimitry Andric 
31950b57cec5SDimitry Andric       // We're done with the declaration-specifiers.
31960b57cec5SDimitry Andric       goto DoneWithDeclSpec;
31970b57cec5SDimitry Andric 
31980b57cec5SDimitry Andric       // typedef-name
31990b57cec5SDimitry Andric     case tok::kw___super:
32000b57cec5SDimitry Andric     case tok::kw_decltype:
32010b57cec5SDimitry Andric     case tok::identifier: {
32020b57cec5SDimitry Andric       // This identifier can only be a typedef name if we haven't already seen
32030b57cec5SDimitry Andric       // a type-specifier.  Without this check we misparse:
32040b57cec5SDimitry Andric       //  typedef int X; struct Y { short X; };  as 'short int'.
32050b57cec5SDimitry Andric       if (DS.hasTypeSpecifier())
32060b57cec5SDimitry Andric         goto DoneWithDeclSpec;
32070b57cec5SDimitry Andric 
32080b57cec5SDimitry Andric       // If the token is an identifier named "__declspec" and Microsoft
32090b57cec5SDimitry Andric       // extensions are not enabled, it is likely that there will be cascading
32100b57cec5SDimitry Andric       // parse errors if this really is a __declspec attribute. Attempt to
32110b57cec5SDimitry Andric       // recognize that scenario and recover gracefully.
32120b57cec5SDimitry Andric       if (!getLangOpts().DeclSpecKeyword && Tok.is(tok::identifier) &&
32130b57cec5SDimitry Andric           Tok.getIdentifierInfo()->getName().equals("__declspec")) {
32140b57cec5SDimitry Andric         Diag(Loc, diag::err_ms_attributes_not_enabled);
32150b57cec5SDimitry Andric 
32160b57cec5SDimitry Andric         // The next token should be an open paren. If it is, eat the entire
32170b57cec5SDimitry Andric         // attribute declaration and continue.
32180b57cec5SDimitry Andric         if (NextToken().is(tok::l_paren)) {
32190b57cec5SDimitry Andric           // Consume the __declspec identifier.
32200b57cec5SDimitry Andric           ConsumeToken();
32210b57cec5SDimitry Andric 
32220b57cec5SDimitry Andric           // Eat the parens and everything between them.
32230b57cec5SDimitry Andric           BalancedDelimiterTracker T(*this, tok::l_paren);
32240b57cec5SDimitry Andric           if (T.consumeOpen()) {
32250b57cec5SDimitry Andric             assert(false && "Not a left paren?");
32260b57cec5SDimitry Andric             return;
32270b57cec5SDimitry Andric           }
32280b57cec5SDimitry Andric           T.skipToEnd();
32290b57cec5SDimitry Andric           continue;
32300b57cec5SDimitry Andric         }
32310b57cec5SDimitry Andric       }
32320b57cec5SDimitry Andric 
32330b57cec5SDimitry Andric       // In C++, check to see if this is a scope specifier like foo::bar::, if
32340b57cec5SDimitry Andric       // so handle it as such.  This is important for ctor parsing.
32350b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus) {
32360b57cec5SDimitry Andric         if (TryAnnotateCXXScopeToken(EnteringContext)) {
32370b57cec5SDimitry Andric           DS.SetTypeSpecError();
32380b57cec5SDimitry Andric           goto DoneWithDeclSpec;
32390b57cec5SDimitry Andric         }
32400b57cec5SDimitry Andric         if (!Tok.is(tok::identifier))
32410b57cec5SDimitry Andric           continue;
32420b57cec5SDimitry Andric       }
32430b57cec5SDimitry Andric 
32440b57cec5SDimitry Andric       // Check for need to substitute AltiVec keyword tokens.
32450b57cec5SDimitry Andric       if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
32460b57cec5SDimitry Andric         break;
32470b57cec5SDimitry Andric 
32480b57cec5SDimitry Andric       // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
32490b57cec5SDimitry Andric       //                allow the use of a typedef name as a type specifier.
32500b57cec5SDimitry Andric       if (DS.isTypeAltiVecVector())
32510b57cec5SDimitry Andric         goto DoneWithDeclSpec;
32520b57cec5SDimitry Andric 
32530b57cec5SDimitry Andric       if (DSContext == DeclSpecContext::DSC_objc_method_result &&
32540b57cec5SDimitry Andric           isObjCInstancetype()) {
32550b57cec5SDimitry Andric         ParsedType TypeRep = Actions.ActOnObjCInstanceType(Loc);
32560b57cec5SDimitry Andric         assert(TypeRep);
32570b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
32580b57cec5SDimitry Andric                                        DiagID, TypeRep, Policy);
32590b57cec5SDimitry Andric         if (isInvalid)
32600b57cec5SDimitry Andric           break;
32610b57cec5SDimitry Andric 
32620b57cec5SDimitry Andric         DS.SetRangeEnd(Loc);
32630b57cec5SDimitry Andric         ConsumeToken();
32640b57cec5SDimitry Andric         continue;
32650b57cec5SDimitry Andric       }
32660b57cec5SDimitry Andric 
32670b57cec5SDimitry Andric       // If we're in a context where the identifier could be a class name,
32680b57cec5SDimitry Andric       // check whether this is a constructor declaration.
32690b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class &&
32700b57cec5SDimitry Andric           Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
32710b57cec5SDimitry Andric           isConstructorDeclarator(/*Unqualified*/true))
32720b57cec5SDimitry Andric         goto DoneWithDeclSpec;
32730b57cec5SDimitry Andric 
32740b57cec5SDimitry Andric       ParsedType TypeRep = Actions.getTypeName(
32750b57cec5SDimitry Andric           *Tok.getIdentifierInfo(), Tok.getLocation(), getCurScope(), nullptr,
32760b57cec5SDimitry Andric           false, false, nullptr, false, false,
32770b57cec5SDimitry Andric           isClassTemplateDeductionContext(DSContext));
32780b57cec5SDimitry Andric 
32790b57cec5SDimitry Andric       // If this is not a typedef name, don't parse it as part of the declspec,
32800b57cec5SDimitry Andric       // it must be an implicit int or an error.
32810b57cec5SDimitry Andric       if (!TypeRep) {
328255e4f9d5SDimitry Andric         if (TryAnnotateTypeConstraint())
328355e4f9d5SDimitry Andric           goto DoneWithDeclSpec;
3284*5ffd83dbSDimitry Andric         if (Tok.isNot(tok::identifier))
3285aec4c088SDimitry Andric           continue;
32860b57cec5SDimitry Andric         ParsedAttributesWithRange Attrs(AttrFactory);
32870b57cec5SDimitry Andric         if (ParseImplicitInt(DS, nullptr, TemplateInfo, AS, DSContext, Attrs)) {
32880b57cec5SDimitry Andric           if (!Attrs.empty()) {
32890b57cec5SDimitry Andric             AttrsLastTime = true;
32900b57cec5SDimitry Andric             attrs.takeAllFrom(Attrs);
32910b57cec5SDimitry Andric           }
32920b57cec5SDimitry Andric           continue;
32930b57cec5SDimitry Andric         }
32940b57cec5SDimitry Andric         goto DoneWithDeclSpec;
32950b57cec5SDimitry Andric       }
32960b57cec5SDimitry Andric 
32970b57cec5SDimitry Andric       // Likewise, if this is a context where the identifier could be a template
32980b57cec5SDimitry Andric       // name, check whether this is a deduction guide declaration.
32990b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus17 &&
33000b57cec5SDimitry Andric           (DSContext == DeclSpecContext::DSC_class ||
33010b57cec5SDimitry Andric            DSContext == DeclSpecContext::DSC_top_level) &&
33020b57cec5SDimitry Andric           Actions.isDeductionGuideName(getCurScope(), *Tok.getIdentifierInfo(),
33030b57cec5SDimitry Andric                                        Tok.getLocation()) &&
33040b57cec5SDimitry Andric           isConstructorDeclarator(/*Unqualified*/ true,
33050b57cec5SDimitry Andric                                   /*DeductionGuide*/ true))
33060b57cec5SDimitry Andric         goto DoneWithDeclSpec;
33070b57cec5SDimitry Andric 
33080b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
33090b57cec5SDimitry Andric                                      DiagID, TypeRep, Policy);
33100b57cec5SDimitry Andric       if (isInvalid)
33110b57cec5SDimitry Andric         break;
33120b57cec5SDimitry Andric 
33130b57cec5SDimitry Andric       DS.SetRangeEnd(Tok.getLocation());
33140b57cec5SDimitry Andric       ConsumeToken(); // The identifier
33150b57cec5SDimitry Andric 
33160b57cec5SDimitry Andric       // Objective-C supports type arguments and protocol references
33170b57cec5SDimitry Andric       // following an Objective-C object or object pointer
33180b57cec5SDimitry Andric       // type. Handle either one of them.
33190b57cec5SDimitry Andric       if (Tok.is(tok::less) && getLangOpts().ObjC) {
33200b57cec5SDimitry Andric         SourceLocation NewEndLoc;
33210b57cec5SDimitry Andric         TypeResult NewTypeRep = parseObjCTypeArgsAndProtocolQualifiers(
33220b57cec5SDimitry Andric                                   Loc, TypeRep, /*consumeLastToken=*/true,
33230b57cec5SDimitry Andric                                   NewEndLoc);
33240b57cec5SDimitry Andric         if (NewTypeRep.isUsable()) {
33250b57cec5SDimitry Andric           DS.UpdateTypeRep(NewTypeRep.get());
33260b57cec5SDimitry Andric           DS.SetRangeEnd(NewEndLoc);
33270b57cec5SDimitry Andric         }
33280b57cec5SDimitry Andric       }
33290b57cec5SDimitry Andric 
33300b57cec5SDimitry Andric       // Need to support trailing type qualifiers (e.g. "id<p> const").
33310b57cec5SDimitry Andric       // If a type specifier follows, it will be diagnosed elsewhere.
33320b57cec5SDimitry Andric       continue;
33330b57cec5SDimitry Andric     }
33340b57cec5SDimitry Andric 
333555e4f9d5SDimitry Andric       // type-name or placeholder-specifier
33360b57cec5SDimitry Andric     case tok::annot_template_id: {
33370b57cec5SDimitry Andric       TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
3338*5ffd83dbSDimitry Andric 
3339*5ffd83dbSDimitry Andric       if (TemplateId->hasInvalidName()) {
3340*5ffd83dbSDimitry Andric         DS.SetTypeSpecError();
3341*5ffd83dbSDimitry Andric         break;
3342*5ffd83dbSDimitry Andric       }
3343*5ffd83dbSDimitry Andric 
334455e4f9d5SDimitry Andric       if (TemplateId->Kind == TNK_Concept_template) {
3345*5ffd83dbSDimitry Andric         // If we've already diagnosed that this type-constraint has invalid
3346*5ffd83dbSDimitry Andric         // arguemnts, drop it and just form 'auto' or 'decltype(auto)'.
3347*5ffd83dbSDimitry Andric         if (TemplateId->hasInvalidArgs())
3348*5ffd83dbSDimitry Andric           TemplateId = nullptr;
3349*5ffd83dbSDimitry Andric 
335055e4f9d5SDimitry Andric         if (NextToken().is(tok::identifier)) {
335155e4f9d5SDimitry Andric           Diag(Loc, diag::err_placeholder_expected_auto_or_decltype_auto)
335255e4f9d5SDimitry Andric               << FixItHint::CreateInsertion(NextToken().getLocation(), "auto");
335355e4f9d5SDimitry Andric           // Attempt to continue as if 'auto' was placed here.
335455e4f9d5SDimitry Andric           isInvalid = DS.SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID,
335555e4f9d5SDimitry Andric                                          TemplateId, Policy);
335655e4f9d5SDimitry Andric           break;
335755e4f9d5SDimitry Andric         }
335855e4f9d5SDimitry Andric         if (!NextToken().isOneOf(tok::kw_auto, tok::kw_decltype))
335955e4f9d5SDimitry Andric             goto DoneWithDeclSpec;
336055e4f9d5SDimitry Andric         ConsumeAnnotationToken();
336155e4f9d5SDimitry Andric         SourceLocation AutoLoc = Tok.getLocation();
336255e4f9d5SDimitry Andric         if (TryConsumeToken(tok::kw_decltype)) {
336355e4f9d5SDimitry Andric           BalancedDelimiterTracker Tracker(*this, tok::l_paren);
336455e4f9d5SDimitry Andric           if (Tracker.consumeOpen()) {
336555e4f9d5SDimitry Andric             // Something like `void foo(Iterator decltype i)`
336655e4f9d5SDimitry Andric             Diag(Tok, diag::err_expected) << tok::l_paren;
336755e4f9d5SDimitry Andric           } else {
336855e4f9d5SDimitry Andric             if (!TryConsumeToken(tok::kw_auto)) {
336955e4f9d5SDimitry Andric               // Something like `void foo(Iterator decltype(int) i)`
337055e4f9d5SDimitry Andric               Tracker.skipToEnd();
337155e4f9d5SDimitry Andric               Diag(Tok, diag::err_placeholder_expected_auto_or_decltype_auto)
337255e4f9d5SDimitry Andric                 << FixItHint::CreateReplacement(SourceRange(AutoLoc,
337355e4f9d5SDimitry Andric                                                             Tok.getLocation()),
337455e4f9d5SDimitry Andric                                                 "auto");
337555e4f9d5SDimitry Andric             } else {
337655e4f9d5SDimitry Andric               Tracker.consumeClose();
337755e4f9d5SDimitry Andric             }
337855e4f9d5SDimitry Andric           }
337955e4f9d5SDimitry Andric           ConsumedEnd = Tok.getLocation();
338055e4f9d5SDimitry Andric           // Even if something went wrong above, continue as if we've seen
338155e4f9d5SDimitry Andric           // `decltype(auto)`.
338255e4f9d5SDimitry Andric           isInvalid = DS.SetTypeSpecType(TST_decltype_auto, Loc, PrevSpec,
338355e4f9d5SDimitry Andric                                          DiagID, TemplateId, Policy);
338455e4f9d5SDimitry Andric         } else {
338555e4f9d5SDimitry Andric           isInvalid = DS.SetTypeSpecType(TST_auto, Loc, PrevSpec, DiagID,
338655e4f9d5SDimitry Andric                                          TemplateId, Policy);
338755e4f9d5SDimitry Andric         }
338855e4f9d5SDimitry Andric         break;
338955e4f9d5SDimitry Andric       }
339055e4f9d5SDimitry Andric 
33910b57cec5SDimitry Andric       if (TemplateId->Kind != TNK_Type_template &&
33920b57cec5SDimitry Andric           TemplateId->Kind != TNK_Undeclared_template) {
33930b57cec5SDimitry Andric         // This template-id does not refer to a type name, so we're
33940b57cec5SDimitry Andric         // done with the type-specifiers.
33950b57cec5SDimitry Andric         goto DoneWithDeclSpec;
33960b57cec5SDimitry Andric       }
33970b57cec5SDimitry Andric 
33980b57cec5SDimitry Andric       // If we're in a context where the template-id could be a
33990b57cec5SDimitry Andric       // constructor name or specialization, check whether this is a
34000b57cec5SDimitry Andric       // constructor declaration.
34010b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus && DSContext == DeclSpecContext::DSC_class &&
34020b57cec5SDimitry Andric           Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
340355e4f9d5SDimitry Andric           isConstructorDeclarator(/*Unqualified=*/true))
34040b57cec5SDimitry Andric         goto DoneWithDeclSpec;
34050b57cec5SDimitry Andric 
34060b57cec5SDimitry Andric       // Turn the template-id annotation token into a type annotation
34070b57cec5SDimitry Andric       // token, then try again to parse it as a type-specifier.
340855e4f9d5SDimitry Andric       CXXScopeSpec SS;
340955e4f9d5SDimitry Andric       AnnotateTemplateIdTokenAsType(SS);
34100b57cec5SDimitry Andric       continue;
34110b57cec5SDimitry Andric     }
34120b57cec5SDimitry Andric 
34130b57cec5SDimitry Andric     // GNU attributes support.
34140b57cec5SDimitry Andric     case tok::kw___attribute:
34150b57cec5SDimitry Andric       ParseGNUAttributes(DS.getAttributes(), nullptr, LateAttrs);
34160b57cec5SDimitry Andric       continue;
34170b57cec5SDimitry Andric 
34180b57cec5SDimitry Andric     // Microsoft declspec support.
34190b57cec5SDimitry Andric     case tok::kw___declspec:
34200b57cec5SDimitry Andric       ParseMicrosoftDeclSpecs(DS.getAttributes());
34210b57cec5SDimitry Andric       continue;
34220b57cec5SDimitry Andric 
34230b57cec5SDimitry Andric     // Microsoft single token adornments.
34240b57cec5SDimitry Andric     case tok::kw___forceinline: {
34250b57cec5SDimitry Andric       isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
34260b57cec5SDimitry Andric       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
34270b57cec5SDimitry Andric       SourceLocation AttrNameLoc = Tok.getLocation();
34280b57cec5SDimitry Andric       DS.getAttributes().addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc,
34290b57cec5SDimitry Andric                                 nullptr, 0, ParsedAttr::AS_Keyword);
34300b57cec5SDimitry Andric       break;
34310b57cec5SDimitry Andric     }
34320b57cec5SDimitry Andric 
34330b57cec5SDimitry Andric     case tok::kw___unaligned:
34340b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID,
34350b57cec5SDimitry Andric                                  getLangOpts());
34360b57cec5SDimitry Andric       break;
34370b57cec5SDimitry Andric 
34380b57cec5SDimitry Andric     case tok::kw___sptr:
34390b57cec5SDimitry Andric     case tok::kw___uptr:
34400b57cec5SDimitry Andric     case tok::kw___ptr64:
34410b57cec5SDimitry Andric     case tok::kw___ptr32:
34420b57cec5SDimitry Andric     case tok::kw___w64:
34430b57cec5SDimitry Andric     case tok::kw___cdecl:
34440b57cec5SDimitry Andric     case tok::kw___stdcall:
34450b57cec5SDimitry Andric     case tok::kw___fastcall:
34460b57cec5SDimitry Andric     case tok::kw___thiscall:
34470b57cec5SDimitry Andric     case tok::kw___regcall:
34480b57cec5SDimitry Andric     case tok::kw___vectorcall:
34490b57cec5SDimitry Andric       ParseMicrosoftTypeAttributes(DS.getAttributes());
34500b57cec5SDimitry Andric       continue;
34510b57cec5SDimitry Andric 
34520b57cec5SDimitry Andric     // Borland single token adornments.
34530b57cec5SDimitry Andric     case tok::kw___pascal:
34540b57cec5SDimitry Andric       ParseBorlandTypeAttributes(DS.getAttributes());
34550b57cec5SDimitry Andric       continue;
34560b57cec5SDimitry Andric 
34570b57cec5SDimitry Andric     // OpenCL single token adornments.
34580b57cec5SDimitry Andric     case tok::kw___kernel:
34590b57cec5SDimitry Andric       ParseOpenCLKernelAttributes(DS.getAttributes());
34600b57cec5SDimitry Andric       continue;
34610b57cec5SDimitry Andric 
34620b57cec5SDimitry Andric     // Nullability type specifiers.
34630b57cec5SDimitry Andric     case tok::kw__Nonnull:
34640b57cec5SDimitry Andric     case tok::kw__Nullable:
34650b57cec5SDimitry Andric     case tok::kw__Null_unspecified:
34660b57cec5SDimitry Andric       ParseNullabilityTypeSpecifiers(DS.getAttributes());
34670b57cec5SDimitry Andric       continue;
34680b57cec5SDimitry Andric 
34690b57cec5SDimitry Andric     // Objective-C 'kindof' types.
34700b57cec5SDimitry Andric     case tok::kw___kindof:
34710b57cec5SDimitry Andric       DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc,
34720b57cec5SDimitry Andric                                 nullptr, 0, ParsedAttr::AS_Keyword);
34730b57cec5SDimitry Andric       (void)ConsumeToken();
34740b57cec5SDimitry Andric       continue;
34750b57cec5SDimitry Andric 
34760b57cec5SDimitry Andric     // storage-class-specifier
34770b57cec5SDimitry Andric     case tok::kw_typedef:
34780b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
34790b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
34800b57cec5SDimitry Andric       isStorageClass = true;
34810b57cec5SDimitry Andric       break;
34820b57cec5SDimitry Andric     case tok::kw_extern:
34830b57cec5SDimitry Andric       if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
34840b57cec5SDimitry Andric         Diag(Tok, diag::ext_thread_before) << "extern";
34850b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
34860b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
34870b57cec5SDimitry Andric       isStorageClass = true;
34880b57cec5SDimitry Andric       break;
34890b57cec5SDimitry Andric     case tok::kw___private_extern__:
34900b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
34910b57cec5SDimitry Andric                                          Loc, PrevSpec, DiagID, Policy);
34920b57cec5SDimitry Andric       isStorageClass = true;
34930b57cec5SDimitry Andric       break;
34940b57cec5SDimitry Andric     case tok::kw_static:
34950b57cec5SDimitry Andric       if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
34960b57cec5SDimitry Andric         Diag(Tok, diag::ext_thread_before) << "static";
34970b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
34980b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
34990b57cec5SDimitry Andric       isStorageClass = true;
35000b57cec5SDimitry Andric       break;
35010b57cec5SDimitry Andric     case tok::kw_auto:
35020b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus11) {
35030b57cec5SDimitry Andric         if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
35040b57cec5SDimitry Andric           isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
35050b57cec5SDimitry Andric                                              PrevSpec, DiagID, Policy);
35060b57cec5SDimitry Andric           if (!isInvalid)
35070b57cec5SDimitry Andric             Diag(Tok, diag::ext_auto_storage_class)
35080b57cec5SDimitry Andric               << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
35090b57cec5SDimitry Andric         } else
35100b57cec5SDimitry Andric           isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
35110b57cec5SDimitry Andric                                          DiagID, Policy);
35120b57cec5SDimitry Andric       } else
35130b57cec5SDimitry Andric         isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
35140b57cec5SDimitry Andric                                            PrevSpec, DiagID, Policy);
35150b57cec5SDimitry Andric       isStorageClass = true;
35160b57cec5SDimitry Andric       break;
35170b57cec5SDimitry Andric     case tok::kw___auto_type:
35180b57cec5SDimitry Andric       Diag(Tok, diag::ext_auto_type);
35190b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto_type, Loc, PrevSpec,
35200b57cec5SDimitry Andric                                      DiagID, Policy);
35210b57cec5SDimitry Andric       break;
35220b57cec5SDimitry Andric     case tok::kw_register:
35230b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
35240b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
35250b57cec5SDimitry Andric       isStorageClass = true;
35260b57cec5SDimitry Andric       break;
35270b57cec5SDimitry Andric     case tok::kw_mutable:
35280b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
35290b57cec5SDimitry Andric                                          PrevSpec, DiagID, Policy);
35300b57cec5SDimitry Andric       isStorageClass = true;
35310b57cec5SDimitry Andric       break;
35320b57cec5SDimitry Andric     case tok::kw___thread:
35330b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
35340b57cec5SDimitry Andric                                                PrevSpec, DiagID);
35350b57cec5SDimitry Andric       isStorageClass = true;
35360b57cec5SDimitry Andric       break;
35370b57cec5SDimitry Andric     case tok::kw_thread_local:
35380b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
35390b57cec5SDimitry Andric                                                PrevSpec, DiagID);
35400b57cec5SDimitry Andric       isStorageClass = true;
35410b57cec5SDimitry Andric       break;
35420b57cec5SDimitry Andric     case tok::kw__Thread_local:
3543a7dea167SDimitry Andric       if (!getLangOpts().C11)
3544a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
35450b57cec5SDimitry Andric       isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
35460b57cec5SDimitry Andric                                                Loc, PrevSpec, DiagID);
35470b57cec5SDimitry Andric       isStorageClass = true;
35480b57cec5SDimitry Andric       break;
35490b57cec5SDimitry Andric 
35500b57cec5SDimitry Andric     // function-specifier
35510b57cec5SDimitry Andric     case tok::kw_inline:
35520b57cec5SDimitry Andric       isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
35530b57cec5SDimitry Andric       break;
35540b57cec5SDimitry Andric     case tok::kw_virtual:
35550b57cec5SDimitry Andric       // C++ for OpenCL does not allow virtual function qualifier, to avoid
35560b57cec5SDimitry Andric       // function pointers restricted in OpenCL v2.0 s6.9.a.
35570b57cec5SDimitry Andric       if (getLangOpts().OpenCLCPlusPlus) {
35580b57cec5SDimitry Andric         DiagID = diag::err_openclcxx_virtual_function;
35590b57cec5SDimitry Andric         PrevSpec = Tok.getIdentifierInfo()->getNameStart();
35600b57cec5SDimitry Andric         isInvalid = true;
35610b57cec5SDimitry Andric       }
35620b57cec5SDimitry Andric       else {
35630b57cec5SDimitry Andric         isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
35640b57cec5SDimitry Andric       }
35650b57cec5SDimitry Andric       break;
35660b57cec5SDimitry Andric     case tok::kw_explicit: {
35670b57cec5SDimitry Andric       SourceLocation ExplicitLoc = Loc;
35680b57cec5SDimitry Andric       SourceLocation CloseParenLoc;
35690b57cec5SDimitry Andric       ExplicitSpecifier ExplicitSpec(nullptr, ExplicitSpecKind::ResolvedTrue);
35700b57cec5SDimitry Andric       ConsumedEnd = ExplicitLoc;
35710b57cec5SDimitry Andric       ConsumeToken(); // kw_explicit
35720b57cec5SDimitry Andric       if (Tok.is(tok::l_paren)) {
3573*5ffd83dbSDimitry Andric         if (getLangOpts().CPlusPlus20 || isExplicitBool() == TPResult::True) {
3574*5ffd83dbSDimitry Andric           Diag(Tok.getLocation(), getLangOpts().CPlusPlus20
357555e4f9d5SDimitry Andric                                       ? diag::warn_cxx17_compat_explicit_bool
357655e4f9d5SDimitry Andric                                       : diag::ext_explicit_bool);
357755e4f9d5SDimitry Andric 
35780b57cec5SDimitry Andric           ExprResult ExplicitExpr(static_cast<Expr *>(nullptr));
35790b57cec5SDimitry Andric           BalancedDelimiterTracker Tracker(*this, tok::l_paren);
35800b57cec5SDimitry Andric           Tracker.consumeOpen();
35810b57cec5SDimitry Andric           ExplicitExpr = ParseConstantExpression();
35820b57cec5SDimitry Andric           ConsumedEnd = Tok.getLocation();
35830b57cec5SDimitry Andric           if (ExplicitExpr.isUsable()) {
35840b57cec5SDimitry Andric             CloseParenLoc = Tok.getLocation();
35850b57cec5SDimitry Andric             Tracker.consumeClose();
35860b57cec5SDimitry Andric             ExplicitSpec =
35870b57cec5SDimitry Andric                 Actions.ActOnExplicitBoolSpecifier(ExplicitExpr.get());
35880b57cec5SDimitry Andric           } else
35890b57cec5SDimitry Andric             Tracker.skipToEnd();
359055e4f9d5SDimitry Andric         } else {
3591*5ffd83dbSDimitry Andric           Diag(Tok.getLocation(), diag::warn_cxx20_compat_explicit_bool);
35920b57cec5SDimitry Andric         }
359355e4f9d5SDimitry Andric       }
35940b57cec5SDimitry Andric       isInvalid = DS.setFunctionSpecExplicit(ExplicitLoc, PrevSpec, DiagID,
35950b57cec5SDimitry Andric                                              ExplicitSpec, CloseParenLoc);
35960b57cec5SDimitry Andric       break;
35970b57cec5SDimitry Andric     }
35980b57cec5SDimitry Andric     case tok::kw__Noreturn:
35990b57cec5SDimitry Andric       if (!getLangOpts().C11)
3600a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
36010b57cec5SDimitry Andric       isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
36020b57cec5SDimitry Andric       break;
36030b57cec5SDimitry Andric 
36040b57cec5SDimitry Andric     // alignment-specifier
36050b57cec5SDimitry Andric     case tok::kw__Alignas:
36060b57cec5SDimitry Andric       if (!getLangOpts().C11)
3607a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
36080b57cec5SDimitry Andric       ParseAlignmentSpecifier(DS.getAttributes());
36090b57cec5SDimitry Andric       continue;
36100b57cec5SDimitry Andric 
36110b57cec5SDimitry Andric     // friend
36120b57cec5SDimitry Andric     case tok::kw_friend:
36130b57cec5SDimitry Andric       if (DSContext == DeclSpecContext::DSC_class)
36140b57cec5SDimitry Andric         isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
36150b57cec5SDimitry Andric       else {
36160b57cec5SDimitry Andric         PrevSpec = ""; // not actually used by the diagnostic
36170b57cec5SDimitry Andric         DiagID = diag::err_friend_invalid_in_context;
36180b57cec5SDimitry Andric         isInvalid = true;
36190b57cec5SDimitry Andric       }
36200b57cec5SDimitry Andric       break;
36210b57cec5SDimitry Andric 
36220b57cec5SDimitry Andric     // Modules
36230b57cec5SDimitry Andric     case tok::kw___module_private__:
36240b57cec5SDimitry Andric       isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
36250b57cec5SDimitry Andric       break;
36260b57cec5SDimitry Andric 
3627a7dea167SDimitry Andric     // constexpr, consteval, constinit specifiers
36280b57cec5SDimitry Andric     case tok::kw_constexpr:
36290b57cec5SDimitry Andric       isInvalid = DS.SetConstexprSpec(CSK_constexpr, Loc, PrevSpec, DiagID);
36300b57cec5SDimitry Andric       break;
36310b57cec5SDimitry Andric     case tok::kw_consteval:
36320b57cec5SDimitry Andric       isInvalid = DS.SetConstexprSpec(CSK_consteval, Loc, PrevSpec, DiagID);
36330b57cec5SDimitry Andric       break;
3634a7dea167SDimitry Andric     case tok::kw_constinit:
3635a7dea167SDimitry Andric       isInvalid = DS.SetConstexprSpec(CSK_constinit, Loc, PrevSpec, DiagID);
3636a7dea167SDimitry Andric       break;
36370b57cec5SDimitry Andric 
36380b57cec5SDimitry Andric     // type-specifier
36390b57cec5SDimitry Andric     case tok::kw_short:
36400b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
36410b57cec5SDimitry Andric                                       DiagID, Policy);
36420b57cec5SDimitry Andric       break;
36430b57cec5SDimitry Andric     case tok::kw_long:
36440b57cec5SDimitry Andric       if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
36450b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
36460b57cec5SDimitry Andric                                         DiagID, Policy);
36470b57cec5SDimitry Andric       else
36480b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
36490b57cec5SDimitry Andric                                         DiagID, Policy);
36500b57cec5SDimitry Andric       break;
36510b57cec5SDimitry Andric     case tok::kw___int64:
36520b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
36530b57cec5SDimitry Andric                                         DiagID, Policy);
36540b57cec5SDimitry Andric       break;
36550b57cec5SDimitry Andric     case tok::kw_signed:
36560b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
36570b57cec5SDimitry Andric                                      DiagID);
36580b57cec5SDimitry Andric       break;
36590b57cec5SDimitry Andric     case tok::kw_unsigned:
36600b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
36610b57cec5SDimitry Andric                                      DiagID);
36620b57cec5SDimitry Andric       break;
36630b57cec5SDimitry Andric     case tok::kw__Complex:
3664a7dea167SDimitry Andric       if (!getLangOpts().C99)
3665a7dea167SDimitry Andric         Diag(Tok, diag::ext_c99_feature) << Tok.getName();
36660b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
36670b57cec5SDimitry Andric                                         DiagID);
36680b57cec5SDimitry Andric       break;
36690b57cec5SDimitry Andric     case tok::kw__Imaginary:
3670a7dea167SDimitry Andric       if (!getLangOpts().C99)
3671a7dea167SDimitry Andric         Diag(Tok, diag::ext_c99_feature) << Tok.getName();
36720b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
36730b57cec5SDimitry Andric                                         DiagID);
36740b57cec5SDimitry Andric       break;
36750b57cec5SDimitry Andric     case tok::kw_void:
36760b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
36770b57cec5SDimitry Andric                                      DiagID, Policy);
36780b57cec5SDimitry Andric       break;
36790b57cec5SDimitry Andric     case tok::kw_char:
36800b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
36810b57cec5SDimitry Andric                                      DiagID, Policy);
36820b57cec5SDimitry Andric       break;
36830b57cec5SDimitry Andric     case tok::kw_int:
36840b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
36850b57cec5SDimitry Andric                                      DiagID, Policy);
36860b57cec5SDimitry Andric       break;
3687*5ffd83dbSDimitry Andric     case tok::kw__ExtInt: {
3688*5ffd83dbSDimitry Andric       ExprResult ER = ParseExtIntegerArgument();
3689*5ffd83dbSDimitry Andric       if (ER.isInvalid())
3690*5ffd83dbSDimitry Andric         continue;
3691*5ffd83dbSDimitry Andric       isInvalid = DS.SetExtIntType(Loc, ER.get(), PrevSpec, DiagID, Policy);
3692*5ffd83dbSDimitry Andric       ConsumedEnd = PrevTokLocation;
3693*5ffd83dbSDimitry Andric       break;
3694*5ffd83dbSDimitry Andric     }
36950b57cec5SDimitry Andric     case tok::kw___int128:
36960b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
36970b57cec5SDimitry Andric                                      DiagID, Policy);
36980b57cec5SDimitry Andric       break;
36990b57cec5SDimitry Andric     case tok::kw_half:
37000b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
37010b57cec5SDimitry Andric                                      DiagID, Policy);
37020b57cec5SDimitry Andric       break;
3703*5ffd83dbSDimitry Andric     case tok::kw___bf16:
3704*5ffd83dbSDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_BFloat16, Loc, PrevSpec,
3705*5ffd83dbSDimitry Andric                                      DiagID, Policy);
3706*5ffd83dbSDimitry Andric       break;
37070b57cec5SDimitry Andric     case tok::kw_float:
37080b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
37090b57cec5SDimitry Andric                                      DiagID, Policy);
37100b57cec5SDimitry Andric       break;
37110b57cec5SDimitry Andric     case tok::kw_double:
37120b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
37130b57cec5SDimitry Andric                                      DiagID, Policy);
37140b57cec5SDimitry Andric       break;
37150b57cec5SDimitry Andric     case tok::kw__Float16:
37160b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float16, Loc, PrevSpec,
37170b57cec5SDimitry Andric                                      DiagID, Policy);
37180b57cec5SDimitry Andric       break;
37190b57cec5SDimitry Andric     case tok::kw__Accum:
37200b57cec5SDimitry Andric       if (!getLangOpts().FixedPoint) {
37210b57cec5SDimitry Andric         SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
37220b57cec5SDimitry Andric       } else {
37230b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_accum, Loc, PrevSpec,
37240b57cec5SDimitry Andric                                        DiagID, Policy);
37250b57cec5SDimitry Andric       }
37260b57cec5SDimitry Andric       break;
37270b57cec5SDimitry Andric     case tok::kw__Fract:
37280b57cec5SDimitry Andric       if (!getLangOpts().FixedPoint) {
37290b57cec5SDimitry Andric         SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
37300b57cec5SDimitry Andric       } else {
37310b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_fract, Loc, PrevSpec,
37320b57cec5SDimitry Andric                                        DiagID, Policy);
37330b57cec5SDimitry Andric       }
37340b57cec5SDimitry Andric       break;
37350b57cec5SDimitry Andric     case tok::kw__Sat:
37360b57cec5SDimitry Andric       if (!getLangOpts().FixedPoint) {
37370b57cec5SDimitry Andric         SetupFixedPointError(getLangOpts(), PrevSpec, DiagID, isInvalid);
37380b57cec5SDimitry Andric       } else {
37390b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecSat(Loc, PrevSpec, DiagID);
37400b57cec5SDimitry Andric       }
37410b57cec5SDimitry Andric       break;
37420b57cec5SDimitry Andric     case tok::kw___float128:
37430b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float128, Loc, PrevSpec,
37440b57cec5SDimitry Andric                                      DiagID, Policy);
37450b57cec5SDimitry Andric       break;
37460b57cec5SDimitry Andric     case tok::kw_wchar_t:
37470b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
37480b57cec5SDimitry Andric                                      DiagID, Policy);
37490b57cec5SDimitry Andric       break;
37500b57cec5SDimitry Andric     case tok::kw_char8_t:
37510b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char8, Loc, PrevSpec,
37520b57cec5SDimitry Andric                                      DiagID, Policy);
37530b57cec5SDimitry Andric       break;
37540b57cec5SDimitry Andric     case tok::kw_char16_t:
37550b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
37560b57cec5SDimitry Andric                                      DiagID, Policy);
37570b57cec5SDimitry Andric       break;
37580b57cec5SDimitry Andric     case tok::kw_char32_t:
37590b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
37600b57cec5SDimitry Andric                                      DiagID, Policy);
37610b57cec5SDimitry Andric       break;
37620b57cec5SDimitry Andric     case tok::kw_bool:
37630b57cec5SDimitry Andric     case tok::kw__Bool:
3764a7dea167SDimitry Andric       if (Tok.is(tok::kw__Bool) && !getLangOpts().C99)
3765a7dea167SDimitry Andric         Diag(Tok, diag::ext_c99_feature) << Tok.getName();
3766a7dea167SDimitry Andric 
37670b57cec5SDimitry Andric       if (Tok.is(tok::kw_bool) &&
37680b57cec5SDimitry Andric           DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
37690b57cec5SDimitry Andric           DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
37700b57cec5SDimitry Andric         PrevSpec = ""; // Not used by the diagnostic.
37710b57cec5SDimitry Andric         DiagID = diag::err_bool_redeclaration;
37720b57cec5SDimitry Andric         // For better error recovery.
37730b57cec5SDimitry Andric         Tok.setKind(tok::identifier);
37740b57cec5SDimitry Andric         isInvalid = true;
37750b57cec5SDimitry Andric       } else {
37760b57cec5SDimitry Andric         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
37770b57cec5SDimitry Andric                                        DiagID, Policy);
37780b57cec5SDimitry Andric       }
37790b57cec5SDimitry Andric       break;
37800b57cec5SDimitry Andric     case tok::kw__Decimal32:
37810b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
37820b57cec5SDimitry Andric                                      DiagID, Policy);
37830b57cec5SDimitry Andric       break;
37840b57cec5SDimitry Andric     case tok::kw__Decimal64:
37850b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
37860b57cec5SDimitry Andric                                      DiagID, Policy);
37870b57cec5SDimitry Andric       break;
37880b57cec5SDimitry Andric     case tok::kw__Decimal128:
37890b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
37900b57cec5SDimitry Andric                                      DiagID, Policy);
37910b57cec5SDimitry Andric       break;
37920b57cec5SDimitry Andric     case tok::kw___vector:
37930b57cec5SDimitry Andric       isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
37940b57cec5SDimitry Andric       break;
37950b57cec5SDimitry Andric     case tok::kw___pixel:
37960b57cec5SDimitry Andric       isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
37970b57cec5SDimitry Andric       break;
37980b57cec5SDimitry Andric     case tok::kw___bool:
37990b57cec5SDimitry Andric       isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
38000b57cec5SDimitry Andric       break;
38010b57cec5SDimitry Andric     case tok::kw_pipe:
38020b57cec5SDimitry Andric       if (!getLangOpts().OpenCL || (getLangOpts().OpenCLVersion < 200 &&
38030b57cec5SDimitry Andric                                     !getLangOpts().OpenCLCPlusPlus)) {
38040b57cec5SDimitry Andric         // OpenCL 2.0 defined this keyword. OpenCL 1.2 and earlier should
38050b57cec5SDimitry Andric         // support the "pipe" word as identifier.
38060b57cec5SDimitry Andric         Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
38070b57cec5SDimitry Andric         goto DoneWithDeclSpec;
38080b57cec5SDimitry Andric       }
38090b57cec5SDimitry Andric       isInvalid = DS.SetTypePipe(true, Loc, PrevSpec, DiagID, Policy);
38100b57cec5SDimitry Andric       break;
38110b57cec5SDimitry Andric #define GENERIC_IMAGE_TYPE(ImgType, Id) \
38120b57cec5SDimitry Andric   case tok::kw_##ImgType##_t: \
38130b57cec5SDimitry Andric     isInvalid = DS.SetTypeSpecType(DeclSpec::TST_##ImgType##_t, Loc, PrevSpec, \
38140b57cec5SDimitry Andric                                    DiagID, Policy); \
38150b57cec5SDimitry Andric     break;
38160b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def"
38170b57cec5SDimitry Andric     case tok::kw___unknown_anytype:
38180b57cec5SDimitry Andric       isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
38190b57cec5SDimitry Andric                                      PrevSpec, DiagID, Policy);
38200b57cec5SDimitry Andric       break;
38210b57cec5SDimitry Andric 
38220b57cec5SDimitry Andric     // class-specifier:
38230b57cec5SDimitry Andric     case tok::kw_class:
38240b57cec5SDimitry Andric     case tok::kw_struct:
38250b57cec5SDimitry Andric     case tok::kw___interface:
38260b57cec5SDimitry Andric     case tok::kw_union: {
38270b57cec5SDimitry Andric       tok::TokenKind Kind = Tok.getKind();
38280b57cec5SDimitry Andric       ConsumeToken();
38290b57cec5SDimitry Andric 
38300b57cec5SDimitry Andric       // These are attributes following class specifiers.
38310b57cec5SDimitry Andric       // To produce better diagnostic, we parse them when
38320b57cec5SDimitry Andric       // parsing class specifier.
38330b57cec5SDimitry Andric       ParsedAttributesWithRange Attributes(AttrFactory);
38340b57cec5SDimitry Andric       ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
38350b57cec5SDimitry Andric                           EnteringContext, DSContext, Attributes);
38360b57cec5SDimitry Andric 
38370b57cec5SDimitry Andric       // If there are attributes following class specifier,
38380b57cec5SDimitry Andric       // take them over and handle them here.
38390b57cec5SDimitry Andric       if (!Attributes.empty()) {
38400b57cec5SDimitry Andric         AttrsLastTime = true;
38410b57cec5SDimitry Andric         attrs.takeAllFrom(Attributes);
38420b57cec5SDimitry Andric       }
38430b57cec5SDimitry Andric       continue;
38440b57cec5SDimitry Andric     }
38450b57cec5SDimitry Andric 
38460b57cec5SDimitry Andric     // enum-specifier:
38470b57cec5SDimitry Andric     case tok::kw_enum:
38480b57cec5SDimitry Andric       ConsumeToken();
38490b57cec5SDimitry Andric       ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
38500b57cec5SDimitry Andric       continue;
38510b57cec5SDimitry Andric 
38520b57cec5SDimitry Andric     // cv-qualifier:
38530b57cec5SDimitry Andric     case tok::kw_const:
38540b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
38550b57cec5SDimitry Andric                                  getLangOpts());
38560b57cec5SDimitry Andric       break;
38570b57cec5SDimitry Andric     case tok::kw_volatile:
38580b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
38590b57cec5SDimitry Andric                                  getLangOpts());
38600b57cec5SDimitry Andric       break;
38610b57cec5SDimitry Andric     case tok::kw_restrict:
38620b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
38630b57cec5SDimitry Andric                                  getLangOpts());
38640b57cec5SDimitry Andric       break;
38650b57cec5SDimitry Andric 
38660b57cec5SDimitry Andric     // C++ typename-specifier:
38670b57cec5SDimitry Andric     case tok::kw_typename:
38680b57cec5SDimitry Andric       if (TryAnnotateTypeOrScopeToken()) {
38690b57cec5SDimitry Andric         DS.SetTypeSpecError();
38700b57cec5SDimitry Andric         goto DoneWithDeclSpec;
38710b57cec5SDimitry Andric       }
38720b57cec5SDimitry Andric       if (!Tok.is(tok::kw_typename))
38730b57cec5SDimitry Andric         continue;
38740b57cec5SDimitry Andric       break;
38750b57cec5SDimitry Andric 
38760b57cec5SDimitry Andric     // GNU typeof support.
38770b57cec5SDimitry Andric     case tok::kw_typeof:
38780b57cec5SDimitry Andric       ParseTypeofSpecifier(DS);
38790b57cec5SDimitry Andric       continue;
38800b57cec5SDimitry Andric 
38810b57cec5SDimitry Andric     case tok::annot_decltype:
38820b57cec5SDimitry Andric       ParseDecltypeSpecifier(DS);
38830b57cec5SDimitry Andric       continue;
38840b57cec5SDimitry Andric 
38850b57cec5SDimitry Andric     case tok::annot_pragma_pack:
38860b57cec5SDimitry Andric       HandlePragmaPack();
38870b57cec5SDimitry Andric       continue;
38880b57cec5SDimitry Andric 
38890b57cec5SDimitry Andric     case tok::annot_pragma_ms_pragma:
38900b57cec5SDimitry Andric       HandlePragmaMSPragma();
38910b57cec5SDimitry Andric       continue;
38920b57cec5SDimitry Andric 
38930b57cec5SDimitry Andric     case tok::annot_pragma_ms_vtordisp:
38940b57cec5SDimitry Andric       HandlePragmaMSVtorDisp();
38950b57cec5SDimitry Andric       continue;
38960b57cec5SDimitry Andric 
38970b57cec5SDimitry Andric     case tok::annot_pragma_ms_pointers_to_members:
38980b57cec5SDimitry Andric       HandlePragmaMSPointersToMembers();
38990b57cec5SDimitry Andric       continue;
39000b57cec5SDimitry Andric 
39010b57cec5SDimitry Andric     case tok::kw___underlying_type:
39020b57cec5SDimitry Andric       ParseUnderlyingTypeSpecifier(DS);
39030b57cec5SDimitry Andric       continue;
39040b57cec5SDimitry Andric 
39050b57cec5SDimitry Andric     case tok::kw__Atomic:
39060b57cec5SDimitry Andric       // C11 6.7.2.4/4:
39070b57cec5SDimitry Andric       //   If the _Atomic keyword is immediately followed by a left parenthesis,
39080b57cec5SDimitry Andric       //   it is interpreted as a type specifier (with a type name), not as a
39090b57cec5SDimitry Andric       //   type qualifier.
3910a7dea167SDimitry Andric       if (!getLangOpts().C11)
3911a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
3912a7dea167SDimitry Andric 
39130b57cec5SDimitry Andric       if (NextToken().is(tok::l_paren)) {
39140b57cec5SDimitry Andric         ParseAtomicSpecifier(DS);
39150b57cec5SDimitry Andric         continue;
39160b57cec5SDimitry Andric       }
39170b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
39180b57cec5SDimitry Andric                                  getLangOpts());
39190b57cec5SDimitry Andric       break;
39200b57cec5SDimitry Andric 
39210b57cec5SDimitry Andric     // OpenCL address space qualifiers:
39220b57cec5SDimitry Andric     case tok::kw___generic:
39230b57cec5SDimitry Andric       // generic address space is introduced only in OpenCL v2.0
39240b57cec5SDimitry Andric       // see OpenCL C Spec v2.0 s6.5.5
39250b57cec5SDimitry Andric       if (Actions.getLangOpts().OpenCLVersion < 200 &&
39260b57cec5SDimitry Andric           !Actions.getLangOpts().OpenCLCPlusPlus) {
39270b57cec5SDimitry Andric         DiagID = diag::err_opencl_unknown_type_specifier;
39280b57cec5SDimitry Andric         PrevSpec = Tok.getIdentifierInfo()->getNameStart();
39290b57cec5SDimitry Andric         isInvalid = true;
39300b57cec5SDimitry Andric         break;
3931480093f4SDimitry Andric       }
39320b57cec5SDimitry Andric       LLVM_FALLTHROUGH;
39330b57cec5SDimitry Andric     case tok::kw_private:
3934480093f4SDimitry Andric       // It's fine (but redundant) to check this for __generic on the
3935480093f4SDimitry Andric       // fallthrough path; we only form the __generic token in OpenCL mode.
3936480093f4SDimitry Andric       if (!getLangOpts().OpenCL)
3937480093f4SDimitry Andric         goto DoneWithDeclSpec;
3938480093f4SDimitry Andric       LLVM_FALLTHROUGH;
39390b57cec5SDimitry Andric     case tok::kw___private:
39400b57cec5SDimitry Andric     case tok::kw___global:
39410b57cec5SDimitry Andric     case tok::kw___local:
39420b57cec5SDimitry Andric     case tok::kw___constant:
39430b57cec5SDimitry Andric     // OpenCL access qualifiers:
39440b57cec5SDimitry Andric     case tok::kw___read_only:
39450b57cec5SDimitry Andric     case tok::kw___write_only:
39460b57cec5SDimitry Andric     case tok::kw___read_write:
39470b57cec5SDimitry Andric       ParseOpenCLQualifiers(DS.getAttributes());
39480b57cec5SDimitry Andric       break;
39490b57cec5SDimitry Andric 
39500b57cec5SDimitry Andric     case tok::less:
39510b57cec5SDimitry Andric       // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
39520b57cec5SDimitry Andric       // "id<SomeProtocol>".  This is hopelessly old fashioned and dangerous,
39530b57cec5SDimitry Andric       // but we support it.
39540b57cec5SDimitry Andric       if (DS.hasTypeSpecifier() || !getLangOpts().ObjC)
39550b57cec5SDimitry Andric         goto DoneWithDeclSpec;
39560b57cec5SDimitry Andric 
39570b57cec5SDimitry Andric       SourceLocation StartLoc = Tok.getLocation();
39580b57cec5SDimitry Andric       SourceLocation EndLoc;
39590b57cec5SDimitry Andric       TypeResult Type = parseObjCProtocolQualifierType(EndLoc);
39600b57cec5SDimitry Andric       if (Type.isUsable()) {
39610b57cec5SDimitry Andric         if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, StartLoc,
39620b57cec5SDimitry Andric                                PrevSpec, DiagID, Type.get(),
39630b57cec5SDimitry Andric                                Actions.getASTContext().getPrintingPolicy()))
39640b57cec5SDimitry Andric           Diag(StartLoc, DiagID) << PrevSpec;
39650b57cec5SDimitry Andric 
39660b57cec5SDimitry Andric         DS.SetRangeEnd(EndLoc);
39670b57cec5SDimitry Andric       } else {
39680b57cec5SDimitry Andric         DS.SetTypeSpecError();
39690b57cec5SDimitry Andric       }
39700b57cec5SDimitry Andric 
39710b57cec5SDimitry Andric       // Need to support trailing type qualifiers (e.g. "id<p> const").
39720b57cec5SDimitry Andric       // If a type specifier follows, it will be diagnosed elsewhere.
39730b57cec5SDimitry Andric       continue;
39740b57cec5SDimitry Andric     }
39750b57cec5SDimitry Andric 
39760b57cec5SDimitry Andric     DS.SetRangeEnd(ConsumedEnd.isValid() ? ConsumedEnd : Tok.getLocation());
39770b57cec5SDimitry Andric 
39780b57cec5SDimitry Andric     // If the specifier wasn't legal, issue a diagnostic.
39790b57cec5SDimitry Andric     if (isInvalid) {
39800b57cec5SDimitry Andric       assert(PrevSpec && "Method did not return previous specifier!");
39810b57cec5SDimitry Andric       assert(DiagID);
39820b57cec5SDimitry Andric 
39830b57cec5SDimitry Andric       if (DiagID == diag::ext_duplicate_declspec ||
39840b57cec5SDimitry Andric           DiagID == diag::ext_warn_duplicate_declspec ||
39850b57cec5SDimitry Andric           DiagID == diag::err_duplicate_declspec)
39860b57cec5SDimitry Andric         Diag(Loc, DiagID) << PrevSpec
39870b57cec5SDimitry Andric                           << FixItHint::CreateRemoval(
39880b57cec5SDimitry Andric                                  SourceRange(Loc, DS.getEndLoc()));
39890b57cec5SDimitry Andric       else if (DiagID == diag::err_opencl_unknown_type_specifier) {
39900b57cec5SDimitry Andric         Diag(Loc, DiagID) << getLangOpts().OpenCLCPlusPlus
39910b57cec5SDimitry Andric                           << getLangOpts().getOpenCLVersionTuple().getAsString()
39920b57cec5SDimitry Andric                           << PrevSpec << isStorageClass;
39930b57cec5SDimitry Andric       } else
39940b57cec5SDimitry Andric         Diag(Loc, DiagID) << PrevSpec;
39950b57cec5SDimitry Andric     }
39960b57cec5SDimitry Andric 
39970b57cec5SDimitry Andric     if (DiagID != diag::err_bool_redeclaration && ConsumedEnd.isInvalid())
39980b57cec5SDimitry Andric       // After an error the next token can be an annotation token.
39990b57cec5SDimitry Andric       ConsumeAnyToken();
40000b57cec5SDimitry Andric 
40010b57cec5SDimitry Andric     AttrsLastTime = false;
40020b57cec5SDimitry Andric   }
40030b57cec5SDimitry Andric }
40040b57cec5SDimitry Andric 
40050b57cec5SDimitry Andric /// ParseStructDeclaration - Parse a struct declaration without the terminating
40060b57cec5SDimitry Andric /// semicolon.
40070b57cec5SDimitry Andric ///
40080b57cec5SDimitry Andric /// Note that a struct declaration refers to a declaration in a struct,
40090b57cec5SDimitry Andric /// not to the declaration of a struct.
40100b57cec5SDimitry Andric ///
40110b57cec5SDimitry Andric ///       struct-declaration:
40120b57cec5SDimitry Andric /// [C2x]   attributes-specifier-seq[opt]
40130b57cec5SDimitry Andric ///           specifier-qualifier-list struct-declarator-list
40140b57cec5SDimitry Andric /// [GNU]   __extension__ struct-declaration
40150b57cec5SDimitry Andric /// [GNU]   specifier-qualifier-list
40160b57cec5SDimitry Andric ///       struct-declarator-list:
40170b57cec5SDimitry Andric ///         struct-declarator
40180b57cec5SDimitry Andric ///         struct-declarator-list ',' struct-declarator
40190b57cec5SDimitry Andric /// [GNU]   struct-declarator-list ',' attributes[opt] struct-declarator
40200b57cec5SDimitry Andric ///       struct-declarator:
40210b57cec5SDimitry Andric ///         declarator
40220b57cec5SDimitry Andric /// [GNU]   declarator attributes[opt]
40230b57cec5SDimitry Andric ///         declarator[opt] ':' constant-expression
40240b57cec5SDimitry Andric /// [GNU]   declarator[opt] ':' constant-expression attributes[opt]
40250b57cec5SDimitry Andric ///
40260b57cec5SDimitry Andric void Parser::ParseStructDeclaration(
40270b57cec5SDimitry Andric     ParsingDeclSpec &DS,
40280b57cec5SDimitry Andric     llvm::function_ref<void(ParsingFieldDeclarator &)> FieldsCallback) {
40290b57cec5SDimitry Andric 
40300b57cec5SDimitry Andric   if (Tok.is(tok::kw___extension__)) {
40310b57cec5SDimitry Andric     // __extension__ silences extension warnings in the subexpression.
40320b57cec5SDimitry Andric     ExtensionRAIIObject O(Diags);  // Use RAII to do this.
40330b57cec5SDimitry Andric     ConsumeToken();
40340b57cec5SDimitry Andric     return ParseStructDeclaration(DS, FieldsCallback);
40350b57cec5SDimitry Andric   }
40360b57cec5SDimitry Andric 
40370b57cec5SDimitry Andric   // Parse leading attributes.
40380b57cec5SDimitry Andric   ParsedAttributesWithRange Attrs(AttrFactory);
40390b57cec5SDimitry Andric   MaybeParseCXX11Attributes(Attrs);
40400b57cec5SDimitry Andric   DS.takeAttributesFrom(Attrs);
40410b57cec5SDimitry Andric 
40420b57cec5SDimitry Andric   // Parse the common specifier-qualifiers-list piece.
40430b57cec5SDimitry Andric   ParseSpecifierQualifierList(DS);
40440b57cec5SDimitry Andric 
40450b57cec5SDimitry Andric   // If there are no declarators, this is a free-standing declaration
40460b57cec5SDimitry Andric   // specifier. Let the actions module cope with it.
40470b57cec5SDimitry Andric   if (Tok.is(tok::semi)) {
40480b57cec5SDimitry Andric     RecordDecl *AnonRecord = nullptr;
40490b57cec5SDimitry Andric     Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
40500b57cec5SDimitry Andric                                                        DS, AnonRecord);
40510b57cec5SDimitry Andric     assert(!AnonRecord && "Did not expect anonymous struct or union here");
40520b57cec5SDimitry Andric     DS.complete(TheDecl);
40530b57cec5SDimitry Andric     return;
40540b57cec5SDimitry Andric   }
40550b57cec5SDimitry Andric 
40560b57cec5SDimitry Andric   // Read struct-declarators until we find the semicolon.
40570b57cec5SDimitry Andric   bool FirstDeclarator = true;
40580b57cec5SDimitry Andric   SourceLocation CommaLoc;
40590b57cec5SDimitry Andric   while (1) {
40600b57cec5SDimitry Andric     ParsingFieldDeclarator DeclaratorInfo(*this, DS);
40610b57cec5SDimitry Andric     DeclaratorInfo.D.setCommaLoc(CommaLoc);
40620b57cec5SDimitry Andric 
40630b57cec5SDimitry Andric     // Attributes are only allowed here on successive declarators.
40640b57cec5SDimitry Andric     if (!FirstDeclarator)
40650b57cec5SDimitry Andric       MaybeParseGNUAttributes(DeclaratorInfo.D);
40660b57cec5SDimitry Andric 
40670b57cec5SDimitry Andric     /// struct-declarator: declarator
40680b57cec5SDimitry Andric     /// struct-declarator: declarator[opt] ':' constant-expression
40690b57cec5SDimitry Andric     if (Tok.isNot(tok::colon)) {
40700b57cec5SDimitry Andric       // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
40710b57cec5SDimitry Andric       ColonProtectionRAIIObject X(*this);
40720b57cec5SDimitry Andric       ParseDeclarator(DeclaratorInfo.D);
40730b57cec5SDimitry Andric     } else
40740b57cec5SDimitry Andric       DeclaratorInfo.D.SetIdentifier(nullptr, Tok.getLocation());
40750b57cec5SDimitry Andric 
40760b57cec5SDimitry Andric     if (TryConsumeToken(tok::colon)) {
40770b57cec5SDimitry Andric       ExprResult Res(ParseConstantExpression());
40780b57cec5SDimitry Andric       if (Res.isInvalid())
40790b57cec5SDimitry Andric         SkipUntil(tok::semi, StopBeforeMatch);
40800b57cec5SDimitry Andric       else
40810b57cec5SDimitry Andric         DeclaratorInfo.BitfieldSize = Res.get();
40820b57cec5SDimitry Andric     }
40830b57cec5SDimitry Andric 
40840b57cec5SDimitry Andric     // If attributes exist after the declarator, parse them.
40850b57cec5SDimitry Andric     MaybeParseGNUAttributes(DeclaratorInfo.D);
40860b57cec5SDimitry Andric 
40870b57cec5SDimitry Andric     // We're done with this declarator;  invoke the callback.
40880b57cec5SDimitry Andric     FieldsCallback(DeclaratorInfo);
40890b57cec5SDimitry Andric 
40900b57cec5SDimitry Andric     // If we don't have a comma, it is either the end of the list (a ';')
40910b57cec5SDimitry Andric     // or an error, bail out.
40920b57cec5SDimitry Andric     if (!TryConsumeToken(tok::comma, CommaLoc))
40930b57cec5SDimitry Andric       return;
40940b57cec5SDimitry Andric 
40950b57cec5SDimitry Andric     FirstDeclarator = false;
40960b57cec5SDimitry Andric   }
40970b57cec5SDimitry Andric }
40980b57cec5SDimitry Andric 
40990b57cec5SDimitry Andric /// ParseStructUnionBody
41000b57cec5SDimitry Andric ///       struct-contents:
41010b57cec5SDimitry Andric ///         struct-declaration-list
41020b57cec5SDimitry Andric /// [EXT]   empty
41030b57cec5SDimitry Andric /// [GNU]   "struct-declaration-list" without terminatoring ';'
41040b57cec5SDimitry Andric ///       struct-declaration-list:
41050b57cec5SDimitry Andric ///         struct-declaration
41060b57cec5SDimitry Andric ///         struct-declaration-list struct-declaration
41070b57cec5SDimitry Andric /// [OBC]   '@' 'defs' '(' class-name ')'
41080b57cec5SDimitry Andric ///
41090b57cec5SDimitry Andric void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
4110*5ffd83dbSDimitry Andric                                   DeclSpec::TST TagType, RecordDecl *TagDecl) {
41110b57cec5SDimitry Andric   PrettyDeclStackTraceEntry CrashInfo(Actions.Context, TagDecl, RecordLoc,
41120b57cec5SDimitry Andric                                       "parsing struct/union body");
41130b57cec5SDimitry Andric   assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
41140b57cec5SDimitry Andric 
41150b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_brace);
41160b57cec5SDimitry Andric   if (T.consumeOpen())
41170b57cec5SDimitry Andric     return;
41180b57cec5SDimitry Andric 
41190b57cec5SDimitry Andric   ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
41200b57cec5SDimitry Andric   Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
41210b57cec5SDimitry Andric 
41220b57cec5SDimitry Andric   // While we still have something to read, read the declarations in the struct.
41230b57cec5SDimitry Andric   while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
41240b57cec5SDimitry Andric          Tok.isNot(tok::eof)) {
41250b57cec5SDimitry Andric     // Each iteration of this loop reads one struct-declaration.
41260b57cec5SDimitry Andric 
41270b57cec5SDimitry Andric     // Check for extraneous top-level semicolon.
41280b57cec5SDimitry Andric     if (Tok.is(tok::semi)) {
41290b57cec5SDimitry Andric       ConsumeExtraSemi(InsideStruct, TagType);
41300b57cec5SDimitry Andric       continue;
41310b57cec5SDimitry Andric     }
41320b57cec5SDimitry Andric 
41330b57cec5SDimitry Andric     // Parse _Static_assert declaration.
41340b57cec5SDimitry Andric     if (Tok.is(tok::kw__Static_assert)) {
41350b57cec5SDimitry Andric       SourceLocation DeclEnd;
41360b57cec5SDimitry Andric       ParseStaticAssertDeclaration(DeclEnd);
41370b57cec5SDimitry Andric       continue;
41380b57cec5SDimitry Andric     }
41390b57cec5SDimitry Andric 
41400b57cec5SDimitry Andric     if (Tok.is(tok::annot_pragma_pack)) {
41410b57cec5SDimitry Andric       HandlePragmaPack();
41420b57cec5SDimitry Andric       continue;
41430b57cec5SDimitry Andric     }
41440b57cec5SDimitry Andric 
41450b57cec5SDimitry Andric     if (Tok.is(tok::annot_pragma_align)) {
41460b57cec5SDimitry Andric       HandlePragmaAlign();
41470b57cec5SDimitry Andric       continue;
41480b57cec5SDimitry Andric     }
41490b57cec5SDimitry Andric 
41500b57cec5SDimitry Andric     if (Tok.is(tok::annot_pragma_openmp)) {
41510b57cec5SDimitry Andric       // Result can be ignored, because it must be always empty.
41520b57cec5SDimitry Andric       AccessSpecifier AS = AS_none;
41530b57cec5SDimitry Andric       ParsedAttributesWithRange Attrs(AttrFactory);
41540b57cec5SDimitry Andric       (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs);
41550b57cec5SDimitry Andric       continue;
41560b57cec5SDimitry Andric     }
41570b57cec5SDimitry Andric 
4158a7dea167SDimitry Andric     if (tok::isPragmaAnnotation(Tok.getKind())) {
4159a7dea167SDimitry Andric       Diag(Tok.getLocation(), diag::err_pragma_misplaced_in_decl)
4160a7dea167SDimitry Andric           << DeclSpec::getSpecifierName(
4161a7dea167SDimitry Andric                  TagType, Actions.getASTContext().getPrintingPolicy());
4162a7dea167SDimitry Andric       ConsumeAnnotationToken();
4163a7dea167SDimitry Andric       continue;
4164a7dea167SDimitry Andric     }
4165a7dea167SDimitry Andric 
41660b57cec5SDimitry Andric     if (!Tok.is(tok::at)) {
41670b57cec5SDimitry Andric       auto CFieldCallback = [&](ParsingFieldDeclarator &FD) {
41680b57cec5SDimitry Andric         // Install the declarator into the current TagDecl.
41690b57cec5SDimitry Andric         Decl *Field =
41700b57cec5SDimitry Andric             Actions.ActOnField(getCurScope(), TagDecl,
41710b57cec5SDimitry Andric                                FD.D.getDeclSpec().getSourceRange().getBegin(),
41720b57cec5SDimitry Andric                                FD.D, FD.BitfieldSize);
41730b57cec5SDimitry Andric         FD.complete(Field);
41740b57cec5SDimitry Andric       };
41750b57cec5SDimitry Andric 
41760b57cec5SDimitry Andric       // Parse all the comma separated declarators.
41770b57cec5SDimitry Andric       ParsingDeclSpec DS(*this);
41780b57cec5SDimitry Andric       ParseStructDeclaration(DS, CFieldCallback);
41790b57cec5SDimitry Andric     } else { // Handle @defs
41800b57cec5SDimitry Andric       ConsumeToken();
41810b57cec5SDimitry Andric       if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
41820b57cec5SDimitry Andric         Diag(Tok, diag::err_unexpected_at);
41830b57cec5SDimitry Andric         SkipUntil(tok::semi);
41840b57cec5SDimitry Andric         continue;
41850b57cec5SDimitry Andric       }
41860b57cec5SDimitry Andric       ConsumeToken();
41870b57cec5SDimitry Andric       ExpectAndConsume(tok::l_paren);
41880b57cec5SDimitry Andric       if (!Tok.is(tok::identifier)) {
41890b57cec5SDimitry Andric         Diag(Tok, diag::err_expected) << tok::identifier;
41900b57cec5SDimitry Andric         SkipUntil(tok::semi);
41910b57cec5SDimitry Andric         continue;
41920b57cec5SDimitry Andric       }
41930b57cec5SDimitry Andric       SmallVector<Decl *, 16> Fields;
41940b57cec5SDimitry Andric       Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
41950b57cec5SDimitry Andric                         Tok.getIdentifierInfo(), Fields);
41960b57cec5SDimitry Andric       ConsumeToken();
41970b57cec5SDimitry Andric       ExpectAndConsume(tok::r_paren);
41980b57cec5SDimitry Andric     }
41990b57cec5SDimitry Andric 
42000b57cec5SDimitry Andric     if (TryConsumeToken(tok::semi))
42010b57cec5SDimitry Andric       continue;
42020b57cec5SDimitry Andric 
42030b57cec5SDimitry Andric     if (Tok.is(tok::r_brace)) {
42040b57cec5SDimitry Andric       ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
42050b57cec5SDimitry Andric       break;
42060b57cec5SDimitry Andric     }
42070b57cec5SDimitry Andric 
42080b57cec5SDimitry Andric     ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
42090b57cec5SDimitry Andric     // Skip to end of block or statement to avoid ext-warning on extra ';'.
42100b57cec5SDimitry Andric     SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
42110b57cec5SDimitry Andric     // If we stopped at a ';', eat it.
42120b57cec5SDimitry Andric     TryConsumeToken(tok::semi);
42130b57cec5SDimitry Andric   }
42140b57cec5SDimitry Andric 
42150b57cec5SDimitry Andric   T.consumeClose();
42160b57cec5SDimitry Andric 
42170b57cec5SDimitry Andric   ParsedAttributes attrs(AttrFactory);
42180b57cec5SDimitry Andric   // If attributes exist after struct contents, parse them.
42190b57cec5SDimitry Andric   MaybeParseGNUAttributes(attrs);
42200b57cec5SDimitry Andric 
4221*5ffd83dbSDimitry Andric   SmallVector<Decl *, 32> FieldDecls(TagDecl->field_begin(),
4222*5ffd83dbSDimitry Andric                                      TagDecl->field_end());
4223*5ffd83dbSDimitry Andric 
42240b57cec5SDimitry Andric   Actions.ActOnFields(getCurScope(), RecordLoc, TagDecl, FieldDecls,
42250b57cec5SDimitry Andric                       T.getOpenLocation(), T.getCloseLocation(), attrs);
42260b57cec5SDimitry Andric   StructScope.Exit();
42270b57cec5SDimitry Andric   Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl, T.getRange());
42280b57cec5SDimitry Andric }
42290b57cec5SDimitry Andric 
42300b57cec5SDimitry Andric /// ParseEnumSpecifier
42310b57cec5SDimitry Andric ///       enum-specifier: [C99 6.7.2.2]
42320b57cec5SDimitry Andric ///         'enum' identifier[opt] '{' enumerator-list '}'
42330b57cec5SDimitry Andric ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
42340b57cec5SDimitry Andric /// [GNU]   'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
42350b57cec5SDimitry Andric ///                                                 '}' attributes[opt]
42360b57cec5SDimitry Andric /// [MS]    'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
42370b57cec5SDimitry Andric ///                                                 '}'
42380b57cec5SDimitry Andric ///         'enum' identifier
42390b57cec5SDimitry Andric /// [GNU]   'enum' attributes[opt] identifier
42400b57cec5SDimitry Andric ///
42410b57cec5SDimitry Andric /// [C++11] enum-head '{' enumerator-list[opt] '}'
42420b57cec5SDimitry Andric /// [C++11] enum-head '{' enumerator-list ','  '}'
42430b57cec5SDimitry Andric ///
42440b57cec5SDimitry Andric ///       enum-head: [C++11]
42450b57cec5SDimitry Andric ///         enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
42460b57cec5SDimitry Andric ///         enum-key attribute-specifier-seq[opt] nested-name-specifier
42470b57cec5SDimitry Andric ///             identifier enum-base[opt]
42480b57cec5SDimitry Andric ///
42490b57cec5SDimitry Andric ///       enum-key: [C++11]
42500b57cec5SDimitry Andric ///         'enum'
42510b57cec5SDimitry Andric ///         'enum' 'class'
42520b57cec5SDimitry Andric ///         'enum' 'struct'
42530b57cec5SDimitry Andric ///
42540b57cec5SDimitry Andric ///       enum-base: [C++11]
42550b57cec5SDimitry Andric ///         ':' type-specifier-seq
42560b57cec5SDimitry Andric ///
42570b57cec5SDimitry Andric /// [C++] elaborated-type-specifier:
4258*5ffd83dbSDimitry Andric /// [C++]   'enum' nested-name-specifier[opt] identifier
42590b57cec5SDimitry Andric ///
42600b57cec5SDimitry Andric void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
42610b57cec5SDimitry Andric                                 const ParsedTemplateInfo &TemplateInfo,
42620b57cec5SDimitry Andric                                 AccessSpecifier AS, DeclSpecContext DSC) {
42630b57cec5SDimitry Andric   // Parse the tag portion of this.
42640b57cec5SDimitry Andric   if (Tok.is(tok::code_completion)) {
42650b57cec5SDimitry Andric     // Code completion for an enum name.
42660b57cec5SDimitry Andric     Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
42670b57cec5SDimitry Andric     return cutOffParsing();
42680b57cec5SDimitry Andric   }
42690b57cec5SDimitry Andric 
42700b57cec5SDimitry Andric   // If attributes exist after tag, parse them.
42710b57cec5SDimitry Andric   ParsedAttributesWithRange attrs(AttrFactory);
42720b57cec5SDimitry Andric   MaybeParseGNUAttributes(attrs);
42730b57cec5SDimitry Andric   MaybeParseCXX11Attributes(attrs);
42740b57cec5SDimitry Andric   MaybeParseMicrosoftDeclSpecs(attrs);
42750b57cec5SDimitry Andric 
42760b57cec5SDimitry Andric   SourceLocation ScopedEnumKWLoc;
42770b57cec5SDimitry Andric   bool IsScopedUsingClassTag = false;
42780b57cec5SDimitry Andric 
42790b57cec5SDimitry Andric   // In C++11, recognize 'enum class' and 'enum struct'.
42800b57cec5SDimitry Andric   if (Tok.isOneOf(tok::kw_class, tok::kw_struct)) {
42810b57cec5SDimitry Andric     Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
42820b57cec5SDimitry Andric                                         : diag::ext_scoped_enum);
42830b57cec5SDimitry Andric     IsScopedUsingClassTag = Tok.is(tok::kw_class);
42840b57cec5SDimitry Andric     ScopedEnumKWLoc = ConsumeToken();
42850b57cec5SDimitry Andric 
42860b57cec5SDimitry Andric     // Attributes are not allowed between these keywords.  Diagnose,
42870b57cec5SDimitry Andric     // but then just treat them like they appeared in the right place.
42880b57cec5SDimitry Andric     ProhibitAttributes(attrs);
42890b57cec5SDimitry Andric 
42900b57cec5SDimitry Andric     // They are allowed afterwards, though.
42910b57cec5SDimitry Andric     MaybeParseGNUAttributes(attrs);
42920b57cec5SDimitry Andric     MaybeParseCXX11Attributes(attrs);
42930b57cec5SDimitry Andric     MaybeParseMicrosoftDeclSpecs(attrs);
42940b57cec5SDimitry Andric   }
42950b57cec5SDimitry Andric 
42960b57cec5SDimitry Andric   // C++11 [temp.explicit]p12:
42970b57cec5SDimitry Andric   //   The usual access controls do not apply to names used to specify
42980b57cec5SDimitry Andric   //   explicit instantiations.
42990b57cec5SDimitry Andric   // We extend this to also cover explicit specializations.  Note that
43000b57cec5SDimitry Andric   // we don't suppress if this turns out to be an elaborated type
43010b57cec5SDimitry Andric   // specifier.
43020b57cec5SDimitry Andric   bool shouldDelayDiagsInTag =
43030b57cec5SDimitry Andric     (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
43040b57cec5SDimitry Andric      TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
43050b57cec5SDimitry Andric   SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
43060b57cec5SDimitry Andric 
4307*5ffd83dbSDimitry Andric   // Determine whether this declaration is permitted to have an enum-base.
4308*5ffd83dbSDimitry Andric   AllowDefiningTypeSpec AllowEnumSpecifier =
4309*5ffd83dbSDimitry Andric       isDefiningTypeSpecifierContext(DSC);
4310*5ffd83dbSDimitry Andric   bool CanBeOpaqueEnumDeclaration =
4311*5ffd83dbSDimitry Andric       DS.isEmpty() && isOpaqueEnumDeclarationContext(DSC);
4312*5ffd83dbSDimitry Andric   bool CanHaveEnumBase = (getLangOpts().CPlusPlus11 || getLangOpts().ObjC ||
4313*5ffd83dbSDimitry Andric                           getLangOpts().MicrosoftExt) &&
4314*5ffd83dbSDimitry Andric                          (AllowEnumSpecifier == AllowDefiningTypeSpec::Yes ||
4315*5ffd83dbSDimitry Andric                           CanBeOpaqueEnumDeclaration);
43160b57cec5SDimitry Andric 
43170b57cec5SDimitry Andric   CXXScopeSpec &SS = DS.getTypeSpecScope();
43180b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus) {
4319*5ffd83dbSDimitry Andric     // "enum foo : bar;" is not a potential typo for "enum foo::bar;".
4320*5ffd83dbSDimitry Andric     ColonProtectionRAIIObject X(*this);
43210b57cec5SDimitry Andric 
43220b57cec5SDimitry Andric     CXXScopeSpec Spec;
4323*5ffd83dbSDimitry Andric     if (ParseOptionalCXXScopeSpecifier(Spec, /*ObjectType=*/nullptr,
4324*5ffd83dbSDimitry Andric                                        /*ObjectHadErrors=*/false,
43250b57cec5SDimitry Andric                                        /*EnteringContext=*/true))
43260b57cec5SDimitry Andric       return;
43270b57cec5SDimitry Andric 
43280b57cec5SDimitry Andric     if (Spec.isSet() && Tok.isNot(tok::identifier)) {
43290b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::identifier;
43300b57cec5SDimitry Andric       if (Tok.isNot(tok::l_brace)) {
43310b57cec5SDimitry Andric         // Has no name and is not a definition.
43320b57cec5SDimitry Andric         // Skip the rest of this declarator, up until the comma or semicolon.
43330b57cec5SDimitry Andric         SkipUntil(tok::comma, StopAtSemi);
43340b57cec5SDimitry Andric         return;
43350b57cec5SDimitry Andric       }
43360b57cec5SDimitry Andric     }
43370b57cec5SDimitry Andric 
43380b57cec5SDimitry Andric     SS = Spec;
43390b57cec5SDimitry Andric   }
43400b57cec5SDimitry Andric 
4341*5ffd83dbSDimitry Andric   // Must have either 'enum name' or 'enum {...}' or (rarely) 'enum : T { ... }'.
43420b57cec5SDimitry Andric   if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
4343*5ffd83dbSDimitry Andric       Tok.isNot(tok::colon)) {
43440b57cec5SDimitry Andric     Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
43450b57cec5SDimitry Andric 
43460b57cec5SDimitry Andric     // Skip the rest of this declarator, up until the comma or semicolon.
43470b57cec5SDimitry Andric     SkipUntil(tok::comma, StopAtSemi);
43480b57cec5SDimitry Andric     return;
43490b57cec5SDimitry Andric   }
43500b57cec5SDimitry Andric 
43510b57cec5SDimitry Andric   // If an identifier is present, consume and remember it.
43520b57cec5SDimitry Andric   IdentifierInfo *Name = nullptr;
43530b57cec5SDimitry Andric   SourceLocation NameLoc;
43540b57cec5SDimitry Andric   if (Tok.is(tok::identifier)) {
43550b57cec5SDimitry Andric     Name = Tok.getIdentifierInfo();
43560b57cec5SDimitry Andric     NameLoc = ConsumeToken();
43570b57cec5SDimitry Andric   }
43580b57cec5SDimitry Andric 
43590b57cec5SDimitry Andric   if (!Name && ScopedEnumKWLoc.isValid()) {
43600b57cec5SDimitry Andric     // C++0x 7.2p2: The optional identifier shall not be omitted in the
43610b57cec5SDimitry Andric     // declaration of a scoped enumeration.
43620b57cec5SDimitry Andric     Diag(Tok, diag::err_scoped_enum_missing_identifier);
43630b57cec5SDimitry Andric     ScopedEnumKWLoc = SourceLocation();
43640b57cec5SDimitry Andric     IsScopedUsingClassTag = false;
43650b57cec5SDimitry Andric   }
43660b57cec5SDimitry Andric 
43670b57cec5SDimitry Andric   // Okay, end the suppression area.  We'll decide whether to emit the
43680b57cec5SDimitry Andric   // diagnostics in a second.
43690b57cec5SDimitry Andric   if (shouldDelayDiagsInTag)
43700b57cec5SDimitry Andric     diagsFromTag.done();
43710b57cec5SDimitry Andric 
43720b57cec5SDimitry Andric   TypeResult BaseType;
4373*5ffd83dbSDimitry Andric   SourceRange BaseRange;
4374*5ffd83dbSDimitry Andric 
4375*5ffd83dbSDimitry Andric   bool CanBeBitfield = (getCurScope()->getFlags() & Scope::ClassScope) &&
4376*5ffd83dbSDimitry Andric                        ScopedEnumKWLoc.isInvalid() && Name;
43770b57cec5SDimitry Andric 
43780b57cec5SDimitry Andric   // Parse the fixed underlying type.
4379*5ffd83dbSDimitry Andric   if (Tok.is(tok::colon)) {
4380*5ffd83dbSDimitry Andric     // This might be an enum-base or part of some unrelated enclosing context.
4381*5ffd83dbSDimitry Andric     //
4382*5ffd83dbSDimitry Andric     // 'enum E : base' is permitted in two circumstances:
4383*5ffd83dbSDimitry Andric     //
4384*5ffd83dbSDimitry Andric     // 1) As a defining-type-specifier, when followed by '{'.
4385*5ffd83dbSDimitry Andric     // 2) As the sole constituent of a complete declaration -- when DS is empty
4386*5ffd83dbSDimitry Andric     //    and the next token is ';'.
4387*5ffd83dbSDimitry Andric     //
4388*5ffd83dbSDimitry Andric     // The restriction to defining-type-specifiers is important to allow parsing
4389*5ffd83dbSDimitry Andric     //   a ? new enum E : int{}
4390*5ffd83dbSDimitry Andric     //   _Generic(a, enum E : int{})
4391*5ffd83dbSDimitry Andric     // properly.
4392*5ffd83dbSDimitry Andric     //
4393*5ffd83dbSDimitry Andric     // One additional consideration applies:
4394*5ffd83dbSDimitry Andric     //
4395*5ffd83dbSDimitry Andric     // C++ [dcl.enum]p1:
4396*5ffd83dbSDimitry Andric     //   A ':' following "enum nested-name-specifier[opt] identifier" within
4397*5ffd83dbSDimitry Andric     //   the decl-specifier-seq of a member-declaration is parsed as part of
4398*5ffd83dbSDimitry Andric     //   an enum-base.
4399*5ffd83dbSDimitry Andric     //
4400*5ffd83dbSDimitry Andric     // Other language modes supporting enumerations with fixed underlying types
4401*5ffd83dbSDimitry Andric     // do not have clear rules on this, so we disambiguate to determine whether
4402*5ffd83dbSDimitry Andric     // the tokens form a bit-field width or an enum-base.
44030b57cec5SDimitry Andric 
4404*5ffd83dbSDimitry Andric     if (CanBeBitfield && !isEnumBase(CanBeOpaqueEnumDeclaration)) {
4405*5ffd83dbSDimitry Andric       // Outside C++11, do not interpret the tokens as an enum-base if they do
4406*5ffd83dbSDimitry Andric       // not make sense as one. In C++11, it's an error if this happens.
4407*5ffd83dbSDimitry Andric       if (getLangOpts().CPlusPlus11)
4408*5ffd83dbSDimitry Andric         Diag(Tok.getLocation(), diag::err_anonymous_enum_bitfield);
4409*5ffd83dbSDimitry Andric     } else if (CanHaveEnumBase || !ColonIsSacred) {
4410*5ffd83dbSDimitry Andric       SourceLocation ColonLoc = ConsumeToken();
44110b57cec5SDimitry Andric 
4412*5ffd83dbSDimitry Andric       // Parse a type-specifier-seq as a type. We can't just ParseTypeName here,
4413*5ffd83dbSDimitry Andric       // because under -fms-extensions,
4414*5ffd83dbSDimitry Andric       //   enum E : int *p;
4415*5ffd83dbSDimitry Andric       // declares 'enum E : int; E *p;' not 'enum E : int*; E p;'.
4416*5ffd83dbSDimitry Andric       DeclSpec DS(AttrFactory);
4417*5ffd83dbSDimitry Andric       ParseSpecifierQualifierList(DS, AS, DeclSpecContext::DSC_type_specifier);
4418*5ffd83dbSDimitry Andric       Declarator DeclaratorInfo(DS, DeclaratorContext::TypeNameContext);
4419*5ffd83dbSDimitry Andric       BaseType = Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
44200b57cec5SDimitry Andric 
4421*5ffd83dbSDimitry Andric       BaseRange = SourceRange(ColonLoc, DeclaratorInfo.getSourceRange().getEnd());
44220b57cec5SDimitry Andric 
44230b57cec5SDimitry Andric       if (!getLangOpts().ObjC) {
44240b57cec5SDimitry Andric         if (getLangOpts().CPlusPlus11)
4425*5ffd83dbSDimitry Andric           Diag(ColonLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type)
4426*5ffd83dbSDimitry Andric               << BaseRange;
44270b57cec5SDimitry Andric         else if (getLangOpts().CPlusPlus)
4428*5ffd83dbSDimitry Andric           Diag(ColonLoc, diag::ext_cxx11_enum_fixed_underlying_type)
4429*5ffd83dbSDimitry Andric               << BaseRange;
44300b57cec5SDimitry Andric         else if (getLangOpts().MicrosoftExt)
4431*5ffd83dbSDimitry Andric           Diag(ColonLoc, diag::ext_ms_c_enum_fixed_underlying_type)
4432*5ffd83dbSDimitry Andric               << BaseRange;
44330b57cec5SDimitry Andric         else
4434*5ffd83dbSDimitry Andric           Diag(ColonLoc, diag::ext_clang_c_enum_fixed_underlying_type)
4435*5ffd83dbSDimitry Andric               << BaseRange;
44360b57cec5SDimitry Andric       }
44370b57cec5SDimitry Andric     }
44380b57cec5SDimitry Andric   }
44390b57cec5SDimitry Andric 
44400b57cec5SDimitry Andric   // There are four options here.  If we have 'friend enum foo;' then this is a
44410b57cec5SDimitry Andric   // friend declaration, and cannot have an accompanying definition. If we have
44420b57cec5SDimitry Andric   // 'enum foo;', then this is a forward declaration.  If we have
44430b57cec5SDimitry Andric   // 'enum foo {...' then this is a definition. Otherwise we have something
44440b57cec5SDimitry Andric   // like 'enum foo xyz', a reference.
44450b57cec5SDimitry Andric   //
44460b57cec5SDimitry Andric   // This is needed to handle stuff like this right (C99 6.7.2.3p11):
44470b57cec5SDimitry Andric   // enum foo {..};  void bar() { enum foo; }    <- new foo in bar.
44480b57cec5SDimitry Andric   // enum foo {..};  void bar() { enum foo x; }  <- use of old foo.
44490b57cec5SDimitry Andric   //
44500b57cec5SDimitry Andric   Sema::TagUseKind TUK;
4451*5ffd83dbSDimitry Andric   if (AllowEnumSpecifier == AllowDefiningTypeSpec::No)
44520b57cec5SDimitry Andric     TUK = Sema::TUK_Reference;
4453*5ffd83dbSDimitry Andric   else if (Tok.is(tok::l_brace)) {
44540b57cec5SDimitry Andric     if (DS.isFriendSpecified()) {
44550b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
44560b57cec5SDimitry Andric         << SourceRange(DS.getFriendSpecLoc());
44570b57cec5SDimitry Andric       ConsumeBrace();
44580b57cec5SDimitry Andric       SkipUntil(tok::r_brace, StopAtSemi);
4459*5ffd83dbSDimitry Andric       // Discard any other definition-only pieces.
4460*5ffd83dbSDimitry Andric       attrs.clear();
4461*5ffd83dbSDimitry Andric       ScopedEnumKWLoc = SourceLocation();
4462*5ffd83dbSDimitry Andric       IsScopedUsingClassTag = false;
4463*5ffd83dbSDimitry Andric       BaseType = TypeResult();
44640b57cec5SDimitry Andric       TUK = Sema::TUK_Friend;
44650b57cec5SDimitry Andric     } else {
44660b57cec5SDimitry Andric       TUK = Sema::TUK_Definition;
44670b57cec5SDimitry Andric     }
44680b57cec5SDimitry Andric   } else if (!isTypeSpecifier(DSC) &&
44690b57cec5SDimitry Andric              (Tok.is(tok::semi) ||
44700b57cec5SDimitry Andric               (Tok.isAtStartOfLine() &&
44710b57cec5SDimitry Andric                !isValidAfterTypeSpecifier(CanBeBitfield)))) {
4472*5ffd83dbSDimitry Andric     // An opaque-enum-declaration is required to be standalone (no preceding or
4473*5ffd83dbSDimitry Andric     // following tokens in the declaration). Sema enforces this separately by
4474*5ffd83dbSDimitry Andric     // diagnosing anything else in the DeclSpec.
44750b57cec5SDimitry Andric     TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
44760b57cec5SDimitry Andric     if (Tok.isNot(tok::semi)) {
44770b57cec5SDimitry Andric       // A semicolon was missing after this declaration. Diagnose and recover.
44780b57cec5SDimitry Andric       ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
44790b57cec5SDimitry Andric       PP.EnterToken(Tok, /*IsReinject=*/true);
44800b57cec5SDimitry Andric       Tok.setKind(tok::semi);
44810b57cec5SDimitry Andric     }
44820b57cec5SDimitry Andric   } else {
44830b57cec5SDimitry Andric     TUK = Sema::TUK_Reference;
44840b57cec5SDimitry Andric   }
44850b57cec5SDimitry Andric 
4486*5ffd83dbSDimitry Andric   bool IsElaboratedTypeSpecifier =
4487*5ffd83dbSDimitry Andric       TUK == Sema::TUK_Reference || TUK == Sema::TUK_Friend;
4488*5ffd83dbSDimitry Andric 
4489*5ffd83dbSDimitry Andric   // If this is an elaborated type specifier nested in a larger declaration,
4490*5ffd83dbSDimitry Andric   // and we delayed diagnostics before, just merge them into the current pool.
44910b57cec5SDimitry Andric   if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
44920b57cec5SDimitry Andric     diagsFromTag.redelay();
44930b57cec5SDimitry Andric   }
44940b57cec5SDimitry Andric 
44950b57cec5SDimitry Andric   MultiTemplateParamsArg TParams;
44960b57cec5SDimitry Andric   if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
44970b57cec5SDimitry Andric       TUK != Sema::TUK_Reference) {
44980b57cec5SDimitry Andric     if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
44990b57cec5SDimitry Andric       // Skip the rest of this declarator, up until the comma or semicolon.
45000b57cec5SDimitry Andric       Diag(Tok, diag::err_enum_template);
45010b57cec5SDimitry Andric       SkipUntil(tok::comma, StopAtSemi);
45020b57cec5SDimitry Andric       return;
45030b57cec5SDimitry Andric     }
45040b57cec5SDimitry Andric 
45050b57cec5SDimitry Andric     if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
45060b57cec5SDimitry Andric       // Enumerations can't be explicitly instantiated.
45070b57cec5SDimitry Andric       DS.SetTypeSpecError();
45080b57cec5SDimitry Andric       Diag(StartLoc, diag::err_explicit_instantiation_enum);
45090b57cec5SDimitry Andric       return;
45100b57cec5SDimitry Andric     }
45110b57cec5SDimitry Andric 
45120b57cec5SDimitry Andric     assert(TemplateInfo.TemplateParams && "no template parameters");
45130b57cec5SDimitry Andric     TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
45140b57cec5SDimitry Andric                                      TemplateInfo.TemplateParams->size());
45150b57cec5SDimitry Andric   }
45160b57cec5SDimitry Andric 
45170b57cec5SDimitry Andric   if (!Name && TUK != Sema::TUK_Definition) {
45180b57cec5SDimitry Andric     Diag(Tok, diag::err_enumerator_unnamed_no_def);
45190b57cec5SDimitry Andric 
45200b57cec5SDimitry Andric     // Skip the rest of this declarator, up until the comma or semicolon.
45210b57cec5SDimitry Andric     SkipUntil(tok::comma, StopAtSemi);
45220b57cec5SDimitry Andric     return;
45230b57cec5SDimitry Andric   }
45240b57cec5SDimitry Andric 
4525*5ffd83dbSDimitry Andric   // An elaborated-type-specifier has a much more constrained grammar:
4526*5ffd83dbSDimitry Andric   //
4527*5ffd83dbSDimitry Andric   //   'enum' nested-name-specifier[opt] identifier
4528*5ffd83dbSDimitry Andric   //
4529*5ffd83dbSDimitry Andric   // If we parsed any other bits, reject them now.
4530*5ffd83dbSDimitry Andric   //
4531*5ffd83dbSDimitry Andric   // MSVC and (for now at least) Objective-C permit a full enum-specifier
4532*5ffd83dbSDimitry Andric   // or opaque-enum-declaration anywhere.
4533*5ffd83dbSDimitry Andric   if (IsElaboratedTypeSpecifier && !getLangOpts().MicrosoftExt &&
4534*5ffd83dbSDimitry Andric       !getLangOpts().ObjC) {
4535*5ffd83dbSDimitry Andric     ProhibitAttributes(attrs);
4536*5ffd83dbSDimitry Andric     if (BaseType.isUsable())
4537*5ffd83dbSDimitry Andric       Diag(BaseRange.getBegin(), diag::ext_enum_base_in_type_specifier)
4538*5ffd83dbSDimitry Andric           << (AllowEnumSpecifier == AllowDefiningTypeSpec::Yes) << BaseRange;
4539*5ffd83dbSDimitry Andric     else if (ScopedEnumKWLoc.isValid())
4540*5ffd83dbSDimitry Andric       Diag(ScopedEnumKWLoc, diag::ext_elaborated_enum_class)
4541*5ffd83dbSDimitry Andric         << FixItHint::CreateRemoval(ScopedEnumKWLoc) << IsScopedUsingClassTag;
4542*5ffd83dbSDimitry Andric   }
4543*5ffd83dbSDimitry Andric 
45440b57cec5SDimitry Andric   stripTypeAttributesOffDeclSpec(attrs, DS, TUK);
45450b57cec5SDimitry Andric 
45460b57cec5SDimitry Andric   Sema::SkipBodyInfo SkipBody;
45470b57cec5SDimitry Andric   if (!Name && TUK == Sema::TUK_Definition && Tok.is(tok::l_brace) &&
45480b57cec5SDimitry Andric       NextToken().is(tok::identifier))
45490b57cec5SDimitry Andric     SkipBody = Actions.shouldSkipAnonEnumBody(getCurScope(),
45500b57cec5SDimitry Andric                                               NextToken().getIdentifierInfo(),
45510b57cec5SDimitry Andric                                               NextToken().getLocation());
45520b57cec5SDimitry Andric 
45530b57cec5SDimitry Andric   bool Owned = false;
45540b57cec5SDimitry Andric   bool IsDependent = false;
45550b57cec5SDimitry Andric   const char *PrevSpec = nullptr;
45560b57cec5SDimitry Andric   unsigned DiagID;
45570b57cec5SDimitry Andric   Decl *TagDecl = Actions.ActOnTag(
45580b57cec5SDimitry Andric       getCurScope(), DeclSpec::TST_enum, TUK, StartLoc, SS, Name, NameLoc,
45590b57cec5SDimitry Andric       attrs, AS, DS.getModulePrivateSpecLoc(), TParams, Owned, IsDependent,
45600b57cec5SDimitry Andric       ScopedEnumKWLoc, IsScopedUsingClassTag, BaseType,
45610b57cec5SDimitry Andric       DSC == DeclSpecContext::DSC_type_specifier,
45620b57cec5SDimitry Andric       DSC == DeclSpecContext::DSC_template_param ||
45630b57cec5SDimitry Andric           DSC == DeclSpecContext::DSC_template_type_arg,
45640b57cec5SDimitry Andric       &SkipBody);
45650b57cec5SDimitry Andric 
45660b57cec5SDimitry Andric   if (SkipBody.ShouldSkip) {
45670b57cec5SDimitry Andric     assert(TUK == Sema::TUK_Definition && "can only skip a definition");
45680b57cec5SDimitry Andric 
45690b57cec5SDimitry Andric     BalancedDelimiterTracker T(*this, tok::l_brace);
45700b57cec5SDimitry Andric     T.consumeOpen();
45710b57cec5SDimitry Andric     T.skipToEnd();
45720b57cec5SDimitry Andric 
45730b57cec5SDimitry Andric     if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
45740b57cec5SDimitry Andric                            NameLoc.isValid() ? NameLoc : StartLoc,
45750b57cec5SDimitry Andric                            PrevSpec, DiagID, TagDecl, Owned,
45760b57cec5SDimitry Andric                            Actions.getASTContext().getPrintingPolicy()))
45770b57cec5SDimitry Andric       Diag(StartLoc, DiagID) << PrevSpec;
45780b57cec5SDimitry Andric     return;
45790b57cec5SDimitry Andric   }
45800b57cec5SDimitry Andric 
45810b57cec5SDimitry Andric   if (IsDependent) {
45820b57cec5SDimitry Andric     // This enum has a dependent nested-name-specifier. Handle it as a
45830b57cec5SDimitry Andric     // dependent tag.
45840b57cec5SDimitry Andric     if (!Name) {
45850b57cec5SDimitry Andric       DS.SetTypeSpecError();
45860b57cec5SDimitry Andric       Diag(Tok, diag::err_expected_type_name_after_typename);
45870b57cec5SDimitry Andric       return;
45880b57cec5SDimitry Andric     }
45890b57cec5SDimitry Andric 
45900b57cec5SDimitry Andric     TypeResult Type = Actions.ActOnDependentTag(
45910b57cec5SDimitry Andric         getCurScope(), DeclSpec::TST_enum, TUK, SS, Name, StartLoc, NameLoc);
45920b57cec5SDimitry Andric     if (Type.isInvalid()) {
45930b57cec5SDimitry Andric       DS.SetTypeSpecError();
45940b57cec5SDimitry Andric       return;
45950b57cec5SDimitry Andric     }
45960b57cec5SDimitry Andric 
45970b57cec5SDimitry Andric     if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
45980b57cec5SDimitry Andric                            NameLoc.isValid() ? NameLoc : StartLoc,
45990b57cec5SDimitry Andric                            PrevSpec, DiagID, Type.get(),
46000b57cec5SDimitry Andric                            Actions.getASTContext().getPrintingPolicy()))
46010b57cec5SDimitry Andric       Diag(StartLoc, DiagID) << PrevSpec;
46020b57cec5SDimitry Andric 
46030b57cec5SDimitry Andric     return;
46040b57cec5SDimitry Andric   }
46050b57cec5SDimitry Andric 
46060b57cec5SDimitry Andric   if (!TagDecl) {
46070b57cec5SDimitry Andric     // The action failed to produce an enumeration tag. If this is a
46080b57cec5SDimitry Andric     // definition, consume the entire definition.
46090b57cec5SDimitry Andric     if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
46100b57cec5SDimitry Andric       ConsumeBrace();
46110b57cec5SDimitry Andric       SkipUntil(tok::r_brace, StopAtSemi);
46120b57cec5SDimitry Andric     }
46130b57cec5SDimitry Andric 
46140b57cec5SDimitry Andric     DS.SetTypeSpecError();
46150b57cec5SDimitry Andric     return;
46160b57cec5SDimitry Andric   }
46170b57cec5SDimitry Andric 
4618*5ffd83dbSDimitry Andric   if (Tok.is(tok::l_brace) && TUK == Sema::TUK_Definition) {
46190b57cec5SDimitry Andric     Decl *D = SkipBody.CheckSameAsPrevious ? SkipBody.New : TagDecl;
46200b57cec5SDimitry Andric     ParseEnumBody(StartLoc, D);
46210b57cec5SDimitry Andric     if (SkipBody.CheckSameAsPrevious &&
46220b57cec5SDimitry Andric         !Actions.ActOnDuplicateDefinition(DS, TagDecl, SkipBody)) {
46230b57cec5SDimitry Andric       DS.SetTypeSpecError();
46240b57cec5SDimitry Andric       return;
46250b57cec5SDimitry Andric     }
46260b57cec5SDimitry Andric   }
46270b57cec5SDimitry Andric 
46280b57cec5SDimitry Andric   if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
46290b57cec5SDimitry Andric                          NameLoc.isValid() ? NameLoc : StartLoc,
46300b57cec5SDimitry Andric                          PrevSpec, DiagID, TagDecl, Owned,
46310b57cec5SDimitry Andric                          Actions.getASTContext().getPrintingPolicy()))
46320b57cec5SDimitry Andric     Diag(StartLoc, DiagID) << PrevSpec;
46330b57cec5SDimitry Andric }
46340b57cec5SDimitry Andric 
46350b57cec5SDimitry Andric /// ParseEnumBody - Parse a {} enclosed enumerator-list.
46360b57cec5SDimitry Andric ///       enumerator-list:
46370b57cec5SDimitry Andric ///         enumerator
46380b57cec5SDimitry Andric ///         enumerator-list ',' enumerator
46390b57cec5SDimitry Andric ///       enumerator:
46400b57cec5SDimitry Andric ///         enumeration-constant attributes[opt]
46410b57cec5SDimitry Andric ///         enumeration-constant attributes[opt] '=' constant-expression
46420b57cec5SDimitry Andric ///       enumeration-constant:
46430b57cec5SDimitry Andric ///         identifier
46440b57cec5SDimitry Andric ///
46450b57cec5SDimitry Andric void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
46460b57cec5SDimitry Andric   // Enter the scope of the enum body and start the definition.
46470b57cec5SDimitry Andric   ParseScope EnumScope(this, Scope::DeclScope | Scope::EnumScope);
46480b57cec5SDimitry Andric   Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
46490b57cec5SDimitry Andric 
46500b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_brace);
46510b57cec5SDimitry Andric   T.consumeOpen();
46520b57cec5SDimitry Andric 
46530b57cec5SDimitry Andric   // C does not allow an empty enumerator-list, C++ does [dcl.enum].
46540b57cec5SDimitry Andric   if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
46550b57cec5SDimitry Andric     Diag(Tok, diag::err_empty_enum);
46560b57cec5SDimitry Andric 
46570b57cec5SDimitry Andric   SmallVector<Decl *, 32> EnumConstantDecls;
46580b57cec5SDimitry Andric   SmallVector<SuppressAccessChecks, 32> EnumAvailabilityDiags;
46590b57cec5SDimitry Andric 
46600b57cec5SDimitry Andric   Decl *LastEnumConstDecl = nullptr;
46610b57cec5SDimitry Andric 
46620b57cec5SDimitry Andric   // Parse the enumerator-list.
46630b57cec5SDimitry Andric   while (Tok.isNot(tok::r_brace)) {
46640b57cec5SDimitry Andric     // Parse enumerator. If failed, try skipping till the start of the next
46650b57cec5SDimitry Andric     // enumerator definition.
46660b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
46670b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
46680b57cec5SDimitry Andric       if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) &&
46690b57cec5SDimitry Andric           TryConsumeToken(tok::comma))
46700b57cec5SDimitry Andric         continue;
46710b57cec5SDimitry Andric       break;
46720b57cec5SDimitry Andric     }
46730b57cec5SDimitry Andric     IdentifierInfo *Ident = Tok.getIdentifierInfo();
46740b57cec5SDimitry Andric     SourceLocation IdentLoc = ConsumeToken();
46750b57cec5SDimitry Andric 
46760b57cec5SDimitry Andric     // If attributes exist after the enumerator, parse them.
46770b57cec5SDimitry Andric     ParsedAttributesWithRange attrs(AttrFactory);
46780b57cec5SDimitry Andric     MaybeParseGNUAttributes(attrs);
46790b57cec5SDimitry Andric     ProhibitAttributes(attrs); // GNU-style attributes are prohibited.
46800b57cec5SDimitry Andric     if (standardAttributesAllowed() && isCXX11AttributeSpecifier()) {
46810b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus)
46820b57cec5SDimitry Andric         Diag(Tok.getLocation(), getLangOpts().CPlusPlus17
46830b57cec5SDimitry Andric                                     ? diag::warn_cxx14_compat_ns_enum_attribute
46840b57cec5SDimitry Andric                                     : diag::ext_ns_enum_attribute)
46850b57cec5SDimitry Andric             << 1 /*enumerator*/;
46860b57cec5SDimitry Andric       ParseCXX11Attributes(attrs);
46870b57cec5SDimitry Andric     }
46880b57cec5SDimitry Andric 
46890b57cec5SDimitry Andric     SourceLocation EqualLoc;
46900b57cec5SDimitry Andric     ExprResult AssignedVal;
46910b57cec5SDimitry Andric     EnumAvailabilityDiags.emplace_back(*this);
46920b57cec5SDimitry Andric 
4693a7dea167SDimitry Andric     EnterExpressionEvaluationContext ConstantEvaluated(
4694a7dea167SDimitry Andric         Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
46950b57cec5SDimitry Andric     if (TryConsumeToken(tok::equal, EqualLoc)) {
4696a7dea167SDimitry Andric       AssignedVal = ParseConstantExpressionInExprEvalContext();
46970b57cec5SDimitry Andric       if (AssignedVal.isInvalid())
46980b57cec5SDimitry Andric         SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch);
46990b57cec5SDimitry Andric     }
47000b57cec5SDimitry Andric 
47010b57cec5SDimitry Andric     // Install the enumerator constant into EnumDecl.
47020b57cec5SDimitry Andric     Decl *EnumConstDecl = Actions.ActOnEnumConstant(
47030b57cec5SDimitry Andric         getCurScope(), EnumDecl, LastEnumConstDecl, IdentLoc, Ident, attrs,
47040b57cec5SDimitry Andric         EqualLoc, AssignedVal.get());
47050b57cec5SDimitry Andric     EnumAvailabilityDiags.back().done();
47060b57cec5SDimitry Andric 
47070b57cec5SDimitry Andric     EnumConstantDecls.push_back(EnumConstDecl);
47080b57cec5SDimitry Andric     LastEnumConstDecl = EnumConstDecl;
47090b57cec5SDimitry Andric 
47100b57cec5SDimitry Andric     if (Tok.is(tok::identifier)) {
47110b57cec5SDimitry Andric       // We're missing a comma between enumerators.
47120b57cec5SDimitry Andric       SourceLocation Loc = getEndOfPreviousToken();
47130b57cec5SDimitry Andric       Diag(Loc, diag::err_enumerator_list_missing_comma)
47140b57cec5SDimitry Andric         << FixItHint::CreateInsertion(Loc, ", ");
47150b57cec5SDimitry Andric       continue;
47160b57cec5SDimitry Andric     }
47170b57cec5SDimitry Andric 
47180b57cec5SDimitry Andric     // Emumerator definition must be finished, only comma or r_brace are
47190b57cec5SDimitry Andric     // allowed here.
47200b57cec5SDimitry Andric     SourceLocation CommaLoc;
47210b57cec5SDimitry Andric     if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) {
47220b57cec5SDimitry Andric       if (EqualLoc.isValid())
47230b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace
47240b57cec5SDimitry Andric                                                            << tok::comma;
47250b57cec5SDimitry Andric       else
47260b57cec5SDimitry Andric         Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator);
47270b57cec5SDimitry Andric       if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) {
47280b57cec5SDimitry Andric         if (TryConsumeToken(tok::comma, CommaLoc))
47290b57cec5SDimitry Andric           continue;
47300b57cec5SDimitry Andric       } else {
47310b57cec5SDimitry Andric         break;
47320b57cec5SDimitry Andric       }
47330b57cec5SDimitry Andric     }
47340b57cec5SDimitry Andric 
47350b57cec5SDimitry Andric     // If comma is followed by r_brace, emit appropriate warning.
47360b57cec5SDimitry Andric     if (Tok.is(tok::r_brace) && CommaLoc.isValid()) {
47370b57cec5SDimitry Andric       if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
47380b57cec5SDimitry Andric         Diag(CommaLoc, getLangOpts().CPlusPlus ?
47390b57cec5SDimitry Andric                diag::ext_enumerator_list_comma_cxx :
47400b57cec5SDimitry Andric                diag::ext_enumerator_list_comma_c)
47410b57cec5SDimitry Andric           << FixItHint::CreateRemoval(CommaLoc);
47420b57cec5SDimitry Andric       else if (getLangOpts().CPlusPlus11)
47430b57cec5SDimitry Andric         Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
47440b57cec5SDimitry Andric           << FixItHint::CreateRemoval(CommaLoc);
47450b57cec5SDimitry Andric       break;
47460b57cec5SDimitry Andric     }
47470b57cec5SDimitry Andric   }
47480b57cec5SDimitry Andric 
47490b57cec5SDimitry Andric   // Eat the }.
47500b57cec5SDimitry Andric   T.consumeClose();
47510b57cec5SDimitry Andric 
47520b57cec5SDimitry Andric   // If attributes exist after the identifier list, parse them.
47530b57cec5SDimitry Andric   ParsedAttributes attrs(AttrFactory);
47540b57cec5SDimitry Andric   MaybeParseGNUAttributes(attrs);
47550b57cec5SDimitry Andric 
47560b57cec5SDimitry Andric   Actions.ActOnEnumBody(StartLoc, T.getRange(), EnumDecl, EnumConstantDecls,
47570b57cec5SDimitry Andric                         getCurScope(), attrs);
47580b57cec5SDimitry Andric 
47590b57cec5SDimitry Andric   // Now handle enum constant availability diagnostics.
47600b57cec5SDimitry Andric   assert(EnumConstantDecls.size() == EnumAvailabilityDiags.size());
47610b57cec5SDimitry Andric   for (size_t i = 0, e = EnumConstantDecls.size(); i != e; ++i) {
47620b57cec5SDimitry Andric     ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
47630b57cec5SDimitry Andric     EnumAvailabilityDiags[i].redelay();
47640b57cec5SDimitry Andric     PD.complete(EnumConstantDecls[i]);
47650b57cec5SDimitry Andric   }
47660b57cec5SDimitry Andric 
47670b57cec5SDimitry Andric   EnumScope.Exit();
47680b57cec5SDimitry Andric   Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl, T.getRange());
47690b57cec5SDimitry Andric 
47700b57cec5SDimitry Andric   // The next token must be valid after an enum definition. If not, a ';'
47710b57cec5SDimitry Andric   // was probably forgotten.
47720b57cec5SDimitry Andric   bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
47730b57cec5SDimitry Andric   if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
47740b57cec5SDimitry Andric     ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
47750b57cec5SDimitry Andric     // Push this token back into the preprocessor and change our current token
47760b57cec5SDimitry Andric     // to ';' so that the rest of the code recovers as though there were an
47770b57cec5SDimitry Andric     // ';' after the definition.
47780b57cec5SDimitry Andric     PP.EnterToken(Tok, /*IsReinject=*/true);
47790b57cec5SDimitry Andric     Tok.setKind(tok::semi);
47800b57cec5SDimitry Andric   }
47810b57cec5SDimitry Andric }
47820b57cec5SDimitry Andric 
47830b57cec5SDimitry Andric /// isKnownToBeTypeSpecifier - Return true if we know that the specified token
47840b57cec5SDimitry Andric /// is definitely a type-specifier.  Return false if it isn't part of a type
47850b57cec5SDimitry Andric /// specifier or if we're not sure.
47860b57cec5SDimitry Andric bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
47870b57cec5SDimitry Andric   switch (Tok.getKind()) {
47880b57cec5SDimitry Andric   default: return false;
47890b57cec5SDimitry Andric     // type-specifiers
47900b57cec5SDimitry Andric   case tok::kw_short:
47910b57cec5SDimitry Andric   case tok::kw_long:
47920b57cec5SDimitry Andric   case tok::kw___int64:
47930b57cec5SDimitry Andric   case tok::kw___int128:
47940b57cec5SDimitry Andric   case tok::kw_signed:
47950b57cec5SDimitry Andric   case tok::kw_unsigned:
47960b57cec5SDimitry Andric   case tok::kw__Complex:
47970b57cec5SDimitry Andric   case tok::kw__Imaginary:
47980b57cec5SDimitry Andric   case tok::kw_void:
47990b57cec5SDimitry Andric   case tok::kw_char:
48000b57cec5SDimitry Andric   case tok::kw_wchar_t:
48010b57cec5SDimitry Andric   case tok::kw_char8_t:
48020b57cec5SDimitry Andric   case tok::kw_char16_t:
48030b57cec5SDimitry Andric   case tok::kw_char32_t:
48040b57cec5SDimitry Andric   case tok::kw_int:
4805*5ffd83dbSDimitry Andric   case tok::kw__ExtInt:
4806*5ffd83dbSDimitry Andric   case tok::kw___bf16:
48070b57cec5SDimitry Andric   case tok::kw_half:
48080b57cec5SDimitry Andric   case tok::kw_float:
48090b57cec5SDimitry Andric   case tok::kw_double:
48100b57cec5SDimitry Andric   case tok::kw__Accum:
48110b57cec5SDimitry Andric   case tok::kw__Fract:
48120b57cec5SDimitry Andric   case tok::kw__Float16:
48130b57cec5SDimitry Andric   case tok::kw___float128:
48140b57cec5SDimitry Andric   case tok::kw_bool:
48150b57cec5SDimitry Andric   case tok::kw__Bool:
48160b57cec5SDimitry Andric   case tok::kw__Decimal32:
48170b57cec5SDimitry Andric   case tok::kw__Decimal64:
48180b57cec5SDimitry Andric   case tok::kw__Decimal128:
48190b57cec5SDimitry Andric   case tok::kw___vector:
48200b57cec5SDimitry Andric #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
48210b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def"
48220b57cec5SDimitry Andric 
48230b57cec5SDimitry Andric     // struct-or-union-specifier (C99) or class-specifier (C++)
48240b57cec5SDimitry Andric   case tok::kw_class:
48250b57cec5SDimitry Andric   case tok::kw_struct:
48260b57cec5SDimitry Andric   case tok::kw___interface:
48270b57cec5SDimitry Andric   case tok::kw_union:
48280b57cec5SDimitry Andric     // enum-specifier
48290b57cec5SDimitry Andric   case tok::kw_enum:
48300b57cec5SDimitry Andric 
48310b57cec5SDimitry Andric     // typedef-name
48320b57cec5SDimitry Andric   case tok::annot_typename:
48330b57cec5SDimitry Andric     return true;
48340b57cec5SDimitry Andric   }
48350b57cec5SDimitry Andric }
48360b57cec5SDimitry Andric 
48370b57cec5SDimitry Andric /// isTypeSpecifierQualifier - Return true if the current token could be the
48380b57cec5SDimitry Andric /// start of a specifier-qualifier-list.
48390b57cec5SDimitry Andric bool Parser::isTypeSpecifierQualifier() {
48400b57cec5SDimitry Andric   switch (Tok.getKind()) {
48410b57cec5SDimitry Andric   default: return false;
48420b57cec5SDimitry Andric 
48430b57cec5SDimitry Andric   case tok::identifier:   // foo::bar
48440b57cec5SDimitry Andric     if (TryAltiVecVectorToken())
48450b57cec5SDimitry Andric       return true;
48460b57cec5SDimitry Andric     LLVM_FALLTHROUGH;
48470b57cec5SDimitry Andric   case tok::kw_typename:  // typename T::type
48480b57cec5SDimitry Andric     // Annotate typenames and C++ scope specifiers.  If we get one, just
48490b57cec5SDimitry Andric     // recurse to handle whatever we get.
48500b57cec5SDimitry Andric     if (TryAnnotateTypeOrScopeToken())
48510b57cec5SDimitry Andric       return true;
48520b57cec5SDimitry Andric     if (Tok.is(tok::identifier))
48530b57cec5SDimitry Andric       return false;
48540b57cec5SDimitry Andric     return isTypeSpecifierQualifier();
48550b57cec5SDimitry Andric 
48560b57cec5SDimitry Andric   case tok::coloncolon:   // ::foo::bar
48570b57cec5SDimitry Andric     if (NextToken().is(tok::kw_new) ||    // ::new
48580b57cec5SDimitry Andric         NextToken().is(tok::kw_delete))   // ::delete
48590b57cec5SDimitry Andric       return false;
48600b57cec5SDimitry Andric 
48610b57cec5SDimitry Andric     if (TryAnnotateTypeOrScopeToken())
48620b57cec5SDimitry Andric       return true;
48630b57cec5SDimitry Andric     return isTypeSpecifierQualifier();
48640b57cec5SDimitry Andric 
48650b57cec5SDimitry Andric     // GNU attributes support.
48660b57cec5SDimitry Andric   case tok::kw___attribute:
48670b57cec5SDimitry Andric     // GNU typeof support.
48680b57cec5SDimitry Andric   case tok::kw_typeof:
48690b57cec5SDimitry Andric 
48700b57cec5SDimitry Andric     // type-specifiers
48710b57cec5SDimitry Andric   case tok::kw_short:
48720b57cec5SDimitry Andric   case tok::kw_long:
48730b57cec5SDimitry Andric   case tok::kw___int64:
48740b57cec5SDimitry Andric   case tok::kw___int128:
48750b57cec5SDimitry Andric   case tok::kw_signed:
48760b57cec5SDimitry Andric   case tok::kw_unsigned:
48770b57cec5SDimitry Andric   case tok::kw__Complex:
48780b57cec5SDimitry Andric   case tok::kw__Imaginary:
48790b57cec5SDimitry Andric   case tok::kw_void:
48800b57cec5SDimitry Andric   case tok::kw_char:
48810b57cec5SDimitry Andric   case tok::kw_wchar_t:
48820b57cec5SDimitry Andric   case tok::kw_char8_t:
48830b57cec5SDimitry Andric   case tok::kw_char16_t:
48840b57cec5SDimitry Andric   case tok::kw_char32_t:
48850b57cec5SDimitry Andric   case tok::kw_int:
4886*5ffd83dbSDimitry Andric   case tok::kw__ExtInt:
48870b57cec5SDimitry Andric   case tok::kw_half:
4888*5ffd83dbSDimitry Andric   case tok::kw___bf16:
48890b57cec5SDimitry Andric   case tok::kw_float:
48900b57cec5SDimitry Andric   case tok::kw_double:
48910b57cec5SDimitry Andric   case tok::kw__Accum:
48920b57cec5SDimitry Andric   case tok::kw__Fract:
48930b57cec5SDimitry Andric   case tok::kw__Float16:
48940b57cec5SDimitry Andric   case tok::kw___float128:
48950b57cec5SDimitry Andric   case tok::kw_bool:
48960b57cec5SDimitry Andric   case tok::kw__Bool:
48970b57cec5SDimitry Andric   case tok::kw__Decimal32:
48980b57cec5SDimitry Andric   case tok::kw__Decimal64:
48990b57cec5SDimitry Andric   case tok::kw__Decimal128:
49000b57cec5SDimitry Andric   case tok::kw___vector:
49010b57cec5SDimitry Andric #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
49020b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def"
49030b57cec5SDimitry Andric 
49040b57cec5SDimitry Andric     // struct-or-union-specifier (C99) or class-specifier (C++)
49050b57cec5SDimitry Andric   case tok::kw_class:
49060b57cec5SDimitry Andric   case tok::kw_struct:
49070b57cec5SDimitry Andric   case tok::kw___interface:
49080b57cec5SDimitry Andric   case tok::kw_union:
49090b57cec5SDimitry Andric     // enum-specifier
49100b57cec5SDimitry Andric   case tok::kw_enum:
49110b57cec5SDimitry Andric 
49120b57cec5SDimitry Andric     // type-qualifier
49130b57cec5SDimitry Andric   case tok::kw_const:
49140b57cec5SDimitry Andric   case tok::kw_volatile:
49150b57cec5SDimitry Andric   case tok::kw_restrict:
49160b57cec5SDimitry Andric   case tok::kw__Sat:
49170b57cec5SDimitry Andric 
49180b57cec5SDimitry Andric     // Debugger support.
49190b57cec5SDimitry Andric   case tok::kw___unknown_anytype:
49200b57cec5SDimitry Andric 
49210b57cec5SDimitry Andric     // typedef-name
49220b57cec5SDimitry Andric   case tok::annot_typename:
49230b57cec5SDimitry Andric     return true;
49240b57cec5SDimitry Andric 
49250b57cec5SDimitry Andric     // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
49260b57cec5SDimitry Andric   case tok::less:
49270b57cec5SDimitry Andric     return getLangOpts().ObjC;
49280b57cec5SDimitry Andric 
49290b57cec5SDimitry Andric   case tok::kw___cdecl:
49300b57cec5SDimitry Andric   case tok::kw___stdcall:
49310b57cec5SDimitry Andric   case tok::kw___fastcall:
49320b57cec5SDimitry Andric   case tok::kw___thiscall:
49330b57cec5SDimitry Andric   case tok::kw___regcall:
49340b57cec5SDimitry Andric   case tok::kw___vectorcall:
49350b57cec5SDimitry Andric   case tok::kw___w64:
49360b57cec5SDimitry Andric   case tok::kw___ptr64:
49370b57cec5SDimitry Andric   case tok::kw___ptr32:
49380b57cec5SDimitry Andric   case tok::kw___pascal:
49390b57cec5SDimitry Andric   case tok::kw___unaligned:
49400b57cec5SDimitry Andric 
49410b57cec5SDimitry Andric   case tok::kw__Nonnull:
49420b57cec5SDimitry Andric   case tok::kw__Nullable:
49430b57cec5SDimitry Andric   case tok::kw__Null_unspecified:
49440b57cec5SDimitry Andric 
49450b57cec5SDimitry Andric   case tok::kw___kindof:
49460b57cec5SDimitry Andric 
49470b57cec5SDimitry Andric   case tok::kw___private:
49480b57cec5SDimitry Andric   case tok::kw___local:
49490b57cec5SDimitry Andric   case tok::kw___global:
49500b57cec5SDimitry Andric   case tok::kw___constant:
49510b57cec5SDimitry Andric   case tok::kw___generic:
49520b57cec5SDimitry Andric   case tok::kw___read_only:
49530b57cec5SDimitry Andric   case tok::kw___read_write:
49540b57cec5SDimitry Andric   case tok::kw___write_only:
49550b57cec5SDimitry Andric     return true;
49560b57cec5SDimitry Andric 
49570b57cec5SDimitry Andric   case tok::kw_private:
49580b57cec5SDimitry Andric     return getLangOpts().OpenCL;
49590b57cec5SDimitry Andric 
49600b57cec5SDimitry Andric   // C11 _Atomic
49610b57cec5SDimitry Andric   case tok::kw__Atomic:
49620b57cec5SDimitry Andric     return true;
49630b57cec5SDimitry Andric   }
49640b57cec5SDimitry Andric }
49650b57cec5SDimitry Andric 
49660b57cec5SDimitry Andric /// isDeclarationSpecifier() - Return true if the current token is part of a
49670b57cec5SDimitry Andric /// declaration specifier.
49680b57cec5SDimitry Andric ///
49690b57cec5SDimitry Andric /// \param DisambiguatingWithExpression True to indicate that the purpose of
49700b57cec5SDimitry Andric /// this check is to disambiguate between an expression and a declaration.
49710b57cec5SDimitry Andric bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
49720b57cec5SDimitry Andric   switch (Tok.getKind()) {
49730b57cec5SDimitry Andric   default: return false;
49740b57cec5SDimitry Andric 
49750b57cec5SDimitry Andric   case tok::kw_pipe:
49760b57cec5SDimitry Andric     return (getLangOpts().OpenCL && getLangOpts().OpenCLVersion >= 200) ||
49770b57cec5SDimitry Andric            getLangOpts().OpenCLCPlusPlus;
49780b57cec5SDimitry Andric 
49790b57cec5SDimitry Andric   case tok::identifier:   // foo::bar
49800b57cec5SDimitry Andric     // Unfortunate hack to support "Class.factoryMethod" notation.
49810b57cec5SDimitry Andric     if (getLangOpts().ObjC && NextToken().is(tok::period))
49820b57cec5SDimitry Andric       return false;
49830b57cec5SDimitry Andric     if (TryAltiVecVectorToken())
49840b57cec5SDimitry Andric       return true;
49850b57cec5SDimitry Andric     LLVM_FALLTHROUGH;
49860b57cec5SDimitry Andric   case tok::kw_decltype: // decltype(T())::type
49870b57cec5SDimitry Andric   case tok::kw_typename: // typename T::type
49880b57cec5SDimitry Andric     // Annotate typenames and C++ scope specifiers.  If we get one, just
49890b57cec5SDimitry Andric     // recurse to handle whatever we get.
49900b57cec5SDimitry Andric     if (TryAnnotateTypeOrScopeToken())
49910b57cec5SDimitry Andric       return true;
499213138422SDimitry Andric     if (TryAnnotateTypeConstraint())
499313138422SDimitry Andric       return true;
49940b57cec5SDimitry Andric     if (Tok.is(tok::identifier))
49950b57cec5SDimitry Andric       return false;
49960b57cec5SDimitry Andric 
49970b57cec5SDimitry Andric     // If we're in Objective-C and we have an Objective-C class type followed
49980b57cec5SDimitry Andric     // by an identifier and then either ':' or ']', in a place where an
49990b57cec5SDimitry Andric     // expression is permitted, then this is probably a class message send
50000b57cec5SDimitry Andric     // missing the initial '['. In this case, we won't consider this to be
50010b57cec5SDimitry Andric     // the start of a declaration.
50020b57cec5SDimitry Andric     if (DisambiguatingWithExpression &&
50030b57cec5SDimitry Andric         isStartOfObjCClassMessageMissingOpenBracket())
50040b57cec5SDimitry Andric       return false;
50050b57cec5SDimitry Andric 
50060b57cec5SDimitry Andric     return isDeclarationSpecifier();
50070b57cec5SDimitry Andric 
50080b57cec5SDimitry Andric   case tok::coloncolon:   // ::foo::bar
50090b57cec5SDimitry Andric     if (NextToken().is(tok::kw_new) ||    // ::new
50100b57cec5SDimitry Andric         NextToken().is(tok::kw_delete))   // ::delete
50110b57cec5SDimitry Andric       return false;
50120b57cec5SDimitry Andric 
50130b57cec5SDimitry Andric     // Annotate typenames and C++ scope specifiers.  If we get one, just
50140b57cec5SDimitry Andric     // recurse to handle whatever we get.
50150b57cec5SDimitry Andric     if (TryAnnotateTypeOrScopeToken())
50160b57cec5SDimitry Andric       return true;
50170b57cec5SDimitry Andric     return isDeclarationSpecifier();
50180b57cec5SDimitry Andric 
50190b57cec5SDimitry Andric     // storage-class-specifier
50200b57cec5SDimitry Andric   case tok::kw_typedef:
50210b57cec5SDimitry Andric   case tok::kw_extern:
50220b57cec5SDimitry Andric   case tok::kw___private_extern__:
50230b57cec5SDimitry Andric   case tok::kw_static:
50240b57cec5SDimitry Andric   case tok::kw_auto:
50250b57cec5SDimitry Andric   case tok::kw___auto_type:
50260b57cec5SDimitry Andric   case tok::kw_register:
50270b57cec5SDimitry Andric   case tok::kw___thread:
50280b57cec5SDimitry Andric   case tok::kw_thread_local:
50290b57cec5SDimitry Andric   case tok::kw__Thread_local:
50300b57cec5SDimitry Andric 
50310b57cec5SDimitry Andric     // Modules
50320b57cec5SDimitry Andric   case tok::kw___module_private__:
50330b57cec5SDimitry Andric 
50340b57cec5SDimitry Andric     // Debugger support
50350b57cec5SDimitry Andric   case tok::kw___unknown_anytype:
50360b57cec5SDimitry Andric 
50370b57cec5SDimitry Andric     // type-specifiers
50380b57cec5SDimitry Andric   case tok::kw_short:
50390b57cec5SDimitry Andric   case tok::kw_long:
50400b57cec5SDimitry Andric   case tok::kw___int64:
50410b57cec5SDimitry Andric   case tok::kw___int128:
50420b57cec5SDimitry Andric   case tok::kw_signed:
50430b57cec5SDimitry Andric   case tok::kw_unsigned:
50440b57cec5SDimitry Andric   case tok::kw__Complex:
50450b57cec5SDimitry Andric   case tok::kw__Imaginary:
50460b57cec5SDimitry Andric   case tok::kw_void:
50470b57cec5SDimitry Andric   case tok::kw_char:
50480b57cec5SDimitry Andric   case tok::kw_wchar_t:
50490b57cec5SDimitry Andric   case tok::kw_char8_t:
50500b57cec5SDimitry Andric   case tok::kw_char16_t:
50510b57cec5SDimitry Andric   case tok::kw_char32_t:
50520b57cec5SDimitry Andric 
50530b57cec5SDimitry Andric   case tok::kw_int:
5054*5ffd83dbSDimitry Andric   case tok::kw__ExtInt:
50550b57cec5SDimitry Andric   case tok::kw_half:
5056*5ffd83dbSDimitry Andric   case tok::kw___bf16:
50570b57cec5SDimitry Andric   case tok::kw_float:
50580b57cec5SDimitry Andric   case tok::kw_double:
50590b57cec5SDimitry Andric   case tok::kw__Accum:
50600b57cec5SDimitry Andric   case tok::kw__Fract:
50610b57cec5SDimitry Andric   case tok::kw__Float16:
50620b57cec5SDimitry Andric   case tok::kw___float128:
50630b57cec5SDimitry Andric   case tok::kw_bool:
50640b57cec5SDimitry Andric   case tok::kw__Bool:
50650b57cec5SDimitry Andric   case tok::kw__Decimal32:
50660b57cec5SDimitry Andric   case tok::kw__Decimal64:
50670b57cec5SDimitry Andric   case tok::kw__Decimal128:
50680b57cec5SDimitry Andric   case tok::kw___vector:
50690b57cec5SDimitry Andric 
50700b57cec5SDimitry Andric     // struct-or-union-specifier (C99) or class-specifier (C++)
50710b57cec5SDimitry Andric   case tok::kw_class:
50720b57cec5SDimitry Andric   case tok::kw_struct:
50730b57cec5SDimitry Andric   case tok::kw_union:
50740b57cec5SDimitry Andric   case tok::kw___interface:
50750b57cec5SDimitry Andric     // enum-specifier
50760b57cec5SDimitry Andric   case tok::kw_enum:
50770b57cec5SDimitry Andric 
50780b57cec5SDimitry Andric     // type-qualifier
50790b57cec5SDimitry Andric   case tok::kw_const:
50800b57cec5SDimitry Andric   case tok::kw_volatile:
50810b57cec5SDimitry Andric   case tok::kw_restrict:
50820b57cec5SDimitry Andric   case tok::kw__Sat:
50830b57cec5SDimitry Andric 
50840b57cec5SDimitry Andric     // function-specifier
50850b57cec5SDimitry Andric   case tok::kw_inline:
50860b57cec5SDimitry Andric   case tok::kw_virtual:
50870b57cec5SDimitry Andric   case tok::kw_explicit:
50880b57cec5SDimitry Andric   case tok::kw__Noreturn:
50890b57cec5SDimitry Andric 
50900b57cec5SDimitry Andric     // alignment-specifier
50910b57cec5SDimitry Andric   case tok::kw__Alignas:
50920b57cec5SDimitry Andric 
50930b57cec5SDimitry Andric     // friend keyword.
50940b57cec5SDimitry Andric   case tok::kw_friend:
50950b57cec5SDimitry Andric 
50960b57cec5SDimitry Andric     // static_assert-declaration
50970b57cec5SDimitry Andric   case tok::kw__Static_assert:
50980b57cec5SDimitry Andric 
50990b57cec5SDimitry Andric     // GNU typeof support.
51000b57cec5SDimitry Andric   case tok::kw_typeof:
51010b57cec5SDimitry Andric 
51020b57cec5SDimitry Andric     // GNU attributes.
51030b57cec5SDimitry Andric   case tok::kw___attribute:
51040b57cec5SDimitry Andric 
51050b57cec5SDimitry Andric     // C++11 decltype and constexpr.
51060b57cec5SDimitry Andric   case tok::annot_decltype:
51070b57cec5SDimitry Andric   case tok::kw_constexpr:
51080b57cec5SDimitry Andric 
5109a7dea167SDimitry Andric     // C++20 consteval and constinit.
51100b57cec5SDimitry Andric   case tok::kw_consteval:
5111a7dea167SDimitry Andric   case tok::kw_constinit:
51120b57cec5SDimitry Andric 
51130b57cec5SDimitry Andric     // C11 _Atomic
51140b57cec5SDimitry Andric   case tok::kw__Atomic:
51150b57cec5SDimitry Andric     return true;
51160b57cec5SDimitry Andric 
51170b57cec5SDimitry Andric     // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
51180b57cec5SDimitry Andric   case tok::less:
51190b57cec5SDimitry Andric     return getLangOpts().ObjC;
51200b57cec5SDimitry Andric 
51210b57cec5SDimitry Andric     // typedef-name
51220b57cec5SDimitry Andric   case tok::annot_typename:
51230b57cec5SDimitry Andric     return !DisambiguatingWithExpression ||
51240b57cec5SDimitry Andric            !isStartOfObjCClassMessageMissingOpenBracket();
51250b57cec5SDimitry Andric 
5126480093f4SDimitry Andric     // placeholder-type-specifier
5127480093f4SDimitry Andric   case tok::annot_template_id: {
5128*5ffd83dbSDimitry Andric     TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
5129*5ffd83dbSDimitry Andric     if (TemplateId->hasInvalidName())
5130*5ffd83dbSDimitry Andric       return true;
5131*5ffd83dbSDimitry Andric     // FIXME: What about type templates that have only been annotated as
5132*5ffd83dbSDimitry Andric     // annot_template_id, not as annot_typename?
513313138422SDimitry Andric     return isTypeConstraintAnnotation() &&
5134480093f4SDimitry Andric            (NextToken().is(tok::kw_auto) || NextToken().is(tok::kw_decltype));
5135480093f4SDimitry Andric   }
5136*5ffd83dbSDimitry Andric 
5137*5ffd83dbSDimitry Andric   case tok::annot_cxxscope: {
5138*5ffd83dbSDimitry Andric     TemplateIdAnnotation *TemplateId =
5139*5ffd83dbSDimitry Andric         NextToken().is(tok::annot_template_id)
5140*5ffd83dbSDimitry Andric             ? takeTemplateIdAnnotation(NextToken())
5141*5ffd83dbSDimitry Andric             : nullptr;
5142*5ffd83dbSDimitry Andric     if (TemplateId && TemplateId->hasInvalidName())
5143*5ffd83dbSDimitry Andric       return true;
5144*5ffd83dbSDimitry Andric     // FIXME: What about type templates that have only been annotated as
5145*5ffd83dbSDimitry Andric     // annot_template_id, not as annot_typename?
514613138422SDimitry Andric     if (NextToken().is(tok::identifier) && TryAnnotateTypeConstraint())
514713138422SDimitry Andric       return true;
514813138422SDimitry Andric     return isTypeConstraintAnnotation() &&
514913138422SDimitry Andric         GetLookAheadToken(2).isOneOf(tok::kw_auto, tok::kw_decltype);
5150*5ffd83dbSDimitry Andric   }
5151*5ffd83dbSDimitry Andric 
51520b57cec5SDimitry Andric   case tok::kw___declspec:
51530b57cec5SDimitry Andric   case tok::kw___cdecl:
51540b57cec5SDimitry Andric   case tok::kw___stdcall:
51550b57cec5SDimitry Andric   case tok::kw___fastcall:
51560b57cec5SDimitry Andric   case tok::kw___thiscall:
51570b57cec5SDimitry Andric   case tok::kw___regcall:
51580b57cec5SDimitry Andric   case tok::kw___vectorcall:
51590b57cec5SDimitry Andric   case tok::kw___w64:
51600b57cec5SDimitry Andric   case tok::kw___sptr:
51610b57cec5SDimitry Andric   case tok::kw___uptr:
51620b57cec5SDimitry Andric   case tok::kw___ptr64:
51630b57cec5SDimitry Andric   case tok::kw___ptr32:
51640b57cec5SDimitry Andric   case tok::kw___forceinline:
51650b57cec5SDimitry Andric   case tok::kw___pascal:
51660b57cec5SDimitry Andric   case tok::kw___unaligned:
51670b57cec5SDimitry Andric 
51680b57cec5SDimitry Andric   case tok::kw__Nonnull:
51690b57cec5SDimitry Andric   case tok::kw__Nullable:
51700b57cec5SDimitry Andric   case tok::kw__Null_unspecified:
51710b57cec5SDimitry Andric 
51720b57cec5SDimitry Andric   case tok::kw___kindof:
51730b57cec5SDimitry Andric 
51740b57cec5SDimitry Andric   case tok::kw___private:
51750b57cec5SDimitry Andric   case tok::kw___local:
51760b57cec5SDimitry Andric   case tok::kw___global:
51770b57cec5SDimitry Andric   case tok::kw___constant:
51780b57cec5SDimitry Andric   case tok::kw___generic:
51790b57cec5SDimitry Andric   case tok::kw___read_only:
51800b57cec5SDimitry Andric   case tok::kw___read_write:
51810b57cec5SDimitry Andric   case tok::kw___write_only:
51820b57cec5SDimitry Andric #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
51830b57cec5SDimitry Andric #include "clang/Basic/OpenCLImageTypes.def"
51840b57cec5SDimitry Andric 
51850b57cec5SDimitry Andric     return true;
51860b57cec5SDimitry Andric 
51870b57cec5SDimitry Andric   case tok::kw_private:
51880b57cec5SDimitry Andric     return getLangOpts().OpenCL;
51890b57cec5SDimitry Andric   }
51900b57cec5SDimitry Andric }
51910b57cec5SDimitry Andric 
51920b57cec5SDimitry Andric bool Parser::isConstructorDeclarator(bool IsUnqualified, bool DeductionGuide) {
51930b57cec5SDimitry Andric   TentativeParsingAction TPA(*this);
51940b57cec5SDimitry Andric 
51950b57cec5SDimitry Andric   // Parse the C++ scope specifier.
51960b57cec5SDimitry Andric   CXXScopeSpec SS;
5197*5ffd83dbSDimitry Andric   if (ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
5198*5ffd83dbSDimitry Andric                                      /*ObjectHadErrors=*/false,
51990b57cec5SDimitry Andric                                      /*EnteringContext=*/true)) {
52000b57cec5SDimitry Andric     TPA.Revert();
52010b57cec5SDimitry Andric     return false;
52020b57cec5SDimitry Andric   }
52030b57cec5SDimitry Andric 
52040b57cec5SDimitry Andric   // Parse the constructor name.
52050b57cec5SDimitry Andric   if (Tok.is(tok::identifier)) {
52060b57cec5SDimitry Andric     // We already know that we have a constructor name; just consume
52070b57cec5SDimitry Andric     // the token.
52080b57cec5SDimitry Andric     ConsumeToken();
52090b57cec5SDimitry Andric   } else if (Tok.is(tok::annot_template_id)) {
52100b57cec5SDimitry Andric     ConsumeAnnotationToken();
52110b57cec5SDimitry Andric   } else {
52120b57cec5SDimitry Andric     TPA.Revert();
52130b57cec5SDimitry Andric     return false;
52140b57cec5SDimitry Andric   }
52150b57cec5SDimitry Andric 
52160b57cec5SDimitry Andric   // There may be attributes here, appertaining to the constructor name or type
52170b57cec5SDimitry Andric   // we just stepped past.
52180b57cec5SDimitry Andric   SkipCXX11Attributes();
52190b57cec5SDimitry Andric 
52200b57cec5SDimitry Andric   // Current class name must be followed by a left parenthesis.
52210b57cec5SDimitry Andric   if (Tok.isNot(tok::l_paren)) {
52220b57cec5SDimitry Andric     TPA.Revert();
52230b57cec5SDimitry Andric     return false;
52240b57cec5SDimitry Andric   }
52250b57cec5SDimitry Andric   ConsumeParen();
52260b57cec5SDimitry Andric 
52270b57cec5SDimitry Andric   // A right parenthesis, or ellipsis followed by a right parenthesis signals
52280b57cec5SDimitry Andric   // that we have a constructor.
52290b57cec5SDimitry Andric   if (Tok.is(tok::r_paren) ||
52300b57cec5SDimitry Andric       (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
52310b57cec5SDimitry Andric     TPA.Revert();
52320b57cec5SDimitry Andric     return true;
52330b57cec5SDimitry Andric   }
52340b57cec5SDimitry Andric 
52350b57cec5SDimitry Andric   // A C++11 attribute here signals that we have a constructor, and is an
52360b57cec5SDimitry Andric   // attribute on the first constructor parameter.
52370b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus11 &&
52380b57cec5SDimitry Andric       isCXX11AttributeSpecifier(/*Disambiguate*/ false,
52390b57cec5SDimitry Andric                                 /*OuterMightBeMessageSend*/ true)) {
52400b57cec5SDimitry Andric     TPA.Revert();
52410b57cec5SDimitry Andric     return true;
52420b57cec5SDimitry Andric   }
52430b57cec5SDimitry Andric 
52440b57cec5SDimitry Andric   // If we need to, enter the specified scope.
52450b57cec5SDimitry Andric   DeclaratorScopeObj DeclScopeObj(*this, SS);
52460b57cec5SDimitry Andric   if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
52470b57cec5SDimitry Andric     DeclScopeObj.EnterDeclaratorScope();
52480b57cec5SDimitry Andric 
52490b57cec5SDimitry Andric   // Optionally skip Microsoft attributes.
52500b57cec5SDimitry Andric   ParsedAttributes Attrs(AttrFactory);
52510b57cec5SDimitry Andric   MaybeParseMicrosoftAttributes(Attrs);
52520b57cec5SDimitry Andric 
52530b57cec5SDimitry Andric   // Check whether the next token(s) are part of a declaration
52540b57cec5SDimitry Andric   // specifier, in which case we have the start of a parameter and,
52550b57cec5SDimitry Andric   // therefore, we know that this is a constructor.
52560b57cec5SDimitry Andric   bool IsConstructor = false;
52570b57cec5SDimitry Andric   if (isDeclarationSpecifier())
52580b57cec5SDimitry Andric     IsConstructor = true;
52590b57cec5SDimitry Andric   else if (Tok.is(tok::identifier) ||
52600b57cec5SDimitry Andric            (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
52610b57cec5SDimitry Andric     // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
52620b57cec5SDimitry Andric     // This might be a parenthesized member name, but is more likely to
52630b57cec5SDimitry Andric     // be a constructor declaration with an invalid argument type. Keep
52640b57cec5SDimitry Andric     // looking.
52650b57cec5SDimitry Andric     if (Tok.is(tok::annot_cxxscope))
52660b57cec5SDimitry Andric       ConsumeAnnotationToken();
52670b57cec5SDimitry Andric     ConsumeToken();
52680b57cec5SDimitry Andric 
52690b57cec5SDimitry Andric     // If this is not a constructor, we must be parsing a declarator,
52700b57cec5SDimitry Andric     // which must have one of the following syntactic forms (see the
52710b57cec5SDimitry Andric     // grammar extract at the start of ParseDirectDeclarator):
52720b57cec5SDimitry Andric     switch (Tok.getKind()) {
52730b57cec5SDimitry Andric     case tok::l_paren:
52740b57cec5SDimitry Andric       // C(X   (   int));
52750b57cec5SDimitry Andric     case tok::l_square:
52760b57cec5SDimitry Andric       // C(X   [   5]);
52770b57cec5SDimitry Andric       // C(X   [   [attribute]]);
52780b57cec5SDimitry Andric     case tok::coloncolon:
52790b57cec5SDimitry Andric       // C(X   ::   Y);
52800b57cec5SDimitry Andric       // C(X   ::   *p);
52810b57cec5SDimitry Andric       // Assume this isn't a constructor, rather than assuming it's a
52820b57cec5SDimitry Andric       // constructor with an unnamed parameter of an ill-formed type.
52830b57cec5SDimitry Andric       break;
52840b57cec5SDimitry Andric 
52850b57cec5SDimitry Andric     case tok::r_paren:
52860b57cec5SDimitry Andric       // C(X   )
52870b57cec5SDimitry Andric 
52880b57cec5SDimitry Andric       // Skip past the right-paren and any following attributes to get to
52890b57cec5SDimitry Andric       // the function body or trailing-return-type.
52900b57cec5SDimitry Andric       ConsumeParen();
52910b57cec5SDimitry Andric       SkipCXX11Attributes();
52920b57cec5SDimitry Andric 
52930b57cec5SDimitry Andric       if (DeductionGuide) {
52940b57cec5SDimitry Andric         // C(X) -> ... is a deduction guide.
52950b57cec5SDimitry Andric         IsConstructor = Tok.is(tok::arrow);
52960b57cec5SDimitry Andric         break;
52970b57cec5SDimitry Andric       }
52980b57cec5SDimitry Andric       if (Tok.is(tok::colon) || Tok.is(tok::kw_try)) {
52990b57cec5SDimitry Andric         // Assume these were meant to be constructors:
53000b57cec5SDimitry Andric         //   C(X)   :    (the name of a bit-field cannot be parenthesized).
53010b57cec5SDimitry Andric         //   C(X)   try  (this is otherwise ill-formed).
53020b57cec5SDimitry Andric         IsConstructor = true;
53030b57cec5SDimitry Andric       }
53040b57cec5SDimitry Andric       if (Tok.is(tok::semi) || Tok.is(tok::l_brace)) {
53050b57cec5SDimitry Andric         // If we have a constructor name within the class definition,
53060b57cec5SDimitry Andric         // assume these were meant to be constructors:
53070b57cec5SDimitry Andric         //   C(X)   {
53080b57cec5SDimitry Andric         //   C(X)   ;
53090b57cec5SDimitry Andric         // ... because otherwise we would be declaring a non-static data
53100b57cec5SDimitry Andric         // member that is ill-formed because it's of the same type as its
53110b57cec5SDimitry Andric         // surrounding class.
53120b57cec5SDimitry Andric         //
53130b57cec5SDimitry Andric         // FIXME: We can actually do this whether or not the name is qualified,
53140b57cec5SDimitry Andric         // because if it is qualified in this context it must be being used as
53150b57cec5SDimitry Andric         // a constructor name.
53160b57cec5SDimitry Andric         // currently, so we're somewhat conservative here.
53170b57cec5SDimitry Andric         IsConstructor = IsUnqualified;
53180b57cec5SDimitry Andric       }
53190b57cec5SDimitry Andric       break;
53200b57cec5SDimitry Andric 
53210b57cec5SDimitry Andric     default:
53220b57cec5SDimitry Andric       IsConstructor = true;
53230b57cec5SDimitry Andric       break;
53240b57cec5SDimitry Andric     }
53250b57cec5SDimitry Andric   }
53260b57cec5SDimitry Andric 
53270b57cec5SDimitry Andric   TPA.Revert();
53280b57cec5SDimitry Andric   return IsConstructor;
53290b57cec5SDimitry Andric }
53300b57cec5SDimitry Andric 
53310b57cec5SDimitry Andric /// ParseTypeQualifierListOpt
53320b57cec5SDimitry Andric ///          type-qualifier-list: [C99 6.7.5]
53330b57cec5SDimitry Andric ///            type-qualifier
53340b57cec5SDimitry Andric /// [vendor]   attributes
53350b57cec5SDimitry Andric ///              [ only if AttrReqs & AR_VendorAttributesParsed ]
53360b57cec5SDimitry Andric ///            type-qualifier-list type-qualifier
53370b57cec5SDimitry Andric /// [vendor]   type-qualifier-list attributes
53380b57cec5SDimitry Andric ///              [ only if AttrReqs & AR_VendorAttributesParsed ]
53390b57cec5SDimitry Andric /// [C++0x]    attribute-specifier[opt] is allowed before cv-qualifier-seq
53400b57cec5SDimitry Andric ///              [ only if AttReqs & AR_CXX11AttributesParsed ]
53410b57cec5SDimitry Andric /// Note: vendor can be GNU, MS, etc and can be explicitly controlled via
53420b57cec5SDimitry Andric /// AttrRequirements bitmask values.
53430b57cec5SDimitry Andric void Parser::ParseTypeQualifierListOpt(
53440b57cec5SDimitry Andric     DeclSpec &DS, unsigned AttrReqs, bool AtomicAllowed,
53450b57cec5SDimitry Andric     bool IdentifierRequired,
53460b57cec5SDimitry Andric     Optional<llvm::function_ref<void()>> CodeCompletionHandler) {
53470b57cec5SDimitry Andric   if (standardAttributesAllowed() && (AttrReqs & AR_CXX11AttributesParsed) &&
53480b57cec5SDimitry Andric       isCXX11AttributeSpecifier()) {
53490b57cec5SDimitry Andric     ParsedAttributesWithRange attrs(AttrFactory);
53500b57cec5SDimitry Andric     ParseCXX11Attributes(attrs);
53510b57cec5SDimitry Andric     DS.takeAttributesFrom(attrs);
53520b57cec5SDimitry Andric   }
53530b57cec5SDimitry Andric 
53540b57cec5SDimitry Andric   SourceLocation EndLoc;
53550b57cec5SDimitry Andric 
53560b57cec5SDimitry Andric   while (1) {
53570b57cec5SDimitry Andric     bool isInvalid = false;
53580b57cec5SDimitry Andric     const char *PrevSpec = nullptr;
53590b57cec5SDimitry Andric     unsigned DiagID = 0;
53600b57cec5SDimitry Andric     SourceLocation Loc = Tok.getLocation();
53610b57cec5SDimitry Andric 
53620b57cec5SDimitry Andric     switch (Tok.getKind()) {
53630b57cec5SDimitry Andric     case tok::code_completion:
53640b57cec5SDimitry Andric       if (CodeCompletionHandler)
53650b57cec5SDimitry Andric         (*CodeCompletionHandler)();
53660b57cec5SDimitry Andric       else
53670b57cec5SDimitry Andric         Actions.CodeCompleteTypeQualifiers(DS);
53680b57cec5SDimitry Andric       return cutOffParsing();
53690b57cec5SDimitry Andric 
53700b57cec5SDimitry Andric     case tok::kw_const:
53710b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_const   , Loc, PrevSpec, DiagID,
53720b57cec5SDimitry Andric                                  getLangOpts());
53730b57cec5SDimitry Andric       break;
53740b57cec5SDimitry Andric     case tok::kw_volatile:
53750b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
53760b57cec5SDimitry Andric                                  getLangOpts());
53770b57cec5SDimitry Andric       break;
53780b57cec5SDimitry Andric     case tok::kw_restrict:
53790b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
53800b57cec5SDimitry Andric                                  getLangOpts());
53810b57cec5SDimitry Andric       break;
53820b57cec5SDimitry Andric     case tok::kw__Atomic:
53830b57cec5SDimitry Andric       if (!AtomicAllowed)
53840b57cec5SDimitry Andric         goto DoneWithTypeQuals;
5385a7dea167SDimitry Andric       if (!getLangOpts().C11)
5386a7dea167SDimitry Andric         Diag(Tok, diag::ext_c11_feature) << Tok.getName();
53870b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
53880b57cec5SDimitry Andric                                  getLangOpts());
53890b57cec5SDimitry Andric       break;
53900b57cec5SDimitry Andric 
53910b57cec5SDimitry Andric     // OpenCL qualifiers:
53920b57cec5SDimitry Andric     case tok::kw_private:
53930b57cec5SDimitry Andric       if (!getLangOpts().OpenCL)
53940b57cec5SDimitry Andric         goto DoneWithTypeQuals;
53950b57cec5SDimitry Andric       LLVM_FALLTHROUGH;
53960b57cec5SDimitry Andric     case tok::kw___private:
53970b57cec5SDimitry Andric     case tok::kw___global:
53980b57cec5SDimitry Andric     case tok::kw___local:
53990b57cec5SDimitry Andric     case tok::kw___constant:
54000b57cec5SDimitry Andric     case tok::kw___generic:
54010b57cec5SDimitry Andric     case tok::kw___read_only:
54020b57cec5SDimitry Andric     case tok::kw___write_only:
54030b57cec5SDimitry Andric     case tok::kw___read_write:
54040b57cec5SDimitry Andric       ParseOpenCLQualifiers(DS.getAttributes());
54050b57cec5SDimitry Andric       break;
54060b57cec5SDimitry Andric 
54070b57cec5SDimitry Andric     case tok::kw___unaligned:
54080b57cec5SDimitry Andric       isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID,
54090b57cec5SDimitry Andric                                  getLangOpts());
54100b57cec5SDimitry Andric       break;
54110b57cec5SDimitry Andric     case tok::kw___uptr:
54120b57cec5SDimitry Andric       // GNU libc headers in C mode use '__uptr' as an identifier which conflicts
54130b57cec5SDimitry Andric       // with the MS modifier keyword.
54140b57cec5SDimitry Andric       if ((AttrReqs & AR_DeclspecAttributesParsed) && !getLangOpts().CPlusPlus &&
54150b57cec5SDimitry Andric           IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
54160b57cec5SDimitry Andric         if (TryKeywordIdentFallback(false))
54170b57cec5SDimitry Andric           continue;
54180b57cec5SDimitry Andric       }
54190b57cec5SDimitry Andric       LLVM_FALLTHROUGH;
54200b57cec5SDimitry Andric     case tok::kw___sptr:
54210b57cec5SDimitry Andric     case tok::kw___w64:
54220b57cec5SDimitry Andric     case tok::kw___ptr64:
54230b57cec5SDimitry Andric     case tok::kw___ptr32:
54240b57cec5SDimitry Andric     case tok::kw___cdecl:
54250b57cec5SDimitry Andric     case tok::kw___stdcall:
54260b57cec5SDimitry Andric     case tok::kw___fastcall:
54270b57cec5SDimitry Andric     case tok::kw___thiscall:
54280b57cec5SDimitry Andric     case tok::kw___regcall:
54290b57cec5SDimitry Andric     case tok::kw___vectorcall:
54300b57cec5SDimitry Andric       if (AttrReqs & AR_DeclspecAttributesParsed) {
54310b57cec5SDimitry Andric         ParseMicrosoftTypeAttributes(DS.getAttributes());
54320b57cec5SDimitry Andric         continue;
54330b57cec5SDimitry Andric       }
54340b57cec5SDimitry Andric       goto DoneWithTypeQuals;
54350b57cec5SDimitry Andric     case tok::kw___pascal:
54360b57cec5SDimitry Andric       if (AttrReqs & AR_VendorAttributesParsed) {
54370b57cec5SDimitry Andric         ParseBorlandTypeAttributes(DS.getAttributes());
54380b57cec5SDimitry Andric         continue;
54390b57cec5SDimitry Andric       }
54400b57cec5SDimitry Andric       goto DoneWithTypeQuals;
54410b57cec5SDimitry Andric 
54420b57cec5SDimitry Andric     // Nullability type specifiers.
54430b57cec5SDimitry Andric     case tok::kw__Nonnull:
54440b57cec5SDimitry Andric     case tok::kw__Nullable:
54450b57cec5SDimitry Andric     case tok::kw__Null_unspecified:
54460b57cec5SDimitry Andric       ParseNullabilityTypeSpecifiers(DS.getAttributes());
54470b57cec5SDimitry Andric       continue;
54480b57cec5SDimitry Andric 
54490b57cec5SDimitry Andric     // Objective-C 'kindof' types.
54500b57cec5SDimitry Andric     case tok::kw___kindof:
54510b57cec5SDimitry Andric       DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc,
54520b57cec5SDimitry Andric                                 nullptr, 0, ParsedAttr::AS_Keyword);
54530b57cec5SDimitry Andric       (void)ConsumeToken();
54540b57cec5SDimitry Andric       continue;
54550b57cec5SDimitry Andric 
54560b57cec5SDimitry Andric     case tok::kw___attribute:
54570b57cec5SDimitry Andric       if (AttrReqs & AR_GNUAttributesParsedAndRejected)
54580b57cec5SDimitry Andric         // When GNU attributes are expressly forbidden, diagnose their usage.
54590b57cec5SDimitry Andric         Diag(Tok, diag::err_attributes_not_allowed);
54600b57cec5SDimitry Andric 
54610b57cec5SDimitry Andric       // Parse the attributes even if they are rejected to ensure that error
54620b57cec5SDimitry Andric       // recovery is graceful.
54630b57cec5SDimitry Andric       if (AttrReqs & AR_GNUAttributesParsed ||
54640b57cec5SDimitry Andric           AttrReqs & AR_GNUAttributesParsedAndRejected) {
54650b57cec5SDimitry Andric         ParseGNUAttributes(DS.getAttributes());
54660b57cec5SDimitry Andric         continue; // do *not* consume the next token!
54670b57cec5SDimitry Andric       }
54680b57cec5SDimitry Andric       // otherwise, FALL THROUGH!
54690b57cec5SDimitry Andric       LLVM_FALLTHROUGH;
54700b57cec5SDimitry Andric     default:
54710b57cec5SDimitry Andric       DoneWithTypeQuals:
54720b57cec5SDimitry Andric       // If this is not a type-qualifier token, we're done reading type
54730b57cec5SDimitry Andric       // qualifiers.  First verify that DeclSpec's are consistent.
54740b57cec5SDimitry Andric       DS.Finish(Actions, Actions.getASTContext().getPrintingPolicy());
54750b57cec5SDimitry Andric       if (EndLoc.isValid())
54760b57cec5SDimitry Andric         DS.SetRangeEnd(EndLoc);
54770b57cec5SDimitry Andric       return;
54780b57cec5SDimitry Andric     }
54790b57cec5SDimitry Andric 
54800b57cec5SDimitry Andric     // If the specifier combination wasn't legal, issue a diagnostic.
54810b57cec5SDimitry Andric     if (isInvalid) {
54820b57cec5SDimitry Andric       assert(PrevSpec && "Method did not return previous specifier!");
54830b57cec5SDimitry Andric       Diag(Tok, DiagID) << PrevSpec;
54840b57cec5SDimitry Andric     }
54850b57cec5SDimitry Andric     EndLoc = ConsumeToken();
54860b57cec5SDimitry Andric   }
54870b57cec5SDimitry Andric }
54880b57cec5SDimitry Andric 
54890b57cec5SDimitry Andric /// ParseDeclarator - Parse and verify a newly-initialized declarator.
54900b57cec5SDimitry Andric ///
54910b57cec5SDimitry Andric void Parser::ParseDeclarator(Declarator &D) {
54920b57cec5SDimitry Andric   /// This implements the 'declarator' production in the C grammar, then checks
54930b57cec5SDimitry Andric   /// for well-formedness and issues diagnostics.
54940b57cec5SDimitry Andric   ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
54950b57cec5SDimitry Andric }
54960b57cec5SDimitry Andric 
54970b57cec5SDimitry Andric static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang,
54980b57cec5SDimitry Andric                                DeclaratorContext TheContext) {
54990b57cec5SDimitry Andric   if (Kind == tok::star || Kind == tok::caret)
55000b57cec5SDimitry Andric     return true;
55010b57cec5SDimitry Andric 
55020b57cec5SDimitry Andric   if (Kind == tok::kw_pipe &&
55030b57cec5SDimitry Andric       ((Lang.OpenCL && Lang.OpenCLVersion >= 200) || Lang.OpenCLCPlusPlus))
55040b57cec5SDimitry Andric     return true;
55050b57cec5SDimitry Andric 
55060b57cec5SDimitry Andric   if (!Lang.CPlusPlus)
55070b57cec5SDimitry Andric     return false;
55080b57cec5SDimitry Andric 
55090b57cec5SDimitry Andric   if (Kind == tok::amp)
55100b57cec5SDimitry Andric     return true;
55110b57cec5SDimitry Andric 
55120b57cec5SDimitry Andric   // We parse rvalue refs in C++03, because otherwise the errors are scary.
55130b57cec5SDimitry Andric   // But we must not parse them in conversion-type-ids and new-type-ids, since
55140b57cec5SDimitry Andric   // those can be legitimately followed by a && operator.
55150b57cec5SDimitry Andric   // (The same thing can in theory happen after a trailing-return-type, but
55160b57cec5SDimitry Andric   // since those are a C++11 feature, there is no rejects-valid issue there.)
55170b57cec5SDimitry Andric   if (Kind == tok::ampamp)
55180b57cec5SDimitry Andric     return Lang.CPlusPlus11 ||
55190b57cec5SDimitry Andric            (TheContext != DeclaratorContext::ConversionIdContext &&
55200b57cec5SDimitry Andric             TheContext != DeclaratorContext::CXXNewContext);
55210b57cec5SDimitry Andric 
55220b57cec5SDimitry Andric   return false;
55230b57cec5SDimitry Andric }
55240b57cec5SDimitry Andric 
55250b57cec5SDimitry Andric // Indicates whether the given declarator is a pipe declarator.
55260b57cec5SDimitry Andric static bool isPipeDeclerator(const Declarator &D) {
55270b57cec5SDimitry Andric   const unsigned NumTypes = D.getNumTypeObjects();
55280b57cec5SDimitry Andric 
55290b57cec5SDimitry Andric   for (unsigned Idx = 0; Idx != NumTypes; ++Idx)
55300b57cec5SDimitry Andric     if (DeclaratorChunk::Pipe == D.getTypeObject(Idx).Kind)
55310b57cec5SDimitry Andric       return true;
55320b57cec5SDimitry Andric 
55330b57cec5SDimitry Andric   return false;
55340b57cec5SDimitry Andric }
55350b57cec5SDimitry Andric 
55360b57cec5SDimitry Andric /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
55370b57cec5SDimitry Andric /// is parsed by the function passed to it. Pass null, and the direct-declarator
55380b57cec5SDimitry Andric /// isn't parsed at all, making this function effectively parse the C++
55390b57cec5SDimitry Andric /// ptr-operator production.
55400b57cec5SDimitry Andric ///
55410b57cec5SDimitry Andric /// If the grammar of this construct is extended, matching changes must also be
55420b57cec5SDimitry Andric /// made to TryParseDeclarator and MightBeDeclarator, and possibly to
55430b57cec5SDimitry Andric /// isConstructorDeclarator.
55440b57cec5SDimitry Andric ///
55450b57cec5SDimitry Andric ///       declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
55460b57cec5SDimitry Andric /// [C]     pointer[opt] direct-declarator
55470b57cec5SDimitry Andric /// [C++]   direct-declarator
55480b57cec5SDimitry Andric /// [C++]   ptr-operator declarator
55490b57cec5SDimitry Andric ///
55500b57cec5SDimitry Andric ///       pointer: [C99 6.7.5]
55510b57cec5SDimitry Andric ///         '*' type-qualifier-list[opt]
55520b57cec5SDimitry Andric ///         '*' type-qualifier-list[opt] pointer
55530b57cec5SDimitry Andric ///
55540b57cec5SDimitry Andric ///       ptr-operator:
55550b57cec5SDimitry Andric ///         '*' cv-qualifier-seq[opt]
55560b57cec5SDimitry Andric ///         '&'
55570b57cec5SDimitry Andric /// [C++0x] '&&'
55580b57cec5SDimitry Andric /// [GNU]   '&' restrict[opt] attributes[opt]
55590b57cec5SDimitry Andric /// [GNU?]  '&&' restrict[opt] attributes[opt]
55600b57cec5SDimitry Andric ///         '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
55610b57cec5SDimitry Andric void Parser::ParseDeclaratorInternal(Declarator &D,
55620b57cec5SDimitry Andric                                      DirectDeclParseFunction DirectDeclParser) {
55630b57cec5SDimitry Andric   if (Diags.hasAllExtensionsSilenced())
55640b57cec5SDimitry Andric     D.setExtension();
55650b57cec5SDimitry Andric 
55660b57cec5SDimitry Andric   // C++ member pointers start with a '::' or a nested-name.
55670b57cec5SDimitry Andric   // Member pointers get special handling, since there's no place for the
55680b57cec5SDimitry Andric   // scope spec in the generic path below.
55690b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus &&
55700b57cec5SDimitry Andric       (Tok.is(tok::coloncolon) || Tok.is(tok::kw_decltype) ||
55710b57cec5SDimitry Andric        (Tok.is(tok::identifier) &&
55720b57cec5SDimitry Andric         (NextToken().is(tok::coloncolon) || NextToken().is(tok::less))) ||
55730b57cec5SDimitry Andric        Tok.is(tok::annot_cxxscope))) {
55740b57cec5SDimitry Andric     bool EnteringContext =
55750b57cec5SDimitry Andric         D.getContext() == DeclaratorContext::FileContext ||
55760b57cec5SDimitry Andric         D.getContext() == DeclaratorContext::MemberContext;
55770b57cec5SDimitry Andric     CXXScopeSpec SS;
5578*5ffd83dbSDimitry Andric     ParseOptionalCXXScopeSpecifier(SS, /*ObjectType=*/nullptr,
5579*5ffd83dbSDimitry Andric                                    /*ObjectHadErrors=*/false, EnteringContext);
55800b57cec5SDimitry Andric 
55810b57cec5SDimitry Andric     if (SS.isNotEmpty()) {
55820b57cec5SDimitry Andric       if (Tok.isNot(tok::star)) {
55830b57cec5SDimitry Andric         // The scope spec really belongs to the direct-declarator.
55840b57cec5SDimitry Andric         if (D.mayHaveIdentifier())
55850b57cec5SDimitry Andric           D.getCXXScopeSpec() = SS;
55860b57cec5SDimitry Andric         else
55870b57cec5SDimitry Andric           AnnotateScopeToken(SS, true);
55880b57cec5SDimitry Andric 
55890b57cec5SDimitry Andric         if (DirectDeclParser)
55900b57cec5SDimitry Andric           (this->*DirectDeclParser)(D);
55910b57cec5SDimitry Andric         return;
55920b57cec5SDimitry Andric       }
55930b57cec5SDimitry Andric 
5594*5ffd83dbSDimitry Andric       SourceLocation StarLoc = ConsumeToken();
5595*5ffd83dbSDimitry Andric       D.SetRangeEnd(StarLoc);
55960b57cec5SDimitry Andric       DeclSpec DS(AttrFactory);
55970b57cec5SDimitry Andric       ParseTypeQualifierListOpt(DS);
55980b57cec5SDimitry Andric       D.ExtendWithDeclSpec(DS);
55990b57cec5SDimitry Andric 
56000b57cec5SDimitry Andric       // Recurse to parse whatever is left.
56010b57cec5SDimitry Andric       ParseDeclaratorInternal(D, DirectDeclParser);
56020b57cec5SDimitry Andric 
56030b57cec5SDimitry Andric       // Sema will have to catch (syntactically invalid) pointers into global
56040b57cec5SDimitry Andric       // scope. It has to catch pointers into namespace scope anyway.
56050b57cec5SDimitry Andric       D.AddTypeInfo(DeclaratorChunk::getMemberPointer(
5606*5ffd83dbSDimitry Andric                         SS, DS.getTypeQualifiers(), StarLoc, DS.getEndLoc()),
56070b57cec5SDimitry Andric                     std::move(DS.getAttributes()),
56080b57cec5SDimitry Andric                     /* Don't replace range end. */ SourceLocation());
56090b57cec5SDimitry Andric       return;
56100b57cec5SDimitry Andric     }
56110b57cec5SDimitry Andric   }
56120b57cec5SDimitry Andric 
56130b57cec5SDimitry Andric   tok::TokenKind Kind = Tok.getKind();
56140b57cec5SDimitry Andric 
56150b57cec5SDimitry Andric   if (D.getDeclSpec().isTypeSpecPipe() && !isPipeDeclerator(D)) {
56160b57cec5SDimitry Andric     DeclSpec DS(AttrFactory);
56170b57cec5SDimitry Andric     ParseTypeQualifierListOpt(DS);
56180b57cec5SDimitry Andric 
56190b57cec5SDimitry Andric     D.AddTypeInfo(
56200b57cec5SDimitry Andric         DeclaratorChunk::getPipe(DS.getTypeQualifiers(), DS.getPipeLoc()),
56210b57cec5SDimitry Andric         std::move(DS.getAttributes()), SourceLocation());
56220b57cec5SDimitry Andric   }
56230b57cec5SDimitry Andric 
56240b57cec5SDimitry Andric   // Not a pointer, C++ reference, or block.
56250b57cec5SDimitry Andric   if (!isPtrOperatorToken(Kind, getLangOpts(), D.getContext())) {
56260b57cec5SDimitry Andric     if (DirectDeclParser)
56270b57cec5SDimitry Andric       (this->*DirectDeclParser)(D);
56280b57cec5SDimitry Andric     return;
56290b57cec5SDimitry Andric   }
56300b57cec5SDimitry Andric 
56310b57cec5SDimitry Andric   // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
56320b57cec5SDimitry Andric   // '&&' -> rvalue reference
56330b57cec5SDimitry Andric   SourceLocation Loc = ConsumeToken();  // Eat the *, ^, & or &&.
56340b57cec5SDimitry Andric   D.SetRangeEnd(Loc);
56350b57cec5SDimitry Andric 
56360b57cec5SDimitry Andric   if (Kind == tok::star || Kind == tok::caret) {
56370b57cec5SDimitry Andric     // Is a pointer.
56380b57cec5SDimitry Andric     DeclSpec DS(AttrFactory);
56390b57cec5SDimitry Andric 
56400b57cec5SDimitry Andric     // GNU attributes are not allowed here in a new-type-id, but Declspec and
56410b57cec5SDimitry Andric     // C++11 attributes are allowed.
56420b57cec5SDimitry Andric     unsigned Reqs = AR_CXX11AttributesParsed | AR_DeclspecAttributesParsed |
56430b57cec5SDimitry Andric                     ((D.getContext() != DeclaratorContext::CXXNewContext)
56440b57cec5SDimitry Andric                          ? AR_GNUAttributesParsed
56450b57cec5SDimitry Andric                          : AR_GNUAttributesParsedAndRejected);
56460b57cec5SDimitry Andric     ParseTypeQualifierListOpt(DS, Reqs, true, !D.mayOmitIdentifier());
56470b57cec5SDimitry Andric     D.ExtendWithDeclSpec(DS);
56480b57cec5SDimitry Andric 
56490b57cec5SDimitry Andric     // Recursively parse the declarator.
56500b57cec5SDimitry Andric     ParseDeclaratorInternal(D, DirectDeclParser);
56510b57cec5SDimitry Andric     if (Kind == tok::star)
56520b57cec5SDimitry Andric       // Remember that we parsed a pointer type, and remember the type-quals.
56530b57cec5SDimitry Andric       D.AddTypeInfo(DeclaratorChunk::getPointer(
56540b57cec5SDimitry Andric                         DS.getTypeQualifiers(), Loc, DS.getConstSpecLoc(),
56550b57cec5SDimitry Andric                         DS.getVolatileSpecLoc(), DS.getRestrictSpecLoc(),
56560b57cec5SDimitry Andric                         DS.getAtomicSpecLoc(), DS.getUnalignedSpecLoc()),
56570b57cec5SDimitry Andric                     std::move(DS.getAttributes()), SourceLocation());
56580b57cec5SDimitry Andric     else
56590b57cec5SDimitry Andric       // Remember that we parsed a Block type, and remember the type-quals.
56600b57cec5SDimitry Andric       D.AddTypeInfo(
56610b57cec5SDimitry Andric           DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(), Loc),
56620b57cec5SDimitry Andric           std::move(DS.getAttributes()), SourceLocation());
56630b57cec5SDimitry Andric   } else {
56640b57cec5SDimitry Andric     // Is a reference
56650b57cec5SDimitry Andric     DeclSpec DS(AttrFactory);
56660b57cec5SDimitry Andric 
56670b57cec5SDimitry Andric     // Complain about rvalue references in C++03, but then go on and build
56680b57cec5SDimitry Andric     // the declarator.
56690b57cec5SDimitry Andric     if (Kind == tok::ampamp)
56700b57cec5SDimitry Andric       Diag(Loc, getLangOpts().CPlusPlus11 ?
56710b57cec5SDimitry Andric            diag::warn_cxx98_compat_rvalue_reference :
56720b57cec5SDimitry Andric            diag::ext_rvalue_reference);
56730b57cec5SDimitry Andric 
56740b57cec5SDimitry Andric     // GNU-style and C++11 attributes are allowed here, as is restrict.
56750b57cec5SDimitry Andric     ParseTypeQualifierListOpt(DS);
56760b57cec5SDimitry Andric     D.ExtendWithDeclSpec(DS);
56770b57cec5SDimitry Andric 
56780b57cec5SDimitry Andric     // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
56790b57cec5SDimitry Andric     // cv-qualifiers are introduced through the use of a typedef or of a
56800b57cec5SDimitry Andric     // template type argument, in which case the cv-qualifiers are ignored.
56810b57cec5SDimitry Andric     if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
56820b57cec5SDimitry Andric       if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
56830b57cec5SDimitry Andric         Diag(DS.getConstSpecLoc(),
56840b57cec5SDimitry Andric              diag::err_invalid_reference_qualifier_application) << "const";
56850b57cec5SDimitry Andric       if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
56860b57cec5SDimitry Andric         Diag(DS.getVolatileSpecLoc(),
56870b57cec5SDimitry Andric              diag::err_invalid_reference_qualifier_application) << "volatile";
56880b57cec5SDimitry Andric       // 'restrict' is permitted as an extension.
56890b57cec5SDimitry Andric       if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
56900b57cec5SDimitry Andric         Diag(DS.getAtomicSpecLoc(),
56910b57cec5SDimitry Andric              diag::err_invalid_reference_qualifier_application) << "_Atomic";
56920b57cec5SDimitry Andric     }
56930b57cec5SDimitry Andric 
56940b57cec5SDimitry Andric     // Recursively parse the declarator.
56950b57cec5SDimitry Andric     ParseDeclaratorInternal(D, DirectDeclParser);
56960b57cec5SDimitry Andric 
56970b57cec5SDimitry Andric     if (D.getNumTypeObjects() > 0) {
56980b57cec5SDimitry Andric       // C++ [dcl.ref]p4: There shall be no references to references.
56990b57cec5SDimitry Andric       DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
57000b57cec5SDimitry Andric       if (InnerChunk.Kind == DeclaratorChunk::Reference) {
57010b57cec5SDimitry Andric         if (const IdentifierInfo *II = D.getIdentifier())
57020b57cec5SDimitry Andric           Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
57030b57cec5SDimitry Andric            << II;
57040b57cec5SDimitry Andric         else
57050b57cec5SDimitry Andric           Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
57060b57cec5SDimitry Andric             << "type name";
57070b57cec5SDimitry Andric 
57080b57cec5SDimitry Andric         // Once we've complained about the reference-to-reference, we
57090b57cec5SDimitry Andric         // can go ahead and build the (technically ill-formed)
57100b57cec5SDimitry Andric         // declarator: reference collapsing will take care of it.
57110b57cec5SDimitry Andric       }
57120b57cec5SDimitry Andric     }
57130b57cec5SDimitry Andric 
57140b57cec5SDimitry Andric     // Remember that we parsed a reference type.
57150b57cec5SDimitry Andric     D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
57160b57cec5SDimitry Andric                                                 Kind == tok::amp),
57170b57cec5SDimitry Andric                   std::move(DS.getAttributes()), SourceLocation());
57180b57cec5SDimitry Andric   }
57190b57cec5SDimitry Andric }
57200b57cec5SDimitry Andric 
57210b57cec5SDimitry Andric // When correcting from misplaced brackets before the identifier, the location
57220b57cec5SDimitry Andric // is saved inside the declarator so that other diagnostic messages can use
57230b57cec5SDimitry Andric // them.  This extracts and returns that location, or returns the provided
57240b57cec5SDimitry Andric // location if a stored location does not exist.
57250b57cec5SDimitry Andric static SourceLocation getMissingDeclaratorIdLoc(Declarator &D,
57260b57cec5SDimitry Andric                                                 SourceLocation Loc) {
57270b57cec5SDimitry Andric   if (D.getName().StartLocation.isInvalid() &&
57280b57cec5SDimitry Andric       D.getName().EndLocation.isValid())
57290b57cec5SDimitry Andric     return D.getName().EndLocation;
57300b57cec5SDimitry Andric 
57310b57cec5SDimitry Andric   return Loc;
57320b57cec5SDimitry Andric }
57330b57cec5SDimitry Andric 
57340b57cec5SDimitry Andric /// ParseDirectDeclarator
57350b57cec5SDimitry Andric ///       direct-declarator: [C99 6.7.5]
57360b57cec5SDimitry Andric /// [C99]   identifier
57370b57cec5SDimitry Andric ///         '(' declarator ')'
57380b57cec5SDimitry Andric /// [GNU]   '(' attributes declarator ')'
57390b57cec5SDimitry Andric /// [C90]   direct-declarator '[' constant-expression[opt] ']'
57400b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
57410b57cec5SDimitry Andric /// [C99]   direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
57420b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list 'static' assignment-expr ']'
57430b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list[opt] '*' ']'
57440b57cec5SDimitry Andric /// [C++11] direct-declarator '[' constant-expression[opt] ']'
57450b57cec5SDimitry Andric ///                    attribute-specifier-seq[opt]
57460b57cec5SDimitry Andric ///         direct-declarator '(' parameter-type-list ')'
57470b57cec5SDimitry Andric ///         direct-declarator '(' identifier-list[opt] ')'
57480b57cec5SDimitry Andric /// [GNU]   direct-declarator '(' parameter-forward-declarations
57490b57cec5SDimitry Andric ///                    parameter-type-list[opt] ')'
57500b57cec5SDimitry Andric /// [C++]   direct-declarator '(' parameter-declaration-clause ')'
57510b57cec5SDimitry Andric ///                    cv-qualifier-seq[opt] exception-specification[opt]
57520b57cec5SDimitry Andric /// [C++11] direct-declarator '(' parameter-declaration-clause ')'
57530b57cec5SDimitry Andric ///                    attribute-specifier-seq[opt] cv-qualifier-seq[opt]
57540b57cec5SDimitry Andric ///                    ref-qualifier[opt] exception-specification[opt]
57550b57cec5SDimitry Andric /// [C++]   declarator-id
57560b57cec5SDimitry Andric /// [C++11] declarator-id attribute-specifier-seq[opt]
57570b57cec5SDimitry Andric ///
57580b57cec5SDimitry Andric ///       declarator-id: [C++ 8]
57590b57cec5SDimitry Andric ///         '...'[opt] id-expression
57600b57cec5SDimitry Andric ///         '::'[opt] nested-name-specifier[opt] type-name
57610b57cec5SDimitry Andric ///
57620b57cec5SDimitry Andric ///       id-expression: [C++ 5.1]
57630b57cec5SDimitry Andric ///         unqualified-id
57640b57cec5SDimitry Andric ///         qualified-id
57650b57cec5SDimitry Andric ///
57660b57cec5SDimitry Andric ///       unqualified-id: [C++ 5.1]
57670b57cec5SDimitry Andric ///         identifier
57680b57cec5SDimitry Andric ///         operator-function-id
57690b57cec5SDimitry Andric ///         conversion-function-id
57700b57cec5SDimitry Andric ///          '~' class-name
57710b57cec5SDimitry Andric ///         template-id
57720b57cec5SDimitry Andric ///
57730b57cec5SDimitry Andric /// C++17 adds the following, which we also handle here:
57740b57cec5SDimitry Andric ///
57750b57cec5SDimitry Andric ///       simple-declaration:
57760b57cec5SDimitry Andric ///         <decl-spec> '[' identifier-list ']' brace-or-equal-initializer ';'
57770b57cec5SDimitry Andric ///
57780b57cec5SDimitry Andric /// Note, any additional constructs added here may need corresponding changes
57790b57cec5SDimitry Andric /// in isConstructorDeclarator.
57800b57cec5SDimitry Andric void Parser::ParseDirectDeclarator(Declarator &D) {
57810b57cec5SDimitry Andric   DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
57820b57cec5SDimitry Andric 
57830b57cec5SDimitry Andric   if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
57840b57cec5SDimitry Andric     // This might be a C++17 structured binding.
57850b57cec5SDimitry Andric     if (Tok.is(tok::l_square) && !D.mayOmitIdentifier() &&
57860b57cec5SDimitry Andric         D.getCXXScopeSpec().isEmpty())
57870b57cec5SDimitry Andric       return ParseDecompositionDeclarator(D);
57880b57cec5SDimitry Andric 
57890b57cec5SDimitry Andric     // Don't parse FOO:BAR as if it were a typo for FOO::BAR inside a class, in
57900b57cec5SDimitry Andric     // this context it is a bitfield. Also in range-based for statement colon
57910b57cec5SDimitry Andric     // may delimit for-range-declaration.
57920b57cec5SDimitry Andric     ColonProtectionRAIIObject X(
57930b57cec5SDimitry Andric         *this, D.getContext() == DeclaratorContext::MemberContext ||
57940b57cec5SDimitry Andric                    (D.getContext() == DeclaratorContext::ForContext &&
57950b57cec5SDimitry Andric                     getLangOpts().CPlusPlus11));
57960b57cec5SDimitry Andric 
57970b57cec5SDimitry Andric     // ParseDeclaratorInternal might already have parsed the scope.
57980b57cec5SDimitry Andric     if (D.getCXXScopeSpec().isEmpty()) {
57990b57cec5SDimitry Andric       bool EnteringContext =
58000b57cec5SDimitry Andric           D.getContext() == DeclaratorContext::FileContext ||
58010b57cec5SDimitry Andric           D.getContext() == DeclaratorContext::MemberContext;
5802*5ffd83dbSDimitry Andric       ParseOptionalCXXScopeSpecifier(
5803*5ffd83dbSDimitry Andric           D.getCXXScopeSpec(), /*ObjectType=*/nullptr,
5804*5ffd83dbSDimitry Andric           /*ObjectHadErrors=*/false, EnteringContext);
58050b57cec5SDimitry Andric     }
58060b57cec5SDimitry Andric 
58070b57cec5SDimitry Andric     if (D.getCXXScopeSpec().isValid()) {
58080b57cec5SDimitry Andric       if (Actions.ShouldEnterDeclaratorScope(getCurScope(),
58090b57cec5SDimitry Andric                                              D.getCXXScopeSpec()))
58100b57cec5SDimitry Andric         // Change the declaration context for name lookup, until this function
58110b57cec5SDimitry Andric         // is exited (and the declarator has been parsed).
58120b57cec5SDimitry Andric         DeclScopeObj.EnterDeclaratorScope();
58130b57cec5SDimitry Andric       else if (getObjCDeclContext()) {
58140b57cec5SDimitry Andric         // Ensure that we don't interpret the next token as an identifier when
58150b57cec5SDimitry Andric         // dealing with declarations in an Objective-C container.
58160b57cec5SDimitry Andric         D.SetIdentifier(nullptr, Tok.getLocation());
58170b57cec5SDimitry Andric         D.setInvalidType(true);
58180b57cec5SDimitry Andric         ConsumeToken();
58190b57cec5SDimitry Andric         goto PastIdentifier;
58200b57cec5SDimitry Andric       }
58210b57cec5SDimitry Andric     }
58220b57cec5SDimitry Andric 
58230b57cec5SDimitry Andric     // C++0x [dcl.fct]p14:
58240b57cec5SDimitry Andric     //   There is a syntactic ambiguity when an ellipsis occurs at the end of a
58250b57cec5SDimitry Andric     //   parameter-declaration-clause without a preceding comma. In this case,
58260b57cec5SDimitry Andric     //   the ellipsis is parsed as part of the abstract-declarator if the type
58270b57cec5SDimitry Andric     //   of the parameter either names a template parameter pack that has not
58280b57cec5SDimitry Andric     //   been expanded or contains auto; otherwise, it is parsed as part of the
58290b57cec5SDimitry Andric     //   parameter-declaration-clause.
58300b57cec5SDimitry Andric     if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
58310b57cec5SDimitry Andric         !((D.getContext() == DeclaratorContext::PrototypeContext ||
58320b57cec5SDimitry Andric            D.getContext() == DeclaratorContext::LambdaExprParameterContext ||
58330b57cec5SDimitry Andric            D.getContext() == DeclaratorContext::BlockLiteralContext) &&
58340b57cec5SDimitry Andric           NextToken().is(tok::r_paren) &&
58350b57cec5SDimitry Andric           !D.hasGroupingParens() &&
58360b57cec5SDimitry Andric           !Actions.containsUnexpandedParameterPacks(D) &&
58370b57cec5SDimitry Andric           D.getDeclSpec().getTypeSpecType() != TST_auto)) {
58380b57cec5SDimitry Andric       SourceLocation EllipsisLoc = ConsumeToken();
58390b57cec5SDimitry Andric       if (isPtrOperatorToken(Tok.getKind(), getLangOpts(), D.getContext())) {
58400b57cec5SDimitry Andric         // The ellipsis was put in the wrong place. Recover, and explain to
58410b57cec5SDimitry Andric         // the user what they should have done.
58420b57cec5SDimitry Andric         ParseDeclarator(D);
58430b57cec5SDimitry Andric         if (EllipsisLoc.isValid())
58440b57cec5SDimitry Andric           DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
58450b57cec5SDimitry Andric         return;
58460b57cec5SDimitry Andric       } else
58470b57cec5SDimitry Andric         D.setEllipsisLoc(EllipsisLoc);
58480b57cec5SDimitry Andric 
58490b57cec5SDimitry Andric       // The ellipsis can't be followed by a parenthesized declarator. We
58500b57cec5SDimitry Andric       // check for that in ParseParenDeclarator, after we have disambiguated
58510b57cec5SDimitry Andric       // the l_paren token.
58520b57cec5SDimitry Andric     }
58530b57cec5SDimitry Andric 
58540b57cec5SDimitry Andric     if (Tok.isOneOf(tok::identifier, tok::kw_operator, tok::annot_template_id,
58550b57cec5SDimitry Andric                     tok::tilde)) {
58560b57cec5SDimitry Andric       // We found something that indicates the start of an unqualified-id.
58570b57cec5SDimitry Andric       // Parse that unqualified-id.
58580b57cec5SDimitry Andric       bool AllowConstructorName;
58590b57cec5SDimitry Andric       bool AllowDeductionGuide;
58600b57cec5SDimitry Andric       if (D.getDeclSpec().hasTypeSpecifier()) {
58610b57cec5SDimitry Andric         AllowConstructorName = false;
58620b57cec5SDimitry Andric         AllowDeductionGuide = false;
58630b57cec5SDimitry Andric       } else if (D.getCXXScopeSpec().isSet()) {
58640b57cec5SDimitry Andric         AllowConstructorName =
58650b57cec5SDimitry Andric           (D.getContext() == DeclaratorContext::FileContext ||
58660b57cec5SDimitry Andric            D.getContext() == DeclaratorContext::MemberContext);
58670b57cec5SDimitry Andric         AllowDeductionGuide = false;
58680b57cec5SDimitry Andric       } else {
58690b57cec5SDimitry Andric         AllowConstructorName =
58700b57cec5SDimitry Andric             (D.getContext() == DeclaratorContext::MemberContext);
58710b57cec5SDimitry Andric         AllowDeductionGuide =
58720b57cec5SDimitry Andric           (D.getContext() == DeclaratorContext::FileContext ||
58730b57cec5SDimitry Andric            D.getContext() == DeclaratorContext::MemberContext);
58740b57cec5SDimitry Andric       }
58750b57cec5SDimitry Andric 
58760b57cec5SDimitry Andric       bool HadScope = D.getCXXScopeSpec().isValid();
58770b57cec5SDimitry Andric       if (ParseUnqualifiedId(D.getCXXScopeSpec(),
5878*5ffd83dbSDimitry Andric                              /*ObjectType=*/nullptr,
5879*5ffd83dbSDimitry Andric                              /*ObjectHadErrors=*/false,
58800b57cec5SDimitry Andric                              /*EnteringContext=*/true,
58810b57cec5SDimitry Andric                              /*AllowDestructorName=*/true, AllowConstructorName,
5882*5ffd83dbSDimitry Andric                              AllowDeductionGuide, nullptr, D.getName()) ||
58830b57cec5SDimitry Andric           // Once we're past the identifier, if the scope was bad, mark the
58840b57cec5SDimitry Andric           // whole declarator bad.
58850b57cec5SDimitry Andric           D.getCXXScopeSpec().isInvalid()) {
58860b57cec5SDimitry Andric         D.SetIdentifier(nullptr, Tok.getLocation());
58870b57cec5SDimitry Andric         D.setInvalidType(true);
58880b57cec5SDimitry Andric       } else {
58890b57cec5SDimitry Andric         // ParseUnqualifiedId might have parsed a scope specifier during error
58900b57cec5SDimitry Andric         // recovery. If it did so, enter that scope.
58910b57cec5SDimitry Andric         if (!HadScope && D.getCXXScopeSpec().isValid() &&
58920b57cec5SDimitry Andric             Actions.ShouldEnterDeclaratorScope(getCurScope(),
58930b57cec5SDimitry Andric                                                D.getCXXScopeSpec()))
58940b57cec5SDimitry Andric           DeclScopeObj.EnterDeclaratorScope();
58950b57cec5SDimitry Andric 
58960b57cec5SDimitry Andric         // Parsed the unqualified-id; update range information and move along.
58970b57cec5SDimitry Andric         if (D.getSourceRange().getBegin().isInvalid())
58980b57cec5SDimitry Andric           D.SetRangeBegin(D.getName().getSourceRange().getBegin());
58990b57cec5SDimitry Andric         D.SetRangeEnd(D.getName().getSourceRange().getEnd());
59000b57cec5SDimitry Andric       }
59010b57cec5SDimitry Andric       goto PastIdentifier;
59020b57cec5SDimitry Andric     }
59030b57cec5SDimitry Andric 
59040b57cec5SDimitry Andric     if (D.getCXXScopeSpec().isNotEmpty()) {
59050b57cec5SDimitry Andric       // We have a scope specifier but no following unqualified-id.
59060b57cec5SDimitry Andric       Diag(PP.getLocForEndOfToken(D.getCXXScopeSpec().getEndLoc()),
59070b57cec5SDimitry Andric            diag::err_expected_unqualified_id)
59080b57cec5SDimitry Andric           << /*C++*/1;
59090b57cec5SDimitry Andric       D.SetIdentifier(nullptr, Tok.getLocation());
59100b57cec5SDimitry Andric       goto PastIdentifier;
59110b57cec5SDimitry Andric     }
59120b57cec5SDimitry Andric   } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
59130b57cec5SDimitry Andric     assert(!getLangOpts().CPlusPlus &&
59140b57cec5SDimitry Andric            "There's a C++-specific check for tok::identifier above");
59150b57cec5SDimitry Andric     assert(Tok.getIdentifierInfo() && "Not an identifier?");
59160b57cec5SDimitry Andric     D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
59170b57cec5SDimitry Andric     D.SetRangeEnd(Tok.getLocation());
59180b57cec5SDimitry Andric     ConsumeToken();
59190b57cec5SDimitry Andric     goto PastIdentifier;
59200b57cec5SDimitry Andric   } else if (Tok.is(tok::identifier) && !D.mayHaveIdentifier()) {
59210b57cec5SDimitry Andric     // We're not allowed an identifier here, but we got one. Try to figure out
59220b57cec5SDimitry Andric     // if the user was trying to attach a name to the type, or whether the name
59230b57cec5SDimitry Andric     // is some unrelated trailing syntax.
59240b57cec5SDimitry Andric     bool DiagnoseIdentifier = false;
59250b57cec5SDimitry Andric     if (D.hasGroupingParens())
59260b57cec5SDimitry Andric       // An identifier within parens is unlikely to be intended to be anything
59270b57cec5SDimitry Andric       // other than a name being "declared".
59280b57cec5SDimitry Andric       DiagnoseIdentifier = true;
59290b57cec5SDimitry Andric     else if (D.getContext() == DeclaratorContext::TemplateArgContext)
59300b57cec5SDimitry Andric       // T<int N> is an accidental identifier; T<int N indicates a missing '>'.
59310b57cec5SDimitry Andric       DiagnoseIdentifier =
59320b57cec5SDimitry Andric           NextToken().isOneOf(tok::comma, tok::greater, tok::greatergreater);
59330b57cec5SDimitry Andric     else if (D.getContext() == DeclaratorContext::AliasDeclContext ||
59340b57cec5SDimitry Andric              D.getContext() == DeclaratorContext::AliasTemplateContext)
59350b57cec5SDimitry Andric       // The most likely error is that the ';' was forgotten.
59360b57cec5SDimitry Andric       DiagnoseIdentifier = NextToken().isOneOf(tok::comma, tok::semi);
59370b57cec5SDimitry Andric     else if ((D.getContext() == DeclaratorContext::TrailingReturnContext ||
59380b57cec5SDimitry Andric               D.getContext() == DeclaratorContext::TrailingReturnVarContext) &&
59390b57cec5SDimitry Andric              !isCXX11VirtSpecifier(Tok))
59400b57cec5SDimitry Andric       DiagnoseIdentifier = NextToken().isOneOf(
59410b57cec5SDimitry Andric           tok::comma, tok::semi, tok::equal, tok::l_brace, tok::kw_try);
59420b57cec5SDimitry Andric     if (DiagnoseIdentifier) {
59430b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
59440b57cec5SDimitry Andric         << FixItHint::CreateRemoval(Tok.getLocation());
59450b57cec5SDimitry Andric       D.SetIdentifier(nullptr, Tok.getLocation());
59460b57cec5SDimitry Andric       ConsumeToken();
59470b57cec5SDimitry Andric       goto PastIdentifier;
59480b57cec5SDimitry Andric     }
59490b57cec5SDimitry Andric   }
59500b57cec5SDimitry Andric 
59510b57cec5SDimitry Andric   if (Tok.is(tok::l_paren)) {
59520b57cec5SDimitry Andric     // If this might be an abstract-declarator followed by a direct-initializer,
59530b57cec5SDimitry Andric     // check whether this is a valid declarator chunk. If it can't be, assume
59540b57cec5SDimitry Andric     // that it's an initializer instead.
59550b57cec5SDimitry Andric     if (D.mayOmitIdentifier() && D.mayBeFollowedByCXXDirectInit()) {
59560b57cec5SDimitry Andric       RevertingTentativeParsingAction PA(*this);
59570b57cec5SDimitry Andric       if (TryParseDeclarator(true, D.mayHaveIdentifier(), true) ==
59580b57cec5SDimitry Andric               TPResult::False) {
59590b57cec5SDimitry Andric         D.SetIdentifier(nullptr, Tok.getLocation());
59600b57cec5SDimitry Andric         goto PastIdentifier;
59610b57cec5SDimitry Andric       }
59620b57cec5SDimitry Andric     }
59630b57cec5SDimitry Andric 
59640b57cec5SDimitry Andric     // direct-declarator: '(' declarator ')'
59650b57cec5SDimitry Andric     // direct-declarator: '(' attributes declarator ')'
59660b57cec5SDimitry Andric     // Example: 'char (*X)'   or 'int (*XX)(void)'
59670b57cec5SDimitry Andric     ParseParenDeclarator(D);
59680b57cec5SDimitry Andric 
59690b57cec5SDimitry Andric     // If the declarator was parenthesized, we entered the declarator
59700b57cec5SDimitry Andric     // scope when parsing the parenthesized declarator, then exited
59710b57cec5SDimitry Andric     // the scope already. Re-enter the scope, if we need to.
59720b57cec5SDimitry Andric     if (D.getCXXScopeSpec().isSet()) {
59730b57cec5SDimitry Andric       // If there was an error parsing parenthesized declarator, declarator
59740b57cec5SDimitry Andric       // scope may have been entered before. Don't do it again.
59750b57cec5SDimitry Andric       if (!D.isInvalidType() &&
59760b57cec5SDimitry Andric           Actions.ShouldEnterDeclaratorScope(getCurScope(),
59770b57cec5SDimitry Andric                                              D.getCXXScopeSpec()))
59780b57cec5SDimitry Andric         // Change the declaration context for name lookup, until this function
59790b57cec5SDimitry Andric         // is exited (and the declarator has been parsed).
59800b57cec5SDimitry Andric         DeclScopeObj.EnterDeclaratorScope();
59810b57cec5SDimitry Andric     }
59820b57cec5SDimitry Andric   } else if (D.mayOmitIdentifier()) {
59830b57cec5SDimitry Andric     // This could be something simple like "int" (in which case the declarator
59840b57cec5SDimitry Andric     // portion is empty), if an abstract-declarator is allowed.
59850b57cec5SDimitry Andric     D.SetIdentifier(nullptr, Tok.getLocation());
59860b57cec5SDimitry Andric 
59870b57cec5SDimitry Andric     // The grammar for abstract-pack-declarator does not allow grouping parens.
59880b57cec5SDimitry Andric     // FIXME: Revisit this once core issue 1488 is resolved.
59890b57cec5SDimitry Andric     if (D.hasEllipsis() && D.hasGroupingParens())
59900b57cec5SDimitry Andric       Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
59910b57cec5SDimitry Andric            diag::ext_abstract_pack_declarator_parens);
59920b57cec5SDimitry Andric   } else {
59930b57cec5SDimitry Andric     if (Tok.getKind() == tok::annot_pragma_parser_crash)
59940b57cec5SDimitry Andric       LLVM_BUILTIN_TRAP;
59950b57cec5SDimitry Andric     if (Tok.is(tok::l_square))
59960b57cec5SDimitry Andric       return ParseMisplacedBracketDeclarator(D);
59970b57cec5SDimitry Andric     if (D.getContext() == DeclaratorContext::MemberContext) {
59980b57cec5SDimitry Andric       // Objective-C++: Detect C++ keywords and try to prevent further errors by
59990b57cec5SDimitry Andric       // treating these keyword as valid member names.
60000b57cec5SDimitry Andric       if (getLangOpts().ObjC && getLangOpts().CPlusPlus &&
60010b57cec5SDimitry Andric           Tok.getIdentifierInfo() &&
60020b57cec5SDimitry Andric           Tok.getIdentifierInfo()->isCPlusPlusKeyword(getLangOpts())) {
60030b57cec5SDimitry Andric         Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
60040b57cec5SDimitry Andric              diag::err_expected_member_name_or_semi_objcxx_keyword)
60050b57cec5SDimitry Andric             << Tok.getIdentifierInfo()
60060b57cec5SDimitry Andric             << (D.getDeclSpec().isEmpty() ? SourceRange()
60070b57cec5SDimitry Andric                                           : D.getDeclSpec().getSourceRange());
60080b57cec5SDimitry Andric         D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
60090b57cec5SDimitry Andric         D.SetRangeEnd(Tok.getLocation());
60100b57cec5SDimitry Andric         ConsumeToken();
60110b57cec5SDimitry Andric         goto PastIdentifier;
60120b57cec5SDimitry Andric       }
60130b57cec5SDimitry Andric       Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
60140b57cec5SDimitry Andric            diag::err_expected_member_name_or_semi)
60150b57cec5SDimitry Andric           << (D.getDeclSpec().isEmpty() ? SourceRange()
60160b57cec5SDimitry Andric                                         : D.getDeclSpec().getSourceRange());
60170b57cec5SDimitry Andric     } else if (getLangOpts().CPlusPlus) {
60180b57cec5SDimitry Andric       if (Tok.isOneOf(tok::period, tok::arrow))
60190b57cec5SDimitry Andric         Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
60200b57cec5SDimitry Andric       else {
60210b57cec5SDimitry Andric         SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
60220b57cec5SDimitry Andric         if (Tok.isAtStartOfLine() && Loc.isValid())
60230b57cec5SDimitry Andric           Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
60240b57cec5SDimitry Andric               << getLangOpts().CPlusPlus;
60250b57cec5SDimitry Andric         else
60260b57cec5SDimitry Andric           Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
60270b57cec5SDimitry Andric                diag::err_expected_unqualified_id)
60280b57cec5SDimitry Andric               << getLangOpts().CPlusPlus;
60290b57cec5SDimitry Andric       }
60300b57cec5SDimitry Andric     } else {
60310b57cec5SDimitry Andric       Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
60320b57cec5SDimitry Andric            diag::err_expected_either)
60330b57cec5SDimitry Andric           << tok::identifier << tok::l_paren;
60340b57cec5SDimitry Andric     }
60350b57cec5SDimitry Andric     D.SetIdentifier(nullptr, Tok.getLocation());
60360b57cec5SDimitry Andric     D.setInvalidType(true);
60370b57cec5SDimitry Andric   }
60380b57cec5SDimitry Andric 
60390b57cec5SDimitry Andric  PastIdentifier:
60400b57cec5SDimitry Andric   assert(D.isPastIdentifier() &&
60410b57cec5SDimitry Andric          "Haven't past the location of the identifier yet?");
60420b57cec5SDimitry Andric 
60430b57cec5SDimitry Andric   // Don't parse attributes unless we have parsed an unparenthesized name.
60440b57cec5SDimitry Andric   if (D.hasName() && !D.getNumTypeObjects())
60450b57cec5SDimitry Andric     MaybeParseCXX11Attributes(D);
60460b57cec5SDimitry Andric 
60470b57cec5SDimitry Andric   while (1) {
60480b57cec5SDimitry Andric     if (Tok.is(tok::l_paren)) {
604955e4f9d5SDimitry Andric       bool IsFunctionDeclaration = D.isFunctionDeclaratorAFunctionDeclaration();
60500b57cec5SDimitry Andric       // Enter function-declaration scope, limiting any declarators to the
60510b57cec5SDimitry Andric       // function prototype scope, including parameter declarators.
60520b57cec5SDimitry Andric       ParseScope PrototypeScope(this,
60530b57cec5SDimitry Andric                                 Scope::FunctionPrototypeScope|Scope::DeclScope|
605455e4f9d5SDimitry Andric                                 (IsFunctionDeclaration
60550b57cec5SDimitry Andric                                    ? Scope::FunctionDeclarationScope : 0));
60560b57cec5SDimitry Andric 
60570b57cec5SDimitry Andric       // The paren may be part of a C++ direct initializer, eg. "int x(1);".
60580b57cec5SDimitry Andric       // In such a case, check if we actually have a function declarator; if it
60590b57cec5SDimitry Andric       // is not, the declarator has been fully parsed.
60600b57cec5SDimitry Andric       bool IsAmbiguous = false;
60610b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
60620b57cec5SDimitry Andric         // The name of the declarator, if any, is tentatively declared within
60630b57cec5SDimitry Andric         // a possible direct initializer.
60640b57cec5SDimitry Andric         TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
60650b57cec5SDimitry Andric         bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
60660b57cec5SDimitry Andric         TentativelyDeclaredIdentifiers.pop_back();
60670b57cec5SDimitry Andric         if (!IsFunctionDecl)
60680b57cec5SDimitry Andric           break;
60690b57cec5SDimitry Andric       }
60700b57cec5SDimitry Andric       ParsedAttributes attrs(AttrFactory);
60710b57cec5SDimitry Andric       BalancedDelimiterTracker T(*this, tok::l_paren);
60720b57cec5SDimitry Andric       T.consumeOpen();
607355e4f9d5SDimitry Andric       if (IsFunctionDeclaration)
607455e4f9d5SDimitry Andric         Actions.ActOnStartFunctionDeclarationDeclarator(D,
607555e4f9d5SDimitry Andric                                                         TemplateParameterDepth);
60760b57cec5SDimitry Andric       ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
607755e4f9d5SDimitry Andric       if (IsFunctionDeclaration)
607855e4f9d5SDimitry Andric         Actions.ActOnFinishFunctionDeclarationDeclarator(D);
60790b57cec5SDimitry Andric       PrototypeScope.Exit();
60800b57cec5SDimitry Andric     } else if (Tok.is(tok::l_square)) {
60810b57cec5SDimitry Andric       ParseBracketDeclarator(D);
6082480093f4SDimitry Andric     } else if (Tok.is(tok::kw_requires) && D.hasGroupingParens()) {
6083480093f4SDimitry Andric       // This declarator is declaring a function, but the requires clause is
6084480093f4SDimitry Andric       // in the wrong place:
6085480093f4SDimitry Andric       //   void (f() requires true);
6086480093f4SDimitry Andric       // instead of
6087480093f4SDimitry Andric       //   void f() requires true;
6088480093f4SDimitry Andric       // or
6089480093f4SDimitry Andric       //   void (f()) requires true;
6090480093f4SDimitry Andric       Diag(Tok, diag::err_requires_clause_inside_parens);
6091480093f4SDimitry Andric       ConsumeToken();
6092480093f4SDimitry Andric       ExprResult TrailingRequiresClause = Actions.CorrectDelayedTyposInExpr(
6093480093f4SDimitry Andric          ParseConstraintLogicalOrExpression(/*IsTrailingRequiresClause=*/true));
6094480093f4SDimitry Andric       if (TrailingRequiresClause.isUsable() && D.isFunctionDeclarator() &&
6095480093f4SDimitry Andric           !D.hasTrailingRequiresClause())
6096480093f4SDimitry Andric         // We're already ill-formed if we got here but we'll accept it anyway.
6097480093f4SDimitry Andric         D.setTrailingRequiresClause(TrailingRequiresClause.get());
60980b57cec5SDimitry Andric     } else {
60990b57cec5SDimitry Andric       break;
61000b57cec5SDimitry Andric     }
61010b57cec5SDimitry Andric   }
61020b57cec5SDimitry Andric }
61030b57cec5SDimitry Andric 
61040b57cec5SDimitry Andric void Parser::ParseDecompositionDeclarator(Declarator &D) {
61050b57cec5SDimitry Andric   assert(Tok.is(tok::l_square));
61060b57cec5SDimitry Andric 
61070b57cec5SDimitry Andric   // If this doesn't look like a structured binding, maybe it's a misplaced
61080b57cec5SDimitry Andric   // array declarator.
61090b57cec5SDimitry Andric   // FIXME: Consume the l_square first so we don't need extra lookahead for
61100b57cec5SDimitry Andric   // this.
61110b57cec5SDimitry Andric   if (!(NextToken().is(tok::identifier) &&
61120b57cec5SDimitry Andric         GetLookAheadToken(2).isOneOf(tok::comma, tok::r_square)) &&
61130b57cec5SDimitry Andric       !(NextToken().is(tok::r_square) &&
61140b57cec5SDimitry Andric         GetLookAheadToken(2).isOneOf(tok::equal, tok::l_brace)))
61150b57cec5SDimitry Andric     return ParseMisplacedBracketDeclarator(D);
61160b57cec5SDimitry Andric 
61170b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_square);
61180b57cec5SDimitry Andric   T.consumeOpen();
61190b57cec5SDimitry Andric 
61200b57cec5SDimitry Andric   SmallVector<DecompositionDeclarator::Binding, 32> Bindings;
61210b57cec5SDimitry Andric   while (Tok.isNot(tok::r_square)) {
61220b57cec5SDimitry Andric     if (!Bindings.empty()) {
61230b57cec5SDimitry Andric       if (Tok.is(tok::comma))
61240b57cec5SDimitry Andric         ConsumeToken();
61250b57cec5SDimitry Andric       else {
61260b57cec5SDimitry Andric         if (Tok.is(tok::identifier)) {
61270b57cec5SDimitry Andric           SourceLocation EndLoc = getEndOfPreviousToken();
61280b57cec5SDimitry Andric           Diag(EndLoc, diag::err_expected)
61290b57cec5SDimitry Andric               << tok::comma << FixItHint::CreateInsertion(EndLoc, ",");
61300b57cec5SDimitry Andric         } else {
61310b57cec5SDimitry Andric           Diag(Tok, diag::err_expected_comma_or_rsquare);
61320b57cec5SDimitry Andric         }
61330b57cec5SDimitry Andric 
61340b57cec5SDimitry Andric         SkipUntil(tok::r_square, tok::comma, tok::identifier,
61350b57cec5SDimitry Andric                   StopAtSemi | StopBeforeMatch);
61360b57cec5SDimitry Andric         if (Tok.is(tok::comma))
61370b57cec5SDimitry Andric           ConsumeToken();
61380b57cec5SDimitry Andric         else if (Tok.isNot(tok::identifier))
61390b57cec5SDimitry Andric           break;
61400b57cec5SDimitry Andric       }
61410b57cec5SDimitry Andric     }
61420b57cec5SDimitry Andric 
61430b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
61440b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::identifier;
61450b57cec5SDimitry Andric       break;
61460b57cec5SDimitry Andric     }
61470b57cec5SDimitry Andric 
61480b57cec5SDimitry Andric     Bindings.push_back({Tok.getIdentifierInfo(), Tok.getLocation()});
61490b57cec5SDimitry Andric     ConsumeToken();
61500b57cec5SDimitry Andric   }
61510b57cec5SDimitry Andric 
61520b57cec5SDimitry Andric   if (Tok.isNot(tok::r_square))
61530b57cec5SDimitry Andric     // We've already diagnosed a problem here.
61540b57cec5SDimitry Andric     T.skipToEnd();
61550b57cec5SDimitry Andric   else {
61560b57cec5SDimitry Andric     // C++17 does not allow the identifier-list in a structured binding
61570b57cec5SDimitry Andric     // to be empty.
61580b57cec5SDimitry Andric     if (Bindings.empty())
61590b57cec5SDimitry Andric       Diag(Tok.getLocation(), diag::ext_decomp_decl_empty);
61600b57cec5SDimitry Andric 
61610b57cec5SDimitry Andric     T.consumeClose();
61620b57cec5SDimitry Andric   }
61630b57cec5SDimitry Andric 
61640b57cec5SDimitry Andric   return D.setDecompositionBindings(T.getOpenLocation(), Bindings,
61650b57cec5SDimitry Andric                                     T.getCloseLocation());
61660b57cec5SDimitry Andric }
61670b57cec5SDimitry Andric 
61680b57cec5SDimitry Andric /// ParseParenDeclarator - We parsed the declarator D up to a paren.  This is
61690b57cec5SDimitry Andric /// only called before the identifier, so these are most likely just grouping
61700b57cec5SDimitry Andric /// parens for precedence.  If we find that these are actually function
61710b57cec5SDimitry Andric /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
61720b57cec5SDimitry Andric ///
61730b57cec5SDimitry Andric ///       direct-declarator:
61740b57cec5SDimitry Andric ///         '(' declarator ')'
61750b57cec5SDimitry Andric /// [GNU]   '(' attributes declarator ')'
61760b57cec5SDimitry Andric ///         direct-declarator '(' parameter-type-list ')'
61770b57cec5SDimitry Andric ///         direct-declarator '(' identifier-list[opt] ')'
61780b57cec5SDimitry Andric /// [GNU]   direct-declarator '(' parameter-forward-declarations
61790b57cec5SDimitry Andric ///                    parameter-type-list[opt] ')'
61800b57cec5SDimitry Andric ///
61810b57cec5SDimitry Andric void Parser::ParseParenDeclarator(Declarator &D) {
61820b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
61830b57cec5SDimitry Andric   T.consumeOpen();
61840b57cec5SDimitry Andric 
61850b57cec5SDimitry Andric   assert(!D.isPastIdentifier() && "Should be called before passing identifier");
61860b57cec5SDimitry Andric 
61870b57cec5SDimitry Andric   // Eat any attributes before we look at whether this is a grouping or function
61880b57cec5SDimitry Andric   // declarator paren.  If this is a grouping paren, the attribute applies to
61890b57cec5SDimitry Andric   // the type being built up, for example:
61900b57cec5SDimitry Andric   //     int (__attribute__(()) *x)(long y)
61910b57cec5SDimitry Andric   // If this ends up not being a grouping paren, the attribute applies to the
61920b57cec5SDimitry Andric   // first argument, for example:
61930b57cec5SDimitry Andric   //     int (__attribute__(()) int x)
61940b57cec5SDimitry Andric   // In either case, we need to eat any attributes to be able to determine what
61950b57cec5SDimitry Andric   // sort of paren this is.
61960b57cec5SDimitry Andric   //
61970b57cec5SDimitry Andric   ParsedAttributes attrs(AttrFactory);
61980b57cec5SDimitry Andric   bool RequiresArg = false;
61990b57cec5SDimitry Andric   if (Tok.is(tok::kw___attribute)) {
62000b57cec5SDimitry Andric     ParseGNUAttributes(attrs);
62010b57cec5SDimitry Andric 
62020b57cec5SDimitry Andric     // We require that the argument list (if this is a non-grouping paren) be
62030b57cec5SDimitry Andric     // present even if the attribute list was empty.
62040b57cec5SDimitry Andric     RequiresArg = true;
62050b57cec5SDimitry Andric   }
62060b57cec5SDimitry Andric 
62070b57cec5SDimitry Andric   // Eat any Microsoft extensions.
62080b57cec5SDimitry Andric   ParseMicrosoftTypeAttributes(attrs);
62090b57cec5SDimitry Andric 
62100b57cec5SDimitry Andric   // Eat any Borland extensions.
62110b57cec5SDimitry Andric   if  (Tok.is(tok::kw___pascal))
62120b57cec5SDimitry Andric     ParseBorlandTypeAttributes(attrs);
62130b57cec5SDimitry Andric 
62140b57cec5SDimitry Andric   // If we haven't past the identifier yet (or where the identifier would be
62150b57cec5SDimitry Andric   // stored, if this is an abstract declarator), then this is probably just
62160b57cec5SDimitry Andric   // grouping parens. However, if this could be an abstract-declarator, then
62170b57cec5SDimitry Andric   // this could also be the start of function arguments (consider 'void()').
62180b57cec5SDimitry Andric   bool isGrouping;
62190b57cec5SDimitry Andric 
62200b57cec5SDimitry Andric   if (!D.mayOmitIdentifier()) {
62210b57cec5SDimitry Andric     // If this can't be an abstract-declarator, this *must* be a grouping
62220b57cec5SDimitry Andric     // paren, because we haven't seen the identifier yet.
62230b57cec5SDimitry Andric     isGrouping = true;
62240b57cec5SDimitry Andric   } else if (Tok.is(tok::r_paren) ||           // 'int()' is a function.
62250b57cec5SDimitry Andric              (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
62260b57cec5SDimitry Andric               NextToken().is(tok::r_paren)) || // C++ int(...)
62270b57cec5SDimitry Andric              isDeclarationSpecifier() ||       // 'int(int)' is a function.
62280b57cec5SDimitry Andric              isCXX11AttributeSpecifier()) {    // 'int([[]]int)' is a function.
62290b57cec5SDimitry Andric     // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
62300b57cec5SDimitry Andric     // considered to be a type, not a K&R identifier-list.
62310b57cec5SDimitry Andric     isGrouping = false;
62320b57cec5SDimitry Andric   } else {
62330b57cec5SDimitry Andric     // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
62340b57cec5SDimitry Andric     isGrouping = true;
62350b57cec5SDimitry Andric   }
62360b57cec5SDimitry Andric 
62370b57cec5SDimitry Andric   // If this is a grouping paren, handle:
62380b57cec5SDimitry Andric   // direct-declarator: '(' declarator ')'
62390b57cec5SDimitry Andric   // direct-declarator: '(' attributes declarator ')'
62400b57cec5SDimitry Andric   if (isGrouping) {
62410b57cec5SDimitry Andric     SourceLocation EllipsisLoc = D.getEllipsisLoc();
62420b57cec5SDimitry Andric     D.setEllipsisLoc(SourceLocation());
62430b57cec5SDimitry Andric 
62440b57cec5SDimitry Andric     bool hadGroupingParens = D.hasGroupingParens();
62450b57cec5SDimitry Andric     D.setGroupingParens(true);
62460b57cec5SDimitry Andric     ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
62470b57cec5SDimitry Andric     // Match the ')'.
62480b57cec5SDimitry Andric     T.consumeClose();
62490b57cec5SDimitry Andric     D.AddTypeInfo(
62500b57cec5SDimitry Andric         DeclaratorChunk::getParen(T.getOpenLocation(), T.getCloseLocation()),
62510b57cec5SDimitry Andric         std::move(attrs), T.getCloseLocation());
62520b57cec5SDimitry Andric 
62530b57cec5SDimitry Andric     D.setGroupingParens(hadGroupingParens);
62540b57cec5SDimitry Andric 
62550b57cec5SDimitry Andric     // An ellipsis cannot be placed outside parentheses.
62560b57cec5SDimitry Andric     if (EllipsisLoc.isValid())
62570b57cec5SDimitry Andric       DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
62580b57cec5SDimitry Andric 
62590b57cec5SDimitry Andric     return;
62600b57cec5SDimitry Andric   }
62610b57cec5SDimitry Andric 
62620b57cec5SDimitry Andric   // Okay, if this wasn't a grouping paren, it must be the start of a function
62630b57cec5SDimitry Andric   // argument list.  Recognize that this declarator will never have an
62640b57cec5SDimitry Andric   // identifier (and remember where it would have been), then call into
62650b57cec5SDimitry Andric   // ParseFunctionDeclarator to handle of argument list.
62660b57cec5SDimitry Andric   D.SetIdentifier(nullptr, Tok.getLocation());
62670b57cec5SDimitry Andric 
62680b57cec5SDimitry Andric   // Enter function-declaration scope, limiting any declarators to the
62690b57cec5SDimitry Andric   // function prototype scope, including parameter declarators.
62700b57cec5SDimitry Andric   ParseScope PrototypeScope(this,
62710b57cec5SDimitry Andric                             Scope::FunctionPrototypeScope | Scope::DeclScope |
62720b57cec5SDimitry Andric                             (D.isFunctionDeclaratorAFunctionDeclaration()
62730b57cec5SDimitry Andric                                ? Scope::FunctionDeclarationScope : 0));
62740b57cec5SDimitry Andric   ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
62750b57cec5SDimitry Andric   PrototypeScope.Exit();
62760b57cec5SDimitry Andric }
62770b57cec5SDimitry Andric 
6278480093f4SDimitry Andric void Parser::InitCXXThisScopeForDeclaratorIfRelevant(
6279480093f4SDimitry Andric     const Declarator &D, const DeclSpec &DS,
6280480093f4SDimitry Andric     llvm::Optional<Sema::CXXThisScopeRAII> &ThisScope) {
6281480093f4SDimitry Andric   // C++11 [expr.prim.general]p3:
6282480093f4SDimitry Andric   //   If a declaration declares a member function or member function
6283480093f4SDimitry Andric   //   template of a class X, the expression this is a prvalue of type
6284480093f4SDimitry Andric   //   "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
6285480093f4SDimitry Andric   //   and the end of the function-definition, member-declarator, or
6286480093f4SDimitry Andric   //   declarator.
6287480093f4SDimitry Andric   // FIXME: currently, "static" case isn't handled correctly.
6288480093f4SDimitry Andric   bool IsCXX11MemberFunction = getLangOpts().CPlusPlus11 &&
6289480093f4SDimitry Andric         D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
6290480093f4SDimitry Andric         (D.getContext() == DeclaratorContext::MemberContext
6291480093f4SDimitry Andric          ? !D.getDeclSpec().isFriendSpecified()
6292480093f4SDimitry Andric          : D.getContext() == DeclaratorContext::FileContext &&
6293480093f4SDimitry Andric            D.getCXXScopeSpec().isValid() &&
6294480093f4SDimitry Andric            Actions.CurContext->isRecord());
6295480093f4SDimitry Andric   if (!IsCXX11MemberFunction)
6296480093f4SDimitry Andric     return;
6297480093f4SDimitry Andric 
6298480093f4SDimitry Andric   Qualifiers Q = Qualifiers::fromCVRUMask(DS.getTypeQualifiers());
6299480093f4SDimitry Andric   if (D.getDeclSpec().hasConstexprSpecifier() && !getLangOpts().CPlusPlus14)
6300480093f4SDimitry Andric     Q.addConst();
6301480093f4SDimitry Andric   // FIXME: Collect C++ address spaces.
6302480093f4SDimitry Andric   // If there are multiple different address spaces, the source is invalid.
6303480093f4SDimitry Andric   // Carry on using the first addr space for the qualifiers of 'this'.
6304480093f4SDimitry Andric   // The diagnostic will be given later while creating the function
6305480093f4SDimitry Andric   // prototype for the method.
6306480093f4SDimitry Andric   if (getLangOpts().OpenCLCPlusPlus) {
6307480093f4SDimitry Andric     for (ParsedAttr &attr : DS.getAttributes()) {
6308480093f4SDimitry Andric       LangAS ASIdx = attr.asOpenCLLangAS();
6309480093f4SDimitry Andric       if (ASIdx != LangAS::Default) {
6310480093f4SDimitry Andric         Q.addAddressSpace(ASIdx);
6311480093f4SDimitry Andric         break;
6312480093f4SDimitry Andric       }
6313480093f4SDimitry Andric     }
6314480093f4SDimitry Andric   }
6315480093f4SDimitry Andric   ThisScope.emplace(Actions, dyn_cast<CXXRecordDecl>(Actions.CurContext), Q,
6316480093f4SDimitry Andric                     IsCXX11MemberFunction);
6317480093f4SDimitry Andric }
6318480093f4SDimitry Andric 
63190b57cec5SDimitry Andric /// ParseFunctionDeclarator - We are after the identifier and have parsed the
63200b57cec5SDimitry Andric /// declarator D up to a paren, which indicates that we are parsing function
63210b57cec5SDimitry Andric /// arguments.
63220b57cec5SDimitry Andric ///
63230b57cec5SDimitry Andric /// If FirstArgAttrs is non-null, then the caller parsed those arguments
63240b57cec5SDimitry Andric /// immediately after the open paren - they should be considered to be the
63250b57cec5SDimitry Andric /// first argument of a parameter.
63260b57cec5SDimitry Andric ///
63270b57cec5SDimitry Andric /// If RequiresArg is true, then the first argument of the function is required
63280b57cec5SDimitry Andric /// to be present and required to not be an identifier list.
63290b57cec5SDimitry Andric ///
63300b57cec5SDimitry Andric /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
63310b57cec5SDimitry Andric /// (C++11) ref-qualifier[opt], exception-specification[opt],
6332480093f4SDimitry Andric /// (C++11) attribute-specifier-seq[opt], (C++11) trailing-return-type[opt] and
6333480093f4SDimitry Andric /// (C++2a) the trailing requires-clause.
63340b57cec5SDimitry Andric ///
63350b57cec5SDimitry Andric /// [C++11] exception-specification:
63360b57cec5SDimitry Andric ///           dynamic-exception-specification
63370b57cec5SDimitry Andric ///           noexcept-specification
63380b57cec5SDimitry Andric ///
63390b57cec5SDimitry Andric void Parser::ParseFunctionDeclarator(Declarator &D,
63400b57cec5SDimitry Andric                                      ParsedAttributes &FirstArgAttrs,
63410b57cec5SDimitry Andric                                      BalancedDelimiterTracker &Tracker,
63420b57cec5SDimitry Andric                                      bool IsAmbiguous,
63430b57cec5SDimitry Andric                                      bool RequiresArg) {
63440b57cec5SDimitry Andric   assert(getCurScope()->isFunctionPrototypeScope() &&
63450b57cec5SDimitry Andric          "Should call from a Function scope");
63460b57cec5SDimitry Andric   // lparen is already consumed!
63470b57cec5SDimitry Andric   assert(D.isPastIdentifier() && "Should not call before identifier!");
63480b57cec5SDimitry Andric 
63490b57cec5SDimitry Andric   // This should be true when the function has typed arguments.
63500b57cec5SDimitry Andric   // Otherwise, it is treated as a K&R-style function.
63510b57cec5SDimitry Andric   bool HasProto = false;
63520b57cec5SDimitry Andric   // Build up an array of information about the parsed arguments.
63530b57cec5SDimitry Andric   SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
63540b57cec5SDimitry Andric   // Remember where we see an ellipsis, if any.
63550b57cec5SDimitry Andric   SourceLocation EllipsisLoc;
63560b57cec5SDimitry Andric 
63570b57cec5SDimitry Andric   DeclSpec DS(AttrFactory);
63580b57cec5SDimitry Andric   bool RefQualifierIsLValueRef = true;
63590b57cec5SDimitry Andric   SourceLocation RefQualifierLoc;
63600b57cec5SDimitry Andric   ExceptionSpecificationType ESpecType = EST_None;
63610b57cec5SDimitry Andric   SourceRange ESpecRange;
63620b57cec5SDimitry Andric   SmallVector<ParsedType, 2> DynamicExceptions;
63630b57cec5SDimitry Andric   SmallVector<SourceRange, 2> DynamicExceptionRanges;
63640b57cec5SDimitry Andric   ExprResult NoexceptExpr;
63650b57cec5SDimitry Andric   CachedTokens *ExceptionSpecTokens = nullptr;
63660b57cec5SDimitry Andric   ParsedAttributesWithRange FnAttrs(AttrFactory);
63670b57cec5SDimitry Andric   TypeResult TrailingReturnType;
63680b57cec5SDimitry Andric 
63690b57cec5SDimitry Andric   /* LocalEndLoc is the end location for the local FunctionTypeLoc.
63700b57cec5SDimitry Andric      EndLoc is the end location for the function declarator.
63710b57cec5SDimitry Andric      They differ for trailing return types. */
63720b57cec5SDimitry Andric   SourceLocation StartLoc, LocalEndLoc, EndLoc;
63730b57cec5SDimitry Andric   SourceLocation LParenLoc, RParenLoc;
63740b57cec5SDimitry Andric   LParenLoc = Tracker.getOpenLocation();
63750b57cec5SDimitry Andric   StartLoc = LParenLoc;
63760b57cec5SDimitry Andric 
63770b57cec5SDimitry Andric   if (isFunctionDeclaratorIdentifierList()) {
63780b57cec5SDimitry Andric     if (RequiresArg)
63790b57cec5SDimitry Andric       Diag(Tok, diag::err_argument_required_after_attribute);
63800b57cec5SDimitry Andric 
63810b57cec5SDimitry Andric     ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
63820b57cec5SDimitry Andric 
63830b57cec5SDimitry Andric     Tracker.consumeClose();
63840b57cec5SDimitry Andric     RParenLoc = Tracker.getCloseLocation();
63850b57cec5SDimitry Andric     LocalEndLoc = RParenLoc;
63860b57cec5SDimitry Andric     EndLoc = RParenLoc;
63870b57cec5SDimitry Andric 
63880b57cec5SDimitry Andric     // If there are attributes following the identifier list, parse them and
63890b57cec5SDimitry Andric     // prohibit them.
63900b57cec5SDimitry Andric     MaybeParseCXX11Attributes(FnAttrs);
63910b57cec5SDimitry Andric     ProhibitAttributes(FnAttrs);
63920b57cec5SDimitry Andric   } else {
63930b57cec5SDimitry Andric     if (Tok.isNot(tok::r_paren))
639455e4f9d5SDimitry Andric       ParseParameterDeclarationClause(D.getContext(), FirstArgAttrs, ParamInfo,
63950b57cec5SDimitry Andric                                       EllipsisLoc);
63960b57cec5SDimitry Andric     else if (RequiresArg)
63970b57cec5SDimitry Andric       Diag(Tok, diag::err_argument_required_after_attribute);
63980b57cec5SDimitry Andric 
63990b57cec5SDimitry Andric     HasProto = ParamInfo.size() || getLangOpts().CPlusPlus
64000b57cec5SDimitry Andric                                 || getLangOpts().OpenCL;
64010b57cec5SDimitry Andric 
64020b57cec5SDimitry Andric     // If we have the closing ')', eat it.
64030b57cec5SDimitry Andric     Tracker.consumeClose();
64040b57cec5SDimitry Andric     RParenLoc = Tracker.getCloseLocation();
64050b57cec5SDimitry Andric     LocalEndLoc = RParenLoc;
64060b57cec5SDimitry Andric     EndLoc = RParenLoc;
64070b57cec5SDimitry Andric 
64080b57cec5SDimitry Andric     if (getLangOpts().CPlusPlus) {
64090b57cec5SDimitry Andric       // FIXME: Accept these components in any order, and produce fixits to
64100b57cec5SDimitry Andric       // correct the order if the user gets it wrong. Ideally we should deal
64110b57cec5SDimitry Andric       // with the pure-specifier in the same way.
64120b57cec5SDimitry Andric 
64130b57cec5SDimitry Andric       // Parse cv-qualifier-seq[opt].
64140b57cec5SDimitry Andric       ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed,
64150b57cec5SDimitry Andric                                 /*AtomicAllowed*/ false,
64160b57cec5SDimitry Andric                                 /*IdentifierRequired=*/false,
64170b57cec5SDimitry Andric                                 llvm::function_ref<void()>([&]() {
64180b57cec5SDimitry Andric                                   Actions.CodeCompleteFunctionQualifiers(DS, D);
64190b57cec5SDimitry Andric                                 }));
64200b57cec5SDimitry Andric       if (!DS.getSourceRange().getEnd().isInvalid()) {
64210b57cec5SDimitry Andric         EndLoc = DS.getSourceRange().getEnd();
64220b57cec5SDimitry Andric       }
64230b57cec5SDimitry Andric 
64240b57cec5SDimitry Andric       // Parse ref-qualifier[opt].
64250b57cec5SDimitry Andric       if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc))
64260b57cec5SDimitry Andric         EndLoc = RefQualifierLoc;
64270b57cec5SDimitry Andric 
6428480093f4SDimitry Andric       llvm::Optional<Sema::CXXThisScopeRAII> ThisScope;
6429480093f4SDimitry Andric       InitCXXThisScopeForDeclaratorIfRelevant(D, DS, ThisScope);
64300b57cec5SDimitry Andric 
64310b57cec5SDimitry Andric       // Parse exception-specification[opt].
64320b57cec5SDimitry Andric       bool Delayed = D.isFirstDeclarationOfMember() &&
64330b57cec5SDimitry Andric                      D.isFunctionDeclaratorAFunctionDeclaration();
64340b57cec5SDimitry Andric       if (Delayed && Actions.isLibstdcxxEagerExceptionSpecHack(D) &&
64350b57cec5SDimitry Andric           GetLookAheadToken(0).is(tok::kw_noexcept) &&
64360b57cec5SDimitry Andric           GetLookAheadToken(1).is(tok::l_paren) &&
64370b57cec5SDimitry Andric           GetLookAheadToken(2).is(tok::kw_noexcept) &&
64380b57cec5SDimitry Andric           GetLookAheadToken(3).is(tok::l_paren) &&
64390b57cec5SDimitry Andric           GetLookAheadToken(4).is(tok::identifier) &&
64400b57cec5SDimitry Andric           GetLookAheadToken(4).getIdentifierInfo()->isStr("swap")) {
64410b57cec5SDimitry Andric         // HACK: We've got an exception-specification
64420b57cec5SDimitry Andric         //   noexcept(noexcept(swap(...)))
64430b57cec5SDimitry Andric         // or
64440b57cec5SDimitry Andric         //   noexcept(noexcept(swap(...)) && noexcept(swap(...)))
64450b57cec5SDimitry Andric         // on a 'swap' member function. This is a libstdc++ bug; the lookup
64460b57cec5SDimitry Andric         // for 'swap' will only find the function we're currently declaring,
64470b57cec5SDimitry Andric         // whereas it expects to find a non-member swap through ADL. Turn off
64480b57cec5SDimitry Andric         // delayed parsing to give it a chance to find what it expects.
64490b57cec5SDimitry Andric         Delayed = false;
64500b57cec5SDimitry Andric       }
64510b57cec5SDimitry Andric       ESpecType = tryParseExceptionSpecification(Delayed,
64520b57cec5SDimitry Andric                                                  ESpecRange,
64530b57cec5SDimitry Andric                                                  DynamicExceptions,
64540b57cec5SDimitry Andric                                                  DynamicExceptionRanges,
64550b57cec5SDimitry Andric                                                  NoexceptExpr,
64560b57cec5SDimitry Andric                                                  ExceptionSpecTokens);
64570b57cec5SDimitry Andric       if (ESpecType != EST_None)
64580b57cec5SDimitry Andric         EndLoc = ESpecRange.getEnd();
64590b57cec5SDimitry Andric 
64600b57cec5SDimitry Andric       // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
64610b57cec5SDimitry Andric       // after the exception-specification.
64620b57cec5SDimitry Andric       MaybeParseCXX11Attributes(FnAttrs);
64630b57cec5SDimitry Andric 
64640b57cec5SDimitry Andric       // Parse trailing-return-type[opt].
64650b57cec5SDimitry Andric       LocalEndLoc = EndLoc;
64660b57cec5SDimitry Andric       if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
64670b57cec5SDimitry Andric         Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
64680b57cec5SDimitry Andric         if (D.getDeclSpec().getTypeSpecType() == TST_auto)
64690b57cec5SDimitry Andric           StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
64700b57cec5SDimitry Andric         LocalEndLoc = Tok.getLocation();
64710b57cec5SDimitry Andric         SourceRange Range;
64720b57cec5SDimitry Andric         TrailingReturnType =
64730b57cec5SDimitry Andric             ParseTrailingReturnType(Range, D.mayBeFollowedByCXXDirectInit());
64740b57cec5SDimitry Andric         EndLoc = Range.getEnd();
64750b57cec5SDimitry Andric       }
64760b57cec5SDimitry Andric     } else if (standardAttributesAllowed()) {
64770b57cec5SDimitry Andric       MaybeParseCXX11Attributes(FnAttrs);
64780b57cec5SDimitry Andric     }
64790b57cec5SDimitry Andric   }
64800b57cec5SDimitry Andric 
64810b57cec5SDimitry Andric   // Collect non-parameter declarations from the prototype if this is a function
64820b57cec5SDimitry Andric   // declaration. They will be moved into the scope of the function. Only do
64830b57cec5SDimitry Andric   // this in C and not C++, where the decls will continue to live in the
64840b57cec5SDimitry Andric   // surrounding context.
64850b57cec5SDimitry Andric   SmallVector<NamedDecl *, 0> DeclsInPrototype;
64860b57cec5SDimitry Andric   if (getCurScope()->getFlags() & Scope::FunctionDeclarationScope &&
64870b57cec5SDimitry Andric       !getLangOpts().CPlusPlus) {
64880b57cec5SDimitry Andric     for (Decl *D : getCurScope()->decls()) {
64890b57cec5SDimitry Andric       NamedDecl *ND = dyn_cast<NamedDecl>(D);
64900b57cec5SDimitry Andric       if (!ND || isa<ParmVarDecl>(ND))
64910b57cec5SDimitry Andric         continue;
64920b57cec5SDimitry Andric       DeclsInPrototype.push_back(ND);
64930b57cec5SDimitry Andric     }
64940b57cec5SDimitry Andric   }
64950b57cec5SDimitry Andric 
64960b57cec5SDimitry Andric   // Remember that we parsed a function type, and remember the attributes.
64970b57cec5SDimitry Andric   D.AddTypeInfo(DeclaratorChunk::getFunction(
64980b57cec5SDimitry Andric                     HasProto, IsAmbiguous, LParenLoc, ParamInfo.data(),
64990b57cec5SDimitry Andric                     ParamInfo.size(), EllipsisLoc, RParenLoc,
65000b57cec5SDimitry Andric                     RefQualifierIsLValueRef, RefQualifierLoc,
65010b57cec5SDimitry Andric                     /*MutableLoc=*/SourceLocation(),
65020b57cec5SDimitry Andric                     ESpecType, ESpecRange, DynamicExceptions.data(),
65030b57cec5SDimitry Andric                     DynamicExceptionRanges.data(), DynamicExceptions.size(),
65040b57cec5SDimitry Andric                     NoexceptExpr.isUsable() ? NoexceptExpr.get() : nullptr,
65050b57cec5SDimitry Andric                     ExceptionSpecTokens, DeclsInPrototype, StartLoc,
65060b57cec5SDimitry Andric                     LocalEndLoc, D, TrailingReturnType, &DS),
65070b57cec5SDimitry Andric                 std::move(FnAttrs), EndLoc);
65080b57cec5SDimitry Andric }
65090b57cec5SDimitry Andric 
65100b57cec5SDimitry Andric /// ParseRefQualifier - Parses a member function ref-qualifier. Returns
65110b57cec5SDimitry Andric /// true if a ref-qualifier is found.
65120b57cec5SDimitry Andric bool Parser::ParseRefQualifier(bool &RefQualifierIsLValueRef,
65130b57cec5SDimitry Andric                                SourceLocation &RefQualifierLoc) {
65140b57cec5SDimitry Andric   if (Tok.isOneOf(tok::amp, tok::ampamp)) {
65150b57cec5SDimitry Andric     Diag(Tok, getLangOpts().CPlusPlus11 ?
65160b57cec5SDimitry Andric          diag::warn_cxx98_compat_ref_qualifier :
65170b57cec5SDimitry Andric          diag::ext_ref_qualifier);
65180b57cec5SDimitry Andric 
65190b57cec5SDimitry Andric     RefQualifierIsLValueRef = Tok.is(tok::amp);
65200b57cec5SDimitry Andric     RefQualifierLoc = ConsumeToken();
65210b57cec5SDimitry Andric     return true;
65220b57cec5SDimitry Andric   }
65230b57cec5SDimitry Andric   return false;
65240b57cec5SDimitry Andric }
65250b57cec5SDimitry Andric 
65260b57cec5SDimitry Andric /// isFunctionDeclaratorIdentifierList - This parameter list may have an
65270b57cec5SDimitry Andric /// identifier list form for a K&R-style function:  void foo(a,b,c)
65280b57cec5SDimitry Andric ///
65290b57cec5SDimitry Andric /// Note that identifier-lists are only allowed for normal declarators, not for
65300b57cec5SDimitry Andric /// abstract-declarators.
65310b57cec5SDimitry Andric bool Parser::isFunctionDeclaratorIdentifierList() {
65320b57cec5SDimitry Andric   return !getLangOpts().CPlusPlus
65330b57cec5SDimitry Andric          && Tok.is(tok::identifier)
65340b57cec5SDimitry Andric          && !TryAltiVecVectorToken()
65350b57cec5SDimitry Andric          // K&R identifier lists can't have typedefs as identifiers, per C99
65360b57cec5SDimitry Andric          // 6.7.5.3p11.
65370b57cec5SDimitry Andric          && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
65380b57cec5SDimitry Andric          // Identifier lists follow a really simple grammar: the identifiers can
65390b57cec5SDimitry Andric          // be followed *only* by a ", identifier" or ")".  However, K&R
65400b57cec5SDimitry Andric          // identifier lists are really rare in the brave new modern world, and
65410b57cec5SDimitry Andric          // it is very common for someone to typo a type in a non-K&R style
65420b57cec5SDimitry Andric          // list.  If we are presented with something like: "void foo(intptr x,
65430b57cec5SDimitry Andric          // float y)", we don't want to start parsing the function declarator as
65440b57cec5SDimitry Andric          // though it is a K&R style declarator just because intptr is an
65450b57cec5SDimitry Andric          // invalid type.
65460b57cec5SDimitry Andric          //
65470b57cec5SDimitry Andric          // To handle this, we check to see if the token after the first
65480b57cec5SDimitry Andric          // identifier is a "," or ")".  Only then do we parse it as an
65490b57cec5SDimitry Andric          // identifier list.
65500b57cec5SDimitry Andric          && (!Tok.is(tok::eof) &&
65510b57cec5SDimitry Andric              (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)));
65520b57cec5SDimitry Andric }
65530b57cec5SDimitry Andric 
65540b57cec5SDimitry Andric /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
65550b57cec5SDimitry Andric /// we found a K&R-style identifier list instead of a typed parameter list.
65560b57cec5SDimitry Andric ///
65570b57cec5SDimitry Andric /// After returning, ParamInfo will hold the parsed parameters.
65580b57cec5SDimitry Andric ///
65590b57cec5SDimitry Andric ///       identifier-list: [C99 6.7.5]
65600b57cec5SDimitry Andric ///         identifier
65610b57cec5SDimitry Andric ///         identifier-list ',' identifier
65620b57cec5SDimitry Andric ///
65630b57cec5SDimitry Andric void Parser::ParseFunctionDeclaratorIdentifierList(
65640b57cec5SDimitry Andric        Declarator &D,
65650b57cec5SDimitry Andric        SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
65660b57cec5SDimitry Andric   // If there was no identifier specified for the declarator, either we are in
65670b57cec5SDimitry Andric   // an abstract-declarator, or we are in a parameter declarator which was found
65680b57cec5SDimitry Andric   // to be abstract.  In abstract-declarators, identifier lists are not valid:
65690b57cec5SDimitry Andric   // diagnose this.
65700b57cec5SDimitry Andric   if (!D.getIdentifier())
65710b57cec5SDimitry Andric     Diag(Tok, diag::ext_ident_list_in_param);
65720b57cec5SDimitry Andric 
65730b57cec5SDimitry Andric   // Maintain an efficient lookup of params we have seen so far.
65740b57cec5SDimitry Andric   llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
65750b57cec5SDimitry Andric 
65760b57cec5SDimitry Andric   do {
65770b57cec5SDimitry Andric     // If this isn't an identifier, report the error and skip until ')'.
65780b57cec5SDimitry Andric     if (Tok.isNot(tok::identifier)) {
65790b57cec5SDimitry Andric       Diag(Tok, diag::err_expected) << tok::identifier;
65800b57cec5SDimitry Andric       SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
65810b57cec5SDimitry Andric       // Forget we parsed anything.
65820b57cec5SDimitry Andric       ParamInfo.clear();
65830b57cec5SDimitry Andric       return;
65840b57cec5SDimitry Andric     }
65850b57cec5SDimitry Andric 
65860b57cec5SDimitry Andric     IdentifierInfo *ParmII = Tok.getIdentifierInfo();
65870b57cec5SDimitry Andric 
65880b57cec5SDimitry Andric     // Reject 'typedef int y; int test(x, y)', but continue parsing.
65890b57cec5SDimitry Andric     if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
65900b57cec5SDimitry Andric       Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
65910b57cec5SDimitry Andric 
65920b57cec5SDimitry Andric     // Verify that the argument identifier has not already been mentioned.
65930b57cec5SDimitry Andric     if (!ParamsSoFar.insert(ParmII).second) {
65940b57cec5SDimitry Andric       Diag(Tok, diag::err_param_redefinition) << ParmII;
65950b57cec5SDimitry Andric     } else {
65960b57cec5SDimitry Andric       // Remember this identifier in ParamInfo.
65970b57cec5SDimitry Andric       ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
65980b57cec5SDimitry Andric                                                      Tok.getLocation(),
65990b57cec5SDimitry Andric                                                      nullptr));
66000b57cec5SDimitry Andric     }
66010b57cec5SDimitry Andric 
66020b57cec5SDimitry Andric     // Eat the identifier.
66030b57cec5SDimitry Andric     ConsumeToken();
66040b57cec5SDimitry Andric     // The list continues if we see a comma.
66050b57cec5SDimitry Andric   } while (TryConsumeToken(tok::comma));
66060b57cec5SDimitry Andric }
66070b57cec5SDimitry Andric 
66080b57cec5SDimitry Andric /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
66090b57cec5SDimitry Andric /// after the opening parenthesis. This function will not parse a K&R-style
66100b57cec5SDimitry Andric /// identifier list.
66110b57cec5SDimitry Andric ///
661255e4f9d5SDimitry Andric /// DeclContext is the context of the declarator being parsed.  If FirstArgAttrs
661355e4f9d5SDimitry Andric /// is non-null, then the caller parsed those attributes immediately after the
661455e4f9d5SDimitry Andric /// open paren - they should be considered to be part of the first parameter.
66150b57cec5SDimitry Andric ///
66160b57cec5SDimitry Andric /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
66170b57cec5SDimitry Andric /// be the location of the ellipsis, if any was parsed.
66180b57cec5SDimitry Andric ///
66190b57cec5SDimitry Andric ///       parameter-type-list: [C99 6.7.5]
66200b57cec5SDimitry Andric ///         parameter-list
66210b57cec5SDimitry Andric ///         parameter-list ',' '...'
66220b57cec5SDimitry Andric /// [C++]   parameter-list '...'
66230b57cec5SDimitry Andric ///
66240b57cec5SDimitry Andric ///       parameter-list: [C99 6.7.5]
66250b57cec5SDimitry Andric ///         parameter-declaration
66260b57cec5SDimitry Andric ///         parameter-list ',' parameter-declaration
66270b57cec5SDimitry Andric ///
66280b57cec5SDimitry Andric ///       parameter-declaration: [C99 6.7.5]
66290b57cec5SDimitry Andric ///         declaration-specifiers declarator
66300b57cec5SDimitry Andric /// [C++]   declaration-specifiers declarator '=' assignment-expression
66310b57cec5SDimitry Andric /// [C++11]                                       initializer-clause
66320b57cec5SDimitry Andric /// [GNU]   declaration-specifiers declarator attributes
66330b57cec5SDimitry Andric ///         declaration-specifiers abstract-declarator[opt]
66340b57cec5SDimitry Andric /// [C++]   declaration-specifiers abstract-declarator[opt]
66350b57cec5SDimitry Andric ///           '=' assignment-expression
66360b57cec5SDimitry Andric /// [GNU]   declaration-specifiers abstract-declarator[opt] attributes
66370b57cec5SDimitry Andric /// [C++11] attribute-specifier-seq parameter-declaration
66380b57cec5SDimitry Andric ///
66390b57cec5SDimitry Andric void Parser::ParseParameterDeclarationClause(
664055e4f9d5SDimitry Andric        DeclaratorContext DeclaratorCtx,
66410b57cec5SDimitry Andric        ParsedAttributes &FirstArgAttrs,
66420b57cec5SDimitry Andric        SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
66430b57cec5SDimitry Andric        SourceLocation &EllipsisLoc) {
6644480093f4SDimitry Andric 
6645480093f4SDimitry Andric   // Avoid exceeding the maximum function scope depth.
6646480093f4SDimitry Andric   // See https://bugs.llvm.org/show_bug.cgi?id=19607
6647480093f4SDimitry Andric   // Note Sema::ActOnParamDeclarator calls ParmVarDecl::setScopeInfo with
6648480093f4SDimitry Andric   // getFunctionPrototypeDepth() - 1.
6649480093f4SDimitry Andric   if (getCurScope()->getFunctionPrototypeDepth() - 1 >
6650480093f4SDimitry Andric       ParmVarDecl::getMaxFunctionScopeDepth()) {
6651480093f4SDimitry Andric     Diag(Tok.getLocation(), diag::err_function_scope_depth_exceeded)
6652480093f4SDimitry Andric         << ParmVarDecl::getMaxFunctionScopeDepth();
6653480093f4SDimitry Andric     cutOffParsing();
6654480093f4SDimitry Andric     return;
6655480093f4SDimitry Andric   }
6656480093f4SDimitry Andric 
66570b57cec5SDimitry Andric   do {
66580b57cec5SDimitry Andric     // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
66590b57cec5SDimitry Andric     // before deciding this was a parameter-declaration-clause.
66600b57cec5SDimitry Andric     if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
66610b57cec5SDimitry Andric       break;
66620b57cec5SDimitry Andric 
66630b57cec5SDimitry Andric     // Parse the declaration-specifiers.
66640b57cec5SDimitry Andric     // Just use the ParsingDeclaration "scope" of the declarator.
66650b57cec5SDimitry Andric     DeclSpec DS(AttrFactory);
66660b57cec5SDimitry Andric 
66670b57cec5SDimitry Andric     // Parse any C++11 attributes.
66680b57cec5SDimitry Andric     MaybeParseCXX11Attributes(DS.getAttributes());
66690b57cec5SDimitry Andric 
66700b57cec5SDimitry Andric     // Skip any Microsoft attributes before a param.
66710b57cec5SDimitry Andric     MaybeParseMicrosoftAttributes(DS.getAttributes());
66720b57cec5SDimitry Andric 
66730b57cec5SDimitry Andric     SourceLocation DSStart = Tok.getLocation();
66740b57cec5SDimitry Andric 
66750b57cec5SDimitry Andric     // If the caller parsed attributes for the first argument, add them now.
66760b57cec5SDimitry Andric     // Take them so that we only apply the attributes to the first parameter.
66770b57cec5SDimitry Andric     // FIXME: If we can leave the attributes in the token stream somehow, we can
66780b57cec5SDimitry Andric     // get rid of a parameter (FirstArgAttrs) and this statement. It might be
66790b57cec5SDimitry Andric     // too much hassle.
66800b57cec5SDimitry Andric     DS.takeAttributesFrom(FirstArgAttrs);
66810b57cec5SDimitry Andric 
66820b57cec5SDimitry Andric     ParseDeclarationSpecifiers(DS);
66830b57cec5SDimitry Andric 
66840b57cec5SDimitry Andric 
66850b57cec5SDimitry Andric     // Parse the declarator.  This is "PrototypeContext" or
66860b57cec5SDimitry Andric     // "LambdaExprParameterContext", because we must accept either
66870b57cec5SDimitry Andric     // 'declarator' or 'abstract-declarator' here.
66880b57cec5SDimitry Andric     Declarator ParmDeclarator(
668955e4f9d5SDimitry Andric         DS, DeclaratorCtx == DeclaratorContext::RequiresExprContext
669055e4f9d5SDimitry Andric                 ? DeclaratorContext::RequiresExprContext
669155e4f9d5SDimitry Andric                 : DeclaratorCtx == DeclaratorContext::LambdaExprContext
66920b57cec5SDimitry Andric                       ? DeclaratorContext::LambdaExprParameterContext
66930b57cec5SDimitry Andric                       : DeclaratorContext::PrototypeContext);
66940b57cec5SDimitry Andric     ParseDeclarator(ParmDeclarator);
66950b57cec5SDimitry Andric 
66960b57cec5SDimitry Andric     // Parse GNU attributes, if present.
66970b57cec5SDimitry Andric     MaybeParseGNUAttributes(ParmDeclarator);
66980b57cec5SDimitry Andric 
6699480093f4SDimitry Andric     if (Tok.is(tok::kw_requires)) {
6700480093f4SDimitry Andric       // User tried to define a requires clause in a parameter declaration,
6701480093f4SDimitry Andric       // which is surely not a function declaration.
6702480093f4SDimitry Andric       // void f(int (*g)(int, int) requires true);
6703480093f4SDimitry Andric       Diag(Tok,
6704480093f4SDimitry Andric            diag::err_requires_clause_on_declarator_not_declaring_a_function);
6705480093f4SDimitry Andric       ConsumeToken();
6706480093f4SDimitry Andric       Actions.CorrectDelayedTyposInExpr(
6707480093f4SDimitry Andric          ParseConstraintLogicalOrExpression(/*IsTrailingRequiresClause=*/true));
6708480093f4SDimitry Andric     }
6709480093f4SDimitry Andric 
67100b57cec5SDimitry Andric     // Remember this parsed parameter in ParamInfo.
67110b57cec5SDimitry Andric     IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
67120b57cec5SDimitry Andric 
67130b57cec5SDimitry Andric     // DefArgToks is used when the parsing of default arguments needs
67140b57cec5SDimitry Andric     // to be delayed.
67150b57cec5SDimitry Andric     std::unique_ptr<CachedTokens> DefArgToks;
67160b57cec5SDimitry Andric 
67170b57cec5SDimitry Andric     // If no parameter was specified, verify that *something* was specified,
67180b57cec5SDimitry Andric     // otherwise we have a missing type and identifier.
67190b57cec5SDimitry Andric     if (DS.isEmpty() && ParmDeclarator.getIdentifier() == nullptr &&
67200b57cec5SDimitry Andric         ParmDeclarator.getNumTypeObjects() == 0) {
67210b57cec5SDimitry Andric       // Completely missing, emit error.
67220b57cec5SDimitry Andric       Diag(DSStart, diag::err_missing_param);
67230b57cec5SDimitry Andric     } else {
67240b57cec5SDimitry Andric       // Otherwise, we have something.  Add it and let semantic analysis try
67250b57cec5SDimitry Andric       // to grok it and add the result to the ParamInfo we are building.
67260b57cec5SDimitry Andric 
67270b57cec5SDimitry Andric       // Last chance to recover from a misplaced ellipsis in an attempted
67280b57cec5SDimitry Andric       // parameter pack declaration.
67290b57cec5SDimitry Andric       if (Tok.is(tok::ellipsis) &&
67300b57cec5SDimitry Andric           (NextToken().isNot(tok::r_paren) ||
67310b57cec5SDimitry Andric            (!ParmDeclarator.getEllipsisLoc().isValid() &&
67320b57cec5SDimitry Andric             !Actions.isUnexpandedParameterPackPermitted())) &&
67330b57cec5SDimitry Andric           Actions.containsUnexpandedParameterPacks(ParmDeclarator))
67340b57cec5SDimitry Andric         DiagnoseMisplacedEllipsisInDeclarator(ConsumeToken(), ParmDeclarator);
67350b57cec5SDimitry Andric 
6736*5ffd83dbSDimitry Andric       // Now we are at the point where declarator parsing is finished.
6737*5ffd83dbSDimitry Andric       //
6738*5ffd83dbSDimitry Andric       // Try to catch keywords in place of the identifier in a declarator, and
6739*5ffd83dbSDimitry Andric       // in particular the common case where:
6740*5ffd83dbSDimitry Andric       //   1 identifier comes at the end of the declarator
6741*5ffd83dbSDimitry Andric       //   2 if the identifier is dropped, the declarator is valid but anonymous
6742*5ffd83dbSDimitry Andric       //     (no identifier)
6743*5ffd83dbSDimitry Andric       //   3 declarator parsing succeeds, and then we have a trailing keyword,
6744*5ffd83dbSDimitry Andric       //     which is never valid in a param list (e.g. missing a ',')
6745*5ffd83dbSDimitry Andric       // And we can't handle this in ParseDeclarator because in general keywords
6746*5ffd83dbSDimitry Andric       // may be allowed to follow the declarator. (And in some cases there'd be
6747*5ffd83dbSDimitry Andric       // better recovery like inserting punctuation). ParseDeclarator is just
6748*5ffd83dbSDimitry Andric       // treating this as an anonymous parameter, and fortunately at this point
6749*5ffd83dbSDimitry Andric       // we've already almost done that.
6750*5ffd83dbSDimitry Andric       //
6751*5ffd83dbSDimitry Andric       // We care about case 1) where the declarator type should be known, and
6752*5ffd83dbSDimitry Andric       // the identifier should be null.
6753*5ffd83dbSDimitry Andric       if (!ParmDeclarator.isInvalidType() && !ParmDeclarator.hasName()) {
6754*5ffd83dbSDimitry Andric         if (Tok.getIdentifierInfo() &&
6755*5ffd83dbSDimitry Andric             Tok.getIdentifierInfo()->isKeyword(getLangOpts())) {
6756*5ffd83dbSDimitry Andric           Diag(Tok, diag::err_keyword_as_parameter) << PP.getSpelling(Tok);
6757*5ffd83dbSDimitry Andric           // Consume the keyword.
6758*5ffd83dbSDimitry Andric           ConsumeToken();
6759*5ffd83dbSDimitry Andric         }
6760*5ffd83dbSDimitry Andric       }
67610b57cec5SDimitry Andric       // Inform the actions module about the parameter declarator, so it gets
67620b57cec5SDimitry Andric       // added to the current scope.
67630b57cec5SDimitry Andric       Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
67640b57cec5SDimitry Andric       // Parse the default argument, if any. We parse the default
67650b57cec5SDimitry Andric       // arguments in all dialects; the semantic analysis in
67660b57cec5SDimitry Andric       // ActOnParamDefaultArgument will reject the default argument in
67670b57cec5SDimitry Andric       // C.
67680b57cec5SDimitry Andric       if (Tok.is(tok::equal)) {
67690b57cec5SDimitry Andric         SourceLocation EqualLoc = Tok.getLocation();
67700b57cec5SDimitry Andric 
67710b57cec5SDimitry Andric         // Parse the default argument
677255e4f9d5SDimitry Andric         if (DeclaratorCtx == DeclaratorContext::MemberContext) {
67730b57cec5SDimitry Andric           // If we're inside a class definition, cache the tokens
67740b57cec5SDimitry Andric           // corresponding to the default argument. We'll actually parse
67750b57cec5SDimitry Andric           // them when we see the end of the class definition.
67760b57cec5SDimitry Andric           DefArgToks.reset(new CachedTokens);
67770b57cec5SDimitry Andric 
67780b57cec5SDimitry Andric           SourceLocation ArgStartLoc = NextToken().getLocation();
67790b57cec5SDimitry Andric           if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
67800b57cec5SDimitry Andric             DefArgToks.reset();
67810b57cec5SDimitry Andric             Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
67820b57cec5SDimitry Andric           } else {
67830b57cec5SDimitry Andric             Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
67840b57cec5SDimitry Andric                                                       ArgStartLoc);
67850b57cec5SDimitry Andric           }
67860b57cec5SDimitry Andric         } else {
67870b57cec5SDimitry Andric           // Consume the '='.
67880b57cec5SDimitry Andric           ConsumeToken();
67890b57cec5SDimitry Andric 
67900b57cec5SDimitry Andric           // The argument isn't actually potentially evaluated unless it is
67910b57cec5SDimitry Andric           // used.
67920b57cec5SDimitry Andric           EnterExpressionEvaluationContext Eval(
67930b57cec5SDimitry Andric               Actions,
67940b57cec5SDimitry Andric               Sema::ExpressionEvaluationContext::PotentiallyEvaluatedIfUsed,
67950b57cec5SDimitry Andric               Param);
67960b57cec5SDimitry Andric 
67970b57cec5SDimitry Andric           ExprResult DefArgResult;
67980b57cec5SDimitry Andric           if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
67990b57cec5SDimitry Andric             Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
68000b57cec5SDimitry Andric             DefArgResult = ParseBraceInitializer();
68010b57cec5SDimitry Andric           } else
68020b57cec5SDimitry Andric             DefArgResult = ParseAssignmentExpression();
68030b57cec5SDimitry Andric           DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
68040b57cec5SDimitry Andric           if (DefArgResult.isInvalid()) {
68050b57cec5SDimitry Andric             Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
68060b57cec5SDimitry Andric             SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
68070b57cec5SDimitry Andric           } else {
68080b57cec5SDimitry Andric             // Inform the actions module about the default argument
68090b57cec5SDimitry Andric             Actions.ActOnParamDefaultArgument(Param, EqualLoc,
68100b57cec5SDimitry Andric                                               DefArgResult.get());
68110b57cec5SDimitry Andric           }
68120b57cec5SDimitry Andric         }
68130b57cec5SDimitry Andric       }
68140b57cec5SDimitry Andric 
68150b57cec5SDimitry Andric       ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
68160b57cec5SDimitry Andric                                           ParmDeclarator.getIdentifierLoc(),
68170b57cec5SDimitry Andric                                           Param, std::move(DefArgToks)));
68180b57cec5SDimitry Andric     }
68190b57cec5SDimitry Andric 
68200b57cec5SDimitry Andric     if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) {
68210b57cec5SDimitry Andric       if (!getLangOpts().CPlusPlus) {
68220b57cec5SDimitry Andric         // We have ellipsis without a preceding ',', which is ill-formed
68230b57cec5SDimitry Andric         // in C. Complain and provide the fix.
68240b57cec5SDimitry Andric         Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
68250b57cec5SDimitry Andric             << FixItHint::CreateInsertion(EllipsisLoc, ", ");
68260b57cec5SDimitry Andric       } else if (ParmDeclarator.getEllipsisLoc().isValid() ||
68270b57cec5SDimitry Andric                  Actions.containsUnexpandedParameterPacks(ParmDeclarator)) {
68280b57cec5SDimitry Andric         // It looks like this was supposed to be a parameter pack. Warn and
68290b57cec5SDimitry Andric         // point out where the ellipsis should have gone.
68300b57cec5SDimitry Andric         SourceLocation ParmEllipsis = ParmDeclarator.getEllipsisLoc();
68310b57cec5SDimitry Andric         Diag(EllipsisLoc, diag::warn_misplaced_ellipsis_vararg)
68320b57cec5SDimitry Andric           << ParmEllipsis.isValid() << ParmEllipsis;
68330b57cec5SDimitry Andric         if (ParmEllipsis.isValid()) {
68340b57cec5SDimitry Andric           Diag(ParmEllipsis,
68350b57cec5SDimitry Andric                diag::note_misplaced_ellipsis_vararg_existing_ellipsis);
68360b57cec5SDimitry Andric         } else {
68370b57cec5SDimitry Andric           Diag(ParmDeclarator.getIdentifierLoc(),
68380b57cec5SDimitry Andric                diag::note_misplaced_ellipsis_vararg_add_ellipsis)
68390b57cec5SDimitry Andric             << FixItHint::CreateInsertion(ParmDeclarator.getIdentifierLoc(),
68400b57cec5SDimitry Andric                                           "...")
68410b57cec5SDimitry Andric             << !ParmDeclarator.hasName();
68420b57cec5SDimitry Andric         }
68430b57cec5SDimitry Andric         Diag(EllipsisLoc, diag::note_misplaced_ellipsis_vararg_add_comma)
68440b57cec5SDimitry Andric           << FixItHint::CreateInsertion(EllipsisLoc, ", ");
68450b57cec5SDimitry Andric       }
68460b57cec5SDimitry Andric 
68470b57cec5SDimitry Andric       // We can't have any more parameters after an ellipsis.
68480b57cec5SDimitry Andric       break;
68490b57cec5SDimitry Andric     }
68500b57cec5SDimitry Andric 
68510b57cec5SDimitry Andric     // If the next token is a comma, consume it and keep reading arguments.
68520b57cec5SDimitry Andric   } while (TryConsumeToken(tok::comma));
68530b57cec5SDimitry Andric }
68540b57cec5SDimitry Andric 
68550b57cec5SDimitry Andric /// [C90]   direct-declarator '[' constant-expression[opt] ']'
68560b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
68570b57cec5SDimitry Andric /// [C99]   direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
68580b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list 'static' assignment-expr ']'
68590b57cec5SDimitry Andric /// [C99]   direct-declarator '[' type-qual-list[opt] '*' ']'
68600b57cec5SDimitry Andric /// [C++11] direct-declarator '[' constant-expression[opt] ']'
68610b57cec5SDimitry Andric ///                           attribute-specifier-seq[opt]
68620b57cec5SDimitry Andric void Parser::ParseBracketDeclarator(Declarator &D) {
68630b57cec5SDimitry Andric   if (CheckProhibitedCXX11Attribute())
68640b57cec5SDimitry Andric     return;
68650b57cec5SDimitry Andric 
68660b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_square);
68670b57cec5SDimitry Andric   T.consumeOpen();
68680b57cec5SDimitry Andric 
68690b57cec5SDimitry Andric   // C array syntax has many features, but by-far the most common is [] and [4].
68700b57cec5SDimitry Andric   // This code does a fast path to handle some of the most obvious cases.
68710b57cec5SDimitry Andric   if (Tok.getKind() == tok::r_square) {
68720b57cec5SDimitry Andric     T.consumeClose();
68730b57cec5SDimitry Andric     ParsedAttributes attrs(AttrFactory);
68740b57cec5SDimitry Andric     MaybeParseCXX11Attributes(attrs);
68750b57cec5SDimitry Andric 
68760b57cec5SDimitry Andric     // Remember that we parsed the empty array type.
68770b57cec5SDimitry Andric     D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, nullptr,
68780b57cec5SDimitry Andric                                             T.getOpenLocation(),
68790b57cec5SDimitry Andric                                             T.getCloseLocation()),
68800b57cec5SDimitry Andric                   std::move(attrs), T.getCloseLocation());
68810b57cec5SDimitry Andric     return;
68820b57cec5SDimitry Andric   } else if (Tok.getKind() == tok::numeric_constant &&
68830b57cec5SDimitry Andric              GetLookAheadToken(1).is(tok::r_square)) {
68840b57cec5SDimitry Andric     // [4] is very common.  Parse the numeric constant expression.
68850b57cec5SDimitry Andric     ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
68860b57cec5SDimitry Andric     ConsumeToken();
68870b57cec5SDimitry Andric 
68880b57cec5SDimitry Andric     T.consumeClose();
68890b57cec5SDimitry Andric     ParsedAttributes attrs(AttrFactory);
68900b57cec5SDimitry Andric     MaybeParseCXX11Attributes(attrs);
68910b57cec5SDimitry Andric 
68920b57cec5SDimitry Andric     // Remember that we parsed a array type, and remember its features.
68930b57cec5SDimitry Andric     D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, ExprRes.get(),
68940b57cec5SDimitry Andric                                             T.getOpenLocation(),
68950b57cec5SDimitry Andric                                             T.getCloseLocation()),
68960b57cec5SDimitry Andric                   std::move(attrs), T.getCloseLocation());
68970b57cec5SDimitry Andric     return;
68980b57cec5SDimitry Andric   } else if (Tok.getKind() == tok::code_completion) {
68990b57cec5SDimitry Andric     Actions.CodeCompleteBracketDeclarator(getCurScope());
69000b57cec5SDimitry Andric     return cutOffParsing();
69010b57cec5SDimitry Andric   }
69020b57cec5SDimitry Andric 
69030b57cec5SDimitry Andric   // If valid, this location is the position where we read the 'static' keyword.
69040b57cec5SDimitry Andric   SourceLocation StaticLoc;
69050b57cec5SDimitry Andric   TryConsumeToken(tok::kw_static, StaticLoc);
69060b57cec5SDimitry Andric 
69070b57cec5SDimitry Andric   // If there is a type-qualifier-list, read it now.
69080b57cec5SDimitry Andric   // Type qualifiers in an array subscript are a C99 feature.
69090b57cec5SDimitry Andric   DeclSpec DS(AttrFactory);
69100b57cec5SDimitry Andric   ParseTypeQualifierListOpt(DS, AR_CXX11AttributesParsed);
69110b57cec5SDimitry Andric 
69120b57cec5SDimitry Andric   // If we haven't already read 'static', check to see if there is one after the
69130b57cec5SDimitry Andric   // type-qualifier-list.
69140b57cec5SDimitry Andric   if (!StaticLoc.isValid())
69150b57cec5SDimitry Andric     TryConsumeToken(tok::kw_static, StaticLoc);
69160b57cec5SDimitry Andric 
69170b57cec5SDimitry Andric   // Handle "direct-declarator [ type-qual-list[opt] * ]".
69180b57cec5SDimitry Andric   bool isStar = false;
69190b57cec5SDimitry Andric   ExprResult NumElements;
69200b57cec5SDimitry Andric 
69210b57cec5SDimitry Andric   // Handle the case where we have '[*]' as the array size.  However, a leading
69220b57cec5SDimitry Andric   // star could be the start of an expression, for example 'X[*p + 4]'.  Verify
69230b57cec5SDimitry Andric   // the token after the star is a ']'.  Since stars in arrays are
69240b57cec5SDimitry Andric   // infrequent, use of lookahead is not costly here.
69250b57cec5SDimitry Andric   if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
69260b57cec5SDimitry Andric     ConsumeToken();  // Eat the '*'.
69270b57cec5SDimitry Andric 
69280b57cec5SDimitry Andric     if (StaticLoc.isValid()) {
69290b57cec5SDimitry Andric       Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
69300b57cec5SDimitry Andric       StaticLoc = SourceLocation();  // Drop the static.
69310b57cec5SDimitry Andric     }
69320b57cec5SDimitry Andric     isStar = true;
69330b57cec5SDimitry Andric   } else if (Tok.isNot(tok::r_square)) {
69340b57cec5SDimitry Andric     // Note, in C89, this production uses the constant-expr production instead
69350b57cec5SDimitry Andric     // of assignment-expr.  The only difference is that assignment-expr allows
69360b57cec5SDimitry Andric     // things like '=' and '*='.  Sema rejects these in C89 mode because they
69370b57cec5SDimitry Andric     // are not i-c-e's, so we don't need to distinguish between the two here.
69380b57cec5SDimitry Andric 
69390b57cec5SDimitry Andric     // Parse the constant-expression or assignment-expression now (depending
69400b57cec5SDimitry Andric     // on dialect).
69410b57cec5SDimitry Andric     if (getLangOpts().CPlusPlus) {
69420b57cec5SDimitry Andric       NumElements = ParseConstantExpression();
69430b57cec5SDimitry Andric     } else {
69440b57cec5SDimitry Andric       EnterExpressionEvaluationContext Unevaluated(
69450b57cec5SDimitry Andric           Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
69460b57cec5SDimitry Andric       NumElements =
69470b57cec5SDimitry Andric           Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
69480b57cec5SDimitry Andric     }
69490b57cec5SDimitry Andric   } else {
69500b57cec5SDimitry Andric     if (StaticLoc.isValid()) {
69510b57cec5SDimitry Andric       Diag(StaticLoc, diag::err_unspecified_size_with_static);
69520b57cec5SDimitry Andric       StaticLoc = SourceLocation();  // Drop the static.
69530b57cec5SDimitry Andric     }
69540b57cec5SDimitry Andric   }
69550b57cec5SDimitry Andric 
69560b57cec5SDimitry Andric   // If there was an error parsing the assignment-expression, recover.
69570b57cec5SDimitry Andric   if (NumElements.isInvalid()) {
69580b57cec5SDimitry Andric     D.setInvalidType(true);
69590b57cec5SDimitry Andric     // If the expression was invalid, skip it.
69600b57cec5SDimitry Andric     SkipUntil(tok::r_square, StopAtSemi);
69610b57cec5SDimitry Andric     return;
69620b57cec5SDimitry Andric   }
69630b57cec5SDimitry Andric 
69640b57cec5SDimitry Andric   T.consumeClose();
69650b57cec5SDimitry Andric 
69660b57cec5SDimitry Andric   MaybeParseCXX11Attributes(DS.getAttributes());
69670b57cec5SDimitry Andric 
69680b57cec5SDimitry Andric   // Remember that we parsed a array type, and remember its features.
69690b57cec5SDimitry Andric   D.AddTypeInfo(
69700b57cec5SDimitry Andric       DeclaratorChunk::getArray(DS.getTypeQualifiers(), StaticLoc.isValid(),
69710b57cec5SDimitry Andric                                 isStar, NumElements.get(), T.getOpenLocation(),
69720b57cec5SDimitry Andric                                 T.getCloseLocation()),
69730b57cec5SDimitry Andric       std::move(DS.getAttributes()), T.getCloseLocation());
69740b57cec5SDimitry Andric }
69750b57cec5SDimitry Andric 
69760b57cec5SDimitry Andric /// Diagnose brackets before an identifier.
69770b57cec5SDimitry Andric void Parser::ParseMisplacedBracketDeclarator(Declarator &D) {
69780b57cec5SDimitry Andric   assert(Tok.is(tok::l_square) && "Missing opening bracket");
69790b57cec5SDimitry Andric   assert(!D.mayOmitIdentifier() && "Declarator cannot omit identifier");
69800b57cec5SDimitry Andric 
69810b57cec5SDimitry Andric   SourceLocation StartBracketLoc = Tok.getLocation();
69820b57cec5SDimitry Andric   Declarator TempDeclarator(D.getDeclSpec(), D.getContext());
69830b57cec5SDimitry Andric 
69840b57cec5SDimitry Andric   while (Tok.is(tok::l_square)) {
69850b57cec5SDimitry Andric     ParseBracketDeclarator(TempDeclarator);
69860b57cec5SDimitry Andric   }
69870b57cec5SDimitry Andric 
69880b57cec5SDimitry Andric   // Stuff the location of the start of the brackets into the Declarator.
69890b57cec5SDimitry Andric   // The diagnostics from ParseDirectDeclarator will make more sense if
69900b57cec5SDimitry Andric   // they use this location instead.
69910b57cec5SDimitry Andric   if (Tok.is(tok::semi))
69920b57cec5SDimitry Andric     D.getName().EndLocation = StartBracketLoc;
69930b57cec5SDimitry Andric 
69940b57cec5SDimitry Andric   SourceLocation SuggestParenLoc = Tok.getLocation();
69950b57cec5SDimitry Andric 
69960b57cec5SDimitry Andric   // Now that the brackets are removed, try parsing the declarator again.
69970b57cec5SDimitry Andric   ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
69980b57cec5SDimitry Andric 
69990b57cec5SDimitry Andric   // Something went wrong parsing the brackets, in which case,
70000b57cec5SDimitry Andric   // ParseBracketDeclarator has emitted an error, and we don't need to emit
70010b57cec5SDimitry Andric   // one here.
70020b57cec5SDimitry Andric   if (TempDeclarator.getNumTypeObjects() == 0)
70030b57cec5SDimitry Andric     return;
70040b57cec5SDimitry Andric 
70050b57cec5SDimitry Andric   // Determine if parens will need to be suggested in the diagnostic.
70060b57cec5SDimitry Andric   bool NeedParens = false;
70070b57cec5SDimitry Andric   if (D.getNumTypeObjects() != 0) {
70080b57cec5SDimitry Andric     switch (D.getTypeObject(D.getNumTypeObjects() - 1).Kind) {
70090b57cec5SDimitry Andric     case DeclaratorChunk::Pointer:
70100b57cec5SDimitry Andric     case DeclaratorChunk::Reference:
70110b57cec5SDimitry Andric     case DeclaratorChunk::BlockPointer:
70120b57cec5SDimitry Andric     case DeclaratorChunk::MemberPointer:
70130b57cec5SDimitry Andric     case DeclaratorChunk::Pipe:
70140b57cec5SDimitry Andric       NeedParens = true;
70150b57cec5SDimitry Andric       break;
70160b57cec5SDimitry Andric     case DeclaratorChunk::Array:
70170b57cec5SDimitry Andric     case DeclaratorChunk::Function:
70180b57cec5SDimitry Andric     case DeclaratorChunk::Paren:
70190b57cec5SDimitry Andric       break;
70200b57cec5SDimitry Andric     }
70210b57cec5SDimitry Andric   }
70220b57cec5SDimitry Andric 
70230b57cec5SDimitry Andric   if (NeedParens) {
70240b57cec5SDimitry Andric     // Create a DeclaratorChunk for the inserted parens.
70250b57cec5SDimitry Andric     SourceLocation EndLoc = PP.getLocForEndOfToken(D.getEndLoc());
70260b57cec5SDimitry Andric     D.AddTypeInfo(DeclaratorChunk::getParen(SuggestParenLoc, EndLoc),
70270b57cec5SDimitry Andric                   SourceLocation());
70280b57cec5SDimitry Andric   }
70290b57cec5SDimitry Andric 
70300b57cec5SDimitry Andric   // Adding back the bracket info to the end of the Declarator.
70310b57cec5SDimitry Andric   for (unsigned i = 0, e = TempDeclarator.getNumTypeObjects(); i < e; ++i) {
70320b57cec5SDimitry Andric     const DeclaratorChunk &Chunk = TempDeclarator.getTypeObject(i);
70330b57cec5SDimitry Andric     D.AddTypeInfo(Chunk, SourceLocation());
70340b57cec5SDimitry Andric   }
70350b57cec5SDimitry Andric 
70360b57cec5SDimitry Andric   // The missing identifier would have been diagnosed in ParseDirectDeclarator.
70370b57cec5SDimitry Andric   // If parentheses are required, always suggest them.
70380b57cec5SDimitry Andric   if (!D.getIdentifier() && !NeedParens)
70390b57cec5SDimitry Andric     return;
70400b57cec5SDimitry Andric 
70410b57cec5SDimitry Andric   SourceLocation EndBracketLoc = TempDeclarator.getEndLoc();
70420b57cec5SDimitry Andric 
70430b57cec5SDimitry Andric   // Generate the move bracket error message.
70440b57cec5SDimitry Andric   SourceRange BracketRange(StartBracketLoc, EndBracketLoc);
70450b57cec5SDimitry Andric   SourceLocation EndLoc = PP.getLocForEndOfToken(D.getEndLoc());
70460b57cec5SDimitry Andric 
70470b57cec5SDimitry Andric   if (NeedParens) {
70480b57cec5SDimitry Andric     Diag(EndLoc, diag::err_brackets_go_after_unqualified_id)
70490b57cec5SDimitry Andric         << getLangOpts().CPlusPlus
70500b57cec5SDimitry Andric         << FixItHint::CreateInsertion(SuggestParenLoc, "(")
70510b57cec5SDimitry Andric         << FixItHint::CreateInsertion(EndLoc, ")")
70520b57cec5SDimitry Andric         << FixItHint::CreateInsertionFromRange(
70530b57cec5SDimitry Andric                EndLoc, CharSourceRange(BracketRange, true))
70540b57cec5SDimitry Andric         << FixItHint::CreateRemoval(BracketRange);
70550b57cec5SDimitry Andric   } else {
70560b57cec5SDimitry Andric     Diag(EndLoc, diag::err_brackets_go_after_unqualified_id)
70570b57cec5SDimitry Andric         << getLangOpts().CPlusPlus
70580b57cec5SDimitry Andric         << FixItHint::CreateInsertionFromRange(
70590b57cec5SDimitry Andric                EndLoc, CharSourceRange(BracketRange, true))
70600b57cec5SDimitry Andric         << FixItHint::CreateRemoval(BracketRange);
70610b57cec5SDimitry Andric   }
70620b57cec5SDimitry Andric }
70630b57cec5SDimitry Andric 
70640b57cec5SDimitry Andric /// [GNU]   typeof-specifier:
70650b57cec5SDimitry Andric ///           typeof ( expressions )
70660b57cec5SDimitry Andric ///           typeof ( type-name )
70670b57cec5SDimitry Andric /// [GNU/C++] typeof unary-expression
70680b57cec5SDimitry Andric ///
70690b57cec5SDimitry Andric void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
70700b57cec5SDimitry Andric   assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
70710b57cec5SDimitry Andric   Token OpTok = Tok;
70720b57cec5SDimitry Andric   SourceLocation StartLoc = ConsumeToken();
70730b57cec5SDimitry Andric 
70740b57cec5SDimitry Andric   const bool hasParens = Tok.is(tok::l_paren);
70750b57cec5SDimitry Andric 
70760b57cec5SDimitry Andric   EnterExpressionEvaluationContext Unevaluated(
70770b57cec5SDimitry Andric       Actions, Sema::ExpressionEvaluationContext::Unevaluated,
70780b57cec5SDimitry Andric       Sema::ReuseLambdaContextDecl);
70790b57cec5SDimitry Andric 
70800b57cec5SDimitry Andric   bool isCastExpr;
70810b57cec5SDimitry Andric   ParsedType CastTy;
70820b57cec5SDimitry Andric   SourceRange CastRange;
70830b57cec5SDimitry Andric   ExprResult Operand = Actions.CorrectDelayedTyposInExpr(
70840b57cec5SDimitry Andric       ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, CastTy, CastRange));
70850b57cec5SDimitry Andric   if (hasParens)
70860b57cec5SDimitry Andric     DS.setTypeofParensRange(CastRange);
70870b57cec5SDimitry Andric 
70880b57cec5SDimitry Andric   if (CastRange.getEnd().isInvalid())
70890b57cec5SDimitry Andric     // FIXME: Not accurate, the range gets one token more than it should.
70900b57cec5SDimitry Andric     DS.SetRangeEnd(Tok.getLocation());
70910b57cec5SDimitry Andric   else
70920b57cec5SDimitry Andric     DS.SetRangeEnd(CastRange.getEnd());
70930b57cec5SDimitry Andric 
70940b57cec5SDimitry Andric   if (isCastExpr) {
70950b57cec5SDimitry Andric     if (!CastTy) {
70960b57cec5SDimitry Andric       DS.SetTypeSpecError();
70970b57cec5SDimitry Andric       return;
70980b57cec5SDimitry Andric     }
70990b57cec5SDimitry Andric 
71000b57cec5SDimitry Andric     const char *PrevSpec = nullptr;
71010b57cec5SDimitry Andric     unsigned DiagID;
71020b57cec5SDimitry Andric     // Check for duplicate type specifiers (e.g. "int typeof(int)").
71030b57cec5SDimitry Andric     if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
71040b57cec5SDimitry Andric                            DiagID, CastTy,
71050b57cec5SDimitry Andric                            Actions.getASTContext().getPrintingPolicy()))
71060b57cec5SDimitry Andric       Diag(StartLoc, DiagID) << PrevSpec;
71070b57cec5SDimitry Andric     return;
71080b57cec5SDimitry Andric   }
71090b57cec5SDimitry Andric 
71100b57cec5SDimitry Andric   // If we get here, the operand to the typeof was an expression.
71110b57cec5SDimitry Andric   if (Operand.isInvalid()) {
71120b57cec5SDimitry Andric     DS.SetTypeSpecError();
71130b57cec5SDimitry Andric     return;
71140b57cec5SDimitry Andric   }
71150b57cec5SDimitry Andric 
71160b57cec5SDimitry Andric   // We might need to transform the operand if it is potentially evaluated.
71170b57cec5SDimitry Andric   Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
71180b57cec5SDimitry Andric   if (Operand.isInvalid()) {
71190b57cec5SDimitry Andric     DS.SetTypeSpecError();
71200b57cec5SDimitry Andric     return;
71210b57cec5SDimitry Andric   }
71220b57cec5SDimitry Andric 
71230b57cec5SDimitry Andric   const char *PrevSpec = nullptr;
71240b57cec5SDimitry Andric   unsigned DiagID;
71250b57cec5SDimitry Andric   // Check for duplicate type specifiers (e.g. "int typeof(int)").
71260b57cec5SDimitry Andric   if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
71270b57cec5SDimitry Andric                          DiagID, Operand.get(),
71280b57cec5SDimitry Andric                          Actions.getASTContext().getPrintingPolicy()))
71290b57cec5SDimitry Andric     Diag(StartLoc, DiagID) << PrevSpec;
71300b57cec5SDimitry Andric }
71310b57cec5SDimitry Andric 
71320b57cec5SDimitry Andric /// [C11]   atomic-specifier:
71330b57cec5SDimitry Andric ///           _Atomic ( type-name )
71340b57cec5SDimitry Andric ///
71350b57cec5SDimitry Andric void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
71360b57cec5SDimitry Andric   assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
71370b57cec5SDimitry Andric          "Not an atomic specifier");
71380b57cec5SDimitry Andric 
71390b57cec5SDimitry Andric   SourceLocation StartLoc = ConsumeToken();
71400b57cec5SDimitry Andric   BalancedDelimiterTracker T(*this, tok::l_paren);
71410b57cec5SDimitry Andric   if (T.consumeOpen())
71420b57cec5SDimitry Andric     return;
71430b57cec5SDimitry Andric 
71440b57cec5SDimitry Andric   TypeResult Result = ParseTypeName();
71450b57cec5SDimitry Andric   if (Result.isInvalid()) {
71460b57cec5SDimitry Andric     SkipUntil(tok::r_paren, StopAtSemi);
71470b57cec5SDimitry Andric     return;
71480b57cec5SDimitry Andric   }
71490b57cec5SDimitry Andric 
71500b57cec5SDimitry Andric   // Match the ')'
71510b57cec5SDimitry Andric   T.consumeClose();
71520b57cec5SDimitry Andric 
71530b57cec5SDimitry Andric   if (T.getCloseLocation().isInvalid())
71540b57cec5SDimitry Andric     return;
71550b57cec5SDimitry Andric 
71560b57cec5SDimitry Andric   DS.setTypeofParensRange(T.getRange());
71570b57cec5SDimitry Andric   DS.SetRangeEnd(T.getCloseLocation());
71580b57cec5SDimitry Andric 
71590b57cec5SDimitry Andric   const char *PrevSpec = nullptr;
71600b57cec5SDimitry Andric   unsigned DiagID;
71610b57cec5SDimitry Andric   if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
71620b57cec5SDimitry Andric                          DiagID, Result.get(),
71630b57cec5SDimitry Andric                          Actions.getASTContext().getPrintingPolicy()))
71640b57cec5SDimitry Andric     Diag(StartLoc, DiagID) << PrevSpec;
71650b57cec5SDimitry Andric }
71660b57cec5SDimitry Andric 
71670b57cec5SDimitry Andric /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
71680b57cec5SDimitry Andric /// from TryAltiVecVectorToken.
71690b57cec5SDimitry Andric bool Parser::TryAltiVecVectorTokenOutOfLine() {
71700b57cec5SDimitry Andric   Token Next = NextToken();
71710b57cec5SDimitry Andric   switch (Next.getKind()) {
71720b57cec5SDimitry Andric   default: return false;
71730b57cec5SDimitry Andric   case tok::kw_short:
71740b57cec5SDimitry Andric   case tok::kw_long:
71750b57cec5SDimitry Andric   case tok::kw_signed:
71760b57cec5SDimitry Andric   case tok::kw_unsigned:
71770b57cec5SDimitry Andric   case tok::kw_void:
71780b57cec5SDimitry Andric   case tok::kw_char:
71790b57cec5SDimitry Andric   case tok::kw_int:
71800b57cec5SDimitry Andric   case tok::kw_float:
71810b57cec5SDimitry Andric   case tok::kw_double:
71820b57cec5SDimitry Andric   case tok::kw_bool:
71830b57cec5SDimitry Andric   case tok::kw___bool:
71840b57cec5SDimitry Andric   case tok::kw___pixel:
71850b57cec5SDimitry Andric     Tok.setKind(tok::kw___vector);
71860b57cec5SDimitry Andric     return true;
71870b57cec5SDimitry Andric   case tok::identifier:
71880b57cec5SDimitry Andric     if (Next.getIdentifierInfo() == Ident_pixel) {
71890b57cec5SDimitry Andric       Tok.setKind(tok::kw___vector);
71900b57cec5SDimitry Andric       return true;
71910b57cec5SDimitry Andric     }
71920b57cec5SDimitry Andric     if (Next.getIdentifierInfo() == Ident_bool) {
71930b57cec5SDimitry Andric       Tok.setKind(tok::kw___vector);
71940b57cec5SDimitry Andric       return true;
71950b57cec5SDimitry Andric     }
71960b57cec5SDimitry Andric     return false;
71970b57cec5SDimitry Andric   }
71980b57cec5SDimitry Andric }
71990b57cec5SDimitry Andric 
72000b57cec5SDimitry Andric bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
72010b57cec5SDimitry Andric                                       const char *&PrevSpec, unsigned &DiagID,
72020b57cec5SDimitry Andric                                       bool &isInvalid) {
72030b57cec5SDimitry Andric   const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
72040b57cec5SDimitry Andric   if (Tok.getIdentifierInfo() == Ident_vector) {
72050b57cec5SDimitry Andric     Token Next = NextToken();
72060b57cec5SDimitry Andric     switch (Next.getKind()) {
72070b57cec5SDimitry Andric     case tok::kw_short:
72080b57cec5SDimitry Andric     case tok::kw_long:
72090b57cec5SDimitry Andric     case tok::kw_signed:
72100b57cec5SDimitry Andric     case tok::kw_unsigned:
72110b57cec5SDimitry Andric     case tok::kw_void:
72120b57cec5SDimitry Andric     case tok::kw_char:
72130b57cec5SDimitry Andric     case tok::kw_int:
72140b57cec5SDimitry Andric     case tok::kw_float:
72150b57cec5SDimitry Andric     case tok::kw_double:
72160b57cec5SDimitry Andric     case tok::kw_bool:
72170b57cec5SDimitry Andric     case tok::kw___bool:
72180b57cec5SDimitry Andric     case tok::kw___pixel:
72190b57cec5SDimitry Andric       isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
72200b57cec5SDimitry Andric       return true;
72210b57cec5SDimitry Andric     case tok::identifier:
72220b57cec5SDimitry Andric       if (Next.getIdentifierInfo() == Ident_pixel) {
72230b57cec5SDimitry Andric         isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
72240b57cec5SDimitry Andric         return true;
72250b57cec5SDimitry Andric       }
72260b57cec5SDimitry Andric       if (Next.getIdentifierInfo() == Ident_bool) {
72270b57cec5SDimitry Andric         isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
72280b57cec5SDimitry Andric         return true;
72290b57cec5SDimitry Andric       }
72300b57cec5SDimitry Andric       break;
72310b57cec5SDimitry Andric     default:
72320b57cec5SDimitry Andric       break;
72330b57cec5SDimitry Andric     }
72340b57cec5SDimitry Andric   } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
72350b57cec5SDimitry Andric              DS.isTypeAltiVecVector()) {
72360b57cec5SDimitry Andric     isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
72370b57cec5SDimitry Andric     return true;
72380b57cec5SDimitry Andric   } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
72390b57cec5SDimitry Andric              DS.isTypeAltiVecVector()) {
72400b57cec5SDimitry Andric     isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
72410b57cec5SDimitry Andric     return true;
72420b57cec5SDimitry Andric   }
72430b57cec5SDimitry Andric   return false;
72440b57cec5SDimitry Andric }
7245