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