xref: /aosp_15_r20/external/clang/lib/Parse/ParseDecl.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li //===--- ParseDecl.cpp - Declaration Parsing --------------------*- C++ -*-===//
2*67e74705SXin Li //
3*67e74705SXin Li //                     The LLVM Compiler Infrastructure
4*67e74705SXin Li //
5*67e74705SXin Li // This file is distributed under the University of Illinois Open Source
6*67e74705SXin Li // License. See LICENSE.TXT for details.
7*67e74705SXin Li //
8*67e74705SXin Li //===----------------------------------------------------------------------===//
9*67e74705SXin Li //
10*67e74705SXin Li //  This file implements the Declaration portions of the Parser interfaces.
11*67e74705SXin Li //
12*67e74705SXin Li //===----------------------------------------------------------------------===//
13*67e74705SXin Li 
14*67e74705SXin Li #include "clang/Parse/Parser.h"
15*67e74705SXin Li #include "RAIIObjectsForParser.h"
16*67e74705SXin Li #include "clang/AST/ASTContext.h"
17*67e74705SXin Li #include "clang/AST/DeclTemplate.h"
18*67e74705SXin Li #include "clang/Basic/AddressSpaces.h"
19*67e74705SXin Li #include "clang/Basic/Attributes.h"
20*67e74705SXin Li #include "clang/Basic/CharInfo.h"
21*67e74705SXin Li #include "clang/Basic/TargetInfo.h"
22*67e74705SXin Li #include "clang/Parse/ParseDiagnostic.h"
23*67e74705SXin Li #include "clang/Sema/Lookup.h"
24*67e74705SXin Li #include "clang/Sema/ParsedTemplate.h"
25*67e74705SXin Li #include "clang/Sema/PrettyDeclStackTrace.h"
26*67e74705SXin Li #include "clang/Sema/Scope.h"
27*67e74705SXin Li #include "clang/Sema/SemaDiagnostic.h"
28*67e74705SXin Li #include "llvm/ADT/SmallSet.h"
29*67e74705SXin Li #include "llvm/ADT/SmallString.h"
30*67e74705SXin Li #include "llvm/ADT/StringSwitch.h"
31*67e74705SXin Li #include "llvm/Support/ScopedPrinter.h"
32*67e74705SXin Li 
33*67e74705SXin Li using namespace clang;
34*67e74705SXin Li 
35*67e74705SXin Li //===----------------------------------------------------------------------===//
36*67e74705SXin Li // C99 6.7: Declarations.
37*67e74705SXin Li //===----------------------------------------------------------------------===//
38*67e74705SXin Li 
39*67e74705SXin Li /// ParseTypeName
40*67e74705SXin Li ///       type-name: [C99 6.7.6]
41*67e74705SXin Li ///         specifier-qualifier-list abstract-declarator[opt]
42*67e74705SXin Li ///
43*67e74705SXin Li /// Called type-id in C++.
ParseTypeName(SourceRange * Range,Declarator::TheContext Context,AccessSpecifier AS,Decl ** OwnedType,ParsedAttributes * Attrs)44*67e74705SXin Li TypeResult Parser::ParseTypeName(SourceRange *Range,
45*67e74705SXin Li                                  Declarator::TheContext Context,
46*67e74705SXin Li                                  AccessSpecifier AS,
47*67e74705SXin Li                                  Decl **OwnedType,
48*67e74705SXin Li                                  ParsedAttributes *Attrs) {
49*67e74705SXin Li   DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
50*67e74705SXin Li   if (DSC == DSC_normal)
51*67e74705SXin Li     DSC = DSC_type_specifier;
52*67e74705SXin Li 
53*67e74705SXin Li   // Parse the common declaration-specifiers piece.
54*67e74705SXin Li   DeclSpec DS(AttrFactory);
55*67e74705SXin Li   if (Attrs)
56*67e74705SXin Li     DS.addAttributes(Attrs->getList());
57*67e74705SXin Li   ParseSpecifierQualifierList(DS, AS, DSC);
58*67e74705SXin Li   if (OwnedType)
59*67e74705SXin Li     *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : nullptr;
60*67e74705SXin Li 
61*67e74705SXin Li   // Parse the abstract-declarator, if present.
62*67e74705SXin Li   Declarator DeclaratorInfo(DS, Context);
63*67e74705SXin Li   ParseDeclarator(DeclaratorInfo);
64*67e74705SXin Li   if (Range)
65*67e74705SXin Li     *Range = DeclaratorInfo.getSourceRange();
66*67e74705SXin Li 
67*67e74705SXin Li   if (DeclaratorInfo.isInvalidType())
68*67e74705SXin Li     return true;
69*67e74705SXin Li 
70*67e74705SXin Li   return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
71*67e74705SXin Li }
72*67e74705SXin Li 
73*67e74705SXin Li /// isAttributeLateParsed - Return true if the attribute has arguments that
74*67e74705SXin Li /// require late parsing.
isAttributeLateParsed(const IdentifierInfo & II)75*67e74705SXin Li static bool isAttributeLateParsed(const IdentifierInfo &II) {
76*67e74705SXin Li #define CLANG_ATTR_LATE_PARSED_LIST
77*67e74705SXin Li     return llvm::StringSwitch<bool>(II.getName())
78*67e74705SXin Li #include "clang/Parse/AttrParserStringSwitches.inc"
79*67e74705SXin Li         .Default(false);
80*67e74705SXin Li #undef CLANG_ATTR_LATE_PARSED_LIST
81*67e74705SXin Li }
82*67e74705SXin Li 
83*67e74705SXin Li /// ParseGNUAttributes - Parse a non-empty attributes list.
84*67e74705SXin Li ///
85*67e74705SXin Li /// [GNU] attributes:
86*67e74705SXin Li ///         attribute
87*67e74705SXin Li ///         attributes attribute
88*67e74705SXin Li ///
89*67e74705SXin Li /// [GNU]  attribute:
90*67e74705SXin Li ///          '__attribute__' '(' '(' attribute-list ')' ')'
91*67e74705SXin Li ///
92*67e74705SXin Li /// [GNU]  attribute-list:
93*67e74705SXin Li ///          attrib
94*67e74705SXin Li ///          attribute_list ',' attrib
95*67e74705SXin Li ///
96*67e74705SXin Li /// [GNU]  attrib:
97*67e74705SXin Li ///          empty
98*67e74705SXin Li ///          attrib-name
99*67e74705SXin Li ///          attrib-name '(' identifier ')'
100*67e74705SXin Li ///          attrib-name '(' identifier ',' nonempty-expr-list ')'
101*67e74705SXin Li ///          attrib-name '(' argument-expression-list [C99 6.5.2] ')'
102*67e74705SXin Li ///
103*67e74705SXin Li /// [GNU]  attrib-name:
104*67e74705SXin Li ///          identifier
105*67e74705SXin Li ///          typespec
106*67e74705SXin Li ///          typequal
107*67e74705SXin Li ///          storageclass
108*67e74705SXin Li ///
109*67e74705SXin Li /// Whether an attribute takes an 'identifier' is determined by the
110*67e74705SXin Li /// attrib-name. GCC's behavior here is not worth imitating:
111*67e74705SXin Li ///
112*67e74705SXin Li ///  * In C mode, if the attribute argument list starts with an identifier
113*67e74705SXin Li ///    followed by a ',' or an ')', and the identifier doesn't resolve to
114*67e74705SXin Li ///    a type, it is parsed as an identifier. If the attribute actually
115*67e74705SXin Li ///    wanted an expression, it's out of luck (but it turns out that no
116*67e74705SXin Li ///    attributes work that way, because C constant expressions are very
117*67e74705SXin Li ///    limited).
118*67e74705SXin Li ///  * In C++ mode, if the attribute argument list starts with an identifier,
119*67e74705SXin Li ///    and the attribute *wants* an identifier, it is parsed as an identifier.
120*67e74705SXin Li ///    At block scope, any additional tokens between the identifier and the
121*67e74705SXin Li ///    ',' or ')' are ignored, otherwise they produce a parse error.
122*67e74705SXin Li ///
123*67e74705SXin Li /// We follow the C++ model, but don't allow junk after the identifier.
ParseGNUAttributes(ParsedAttributes & attrs,SourceLocation * endLoc,LateParsedAttrList * LateAttrs,Declarator * D)124*67e74705SXin Li void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
125*67e74705SXin Li                                 SourceLocation *endLoc,
126*67e74705SXin Li                                 LateParsedAttrList *LateAttrs,
127*67e74705SXin Li                                 Declarator *D) {
128*67e74705SXin Li   assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
129*67e74705SXin Li 
130*67e74705SXin Li   while (Tok.is(tok::kw___attribute)) {
131*67e74705SXin Li     ConsumeToken();
132*67e74705SXin Li     if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
133*67e74705SXin Li                          "attribute")) {
134*67e74705SXin Li       SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
135*67e74705SXin Li       return;
136*67e74705SXin Li     }
137*67e74705SXin Li     if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
138*67e74705SXin Li       SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
139*67e74705SXin Li       return;
140*67e74705SXin Li     }
141*67e74705SXin Li     // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
142*67e74705SXin Li     while (true) {
143*67e74705SXin Li       // Allow empty/non-empty attributes. ((__vector_size__(16),,,,))
144*67e74705SXin Li       if (TryConsumeToken(tok::comma))
145*67e74705SXin Li         continue;
146*67e74705SXin Li 
147*67e74705SXin Li       // Expect an identifier or declaration specifier (const, int, etc.)
148*67e74705SXin Li       if (Tok.isAnnotation())
149*67e74705SXin Li         break;
150*67e74705SXin Li       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
151*67e74705SXin Li       if (!AttrName)
152*67e74705SXin Li         break;
153*67e74705SXin Li 
154*67e74705SXin Li       SourceLocation AttrNameLoc = ConsumeToken();
155*67e74705SXin Li 
156*67e74705SXin Li       if (Tok.isNot(tok::l_paren)) {
157*67e74705SXin Li         attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
158*67e74705SXin Li                      AttributeList::AS_GNU);
159*67e74705SXin Li         continue;
160*67e74705SXin Li       }
161*67e74705SXin Li 
162*67e74705SXin Li       // Handle "parameterized" attributes
163*67e74705SXin Li       if (!LateAttrs || !isAttributeLateParsed(*AttrName)) {
164*67e74705SXin Li         ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, nullptr,
165*67e74705SXin Li                               SourceLocation(), AttributeList::AS_GNU, D);
166*67e74705SXin Li         continue;
167*67e74705SXin Li       }
168*67e74705SXin Li 
169*67e74705SXin Li       // Handle attributes with arguments that require late parsing.
170*67e74705SXin Li       LateParsedAttribute *LA =
171*67e74705SXin Li           new LateParsedAttribute(this, *AttrName, AttrNameLoc);
172*67e74705SXin Li       LateAttrs->push_back(LA);
173*67e74705SXin Li 
174*67e74705SXin Li       // Attributes in a class are parsed at the end of the class, along
175*67e74705SXin Li       // with other late-parsed declarations.
176*67e74705SXin Li       if (!ClassStack.empty() && !LateAttrs->parseSoon())
177*67e74705SXin Li         getCurrentClass().LateParsedDeclarations.push_back(LA);
178*67e74705SXin Li 
179*67e74705SXin Li       // consume everything up to and including the matching right parens
180*67e74705SXin Li       ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
181*67e74705SXin Li 
182*67e74705SXin Li       Token Eof;
183*67e74705SXin Li       Eof.startToken();
184*67e74705SXin Li       Eof.setLocation(Tok.getLocation());
185*67e74705SXin Li       LA->Toks.push_back(Eof);
186*67e74705SXin Li     }
187*67e74705SXin Li 
188*67e74705SXin Li     if (ExpectAndConsume(tok::r_paren))
189*67e74705SXin Li       SkipUntil(tok::r_paren, StopAtSemi);
190*67e74705SXin Li     SourceLocation Loc = Tok.getLocation();
191*67e74705SXin Li     if (ExpectAndConsume(tok::r_paren))
192*67e74705SXin Li       SkipUntil(tok::r_paren, StopAtSemi);
193*67e74705SXin Li     if (endLoc)
194*67e74705SXin Li       *endLoc = Loc;
195*67e74705SXin Li   }
196*67e74705SXin Li }
197*67e74705SXin Li 
198*67e74705SXin Li /// \brief Normalizes an attribute name by dropping prefixed and suffixed __.
normalizeAttrName(StringRef Name)199*67e74705SXin Li static StringRef normalizeAttrName(StringRef Name) {
200*67e74705SXin Li   if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
201*67e74705SXin Li     Name = Name.drop_front(2).drop_back(2);
202*67e74705SXin Li   return Name;
203*67e74705SXin Li }
204*67e74705SXin Li 
205*67e74705SXin Li /// \brief Determine whether the given attribute has an identifier argument.
attributeHasIdentifierArg(const IdentifierInfo & II)206*67e74705SXin Li static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
207*67e74705SXin Li #define CLANG_ATTR_IDENTIFIER_ARG_LIST
208*67e74705SXin Li   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
209*67e74705SXin Li #include "clang/Parse/AttrParserStringSwitches.inc"
210*67e74705SXin Li            .Default(false);
211*67e74705SXin Li #undef CLANG_ATTR_IDENTIFIER_ARG_LIST
212*67e74705SXin Li }
213*67e74705SXin Li 
214*67e74705SXin Li /// \brief Determine whether the given attribute parses a type argument.
attributeIsTypeArgAttr(const IdentifierInfo & II)215*67e74705SXin Li static bool attributeIsTypeArgAttr(const IdentifierInfo &II) {
216*67e74705SXin Li #define CLANG_ATTR_TYPE_ARG_LIST
217*67e74705SXin Li   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
218*67e74705SXin Li #include "clang/Parse/AttrParserStringSwitches.inc"
219*67e74705SXin Li            .Default(false);
220*67e74705SXin Li #undef CLANG_ATTR_TYPE_ARG_LIST
221*67e74705SXin Li }
222*67e74705SXin Li 
223*67e74705SXin Li /// \brief Determine whether the given attribute requires parsing its arguments
224*67e74705SXin Li /// in an unevaluated context or not.
attributeParsedArgsUnevaluated(const IdentifierInfo & II)225*67e74705SXin Li static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) {
226*67e74705SXin Li #define CLANG_ATTR_ARG_CONTEXT_LIST
227*67e74705SXin Li   return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
228*67e74705SXin Li #include "clang/Parse/AttrParserStringSwitches.inc"
229*67e74705SXin Li            .Default(false);
230*67e74705SXin Li #undef CLANG_ATTR_ARG_CONTEXT_LIST
231*67e74705SXin Li }
232*67e74705SXin Li 
ParseIdentifierLoc()233*67e74705SXin Li IdentifierLoc *Parser::ParseIdentifierLoc() {
234*67e74705SXin Li   assert(Tok.is(tok::identifier) && "expected an identifier");
235*67e74705SXin Li   IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
236*67e74705SXin Li                                             Tok.getLocation(),
237*67e74705SXin Li                                             Tok.getIdentifierInfo());
238*67e74705SXin Li   ConsumeToken();
239*67e74705SXin Li   return IL;
240*67e74705SXin Li }
241*67e74705SXin Li 
ParseAttributeWithTypeArg(IdentifierInfo & AttrName,SourceLocation AttrNameLoc,ParsedAttributes & Attrs,SourceLocation * EndLoc,IdentifierInfo * ScopeName,SourceLocation ScopeLoc,AttributeList::Syntax Syntax)242*67e74705SXin Li void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
243*67e74705SXin Li                                        SourceLocation AttrNameLoc,
244*67e74705SXin Li                                        ParsedAttributes &Attrs,
245*67e74705SXin Li                                        SourceLocation *EndLoc,
246*67e74705SXin Li                                        IdentifierInfo *ScopeName,
247*67e74705SXin Li                                        SourceLocation ScopeLoc,
248*67e74705SXin Li                                        AttributeList::Syntax Syntax) {
249*67e74705SXin Li   BalancedDelimiterTracker Parens(*this, tok::l_paren);
250*67e74705SXin Li   Parens.consumeOpen();
251*67e74705SXin Li 
252*67e74705SXin Li   TypeResult T;
253*67e74705SXin Li   if (Tok.isNot(tok::r_paren))
254*67e74705SXin Li     T = ParseTypeName();
255*67e74705SXin Li 
256*67e74705SXin Li   if (Parens.consumeClose())
257*67e74705SXin Li     return;
258*67e74705SXin Li 
259*67e74705SXin Li   if (T.isInvalid())
260*67e74705SXin Li     return;
261*67e74705SXin Li 
262*67e74705SXin Li   if (T.isUsable())
263*67e74705SXin Li     Attrs.addNewTypeAttr(&AttrName,
264*67e74705SXin Li                          SourceRange(AttrNameLoc, Parens.getCloseLocation()),
265*67e74705SXin Li                          ScopeName, ScopeLoc, T.get(), Syntax);
266*67e74705SXin Li   else
267*67e74705SXin Li     Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()),
268*67e74705SXin Li                  ScopeName, ScopeLoc, nullptr, 0, Syntax);
269*67e74705SXin Li }
270*67e74705SXin Li 
ParseAttributeArgsCommon(IdentifierInfo * AttrName,SourceLocation AttrNameLoc,ParsedAttributes & Attrs,SourceLocation * EndLoc,IdentifierInfo * ScopeName,SourceLocation ScopeLoc,AttributeList::Syntax Syntax)271*67e74705SXin Li unsigned Parser::ParseAttributeArgsCommon(
272*67e74705SXin Li     IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
273*67e74705SXin Li     ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
274*67e74705SXin Li     SourceLocation ScopeLoc, AttributeList::Syntax Syntax) {
275*67e74705SXin Li   // Ignore the left paren location for now.
276*67e74705SXin Li   ConsumeParen();
277*67e74705SXin Li 
278*67e74705SXin Li   ArgsVector ArgExprs;
279*67e74705SXin Li   if (Tok.is(tok::identifier)) {
280*67e74705SXin Li     // If this attribute wants an 'identifier' argument, make it so.
281*67e74705SXin Li     bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName);
282*67e74705SXin Li     AttributeList::Kind AttrKind =
283*67e74705SXin Li         AttributeList::getKind(AttrName, ScopeName, Syntax);
284*67e74705SXin Li 
285*67e74705SXin Li     // If we don't know how to parse this attribute, but this is the only
286*67e74705SXin Li     // token in this argument, assume it's meant to be an identifier.
287*67e74705SXin Li     if (AttrKind == AttributeList::UnknownAttribute ||
288*67e74705SXin Li         AttrKind == AttributeList::IgnoredAttribute) {
289*67e74705SXin Li       const Token &Next = NextToken();
290*67e74705SXin Li       IsIdentifierArg = Next.isOneOf(tok::r_paren, tok::comma);
291*67e74705SXin Li     }
292*67e74705SXin Li 
293*67e74705SXin Li     if (IsIdentifierArg)
294*67e74705SXin Li       ArgExprs.push_back(ParseIdentifierLoc());
295*67e74705SXin Li   }
296*67e74705SXin Li 
297*67e74705SXin Li   if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
298*67e74705SXin Li     // Eat the comma.
299*67e74705SXin Li     if (!ArgExprs.empty())
300*67e74705SXin Li       ConsumeToken();
301*67e74705SXin Li 
302*67e74705SXin Li     // Parse the non-empty comma-separated list of expressions.
303*67e74705SXin Li     do {
304*67e74705SXin Li       std::unique_ptr<EnterExpressionEvaluationContext> Unevaluated;
305*67e74705SXin Li       if (attributeParsedArgsUnevaluated(*AttrName))
306*67e74705SXin Li         Unevaluated.reset(
307*67e74705SXin Li             new EnterExpressionEvaluationContext(Actions, Sema::Unevaluated));
308*67e74705SXin Li 
309*67e74705SXin Li       ExprResult ArgExpr(
310*67e74705SXin Li           Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression()));
311*67e74705SXin Li       if (ArgExpr.isInvalid()) {
312*67e74705SXin Li         SkipUntil(tok::r_paren, StopAtSemi);
313*67e74705SXin Li         return 0;
314*67e74705SXin Li       }
315*67e74705SXin Li       ArgExprs.push_back(ArgExpr.get());
316*67e74705SXin Li       // Eat the comma, move to the next argument
317*67e74705SXin Li     } while (TryConsumeToken(tok::comma));
318*67e74705SXin Li   }
319*67e74705SXin Li 
320*67e74705SXin Li   SourceLocation RParen = Tok.getLocation();
321*67e74705SXin Li   if (!ExpectAndConsume(tok::r_paren)) {
322*67e74705SXin Li     SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
323*67e74705SXin Li     Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
324*67e74705SXin Li                  ArgExprs.data(), ArgExprs.size(), Syntax);
325*67e74705SXin Li   }
326*67e74705SXin Li 
327*67e74705SXin Li   if (EndLoc)
328*67e74705SXin Li     *EndLoc = RParen;
329*67e74705SXin Li 
330*67e74705SXin Li   return static_cast<unsigned>(ArgExprs.size());
331*67e74705SXin Li }
332*67e74705SXin Li 
333*67e74705SXin Li /// Parse the arguments to a parameterized GNU attribute or
334*67e74705SXin Li /// a C++11 attribute in "gnu" namespace.
ParseGNUAttributeArgs(IdentifierInfo * AttrName,SourceLocation AttrNameLoc,ParsedAttributes & Attrs,SourceLocation * EndLoc,IdentifierInfo * ScopeName,SourceLocation ScopeLoc,AttributeList::Syntax Syntax,Declarator * D)335*67e74705SXin Li void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
336*67e74705SXin Li                                    SourceLocation AttrNameLoc,
337*67e74705SXin Li                                    ParsedAttributes &Attrs,
338*67e74705SXin Li                                    SourceLocation *EndLoc,
339*67e74705SXin Li                                    IdentifierInfo *ScopeName,
340*67e74705SXin Li                                    SourceLocation ScopeLoc,
341*67e74705SXin Li                                    AttributeList::Syntax Syntax,
342*67e74705SXin Li                                    Declarator *D) {
343*67e74705SXin Li 
344*67e74705SXin Li   assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
345*67e74705SXin Li 
346*67e74705SXin Li   AttributeList::Kind AttrKind =
347*67e74705SXin Li       AttributeList::getKind(AttrName, ScopeName, Syntax);
348*67e74705SXin Li 
349*67e74705SXin Li   if (AttrKind == AttributeList::AT_Availability) {
350*67e74705SXin Li     ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
351*67e74705SXin Li                                ScopeLoc, Syntax);
352*67e74705SXin Li     return;
353*67e74705SXin Li   } else if (AttrKind == AttributeList::AT_ObjCBridgeRelated) {
354*67e74705SXin Li     ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
355*67e74705SXin Li                                     ScopeName, ScopeLoc, Syntax);
356*67e74705SXin Li     return;
357*67e74705SXin Li   } else if (AttrKind == AttributeList::AT_TypeTagForDatatype) {
358*67e74705SXin Li     ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
359*67e74705SXin Li                                      ScopeName, ScopeLoc, Syntax);
360*67e74705SXin Li     return;
361*67e74705SXin Li   } else if (attributeIsTypeArgAttr(*AttrName)) {
362*67e74705SXin Li     ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
363*67e74705SXin Li                               ScopeLoc, Syntax);
364*67e74705SXin Li     return;
365*67e74705SXin Li   }
366*67e74705SXin Li 
367*67e74705SXin Li   // These may refer to the function arguments, but need to be parsed early to
368*67e74705SXin Li   // participate in determining whether it's a redeclaration.
369*67e74705SXin Li   std::unique_ptr<ParseScope> PrototypeScope;
370*67e74705SXin Li   if (normalizeAttrName(AttrName->getName()) == "enable_if" &&
371*67e74705SXin Li       D && D->isFunctionDeclarator()) {
372*67e74705SXin Li     DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo();
373*67e74705SXin Li     PrototypeScope.reset(new ParseScope(this, Scope::FunctionPrototypeScope |
374*67e74705SXin Li                                         Scope::FunctionDeclarationScope |
375*67e74705SXin Li                                         Scope::DeclScope));
376*67e74705SXin Li     for (unsigned i = 0; i != FTI.NumParams; ++i) {
377*67e74705SXin Li       ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param);
378*67e74705SXin Li       Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param);
379*67e74705SXin Li     }
380*67e74705SXin Li   }
381*67e74705SXin Li 
382*67e74705SXin Li   ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
383*67e74705SXin Li                            ScopeLoc, Syntax);
384*67e74705SXin Li }
385*67e74705SXin Li 
ParseMicrosoftDeclSpecArgs(IdentifierInfo * AttrName,SourceLocation AttrNameLoc,ParsedAttributes & Attrs)386*67e74705SXin Li bool Parser::ParseMicrosoftDeclSpecArgs(IdentifierInfo *AttrName,
387*67e74705SXin Li                                         SourceLocation AttrNameLoc,
388*67e74705SXin Li                                         ParsedAttributes &Attrs) {
389*67e74705SXin Li   // If the attribute isn't known, we will not attempt to parse any
390*67e74705SXin Li   // arguments.
391*67e74705SXin Li   if (!hasAttribute(AttrSyntax::Declspec, nullptr, AttrName,
392*67e74705SXin Li                     getTargetInfo(), getLangOpts())) {
393*67e74705SXin Li     // Eat the left paren, then skip to the ending right paren.
394*67e74705SXin Li     ConsumeParen();
395*67e74705SXin Li     SkipUntil(tok::r_paren);
396*67e74705SXin Li     return false;
397*67e74705SXin Li   }
398*67e74705SXin Li 
399*67e74705SXin Li   SourceLocation OpenParenLoc = Tok.getLocation();
400*67e74705SXin Li 
401*67e74705SXin Li   if (AttrName->getName() == "property") {
402*67e74705SXin Li     // The property declspec is more complex in that it can take one or two
403*67e74705SXin Li     // assignment expressions as a parameter, but the lhs of the assignment
404*67e74705SXin Li     // must be named get or put.
405*67e74705SXin Li 
406*67e74705SXin Li     BalancedDelimiterTracker T(*this, tok::l_paren);
407*67e74705SXin Li     T.expectAndConsume(diag::err_expected_lparen_after,
408*67e74705SXin Li                        AttrName->getNameStart(), tok::r_paren);
409*67e74705SXin Li 
410*67e74705SXin Li     enum AccessorKind {
411*67e74705SXin Li       AK_Invalid = -1,
412*67e74705SXin Li       AK_Put = 0,
413*67e74705SXin Li       AK_Get = 1 // indices into AccessorNames
414*67e74705SXin Li     };
415*67e74705SXin Li     IdentifierInfo *AccessorNames[] = {nullptr, nullptr};
416*67e74705SXin Li     bool HasInvalidAccessor = false;
417*67e74705SXin Li 
418*67e74705SXin Li     // Parse the accessor specifications.
419*67e74705SXin Li     while (true) {
420*67e74705SXin Li       // Stop if this doesn't look like an accessor spec.
421*67e74705SXin Li       if (!Tok.is(tok::identifier)) {
422*67e74705SXin Li         // If the user wrote a completely empty list, use a special diagnostic.
423*67e74705SXin Li         if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
424*67e74705SXin Li             AccessorNames[AK_Put] == nullptr &&
425*67e74705SXin Li             AccessorNames[AK_Get] == nullptr) {
426*67e74705SXin Li           Diag(AttrNameLoc, diag::err_ms_property_no_getter_or_putter);
427*67e74705SXin Li           break;
428*67e74705SXin Li         }
429*67e74705SXin Li 
430*67e74705SXin Li         Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
431*67e74705SXin Li         break;
432*67e74705SXin Li       }
433*67e74705SXin Li 
434*67e74705SXin Li       AccessorKind Kind;
435*67e74705SXin Li       SourceLocation KindLoc = Tok.getLocation();
436*67e74705SXin Li       StringRef KindStr = Tok.getIdentifierInfo()->getName();
437*67e74705SXin Li       if (KindStr == "get") {
438*67e74705SXin Li         Kind = AK_Get;
439*67e74705SXin Li       } else if (KindStr == "put") {
440*67e74705SXin Li         Kind = AK_Put;
441*67e74705SXin Li 
442*67e74705SXin Li         // Recover from the common mistake of using 'set' instead of 'put'.
443*67e74705SXin Li       } else if (KindStr == "set") {
444*67e74705SXin Li         Diag(KindLoc, diag::err_ms_property_has_set_accessor)
445*67e74705SXin Li             << FixItHint::CreateReplacement(KindLoc, "put");
446*67e74705SXin Li         Kind = AK_Put;
447*67e74705SXin Li 
448*67e74705SXin Li         // Handle the mistake of forgetting the accessor kind by skipping
449*67e74705SXin Li         // this accessor.
450*67e74705SXin Li       } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
451*67e74705SXin Li         Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
452*67e74705SXin Li         ConsumeToken();
453*67e74705SXin Li         HasInvalidAccessor = true;
454*67e74705SXin Li         goto next_property_accessor;
455*67e74705SXin Li 
456*67e74705SXin Li         // Otherwise, complain about the unknown accessor kind.
457*67e74705SXin Li       } else {
458*67e74705SXin Li         Diag(KindLoc, diag::err_ms_property_unknown_accessor);
459*67e74705SXin Li         HasInvalidAccessor = true;
460*67e74705SXin Li         Kind = AK_Invalid;
461*67e74705SXin Li 
462*67e74705SXin Li         // Try to keep parsing unless it doesn't look like an accessor spec.
463*67e74705SXin Li         if (!NextToken().is(tok::equal))
464*67e74705SXin Li           break;
465*67e74705SXin Li       }
466*67e74705SXin Li 
467*67e74705SXin Li       // Consume the identifier.
468*67e74705SXin Li       ConsumeToken();
469*67e74705SXin Li 
470*67e74705SXin Li       // Consume the '='.
471*67e74705SXin Li       if (!TryConsumeToken(tok::equal)) {
472*67e74705SXin Li         Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
473*67e74705SXin Li             << KindStr;
474*67e74705SXin Li         break;
475*67e74705SXin Li       }
476*67e74705SXin Li 
477*67e74705SXin Li       // Expect the method name.
478*67e74705SXin Li       if (!Tok.is(tok::identifier)) {
479*67e74705SXin Li         Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
480*67e74705SXin Li         break;
481*67e74705SXin Li       }
482*67e74705SXin Li 
483*67e74705SXin Li       if (Kind == AK_Invalid) {
484*67e74705SXin Li         // Just drop invalid accessors.
485*67e74705SXin Li       } else if (AccessorNames[Kind] != nullptr) {
486*67e74705SXin Li         // Complain about the repeated accessor, ignore it, and keep parsing.
487*67e74705SXin Li         Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
488*67e74705SXin Li       } else {
489*67e74705SXin Li         AccessorNames[Kind] = Tok.getIdentifierInfo();
490*67e74705SXin Li       }
491*67e74705SXin Li       ConsumeToken();
492*67e74705SXin Li 
493*67e74705SXin Li     next_property_accessor:
494*67e74705SXin Li       // Keep processing accessors until we run out.
495*67e74705SXin Li       if (TryConsumeToken(tok::comma))
496*67e74705SXin Li         continue;
497*67e74705SXin Li 
498*67e74705SXin Li       // If we run into the ')', stop without consuming it.
499*67e74705SXin Li       if (Tok.is(tok::r_paren))
500*67e74705SXin Li         break;
501*67e74705SXin Li 
502*67e74705SXin Li       Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
503*67e74705SXin Li       break;
504*67e74705SXin Li     }
505*67e74705SXin Li 
506*67e74705SXin Li     // Only add the property attribute if it was well-formed.
507*67e74705SXin Li     if (!HasInvalidAccessor)
508*67e74705SXin Li       Attrs.addNewPropertyAttr(AttrName, AttrNameLoc, nullptr, SourceLocation(),
509*67e74705SXin Li                                AccessorNames[AK_Get], AccessorNames[AK_Put],
510*67e74705SXin Li                                AttributeList::AS_Declspec);
511*67e74705SXin Li     T.skipToEnd();
512*67e74705SXin Li     return !HasInvalidAccessor;
513*67e74705SXin Li   }
514*67e74705SXin Li 
515*67e74705SXin Li   unsigned NumArgs =
516*67e74705SXin Li       ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, nullptr, nullptr,
517*67e74705SXin Li                                SourceLocation(), AttributeList::AS_Declspec);
518*67e74705SXin Li 
519*67e74705SXin Li   // If this attribute's args were parsed, and it was expected to have
520*67e74705SXin Li   // arguments but none were provided, emit a diagnostic.
521*67e74705SXin Li   const AttributeList *Attr = Attrs.getList();
522*67e74705SXin Li   if (Attr && Attr->getMaxArgs() && !NumArgs) {
523*67e74705SXin Li     Diag(OpenParenLoc, diag::err_attribute_requires_arguments) << AttrName;
524*67e74705SXin Li     return false;
525*67e74705SXin Li   }
526*67e74705SXin Li   return true;
527*67e74705SXin Li }
528*67e74705SXin Li 
529*67e74705SXin Li /// [MS] decl-specifier:
530*67e74705SXin Li ///             __declspec ( extended-decl-modifier-seq )
531*67e74705SXin Li ///
532*67e74705SXin Li /// [MS] extended-decl-modifier-seq:
533*67e74705SXin Li ///             extended-decl-modifier[opt]
534*67e74705SXin Li ///             extended-decl-modifier extended-decl-modifier-seq
ParseMicrosoftDeclSpecs(ParsedAttributes & Attrs,SourceLocation * End)535*67e74705SXin Li void Parser::ParseMicrosoftDeclSpecs(ParsedAttributes &Attrs,
536*67e74705SXin Li                                      SourceLocation *End) {
537*67e74705SXin Li   assert(getLangOpts().DeclSpecKeyword && "__declspec keyword is not enabled");
538*67e74705SXin Li   assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
539*67e74705SXin Li 
540*67e74705SXin Li   while (Tok.is(tok::kw___declspec)) {
541*67e74705SXin Li     ConsumeToken();
542*67e74705SXin Li     BalancedDelimiterTracker T(*this, tok::l_paren);
543*67e74705SXin Li     if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
544*67e74705SXin Li                            tok::r_paren))
545*67e74705SXin Li       return;
546*67e74705SXin Li 
547*67e74705SXin Li     // An empty declspec is perfectly legal and should not warn.  Additionally,
548*67e74705SXin Li     // you can specify multiple attributes per declspec.
549*67e74705SXin Li     while (Tok.isNot(tok::r_paren)) {
550*67e74705SXin Li       // Attribute not present.
551*67e74705SXin Li       if (TryConsumeToken(tok::comma))
552*67e74705SXin Li         continue;
553*67e74705SXin Li 
554*67e74705SXin Li       // We expect either a well-known identifier or a generic string.  Anything
555*67e74705SXin Li       // else is a malformed declspec.
556*67e74705SXin Li       bool IsString = Tok.getKind() == tok::string_literal;
557*67e74705SXin Li       if (!IsString && Tok.getKind() != tok::identifier &&
558*67e74705SXin Li           Tok.getKind() != tok::kw_restrict) {
559*67e74705SXin Li         Diag(Tok, diag::err_ms_declspec_type);
560*67e74705SXin Li         T.skipToEnd();
561*67e74705SXin Li         return;
562*67e74705SXin Li       }
563*67e74705SXin Li 
564*67e74705SXin Li       IdentifierInfo *AttrName;
565*67e74705SXin Li       SourceLocation AttrNameLoc;
566*67e74705SXin Li       if (IsString) {
567*67e74705SXin Li         SmallString<8> StrBuffer;
568*67e74705SXin Li         bool Invalid = false;
569*67e74705SXin Li         StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
570*67e74705SXin Li         if (Invalid) {
571*67e74705SXin Li           T.skipToEnd();
572*67e74705SXin Li           return;
573*67e74705SXin Li         }
574*67e74705SXin Li         AttrName = PP.getIdentifierInfo(Str);
575*67e74705SXin Li         AttrNameLoc = ConsumeStringToken();
576*67e74705SXin Li       } else {
577*67e74705SXin Li         AttrName = Tok.getIdentifierInfo();
578*67e74705SXin Li         AttrNameLoc = ConsumeToken();
579*67e74705SXin Li       }
580*67e74705SXin Li 
581*67e74705SXin Li       bool AttrHandled = false;
582*67e74705SXin Li 
583*67e74705SXin Li       // Parse attribute arguments.
584*67e74705SXin Li       if (Tok.is(tok::l_paren))
585*67e74705SXin Li         AttrHandled = ParseMicrosoftDeclSpecArgs(AttrName, AttrNameLoc, Attrs);
586*67e74705SXin Li       else if (AttrName->getName() == "property")
587*67e74705SXin Li         // The property attribute must have an argument list.
588*67e74705SXin Li         Diag(Tok.getLocation(), diag::err_expected_lparen_after)
589*67e74705SXin Li             << AttrName->getName();
590*67e74705SXin Li 
591*67e74705SXin Li       if (!AttrHandled)
592*67e74705SXin Li         Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
593*67e74705SXin Li                      AttributeList::AS_Declspec);
594*67e74705SXin Li     }
595*67e74705SXin Li     T.consumeClose();
596*67e74705SXin Li     if (End)
597*67e74705SXin Li       *End = T.getCloseLocation();
598*67e74705SXin Li   }
599*67e74705SXin Li }
600*67e74705SXin Li 
ParseMicrosoftTypeAttributes(ParsedAttributes & attrs)601*67e74705SXin Li void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
602*67e74705SXin Li   // Treat these like attributes
603*67e74705SXin Li   while (true) {
604*67e74705SXin Li     switch (Tok.getKind()) {
605*67e74705SXin Li     case tok::kw___fastcall:
606*67e74705SXin Li     case tok::kw___stdcall:
607*67e74705SXin Li     case tok::kw___thiscall:
608*67e74705SXin Li     case tok::kw___cdecl:
609*67e74705SXin Li     case tok::kw___vectorcall:
610*67e74705SXin Li     case tok::kw___ptr64:
611*67e74705SXin Li     case tok::kw___w64:
612*67e74705SXin Li     case tok::kw___ptr32:
613*67e74705SXin Li     case tok::kw___sptr:
614*67e74705SXin Li     case tok::kw___uptr: {
615*67e74705SXin Li       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
616*67e74705SXin Li       SourceLocation AttrNameLoc = ConsumeToken();
617*67e74705SXin Li       attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
618*67e74705SXin Li                    AttributeList::AS_Keyword);
619*67e74705SXin Li       break;
620*67e74705SXin Li     }
621*67e74705SXin Li     default:
622*67e74705SXin Li       return;
623*67e74705SXin Li     }
624*67e74705SXin Li   }
625*67e74705SXin Li }
626*67e74705SXin Li 
DiagnoseAndSkipExtendedMicrosoftTypeAttributes()627*67e74705SXin Li void Parser::DiagnoseAndSkipExtendedMicrosoftTypeAttributes() {
628*67e74705SXin Li   SourceLocation StartLoc = Tok.getLocation();
629*67e74705SXin Li   SourceLocation EndLoc = SkipExtendedMicrosoftTypeAttributes();
630*67e74705SXin Li 
631*67e74705SXin Li   if (EndLoc.isValid()) {
632*67e74705SXin Li     SourceRange Range(StartLoc, EndLoc);
633*67e74705SXin Li     Diag(StartLoc, diag::warn_microsoft_qualifiers_ignored) << Range;
634*67e74705SXin Li   }
635*67e74705SXin Li }
636*67e74705SXin Li 
SkipExtendedMicrosoftTypeAttributes()637*67e74705SXin Li SourceLocation Parser::SkipExtendedMicrosoftTypeAttributes() {
638*67e74705SXin Li   SourceLocation EndLoc;
639*67e74705SXin Li 
640*67e74705SXin Li   while (true) {
641*67e74705SXin Li     switch (Tok.getKind()) {
642*67e74705SXin Li     case tok::kw_const:
643*67e74705SXin Li     case tok::kw_volatile:
644*67e74705SXin Li     case tok::kw___fastcall:
645*67e74705SXin Li     case tok::kw___stdcall:
646*67e74705SXin Li     case tok::kw___thiscall:
647*67e74705SXin Li     case tok::kw___cdecl:
648*67e74705SXin Li     case tok::kw___vectorcall:
649*67e74705SXin Li     case tok::kw___ptr32:
650*67e74705SXin Li     case tok::kw___ptr64:
651*67e74705SXin Li     case tok::kw___w64:
652*67e74705SXin Li     case tok::kw___unaligned:
653*67e74705SXin Li     case tok::kw___sptr:
654*67e74705SXin Li     case tok::kw___uptr:
655*67e74705SXin Li       EndLoc = ConsumeToken();
656*67e74705SXin Li       break;
657*67e74705SXin Li     default:
658*67e74705SXin Li       return EndLoc;
659*67e74705SXin Li     }
660*67e74705SXin Li   }
661*67e74705SXin Li }
662*67e74705SXin Li 
ParseBorlandTypeAttributes(ParsedAttributes & attrs)663*67e74705SXin Li void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
664*67e74705SXin Li   // Treat these like attributes
665*67e74705SXin Li   while (Tok.is(tok::kw___pascal)) {
666*67e74705SXin Li     IdentifierInfo *AttrName = Tok.getIdentifierInfo();
667*67e74705SXin Li     SourceLocation AttrNameLoc = ConsumeToken();
668*67e74705SXin Li     attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
669*67e74705SXin Li                  AttributeList::AS_Keyword);
670*67e74705SXin Li   }
671*67e74705SXin Li }
672*67e74705SXin Li 
ParseOpenCLKernelAttributes(ParsedAttributes & attrs)673*67e74705SXin Li void Parser::ParseOpenCLKernelAttributes(ParsedAttributes &attrs) {
674*67e74705SXin Li   // Treat these like attributes
675*67e74705SXin Li   while (Tok.is(tok::kw___kernel)) {
676*67e74705SXin Li     IdentifierInfo *AttrName = Tok.getIdentifierInfo();
677*67e74705SXin Li     SourceLocation AttrNameLoc = ConsumeToken();
678*67e74705SXin Li     attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
679*67e74705SXin Li                  AttributeList::AS_Keyword);
680*67e74705SXin Li   }
681*67e74705SXin Li }
682*67e74705SXin Li 
ParseOpenCLQualifiers(ParsedAttributes & Attrs)683*67e74705SXin Li void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) {
684*67e74705SXin Li   IdentifierInfo *AttrName = Tok.getIdentifierInfo();
685*67e74705SXin Li   SourceLocation AttrNameLoc = Tok.getLocation();
686*67e74705SXin Li   Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
687*67e74705SXin Li                AttributeList::AS_Keyword);
688*67e74705SXin Li }
689*67e74705SXin Li 
ParseNullabilityTypeSpecifiers(ParsedAttributes & attrs)690*67e74705SXin Li void Parser::ParseNullabilityTypeSpecifiers(ParsedAttributes &attrs) {
691*67e74705SXin Li   // Treat these like attributes, even though they're type specifiers.
692*67e74705SXin Li   while (true) {
693*67e74705SXin Li     switch (Tok.getKind()) {
694*67e74705SXin Li     case tok::kw__Nonnull:
695*67e74705SXin Li     case tok::kw__Nullable:
696*67e74705SXin Li     case tok::kw__Null_unspecified: {
697*67e74705SXin Li       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
698*67e74705SXin Li       SourceLocation AttrNameLoc = ConsumeToken();
699*67e74705SXin Li       if (!getLangOpts().ObjC1)
700*67e74705SXin Li         Diag(AttrNameLoc, diag::ext_nullability)
701*67e74705SXin Li           << AttrName;
702*67e74705SXin Li       attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
703*67e74705SXin Li                    AttributeList::AS_Keyword);
704*67e74705SXin Li       break;
705*67e74705SXin Li     }
706*67e74705SXin Li     default:
707*67e74705SXin Li       return;
708*67e74705SXin Li     }
709*67e74705SXin Li   }
710*67e74705SXin Li }
711*67e74705SXin Li 
VersionNumberSeparator(const char Separator)712*67e74705SXin Li static bool VersionNumberSeparator(const char Separator) {
713*67e74705SXin Li   return (Separator == '.' || Separator == '_');
714*67e74705SXin Li }
715*67e74705SXin Li 
716*67e74705SXin Li /// \brief Parse a version number.
717*67e74705SXin Li ///
718*67e74705SXin Li /// version:
719*67e74705SXin Li ///   simple-integer
720*67e74705SXin Li ///   simple-integer ',' simple-integer
721*67e74705SXin Li ///   simple-integer ',' simple-integer ',' simple-integer
ParseVersionTuple(SourceRange & Range)722*67e74705SXin Li VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
723*67e74705SXin Li   Range = Tok.getLocation();
724*67e74705SXin Li 
725*67e74705SXin Li   if (!Tok.is(tok::numeric_constant)) {
726*67e74705SXin Li     Diag(Tok, diag::err_expected_version);
727*67e74705SXin Li     SkipUntil(tok::comma, tok::r_paren,
728*67e74705SXin Li               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
729*67e74705SXin Li     return VersionTuple();
730*67e74705SXin Li   }
731*67e74705SXin Li 
732*67e74705SXin Li   // Parse the major (and possibly minor and subminor) versions, which
733*67e74705SXin Li   // are stored in the numeric constant. We utilize a quirk of the
734*67e74705SXin Li   // lexer, which is that it handles something like 1.2.3 as a single
735*67e74705SXin Li   // numeric constant, rather than two separate tokens.
736*67e74705SXin Li   SmallString<512> Buffer;
737*67e74705SXin Li   Buffer.resize(Tok.getLength()+1);
738*67e74705SXin Li   const char *ThisTokBegin = &Buffer[0];
739*67e74705SXin Li 
740*67e74705SXin Li   // Get the spelling of the token, which eliminates trigraphs, etc.
741*67e74705SXin Li   bool Invalid = false;
742*67e74705SXin Li   unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
743*67e74705SXin Li   if (Invalid)
744*67e74705SXin Li     return VersionTuple();
745*67e74705SXin Li 
746*67e74705SXin Li   // Parse the major version.
747*67e74705SXin Li   unsigned AfterMajor = 0;
748*67e74705SXin Li   unsigned Major = 0;
749*67e74705SXin Li   while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
750*67e74705SXin Li     Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
751*67e74705SXin Li     ++AfterMajor;
752*67e74705SXin Li   }
753*67e74705SXin Li 
754*67e74705SXin Li   if (AfterMajor == 0) {
755*67e74705SXin Li     Diag(Tok, diag::err_expected_version);
756*67e74705SXin Li     SkipUntil(tok::comma, tok::r_paren,
757*67e74705SXin Li               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
758*67e74705SXin Li     return VersionTuple();
759*67e74705SXin Li   }
760*67e74705SXin Li 
761*67e74705SXin Li   if (AfterMajor == ActualLength) {
762*67e74705SXin Li     ConsumeToken();
763*67e74705SXin Li 
764*67e74705SXin Li     // We only had a single version component.
765*67e74705SXin Li     if (Major == 0) {
766*67e74705SXin Li       Diag(Tok, diag::err_zero_version);
767*67e74705SXin Li       return VersionTuple();
768*67e74705SXin Li     }
769*67e74705SXin Li 
770*67e74705SXin Li     return VersionTuple(Major);
771*67e74705SXin Li   }
772*67e74705SXin Li 
773*67e74705SXin Li   const char AfterMajorSeparator = ThisTokBegin[AfterMajor];
774*67e74705SXin Li   if (!VersionNumberSeparator(AfterMajorSeparator)
775*67e74705SXin Li       || (AfterMajor + 1 == ActualLength)) {
776*67e74705SXin Li     Diag(Tok, diag::err_expected_version);
777*67e74705SXin Li     SkipUntil(tok::comma, tok::r_paren,
778*67e74705SXin Li               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
779*67e74705SXin Li     return VersionTuple();
780*67e74705SXin Li   }
781*67e74705SXin Li 
782*67e74705SXin Li   // Parse the minor version.
783*67e74705SXin Li   unsigned AfterMinor = AfterMajor + 1;
784*67e74705SXin Li   unsigned Minor = 0;
785*67e74705SXin Li   while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
786*67e74705SXin Li     Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
787*67e74705SXin Li     ++AfterMinor;
788*67e74705SXin Li   }
789*67e74705SXin Li 
790*67e74705SXin Li   if (AfterMinor == ActualLength) {
791*67e74705SXin Li     ConsumeToken();
792*67e74705SXin Li 
793*67e74705SXin Li     // We had major.minor.
794*67e74705SXin Li     if (Major == 0 && Minor == 0) {
795*67e74705SXin Li       Diag(Tok, diag::err_zero_version);
796*67e74705SXin Li       return VersionTuple();
797*67e74705SXin Li     }
798*67e74705SXin Li 
799*67e74705SXin Li     return VersionTuple(Major, Minor, (AfterMajorSeparator == '_'));
800*67e74705SXin Li   }
801*67e74705SXin Li 
802*67e74705SXin Li   const char AfterMinorSeparator = ThisTokBegin[AfterMinor];
803*67e74705SXin Li   // If what follows is not a '.' or '_', we have a problem.
804*67e74705SXin Li   if (!VersionNumberSeparator(AfterMinorSeparator)) {
805*67e74705SXin Li     Diag(Tok, diag::err_expected_version);
806*67e74705SXin Li     SkipUntil(tok::comma, tok::r_paren,
807*67e74705SXin Li               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
808*67e74705SXin Li     return VersionTuple();
809*67e74705SXin Li   }
810*67e74705SXin Li 
811*67e74705SXin Li   // Warn if separators, be it '.' or '_', do not match.
812*67e74705SXin Li   if (AfterMajorSeparator != AfterMinorSeparator)
813*67e74705SXin Li     Diag(Tok, diag::warn_expected_consistent_version_separator);
814*67e74705SXin Li 
815*67e74705SXin Li   // Parse the subminor version.
816*67e74705SXin Li   unsigned AfterSubminor = AfterMinor + 1;
817*67e74705SXin Li   unsigned Subminor = 0;
818*67e74705SXin Li   while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
819*67e74705SXin Li     Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
820*67e74705SXin Li     ++AfterSubminor;
821*67e74705SXin Li   }
822*67e74705SXin Li 
823*67e74705SXin Li   if (AfterSubminor != ActualLength) {
824*67e74705SXin Li     Diag(Tok, diag::err_expected_version);
825*67e74705SXin Li     SkipUntil(tok::comma, tok::r_paren,
826*67e74705SXin Li               StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
827*67e74705SXin Li     return VersionTuple();
828*67e74705SXin Li   }
829*67e74705SXin Li   ConsumeToken();
830*67e74705SXin Li   return VersionTuple(Major, Minor, Subminor, (AfterMajorSeparator == '_'));
831*67e74705SXin Li }
832*67e74705SXin Li 
833*67e74705SXin Li /// \brief Parse the contents of the "availability" attribute.
834*67e74705SXin Li ///
835*67e74705SXin Li /// availability-attribute:
836*67e74705SXin Li ///   'availability' '(' platform ',' opt-strict version-arg-list,
837*67e74705SXin Li ///                      opt-replacement, opt-message')'
838*67e74705SXin Li ///
839*67e74705SXin Li /// platform:
840*67e74705SXin Li ///   identifier
841*67e74705SXin Li ///
842*67e74705SXin Li /// opt-strict:
843*67e74705SXin Li ///   'strict' ','
844*67e74705SXin Li ///
845*67e74705SXin Li /// version-arg-list:
846*67e74705SXin Li ///   version-arg
847*67e74705SXin Li ///   version-arg ',' version-arg-list
848*67e74705SXin Li ///
849*67e74705SXin Li /// version-arg:
850*67e74705SXin Li ///   'introduced' '=' version
851*67e74705SXin Li ///   'deprecated' '=' version
852*67e74705SXin Li ///   'obsoleted' = version
853*67e74705SXin Li ///   'unavailable'
854*67e74705SXin Li /// opt-replacement:
855*67e74705SXin Li ///   'replacement' '=' <string>
856*67e74705SXin Li /// opt-message:
857*67e74705SXin Li ///   'message' '=' <string>
ParseAvailabilityAttribute(IdentifierInfo & Availability,SourceLocation AvailabilityLoc,ParsedAttributes & attrs,SourceLocation * endLoc,IdentifierInfo * ScopeName,SourceLocation ScopeLoc,AttributeList::Syntax Syntax)858*67e74705SXin Li void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
859*67e74705SXin Li                                         SourceLocation AvailabilityLoc,
860*67e74705SXin Li                                         ParsedAttributes &attrs,
861*67e74705SXin Li                                         SourceLocation *endLoc,
862*67e74705SXin Li                                         IdentifierInfo *ScopeName,
863*67e74705SXin Li                                         SourceLocation ScopeLoc,
864*67e74705SXin Li                                         AttributeList::Syntax Syntax) {
865*67e74705SXin Li   enum { Introduced, Deprecated, Obsoleted, Unknown };
866*67e74705SXin Li   AvailabilityChange Changes[Unknown];
867*67e74705SXin Li   ExprResult MessageExpr, ReplacementExpr;
868*67e74705SXin Li 
869*67e74705SXin Li   // Opening '('.
870*67e74705SXin Li   BalancedDelimiterTracker T(*this, tok::l_paren);
871*67e74705SXin Li   if (T.consumeOpen()) {
872*67e74705SXin Li     Diag(Tok, diag::err_expected) << tok::l_paren;
873*67e74705SXin Li     return;
874*67e74705SXin Li   }
875*67e74705SXin Li 
876*67e74705SXin Li   // Parse the platform name.
877*67e74705SXin Li   if (Tok.isNot(tok::identifier)) {
878*67e74705SXin Li     Diag(Tok, diag::err_availability_expected_platform);
879*67e74705SXin Li     SkipUntil(tok::r_paren, StopAtSemi);
880*67e74705SXin Li     return;
881*67e74705SXin Li   }
882*67e74705SXin Li   IdentifierLoc *Platform = ParseIdentifierLoc();
883*67e74705SXin Li   // Canonicalize platform name from "macosx" to "macos".
884*67e74705SXin Li   if (Platform->Ident && Platform->Ident->getName() == "macosx")
885*67e74705SXin Li     Platform->Ident = PP.getIdentifierInfo("macos");
886*67e74705SXin Li   // Canonicalize platform name from "macosx_app_extension" to
887*67e74705SXin Li   // "macos_app_extension".
888*67e74705SXin Li   if (Platform->Ident && Platform->Ident->getName() == "macosx_app_extension")
889*67e74705SXin Li     Platform->Ident = PP.getIdentifierInfo("macos_app_extension");
890*67e74705SXin Li 
891*67e74705SXin Li   // Parse the ',' following the platform name.
892*67e74705SXin Li   if (ExpectAndConsume(tok::comma)) {
893*67e74705SXin Li     SkipUntil(tok::r_paren, StopAtSemi);
894*67e74705SXin Li     return;
895*67e74705SXin Li   }
896*67e74705SXin Li 
897*67e74705SXin Li   // If we haven't grabbed the pointers for the identifiers
898*67e74705SXin Li   // "introduced", "deprecated", and "obsoleted", do so now.
899*67e74705SXin Li   if (!Ident_introduced) {
900*67e74705SXin Li     Ident_introduced = PP.getIdentifierInfo("introduced");
901*67e74705SXin Li     Ident_deprecated = PP.getIdentifierInfo("deprecated");
902*67e74705SXin Li     Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
903*67e74705SXin Li     Ident_unavailable = PP.getIdentifierInfo("unavailable");
904*67e74705SXin Li     Ident_message = PP.getIdentifierInfo("message");
905*67e74705SXin Li     Ident_strict = PP.getIdentifierInfo("strict");
906*67e74705SXin Li     Ident_replacement = PP.getIdentifierInfo("replacement");
907*67e74705SXin Li   }
908*67e74705SXin Li 
909*67e74705SXin Li   // Parse the optional "strict", the optional "replacement" and the set of
910*67e74705SXin Li   // introductions/deprecations/removals.
911*67e74705SXin Li   SourceLocation UnavailableLoc, StrictLoc;
912*67e74705SXin Li   do {
913*67e74705SXin Li     if (Tok.isNot(tok::identifier)) {
914*67e74705SXin Li       Diag(Tok, diag::err_availability_expected_change);
915*67e74705SXin Li       SkipUntil(tok::r_paren, StopAtSemi);
916*67e74705SXin Li       return;
917*67e74705SXin Li     }
918*67e74705SXin Li     IdentifierInfo *Keyword = Tok.getIdentifierInfo();
919*67e74705SXin Li     SourceLocation KeywordLoc = ConsumeToken();
920*67e74705SXin Li 
921*67e74705SXin Li     if (Keyword == Ident_strict) {
922*67e74705SXin Li       if (StrictLoc.isValid()) {
923*67e74705SXin Li         Diag(KeywordLoc, diag::err_availability_redundant)
924*67e74705SXin Li           << Keyword << SourceRange(StrictLoc);
925*67e74705SXin Li       }
926*67e74705SXin Li       StrictLoc = KeywordLoc;
927*67e74705SXin Li       continue;
928*67e74705SXin Li     }
929*67e74705SXin Li 
930*67e74705SXin Li     if (Keyword == Ident_unavailable) {
931*67e74705SXin Li       if (UnavailableLoc.isValid()) {
932*67e74705SXin Li         Diag(KeywordLoc, diag::err_availability_redundant)
933*67e74705SXin Li           << Keyword << SourceRange(UnavailableLoc);
934*67e74705SXin Li       }
935*67e74705SXin Li       UnavailableLoc = KeywordLoc;
936*67e74705SXin Li       continue;
937*67e74705SXin Li     }
938*67e74705SXin Li 
939*67e74705SXin Li     if (Tok.isNot(tok::equal)) {
940*67e74705SXin Li       Diag(Tok, diag::err_expected_after) << Keyword << tok::equal;
941*67e74705SXin Li       SkipUntil(tok::r_paren, StopAtSemi);
942*67e74705SXin Li       return;
943*67e74705SXin Li     }
944*67e74705SXin Li     ConsumeToken();
945*67e74705SXin Li     if (Keyword == Ident_message || Keyword == Ident_replacement) {
946*67e74705SXin Li       if (Tok.isNot(tok::string_literal)) {
947*67e74705SXin Li         Diag(Tok, diag::err_expected_string_literal)
948*67e74705SXin Li           << /*Source='availability attribute'*/2;
949*67e74705SXin Li         SkipUntil(tok::r_paren, StopAtSemi);
950*67e74705SXin Li         return;
951*67e74705SXin Li       }
952*67e74705SXin Li       if (Keyword == Ident_message)
953*67e74705SXin Li         MessageExpr = ParseStringLiteralExpression();
954*67e74705SXin Li       else
955*67e74705SXin Li         ReplacementExpr = ParseStringLiteralExpression();
956*67e74705SXin Li       // Also reject wide string literals.
957*67e74705SXin Li       if (StringLiteral *MessageStringLiteral =
958*67e74705SXin Li               cast_or_null<StringLiteral>(MessageExpr.get())) {
959*67e74705SXin Li         if (MessageStringLiteral->getCharByteWidth() != 1) {
960*67e74705SXin Li           Diag(MessageStringLiteral->getSourceRange().getBegin(),
961*67e74705SXin Li                diag::err_expected_string_literal)
962*67e74705SXin Li             << /*Source='availability attribute'*/ 2;
963*67e74705SXin Li           SkipUntil(tok::r_paren, StopAtSemi);
964*67e74705SXin Li           return;
965*67e74705SXin Li         }
966*67e74705SXin Li       }
967*67e74705SXin Li       if (Keyword == Ident_message)
968*67e74705SXin Li         break;
969*67e74705SXin Li       else
970*67e74705SXin Li         continue;
971*67e74705SXin Li     }
972*67e74705SXin Li 
973*67e74705SXin Li     // Special handling of 'NA' only when applied to introduced or
974*67e74705SXin Li     // deprecated.
975*67e74705SXin Li     if ((Keyword == Ident_introduced || Keyword == Ident_deprecated) &&
976*67e74705SXin Li         Tok.is(tok::identifier)) {
977*67e74705SXin Li       IdentifierInfo *NA = Tok.getIdentifierInfo();
978*67e74705SXin Li       if (NA->getName() == "NA") {
979*67e74705SXin Li         ConsumeToken();
980*67e74705SXin Li         if (Keyword == Ident_introduced)
981*67e74705SXin Li           UnavailableLoc = KeywordLoc;
982*67e74705SXin Li         continue;
983*67e74705SXin Li       }
984*67e74705SXin Li     }
985*67e74705SXin Li 
986*67e74705SXin Li     SourceRange VersionRange;
987*67e74705SXin Li     VersionTuple Version = ParseVersionTuple(VersionRange);
988*67e74705SXin Li 
989*67e74705SXin Li     if (Version.empty()) {
990*67e74705SXin Li       SkipUntil(tok::r_paren, StopAtSemi);
991*67e74705SXin Li       return;
992*67e74705SXin Li     }
993*67e74705SXin Li 
994*67e74705SXin Li     unsigned Index;
995*67e74705SXin Li     if (Keyword == Ident_introduced)
996*67e74705SXin Li       Index = Introduced;
997*67e74705SXin Li     else if (Keyword == Ident_deprecated)
998*67e74705SXin Li       Index = Deprecated;
999*67e74705SXin Li     else if (Keyword == Ident_obsoleted)
1000*67e74705SXin Li       Index = Obsoleted;
1001*67e74705SXin Li     else
1002*67e74705SXin Li       Index = Unknown;
1003*67e74705SXin Li 
1004*67e74705SXin Li     if (Index < Unknown) {
1005*67e74705SXin Li       if (!Changes[Index].KeywordLoc.isInvalid()) {
1006*67e74705SXin Li         Diag(KeywordLoc, diag::err_availability_redundant)
1007*67e74705SXin Li           << Keyword
1008*67e74705SXin Li           << SourceRange(Changes[Index].KeywordLoc,
1009*67e74705SXin Li                          Changes[Index].VersionRange.getEnd());
1010*67e74705SXin Li       }
1011*67e74705SXin Li 
1012*67e74705SXin Li       Changes[Index].KeywordLoc = KeywordLoc;
1013*67e74705SXin Li       Changes[Index].Version = Version;
1014*67e74705SXin Li       Changes[Index].VersionRange = VersionRange;
1015*67e74705SXin Li     } else {
1016*67e74705SXin Li       Diag(KeywordLoc, diag::err_availability_unknown_change)
1017*67e74705SXin Li         << Keyword << VersionRange;
1018*67e74705SXin Li     }
1019*67e74705SXin Li 
1020*67e74705SXin Li   } while (TryConsumeToken(tok::comma));
1021*67e74705SXin Li 
1022*67e74705SXin Li   // Closing ')'.
1023*67e74705SXin Li   if (T.consumeClose())
1024*67e74705SXin Li     return;
1025*67e74705SXin Li 
1026*67e74705SXin Li   if (endLoc)
1027*67e74705SXin Li     *endLoc = T.getCloseLocation();
1028*67e74705SXin Li 
1029*67e74705SXin Li   // The 'unavailable' availability cannot be combined with any other
1030*67e74705SXin Li   // availability changes. Make sure that hasn't happened.
1031*67e74705SXin Li   if (UnavailableLoc.isValid()) {
1032*67e74705SXin Li     bool Complained = false;
1033*67e74705SXin Li     for (unsigned Index = Introduced; Index != Unknown; ++Index) {
1034*67e74705SXin Li       if (Changes[Index].KeywordLoc.isValid()) {
1035*67e74705SXin Li         if (!Complained) {
1036*67e74705SXin Li           Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
1037*67e74705SXin Li             << SourceRange(Changes[Index].KeywordLoc,
1038*67e74705SXin Li                            Changes[Index].VersionRange.getEnd());
1039*67e74705SXin Li           Complained = true;
1040*67e74705SXin Li         }
1041*67e74705SXin Li 
1042*67e74705SXin Li         // Clear out the availability.
1043*67e74705SXin Li         Changes[Index] = AvailabilityChange();
1044*67e74705SXin Li       }
1045*67e74705SXin Li     }
1046*67e74705SXin Li   }
1047*67e74705SXin Li 
1048*67e74705SXin Li   // Record this attribute
1049*67e74705SXin Li   attrs.addNew(&Availability,
1050*67e74705SXin Li                SourceRange(AvailabilityLoc, T.getCloseLocation()),
1051*67e74705SXin Li                ScopeName, ScopeLoc,
1052*67e74705SXin Li                Platform,
1053*67e74705SXin Li                Changes[Introduced],
1054*67e74705SXin Li                Changes[Deprecated],
1055*67e74705SXin Li                Changes[Obsoleted],
1056*67e74705SXin Li                UnavailableLoc, MessageExpr.get(),
1057*67e74705SXin Li                Syntax, StrictLoc, ReplacementExpr.get());
1058*67e74705SXin Li }
1059*67e74705SXin Li 
1060*67e74705SXin Li /// \brief Parse the contents of the "objc_bridge_related" attribute.
1061*67e74705SXin Li /// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')'
1062*67e74705SXin Li /// related_class:
1063*67e74705SXin Li ///     Identifier
1064*67e74705SXin Li ///
1065*67e74705SXin Li /// opt-class_method:
1066*67e74705SXin Li ///     Identifier: | <empty>
1067*67e74705SXin Li ///
1068*67e74705SXin Li /// opt-instance_method:
1069*67e74705SXin Li ///     Identifier | <empty>
1070*67e74705SXin Li ///
ParseObjCBridgeRelatedAttribute(IdentifierInfo & ObjCBridgeRelated,SourceLocation ObjCBridgeRelatedLoc,ParsedAttributes & attrs,SourceLocation * endLoc,IdentifierInfo * ScopeName,SourceLocation ScopeLoc,AttributeList::Syntax Syntax)1071*67e74705SXin Li void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated,
1072*67e74705SXin Li                                 SourceLocation ObjCBridgeRelatedLoc,
1073*67e74705SXin Li                                 ParsedAttributes &attrs,
1074*67e74705SXin Li                                 SourceLocation *endLoc,
1075*67e74705SXin Li                                 IdentifierInfo *ScopeName,
1076*67e74705SXin Li                                 SourceLocation ScopeLoc,
1077*67e74705SXin Li                                 AttributeList::Syntax Syntax) {
1078*67e74705SXin Li   // Opening '('.
1079*67e74705SXin Li   BalancedDelimiterTracker T(*this, tok::l_paren);
1080*67e74705SXin Li   if (T.consumeOpen()) {
1081*67e74705SXin Li     Diag(Tok, diag::err_expected) << tok::l_paren;
1082*67e74705SXin Li     return;
1083*67e74705SXin Li   }
1084*67e74705SXin Li 
1085*67e74705SXin Li   // Parse the related class name.
1086*67e74705SXin Li   if (Tok.isNot(tok::identifier)) {
1087*67e74705SXin Li     Diag(Tok, diag::err_objcbridge_related_expected_related_class);
1088*67e74705SXin Li     SkipUntil(tok::r_paren, StopAtSemi);
1089*67e74705SXin Li     return;
1090*67e74705SXin Li   }
1091*67e74705SXin Li   IdentifierLoc *RelatedClass = ParseIdentifierLoc();
1092*67e74705SXin Li   if (ExpectAndConsume(tok::comma)) {
1093*67e74705SXin Li     SkipUntil(tok::r_paren, StopAtSemi);
1094*67e74705SXin Li     return;
1095*67e74705SXin Li   }
1096*67e74705SXin Li 
1097*67e74705SXin Li   // Parse optional class method name.
1098*67e74705SXin Li   IdentifierLoc *ClassMethod = nullptr;
1099*67e74705SXin Li   if (Tok.is(tok::identifier)) {
1100*67e74705SXin Li     ClassMethod = ParseIdentifierLoc();
1101*67e74705SXin Li     if (!TryConsumeToken(tok::colon)) {
1102*67e74705SXin Li       Diag(Tok, diag::err_objcbridge_related_selector_name);
1103*67e74705SXin Li       SkipUntil(tok::r_paren, StopAtSemi);
1104*67e74705SXin Li       return;
1105*67e74705SXin Li     }
1106*67e74705SXin Li   }
1107*67e74705SXin Li   if (!TryConsumeToken(tok::comma)) {
1108*67e74705SXin Li     if (Tok.is(tok::colon))
1109*67e74705SXin Li       Diag(Tok, diag::err_objcbridge_related_selector_name);
1110*67e74705SXin Li     else
1111*67e74705SXin Li       Diag(Tok, diag::err_expected) << tok::comma;
1112*67e74705SXin Li     SkipUntil(tok::r_paren, StopAtSemi);
1113*67e74705SXin Li     return;
1114*67e74705SXin Li   }
1115*67e74705SXin Li 
1116*67e74705SXin Li   // Parse optional instance method name.
1117*67e74705SXin Li   IdentifierLoc *InstanceMethod = nullptr;
1118*67e74705SXin Li   if (Tok.is(tok::identifier))
1119*67e74705SXin Li     InstanceMethod = ParseIdentifierLoc();
1120*67e74705SXin Li   else if (Tok.isNot(tok::r_paren)) {
1121*67e74705SXin Li     Diag(Tok, diag::err_expected) << tok::r_paren;
1122*67e74705SXin Li     SkipUntil(tok::r_paren, StopAtSemi);
1123*67e74705SXin Li     return;
1124*67e74705SXin Li   }
1125*67e74705SXin Li 
1126*67e74705SXin Li   // Closing ')'.
1127*67e74705SXin Li   if (T.consumeClose())
1128*67e74705SXin Li     return;
1129*67e74705SXin Li 
1130*67e74705SXin Li   if (endLoc)
1131*67e74705SXin Li     *endLoc = T.getCloseLocation();
1132*67e74705SXin Li 
1133*67e74705SXin Li   // Record this attribute
1134*67e74705SXin Li   attrs.addNew(&ObjCBridgeRelated,
1135*67e74705SXin Li                SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()),
1136*67e74705SXin Li                ScopeName, ScopeLoc,
1137*67e74705SXin Li                RelatedClass,
1138*67e74705SXin Li                ClassMethod,
1139*67e74705SXin Li                InstanceMethod,
1140*67e74705SXin Li                Syntax);
1141*67e74705SXin Li }
1142*67e74705SXin Li 
1143*67e74705SXin Li // Late Parsed Attributes:
1144*67e74705SXin Li // See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
1145*67e74705SXin Li 
ParseLexedAttributes()1146*67e74705SXin Li void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
1147*67e74705SXin Li 
ParseLexedAttributes()1148*67e74705SXin Li void Parser::LateParsedClass::ParseLexedAttributes() {
1149*67e74705SXin Li   Self->ParseLexedAttributes(*Class);
1150*67e74705SXin Li }
1151*67e74705SXin Li 
ParseLexedAttributes()1152*67e74705SXin Li void Parser::LateParsedAttribute::ParseLexedAttributes() {
1153*67e74705SXin Li   Self->ParseLexedAttribute(*this, true, false);
1154*67e74705SXin Li }
1155*67e74705SXin Li 
1156*67e74705SXin Li /// Wrapper class which calls ParseLexedAttribute, after setting up the
1157*67e74705SXin Li /// scope appropriately.
ParseLexedAttributes(ParsingClass & Class)1158*67e74705SXin Li void Parser::ParseLexedAttributes(ParsingClass &Class) {
1159*67e74705SXin Li   // Deal with templates
1160*67e74705SXin Li   // FIXME: Test cases to make sure this does the right thing for templates.
1161*67e74705SXin Li   bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
1162*67e74705SXin Li   ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
1163*67e74705SXin Li                                 HasTemplateScope);
1164*67e74705SXin Li   if (HasTemplateScope)
1165*67e74705SXin Li     Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
1166*67e74705SXin Li 
1167*67e74705SXin Li   // Set or update the scope flags.
1168*67e74705SXin Li   bool AlreadyHasClassScope = Class.TopLevelClass;
1169*67e74705SXin Li   unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
1170*67e74705SXin Li   ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1171*67e74705SXin Li   ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1172*67e74705SXin Li 
1173*67e74705SXin Li   // Enter the scope of nested classes
1174*67e74705SXin Li   if (!AlreadyHasClassScope)
1175*67e74705SXin Li     Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1176*67e74705SXin Li                                                 Class.TagOrTemplate);
1177*67e74705SXin Li   if (!Class.LateParsedDeclarations.empty()) {
1178*67e74705SXin Li     for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1179*67e74705SXin Li       Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1180*67e74705SXin Li     }
1181*67e74705SXin Li   }
1182*67e74705SXin Li 
1183*67e74705SXin Li   if (!AlreadyHasClassScope)
1184*67e74705SXin Li     Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1185*67e74705SXin Li                                                  Class.TagOrTemplate);
1186*67e74705SXin Li }
1187*67e74705SXin Li 
1188*67e74705SXin Li /// \brief Parse all attributes in LAs, and attach them to Decl D.
ParseLexedAttributeList(LateParsedAttrList & LAs,Decl * D,bool EnterScope,bool OnDefinition)1189*67e74705SXin Li void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1190*67e74705SXin Li                                      bool EnterScope, bool OnDefinition) {
1191*67e74705SXin Li   assert(LAs.parseSoon() &&
1192*67e74705SXin Li          "Attribute list should be marked for immediate parsing.");
1193*67e74705SXin Li   for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
1194*67e74705SXin Li     if (D)
1195*67e74705SXin Li       LAs[i]->addDecl(D);
1196*67e74705SXin Li     ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
1197*67e74705SXin Li     delete LAs[i];
1198*67e74705SXin Li   }
1199*67e74705SXin Li   LAs.clear();
1200*67e74705SXin Li }
1201*67e74705SXin Li 
1202*67e74705SXin Li /// \brief Finish parsing an attribute for which parsing was delayed.
1203*67e74705SXin Li /// This will be called at the end of parsing a class declaration
1204*67e74705SXin Li /// for each LateParsedAttribute. We consume the saved tokens and
1205*67e74705SXin Li /// create an attribute with the arguments filled in. We add this
1206*67e74705SXin Li /// to the Attribute list for the decl.
ParseLexedAttribute(LateParsedAttribute & LA,bool EnterScope,bool OnDefinition)1207*67e74705SXin Li void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1208*67e74705SXin Li                                  bool EnterScope, bool OnDefinition) {
1209*67e74705SXin Li   // Create a fake EOF so that attribute parsing won't go off the end of the
1210*67e74705SXin Li   // attribute.
1211*67e74705SXin Li   Token AttrEnd;
1212*67e74705SXin Li   AttrEnd.startToken();
1213*67e74705SXin Li   AttrEnd.setKind(tok::eof);
1214*67e74705SXin Li   AttrEnd.setLocation(Tok.getLocation());
1215*67e74705SXin Li   AttrEnd.setEofData(LA.Toks.data());
1216*67e74705SXin Li   LA.Toks.push_back(AttrEnd);
1217*67e74705SXin Li 
1218*67e74705SXin Li   // Append the current token at the end of the new token stream so that it
1219*67e74705SXin Li   // doesn't get lost.
1220*67e74705SXin Li   LA.Toks.push_back(Tok);
1221*67e74705SXin Li   PP.EnterTokenStream(LA.Toks, true);
1222*67e74705SXin Li   // Consume the previously pushed token.
1223*67e74705SXin Li   ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
1224*67e74705SXin Li 
1225*67e74705SXin Li   ParsedAttributes Attrs(AttrFactory);
1226*67e74705SXin Li   SourceLocation endLoc;
1227*67e74705SXin Li 
1228*67e74705SXin Li   if (LA.Decls.size() > 0) {
1229*67e74705SXin Li     Decl *D = LA.Decls[0];
1230*67e74705SXin Li     NamedDecl *ND  = dyn_cast<NamedDecl>(D);
1231*67e74705SXin Li     RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
1232*67e74705SXin Li 
1233*67e74705SXin Li     // Allow 'this' within late-parsed attributes.
1234*67e74705SXin Li     Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1235*67e74705SXin Li                                      ND && ND->isCXXInstanceMember());
1236*67e74705SXin Li 
1237*67e74705SXin Li     if (LA.Decls.size() == 1) {
1238*67e74705SXin Li       // If the Decl is templatized, add template parameters to scope.
1239*67e74705SXin Li       bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1240*67e74705SXin Li       ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1241*67e74705SXin Li       if (HasTemplateScope)
1242*67e74705SXin Li         Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
1243*67e74705SXin Li 
1244*67e74705SXin Li       // If the Decl is on a function, add function parameters to the scope.
1245*67e74705SXin Li       bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1246*67e74705SXin Li       ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1247*67e74705SXin Li       if (HasFunScope)
1248*67e74705SXin Li         Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
1249*67e74705SXin Li 
1250*67e74705SXin Li       ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
1251*67e74705SXin Li                             nullptr, SourceLocation(), AttributeList::AS_GNU,
1252*67e74705SXin Li                             nullptr);
1253*67e74705SXin Li 
1254*67e74705SXin Li       if (HasFunScope) {
1255*67e74705SXin Li         Actions.ActOnExitFunctionContext();
1256*67e74705SXin Li         FnScope.Exit();  // Pop scope, and remove Decls from IdResolver
1257*67e74705SXin Li       }
1258*67e74705SXin Li       if (HasTemplateScope) {
1259*67e74705SXin Li         TempScope.Exit();
1260*67e74705SXin Li       }
1261*67e74705SXin Li     } else {
1262*67e74705SXin Li       // If there are multiple decls, then the decl cannot be within the
1263*67e74705SXin Li       // function scope.
1264*67e74705SXin Li       ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
1265*67e74705SXin Li                             nullptr, SourceLocation(), AttributeList::AS_GNU,
1266*67e74705SXin Li                             nullptr);
1267*67e74705SXin Li     }
1268*67e74705SXin Li   } else {
1269*67e74705SXin Li     Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
1270*67e74705SXin Li   }
1271*67e74705SXin Li 
1272*67e74705SXin Li   const AttributeList *AL = Attrs.getList();
1273*67e74705SXin Li   if (OnDefinition && AL && !AL->isCXX11Attribute() &&
1274*67e74705SXin Li       AL->isKnownToGCC())
1275*67e74705SXin Li     Diag(Tok, diag::warn_attribute_on_function_definition)
1276*67e74705SXin Li       << &LA.AttrName;
1277*67e74705SXin Li 
1278*67e74705SXin Li   for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i)
1279*67e74705SXin Li     Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
1280*67e74705SXin Li 
1281*67e74705SXin Li   // Due to a parsing error, we either went over the cached tokens or
1282*67e74705SXin Li   // there are still cached tokens left, so we skip the leftover tokens.
1283*67e74705SXin Li   while (Tok.isNot(tok::eof))
1284*67e74705SXin Li     ConsumeAnyToken();
1285*67e74705SXin Li 
1286*67e74705SXin Li   if (Tok.is(tok::eof) && Tok.getEofData() == AttrEnd.getEofData())
1287*67e74705SXin Li     ConsumeAnyToken();
1288*67e74705SXin Li }
1289*67e74705SXin Li 
ParseTypeTagForDatatypeAttribute(IdentifierInfo & AttrName,SourceLocation AttrNameLoc,ParsedAttributes & Attrs,SourceLocation * EndLoc,IdentifierInfo * ScopeName,SourceLocation ScopeLoc,AttributeList::Syntax Syntax)1290*67e74705SXin Li void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1291*67e74705SXin Li                                               SourceLocation AttrNameLoc,
1292*67e74705SXin Li                                               ParsedAttributes &Attrs,
1293*67e74705SXin Li                                               SourceLocation *EndLoc,
1294*67e74705SXin Li                                               IdentifierInfo *ScopeName,
1295*67e74705SXin Li                                               SourceLocation ScopeLoc,
1296*67e74705SXin Li                                               AttributeList::Syntax Syntax) {
1297*67e74705SXin Li   assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1298*67e74705SXin Li 
1299*67e74705SXin Li   BalancedDelimiterTracker T(*this, tok::l_paren);
1300*67e74705SXin Li   T.consumeOpen();
1301*67e74705SXin Li 
1302*67e74705SXin Li   if (Tok.isNot(tok::identifier)) {
1303*67e74705SXin Li     Diag(Tok, diag::err_expected) << tok::identifier;
1304*67e74705SXin Li     T.skipToEnd();
1305*67e74705SXin Li     return;
1306*67e74705SXin Li   }
1307*67e74705SXin Li   IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
1308*67e74705SXin Li 
1309*67e74705SXin Li   if (ExpectAndConsume(tok::comma)) {
1310*67e74705SXin Li     T.skipToEnd();
1311*67e74705SXin Li     return;
1312*67e74705SXin Li   }
1313*67e74705SXin Li 
1314*67e74705SXin Li   SourceRange MatchingCTypeRange;
1315*67e74705SXin Li   TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1316*67e74705SXin Li   if (MatchingCType.isInvalid()) {
1317*67e74705SXin Li     T.skipToEnd();
1318*67e74705SXin Li     return;
1319*67e74705SXin Li   }
1320*67e74705SXin Li 
1321*67e74705SXin Li   bool LayoutCompatible = false;
1322*67e74705SXin Li   bool MustBeNull = false;
1323*67e74705SXin Li   while (TryConsumeToken(tok::comma)) {
1324*67e74705SXin Li     if (Tok.isNot(tok::identifier)) {
1325*67e74705SXin Li       Diag(Tok, diag::err_expected) << tok::identifier;
1326*67e74705SXin Li       T.skipToEnd();
1327*67e74705SXin Li       return;
1328*67e74705SXin Li     }
1329*67e74705SXin Li     IdentifierInfo *Flag = Tok.getIdentifierInfo();
1330*67e74705SXin Li     if (Flag->isStr("layout_compatible"))
1331*67e74705SXin Li       LayoutCompatible = true;
1332*67e74705SXin Li     else if (Flag->isStr("must_be_null"))
1333*67e74705SXin Li       MustBeNull = true;
1334*67e74705SXin Li     else {
1335*67e74705SXin Li       Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1336*67e74705SXin Li       T.skipToEnd();
1337*67e74705SXin Li       return;
1338*67e74705SXin Li     }
1339*67e74705SXin Li     ConsumeToken(); // consume flag
1340*67e74705SXin Li   }
1341*67e74705SXin Li 
1342*67e74705SXin Li   if (!T.consumeClose()) {
1343*67e74705SXin Li     Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, ScopeName, ScopeLoc,
1344*67e74705SXin Li                                    ArgumentKind, MatchingCType.get(),
1345*67e74705SXin Li                                    LayoutCompatible, MustBeNull, Syntax);
1346*67e74705SXin Li   }
1347*67e74705SXin Li 
1348*67e74705SXin Li   if (EndLoc)
1349*67e74705SXin Li     *EndLoc = T.getCloseLocation();
1350*67e74705SXin Li }
1351*67e74705SXin Li 
1352*67e74705SXin Li /// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1353*67e74705SXin Li /// of a C++11 attribute-specifier in a location where an attribute is not
1354*67e74705SXin Li /// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1355*67e74705SXin Li /// situation.
1356*67e74705SXin Li ///
1357*67e74705SXin Li /// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1358*67e74705SXin Li /// this doesn't appear to actually be an attribute-specifier, and the caller
1359*67e74705SXin Li /// should try to parse it.
DiagnoseProhibitedCXX11Attribute()1360*67e74705SXin Li bool Parser::DiagnoseProhibitedCXX11Attribute() {
1361*67e74705SXin Li   assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1362*67e74705SXin Li 
1363*67e74705SXin Li   switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1364*67e74705SXin Li   case CAK_NotAttributeSpecifier:
1365*67e74705SXin Li     // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1366*67e74705SXin Li     return false;
1367*67e74705SXin Li 
1368*67e74705SXin Li   case CAK_InvalidAttributeSpecifier:
1369*67e74705SXin Li     Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1370*67e74705SXin Li     return false;
1371*67e74705SXin Li 
1372*67e74705SXin Li   case CAK_AttributeSpecifier:
1373*67e74705SXin Li     // Parse and discard the attributes.
1374*67e74705SXin Li     SourceLocation BeginLoc = ConsumeBracket();
1375*67e74705SXin Li     ConsumeBracket();
1376*67e74705SXin Li     SkipUntil(tok::r_square);
1377*67e74705SXin Li     assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1378*67e74705SXin Li     SourceLocation EndLoc = ConsumeBracket();
1379*67e74705SXin Li     Diag(BeginLoc, diag::err_attributes_not_allowed)
1380*67e74705SXin Li       << SourceRange(BeginLoc, EndLoc);
1381*67e74705SXin Li     return true;
1382*67e74705SXin Li   }
1383*67e74705SXin Li   llvm_unreachable("All cases handled above.");
1384*67e74705SXin Li }
1385*67e74705SXin Li 
1386*67e74705SXin Li /// \brief We have found the opening square brackets of a C++11
1387*67e74705SXin Li /// attribute-specifier in a location where an attribute is not permitted, but
1388*67e74705SXin Li /// we know where the attributes ought to be written. Parse them anyway, and
1389*67e74705SXin Li /// provide a fixit moving them to the right place.
DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange & Attrs,SourceLocation CorrectLocation)1390*67e74705SXin Li void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1391*67e74705SXin Li                                              SourceLocation CorrectLocation) {
1392*67e74705SXin Li   assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1393*67e74705SXin Li          Tok.is(tok::kw_alignas));
1394*67e74705SXin Li 
1395*67e74705SXin Li   // Consume the attributes.
1396*67e74705SXin Li   SourceLocation Loc = Tok.getLocation();
1397*67e74705SXin Li   ParseCXX11Attributes(Attrs);
1398*67e74705SXin Li   CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1399*67e74705SXin Li 
1400*67e74705SXin Li   Diag(Loc, diag::err_attributes_not_allowed)
1401*67e74705SXin Li     << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1402*67e74705SXin Li     << FixItHint::CreateRemoval(AttrRange);
1403*67e74705SXin Li }
1404*67e74705SXin Li 
DiagnoseProhibitedAttributes(ParsedAttributesWithRange & attrs)1405*67e74705SXin Li void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1406*67e74705SXin Li   Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1407*67e74705SXin Li     << attrs.Range;
1408*67e74705SXin Li }
1409*67e74705SXin Li 
ProhibitCXX11Attributes(ParsedAttributesWithRange & attrs)1410*67e74705SXin Li void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1411*67e74705SXin Li   AttributeList *AttrList = attrs.getList();
1412*67e74705SXin Li   while (AttrList) {
1413*67e74705SXin Li     if (AttrList->isCXX11Attribute()) {
1414*67e74705SXin Li       Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
1415*67e74705SXin Li         << AttrList->getName();
1416*67e74705SXin Li       AttrList->setInvalid();
1417*67e74705SXin Li     }
1418*67e74705SXin Li     AttrList = AttrList->getNext();
1419*67e74705SXin Li   }
1420*67e74705SXin Li }
1421*67e74705SXin Li 
1422*67e74705SXin Li // As an exception to the rule, __declspec(align(...)) before the
1423*67e74705SXin Li // class-key affects the type instead of the variable.
handleDeclspecAlignBeforeClassKey(ParsedAttributesWithRange & Attrs,DeclSpec & DS,Sema::TagUseKind TUK)1424*67e74705SXin Li void Parser::handleDeclspecAlignBeforeClassKey(ParsedAttributesWithRange &Attrs,
1425*67e74705SXin Li                                                DeclSpec &DS,
1426*67e74705SXin Li                                                Sema::TagUseKind TUK) {
1427*67e74705SXin Li   if (TUK == Sema::TUK_Reference)
1428*67e74705SXin Li     return;
1429*67e74705SXin Li 
1430*67e74705SXin Li   ParsedAttributes &PA = DS.getAttributes();
1431*67e74705SXin Li   AttributeList *AL = PA.getList();
1432*67e74705SXin Li   AttributeList *Prev = nullptr;
1433*67e74705SXin Li   while (AL) {
1434*67e74705SXin Li     AttributeList *Next = AL->getNext();
1435*67e74705SXin Li 
1436*67e74705SXin Li     // We only consider attributes using the appropriate '__declspec' spelling.
1437*67e74705SXin Li     // This behavior doesn't extend to any other spellings.
1438*67e74705SXin Li     if (AL->getKind() == AttributeList::AT_Aligned &&
1439*67e74705SXin Li         AL->isDeclspecAttribute()) {
1440*67e74705SXin Li       // Stitch the attribute into the tag's attribute list.
1441*67e74705SXin Li       AL->setNext(nullptr);
1442*67e74705SXin Li       Attrs.add(AL);
1443*67e74705SXin Li 
1444*67e74705SXin Li       // Remove the attribute from the variable's attribute list.
1445*67e74705SXin Li       if (Prev) {
1446*67e74705SXin Li         // Set the last variable attribute's next attribute to be the attribute
1447*67e74705SXin Li         // after the current one.
1448*67e74705SXin Li         Prev->setNext(Next);
1449*67e74705SXin Li       } else {
1450*67e74705SXin Li         // Removing the head of the list requires us to reset the head to the
1451*67e74705SXin Li         // next attribute.
1452*67e74705SXin Li         PA.set(Next);
1453*67e74705SXin Li       }
1454*67e74705SXin Li     } else {
1455*67e74705SXin Li       Prev = AL;
1456*67e74705SXin Li     }
1457*67e74705SXin Li 
1458*67e74705SXin Li     AL = Next;
1459*67e74705SXin Li   }
1460*67e74705SXin Li }
1461*67e74705SXin Li 
1462*67e74705SXin Li /// ParseDeclaration - Parse a full 'declaration', which consists of
1463*67e74705SXin Li /// declaration-specifiers, some number of declarators, and a semicolon.
1464*67e74705SXin Li /// 'Context' should be a Declarator::TheContext value.  This returns the
1465*67e74705SXin Li /// location of the semicolon in DeclEnd.
1466*67e74705SXin Li ///
1467*67e74705SXin Li ///       declaration: [C99 6.7]
1468*67e74705SXin Li ///         block-declaration ->
1469*67e74705SXin Li ///           simple-declaration
1470*67e74705SXin Li ///           others                   [FIXME]
1471*67e74705SXin Li /// [C++]   template-declaration
1472*67e74705SXin Li /// [C++]   namespace-definition
1473*67e74705SXin Li /// [C++]   using-directive
1474*67e74705SXin Li /// [C++]   using-declaration
1475*67e74705SXin Li /// [C++11/C11] static_assert-declaration
1476*67e74705SXin Li ///         others... [FIXME]
1477*67e74705SXin Li ///
ParseDeclaration(unsigned Context,SourceLocation & DeclEnd,ParsedAttributesWithRange & attrs)1478*67e74705SXin Li Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
1479*67e74705SXin Li                                                 SourceLocation &DeclEnd,
1480*67e74705SXin Li                                           ParsedAttributesWithRange &attrs) {
1481*67e74705SXin Li   ParenBraceBracketBalancer BalancerRAIIObj(*this);
1482*67e74705SXin Li   // Must temporarily exit the objective-c container scope for
1483*67e74705SXin Li   // parsing c none objective-c decls.
1484*67e74705SXin Li   ObjCDeclContextSwitch ObjCDC(*this);
1485*67e74705SXin Li 
1486*67e74705SXin Li   Decl *SingleDecl = nullptr;
1487*67e74705SXin Li   Decl *OwnedType = nullptr;
1488*67e74705SXin Li   switch (Tok.getKind()) {
1489*67e74705SXin Li   case tok::kw_template:
1490*67e74705SXin Li   case tok::kw_export:
1491*67e74705SXin Li     ProhibitAttributes(attrs);
1492*67e74705SXin Li     SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
1493*67e74705SXin Li     break;
1494*67e74705SXin Li   case tok::kw_inline:
1495*67e74705SXin Li     // Could be the start of an inline namespace. Allowed as an ext in C++03.
1496*67e74705SXin Li     if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
1497*67e74705SXin Li       ProhibitAttributes(attrs);
1498*67e74705SXin Li       SourceLocation InlineLoc = ConsumeToken();
1499*67e74705SXin Li       return ParseNamespace(Context, DeclEnd, InlineLoc);
1500*67e74705SXin Li     }
1501*67e74705SXin Li     return ParseSimpleDeclaration(Context, DeclEnd, attrs,
1502*67e74705SXin Li                                   true);
1503*67e74705SXin Li   case tok::kw_namespace:
1504*67e74705SXin Li     ProhibitAttributes(attrs);
1505*67e74705SXin Li     return ParseNamespace(Context, DeclEnd);
1506*67e74705SXin Li   case tok::kw_using:
1507*67e74705SXin Li     SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
1508*67e74705SXin Li                                                   DeclEnd, attrs, &OwnedType);
1509*67e74705SXin Li     break;
1510*67e74705SXin Li   case tok::kw_static_assert:
1511*67e74705SXin Li   case tok::kw__Static_assert:
1512*67e74705SXin Li     ProhibitAttributes(attrs);
1513*67e74705SXin Li     SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
1514*67e74705SXin Li     break;
1515*67e74705SXin Li   default:
1516*67e74705SXin Li     return ParseSimpleDeclaration(Context, DeclEnd, attrs, true);
1517*67e74705SXin Li   }
1518*67e74705SXin Li 
1519*67e74705SXin Li   // This routine returns a DeclGroup, if the thing we parsed only contains a
1520*67e74705SXin Li   // single decl, convert it now. Alias declarations can also declare a type;
1521*67e74705SXin Li   // include that too if it is present.
1522*67e74705SXin Li   return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
1523*67e74705SXin Li }
1524*67e74705SXin Li 
1525*67e74705SXin Li ///       simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1526*67e74705SXin Li ///         declaration-specifiers init-declarator-list[opt] ';'
1527*67e74705SXin Li /// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1528*67e74705SXin Li ///             init-declarator-list ';'
1529*67e74705SXin Li ///[C90/C++]init-declarator-list ';'                             [TODO]
1530*67e74705SXin Li /// [OMP]   threadprivate-directive                              [TODO]
1531*67e74705SXin Li ///
1532*67e74705SXin Li ///       for-range-declaration: [C++11 6.5p1: stmt.ranged]
1533*67e74705SXin Li ///         attribute-specifier-seq[opt] type-specifier-seq declarator
1534*67e74705SXin Li ///
1535*67e74705SXin Li /// If RequireSemi is false, this does not check for a ';' at the end of the
1536*67e74705SXin Li /// declaration.  If it is true, it checks for and eats it.
1537*67e74705SXin Li ///
1538*67e74705SXin Li /// If FRI is non-null, we might be parsing a for-range-declaration instead
1539*67e74705SXin Li /// of a simple-declaration. If we find that we are, we also parse the
1540*67e74705SXin Li /// for-range-initializer, and place it here.
1541*67e74705SXin Li Parser::DeclGroupPtrTy
ParseSimpleDeclaration(unsigned Context,SourceLocation & DeclEnd,ParsedAttributesWithRange & Attrs,bool RequireSemi,ForRangeInit * FRI)1542*67e74705SXin Li Parser::ParseSimpleDeclaration(unsigned Context,
1543*67e74705SXin Li                                SourceLocation &DeclEnd,
1544*67e74705SXin Li                                ParsedAttributesWithRange &Attrs,
1545*67e74705SXin Li                                bool RequireSemi, ForRangeInit *FRI) {
1546*67e74705SXin Li   // Parse the common declaration-specifiers piece.
1547*67e74705SXin Li   ParsingDeclSpec DS(*this);
1548*67e74705SXin Li 
1549*67e74705SXin Li   DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
1550*67e74705SXin Li   ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
1551*67e74705SXin Li 
1552*67e74705SXin Li   // If we had a free-standing type definition with a missing semicolon, we
1553*67e74705SXin Li   // may get this far before the problem becomes obvious.
1554*67e74705SXin Li   if (DS.hasTagDefinition() &&
1555*67e74705SXin Li       DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
1556*67e74705SXin Li     return nullptr;
1557*67e74705SXin Li 
1558*67e74705SXin Li   // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1559*67e74705SXin Li   // declaration-specifiers init-declarator-list[opt] ';'
1560*67e74705SXin Li   if (Tok.is(tok::semi)) {
1561*67e74705SXin Li     ProhibitAttributes(Attrs);
1562*67e74705SXin Li     DeclEnd = Tok.getLocation();
1563*67e74705SXin Li     if (RequireSemi) ConsumeToken();
1564*67e74705SXin Li     RecordDecl *AnonRecord = nullptr;
1565*67e74705SXin Li     Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
1566*67e74705SXin Li                                                        DS, AnonRecord);
1567*67e74705SXin Li     DS.complete(TheDecl);
1568*67e74705SXin Li     if (AnonRecord) {
1569*67e74705SXin Li       Decl* decls[] = {AnonRecord, TheDecl};
1570*67e74705SXin Li       return Actions.BuildDeclaratorGroup(decls, /*TypeMayContainAuto=*/false);
1571*67e74705SXin Li     }
1572*67e74705SXin Li     return Actions.ConvertDeclToDeclGroup(TheDecl);
1573*67e74705SXin Li   }
1574*67e74705SXin Li 
1575*67e74705SXin Li   DS.takeAttributesFrom(Attrs);
1576*67e74705SXin Li   return ParseDeclGroup(DS, Context, &DeclEnd, FRI);
1577*67e74705SXin Li }
1578*67e74705SXin Li 
1579*67e74705SXin Li /// Returns true if this might be the start of a declarator, or a common typo
1580*67e74705SXin Li /// for a declarator.
MightBeDeclarator(unsigned Context)1581*67e74705SXin Li bool Parser::MightBeDeclarator(unsigned Context) {
1582*67e74705SXin Li   switch (Tok.getKind()) {
1583*67e74705SXin Li   case tok::annot_cxxscope:
1584*67e74705SXin Li   case tok::annot_template_id:
1585*67e74705SXin Li   case tok::caret:
1586*67e74705SXin Li   case tok::code_completion:
1587*67e74705SXin Li   case tok::coloncolon:
1588*67e74705SXin Li   case tok::ellipsis:
1589*67e74705SXin Li   case tok::kw___attribute:
1590*67e74705SXin Li   case tok::kw_operator:
1591*67e74705SXin Li   case tok::l_paren:
1592*67e74705SXin Li   case tok::star:
1593*67e74705SXin Li     return true;
1594*67e74705SXin Li 
1595*67e74705SXin Li   case tok::amp:
1596*67e74705SXin Li   case tok::ampamp:
1597*67e74705SXin Li     return getLangOpts().CPlusPlus;
1598*67e74705SXin Li 
1599*67e74705SXin Li   case tok::l_square: // Might be an attribute on an unnamed bit-field.
1600*67e74705SXin Li     return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
1601*67e74705SXin Li            NextToken().is(tok::l_square);
1602*67e74705SXin Li 
1603*67e74705SXin Li   case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
1604*67e74705SXin Li     return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
1605*67e74705SXin Li 
1606*67e74705SXin Li   case tok::identifier:
1607*67e74705SXin Li     switch (NextToken().getKind()) {
1608*67e74705SXin Li     case tok::code_completion:
1609*67e74705SXin Li     case tok::coloncolon:
1610*67e74705SXin Li     case tok::comma:
1611*67e74705SXin Li     case tok::equal:
1612*67e74705SXin Li     case tok::equalequal: // Might be a typo for '='.
1613*67e74705SXin Li     case tok::kw_alignas:
1614*67e74705SXin Li     case tok::kw_asm:
1615*67e74705SXin Li     case tok::kw___attribute:
1616*67e74705SXin Li     case tok::l_brace:
1617*67e74705SXin Li     case tok::l_paren:
1618*67e74705SXin Li     case tok::l_square:
1619*67e74705SXin Li     case tok::less:
1620*67e74705SXin Li     case tok::r_brace:
1621*67e74705SXin Li     case tok::r_paren:
1622*67e74705SXin Li     case tok::r_square:
1623*67e74705SXin Li     case tok::semi:
1624*67e74705SXin Li       return true;
1625*67e74705SXin Li 
1626*67e74705SXin Li     case tok::colon:
1627*67e74705SXin Li       // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
1628*67e74705SXin Li       // and in block scope it's probably a label. Inside a class definition,
1629*67e74705SXin Li       // this is a bit-field.
1630*67e74705SXin Li       return Context == Declarator::MemberContext ||
1631*67e74705SXin Li              (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
1632*67e74705SXin Li 
1633*67e74705SXin Li     case tok::identifier: // Possible virt-specifier.
1634*67e74705SXin Li       return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
1635*67e74705SXin Li 
1636*67e74705SXin Li     default:
1637*67e74705SXin Li       return false;
1638*67e74705SXin Li     }
1639*67e74705SXin Li 
1640*67e74705SXin Li   default:
1641*67e74705SXin Li     return false;
1642*67e74705SXin Li   }
1643*67e74705SXin Li }
1644*67e74705SXin Li 
1645*67e74705SXin Li /// Skip until we reach something which seems like a sensible place to pick
1646*67e74705SXin Li /// up parsing after a malformed declaration. This will sometimes stop sooner
1647*67e74705SXin Li /// than SkipUntil(tok::r_brace) would, but will never stop later.
SkipMalformedDecl()1648*67e74705SXin Li void Parser::SkipMalformedDecl() {
1649*67e74705SXin Li   while (true) {
1650*67e74705SXin Li     switch (Tok.getKind()) {
1651*67e74705SXin Li     case tok::l_brace:
1652*67e74705SXin Li       // Skip until matching }, then stop. We've probably skipped over
1653*67e74705SXin Li       // a malformed class or function definition or similar.
1654*67e74705SXin Li       ConsumeBrace();
1655*67e74705SXin Li       SkipUntil(tok::r_brace);
1656*67e74705SXin Li       if (Tok.isOneOf(tok::comma, tok::l_brace, tok::kw_try)) {
1657*67e74705SXin Li         // This declaration isn't over yet. Keep skipping.
1658*67e74705SXin Li         continue;
1659*67e74705SXin Li       }
1660*67e74705SXin Li       TryConsumeToken(tok::semi);
1661*67e74705SXin Li       return;
1662*67e74705SXin Li 
1663*67e74705SXin Li     case tok::l_square:
1664*67e74705SXin Li       ConsumeBracket();
1665*67e74705SXin Li       SkipUntil(tok::r_square);
1666*67e74705SXin Li       continue;
1667*67e74705SXin Li 
1668*67e74705SXin Li     case tok::l_paren:
1669*67e74705SXin Li       ConsumeParen();
1670*67e74705SXin Li       SkipUntil(tok::r_paren);
1671*67e74705SXin Li       continue;
1672*67e74705SXin Li 
1673*67e74705SXin Li     case tok::r_brace:
1674*67e74705SXin Li       return;
1675*67e74705SXin Li 
1676*67e74705SXin Li     case tok::semi:
1677*67e74705SXin Li       ConsumeToken();
1678*67e74705SXin Li       return;
1679*67e74705SXin Li 
1680*67e74705SXin Li     case tok::kw_inline:
1681*67e74705SXin Li       // 'inline namespace' at the start of a line is almost certainly
1682*67e74705SXin Li       // a good place to pick back up parsing, except in an Objective-C
1683*67e74705SXin Li       // @interface context.
1684*67e74705SXin Li       if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1685*67e74705SXin Li           (!ParsingInObjCContainer || CurParsedObjCImpl))
1686*67e74705SXin Li         return;
1687*67e74705SXin Li       break;
1688*67e74705SXin Li 
1689*67e74705SXin Li     case tok::kw_namespace:
1690*67e74705SXin Li       // 'namespace' at the start of a line is almost certainly a good
1691*67e74705SXin Li       // place to pick back up parsing, except in an Objective-C
1692*67e74705SXin Li       // @interface context.
1693*67e74705SXin Li       if (Tok.isAtStartOfLine() &&
1694*67e74705SXin Li           (!ParsingInObjCContainer || CurParsedObjCImpl))
1695*67e74705SXin Li         return;
1696*67e74705SXin Li       break;
1697*67e74705SXin Li 
1698*67e74705SXin Li     case tok::at:
1699*67e74705SXin Li       // @end is very much like } in Objective-C contexts.
1700*67e74705SXin Li       if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1701*67e74705SXin Li           ParsingInObjCContainer)
1702*67e74705SXin Li         return;
1703*67e74705SXin Li       break;
1704*67e74705SXin Li 
1705*67e74705SXin Li     case tok::minus:
1706*67e74705SXin Li     case tok::plus:
1707*67e74705SXin Li       // - and + probably start new method declarations in Objective-C contexts.
1708*67e74705SXin Li       if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
1709*67e74705SXin Li         return;
1710*67e74705SXin Li       break;
1711*67e74705SXin Li 
1712*67e74705SXin Li     case tok::eof:
1713*67e74705SXin Li     case tok::annot_module_begin:
1714*67e74705SXin Li     case tok::annot_module_end:
1715*67e74705SXin Li     case tok::annot_module_include:
1716*67e74705SXin Li       return;
1717*67e74705SXin Li 
1718*67e74705SXin Li     default:
1719*67e74705SXin Li       break;
1720*67e74705SXin Li     }
1721*67e74705SXin Li 
1722*67e74705SXin Li     ConsumeAnyToken();
1723*67e74705SXin Li   }
1724*67e74705SXin Li }
1725*67e74705SXin Li 
1726*67e74705SXin Li /// ParseDeclGroup - Having concluded that this is either a function
1727*67e74705SXin Li /// definition or a group of object declarations, actually parse the
1728*67e74705SXin Li /// result.
ParseDeclGroup(ParsingDeclSpec & DS,unsigned Context,SourceLocation * DeclEnd,ForRangeInit * FRI)1729*67e74705SXin Li Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1730*67e74705SXin Li                                               unsigned Context,
1731*67e74705SXin Li                                               SourceLocation *DeclEnd,
1732*67e74705SXin Li                                               ForRangeInit *FRI) {
1733*67e74705SXin Li   // Parse the first declarator.
1734*67e74705SXin Li   ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
1735*67e74705SXin Li   ParseDeclarator(D);
1736*67e74705SXin Li 
1737*67e74705SXin Li   // Bail out if the first declarator didn't seem well-formed.
1738*67e74705SXin Li   if (!D.hasName() && !D.mayOmitIdentifier()) {
1739*67e74705SXin Li     SkipMalformedDecl();
1740*67e74705SXin Li     return nullptr;
1741*67e74705SXin Li   }
1742*67e74705SXin Li 
1743*67e74705SXin Li   // Save late-parsed attributes for now; they need to be parsed in the
1744*67e74705SXin Li   // appropriate function scope after the function Decl has been constructed.
1745*67e74705SXin Li   // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1746*67e74705SXin Li   LateParsedAttrList LateParsedAttrs(true);
1747*67e74705SXin Li   if (D.isFunctionDeclarator()) {
1748*67e74705SXin Li     MaybeParseGNUAttributes(D, &LateParsedAttrs);
1749*67e74705SXin Li 
1750*67e74705SXin Li     // The _Noreturn keyword can't appear here, unlike the GNU noreturn
1751*67e74705SXin Li     // attribute. If we find the keyword here, tell the user to put it
1752*67e74705SXin Li     // at the start instead.
1753*67e74705SXin Li     if (Tok.is(tok::kw__Noreturn)) {
1754*67e74705SXin Li       SourceLocation Loc = ConsumeToken();
1755*67e74705SXin Li       const char *PrevSpec;
1756*67e74705SXin Li       unsigned DiagID;
1757*67e74705SXin Li 
1758*67e74705SXin Li       // We can offer a fixit if it's valid to mark this function as _Noreturn
1759*67e74705SXin Li       // and we don't have any other declarators in this declaration.
1760*67e74705SXin Li       bool Fixit = !DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
1761*67e74705SXin Li       MaybeParseGNUAttributes(D, &LateParsedAttrs);
1762*67e74705SXin Li       Fixit &= Tok.isOneOf(tok::semi, tok::l_brace, tok::kw_try);
1763*67e74705SXin Li 
1764*67e74705SXin Li       Diag(Loc, diag::err_c11_noreturn_misplaced)
1765*67e74705SXin Li           << (Fixit ? FixItHint::CreateRemoval(Loc) : FixItHint())
1766*67e74705SXin Li           << (Fixit ? FixItHint::CreateInsertion(D.getLocStart(), "_Noreturn ")
1767*67e74705SXin Li                     : FixItHint());
1768*67e74705SXin Li     }
1769*67e74705SXin Li   }
1770*67e74705SXin Li 
1771*67e74705SXin Li   // Check to see if we have a function *definition* which must have a body.
1772*67e74705SXin Li   if (D.isFunctionDeclarator() &&
1773*67e74705SXin Li       // Look at the next token to make sure that this isn't a function
1774*67e74705SXin Li       // declaration.  We have to check this because __attribute__ might be the
1775*67e74705SXin Li       // start of a function definition in GCC-extended K&R C.
1776*67e74705SXin Li       !isDeclarationAfterDeclarator()) {
1777*67e74705SXin Li 
1778*67e74705SXin Li     // Function definitions are only allowed at file scope and in C++ classes.
1779*67e74705SXin Li     // The C++ inline method definition case is handled elsewhere, so we only
1780*67e74705SXin Li     // need to handle the file scope definition case.
1781*67e74705SXin Li     if (Context == Declarator::FileContext) {
1782*67e74705SXin Li       if (isStartOfFunctionDefinition(D)) {
1783*67e74705SXin Li         if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1784*67e74705SXin Li           Diag(Tok, diag::err_function_declared_typedef);
1785*67e74705SXin Li 
1786*67e74705SXin Li           // Recover by treating the 'typedef' as spurious.
1787*67e74705SXin Li           DS.ClearStorageClassSpecs();
1788*67e74705SXin Li         }
1789*67e74705SXin Li 
1790*67e74705SXin Li         Decl *TheDecl =
1791*67e74705SXin Li           ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1792*67e74705SXin Li         return Actions.ConvertDeclToDeclGroup(TheDecl);
1793*67e74705SXin Li       }
1794*67e74705SXin Li 
1795*67e74705SXin Li       if (isDeclarationSpecifier()) {
1796*67e74705SXin Li         // If there is an invalid declaration specifier right after the
1797*67e74705SXin Li         // function prototype, then we must be in a missing semicolon case
1798*67e74705SXin Li         // where this isn't actually a body.  Just fall through into the code
1799*67e74705SXin Li         // that handles it as a prototype, and let the top-level code handle
1800*67e74705SXin Li         // the erroneous declspec where it would otherwise expect a comma or
1801*67e74705SXin Li         // semicolon.
1802*67e74705SXin Li       } else {
1803*67e74705SXin Li         Diag(Tok, diag::err_expected_fn_body);
1804*67e74705SXin Li         SkipUntil(tok::semi);
1805*67e74705SXin Li         return nullptr;
1806*67e74705SXin Li       }
1807*67e74705SXin Li     } else {
1808*67e74705SXin Li       if (Tok.is(tok::l_brace)) {
1809*67e74705SXin Li         Diag(Tok, diag::err_function_definition_not_allowed);
1810*67e74705SXin Li         SkipMalformedDecl();
1811*67e74705SXin Li         return nullptr;
1812*67e74705SXin Li       }
1813*67e74705SXin Li     }
1814*67e74705SXin Li   }
1815*67e74705SXin Li 
1816*67e74705SXin Li   if (ParseAsmAttributesAfterDeclarator(D))
1817*67e74705SXin Li     return nullptr;
1818*67e74705SXin Li 
1819*67e74705SXin Li   // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1820*67e74705SXin Li   // must parse and analyze the for-range-initializer before the declaration is
1821*67e74705SXin Li   // analyzed.
1822*67e74705SXin Li   //
1823*67e74705SXin Li   // Handle the Objective-C for-in loop variable similarly, although we
1824*67e74705SXin Li   // don't need to parse the container in advance.
1825*67e74705SXin Li   if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1826*67e74705SXin Li     bool IsForRangeLoop = false;
1827*67e74705SXin Li     if (TryConsumeToken(tok::colon, FRI->ColonLoc)) {
1828*67e74705SXin Li       IsForRangeLoop = true;
1829*67e74705SXin Li       if (Tok.is(tok::l_brace))
1830*67e74705SXin Li         FRI->RangeExpr = ParseBraceInitializer();
1831*67e74705SXin Li       else
1832*67e74705SXin Li         FRI->RangeExpr = ParseExpression();
1833*67e74705SXin Li     }
1834*67e74705SXin Li 
1835*67e74705SXin Li     Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1836*67e74705SXin Li     if (IsForRangeLoop)
1837*67e74705SXin Li       Actions.ActOnCXXForRangeDecl(ThisDecl);
1838*67e74705SXin Li     Actions.FinalizeDeclaration(ThisDecl);
1839*67e74705SXin Li     D.complete(ThisDecl);
1840*67e74705SXin Li     return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
1841*67e74705SXin Li   }
1842*67e74705SXin Li 
1843*67e74705SXin Li   SmallVector<Decl *, 8> DeclsInGroup;
1844*67e74705SXin Li   Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(
1845*67e74705SXin Li       D, ParsedTemplateInfo(), FRI);
1846*67e74705SXin Li   if (LateParsedAttrs.size() > 0)
1847*67e74705SXin Li     ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
1848*67e74705SXin Li   D.complete(FirstDecl);
1849*67e74705SXin Li   if (FirstDecl)
1850*67e74705SXin Li     DeclsInGroup.push_back(FirstDecl);
1851*67e74705SXin Li 
1852*67e74705SXin Li   bool ExpectSemi = Context != Declarator::ForContext;
1853*67e74705SXin Li 
1854*67e74705SXin Li   // If we don't have a comma, it is either the end of the list (a ';') or an
1855*67e74705SXin Li   // error, bail out.
1856*67e74705SXin Li   SourceLocation CommaLoc;
1857*67e74705SXin Li   while (TryConsumeToken(tok::comma, CommaLoc)) {
1858*67e74705SXin Li     if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1859*67e74705SXin Li       // This comma was followed by a line-break and something which can't be
1860*67e74705SXin Li       // the start of a declarator. The comma was probably a typo for a
1861*67e74705SXin Li       // semicolon.
1862*67e74705SXin Li       Diag(CommaLoc, diag::err_expected_semi_declaration)
1863*67e74705SXin Li         << FixItHint::CreateReplacement(CommaLoc, ";");
1864*67e74705SXin Li       ExpectSemi = false;
1865*67e74705SXin Li       break;
1866*67e74705SXin Li     }
1867*67e74705SXin Li 
1868*67e74705SXin Li     // Parse the next declarator.
1869*67e74705SXin Li     D.clear();
1870*67e74705SXin Li     D.setCommaLoc(CommaLoc);
1871*67e74705SXin Li 
1872*67e74705SXin Li     // Accept attributes in an init-declarator.  In the first declarator in a
1873*67e74705SXin Li     // declaration, these would be part of the declspec.  In subsequent
1874*67e74705SXin Li     // declarators, they become part of the declarator itself, so that they
1875*67e74705SXin Li     // don't apply to declarators after *this* one.  Examples:
1876*67e74705SXin Li     //    short __attribute__((common)) var;    -> declspec
1877*67e74705SXin Li     //    short var __attribute__((common));    -> declarator
1878*67e74705SXin Li     //    short x, __attribute__((common)) var;    -> declarator
1879*67e74705SXin Li     MaybeParseGNUAttributes(D);
1880*67e74705SXin Li 
1881*67e74705SXin Li     // MSVC parses but ignores qualifiers after the comma as an extension.
1882*67e74705SXin Li     if (getLangOpts().MicrosoftExt)
1883*67e74705SXin Li       DiagnoseAndSkipExtendedMicrosoftTypeAttributes();
1884*67e74705SXin Li 
1885*67e74705SXin Li     ParseDeclarator(D);
1886*67e74705SXin Li     if (!D.isInvalidType()) {
1887*67e74705SXin Li       Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1888*67e74705SXin Li       D.complete(ThisDecl);
1889*67e74705SXin Li       if (ThisDecl)
1890*67e74705SXin Li         DeclsInGroup.push_back(ThisDecl);
1891*67e74705SXin Li     }
1892*67e74705SXin Li   }
1893*67e74705SXin Li 
1894*67e74705SXin Li   if (DeclEnd)
1895*67e74705SXin Li     *DeclEnd = Tok.getLocation();
1896*67e74705SXin Li 
1897*67e74705SXin Li   if (ExpectSemi &&
1898*67e74705SXin Li       ExpectAndConsumeSemi(Context == Declarator::FileContext
1899*67e74705SXin Li                            ? diag::err_invalid_token_after_toplevel_declarator
1900*67e74705SXin Li                            : diag::err_expected_semi_declaration)) {
1901*67e74705SXin Li     // Okay, there was no semicolon and one was expected.  If we see a
1902*67e74705SXin Li     // declaration specifier, just assume it was missing and continue parsing.
1903*67e74705SXin Li     // Otherwise things are very confused and we skip to recover.
1904*67e74705SXin Li     if (!isDeclarationSpecifier()) {
1905*67e74705SXin Li       SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
1906*67e74705SXin Li       TryConsumeToken(tok::semi);
1907*67e74705SXin Li     }
1908*67e74705SXin Li   }
1909*67e74705SXin Li 
1910*67e74705SXin Li   return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
1911*67e74705SXin Li }
1912*67e74705SXin Li 
1913*67e74705SXin Li /// Parse an optional simple-asm-expr and attributes, and attach them to a
1914*67e74705SXin Li /// declarator. Returns true on an error.
ParseAsmAttributesAfterDeclarator(Declarator & D)1915*67e74705SXin Li bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
1916*67e74705SXin Li   // If a simple-asm-expr is present, parse it.
1917*67e74705SXin Li   if (Tok.is(tok::kw_asm)) {
1918*67e74705SXin Li     SourceLocation Loc;
1919*67e74705SXin Li     ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1920*67e74705SXin Li     if (AsmLabel.isInvalid()) {
1921*67e74705SXin Li       SkipUntil(tok::semi, StopBeforeMatch);
1922*67e74705SXin Li       return true;
1923*67e74705SXin Li     }
1924*67e74705SXin Li 
1925*67e74705SXin Li     D.setAsmLabel(AsmLabel.get());
1926*67e74705SXin Li     D.SetRangeEnd(Loc);
1927*67e74705SXin Li   }
1928*67e74705SXin Li 
1929*67e74705SXin Li   MaybeParseGNUAttributes(D);
1930*67e74705SXin Li   return false;
1931*67e74705SXin Li }
1932*67e74705SXin Li 
1933*67e74705SXin Li /// \brief Parse 'declaration' after parsing 'declaration-specifiers
1934*67e74705SXin Li /// declarator'. This method parses the remainder of the declaration
1935*67e74705SXin Li /// (including any attributes or initializer, among other things) and
1936*67e74705SXin Li /// finalizes the declaration.
1937*67e74705SXin Li ///
1938*67e74705SXin Li ///       init-declarator: [C99 6.7]
1939*67e74705SXin Li ///         declarator
1940*67e74705SXin Li ///         declarator '=' initializer
1941*67e74705SXin Li /// [GNU]   declarator simple-asm-expr[opt] attributes[opt]
1942*67e74705SXin Li /// [GNU]   declarator simple-asm-expr[opt] attributes[opt] '=' initializer
1943*67e74705SXin Li /// [C++]   declarator initializer[opt]
1944*67e74705SXin Li ///
1945*67e74705SXin Li /// [C++] initializer:
1946*67e74705SXin Li /// [C++]   '=' initializer-clause
1947*67e74705SXin Li /// [C++]   '(' expression-list ')'
1948*67e74705SXin Li /// [C++0x] '=' 'default'                                                [TODO]
1949*67e74705SXin Li /// [C++0x] '=' 'delete'
1950*67e74705SXin Li /// [C++0x] braced-init-list
1951*67e74705SXin Li ///
1952*67e74705SXin Li /// According to the standard grammar, =default and =delete are function
1953*67e74705SXin Li /// definitions, but that definitely doesn't fit with the parser here.
1954*67e74705SXin Li ///
ParseDeclarationAfterDeclarator(Declarator & D,const ParsedTemplateInfo & TemplateInfo)1955*67e74705SXin Li Decl *Parser::ParseDeclarationAfterDeclarator(
1956*67e74705SXin Li     Declarator &D, const ParsedTemplateInfo &TemplateInfo) {
1957*67e74705SXin Li   if (ParseAsmAttributesAfterDeclarator(D))
1958*67e74705SXin Li     return nullptr;
1959*67e74705SXin Li 
1960*67e74705SXin Li   return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1961*67e74705SXin Li }
1962*67e74705SXin Li 
ParseDeclarationAfterDeclaratorAndAttributes(Declarator & D,const ParsedTemplateInfo & TemplateInfo,ForRangeInit * FRI)1963*67e74705SXin Li Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(
1964*67e74705SXin Li     Declarator &D, const ParsedTemplateInfo &TemplateInfo, ForRangeInit *FRI) {
1965*67e74705SXin Li   // Inform the current actions module that we just parsed this declarator.
1966*67e74705SXin Li   Decl *ThisDecl = nullptr;
1967*67e74705SXin Li   switch (TemplateInfo.Kind) {
1968*67e74705SXin Li   case ParsedTemplateInfo::NonTemplate:
1969*67e74705SXin Li     ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1970*67e74705SXin Li     break;
1971*67e74705SXin Li 
1972*67e74705SXin Li   case ParsedTemplateInfo::Template:
1973*67e74705SXin Li   case ParsedTemplateInfo::ExplicitSpecialization: {
1974*67e74705SXin Li     ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
1975*67e74705SXin Li                                                *TemplateInfo.TemplateParams,
1976*67e74705SXin Li                                                D);
1977*67e74705SXin Li     if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
1978*67e74705SXin Li       // Re-direct this decl to refer to the templated decl so that we can
1979*67e74705SXin Li       // initialize it.
1980*67e74705SXin Li       ThisDecl = VT->getTemplatedDecl();
1981*67e74705SXin Li     break;
1982*67e74705SXin Li   }
1983*67e74705SXin Li   case ParsedTemplateInfo::ExplicitInstantiation: {
1984*67e74705SXin Li     if (Tok.is(tok::semi)) {
1985*67e74705SXin Li       DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1986*67e74705SXin Li           getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1987*67e74705SXin Li       if (ThisRes.isInvalid()) {
1988*67e74705SXin Li         SkipUntil(tok::semi, StopBeforeMatch);
1989*67e74705SXin Li         return nullptr;
1990*67e74705SXin Li       }
1991*67e74705SXin Li       ThisDecl = ThisRes.get();
1992*67e74705SXin Li     } else {
1993*67e74705SXin Li       // FIXME: This check should be for a variable template instantiation only.
1994*67e74705SXin Li 
1995*67e74705SXin Li       // Check that this is a valid instantiation
1996*67e74705SXin Li       if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1997*67e74705SXin Li         // If the declarator-id is not a template-id, issue a diagnostic and
1998*67e74705SXin Li         // recover by ignoring the 'template' keyword.
1999*67e74705SXin Li         Diag(Tok, diag::err_template_defn_explicit_instantiation)
2000*67e74705SXin Li             << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
2001*67e74705SXin Li         ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
2002*67e74705SXin Li       } else {
2003*67e74705SXin Li         SourceLocation LAngleLoc =
2004*67e74705SXin Li             PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
2005*67e74705SXin Li         Diag(D.getIdentifierLoc(),
2006*67e74705SXin Li              diag::err_explicit_instantiation_with_definition)
2007*67e74705SXin Li             << SourceRange(TemplateInfo.TemplateLoc)
2008*67e74705SXin Li             << FixItHint::CreateInsertion(LAngleLoc, "<>");
2009*67e74705SXin Li 
2010*67e74705SXin Li         // Recover as if it were an explicit specialization.
2011*67e74705SXin Li         TemplateParameterLists FakedParamLists;
2012*67e74705SXin Li         FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
2013*67e74705SXin Li             0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, None,
2014*67e74705SXin Li             LAngleLoc, nullptr));
2015*67e74705SXin Li 
2016*67e74705SXin Li         ThisDecl =
2017*67e74705SXin Li             Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
2018*67e74705SXin Li       }
2019*67e74705SXin Li     }
2020*67e74705SXin Li     break;
2021*67e74705SXin Li     }
2022*67e74705SXin Li   }
2023*67e74705SXin Li 
2024*67e74705SXin Li   bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
2025*67e74705SXin Li 
2026*67e74705SXin Li   // Parse declarator '=' initializer.
2027*67e74705SXin Li   // If a '==' or '+=' is found, suggest a fixit to '='.
2028*67e74705SXin Li   if (isTokenEqualOrEqualTypo()) {
2029*67e74705SXin Li     SourceLocation EqualLoc = ConsumeToken();
2030*67e74705SXin Li 
2031*67e74705SXin Li     if (Tok.is(tok::kw_delete)) {
2032*67e74705SXin Li       if (D.isFunctionDeclarator())
2033*67e74705SXin Li         Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2034*67e74705SXin Li           << 1 /* delete */;
2035*67e74705SXin Li       else
2036*67e74705SXin Li         Diag(ConsumeToken(), diag::err_deleted_non_function);
2037*67e74705SXin Li     } else if (Tok.is(tok::kw_default)) {
2038*67e74705SXin Li       if (D.isFunctionDeclarator())
2039*67e74705SXin Li         Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
2040*67e74705SXin Li           << 0 /* default */;
2041*67e74705SXin Li       else
2042*67e74705SXin Li         Diag(ConsumeToken(), diag::err_default_special_members);
2043*67e74705SXin Li     } else {
2044*67e74705SXin Li       if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
2045*67e74705SXin Li         EnterScope(0);
2046*67e74705SXin Li         Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
2047*67e74705SXin Li       }
2048*67e74705SXin Li 
2049*67e74705SXin Li       if (Tok.is(tok::code_completion)) {
2050*67e74705SXin Li         Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
2051*67e74705SXin Li         Actions.FinalizeDeclaration(ThisDecl);
2052*67e74705SXin Li         cutOffParsing();
2053*67e74705SXin Li         return nullptr;
2054*67e74705SXin Li       }
2055*67e74705SXin Li 
2056*67e74705SXin Li       ExprResult Init(ParseInitializer());
2057*67e74705SXin Li 
2058*67e74705SXin Li       // If this is the only decl in (possibly) range based for statement,
2059*67e74705SXin Li       // our best guess is that the user meant ':' instead of '='.
2060*67e74705SXin Li       if (Tok.is(tok::r_paren) && FRI && D.isFirstDeclarator()) {
2061*67e74705SXin Li         Diag(EqualLoc, diag::err_single_decl_assign_in_for_range)
2062*67e74705SXin Li             << FixItHint::CreateReplacement(EqualLoc, ":");
2063*67e74705SXin Li         // We are trying to stop parser from looking for ';' in this for
2064*67e74705SXin Li         // statement, therefore preventing spurious errors to be issued.
2065*67e74705SXin Li         FRI->ColonLoc = EqualLoc;
2066*67e74705SXin Li         Init = ExprError();
2067*67e74705SXin Li         FRI->RangeExpr = Init;
2068*67e74705SXin Li       }
2069*67e74705SXin Li 
2070*67e74705SXin Li       if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
2071*67e74705SXin Li         Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
2072*67e74705SXin Li         ExitScope();
2073*67e74705SXin Li       }
2074*67e74705SXin Li 
2075*67e74705SXin Li       if (Init.isInvalid()) {
2076*67e74705SXin Li         SmallVector<tok::TokenKind, 2> StopTokens;
2077*67e74705SXin Li         StopTokens.push_back(tok::comma);
2078*67e74705SXin Li         if (D.getContext() == Declarator::ForContext ||
2079*67e74705SXin Li             D.getContext() == Declarator::InitStmtContext)
2080*67e74705SXin Li           StopTokens.push_back(tok::r_paren);
2081*67e74705SXin Li         SkipUntil(StopTokens, StopAtSemi | StopBeforeMatch);
2082*67e74705SXin Li         Actions.ActOnInitializerError(ThisDecl);
2083*67e74705SXin Li       } else
2084*67e74705SXin Li         Actions.AddInitializerToDecl(ThisDecl, Init.get(),
2085*67e74705SXin Li                                      /*DirectInit=*/false, TypeContainsAuto);
2086*67e74705SXin Li     }
2087*67e74705SXin Li   } else if (Tok.is(tok::l_paren)) {
2088*67e74705SXin Li     // Parse C++ direct initializer: '(' expression-list ')'
2089*67e74705SXin Li     BalancedDelimiterTracker T(*this, tok::l_paren);
2090*67e74705SXin Li     T.consumeOpen();
2091*67e74705SXin Li 
2092*67e74705SXin Li     ExprVector Exprs;
2093*67e74705SXin Li     CommaLocsTy CommaLocs;
2094*67e74705SXin Li 
2095*67e74705SXin Li     if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
2096*67e74705SXin Li       EnterScope(0);
2097*67e74705SXin Li       Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
2098*67e74705SXin Li     }
2099*67e74705SXin Li 
2100*67e74705SXin Li     if (ParseExpressionList(Exprs, CommaLocs, [&] {
2101*67e74705SXin Li           Actions.CodeCompleteConstructor(getCurScope(),
2102*67e74705SXin Li                  cast<VarDecl>(ThisDecl)->getType()->getCanonicalTypeInternal(),
2103*67e74705SXin Li                                           ThisDecl->getLocation(), Exprs);
2104*67e74705SXin Li        })) {
2105*67e74705SXin Li       Actions.ActOnInitializerError(ThisDecl);
2106*67e74705SXin Li       SkipUntil(tok::r_paren, StopAtSemi);
2107*67e74705SXin Li 
2108*67e74705SXin Li       if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
2109*67e74705SXin Li         Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
2110*67e74705SXin Li         ExitScope();
2111*67e74705SXin Li       }
2112*67e74705SXin Li     } else {
2113*67e74705SXin Li       // Match the ')'.
2114*67e74705SXin Li       T.consumeClose();
2115*67e74705SXin Li 
2116*67e74705SXin Li       assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
2117*67e74705SXin Li              "Unexpected number of commas!");
2118*67e74705SXin Li 
2119*67e74705SXin Li       if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
2120*67e74705SXin Li         Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
2121*67e74705SXin Li         ExitScope();
2122*67e74705SXin Li       }
2123*67e74705SXin Li 
2124*67e74705SXin Li       ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
2125*67e74705SXin Li                                                           T.getCloseLocation(),
2126*67e74705SXin Li                                                           Exprs);
2127*67e74705SXin Li       Actions.AddInitializerToDecl(ThisDecl, Initializer.get(),
2128*67e74705SXin Li                                    /*DirectInit=*/true, TypeContainsAuto);
2129*67e74705SXin Li     }
2130*67e74705SXin Li   } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
2131*67e74705SXin Li              (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
2132*67e74705SXin Li     // Parse C++0x braced-init-list.
2133*67e74705SXin Li     Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
2134*67e74705SXin Li 
2135*67e74705SXin Li     if (D.getCXXScopeSpec().isSet()) {
2136*67e74705SXin Li       EnterScope(0);
2137*67e74705SXin Li       Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
2138*67e74705SXin Li     }
2139*67e74705SXin Li 
2140*67e74705SXin Li     ExprResult Init(ParseBraceInitializer());
2141*67e74705SXin Li 
2142*67e74705SXin Li     if (D.getCXXScopeSpec().isSet()) {
2143*67e74705SXin Li       Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
2144*67e74705SXin Li       ExitScope();
2145*67e74705SXin Li     }
2146*67e74705SXin Li 
2147*67e74705SXin Li     if (Init.isInvalid()) {
2148*67e74705SXin Li       Actions.ActOnInitializerError(ThisDecl);
2149*67e74705SXin Li     } else
2150*67e74705SXin Li       Actions.AddInitializerToDecl(ThisDecl, Init.get(),
2151*67e74705SXin Li                                    /*DirectInit=*/true, TypeContainsAuto);
2152*67e74705SXin Li 
2153*67e74705SXin Li   } else {
2154*67e74705SXin Li     Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
2155*67e74705SXin Li   }
2156*67e74705SXin Li 
2157*67e74705SXin Li   Actions.FinalizeDeclaration(ThisDecl);
2158*67e74705SXin Li 
2159*67e74705SXin Li   return ThisDecl;
2160*67e74705SXin Li }
2161*67e74705SXin Li 
2162*67e74705SXin Li /// ParseSpecifierQualifierList
2163*67e74705SXin Li ///        specifier-qualifier-list:
2164*67e74705SXin Li ///          type-specifier specifier-qualifier-list[opt]
2165*67e74705SXin Li ///          type-qualifier specifier-qualifier-list[opt]
2166*67e74705SXin Li /// [GNU]    attributes     specifier-qualifier-list[opt]
2167*67e74705SXin Li ///
ParseSpecifierQualifierList(DeclSpec & DS,AccessSpecifier AS,DeclSpecContext DSC)2168*67e74705SXin Li void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
2169*67e74705SXin Li                                          DeclSpecContext DSC) {
2170*67e74705SXin Li   /// specifier-qualifier-list is a subset of declaration-specifiers.  Just
2171*67e74705SXin Li   /// parse declaration-specifiers and complain about extra stuff.
2172*67e74705SXin Li   /// TODO: diagnose attribute-specifiers and alignment-specifiers.
2173*67e74705SXin Li   ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
2174*67e74705SXin Li 
2175*67e74705SXin Li   // Validate declspec for type-name.
2176*67e74705SXin Li   unsigned Specs = DS.getParsedSpecifiers();
2177*67e74705SXin Li   if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) {
2178*67e74705SXin Li     Diag(Tok, diag::err_expected_type);
2179*67e74705SXin Li     DS.SetTypeSpecError();
2180*67e74705SXin Li   } else if (Specs == DeclSpec::PQ_None && !DS.hasAttributes()) {
2181*67e74705SXin Li     Diag(Tok, diag::err_typename_requires_specqual);
2182*67e74705SXin Li     if (!DS.hasTypeSpecifier())
2183*67e74705SXin Li       DS.SetTypeSpecError();
2184*67e74705SXin Li   }
2185*67e74705SXin Li 
2186*67e74705SXin Li   // Issue diagnostic and remove storage class if present.
2187*67e74705SXin Li   if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
2188*67e74705SXin Li     if (DS.getStorageClassSpecLoc().isValid())
2189*67e74705SXin Li       Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
2190*67e74705SXin Li     else
2191*67e74705SXin Li       Diag(DS.getThreadStorageClassSpecLoc(),
2192*67e74705SXin Li            diag::err_typename_invalid_storageclass);
2193*67e74705SXin Li     DS.ClearStorageClassSpecs();
2194*67e74705SXin Li   }
2195*67e74705SXin Li 
2196*67e74705SXin Li   // Issue diagnostic and remove function specifier if present.
2197*67e74705SXin Li   if (Specs & DeclSpec::PQ_FunctionSpecifier) {
2198*67e74705SXin Li     if (DS.isInlineSpecified())
2199*67e74705SXin Li       Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2200*67e74705SXin Li     if (DS.isVirtualSpecified())
2201*67e74705SXin Li       Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2202*67e74705SXin Li     if (DS.isExplicitSpecified())
2203*67e74705SXin Li       Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
2204*67e74705SXin Li     DS.ClearFunctionSpecs();
2205*67e74705SXin Li   }
2206*67e74705SXin Li 
2207*67e74705SXin Li   // Issue diagnostic and remove constexpr specfier if present.
2208*67e74705SXin Li   if (DS.isConstexprSpecified() && DSC != DSC_condition) {
2209*67e74705SXin Li     Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2210*67e74705SXin Li     DS.ClearConstexprSpec();
2211*67e74705SXin Li   }
2212*67e74705SXin Li }
2213*67e74705SXin Li 
2214*67e74705SXin Li /// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2215*67e74705SXin Li /// specified token is valid after the identifier in a declarator which
2216*67e74705SXin Li /// immediately follows the declspec.  For example, these things are valid:
2217*67e74705SXin Li ///
2218*67e74705SXin Li ///      int x   [             4];         // direct-declarator
2219*67e74705SXin Li ///      int x   (             int y);     // direct-declarator
2220*67e74705SXin Li ///  int(int x   )                         // direct-declarator
2221*67e74705SXin Li ///      int x   ;                         // simple-declaration
2222*67e74705SXin Li ///      int x   =             17;         // init-declarator-list
2223*67e74705SXin Li ///      int x   ,             y;          // init-declarator-list
2224*67e74705SXin Li ///      int x   __asm__       ("foo");    // init-declarator-list
2225*67e74705SXin Li ///      int x   :             4;          // struct-declarator
2226*67e74705SXin Li ///      int x   {             5};         // C++'0x unified initializers
2227*67e74705SXin Li ///
2228*67e74705SXin Li /// This is not, because 'x' does not immediately follow the declspec (though
2229*67e74705SXin Li /// ')' happens to be valid anyway).
2230*67e74705SXin Li ///    int (x)
2231*67e74705SXin Li ///
isValidAfterIdentifierInDeclarator(const Token & T)2232*67e74705SXin Li static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2233*67e74705SXin Li   return T.isOneOf(tok::l_square, tok::l_paren, tok::r_paren, tok::semi,
2234*67e74705SXin Li                    tok::comma, tok::equal, tok::kw_asm, tok::l_brace,
2235*67e74705SXin Li                    tok::colon);
2236*67e74705SXin Li }
2237*67e74705SXin Li 
2238*67e74705SXin Li /// ParseImplicitInt - This method is called when we have an non-typename
2239*67e74705SXin Li /// identifier in a declspec (which normally terminates the decl spec) when
2240*67e74705SXin Li /// the declspec has no type specifier.  In this case, the declspec is either
2241*67e74705SXin Li /// malformed or is "implicit int" (in K&R and C89).
2242*67e74705SXin Li ///
2243*67e74705SXin Li /// This method handles diagnosing this prettily and returns false if the
2244*67e74705SXin Li /// declspec is done being processed.  If it recovers and thinks there may be
2245*67e74705SXin Li /// other pieces of declspec after it, it returns true.
2246*67e74705SXin Li ///
ParseImplicitInt(DeclSpec & DS,CXXScopeSpec * SS,const ParsedTemplateInfo & TemplateInfo,AccessSpecifier AS,DeclSpecContext DSC,ParsedAttributesWithRange & Attrs)2247*67e74705SXin Li bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
2248*67e74705SXin Li                               const ParsedTemplateInfo &TemplateInfo,
2249*67e74705SXin Li                               AccessSpecifier AS, DeclSpecContext DSC,
2250*67e74705SXin Li                               ParsedAttributesWithRange &Attrs) {
2251*67e74705SXin Li   assert(Tok.is(tok::identifier) && "should have identifier");
2252*67e74705SXin Li 
2253*67e74705SXin Li   SourceLocation Loc = Tok.getLocation();
2254*67e74705SXin Li   // If we see an identifier that is not a type name, we normally would
2255*67e74705SXin Li   // parse it as the identifer being declared.  However, when a typename
2256*67e74705SXin Li   // is typo'd or the definition is not included, this will incorrectly
2257*67e74705SXin Li   // parse the typename as the identifier name and fall over misparsing
2258*67e74705SXin Li   // later parts of the diagnostic.
2259*67e74705SXin Li   //
2260*67e74705SXin Li   // As such, we try to do some look-ahead in cases where this would
2261*67e74705SXin Li   // otherwise be an "implicit-int" case to see if this is invalid.  For
2262*67e74705SXin Li   // example: "static foo_t x = 4;"  In this case, if we parsed foo_t as
2263*67e74705SXin Li   // an identifier with implicit int, we'd get a parse error because the
2264*67e74705SXin Li   // next token is obviously invalid for a type.  Parse these as a case
2265*67e74705SXin Li   // with an invalid type specifier.
2266*67e74705SXin Li   assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
2267*67e74705SXin Li 
2268*67e74705SXin Li   // Since we know that this either implicit int (which is rare) or an
2269*67e74705SXin Li   // error, do lookahead to try to do better recovery. This never applies
2270*67e74705SXin Li   // within a type specifier. Outside of C++, we allow this even if the
2271*67e74705SXin Li   // language doesn't "officially" support implicit int -- we support
2272*67e74705SXin Li   // implicit int as an extension in C99 and C11.
2273*67e74705SXin Li   if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus &&
2274*67e74705SXin Li       isValidAfterIdentifierInDeclarator(NextToken())) {
2275*67e74705SXin Li     // If this token is valid for implicit int, e.g. "static x = 4", then
2276*67e74705SXin Li     // we just avoid eating the identifier, so it will be parsed as the
2277*67e74705SXin Li     // identifier in the declarator.
2278*67e74705SXin Li     return false;
2279*67e74705SXin Li   }
2280*67e74705SXin Li 
2281*67e74705SXin Li   if (getLangOpts().CPlusPlus &&
2282*67e74705SXin Li       DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2283*67e74705SXin Li     // Don't require a type specifier if we have the 'auto' storage class
2284*67e74705SXin Li     // specifier in C++98 -- we'll promote it to a type specifier.
2285*67e74705SXin Li     if (SS)
2286*67e74705SXin Li       AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2287*67e74705SXin Li     return false;
2288*67e74705SXin Li   }
2289*67e74705SXin Li 
2290*67e74705SXin Li   if (getLangOpts().CPlusPlus && (!SS || SS->isEmpty()) &&
2291*67e74705SXin Li       getLangOpts().MSVCCompat) {
2292*67e74705SXin Li     // Lookup of an unqualified type name has failed in MSVC compatibility mode.
2293*67e74705SXin Li     // Give Sema a chance to recover if we are in a template with dependent base
2294*67e74705SXin Li     // classes.
2295*67e74705SXin Li     if (ParsedType T = Actions.ActOnMSVCUnknownTypeName(
2296*67e74705SXin Li             *Tok.getIdentifierInfo(), Tok.getLocation(),
2297*67e74705SXin Li             DSC == DSC_template_type_arg)) {
2298*67e74705SXin Li       const char *PrevSpec;
2299*67e74705SXin Li       unsigned DiagID;
2300*67e74705SXin Li       DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
2301*67e74705SXin Li                          Actions.getASTContext().getPrintingPolicy());
2302*67e74705SXin Li       DS.SetRangeEnd(Tok.getLocation());
2303*67e74705SXin Li       ConsumeToken();
2304*67e74705SXin Li       return false;
2305*67e74705SXin Li     }
2306*67e74705SXin Li   }
2307*67e74705SXin Li 
2308*67e74705SXin Li   // Otherwise, if we don't consume this token, we are going to emit an
2309*67e74705SXin Li   // error anyway.  Try to recover from various common problems.  Check
2310*67e74705SXin Li   // to see if this was a reference to a tag name without a tag specified.
2311*67e74705SXin Li   // This is a common problem in C (saying 'foo' instead of 'struct foo').
2312*67e74705SXin Li   //
2313*67e74705SXin Li   // C++ doesn't need this, and isTagName doesn't take SS.
2314*67e74705SXin Li   if (SS == nullptr) {
2315*67e74705SXin Li     const char *TagName = nullptr, *FixitTagName = nullptr;
2316*67e74705SXin Li     tok::TokenKind TagKind = tok::unknown;
2317*67e74705SXin Li 
2318*67e74705SXin Li     switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
2319*67e74705SXin Li       default: break;
2320*67e74705SXin Li       case DeclSpec::TST_enum:
2321*67e74705SXin Li         TagName="enum"  ; FixitTagName = "enum "  ; TagKind=tok::kw_enum ;break;
2322*67e74705SXin Li       case DeclSpec::TST_union:
2323*67e74705SXin Li         TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2324*67e74705SXin Li       case DeclSpec::TST_struct:
2325*67e74705SXin Li         TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
2326*67e74705SXin Li       case DeclSpec::TST_interface:
2327*67e74705SXin Li         TagName="__interface"; FixitTagName = "__interface ";
2328*67e74705SXin Li         TagKind=tok::kw___interface;break;
2329*67e74705SXin Li       case DeclSpec::TST_class:
2330*67e74705SXin Li         TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
2331*67e74705SXin Li     }
2332*67e74705SXin Li 
2333*67e74705SXin Li     if (TagName) {
2334*67e74705SXin Li       IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2335*67e74705SXin Li       LookupResult R(Actions, TokenName, SourceLocation(),
2336*67e74705SXin Li                      Sema::LookupOrdinaryName);
2337*67e74705SXin Li 
2338*67e74705SXin Li       Diag(Loc, diag::err_use_of_tag_name_without_tag)
2339*67e74705SXin Li         << TokenName << TagName << getLangOpts().CPlusPlus
2340*67e74705SXin Li         << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2341*67e74705SXin Li 
2342*67e74705SXin Li       if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2343*67e74705SXin Li         for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2344*67e74705SXin Li              I != IEnd; ++I)
2345*67e74705SXin Li           Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
2346*67e74705SXin Li             << TokenName << TagName;
2347*67e74705SXin Li       }
2348*67e74705SXin Li 
2349*67e74705SXin Li       // Parse this as a tag as if the missing tag were present.
2350*67e74705SXin Li       if (TagKind == tok::kw_enum)
2351*67e74705SXin Li         ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
2352*67e74705SXin Li       else
2353*67e74705SXin Li         ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
2354*67e74705SXin Li                             /*EnteringContext*/ false, DSC_normal, Attrs);
2355*67e74705SXin Li       return true;
2356*67e74705SXin Li     }
2357*67e74705SXin Li   }
2358*67e74705SXin Li 
2359*67e74705SXin Li   // Determine whether this identifier could plausibly be the name of something
2360*67e74705SXin Li   // being declared (with a missing type).
2361*67e74705SXin Li   if (!isTypeSpecifier(DSC) &&
2362*67e74705SXin Li       (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
2363*67e74705SXin Li     // Look ahead to the next token to try to figure out what this declaration
2364*67e74705SXin Li     // was supposed to be.
2365*67e74705SXin Li     switch (NextToken().getKind()) {
2366*67e74705SXin Li     case tok::l_paren: {
2367*67e74705SXin Li       // static x(4); // 'x' is not a type
2368*67e74705SXin Li       // x(int n);    // 'x' is not a type
2369*67e74705SXin Li       // x (*p)[];    // 'x' is a type
2370*67e74705SXin Li       //
2371*67e74705SXin Li       // Since we're in an error case, we can afford to perform a tentative
2372*67e74705SXin Li       // parse to determine which case we're in.
2373*67e74705SXin Li       TentativeParsingAction PA(*this);
2374*67e74705SXin Li       ConsumeToken();
2375*67e74705SXin Li       TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2376*67e74705SXin Li       PA.Revert();
2377*67e74705SXin Li 
2378*67e74705SXin Li       if (TPR != TPResult::False) {
2379*67e74705SXin Li         // The identifier is followed by a parenthesized declarator.
2380*67e74705SXin Li         // It's supposed to be a type.
2381*67e74705SXin Li         break;
2382*67e74705SXin Li       }
2383*67e74705SXin Li 
2384*67e74705SXin Li       // If we're in a context where we could be declaring a constructor,
2385*67e74705SXin Li       // check whether this is a constructor declaration with a bogus name.
2386*67e74705SXin Li       if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2387*67e74705SXin Li         IdentifierInfo *II = Tok.getIdentifierInfo();
2388*67e74705SXin Li         if (Actions.isCurrentClassNameTypo(II, SS)) {
2389*67e74705SXin Li           Diag(Loc, diag::err_constructor_bad_name)
2390*67e74705SXin Li             << Tok.getIdentifierInfo() << II
2391*67e74705SXin Li             << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2392*67e74705SXin Li           Tok.setIdentifierInfo(II);
2393*67e74705SXin Li         }
2394*67e74705SXin Li       }
2395*67e74705SXin Li       // Fall through.
2396*67e74705SXin Li     }
2397*67e74705SXin Li     case tok::comma:
2398*67e74705SXin Li     case tok::equal:
2399*67e74705SXin Li     case tok::kw_asm:
2400*67e74705SXin Li     case tok::l_brace:
2401*67e74705SXin Li     case tok::l_square:
2402*67e74705SXin Li     case tok::semi:
2403*67e74705SXin Li       // This looks like a variable or function declaration. The type is
2404*67e74705SXin Li       // probably missing. We're done parsing decl-specifiers.
2405*67e74705SXin Li       if (SS)
2406*67e74705SXin Li         AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2407*67e74705SXin Li       return false;
2408*67e74705SXin Li 
2409*67e74705SXin Li     default:
2410*67e74705SXin Li       // This is probably supposed to be a type. This includes cases like:
2411*67e74705SXin Li       //   int f(itn);
2412*67e74705SXin Li       //   struct S { unsinged : 4; };
2413*67e74705SXin Li       break;
2414*67e74705SXin Li     }
2415*67e74705SXin Li   }
2416*67e74705SXin Li 
2417*67e74705SXin Li   // This is almost certainly an invalid type name. Let Sema emit a diagnostic
2418*67e74705SXin Li   // and attempt to recover.
2419*67e74705SXin Li   ParsedType T;
2420*67e74705SXin Li   IdentifierInfo *II = Tok.getIdentifierInfo();
2421*67e74705SXin Li   Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T,
2422*67e74705SXin Li                                   getLangOpts().CPlusPlus &&
2423*67e74705SXin Li                                       NextToken().is(tok::less));
2424*67e74705SXin Li   if (T) {
2425*67e74705SXin Li     // The action has suggested that the type T could be used. Set that as
2426*67e74705SXin Li     // the type in the declaration specifiers, consume the would-be type
2427*67e74705SXin Li     // name token, and we're done.
2428*67e74705SXin Li     const char *PrevSpec;
2429*67e74705SXin Li     unsigned DiagID;
2430*67e74705SXin Li     DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
2431*67e74705SXin Li                        Actions.getASTContext().getPrintingPolicy());
2432*67e74705SXin Li     DS.SetRangeEnd(Tok.getLocation());
2433*67e74705SXin Li     ConsumeToken();
2434*67e74705SXin Li     // There may be other declaration specifiers after this.
2435*67e74705SXin Li     return true;
2436*67e74705SXin Li   } else if (II != Tok.getIdentifierInfo()) {
2437*67e74705SXin Li     // If no type was suggested, the correction is to a keyword
2438*67e74705SXin Li     Tok.setKind(II->getTokenID());
2439*67e74705SXin Li     // There may be other declaration specifiers after this.
2440*67e74705SXin Li     return true;
2441*67e74705SXin Li   }
2442*67e74705SXin Li 
2443*67e74705SXin Li   // Otherwise, the action had no suggestion for us.  Mark this as an error.
2444*67e74705SXin Li   DS.SetTypeSpecError();
2445*67e74705SXin Li   DS.SetRangeEnd(Tok.getLocation());
2446*67e74705SXin Li   ConsumeToken();
2447*67e74705SXin Li 
2448*67e74705SXin Li   // TODO: Could inject an invalid typedef decl in an enclosing scope to
2449*67e74705SXin Li   // avoid rippling error messages on subsequent uses of the same type,
2450*67e74705SXin Li   // could be useful if #include was forgotten.
2451*67e74705SXin Li   return false;
2452*67e74705SXin Li }
2453*67e74705SXin Li 
2454*67e74705SXin Li /// \brief Determine the declaration specifier context from the declarator
2455*67e74705SXin Li /// context.
2456*67e74705SXin Li ///
2457*67e74705SXin Li /// \param Context the declarator context, which is one of the
2458*67e74705SXin Li /// Declarator::TheContext enumerator values.
2459*67e74705SXin Li Parser::DeclSpecContext
getDeclSpecContextFromDeclaratorContext(unsigned Context)2460*67e74705SXin Li Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2461*67e74705SXin Li   if (Context == Declarator::MemberContext)
2462*67e74705SXin Li     return DSC_class;
2463*67e74705SXin Li   if (Context == Declarator::FileContext)
2464*67e74705SXin Li     return DSC_top_level;
2465*67e74705SXin Li   if (Context == Declarator::TemplateTypeArgContext)
2466*67e74705SXin Li     return DSC_template_type_arg;
2467*67e74705SXin Li   if (Context == Declarator::TrailingReturnContext)
2468*67e74705SXin Li     return DSC_trailing;
2469*67e74705SXin Li   if (Context == Declarator::AliasDeclContext ||
2470*67e74705SXin Li       Context == Declarator::AliasTemplateContext)
2471*67e74705SXin Li     return DSC_alias_declaration;
2472*67e74705SXin Li   return DSC_normal;
2473*67e74705SXin Li }
2474*67e74705SXin Li 
2475*67e74705SXin Li /// ParseAlignArgument - Parse the argument to an alignment-specifier.
2476*67e74705SXin Li ///
2477*67e74705SXin Li /// FIXME: Simply returns an alignof() expression if the argument is a
2478*67e74705SXin Li /// type. Ideally, the type should be propagated directly into Sema.
2479*67e74705SXin Li ///
2480*67e74705SXin Li /// [C11]   type-id
2481*67e74705SXin Li /// [C11]   constant-expression
2482*67e74705SXin Li /// [C++0x] type-id ...[opt]
2483*67e74705SXin Li /// [C++0x] assignment-expression ...[opt]
ParseAlignArgument(SourceLocation Start,SourceLocation & EllipsisLoc)2484*67e74705SXin Li ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2485*67e74705SXin Li                                       SourceLocation &EllipsisLoc) {
2486*67e74705SXin Li   ExprResult ER;
2487*67e74705SXin Li   if (isTypeIdInParens()) {
2488*67e74705SXin Li     SourceLocation TypeLoc = Tok.getLocation();
2489*67e74705SXin Li     ParsedType Ty = ParseTypeName().get();
2490*67e74705SXin Li     SourceRange TypeRange(Start, Tok.getLocation());
2491*67e74705SXin Li     ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2492*67e74705SXin Li                                                Ty.getAsOpaquePtr(), TypeRange);
2493*67e74705SXin Li   } else
2494*67e74705SXin Li     ER = ParseConstantExpression();
2495*67e74705SXin Li 
2496*67e74705SXin Li   if (getLangOpts().CPlusPlus11)
2497*67e74705SXin Li     TryConsumeToken(tok::ellipsis, EllipsisLoc);
2498*67e74705SXin Li 
2499*67e74705SXin Li   return ER;
2500*67e74705SXin Li }
2501*67e74705SXin Li 
2502*67e74705SXin Li /// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2503*67e74705SXin Li /// attribute to Attrs.
2504*67e74705SXin Li ///
2505*67e74705SXin Li /// alignment-specifier:
2506*67e74705SXin Li /// [C11]   '_Alignas' '(' type-id ')'
2507*67e74705SXin Li /// [C11]   '_Alignas' '(' constant-expression ')'
2508*67e74705SXin Li /// [C++11] 'alignas' '(' type-id ...[opt] ')'
2509*67e74705SXin Li /// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
ParseAlignmentSpecifier(ParsedAttributes & Attrs,SourceLocation * EndLoc)2510*67e74705SXin Li void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2511*67e74705SXin Li                                      SourceLocation *EndLoc) {
2512*67e74705SXin Li   assert(Tok.isOneOf(tok::kw_alignas, tok::kw__Alignas) &&
2513*67e74705SXin Li          "Not an alignment-specifier!");
2514*67e74705SXin Li 
2515*67e74705SXin Li   IdentifierInfo *KWName = Tok.getIdentifierInfo();
2516*67e74705SXin Li   SourceLocation KWLoc = ConsumeToken();
2517*67e74705SXin Li 
2518*67e74705SXin Li   BalancedDelimiterTracker T(*this, tok::l_paren);
2519*67e74705SXin Li   if (T.expectAndConsume())
2520*67e74705SXin Li     return;
2521*67e74705SXin Li 
2522*67e74705SXin Li   SourceLocation EllipsisLoc;
2523*67e74705SXin Li   ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
2524*67e74705SXin Li   if (ArgExpr.isInvalid()) {
2525*67e74705SXin Li     T.skipToEnd();
2526*67e74705SXin Li     return;
2527*67e74705SXin Li   }
2528*67e74705SXin Li 
2529*67e74705SXin Li   T.consumeClose();
2530*67e74705SXin Li   if (EndLoc)
2531*67e74705SXin Li     *EndLoc = T.getCloseLocation();
2532*67e74705SXin Li 
2533*67e74705SXin Li   ArgsVector ArgExprs;
2534*67e74705SXin Li   ArgExprs.push_back(ArgExpr.get());
2535*67e74705SXin Li   Attrs.addNew(KWName, KWLoc, nullptr, KWLoc, ArgExprs.data(), 1,
2536*67e74705SXin Li                AttributeList::AS_Keyword, EllipsisLoc);
2537*67e74705SXin Li }
2538*67e74705SXin Li 
2539*67e74705SXin Li /// Determine whether we're looking at something that might be a declarator
2540*67e74705SXin Li /// in a simple-declaration. If it can't possibly be a declarator, maybe
2541*67e74705SXin Li /// diagnose a missing semicolon after a prior tag definition in the decl
2542*67e74705SXin Li /// specifier.
2543*67e74705SXin Li ///
2544*67e74705SXin Li /// \return \c true if an error occurred and this can't be any kind of
2545*67e74705SXin Li /// declaration.
2546*67e74705SXin Li bool
DiagnoseMissingSemiAfterTagDefinition(DeclSpec & DS,AccessSpecifier AS,DeclSpecContext DSContext,LateParsedAttrList * LateAttrs)2547*67e74705SXin Li Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2548*67e74705SXin Li                                               DeclSpecContext DSContext,
2549*67e74705SXin Li                                               LateParsedAttrList *LateAttrs) {
2550*67e74705SXin Li   assert(DS.hasTagDefinition() && "shouldn't call this");
2551*67e74705SXin Li 
2552*67e74705SXin Li   bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
2553*67e74705SXin Li 
2554*67e74705SXin Li   if (getLangOpts().CPlusPlus &&
2555*67e74705SXin Li       Tok.isOneOf(tok::identifier, tok::coloncolon, tok::kw_decltype,
2556*67e74705SXin Li                   tok::annot_template_id) &&
2557*67e74705SXin Li       TryAnnotateCXXScopeToken(EnteringContext)) {
2558*67e74705SXin Li     SkipMalformedDecl();
2559*67e74705SXin Li     return true;
2560*67e74705SXin Li   }
2561*67e74705SXin Li 
2562*67e74705SXin Li   bool HasScope = Tok.is(tok::annot_cxxscope);
2563*67e74705SXin Li   // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2564*67e74705SXin Li   Token AfterScope = HasScope ? NextToken() : Tok;
2565*67e74705SXin Li 
2566*67e74705SXin Li   // Determine whether the following tokens could possibly be a
2567*67e74705SXin Li   // declarator.
2568*67e74705SXin Li   bool MightBeDeclarator = true;
2569*67e74705SXin Li   if (Tok.isOneOf(tok::kw_typename, tok::annot_typename)) {
2570*67e74705SXin Li     // A declarator-id can't start with 'typename'.
2571*67e74705SXin Li     MightBeDeclarator = false;
2572*67e74705SXin Li   } else if (AfterScope.is(tok::annot_template_id)) {
2573*67e74705SXin Li     // If we have a type expressed as a template-id, this cannot be a
2574*67e74705SXin Li     // declarator-id (such a type cannot be redeclared in a simple-declaration).
2575*67e74705SXin Li     TemplateIdAnnotation *Annot =
2576*67e74705SXin Li         static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2577*67e74705SXin Li     if (Annot->Kind == TNK_Type_template)
2578*67e74705SXin Li       MightBeDeclarator = false;
2579*67e74705SXin Li   } else if (AfterScope.is(tok::identifier)) {
2580*67e74705SXin Li     const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2581*67e74705SXin Li 
2582*67e74705SXin Li     // These tokens cannot come after the declarator-id in a
2583*67e74705SXin Li     // simple-declaration, and are likely to come after a type-specifier.
2584*67e74705SXin Li     if (Next.isOneOf(tok::star, tok::amp, tok::ampamp, tok::identifier,
2585*67e74705SXin Li                      tok::annot_cxxscope, tok::coloncolon)) {
2586*67e74705SXin Li       // Missing a semicolon.
2587*67e74705SXin Li       MightBeDeclarator = false;
2588*67e74705SXin Li     } else if (HasScope) {
2589*67e74705SXin Li       // If the declarator-id has a scope specifier, it must redeclare a
2590*67e74705SXin Li       // previously-declared entity. If that's a type (and this is not a
2591*67e74705SXin Li       // typedef), that's an error.
2592*67e74705SXin Li       CXXScopeSpec SS;
2593*67e74705SXin Li       Actions.RestoreNestedNameSpecifierAnnotation(
2594*67e74705SXin Li           Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2595*67e74705SXin Li       IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2596*67e74705SXin Li       Sema::NameClassification Classification = Actions.ClassifyName(
2597*67e74705SXin Li           getCurScope(), SS, Name, AfterScope.getLocation(), Next,
2598*67e74705SXin Li           /*IsAddressOfOperand*/false);
2599*67e74705SXin Li       switch (Classification.getKind()) {
2600*67e74705SXin Li       case Sema::NC_Error:
2601*67e74705SXin Li         SkipMalformedDecl();
2602*67e74705SXin Li         return true;
2603*67e74705SXin Li 
2604*67e74705SXin Li       case Sema::NC_Keyword:
2605*67e74705SXin Li       case Sema::NC_NestedNameSpecifier:
2606*67e74705SXin Li         llvm_unreachable("typo correction and nested name specifiers not "
2607*67e74705SXin Li                          "possible here");
2608*67e74705SXin Li 
2609*67e74705SXin Li       case Sema::NC_Type:
2610*67e74705SXin Li       case Sema::NC_TypeTemplate:
2611*67e74705SXin Li         // Not a previously-declared non-type entity.
2612*67e74705SXin Li         MightBeDeclarator = false;
2613*67e74705SXin Li         break;
2614*67e74705SXin Li 
2615*67e74705SXin Li       case Sema::NC_Unknown:
2616*67e74705SXin Li       case Sema::NC_Expression:
2617*67e74705SXin Li       case Sema::NC_VarTemplate:
2618*67e74705SXin Li       case Sema::NC_FunctionTemplate:
2619*67e74705SXin Li         // Might be a redeclaration of a prior entity.
2620*67e74705SXin Li         break;
2621*67e74705SXin Li       }
2622*67e74705SXin Li     }
2623*67e74705SXin Li   }
2624*67e74705SXin Li 
2625*67e74705SXin Li   if (MightBeDeclarator)
2626*67e74705SXin Li     return false;
2627*67e74705SXin Li 
2628*67e74705SXin Li   const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
2629*67e74705SXin Li   Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()),
2630*67e74705SXin Li        diag::err_expected_after)
2631*67e74705SXin Li       << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi;
2632*67e74705SXin Li 
2633*67e74705SXin Li   // Try to recover from the typo, by dropping the tag definition and parsing
2634*67e74705SXin Li   // the problematic tokens as a type.
2635*67e74705SXin Li   //
2636*67e74705SXin Li   // FIXME: Split the DeclSpec into pieces for the standalone
2637*67e74705SXin Li   // declaration and pieces for the following declaration, instead
2638*67e74705SXin Li   // of assuming that all the other pieces attach to new declaration,
2639*67e74705SXin Li   // and call ParsedFreeStandingDeclSpec as appropriate.
2640*67e74705SXin Li   DS.ClearTypeSpecType();
2641*67e74705SXin Li   ParsedTemplateInfo NotATemplate;
2642*67e74705SXin Li   ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
2643*67e74705SXin Li   return false;
2644*67e74705SXin Li }
2645*67e74705SXin Li 
2646*67e74705SXin Li /// ParseDeclarationSpecifiers
2647*67e74705SXin Li ///       declaration-specifiers: [C99 6.7]
2648*67e74705SXin Li ///         storage-class-specifier declaration-specifiers[opt]
2649*67e74705SXin Li ///         type-specifier declaration-specifiers[opt]
2650*67e74705SXin Li /// [C99]   function-specifier declaration-specifiers[opt]
2651*67e74705SXin Li /// [C11]   alignment-specifier declaration-specifiers[opt]
2652*67e74705SXin Li /// [GNU]   attributes declaration-specifiers[opt]
2653*67e74705SXin Li /// [Clang] '__module_private__' declaration-specifiers[opt]
2654*67e74705SXin Li /// [ObjC1] '__kindof' declaration-specifiers[opt]
2655*67e74705SXin Li ///
2656*67e74705SXin Li ///       storage-class-specifier: [C99 6.7.1]
2657*67e74705SXin Li ///         'typedef'
2658*67e74705SXin Li ///         'extern'
2659*67e74705SXin Li ///         'static'
2660*67e74705SXin Li ///         'auto'
2661*67e74705SXin Li ///         'register'
2662*67e74705SXin Li /// [C++]   'mutable'
2663*67e74705SXin Li /// [C++11] 'thread_local'
2664*67e74705SXin Li /// [C11]   '_Thread_local'
2665*67e74705SXin Li /// [GNU]   '__thread'
2666*67e74705SXin Li ///       function-specifier: [C99 6.7.4]
2667*67e74705SXin Li /// [C99]   'inline'
2668*67e74705SXin Li /// [C++]   'virtual'
2669*67e74705SXin Li /// [C++]   'explicit'
2670*67e74705SXin Li /// [OpenCL] '__kernel'
2671*67e74705SXin Li ///       'friend': [C++ dcl.friend]
2672*67e74705SXin Li ///       'constexpr': [C++0x dcl.constexpr]
ParseDeclarationSpecifiers(DeclSpec & DS,const ParsedTemplateInfo & TemplateInfo,AccessSpecifier AS,DeclSpecContext DSContext,LateParsedAttrList * LateAttrs)2673*67e74705SXin Li void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
2674*67e74705SXin Li                                         const ParsedTemplateInfo &TemplateInfo,
2675*67e74705SXin Li                                         AccessSpecifier AS,
2676*67e74705SXin Li                                         DeclSpecContext DSContext,
2677*67e74705SXin Li                                         LateParsedAttrList *LateAttrs) {
2678*67e74705SXin Li   if (DS.getSourceRange().isInvalid()) {
2679*67e74705SXin Li     // Start the range at the current token but make the end of the range
2680*67e74705SXin Li     // invalid.  This will make the entire range invalid unless we successfully
2681*67e74705SXin Li     // consume a token.
2682*67e74705SXin Li     DS.SetRangeStart(Tok.getLocation());
2683*67e74705SXin Li     DS.SetRangeEnd(SourceLocation());
2684*67e74705SXin Li   }
2685*67e74705SXin Li 
2686*67e74705SXin Li   bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
2687*67e74705SXin Li   bool AttrsLastTime = false;
2688*67e74705SXin Li   ParsedAttributesWithRange attrs(AttrFactory);
2689*67e74705SXin Li   // We use Sema's policy to get bool macros right.
2690*67e74705SXin Li   PrintingPolicy Policy = Actions.getPrintingPolicy();
2691*67e74705SXin Li   while (1) {
2692*67e74705SXin Li     bool isInvalid = false;
2693*67e74705SXin Li     bool isStorageClass = false;
2694*67e74705SXin Li     const char *PrevSpec = nullptr;
2695*67e74705SXin Li     unsigned DiagID = 0;
2696*67e74705SXin Li 
2697*67e74705SXin Li     // HACK: MSVC doesn't consider _Atomic to be a keyword and its STL
2698*67e74705SXin Li     // implementation for VS2013 uses _Atomic as an identifier for one of the
2699*67e74705SXin Li     // classes in <atomic>.
2700*67e74705SXin Li     //
2701*67e74705SXin Li     // A typedef declaration containing _Atomic<...> is among the places where
2702*67e74705SXin Li     // the class is used.  If we are currently parsing such a declaration, treat
2703*67e74705SXin Li     // the token as an identifier.
2704*67e74705SXin Li     if (getLangOpts().MSVCCompat && Tok.is(tok::kw__Atomic) &&
2705*67e74705SXin Li         DS.getStorageClassSpec() == clang::DeclSpec::SCS_typedef &&
2706*67e74705SXin Li         !DS.hasTypeSpecifier() && GetLookAheadToken(1).is(tok::less))
2707*67e74705SXin Li       Tok.setKind(tok::identifier);
2708*67e74705SXin Li 
2709*67e74705SXin Li     SourceLocation Loc = Tok.getLocation();
2710*67e74705SXin Li 
2711*67e74705SXin Li     switch (Tok.getKind()) {
2712*67e74705SXin Li     default:
2713*67e74705SXin Li     DoneWithDeclSpec:
2714*67e74705SXin Li       if (!AttrsLastTime)
2715*67e74705SXin Li         ProhibitAttributes(attrs);
2716*67e74705SXin Li       else {
2717*67e74705SXin Li         // Reject C++11 attributes that appertain to decl specifiers as
2718*67e74705SXin Li         // we don't support any C++11 attributes that appertain to decl
2719*67e74705SXin Li         // specifiers. This also conforms to what g++ 4.8 is doing.
2720*67e74705SXin Li         ProhibitCXX11Attributes(attrs);
2721*67e74705SXin Li 
2722*67e74705SXin Li         DS.takeAttributesFrom(attrs);
2723*67e74705SXin Li       }
2724*67e74705SXin Li 
2725*67e74705SXin Li       // If this is not a declaration specifier token, we're done reading decl
2726*67e74705SXin Li       // specifiers.  First verify that DeclSpec's are consistent.
2727*67e74705SXin Li       DS.Finish(Actions, Policy);
2728*67e74705SXin Li       return;
2729*67e74705SXin Li 
2730*67e74705SXin Li     case tok::l_square:
2731*67e74705SXin Li     case tok::kw_alignas:
2732*67e74705SXin Li       if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
2733*67e74705SXin Li         goto DoneWithDeclSpec;
2734*67e74705SXin Li 
2735*67e74705SXin Li       ProhibitAttributes(attrs);
2736*67e74705SXin Li       // FIXME: It would be good to recover by accepting the attributes,
2737*67e74705SXin Li       //        but attempting to do that now would cause serious
2738*67e74705SXin Li       //        madness in terms of diagnostics.
2739*67e74705SXin Li       attrs.clear();
2740*67e74705SXin Li       attrs.Range = SourceRange();
2741*67e74705SXin Li 
2742*67e74705SXin Li       ParseCXX11Attributes(attrs);
2743*67e74705SXin Li       AttrsLastTime = true;
2744*67e74705SXin Li       continue;
2745*67e74705SXin Li 
2746*67e74705SXin Li     case tok::code_completion: {
2747*67e74705SXin Li       Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
2748*67e74705SXin Li       if (DS.hasTypeSpecifier()) {
2749*67e74705SXin Li         bool AllowNonIdentifiers
2750*67e74705SXin Li           = (getCurScope()->getFlags() & (Scope::ControlScope |
2751*67e74705SXin Li                                           Scope::BlockScope |
2752*67e74705SXin Li                                           Scope::TemplateParamScope |
2753*67e74705SXin Li                                           Scope::FunctionPrototypeScope |
2754*67e74705SXin Li                                           Scope::AtCatchScope)) == 0;
2755*67e74705SXin Li         bool AllowNestedNameSpecifiers
2756*67e74705SXin Li           = DSContext == DSC_top_level ||
2757*67e74705SXin Li             (DSContext == DSC_class && DS.isFriendSpecified());
2758*67e74705SXin Li 
2759*67e74705SXin Li         Actions.CodeCompleteDeclSpec(getCurScope(), DS,
2760*67e74705SXin Li                                      AllowNonIdentifiers,
2761*67e74705SXin Li                                      AllowNestedNameSpecifiers);
2762*67e74705SXin Li         return cutOffParsing();
2763*67e74705SXin Li       }
2764*67e74705SXin Li 
2765*67e74705SXin Li       if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2766*67e74705SXin Li         CCC = Sema::PCC_LocalDeclarationSpecifiers;
2767*67e74705SXin Li       else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
2768*67e74705SXin Li         CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
2769*67e74705SXin Li                                     : Sema::PCC_Template;
2770*67e74705SXin Li       else if (DSContext == DSC_class)
2771*67e74705SXin Li         CCC = Sema::PCC_Class;
2772*67e74705SXin Li       else if (CurParsedObjCImpl)
2773*67e74705SXin Li         CCC = Sema::PCC_ObjCImplementation;
2774*67e74705SXin Li 
2775*67e74705SXin Li       Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
2776*67e74705SXin Li       return cutOffParsing();
2777*67e74705SXin Li     }
2778*67e74705SXin Li 
2779*67e74705SXin Li     case tok::coloncolon: // ::foo::bar
2780*67e74705SXin Li       // C++ scope specifier.  Annotate and loop, or bail out on error.
2781*67e74705SXin Li       if (TryAnnotateCXXScopeToken(EnteringContext)) {
2782*67e74705SXin Li         if (!DS.hasTypeSpecifier())
2783*67e74705SXin Li           DS.SetTypeSpecError();
2784*67e74705SXin Li         goto DoneWithDeclSpec;
2785*67e74705SXin Li       }
2786*67e74705SXin Li       if (Tok.is(tok::coloncolon)) // ::new or ::delete
2787*67e74705SXin Li         goto DoneWithDeclSpec;
2788*67e74705SXin Li       continue;
2789*67e74705SXin Li 
2790*67e74705SXin Li     case tok::annot_cxxscope: {
2791*67e74705SXin Li       if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
2792*67e74705SXin Li         goto DoneWithDeclSpec;
2793*67e74705SXin Li 
2794*67e74705SXin Li       CXXScopeSpec SS;
2795*67e74705SXin Li       Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2796*67e74705SXin Li                                                    Tok.getAnnotationRange(),
2797*67e74705SXin Li                                                    SS);
2798*67e74705SXin Li 
2799*67e74705SXin Li       // We are looking for a qualified typename.
2800*67e74705SXin Li       Token Next = NextToken();
2801*67e74705SXin Li       if (Next.is(tok::annot_template_id) &&
2802*67e74705SXin Li           static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
2803*67e74705SXin Li             ->Kind == TNK_Type_template) {
2804*67e74705SXin Li         // We have a qualified template-id, e.g., N::A<int>
2805*67e74705SXin Li 
2806*67e74705SXin Li         // C++ [class.qual]p2:
2807*67e74705SXin Li         //   In a lookup in which the constructor is an acceptable lookup
2808*67e74705SXin Li         //   result and the nested-name-specifier nominates a class C:
2809*67e74705SXin Li         //
2810*67e74705SXin Li         //     - if the name specified after the
2811*67e74705SXin Li         //       nested-name-specifier, when looked up in C, is the
2812*67e74705SXin Li         //       injected-class-name of C (Clause 9), or
2813*67e74705SXin Li         //
2814*67e74705SXin Li         //     - if the name specified after the nested-name-specifier
2815*67e74705SXin Li         //       is the same as the identifier or the
2816*67e74705SXin Li         //       simple-template-id's template-name in the last
2817*67e74705SXin Li         //       component of the nested-name-specifier,
2818*67e74705SXin Li         //
2819*67e74705SXin Li         //   the name is instead considered to name the constructor of
2820*67e74705SXin Li         //   class C.
2821*67e74705SXin Li         //
2822*67e74705SXin Li         // Thus, if the template-name is actually the constructor
2823*67e74705SXin Li         // name, then the code is ill-formed; this interpretation is
2824*67e74705SXin Li         // reinforced by the NAD status of core issue 635.
2825*67e74705SXin Li         TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
2826*67e74705SXin Li         if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
2827*67e74705SXin Li             TemplateId->Name &&
2828*67e74705SXin Li             Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
2829*67e74705SXin Li           if (isConstructorDeclarator(/*Unqualified*/false)) {
2830*67e74705SXin Li             // The user meant this to be an out-of-line constructor
2831*67e74705SXin Li             // definition, but template arguments are not allowed
2832*67e74705SXin Li             // there.  Just allow this as a constructor; we'll
2833*67e74705SXin Li             // complain about it later.
2834*67e74705SXin Li             goto DoneWithDeclSpec;
2835*67e74705SXin Li           }
2836*67e74705SXin Li 
2837*67e74705SXin Li           // The user meant this to name a type, but it actually names
2838*67e74705SXin Li           // a constructor with some extraneous template
2839*67e74705SXin Li           // arguments. Complain, then parse it as a type as the user
2840*67e74705SXin Li           // intended.
2841*67e74705SXin Li           Diag(TemplateId->TemplateNameLoc,
2842*67e74705SXin Li                diag::err_out_of_line_template_id_type_names_constructor)
2843*67e74705SXin Li             << TemplateId->Name << 0 /* template name */;
2844*67e74705SXin Li         }
2845*67e74705SXin Li 
2846*67e74705SXin Li         DS.getTypeSpecScope() = SS;
2847*67e74705SXin Li         ConsumeToken(); // The C++ scope.
2848*67e74705SXin Li         assert(Tok.is(tok::annot_template_id) &&
2849*67e74705SXin Li                "ParseOptionalCXXScopeSpecifier not working");
2850*67e74705SXin Li         AnnotateTemplateIdTokenAsType();
2851*67e74705SXin Li         continue;
2852*67e74705SXin Li       }
2853*67e74705SXin Li 
2854*67e74705SXin Li       if (Next.is(tok::annot_typename)) {
2855*67e74705SXin Li         DS.getTypeSpecScope() = SS;
2856*67e74705SXin Li         ConsumeToken(); // The C++ scope.
2857*67e74705SXin Li         if (Tok.getAnnotationValue()) {
2858*67e74705SXin Li           ParsedType T = getTypeAnnotation(Tok);
2859*67e74705SXin Li           isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2860*67e74705SXin Li                                          Tok.getAnnotationEndLoc(),
2861*67e74705SXin Li                                          PrevSpec, DiagID, T, Policy);
2862*67e74705SXin Li           if (isInvalid)
2863*67e74705SXin Li             break;
2864*67e74705SXin Li         }
2865*67e74705SXin Li         else
2866*67e74705SXin Li           DS.SetTypeSpecError();
2867*67e74705SXin Li         DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2868*67e74705SXin Li         ConsumeToken(); // The typename
2869*67e74705SXin Li       }
2870*67e74705SXin Li 
2871*67e74705SXin Li       if (Next.isNot(tok::identifier))
2872*67e74705SXin Li         goto DoneWithDeclSpec;
2873*67e74705SXin Li 
2874*67e74705SXin Li       // If we're in a context where the identifier could be a class name,
2875*67e74705SXin Li       // check whether this is a constructor declaration.
2876*67e74705SXin Li       if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
2877*67e74705SXin Li           Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
2878*67e74705SXin Li                                      &SS)) {
2879*67e74705SXin Li         if (isConstructorDeclarator(/*Unqualified*/false))
2880*67e74705SXin Li           goto DoneWithDeclSpec;
2881*67e74705SXin Li 
2882*67e74705SXin Li         // As noted in C++ [class.qual]p2 (cited above), when the name
2883*67e74705SXin Li         // of the class is qualified in a context where it could name
2884*67e74705SXin Li         // a constructor, its a constructor name. However, we've
2885*67e74705SXin Li         // looked at the declarator, and the user probably meant this
2886*67e74705SXin Li         // to be a type. Complain that it isn't supposed to be treated
2887*67e74705SXin Li         // as a type, then proceed to parse it as a type.
2888*67e74705SXin Li         Diag(Next.getLocation(),
2889*67e74705SXin Li              diag::err_out_of_line_template_id_type_names_constructor)
2890*67e74705SXin Li           << Next.getIdentifierInfo() << 1 /* type */;
2891*67e74705SXin Li       }
2892*67e74705SXin Li 
2893*67e74705SXin Li       ParsedType TypeRep =
2894*67e74705SXin Li           Actions.getTypeName(*Next.getIdentifierInfo(), Next.getLocation(),
2895*67e74705SXin Li                               getCurScope(), &SS, false, false, nullptr,
2896*67e74705SXin Li                               /*IsCtorOrDtorName=*/false,
2897*67e74705SXin Li                               /*NonTrivialSourceInfo=*/true);
2898*67e74705SXin Li 
2899*67e74705SXin Li       // If the referenced identifier is not a type, then this declspec is
2900*67e74705SXin Li       // erroneous: We already checked about that it has no type specifier, and
2901*67e74705SXin Li       // C++ doesn't have implicit int.  Diagnose it as a typo w.r.t. to the
2902*67e74705SXin Li       // typename.
2903*67e74705SXin Li       if (!TypeRep) {
2904*67e74705SXin Li         ConsumeToken();   // Eat the scope spec so the identifier is current.
2905*67e74705SXin Li         ParsedAttributesWithRange Attrs(AttrFactory);
2906*67e74705SXin Li         if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2907*67e74705SXin Li           if (!Attrs.empty()) {
2908*67e74705SXin Li             AttrsLastTime = true;
2909*67e74705SXin Li             attrs.takeAllFrom(Attrs);
2910*67e74705SXin Li           }
2911*67e74705SXin Li           continue;
2912*67e74705SXin Li         }
2913*67e74705SXin Li         goto DoneWithDeclSpec;
2914*67e74705SXin Li       }
2915*67e74705SXin Li 
2916*67e74705SXin Li       DS.getTypeSpecScope() = SS;
2917*67e74705SXin Li       ConsumeToken(); // The C++ scope.
2918*67e74705SXin Li 
2919*67e74705SXin Li       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
2920*67e74705SXin Li                                      DiagID, TypeRep, Policy);
2921*67e74705SXin Li       if (isInvalid)
2922*67e74705SXin Li         break;
2923*67e74705SXin Li 
2924*67e74705SXin Li       DS.SetRangeEnd(Tok.getLocation());
2925*67e74705SXin Li       ConsumeToken(); // The typename.
2926*67e74705SXin Li 
2927*67e74705SXin Li       continue;
2928*67e74705SXin Li     }
2929*67e74705SXin Li 
2930*67e74705SXin Li     case tok::annot_typename: {
2931*67e74705SXin Li       // If we've previously seen a tag definition, we were almost surely
2932*67e74705SXin Li       // missing a semicolon after it.
2933*67e74705SXin Li       if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
2934*67e74705SXin Li         goto DoneWithDeclSpec;
2935*67e74705SXin Li 
2936*67e74705SXin Li       if (Tok.getAnnotationValue()) {
2937*67e74705SXin Li         ParsedType T = getTypeAnnotation(Tok);
2938*67e74705SXin Li         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
2939*67e74705SXin Li                                        DiagID, T, Policy);
2940*67e74705SXin Li       } else
2941*67e74705SXin Li         DS.SetTypeSpecError();
2942*67e74705SXin Li 
2943*67e74705SXin Li       if (isInvalid)
2944*67e74705SXin Li         break;
2945*67e74705SXin Li 
2946*67e74705SXin Li       DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2947*67e74705SXin Li       ConsumeToken(); // The typename
2948*67e74705SXin Li 
2949*67e74705SXin Li       continue;
2950*67e74705SXin Li     }
2951*67e74705SXin Li 
2952*67e74705SXin Li     case tok::kw___is_signed:
2953*67e74705SXin Li       // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2954*67e74705SXin Li       // typically treats it as a trait. If we see __is_signed as it appears
2955*67e74705SXin Li       // in libstdc++, e.g.,
2956*67e74705SXin Li       //
2957*67e74705SXin Li       //   static const bool __is_signed;
2958*67e74705SXin Li       //
2959*67e74705SXin Li       // then treat __is_signed as an identifier rather than as a keyword.
2960*67e74705SXin Li       if (DS.getTypeSpecType() == TST_bool &&
2961*67e74705SXin Li           DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2962*67e74705SXin Li           DS.getStorageClassSpec() == DeclSpec::SCS_static)
2963*67e74705SXin Li         TryKeywordIdentFallback(true);
2964*67e74705SXin Li 
2965*67e74705SXin Li       // We're done with the declaration-specifiers.
2966*67e74705SXin Li       goto DoneWithDeclSpec;
2967*67e74705SXin Li 
2968*67e74705SXin Li       // typedef-name
2969*67e74705SXin Li     case tok::kw___super:
2970*67e74705SXin Li     case tok::kw_decltype:
2971*67e74705SXin Li     case tok::identifier: {
2972*67e74705SXin Li       // This identifier can only be a typedef name if we haven't already seen
2973*67e74705SXin Li       // a type-specifier.  Without this check we misparse:
2974*67e74705SXin Li       //  typedef int X; struct Y { short X; };  as 'short int'.
2975*67e74705SXin Li       if (DS.hasTypeSpecifier())
2976*67e74705SXin Li         goto DoneWithDeclSpec;
2977*67e74705SXin Li 
2978*67e74705SXin Li       // In C++, check to see if this is a scope specifier like foo::bar::, if
2979*67e74705SXin Li       // so handle it as such.  This is important for ctor parsing.
2980*67e74705SXin Li       if (getLangOpts().CPlusPlus) {
2981*67e74705SXin Li         if (TryAnnotateCXXScopeToken(EnteringContext)) {
2982*67e74705SXin Li           DS.SetTypeSpecError();
2983*67e74705SXin Li           goto DoneWithDeclSpec;
2984*67e74705SXin Li         }
2985*67e74705SXin Li         if (!Tok.is(tok::identifier))
2986*67e74705SXin Li           continue;
2987*67e74705SXin Li       }
2988*67e74705SXin Li 
2989*67e74705SXin Li       // Check for need to substitute AltiVec keyword tokens.
2990*67e74705SXin Li       if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2991*67e74705SXin Li         break;
2992*67e74705SXin Li 
2993*67e74705SXin Li       // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2994*67e74705SXin Li       //                allow the use of a typedef name as a type specifier.
2995*67e74705SXin Li       if (DS.isTypeAltiVecVector())
2996*67e74705SXin Li         goto DoneWithDeclSpec;
2997*67e74705SXin Li 
2998*67e74705SXin Li       if (DSContext == DSC_objc_method_result && isObjCInstancetype()) {
2999*67e74705SXin Li         ParsedType TypeRep = Actions.ActOnObjCInstanceType(Loc);
3000*67e74705SXin Li         assert(TypeRep);
3001*67e74705SXin Li         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
3002*67e74705SXin Li                                        DiagID, TypeRep, Policy);
3003*67e74705SXin Li         if (isInvalid)
3004*67e74705SXin Li           break;
3005*67e74705SXin Li 
3006*67e74705SXin Li         DS.SetRangeEnd(Loc);
3007*67e74705SXin Li         ConsumeToken();
3008*67e74705SXin Li         continue;
3009*67e74705SXin Li       }
3010*67e74705SXin Li 
3011*67e74705SXin Li       ParsedType TypeRep =
3012*67e74705SXin Li         Actions.getTypeName(*Tok.getIdentifierInfo(),
3013*67e74705SXin Li                             Tok.getLocation(), getCurScope());
3014*67e74705SXin Li 
3015*67e74705SXin Li       // If this is not a typedef name, don't parse it as part of the declspec,
3016*67e74705SXin Li       // it must be an implicit int or an error.
3017*67e74705SXin Li       if (!TypeRep) {
3018*67e74705SXin Li         ParsedAttributesWithRange Attrs(AttrFactory);
3019*67e74705SXin Li         if (ParseImplicitInt(DS, nullptr, TemplateInfo, AS, DSContext, Attrs)) {
3020*67e74705SXin Li           if (!Attrs.empty()) {
3021*67e74705SXin Li             AttrsLastTime = true;
3022*67e74705SXin Li             attrs.takeAllFrom(Attrs);
3023*67e74705SXin Li           }
3024*67e74705SXin Li           continue;
3025*67e74705SXin Li         }
3026*67e74705SXin Li         goto DoneWithDeclSpec;
3027*67e74705SXin Li       }
3028*67e74705SXin Li 
3029*67e74705SXin Li       // If we're in a context where the identifier could be a class name,
3030*67e74705SXin Li       // check whether this is a constructor declaration.
3031*67e74705SXin Li       if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
3032*67e74705SXin Li           Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
3033*67e74705SXin Li           isConstructorDeclarator(/*Unqualified*/true))
3034*67e74705SXin Li         goto DoneWithDeclSpec;
3035*67e74705SXin Li 
3036*67e74705SXin Li       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
3037*67e74705SXin Li                                      DiagID, TypeRep, Policy);
3038*67e74705SXin Li       if (isInvalid)
3039*67e74705SXin Li         break;
3040*67e74705SXin Li 
3041*67e74705SXin Li       DS.SetRangeEnd(Tok.getLocation());
3042*67e74705SXin Li       ConsumeToken(); // The identifier
3043*67e74705SXin Li 
3044*67e74705SXin Li       // Objective-C supports type arguments and protocol references
3045*67e74705SXin Li       // following an Objective-C object or object pointer
3046*67e74705SXin Li       // type. Handle either one of them.
3047*67e74705SXin Li       if (Tok.is(tok::less) && getLangOpts().ObjC1) {
3048*67e74705SXin Li         SourceLocation NewEndLoc;
3049*67e74705SXin Li         TypeResult NewTypeRep = parseObjCTypeArgsAndProtocolQualifiers(
3050*67e74705SXin Li                                   Loc, TypeRep, /*consumeLastToken=*/true,
3051*67e74705SXin Li                                   NewEndLoc);
3052*67e74705SXin Li         if (NewTypeRep.isUsable()) {
3053*67e74705SXin Li           DS.UpdateTypeRep(NewTypeRep.get());
3054*67e74705SXin Li           DS.SetRangeEnd(NewEndLoc);
3055*67e74705SXin Li         }
3056*67e74705SXin Li       }
3057*67e74705SXin Li 
3058*67e74705SXin Li       // Need to support trailing type qualifiers (e.g. "id<p> const").
3059*67e74705SXin Li       // If a type specifier follows, it will be diagnosed elsewhere.
3060*67e74705SXin Li       continue;
3061*67e74705SXin Li     }
3062*67e74705SXin Li 
3063*67e74705SXin Li       // type-name
3064*67e74705SXin Li     case tok::annot_template_id: {
3065*67e74705SXin Li       TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
3066*67e74705SXin Li       if (TemplateId->Kind != TNK_Type_template) {
3067*67e74705SXin Li         // This template-id does not refer to a type name, so we're
3068*67e74705SXin Li         // done with the type-specifiers.
3069*67e74705SXin Li         goto DoneWithDeclSpec;
3070*67e74705SXin Li       }
3071*67e74705SXin Li 
3072*67e74705SXin Li       // If we're in a context where the template-id could be a
3073*67e74705SXin Li       // constructor name or specialization, check whether this is a
3074*67e74705SXin Li       // constructor declaration.
3075*67e74705SXin Li       if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
3076*67e74705SXin Li           Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
3077*67e74705SXin Li           isConstructorDeclarator(TemplateId->SS.isEmpty()))
3078*67e74705SXin Li         goto DoneWithDeclSpec;
3079*67e74705SXin Li 
3080*67e74705SXin Li       // Turn the template-id annotation token into a type annotation
3081*67e74705SXin Li       // token, then try again to parse it as a type-specifier.
3082*67e74705SXin Li       AnnotateTemplateIdTokenAsType();
3083*67e74705SXin Li       continue;
3084*67e74705SXin Li     }
3085*67e74705SXin Li 
3086*67e74705SXin Li     // GNU attributes support.
3087*67e74705SXin Li     case tok::kw___attribute:
3088*67e74705SXin Li       ParseGNUAttributes(DS.getAttributes(), nullptr, LateAttrs);
3089*67e74705SXin Li       continue;
3090*67e74705SXin Li 
3091*67e74705SXin Li     // Microsoft declspec support.
3092*67e74705SXin Li     case tok::kw___declspec:
3093*67e74705SXin Li       ParseMicrosoftDeclSpecs(DS.getAttributes());
3094*67e74705SXin Li       continue;
3095*67e74705SXin Li 
3096*67e74705SXin Li     // Microsoft single token adornments.
3097*67e74705SXin Li     case tok::kw___forceinline: {
3098*67e74705SXin Li       isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
3099*67e74705SXin Li       IdentifierInfo *AttrName = Tok.getIdentifierInfo();
3100*67e74705SXin Li       SourceLocation AttrNameLoc = Tok.getLocation();
3101*67e74705SXin Li       DS.getAttributes().addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc,
3102*67e74705SXin Li                                 nullptr, 0, AttributeList::AS_Keyword);
3103*67e74705SXin Li       break;
3104*67e74705SXin Li     }
3105*67e74705SXin Li 
3106*67e74705SXin Li     case tok::kw___unaligned:
3107*67e74705SXin Li       isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID,
3108*67e74705SXin Li                                  getLangOpts());
3109*67e74705SXin Li       break;
3110*67e74705SXin Li 
3111*67e74705SXin Li     case tok::kw___sptr:
3112*67e74705SXin Li     case tok::kw___uptr:
3113*67e74705SXin Li     case tok::kw___ptr64:
3114*67e74705SXin Li     case tok::kw___ptr32:
3115*67e74705SXin Li     case tok::kw___w64:
3116*67e74705SXin Li     case tok::kw___cdecl:
3117*67e74705SXin Li     case tok::kw___stdcall:
3118*67e74705SXin Li     case tok::kw___fastcall:
3119*67e74705SXin Li     case tok::kw___thiscall:
3120*67e74705SXin Li     case tok::kw___vectorcall:
3121*67e74705SXin Li       ParseMicrosoftTypeAttributes(DS.getAttributes());
3122*67e74705SXin Li       continue;
3123*67e74705SXin Li 
3124*67e74705SXin Li     // Borland single token adornments.
3125*67e74705SXin Li     case tok::kw___pascal:
3126*67e74705SXin Li       ParseBorlandTypeAttributes(DS.getAttributes());
3127*67e74705SXin Li       continue;
3128*67e74705SXin Li 
3129*67e74705SXin Li     // OpenCL single token adornments.
3130*67e74705SXin Li     case tok::kw___kernel:
3131*67e74705SXin Li       ParseOpenCLKernelAttributes(DS.getAttributes());
3132*67e74705SXin Li       continue;
3133*67e74705SXin Li 
3134*67e74705SXin Li     // Nullability type specifiers.
3135*67e74705SXin Li     case tok::kw__Nonnull:
3136*67e74705SXin Li     case tok::kw__Nullable:
3137*67e74705SXin Li     case tok::kw__Null_unspecified:
3138*67e74705SXin Li       ParseNullabilityTypeSpecifiers(DS.getAttributes());
3139*67e74705SXin Li       continue;
3140*67e74705SXin Li 
3141*67e74705SXin Li     // Objective-C 'kindof' types.
3142*67e74705SXin Li     case tok::kw___kindof:
3143*67e74705SXin Li       DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc,
3144*67e74705SXin Li                                 nullptr, 0, AttributeList::AS_Keyword);
3145*67e74705SXin Li       (void)ConsumeToken();
3146*67e74705SXin Li       continue;
3147*67e74705SXin Li 
3148*67e74705SXin Li     // storage-class-specifier
3149*67e74705SXin Li     case tok::kw_typedef:
3150*67e74705SXin Li       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
3151*67e74705SXin Li                                          PrevSpec, DiagID, Policy);
3152*67e74705SXin Li       isStorageClass = true;
3153*67e74705SXin Li       break;
3154*67e74705SXin Li     case tok::kw_extern:
3155*67e74705SXin Li       if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
3156*67e74705SXin Li         Diag(Tok, diag::ext_thread_before) << "extern";
3157*67e74705SXin Li       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
3158*67e74705SXin Li                                          PrevSpec, DiagID, Policy);
3159*67e74705SXin Li       isStorageClass = true;
3160*67e74705SXin Li       break;
3161*67e74705SXin Li     case tok::kw___private_extern__:
3162*67e74705SXin Li       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
3163*67e74705SXin Li                                          Loc, PrevSpec, DiagID, Policy);
3164*67e74705SXin Li       isStorageClass = true;
3165*67e74705SXin Li       break;
3166*67e74705SXin Li     case tok::kw_static:
3167*67e74705SXin Li       if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
3168*67e74705SXin Li         Diag(Tok, diag::ext_thread_before) << "static";
3169*67e74705SXin Li       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
3170*67e74705SXin Li                                          PrevSpec, DiagID, Policy);
3171*67e74705SXin Li       isStorageClass = true;
3172*67e74705SXin Li       break;
3173*67e74705SXin Li     case tok::kw_auto:
3174*67e74705SXin Li       if (getLangOpts().CPlusPlus11) {
3175*67e74705SXin Li         if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
3176*67e74705SXin Li           isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
3177*67e74705SXin Li                                              PrevSpec, DiagID, Policy);
3178*67e74705SXin Li           if (!isInvalid)
3179*67e74705SXin Li             Diag(Tok, diag::ext_auto_storage_class)
3180*67e74705SXin Li               << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
3181*67e74705SXin Li         } else
3182*67e74705SXin Li           isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
3183*67e74705SXin Li                                          DiagID, Policy);
3184*67e74705SXin Li       } else
3185*67e74705SXin Li         isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
3186*67e74705SXin Li                                            PrevSpec, DiagID, Policy);
3187*67e74705SXin Li       isStorageClass = true;
3188*67e74705SXin Li       break;
3189*67e74705SXin Li     case tok::kw___auto_type:
3190*67e74705SXin Li       Diag(Tok, diag::ext_auto_type);
3191*67e74705SXin Li       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto_type, Loc, PrevSpec,
3192*67e74705SXin Li                                      DiagID, Policy);
3193*67e74705SXin Li       break;
3194*67e74705SXin Li     case tok::kw_register:
3195*67e74705SXin Li       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
3196*67e74705SXin Li                                          PrevSpec, DiagID, Policy);
3197*67e74705SXin Li       isStorageClass = true;
3198*67e74705SXin Li       break;
3199*67e74705SXin Li     case tok::kw_mutable:
3200*67e74705SXin Li       isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
3201*67e74705SXin Li                                          PrevSpec, DiagID, Policy);
3202*67e74705SXin Li       isStorageClass = true;
3203*67e74705SXin Li       break;
3204*67e74705SXin Li     case tok::kw___thread:
3205*67e74705SXin Li       isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
3206*67e74705SXin Li                                                PrevSpec, DiagID);
3207*67e74705SXin Li       isStorageClass = true;
3208*67e74705SXin Li       break;
3209*67e74705SXin Li     case tok::kw_thread_local:
3210*67e74705SXin Li       isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
3211*67e74705SXin Li                                                PrevSpec, DiagID);
3212*67e74705SXin Li       break;
3213*67e74705SXin Li     case tok::kw__Thread_local:
3214*67e74705SXin Li       isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
3215*67e74705SXin Li                                                Loc, PrevSpec, DiagID);
3216*67e74705SXin Li       isStorageClass = true;
3217*67e74705SXin Li       break;
3218*67e74705SXin Li 
3219*67e74705SXin Li     // function-specifier
3220*67e74705SXin Li     case tok::kw_inline:
3221*67e74705SXin Li       isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
3222*67e74705SXin Li       break;
3223*67e74705SXin Li     case tok::kw_virtual:
3224*67e74705SXin Li       isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
3225*67e74705SXin Li       break;
3226*67e74705SXin Li     case tok::kw_explicit:
3227*67e74705SXin Li       isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID);
3228*67e74705SXin Li       break;
3229*67e74705SXin Li     case tok::kw__Noreturn:
3230*67e74705SXin Li       if (!getLangOpts().C11)
3231*67e74705SXin Li         Diag(Loc, diag::ext_c11_noreturn);
3232*67e74705SXin Li       isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
3233*67e74705SXin Li       break;
3234*67e74705SXin Li 
3235*67e74705SXin Li     // alignment-specifier
3236*67e74705SXin Li     case tok::kw__Alignas:
3237*67e74705SXin Li       if (!getLangOpts().C11)
3238*67e74705SXin Li         Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
3239*67e74705SXin Li       ParseAlignmentSpecifier(DS.getAttributes());
3240*67e74705SXin Li       continue;
3241*67e74705SXin Li 
3242*67e74705SXin Li     // friend
3243*67e74705SXin Li     case tok::kw_friend:
3244*67e74705SXin Li       if (DSContext == DSC_class)
3245*67e74705SXin Li         isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
3246*67e74705SXin Li       else {
3247*67e74705SXin Li         PrevSpec = ""; // not actually used by the diagnostic
3248*67e74705SXin Li         DiagID = diag::err_friend_invalid_in_context;
3249*67e74705SXin Li         isInvalid = true;
3250*67e74705SXin Li       }
3251*67e74705SXin Li       break;
3252*67e74705SXin Li 
3253*67e74705SXin Li     // Modules
3254*67e74705SXin Li     case tok::kw___module_private__:
3255*67e74705SXin Li       isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
3256*67e74705SXin Li       break;
3257*67e74705SXin Li 
3258*67e74705SXin Li     // constexpr
3259*67e74705SXin Li     case tok::kw_constexpr:
3260*67e74705SXin Li       isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
3261*67e74705SXin Li       break;
3262*67e74705SXin Li 
3263*67e74705SXin Li     // concept
3264*67e74705SXin Li     case tok::kw_concept:
3265*67e74705SXin Li       isInvalid = DS.SetConceptSpec(Loc, PrevSpec, DiagID);
3266*67e74705SXin Li       break;
3267*67e74705SXin Li 
3268*67e74705SXin Li     // type-specifier
3269*67e74705SXin Li     case tok::kw_short:
3270*67e74705SXin Li       isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
3271*67e74705SXin Li                                       DiagID, Policy);
3272*67e74705SXin Li       break;
3273*67e74705SXin Li     case tok::kw_long:
3274*67e74705SXin Li       if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
3275*67e74705SXin Li         isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
3276*67e74705SXin Li                                         DiagID, Policy);
3277*67e74705SXin Li       else
3278*67e74705SXin Li         isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3279*67e74705SXin Li                                         DiagID, Policy);
3280*67e74705SXin Li       break;
3281*67e74705SXin Li     case tok::kw___int64:
3282*67e74705SXin Li         isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
3283*67e74705SXin Li                                         DiagID, Policy);
3284*67e74705SXin Li       break;
3285*67e74705SXin Li     case tok::kw_signed:
3286*67e74705SXin Li       isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
3287*67e74705SXin Li                                      DiagID);
3288*67e74705SXin Li       break;
3289*67e74705SXin Li     case tok::kw_unsigned:
3290*67e74705SXin Li       isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3291*67e74705SXin Li                                      DiagID);
3292*67e74705SXin Li       break;
3293*67e74705SXin Li     case tok::kw__Complex:
3294*67e74705SXin Li       isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3295*67e74705SXin Li                                         DiagID);
3296*67e74705SXin Li       break;
3297*67e74705SXin Li     case tok::kw__Imaginary:
3298*67e74705SXin Li       isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3299*67e74705SXin Li                                         DiagID);
3300*67e74705SXin Li       break;
3301*67e74705SXin Li     case tok::kw_void:
3302*67e74705SXin Li       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
3303*67e74705SXin Li                                      DiagID, Policy);
3304*67e74705SXin Li       break;
3305*67e74705SXin Li     case tok::kw_char:
3306*67e74705SXin Li       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
3307*67e74705SXin Li                                      DiagID, Policy);
3308*67e74705SXin Li       break;
3309*67e74705SXin Li     case tok::kw_int:
3310*67e74705SXin Li       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
3311*67e74705SXin Li                                      DiagID, Policy);
3312*67e74705SXin Li       break;
3313*67e74705SXin Li     case tok::kw___int128:
3314*67e74705SXin Li       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
3315*67e74705SXin Li                                      DiagID, Policy);
3316*67e74705SXin Li       break;
3317*67e74705SXin Li     case tok::kw_half:
3318*67e74705SXin Li       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
3319*67e74705SXin Li                                      DiagID, Policy);
3320*67e74705SXin Li       break;
3321*67e74705SXin Li     case tok::kw_float:
3322*67e74705SXin Li       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
3323*67e74705SXin Li                                      DiagID, Policy);
3324*67e74705SXin Li       break;
3325*67e74705SXin Li     case tok::kw_double:
3326*67e74705SXin Li       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
3327*67e74705SXin Li                                      DiagID, Policy);
3328*67e74705SXin Li       break;
3329*67e74705SXin Li     case tok::kw___float128:
3330*67e74705SXin Li       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float128, Loc, PrevSpec,
3331*67e74705SXin Li                                      DiagID, Policy);
3332*67e74705SXin Li       break;
3333*67e74705SXin Li     case tok::kw_wchar_t:
3334*67e74705SXin Li       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
3335*67e74705SXin Li                                      DiagID, Policy);
3336*67e74705SXin Li       break;
3337*67e74705SXin Li     case tok::kw_char16_t:
3338*67e74705SXin Li       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
3339*67e74705SXin Li                                      DiagID, Policy);
3340*67e74705SXin Li       break;
3341*67e74705SXin Li     case tok::kw_char32_t:
3342*67e74705SXin Li       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
3343*67e74705SXin Li                                      DiagID, Policy);
3344*67e74705SXin Li       break;
3345*67e74705SXin Li     case tok::kw_bool:
3346*67e74705SXin Li     case tok::kw__Bool:
3347*67e74705SXin Li       if (Tok.is(tok::kw_bool) &&
3348*67e74705SXin Li           DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3349*67e74705SXin Li           DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3350*67e74705SXin Li         PrevSpec = ""; // Not used by the diagnostic.
3351*67e74705SXin Li         DiagID = diag::err_bool_redeclaration;
3352*67e74705SXin Li         // For better error recovery.
3353*67e74705SXin Li         Tok.setKind(tok::identifier);
3354*67e74705SXin Li         isInvalid = true;
3355*67e74705SXin Li       } else {
3356*67e74705SXin Li         isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
3357*67e74705SXin Li                                        DiagID, Policy);
3358*67e74705SXin Li       }
3359*67e74705SXin Li       break;
3360*67e74705SXin Li     case tok::kw__Decimal32:
3361*67e74705SXin Li       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
3362*67e74705SXin Li                                      DiagID, Policy);
3363*67e74705SXin Li       break;
3364*67e74705SXin Li     case tok::kw__Decimal64:
3365*67e74705SXin Li       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
3366*67e74705SXin Li                                      DiagID, Policy);
3367*67e74705SXin Li       break;
3368*67e74705SXin Li     case tok::kw__Decimal128:
3369*67e74705SXin Li       isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
3370*67e74705SXin Li                                      DiagID, Policy);
3371*67e74705SXin Li       break;
3372*67e74705SXin Li     case tok::kw___vector:
3373*67e74705SXin Li       isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
3374*67e74705SXin Li       break;
3375*67e74705SXin Li     case tok::kw___pixel:
3376*67e74705SXin Li       isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
3377*67e74705SXin Li       break;
3378*67e74705SXin Li     case tok::kw___bool:
3379*67e74705SXin Li       isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
3380*67e74705SXin Li       break;
3381*67e74705SXin Li     case tok::kw_pipe:
3382*67e74705SXin Li       if (!getLangOpts().OpenCL || (getLangOpts().OpenCLVersion < 200)) {
3383*67e74705SXin Li         // OpenCL 2.0 defined this keyword. OpenCL 1.2 and earlier should
3384*67e74705SXin Li         // support the "pipe" word as identifier.
3385*67e74705SXin Li         Tok.getIdentifierInfo()->revertTokenIDToIdentifier();
3386*67e74705SXin Li         goto DoneWithDeclSpec;
3387*67e74705SXin Li       }
3388*67e74705SXin Li       isInvalid = DS.SetTypePipe(true, Loc, PrevSpec, DiagID, Policy);
3389*67e74705SXin Li       break;
3390*67e74705SXin Li #define GENERIC_IMAGE_TYPE(ImgType, Id) \
3391*67e74705SXin Li   case tok::kw_##ImgType##_t: \
3392*67e74705SXin Li     isInvalid = DS.SetTypeSpecType(DeclSpec::TST_##ImgType##_t, Loc, PrevSpec, \
3393*67e74705SXin Li                                    DiagID, Policy); \
3394*67e74705SXin Li     break;
3395*67e74705SXin Li #include "clang/Basic/OpenCLImageTypes.def"
3396*67e74705SXin Li     case tok::kw___unknown_anytype:
3397*67e74705SXin Li       isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
3398*67e74705SXin Li                                      PrevSpec, DiagID, Policy);
3399*67e74705SXin Li       break;
3400*67e74705SXin Li 
3401*67e74705SXin Li     // class-specifier:
3402*67e74705SXin Li     case tok::kw_class:
3403*67e74705SXin Li     case tok::kw_struct:
3404*67e74705SXin Li     case tok::kw___interface:
3405*67e74705SXin Li     case tok::kw_union: {
3406*67e74705SXin Li       tok::TokenKind Kind = Tok.getKind();
3407*67e74705SXin Li       ConsumeToken();
3408*67e74705SXin Li 
3409*67e74705SXin Li       // These are attributes following class specifiers.
3410*67e74705SXin Li       // To produce better diagnostic, we parse them when
3411*67e74705SXin Li       // parsing class specifier.
3412*67e74705SXin Li       ParsedAttributesWithRange Attributes(AttrFactory);
3413*67e74705SXin Li       ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
3414*67e74705SXin Li                           EnteringContext, DSContext, Attributes);
3415*67e74705SXin Li 
3416*67e74705SXin Li       // If there are attributes following class specifier,
3417*67e74705SXin Li       // take them over and handle them here.
3418*67e74705SXin Li       if (!Attributes.empty()) {
3419*67e74705SXin Li         AttrsLastTime = true;
3420*67e74705SXin Li         attrs.takeAllFrom(Attributes);
3421*67e74705SXin Li       }
3422*67e74705SXin Li       continue;
3423*67e74705SXin Li     }
3424*67e74705SXin Li 
3425*67e74705SXin Li     // enum-specifier:
3426*67e74705SXin Li     case tok::kw_enum:
3427*67e74705SXin Li       ConsumeToken();
3428*67e74705SXin Li       ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
3429*67e74705SXin Li       continue;
3430*67e74705SXin Li 
3431*67e74705SXin Li     // cv-qualifier:
3432*67e74705SXin Li     case tok::kw_const:
3433*67e74705SXin Li       isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
3434*67e74705SXin Li                                  getLangOpts());
3435*67e74705SXin Li       break;
3436*67e74705SXin Li     case tok::kw_volatile:
3437*67e74705SXin Li       isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
3438*67e74705SXin Li                                  getLangOpts());
3439*67e74705SXin Li       break;
3440*67e74705SXin Li     case tok::kw_restrict:
3441*67e74705SXin Li       isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
3442*67e74705SXin Li                                  getLangOpts());
3443*67e74705SXin Li       break;
3444*67e74705SXin Li 
3445*67e74705SXin Li     // C++ typename-specifier:
3446*67e74705SXin Li     case tok::kw_typename:
3447*67e74705SXin Li       if (TryAnnotateTypeOrScopeToken()) {
3448*67e74705SXin Li         DS.SetTypeSpecError();
3449*67e74705SXin Li         goto DoneWithDeclSpec;
3450*67e74705SXin Li       }
3451*67e74705SXin Li       if (!Tok.is(tok::kw_typename))
3452*67e74705SXin Li         continue;
3453*67e74705SXin Li       break;
3454*67e74705SXin Li 
3455*67e74705SXin Li     // GNU typeof support.
3456*67e74705SXin Li     case tok::kw_typeof:
3457*67e74705SXin Li       ParseTypeofSpecifier(DS);
3458*67e74705SXin Li       continue;
3459*67e74705SXin Li 
3460*67e74705SXin Li     case tok::annot_decltype:
3461*67e74705SXin Li       ParseDecltypeSpecifier(DS);
3462*67e74705SXin Li       continue;
3463*67e74705SXin Li 
3464*67e74705SXin Li     case tok::annot_pragma_pack:
3465*67e74705SXin Li       HandlePragmaPack();
3466*67e74705SXin Li       continue;
3467*67e74705SXin Li 
3468*67e74705SXin Li     case tok::annot_pragma_ms_pragma:
3469*67e74705SXin Li       HandlePragmaMSPragma();
3470*67e74705SXin Li       continue;
3471*67e74705SXin Li 
3472*67e74705SXin Li     case tok::annot_pragma_ms_vtordisp:
3473*67e74705SXin Li       HandlePragmaMSVtorDisp();
3474*67e74705SXin Li       continue;
3475*67e74705SXin Li 
3476*67e74705SXin Li     case tok::annot_pragma_ms_pointers_to_members:
3477*67e74705SXin Li       HandlePragmaMSPointersToMembers();
3478*67e74705SXin Li       continue;
3479*67e74705SXin Li 
3480*67e74705SXin Li     case tok::kw___underlying_type:
3481*67e74705SXin Li       ParseUnderlyingTypeSpecifier(DS);
3482*67e74705SXin Li       continue;
3483*67e74705SXin Li 
3484*67e74705SXin Li     case tok::kw__Atomic:
3485*67e74705SXin Li       // C11 6.7.2.4/4:
3486*67e74705SXin Li       //   If the _Atomic keyword is immediately followed by a left parenthesis,
3487*67e74705SXin Li       //   it is interpreted as a type specifier (with a type name), not as a
3488*67e74705SXin Li       //   type qualifier.
3489*67e74705SXin Li       if (NextToken().is(tok::l_paren)) {
3490*67e74705SXin Li         ParseAtomicSpecifier(DS);
3491*67e74705SXin Li         continue;
3492*67e74705SXin Li       }
3493*67e74705SXin Li       isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3494*67e74705SXin Li                                  getLangOpts());
3495*67e74705SXin Li       break;
3496*67e74705SXin Li 
3497*67e74705SXin Li     // OpenCL qualifiers:
3498*67e74705SXin Li     case tok::kw___generic:
3499*67e74705SXin Li       // generic address space is introduced only in OpenCL v2.0
3500*67e74705SXin Li       // see OpenCL C Spec v2.0 s6.5.5
3501*67e74705SXin Li       if (Actions.getLangOpts().OpenCLVersion < 200) {
3502*67e74705SXin Li         DiagID = diag::err_opencl_unknown_type_specifier;
3503*67e74705SXin Li         PrevSpec = Tok.getIdentifierInfo()->getNameStart();
3504*67e74705SXin Li         isInvalid = true;
3505*67e74705SXin Li         break;
3506*67e74705SXin Li       };
3507*67e74705SXin Li     case tok::kw___private:
3508*67e74705SXin Li     case tok::kw___global:
3509*67e74705SXin Li     case tok::kw___local:
3510*67e74705SXin Li     case tok::kw___constant:
3511*67e74705SXin Li     case tok::kw___read_only:
3512*67e74705SXin Li     case tok::kw___write_only:
3513*67e74705SXin Li     case tok::kw___read_write:
3514*67e74705SXin Li       ParseOpenCLQualifiers(DS.getAttributes());
3515*67e74705SXin Li       break;
3516*67e74705SXin Li 
3517*67e74705SXin Li     case tok::less:
3518*67e74705SXin Li       // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
3519*67e74705SXin Li       // "id<SomeProtocol>".  This is hopelessly old fashioned and dangerous,
3520*67e74705SXin Li       // but we support it.
3521*67e74705SXin Li       if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
3522*67e74705SXin Li         goto DoneWithDeclSpec;
3523*67e74705SXin Li 
3524*67e74705SXin Li       SourceLocation StartLoc = Tok.getLocation();
3525*67e74705SXin Li       SourceLocation EndLoc;
3526*67e74705SXin Li       TypeResult Type = parseObjCProtocolQualifierType(EndLoc);
3527*67e74705SXin Li       if (Type.isUsable()) {
3528*67e74705SXin Li         if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc, StartLoc,
3529*67e74705SXin Li                                PrevSpec, DiagID, Type.get(),
3530*67e74705SXin Li                                Actions.getASTContext().getPrintingPolicy()))
3531*67e74705SXin Li           Diag(StartLoc, DiagID) << PrevSpec;
3532*67e74705SXin Li 
3533*67e74705SXin Li         DS.SetRangeEnd(EndLoc);
3534*67e74705SXin Li       } else {
3535*67e74705SXin Li         DS.SetTypeSpecError();
3536*67e74705SXin Li       }
3537*67e74705SXin Li 
3538*67e74705SXin Li       // Need to support trailing type qualifiers (e.g. "id<p> const").
3539*67e74705SXin Li       // If a type specifier follows, it will be diagnosed elsewhere.
3540*67e74705SXin Li       continue;
3541*67e74705SXin Li     }
3542*67e74705SXin Li     // If the specifier wasn't legal, issue a diagnostic.
3543*67e74705SXin Li     if (isInvalid) {
3544*67e74705SXin Li       assert(PrevSpec && "Method did not return previous specifier!");
3545*67e74705SXin Li       assert(DiagID);
3546*67e74705SXin Li 
3547*67e74705SXin Li       if (DiagID == diag::ext_duplicate_declspec)
3548*67e74705SXin Li         Diag(Tok, DiagID)
3549*67e74705SXin Li           << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3550*67e74705SXin Li       else if (DiagID == diag::err_opencl_unknown_type_specifier) {
3551*67e74705SXin Li         const int OpenCLVer = getLangOpts().OpenCLVersion;
3552*67e74705SXin Li         std::string VerSpec = llvm::to_string(OpenCLVer / 100) +
3553*67e74705SXin Li                               std::string (".") +
3554*67e74705SXin Li                               llvm::to_string((OpenCLVer % 100) / 10);
3555*67e74705SXin Li         Diag(Tok, DiagID) << VerSpec << PrevSpec << isStorageClass;
3556*67e74705SXin Li       } else
3557*67e74705SXin Li         Diag(Tok, DiagID) << PrevSpec;
3558*67e74705SXin Li     }
3559*67e74705SXin Li 
3560*67e74705SXin Li     DS.SetRangeEnd(Tok.getLocation());
3561*67e74705SXin Li     if (DiagID != diag::err_bool_redeclaration)
3562*67e74705SXin Li       ConsumeToken();
3563*67e74705SXin Li 
3564*67e74705SXin Li     AttrsLastTime = false;
3565*67e74705SXin Li   }
3566*67e74705SXin Li }
3567*67e74705SXin Li 
3568*67e74705SXin Li /// ParseStructDeclaration - Parse a struct declaration without the terminating
3569*67e74705SXin Li /// semicolon.
3570*67e74705SXin Li ///
3571*67e74705SXin Li ///       struct-declaration:
3572*67e74705SXin Li ///         specifier-qualifier-list struct-declarator-list
3573*67e74705SXin Li /// [GNU]   __extension__ struct-declaration
3574*67e74705SXin Li /// [GNU]   specifier-qualifier-list
3575*67e74705SXin Li ///       struct-declarator-list:
3576*67e74705SXin Li ///         struct-declarator
3577*67e74705SXin Li ///         struct-declarator-list ',' struct-declarator
3578*67e74705SXin Li /// [GNU]   struct-declarator-list ',' attributes[opt] struct-declarator
3579*67e74705SXin Li ///       struct-declarator:
3580*67e74705SXin Li ///         declarator
3581*67e74705SXin Li /// [GNU]   declarator attributes[opt]
3582*67e74705SXin Li ///         declarator[opt] ':' constant-expression
3583*67e74705SXin Li /// [GNU]   declarator[opt] ':' constant-expression attributes[opt]
3584*67e74705SXin Li ///
ParseStructDeclaration(ParsingDeclSpec & DS,llvm::function_ref<void (ParsingFieldDeclarator &)> FieldsCallback)3585*67e74705SXin Li void Parser::ParseStructDeclaration(
3586*67e74705SXin Li     ParsingDeclSpec &DS,
3587*67e74705SXin Li     llvm::function_ref<void(ParsingFieldDeclarator &)> FieldsCallback) {
3588*67e74705SXin Li 
3589*67e74705SXin Li   if (Tok.is(tok::kw___extension__)) {
3590*67e74705SXin Li     // __extension__ silences extension warnings in the subexpression.
3591*67e74705SXin Li     ExtensionRAIIObject O(Diags);  // Use RAII to do this.
3592*67e74705SXin Li     ConsumeToken();
3593*67e74705SXin Li     return ParseStructDeclaration(DS, FieldsCallback);
3594*67e74705SXin Li   }
3595*67e74705SXin Li 
3596*67e74705SXin Li   // Parse the common specifier-qualifiers-list piece.
3597*67e74705SXin Li   ParseSpecifierQualifierList(DS);
3598*67e74705SXin Li 
3599*67e74705SXin Li   // If there are no declarators, this is a free-standing declaration
3600*67e74705SXin Li   // specifier. Let the actions module cope with it.
3601*67e74705SXin Li   if (Tok.is(tok::semi)) {
3602*67e74705SXin Li     RecordDecl *AnonRecord = nullptr;
3603*67e74705SXin Li     Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3604*67e74705SXin Li                                                        DS, AnonRecord);
3605*67e74705SXin Li     assert(!AnonRecord && "Did not expect anonymous struct or union here");
3606*67e74705SXin Li     DS.complete(TheDecl);
3607*67e74705SXin Li     return;
3608*67e74705SXin Li   }
3609*67e74705SXin Li 
3610*67e74705SXin Li   // Read struct-declarators until we find the semicolon.
3611*67e74705SXin Li   bool FirstDeclarator = true;
3612*67e74705SXin Li   SourceLocation CommaLoc;
3613*67e74705SXin Li   while (1) {
3614*67e74705SXin Li     ParsingFieldDeclarator DeclaratorInfo(*this, DS);
3615*67e74705SXin Li     DeclaratorInfo.D.setCommaLoc(CommaLoc);
3616*67e74705SXin Li 
3617*67e74705SXin Li     // Attributes are only allowed here on successive declarators.
3618*67e74705SXin Li     if (!FirstDeclarator)
3619*67e74705SXin Li       MaybeParseGNUAttributes(DeclaratorInfo.D);
3620*67e74705SXin Li 
3621*67e74705SXin Li     /// struct-declarator: declarator
3622*67e74705SXin Li     /// struct-declarator: declarator[opt] ':' constant-expression
3623*67e74705SXin Li     if (Tok.isNot(tok::colon)) {
3624*67e74705SXin Li       // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3625*67e74705SXin Li       ColonProtectionRAIIObject X(*this);
3626*67e74705SXin Li       ParseDeclarator(DeclaratorInfo.D);
3627*67e74705SXin Li     } else
3628*67e74705SXin Li       DeclaratorInfo.D.SetIdentifier(nullptr, Tok.getLocation());
3629*67e74705SXin Li 
3630*67e74705SXin Li     if (TryConsumeToken(tok::colon)) {
3631*67e74705SXin Li       ExprResult Res(ParseConstantExpression());
3632*67e74705SXin Li       if (Res.isInvalid())
3633*67e74705SXin Li         SkipUntil(tok::semi, StopBeforeMatch);
3634*67e74705SXin Li       else
3635*67e74705SXin Li         DeclaratorInfo.BitfieldSize = Res.get();
3636*67e74705SXin Li     }
3637*67e74705SXin Li 
3638*67e74705SXin Li     // If attributes exist after the declarator, parse them.
3639*67e74705SXin Li     MaybeParseGNUAttributes(DeclaratorInfo.D);
3640*67e74705SXin Li 
3641*67e74705SXin Li     // We're done with this declarator;  invoke the callback.
3642*67e74705SXin Li     FieldsCallback(DeclaratorInfo);
3643*67e74705SXin Li 
3644*67e74705SXin Li     // If we don't have a comma, it is either the end of the list (a ';')
3645*67e74705SXin Li     // or an error, bail out.
3646*67e74705SXin Li     if (!TryConsumeToken(tok::comma, CommaLoc))
3647*67e74705SXin Li       return;
3648*67e74705SXin Li 
3649*67e74705SXin Li     FirstDeclarator = false;
3650*67e74705SXin Li   }
3651*67e74705SXin Li }
3652*67e74705SXin Li 
3653*67e74705SXin Li /// ParseStructUnionBody
3654*67e74705SXin Li ///       struct-contents:
3655*67e74705SXin Li ///         struct-declaration-list
3656*67e74705SXin Li /// [EXT]   empty
3657*67e74705SXin Li /// [GNU]   "struct-declaration-list" without terminatoring ';'
3658*67e74705SXin Li ///       struct-declaration-list:
3659*67e74705SXin Li ///         struct-declaration
3660*67e74705SXin Li ///         struct-declaration-list struct-declaration
3661*67e74705SXin Li /// [OBC]   '@' 'defs' '(' class-name ')'
3662*67e74705SXin Li ///
ParseStructUnionBody(SourceLocation RecordLoc,unsigned TagType,Decl * TagDecl)3663*67e74705SXin Li void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
3664*67e74705SXin Li                                   unsigned TagType, Decl *TagDecl) {
3665*67e74705SXin Li   PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3666*67e74705SXin Li                                       "parsing struct/union body");
3667*67e74705SXin Li   assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
3668*67e74705SXin Li 
3669*67e74705SXin Li   BalancedDelimiterTracker T(*this, tok::l_brace);
3670*67e74705SXin Li   if (T.consumeOpen())
3671*67e74705SXin Li     return;
3672*67e74705SXin Li 
3673*67e74705SXin Li   ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
3674*67e74705SXin Li   Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
3675*67e74705SXin Li 
3676*67e74705SXin Li   SmallVector<Decl *, 32> FieldDecls;
3677*67e74705SXin Li 
3678*67e74705SXin Li   // While we still have something to read, read the declarations in the struct.
3679*67e74705SXin Li   while (!tryParseMisplacedModuleImport() && Tok.isNot(tok::r_brace) &&
3680*67e74705SXin Li          Tok.isNot(tok::eof)) {
3681*67e74705SXin Li     // Each iteration of this loop reads one struct-declaration.
3682*67e74705SXin Li 
3683*67e74705SXin Li     // Check for extraneous top-level semicolon.
3684*67e74705SXin Li     if (Tok.is(tok::semi)) {
3685*67e74705SXin Li       ConsumeExtraSemi(InsideStruct, TagType);
3686*67e74705SXin Li       continue;
3687*67e74705SXin Li     }
3688*67e74705SXin Li 
3689*67e74705SXin Li     // Parse _Static_assert declaration.
3690*67e74705SXin Li     if (Tok.is(tok::kw__Static_assert)) {
3691*67e74705SXin Li       SourceLocation DeclEnd;
3692*67e74705SXin Li       ParseStaticAssertDeclaration(DeclEnd);
3693*67e74705SXin Li       continue;
3694*67e74705SXin Li     }
3695*67e74705SXin Li 
3696*67e74705SXin Li     if (Tok.is(tok::annot_pragma_pack)) {
3697*67e74705SXin Li       HandlePragmaPack();
3698*67e74705SXin Li       continue;
3699*67e74705SXin Li     }
3700*67e74705SXin Li 
3701*67e74705SXin Li     if (Tok.is(tok::annot_pragma_align)) {
3702*67e74705SXin Li       HandlePragmaAlign();
3703*67e74705SXin Li       continue;
3704*67e74705SXin Li     }
3705*67e74705SXin Li 
3706*67e74705SXin Li     if (Tok.is(tok::annot_pragma_openmp)) {
3707*67e74705SXin Li       // Result can be ignored, because it must be always empty.
3708*67e74705SXin Li       AccessSpecifier AS = AS_none;
3709*67e74705SXin Li       ParsedAttributesWithRange Attrs(AttrFactory);
3710*67e74705SXin Li       (void)ParseOpenMPDeclarativeDirectiveWithExtDecl(AS, Attrs);
3711*67e74705SXin Li       continue;
3712*67e74705SXin Li     }
3713*67e74705SXin Li 
3714*67e74705SXin Li     if (!Tok.is(tok::at)) {
3715*67e74705SXin Li       auto CFieldCallback = [&](ParsingFieldDeclarator &FD) {
3716*67e74705SXin Li         // Install the declarator into the current TagDecl.
3717*67e74705SXin Li         Decl *Field =
3718*67e74705SXin Li             Actions.ActOnField(getCurScope(), TagDecl,
3719*67e74705SXin Li                                FD.D.getDeclSpec().getSourceRange().getBegin(),
3720*67e74705SXin Li                                FD.D, FD.BitfieldSize);
3721*67e74705SXin Li         FieldDecls.push_back(Field);
3722*67e74705SXin Li         FD.complete(Field);
3723*67e74705SXin Li       };
3724*67e74705SXin Li 
3725*67e74705SXin Li       // Parse all the comma separated declarators.
3726*67e74705SXin Li       ParsingDeclSpec DS(*this);
3727*67e74705SXin Li       ParseStructDeclaration(DS, CFieldCallback);
3728*67e74705SXin Li     } else { // Handle @defs
3729*67e74705SXin Li       ConsumeToken();
3730*67e74705SXin Li       if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3731*67e74705SXin Li         Diag(Tok, diag::err_unexpected_at);
3732*67e74705SXin Li         SkipUntil(tok::semi);
3733*67e74705SXin Li         continue;
3734*67e74705SXin Li       }
3735*67e74705SXin Li       ConsumeToken();
3736*67e74705SXin Li       ExpectAndConsume(tok::l_paren);
3737*67e74705SXin Li       if (!Tok.is(tok::identifier)) {
3738*67e74705SXin Li         Diag(Tok, diag::err_expected) << tok::identifier;
3739*67e74705SXin Li         SkipUntil(tok::semi);
3740*67e74705SXin Li         continue;
3741*67e74705SXin Li       }
3742*67e74705SXin Li       SmallVector<Decl *, 16> Fields;
3743*67e74705SXin Li       Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
3744*67e74705SXin Li                         Tok.getIdentifierInfo(), Fields);
3745*67e74705SXin Li       FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3746*67e74705SXin Li       ConsumeToken();
3747*67e74705SXin Li       ExpectAndConsume(tok::r_paren);
3748*67e74705SXin Li     }
3749*67e74705SXin Li 
3750*67e74705SXin Li     if (TryConsumeToken(tok::semi))
3751*67e74705SXin Li       continue;
3752*67e74705SXin Li 
3753*67e74705SXin Li     if (Tok.is(tok::r_brace)) {
3754*67e74705SXin Li       ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
3755*67e74705SXin Li       break;
3756*67e74705SXin Li     }
3757*67e74705SXin Li 
3758*67e74705SXin Li     ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3759*67e74705SXin Li     // Skip to end of block or statement to avoid ext-warning on extra ';'.
3760*67e74705SXin Li     SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
3761*67e74705SXin Li     // If we stopped at a ';', eat it.
3762*67e74705SXin Li     TryConsumeToken(tok::semi);
3763*67e74705SXin Li   }
3764*67e74705SXin Li 
3765*67e74705SXin Li   T.consumeClose();
3766*67e74705SXin Li 
3767*67e74705SXin Li   ParsedAttributes attrs(AttrFactory);
3768*67e74705SXin Li   // If attributes exist after struct contents, parse them.
3769*67e74705SXin Li   MaybeParseGNUAttributes(attrs);
3770*67e74705SXin Li 
3771*67e74705SXin Li   Actions.ActOnFields(getCurScope(),
3772*67e74705SXin Li                       RecordLoc, TagDecl, FieldDecls,
3773*67e74705SXin Li                       T.getOpenLocation(), T.getCloseLocation(),
3774*67e74705SXin Li                       attrs.getList());
3775*67e74705SXin Li   StructScope.Exit();
3776*67e74705SXin Li   Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3777*67e74705SXin Li                                    T.getCloseLocation());
3778*67e74705SXin Li }
3779*67e74705SXin Li 
3780*67e74705SXin Li /// ParseEnumSpecifier
3781*67e74705SXin Li ///       enum-specifier: [C99 6.7.2.2]
3782*67e74705SXin Li ///         'enum' identifier[opt] '{' enumerator-list '}'
3783*67e74705SXin Li ///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
3784*67e74705SXin Li /// [GNU]   'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3785*67e74705SXin Li ///                                                 '}' attributes[opt]
3786*67e74705SXin Li /// [MS]    'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3787*67e74705SXin Li ///                                                 '}'
3788*67e74705SXin Li ///         'enum' identifier
3789*67e74705SXin Li /// [GNU]   'enum' attributes[opt] identifier
3790*67e74705SXin Li ///
3791*67e74705SXin Li /// [C++11] enum-head '{' enumerator-list[opt] '}'
3792*67e74705SXin Li /// [C++11] enum-head '{' enumerator-list ','  '}'
3793*67e74705SXin Li ///
3794*67e74705SXin Li ///       enum-head: [C++11]
3795*67e74705SXin Li ///         enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3796*67e74705SXin Li ///         enum-key attribute-specifier-seq[opt] nested-name-specifier
3797*67e74705SXin Li ///             identifier enum-base[opt]
3798*67e74705SXin Li ///
3799*67e74705SXin Li ///       enum-key: [C++11]
3800*67e74705SXin Li ///         'enum'
3801*67e74705SXin Li ///         'enum' 'class'
3802*67e74705SXin Li ///         'enum' 'struct'
3803*67e74705SXin Li ///
3804*67e74705SXin Li ///       enum-base: [C++11]
3805*67e74705SXin Li ///         ':' type-specifier-seq
3806*67e74705SXin Li ///
3807*67e74705SXin Li /// [C++] elaborated-type-specifier:
3808*67e74705SXin Li /// [C++]   'enum' '::'[opt] nested-name-specifier[opt] identifier
3809*67e74705SXin Li ///
ParseEnumSpecifier(SourceLocation StartLoc,DeclSpec & DS,const ParsedTemplateInfo & TemplateInfo,AccessSpecifier AS,DeclSpecContext DSC)3810*67e74705SXin Li void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
3811*67e74705SXin Li                                 const ParsedTemplateInfo &TemplateInfo,
3812*67e74705SXin Li                                 AccessSpecifier AS, DeclSpecContext DSC) {
3813*67e74705SXin Li   // Parse the tag portion of this.
3814*67e74705SXin Li   if (Tok.is(tok::code_completion)) {
3815*67e74705SXin Li     // Code completion for an enum name.
3816*67e74705SXin Li     Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
3817*67e74705SXin Li     return cutOffParsing();
3818*67e74705SXin Li   }
3819*67e74705SXin Li 
3820*67e74705SXin Li   // If attributes exist after tag, parse them.
3821*67e74705SXin Li   ParsedAttributesWithRange attrs(AttrFactory);
3822*67e74705SXin Li   MaybeParseGNUAttributes(attrs);
3823*67e74705SXin Li   MaybeParseCXX11Attributes(attrs);
3824*67e74705SXin Li   MaybeParseMicrosoftDeclSpecs(attrs);
3825*67e74705SXin Li 
3826*67e74705SXin Li   SourceLocation ScopedEnumKWLoc;
3827*67e74705SXin Li   bool IsScopedUsingClassTag = false;
3828*67e74705SXin Li 
3829*67e74705SXin Li   // In C++11, recognize 'enum class' and 'enum struct'.
3830*67e74705SXin Li   if (Tok.isOneOf(tok::kw_class, tok::kw_struct)) {
3831*67e74705SXin Li     Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3832*67e74705SXin Li                                         : diag::ext_scoped_enum);
3833*67e74705SXin Li     IsScopedUsingClassTag = Tok.is(tok::kw_class);
3834*67e74705SXin Li     ScopedEnumKWLoc = ConsumeToken();
3835*67e74705SXin Li 
3836*67e74705SXin Li     // Attributes are not allowed between these keywords.  Diagnose,
3837*67e74705SXin Li     // but then just treat them like they appeared in the right place.
3838*67e74705SXin Li     ProhibitAttributes(attrs);
3839*67e74705SXin Li 
3840*67e74705SXin Li     // They are allowed afterwards, though.
3841*67e74705SXin Li     MaybeParseGNUAttributes(attrs);
3842*67e74705SXin Li     MaybeParseCXX11Attributes(attrs);
3843*67e74705SXin Li     MaybeParseMicrosoftDeclSpecs(attrs);
3844*67e74705SXin Li   }
3845*67e74705SXin Li 
3846*67e74705SXin Li   // C++11 [temp.explicit]p12:
3847*67e74705SXin Li   //   The usual access controls do not apply to names used to specify
3848*67e74705SXin Li   //   explicit instantiations.
3849*67e74705SXin Li   // We extend this to also cover explicit specializations.  Note that
3850*67e74705SXin Li   // we don't suppress if this turns out to be an elaborated type
3851*67e74705SXin Li   // specifier.
3852*67e74705SXin Li   bool shouldDelayDiagsInTag =
3853*67e74705SXin Li     (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3854*67e74705SXin Li      TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3855*67e74705SXin Li   SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
3856*67e74705SXin Li 
3857*67e74705SXin Li   // Enum definitions should not be parsed in a trailing-return-type.
3858*67e74705SXin Li   bool AllowDeclaration = DSC != DSC_trailing;
3859*67e74705SXin Li 
3860*67e74705SXin Li   bool AllowFixedUnderlyingType = AllowDeclaration &&
3861*67e74705SXin Li     (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
3862*67e74705SXin Li      getLangOpts().ObjC2);
3863*67e74705SXin Li 
3864*67e74705SXin Li   CXXScopeSpec &SS = DS.getTypeSpecScope();
3865*67e74705SXin Li   if (getLangOpts().CPlusPlus) {
3866*67e74705SXin Li     // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3867*67e74705SXin Li     // if a fixed underlying type is allowed.
3868*67e74705SXin Li     ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
3869*67e74705SXin Li 
3870*67e74705SXin Li     CXXScopeSpec Spec;
3871*67e74705SXin Li     if (ParseOptionalCXXScopeSpecifier(Spec, nullptr,
3872*67e74705SXin Li                                        /*EnteringContext=*/true))
3873*67e74705SXin Li       return;
3874*67e74705SXin Li 
3875*67e74705SXin Li     if (Spec.isSet() && Tok.isNot(tok::identifier)) {
3876*67e74705SXin Li       Diag(Tok, diag::err_expected) << tok::identifier;
3877*67e74705SXin Li       if (Tok.isNot(tok::l_brace)) {
3878*67e74705SXin Li         // Has no name and is not a definition.
3879*67e74705SXin Li         // Skip the rest of this declarator, up until the comma or semicolon.
3880*67e74705SXin Li         SkipUntil(tok::comma, StopAtSemi);
3881*67e74705SXin Li         return;
3882*67e74705SXin Li       }
3883*67e74705SXin Li     }
3884*67e74705SXin Li 
3885*67e74705SXin Li     SS = Spec;
3886*67e74705SXin Li   }
3887*67e74705SXin Li 
3888*67e74705SXin Li   // Must have either 'enum name' or 'enum {...}'.
3889*67e74705SXin Li   if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
3890*67e74705SXin Li       !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
3891*67e74705SXin Li     Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
3892*67e74705SXin Li 
3893*67e74705SXin Li     // Skip the rest of this declarator, up until the comma or semicolon.
3894*67e74705SXin Li     SkipUntil(tok::comma, StopAtSemi);
3895*67e74705SXin Li     return;
3896*67e74705SXin Li   }
3897*67e74705SXin Li 
3898*67e74705SXin Li   // If an identifier is present, consume and remember it.
3899*67e74705SXin Li   IdentifierInfo *Name = nullptr;
3900*67e74705SXin Li   SourceLocation NameLoc;
3901*67e74705SXin Li   if (Tok.is(tok::identifier)) {
3902*67e74705SXin Li     Name = Tok.getIdentifierInfo();
3903*67e74705SXin Li     NameLoc = ConsumeToken();
3904*67e74705SXin Li   }
3905*67e74705SXin Li 
3906*67e74705SXin Li   if (!Name && ScopedEnumKWLoc.isValid()) {
3907*67e74705SXin Li     // C++0x 7.2p2: The optional identifier shall not be omitted in the
3908*67e74705SXin Li     // declaration of a scoped enumeration.
3909*67e74705SXin Li     Diag(Tok, diag::err_scoped_enum_missing_identifier);
3910*67e74705SXin Li     ScopedEnumKWLoc = SourceLocation();
3911*67e74705SXin Li     IsScopedUsingClassTag = false;
3912*67e74705SXin Li   }
3913*67e74705SXin Li 
3914*67e74705SXin Li   // Okay, end the suppression area.  We'll decide whether to emit the
3915*67e74705SXin Li   // diagnostics in a second.
3916*67e74705SXin Li   if (shouldDelayDiagsInTag)
3917*67e74705SXin Li     diagsFromTag.done();
3918*67e74705SXin Li 
3919*67e74705SXin Li   TypeResult BaseType;
3920*67e74705SXin Li 
3921*67e74705SXin Li   // Parse the fixed underlying type.
3922*67e74705SXin Li   bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3923*67e74705SXin Li   if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
3924*67e74705SXin Li     bool PossibleBitfield = false;
3925*67e74705SXin Li     if (CanBeBitfield) {
3926*67e74705SXin Li       // If we're in class scope, this can either be an enum declaration with
3927*67e74705SXin Li       // an underlying type, or a declaration of a bitfield member. We try to
3928*67e74705SXin Li       // use a simple disambiguation scheme first to catch the common cases
3929*67e74705SXin Li       // (integer literal, sizeof); if it's still ambiguous, we then consider
3930*67e74705SXin Li       // anything that's a simple-type-specifier followed by '(' as an
3931*67e74705SXin Li       // expression. This suffices because function types are not valid
3932*67e74705SXin Li       // underlying types anyway.
3933*67e74705SXin Li       EnterExpressionEvaluationContext Unevaluated(Actions,
3934*67e74705SXin Li                                                    Sema::ConstantEvaluated);
3935*67e74705SXin Li       TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
3936*67e74705SXin Li       // If the next token starts an expression, we know we're parsing a
3937*67e74705SXin Li       // bit-field. This is the common case.
3938*67e74705SXin Li       if (TPR == TPResult::True)
3939*67e74705SXin Li         PossibleBitfield = true;
3940*67e74705SXin Li       // If the next token starts a type-specifier-seq, it may be either a
3941*67e74705SXin Li       // a fixed underlying type or the start of a function-style cast in C++;
3942*67e74705SXin Li       // lookahead one more token to see if it's obvious that we have a
3943*67e74705SXin Li       // fixed underlying type.
3944*67e74705SXin Li       else if (TPR == TPResult::False &&
3945*67e74705SXin Li                GetLookAheadToken(2).getKind() == tok::semi) {
3946*67e74705SXin Li         // Consume the ':'.
3947*67e74705SXin Li         ConsumeToken();
3948*67e74705SXin Li       } else {
3949*67e74705SXin Li         // We have the start of a type-specifier-seq, so we have to perform
3950*67e74705SXin Li         // tentative parsing to determine whether we have an expression or a
3951*67e74705SXin Li         // type.
3952*67e74705SXin Li         TentativeParsingAction TPA(*this);
3953*67e74705SXin Li 
3954*67e74705SXin Li         // Consume the ':'.
3955*67e74705SXin Li         ConsumeToken();
3956*67e74705SXin Li 
3957*67e74705SXin Li         // If we see a type specifier followed by an open-brace, we have an
3958*67e74705SXin Li         // ambiguity between an underlying type and a C++11 braced
3959*67e74705SXin Li         // function-style cast. Resolve this by always treating it as an
3960*67e74705SXin Li         // underlying type.
3961*67e74705SXin Li         // FIXME: The standard is not entirely clear on how to disambiguate in
3962*67e74705SXin Li         // this case.
3963*67e74705SXin Li         if ((getLangOpts().CPlusPlus &&
3964*67e74705SXin Li              isCXXDeclarationSpecifier(TPResult::True) != TPResult::True) ||
3965*67e74705SXin Li             (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
3966*67e74705SXin Li           // We'll parse this as a bitfield later.
3967*67e74705SXin Li           PossibleBitfield = true;
3968*67e74705SXin Li           TPA.Revert();
3969*67e74705SXin Li         } else {
3970*67e74705SXin Li           // We have a type-specifier-seq.
3971*67e74705SXin Li           TPA.Commit();
3972*67e74705SXin Li         }
3973*67e74705SXin Li       }
3974*67e74705SXin Li     } else {
3975*67e74705SXin Li       // Consume the ':'.
3976*67e74705SXin Li       ConsumeToken();
3977*67e74705SXin Li     }
3978*67e74705SXin Li 
3979*67e74705SXin Li     if (!PossibleBitfield) {
3980*67e74705SXin Li       SourceRange Range;
3981*67e74705SXin Li       BaseType = ParseTypeName(&Range);
3982*67e74705SXin Li 
3983*67e74705SXin Li       if (getLangOpts().CPlusPlus11) {
3984*67e74705SXin Li         Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
3985*67e74705SXin Li       } else if (!getLangOpts().ObjC2) {
3986*67e74705SXin Li         if (getLangOpts().CPlusPlus)
3987*67e74705SXin Li           Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3988*67e74705SXin Li         else
3989*67e74705SXin Li           Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3990*67e74705SXin Li       }
3991*67e74705SXin Li     }
3992*67e74705SXin Li   }
3993*67e74705SXin Li 
3994*67e74705SXin Li   // There are four options here.  If we have 'friend enum foo;' then this is a
3995*67e74705SXin Li   // friend declaration, and cannot have an accompanying definition. If we have
3996*67e74705SXin Li   // 'enum foo;', then this is a forward declaration.  If we have
3997*67e74705SXin Li   // 'enum foo {...' then this is a definition. Otherwise we have something
3998*67e74705SXin Li   // like 'enum foo xyz', a reference.
3999*67e74705SXin Li   //
4000*67e74705SXin Li   // This is needed to handle stuff like this right (C99 6.7.2.3p11):
4001*67e74705SXin Li   // enum foo {..};  void bar() { enum foo; }    <- new foo in bar.
4002*67e74705SXin Li   // enum foo {..};  void bar() { enum foo x; }  <- use of old foo.
4003*67e74705SXin Li   //
4004*67e74705SXin Li   Sema::TagUseKind TUK;
4005*67e74705SXin Li   if (!AllowDeclaration) {
4006*67e74705SXin Li     TUK = Sema::TUK_Reference;
4007*67e74705SXin Li   } else if (Tok.is(tok::l_brace)) {
4008*67e74705SXin Li     if (DS.isFriendSpecified()) {
4009*67e74705SXin Li       Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
4010*67e74705SXin Li         << SourceRange(DS.getFriendSpecLoc());
4011*67e74705SXin Li       ConsumeBrace();
4012*67e74705SXin Li       SkipUntil(tok::r_brace, StopAtSemi);
4013*67e74705SXin Li       TUK = Sema::TUK_Friend;
4014*67e74705SXin Li     } else {
4015*67e74705SXin Li       TUK = Sema::TUK_Definition;
4016*67e74705SXin Li     }
4017*67e74705SXin Li   } else if (!isTypeSpecifier(DSC) &&
4018*67e74705SXin Li              (Tok.is(tok::semi) ||
4019*67e74705SXin Li               (Tok.isAtStartOfLine() &&
4020*67e74705SXin Li                !isValidAfterTypeSpecifier(CanBeBitfield)))) {
4021*67e74705SXin Li     TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
4022*67e74705SXin Li     if (Tok.isNot(tok::semi)) {
4023*67e74705SXin Li       // A semicolon was missing after this declaration. Diagnose and recover.
4024*67e74705SXin Li       ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
4025*67e74705SXin Li       PP.EnterToken(Tok);
4026*67e74705SXin Li       Tok.setKind(tok::semi);
4027*67e74705SXin Li     }
4028*67e74705SXin Li   } else {
4029*67e74705SXin Li     TUK = Sema::TUK_Reference;
4030*67e74705SXin Li   }
4031*67e74705SXin Li 
4032*67e74705SXin Li   // If this is an elaborated type specifier, and we delayed
4033*67e74705SXin Li   // diagnostics before, just merge them into the current pool.
4034*67e74705SXin Li   if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
4035*67e74705SXin Li     diagsFromTag.redelay();
4036*67e74705SXin Li   }
4037*67e74705SXin Li 
4038*67e74705SXin Li   MultiTemplateParamsArg TParams;
4039*67e74705SXin Li   if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
4040*67e74705SXin Li       TUK != Sema::TUK_Reference) {
4041*67e74705SXin Li     if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
4042*67e74705SXin Li       // Skip the rest of this declarator, up until the comma or semicolon.
4043*67e74705SXin Li       Diag(Tok, diag::err_enum_template);
4044*67e74705SXin Li       SkipUntil(tok::comma, StopAtSemi);
4045*67e74705SXin Li       return;
4046*67e74705SXin Li     }
4047*67e74705SXin Li 
4048*67e74705SXin Li     if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
4049*67e74705SXin Li       // Enumerations can't be explicitly instantiated.
4050*67e74705SXin Li       DS.SetTypeSpecError();
4051*67e74705SXin Li       Diag(StartLoc, diag::err_explicit_instantiation_enum);
4052*67e74705SXin Li       return;
4053*67e74705SXin Li     }
4054*67e74705SXin Li 
4055*67e74705SXin Li     assert(TemplateInfo.TemplateParams && "no template parameters");
4056*67e74705SXin Li     TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
4057*67e74705SXin Li                                      TemplateInfo.TemplateParams->size());
4058*67e74705SXin Li   }
4059*67e74705SXin Li 
4060*67e74705SXin Li   if (TUK == Sema::TUK_Reference)
4061*67e74705SXin Li     ProhibitAttributes(attrs);
4062*67e74705SXin Li 
4063*67e74705SXin Li   if (!Name && TUK != Sema::TUK_Definition) {
4064*67e74705SXin Li     Diag(Tok, diag::err_enumerator_unnamed_no_def);
4065*67e74705SXin Li 
4066*67e74705SXin Li     // Skip the rest of this declarator, up until the comma or semicolon.
4067*67e74705SXin Li     SkipUntil(tok::comma, StopAtSemi);
4068*67e74705SXin Li     return;
4069*67e74705SXin Li   }
4070*67e74705SXin Li 
4071*67e74705SXin Li   handleDeclspecAlignBeforeClassKey(attrs, DS, TUK);
4072*67e74705SXin Li 
4073*67e74705SXin Li   Sema::SkipBodyInfo SkipBody;
4074*67e74705SXin Li   if (!Name && TUK == Sema::TUK_Definition && Tok.is(tok::l_brace) &&
4075*67e74705SXin Li       NextToken().is(tok::identifier))
4076*67e74705SXin Li     SkipBody = Actions.shouldSkipAnonEnumBody(getCurScope(),
4077*67e74705SXin Li                                               NextToken().getIdentifierInfo(),
4078*67e74705SXin Li                                               NextToken().getLocation());
4079*67e74705SXin Li 
4080*67e74705SXin Li   bool Owned = false;
4081*67e74705SXin Li   bool IsDependent = false;
4082*67e74705SXin Li   const char *PrevSpec = nullptr;
4083*67e74705SXin Li   unsigned DiagID;
4084*67e74705SXin Li   Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
4085*67e74705SXin Li                                    StartLoc, SS, Name, NameLoc, attrs.getList(),
4086*67e74705SXin Li                                    AS, DS.getModulePrivateSpecLoc(), TParams,
4087*67e74705SXin Li                                    Owned, IsDependent, ScopedEnumKWLoc,
4088*67e74705SXin Li                                    IsScopedUsingClassTag, BaseType,
4089*67e74705SXin Li                                    DSC == DSC_type_specifier, &SkipBody);
4090*67e74705SXin Li 
4091*67e74705SXin Li   if (SkipBody.ShouldSkip) {
4092*67e74705SXin Li     assert(TUK == Sema::TUK_Definition && "can only skip a definition");
4093*67e74705SXin Li 
4094*67e74705SXin Li     BalancedDelimiterTracker T(*this, tok::l_brace);
4095*67e74705SXin Li     T.consumeOpen();
4096*67e74705SXin Li     T.skipToEnd();
4097*67e74705SXin Li 
4098*67e74705SXin Li     if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
4099*67e74705SXin Li                            NameLoc.isValid() ? NameLoc : StartLoc,
4100*67e74705SXin Li                            PrevSpec, DiagID, TagDecl, Owned,
4101*67e74705SXin Li                            Actions.getASTContext().getPrintingPolicy()))
4102*67e74705SXin Li       Diag(StartLoc, DiagID) << PrevSpec;
4103*67e74705SXin Li     return;
4104*67e74705SXin Li   }
4105*67e74705SXin Li 
4106*67e74705SXin Li   if (IsDependent) {
4107*67e74705SXin Li     // This enum has a dependent nested-name-specifier. Handle it as a
4108*67e74705SXin Li     // dependent tag.
4109*67e74705SXin Li     if (!Name) {
4110*67e74705SXin Li       DS.SetTypeSpecError();
4111*67e74705SXin Li       Diag(Tok, diag::err_expected_type_name_after_typename);
4112*67e74705SXin Li       return;
4113*67e74705SXin Li     }
4114*67e74705SXin Li 
4115*67e74705SXin Li     TypeResult Type = Actions.ActOnDependentTag(
4116*67e74705SXin Li         getCurScope(), DeclSpec::TST_enum, TUK, SS, Name, StartLoc, NameLoc);
4117*67e74705SXin Li     if (Type.isInvalid()) {
4118*67e74705SXin Li       DS.SetTypeSpecError();
4119*67e74705SXin Li       return;
4120*67e74705SXin Li     }
4121*67e74705SXin Li 
4122*67e74705SXin Li     if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
4123*67e74705SXin Li                            NameLoc.isValid() ? NameLoc : StartLoc,
4124*67e74705SXin Li                            PrevSpec, DiagID, Type.get(),
4125*67e74705SXin Li                            Actions.getASTContext().getPrintingPolicy()))
4126*67e74705SXin Li       Diag(StartLoc, DiagID) << PrevSpec;
4127*67e74705SXin Li 
4128*67e74705SXin Li     return;
4129*67e74705SXin Li   }
4130*67e74705SXin Li 
4131*67e74705SXin Li   if (!TagDecl) {
4132*67e74705SXin Li     // The action failed to produce an enumeration tag. If this is a
4133*67e74705SXin Li     // definition, consume the entire definition.
4134*67e74705SXin Li     if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
4135*67e74705SXin Li       ConsumeBrace();
4136*67e74705SXin Li       SkipUntil(tok::r_brace, StopAtSemi);
4137*67e74705SXin Li     }
4138*67e74705SXin Li 
4139*67e74705SXin Li     DS.SetTypeSpecError();
4140*67e74705SXin Li     return;
4141*67e74705SXin Li   }
4142*67e74705SXin Li 
4143*67e74705SXin Li   if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
4144*67e74705SXin Li     ParseEnumBody(StartLoc, TagDecl);
4145*67e74705SXin Li 
4146*67e74705SXin Li   if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
4147*67e74705SXin Li                          NameLoc.isValid() ? NameLoc : StartLoc,
4148*67e74705SXin Li                          PrevSpec, DiagID, TagDecl, Owned,
4149*67e74705SXin Li                          Actions.getASTContext().getPrintingPolicy()))
4150*67e74705SXin Li     Diag(StartLoc, DiagID) << PrevSpec;
4151*67e74705SXin Li }
4152*67e74705SXin Li 
4153*67e74705SXin Li /// ParseEnumBody - Parse a {} enclosed enumerator-list.
4154*67e74705SXin Li ///       enumerator-list:
4155*67e74705SXin Li ///         enumerator
4156*67e74705SXin Li ///         enumerator-list ',' enumerator
4157*67e74705SXin Li ///       enumerator:
4158*67e74705SXin Li ///         enumeration-constant attributes[opt]
4159*67e74705SXin Li ///         enumeration-constant attributes[opt] '=' constant-expression
4160*67e74705SXin Li ///       enumeration-constant:
4161*67e74705SXin Li ///         identifier
4162*67e74705SXin Li ///
ParseEnumBody(SourceLocation StartLoc,Decl * EnumDecl)4163*67e74705SXin Li void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
4164*67e74705SXin Li   // Enter the scope of the enum body and start the definition.
4165*67e74705SXin Li   ParseScope EnumScope(this, Scope::DeclScope | Scope::EnumScope);
4166*67e74705SXin Li   Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
4167*67e74705SXin Li 
4168*67e74705SXin Li   BalancedDelimiterTracker T(*this, tok::l_brace);
4169*67e74705SXin Li   T.consumeOpen();
4170*67e74705SXin Li 
4171*67e74705SXin Li   // C does not allow an empty enumerator-list, C++ does [dcl.enum].
4172*67e74705SXin Li   if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
4173*67e74705SXin Li     Diag(Tok, diag::error_empty_enum);
4174*67e74705SXin Li 
4175*67e74705SXin Li   SmallVector<Decl *, 32> EnumConstantDecls;
4176*67e74705SXin Li   SmallVector<SuppressAccessChecks, 32> EnumAvailabilityDiags;
4177*67e74705SXin Li 
4178*67e74705SXin Li   Decl *LastEnumConstDecl = nullptr;
4179*67e74705SXin Li 
4180*67e74705SXin Li   // Parse the enumerator-list.
4181*67e74705SXin Li   while (Tok.isNot(tok::r_brace)) {
4182*67e74705SXin Li     // Parse enumerator. If failed, try skipping till the start of the next
4183*67e74705SXin Li     // enumerator definition.
4184*67e74705SXin Li     if (Tok.isNot(tok::identifier)) {
4185*67e74705SXin Li       Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
4186*67e74705SXin Li       if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) &&
4187*67e74705SXin Li           TryConsumeToken(tok::comma))
4188*67e74705SXin Li         continue;
4189*67e74705SXin Li       break;
4190*67e74705SXin Li     }
4191*67e74705SXin Li     IdentifierInfo *Ident = Tok.getIdentifierInfo();
4192*67e74705SXin Li     SourceLocation IdentLoc = ConsumeToken();
4193*67e74705SXin Li 
4194*67e74705SXin Li     // If attributes exist after the enumerator, parse them.
4195*67e74705SXin Li     ParsedAttributesWithRange attrs(AttrFactory);
4196*67e74705SXin Li     MaybeParseGNUAttributes(attrs);
4197*67e74705SXin Li     ProhibitAttributes(attrs); // GNU-style attributes are prohibited.
4198*67e74705SXin Li     if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) {
4199*67e74705SXin Li       if (!getLangOpts().CPlusPlus1z)
4200*67e74705SXin Li         Diag(Tok.getLocation(), diag::warn_cxx14_compat_attribute)
4201*67e74705SXin Li             << 1 /*enumerator*/;
4202*67e74705SXin Li       ParseCXX11Attributes(attrs);
4203*67e74705SXin Li     }
4204*67e74705SXin Li 
4205*67e74705SXin Li     SourceLocation EqualLoc;
4206*67e74705SXin Li     ExprResult AssignedVal;
4207*67e74705SXin Li     EnumAvailabilityDiags.emplace_back(*this);
4208*67e74705SXin Li 
4209*67e74705SXin Li     if (TryConsumeToken(tok::equal, EqualLoc)) {
4210*67e74705SXin Li       AssignedVal = ParseConstantExpression();
4211*67e74705SXin Li       if (AssignedVal.isInvalid())
4212*67e74705SXin Li         SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch);
4213*67e74705SXin Li     }
4214*67e74705SXin Li 
4215*67e74705SXin Li     // Install the enumerator constant into EnumDecl.
4216*67e74705SXin Li     Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
4217*67e74705SXin Li                                                     LastEnumConstDecl,
4218*67e74705SXin Li                                                     IdentLoc, Ident,
4219*67e74705SXin Li                                                     attrs.getList(), EqualLoc,
4220*67e74705SXin Li                                                     AssignedVal.get());
4221*67e74705SXin Li     EnumAvailabilityDiags.back().done();
4222*67e74705SXin Li 
4223*67e74705SXin Li     EnumConstantDecls.push_back(EnumConstDecl);
4224*67e74705SXin Li     LastEnumConstDecl = EnumConstDecl;
4225*67e74705SXin Li 
4226*67e74705SXin Li     if (Tok.is(tok::identifier)) {
4227*67e74705SXin Li       // We're missing a comma between enumerators.
4228*67e74705SXin Li       SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
4229*67e74705SXin Li       Diag(Loc, diag::err_enumerator_list_missing_comma)
4230*67e74705SXin Li         << FixItHint::CreateInsertion(Loc, ", ");
4231*67e74705SXin Li       continue;
4232*67e74705SXin Li     }
4233*67e74705SXin Li 
4234*67e74705SXin Li     // Emumerator definition must be finished, only comma or r_brace are
4235*67e74705SXin Li     // allowed here.
4236*67e74705SXin Li     SourceLocation CommaLoc;
4237*67e74705SXin Li     if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) {
4238*67e74705SXin Li       if (EqualLoc.isValid())
4239*67e74705SXin Li         Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace
4240*67e74705SXin Li                                                            << tok::comma;
4241*67e74705SXin Li       else
4242*67e74705SXin Li         Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator);
4243*67e74705SXin Li       if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) {
4244*67e74705SXin Li         if (TryConsumeToken(tok::comma, CommaLoc))
4245*67e74705SXin Li           continue;
4246*67e74705SXin Li       } else {
4247*67e74705SXin Li         break;
4248*67e74705SXin Li       }
4249*67e74705SXin Li     }
4250*67e74705SXin Li 
4251*67e74705SXin Li     // If comma is followed by r_brace, emit appropriate warning.
4252*67e74705SXin Li     if (Tok.is(tok::r_brace) && CommaLoc.isValid()) {
4253*67e74705SXin Li       if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
4254*67e74705SXin Li         Diag(CommaLoc, getLangOpts().CPlusPlus ?
4255*67e74705SXin Li                diag::ext_enumerator_list_comma_cxx :
4256*67e74705SXin Li                diag::ext_enumerator_list_comma_c)
4257*67e74705SXin Li           << FixItHint::CreateRemoval(CommaLoc);
4258*67e74705SXin Li       else if (getLangOpts().CPlusPlus11)
4259*67e74705SXin Li         Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
4260*67e74705SXin Li           << FixItHint::CreateRemoval(CommaLoc);
4261*67e74705SXin Li       break;
4262*67e74705SXin Li     }
4263*67e74705SXin Li   }
4264*67e74705SXin Li 
4265*67e74705SXin Li   // Eat the }.
4266*67e74705SXin Li   T.consumeClose();
4267*67e74705SXin Li 
4268*67e74705SXin Li   // If attributes exist after the identifier list, parse them.
4269*67e74705SXin Li   ParsedAttributes attrs(AttrFactory);
4270*67e74705SXin Li   MaybeParseGNUAttributes(attrs);
4271*67e74705SXin Li 
4272*67e74705SXin Li   Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
4273*67e74705SXin Li                         EnumDecl, EnumConstantDecls,
4274*67e74705SXin Li                         getCurScope(),
4275*67e74705SXin Li                         attrs.getList());
4276*67e74705SXin Li 
4277*67e74705SXin Li   // Now handle enum constant availability diagnostics.
4278*67e74705SXin Li   assert(EnumConstantDecls.size() == EnumAvailabilityDiags.size());
4279*67e74705SXin Li   for (size_t i = 0, e = EnumConstantDecls.size(); i != e; ++i) {
4280*67e74705SXin Li     ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
4281*67e74705SXin Li     EnumAvailabilityDiags[i].redelay();
4282*67e74705SXin Li     PD.complete(EnumConstantDecls[i]);
4283*67e74705SXin Li   }
4284*67e74705SXin Li 
4285*67e74705SXin Li   EnumScope.Exit();
4286*67e74705SXin Li   Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
4287*67e74705SXin Li                                    T.getCloseLocation());
4288*67e74705SXin Li 
4289*67e74705SXin Li   // The next token must be valid after an enum definition. If not, a ';'
4290*67e74705SXin Li   // was probably forgotten.
4291*67e74705SXin Li   bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
4292*67e74705SXin Li   if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
4293*67e74705SXin Li     ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
4294*67e74705SXin Li     // Push this token back into the preprocessor and change our current token
4295*67e74705SXin Li     // to ';' so that the rest of the code recovers as though there were an
4296*67e74705SXin Li     // ';' after the definition.
4297*67e74705SXin Li     PP.EnterToken(Tok);
4298*67e74705SXin Li     Tok.setKind(tok::semi);
4299*67e74705SXin Li   }
4300*67e74705SXin Li }
4301*67e74705SXin Li 
4302*67e74705SXin Li /// isKnownToBeTypeSpecifier - Return true if we know that the specified token
4303*67e74705SXin Li /// is definitely a type-specifier.  Return false if it isn't part of a type
4304*67e74705SXin Li /// specifier or if we're not sure.
isKnownToBeTypeSpecifier(const Token & Tok) const4305*67e74705SXin Li bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
4306*67e74705SXin Li   switch (Tok.getKind()) {
4307*67e74705SXin Li   default: return false;
4308*67e74705SXin Li     // type-specifiers
4309*67e74705SXin Li   case tok::kw_short:
4310*67e74705SXin Li   case tok::kw_long:
4311*67e74705SXin Li   case tok::kw___int64:
4312*67e74705SXin Li   case tok::kw___int128:
4313*67e74705SXin Li   case tok::kw_signed:
4314*67e74705SXin Li   case tok::kw_unsigned:
4315*67e74705SXin Li   case tok::kw__Complex:
4316*67e74705SXin Li   case tok::kw__Imaginary:
4317*67e74705SXin Li   case tok::kw_void:
4318*67e74705SXin Li   case tok::kw_char:
4319*67e74705SXin Li   case tok::kw_wchar_t:
4320*67e74705SXin Li   case tok::kw_char16_t:
4321*67e74705SXin Li   case tok::kw_char32_t:
4322*67e74705SXin Li   case tok::kw_int:
4323*67e74705SXin Li   case tok::kw_half:
4324*67e74705SXin Li   case tok::kw_float:
4325*67e74705SXin Li   case tok::kw_double:
4326*67e74705SXin Li   case tok::kw___float128:
4327*67e74705SXin Li   case tok::kw_bool:
4328*67e74705SXin Li   case tok::kw__Bool:
4329*67e74705SXin Li   case tok::kw__Decimal32:
4330*67e74705SXin Li   case tok::kw__Decimal64:
4331*67e74705SXin Li   case tok::kw__Decimal128:
4332*67e74705SXin Li   case tok::kw___vector:
4333*67e74705SXin Li #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
4334*67e74705SXin Li #include "clang/Basic/OpenCLImageTypes.def"
4335*67e74705SXin Li 
4336*67e74705SXin Li     // struct-or-union-specifier (C99) or class-specifier (C++)
4337*67e74705SXin Li   case tok::kw_class:
4338*67e74705SXin Li   case tok::kw_struct:
4339*67e74705SXin Li   case tok::kw___interface:
4340*67e74705SXin Li   case tok::kw_union:
4341*67e74705SXin Li     // enum-specifier
4342*67e74705SXin Li   case tok::kw_enum:
4343*67e74705SXin Li 
4344*67e74705SXin Li     // typedef-name
4345*67e74705SXin Li   case tok::annot_typename:
4346*67e74705SXin Li     return true;
4347*67e74705SXin Li   }
4348*67e74705SXin Li }
4349*67e74705SXin Li 
4350*67e74705SXin Li /// isTypeSpecifierQualifier - Return true if the current token could be the
4351*67e74705SXin Li /// start of a specifier-qualifier-list.
isTypeSpecifierQualifier()4352*67e74705SXin Li bool Parser::isTypeSpecifierQualifier() {
4353*67e74705SXin Li   switch (Tok.getKind()) {
4354*67e74705SXin Li   default: return false;
4355*67e74705SXin Li 
4356*67e74705SXin Li   case tok::identifier:   // foo::bar
4357*67e74705SXin Li     if (TryAltiVecVectorToken())
4358*67e74705SXin Li       return true;
4359*67e74705SXin Li     // Fall through.
4360*67e74705SXin Li   case tok::kw_typename:  // typename T::type
4361*67e74705SXin Li     // Annotate typenames and C++ scope specifiers.  If we get one, just
4362*67e74705SXin Li     // recurse to handle whatever we get.
4363*67e74705SXin Li     if (TryAnnotateTypeOrScopeToken())
4364*67e74705SXin Li       return true;
4365*67e74705SXin Li     if (Tok.is(tok::identifier))
4366*67e74705SXin Li       return false;
4367*67e74705SXin Li     return isTypeSpecifierQualifier();
4368*67e74705SXin Li 
4369*67e74705SXin Li   case tok::coloncolon:   // ::foo::bar
4370*67e74705SXin Li     if (NextToken().is(tok::kw_new) ||    // ::new
4371*67e74705SXin Li         NextToken().is(tok::kw_delete))   // ::delete
4372*67e74705SXin Li       return false;
4373*67e74705SXin Li 
4374*67e74705SXin Li     if (TryAnnotateTypeOrScopeToken())
4375*67e74705SXin Li       return true;
4376*67e74705SXin Li     return isTypeSpecifierQualifier();
4377*67e74705SXin Li 
4378*67e74705SXin Li     // GNU attributes support.
4379*67e74705SXin Li   case tok::kw___attribute:
4380*67e74705SXin Li     // GNU typeof support.
4381*67e74705SXin Li   case tok::kw_typeof:
4382*67e74705SXin Li 
4383*67e74705SXin Li     // type-specifiers
4384*67e74705SXin Li   case tok::kw_short:
4385*67e74705SXin Li   case tok::kw_long:
4386*67e74705SXin Li   case tok::kw___int64:
4387*67e74705SXin Li   case tok::kw___int128:
4388*67e74705SXin Li   case tok::kw_signed:
4389*67e74705SXin Li   case tok::kw_unsigned:
4390*67e74705SXin Li   case tok::kw__Complex:
4391*67e74705SXin Li   case tok::kw__Imaginary:
4392*67e74705SXin Li   case tok::kw_void:
4393*67e74705SXin Li   case tok::kw_char:
4394*67e74705SXin Li   case tok::kw_wchar_t:
4395*67e74705SXin Li   case tok::kw_char16_t:
4396*67e74705SXin Li   case tok::kw_char32_t:
4397*67e74705SXin Li   case tok::kw_int:
4398*67e74705SXin Li   case tok::kw_half:
4399*67e74705SXin Li   case tok::kw_float:
4400*67e74705SXin Li   case tok::kw_double:
4401*67e74705SXin Li   case tok::kw___float128:
4402*67e74705SXin Li   case tok::kw_bool:
4403*67e74705SXin Li   case tok::kw__Bool:
4404*67e74705SXin Li   case tok::kw__Decimal32:
4405*67e74705SXin Li   case tok::kw__Decimal64:
4406*67e74705SXin Li   case tok::kw__Decimal128:
4407*67e74705SXin Li   case tok::kw___vector:
4408*67e74705SXin Li #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
4409*67e74705SXin Li #include "clang/Basic/OpenCLImageTypes.def"
4410*67e74705SXin Li 
4411*67e74705SXin Li     // struct-or-union-specifier (C99) or class-specifier (C++)
4412*67e74705SXin Li   case tok::kw_class:
4413*67e74705SXin Li   case tok::kw_struct:
4414*67e74705SXin Li   case tok::kw___interface:
4415*67e74705SXin Li   case tok::kw_union:
4416*67e74705SXin Li     // enum-specifier
4417*67e74705SXin Li   case tok::kw_enum:
4418*67e74705SXin Li 
4419*67e74705SXin Li     // type-qualifier
4420*67e74705SXin Li   case tok::kw_const:
4421*67e74705SXin Li   case tok::kw_volatile:
4422*67e74705SXin Li   case tok::kw_restrict:
4423*67e74705SXin Li 
4424*67e74705SXin Li     // Debugger support.
4425*67e74705SXin Li   case tok::kw___unknown_anytype:
4426*67e74705SXin Li 
4427*67e74705SXin Li     // typedef-name
4428*67e74705SXin Li   case tok::annot_typename:
4429*67e74705SXin Li     return true;
4430*67e74705SXin Li 
4431*67e74705SXin Li     // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4432*67e74705SXin Li   case tok::less:
4433*67e74705SXin Li     return getLangOpts().ObjC1;
4434*67e74705SXin Li 
4435*67e74705SXin Li   case tok::kw___cdecl:
4436*67e74705SXin Li   case tok::kw___stdcall:
4437*67e74705SXin Li   case tok::kw___fastcall:
4438*67e74705SXin Li   case tok::kw___thiscall:
4439*67e74705SXin Li   case tok::kw___vectorcall:
4440*67e74705SXin Li   case tok::kw___w64:
4441*67e74705SXin Li   case tok::kw___ptr64:
4442*67e74705SXin Li   case tok::kw___ptr32:
4443*67e74705SXin Li   case tok::kw___pascal:
4444*67e74705SXin Li   case tok::kw___unaligned:
4445*67e74705SXin Li 
4446*67e74705SXin Li   case tok::kw__Nonnull:
4447*67e74705SXin Li   case tok::kw__Nullable:
4448*67e74705SXin Li   case tok::kw__Null_unspecified:
4449*67e74705SXin Li 
4450*67e74705SXin Li   case tok::kw___kindof:
4451*67e74705SXin Li 
4452*67e74705SXin Li   case tok::kw___private:
4453*67e74705SXin Li   case tok::kw___local:
4454*67e74705SXin Li   case tok::kw___global:
4455*67e74705SXin Li   case tok::kw___constant:
4456*67e74705SXin Li   case tok::kw___generic:
4457*67e74705SXin Li   case tok::kw___read_only:
4458*67e74705SXin Li   case tok::kw___read_write:
4459*67e74705SXin Li   case tok::kw___write_only:
4460*67e74705SXin Li 
4461*67e74705SXin Li     return true;
4462*67e74705SXin Li 
4463*67e74705SXin Li   // C11 _Atomic
4464*67e74705SXin Li   case tok::kw__Atomic:
4465*67e74705SXin Li     return true;
4466*67e74705SXin Li   }
4467*67e74705SXin Li }
4468*67e74705SXin Li 
4469*67e74705SXin Li /// isDeclarationSpecifier() - Return true if the current token is part of a
4470*67e74705SXin Li /// declaration specifier.
4471*67e74705SXin Li ///
4472*67e74705SXin Li /// \param DisambiguatingWithExpression True to indicate that the purpose of
4473*67e74705SXin Li /// this check is to disambiguate between an expression and a declaration.
isDeclarationSpecifier(bool DisambiguatingWithExpression)4474*67e74705SXin Li bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
4475*67e74705SXin Li   switch (Tok.getKind()) {
4476*67e74705SXin Li   default: return false;
4477*67e74705SXin Li 
4478*67e74705SXin Li   case tok::kw_pipe:
4479*67e74705SXin Li     return getLangOpts().OpenCL && (getLangOpts().OpenCLVersion >= 200);
4480*67e74705SXin Li 
4481*67e74705SXin Li   case tok::identifier:   // foo::bar
4482*67e74705SXin Li     // Unfortunate hack to support "Class.factoryMethod" notation.
4483*67e74705SXin Li     if (getLangOpts().ObjC1 && NextToken().is(tok::period))
4484*67e74705SXin Li       return false;
4485*67e74705SXin Li     if (TryAltiVecVectorToken())
4486*67e74705SXin Li       return true;
4487*67e74705SXin Li     // Fall through.
4488*67e74705SXin Li   case tok::kw_decltype: // decltype(T())::type
4489*67e74705SXin Li   case tok::kw_typename: // typename T::type
4490*67e74705SXin Li     // Annotate typenames and C++ scope specifiers.  If we get one, just
4491*67e74705SXin Li     // recurse to handle whatever we get.
4492*67e74705SXin Li     if (TryAnnotateTypeOrScopeToken())
4493*67e74705SXin Li       return true;
4494*67e74705SXin Li     if (Tok.is(tok::identifier))
4495*67e74705SXin Li       return false;
4496*67e74705SXin Li 
4497*67e74705SXin Li     // If we're in Objective-C and we have an Objective-C class type followed
4498*67e74705SXin Li     // by an identifier and then either ':' or ']', in a place where an
4499*67e74705SXin Li     // expression is permitted, then this is probably a class message send
4500*67e74705SXin Li     // missing the initial '['. In this case, we won't consider this to be
4501*67e74705SXin Li     // the start of a declaration.
4502*67e74705SXin Li     if (DisambiguatingWithExpression &&
4503*67e74705SXin Li         isStartOfObjCClassMessageMissingOpenBracket())
4504*67e74705SXin Li       return false;
4505*67e74705SXin Li 
4506*67e74705SXin Li     return isDeclarationSpecifier();
4507*67e74705SXin Li 
4508*67e74705SXin Li   case tok::coloncolon:   // ::foo::bar
4509*67e74705SXin Li     if (NextToken().is(tok::kw_new) ||    // ::new
4510*67e74705SXin Li         NextToken().is(tok::kw_delete))   // ::delete
4511*67e74705SXin Li       return false;
4512*67e74705SXin Li 
4513*67e74705SXin Li     // Annotate typenames and C++ scope specifiers.  If we get one, just
4514*67e74705SXin Li     // recurse to handle whatever we get.
4515*67e74705SXin Li     if (TryAnnotateTypeOrScopeToken())
4516*67e74705SXin Li       return true;
4517*67e74705SXin Li     return isDeclarationSpecifier();
4518*67e74705SXin Li 
4519*67e74705SXin Li     // storage-class-specifier
4520*67e74705SXin Li   case tok::kw_typedef:
4521*67e74705SXin Li   case tok::kw_extern:
4522*67e74705SXin Li   case tok::kw___private_extern__:
4523*67e74705SXin Li   case tok::kw_static:
4524*67e74705SXin Li   case tok::kw_auto:
4525*67e74705SXin Li   case tok::kw___auto_type:
4526*67e74705SXin Li   case tok::kw_register:
4527*67e74705SXin Li   case tok::kw___thread:
4528*67e74705SXin Li   case tok::kw_thread_local:
4529*67e74705SXin Li   case tok::kw__Thread_local:
4530*67e74705SXin Li 
4531*67e74705SXin Li     // Modules
4532*67e74705SXin Li   case tok::kw___module_private__:
4533*67e74705SXin Li 
4534*67e74705SXin Li     // Debugger support
4535*67e74705SXin Li   case tok::kw___unknown_anytype:
4536*67e74705SXin Li 
4537*67e74705SXin Li     // type-specifiers
4538*67e74705SXin Li   case tok::kw_short:
4539*67e74705SXin Li   case tok::kw_long:
4540*67e74705SXin Li   case tok::kw___int64:
4541*67e74705SXin Li   case tok::kw___int128:
4542*67e74705SXin Li   case tok::kw_signed:
4543*67e74705SXin Li   case tok::kw_unsigned:
4544*67e74705SXin Li   case tok::kw__Complex:
4545*67e74705SXin Li   case tok::kw__Imaginary:
4546*67e74705SXin Li   case tok::kw_void:
4547*67e74705SXin Li   case tok::kw_char:
4548*67e74705SXin Li   case tok::kw_wchar_t:
4549*67e74705SXin Li   case tok::kw_char16_t:
4550*67e74705SXin Li   case tok::kw_char32_t:
4551*67e74705SXin Li 
4552*67e74705SXin Li   case tok::kw_int:
4553*67e74705SXin Li   case tok::kw_half:
4554*67e74705SXin Li   case tok::kw_float:
4555*67e74705SXin Li   case tok::kw_double:
4556*67e74705SXin Li   case tok::kw___float128:
4557*67e74705SXin Li   case tok::kw_bool:
4558*67e74705SXin Li   case tok::kw__Bool:
4559*67e74705SXin Li   case tok::kw__Decimal32:
4560*67e74705SXin Li   case tok::kw__Decimal64:
4561*67e74705SXin Li   case tok::kw__Decimal128:
4562*67e74705SXin Li   case tok::kw___vector:
4563*67e74705SXin Li 
4564*67e74705SXin Li     // struct-or-union-specifier (C99) or class-specifier (C++)
4565*67e74705SXin Li   case tok::kw_class:
4566*67e74705SXin Li   case tok::kw_struct:
4567*67e74705SXin Li   case tok::kw_union:
4568*67e74705SXin Li   case tok::kw___interface:
4569*67e74705SXin Li     // enum-specifier
4570*67e74705SXin Li   case tok::kw_enum:
4571*67e74705SXin Li 
4572*67e74705SXin Li     // type-qualifier
4573*67e74705SXin Li   case tok::kw_const:
4574*67e74705SXin Li   case tok::kw_volatile:
4575*67e74705SXin Li   case tok::kw_restrict:
4576*67e74705SXin Li 
4577*67e74705SXin Li     // function-specifier
4578*67e74705SXin Li   case tok::kw_inline:
4579*67e74705SXin Li   case tok::kw_virtual:
4580*67e74705SXin Li   case tok::kw_explicit:
4581*67e74705SXin Li   case tok::kw__Noreturn:
4582*67e74705SXin Li 
4583*67e74705SXin Li     // alignment-specifier
4584*67e74705SXin Li   case tok::kw__Alignas:
4585*67e74705SXin Li 
4586*67e74705SXin Li     // friend keyword.
4587*67e74705SXin Li   case tok::kw_friend:
4588*67e74705SXin Li 
4589*67e74705SXin Li     // static_assert-declaration
4590*67e74705SXin Li   case tok::kw__Static_assert:
4591*67e74705SXin Li 
4592*67e74705SXin Li     // GNU typeof support.
4593*67e74705SXin Li   case tok::kw_typeof:
4594*67e74705SXin Li 
4595*67e74705SXin Li     // GNU attributes.
4596*67e74705SXin Li   case tok::kw___attribute:
4597*67e74705SXin Li 
4598*67e74705SXin Li     // C++11 decltype and constexpr.
4599*67e74705SXin Li   case tok::annot_decltype:
4600*67e74705SXin Li   case tok::kw_constexpr:
4601*67e74705SXin Li 
4602*67e74705SXin Li     // C++ Concepts TS - concept
4603*67e74705SXin Li   case tok::kw_concept:
4604*67e74705SXin Li 
4605*67e74705SXin Li     // C11 _Atomic
4606*67e74705SXin Li   case tok::kw__Atomic:
4607*67e74705SXin Li     return true;
4608*67e74705SXin Li 
4609*67e74705SXin Li     // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4610*67e74705SXin Li   case tok::less:
4611*67e74705SXin Li     return getLangOpts().ObjC1;
4612*67e74705SXin Li 
4613*67e74705SXin Li     // typedef-name
4614*67e74705SXin Li   case tok::annot_typename:
4615*67e74705SXin Li     return !DisambiguatingWithExpression ||
4616*67e74705SXin Li            !isStartOfObjCClassMessageMissingOpenBracket();
4617*67e74705SXin Li 
4618*67e74705SXin Li   case tok::kw___declspec:
4619*67e74705SXin Li   case tok::kw___cdecl:
4620*67e74705SXin Li   case tok::kw___stdcall:
4621*67e74705SXin Li   case tok::kw___fastcall:
4622*67e74705SXin Li   case tok::kw___thiscall:
4623*67e74705SXin Li   case tok::kw___vectorcall:
4624*67e74705SXin Li   case tok::kw___w64:
4625*67e74705SXin Li   case tok::kw___sptr:
4626*67e74705SXin Li   case tok::kw___uptr:
4627*67e74705SXin Li   case tok::kw___ptr64:
4628*67e74705SXin Li   case tok::kw___ptr32:
4629*67e74705SXin Li   case tok::kw___forceinline:
4630*67e74705SXin Li   case tok::kw___pascal:
4631*67e74705SXin Li   case tok::kw___unaligned:
4632*67e74705SXin Li 
4633*67e74705SXin Li   case tok::kw__Nonnull:
4634*67e74705SXin Li   case tok::kw__Nullable:
4635*67e74705SXin Li   case tok::kw__Null_unspecified:
4636*67e74705SXin Li 
4637*67e74705SXin Li   case tok::kw___kindof:
4638*67e74705SXin Li 
4639*67e74705SXin Li   case tok::kw___private:
4640*67e74705SXin Li   case tok::kw___local:
4641*67e74705SXin Li   case tok::kw___global:
4642*67e74705SXin Li   case tok::kw___constant:
4643*67e74705SXin Li   case tok::kw___generic:
4644*67e74705SXin Li   case tok::kw___read_only:
4645*67e74705SXin Li   case tok::kw___read_write:
4646*67e74705SXin Li   case tok::kw___write_only:
4647*67e74705SXin Li #define GENERIC_IMAGE_TYPE(ImgType, Id) case tok::kw_##ImgType##_t:
4648*67e74705SXin Li #include "clang/Basic/OpenCLImageTypes.def"
4649*67e74705SXin Li 
4650*67e74705SXin Li     return true;
4651*67e74705SXin Li   }
4652*67e74705SXin Li }
4653*67e74705SXin Li 
isConstructorDeclarator(bool IsUnqualified)4654*67e74705SXin Li bool Parser::isConstructorDeclarator(bool IsUnqualified) {
4655*67e74705SXin Li   TentativeParsingAction TPA(*this);
4656*67e74705SXin Li 
4657*67e74705SXin Li   // Parse the C++ scope specifier.
4658*67e74705SXin Li   CXXScopeSpec SS;
4659*67e74705SXin Li   if (ParseOptionalCXXScopeSpecifier(SS, nullptr,
4660*67e74705SXin Li                                      /*EnteringContext=*/true)) {
4661*67e74705SXin Li     TPA.Revert();
4662*67e74705SXin Li     return false;
4663*67e74705SXin Li   }
4664*67e74705SXin Li 
4665*67e74705SXin Li   // Parse the constructor name.
4666*67e74705SXin Li   if (Tok.isOneOf(tok::identifier, tok::annot_template_id)) {
4667*67e74705SXin Li     // We already know that we have a constructor name; just consume
4668*67e74705SXin Li     // the token.
4669*67e74705SXin Li     ConsumeToken();
4670*67e74705SXin Li   } else {
4671*67e74705SXin Li     TPA.Revert();
4672*67e74705SXin Li     return false;
4673*67e74705SXin Li   }
4674*67e74705SXin Li 
4675*67e74705SXin Li   // Current class name must be followed by a left parenthesis.
4676*67e74705SXin Li   if (Tok.isNot(tok::l_paren)) {
4677*67e74705SXin Li     TPA.Revert();
4678*67e74705SXin Li     return false;
4679*67e74705SXin Li   }
4680*67e74705SXin Li   ConsumeParen();
4681*67e74705SXin Li 
4682*67e74705SXin Li   // A right parenthesis, or ellipsis followed by a right parenthesis signals
4683*67e74705SXin Li   // that we have a constructor.
4684*67e74705SXin Li   if (Tok.is(tok::r_paren) ||
4685*67e74705SXin Li       (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
4686*67e74705SXin Li     TPA.Revert();
4687*67e74705SXin Li     return true;
4688*67e74705SXin Li   }
4689*67e74705SXin Li 
4690*67e74705SXin Li   // A C++11 attribute here signals that we have a constructor, and is an
4691*67e74705SXin Li   // attribute on the first constructor parameter.
4692*67e74705SXin Li   if (getLangOpts().CPlusPlus11 &&
4693*67e74705SXin Li       isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4694*67e74705SXin Li                                 /*OuterMightBeMessageSend*/ true)) {
4695*67e74705SXin Li     TPA.Revert();
4696*67e74705SXin Li     return true;
4697*67e74705SXin Li   }
4698*67e74705SXin Li 
4699*67e74705SXin Li   // If we need to, enter the specified scope.
4700*67e74705SXin Li   DeclaratorScopeObj DeclScopeObj(*this, SS);
4701*67e74705SXin Li   if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
4702*67e74705SXin Li     DeclScopeObj.EnterDeclaratorScope();
4703*67e74705SXin Li 
4704*67e74705SXin Li   // Optionally skip Microsoft attributes.
4705*67e74705SXin Li   ParsedAttributes Attrs(AttrFactory);
4706*67e74705SXin Li   MaybeParseMicrosoftAttributes(Attrs);
4707*67e74705SXin Li 
4708*67e74705SXin Li   // Check whether the next token(s) are part of a declaration
4709*67e74705SXin Li   // specifier, in which case we have the start of a parameter and,
4710*67e74705SXin Li   // therefore, we know that this is a constructor.
4711*67e74705SXin Li   bool IsConstructor = false;
4712*67e74705SXin Li   if (isDeclarationSpecifier())
4713*67e74705SXin Li     IsConstructor = true;
4714*67e74705SXin Li   else if (Tok.is(tok::identifier) ||
4715*67e74705SXin Li            (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4716*67e74705SXin Li     // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4717*67e74705SXin Li     // This might be a parenthesized member name, but is more likely to
4718*67e74705SXin Li     // be a constructor declaration with an invalid argument type. Keep
4719*67e74705SXin Li     // looking.
4720*67e74705SXin Li     if (Tok.is(tok::annot_cxxscope))
4721*67e74705SXin Li       ConsumeToken();
4722*67e74705SXin Li     ConsumeToken();
4723*67e74705SXin Li 
4724*67e74705SXin Li     // If this is not a constructor, we must be parsing a declarator,
4725*67e74705SXin Li     // which must have one of the following syntactic forms (see the
4726*67e74705SXin Li     // grammar extract at the start of ParseDirectDeclarator):
4727*67e74705SXin Li     switch (Tok.getKind()) {
4728*67e74705SXin Li     case tok::l_paren:
4729*67e74705SXin Li       // C(X   (   int));
4730*67e74705SXin Li     case tok::l_square:
4731*67e74705SXin Li       // C(X   [   5]);
4732*67e74705SXin Li       // C(X   [   [attribute]]);
4733*67e74705SXin Li     case tok::coloncolon:
4734*67e74705SXin Li       // C(X   ::   Y);
4735*67e74705SXin Li       // C(X   ::   *p);
4736*67e74705SXin Li       // Assume this isn't a constructor, rather than assuming it's a
4737*67e74705SXin Li       // constructor with an unnamed parameter of an ill-formed type.
4738*67e74705SXin Li       break;
4739*67e74705SXin Li 
4740*67e74705SXin Li     case tok::r_paren:
4741*67e74705SXin Li       // C(X   )
4742*67e74705SXin Li       if (NextToken().is(tok::colon) || NextToken().is(tok::kw_try)) {
4743*67e74705SXin Li         // Assume these were meant to be constructors:
4744*67e74705SXin Li         //   C(X)   :    (the name of a bit-field cannot be parenthesized).
4745*67e74705SXin Li         //   C(X)   try  (this is otherwise ill-formed).
4746*67e74705SXin Li         IsConstructor = true;
4747*67e74705SXin Li       }
4748*67e74705SXin Li       if (NextToken().is(tok::semi) || NextToken().is(tok::l_brace)) {
4749*67e74705SXin Li         // If we have a constructor name within the class definition,
4750*67e74705SXin Li         // assume these were meant to be constructors:
4751*67e74705SXin Li         //   C(X)   {
4752*67e74705SXin Li         //   C(X)   ;
4753*67e74705SXin Li         // ... because otherwise we would be declaring a non-static data
4754*67e74705SXin Li         // member that is ill-formed because it's of the same type as its
4755*67e74705SXin Li         // surrounding class.
4756*67e74705SXin Li         //
4757*67e74705SXin Li         // FIXME: We can actually do this whether or not the name is qualified,
4758*67e74705SXin Li         // because if it is qualified in this context it must be being used as
4759*67e74705SXin Li         // a constructor name. However, we do not implement that rule correctly
4760*67e74705SXin Li         // currently, so we're somewhat conservative here.
4761*67e74705SXin Li         IsConstructor = IsUnqualified;
4762*67e74705SXin Li       }
4763*67e74705SXin Li       break;
4764*67e74705SXin Li 
4765*67e74705SXin Li     default:
4766*67e74705SXin Li       IsConstructor = true;
4767*67e74705SXin Li       break;
4768*67e74705SXin Li     }
4769*67e74705SXin Li   }
4770*67e74705SXin Li 
4771*67e74705SXin Li   TPA.Revert();
4772*67e74705SXin Li   return IsConstructor;
4773*67e74705SXin Li }
4774*67e74705SXin Li 
4775*67e74705SXin Li /// ParseTypeQualifierListOpt
4776*67e74705SXin Li ///          type-qualifier-list: [C99 6.7.5]
4777*67e74705SXin Li ///            type-qualifier
4778*67e74705SXin Li /// [vendor]   attributes
4779*67e74705SXin Li ///              [ only if AttrReqs & AR_VendorAttributesParsed ]
4780*67e74705SXin Li ///            type-qualifier-list type-qualifier
4781*67e74705SXin Li /// [vendor]   type-qualifier-list attributes
4782*67e74705SXin Li ///              [ only if AttrReqs & AR_VendorAttributesParsed ]
4783*67e74705SXin Li /// [C++0x]    attribute-specifier[opt] is allowed before cv-qualifier-seq
4784*67e74705SXin Li ///              [ only if AttReqs & AR_CXX11AttributesParsed ]
4785*67e74705SXin Li /// Note: vendor can be GNU, MS, etc and can be explicitly controlled via
4786*67e74705SXin Li /// AttrRequirements bitmask values.
ParseTypeQualifierListOpt(DeclSpec & DS,unsigned AttrReqs,bool AtomicAllowed,bool IdentifierRequired)4787*67e74705SXin Li void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, unsigned AttrReqs,
4788*67e74705SXin Li                                        bool AtomicAllowed,
4789*67e74705SXin Li                                        bool IdentifierRequired) {
4790*67e74705SXin Li   if (getLangOpts().CPlusPlus11 && (AttrReqs & AR_CXX11AttributesParsed) &&
4791*67e74705SXin Li       isCXX11AttributeSpecifier()) {
4792*67e74705SXin Li     ParsedAttributesWithRange attrs(AttrFactory);
4793*67e74705SXin Li     ParseCXX11Attributes(attrs);
4794*67e74705SXin Li     DS.takeAttributesFrom(attrs);
4795*67e74705SXin Li   }
4796*67e74705SXin Li 
4797*67e74705SXin Li   SourceLocation EndLoc;
4798*67e74705SXin Li 
4799*67e74705SXin Li   while (1) {
4800*67e74705SXin Li     bool isInvalid = false;
4801*67e74705SXin Li     const char *PrevSpec = nullptr;
4802*67e74705SXin Li     unsigned DiagID = 0;
4803*67e74705SXin Li     SourceLocation Loc = Tok.getLocation();
4804*67e74705SXin Li 
4805*67e74705SXin Li     switch (Tok.getKind()) {
4806*67e74705SXin Li     case tok::code_completion:
4807*67e74705SXin Li       Actions.CodeCompleteTypeQualifiers(DS);
4808*67e74705SXin Li       return cutOffParsing();
4809*67e74705SXin Li 
4810*67e74705SXin Li     case tok::kw_const:
4811*67e74705SXin Li       isInvalid = DS.SetTypeQual(DeclSpec::TQ_const   , Loc, PrevSpec, DiagID,
4812*67e74705SXin Li                                  getLangOpts());
4813*67e74705SXin Li       break;
4814*67e74705SXin Li     case tok::kw_volatile:
4815*67e74705SXin Li       isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
4816*67e74705SXin Li                                  getLangOpts());
4817*67e74705SXin Li       break;
4818*67e74705SXin Li     case tok::kw_restrict:
4819*67e74705SXin Li       isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
4820*67e74705SXin Li                                  getLangOpts());
4821*67e74705SXin Li       break;
4822*67e74705SXin Li     case tok::kw__Atomic:
4823*67e74705SXin Li       if (!AtomicAllowed)
4824*67e74705SXin Li         goto DoneWithTypeQuals;
4825*67e74705SXin Li       isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4826*67e74705SXin Li                                  getLangOpts());
4827*67e74705SXin Li       break;
4828*67e74705SXin Li 
4829*67e74705SXin Li     // OpenCL qualifiers:
4830*67e74705SXin Li     case tok::kw___private:
4831*67e74705SXin Li     case tok::kw___global:
4832*67e74705SXin Li     case tok::kw___local:
4833*67e74705SXin Li     case tok::kw___constant:
4834*67e74705SXin Li     case tok::kw___generic:
4835*67e74705SXin Li     case tok::kw___read_only:
4836*67e74705SXin Li     case tok::kw___write_only:
4837*67e74705SXin Li     case tok::kw___read_write:
4838*67e74705SXin Li       ParseOpenCLQualifiers(DS.getAttributes());
4839*67e74705SXin Li       break;
4840*67e74705SXin Li 
4841*67e74705SXin Li     case tok::kw___unaligned:
4842*67e74705SXin Li       isInvalid = DS.SetTypeQual(DeclSpec::TQ_unaligned, Loc, PrevSpec, DiagID,
4843*67e74705SXin Li                                  getLangOpts());
4844*67e74705SXin Li       break;
4845*67e74705SXin Li     case tok::kw___uptr:
4846*67e74705SXin Li       // GNU libc headers in C mode use '__uptr' as an identifer which conflicts
4847*67e74705SXin Li       // with the MS modifier keyword.
4848*67e74705SXin Li       if ((AttrReqs & AR_DeclspecAttributesParsed) && !getLangOpts().CPlusPlus &&
4849*67e74705SXin Li           IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
4850*67e74705SXin Li         if (TryKeywordIdentFallback(false))
4851*67e74705SXin Li           continue;
4852*67e74705SXin Li       }
4853*67e74705SXin Li     case tok::kw___sptr:
4854*67e74705SXin Li     case tok::kw___w64:
4855*67e74705SXin Li     case tok::kw___ptr64:
4856*67e74705SXin Li     case tok::kw___ptr32:
4857*67e74705SXin Li     case tok::kw___cdecl:
4858*67e74705SXin Li     case tok::kw___stdcall:
4859*67e74705SXin Li     case tok::kw___fastcall:
4860*67e74705SXin Li     case tok::kw___thiscall:
4861*67e74705SXin Li     case tok::kw___vectorcall:
4862*67e74705SXin Li       if (AttrReqs & AR_DeclspecAttributesParsed) {
4863*67e74705SXin Li         ParseMicrosoftTypeAttributes(DS.getAttributes());
4864*67e74705SXin Li         continue;
4865*67e74705SXin Li       }
4866*67e74705SXin Li       goto DoneWithTypeQuals;
4867*67e74705SXin Li     case tok::kw___pascal:
4868*67e74705SXin Li       if (AttrReqs & AR_VendorAttributesParsed) {
4869*67e74705SXin Li         ParseBorlandTypeAttributes(DS.getAttributes());
4870*67e74705SXin Li         continue;
4871*67e74705SXin Li       }
4872*67e74705SXin Li       goto DoneWithTypeQuals;
4873*67e74705SXin Li 
4874*67e74705SXin Li     // Nullability type specifiers.
4875*67e74705SXin Li     case tok::kw__Nonnull:
4876*67e74705SXin Li     case tok::kw__Nullable:
4877*67e74705SXin Li     case tok::kw__Null_unspecified:
4878*67e74705SXin Li       ParseNullabilityTypeSpecifiers(DS.getAttributes());
4879*67e74705SXin Li       continue;
4880*67e74705SXin Li 
4881*67e74705SXin Li     // Objective-C 'kindof' types.
4882*67e74705SXin Li     case tok::kw___kindof:
4883*67e74705SXin Li       DS.getAttributes().addNew(Tok.getIdentifierInfo(), Loc, nullptr, Loc,
4884*67e74705SXin Li                                 nullptr, 0, AttributeList::AS_Keyword);
4885*67e74705SXin Li       (void)ConsumeToken();
4886*67e74705SXin Li       continue;
4887*67e74705SXin Li 
4888*67e74705SXin Li     case tok::kw___attribute:
4889*67e74705SXin Li       if (AttrReqs & AR_GNUAttributesParsedAndRejected)
4890*67e74705SXin Li         // When GNU attributes are expressly forbidden, diagnose their usage.
4891*67e74705SXin Li         Diag(Tok, diag::err_attributes_not_allowed);
4892*67e74705SXin Li 
4893*67e74705SXin Li       // Parse the attributes even if they are rejected to ensure that error
4894*67e74705SXin Li       // recovery is graceful.
4895*67e74705SXin Li       if (AttrReqs & AR_GNUAttributesParsed ||
4896*67e74705SXin Li           AttrReqs & AR_GNUAttributesParsedAndRejected) {
4897*67e74705SXin Li         ParseGNUAttributes(DS.getAttributes());
4898*67e74705SXin Li         continue; // do *not* consume the next token!
4899*67e74705SXin Li       }
4900*67e74705SXin Li       // otherwise, FALL THROUGH!
4901*67e74705SXin Li     default:
4902*67e74705SXin Li       DoneWithTypeQuals:
4903*67e74705SXin Li       // If this is not a type-qualifier token, we're done reading type
4904*67e74705SXin Li       // qualifiers.  First verify that DeclSpec's are consistent.
4905*67e74705SXin Li       DS.Finish(Actions, Actions.getASTContext().getPrintingPolicy());
4906*67e74705SXin Li       if (EndLoc.isValid())
4907*67e74705SXin Li         DS.SetRangeEnd(EndLoc);
4908*67e74705SXin Li       return;
4909*67e74705SXin Li     }
4910*67e74705SXin Li 
4911*67e74705SXin Li     // If the specifier combination wasn't legal, issue a diagnostic.
4912*67e74705SXin Li     if (isInvalid) {
4913*67e74705SXin Li       assert(PrevSpec && "Method did not return previous specifier!");
4914*67e74705SXin Li       Diag(Tok, DiagID) << PrevSpec;
4915*67e74705SXin Li     }
4916*67e74705SXin Li     EndLoc = ConsumeToken();
4917*67e74705SXin Li   }
4918*67e74705SXin Li }
4919*67e74705SXin Li 
4920*67e74705SXin Li /// ParseDeclarator - Parse and verify a newly-initialized declarator.
4921*67e74705SXin Li ///
ParseDeclarator(Declarator & D)4922*67e74705SXin Li void Parser::ParseDeclarator(Declarator &D) {
4923*67e74705SXin Li   /// This implements the 'declarator' production in the C grammar, then checks
4924*67e74705SXin Li   /// for well-formedness and issues diagnostics.
4925*67e74705SXin Li   ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
4926*67e74705SXin Li }
4927*67e74705SXin Li 
isPtrOperatorToken(tok::TokenKind Kind,const LangOptions & Lang,unsigned TheContext)4928*67e74705SXin Li static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang,
4929*67e74705SXin Li                                unsigned TheContext) {
4930*67e74705SXin Li   if (Kind == tok::star || Kind == tok::caret)
4931*67e74705SXin Li     return true;
4932*67e74705SXin Li 
4933*67e74705SXin Li   if ((Kind == tok::kw_pipe) && Lang.OpenCL && (Lang.OpenCLVersion >= 200))
4934*67e74705SXin Li     return true;
4935*67e74705SXin Li 
4936*67e74705SXin Li   if (!Lang.CPlusPlus)
4937*67e74705SXin Li     return false;
4938*67e74705SXin Li 
4939*67e74705SXin Li   if (Kind == tok::amp)
4940*67e74705SXin Li     return true;
4941*67e74705SXin Li 
4942*67e74705SXin Li   // We parse rvalue refs in C++03, because otherwise the errors are scary.
4943*67e74705SXin Li   // But we must not parse them in conversion-type-ids and new-type-ids, since
4944*67e74705SXin Li   // those can be legitimately followed by a && operator.
4945*67e74705SXin Li   // (The same thing can in theory happen after a trailing-return-type, but
4946*67e74705SXin Li   // since those are a C++11 feature, there is no rejects-valid issue there.)
4947*67e74705SXin Li   if (Kind == tok::ampamp)
4948*67e74705SXin Li     return Lang.CPlusPlus11 || (TheContext != Declarator::ConversionIdContext &&
4949*67e74705SXin Li                                 TheContext != Declarator::CXXNewContext);
4950*67e74705SXin Li 
4951*67e74705SXin Li   return false;
4952*67e74705SXin Li }
4953*67e74705SXin Li 
4954*67e74705SXin Li // Indicates whether the given declarator is a pipe declarator.
isPipeDeclerator(const Declarator & D)4955*67e74705SXin Li static bool isPipeDeclerator(const Declarator &D) {
4956*67e74705SXin Li   const unsigned NumTypes = D.getNumTypeObjects();
4957*67e74705SXin Li 
4958*67e74705SXin Li   for (unsigned Idx = 0; Idx != NumTypes; ++Idx)
4959*67e74705SXin Li     if (DeclaratorChunk::Pipe == D.getTypeObject(Idx).Kind)
4960*67e74705SXin Li       return true;
4961*67e74705SXin Li 
4962*67e74705SXin Li   return false;
4963*67e74705SXin Li }
4964*67e74705SXin Li 
4965*67e74705SXin Li /// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4966*67e74705SXin Li /// is parsed by the function passed to it. Pass null, and the direct-declarator
4967*67e74705SXin Li /// isn't parsed at all, making this function effectively parse the C++
4968*67e74705SXin Li /// ptr-operator production.
4969*67e74705SXin Li ///
4970*67e74705SXin Li /// If the grammar of this construct is extended, matching changes must also be
4971*67e74705SXin Li /// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4972*67e74705SXin Li /// isConstructorDeclarator.
4973*67e74705SXin Li ///
4974*67e74705SXin Li ///       declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4975*67e74705SXin Li /// [C]     pointer[opt] direct-declarator
4976*67e74705SXin Li /// [C++]   direct-declarator
4977*67e74705SXin Li /// [C++]   ptr-operator declarator
4978*67e74705SXin Li ///
4979*67e74705SXin Li ///       pointer: [C99 6.7.5]
4980*67e74705SXin Li ///         '*' type-qualifier-list[opt]
4981*67e74705SXin Li ///         '*' type-qualifier-list[opt] pointer
4982*67e74705SXin Li ///
4983*67e74705SXin Li ///       ptr-operator:
4984*67e74705SXin Li ///         '*' cv-qualifier-seq[opt]
4985*67e74705SXin Li ///         '&'
4986*67e74705SXin Li /// [C++0x] '&&'
4987*67e74705SXin Li /// [GNU]   '&' restrict[opt] attributes[opt]
4988*67e74705SXin Li /// [GNU?]  '&&' restrict[opt] attributes[opt]
4989*67e74705SXin Li ///         '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
ParseDeclaratorInternal(Declarator & D,DirectDeclParseFunction DirectDeclParser)4990*67e74705SXin Li void Parser::ParseDeclaratorInternal(Declarator &D,
4991*67e74705SXin Li                                      DirectDeclParseFunction DirectDeclParser) {
4992*67e74705SXin Li   if (Diags.hasAllExtensionsSilenced())
4993*67e74705SXin Li     D.setExtension();
4994*67e74705SXin Li 
4995*67e74705SXin Li   // C++ member pointers start with a '::' or a nested-name.
4996*67e74705SXin Li   // Member pointers get special handling, since there's no place for the
4997*67e74705SXin Li   // scope spec in the generic path below.
4998*67e74705SXin Li   if (getLangOpts().CPlusPlus &&
4999*67e74705SXin Li       (Tok.is(tok::coloncolon) || Tok.is(tok::kw_decltype) ||
5000*67e74705SXin Li        (Tok.is(tok::identifier) &&
5001*67e74705SXin Li         (NextToken().is(tok::coloncolon) || NextToken().is(tok::less))) ||
5002*67e74705SXin Li        Tok.is(tok::annot_cxxscope))) {
5003*67e74705SXin Li     bool EnteringContext = D.getContext() == Declarator::FileContext ||
5004*67e74705SXin Li                            D.getContext() == Declarator::MemberContext;
5005*67e74705SXin Li     CXXScopeSpec SS;
5006*67e74705SXin Li     ParseOptionalCXXScopeSpecifier(SS, nullptr, EnteringContext);
5007*67e74705SXin Li 
5008*67e74705SXin Li     if (SS.isNotEmpty()) {
5009*67e74705SXin Li       if (Tok.isNot(tok::star)) {
5010*67e74705SXin Li         // The scope spec really belongs to the direct-declarator.
5011*67e74705SXin Li         if (D.mayHaveIdentifier())
5012*67e74705SXin Li           D.getCXXScopeSpec() = SS;
5013*67e74705SXin Li         else
5014*67e74705SXin Li           AnnotateScopeToken(SS, true);
5015*67e74705SXin Li 
5016*67e74705SXin Li         if (DirectDeclParser)
5017*67e74705SXin Li           (this->*DirectDeclParser)(D);
5018*67e74705SXin Li         return;
5019*67e74705SXin Li       }
5020*67e74705SXin Li 
5021*67e74705SXin Li       SourceLocation Loc = ConsumeToken();
5022*67e74705SXin Li       D.SetRangeEnd(Loc);
5023*67e74705SXin Li       DeclSpec DS(AttrFactory);
5024*67e74705SXin Li       ParseTypeQualifierListOpt(DS);
5025*67e74705SXin Li       D.ExtendWithDeclSpec(DS);
5026*67e74705SXin Li 
5027*67e74705SXin Li       // Recurse to parse whatever is left.
5028*67e74705SXin Li       ParseDeclaratorInternal(D, DirectDeclParser);
5029*67e74705SXin Li 
5030*67e74705SXin Li       // Sema will have to catch (syntactically invalid) pointers into global
5031*67e74705SXin Li       // scope. It has to catch pointers into namespace scope anyway.
5032*67e74705SXin Li       D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
5033*67e74705SXin Li                                                       DS.getLocEnd()),
5034*67e74705SXin Li                     DS.getAttributes(),
5035*67e74705SXin Li                     /* Don't replace range end. */SourceLocation());
5036*67e74705SXin Li       return;
5037*67e74705SXin Li     }
5038*67e74705SXin Li   }
5039*67e74705SXin Li 
5040*67e74705SXin Li   tok::TokenKind Kind = Tok.getKind();
5041*67e74705SXin Li 
5042*67e74705SXin Li   if (D.getDeclSpec().isTypeSpecPipe() && !isPipeDeclerator(D)) {
5043*67e74705SXin Li     DeclSpec DS(AttrFactory);
5044*67e74705SXin Li     ParseTypeQualifierListOpt(DS);
5045*67e74705SXin Li 
5046*67e74705SXin Li     D.AddTypeInfo(
5047*67e74705SXin Li         DeclaratorChunk::getPipe(DS.getTypeQualifiers(), DS.getPipeLoc()),
5048*67e74705SXin Li         DS.getAttributes(), SourceLocation());
5049*67e74705SXin Li   }
5050*67e74705SXin Li 
5051*67e74705SXin Li   // Not a pointer, C++ reference, or block.
5052*67e74705SXin Li   if (!isPtrOperatorToken(Kind, getLangOpts(), D.getContext())) {
5053*67e74705SXin Li     if (DirectDeclParser)
5054*67e74705SXin Li       (this->*DirectDeclParser)(D);
5055*67e74705SXin Li     return;
5056*67e74705SXin Li   }
5057*67e74705SXin Li 
5058*67e74705SXin Li   // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
5059*67e74705SXin Li   // '&&' -> rvalue reference
5060*67e74705SXin Li   SourceLocation Loc = ConsumeToken();  // Eat the *, ^, & or &&.
5061*67e74705SXin Li   D.SetRangeEnd(Loc);
5062*67e74705SXin Li 
5063*67e74705SXin Li   if (Kind == tok::star || Kind == tok::caret) {
5064*67e74705SXin Li     // Is a pointer.
5065*67e74705SXin Li     DeclSpec DS(AttrFactory);
5066*67e74705SXin Li 
5067*67e74705SXin Li     // GNU attributes are not allowed here in a new-type-id, but Declspec and
5068*67e74705SXin Li     // C++11 attributes are allowed.
5069*67e74705SXin Li     unsigned Reqs = AR_CXX11AttributesParsed | AR_DeclspecAttributesParsed |
5070*67e74705SXin Li                             ((D.getContext() != Declarator::CXXNewContext)
5071*67e74705SXin Li                                  ? AR_GNUAttributesParsed
5072*67e74705SXin Li                                  : AR_GNUAttributesParsedAndRejected);
5073*67e74705SXin Li     ParseTypeQualifierListOpt(DS, Reqs, true, !D.mayOmitIdentifier());
5074*67e74705SXin Li     D.ExtendWithDeclSpec(DS);
5075*67e74705SXin Li 
5076*67e74705SXin Li     // Recursively parse the declarator.
5077*67e74705SXin Li     ParseDeclaratorInternal(D, DirectDeclParser);
5078*67e74705SXin Li     if (Kind == tok::star)
5079*67e74705SXin Li       // Remember that we parsed a pointer type, and remember the type-quals.
5080*67e74705SXin Li       D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
5081*67e74705SXin Li                                                 DS.getConstSpecLoc(),
5082*67e74705SXin Li                                                 DS.getVolatileSpecLoc(),
5083*67e74705SXin Li                                                 DS.getRestrictSpecLoc(),
5084*67e74705SXin Li                                                 DS.getAtomicSpecLoc(),
5085*67e74705SXin Li                                                 DS.getUnalignedSpecLoc()),
5086*67e74705SXin Li                     DS.getAttributes(),
5087*67e74705SXin Li                     SourceLocation());
5088*67e74705SXin Li     else
5089*67e74705SXin Li       // Remember that we parsed a Block type, and remember the type-quals.
5090*67e74705SXin Li       D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
5091*67e74705SXin Li                                                      Loc),
5092*67e74705SXin Li                     DS.getAttributes(),
5093*67e74705SXin Li                     SourceLocation());
5094*67e74705SXin Li   } else {
5095*67e74705SXin Li     // Is a reference
5096*67e74705SXin Li     DeclSpec DS(AttrFactory);
5097*67e74705SXin Li 
5098*67e74705SXin Li     // Complain about rvalue references in C++03, but then go on and build
5099*67e74705SXin Li     // the declarator.
5100*67e74705SXin Li     if (Kind == tok::ampamp)
5101*67e74705SXin Li       Diag(Loc, getLangOpts().CPlusPlus11 ?
5102*67e74705SXin Li            diag::warn_cxx98_compat_rvalue_reference :
5103*67e74705SXin Li            diag::ext_rvalue_reference);
5104*67e74705SXin Li 
5105*67e74705SXin Li     // GNU-style and C++11 attributes are allowed here, as is restrict.
5106*67e74705SXin Li     ParseTypeQualifierListOpt(DS);
5107*67e74705SXin Li     D.ExtendWithDeclSpec(DS);
5108*67e74705SXin Li 
5109*67e74705SXin Li     // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
5110*67e74705SXin Li     // cv-qualifiers are introduced through the use of a typedef or of a
5111*67e74705SXin Li     // template type argument, in which case the cv-qualifiers are ignored.
5112*67e74705SXin Li     if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
5113*67e74705SXin Li       if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
5114*67e74705SXin Li         Diag(DS.getConstSpecLoc(),
5115*67e74705SXin Li              diag::err_invalid_reference_qualifier_application) << "const";
5116*67e74705SXin Li       if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
5117*67e74705SXin Li         Diag(DS.getVolatileSpecLoc(),
5118*67e74705SXin Li              diag::err_invalid_reference_qualifier_application) << "volatile";
5119*67e74705SXin Li       // 'restrict' is permitted as an extension.
5120*67e74705SXin Li       if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
5121*67e74705SXin Li         Diag(DS.getAtomicSpecLoc(),
5122*67e74705SXin Li              diag::err_invalid_reference_qualifier_application) << "_Atomic";
5123*67e74705SXin Li     }
5124*67e74705SXin Li 
5125*67e74705SXin Li     // Recursively parse the declarator.
5126*67e74705SXin Li     ParseDeclaratorInternal(D, DirectDeclParser);
5127*67e74705SXin Li 
5128*67e74705SXin Li     if (D.getNumTypeObjects() > 0) {
5129*67e74705SXin Li       // C++ [dcl.ref]p4: There shall be no references to references.
5130*67e74705SXin Li       DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
5131*67e74705SXin Li       if (InnerChunk.Kind == DeclaratorChunk::Reference) {
5132*67e74705SXin Li         if (const IdentifierInfo *II = D.getIdentifier())
5133*67e74705SXin Li           Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
5134*67e74705SXin Li            << II;
5135*67e74705SXin Li         else
5136*67e74705SXin Li           Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
5137*67e74705SXin Li             << "type name";
5138*67e74705SXin Li 
5139*67e74705SXin Li         // Once we've complained about the reference-to-reference, we
5140*67e74705SXin Li         // can go ahead and build the (technically ill-formed)
5141*67e74705SXin Li         // declarator: reference collapsing will take care of it.
5142*67e74705SXin Li       }
5143*67e74705SXin Li     }
5144*67e74705SXin Li 
5145*67e74705SXin Li     // Remember that we parsed a reference type.
5146*67e74705SXin Li     D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
5147*67e74705SXin Li                                                 Kind == tok::amp),
5148*67e74705SXin Li                   DS.getAttributes(),
5149*67e74705SXin Li                   SourceLocation());
5150*67e74705SXin Li   }
5151*67e74705SXin Li }
5152*67e74705SXin Li 
5153*67e74705SXin Li // When correcting from misplaced brackets before the identifier, the location
5154*67e74705SXin Li // is saved inside the declarator so that other diagnostic messages can use
5155*67e74705SXin Li // them.  This extracts and returns that location, or returns the provided
5156*67e74705SXin Li // location if a stored location does not exist.
getMissingDeclaratorIdLoc(Declarator & D,SourceLocation Loc)5157*67e74705SXin Li static SourceLocation getMissingDeclaratorIdLoc(Declarator &D,
5158*67e74705SXin Li                                                 SourceLocation Loc) {
5159*67e74705SXin Li   if (D.getName().StartLocation.isInvalid() &&
5160*67e74705SXin Li       D.getName().EndLocation.isValid())
5161*67e74705SXin Li     return D.getName().EndLocation;
5162*67e74705SXin Li 
5163*67e74705SXin Li   return Loc;
5164*67e74705SXin Li }
5165*67e74705SXin Li 
5166*67e74705SXin Li /// ParseDirectDeclarator
5167*67e74705SXin Li ///       direct-declarator: [C99 6.7.5]
5168*67e74705SXin Li /// [C99]   identifier
5169*67e74705SXin Li ///         '(' declarator ')'
5170*67e74705SXin Li /// [GNU]   '(' attributes declarator ')'
5171*67e74705SXin Li /// [C90]   direct-declarator '[' constant-expression[opt] ']'
5172*67e74705SXin Li /// [C99]   direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5173*67e74705SXin Li /// [C99]   direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5174*67e74705SXin Li /// [C99]   direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5175*67e74705SXin Li /// [C99]   direct-declarator '[' type-qual-list[opt] '*' ']'
5176*67e74705SXin Li /// [C++11] direct-declarator '[' constant-expression[opt] ']'
5177*67e74705SXin Li ///                    attribute-specifier-seq[opt]
5178*67e74705SXin Li ///         direct-declarator '(' parameter-type-list ')'
5179*67e74705SXin Li ///         direct-declarator '(' identifier-list[opt] ')'
5180*67e74705SXin Li /// [GNU]   direct-declarator '(' parameter-forward-declarations
5181*67e74705SXin Li ///                    parameter-type-list[opt] ')'
5182*67e74705SXin Li /// [C++]   direct-declarator '(' parameter-declaration-clause ')'
5183*67e74705SXin Li ///                    cv-qualifier-seq[opt] exception-specification[opt]
5184*67e74705SXin Li /// [C++11] direct-declarator '(' parameter-declaration-clause ')'
5185*67e74705SXin Li ///                    attribute-specifier-seq[opt] cv-qualifier-seq[opt]
5186*67e74705SXin Li ///                    ref-qualifier[opt] exception-specification[opt]
5187*67e74705SXin Li /// [C++]   declarator-id
5188*67e74705SXin Li /// [C++11] declarator-id attribute-specifier-seq[opt]
5189*67e74705SXin Li ///
5190*67e74705SXin Li ///       declarator-id: [C++ 8]
5191*67e74705SXin Li ///         '...'[opt] id-expression
5192*67e74705SXin Li ///         '::'[opt] nested-name-specifier[opt] type-name
5193*67e74705SXin Li ///
5194*67e74705SXin Li ///       id-expression: [C++ 5.1]
5195*67e74705SXin Li ///         unqualified-id
5196*67e74705SXin Li ///         qualified-id
5197*67e74705SXin Li ///
5198*67e74705SXin Li ///       unqualified-id: [C++ 5.1]
5199*67e74705SXin Li ///         identifier
5200*67e74705SXin Li ///         operator-function-id
5201*67e74705SXin Li ///         conversion-function-id
5202*67e74705SXin Li ///          '~' class-name
5203*67e74705SXin Li ///         template-id
5204*67e74705SXin Li ///
5205*67e74705SXin Li /// Note, any additional constructs added here may need corresponding changes
5206*67e74705SXin Li /// in isConstructorDeclarator.
ParseDirectDeclarator(Declarator & D)5207*67e74705SXin Li void Parser::ParseDirectDeclarator(Declarator &D) {
5208*67e74705SXin Li   DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
5209*67e74705SXin Li 
5210*67e74705SXin Li   if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
5211*67e74705SXin Li     // Don't parse FOO:BAR as if it were a typo for FOO::BAR inside a class, in
5212*67e74705SXin Li     // this context it is a bitfield. Also in range-based for statement colon
5213*67e74705SXin Li     // may delimit for-range-declaration.
5214*67e74705SXin Li     ColonProtectionRAIIObject X(*this,
5215*67e74705SXin Li                                 D.getContext() == Declarator::MemberContext ||
5216*67e74705SXin Li                                     (D.getContext() == Declarator::ForContext &&
5217*67e74705SXin Li                                      getLangOpts().CPlusPlus11));
5218*67e74705SXin Li 
5219*67e74705SXin Li     // ParseDeclaratorInternal might already have parsed the scope.
5220*67e74705SXin Li     if (D.getCXXScopeSpec().isEmpty()) {
5221*67e74705SXin Li       bool EnteringContext = D.getContext() == Declarator::FileContext ||
5222*67e74705SXin Li                              D.getContext() == Declarator::MemberContext;
5223*67e74705SXin Li       ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), nullptr,
5224*67e74705SXin Li                                      EnteringContext);
5225*67e74705SXin Li     }
5226*67e74705SXin Li 
5227*67e74705SXin Li     if (D.getCXXScopeSpec().isValid()) {
5228*67e74705SXin Li       if (Actions.ShouldEnterDeclaratorScope(getCurScope(),
5229*67e74705SXin Li                                              D.getCXXScopeSpec()))
5230*67e74705SXin Li         // Change the declaration context for name lookup, until this function
5231*67e74705SXin Li         // is exited (and the declarator has been parsed).
5232*67e74705SXin Li         DeclScopeObj.EnterDeclaratorScope();
5233*67e74705SXin Li     }
5234*67e74705SXin Li 
5235*67e74705SXin Li     // C++0x [dcl.fct]p14:
5236*67e74705SXin Li     //   There is a syntactic ambiguity when an ellipsis occurs at the end of a
5237*67e74705SXin Li     //   parameter-declaration-clause without a preceding comma. In this case,
5238*67e74705SXin Li     //   the ellipsis is parsed as part of the abstract-declarator if the type
5239*67e74705SXin Li     //   of the parameter either names a template parameter pack that has not
5240*67e74705SXin Li     //   been expanded or contains auto; otherwise, it is parsed as part of the
5241*67e74705SXin Li     //   parameter-declaration-clause.
5242*67e74705SXin Li     if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
5243*67e74705SXin Li         !((D.getContext() == Declarator::PrototypeContext ||
5244*67e74705SXin Li            D.getContext() == Declarator::LambdaExprParameterContext ||
5245*67e74705SXin Li            D.getContext() == Declarator::BlockLiteralContext) &&
5246*67e74705SXin Li           NextToken().is(tok::r_paren) &&
5247*67e74705SXin Li           !D.hasGroupingParens() &&
5248*67e74705SXin Li           !Actions.containsUnexpandedParameterPacks(D) &&
5249*67e74705SXin Li           D.getDeclSpec().getTypeSpecType() != TST_auto)) {
5250*67e74705SXin Li       SourceLocation EllipsisLoc = ConsumeToken();
5251*67e74705SXin Li       if (isPtrOperatorToken(Tok.getKind(), getLangOpts(), D.getContext())) {
5252*67e74705SXin Li         // The ellipsis was put in the wrong place. Recover, and explain to
5253*67e74705SXin Li         // the user what they should have done.
5254*67e74705SXin Li         ParseDeclarator(D);
5255*67e74705SXin Li         if (EllipsisLoc.isValid())
5256*67e74705SXin Li           DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
5257*67e74705SXin Li         return;
5258*67e74705SXin Li       } else
5259*67e74705SXin Li         D.setEllipsisLoc(EllipsisLoc);
5260*67e74705SXin Li 
5261*67e74705SXin Li       // The ellipsis can't be followed by a parenthesized declarator. We
5262*67e74705SXin Li       // check for that in ParseParenDeclarator, after we have disambiguated
5263*67e74705SXin Li       // the l_paren token.
5264*67e74705SXin Li     }
5265*67e74705SXin Li 
5266*67e74705SXin Li     if (Tok.isOneOf(tok::identifier, tok::kw_operator, tok::annot_template_id,
5267*67e74705SXin Li                     tok::tilde)) {
5268*67e74705SXin Li       // We found something that indicates the start of an unqualified-id.
5269*67e74705SXin Li       // Parse that unqualified-id.
5270*67e74705SXin Li       bool AllowConstructorName;
5271*67e74705SXin Li       if (D.getDeclSpec().hasTypeSpecifier())
5272*67e74705SXin Li         AllowConstructorName = false;
5273*67e74705SXin Li       else if (D.getCXXScopeSpec().isSet())
5274*67e74705SXin Li         AllowConstructorName =
5275*67e74705SXin Li           (D.getContext() == Declarator::FileContext ||
5276*67e74705SXin Li            D.getContext() == Declarator::MemberContext);
5277*67e74705SXin Li       else
5278*67e74705SXin Li         AllowConstructorName = (D.getContext() == Declarator::MemberContext);
5279*67e74705SXin Li 
5280*67e74705SXin Li       SourceLocation TemplateKWLoc;
5281*67e74705SXin Li       bool HadScope = D.getCXXScopeSpec().isValid();
5282*67e74705SXin Li       if (ParseUnqualifiedId(D.getCXXScopeSpec(),
5283*67e74705SXin Li                              /*EnteringContext=*/true,
5284*67e74705SXin Li                              /*AllowDestructorName=*/true, AllowConstructorName,
5285*67e74705SXin Li                              nullptr, TemplateKWLoc, D.getName()) ||
5286*67e74705SXin Li           // Once we're past the identifier, if the scope was bad, mark the
5287*67e74705SXin Li           // whole declarator bad.
5288*67e74705SXin Li           D.getCXXScopeSpec().isInvalid()) {
5289*67e74705SXin Li         D.SetIdentifier(nullptr, Tok.getLocation());
5290*67e74705SXin Li         D.setInvalidType(true);
5291*67e74705SXin Li       } else {
5292*67e74705SXin Li         // ParseUnqualifiedId might have parsed a scope specifier during error
5293*67e74705SXin Li         // recovery. If it did so, enter that scope.
5294*67e74705SXin Li         if (!HadScope && D.getCXXScopeSpec().isValid() &&
5295*67e74705SXin Li             Actions.ShouldEnterDeclaratorScope(getCurScope(),
5296*67e74705SXin Li                                                D.getCXXScopeSpec()))
5297*67e74705SXin Li           DeclScopeObj.EnterDeclaratorScope();
5298*67e74705SXin Li 
5299*67e74705SXin Li         // Parsed the unqualified-id; update range information and move along.
5300*67e74705SXin Li         if (D.getSourceRange().getBegin().isInvalid())
5301*67e74705SXin Li           D.SetRangeBegin(D.getName().getSourceRange().getBegin());
5302*67e74705SXin Li         D.SetRangeEnd(D.getName().getSourceRange().getEnd());
5303*67e74705SXin Li       }
5304*67e74705SXin Li       goto PastIdentifier;
5305*67e74705SXin Li     }
5306*67e74705SXin Li 
5307*67e74705SXin Li     if (D.getCXXScopeSpec().isNotEmpty()) {
5308*67e74705SXin Li       // We have a scope specifier but no following unqualified-id.
5309*67e74705SXin Li       Diag(PP.getLocForEndOfToken(D.getCXXScopeSpec().getEndLoc()),
5310*67e74705SXin Li            diag::err_expected_unqualified_id)
5311*67e74705SXin Li           << /*C++*/1;
5312*67e74705SXin Li       D.SetIdentifier(nullptr, Tok.getLocation());
5313*67e74705SXin Li       goto PastIdentifier;
5314*67e74705SXin Li     }
5315*67e74705SXin Li   } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
5316*67e74705SXin Li     assert(!getLangOpts().CPlusPlus &&
5317*67e74705SXin Li            "There's a C++-specific check for tok::identifier above");
5318*67e74705SXin Li     assert(Tok.getIdentifierInfo() && "Not an identifier?");
5319*67e74705SXin Li     D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
5320*67e74705SXin Li     D.SetRangeEnd(Tok.getLocation());
5321*67e74705SXin Li     ConsumeToken();
5322*67e74705SXin Li     goto PastIdentifier;
5323*67e74705SXin Li   } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
5324*67e74705SXin Li     // A virt-specifier isn't treated as an identifier if it appears after a
5325*67e74705SXin Li     // trailing-return-type.
5326*67e74705SXin Li     if (D.getContext() != Declarator::TrailingReturnContext ||
5327*67e74705SXin Li         !isCXX11VirtSpecifier(Tok)) {
5328*67e74705SXin Li       Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
5329*67e74705SXin Li         << FixItHint::CreateRemoval(Tok.getLocation());
5330*67e74705SXin Li       D.SetIdentifier(nullptr, Tok.getLocation());
5331*67e74705SXin Li       ConsumeToken();
5332*67e74705SXin Li       goto PastIdentifier;
5333*67e74705SXin Li     }
5334*67e74705SXin Li   }
5335*67e74705SXin Li 
5336*67e74705SXin Li   if (Tok.is(tok::l_paren)) {
5337*67e74705SXin Li     // direct-declarator: '(' declarator ')'
5338*67e74705SXin Li     // direct-declarator: '(' attributes declarator ')'
5339*67e74705SXin Li     // Example: 'char (*X)'   or 'int (*XX)(void)'
5340*67e74705SXin Li     ParseParenDeclarator(D);
5341*67e74705SXin Li 
5342*67e74705SXin Li     // If the declarator was parenthesized, we entered the declarator
5343*67e74705SXin Li     // scope when parsing the parenthesized declarator, then exited
5344*67e74705SXin Li     // the scope already. Re-enter the scope, if we need to.
5345*67e74705SXin Li     if (D.getCXXScopeSpec().isSet()) {
5346*67e74705SXin Li       // If there was an error parsing parenthesized declarator, declarator
5347*67e74705SXin Li       // scope may have been entered before. Don't do it again.
5348*67e74705SXin Li       if (!D.isInvalidType() &&
5349*67e74705SXin Li           Actions.ShouldEnterDeclaratorScope(getCurScope(),
5350*67e74705SXin Li                                              D.getCXXScopeSpec()))
5351*67e74705SXin Li         // Change the declaration context for name lookup, until this function
5352*67e74705SXin Li         // is exited (and the declarator has been parsed).
5353*67e74705SXin Li         DeclScopeObj.EnterDeclaratorScope();
5354*67e74705SXin Li     }
5355*67e74705SXin Li   } else if (D.mayOmitIdentifier()) {
5356*67e74705SXin Li     // This could be something simple like "int" (in which case the declarator
5357*67e74705SXin Li     // portion is empty), if an abstract-declarator is allowed.
5358*67e74705SXin Li     D.SetIdentifier(nullptr, Tok.getLocation());
5359*67e74705SXin Li 
5360*67e74705SXin Li     // The grammar for abstract-pack-declarator does not allow grouping parens.
5361*67e74705SXin Li     // FIXME: Revisit this once core issue 1488 is resolved.
5362*67e74705SXin Li     if (D.hasEllipsis() && D.hasGroupingParens())
5363*67e74705SXin Li       Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
5364*67e74705SXin Li            diag::ext_abstract_pack_declarator_parens);
5365*67e74705SXin Li   } else {
5366*67e74705SXin Li     if (Tok.getKind() == tok::annot_pragma_parser_crash)
5367*67e74705SXin Li       LLVM_BUILTIN_TRAP;
5368*67e74705SXin Li     if (Tok.is(tok::l_square))
5369*67e74705SXin Li       return ParseMisplacedBracketDeclarator(D);
5370*67e74705SXin Li     if (D.getContext() == Declarator::MemberContext) {
5371*67e74705SXin Li       Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
5372*67e74705SXin Li            diag::err_expected_member_name_or_semi)
5373*67e74705SXin Li           << (D.getDeclSpec().isEmpty() ? SourceRange()
5374*67e74705SXin Li                                         : D.getDeclSpec().getSourceRange());
5375*67e74705SXin Li     } else if (getLangOpts().CPlusPlus) {
5376*67e74705SXin Li       if (Tok.isOneOf(tok::period, tok::arrow))
5377*67e74705SXin Li         Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
5378*67e74705SXin Li       else {
5379*67e74705SXin Li         SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
5380*67e74705SXin Li         if (Tok.isAtStartOfLine() && Loc.isValid())
5381*67e74705SXin Li           Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
5382*67e74705SXin Li               << getLangOpts().CPlusPlus;
5383*67e74705SXin Li         else
5384*67e74705SXin Li           Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
5385*67e74705SXin Li                diag::err_expected_unqualified_id)
5386*67e74705SXin Li               << getLangOpts().CPlusPlus;
5387*67e74705SXin Li       }
5388*67e74705SXin Li     } else {
5389*67e74705SXin Li       Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
5390*67e74705SXin Li            diag::err_expected_either)
5391*67e74705SXin Li           << tok::identifier << tok::l_paren;
5392*67e74705SXin Li     }
5393*67e74705SXin Li     D.SetIdentifier(nullptr, Tok.getLocation());
5394*67e74705SXin Li     D.setInvalidType(true);
5395*67e74705SXin Li   }
5396*67e74705SXin Li 
5397*67e74705SXin Li  PastIdentifier:
5398*67e74705SXin Li   assert(D.isPastIdentifier() &&
5399*67e74705SXin Li          "Haven't past the location of the identifier yet?");
5400*67e74705SXin Li 
5401*67e74705SXin Li   // Don't parse attributes unless we have parsed an unparenthesized name.
5402*67e74705SXin Li   if (D.hasName() && !D.getNumTypeObjects())
5403*67e74705SXin Li     MaybeParseCXX11Attributes(D);
5404*67e74705SXin Li 
5405*67e74705SXin Li   while (1) {
5406*67e74705SXin Li     if (Tok.is(tok::l_paren)) {
5407*67e74705SXin Li       // Enter function-declaration scope, limiting any declarators to the
5408*67e74705SXin Li       // function prototype scope, including parameter declarators.
5409*67e74705SXin Li       ParseScope PrototypeScope(this,
5410*67e74705SXin Li                                 Scope::FunctionPrototypeScope|Scope::DeclScope|
5411*67e74705SXin Li                                 (D.isFunctionDeclaratorAFunctionDeclaration()
5412*67e74705SXin Li                                    ? Scope::FunctionDeclarationScope : 0));
5413*67e74705SXin Li 
5414*67e74705SXin Li       // The paren may be part of a C++ direct initializer, eg. "int x(1);".
5415*67e74705SXin Li       // In such a case, check if we actually have a function declarator; if it
5416*67e74705SXin Li       // is not, the declarator has been fully parsed.
5417*67e74705SXin Li       bool IsAmbiguous = false;
5418*67e74705SXin Li       if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
5419*67e74705SXin Li         // The name of the declarator, if any, is tentatively declared within
5420*67e74705SXin Li         // a possible direct initializer.
5421*67e74705SXin Li         TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
5422*67e74705SXin Li         bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
5423*67e74705SXin Li         TentativelyDeclaredIdentifiers.pop_back();
5424*67e74705SXin Li         if (!IsFunctionDecl)
5425*67e74705SXin Li           break;
5426*67e74705SXin Li       }
5427*67e74705SXin Li       ParsedAttributes attrs(AttrFactory);
5428*67e74705SXin Li       BalancedDelimiterTracker T(*this, tok::l_paren);
5429*67e74705SXin Li       T.consumeOpen();
5430*67e74705SXin Li       ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
5431*67e74705SXin Li       PrototypeScope.Exit();
5432*67e74705SXin Li     } else if (Tok.is(tok::l_square)) {
5433*67e74705SXin Li       ParseBracketDeclarator(D);
5434*67e74705SXin Li     } else {
5435*67e74705SXin Li       break;
5436*67e74705SXin Li     }
5437*67e74705SXin Li   }
5438*67e74705SXin Li }
5439*67e74705SXin Li 
5440*67e74705SXin Li /// ParseParenDeclarator - We parsed the declarator D up to a paren.  This is
5441*67e74705SXin Li /// only called before the identifier, so these are most likely just grouping
5442*67e74705SXin Li /// parens for precedence.  If we find that these are actually function
5443*67e74705SXin Li /// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
5444*67e74705SXin Li ///
5445*67e74705SXin Li ///       direct-declarator:
5446*67e74705SXin Li ///         '(' declarator ')'
5447*67e74705SXin Li /// [GNU]   '(' attributes declarator ')'
5448*67e74705SXin Li ///         direct-declarator '(' parameter-type-list ')'
5449*67e74705SXin Li ///         direct-declarator '(' identifier-list[opt] ')'
5450*67e74705SXin Li /// [GNU]   direct-declarator '(' parameter-forward-declarations
5451*67e74705SXin Li ///                    parameter-type-list[opt] ')'
5452*67e74705SXin Li ///
ParseParenDeclarator(Declarator & D)5453*67e74705SXin Li void Parser::ParseParenDeclarator(Declarator &D) {
5454*67e74705SXin Li   BalancedDelimiterTracker T(*this, tok::l_paren);
5455*67e74705SXin Li   T.consumeOpen();
5456*67e74705SXin Li 
5457*67e74705SXin Li   assert(!D.isPastIdentifier() && "Should be called before passing identifier");
5458*67e74705SXin Li 
5459*67e74705SXin Li   // Eat any attributes before we look at whether this is a grouping or function
5460*67e74705SXin Li   // declarator paren.  If this is a grouping paren, the attribute applies to
5461*67e74705SXin Li   // the type being built up, for example:
5462*67e74705SXin Li   //     int (__attribute__(()) *x)(long y)
5463*67e74705SXin Li   // If this ends up not being a grouping paren, the attribute applies to the
5464*67e74705SXin Li   // first argument, for example:
5465*67e74705SXin Li   //     int (__attribute__(()) int x)
5466*67e74705SXin Li   // In either case, we need to eat any attributes to be able to determine what
5467*67e74705SXin Li   // sort of paren this is.
5468*67e74705SXin Li   //
5469*67e74705SXin Li   ParsedAttributes attrs(AttrFactory);
5470*67e74705SXin Li   bool RequiresArg = false;
5471*67e74705SXin Li   if (Tok.is(tok::kw___attribute)) {
5472*67e74705SXin Li     ParseGNUAttributes(attrs);
5473*67e74705SXin Li 
5474*67e74705SXin Li     // We require that the argument list (if this is a non-grouping paren) be
5475*67e74705SXin Li     // present even if the attribute list was empty.
5476*67e74705SXin Li     RequiresArg = true;
5477*67e74705SXin Li   }
5478*67e74705SXin Li 
5479*67e74705SXin Li   // Eat any Microsoft extensions.
5480*67e74705SXin Li   ParseMicrosoftTypeAttributes(attrs);
5481*67e74705SXin Li 
5482*67e74705SXin Li   // Eat any Borland extensions.
5483*67e74705SXin Li   if  (Tok.is(tok::kw___pascal))
5484*67e74705SXin Li     ParseBorlandTypeAttributes(attrs);
5485*67e74705SXin Li 
5486*67e74705SXin Li   // If we haven't past the identifier yet (or where the identifier would be
5487*67e74705SXin Li   // stored, if this is an abstract declarator), then this is probably just
5488*67e74705SXin Li   // grouping parens. However, if this could be an abstract-declarator, then
5489*67e74705SXin Li   // this could also be the start of function arguments (consider 'void()').
5490*67e74705SXin Li   bool isGrouping;
5491*67e74705SXin Li 
5492*67e74705SXin Li   if (!D.mayOmitIdentifier()) {
5493*67e74705SXin Li     // If this can't be an abstract-declarator, this *must* be a grouping
5494*67e74705SXin Li     // paren, because we haven't seen the identifier yet.
5495*67e74705SXin Li     isGrouping = true;
5496*67e74705SXin Li   } else if (Tok.is(tok::r_paren) ||           // 'int()' is a function.
5497*67e74705SXin Li              (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
5498*67e74705SXin Li               NextToken().is(tok::r_paren)) || // C++ int(...)
5499*67e74705SXin Li              isDeclarationSpecifier() ||       // 'int(int)' is a function.
5500*67e74705SXin Li              isCXX11AttributeSpecifier()) {    // 'int([[]]int)' is a function.
5501*67e74705SXin Li     // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
5502*67e74705SXin Li     // considered to be a type, not a K&R identifier-list.
5503*67e74705SXin Li     isGrouping = false;
5504*67e74705SXin Li   } else {
5505*67e74705SXin Li     // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
5506*67e74705SXin Li     isGrouping = true;
5507*67e74705SXin Li   }
5508*67e74705SXin Li 
5509*67e74705SXin Li   // If this is a grouping paren, handle:
5510*67e74705SXin Li   // direct-declarator: '(' declarator ')'
5511*67e74705SXin Li   // direct-declarator: '(' attributes declarator ')'
5512*67e74705SXin Li   if (isGrouping) {
5513*67e74705SXin Li     SourceLocation EllipsisLoc = D.getEllipsisLoc();
5514*67e74705SXin Li     D.setEllipsisLoc(SourceLocation());
5515*67e74705SXin Li 
5516*67e74705SXin Li     bool hadGroupingParens = D.hasGroupingParens();
5517*67e74705SXin Li     D.setGroupingParens(true);
5518*67e74705SXin Li     ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
5519*67e74705SXin Li     // Match the ')'.
5520*67e74705SXin Li     T.consumeClose();
5521*67e74705SXin Li     D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
5522*67e74705SXin Li                                             T.getCloseLocation()),
5523*67e74705SXin Li                   attrs, T.getCloseLocation());
5524*67e74705SXin Li 
5525*67e74705SXin Li     D.setGroupingParens(hadGroupingParens);
5526*67e74705SXin Li 
5527*67e74705SXin Li     // An ellipsis cannot be placed outside parentheses.
5528*67e74705SXin Li     if (EllipsisLoc.isValid())
5529*67e74705SXin Li       DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
5530*67e74705SXin Li 
5531*67e74705SXin Li     return;
5532*67e74705SXin Li   }
5533*67e74705SXin Li 
5534*67e74705SXin Li   // Okay, if this wasn't a grouping paren, it must be the start of a function
5535*67e74705SXin Li   // argument list.  Recognize that this declarator will never have an
5536*67e74705SXin Li   // identifier (and remember where it would have been), then call into
5537*67e74705SXin Li   // ParseFunctionDeclarator to handle of argument list.
5538*67e74705SXin Li   D.SetIdentifier(nullptr, Tok.getLocation());
5539*67e74705SXin Li 
5540*67e74705SXin Li   // Enter function-declaration scope, limiting any declarators to the
5541*67e74705SXin Li   // function prototype scope, including parameter declarators.
5542*67e74705SXin Li   ParseScope PrototypeScope(this,
5543*67e74705SXin Li                             Scope::FunctionPrototypeScope | Scope::DeclScope |
5544*67e74705SXin Li                             (D.isFunctionDeclaratorAFunctionDeclaration()
5545*67e74705SXin Li                                ? Scope::FunctionDeclarationScope : 0));
5546*67e74705SXin Li   ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
5547*67e74705SXin Li   PrototypeScope.Exit();
5548*67e74705SXin Li }
5549*67e74705SXin Li 
5550*67e74705SXin Li /// ParseFunctionDeclarator - We are after the identifier and have parsed the
5551*67e74705SXin Li /// declarator D up to a paren, which indicates that we are parsing function
5552*67e74705SXin Li /// arguments.
5553*67e74705SXin Li ///
5554*67e74705SXin Li /// If FirstArgAttrs is non-null, then the caller parsed those arguments
5555*67e74705SXin Li /// immediately after the open paren - they should be considered to be the
5556*67e74705SXin Li /// first argument of a parameter.
5557*67e74705SXin Li ///
5558*67e74705SXin Li /// If RequiresArg is true, then the first argument of the function is required
5559*67e74705SXin Li /// to be present and required to not be an identifier list.
5560*67e74705SXin Li ///
5561*67e74705SXin Li /// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
5562*67e74705SXin Li /// (C++11) ref-qualifier[opt], exception-specification[opt],
5563*67e74705SXin Li /// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
5564*67e74705SXin Li ///
5565*67e74705SXin Li /// [C++11] exception-specification:
5566*67e74705SXin Li ///           dynamic-exception-specification
5567*67e74705SXin Li ///           noexcept-specification
5568*67e74705SXin Li ///
ParseFunctionDeclarator(Declarator & D,ParsedAttributes & FirstArgAttrs,BalancedDelimiterTracker & Tracker,bool IsAmbiguous,bool RequiresArg)5569*67e74705SXin Li void Parser::ParseFunctionDeclarator(Declarator &D,
5570*67e74705SXin Li                                      ParsedAttributes &FirstArgAttrs,
5571*67e74705SXin Li                                      BalancedDelimiterTracker &Tracker,
5572*67e74705SXin Li                                      bool IsAmbiguous,
5573*67e74705SXin Li                                      bool RequiresArg) {
5574*67e74705SXin Li   assert(getCurScope()->isFunctionPrototypeScope() &&
5575*67e74705SXin Li          "Should call from a Function scope");
5576*67e74705SXin Li   // lparen is already consumed!
5577*67e74705SXin Li   assert(D.isPastIdentifier() && "Should not call before identifier!");
5578*67e74705SXin Li 
5579*67e74705SXin Li   // This should be true when the function has typed arguments.
5580*67e74705SXin Li   // Otherwise, it is treated as a K&R-style function.
5581*67e74705SXin Li   bool HasProto = false;
5582*67e74705SXin Li   // Build up an array of information about the parsed arguments.
5583*67e74705SXin Li   SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
5584*67e74705SXin Li   // Remember where we see an ellipsis, if any.
5585*67e74705SXin Li   SourceLocation EllipsisLoc;
5586*67e74705SXin Li 
5587*67e74705SXin Li   DeclSpec DS(AttrFactory);
5588*67e74705SXin Li   bool RefQualifierIsLValueRef = true;
5589*67e74705SXin Li   SourceLocation RefQualifierLoc;
5590*67e74705SXin Li   SourceLocation ConstQualifierLoc;
5591*67e74705SXin Li   SourceLocation VolatileQualifierLoc;
5592*67e74705SXin Li   SourceLocation RestrictQualifierLoc;
5593*67e74705SXin Li   ExceptionSpecificationType ESpecType = EST_None;
5594*67e74705SXin Li   SourceRange ESpecRange;
5595*67e74705SXin Li   SmallVector<ParsedType, 2> DynamicExceptions;
5596*67e74705SXin Li   SmallVector<SourceRange, 2> DynamicExceptionRanges;
5597*67e74705SXin Li   ExprResult NoexceptExpr;
5598*67e74705SXin Li   CachedTokens *ExceptionSpecTokens = nullptr;
5599*67e74705SXin Li   ParsedAttributes FnAttrs(AttrFactory);
5600*67e74705SXin Li   TypeResult TrailingReturnType;
5601*67e74705SXin Li 
5602*67e74705SXin Li   /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5603*67e74705SXin Li      EndLoc is the end location for the function declarator.
5604*67e74705SXin Li      They differ for trailing return types. */
5605*67e74705SXin Li   SourceLocation StartLoc, LocalEndLoc, EndLoc;
5606*67e74705SXin Li   SourceLocation LParenLoc, RParenLoc;
5607*67e74705SXin Li   LParenLoc = Tracker.getOpenLocation();
5608*67e74705SXin Li   StartLoc = LParenLoc;
5609*67e74705SXin Li 
5610*67e74705SXin Li   if (isFunctionDeclaratorIdentifierList()) {
5611*67e74705SXin Li     if (RequiresArg)
5612*67e74705SXin Li       Diag(Tok, diag::err_argument_required_after_attribute);
5613*67e74705SXin Li 
5614*67e74705SXin Li     ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5615*67e74705SXin Li 
5616*67e74705SXin Li     Tracker.consumeClose();
5617*67e74705SXin Li     RParenLoc = Tracker.getCloseLocation();
5618*67e74705SXin Li     LocalEndLoc = RParenLoc;
5619*67e74705SXin Li     EndLoc = RParenLoc;
5620*67e74705SXin Li   } else {
5621*67e74705SXin Li     if (Tok.isNot(tok::r_paren))
5622*67e74705SXin Li       ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5623*67e74705SXin Li                                       EllipsisLoc);
5624*67e74705SXin Li     else if (RequiresArg)
5625*67e74705SXin Li       Diag(Tok, diag::err_argument_required_after_attribute);
5626*67e74705SXin Li 
5627*67e74705SXin Li     HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
5628*67e74705SXin Li 
5629*67e74705SXin Li     // If we have the closing ')', eat it.
5630*67e74705SXin Li     Tracker.consumeClose();
5631*67e74705SXin Li     RParenLoc = Tracker.getCloseLocation();
5632*67e74705SXin Li     LocalEndLoc = RParenLoc;
5633*67e74705SXin Li     EndLoc = RParenLoc;
5634*67e74705SXin Li 
5635*67e74705SXin Li     if (getLangOpts().CPlusPlus) {
5636*67e74705SXin Li       // FIXME: Accept these components in any order, and produce fixits to
5637*67e74705SXin Li       // correct the order if the user gets it wrong. Ideally we should deal
5638*67e74705SXin Li       // with the pure-specifier in the same way.
5639*67e74705SXin Li 
5640*67e74705SXin Li       // Parse cv-qualifier-seq[opt].
5641*67e74705SXin Li       ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed,
5642*67e74705SXin Li                                 /*AtomicAllowed*/ false);
5643*67e74705SXin Li       if (!DS.getSourceRange().getEnd().isInvalid()) {
5644*67e74705SXin Li         EndLoc = DS.getSourceRange().getEnd();
5645*67e74705SXin Li         ConstQualifierLoc = DS.getConstSpecLoc();
5646*67e74705SXin Li         VolatileQualifierLoc = DS.getVolatileSpecLoc();
5647*67e74705SXin Li         RestrictQualifierLoc = DS.getRestrictSpecLoc();
5648*67e74705SXin Li       }
5649*67e74705SXin Li 
5650*67e74705SXin Li       // Parse ref-qualifier[opt].
5651*67e74705SXin Li       if (ParseRefQualifier(RefQualifierIsLValueRef, RefQualifierLoc))
5652*67e74705SXin Li         EndLoc = RefQualifierLoc;
5653*67e74705SXin Li 
5654*67e74705SXin Li       // C++11 [expr.prim.general]p3:
5655*67e74705SXin Li       //   If a declaration declares a member function or member function
5656*67e74705SXin Li       //   template of a class X, the expression this is a prvalue of type
5657*67e74705SXin Li       //   "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
5658*67e74705SXin Li       //   and the end of the function-definition, member-declarator, or
5659*67e74705SXin Li       //   declarator.
5660*67e74705SXin Li       // FIXME: currently, "static" case isn't handled correctly.
5661*67e74705SXin Li       bool IsCXX11MemberFunction =
5662*67e74705SXin Li         getLangOpts().CPlusPlus11 &&
5663*67e74705SXin Li         D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
5664*67e74705SXin Li         (D.getContext() == Declarator::MemberContext
5665*67e74705SXin Li          ? !D.getDeclSpec().isFriendSpecified()
5666*67e74705SXin Li          : D.getContext() == Declarator::FileContext &&
5667*67e74705SXin Li            D.getCXXScopeSpec().isValid() &&
5668*67e74705SXin Li            Actions.CurContext->isRecord());
5669*67e74705SXin Li       Sema::CXXThisScopeRAII ThisScope(Actions,
5670*67e74705SXin Li                                dyn_cast<CXXRecordDecl>(Actions.CurContext),
5671*67e74705SXin Li                                DS.getTypeQualifiers() |
5672*67e74705SXin Li                                (D.getDeclSpec().isConstexprSpecified() &&
5673*67e74705SXin Li                                 !getLangOpts().CPlusPlus14
5674*67e74705SXin Li                                   ? Qualifiers::Const : 0),
5675*67e74705SXin Li                                IsCXX11MemberFunction);
5676*67e74705SXin Li 
5677*67e74705SXin Li       // Parse exception-specification[opt].
5678*67e74705SXin Li       bool Delayed = D.isFirstDeclarationOfMember() &&
5679*67e74705SXin Li                      D.isFunctionDeclaratorAFunctionDeclaration();
5680*67e74705SXin Li       if (Delayed && Actions.isLibstdcxxEagerExceptionSpecHack(D) &&
5681*67e74705SXin Li           GetLookAheadToken(0).is(tok::kw_noexcept) &&
5682*67e74705SXin Li           GetLookAheadToken(1).is(tok::l_paren) &&
5683*67e74705SXin Li           GetLookAheadToken(2).is(tok::kw_noexcept) &&
5684*67e74705SXin Li           GetLookAheadToken(3).is(tok::l_paren) &&
5685*67e74705SXin Li           GetLookAheadToken(4).is(tok::identifier) &&
5686*67e74705SXin Li           GetLookAheadToken(4).getIdentifierInfo()->isStr("swap")) {
5687*67e74705SXin Li         // HACK: We've got an exception-specification
5688*67e74705SXin Li         //   noexcept(noexcept(swap(...)))
5689*67e74705SXin Li         // or
5690*67e74705SXin Li         //   noexcept(noexcept(swap(...)) && noexcept(swap(...)))
5691*67e74705SXin Li         // on a 'swap' member function. This is a libstdc++ bug; the lookup
5692*67e74705SXin Li         // for 'swap' will only find the function we're currently declaring,
5693*67e74705SXin Li         // whereas it expects to find a non-member swap through ADL. Turn off
5694*67e74705SXin Li         // delayed parsing to give it a chance to find what it expects.
5695*67e74705SXin Li         Delayed = false;
5696*67e74705SXin Li       }
5697*67e74705SXin Li       ESpecType = tryParseExceptionSpecification(Delayed,
5698*67e74705SXin Li                                                  ESpecRange,
5699*67e74705SXin Li                                                  DynamicExceptions,
5700*67e74705SXin Li                                                  DynamicExceptionRanges,
5701*67e74705SXin Li                                                  NoexceptExpr,
5702*67e74705SXin Li                                                  ExceptionSpecTokens);
5703*67e74705SXin Li       if (ESpecType != EST_None)
5704*67e74705SXin Li         EndLoc = ESpecRange.getEnd();
5705*67e74705SXin Li 
5706*67e74705SXin Li       // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5707*67e74705SXin Li       // after the exception-specification.
5708*67e74705SXin Li       MaybeParseCXX11Attributes(FnAttrs);
5709*67e74705SXin Li 
5710*67e74705SXin Li       // Parse trailing-return-type[opt].
5711*67e74705SXin Li       LocalEndLoc = EndLoc;
5712*67e74705SXin Li       if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
5713*67e74705SXin Li         Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
5714*67e74705SXin Li         if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5715*67e74705SXin Li           StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
5716*67e74705SXin Li         LocalEndLoc = Tok.getLocation();
5717*67e74705SXin Li         SourceRange Range;
5718*67e74705SXin Li         TrailingReturnType = ParseTrailingReturnType(Range);
5719*67e74705SXin Li         EndLoc = Range.getEnd();
5720*67e74705SXin Li       }
5721*67e74705SXin Li     }
5722*67e74705SXin Li   }
5723*67e74705SXin Li 
5724*67e74705SXin Li   // Remember that we parsed a function type, and remember the attributes.
5725*67e74705SXin Li   D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
5726*67e74705SXin Li                                              IsAmbiguous,
5727*67e74705SXin Li                                              LParenLoc,
5728*67e74705SXin Li                                              ParamInfo.data(), ParamInfo.size(),
5729*67e74705SXin Li                                              EllipsisLoc, RParenLoc,
5730*67e74705SXin Li                                              DS.getTypeQualifiers(),
5731*67e74705SXin Li                                              RefQualifierIsLValueRef,
5732*67e74705SXin Li                                              RefQualifierLoc, ConstQualifierLoc,
5733*67e74705SXin Li                                              VolatileQualifierLoc,
5734*67e74705SXin Li                                              RestrictQualifierLoc,
5735*67e74705SXin Li                                              /*MutableLoc=*/SourceLocation(),
5736*67e74705SXin Li                                              ESpecType, ESpecRange,
5737*67e74705SXin Li                                              DynamicExceptions.data(),
5738*67e74705SXin Li                                              DynamicExceptionRanges.data(),
5739*67e74705SXin Li                                              DynamicExceptions.size(),
5740*67e74705SXin Li                                              NoexceptExpr.isUsable() ?
5741*67e74705SXin Li                                                NoexceptExpr.get() : nullptr,
5742*67e74705SXin Li                                              ExceptionSpecTokens,
5743*67e74705SXin Li                                              StartLoc, LocalEndLoc, D,
5744*67e74705SXin Li                                              TrailingReturnType),
5745*67e74705SXin Li                 FnAttrs, EndLoc);
5746*67e74705SXin Li }
5747*67e74705SXin Li 
5748*67e74705SXin Li /// ParseRefQualifier - Parses a member function ref-qualifier. Returns
5749*67e74705SXin Li /// true if a ref-qualifier is found.
ParseRefQualifier(bool & RefQualifierIsLValueRef,SourceLocation & RefQualifierLoc)5750*67e74705SXin Li bool Parser::ParseRefQualifier(bool &RefQualifierIsLValueRef,
5751*67e74705SXin Li                                SourceLocation &RefQualifierLoc) {
5752*67e74705SXin Li   if (Tok.isOneOf(tok::amp, tok::ampamp)) {
5753*67e74705SXin Li     Diag(Tok, getLangOpts().CPlusPlus11 ?
5754*67e74705SXin Li          diag::warn_cxx98_compat_ref_qualifier :
5755*67e74705SXin Li          diag::ext_ref_qualifier);
5756*67e74705SXin Li 
5757*67e74705SXin Li     RefQualifierIsLValueRef = Tok.is(tok::amp);
5758*67e74705SXin Li     RefQualifierLoc = ConsumeToken();
5759*67e74705SXin Li     return true;
5760*67e74705SXin Li   }
5761*67e74705SXin Li   return false;
5762*67e74705SXin Li }
5763*67e74705SXin Li 
5764*67e74705SXin Li /// isFunctionDeclaratorIdentifierList - This parameter list may have an
5765*67e74705SXin Li /// identifier list form for a K&R-style function:  void foo(a,b,c)
5766*67e74705SXin Li ///
5767*67e74705SXin Li /// Note that identifier-lists are only allowed for normal declarators, not for
5768*67e74705SXin Li /// abstract-declarators.
isFunctionDeclaratorIdentifierList()5769*67e74705SXin Li bool Parser::isFunctionDeclaratorIdentifierList() {
5770*67e74705SXin Li   return !getLangOpts().CPlusPlus
5771*67e74705SXin Li          && Tok.is(tok::identifier)
5772*67e74705SXin Li          && !TryAltiVecVectorToken()
5773*67e74705SXin Li          // K&R identifier lists can't have typedefs as identifiers, per C99
5774*67e74705SXin Li          // 6.7.5.3p11.
5775*67e74705SXin Li          && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5776*67e74705SXin Li          // Identifier lists follow a really simple grammar: the identifiers can
5777*67e74705SXin Li          // be followed *only* by a ", identifier" or ")".  However, K&R
5778*67e74705SXin Li          // identifier lists are really rare in the brave new modern world, and
5779*67e74705SXin Li          // it is very common for someone to typo a type in a non-K&R style
5780*67e74705SXin Li          // list.  If we are presented with something like: "void foo(intptr x,
5781*67e74705SXin Li          // float y)", we don't want to start parsing the function declarator as
5782*67e74705SXin Li          // though it is a K&R style declarator just because intptr is an
5783*67e74705SXin Li          // invalid type.
5784*67e74705SXin Li          //
5785*67e74705SXin Li          // To handle this, we check to see if the token after the first
5786*67e74705SXin Li          // identifier is a "," or ")".  Only then do we parse it as an
5787*67e74705SXin Li          // identifier list.
5788*67e74705SXin Li          && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5789*67e74705SXin Li }
5790*67e74705SXin Li 
5791*67e74705SXin Li /// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5792*67e74705SXin Li /// we found a K&R-style identifier list instead of a typed parameter list.
5793*67e74705SXin Li ///
5794*67e74705SXin Li /// After returning, ParamInfo will hold the parsed parameters.
5795*67e74705SXin Li ///
5796*67e74705SXin Li ///       identifier-list: [C99 6.7.5]
5797*67e74705SXin Li ///         identifier
5798*67e74705SXin Li ///         identifier-list ',' identifier
5799*67e74705SXin Li ///
ParseFunctionDeclaratorIdentifierList(Declarator & D,SmallVectorImpl<DeclaratorChunk::ParamInfo> & ParamInfo)5800*67e74705SXin Li void Parser::ParseFunctionDeclaratorIdentifierList(
5801*67e74705SXin Li        Declarator &D,
5802*67e74705SXin Li        SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
5803*67e74705SXin Li   // If there was no identifier specified for the declarator, either we are in
5804*67e74705SXin Li   // an abstract-declarator, or we are in a parameter declarator which was found
5805*67e74705SXin Li   // to be abstract.  In abstract-declarators, identifier lists are not valid:
5806*67e74705SXin Li   // diagnose this.
5807*67e74705SXin Li   if (!D.getIdentifier())
5808*67e74705SXin Li     Diag(Tok, diag::ext_ident_list_in_param);
5809*67e74705SXin Li 
5810*67e74705SXin Li   // Maintain an efficient lookup of params we have seen so far.
5811*67e74705SXin Li   llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5812*67e74705SXin Li 
5813*67e74705SXin Li   do {
5814*67e74705SXin Li     // If this isn't an identifier, report the error and skip until ')'.
5815*67e74705SXin Li     if (Tok.isNot(tok::identifier)) {
5816*67e74705SXin Li       Diag(Tok, diag::err_expected) << tok::identifier;
5817*67e74705SXin Li       SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
5818*67e74705SXin Li       // Forget we parsed anything.
5819*67e74705SXin Li       ParamInfo.clear();
5820*67e74705SXin Li       return;
5821*67e74705SXin Li     }
5822*67e74705SXin Li 
5823*67e74705SXin Li     IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5824*67e74705SXin Li 
5825*67e74705SXin Li     // Reject 'typedef int y; int test(x, y)', but continue parsing.
5826*67e74705SXin Li     if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5827*67e74705SXin Li       Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5828*67e74705SXin Li 
5829*67e74705SXin Li     // Verify that the argument identifier has not already been mentioned.
5830*67e74705SXin Li     if (!ParamsSoFar.insert(ParmII).second) {
5831*67e74705SXin Li       Diag(Tok, diag::err_param_redefinition) << ParmII;
5832*67e74705SXin Li     } else {
5833*67e74705SXin Li       // Remember this identifier in ParamInfo.
5834*67e74705SXin Li       ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5835*67e74705SXin Li                                                      Tok.getLocation(),
5836*67e74705SXin Li                                                      nullptr));
5837*67e74705SXin Li     }
5838*67e74705SXin Li 
5839*67e74705SXin Li     // Eat the identifier.
5840*67e74705SXin Li     ConsumeToken();
5841*67e74705SXin Li     // The list continues if we see a comma.
5842*67e74705SXin Li   } while (TryConsumeToken(tok::comma));
5843*67e74705SXin Li }
5844*67e74705SXin Li 
5845*67e74705SXin Li /// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5846*67e74705SXin Li /// after the opening parenthesis. This function will not parse a K&R-style
5847*67e74705SXin Li /// identifier list.
5848*67e74705SXin Li ///
5849*67e74705SXin Li /// D is the declarator being parsed.  If FirstArgAttrs is non-null, then the
5850*67e74705SXin Li /// caller parsed those arguments immediately after the open paren - they should
5851*67e74705SXin Li /// be considered to be part of the first parameter.
5852*67e74705SXin Li ///
5853*67e74705SXin Li /// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5854*67e74705SXin Li /// be the location of the ellipsis, if any was parsed.
5855*67e74705SXin Li ///
5856*67e74705SXin Li ///       parameter-type-list: [C99 6.7.5]
5857*67e74705SXin Li ///         parameter-list
5858*67e74705SXin Li ///         parameter-list ',' '...'
5859*67e74705SXin Li /// [C++]   parameter-list '...'
5860*67e74705SXin Li ///
5861*67e74705SXin Li ///       parameter-list: [C99 6.7.5]
5862*67e74705SXin Li ///         parameter-declaration
5863*67e74705SXin Li ///         parameter-list ',' parameter-declaration
5864*67e74705SXin Li ///
5865*67e74705SXin Li ///       parameter-declaration: [C99 6.7.5]
5866*67e74705SXin Li ///         declaration-specifiers declarator
5867*67e74705SXin Li /// [C++]   declaration-specifiers declarator '=' assignment-expression
5868*67e74705SXin Li /// [C++11]                                       initializer-clause
5869*67e74705SXin Li /// [GNU]   declaration-specifiers declarator attributes
5870*67e74705SXin Li ///         declaration-specifiers abstract-declarator[opt]
5871*67e74705SXin Li /// [C++]   declaration-specifiers abstract-declarator[opt]
5872*67e74705SXin Li ///           '=' assignment-expression
5873*67e74705SXin Li /// [GNU]   declaration-specifiers abstract-declarator[opt] attributes
5874*67e74705SXin Li /// [C++11] attribute-specifier-seq parameter-declaration
5875*67e74705SXin Li ///
ParseParameterDeclarationClause(Declarator & D,ParsedAttributes & FirstArgAttrs,SmallVectorImpl<DeclaratorChunk::ParamInfo> & ParamInfo,SourceLocation & EllipsisLoc)5876*67e74705SXin Li void Parser::ParseParameterDeclarationClause(
5877*67e74705SXin Li        Declarator &D,
5878*67e74705SXin Li        ParsedAttributes &FirstArgAttrs,
5879*67e74705SXin Li        SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
5880*67e74705SXin Li        SourceLocation &EllipsisLoc) {
5881*67e74705SXin Li   do {
5882*67e74705SXin Li     // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5883*67e74705SXin Li     // before deciding this was a parameter-declaration-clause.
5884*67e74705SXin Li     if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
5885*67e74705SXin Li       break;
5886*67e74705SXin Li 
5887*67e74705SXin Li     // Parse the declaration-specifiers.
5888*67e74705SXin Li     // Just use the ParsingDeclaration "scope" of the declarator.
5889*67e74705SXin Li     DeclSpec DS(AttrFactory);
5890*67e74705SXin Li 
5891*67e74705SXin Li     // Parse any C++11 attributes.
5892*67e74705SXin Li     MaybeParseCXX11Attributes(DS.getAttributes());
5893*67e74705SXin Li 
5894*67e74705SXin Li     // Skip any Microsoft attributes before a param.
5895*67e74705SXin Li     MaybeParseMicrosoftAttributes(DS.getAttributes());
5896*67e74705SXin Li 
5897*67e74705SXin Li     SourceLocation DSStart = Tok.getLocation();
5898*67e74705SXin Li 
5899*67e74705SXin Li     // If the caller parsed attributes for the first argument, add them now.
5900*67e74705SXin Li     // Take them so that we only apply the attributes to the first parameter.
5901*67e74705SXin Li     // FIXME: If we can leave the attributes in the token stream somehow, we can
5902*67e74705SXin Li     // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5903*67e74705SXin Li     // too much hassle.
5904*67e74705SXin Li     DS.takeAttributesFrom(FirstArgAttrs);
5905*67e74705SXin Li 
5906*67e74705SXin Li     ParseDeclarationSpecifiers(DS);
5907*67e74705SXin Li 
5908*67e74705SXin Li 
5909*67e74705SXin Li     // Parse the declarator.  This is "PrototypeContext" or
5910*67e74705SXin Li     // "LambdaExprParameterContext", because we must accept either
5911*67e74705SXin Li     // 'declarator' or 'abstract-declarator' here.
5912*67e74705SXin Li     Declarator ParmDeclarator(DS,
5913*67e74705SXin Li               D.getContext() == Declarator::LambdaExprContext ?
5914*67e74705SXin Li                                   Declarator::LambdaExprParameterContext :
5915*67e74705SXin Li                                                 Declarator::PrototypeContext);
5916*67e74705SXin Li     ParseDeclarator(ParmDeclarator);
5917*67e74705SXin Li 
5918*67e74705SXin Li     // Parse GNU attributes, if present.
5919*67e74705SXin Li     MaybeParseGNUAttributes(ParmDeclarator);
5920*67e74705SXin Li 
5921*67e74705SXin Li     // Remember this parsed parameter in ParamInfo.
5922*67e74705SXin Li     IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
5923*67e74705SXin Li 
5924*67e74705SXin Li     // DefArgToks is used when the parsing of default arguments needs
5925*67e74705SXin Li     // to be delayed.
5926*67e74705SXin Li     CachedTokens *DefArgToks = nullptr;
5927*67e74705SXin Li 
5928*67e74705SXin Li     // If no parameter was specified, verify that *something* was specified,
5929*67e74705SXin Li     // otherwise we have a missing type and identifier.
5930*67e74705SXin Li     if (DS.isEmpty() && ParmDeclarator.getIdentifier() == nullptr &&
5931*67e74705SXin Li         ParmDeclarator.getNumTypeObjects() == 0) {
5932*67e74705SXin Li       // Completely missing, emit error.
5933*67e74705SXin Li       Diag(DSStart, diag::err_missing_param);
5934*67e74705SXin Li     } else {
5935*67e74705SXin Li       // Otherwise, we have something.  Add it and let semantic analysis try
5936*67e74705SXin Li       // to grok it and add the result to the ParamInfo we are building.
5937*67e74705SXin Li 
5938*67e74705SXin Li       // Last chance to recover from a misplaced ellipsis in an attempted
5939*67e74705SXin Li       // parameter pack declaration.
5940*67e74705SXin Li       if (Tok.is(tok::ellipsis) &&
5941*67e74705SXin Li           (NextToken().isNot(tok::r_paren) ||
5942*67e74705SXin Li            (!ParmDeclarator.getEllipsisLoc().isValid() &&
5943*67e74705SXin Li             !Actions.isUnexpandedParameterPackPermitted())) &&
5944*67e74705SXin Li           Actions.containsUnexpandedParameterPacks(ParmDeclarator))
5945*67e74705SXin Li         DiagnoseMisplacedEllipsisInDeclarator(ConsumeToken(), ParmDeclarator);
5946*67e74705SXin Li 
5947*67e74705SXin Li       // Inform the actions module about the parameter declarator, so it gets
5948*67e74705SXin Li       // added to the current scope.
5949*67e74705SXin Li       Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
5950*67e74705SXin Li       // Parse the default argument, if any. We parse the default
5951*67e74705SXin Li       // arguments in all dialects; the semantic analysis in
5952*67e74705SXin Li       // ActOnParamDefaultArgument will reject the default argument in
5953*67e74705SXin Li       // C.
5954*67e74705SXin Li       if (Tok.is(tok::equal)) {
5955*67e74705SXin Li         SourceLocation EqualLoc = Tok.getLocation();
5956*67e74705SXin Li 
5957*67e74705SXin Li         // Parse the default argument
5958*67e74705SXin Li         if (D.getContext() == Declarator::MemberContext) {
5959*67e74705SXin Li           // If we're inside a class definition, cache the tokens
5960*67e74705SXin Li           // corresponding to the default argument. We'll actually parse
5961*67e74705SXin Li           // them when we see the end of the class definition.
5962*67e74705SXin Li           // FIXME: Can we use a smart pointer for Toks?
5963*67e74705SXin Li           DefArgToks = new CachedTokens;
5964*67e74705SXin Li 
5965*67e74705SXin Li           SourceLocation ArgStartLoc = NextToken().getLocation();
5966*67e74705SXin Li           if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
5967*67e74705SXin Li             delete DefArgToks;
5968*67e74705SXin Li             DefArgToks = nullptr;
5969*67e74705SXin Li             Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
5970*67e74705SXin Li           } else {
5971*67e74705SXin Li             Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
5972*67e74705SXin Li                                                       ArgStartLoc);
5973*67e74705SXin Li           }
5974*67e74705SXin Li         } else {
5975*67e74705SXin Li           // Consume the '='.
5976*67e74705SXin Li           ConsumeToken();
5977*67e74705SXin Li 
5978*67e74705SXin Li           // The argument isn't actually potentially evaluated unless it is
5979*67e74705SXin Li           // used.
5980*67e74705SXin Li           EnterExpressionEvaluationContext Eval(Actions,
5981*67e74705SXin Li                                               Sema::PotentiallyEvaluatedIfUsed,
5982*67e74705SXin Li                                                 Param);
5983*67e74705SXin Li 
5984*67e74705SXin Li           ExprResult DefArgResult;
5985*67e74705SXin Li           if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
5986*67e74705SXin Li             Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
5987*67e74705SXin Li             DefArgResult = ParseBraceInitializer();
5988*67e74705SXin Li           } else
5989*67e74705SXin Li             DefArgResult = ParseAssignmentExpression();
5990*67e74705SXin Li           DefArgResult = Actions.CorrectDelayedTyposInExpr(DefArgResult);
5991*67e74705SXin Li           if (DefArgResult.isInvalid()) {
5992*67e74705SXin Li             Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
5993*67e74705SXin Li             SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
5994*67e74705SXin Li           } else {
5995*67e74705SXin Li             // Inform the actions module about the default argument
5996*67e74705SXin Li             Actions.ActOnParamDefaultArgument(Param, EqualLoc,
5997*67e74705SXin Li                                               DefArgResult.get());
5998*67e74705SXin Li           }
5999*67e74705SXin Li         }
6000*67e74705SXin Li       }
6001*67e74705SXin Li 
6002*67e74705SXin Li       ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
6003*67e74705SXin Li                                           ParmDeclarator.getIdentifierLoc(),
6004*67e74705SXin Li                                           Param, DefArgToks));
6005*67e74705SXin Li     }
6006*67e74705SXin Li 
6007*67e74705SXin Li     if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) {
6008*67e74705SXin Li       if (!getLangOpts().CPlusPlus) {
6009*67e74705SXin Li         // We have ellipsis without a preceding ',', which is ill-formed
6010*67e74705SXin Li         // in C. Complain and provide the fix.
6011*67e74705SXin Li         Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
6012*67e74705SXin Li             << FixItHint::CreateInsertion(EllipsisLoc, ", ");
6013*67e74705SXin Li       } else if (ParmDeclarator.getEllipsisLoc().isValid() ||
6014*67e74705SXin Li                  Actions.containsUnexpandedParameterPacks(ParmDeclarator)) {
6015*67e74705SXin Li         // It looks like this was supposed to be a parameter pack. Warn and
6016*67e74705SXin Li         // point out where the ellipsis should have gone.
6017*67e74705SXin Li         SourceLocation ParmEllipsis = ParmDeclarator.getEllipsisLoc();
6018*67e74705SXin Li         Diag(EllipsisLoc, diag::warn_misplaced_ellipsis_vararg)
6019*67e74705SXin Li           << ParmEllipsis.isValid() << ParmEllipsis;
6020*67e74705SXin Li         if (ParmEllipsis.isValid()) {
6021*67e74705SXin Li           Diag(ParmEllipsis,
6022*67e74705SXin Li                diag::note_misplaced_ellipsis_vararg_existing_ellipsis);
6023*67e74705SXin Li         } else {
6024*67e74705SXin Li           Diag(ParmDeclarator.getIdentifierLoc(),
6025*67e74705SXin Li                diag::note_misplaced_ellipsis_vararg_add_ellipsis)
6026*67e74705SXin Li             << FixItHint::CreateInsertion(ParmDeclarator.getIdentifierLoc(),
6027*67e74705SXin Li                                           "...")
6028*67e74705SXin Li             << !ParmDeclarator.hasName();
6029*67e74705SXin Li         }
6030*67e74705SXin Li         Diag(EllipsisLoc, diag::note_misplaced_ellipsis_vararg_add_comma)
6031*67e74705SXin Li           << FixItHint::CreateInsertion(EllipsisLoc, ", ");
6032*67e74705SXin Li       }
6033*67e74705SXin Li 
6034*67e74705SXin Li       // We can't have any more parameters after an ellipsis.
6035*67e74705SXin Li       break;
6036*67e74705SXin Li     }
6037*67e74705SXin Li 
6038*67e74705SXin Li     // If the next token is a comma, consume it and keep reading arguments.
6039*67e74705SXin Li   } while (TryConsumeToken(tok::comma));
6040*67e74705SXin Li }
6041*67e74705SXin Li 
6042*67e74705SXin Li /// [C90]   direct-declarator '[' constant-expression[opt] ']'
6043*67e74705SXin Li /// [C99]   direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
6044*67e74705SXin Li /// [C99]   direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
6045*67e74705SXin Li /// [C99]   direct-declarator '[' type-qual-list 'static' assignment-expr ']'
6046*67e74705SXin Li /// [C99]   direct-declarator '[' type-qual-list[opt] '*' ']'
6047*67e74705SXin Li /// [C++11] direct-declarator '[' constant-expression[opt] ']'
6048*67e74705SXin Li ///                           attribute-specifier-seq[opt]
ParseBracketDeclarator(Declarator & D)6049*67e74705SXin Li void Parser::ParseBracketDeclarator(Declarator &D) {
6050*67e74705SXin Li   if (CheckProhibitedCXX11Attribute())
6051*67e74705SXin Li     return;
6052*67e74705SXin Li 
6053*67e74705SXin Li   BalancedDelimiterTracker T(*this, tok::l_square);
6054*67e74705SXin Li   T.consumeOpen();
6055*67e74705SXin Li 
6056*67e74705SXin Li   // C array syntax has many features, but by-far the most common is [] and [4].
6057*67e74705SXin Li   // This code does a fast path to handle some of the most obvious cases.
6058*67e74705SXin Li   if (Tok.getKind() == tok::r_square) {
6059*67e74705SXin Li     T.consumeClose();
6060*67e74705SXin Li     ParsedAttributes attrs(AttrFactory);
6061*67e74705SXin Li     MaybeParseCXX11Attributes(attrs);
6062*67e74705SXin Li 
6063*67e74705SXin Li     // Remember that we parsed the empty array type.
6064*67e74705SXin Li     D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, nullptr,
6065*67e74705SXin Li                                             T.getOpenLocation(),
6066*67e74705SXin Li                                             T.getCloseLocation()),
6067*67e74705SXin Li                   attrs, T.getCloseLocation());
6068*67e74705SXin Li     return;
6069*67e74705SXin Li   } else if (Tok.getKind() == tok::numeric_constant &&
6070*67e74705SXin Li              GetLookAheadToken(1).is(tok::r_square)) {
6071*67e74705SXin Li     // [4] is very common.  Parse the numeric constant expression.
6072*67e74705SXin Li     ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
6073*67e74705SXin Li     ConsumeToken();
6074*67e74705SXin Li 
6075*67e74705SXin Li     T.consumeClose();
6076*67e74705SXin Li     ParsedAttributes attrs(AttrFactory);
6077*67e74705SXin Li     MaybeParseCXX11Attributes(attrs);
6078*67e74705SXin Li 
6079*67e74705SXin Li     // Remember that we parsed a array type, and remember its features.
6080*67e74705SXin Li     D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
6081*67e74705SXin Li                                             ExprRes.get(),
6082*67e74705SXin Li                                             T.getOpenLocation(),
6083*67e74705SXin Li                                             T.getCloseLocation()),
6084*67e74705SXin Li                   attrs, T.getCloseLocation());
6085*67e74705SXin Li     return;
6086*67e74705SXin Li   } else if (Tok.getKind() == tok::code_completion) {
6087*67e74705SXin Li     Actions.CodeCompleteBracketDeclarator(getCurScope());
6088*67e74705SXin Li     return cutOffParsing();
6089*67e74705SXin Li   }
6090*67e74705SXin Li 
6091*67e74705SXin Li   // If valid, this location is the position where we read the 'static' keyword.
6092*67e74705SXin Li   SourceLocation StaticLoc;
6093*67e74705SXin Li   TryConsumeToken(tok::kw_static, StaticLoc);
6094*67e74705SXin Li 
6095*67e74705SXin Li   // If there is a type-qualifier-list, read it now.
6096*67e74705SXin Li   // Type qualifiers in an array subscript are a C99 feature.
6097*67e74705SXin Li   DeclSpec DS(AttrFactory);
6098*67e74705SXin Li   ParseTypeQualifierListOpt(DS, AR_CXX11AttributesParsed);
6099*67e74705SXin Li 
6100*67e74705SXin Li   // If we haven't already read 'static', check to see if there is one after the
6101*67e74705SXin Li   // type-qualifier-list.
6102*67e74705SXin Li   if (!StaticLoc.isValid())
6103*67e74705SXin Li     TryConsumeToken(tok::kw_static, StaticLoc);
6104*67e74705SXin Li 
6105*67e74705SXin Li   // Handle "direct-declarator [ type-qual-list[opt] * ]".
6106*67e74705SXin Li   bool isStar = false;
6107*67e74705SXin Li   ExprResult NumElements;
6108*67e74705SXin Li 
6109*67e74705SXin Li   // Handle the case where we have '[*]' as the array size.  However, a leading
6110*67e74705SXin Li   // star could be the start of an expression, for example 'X[*p + 4]'.  Verify
6111*67e74705SXin Li   // the token after the star is a ']'.  Since stars in arrays are
6112*67e74705SXin Li   // infrequent, use of lookahead is not costly here.
6113*67e74705SXin Li   if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
6114*67e74705SXin Li     ConsumeToken();  // Eat the '*'.
6115*67e74705SXin Li 
6116*67e74705SXin Li     if (StaticLoc.isValid()) {
6117*67e74705SXin Li       Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
6118*67e74705SXin Li       StaticLoc = SourceLocation();  // Drop the static.
6119*67e74705SXin Li     }
6120*67e74705SXin Li     isStar = true;
6121*67e74705SXin Li   } else if (Tok.isNot(tok::r_square)) {
6122*67e74705SXin Li     // Note, in C89, this production uses the constant-expr production instead
6123*67e74705SXin Li     // of assignment-expr.  The only difference is that assignment-expr allows
6124*67e74705SXin Li     // things like '=' and '*='.  Sema rejects these in C89 mode because they
6125*67e74705SXin Li     // are not i-c-e's, so we don't need to distinguish between the two here.
6126*67e74705SXin Li 
6127*67e74705SXin Li     // Parse the constant-expression or assignment-expression now (depending
6128*67e74705SXin Li     // on dialect).
6129*67e74705SXin Li     if (getLangOpts().CPlusPlus) {
6130*67e74705SXin Li       NumElements = ParseConstantExpression();
6131*67e74705SXin Li     } else {
6132*67e74705SXin Li       EnterExpressionEvaluationContext Unevaluated(Actions,
6133*67e74705SXin Li                                                    Sema::ConstantEvaluated);
6134*67e74705SXin Li       NumElements =
6135*67e74705SXin Li           Actions.CorrectDelayedTyposInExpr(ParseAssignmentExpression());
6136*67e74705SXin Li     }
6137*67e74705SXin Li   } else {
6138*67e74705SXin Li     if (StaticLoc.isValid()) {
6139*67e74705SXin Li       Diag(StaticLoc, diag::err_unspecified_size_with_static);
6140*67e74705SXin Li       StaticLoc = SourceLocation();  // Drop the static.
6141*67e74705SXin Li     }
6142*67e74705SXin Li   }
6143*67e74705SXin Li 
6144*67e74705SXin Li   // If there was an error parsing the assignment-expression, recover.
6145*67e74705SXin Li   if (NumElements.isInvalid()) {
6146*67e74705SXin Li     D.setInvalidType(true);
6147*67e74705SXin Li     // If the expression was invalid, skip it.
6148*67e74705SXin Li     SkipUntil(tok::r_square, StopAtSemi);
6149*67e74705SXin Li     return;
6150*67e74705SXin Li   }
6151*67e74705SXin Li 
6152*67e74705SXin Li   T.consumeClose();
6153*67e74705SXin Li 
6154*67e74705SXin Li   ParsedAttributes attrs(AttrFactory);
6155*67e74705SXin Li   MaybeParseCXX11Attributes(attrs);
6156*67e74705SXin Li 
6157*67e74705SXin Li   // Remember that we parsed a array type, and remember its features.
6158*67e74705SXin Li   D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
6159*67e74705SXin Li                                           StaticLoc.isValid(), isStar,
6160*67e74705SXin Li                                           NumElements.get(),
6161*67e74705SXin Li                                           T.getOpenLocation(),
6162*67e74705SXin Li                                           T.getCloseLocation()),
6163*67e74705SXin Li                 attrs, T.getCloseLocation());
6164*67e74705SXin Li }
6165*67e74705SXin Li 
6166*67e74705SXin Li /// Diagnose brackets before an identifier.
ParseMisplacedBracketDeclarator(Declarator & D)6167*67e74705SXin Li void Parser::ParseMisplacedBracketDeclarator(Declarator &D) {
6168*67e74705SXin Li   assert(Tok.is(tok::l_square) && "Missing opening bracket");
6169*67e74705SXin Li   assert(!D.mayOmitIdentifier() && "Declarator cannot omit identifier");
6170*67e74705SXin Li 
6171*67e74705SXin Li   SourceLocation StartBracketLoc = Tok.getLocation();
6172*67e74705SXin Li   Declarator TempDeclarator(D.getDeclSpec(), D.getContext());
6173*67e74705SXin Li 
6174*67e74705SXin Li   while (Tok.is(tok::l_square)) {
6175*67e74705SXin Li     ParseBracketDeclarator(TempDeclarator);
6176*67e74705SXin Li   }
6177*67e74705SXin Li 
6178*67e74705SXin Li   // Stuff the location of the start of the brackets into the Declarator.
6179*67e74705SXin Li   // The diagnostics from ParseDirectDeclarator will make more sense if
6180*67e74705SXin Li   // they use this location instead.
6181*67e74705SXin Li   if (Tok.is(tok::semi))
6182*67e74705SXin Li     D.getName().EndLocation = StartBracketLoc;
6183*67e74705SXin Li 
6184*67e74705SXin Li   SourceLocation SuggestParenLoc = Tok.getLocation();
6185*67e74705SXin Li 
6186*67e74705SXin Li   // Now that the brackets are removed, try parsing the declarator again.
6187*67e74705SXin Li   ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
6188*67e74705SXin Li 
6189*67e74705SXin Li   // Something went wrong parsing the brackets, in which case,
6190*67e74705SXin Li   // ParseBracketDeclarator has emitted an error, and we don't need to emit
6191*67e74705SXin Li   // one here.
6192*67e74705SXin Li   if (TempDeclarator.getNumTypeObjects() == 0)
6193*67e74705SXin Li     return;
6194*67e74705SXin Li 
6195*67e74705SXin Li   // Determine if parens will need to be suggested in the diagnostic.
6196*67e74705SXin Li   bool NeedParens = false;
6197*67e74705SXin Li   if (D.getNumTypeObjects() != 0) {
6198*67e74705SXin Li     switch (D.getTypeObject(D.getNumTypeObjects() - 1).Kind) {
6199*67e74705SXin Li     case DeclaratorChunk::Pointer:
6200*67e74705SXin Li     case DeclaratorChunk::Reference:
6201*67e74705SXin Li     case DeclaratorChunk::BlockPointer:
6202*67e74705SXin Li     case DeclaratorChunk::MemberPointer:
6203*67e74705SXin Li     case DeclaratorChunk::Pipe:
6204*67e74705SXin Li       NeedParens = true;
6205*67e74705SXin Li       break;
6206*67e74705SXin Li     case DeclaratorChunk::Array:
6207*67e74705SXin Li     case DeclaratorChunk::Function:
6208*67e74705SXin Li     case DeclaratorChunk::Paren:
6209*67e74705SXin Li       break;
6210*67e74705SXin Li     }
6211*67e74705SXin Li   }
6212*67e74705SXin Li 
6213*67e74705SXin Li   if (NeedParens) {
6214*67e74705SXin Li     // Create a DeclaratorChunk for the inserted parens.
6215*67e74705SXin Li     ParsedAttributes attrs(AttrFactory);
6216*67e74705SXin Li     SourceLocation EndLoc = PP.getLocForEndOfToken(D.getLocEnd());
6217*67e74705SXin Li     D.AddTypeInfo(DeclaratorChunk::getParen(SuggestParenLoc, EndLoc), attrs,
6218*67e74705SXin Li                   SourceLocation());
6219*67e74705SXin Li   }
6220*67e74705SXin Li 
6221*67e74705SXin Li   // Adding back the bracket info to the end of the Declarator.
6222*67e74705SXin Li   for (unsigned i = 0, e = TempDeclarator.getNumTypeObjects(); i < e; ++i) {
6223*67e74705SXin Li     const DeclaratorChunk &Chunk = TempDeclarator.getTypeObject(i);
6224*67e74705SXin Li     ParsedAttributes attrs(AttrFactory);
6225*67e74705SXin Li     attrs.set(Chunk.Common.AttrList);
6226*67e74705SXin Li     D.AddTypeInfo(Chunk, attrs, SourceLocation());
6227*67e74705SXin Li   }
6228*67e74705SXin Li 
6229*67e74705SXin Li   // The missing identifier would have been diagnosed in ParseDirectDeclarator.
6230*67e74705SXin Li   // If parentheses are required, always suggest them.
6231*67e74705SXin Li   if (!D.getIdentifier() && !NeedParens)
6232*67e74705SXin Li     return;
6233*67e74705SXin Li 
6234*67e74705SXin Li   SourceLocation EndBracketLoc = TempDeclarator.getLocEnd();
6235*67e74705SXin Li 
6236*67e74705SXin Li   // Generate the move bracket error message.
6237*67e74705SXin Li   SourceRange BracketRange(StartBracketLoc, EndBracketLoc);
6238*67e74705SXin Li   SourceLocation EndLoc = PP.getLocForEndOfToken(D.getLocEnd());
6239*67e74705SXin Li 
6240*67e74705SXin Li   if (NeedParens) {
6241*67e74705SXin Li     Diag(EndLoc, diag::err_brackets_go_after_unqualified_id)
6242*67e74705SXin Li         << getLangOpts().CPlusPlus
6243*67e74705SXin Li         << FixItHint::CreateInsertion(SuggestParenLoc, "(")
6244*67e74705SXin Li         << FixItHint::CreateInsertion(EndLoc, ")")
6245*67e74705SXin Li         << FixItHint::CreateInsertionFromRange(
6246*67e74705SXin Li                EndLoc, CharSourceRange(BracketRange, true))
6247*67e74705SXin Li         << FixItHint::CreateRemoval(BracketRange);
6248*67e74705SXin Li   } else {
6249*67e74705SXin Li     Diag(EndLoc, diag::err_brackets_go_after_unqualified_id)
6250*67e74705SXin Li         << getLangOpts().CPlusPlus
6251*67e74705SXin Li         << FixItHint::CreateInsertionFromRange(
6252*67e74705SXin Li                EndLoc, CharSourceRange(BracketRange, true))
6253*67e74705SXin Li         << FixItHint::CreateRemoval(BracketRange);
6254*67e74705SXin Li   }
6255*67e74705SXin Li }
6256*67e74705SXin Li 
6257*67e74705SXin Li /// [GNU]   typeof-specifier:
6258*67e74705SXin Li ///           typeof ( expressions )
6259*67e74705SXin Li ///           typeof ( type-name )
6260*67e74705SXin Li /// [GNU/C++] typeof unary-expression
6261*67e74705SXin Li ///
ParseTypeofSpecifier(DeclSpec & DS)6262*67e74705SXin Li void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
6263*67e74705SXin Li   assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
6264*67e74705SXin Li   Token OpTok = Tok;
6265*67e74705SXin Li   SourceLocation StartLoc = ConsumeToken();
6266*67e74705SXin Li 
6267*67e74705SXin Li   const bool hasParens = Tok.is(tok::l_paren);
6268*67e74705SXin Li 
6269*67e74705SXin Li   EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
6270*67e74705SXin Li                                                Sema::ReuseLambdaContextDecl);
6271*67e74705SXin Li 
6272*67e74705SXin Li   bool isCastExpr;
6273*67e74705SXin Li   ParsedType CastTy;
6274*67e74705SXin Li   SourceRange CastRange;
6275*67e74705SXin Li   ExprResult Operand = Actions.CorrectDelayedTyposInExpr(
6276*67e74705SXin Li       ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr, CastTy, CastRange));
6277*67e74705SXin Li   if (hasParens)
6278*67e74705SXin Li     DS.setTypeofParensRange(CastRange);
6279*67e74705SXin Li 
6280*67e74705SXin Li   if (CastRange.getEnd().isInvalid())
6281*67e74705SXin Li     // FIXME: Not accurate, the range gets one token more than it should.
6282*67e74705SXin Li     DS.SetRangeEnd(Tok.getLocation());
6283*67e74705SXin Li   else
6284*67e74705SXin Li     DS.SetRangeEnd(CastRange.getEnd());
6285*67e74705SXin Li 
6286*67e74705SXin Li   if (isCastExpr) {
6287*67e74705SXin Li     if (!CastTy) {
6288*67e74705SXin Li       DS.SetTypeSpecError();
6289*67e74705SXin Li       return;
6290*67e74705SXin Li     }
6291*67e74705SXin Li 
6292*67e74705SXin Li     const char *PrevSpec = nullptr;
6293*67e74705SXin Li     unsigned DiagID;
6294*67e74705SXin Li     // Check for duplicate type specifiers (e.g. "int typeof(int)").
6295*67e74705SXin Li     if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
6296*67e74705SXin Li                            DiagID, CastTy,
6297*67e74705SXin Li                            Actions.getASTContext().getPrintingPolicy()))
6298*67e74705SXin Li       Diag(StartLoc, DiagID) << PrevSpec;
6299*67e74705SXin Li     return;
6300*67e74705SXin Li   }
6301*67e74705SXin Li 
6302*67e74705SXin Li   // If we get here, the operand to the typeof was an expresion.
6303*67e74705SXin Li   if (Operand.isInvalid()) {
6304*67e74705SXin Li     DS.SetTypeSpecError();
6305*67e74705SXin Li     return;
6306*67e74705SXin Li   }
6307*67e74705SXin Li 
6308*67e74705SXin Li   // We might need to transform the operand if it is potentially evaluated.
6309*67e74705SXin Li   Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
6310*67e74705SXin Li   if (Operand.isInvalid()) {
6311*67e74705SXin Li     DS.SetTypeSpecError();
6312*67e74705SXin Li     return;
6313*67e74705SXin Li   }
6314*67e74705SXin Li 
6315*67e74705SXin Li   const char *PrevSpec = nullptr;
6316*67e74705SXin Li   unsigned DiagID;
6317*67e74705SXin Li   // Check for duplicate type specifiers (e.g. "int typeof(int)").
6318*67e74705SXin Li   if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
6319*67e74705SXin Li                          DiagID, Operand.get(),
6320*67e74705SXin Li                          Actions.getASTContext().getPrintingPolicy()))
6321*67e74705SXin Li     Diag(StartLoc, DiagID) << PrevSpec;
6322*67e74705SXin Li }
6323*67e74705SXin Li 
6324*67e74705SXin Li /// [C11]   atomic-specifier:
6325*67e74705SXin Li ///           _Atomic ( type-name )
6326*67e74705SXin Li ///
ParseAtomicSpecifier(DeclSpec & DS)6327*67e74705SXin Li void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
6328*67e74705SXin Li   assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
6329*67e74705SXin Li          "Not an atomic specifier");
6330*67e74705SXin Li 
6331*67e74705SXin Li   SourceLocation StartLoc = ConsumeToken();
6332*67e74705SXin Li   BalancedDelimiterTracker T(*this, tok::l_paren);
6333*67e74705SXin Li   if (T.consumeOpen())
6334*67e74705SXin Li     return;
6335*67e74705SXin Li 
6336*67e74705SXin Li   TypeResult Result = ParseTypeName();
6337*67e74705SXin Li   if (Result.isInvalid()) {
6338*67e74705SXin Li     SkipUntil(tok::r_paren, StopAtSemi);
6339*67e74705SXin Li     return;
6340*67e74705SXin Li   }
6341*67e74705SXin Li 
6342*67e74705SXin Li   // Match the ')'
6343*67e74705SXin Li   T.consumeClose();
6344*67e74705SXin Li 
6345*67e74705SXin Li   if (T.getCloseLocation().isInvalid())
6346*67e74705SXin Li     return;
6347*67e74705SXin Li 
6348*67e74705SXin Li   DS.setTypeofParensRange(T.getRange());
6349*67e74705SXin Li   DS.SetRangeEnd(T.getCloseLocation());
6350*67e74705SXin Li 
6351*67e74705SXin Li   const char *PrevSpec = nullptr;
6352*67e74705SXin Li   unsigned DiagID;
6353*67e74705SXin Li   if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
6354*67e74705SXin Li                          DiagID, Result.get(),
6355*67e74705SXin Li                          Actions.getASTContext().getPrintingPolicy()))
6356*67e74705SXin Li     Diag(StartLoc, DiagID) << PrevSpec;
6357*67e74705SXin Li }
6358*67e74705SXin Li 
6359*67e74705SXin Li /// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
6360*67e74705SXin Li /// from TryAltiVecVectorToken.
TryAltiVecVectorTokenOutOfLine()6361*67e74705SXin Li bool Parser::TryAltiVecVectorTokenOutOfLine() {
6362*67e74705SXin Li   Token Next = NextToken();
6363*67e74705SXin Li   switch (Next.getKind()) {
6364*67e74705SXin Li   default: return false;
6365*67e74705SXin Li   case tok::kw_short:
6366*67e74705SXin Li   case tok::kw_long:
6367*67e74705SXin Li   case tok::kw_signed:
6368*67e74705SXin Li   case tok::kw_unsigned:
6369*67e74705SXin Li   case tok::kw_void:
6370*67e74705SXin Li   case tok::kw_char:
6371*67e74705SXin Li   case tok::kw_int:
6372*67e74705SXin Li   case tok::kw_float:
6373*67e74705SXin Li   case tok::kw_double:
6374*67e74705SXin Li   case tok::kw_bool:
6375*67e74705SXin Li   case tok::kw___bool:
6376*67e74705SXin Li   case tok::kw___pixel:
6377*67e74705SXin Li     Tok.setKind(tok::kw___vector);
6378*67e74705SXin Li     return true;
6379*67e74705SXin Li   case tok::identifier:
6380*67e74705SXin Li     if (Next.getIdentifierInfo() == Ident_pixel) {
6381*67e74705SXin Li       Tok.setKind(tok::kw___vector);
6382*67e74705SXin Li       return true;
6383*67e74705SXin Li     }
6384*67e74705SXin Li     if (Next.getIdentifierInfo() == Ident_bool) {
6385*67e74705SXin Li       Tok.setKind(tok::kw___vector);
6386*67e74705SXin Li       return true;
6387*67e74705SXin Li     }
6388*67e74705SXin Li     return false;
6389*67e74705SXin Li   }
6390*67e74705SXin Li }
6391*67e74705SXin Li 
TryAltiVecTokenOutOfLine(DeclSpec & DS,SourceLocation Loc,const char * & PrevSpec,unsigned & DiagID,bool & isInvalid)6392*67e74705SXin Li bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
6393*67e74705SXin Li                                       const char *&PrevSpec, unsigned &DiagID,
6394*67e74705SXin Li                                       bool &isInvalid) {
6395*67e74705SXin Li   const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
6396*67e74705SXin Li   if (Tok.getIdentifierInfo() == Ident_vector) {
6397*67e74705SXin Li     Token Next = NextToken();
6398*67e74705SXin Li     switch (Next.getKind()) {
6399*67e74705SXin Li     case tok::kw_short:
6400*67e74705SXin Li     case tok::kw_long:
6401*67e74705SXin Li     case tok::kw_signed:
6402*67e74705SXin Li     case tok::kw_unsigned:
6403*67e74705SXin Li     case tok::kw_void:
6404*67e74705SXin Li     case tok::kw_char:
6405*67e74705SXin Li     case tok::kw_int:
6406*67e74705SXin Li     case tok::kw_float:
6407*67e74705SXin Li     case tok::kw_double:
6408*67e74705SXin Li     case tok::kw_bool:
6409*67e74705SXin Li     case tok::kw___bool:
6410*67e74705SXin Li     case tok::kw___pixel:
6411*67e74705SXin Li       isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
6412*67e74705SXin Li       return true;
6413*67e74705SXin Li     case tok::identifier:
6414*67e74705SXin Li       if (Next.getIdentifierInfo() == Ident_pixel) {
6415*67e74705SXin Li         isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
6416*67e74705SXin Li         return true;
6417*67e74705SXin Li       }
6418*67e74705SXin Li       if (Next.getIdentifierInfo() == Ident_bool) {
6419*67e74705SXin Li         isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
6420*67e74705SXin Li         return true;
6421*67e74705SXin Li       }
6422*67e74705SXin Li       break;
6423*67e74705SXin Li     default:
6424*67e74705SXin Li       break;
6425*67e74705SXin Li     }
6426*67e74705SXin Li   } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
6427*67e74705SXin Li              DS.isTypeAltiVecVector()) {
6428*67e74705SXin Li     isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
6429*67e74705SXin Li     return true;
6430*67e74705SXin Li   } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
6431*67e74705SXin Li              DS.isTypeAltiVecVector()) {
6432*67e74705SXin Li     isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
6433*67e74705SXin Li     return true;
6434*67e74705SXin Li   }
6435*67e74705SXin Li   return false;
6436*67e74705SXin Li }
6437