blob: 66b98c2c4da906e0ff6c4b6d3620e5f05161a674 [file] [log] [blame]
Chris Lattner7ad0fbe2006-11-05 07:46:30 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner5b12ab82007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Erik Verbruggen888d52a2014-01-15 09:15:43 +000016#include "clang/AST/ASTContext.h"
Chandler Carruth757fcd62014-03-04 10:05:20 +000017#include "clang/AST/DeclTemplate.h"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000018#include "clang/Basic/AddressSpaces.h"
Aaron Ballmanfdd783a2014-03-31 18:18:43 +000019#include "clang/Basic/Attributes.h"
Jordan Rosea7d03842013-02-08 22:30:41 +000020#include "clang/Basic/CharInfo.h"
Aaron Ballmanfdd783a2014-03-31 18:18:43 +000021#include "clang/Basic/TargetInfo.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000023#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000024#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000025#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000026#include "clang/Sema/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000027#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000028#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000029#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000030using namespace clang;
31
32//===----------------------------------------------------------------------===//
33// C99 6.7: Declarations.
34//===----------------------------------------------------------------------===//
35
Chris Lattnerf5fbd792006-08-10 23:56:11 +000036/// ParseTypeName
37/// type-name: [C99 6.7.6]
38/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000039///
40/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000041TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000042 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000043 AccessSpecifier AS,
Richard Smith54ecd982013-02-20 19:22:51 +000044 Decl **OwnedType,
45 ParsedAttributes *Attrs) {
Richard Smith62dad822012-03-15 01:02:11 +000046 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith2f07ad52012-05-09 20:55:26 +000047 if (DSC == DSC_normal)
48 DSC = DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000049
Chris Lattnerf5fbd792006-08-10 23:56:11 +000050 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000051 DeclSpec DS(AttrFactory);
Richard Smith54ecd982013-02-20 19:22:51 +000052 if (Attrs)
53 DS.addAttributes(Attrs->getList());
Richard Smithbfdb1082012-03-12 08:56:40 +000054 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000055 if (OwnedType)
Craig Topper161e4db2014-05-21 06:02:52 +000056 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : nullptr;
Sebastian Redld6434562009-05-29 18:02:33 +000057
Chris Lattnerf5fbd792006-08-10 23:56:11 +000058 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000059 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000060 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000061 if (Range)
62 *Range = DeclaratorInfo.getSourceRange();
63
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000064 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000065 return true;
66
Douglas Gregor0be31a22010-07-02 17:43:08 +000067 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000068}
69
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000070
71/// isAttributeLateParsed - Return true if the attribute has arguments that
72/// require late parsing.
73static bool isAttributeLateParsed(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +000074#define CLANG_ATTR_LATE_PARSED_LIST
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000075 return llvm::StringSwitch<bool>(II.getName())
Aaron Ballman35db2b32014-01-29 22:13:45 +000076#include "clang/Parse/AttrParserStringSwitches.inc"
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000077 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +000078#undef CLANG_ATTR_LATE_PARSED_LIST
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000079}
80
Alexis Hunt96d5c762009-11-21 08:43:09 +000081/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000082///
83/// [GNU] attributes:
84/// attribute
85/// attributes attribute
86///
87/// [GNU] attribute:
88/// '__attribute__' '(' '(' attribute-list ')' ')'
89///
90/// [GNU] attribute-list:
91/// attrib
92/// attribute_list ',' attrib
93///
94/// [GNU] attrib:
95/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000096/// attrib-name
97/// attrib-name '(' identifier ')'
98/// attrib-name '(' identifier ',' nonempty-expr-list ')'
99/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000100///
Steve Naroff0f2fe172007-06-01 17:11:19 +0000101/// [GNU] attrib-name:
102/// identifier
103/// typespec
104/// typequal
105/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +0000106///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000107/// Whether an attribute takes an 'identifier' is determined by the
108/// attrib-name. GCC's behavior here is not worth imitating:
Steve Naroff0f2fe172007-06-01 17:11:19 +0000109///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000110/// * In C mode, if the attribute argument list starts with an identifier
111/// followed by a ',' or an ')', and the identifier doesn't resolve to
112/// a type, it is parsed as an identifier. If the attribute actually
113/// wanted an expression, it's out of luck (but it turns out that no
114/// attributes work that way, because C constant expressions are very
115/// limited).
116/// * In C++ mode, if the attribute argument list starts with an identifier,
117/// and the attribute *wants* an identifier, it is parsed as an identifier.
118/// At block scope, any additional tokens between the identifier and the
119/// ',' or ')' are ignored, otherwise they produce a parse error.
Richard Smithb12bf692011-10-17 21:20:17 +0000120///
Richard Smithf7ca0c02013-09-03 18:57:36 +0000121/// We follow the C++ model, but don't allow junk after the identifier.
John McCall53fa7142010-12-24 02:08:15 +0000122void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000123 SourceLocation *endLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000124 LateParsedAttrList *LateAttrs,
125 Declarator *D) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000126 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000127
Chris Lattner76c72282007-10-09 17:33:22 +0000128 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000129 ConsumeToken();
130 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
131 "attribute")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000132 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000133 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000134 }
135 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000136 SkipUntil(tok::r_paren, StopAtSemi); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000137 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000138 }
139 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Alp Toker094e5212014-01-05 03:27:11 +0000140 while (true) {
141 // Allow empty/non-empty attributes. ((__vector_size__(16),,,,))
142 if (TryConsumeToken(tok::comma))
Steve Naroff0f2fe172007-06-01 17:11:19 +0000143 continue;
Alp Toker094e5212014-01-05 03:27:11 +0000144
145 // Expect an identifier or declaration specifier (const, int, etc.)
146 if (Tok.isNot(tok::identifier) && !isDeclarationSpecifier())
147 break;
148
Steve Naroff0f2fe172007-06-01 17:11:19 +0000149 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
150 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000151
Alp Toker094e5212014-01-05 03:27:11 +0000152 if (Tok.isNot(tok::l_paren)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000153 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Aaron Ballman00e99962013-08-31 01:11:41 +0000154 AttributeList::AS_GNU);
Alp Toker094e5212014-01-05 03:27:11 +0000155 continue;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000156 }
Alp Toker094e5212014-01-05 03:27:11 +0000157
158 // Handle "parameterized" attributes
159 if (!LateAttrs || !isAttributeLateParsed(*AttrName)) {
Craig Topper161e4db2014-05-21 06:02:52 +0000160 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc, nullptr,
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000161 SourceLocation(), AttributeList::AS_GNU, D);
Alp Toker094e5212014-01-05 03:27:11 +0000162 continue;
163 }
164
165 // Handle attributes with arguments that require late parsing.
166 LateParsedAttribute *LA =
167 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
168 LateAttrs->push_back(LA);
169
170 // Attributes in a class are parsed at the end of the class, along
171 // with other late-parsed declarations.
172 if (!ClassStack.empty() && !LateAttrs->parseSoon())
173 getCurrentClass().LateParsedDeclarations.push_back(LA);
174
175 // consume everything up to and including the matching right parens
176 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
177
178 Token Eof;
179 Eof.startToken();
180 Eof.setLocation(Tok.getLocation());
181 LA->Toks.push_back(Eof);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000182 }
Alp Toker094e5212014-01-05 03:27:11 +0000183
Alp Toker383d2c42014-01-01 03:08:43 +0000184 if (ExpectAndConsume(tok::r_paren))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000185 SkipUntil(tok::r_paren, StopAtSemi);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000186 SourceLocation Loc = Tok.getLocation();
Alp Toker383d2c42014-01-01 03:08:43 +0000187 if (ExpectAndConsume(tok::r_paren))
Alexey Bataevee6507d2013-11-18 08:17:37 +0000188 SkipUntil(tok::r_paren, StopAtSemi);
John McCall53fa7142010-12-24 02:08:15 +0000189 if (endLoc)
190 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000191 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000192}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000193
Aaron Ballman4768b312013-11-04 12:55:56 +0000194/// \brief Normalizes an attribute name by dropping prefixed and suffixed __.
195static StringRef normalizeAttrName(StringRef Name) {
Richard Smith66e71682013-10-24 01:07:54 +0000196 if (Name.size() >= 4 && Name.startswith("__") && Name.endswith("__"))
197 Name = Name.drop_front(2).drop_back(2);
Aaron Ballman4768b312013-11-04 12:55:56 +0000198 return Name;
199}
200
201/// \brief Determine whether the given attribute has an identifier argument.
202static bool attributeHasIdentifierArg(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +0000203#define CLANG_ATTR_IDENTIFIER_ARG_LIST
Aaron Ballman4768b312013-11-04 12:55:56 +0000204 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Aaron Ballman35db2b32014-01-29 22:13:45 +0000205#include "clang/Parse/AttrParserStringSwitches.inc"
Douglas Gregord2472d42013-05-02 23:25:32 +0000206 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +0000207#undef CLANG_ATTR_IDENTIFIER_ARG_LIST
Douglas Gregord2472d42013-05-02 23:25:32 +0000208}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000209
Aaron Ballman4768b312013-11-04 12:55:56 +0000210/// \brief Determine whether the given attribute parses a type argument.
211static bool attributeIsTypeArgAttr(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +0000212#define CLANG_ATTR_TYPE_ARG_LIST
Aaron Ballman4768b312013-11-04 12:55:56 +0000213 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Aaron Ballman35db2b32014-01-29 22:13:45 +0000214#include "clang/Parse/AttrParserStringSwitches.inc"
Aaron Ballman4768b312013-11-04 12:55:56 +0000215 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +0000216#undef CLANG_ATTR_TYPE_ARG_LIST
Aaron Ballman4768b312013-11-04 12:55:56 +0000217}
218
Aaron Ballman15b27b92014-01-09 19:39:35 +0000219/// \brief Determine whether the given attribute requires parsing its arguments
220/// in an unevaluated context or not.
221static bool attributeParsedArgsUnevaluated(const IdentifierInfo &II) {
Aaron Ballman35db2b32014-01-29 22:13:45 +0000222#define CLANG_ATTR_ARG_CONTEXT_LIST
Aaron Ballman15b27b92014-01-09 19:39:35 +0000223 return llvm::StringSwitch<bool>(normalizeAttrName(II.getName()))
Aaron Ballman35db2b32014-01-29 22:13:45 +0000224#include "clang/Parse/AttrParserStringSwitches.inc"
Aaron Ballman15b27b92014-01-09 19:39:35 +0000225 .Default(false);
Aaron Ballman35db2b32014-01-29 22:13:45 +0000226#undef CLANG_ATTR_ARG_CONTEXT_LIST
Aaron Ballman15b27b92014-01-09 19:39:35 +0000227}
228
Richard Smithfeefaf52013-09-03 18:01:40 +0000229IdentifierLoc *Parser::ParseIdentifierLoc() {
230 assert(Tok.is(tok::identifier) && "expected an identifier");
231 IdentifierLoc *IL = IdentifierLoc::create(Actions.Context,
232 Tok.getLocation(),
233 Tok.getIdentifierInfo());
234 ConsumeToken();
235 return IL;
236}
237
Richard Smithb1f9a282013-10-31 01:56:18 +0000238void Parser::ParseAttributeWithTypeArg(IdentifierInfo &AttrName,
239 SourceLocation AttrNameLoc,
240 ParsedAttributes &Attrs,
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000241 SourceLocation *EndLoc,
242 IdentifierInfo *ScopeName,
243 SourceLocation ScopeLoc,
244 AttributeList::Syntax Syntax) {
Richard Smithb1f9a282013-10-31 01:56:18 +0000245 BalancedDelimiterTracker Parens(*this, tok::l_paren);
246 Parens.consumeOpen();
247
248 TypeResult T;
249 if (Tok.isNot(tok::r_paren))
250 T = ParseTypeName();
251
252 if (Parens.consumeClose())
253 return;
254
255 if (T.isInvalid())
256 return;
257
258 if (T.isUsable())
259 Attrs.addNewTypeAttr(&AttrName,
Craig Topper161e4db2014-05-21 06:02:52 +0000260 SourceRange(AttrNameLoc, Parens.getCloseLocation()),
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000261 ScopeName, ScopeLoc, T.get(), Syntax);
Richard Smithb1f9a282013-10-31 01:56:18 +0000262 else
263 Attrs.addNew(&AttrName, SourceRange(AttrNameLoc, Parens.getCloseLocation()),
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000264 ScopeName, ScopeLoc, nullptr, 0, Syntax);
Richard Smithb1f9a282013-10-31 01:56:18 +0000265}
266
Aaron Ballman35f94212014-04-14 16:03:22 +0000267unsigned Parser::ParseAttributeArgsCommon(
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000268 IdentifierInfo *AttrName, SourceLocation AttrNameLoc,
269 ParsedAttributes &Attrs, SourceLocation *EndLoc, IdentifierInfo *ScopeName,
270 SourceLocation ScopeLoc, AttributeList::Syntax Syntax) {
271 // Ignore the left paren location for now.
272 ConsumeParen();
273
274 ArgsVector ArgExprs;
275 if (Tok.is(tok::identifier)) {
276 // If this attribute wants an 'identifier' argument, make it so.
277 bool IsIdentifierArg = attributeHasIdentifierArg(*AttrName);
278 AttributeList::Kind AttrKind =
279 AttributeList::getKind(AttrName, ScopeName, Syntax);
280
281 // If we don't know how to parse this attribute, but this is the only
282 // token in this argument, assume it's meant to be an identifier.
283 if (AttrKind == AttributeList::UnknownAttribute ||
284 AttrKind == AttributeList::IgnoredAttribute) {
285 const Token &Next = NextToken();
286 IsIdentifierArg = Next.is(tok::r_paren) || Next.is(tok::comma);
287 }
288
289 if (IsIdentifierArg)
290 ArgExprs.push_back(ParseIdentifierLoc());
291 }
292
293 if (!ArgExprs.empty() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren)) {
294 // Eat the comma.
295 if (!ArgExprs.empty())
296 ConsumeToken();
297
298 // Parse the non-empty comma-separated list of expressions.
299 do {
300 std::unique_ptr<EnterExpressionEvaluationContext> Unevaluated;
301 if (attributeParsedArgsUnevaluated(*AttrName))
302 Unevaluated.reset(
303 new EnterExpressionEvaluationContext(Actions, Sema::Unevaluated));
304
305 ExprResult ArgExpr(ParseAssignmentExpression());
306 if (ArgExpr.isInvalid()) {
307 SkipUntil(tok::r_paren, StopAtSemi);
Aaron Ballman35f94212014-04-14 16:03:22 +0000308 return 0;
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000309 }
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000310 ArgExprs.push_back(ArgExpr.get());
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000311 // Eat the comma, move to the next argument
312 } while (TryConsumeToken(tok::comma));
313 }
314
315 SourceLocation RParen = Tok.getLocation();
316 if (!ExpectAndConsume(tok::r_paren)) {
317 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
318 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen), ScopeName, ScopeLoc,
319 ArgExprs.data(), ArgExprs.size(), Syntax);
320 }
321
322 if (EndLoc)
323 *EndLoc = RParen;
Aaron Ballman35f94212014-04-14 16:03:22 +0000324
325 return static_cast<unsigned>(ArgExprs.size());
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000326}
327
Michael Han23214e52012-10-03 01:56:22 +0000328/// Parse the arguments to a parameterized GNU attribute or
329/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000330void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
331 SourceLocation AttrNameLoc,
332 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000333 SourceLocation *EndLoc,
334 IdentifierInfo *ScopeName,
335 SourceLocation ScopeLoc,
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000336 AttributeList::Syntax Syntax,
337 Declarator *D) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000338
339 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
340
Richard Smith66e71682013-10-24 01:07:54 +0000341 AttributeList::Kind AttrKind =
Richard Smithb1f9a282013-10-31 01:56:18 +0000342 AttributeList::getKind(AttrName, ScopeName, Syntax);
Richard Smith66e71682013-10-24 01:07:54 +0000343
Richard Smith66e71682013-10-24 01:07:54 +0000344 if (AttrKind == AttributeList::AT_Availability) {
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000345 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
346 ScopeLoc, Syntax);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000347 return;
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000348 } else if (AttrKind == AttributeList::AT_ObjCBridgeRelated) {
349 ParseObjCBridgeRelatedAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
350 ScopeName, ScopeLoc, Syntax);
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000351 return;
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000352 } else if (AttrKind == AttributeList::AT_TypeTagForDatatype) {
353 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc,
354 ScopeName, ScopeLoc, Syntax);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000355 return;
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000356 } else if (attributeIsTypeArgAttr(*AttrName)) {
357 ParseAttributeWithTypeArg(*AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
358 ScopeLoc, Syntax);
Richard Smithb1f9a282013-10-31 01:56:18 +0000359 return;
360 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000361
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000362 // These may refer to the function arguments, but need to be parsed early to
363 // participate in determining whether it's a redeclaration.
Ahmed Charlesb8984322014-03-07 20:03:18 +0000364 std::unique_ptr<ParseScope> PrototypeScope;
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000365 if (AttrName->isStr("enable_if") && D && D->isFunctionDeclarator()) {
366 DeclaratorChunk::FunctionTypeInfo FTI = D->getFunctionTypeInfo();
367 PrototypeScope.reset(new ParseScope(this, Scope::FunctionPrototypeScope |
368 Scope::FunctionDeclarationScope |
369 Scope::DeclScope));
Alp Tokerc5350722014-02-26 22:27:52 +0000370 for (unsigned i = 0; i != FTI.NumParams; ++i) {
371 ParmVarDecl *Param = cast<ParmVarDecl>(FTI.Params[i].Param);
Nick Lewycky35a6ef42014-01-11 02:50:57 +0000372 Actions.ActOnReenterCXXMethodParameter(getCurScope(), Param);
373 }
374 }
375
Aaron Ballmanb8e20392014-03-31 17:32:39 +0000376 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, EndLoc, ScopeName,
377 ScopeLoc, Syntax);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000378}
379
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000380bool Parser::ParseMicrosoftDeclSpecArgs(IdentifierInfo *AttrName,
381 SourceLocation AttrNameLoc,
382 ParsedAttributes &Attrs) {
383 // If the attribute isn't known, we will not attempt to parse any
384 // arguments.
385 if (!hasAttribute(AttrSyntax::Declspec, nullptr, AttrName,
386 getTargetInfo().getTriple(), getLangOpts())) {
387 // Eat the left paren, then skip to the ending right paren.
388 ConsumeParen();
389 SkipUntil(tok::r_paren);
390 return false;
Aaron Ballman478faed2012-06-19 22:09:27 +0000391 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000392
Aaron Ballman95d57032014-04-14 16:44:26 +0000393 SourceLocation OpenParenLoc = Tok.getLocation();
394
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000395 if (AttrName->getName() == "property") {
Aaron Ballman478faed2012-06-19 22:09:27 +0000396 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000397 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000398 // must be named get or put.
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000399
John McCall5e77d762013-04-16 07:28:30 +0000400 BalancedDelimiterTracker T(*this, tok::l_paren);
401 T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000402 AttrName->getNameStart(), tok::r_paren);
John McCall5e77d762013-04-16 07:28:30 +0000403
404 enum AccessorKind {
405 AK_Invalid = -1,
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000406 AK_Put = 0,
407 AK_Get = 1 // indices into AccessorNames
John McCall5e77d762013-04-16 07:28:30 +0000408 };
Craig Topper161e4db2014-05-21 06:02:52 +0000409 IdentifierInfo *AccessorNames[] = {nullptr, nullptr};
John McCall5e77d762013-04-16 07:28:30 +0000410 bool HasInvalidAccessor = false;
411
412 // Parse the accessor specifications.
413 while (true) {
414 // Stop if this doesn't look like an accessor spec.
415 if (!Tok.is(tok::identifier)) {
416 // If the user wrote a completely empty list, use a special diagnostic.
417 if (Tok.is(tok::r_paren) && !HasInvalidAccessor &&
Craig Topper161e4db2014-05-21 06:02:52 +0000418 AccessorNames[AK_Put] == nullptr &&
419 AccessorNames[AK_Get] == nullptr) {
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000420 Diag(AttrNameLoc, diag::err_ms_property_no_getter_or_putter);
John McCall5e77d762013-04-16 07:28:30 +0000421 break;
422 }
423
424 Diag(Tok.getLocation(), diag::err_ms_property_unknown_accessor);
425 break;
426 }
427
428 AccessorKind Kind;
429 SourceLocation KindLoc = Tok.getLocation();
430 StringRef KindStr = Tok.getIdentifierInfo()->getName();
431 if (KindStr == "get") {
432 Kind = AK_Get;
433 } else if (KindStr == "put") {
434 Kind = AK_Put;
435
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000436 // Recover from the common mistake of using 'set' instead of 'put'.
John McCall5e77d762013-04-16 07:28:30 +0000437 } else if (KindStr == "set") {
438 Diag(KindLoc, diag::err_ms_property_has_set_accessor)
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000439 << FixItHint::CreateReplacement(KindLoc, "put");
John McCall5e77d762013-04-16 07:28:30 +0000440 Kind = AK_Put;
441
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000442 // Handle the mistake of forgetting the accessor kind by skipping
443 // this accessor.
John McCall5e77d762013-04-16 07:28:30 +0000444 } else if (NextToken().is(tok::comma) || NextToken().is(tok::r_paren)) {
445 Diag(KindLoc, diag::err_ms_property_missing_accessor_kind);
446 ConsumeToken();
447 HasInvalidAccessor = true;
448 goto next_property_accessor;
449
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000450 // Otherwise, complain about the unknown accessor kind.
John McCall5e77d762013-04-16 07:28:30 +0000451 } else {
452 Diag(KindLoc, diag::err_ms_property_unknown_accessor);
453 HasInvalidAccessor = true;
454 Kind = AK_Invalid;
455
456 // Try to keep parsing unless it doesn't look like an accessor spec.
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000457 if (!NextToken().is(tok::equal))
458 break;
John McCall5e77d762013-04-16 07:28:30 +0000459 }
460
461 // Consume the identifier.
462 ConsumeToken();
463
464 // Consume the '='.
Alp Toker8fbec672013-12-17 23:29:36 +0000465 if (!TryConsumeToken(tok::equal)) {
John McCall5e77d762013-04-16 07:28:30 +0000466 Diag(Tok.getLocation(), diag::err_ms_property_expected_equal)
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000467 << KindStr;
John McCall5e77d762013-04-16 07:28:30 +0000468 break;
469 }
470
471 // Expect the method name.
472 if (!Tok.is(tok::identifier)) {
473 Diag(Tok.getLocation(), diag::err_ms_property_expected_accessor_name);
474 break;
475 }
476
477 if (Kind == AK_Invalid) {
478 // Just drop invalid accessors.
Craig Topper161e4db2014-05-21 06:02:52 +0000479 } else if (AccessorNames[Kind] != nullptr) {
John McCall5e77d762013-04-16 07:28:30 +0000480 // Complain about the repeated accessor, ignore it, and keep parsing.
481 Diag(KindLoc, diag::err_ms_property_duplicate_accessor) << KindStr;
482 } else {
483 AccessorNames[Kind] = Tok.getIdentifierInfo();
484 }
485 ConsumeToken();
486
487 next_property_accessor:
488 // Keep processing accessors until we run out.
Alp Toker094e5212014-01-05 03:27:11 +0000489 if (TryConsumeToken(tok::comma))
John McCall5e77d762013-04-16 07:28:30 +0000490 continue;
491
492 // If we run into the ')', stop without consuming it.
Alp Toker094e5212014-01-05 03:27:11 +0000493 if (Tok.is(tok::r_paren))
John McCall5e77d762013-04-16 07:28:30 +0000494 break;
Alp Toker094e5212014-01-05 03:27:11 +0000495
496 Diag(Tok.getLocation(), diag::err_ms_property_expected_comma_or_rparen);
497 break;
John McCall5e77d762013-04-16 07:28:30 +0000498 }
499
500 // Only add the property attribute if it was well-formed.
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000501 if (!HasInvalidAccessor)
Craig Topper161e4db2014-05-21 06:02:52 +0000502 Attrs.addNewPropertyAttr(AttrName, AttrNameLoc, nullptr, SourceLocation(),
John McCall5e77d762013-04-16 07:28:30 +0000503 AccessorNames[AK_Get], AccessorNames[AK_Put],
504 AttributeList::AS_Declspec);
Aaron Ballman478faed2012-06-19 22:09:27 +0000505 T.skipToEnd();
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000506 return !HasInvalidAccessor;
Aaron Ballman478faed2012-06-19 22:09:27 +0000507 }
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000508
Aaron Ballman95d57032014-04-14 16:44:26 +0000509 unsigned NumArgs =
510 ParseAttributeArgsCommon(AttrName, AttrNameLoc, Attrs, nullptr, nullptr,
511 SourceLocation(), AttributeList::AS_Declspec);
512
513 // If this attribute's args were parsed, and it was expected to have
514 // arguments but none were provided, emit a diagnostic.
515 const AttributeList *Attr = Attrs.getList();
516 if (Attr && Attr->getMaxArgs() && !NumArgs) {
Aaron Ballmanef5d94c2014-04-15 00:36:39 +0000517 Diag(OpenParenLoc, diag::err_attribute_requires_arguments) << AttrName;
Aaron Ballman95d57032014-04-14 16:44:26 +0000518 return false;
519 }
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000520 return true;
Aaron Ballman478faed2012-06-19 22:09:27 +0000521}
522
Eli Friedman06de2b52009-06-08 07:21:15 +0000523/// [MS] decl-specifier:
524/// __declspec ( extended-decl-modifier-seq )
525///
526/// [MS] extended-decl-modifier-seq:
527/// extended-decl-modifier[opt]
528/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000529void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000530 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000531
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000532 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000533 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000534 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000535 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000536 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000537
Chad Rosierc1183952012-06-26 22:30:43 +0000538 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000539 // you can specify multiple attributes per declspec.
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000540 while (Tok.isNot(tok::r_paren)) {
541 // Attribute not present.
542 if (TryConsumeToken(tok::comma))
543 continue;
544
Aaron Ballman478faed2012-06-19 22:09:27 +0000545 // We expect either a well-known identifier or a generic string. Anything
546 // else is a malformed declspec.
547 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000548 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000549 Tok.getKind() != tok::kw_restrict) {
550 Diag(Tok, diag::err_ms_declspec_type);
551 T.skipToEnd();
552 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000553 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000554
555 IdentifierInfo *AttrName;
556 SourceLocation AttrNameLoc;
557 if (IsString) {
558 SmallString<8> StrBuffer;
559 bool Invalid = false;
560 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
561 if (Invalid) {
562 T.skipToEnd();
563 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000564 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000565 AttrName = PP.getIdentifierInfo(Str);
566 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000567 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000568 AttrName = Tok.getIdentifierInfo();
569 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000570 }
Chad Rosierc1183952012-06-26 22:30:43 +0000571
Aaron Ballmanfdd783a2014-03-31 18:18:43 +0000572 bool AttrHandled = false;
573
574 // Parse attribute arguments.
575 if (Tok.is(tok::l_paren))
576 AttrHandled = ParseMicrosoftDeclSpecArgs(AttrName, AttrNameLoc, Attrs);
577 else if (AttrName->getName() == "property")
578 // The property attribute must have an argument list.
579 Diag(Tok.getLocation(), diag::err_expected_lparen_after)
580 << AttrName->getName();
581
582 if (!AttrHandled)
Craig Topper161e4db2014-05-21 06:02:52 +0000583 Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Aaron Ballman00e99962013-08-31 01:11:41 +0000584 AttributeList::AS_Declspec);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000585 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000586 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000587}
588
John McCall53fa7142010-12-24 02:08:15 +0000589void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000590 // Treat these like attributes
Reid Klecknerd7857f02014-10-24 17:42:17 +0000591 while (true) {
592 switch (Tok.getKind()) {
593 case tok::kw___fastcall:
594 case tok::kw___stdcall:
595 case tok::kw___thiscall:
596 case tok::kw___cdecl:
597 case tok::kw___vectorcall:
598 case tok::kw___ptr64:
599 case tok::kw___w64:
600 case tok::kw___ptr32:
601 case tok::kw___unaligned:
602 case tok::kw___sptr:
603 case tok::kw___uptr: {
604 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
605 SourceLocation AttrNameLoc = ConsumeToken();
606 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
607 AttributeList::AS_Keyword);
608 break;
609 }
610 default:
611 return;
612 }
Eli Friedman53339e02009-06-08 23:27:34 +0000613 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000614}
615
John McCall53fa7142010-12-24 02:08:15 +0000616void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000617 // Treat these like attributes
618 while (Tok.is(tok::kw___pascal)) {
619 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
620 SourceLocation AttrNameLoc = ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +0000621 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Aaron Ballman00e99962013-08-31 01:11:41 +0000622 AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000623 }
John McCall53fa7142010-12-24 02:08:15 +0000624}
625
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000626void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
627 // Treat these like attributes
628 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000629 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000630 SourceLocation AttrNameLoc = ConsumeToken();
Craig Topper161e4db2014-05-21 06:02:52 +0000631 attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Aaron Ballman00e99962013-08-31 01:11:41 +0000632 AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000633 }
634}
635
Aaron Ballman05d76ea2014-01-14 01:29:54 +0000636void Parser::ParseOpenCLQualifiers(ParsedAttributes &Attrs) {
637 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
638 SourceLocation AttrNameLoc = Tok.getLocation();
Craig Topper161e4db2014-05-21 06:02:52 +0000639 Attrs.addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc, nullptr, 0,
Aaron Ballman26891332014-01-14 17:41:53 +0000640 AttributeList::AS_Keyword);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000641}
642
Fariborz Jahaniandbc956d2014-10-02 16:39:45 +0000643static bool VersionNumberSeparator(const char Separator) {
644 return (Separator == '.' || Separator == '_');
645}
646
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000647/// \brief Parse a version number.
648///
649/// version:
650/// simple-integer
651/// simple-integer ',' simple-integer
652/// simple-integer ',' simple-integer ',' simple-integer
653VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
654 Range = Tok.getLocation();
655
656 if (!Tok.is(tok::numeric_constant)) {
657 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000658 SkipUntil(tok::comma, tok::r_paren,
659 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000660 return VersionTuple();
661 }
662
663 // Parse the major (and possibly minor and subminor) versions, which
664 // are stored in the numeric constant. We utilize a quirk of the
665 // lexer, which is that it handles something like 1.2.3 as a single
666 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000667 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000668 Buffer.resize(Tok.getLength()+1);
669 const char *ThisTokBegin = &Buffer[0];
670
671 // Get the spelling of the token, which eliminates trigraphs, etc.
672 bool Invalid = false;
673 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
674 if (Invalid)
675 return VersionTuple();
676
677 // Parse the major version.
678 unsigned AfterMajor = 0;
679 unsigned Major = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000680 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000681 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
682 ++AfterMajor;
683 }
684
685 if (AfterMajor == 0) {
686 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000687 SkipUntil(tok::comma, tok::r_paren,
688 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000689 return VersionTuple();
690 }
691
692 if (AfterMajor == ActualLength) {
693 ConsumeToken();
694
695 // We only had a single version component.
696 if (Major == 0) {
697 Diag(Tok, diag::err_zero_version);
698 return VersionTuple();
699 }
700
701 return VersionTuple(Major);
702 }
703
Fariborz Jahaniance72e632014-10-02 17:57:26 +0000704 const char AfterMajorSeparator = ThisTokBegin[AfterMajor];
705 if (!VersionNumberSeparator(AfterMajorSeparator)
Fariborz Jahaniandbc956d2014-10-02 16:39:45 +0000706 || (AfterMajor + 1 == ActualLength)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000707 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000708 SkipUntil(tok::comma, tok::r_paren,
709 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000710 return VersionTuple();
711 }
712
713 // Parse the minor version.
714 unsigned AfterMinor = AfterMajor + 1;
715 unsigned Minor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000716 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000717 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
718 ++AfterMinor;
719 }
720
721 if (AfterMinor == ActualLength) {
722 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000723
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000724 // We had major.minor.
725 if (Major == 0 && Minor == 0) {
726 Diag(Tok, diag::err_zero_version);
727 return VersionTuple();
728 }
729
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000730 return VersionTuple(Major, Minor, (AfterMajorSeparator == '_'));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000731 }
732
Fariborz Jahaniance72e632014-10-02 17:57:26 +0000733 const char AfterMinorSeparator = ThisTokBegin[AfterMinor];
Fariborz Jahaniandbc956d2014-10-02 16:39:45 +0000734 // If what follows is not a '.' or '_', we have a problem.
Fariborz Jahaniance72e632014-10-02 17:57:26 +0000735 if (!VersionNumberSeparator(AfterMinorSeparator)) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000736 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000737 SkipUntil(tok::comma, tok::r_paren,
738 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Chad Rosierc1183952012-06-26 22:30:43 +0000739 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000740 }
Fariborz Jahaniance72e632014-10-02 17:57:26 +0000741
Fariborz Jahanianb6161612014-10-03 17:21:12 +0000742 // Warn if separators, be it '.' or '_', do not match.
Fariborz Jahaniance72e632014-10-02 17:57:26 +0000743 if (AfterMajorSeparator != AfterMinorSeparator)
744 Diag(Tok, diag::warn_expected_consistent_version_separator);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000745
746 // Parse the subminor version.
747 unsigned AfterSubminor = AfterMinor + 1;
748 unsigned Subminor = 0;
Jordan Rosea7d03842013-02-08 22:30:41 +0000749 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000750 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
751 ++AfterSubminor;
752 }
753
754 if (AfterSubminor != ActualLength) {
755 Diag(Tok, diag::err_expected_version);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000756 SkipUntil(tok::comma, tok::r_paren,
757 StopAtSemi | StopBeforeMatch | StopAtCodeCompletion);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000758 return VersionTuple();
759 }
760 ConsumeToken();
Fariborz Jahanian2618dba2014-10-06 16:46:02 +0000761 return VersionTuple(Major, Minor, Subminor, (AfterMajorSeparator == '_'));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000762}
763
764/// \brief Parse the contents of the "availability" attribute.
765///
766/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000767/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000768///
769/// platform:
770/// identifier
771///
772/// version-arg-list:
773/// version-arg
774/// version-arg ',' version-arg-list
775///
776/// version-arg:
777/// 'introduced' '=' version
778/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000779/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000780/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000781/// opt-message:
782/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000783void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
784 SourceLocation AvailabilityLoc,
785 ParsedAttributes &attrs,
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000786 SourceLocation *endLoc,
787 IdentifierInfo *ScopeName,
788 SourceLocation ScopeLoc,
789 AttributeList::Syntax Syntax) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000790 enum { Introduced, Deprecated, Obsoleted, Unknown };
791 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000792 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000793
794 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000795 BalancedDelimiterTracker T(*this, tok::l_paren);
796 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000797 Diag(Tok, diag::err_expected) << tok::l_paren;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000798 return;
799 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000800
801 // Parse the platform name,
802 if (Tok.isNot(tok::identifier)) {
803 Diag(Tok, diag::err_availability_expected_platform);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000804 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000805 return;
806 }
Richard Smithfeefaf52013-09-03 18:01:40 +0000807 IdentifierLoc *Platform = ParseIdentifierLoc();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000808
809 // Parse the ',' following the platform name.
Alp Toker383d2c42014-01-01 03:08:43 +0000810 if (ExpectAndConsume(tok::comma)) {
811 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000812 return;
Alp Toker383d2c42014-01-01 03:08:43 +0000813 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000814
815 // If we haven't grabbed the pointers for the identifiers
816 // "introduced", "deprecated", and "obsoleted", do so now.
817 if (!Ident_introduced) {
818 Ident_introduced = PP.getIdentifierInfo("introduced");
819 Ident_deprecated = PP.getIdentifierInfo("deprecated");
820 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000821 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000822 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000823 }
824
825 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000826 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000827 do {
828 if (Tok.isNot(tok::identifier)) {
829 Diag(Tok, diag::err_availability_expected_change);
Alexey Bataevee6507d2013-11-18 08:17:37 +0000830 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000831 return;
832 }
833 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
834 SourceLocation KeywordLoc = ConsumeToken();
835
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000836 if (Keyword == Ident_unavailable) {
837 if (UnavailableLoc.isValid()) {
838 Diag(KeywordLoc, diag::err_availability_redundant)
839 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000840 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000841 UnavailableLoc = KeywordLoc;
Alp Toker97650562014-01-10 11:19:30 +0000842 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000843 }
844
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000845 if (Tok.isNot(tok::equal)) {
Alp Tokerec543272013-12-24 09:48:30 +0000846 Diag(Tok, diag::err_expected_after) << Keyword << tok::equal;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000847 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000848 return;
849 }
850 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000851 if (Keyword == Ident_message) {
David Majnemer2e498302014-07-18 05:43:12 +0000852 if (Tok.isNot(tok::string_literal)) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000853 Diag(Tok, diag::err_expected_string_literal)
854 << /*Source='availability attribute'*/2;
Alexey Bataevee6507d2013-11-18 08:17:37 +0000855 SkipUntil(tok::r_paren, StopAtSemi);
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000856 return;
857 }
858 MessageExpr = ParseStringLiteralExpression();
David Majnemer2e498302014-07-18 05:43:12 +0000859 // Also reject wide string literals.
860 if (StringLiteral *MessageStringLiteral =
861 cast_or_null<StringLiteral>(MessageExpr.get())) {
862 if (MessageStringLiteral->getCharByteWidth() != 1) {
863 Diag(MessageStringLiteral->getSourceRange().getBegin(),
864 diag::err_expected_string_literal)
865 << /*Source='availability attribute'*/ 2;
866 SkipUntil(tok::r_paren, StopAtSemi);
867 return;
868 }
869 }
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000870 break;
871 }
Chad Rosierc1183952012-06-26 22:30:43 +0000872
Fariborz Jahanian5a29e6a2014-11-05 23:58:55 +0000873 // Special handling of 'NA' only when applied to introduced or
874 // deprecated.
875 if ((Keyword == Ident_introduced || Keyword == Ident_deprecated) &&
876 Tok.is(tok::identifier)) {
877 IdentifierInfo *NA = Tok.getIdentifierInfo();
878 if (NA->getName() == "NA") {
879 ConsumeToken();
880 if (Keyword == Ident_introduced)
881 UnavailableLoc = KeywordLoc;
882 continue;
883 }
884 }
885
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000886 SourceRange VersionRange;
887 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000888
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000889 if (Version.empty()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +0000890 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000891 return;
892 }
893
894 unsigned Index;
895 if (Keyword == Ident_introduced)
896 Index = Introduced;
897 else if (Keyword == Ident_deprecated)
898 Index = Deprecated;
899 else if (Keyword == Ident_obsoleted)
900 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000901 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000902 Index = Unknown;
903
904 if (Index < Unknown) {
905 if (!Changes[Index].KeywordLoc.isInvalid()) {
906 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000907 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000908 << SourceRange(Changes[Index].KeywordLoc,
909 Changes[Index].VersionRange.getEnd());
910 }
911
912 Changes[Index].KeywordLoc = KeywordLoc;
913 Changes[Index].Version = Version;
914 Changes[Index].VersionRange = VersionRange;
915 } else {
916 Diag(KeywordLoc, diag::err_availability_unknown_change)
917 << Keyword << VersionRange;
918 }
919
Alp Toker97650562014-01-10 11:19:30 +0000920 } while (TryConsumeToken(tok::comma));
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000921
922 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000923 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000924 return;
925
926 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000927 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000928
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000929 // The 'unavailable' availability cannot be combined with any other
930 // availability changes. Make sure that hasn't happened.
931 if (UnavailableLoc.isValid()) {
932 bool Complained = false;
933 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
934 if (Changes[Index].KeywordLoc.isValid()) {
935 if (!Complained) {
936 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
937 << SourceRange(Changes[Index].KeywordLoc,
938 Changes[Index].VersionRange.getEnd());
939 Complained = true;
940 }
941
942 // Clear out the availability.
943 Changes[Index] = AvailabilityChange();
944 }
945 }
946 }
947
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000948 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000949 attrs.addNew(&Availability,
950 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000951 ScopeName, ScopeLoc,
Aaron Ballman00e99962013-08-31 01:11:41 +0000952 Platform,
John McCall084e83d2011-03-24 11:26:52 +0000953 Changes[Introduced],
954 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000955 Changes[Obsoleted],
Nikola Smiljanic01a75982014-05-29 10:55:11 +0000956 UnavailableLoc, MessageExpr.get(),
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000957 Syntax);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000958}
959
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000960/// \brief Parse the contents of the "objc_bridge_related" attribute.
961/// objc_bridge_related '(' related_class ',' opt-class_method ',' opt-instance_method ')'
962/// related_class:
963/// Identifier
964///
965/// opt-class_method:
966/// Identifier: | <empty>
967///
968/// opt-instance_method:
969/// Identifier | <empty>
970///
971void Parser::ParseObjCBridgeRelatedAttribute(IdentifierInfo &ObjCBridgeRelated,
972 SourceLocation ObjCBridgeRelatedLoc,
973 ParsedAttributes &attrs,
Aaron Ballman80f1529c2014-07-16 20:21:50 +0000974 SourceLocation *endLoc,
975 IdentifierInfo *ScopeName,
976 SourceLocation ScopeLoc,
977 AttributeList::Syntax Syntax) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000978 // Opening '('.
979 BalancedDelimiterTracker T(*this, tok::l_paren);
980 if (T.consumeOpen()) {
Alp Tokerec543272013-12-24 09:48:30 +0000981 Diag(Tok, diag::err_expected) << tok::l_paren;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000982 return;
983 }
984
985 // Parse the related class name.
986 if (Tok.isNot(tok::identifier)) {
987 Diag(Tok, diag::err_objcbridge_related_expected_related_class);
988 SkipUntil(tok::r_paren, StopAtSemi);
989 return;
990 }
991 IdentifierLoc *RelatedClass = ParseIdentifierLoc();
Alp Toker97650562014-01-10 11:19:30 +0000992 if (ExpectAndConsume(tok::comma)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000993 SkipUntil(tok::r_paren, StopAtSemi);
994 return;
995 }
Alp Toker8fbec672013-12-17 23:29:36 +0000996
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000997 // Parse optional class method name.
Craig Topper161e4db2014-05-21 06:02:52 +0000998 IdentifierLoc *ClassMethod = nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +0000999 if (Tok.is(tok::identifier)) {
1000 ClassMethod = ParseIdentifierLoc();
Alp Toker8fbec672013-12-17 23:29:36 +00001001 if (!TryConsumeToken(tok::colon)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001002 Diag(Tok, diag::err_objcbridge_related_selector_name);
1003 SkipUntil(tok::r_paren, StopAtSemi);
1004 return;
1005 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001006 }
Alp Toker8fbec672013-12-17 23:29:36 +00001007 if (!TryConsumeToken(tok::comma)) {
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001008 if (Tok.is(tok::colon))
1009 Diag(Tok, diag::err_objcbridge_related_selector_name);
1010 else
Alp Tokerec543272013-12-24 09:48:30 +00001011 Diag(Tok, diag::err_expected) << tok::comma;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001012 SkipUntil(tok::r_paren, StopAtSemi);
1013 return;
1014 }
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001015
1016 // Parse optional instance method name.
Craig Topper161e4db2014-05-21 06:02:52 +00001017 IdentifierLoc *InstanceMethod = nullptr;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001018 if (Tok.is(tok::identifier))
1019 InstanceMethod = ParseIdentifierLoc();
1020 else if (Tok.isNot(tok::r_paren)) {
Alp Tokerec543272013-12-24 09:48:30 +00001021 Diag(Tok, diag::err_expected) << tok::r_paren;
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001022 SkipUntil(tok::r_paren, StopAtSemi);
1023 return;
1024 }
1025
1026 // Closing ')'.
1027 if (T.consumeClose())
1028 return;
1029
1030 if (endLoc)
1031 *endLoc = T.getCloseLocation();
1032
1033 // Record this attribute
1034 attrs.addNew(&ObjCBridgeRelated,
1035 SourceRange(ObjCBridgeRelatedLoc, T.getCloseLocation()),
Aaron Ballman80f1529c2014-07-16 20:21:50 +00001036 ScopeName, ScopeLoc,
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001037 RelatedClass,
1038 ClassMethod,
1039 InstanceMethod,
Aaron Ballman80f1529c2014-07-16 20:21:50 +00001040 Syntax);
Fariborz Jahanian1a2519a2013-12-04 20:32:50 +00001041}
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001042
Bill Wendling44426052012-12-20 19:22:21 +00001043// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001044// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
1045
1046void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
1047
1048void Parser::LateParsedClass::ParseLexedAttributes() {
1049 Self->ParseLexedAttributes(*Class);
1050}
1051
1052void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001053 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001054}
1055
1056/// Wrapper class which calls ParseLexedAttribute, after setting up the
1057/// scope appropriately.
1058void Parser::ParseLexedAttributes(ParsingClass &Class) {
1059 // Deal with templates
1060 // FIXME: Test cases to make sure this does the right thing for templates.
1061 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
1062 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
1063 HasTemplateScope);
1064 if (HasTemplateScope)
1065 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
1066
Douglas Gregor3024f072012-04-16 07:05:22 +00001067 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001068 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +00001069 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001070 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
1071 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
1072
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001073 // Enter the scope of nested classes
1074 if (!AlreadyHasClassScope)
1075 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
1076 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +00001077 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +00001078 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
1079 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
1080 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001081 }
Chad Rosierc1183952012-06-26 22:30:43 +00001082
DeLesley Hutchins6f860042012-04-06 15:10:17 +00001083 if (!AlreadyHasClassScope)
1084 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
1085 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001086}
1087
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001088
1089/// \brief Parse all attributes in LAs, and attach them to Decl D.
1090void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
1091 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001092 assert(LAs.parseSoon() &&
1093 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001094 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +00001095 if (D)
1096 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001097 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +00001098 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001099 }
1100 LAs.clear();
1101}
1102
1103
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001104/// \brief Finish parsing an attribute for which parsing was delayed.
1105/// This will be called at the end of parsing a class declaration
1106/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +00001107/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001108/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001109void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
1110 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001111 // Save the current token position.
1112 SourceLocation OrigLoc = Tok.getLocation();
1113
1114 // Append the current token at the end of the new token stream so that it
1115 // doesn't get lost.
1116 LA.Toks.push_back(Tok);
1117 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
1118 // Consume the previously pushed token.
Argyrios Kyrtzidisc36633c2013-03-27 23:58:17 +00001119 ConsumeAnyToken(/*ConsumeCodeCompletionTok=*/true);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001120
1121 ParsedAttributes Attrs(AttrFactory);
1122 SourceLocation endLoc;
1123
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001124 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001125 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001126 NamedDecl *ND = dyn_cast<NamedDecl>(D);
1127 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001128
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001129 // Allow 'this' within late-parsed attributes.
Richard Smithc3d2ebb2013-06-07 02:33:37 +00001130 Sema::CXXThisScopeRAII ThisScope(Actions, RD, /*TypeQuals=*/0,
1131 ND && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001132
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001133 if (LA.Decls.size() == 1) {
1134 // If the Decl is templatized, add template parameters to scope.
1135 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
1136 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
1137 if (HasTemplateScope)
1138 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001139
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001140 // If the Decl is on a function, add function parameters to the scope.
1141 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
1142 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
1143 if (HasFunScope)
1144 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001145
Michael Han23214e52012-10-03 01:56:22 +00001146 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Craig Topper161e4db2014-05-21 06:02:52 +00001147 nullptr, SourceLocation(), AttributeList::AS_GNU,
1148 nullptr);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +00001149
1150 if (HasFunScope) {
1151 Actions.ActOnExitFunctionContext();
1152 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
1153 }
1154 if (HasTemplateScope) {
1155 TempScope.Exit();
1156 }
1157 } else {
1158 // If there are multiple decls, then the decl cannot be within the
1159 // function scope.
Michael Han23214e52012-10-03 01:56:22 +00001160 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Craig Topper161e4db2014-05-21 06:02:52 +00001161 nullptr, SourceLocation(), AttributeList::AS_GNU,
1162 nullptr);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001163 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +00001164 } else {
1165 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +00001166 }
1167
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00001168 const AttributeList *AL = Attrs.getList();
1169 if (OnDefinition && AL && !AL->isCXX11Attribute() &&
Aaron Ballmanc669cc02014-01-27 22:10:04 +00001170 AL->isKnownToGCC())
Aaron Ballman9a99e0d2014-01-20 17:18:35 +00001171 Diag(Tok, diag::warn_attribute_on_function_definition)
1172 << &LA.AttrName;
1173
1174 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i)
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00001175 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001176
1177 if (Tok.getLocation() != OrigLoc) {
1178 // Due to a parsing error, we either went over the cached tokens or
1179 // there are still cached tokens left, so we skip the leftover tokens.
1180 // Since this is an uncommon situation that should be avoided, use the
1181 // expensive isBeforeInTranslationUnit call.
1182 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
1183 OrigLoc))
1184 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +00001185 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001186 }
1187}
1188
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001189void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1190 SourceLocation AttrNameLoc,
1191 ParsedAttributes &Attrs,
Aaron Ballman80f1529c2014-07-16 20:21:50 +00001192 SourceLocation *EndLoc,
1193 IdentifierInfo *ScopeName,
1194 SourceLocation ScopeLoc,
1195 AttributeList::Syntax Syntax) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001196 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1197
1198 BalancedDelimiterTracker T(*this, tok::l_paren);
1199 T.consumeOpen();
1200
1201 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001202 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001203 T.skipToEnd();
1204 return;
1205 }
Richard Smithfeefaf52013-09-03 18:01:40 +00001206 IdentifierLoc *ArgumentKind = ParseIdentifierLoc();
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001207
Alp Toker094e5212014-01-05 03:27:11 +00001208 if (ExpectAndConsume(tok::comma)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001209 T.skipToEnd();
1210 return;
1211 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001212
1213 SourceRange MatchingCTypeRange;
1214 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1215 if (MatchingCType.isInvalid()) {
1216 T.skipToEnd();
1217 return;
1218 }
1219
1220 bool LayoutCompatible = false;
1221 bool MustBeNull = false;
Alp Toker8fbec672013-12-17 23:29:36 +00001222 while (TryConsumeToken(tok::comma)) {
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001223 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00001224 Diag(Tok, diag::err_expected) << tok::identifier;
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001225 T.skipToEnd();
1226 return;
1227 }
1228 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1229 if (Flag->isStr("layout_compatible"))
1230 LayoutCompatible = true;
1231 else if (Flag->isStr("must_be_null"))
1232 MustBeNull = true;
1233 else {
1234 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1235 T.skipToEnd();
1236 return;
1237 }
1238 ConsumeToken(); // consume flag
1239 }
1240
1241 if (!T.consumeClose()) {
Aaron Ballman80f1529c2014-07-16 20:21:50 +00001242 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, ScopeName, ScopeLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001243 ArgumentKind, MatchingCType.get(),
Aaron Ballman80f1529c2014-07-16 20:21:50 +00001244 LayoutCompatible, MustBeNull, Syntax);
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001245 }
1246
1247 if (EndLoc)
1248 *EndLoc = T.getCloseLocation();
1249}
1250
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001251/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1252/// of a C++11 attribute-specifier in a location where an attribute is not
1253/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1254/// situation.
1255///
1256/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1257/// this doesn't appear to actually be an attribute-specifier, and the caller
1258/// should try to parse it.
1259bool Parser::DiagnoseProhibitedCXX11Attribute() {
1260 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1261
1262 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1263 case CAK_NotAttributeSpecifier:
1264 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1265 return false;
1266
1267 case CAK_InvalidAttributeSpecifier:
1268 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1269 return false;
1270
1271 case CAK_AttributeSpecifier:
1272 // Parse and discard the attributes.
1273 SourceLocation BeginLoc = ConsumeBracket();
1274 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001275 SkipUntil(tok::r_square);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001276 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1277 SourceLocation EndLoc = ConsumeBracket();
1278 Diag(BeginLoc, diag::err_attributes_not_allowed)
1279 << SourceRange(BeginLoc, EndLoc);
1280 return true;
1281 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001282 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001283}
1284
Richard Smith98155ad2013-02-20 01:17:14 +00001285/// \brief We have found the opening square brackets of a C++11
1286/// attribute-specifier in a location where an attribute is not permitted, but
1287/// we know where the attributes ought to be written. Parse them anyway, and
1288/// provide a fixit moving them to the right place.
Richard Smith4c96e992013-02-19 23:47:15 +00001289void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1290 SourceLocation CorrectLocation) {
1291 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1292 Tok.is(tok::kw_alignas));
1293
1294 // Consume the attributes.
1295 SourceLocation Loc = Tok.getLocation();
1296 ParseCXX11Attributes(Attrs);
1297 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1298
1299 Diag(Loc, diag::err_attributes_not_allowed)
1300 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1301 << FixItHint::CreateRemoval(AttrRange);
1302}
1303
John McCall53fa7142010-12-24 02:08:15 +00001304void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1305 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1306 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001307}
1308
Michael Han64536a62012-11-06 19:34:54 +00001309void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1310 AttributeList *AttrList = attrs.getList();
1311 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001312 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001313 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001314 << AttrList->getName();
1315 AttrList->setInvalid();
1316 }
1317 AttrList = AttrList->getNext();
1318 }
1319}
1320
Chris Lattner53361ac2006-08-10 05:19:57 +00001321/// ParseDeclaration - Parse a full 'declaration', which consists of
1322/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001323/// 'Context' should be a Declarator::TheContext value. This returns the
1324/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001325///
1326/// declaration: [C99 6.7]
1327/// block-declaration ->
1328/// simple-declaration
1329/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001330/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001331/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001332/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001333/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001334/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001335/// others... [FIXME]
1336///
Rafael Espindola1bd906d2014-10-22 14:27:08 +00001337Parser::DeclGroupPtrTy Parser::ParseDeclaration(unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001338 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001339 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001340 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001341 // Must temporarily exit the objective-c container scope for
1342 // parsing c none objective-c decls.
1343 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001344
Craig Topper161e4db2014-05-21 06:02:52 +00001345 Decl *SingleDecl = nullptr;
1346 Decl *OwnedType = nullptr;
Chris Lattnera5235172007-08-25 06:57:03 +00001347 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001348 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001349 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001350 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001351 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001352 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001353 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001354 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001355 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001356 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001357 SourceLocation InlineLoc = ConsumeToken();
1358 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1359 break;
1360 }
Rafael Espindola1bd906d2014-10-22 14:27:08 +00001361 return ParseSimpleDeclaration(Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001362 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001363 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001364 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001365 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001366 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001367 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001368 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001369 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001370 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001371 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001372 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001373 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001374 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001375 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001376 default:
Rafael Espindola1bd906d2014-10-22 14:27:08 +00001377 return ParseSimpleDeclaration(Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001378 }
Chad Rosierc1183952012-06-26 22:30:43 +00001379
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001380 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001381 // single decl, convert it now. Alias declarations can also declare a type;
1382 // include that too if it is present.
1383 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001384}
1385
1386/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1387/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001388/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1389/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001390///[C90/C++]init-declarator-list ';' [TODO]
1391/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001392///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001393/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001394/// attribute-specifier-seq[opt] type-specifier-seq declarator
1395///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001396/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001397/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001398///
1399/// If FRI is non-null, we might be parsing a for-range-declaration instead
1400/// of a simple-declaration. If we find that we are, we also parse the
1401/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001402Parser::DeclGroupPtrTy
Rafael Espindola1bd906d2014-10-22 14:27:08 +00001403Parser::ParseSimpleDeclaration(unsigned Context,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001404 SourceLocation &DeclEnd,
Richard Smith2386c8b2013-02-22 09:06:26 +00001405 ParsedAttributesWithRange &Attrs,
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001406 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001407 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001408 ParsingDeclSpec DS(*this);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001409
Richard Smith404dfb42013-11-19 22:47:36 +00001410 DeclSpecContext DSContext = getDeclSpecContextFromDeclaratorContext(Context);
1411 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none, DSContext);
1412
1413 // If we had a free-standing type definition with a missing semicolon, we
1414 // may get this far before the problem becomes obvious.
1415 if (DS.hasTagDefinition() &&
1416 DiagnoseMissingSemiAfterTagDefinition(DS, AS_none, DSContext))
1417 return DeclGroupPtrTy();
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001418
Chris Lattner0e894622006-08-13 19:58:17 +00001419 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1420 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001421 if (Tok.is(tok::semi)) {
Richard Smith2386c8b2013-02-22 09:06:26 +00001422 ProhibitAttributes(Attrs);
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001423 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001424 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001425 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001426 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001427 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001428 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001429 }
Chad Rosierc1183952012-06-26 22:30:43 +00001430
Richard Smith2386c8b2013-02-22 09:06:26 +00001431 DS.takeAttributesFrom(Attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00001432 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001433}
Mike Stump11289f42009-09-09 15:08:12 +00001434
Richard Smith09f76ee2011-10-19 21:33:05 +00001435/// Returns true if this might be the start of a declarator, or a common typo
1436/// for a declarator.
1437bool Parser::MightBeDeclarator(unsigned Context) {
1438 switch (Tok.getKind()) {
1439 case tok::annot_cxxscope:
1440 case tok::annot_template_id:
1441 case tok::caret:
1442 case tok::code_completion:
1443 case tok::coloncolon:
1444 case tok::ellipsis:
1445 case tok::kw___attribute:
1446 case tok::kw_operator:
1447 case tok::l_paren:
1448 case tok::star:
1449 return true;
1450
1451 case tok::amp:
1452 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001453 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001454
Richard Smithc8a79032012-01-09 22:31:44 +00001455 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001456 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001457 NextToken().is(tok::l_square);
1458
1459 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001460 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001461
Richard Smith09f76ee2011-10-19 21:33:05 +00001462 case tok::identifier:
1463 switch (NextToken().getKind()) {
1464 case tok::code_completion:
1465 case tok::coloncolon:
1466 case tok::comma:
1467 case tok::equal:
1468 case tok::equalequal: // Might be a typo for '='.
1469 case tok::kw_alignas:
1470 case tok::kw_asm:
1471 case tok::kw___attribute:
1472 case tok::l_brace:
1473 case tok::l_paren:
1474 case tok::l_square:
1475 case tok::less:
1476 case tok::r_brace:
1477 case tok::r_paren:
1478 case tok::r_square:
1479 case tok::semi:
1480 return true;
1481
1482 case tok::colon:
1483 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001484 // and in block scope it's probably a label. Inside a class definition,
1485 // this is a bit-field.
1486 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001487 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001488
1489 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001490 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001491
1492 default:
1493 return false;
1494 }
1495
1496 default:
1497 return false;
1498 }
1499}
1500
Richard Smithb8caac82012-04-11 20:59:20 +00001501/// Skip until we reach something which seems like a sensible place to pick
1502/// up parsing after a malformed declaration. This will sometimes stop sooner
1503/// than SkipUntil(tok::r_brace) would, but will never stop later.
1504void Parser::SkipMalformedDecl() {
1505 while (true) {
1506 switch (Tok.getKind()) {
1507 case tok::l_brace:
1508 // Skip until matching }, then stop. We've probably skipped over
1509 // a malformed class or function definition or similar.
1510 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001511 SkipUntil(tok::r_brace);
Richard Smithb8caac82012-04-11 20:59:20 +00001512 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1513 // This declaration isn't over yet. Keep skipping.
1514 continue;
1515 }
Alp Toker8fbec672013-12-17 23:29:36 +00001516 TryConsumeToken(tok::semi);
Richard Smithb8caac82012-04-11 20:59:20 +00001517 return;
1518
1519 case tok::l_square:
1520 ConsumeBracket();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001521 SkipUntil(tok::r_square);
Richard Smithb8caac82012-04-11 20:59:20 +00001522 continue;
1523
1524 case tok::l_paren:
1525 ConsumeParen();
Alexey Bataevee6507d2013-11-18 08:17:37 +00001526 SkipUntil(tok::r_paren);
Richard Smithb8caac82012-04-11 20:59:20 +00001527 continue;
1528
1529 case tok::r_brace:
1530 return;
1531
1532 case tok::semi:
1533 ConsumeToken();
1534 return;
1535
1536 case tok::kw_inline:
1537 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001538 // a good place to pick back up parsing, except in an Objective-C
1539 // @interface context.
1540 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1541 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001542 return;
1543 break;
1544
1545 case tok::kw_namespace:
1546 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001547 // place to pick back up parsing, except in an Objective-C
1548 // @interface context.
1549 if (Tok.isAtStartOfLine() &&
1550 (!ParsingInObjCContainer || CurParsedObjCImpl))
1551 return;
1552 break;
1553
1554 case tok::at:
1555 // @end is very much like } in Objective-C contexts.
1556 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1557 ParsingInObjCContainer)
1558 return;
1559 break;
1560
1561 case tok::minus:
1562 case tok::plus:
1563 // - and + probably start new method declarations in Objective-C contexts.
1564 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001565 return;
1566 break;
1567
1568 case tok::eof:
Richard Smith34f30512013-11-23 04:06:09 +00001569 case tok::annot_module_begin:
1570 case tok::annot_module_end:
1571 case tok::annot_module_include:
Richard Smithb8caac82012-04-11 20:59:20 +00001572 return;
1573
1574 default:
1575 break;
1576 }
1577
1578 ConsumeAnyToken();
1579 }
1580}
1581
John McCalld5a36322009-11-03 19:26:08 +00001582/// ParseDeclGroup - Having concluded that this is either a function
1583/// definition or a group of object declarations, actually parse the
1584/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001585Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1586 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001587 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001588 SourceLocation *DeclEnd,
1589 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001590 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001591 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001592 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001593
John McCalld5a36322009-11-03 19:26:08 +00001594 // Bail out if the first declarator didn't seem well-formed.
1595 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001596 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001597 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001598 }
Mike Stump11289f42009-09-09 15:08:12 +00001599
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001600 // Save late-parsed attributes for now; they need to be parsed in the
1601 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001602 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1603 LateParsedAttrList LateParsedAttrs(true);
Richard Smith99c464c2014-11-10 21:10:32 +00001604 if (D.isFunctionDeclarator()) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001605 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1606
Richard Smith99c464c2014-11-10 21:10:32 +00001607 // The _Noreturn keyword can't appear here, unlike the GNU noreturn
1608 // attribute. If we find the keyword here, tell the user to put it
1609 // at the start instead.
1610 if (Tok.is(tok::kw__Noreturn)) {
1611 SourceLocation Loc = ConsumeToken();
1612 const char *PrevSpec;
1613 unsigned DiagID;
1614
1615 // We can offer a fixit if it's valid to mark this function as _Noreturn
1616 // and we don't have any other declarators in this declaration.
1617 bool Fixit = !DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
1618 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1619 Fixit &= Tok.is(tok::semi) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try);
1620
1621 Diag(Loc, diag::err_c11_noreturn_misplaced)
1622 << (Fixit ? FixItHint::CreateRemoval(Loc) : FixItHint())
1623 << (Fixit ? FixItHint::CreateInsertion(D.getLocStart(), "_Noreturn ")
1624 : FixItHint());
1625 }
1626 }
1627
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001628 // Check to see if we have a function *definition* which must have a body.
Douglas Gregor012efe22013-04-16 16:01:32 +00001629 if (D.isFunctionDeclarator() &&
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001630 // Look at the next token to make sure that this isn't a function
1631 // declaration. We have to check this because __attribute__ might be the
1632 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001633 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001634
Douglas Gregor012efe22013-04-16 16:01:32 +00001635 if (AllowFunctionDefinitions) {
1636 if (isStartOfFunctionDefinition(D)) {
1637 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1638 Diag(Tok, diag::err_function_declared_typedef);
John McCalld5a36322009-11-03 19:26:08 +00001639
Douglas Gregor012efe22013-04-16 16:01:32 +00001640 // Recover by treating the 'typedef' as spurious.
1641 DS.ClearStorageClassSpecs();
1642 }
1643
1644 Decl *TheDecl =
1645 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
1646 return Actions.ConvertDeclToDeclGroup(TheDecl);
John McCalld5a36322009-11-03 19:26:08 +00001647 }
1648
Douglas Gregor012efe22013-04-16 16:01:32 +00001649 if (isDeclarationSpecifier()) {
1650 // If there is an invalid declaration specifier right after the function
1651 // prototype, then we must be in a missing semicolon case where this isn't
1652 // actually a body. Just fall through into the code that handles it as a
1653 // prototype, and let the top-level code handle the erroneous declspec
1654 // where it would otherwise expect a comma or semicolon.
1655 } else {
1656 Diag(Tok, diag::err_expected_fn_body);
1657 SkipUntil(tok::semi);
1658 return DeclGroupPtrTy();
1659 }
John McCalld5a36322009-11-03 19:26:08 +00001660 } else {
Douglas Gregor012efe22013-04-16 16:01:32 +00001661 if (Tok.is(tok::l_brace)) {
1662 Diag(Tok, diag::err_function_definition_not_allowed);
Serge Pavlov1de51512013-12-09 05:25:47 +00001663 SkipMalformedDecl();
1664 return DeclGroupPtrTy();
Douglas Gregor012efe22013-04-16 16:01:32 +00001665 }
John McCalld5a36322009-11-03 19:26:08 +00001666 }
1667 }
1668
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001669 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001670 return DeclGroupPtrTy();
1671
1672 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1673 // must parse and analyze the for-range-initializer before the declaration is
1674 // analyzed.
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001675 //
1676 // Handle the Objective-C for-in loop variable similarly, although we
1677 // don't need to parse the container in advance.
1678 if (FRI && (Tok.is(tok::colon) || isTokIdentifier_in())) {
1679 bool IsForRangeLoop = false;
Alp Toker8fbec672013-12-17 23:29:36 +00001680 if (TryConsumeToken(tok::colon, FRI->ColonLoc)) {
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001681 IsForRangeLoop = true;
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001682 if (Tok.is(tok::l_brace))
1683 FRI->RangeExpr = ParseBraceInitializer();
1684 else
1685 FRI->RangeExpr = ParseExpression();
1686 }
1687
Richard Smith02e85f32011-04-14 22:09:26 +00001688 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor2eb1c572013-04-08 20:52:24 +00001689 if (IsForRangeLoop)
1690 Actions.ActOnCXXForRangeDecl(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001691 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001692 D.complete(ThisDecl);
Rafael Espindolaab417692013-07-09 12:05:01 +00001693 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001694 }
1695
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001696 SmallVector<Decl *, 8> DeclsInGroup;
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001697 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(
1698 D, ParsedTemplateInfo(), FRI);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001699 if (LateParsedAttrs.size() > 0)
1700 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001701 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001702 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001703 DeclsInGroup.push_back(FirstDecl);
1704
Richard Smith09f76ee2011-10-19 21:33:05 +00001705 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001706
John McCalld5a36322009-11-03 19:26:08 +00001707 // If we don't have a comma, it is either the end of the list (a ';') or an
1708 // error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00001709 SourceLocation CommaLoc;
1710 while (TryConsumeToken(tok::comma, CommaLoc)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001711 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1712 // This comma was followed by a line-break and something which can't be
1713 // the start of a declarator. The comma was probably a typo for a
1714 // semicolon.
1715 Diag(CommaLoc, diag::err_expected_semi_declaration)
1716 << FixItHint::CreateReplacement(CommaLoc, ";");
1717 ExpectSemi = false;
1718 break;
1719 }
John McCalld5a36322009-11-03 19:26:08 +00001720
1721 // Parse the next declarator.
1722 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001723 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001724
1725 // Accept attributes in an init-declarator. In the first declarator in a
1726 // declaration, these would be part of the declspec. In subsequent
1727 // declarators, they become part of the declarator itself, so that they
1728 // don't apply to declarators after *this* one. Examples:
1729 // short __attribute__((common)) var; -> declspec
1730 // short var __attribute__((common)); -> declarator
1731 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001732 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001733
1734 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001735 if (!D.isInvalidType()) {
1736 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1737 D.complete(ThisDecl);
1738 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001739 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001740 }
John McCalld5a36322009-11-03 19:26:08 +00001741 }
1742
1743 if (DeclEnd)
1744 *DeclEnd = Tok.getLocation();
1745
Richard Smith09f76ee2011-10-19 21:33:05 +00001746 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001747 ExpectAndConsumeSemi(Context == Declarator::FileContext
1748 ? diag::err_invalid_token_after_toplevel_declarator
1749 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001750 // Okay, there was no semicolon and one was expected. If we see a
1751 // declaration specifier, just assume it was missing and continue parsing.
1752 // Otherwise things are very confused and we skip to recover.
1753 if (!isDeclarationSpecifier()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001754 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
Alp Toker8fbec672013-12-17 23:29:36 +00001755 TryConsumeToken(tok::semi);
Chris Lattner13901342010-07-11 22:42:07 +00001756 }
John McCalld5a36322009-11-03 19:26:08 +00001757 }
1758
Rafael Espindolaab417692013-07-09 12:05:01 +00001759 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
Chris Lattner53361ac2006-08-10 05:19:57 +00001760}
1761
Richard Smith02e85f32011-04-14 22:09:26 +00001762/// Parse an optional simple-asm-expr and attributes, and attach them to a
1763/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001764bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001765 // If a simple-asm-expr is present, parse it.
1766 if (Tok.is(tok::kw_asm)) {
1767 SourceLocation Loc;
1768 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1769 if (AsmLabel.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001770 SkipUntil(tok::semi, StopBeforeMatch);
Richard Smith02e85f32011-04-14 22:09:26 +00001771 return true;
1772 }
1773
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001774 D.setAsmLabel(AsmLabel.get());
Richard Smith02e85f32011-04-14 22:09:26 +00001775 D.SetRangeEnd(Loc);
1776 }
1777
1778 MaybeParseGNUAttributes(D);
1779 return false;
1780}
1781
Douglas Gregor23996282009-05-12 21:31:51 +00001782/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1783/// declarator'. This method parses the remainder of the declaration
1784/// (including any attributes or initializer, among other things) and
1785/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001786///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001787/// init-declarator: [C99 6.7]
1788/// declarator
1789/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001790/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1791/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001792/// [C++] declarator initializer[opt]
1793///
1794/// [C++] initializer:
1795/// [C++] '=' initializer-clause
1796/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001797/// [C++0x] '=' 'default' [TODO]
1798/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001799/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001800///
1801/// According to the standard grammar, =default and =delete are function
1802/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001803///
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001804Decl *Parser::ParseDeclarationAfterDeclarator(
1805 Declarator &D, const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001806 if (ParseAsmAttributesAfterDeclarator(D))
Craig Topper161e4db2014-05-21 06:02:52 +00001807 return nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00001808
Richard Smith02e85f32011-04-14 22:09:26 +00001809 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1810}
Mike Stump11289f42009-09-09 15:08:12 +00001811
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001812Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(
1813 Declarator &D, const ParsedTemplateInfo &TemplateInfo, ForRangeInit *FRI) {
Douglas Gregor23996282009-05-12 21:31:51 +00001814 // Inform the current actions module that we just parsed this declarator.
Craig Topper161e4db2014-05-21 06:02:52 +00001815 Decl *ThisDecl = nullptr;
Douglas Gregor450f00842009-09-25 18:43:00 +00001816 switch (TemplateInfo.Kind) {
1817 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001818 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001819 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001820
Douglas Gregor450f00842009-09-25 18:43:00 +00001821 case ParsedTemplateInfo::Template:
Larisse Voufo39a1e502013-08-06 01:03:05 +00001822 case ParsedTemplateInfo::ExplicitSpecialization: {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001823 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001824 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001825 D);
Larisse Voufo833b05a2013-08-06 07:33:00 +00001826 if (VarTemplateDecl *VT = dyn_cast_or_null<VarTemplateDecl>(ThisDecl))
Larisse Voufo39a1e502013-08-06 01:03:05 +00001827 // Re-direct this decl to refer to the templated decl so that we can
1828 // initialize it.
1829 ThisDecl = VT->getTemplatedDecl();
1830 break;
1831 }
1832 case ParsedTemplateInfo::ExplicitInstantiation: {
1833 if (Tok.is(tok::semi)) {
1834 DeclResult ThisRes = Actions.ActOnExplicitInstantiation(
1835 getCurScope(), TemplateInfo.ExternLoc, TemplateInfo.TemplateLoc, D);
1836 if (ThisRes.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00001837 SkipUntil(tok::semi, StopBeforeMatch);
Craig Topper161e4db2014-05-21 06:02:52 +00001838 return nullptr;
Larisse Voufo39a1e502013-08-06 01:03:05 +00001839 }
1840 ThisDecl = ThisRes.get();
1841 } else {
1842 // FIXME: This check should be for a variable template instantiation only.
1843
1844 // Check that this is a valid instantiation
1845 if (D.getName().getKind() != UnqualifiedId::IK_TemplateId) {
1846 // If the declarator-id is not a template-id, issue a diagnostic and
1847 // recover by ignoring the 'template' keyword.
1848 Diag(Tok, diag::err_template_defn_explicit_instantiation)
1849 << 2 << FixItHint::CreateRemoval(TemplateInfo.TemplateLoc);
1850 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1851 } else {
1852 SourceLocation LAngleLoc =
1853 PP.getLocForEndOfToken(TemplateInfo.TemplateLoc);
1854 Diag(D.getIdentifierLoc(),
1855 diag::err_explicit_instantiation_with_definition)
1856 << SourceRange(TemplateInfo.TemplateLoc)
1857 << FixItHint::CreateInsertion(LAngleLoc, "<>");
1858
1859 // Recover as if it were an explicit specialization.
1860 TemplateParameterLists FakedParamLists;
1861 FakedParamLists.push_back(Actions.ActOnTemplateParameterList(
Craig Topper161e4db2014-05-21 06:02:52 +00001862 0, SourceLocation(), TemplateInfo.TemplateLoc, LAngleLoc, nullptr,
1863 0, LAngleLoc));
Larisse Voufo39a1e502013-08-06 01:03:05 +00001864
1865 ThisDecl =
1866 Actions.ActOnTemplateDeclarator(getCurScope(), FakedParamLists, D);
1867 }
1868 }
Douglas Gregor450f00842009-09-25 18:43:00 +00001869 break;
1870 }
1871 }
Mike Stump11289f42009-09-09 15:08:12 +00001872
Richard Smith74aeef52013-04-26 16:15:35 +00001873 bool TypeContainsAuto = D.getDeclSpec().containsPlaceholderType();
Richard Smith30482bc2011-02-20 03:19:35 +00001874
Douglas Gregor23996282009-05-12 21:31:51 +00001875 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001876 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001877 if (isTokenEqualOrEqualTypo()) {
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001878 SourceLocation EqualLoc = ConsumeToken();
Larisse Voufo39a1e502013-08-06 01:03:05 +00001879
Anders Carlsson991285e2010-09-24 21:25:25 +00001880 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001881 if (D.isFunctionDeclarator())
1882 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1883 << 1 /* delete */;
1884 else
1885 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001886 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001887 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001888 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1889 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001890 else
1891 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001892 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001893 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001894 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001895 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001896 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001897
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001898 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001899 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001900 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001901 cutOffParsing();
Craig Topper161e4db2014-05-21 06:02:52 +00001902 return nullptr;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001903 }
Chad Rosierc1183952012-06-26 22:30:43 +00001904
John McCalldadc5752010-08-24 06:29:42 +00001905 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001906
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001907 // If this is the only decl in (possibly) range based for statement,
1908 // our best guess is that the user meant ':' instead of '='.
1909 if (Tok.is(tok::r_paren) && FRI && D.isFirstDeclarator()) {
1910 Diag(EqualLoc, diag::err_single_decl_assign_in_for_range)
1911 << FixItHint::CreateReplacement(EqualLoc, ":");
1912 // We are trying to stop parser from looking for ';' in this for
1913 // statement, therefore preventing spurious errors to be issued.
1914 FRI->ColonLoc = EqualLoc;
1915 Init = ExprError();
1916 FRI->RangeExpr = Init;
1917 }
1918
David Blaikiebbafb8a2012-03-11 07:00:24 +00001919 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001920 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001921 ExitScope();
1922 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001923
Douglas Gregor23996282009-05-12 21:31:51 +00001924 if (Init.isInvalid()) {
Ismail Pazarbasi49ff7542014-05-08 11:28:25 +00001925 SmallVector<tok::TokenKind, 2> StopTokens;
1926 StopTokens.push_back(tok::comma);
1927 if (D.getContext() == Declarator::ForContext)
1928 StopTokens.push_back(tok::r_paren);
1929 SkipUntil(StopTokens, StopAtSemi | StopBeforeMatch);
Douglas Gregor604c3022010-03-01 18:27:54 +00001930 Actions.ActOnInitializerError(ThisDecl);
1931 } else
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001932 Actions.AddInitializerToDecl(ThisDecl, Init.get(),
Richard Smith30482bc2011-02-20 03:19:35 +00001933 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001934 }
1935 } else if (Tok.is(tok::l_paren)) {
1936 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001937 BalancedDelimiterTracker T(*this, tok::l_paren);
1938 T.consumeOpen();
1939
Benjamin Kramerf0623432012-08-23 22:51:59 +00001940 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001941 CommaLocsTy CommaLocs;
1942
David Blaikiebbafb8a2012-03-11 07:00:24 +00001943 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001944 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001945 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001946 }
1947
Douglas Gregor23996282009-05-12 21:31:51 +00001948 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001949 Actions.ActOnInitializerError(ThisDecl);
Alexey Bataevee6507d2013-11-18 08:17:37 +00001950 SkipUntil(tok::r_paren, StopAtSemi);
Douglas Gregor613bf102009-12-22 17:47:17 +00001951
David Blaikiebbafb8a2012-03-11 07:00:24 +00001952 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001953 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001954 ExitScope();
1955 }
Douglas Gregor23996282009-05-12 21:31:51 +00001956 } else {
1957 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001958 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001959
1960 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1961 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001962
David Blaikiebbafb8a2012-03-11 07:00:24 +00001963 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001964 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001965 ExitScope();
1966 }
1967
Sebastian Redla9351792012-02-11 23:51:47 +00001968 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1969 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001970 Exprs);
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001971 Actions.AddInitializerToDecl(ThisDecl, Initializer.get(),
Sebastian Redla9351792012-02-11 23:51:47 +00001972 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001973 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001974 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001975 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001976 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001977 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1978
Sebastian Redl3da34892011-06-05 12:23:16 +00001979 if (D.getCXXScopeSpec().isSet()) {
1980 EnterScope(0);
1981 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1982 }
1983
1984 ExprResult Init(ParseBraceInitializer());
1985
1986 if (D.getCXXScopeSpec().isSet()) {
1987 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1988 ExitScope();
1989 }
1990
1991 if (Init.isInvalid()) {
1992 Actions.ActOnInitializerError(ThisDecl);
1993 } else
Nikola Smiljanic01a75982014-05-29 10:55:11 +00001994 Actions.AddInitializerToDecl(ThisDecl, Init.get(),
Sebastian Redl3da34892011-06-05 12:23:16 +00001995 /*DirectInit=*/true, TypeContainsAuto);
1996
Douglas Gregor23996282009-05-12 21:31:51 +00001997 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001998 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001999 }
2000
Richard Smithb2bc2e62011-02-21 20:05:19 +00002001 Actions.FinalizeDeclaration(ThisDecl);
2002
Douglas Gregor23996282009-05-12 21:31:51 +00002003 return ThisDecl;
2004}
2005
Chris Lattner1890ac82006-08-13 01:16:23 +00002006/// ParseSpecifierQualifierList
2007/// specifier-qualifier-list:
2008/// type-specifier specifier-qualifier-list[opt]
2009/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002010/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00002011///
Richard Smithc5b05522012-03-12 07:56:15 +00002012void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
2013 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002014 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
2015 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002016 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00002017 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00002018
Chris Lattner1890ac82006-08-13 01:16:23 +00002019 // Validate declspec for type-name.
2020 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith649c7b062014-01-08 00:56:48 +00002021 if (isTypeSpecifier(DSC) && !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00002022 Diag(Tok, diag::err_expected_type);
2023 DS.SetTypeSpecError();
2024 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
2025 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00002026 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00002027 if (!DS.hasTypeSpecifier())
2028 DS.SetTypeSpecError();
2029 }
Mike Stump11289f42009-09-09 15:08:12 +00002030
Chris Lattner1b22eed2006-11-28 05:12:07 +00002031 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002032 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00002033 if (DS.getStorageClassSpecLoc().isValid())
2034 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
2035 else
Richard Smithb4a9e862013-04-12 22:46:28 +00002036 Diag(DS.getThreadStorageClassSpecLoc(),
2037 diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00002038 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002039 }
Mike Stump11289f42009-09-09 15:08:12 +00002040
Chris Lattner1b22eed2006-11-28 05:12:07 +00002041 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00002042 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00002043 if (DS.isInlineSpecified())
2044 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
2045 if (DS.isVirtualSpecified())
2046 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
2047 if (DS.isExplicitSpecified())
2048 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00002049 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00002050 }
Richard Smithc5b05522012-03-12 07:56:15 +00002051
2052 // Issue diagnostic and remove constexpr specfier if present.
2053 if (DS.isConstexprSpecified()) {
2054 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
2055 DS.ClearConstexprSpec();
2056 }
Chris Lattner1890ac82006-08-13 01:16:23 +00002057}
Chris Lattner53361ac2006-08-10 05:19:57 +00002058
Chris Lattner6cc055a2009-04-12 20:42:31 +00002059/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
2060/// specified token is valid after the identifier in a declarator which
2061/// immediately follows the declspec. For example, these things are valid:
2062///
2063/// int x [ 4]; // direct-declarator
2064/// int x ( int y); // direct-declarator
2065/// int(int x ) // direct-declarator
2066/// int x ; // simple-declaration
2067/// int x = 17; // init-declarator-list
2068/// int x , y; // init-declarator-list
2069/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00002070/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00002071/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00002072///
2073/// This is not, because 'x' does not immediately follow the declspec (though
2074/// ')' happens to be valid anyway).
2075/// int (x)
2076///
2077static bool isValidAfterIdentifierInDeclarator(const Token &T) {
2078 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
2079 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00002080 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00002081}
2082
Chris Lattner20a0c612009-04-14 21:34:55 +00002083
2084/// ParseImplicitInt - This method is called when we have an non-typename
2085/// identifier in a declspec (which normally terminates the decl spec) when
2086/// the declspec has no type specifier. In this case, the declspec is either
2087/// malformed or is "implicit int" (in K&R and C89).
2088///
2089/// This method handles diagnosing this prettily and returns false if the
2090/// declspec is done being processed. If it recovers and thinks there may be
2091/// other pieces of declspec after it, it returns true.
2092///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002093bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002094 const ParsedTemplateInfo &TemplateInfo,
Richard Smitha0a5d502014-05-08 22:32:00 +00002095 AccessSpecifier AS, DeclSpecContext DSC,
Michael Han9407e502012-11-26 22:54:45 +00002096 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002097 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00002098
Chris Lattner20a0c612009-04-14 21:34:55 +00002099 SourceLocation Loc = Tok.getLocation();
2100 // If we see an identifier that is not a type name, we normally would
2101 // parse it as the identifer being declared. However, when a typename
2102 // is typo'd or the definition is not included, this will incorrectly
2103 // parse the typename as the identifier name and fall over misparsing
2104 // later parts of the diagnostic.
2105 //
2106 // As such, we try to do some look-ahead in cases where this would
2107 // otherwise be an "implicit-int" case to see if this is invalid. For
2108 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
2109 // an identifier with implicit int, we'd get a parse error because the
2110 // next token is obviously invalid for a type. Parse these as a case
2111 // with an invalid type specifier.
2112 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00002113
Chris Lattner20a0c612009-04-14 21:34:55 +00002114 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00002115 // error, do lookahead to try to do better recovery. This never applies
2116 // within a type specifier. Outside of C++, we allow this even if the
2117 // language doesn't "officially" support implicit int -- we support
Richard Smith3b870382013-04-30 22:43:51 +00002118 // implicit int as an extension in C99 and C11.
Richard Smith649c7b062014-01-08 00:56:48 +00002119 if (!isTypeSpecifier(DSC) && !getLangOpts().CPlusPlus &&
Richard Smithc5b05522012-03-12 07:56:15 +00002120 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002121 // If this token is valid for implicit int, e.g. "static x = 4", then
2122 // we just avoid eating the identifier, so it will be parsed as the
2123 // identifier in the declarator.
2124 return false;
2125 }
Mike Stump11289f42009-09-09 15:08:12 +00002126
Richard Smitha952ebb2012-05-15 21:01:51 +00002127 if (getLangOpts().CPlusPlus &&
2128 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
2129 // Don't require a type specifier if we have the 'auto' storage class
2130 // specifier in C++98 -- we'll promote it to a type specifier.
Richard Smithfb8b7b92013-10-15 00:00:26 +00002131 if (SS)
2132 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
Richard Smitha952ebb2012-05-15 21:01:51 +00002133 return false;
2134 }
2135
Chris Lattner20a0c612009-04-14 21:34:55 +00002136 // Otherwise, if we don't consume this token, we are going to emit an
2137 // error anyway. Try to recover from various common problems. Check
2138 // to see if this was a reference to a tag name without a tag specified.
2139 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002140 //
2141 // C++ doesn't need this, and isTagName doesn't take SS.
Craig Topper161e4db2014-05-21 06:02:52 +00002142 if (SS == nullptr) {
2143 const char *TagName = nullptr, *FixitTagName = nullptr;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002144 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00002145
Douglas Gregor0be31a22010-07-02 17:43:08 +00002146 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00002147 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002148 case DeclSpec::TST_enum:
2149 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
2150 case DeclSpec::TST_union:
2151 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
2152 case DeclSpec::TST_struct:
2153 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00002154 case DeclSpec::TST_interface:
2155 TagName="__interface"; FixitTagName = "__interface ";
2156 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00002157 case DeclSpec::TST_class:
2158 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00002159 }
Mike Stump11289f42009-09-09 15:08:12 +00002160
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002161 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002162 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
2163 LookupResult R(Actions, TokenName, SourceLocation(),
2164 Sema::LookupOrdinaryName);
2165
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002166 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002167 << TokenName << TagName << getLangOpts().CPlusPlus
2168 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
2169
2170 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
2171 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
2172 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00002173 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00002174 << TokenName << TagName;
2175 }
Mike Stump11289f42009-09-09 15:08:12 +00002176
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002177 // Parse this as a tag as if the missing tag were present.
2178 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00002179 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002180 else
Richard Smithc5b05522012-03-12 07:56:15 +00002181 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00002182 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002183 return true;
2184 }
Chris Lattner20a0c612009-04-14 21:34:55 +00002185 }
Mike Stump11289f42009-09-09 15:08:12 +00002186
Richard Smithfe904f02012-05-15 21:29:55 +00002187 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00002188 // being declared (with a missing type).
Richard Smith649c7b062014-01-08 00:56:48 +00002189 if (!isTypeSpecifier(DSC) &&
Richard Smithfe904f02012-05-15 21:29:55 +00002190 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002191 // Look ahead to the next token to try to figure out what this declaration
2192 // was supposed to be.
2193 switch (NextToken().getKind()) {
Richard Smitha952ebb2012-05-15 21:01:51 +00002194 case tok::l_paren: {
2195 // static x(4); // 'x' is not a type
2196 // x(int n); // 'x' is not a type
2197 // x (*p)[]; // 'x' is a type
2198 //
Richard Smitha0a5d502014-05-08 22:32:00 +00002199 // Since we're in an error case, we can afford to perform a tentative
2200 // parse to determine which case we're in.
Richard Smitha952ebb2012-05-15 21:01:51 +00002201 TentativeParsingAction PA(*this);
2202 ConsumeToken();
2203 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
2204 PA.Revert();
Richard Smithfb8b7b92013-10-15 00:00:26 +00002205
Richard Smithee390432014-05-16 01:56:53 +00002206 if (TPR != TPResult::False) {
Richard Smithfb8b7b92013-10-15 00:00:26 +00002207 // The identifier is followed by a parenthesized declarator.
2208 // It's supposed to be a type.
2209 break;
2210 }
2211
2212 // If we're in a context where we could be declaring a constructor,
2213 // check whether this is a constructor declaration with a bogus name.
2214 if (DSC == DSC_class || (DSC == DSC_top_level && SS)) {
2215 IdentifierInfo *II = Tok.getIdentifierInfo();
2216 if (Actions.isCurrentClassNameTypo(II, SS)) {
2217 Diag(Loc, diag::err_constructor_bad_name)
2218 << Tok.getIdentifierInfo() << II
2219 << FixItHint::CreateReplacement(Tok.getLocation(), II->getName());
2220 Tok.setIdentifierInfo(II);
2221 }
2222 }
2223 // Fall through.
Richard Smitha952ebb2012-05-15 21:01:51 +00002224 }
Richard Smithfb8b7b92013-10-15 00:00:26 +00002225 case tok::comma:
2226 case tok::equal:
2227 case tok::kw_asm:
2228 case tok::l_brace:
2229 case tok::l_square:
2230 case tok::semi:
2231 // This looks like a variable or function declaration. The type is
2232 // probably missing. We're done parsing decl-specifiers.
2233 if (SS)
2234 AnnotateScopeToken(*SS, /*IsNewAnnotation*/false);
2235 return false;
Richard Smitha952ebb2012-05-15 21:01:51 +00002236
2237 default:
2238 // This is probably supposed to be a type. This includes cases like:
2239 // int f(itn);
2240 // struct S { unsinged : 4; };
2241 break;
2242 }
2243 }
2244
Reid Klecknerc05ca5e2014-06-19 01:23:22 +00002245 // This is almost certainly an invalid type name. Let Sema emit a diagnostic
2246 // and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00002247 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002248 IdentifierInfo *II = Tok.getIdentifierInfo();
Reid Klecknerc05ca5e2014-06-19 01:23:22 +00002249 Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T,
2250 getLangOpts().CPlusPlus &&
2251 NextToken().is(tok::less));
2252 if (T) {
2253 // The action has suggested that the type T could be used. Set that as
2254 // the type in the declaration specifiers, consume the would-be type
2255 // name token, and we're done.
2256 const char *PrevSpec;
2257 unsigned DiagID;
2258 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T,
2259 Actions.getASTContext().getPrintingPolicy());
2260 DS.SetRangeEnd(Tok.getLocation());
2261 ConsumeToken();
2262 // There may be other declaration specifiers after this.
2263 return true;
2264 } else if (II != Tok.getIdentifierInfo()) {
2265 // If no type was suggested, the correction is to a keyword
2266 Tok.setKind(II->getTokenID());
2267 // There may be other declaration specifiers after this.
2268 return true;
Douglas Gregor15e56022009-10-13 23:27:22 +00002269 }
Mike Stump11289f42009-09-09 15:08:12 +00002270
Reid Klecknerc05ca5e2014-06-19 01:23:22 +00002271 // Otherwise, the action had no suggestion for us. Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002272 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002273 DS.SetRangeEnd(Tok.getLocation());
2274 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002275
Chris Lattner20a0c612009-04-14 21:34:55 +00002276 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2277 // avoid rippling error messages on subsequent uses of the same type,
2278 // could be useful if #include was forgotten.
2279 return false;
2280}
2281
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002282/// \brief Determine the declaration specifier context from the declarator
2283/// context.
2284///
2285/// \param Context the declarator context, which is one of the
2286/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002287Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002288Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2289 if (Context == Declarator::MemberContext)
2290 return DSC_class;
2291 if (Context == Declarator::FileContext)
2292 return DSC_top_level;
Reid Klecknerdf6e4a02014-06-06 22:36:36 +00002293 if (Context == Declarator::TemplateTypeArgContext)
2294 return DSC_template_type_arg;
Richard Smith62dad822012-03-15 01:02:11 +00002295 if (Context == Declarator::TrailingReturnContext)
2296 return DSC_trailing;
Richard Smith649c7b062014-01-08 00:56:48 +00002297 if (Context == Declarator::AliasDeclContext ||
2298 Context == Declarator::AliasTemplateContext)
2299 return DSC_alias_declaration;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002300 return DSC_normal;
2301}
2302
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002303/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2304///
2305/// FIXME: Simply returns an alignof() expression if the argument is a
2306/// type. Ideally, the type should be propagated directly into Sema.
2307///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002308/// [C11] type-id
2309/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002310/// [C++0x] type-id ...[opt]
2311/// [C++0x] assignment-expression ...[opt]
2312ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2313 SourceLocation &EllipsisLoc) {
2314 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002315 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002316 SourceLocation TypeLoc = Tok.getLocation();
2317 ParsedType Ty = ParseTypeName().get();
2318 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002319 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2320 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002321 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002322 ER = ParseConstantExpression();
2323
Alp Toker8fbec672013-12-17 23:29:36 +00002324 if (getLangOpts().CPlusPlus11)
2325 TryConsumeToken(tok::ellipsis, EllipsisLoc);
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002326
2327 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002328}
2329
2330/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2331/// attribute to Attrs.
2332///
2333/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002334/// [C11] '_Alignas' '(' type-id ')'
2335/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002336/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2337/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002338void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smith44c247f2013-02-22 08:32:16 +00002339 SourceLocation *EndLoc) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002340 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2341 "Not an alignment-specifier!");
2342
Richard Smithd11c7a12013-01-29 01:48:07 +00002343 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2344 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002345
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002346 BalancedDelimiterTracker T(*this, tok::l_paren);
Alp Toker383d2c42014-01-01 03:08:43 +00002347 if (T.expectAndConsume())
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002348 return;
2349
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002350 SourceLocation EllipsisLoc;
2351 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002352 if (ArgExpr.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00002353 T.skipToEnd();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002354 return;
2355 }
2356
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002357 T.consumeClose();
Richard Smith44c247f2013-02-22 08:32:16 +00002358 if (EndLoc)
2359 *EndLoc = T.getCloseLocation();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002360
Aaron Ballman00e99962013-08-31 01:11:41 +00002361 ArgsVector ArgExprs;
Nikola Smiljanic01a75982014-05-29 10:55:11 +00002362 ArgExprs.push_back(ArgExpr.get());
Craig Topper161e4db2014-05-21 06:02:52 +00002363 Attrs.addNew(KWName, KWLoc, nullptr, KWLoc, ArgExprs.data(), 1,
Aaron Ballman00e99962013-08-31 01:11:41 +00002364 AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002365}
2366
Richard Smith404dfb42013-11-19 22:47:36 +00002367/// Determine whether we're looking at something that might be a declarator
2368/// in a simple-declaration. If it can't possibly be a declarator, maybe
2369/// diagnose a missing semicolon after a prior tag definition in the decl
2370/// specifier.
2371///
2372/// \return \c true if an error occurred and this can't be any kind of
2373/// declaration.
2374bool
2375Parser::DiagnoseMissingSemiAfterTagDefinition(DeclSpec &DS, AccessSpecifier AS,
2376 DeclSpecContext DSContext,
2377 LateParsedAttrList *LateAttrs) {
2378 assert(DS.hasTagDefinition() && "shouldn't call this");
2379
2380 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Richard Smith404dfb42013-11-19 22:47:36 +00002381
2382 if (getLangOpts().CPlusPlus &&
2383 (Tok.is(tok::identifier) || Tok.is(tok::coloncolon) ||
2384 Tok.is(tok::kw_decltype) || Tok.is(tok::annot_template_id)) &&
2385 TryAnnotateCXXScopeToken(EnteringContext)) {
2386 SkipMalformedDecl();
2387 return true;
2388 }
2389
Richard Smith698875a2013-11-20 23:40:57 +00002390 bool HasScope = Tok.is(tok::annot_cxxscope);
2391 // Make a copy in case GetLookAheadToken invalidates the result of NextToken.
2392 Token AfterScope = HasScope ? NextToken() : Tok;
2393
Richard Smith404dfb42013-11-19 22:47:36 +00002394 // Determine whether the following tokens could possibly be a
2395 // declarator.
Richard Smith698875a2013-11-20 23:40:57 +00002396 bool MightBeDeclarator = true;
2397 if (Tok.is(tok::kw_typename) || Tok.is(tok::annot_typename)) {
2398 // A declarator-id can't start with 'typename'.
2399 MightBeDeclarator = false;
2400 } else if (AfterScope.is(tok::annot_template_id)) {
2401 // If we have a type expressed as a template-id, this cannot be a
2402 // declarator-id (such a type cannot be redeclared in a simple-declaration).
2403 TemplateIdAnnotation *Annot =
2404 static_cast<TemplateIdAnnotation *>(AfterScope.getAnnotationValue());
2405 if (Annot->Kind == TNK_Type_template)
2406 MightBeDeclarator = false;
2407 } else if (AfterScope.is(tok::identifier)) {
2408 const Token &Next = HasScope ? GetLookAheadToken(2) : NextToken();
2409
Richard Smith404dfb42013-11-19 22:47:36 +00002410 // These tokens cannot come after the declarator-id in a
2411 // simple-declaration, and are likely to come after a type-specifier.
Richard Smith698875a2013-11-20 23:40:57 +00002412 if (Next.is(tok::star) || Next.is(tok::amp) || Next.is(tok::ampamp) ||
2413 Next.is(tok::identifier) || Next.is(tok::annot_cxxscope) ||
2414 Next.is(tok::coloncolon)) {
2415 // Missing a semicolon.
2416 MightBeDeclarator = false;
2417 } else if (HasScope) {
2418 // If the declarator-id has a scope specifier, it must redeclare a
2419 // previously-declared entity. If that's a type (and this is not a
2420 // typedef), that's an error.
2421 CXXScopeSpec SS;
2422 Actions.RestoreNestedNameSpecifierAnnotation(
2423 Tok.getAnnotationValue(), Tok.getAnnotationRange(), SS);
2424 IdentifierInfo *Name = AfterScope.getIdentifierInfo();
2425 Sema::NameClassification Classification = Actions.ClassifyName(
2426 getCurScope(), SS, Name, AfterScope.getLocation(), Next,
2427 /*IsAddressOfOperand*/false);
2428 switch (Classification.getKind()) {
2429 case Sema::NC_Error:
2430 SkipMalformedDecl();
2431 return true;
Richard Smith404dfb42013-11-19 22:47:36 +00002432
Richard Smith698875a2013-11-20 23:40:57 +00002433 case Sema::NC_Keyword:
2434 case Sema::NC_NestedNameSpecifier:
2435 llvm_unreachable("typo correction and nested name specifiers not "
2436 "possible here");
Richard Smith404dfb42013-11-19 22:47:36 +00002437
Richard Smith698875a2013-11-20 23:40:57 +00002438 case Sema::NC_Type:
2439 case Sema::NC_TypeTemplate:
2440 // Not a previously-declared non-type entity.
2441 MightBeDeclarator = false;
2442 break;
Richard Smith404dfb42013-11-19 22:47:36 +00002443
Richard Smith698875a2013-11-20 23:40:57 +00002444 case Sema::NC_Unknown:
2445 case Sema::NC_Expression:
2446 case Sema::NC_VarTemplate:
2447 case Sema::NC_FunctionTemplate:
2448 // Might be a redeclaration of a prior entity.
2449 break;
2450 }
Richard Smith404dfb42013-11-19 22:47:36 +00002451 }
Richard Smith404dfb42013-11-19 22:47:36 +00002452 }
2453
Richard Smith698875a2013-11-20 23:40:57 +00002454 if (MightBeDeclarator)
Richard Smith404dfb42013-11-19 22:47:36 +00002455 return false;
2456
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002457 const PrintingPolicy &PPol = Actions.getASTContext().getPrintingPolicy();
Richard Smith404dfb42013-11-19 22:47:36 +00002458 Diag(PP.getLocForEndOfToken(DS.getRepAsDecl()->getLocEnd()),
Alp Toker383d2c42014-01-01 03:08:43 +00002459 diag::err_expected_after)
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002460 << DeclSpec::getSpecifierName(DS.getTypeSpecType(), PPol) << tok::semi;
Richard Smith404dfb42013-11-19 22:47:36 +00002461
2462 // Try to recover from the typo, by dropping the tag definition and parsing
2463 // the problematic tokens as a type.
2464 //
2465 // FIXME: Split the DeclSpec into pieces for the standalone
2466 // declaration and pieces for the following declaration, instead
2467 // of assuming that all the other pieces attach to new declaration,
2468 // and call ParsedFreeStandingDeclSpec as appropriate.
2469 DS.ClearTypeSpecType();
2470 ParsedTemplateInfo NotATemplate;
2471 ParseDeclarationSpecifiers(DS, NotATemplate, AS, DSContext, LateAttrs);
2472 return false;
2473}
2474
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002475/// ParseDeclarationSpecifiers
2476/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002477/// storage-class-specifier declaration-specifiers[opt]
2478/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002479/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002480/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002481/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002482/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002483///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002484/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002485/// 'typedef'
2486/// 'extern'
2487/// 'static'
2488/// 'auto'
2489/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002490/// [C++] 'mutable'
Richard Smithb4a9e862013-04-12 22:46:28 +00002491/// [C++11] 'thread_local'
2492/// [C11] '_Thread_local'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002493/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002494/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002495/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002496/// [C++] 'virtual'
2497/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002498/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002499/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002500/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002501
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002502///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002503void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002504 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002505 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002506 DeclSpecContext DSContext,
2507 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002508 if (DS.getSourceRange().isInvalid()) {
David Majnemer24b28302014-07-26 05:41:31 +00002509 // Start the range at the current token but make the end of the range
2510 // invalid. This will make the entire range invalid unless we successfully
2511 // consume a token.
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002512 DS.SetRangeStart(Tok.getLocation());
David Majnemer24b28302014-07-26 05:41:31 +00002513 DS.SetRangeEnd(SourceLocation());
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002514 }
Chad Rosierc1183952012-06-26 22:30:43 +00002515
Douglas Gregordf593fb2011-11-07 17:33:42 +00002516 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002517 bool AttrsLastTime = false;
2518 ParsedAttributesWithRange attrs(AttrFactory);
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002519 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002520 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002521 bool isInvalid = false;
Craig Topper161e4db2014-05-21 06:02:52 +00002522 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +00002523 unsigned DiagID = 0;
2524
Chris Lattner4d8f8732006-11-28 05:05:08 +00002525 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002526
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002527 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002528 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002529 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002530 if (!AttrsLastTime)
2531 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002532 else {
2533 // Reject C++11 attributes that appertain to decl specifiers as
2534 // we don't support any C++11 attributes that appertain to decl
2535 // specifiers. This also conforms to what g++ 4.8 is doing.
2536 ProhibitCXX11Attributes(attrs);
2537
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002538 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002539 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002540
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002541 // If this is not a declaration specifier token, we're done reading decl
2542 // specifiers. First verify that DeclSpec's are consistent.
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002543 DS.Finish(Diags, PP, Policy);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002544 return;
Mike Stump11289f42009-09-09 15:08:12 +00002545
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002546 case tok::l_square:
2547 case tok::kw_alignas:
Richard Smith4cabd042013-02-22 09:15:49 +00002548 if (!getLangOpts().CPlusPlus11 || !isCXX11AttributeSpecifier())
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002549 goto DoneWithDeclSpec;
2550
2551 ProhibitAttributes(attrs);
2552 // FIXME: It would be good to recover by accepting the attributes,
2553 // but attempting to do that now would cause serious
2554 // madness in terms of diagnostics.
2555 attrs.clear();
2556 attrs.Range = SourceRange();
2557
2558 ParseCXX11Attributes(attrs);
2559 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002560 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002561
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002562 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002563 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002564 if (DS.hasTypeSpecifier()) {
2565 bool AllowNonIdentifiers
2566 = (getCurScope()->getFlags() & (Scope::ControlScope |
2567 Scope::BlockScope |
2568 Scope::TemplateParamScope |
2569 Scope::FunctionPrototypeScope |
2570 Scope::AtCatchScope)) == 0;
2571 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002572 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002573 (DSContext == DSC_class && DS.isFriendSpecified());
2574
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002575 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002576 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002577 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002578 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002579 }
2580
Douglas Gregor80039242011-02-15 20:33:25 +00002581 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2582 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2583 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002584 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002585 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002586 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002587 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002588 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002589 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002590
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002591 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002592 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002593 }
2594
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002595 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002596 // C++ scope specifier. Annotate and loop, or bail out on error.
Eli Friedman2a1d9a92013-08-15 23:59:20 +00002597 if (TryAnnotateCXXScopeToken(EnteringContext)) {
John McCall1f476a12010-02-26 08:45:28 +00002598 if (!DS.hasTypeSpecifier())
2599 DS.SetTypeSpecError();
2600 goto DoneWithDeclSpec;
2601 }
John McCall8bc2a702010-03-01 18:20:46 +00002602 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2603 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002604 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002605
2606 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002607 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002608 goto DoneWithDeclSpec;
2609
John McCall9dab4e62009-12-12 11:40:51 +00002610 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002611 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2612 Tok.getAnnotationRange(),
2613 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002614
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002615 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002616 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002617 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002618 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002619 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002620 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002621
2622 // C++ [class.qual]p2:
2623 // In a lookup in which the constructor is an acceptable lookup
2624 // result and the nested-name-specifier nominates a class C:
2625 //
2626 // - if the name specified after the
2627 // nested-name-specifier, when looked up in C, is the
2628 // injected-class-name of C (Clause 9), or
2629 //
2630 // - if the name specified after the nested-name-specifier
2631 // is the same as the identifier or the
2632 // simple-template-id's template-name in the last
2633 // component of the nested-name-specifier,
2634 //
2635 // the name is instead considered to name the constructor of
2636 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002637 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002638 // Thus, if the template-name is actually the constructor
2639 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002640 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002641 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002642 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCall84821e72010-04-13 06:39:49 +00002643 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002644 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Richard Smith446161b2014-03-03 21:12:53 +00002645 if (isConstructorDeclarator(/*Unqualified*/false)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002646 // The user meant this to be an out-of-line constructor
2647 // definition, but template arguments are not allowed
2648 // there. Just allow this as a constructor; we'll
2649 // complain about it later.
2650 goto DoneWithDeclSpec;
2651 }
2652
2653 // The user meant this to name a type, but it actually names
2654 // a constructor with some extraneous template
2655 // arguments. Complain, then parse it as a type as the user
2656 // intended.
2657 Diag(TemplateId->TemplateNameLoc,
2658 diag::err_out_of_line_template_id_names_constructor)
2659 << TemplateId->Name;
2660 }
2661
John McCall9dab4e62009-12-12 11:40:51 +00002662 DS.getTypeSpecScope() = SS;
2663 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002664 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002665 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002666 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002667 continue;
2668 }
2669
Douglas Gregorc5790df2009-09-28 07:26:33 +00002670 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002671 DS.getTypeSpecScope() = SS;
2672 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002673 if (Tok.getAnnotationValue()) {
2674 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002675 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002676 Tok.getAnnotationEndLoc(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002677 PrevSpec, DiagID, T, Policy);
Richard Smithda837032012-09-14 18:27:01 +00002678 if (isInvalid)
2679 break;
John McCallba7bf592010-08-24 05:47:05 +00002680 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002681 else
2682 DS.SetTypeSpecError();
2683 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2684 ConsumeToken(); // The typename
2685 }
2686
Douglas Gregor167fa622009-03-25 15:40:00 +00002687 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002688 goto DoneWithDeclSpec;
2689
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002690 // If we're in a context where the identifier could be a class name,
2691 // check whether this is a constructor declaration.
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00002692 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002693 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002694 &SS)) {
Richard Smith446161b2014-03-03 21:12:53 +00002695 if (isConstructorDeclarator(/*Unqualified*/false))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002696 goto DoneWithDeclSpec;
2697
2698 // As noted in C++ [class.qual]p2 (cited above), when the name
2699 // of the class is qualified in a context where it could name
2700 // a constructor, its a constructor name. However, we've
2701 // looked at the declarator, and the user probably meant this
2702 // to be a type. Complain that it isn't supposed to be treated
2703 // as a type, then proceed to parse it as a type.
2704 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2705 << Next.getIdentifierInfo();
2706 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002707
John McCallba7bf592010-08-24 05:47:05 +00002708 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2709 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002710 getCurScope(), &SS,
2711 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002712 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002713 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002714
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002715 // If the referenced identifier is not a type, then this declspec is
2716 // erroneous: We already checked about that it has no type specifier, and
2717 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002718 // typename.
David Blaikie7d170102013-05-15 07:37:26 +00002719 if (!TypeRep) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002720 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002721 ParsedAttributesWithRange Attrs(AttrFactory);
2722 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2723 if (!Attrs.empty()) {
2724 AttrsLastTime = true;
2725 attrs.takeAllFrom(Attrs);
2726 }
2727 continue;
2728 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002729 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002730 }
Mike Stump11289f42009-09-09 15:08:12 +00002731
John McCall9dab4e62009-12-12 11:40:51 +00002732 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002733 ConsumeToken(); // The C++ scope.
2734
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002735 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002736 DiagID, TypeRep, Policy);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002737 if (isInvalid)
2738 break;
Mike Stump11289f42009-09-09 15:08:12 +00002739
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002740 DS.SetRangeEnd(Tok.getLocation());
2741 ConsumeToken(); // The typename.
2742
2743 continue;
2744 }
Mike Stump11289f42009-09-09 15:08:12 +00002745
Chris Lattnere387d9e2009-01-21 19:48:37 +00002746 case tok::annot_typename: {
Richard Smith404dfb42013-11-19 22:47:36 +00002747 // If we've previously seen a tag definition, we were almost surely
2748 // missing a semicolon after it.
2749 if (DS.hasTypeSpecifier() && DS.hasTagDefinition())
2750 goto DoneWithDeclSpec;
2751
John McCallba7bf592010-08-24 05:47:05 +00002752 if (Tok.getAnnotationValue()) {
2753 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002754 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002755 DiagID, T, Policy);
John McCallba7bf592010-08-24 05:47:05 +00002756 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002757 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002758
Chris Lattner005fc1b2010-04-05 18:18:31 +00002759 if (isInvalid)
2760 break;
2761
Chris Lattnere387d9e2009-01-21 19:48:37 +00002762 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2763 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002764
Chris Lattnere387d9e2009-01-21 19:48:37 +00002765 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2766 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002767 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002768 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002769 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002770
Chris Lattnere387d9e2009-01-21 19:48:37 +00002771 continue;
2772 }
Mike Stump11289f42009-09-09 15:08:12 +00002773
Douglas Gregor06873092011-04-28 15:48:45 +00002774 case tok::kw___is_signed:
2775 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2776 // typically treats it as a trait. If we see __is_signed as it appears
2777 // in libstdc++, e.g.,
2778 //
2779 // static const bool __is_signed;
2780 //
2781 // then treat __is_signed as an identifier rather than as a keyword.
2782 if (DS.getTypeSpecType() == TST_bool &&
2783 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
Alp Toker47642d22013-12-03 06:13:01 +00002784 DS.getStorageClassSpec() == DeclSpec::SCS_static)
2785 TryKeywordIdentFallback(true);
Douglas Gregor06873092011-04-28 15:48:45 +00002786
2787 // We're done with the declaration-specifiers.
2788 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002789
Chris Lattner16fac4f2008-07-26 01:18:38 +00002790 // typedef-name
Nikola Smiljanic67860242014-09-26 00:28:20 +00002791 case tok::kw___super:
David Blaikie15a430a2011-12-04 05:04:18 +00002792 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002793 case tok::identifier: {
Reid Klecknerc582f012014-07-14 18:19:58 +00002794 // This identifier can only be a typedef name if we haven't already seen
2795 // a type-specifier. Without this check we misparse:
2796 // typedef int X; struct Y { short X; }; as 'short int'.
2797 if (DS.hasTypeSpecifier())
2798 goto DoneWithDeclSpec;
2799
Serge Pavlov458ea762014-07-16 05:16:52 +00002800 // In C++, check to see if this is a scope specifier like foo::bar::, if
2801 // so handle it as such. This is important for ctor parsing.
2802 if (getLangOpts().CPlusPlus) {
2803 if (TryAnnotateCXXScopeToken(EnteringContext)) {
2804 DS.SetTypeSpecError();
2805 goto DoneWithDeclSpec;
2806 }
2807 if (!Tok.is(tok::identifier))
2808 continue;
2809 }
2810
John Thompson22334602010-02-05 00:12:22 +00002811 // Check for need to substitute AltiVec keyword tokens.
2812 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2813 break;
2814
Richard Smith3092a3b2012-05-09 18:56:43 +00002815 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2816 // allow the use of a typedef name as a type specifier.
2817 if (DS.isTypeAltiVecVector())
2818 goto DoneWithDeclSpec;
2819
John McCallba7bf592010-08-24 05:47:05 +00002820 ParsedType TypeRep =
2821 Actions.getTypeName(*Tok.getIdentifierInfo(),
2822 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002823
Reid Klecknerdf6e4a02014-06-06 22:36:36 +00002824 // MSVC: If we weren't able to parse a default template argument, and it's
2825 // just a simple identifier, create a DependentNameType. This will allow us
2826 // to defer the name lookup to template instantiation time, as long we forge a
2827 // NestedNameSpecifier for the current context.
2828 if (!TypeRep && DSContext == DSC_template_type_arg &&
2829 getLangOpts().MSVCCompat && getCurScope()->isTemplateParamScope()) {
2830 TypeRep = Actions.ActOnDelayedDefaultTemplateArg(
2831 *Tok.getIdentifierInfo(), Tok.getLocation());
2832 }
2833
Chris Lattner6cc055a2009-04-12 20:42:31 +00002834 // If this is not a typedef name, don't parse it as part of the declspec,
2835 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002836 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002837 ParsedAttributesWithRange Attrs(AttrFactory);
Craig Topper161e4db2014-05-21 06:02:52 +00002838 if (ParseImplicitInt(DS, nullptr, TemplateInfo, AS, DSContext, Attrs)) {
Michael Han9407e502012-11-26 22:54:45 +00002839 if (!Attrs.empty()) {
2840 AttrsLastTime = true;
2841 attrs.takeAllFrom(Attrs);
2842 }
2843 continue;
2844 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002845 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002846 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002847
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002848 // If we're in a context where the identifier could be a class name,
2849 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002850 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002851 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Richard Smith446161b2014-03-03 21:12:53 +00002852 isConstructorDeclarator(/*Unqualified*/true))
Douglas Gregor61956c42008-10-31 09:07:45 +00002853 goto DoneWithDeclSpec;
2854
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002855 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002856 DiagID, TypeRep, Policy);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002857 if (isInvalid)
2858 break;
Mike Stump11289f42009-09-09 15:08:12 +00002859
Chris Lattner16fac4f2008-07-26 01:18:38 +00002860 DS.SetRangeEnd(Tok.getLocation());
2861 ConsumeToken(); // The identifier
2862
2863 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2864 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002865 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002866 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002867 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002868
Steve Naroffcd5e7822008-09-22 10:28:57 +00002869 // Need to support trailing type qualifiers (e.g. "id<p> const").
2870 // If a type specifier follows, it will be diagnosed elsewhere.
2871 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002872 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002873
2874 // type-name
2875 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002876 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002877 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002878 // This template-id does not refer to a type name, so we're
2879 // done with the type-specifiers.
2880 goto DoneWithDeclSpec;
2881 }
2882
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002883 // If we're in a context where the template-id could be a
2884 // constructor name or specialization, check whether this is a
2885 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002886 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002887 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Richard Smith446161b2014-03-03 21:12:53 +00002888 isConstructorDeclarator(TemplateId->SS.isEmpty()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002889 goto DoneWithDeclSpec;
2890
Douglas Gregor7f741122009-02-25 19:37:18 +00002891 // Turn the template-id annotation token into a type annotation
2892 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002893 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002894 continue;
2895 }
2896
Chris Lattnere37e2332006-08-15 04:50:22 +00002897 // GNU attributes support.
2898 case tok::kw___attribute:
Craig Topper161e4db2014-05-21 06:02:52 +00002899 ParseGNUAttributes(DS.getAttributes(), nullptr, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002900 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002901
2902 // Microsoft declspec support.
2903 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002904 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002905 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002906
Steve Naroff44ac7772008-12-25 14:16:32 +00002907 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002908 case tok::kw___forceinline: {
Serge Pavlov750db652013-11-13 06:57:53 +00002909 isInvalid = DS.setFunctionSpecForceInline(Loc, PrevSpec, DiagID);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002910 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002911 SourceLocation AttrNameLoc = Tok.getLocation();
Craig Topper161e4db2014-05-21 06:02:52 +00002912 DS.getAttributes().addNew(AttrName, AttrNameLoc, nullptr, AttrNameLoc,
2913 nullptr, 0, AttributeList::AS_Keyword);
Richard Smithda837032012-09-14 18:27:01 +00002914 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002915 }
Eli Friedman53339e02009-06-08 23:27:34 +00002916
Aaron Ballman317a77f2013-05-22 23:25:32 +00002917 case tok::kw___sptr:
2918 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00002919 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002920 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002921 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002922 case tok::kw___cdecl:
2923 case tok::kw___stdcall:
2924 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002925 case tok::kw___thiscall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00002926 case tok::kw___vectorcall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002927 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002928 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002929 continue;
2930
Dawn Perchik335e16b2010-09-03 01:29:35 +00002931 // Borland single token adornments.
2932 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002933 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002934 continue;
2935
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002936 // OpenCL single token adornments.
2937 case tok::kw___kernel:
2938 ParseOpenCLAttributes(DS.getAttributes());
2939 continue;
2940
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002941 // storage-class-specifier
2942 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002943 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002944 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002945 break;
2946 case tok::kw_extern:
Richard Smithb4a9e862013-04-12 22:46:28 +00002947 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002948 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002949 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002950 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002951 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002952 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002953 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002954 Loc, PrevSpec, DiagID, Policy);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002955 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002956 case tok::kw_static:
Richard Smithb4a9e862013-04-12 22:46:28 +00002957 if (DS.getThreadStorageClassSpec() == DeclSpec::TSCS___thread)
Chris Lattner6d29c102008-11-18 07:48:38 +00002958 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002959 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002960 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002961 break;
2962 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002963 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002964 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002965 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002966 PrevSpec, DiagID, Policy);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002967 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002968 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002969 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002970 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002971 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002972 DiagID, Policy);
Richard Smith58c74332011-09-04 19:54:14 +00002973 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002974 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002975 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002976 break;
2977 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002978 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002979 PrevSpec, DiagID, Policy);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002980 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002981 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002982 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00002983 PrevSpec, DiagID, Policy);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002984 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002985 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00002986 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS___thread, Loc,
2987 PrevSpec, DiagID);
2988 break;
2989 case tok::kw_thread_local:
2990 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS_thread_local, Loc,
2991 PrevSpec, DiagID);
2992 break;
2993 case tok::kw__Thread_local:
2994 isInvalid = DS.SetStorageClassSpecThread(DeclSpec::TSCS__Thread_local,
2995 Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002996 break;
Mike Stump11289f42009-09-09 15:08:12 +00002997
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002998 // function-specifier
2999 case tok::kw_inline:
Serge Pavlov750db652013-11-13 06:57:53 +00003000 isInvalid = DS.setFunctionSpecInline(Loc, PrevSpec, DiagID);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003001 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00003002 case tok::kw_virtual:
Serge Pavlov750db652013-11-13 06:57:53 +00003003 isInvalid = DS.setFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00003004 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00003005 case tok::kw_explicit:
Serge Pavlov750db652013-11-13 06:57:53 +00003006 isInvalid = DS.setFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregor61956c42008-10-31 09:07:45 +00003007 break;
Richard Smith0015f092013-01-17 22:16:11 +00003008 case tok::kw__Noreturn:
3009 if (!getLangOpts().C11)
3010 Diag(Loc, diag::ext_c11_noreturn);
Serge Pavlov750db652013-11-13 06:57:53 +00003011 isInvalid = DS.setFunctionSpecNoreturn(Loc, PrevSpec, DiagID);
Richard Smith0015f092013-01-17 22:16:11 +00003012 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003013
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003014 // alignment-specifier
3015 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003016 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00003017 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00003018 ParseAlignmentSpecifier(DS.getAttributes());
3019 continue;
3020
Anders Carlssoncd8db412009-05-06 04:46:28 +00003021 // friend
3022 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00003023 if (DSContext == DSC_class)
3024 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
3025 else {
3026 PrevSpec = ""; // not actually used by the diagnostic
3027 DiagID = diag::err_friend_invalid_in_context;
3028 isInvalid = true;
3029 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00003030 break;
Mike Stump11289f42009-09-09 15:08:12 +00003031
Douglas Gregor26701a42011-09-09 02:06:17 +00003032 // Modules
3033 case tok::kw___module_private__:
3034 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
3035 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003036
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00003037 // constexpr
3038 case tok::kw_constexpr:
3039 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
3040 break;
3041
Chris Lattnere387d9e2009-01-21 19:48:37 +00003042 // type-specifier
3043 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00003044 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003045 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003046 break;
3047 case tok::kw_long:
3048 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00003049 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003050 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003051 else
John McCall49bfce42009-08-03 20:12:06 +00003052 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003053 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003054 break;
Francois Pichet84133e42011-04-28 01:59:37 +00003055 case tok::kw___int64:
3056 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003057 DiagID, Policy);
Francois Pichet84133e42011-04-28 01:59:37 +00003058 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003059 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00003060 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
3061 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003062 break;
3063 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00003064 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
3065 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003066 break;
3067 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00003068 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
3069 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003070 break;
3071 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00003072 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
3073 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003074 break;
3075 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00003076 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003077 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003078 break;
3079 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00003080 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003081 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003082 break;
3083 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00003084 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003085 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003086 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00003087 case tok::kw___int128:
3088 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003089 DiagID, Policy);
Richard Smithf016bbc2012-04-04 06:24:32 +00003090 break;
3091 case tok::kw_half:
3092 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003093 DiagID, Policy);
Richard Smithf016bbc2012-04-04 06:24:32 +00003094 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003095 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00003096 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003097 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003098 break;
3099 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00003100 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003101 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003102 break;
3103 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00003104 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003105 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003106 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003107 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00003108 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003109 DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003110 break;
3111 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00003112 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003113 DiagID, Policy);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003114 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003115 case tok::kw_bool:
3116 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003117 if (Tok.is(tok::kw_bool) &&
3118 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
3119 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
3120 PrevSpec = ""; // Not used by the diagnostic.
3121 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003122 // For better error recovery.
3123 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003124 isInvalid = true;
3125 } else {
3126 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003127 DiagID, Policy);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00003128 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003129 break;
3130 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00003131 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003132 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003133 break;
3134 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00003135 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003136 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003137 break;
3138 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00003139 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003140 DiagID, Policy);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003141 break;
John Thompson22334602010-02-05 00:12:22 +00003142 case tok::kw___vector:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003143 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
John Thompson22334602010-02-05 00:12:22 +00003144 break;
3145 case tok::kw___pixel:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003146 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
John Thompson22334602010-02-05 00:12:22 +00003147 break;
John McCall39439732011-04-09 22:50:59 +00003148 case tok::kw___unknown_anytype:
3149 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003150 PrevSpec, DiagID, Policy);
John McCall39439732011-04-09 22:50:59 +00003151 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00003152
3153 // class-specifier:
3154 case tok::kw_class:
3155 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003156 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003157 case tok::kw_union: {
3158 tok::TokenKind Kind = Tok.getKind();
3159 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00003160
3161 // These are attributes following class specifiers.
3162 // To produce better diagnostic, we parse them when
3163 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00003164 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00003165 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00003166 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003167
3168 // If there are attributes following class specifier,
3169 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00003170 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00003171 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00003172 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00003173 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003174 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003175 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00003176
3177 // enum-specifier:
3178 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003179 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00003180 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00003181 continue;
3182
3183 // cv-qualifier:
3184 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00003185 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003186 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003187 break;
3188 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00003189 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003190 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003191 break;
3192 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00003193 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00003194 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00003195 break;
3196
Douglas Gregor333489b2009-03-27 23:10:48 +00003197 // C++ typename-specifier:
3198 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00003199 if (TryAnnotateTypeOrScopeToken()) {
3200 DS.SetTypeSpecError();
3201 goto DoneWithDeclSpec;
3202 }
3203 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00003204 continue;
3205 break;
3206
Chris Lattnere387d9e2009-01-21 19:48:37 +00003207 // GNU typeof support.
3208 case tok::kw_typeof:
3209 ParseTypeofSpecifier(DS);
3210 continue;
3211
David Blaikie15a430a2011-12-04 05:04:18 +00003212 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00003213 ParseDecltypeSpecifier(DS);
3214 continue;
3215
Alexis Hunt4a257072011-05-19 05:37:45 +00003216 case tok::kw___underlying_type:
3217 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00003218 continue;
3219
3220 case tok::kw__Atomic:
Richard Smith8e1ac332013-03-28 01:55:44 +00003221 // C11 6.7.2.4/4:
3222 // If the _Atomic keyword is immediately followed by a left parenthesis,
3223 // it is interpreted as a type specifier (with a type name), not as a
3224 // type qualifier.
3225 if (NextToken().is(tok::l_paren)) {
3226 ParseAtomicSpecifier(DS);
3227 continue;
3228 }
3229 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
3230 getLangOpts());
3231 break;
Alexis Hunt4a257072011-05-19 05:37:45 +00003232
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003233 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003234 case tok::kw___private:
3235 case tok::kw___global:
3236 case tok::kw___local:
3237 case tok::kw___constant:
3238 case tok::kw___read_only:
3239 case tok::kw___write_only:
3240 case tok::kw___read_write:
Aaron Ballman05d76ea2014-01-14 01:29:54 +00003241 ParseOpenCLQualifiers(DS.getAttributes());
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003242 break;
Chad Rosierc1183952012-06-26 22:30:43 +00003243
Steve Naroffcfdf6162008-06-05 00:02:44 +00003244 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00003245 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00003246 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
3247 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003248 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00003249 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00003250
Douglas Gregor3a001f42010-11-19 17:10:50 +00003251 if (!ParseObjCProtocolQualifiers(DS))
3252 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
3253 << FixItHint::CreateInsertion(Loc, "id")
3254 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00003255
Douglas Gregor06e41ae2010-10-21 23:17:00 +00003256 // Need to support trailing type qualifiers (e.g. "id<p> const").
3257 // If a type specifier follows, it will be diagnosed elsewhere.
3258 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003259 }
John McCall49bfce42009-08-03 20:12:06 +00003260 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003261 if (isInvalid) {
3262 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00003263 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00003264
Douglas Gregora05f5ab2010-08-23 14:34:43 +00003265 if (DiagID == diag::ext_duplicate_declspec)
3266 Diag(Tok, DiagID)
3267 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
3268 else
3269 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00003270 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00003271
Chris Lattner2e232092008-03-13 06:29:04 +00003272 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00003273 if (DiagID != diag::err_bool_redeclaration)
3274 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003275
3276 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00003277 }
3278}
Douglas Gregoreb31f392008-12-01 23:54:00 +00003279
Chris Lattner70ae4912007-10-29 04:42:53 +00003280/// ParseStructDeclaration - Parse a struct declaration without the terminating
3281/// semicolon.
3282///
Chris Lattner90a26b02007-01-23 04:38:16 +00003283/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00003284/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00003285/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00003286/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00003287/// struct-declarator-list:
3288/// struct-declarator
3289/// struct-declarator-list ',' struct-declarator
3290/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
3291/// struct-declarator:
3292/// declarator
3293/// [GNU] declarator attributes[opt]
3294/// declarator[opt] ':' constant-expression
3295/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
3296///
Benjamin Kramera39beb92014-09-03 11:06:10 +00003297void Parser::ParseStructDeclaration(
3298 ParsingDeclSpec &DS,
3299 llvm::function_ref<void(ParsingFieldDeclarator &)> FieldsCallback) {
Chad Rosierc1183952012-06-26 22:30:43 +00003300
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003301 if (Tok.is(tok::kw___extension__)) {
3302 // __extension__ silences extension warnings in the subexpression.
3303 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00003304 ConsumeToken();
Benjamin Kramera39beb92014-09-03 11:06:10 +00003305 return ParseStructDeclaration(DS, FieldsCallback);
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00003306 }
Mike Stump11289f42009-09-09 15:08:12 +00003307
Steve Naroff97170802007-08-20 22:28:22 +00003308 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00003309 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00003310
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00003311 // If there are no declarators, this is a free-standing declaration
3312 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00003313 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003314 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
3315 DS);
3316 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00003317 return;
3318 }
3319
3320 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00003321 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00003322 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00003323 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003324 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00003325 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00003326
Bill Wendling44426052012-12-20 19:22:21 +00003327 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00003328 if (!FirstDeclarator)
3329 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00003330
Steve Naroff97170802007-08-20 22:28:22 +00003331 /// struct-declarator: declarator
3332 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00003333 if (Tok.isNot(tok::colon)) {
3334 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
3335 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00003336 ParseDeclarator(DeclaratorInfo.D);
Richard Smith3d1a94c2014-08-12 00:22:39 +00003337 } else
3338 DeclaratorInfo.D.SetIdentifier(nullptr, Tok.getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00003339
Alp Toker8fbec672013-12-17 23:29:36 +00003340 if (TryConsumeToken(tok::colon)) {
John McCalldadc5752010-08-24 06:29:42 +00003341 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003342 if (Res.isInvalid())
Alexey Bataevee6507d2013-11-18 08:17:37 +00003343 SkipUntil(tok::semi, StopBeforeMatch);
Chris Lattner32295d32008-04-10 06:15:14 +00003344 else
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003345 DeclaratorInfo.BitfieldSize = Res.get();
Steve Naroff97170802007-08-20 22:28:22 +00003346 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003347
Steve Naroff97170802007-08-20 22:28:22 +00003348 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003349 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003350
John McCallcfefb6d2009-11-03 02:38:08 +00003351 // We're done with this declarator; invoke the callback.
Benjamin Kramera39beb92014-09-03 11:06:10 +00003352 FieldsCallback(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00003353
Steve Naroff97170802007-08-20 22:28:22 +00003354 // If we don't have a comma, it is either the end of the list (a ';')
3355 // or an error, bail out.
Alp Toker8fbec672013-12-17 23:29:36 +00003356 if (!TryConsumeToken(tok::comma, CommaLoc))
Chris Lattner70ae4912007-10-29 04:42:53 +00003357 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00003358
John McCallcfefb6d2009-11-03 02:38:08 +00003359 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003360 }
Steve Naroff97170802007-08-20 22:28:22 +00003361}
3362
3363/// ParseStructUnionBody
3364/// struct-contents:
3365/// struct-declaration-list
3366/// [EXT] empty
3367/// [GNU] "struct-declaration-list" without terminatoring ';'
3368/// struct-declaration-list:
3369/// struct-declaration
3370/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003371/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003372///
Chris Lattner1300fb92007-01-23 23:42:53 +00003373void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003374 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003375 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3376 "parsing struct/union body");
Andy Gibbs22e140b2013-04-03 09:31:19 +00003377 assert(!getLangOpts().CPlusPlus && "C++ declarations not supported");
Mike Stump11289f42009-09-09 15:08:12 +00003378
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003379 BalancedDelimiterTracker T(*this, tok::l_brace);
3380 if (T.consumeOpen())
3381 return;
Mike Stump11289f42009-09-09 15:08:12 +00003382
Douglas Gregor658b9552009-01-09 22:42:13 +00003383 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003384 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003385
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003386 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003387
Chris Lattner7b9ace62007-01-23 20:11:08 +00003388 // While we still have something to read, read the declarations in the struct.
Richard Smith34f30512013-11-23 04:06:09 +00003389 while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003390 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003391
Chris Lattner736ed5d2007-06-09 05:59:07 +00003392 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003393 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003394 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003395 continue;
3396 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003397
Andy Gibbsc804e082013-04-03 09:46:04 +00003398 // Parse _Static_assert declaration.
3399 if (Tok.is(tok::kw__Static_assert)) {
3400 SourceLocation DeclEnd;
3401 ParseStaticAssertDeclaration(DeclEnd);
3402 continue;
3403 }
3404
Argyrios Kyrtzidis71c12fb2013-04-18 01:42:35 +00003405 if (Tok.is(tok::annot_pragma_pack)) {
3406 HandlePragmaPack();
3407 continue;
3408 }
3409
3410 if (Tok.is(tok::annot_pragma_align)) {
3411 HandlePragmaAlign();
3412 continue;
3413 }
3414
John McCallcfefb6d2009-11-03 02:38:08 +00003415 if (!Tok.is(tok::at)) {
Benjamin Kramera39beb92014-09-03 11:06:10 +00003416 auto CFieldCallback = [&](ParsingFieldDeclarator &FD) {
3417 // Install the declarator into the current TagDecl.
3418 Decl *Field =
3419 Actions.ActOnField(getCurScope(), TagDecl,
3420 FD.D.getDeclSpec().getSourceRange().getBegin(),
3421 FD.D, FD.BitfieldSize);
3422 FieldDecls.push_back(Field);
3423 FD.complete(Field);
3424 };
John McCallcfefb6d2009-11-03 02:38:08 +00003425
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003426 // Parse all the comma separated declarators.
3427 ParsingDeclSpec DS(*this);
Benjamin Kramera39beb92014-09-03 11:06:10 +00003428 ParseStructDeclaration(DS, CFieldCallback);
Chris Lattner535b8302008-06-21 19:39:06 +00003429 } else { // Handle @defs
3430 ConsumeToken();
3431 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3432 Diag(Tok, diag::err_unexpected_at);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003433 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003434 continue;
3435 }
3436 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00003437 ExpectAndConsume(tok::l_paren);
Chris Lattner535b8302008-06-21 19:39:06 +00003438 if (!Tok.is(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00003439 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00003440 SkipUntil(tok::semi);
Chris Lattner535b8302008-06-21 19:39:06 +00003441 continue;
3442 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003443 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003444 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003445 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003446 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3447 ConsumeToken();
Alp Toker383d2c42014-01-01 03:08:43 +00003448 ExpectAndConsume(tok::r_paren);
Mike Stump11289f42009-09-09 15:08:12 +00003449 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003450
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003451 if (TryConsumeToken(tok::semi))
3452 continue;
3453
3454 if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003455 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003456 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003457 }
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003458
3459 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3460 // Skip to end of block or statement to avoid ext-warning on extra ';'.
3461 SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
3462 // If we stopped at a ';', eat it.
3463 TryConsumeToken(tok::semi);
Chris Lattner90a26b02007-01-23 04:38:16 +00003464 }
Mike Stump11289f42009-09-09 15:08:12 +00003465
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003466 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003467
John McCall084e83d2011-03-24 11:26:52 +00003468 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003469 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003470 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003471
Douglas Gregor0be31a22010-07-02 17:43:08 +00003472 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003473 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003474 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003475 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003476 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003477 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3478 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003479}
3480
Chris Lattner3b561a32006-08-13 00:12:11 +00003481/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003482/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003483/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003484///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003485/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3486/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003487/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3488/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003489/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003490/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003491///
Richard Smith7d137e32012-03-23 03:33:32 +00003492/// [C++11] enum-head '{' enumerator-list[opt] '}'
3493/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003494///
Richard Smith7d137e32012-03-23 03:33:32 +00003495/// enum-head: [C++11]
3496/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3497/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3498/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003499///
Richard Smith7d137e32012-03-23 03:33:32 +00003500/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003501/// 'enum'
3502/// 'enum' 'class'
3503/// 'enum' 'struct'
3504///
Richard Smith7d137e32012-03-23 03:33:32 +00003505/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003506/// ':' type-specifier-seq
3507///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003508/// [C++] elaborated-type-specifier:
3509/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3510///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003511void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003512 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003513 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003514 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003515 if (Tok.is(tok::code_completion)) {
3516 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003517 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003518 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003519 }
John McCallcb432fa2011-07-06 05:58:41 +00003520
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003521 // If attributes exist after tag, parse them.
3522 ParsedAttributesWithRange attrs(AttrFactory);
3523 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003524 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003525
3526 // If declspecs exist after tag, parse them.
3527 while (Tok.is(tok::kw___declspec))
3528 ParseMicrosoftDeclSpec(attrs);
3529
Richard Smith0f8ee222012-01-10 01:33:14 +00003530 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003531 bool IsScopedUsingClassTag = false;
3532
John McCallbeae29a2012-06-23 22:30:04 +00003533 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Trieud0d87b52013-04-23 02:47:36 +00003534 if (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct)) {
3535 Diag(Tok, getLangOpts().CPlusPlus11 ? diag::warn_cxx98_compat_scoped_enum
3536 : diag::ext_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003537 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003538 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003539
Bill Wendling44426052012-12-20 19:22:21 +00003540 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003541 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003542 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003543
3544 // They are allowed afterwards, though.
3545 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003546 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003547 while (Tok.is(tok::kw___declspec))
3548 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003549 }
Richard Smith7d137e32012-03-23 03:33:32 +00003550
John McCall6347b682012-05-07 06:16:58 +00003551 // C++11 [temp.explicit]p12:
3552 // The usual access controls do not apply to names used to specify
3553 // explicit instantiations.
3554 // We extend this to also cover explicit specializations. Note that
3555 // we don't suppress if this turns out to be an elaborated type
3556 // specifier.
3557 bool shouldDelayDiagsInTag =
3558 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3559 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3560 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003561
Richard Smithbfdb1082012-03-12 08:56:40 +00003562 // Enum definitions should not be parsed in a trailing-return-type.
3563 bool AllowDeclaration = DSC != DSC_trailing;
3564
3565 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003566 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003567 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003568
Abramo Bagnarad7548482010-05-19 21:37:53 +00003569 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003570 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003571 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3572 // if a fixed underlying type is allowed.
3573 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003574
3575 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Richard Smith1d4b2e12013-04-01 21:43:41 +00003576 /*EnteringContext=*/true))
John McCall1f476a12010-02-26 08:45:28 +00003577 return;
3578
3579 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00003580 Diag(Tok, diag::err_expected) << tok::identifier;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003581 if (Tok.isNot(tok::l_brace)) {
3582 // Has no name and is not a definition.
3583 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003584 SkipUntil(tok::comma, StopAtSemi);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003585 return;
3586 }
3587 }
3588 }
Mike Stump11289f42009-09-09 15:08:12 +00003589
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003590 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003591 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003592 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Alp Tokerec543272013-12-24 09:48:30 +00003593 Diag(Tok, diag::err_expected_either) << tok::identifier << tok::l_brace;
Mike Stump11289f42009-09-09 15:08:12 +00003594
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003595 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003596 SkipUntil(tok::comma, StopAtSemi);
Chris Lattner3b561a32006-08-13 00:12:11 +00003597 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003598 }
Mike Stump11289f42009-09-09 15:08:12 +00003599
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003600 // If an identifier is present, consume and remember it.
Craig Topper161e4db2014-05-21 06:02:52 +00003601 IdentifierInfo *Name = nullptr;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003602 SourceLocation NameLoc;
3603 if (Tok.is(tok::identifier)) {
3604 Name = Tok.getIdentifierInfo();
3605 NameLoc = ConsumeToken();
3606 }
Mike Stump11289f42009-09-09 15:08:12 +00003607
Richard Smith0f8ee222012-01-10 01:33:14 +00003608 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003609 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3610 // declaration of a scoped enumeration.
3611 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003612 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003613 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003614 }
3615
John McCall6347b682012-05-07 06:16:58 +00003616 // Okay, end the suppression area. We'll decide whether to emit the
3617 // diagnostics in a second.
3618 if (shouldDelayDiagsInTag)
3619 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003620
Douglas Gregor0bf31402010-10-08 23:50:27 +00003621 TypeResult BaseType;
3622
Douglas Gregord1f69f62010-12-01 17:42:47 +00003623 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003624 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003625 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003626 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003627 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003628 // If we're in class scope, this can either be an enum declaration with
3629 // an underlying type, or a declaration of a bitfield member. We try to
3630 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003631 // (integer literal, sizeof); if it's still ambiguous, we then consider
3632 // anything that's a simple-type-specifier followed by '(' as an
3633 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003634 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003635 EnterExpressionEvaluationContext Unevaluated(Actions,
3636 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003637 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003638 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003639 // bit-field. This is the common case.
Richard Smithee390432014-05-16 01:56:53 +00003640 if (TPR == TPResult::True)
Douglas Gregord1f69f62010-12-01 17:42:47 +00003641 PossibleBitfield = true;
3642 // If the next token starts a type-specifier-seq, it may be either a
3643 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003644 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003645 // fixed underlying type.
Richard Smithee390432014-05-16 01:56:53 +00003646 else if (TPR == TPResult::False &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003647 GetLookAheadToken(2).getKind() == tok::semi) {
3648 // Consume the ':'.
3649 ConsumeToken();
3650 } else {
3651 // We have the start of a type-specifier-seq, so we have to perform
3652 // tentative parsing to determine whether we have an expression or a
3653 // type.
3654 TentativeParsingAction TPA(*this);
3655
3656 // Consume the ':'.
3657 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003658
3659 // If we see a type specifier followed by an open-brace, we have an
3660 // ambiguity between an underlying type and a C++11 braced
3661 // function-style cast. Resolve this by always treating it as an
3662 // underlying type.
3663 // FIXME: The standard is not entirely clear on how to disambiguate in
3664 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003665 if ((getLangOpts().CPlusPlus &&
Richard Smithee390432014-05-16 01:56:53 +00003666 isCXXDeclarationSpecifier(TPResult::True) != TPResult::True) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003667 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003668 // We'll parse this as a bitfield later.
3669 PossibleBitfield = true;
3670 TPA.Revert();
3671 } else {
3672 // We have a type-specifier-seq.
3673 TPA.Commit();
3674 }
3675 }
3676 } else {
3677 // Consume the ':'.
3678 ConsumeToken();
3679 }
3680
3681 if (!PossibleBitfield) {
3682 SourceRange Range;
3683 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003684
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003685 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003686 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003687 } else if (!getLangOpts().ObjC2) {
3688 if (getLangOpts().CPlusPlus)
3689 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3690 else
3691 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3692 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003693 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003694 }
3695
Richard Smith0f8ee222012-01-10 01:33:14 +00003696 // There are four options here. If we have 'friend enum foo;' then this is a
3697 // friend declaration, and cannot have an accompanying definition. If we have
3698 // 'enum foo;', then this is a forward declaration. If we have
3699 // 'enum foo {...' then this is a definition. Otherwise we have something
3700 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003701 //
3702 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3703 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3704 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3705 //
John McCallfaf5fb42010-08-26 23:41:50 +00003706 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003707 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003708 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003709 } else if (Tok.is(tok::l_brace)) {
3710 if (DS.isFriendSpecified()) {
3711 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3712 << SourceRange(DS.getFriendSpecLoc());
3713 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003714 SkipUntil(tok::r_brace, StopAtSemi);
John McCall6347b682012-05-07 06:16:58 +00003715 TUK = Sema::TUK_Friend;
3716 } else {
3717 TUK = Sema::TUK_Definition;
3718 }
Richard Smith649c7b062014-01-08 00:56:48 +00003719 } else if (!isTypeSpecifier(DSC) &&
Richard Smith369b9f92012-06-25 21:37:02 +00003720 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003721 (Tok.isAtStartOfLine() &&
3722 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003723 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3724 if (Tok.isNot(tok::semi)) {
3725 // A semicolon was missing after this declaration. Diagnose and recover.
Alp Toker383d2c42014-01-01 03:08:43 +00003726 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00003727 PP.EnterToken(Tok);
3728 Tok.setKind(tok::semi);
3729 }
John McCall6347b682012-05-07 06:16:58 +00003730 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003731 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003732 }
3733
3734 // If this is an elaborated type specifier, and we delayed
3735 // diagnostics before, just merge them into the current pool.
3736 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3737 diagsFromTag.redelay();
3738 }
Richard Smith7d137e32012-03-23 03:33:32 +00003739
3740 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003741 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003742 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003743 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003744 // Skip the rest of this declarator, up until the comma or semicolon.
3745 Diag(Tok, diag::err_enum_template);
Alexey Bataevee6507d2013-11-18 08:17:37 +00003746 SkipUntil(tok::comma, StopAtSemi);
Richard Smith7d137e32012-03-23 03:33:32 +00003747 return;
3748 }
3749
3750 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3751 // Enumerations can't be explicitly instantiated.
3752 DS.SetTypeSpecError();
3753 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3754 return;
3755 }
3756
3757 assert(TemplateInfo.TemplateParams && "no template parameters");
3758 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3759 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003760 }
Chad Rosierc1183952012-06-26 22:30:43 +00003761
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003762 if (TUK == Sema::TUK_Reference)
3763 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003764
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003765 if (!Name && TUK != Sema::TUK_Definition) {
3766 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003767
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003768 // Skip the rest of this declarator, up until the comma or semicolon.
Alexey Bataevee6507d2013-11-18 08:17:37 +00003769 SkipUntil(tok::comma, StopAtSemi);
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003770 return;
3771 }
Richard Smith7d137e32012-03-23 03:33:32 +00003772
Douglas Gregord6ab8742009-05-28 23:31:59 +00003773 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003774 bool IsDependent = false;
Craig Topper161e4db2014-05-21 06:02:52 +00003775 const char *PrevSpec = nullptr;
Douglas Gregorba41d012010-04-24 16:38:41 +00003776 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003777 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003778 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003779 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003780 Owned, IsDependent, ScopedEnumKWLoc,
Richard Smith649c7b062014-01-08 00:56:48 +00003781 IsScopedUsingClassTag, BaseType,
3782 DSC == DSC_type_specifier);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003783
Douglas Gregorba41d012010-04-24 16:38:41 +00003784 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003785 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003786 // dependent tag.
3787 if (!Name) {
3788 DS.SetTypeSpecError();
3789 Diag(Tok, diag::err_expected_type_name_after_typename);
3790 return;
3791 }
Chad Rosierc1183952012-06-26 22:30:43 +00003792
Nico Weber83ea0122014-05-03 21:57:40 +00003793 TypeResult Type = Actions.ActOnDependentTag(
3794 getCurScope(), DeclSpec::TST_enum, TUK, SS, Name, StartLoc, NameLoc);
Douglas Gregorba41d012010-04-24 16:38:41 +00003795 if (Type.isInvalid()) {
3796 DS.SetTypeSpecError();
3797 return;
3798 }
Chad Rosierc1183952012-06-26 22:30:43 +00003799
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003800 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3801 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003802 PrevSpec, DiagID, Type.get(),
3803 Actions.getASTContext().getPrintingPolicy()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003804 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003805
Douglas Gregorba41d012010-04-24 16:38:41 +00003806 return;
3807 }
Mike Stump11289f42009-09-09 15:08:12 +00003808
John McCall48871652010-08-21 09:40:31 +00003809 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003810 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003811 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003812 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003813 ConsumeBrace();
Alexey Bataevee6507d2013-11-18 08:17:37 +00003814 SkipUntil(tok::r_brace, StopAtSemi);
Douglas Gregorba41d012010-04-24 16:38:41 +00003815 }
Chad Rosierc1183952012-06-26 22:30:43 +00003816
Douglas Gregorba41d012010-04-24 16:38:41 +00003817 DS.SetTypeSpecError();
3818 return;
3819 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003820
Richard Smith369b9f92012-06-25 21:37:02 +00003821 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003822 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003823
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003824 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3825 NameLoc.isValid() ? NameLoc : StartLoc,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00003826 PrevSpec, DiagID, TagDecl, Owned,
3827 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00003828 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003829}
3830
Chris Lattnerc1915e22007-01-25 07:29:02 +00003831/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3832/// enumerator-list:
3833/// enumerator
3834/// enumerator-list ',' enumerator
3835/// enumerator:
Aaron Ballman730476b2014-11-08 15:33:35 +00003836/// enumeration-constant attributes[opt]
3837/// enumeration-constant attributes[opt] '=' constant-expression
Chris Lattnerc1915e22007-01-25 07:29:02 +00003838/// enumeration-constant:
3839/// identifier
3840///
John McCall48871652010-08-21 09:40:31 +00003841void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003842 // Enter the scope of the enum body and start the definition.
Hans Wennborgfe781452014-06-17 00:00:18 +00003843 ParseScope EnumScope(this, Scope::DeclScope | Scope::EnumScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003844 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003845
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003846 BalancedDelimiterTracker T(*this, tok::l_brace);
3847 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003848
Chris Lattner37256fb2007-08-27 17:24:30 +00003849 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003850 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003851 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003852
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003853 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003854
Craig Topper161e4db2014-05-21 06:02:52 +00003855 Decl *LastEnumConstDecl = nullptr;
Mike Stump11289f42009-09-09 15:08:12 +00003856
Chris Lattnerc1915e22007-01-25 07:29:02 +00003857 // Parse the enumerator-list.
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003858 while (Tok.isNot(tok::r_brace)) {
3859 // Parse enumerator. If failed, try skipping till the start of the next
3860 // enumerator definition.
3861 if (Tok.isNot(tok::identifier)) {
3862 Diag(Tok.getLocation(), diag::err_expected) << tok::identifier;
3863 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch) &&
3864 TryConsumeToken(tok::comma))
3865 continue;
3866 break;
3867 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003868 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3869 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003870
John McCall811a0f52010-10-22 23:36:17 +00003871 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003872 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003873 MaybeParseGNUAttributes(attrs);
Aaron Ballman730476b2014-11-08 15:33:35 +00003874 ProhibitAttributes(attrs); // GNU-style attributes are prohibited.
3875 if (getLangOpts().CPlusPlus11 && isCXX11AttributeSpecifier()) {
3876 if (!getLangOpts().CPlusPlus1z)
3877 Diag(Tok.getLocation(), diag::warn_cxx14_compat_attribute)
Aaron Ballmanc0ae7df2014-11-08 17:07:15 +00003878 << 1 /*enumerator*/;
Aaron Ballman730476b2014-11-08 15:33:35 +00003879 ParseCXX11Attributes(attrs);
3880 }
John McCall811a0f52010-10-22 23:36:17 +00003881
Chris Lattnerc1915e22007-01-25 07:29:02 +00003882 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003883 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003884 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003885
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003886 if (TryConsumeToken(tok::equal, EqualLoc)) {
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003887 AssignedVal = ParseConstantExpression();
3888 if (AssignedVal.isInvalid())
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003889 SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003890 }
Mike Stump11289f42009-09-09 15:08:12 +00003891
Chris Lattnerc1915e22007-01-25 07:29:02 +00003892 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003893 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3894 LastEnumConstDecl,
3895 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003896 attrs.getList(), EqualLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00003897 AssignedVal.get());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003898 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003899
Chris Lattner4ef40012007-06-11 01:28:17 +00003900 EnumConstantDecls.push_back(EnumConstDecl);
3901 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003902
Douglas Gregorce66d022010-09-07 14:51:08 +00003903 if (Tok.is(tok::identifier)) {
3904 // We're missing a comma between enumerators.
3905 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003906 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003907 << FixItHint::CreateInsertion(Loc, ", ");
3908 continue;
3909 }
Chad Rosierc1183952012-06-26 22:30:43 +00003910
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003911 // Emumerator definition must be finished, only comma or r_brace are
3912 // allowed here.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00003913 SourceLocation CommaLoc;
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003914 if (Tok.isNot(tok::r_brace) && !TryConsumeToken(tok::comma, CommaLoc)) {
3915 if (EqualLoc.isValid())
3916 Diag(Tok.getLocation(), diag::err_expected_either) << tok::r_brace
3917 << tok::comma;
3918 else
3919 Diag(Tok.getLocation(), diag::err_expected_end_of_enumerator);
3920 if (SkipUntil(tok::comma, tok::r_brace, StopBeforeMatch)) {
3921 if (TryConsumeToken(tok::comma, CommaLoc))
3922 continue;
3923 } else {
3924 break;
3925 }
3926 }
Mike Stump11289f42009-09-09 15:08:12 +00003927
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003928 // If comma is followed by r_brace, emit appropriate warning.
3929 if (Tok.is(tok::r_brace) && CommaLoc.isValid()) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003930 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003931 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3932 diag::ext_enumerator_list_comma_cxx :
3933 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003934 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003935 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003936 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3937 << FixItHint::CreateRemoval(CommaLoc);
Serge Pavlov2e3ecb62013-12-31 06:26:03 +00003938 break;
Richard Smith5d164bc2011-10-15 05:09:34 +00003939 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003940 }
Mike Stump11289f42009-09-09 15:08:12 +00003941
Chris Lattnerc1915e22007-01-25 07:29:02 +00003942 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003943 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003944
Chris Lattnerc1915e22007-01-25 07:29:02 +00003945 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003946 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003947 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003948
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003949 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
Dmitri Gribenkoe5fde992013-04-27 20:23:52 +00003950 EnumDecl, EnumConstantDecls,
3951 getCurScope(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003952 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003953
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003954 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003955 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3956 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003957
3958 // The next token must be valid after an enum definition. If not, a ';'
3959 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003960 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3961 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Alp Toker383d2c42014-01-01 03:08:43 +00003962 ExpectAndConsume(tok::semi, diag::err_expected_after, "enum");
Richard Smith369b9f92012-06-25 21:37:02 +00003963 // Push this token back into the preprocessor and change our current token
3964 // to ';' so that the rest of the code recovers as though there were an
3965 // ';' after the definition.
3966 PP.EnterToken(Tok);
3967 Tok.setKind(tok::semi);
3968 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003969}
Chris Lattner3b561a32006-08-13 00:12:11 +00003970
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003971/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003972/// start of a type-qualifier-list.
3973bool Parser::isTypeQualifier() const {
3974 switch (Tok.getKind()) {
3975 default: return false;
Alp Tokerde50ff32013-12-17 18:17:46 +00003976 // type-qualifier
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003977 case tok::kw_const:
3978 case tok::kw_volatile:
3979 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003980 case tok::kw___private:
3981 case tok::kw___local:
3982 case tok::kw___global:
3983 case tok::kw___constant:
3984 case tok::kw___read_only:
3985 case tok::kw___read_write:
3986 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003987 return true;
3988 }
3989}
3990
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003991/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3992/// is definitely a type-specifier. Return false if it isn't part of a type
3993/// specifier or if we're not sure.
3994bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3995 switch (Tok.getKind()) {
3996 default: return false;
3997 // type-specifiers
3998 case tok::kw_short:
3999 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004000 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004001 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004002 case tok::kw_signed:
4003 case tok::kw_unsigned:
4004 case tok::kw__Complex:
4005 case tok::kw__Imaginary:
4006 case tok::kw_void:
4007 case tok::kw_char:
4008 case tok::kw_wchar_t:
4009 case tok::kw_char16_t:
4010 case tok::kw_char32_t:
4011 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004012 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004013 case tok::kw_float:
4014 case tok::kw_double:
4015 case tok::kw_bool:
4016 case tok::kw__Bool:
4017 case tok::kw__Decimal32:
4018 case tok::kw__Decimal64:
4019 case tok::kw__Decimal128:
4020 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00004021
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004022 // struct-or-union-specifier (C99) or class-specifier (C++)
4023 case tok::kw_class:
4024 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004025 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004026 case tok::kw_union:
4027 // enum-specifier
4028 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00004029
Chris Lattnerfd48afe2010-02-28 18:18:36 +00004030 // typedef-name
4031 case tok::annot_typename:
4032 return true;
4033 }
4034}
4035
Steve Naroff69e8f9e2008-02-11 23:15:56 +00004036/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004037/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004038bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004039 switch (Tok.getKind()) {
4040 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004041
Chris Lattner020bab92009-01-04 23:41:41 +00004042 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00004043 if (TryAltiVecVectorToken())
4044 return true;
4045 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00004046 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004047 // Annotate typenames and C++ scope specifiers. If we get one, just
4048 // recurse to handle whatever we get.
4049 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004050 return true;
4051 if (Tok.is(tok::identifier))
4052 return false;
4053 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00004054
Chris Lattner020bab92009-01-04 23:41:41 +00004055 case tok::coloncolon: // ::foo::bar
4056 if (NextToken().is(tok::kw_new) || // ::new
4057 NextToken().is(tok::kw_delete)) // ::delete
4058 return false;
4059
Chris Lattner020bab92009-01-04 23:41:41 +00004060 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004061 return true;
4062 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00004063
Chris Lattnere37e2332006-08-15 04:50:22 +00004064 // GNU attributes support.
4065 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00004066 // GNU typeof support.
4067 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004068
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004069 // type-specifiers
4070 case tok::kw_short:
4071 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004072 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004073 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004074 case tok::kw_signed:
4075 case tok::kw_unsigned:
4076 case tok::kw__Complex:
4077 case tok::kw__Imaginary:
4078 case tok::kw_void:
4079 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004080 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004081 case tok::kw_char16_t:
4082 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004083 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004084 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004085 case tok::kw_float:
4086 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004087 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004088 case tok::kw__Bool:
4089 case tok::kw__Decimal32:
4090 case tok::kw__Decimal64:
4091 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004092 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004093
Chris Lattner861a2262008-04-13 18:59:07 +00004094 // struct-or-union-specifier (C99) or class-specifier (C++)
4095 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004096 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00004097 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004098 case tok::kw_union:
4099 // enum-specifier
4100 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004101
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004102 // type-qualifier
4103 case tok::kw_const:
4104 case tok::kw_volatile:
4105 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004106
John McCallea0a39e2012-11-14 00:49:39 +00004107 // Debugger support.
4108 case tok::kw___unknown_anytype:
4109
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004110 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00004111 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004112 return true;
Mike Stump11289f42009-09-09 15:08:12 +00004113
Chris Lattner409bf7d2008-10-20 00:25:30 +00004114 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4115 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004116 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004117
Steve Naroff44ac7772008-12-25 14:16:32 +00004118 case tok::kw___cdecl:
4119 case tok::kw___stdcall:
4120 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004121 case tok::kw___thiscall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004122 case tok::kw___vectorcall:
Eli Friedman53339e02009-06-08 23:27:34 +00004123 case tok::kw___w64:
4124 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004125 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004126 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004127 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004128
4129 case tok::kw___private:
4130 case tok::kw___local:
4131 case tok::kw___global:
4132 case tok::kw___constant:
4133 case tok::kw___read_only:
4134 case tok::kw___read_write:
4135 case tok::kw___write_only:
4136
Eli Friedman53339e02009-06-08 23:27:34 +00004137 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004138
Richard Smith8e1ac332013-03-28 01:55:44 +00004139 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004140 case tok::kw__Atomic:
4141 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00004142 }
4143}
4144
Chris Lattneracd58a32006-08-06 17:24:14 +00004145/// isDeclarationSpecifier() - Return true if the current token is part of a
4146/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004147///
4148/// \param DisambiguatingWithExpression True to indicate that the purpose of
4149/// this check is to disambiguate between an expression and a declaration.
4150bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004151 switch (Tok.getKind()) {
4152 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00004153
Chris Lattner020bab92009-01-04 23:41:41 +00004154 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00004155 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004156 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00004157 return false;
John Thompson22334602010-02-05 00:12:22 +00004158 if (TryAltiVecVectorToken())
4159 return true;
4160 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00004161 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00004162 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00004163 // Annotate typenames and C++ scope specifiers. If we get one, just
4164 // recurse to handle whatever we get.
4165 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004166 return true;
4167 if (Tok.is(tok::identifier))
4168 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004169
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004170 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00004171 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004172 // expression is permitted, then this is probably a class message send
4173 // missing the initial '['. In this case, we won't consider this to be
4174 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00004175 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00004176 isStartOfObjCClassMessageMissingOpenBracket())
4177 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00004178
John McCall1f476a12010-02-26 08:45:28 +00004179 return isDeclarationSpecifier();
4180
Chris Lattner020bab92009-01-04 23:41:41 +00004181 case tok::coloncolon: // ::foo::bar
4182 if (NextToken().is(tok::kw_new) || // ::new
4183 NextToken().is(tok::kw_delete)) // ::delete
4184 return false;
Mike Stump11289f42009-09-09 15:08:12 +00004185
Chris Lattner020bab92009-01-04 23:41:41 +00004186 // Annotate typenames and C++ scope specifiers. If we get one, just
4187 // recurse to handle whatever we get.
4188 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00004189 return true;
4190 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00004191
Chris Lattneracd58a32006-08-06 17:24:14 +00004192 // storage-class-specifier
4193 case tok::kw_typedef:
4194 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00004195 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00004196 case tok::kw_static:
4197 case tok::kw_auto:
4198 case tok::kw_register:
4199 case tok::kw___thread:
Richard Smithb4a9e862013-04-12 22:46:28 +00004200 case tok::kw_thread_local:
4201 case tok::kw__Thread_local:
Mike Stump11289f42009-09-09 15:08:12 +00004202
Douglas Gregor26701a42011-09-09 02:06:17 +00004203 // Modules
4204 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00004205
John McCallea0a39e2012-11-14 00:49:39 +00004206 // Debugger support
4207 case tok::kw___unknown_anytype:
4208
Chris Lattneracd58a32006-08-06 17:24:14 +00004209 // type-specifiers
4210 case tok::kw_short:
4211 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00004212 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00004213 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00004214 case tok::kw_signed:
4215 case tok::kw_unsigned:
4216 case tok::kw__Complex:
4217 case tok::kw__Imaginary:
4218 case tok::kw_void:
4219 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00004220 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00004221 case tok::kw_char16_t:
4222 case tok::kw_char32_t:
4223
Chris Lattneracd58a32006-08-06 17:24:14 +00004224 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00004225 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00004226 case tok::kw_float:
4227 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00004228 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00004229 case tok::kw__Bool:
4230 case tok::kw__Decimal32:
4231 case tok::kw__Decimal64:
4232 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00004233 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00004234
Chris Lattner861a2262008-04-13 18:59:07 +00004235 // struct-or-union-specifier (C99) or class-specifier (C++)
4236 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00004237 case tok::kw_struct:
4238 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00004239 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00004240 // enum-specifier
4241 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00004242
Chris Lattneracd58a32006-08-06 17:24:14 +00004243 // type-qualifier
4244 case tok::kw_const:
4245 case tok::kw_volatile:
4246 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00004247
Chris Lattneracd58a32006-08-06 17:24:14 +00004248 // function-specifier
4249 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00004250 case tok::kw_virtual:
4251 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00004252 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00004253
Richard Smith1dba27c2013-01-29 09:02:09 +00004254 // alignment-specifier
4255 case tok::kw__Alignas:
4256
Richard Smithd16fe122012-10-25 00:00:53 +00004257 // friend keyword.
4258 case tok::kw_friend:
4259
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00004260 // static_assert-declaration
4261 case tok::kw__Static_assert:
4262
Chris Lattner599e47e2007-08-09 17:01:07 +00004263 // GNU typeof support.
4264 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00004265
Chris Lattner599e47e2007-08-09 17:01:07 +00004266 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00004267 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00004268
Richard Smithd16fe122012-10-25 00:00:53 +00004269 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00004270 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00004271 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00004272
Richard Smith8e1ac332013-03-28 01:55:44 +00004273 // C11 _Atomic
Eli Friedman0dfb8892011-10-06 23:00:33 +00004274 case tok::kw__Atomic:
4275 return true;
4276
Chris Lattner8b2ec162008-07-26 03:38:44 +00004277 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
4278 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004279 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00004280
Douglas Gregor19b7acf2011-04-27 05:41:15 +00004281 // typedef-name
4282 case tok::annot_typename:
4283 return !DisambiguatingWithExpression ||
4284 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00004285
Steve Narofff192fab2009-01-06 19:34:12 +00004286 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00004287 case tok::kw___cdecl:
4288 case tok::kw___stdcall:
4289 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004290 case tok::kw___thiscall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004291 case tok::kw___vectorcall:
Eli Friedman53339e02009-06-08 23:27:34 +00004292 case tok::kw___w64:
Aaron Ballman317a77f2013-05-22 23:25:32 +00004293 case tok::kw___sptr:
4294 case tok::kw___uptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004295 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004296 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00004297 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004298 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00004299 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004300
4301 case tok::kw___private:
4302 case tok::kw___local:
4303 case tok::kw___global:
4304 case tok::kw___constant:
4305 case tok::kw___read_only:
4306 case tok::kw___read_write:
4307 case tok::kw___write_only:
4308
Eli Friedman53339e02009-06-08 23:27:34 +00004309 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00004310 }
4311}
4312
Richard Smith446161b2014-03-03 21:12:53 +00004313bool Parser::isConstructorDeclarator(bool IsUnqualified) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004314 TentativeParsingAction TPA(*this);
4315
4316 // Parse the C++ scope specifier.
4317 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00004318 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004319 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00004320 TPA.Revert();
4321 return false;
4322 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004323
4324 // Parse the constructor name.
4325 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
4326 // We already know that we have a constructor name; just consume
4327 // the token.
4328 ConsumeToken();
4329 } else {
4330 TPA.Revert();
4331 return false;
4332 }
4333
Richard Smith43f340f2012-03-27 23:05:05 +00004334 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004335 if (Tok.isNot(tok::l_paren)) {
4336 TPA.Revert();
4337 return false;
4338 }
4339 ConsumeParen();
4340
Richard Smith43f340f2012-03-27 23:05:05 +00004341 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4342 // that we have a constructor.
4343 if (Tok.is(tok::r_paren) ||
4344 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004345 TPA.Revert();
4346 return true;
4347 }
4348
Richard Smithf2163662013-09-06 00:12:20 +00004349 // A C++11 attribute here signals that we have a constructor, and is an
4350 // attribute on the first constructor parameter.
4351 if (getLangOpts().CPlusPlus11 &&
4352 isCXX11AttributeSpecifier(/*Disambiguate*/ false,
4353 /*OuterMightBeMessageSend*/ true)) {
4354 TPA.Revert();
4355 return true;
4356 }
4357
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004358 // If we need to, enter the specified scope.
4359 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00004360 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004361 DeclScopeObj.EnterDeclaratorScope();
4362
Francois Pichet79f3a872011-01-31 04:54:32 +00004363 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00004364 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004365 MaybeParseMicrosoftAttributes(Attrs);
4366
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004367 // Check whether the next token(s) are part of a declaration
4368 // specifier, in which case we have the start of a parameter and,
4369 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004370 bool IsConstructor = false;
4371 if (isDeclarationSpecifier())
4372 IsConstructor = true;
4373 else if (Tok.is(tok::identifier) ||
4374 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4375 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4376 // This might be a parenthesized member name, but is more likely to
4377 // be a constructor declaration with an invalid argument type. Keep
4378 // looking.
4379 if (Tok.is(tok::annot_cxxscope))
4380 ConsumeToken();
4381 ConsumeToken();
4382
4383 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004384 // which must have one of the following syntactic forms (see the
4385 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004386 switch (Tok.getKind()) {
4387 case tok::l_paren:
4388 // C(X ( int));
4389 case tok::l_square:
4390 // C(X [ 5]);
4391 // C(X [ [attribute]]);
4392 case tok::coloncolon:
4393 // C(X :: Y);
4394 // C(X :: *p);
Richard Smithefd009d2012-03-27 00:56:56 +00004395 // Assume this isn't a constructor, rather than assuming it's a
4396 // constructor with an unnamed parameter of an ill-formed type.
4397 break;
4398
Richard Smith446161b2014-03-03 21:12:53 +00004399 case tok::r_paren:
4400 // C(X )
4401 if (NextToken().is(tok::colon) || NextToken().is(tok::kw_try)) {
4402 // Assume these were meant to be constructors:
4403 // C(X) : (the name of a bit-field cannot be parenthesized).
4404 // C(X) try (this is otherwise ill-formed).
4405 IsConstructor = true;
4406 }
4407 if (NextToken().is(tok::semi) || NextToken().is(tok::l_brace)) {
4408 // If we have a constructor name within the class definition,
4409 // assume these were meant to be constructors:
4410 // C(X) {
4411 // C(X) ;
4412 // ... because otherwise we would be declaring a non-static data
4413 // member that is ill-formed because it's of the same type as its
4414 // surrounding class.
4415 //
4416 // FIXME: We can actually do this whether or not the name is qualified,
4417 // because if it is qualified in this context it must be being used as
4418 // a constructor name. However, we do not implement that rule correctly
4419 // currently, so we're somewhat conservative here.
4420 IsConstructor = IsUnqualified;
4421 }
4422 break;
4423
Richard Smithefd009d2012-03-27 00:56:56 +00004424 default:
4425 IsConstructor = true;
4426 break;
4427 }
4428 }
4429
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004430 TPA.Revert();
4431 return IsConstructor;
4432}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004433
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004434/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004435/// type-qualifier-list: [C99 6.7.5]
4436/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004437/// [vendor] attributes
Aaron Ballman08b06592014-07-22 12:44:22 +00004438/// [ only if AttrReqs & AR_VendorAttributesParsed ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004439/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004440/// [vendor] type-qualifier-list attributes
Aaron Ballman08b06592014-07-22 12:44:22 +00004441/// [ only if AttrReqs & AR_VendorAttributesParsed ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004442/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Aaron Ballman08b06592014-07-22 12:44:22 +00004443/// [ only if AttReqs & AR_CXX11AttributesParsed ]
4444/// Note: vendor can be GNU, MS, etc and can be explicitly controlled via
4445/// AttrRequirements bitmask values.
4446void Parser::ParseTypeQualifierListOpt(DeclSpec &DS, unsigned AttrReqs,
Alp Toker62c5b572013-11-26 01:30:10 +00004447 bool AtomicAllowed,
4448 bool IdentifierRequired) {
Aaron Ballman08b06592014-07-22 12:44:22 +00004449 if (getLangOpts().CPlusPlus11 && (AttrReqs & AR_CXX11AttributesParsed) &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004450 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004451 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004452 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004453 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004454 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004455
4456 SourceLocation EndLoc;
4457
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004458 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004459 bool isInvalid = false;
Craig Topper161e4db2014-05-21 06:02:52 +00004460 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +00004461 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004462 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004463
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004464 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004465 case tok::code_completion:
4466 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004467 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004468
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004469 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004470 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004471 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004472 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004473 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004474 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004475 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004476 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004477 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004478 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004479 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004480 break;
Richard Smith8e1ac332013-03-28 01:55:44 +00004481 case tok::kw__Atomic:
4482 if (!AtomicAllowed)
4483 goto DoneWithTypeQuals;
4484 isInvalid = DS.SetTypeQual(DeclSpec::TQ_atomic, Loc, PrevSpec, DiagID,
4485 getLangOpts());
4486 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004487
4488 // OpenCL qualifiers:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004489 case tok::kw___private:
4490 case tok::kw___global:
4491 case tok::kw___local:
4492 case tok::kw___constant:
4493 case tok::kw___read_only:
4494 case tok::kw___write_only:
4495 case tok::kw___read_write:
Aaron Ballman05d76ea2014-01-14 01:29:54 +00004496 ParseOpenCLQualifiers(DS.getAttributes());
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004497 break;
4498
Aaron Ballman317a77f2013-05-22 23:25:32 +00004499 case tok::kw___uptr:
Alp Toker62c5b572013-11-26 01:30:10 +00004500 // GNU libc headers in C mode use '__uptr' as an identifer which conflicts
4501 // with the MS modifier keyword.
Aaron Ballman08b06592014-07-22 12:44:22 +00004502 if ((AttrReqs & AR_DeclspecAttributesParsed) && !getLangOpts().CPlusPlus &&
Alp Toker47642d22013-12-03 06:13:01 +00004503 IdentifierRequired && DS.isEmpty() && NextToken().is(tok::semi)) {
4504 if (TryKeywordIdentFallback(false))
4505 continue;
Alp Toker62c5b572013-11-26 01:30:10 +00004506 }
4507 case tok::kw___sptr:
Eli Friedman53339e02009-06-08 23:27:34 +00004508 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004509 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004510 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004511 case tok::kw___cdecl:
4512 case tok::kw___stdcall:
4513 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004514 case tok::kw___thiscall:
Reid Klecknerd7857f02014-10-24 17:42:17 +00004515 case tok::kw___vectorcall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004516 case tok::kw___unaligned:
Aaron Ballman08b06592014-07-22 12:44:22 +00004517 if (AttrReqs & AR_DeclspecAttributesParsed) {
John McCall53fa7142010-12-24 02:08:15 +00004518 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004519 continue;
4520 }
4521 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004522 case tok::kw___pascal:
Aaron Ballman08b06592014-07-22 12:44:22 +00004523 if (AttrReqs & AR_VendorAttributesParsed) {
John McCall53fa7142010-12-24 02:08:15 +00004524 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004525 continue;
4526 }
4527 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004528 case tok::kw___attribute:
Aaron Ballman08b06592014-07-22 12:44:22 +00004529 if (AttrReqs & AR_GNUAttributesParsedAndRejected)
4530 // When GNU attributes are expressly forbidden, diagnose their usage.
4531 Diag(Tok, diag::err_attributes_not_allowed);
4532
4533 // Parse the attributes even if they are rejected to ensure that error
4534 // recovery is graceful.
4535 if (AttrReqs & AR_GNUAttributesParsed ||
4536 AttrReqs & AR_GNUAttributesParsedAndRejected) {
John McCall53fa7142010-12-24 02:08:15 +00004537 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004538 continue; // do *not* consume the next token!
4539 }
4540 // otherwise, FALL THROUGH!
4541 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004542 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004543 // If this is not a type-qualifier token, we're done reading type
4544 // qualifiers. First verify that DeclSpec's are consistent.
Erik Verbruggen888d52a2014-01-15 09:15:43 +00004545 DS.Finish(Diags, PP, Actions.getASTContext().getPrintingPolicy());
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004546 if (EndLoc.isValid())
4547 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004548 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004549 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004550
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004551 // If the specifier combination wasn't legal, issue a diagnostic.
4552 if (isInvalid) {
4553 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004554 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004555 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004556 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004557 }
4558}
4559
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004560
4561/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4562///
4563void Parser::ParseDeclarator(Declarator &D) {
4564 /// This implements the 'declarator' production in the C grammar, then checks
4565 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004566 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004567}
4568
Richard Smith0b350b92014-10-28 16:55:02 +00004569static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang,
4570 unsigned TheContext) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004571 if (Kind == tok::star || Kind == tok::caret)
4572 return true;
4573
Richard Smith0efa75c2012-03-29 01:16:42 +00004574 if (!Lang.CPlusPlus)
4575 return false;
4576
Richard Smith0b350b92014-10-28 16:55:02 +00004577 if (Kind == tok::amp)
4578 return true;
4579
4580 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4581 // But we must not parse them in conversion-type-ids and new-type-ids, since
4582 // those can be legitimately followed by a && operator.
4583 // (The same thing can in theory happen after a trailing-return-type, but
4584 // since those are a C++11 feature, there is no rejects-valid issue there.)
4585 if (Kind == tok::ampamp)
4586 return Lang.CPlusPlus11 || (TheContext != Declarator::ConversionIdContext &&
4587 TheContext != Declarator::CXXNewContext);
4588
4589 return false;
Richard Smith0efa75c2012-03-29 01:16:42 +00004590}
4591
Sebastian Redlbd150f42008-11-21 19:14:01 +00004592/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4593/// is parsed by the function passed to it. Pass null, and the direct-declarator
4594/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004595/// ptr-operator production.
4596///
Richard Smith09f76ee2011-10-19 21:33:05 +00004597/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004598/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4599/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004600///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004601/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4602/// [C] pointer[opt] direct-declarator
4603/// [C++] direct-declarator
4604/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004605///
4606/// pointer: [C99 6.7.5]
4607/// '*' type-qualifier-list[opt]
4608/// '*' type-qualifier-list[opt] pointer
4609///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004610/// ptr-operator:
4611/// '*' cv-qualifier-seq[opt]
4612/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004613/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004614/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004615/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004616/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004617void Parser::ParseDeclaratorInternal(Declarator &D,
4618 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004619 if (Diags.hasAllExtensionsSilenced())
4620 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004621
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004622 // C++ member pointers start with a '::' or a nested-name.
4623 // Member pointers get special handling, since there's no place for the
4624 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004625 if (getLangOpts().CPlusPlus &&
Serge Pavlov458ea762014-07-16 05:16:52 +00004626 (Tok.is(tok::coloncolon) ||
4627 (Tok.is(tok::identifier) &&
4628 (NextToken().is(tok::coloncolon) || NextToken().is(tok::less))) ||
Chris Lattner803802d2009-03-24 17:04:48 +00004629 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004630 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4631 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004632 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004633 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004634
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004635 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004636 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004637 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004638 if (D.mayHaveIdentifier())
4639 D.getCXXScopeSpec() = SS;
4640 else
4641 AnnotateScopeToken(SS, true);
4642
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004643 if (DirectDeclParser)
4644 (this->*DirectDeclParser)(D);
4645 return;
4646 }
4647
4648 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004649 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004650 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004651 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004652 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004653
4654 // Recurse to parse whatever is left.
4655 ParseDeclaratorInternal(D, DirectDeclParser);
4656
4657 // Sema will have to catch (syntactically invalid) pointers into global
4658 // scope. It has to catch pointers into namespace scope anyway.
4659 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004660 Loc),
4661 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004662 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004663 return;
4664 }
4665 }
4666
4667 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004668 // Not a pointer, C++ reference, or block.
Richard Smith0b350b92014-10-28 16:55:02 +00004669 if (!isPtrOperatorToken(Kind, getLangOpts(), D.getContext())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004670 if (DirectDeclParser)
4671 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004672 return;
4673 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004674
Sebastian Redled0f3b02009-03-15 22:02:01 +00004675 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4676 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004677 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004678 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004679
Chris Lattner9eac9312009-03-27 04:18:06 +00004680 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004681 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004682 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004683
Aaron Ballman08b06592014-07-22 12:44:22 +00004684 // GNU attributes are not allowed here in a new-type-id, but Declspec and
4685 // C++11 attributes are allowed.
4686 unsigned Reqs = AR_CXX11AttributesParsed | AR_DeclspecAttributesParsed |
4687 ((D.getContext() != Declarator::CXXNewContext)
4688 ? AR_GNUAttributesParsed
4689 : AR_GNUAttributesParsedAndRejected);
4690 ParseTypeQualifierListOpt(DS, Reqs, true, !D.mayOmitIdentifier());
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004691 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004692
Bill Wendling3708c182007-05-27 10:15:43 +00004693 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004694 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004695 if (Kind == tok::star)
4696 // Remember that we parsed a pointer type, and remember the type-quals.
4697 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004698 DS.getConstSpecLoc(),
4699 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004700 DS.getRestrictSpecLoc()),
4701 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004702 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004703 else
4704 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004705 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004706 Loc),
4707 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004708 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004709 } else {
4710 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004711 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004712
Sebastian Redl3b27be62009-03-23 00:00:23 +00004713 // Complain about rvalue references in C++03, but then go on and build
4714 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004715 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004716 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004717 diag::warn_cxx98_compat_rvalue_reference :
4718 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004719
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004720 // GNU-style and C++11 attributes are allowed here, as is restrict.
4721 ParseTypeQualifierListOpt(DS);
4722 D.ExtendWithDeclSpec(DS);
4723
Bill Wendling93efb222007-06-02 23:28:54 +00004724 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4725 // cv-qualifiers are introduced through the use of a typedef or of a
4726 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004727 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4728 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4729 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004730 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004731 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4732 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004733 diag::err_invalid_reference_qualifier_application) << "volatile";
Richard Smith8e1ac332013-03-28 01:55:44 +00004734 // 'restrict' is permitted as an extension.
4735 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
4736 Diag(DS.getAtomicSpecLoc(),
4737 diag::err_invalid_reference_qualifier_application) << "_Atomic";
Bill Wendling93efb222007-06-02 23:28:54 +00004738 }
Bill Wendling3708c182007-05-27 10:15:43 +00004739
4740 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004741 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004742
Douglas Gregor66583c52008-11-03 15:51:28 +00004743 if (D.getNumTypeObjects() > 0) {
4744 // C++ [dcl.ref]p4: There shall be no references to references.
4745 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4746 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004747 if (const IdentifierInfo *II = D.getIdentifier())
4748 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4749 << II;
4750 else
4751 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4752 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004753
Sebastian Redlbd150f42008-11-21 19:14:01 +00004754 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004755 // can go ahead and build the (technically ill-formed)
4756 // declarator: reference collapsing will take care of it.
4757 }
4758 }
4759
Richard Smith8e1ac332013-03-28 01:55:44 +00004760 // Remember that we parsed a reference type.
Chris Lattner788404f2008-02-21 01:32:26 +00004761 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004762 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004763 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004764 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004765 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004766}
4767
Richard Trieuf4b81d02014-06-24 23:14:24 +00004768// When correcting from misplaced brackets before the identifier, the location
4769// is saved inside the declarator so that other diagnostic messages can use
4770// them. This extracts and returns that location, or returns the provided
4771// location if a stored location does not exist.
4772static SourceLocation getMissingDeclaratorIdLoc(Declarator &D,
4773 SourceLocation Loc) {
4774 if (D.getName().StartLocation.isInvalid() &&
4775 D.getName().EndLocation.isValid())
4776 return D.getName().EndLocation;
4777
4778 return Loc;
4779}
4780
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004781/// ParseDirectDeclarator
4782/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004783/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004784/// '(' declarator ')'
4785/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004786/// [C90] direct-declarator '[' constant-expression[opt] ']'
4787/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4788/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4789/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4790/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004791/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4792/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004793/// direct-declarator '(' parameter-type-list ')'
4794/// direct-declarator '(' identifier-list[opt] ')'
4795/// [GNU] direct-declarator '(' parameter-forward-declarations
4796/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004797/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4798/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004799/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4800/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4801/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004802/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004803/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004804///
4805/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004806/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004807/// '::'[opt] nested-name-specifier[opt] type-name
4808///
4809/// id-expression: [C++ 5.1]
4810/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004811/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004812///
4813/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004814/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004815/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004816/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004817/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004818/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004819///
Richard Smith1453e312012-03-27 01:42:32 +00004820/// Note, any additional constructs added here may need corresponding changes
4821/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004822void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004823 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004824
David Blaikiebbafb8a2012-03-11 07:00:24 +00004825 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Serge Pavlov458ea762014-07-16 05:16:52 +00004826 // Don't parse FOO:BAR as if it were a typo for FOO::BAR inside a class, in
4827 // this context it is a bitfield. Also in range-based for statement colon
4828 // may delimit for-range-declaration.
4829 ColonProtectionRAIIObject X(*this,
4830 D.getContext() == Declarator::MemberContext ||
4831 (D.getContext() == Declarator::ForContext &&
4832 getLangOpts().CPlusPlus11));
4833
Douglas Gregor7861a802009-11-03 01:35:08 +00004834 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004835 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004836 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4837 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004838 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004839 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004840 }
4841
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004842 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004843 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004844 // Change the declaration context for name lookup, until this function
4845 // is exited (and the declarator has been parsed).
4846 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004847 }
4848
Douglas Gregor27b4c162010-12-23 22:44:42 +00004849 // C++0x [dcl.fct]p14:
4850 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004851 // of a parameter-declaration-clause without a preceding comma. In
4852 // this case, the ellipsis is parsed as part of the
4853 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004854 // parameter pack that has not been expanded; otherwise, it is parsed
4855 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004856 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004857 !((D.getContext() == Declarator::PrototypeContext ||
Faisal Vali2b391ab2013-09-26 19:54:12 +00004858 D.getContext() == Declarator::LambdaExprParameterContext ||
Douglas Gregor27b4c162010-12-23 22:44:42 +00004859 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004860 NextToken().is(tok::r_paren) &&
Richard Smithb19337f2013-02-20 20:19:27 +00004861 !D.hasGroupingParens() &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004862 !Actions.containsUnexpandedParameterPacks(D))) {
4863 SourceLocation EllipsisLoc = ConsumeToken();
Richard Smith0b350b92014-10-28 16:55:02 +00004864 if (isPtrOperatorToken(Tok.getKind(), getLangOpts(), D.getContext())) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004865 // The ellipsis was put in the wrong place. Recover, and explain to
4866 // the user what they should have done.
4867 ParseDeclarator(D);
Nikola Smiljanic69fdc9f2014-06-06 02:58:59 +00004868 if (EllipsisLoc.isValid())
4869 DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
Richard Smith0efa75c2012-03-29 01:16:42 +00004870 return;
4871 } else
4872 D.setEllipsisLoc(EllipsisLoc);
4873
4874 // The ellipsis can't be followed by a parenthesized declarator. We
4875 // check for that in ParseParenDeclarator, after we have disambiguated
4876 // the l_paren token.
4877 }
4878
Douglas Gregor7861a802009-11-03 01:35:08 +00004879 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4880 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4881 // We found something that indicates the start of an unqualified-id.
4882 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004883 bool AllowConstructorName;
4884 if (D.getDeclSpec().hasTypeSpecifier())
4885 AllowConstructorName = false;
4886 else if (D.getCXXScopeSpec().isSet())
4887 AllowConstructorName =
4888 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenkod1c91f12013-02-12 17:27:41 +00004889 D.getContext() == Declarator::MemberContext);
John McCall84821e72010-04-13 06:39:49 +00004890 else
4891 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4892
Abramo Bagnara7945c982012-01-27 09:46:47 +00004893 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004894 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4895 /*EnteringContext=*/true,
4896 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004897 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004898 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004899 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004900 D.getName()) ||
4901 // Once we're past the identifier, if the scope was bad, mark the
4902 // whole declarator bad.
4903 D.getCXXScopeSpec().isInvalid()) {
Craig Topper161e4db2014-05-21 06:02:52 +00004904 D.SetIdentifier(nullptr, Tok.getLocation());
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004905 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004906 } else {
4907 // Parsed the unqualified-id; update range information and move along.
4908 if (D.getSourceRange().getBegin().isInvalid())
4909 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4910 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004911 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004912 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004913 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004914 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004915 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004916 "There's a C++-specific check for tok::identifier above");
4917 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4918 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
Richard Trieu2664dc12014-05-05 22:06:50 +00004919 D.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004920 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004921 goto PastIdentifier;
Richard Smith9ce302e2013-07-11 05:10:21 +00004922 } else if (Tok.is(tok::identifier) && D.diagnoseIdentifier()) {
Richard Smithf39720b2013-10-13 22:12:28 +00004923 // A virt-specifier isn't treated as an identifier if it appears after a
4924 // trailing-return-type.
4925 if (D.getContext() != Declarator::TrailingReturnContext ||
4926 !isCXX11VirtSpecifier(Tok)) {
4927 Diag(Tok.getLocation(), diag::err_unexpected_unqualified_id)
4928 << FixItHint::CreateRemoval(Tok.getLocation());
Craig Topper161e4db2014-05-21 06:02:52 +00004929 D.SetIdentifier(nullptr, Tok.getLocation());
Richard Smithf39720b2013-10-13 22:12:28 +00004930 ConsumeToken();
4931 goto PastIdentifier;
4932 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004933 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004934
Douglas Gregor7861a802009-11-03 01:35:08 +00004935 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004936 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004937 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004938 // Example: 'char (*X)' or 'int (*XX)(void)'
4939 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004940
4941 // If the declarator was parenthesized, we entered the declarator
4942 // scope when parsing the parenthesized declarator, then exited
4943 // the scope already. Re-enter the scope, if we need to.
4944 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004945 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004946 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004947 if (!D.isInvalidType() &&
4948 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004949 // Change the declaration context for name lookup, until this function
4950 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004951 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004952 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004953 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004954 // This could be something simple like "int" (in which case the declarator
4955 // portion is empty), if an abstract-declarator is allowed.
Craig Topper161e4db2014-05-21 06:02:52 +00004956 D.SetIdentifier(nullptr, Tok.getLocation());
Richard Smithb19337f2013-02-20 20:19:27 +00004957
4958 // The grammar for abstract-pack-declarator does not allow grouping parens.
4959 // FIXME: Revisit this once core issue 1488 is resolved.
4960 if (D.hasEllipsis() && D.hasGroupingParens())
4961 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4962 diag::ext_abstract_pack_declarator_parens);
Chris Lattneracd58a32006-08-06 17:24:14 +00004963 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004964 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004965 LLVM_BUILTIN_TRAP;
Richard Trieuf4b81d02014-06-24 23:14:24 +00004966 if (Tok.is(tok::l_square))
4967 return ParseMisplacedBracketDeclarator(D);
4968 if (D.getContext() == Declarator::MemberContext) {
4969 Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
4970 diag::err_expected_member_name_or_semi)
Richard Trieua1342402014-05-02 23:40:32 +00004971 << (D.getDeclSpec().isEmpty() ? SourceRange()
4972 : D.getDeclSpec().getSourceRange());
Richard Trieuf4b81d02014-06-24 23:14:24 +00004973 } else if (getLangOpts().CPlusPlus) {
Richard Trieu9c672672013-01-26 02:31:38 +00004974 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4975 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
Richard Trieu2f586962013-09-05 02:31:33 +00004976 else {
4977 SourceLocation Loc = D.getCXXScopeSpec().getEndLoc();
4978 if (Tok.isAtStartOfLine() && Loc.isValid())
4979 Diag(PP.getLocForEndOfToken(Loc), diag::err_expected_unqualified_id)
4980 << getLangOpts().CPlusPlus;
4981 else
Richard Trieuf4b81d02014-06-24 23:14:24 +00004982 Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
4983 diag::err_expected_unqualified_id)
Richard Trieu2f586962013-09-05 02:31:33 +00004984 << getLangOpts().CPlusPlus;
4985 }
Richard Trieuf4b81d02014-06-24 23:14:24 +00004986 } else {
4987 Diag(getMissingDeclaratorIdLoc(D, Tok.getLocation()),
4988 diag::err_expected_either)
4989 << tok::identifier << tok::l_paren;
4990 }
Craig Topper161e4db2014-05-21 06:02:52 +00004991 D.SetIdentifier(nullptr, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004992 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004993 }
Mike Stump11289f42009-09-09 15:08:12 +00004994
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004995 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004996 assert(D.isPastIdentifier() &&
4997 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004998
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004999 // Don't parse attributes unless we have parsed an unparenthesized name.
5000 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00005001 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005002
Chris Lattneracd58a32006-08-06 17:24:14 +00005003 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00005004 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00005005 // Enter function-declaration scope, limiting any declarators to the
5006 // function prototype scope, including parameter declarators.
5007 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00005008 Scope::FunctionPrototypeScope|Scope::DeclScope|
5009 (D.isFunctionDeclaratorAFunctionDeclaration()
5010 ? Scope::FunctionDeclarationScope : 0));
5011
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00005012 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
5013 // In such a case, check if we actually have a function declarator; if it
5014 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00005015 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00005016 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
5017 // The name of the declarator, if any, is tentatively declared within
5018 // a possible direct initializer.
5019 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
5020 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
5021 TentativelyDeclaredIdentifiers.pop_back();
5022 if (!IsFunctionDecl)
5023 break;
5024 }
John McCall084e83d2011-03-24 11:26:52 +00005025 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005026 BalancedDelimiterTracker T(*this, tok::l_paren);
5027 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00005028 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00005029 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00005030 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00005031 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00005032 } else {
5033 break;
5034 }
5035 }
Chad Rosierc1183952012-06-26 22:30:43 +00005036}
Chris Lattneracd58a32006-08-06 17:24:14 +00005037
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005038/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
5039/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00005040/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005041/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
5042///
5043/// direct-declarator:
5044/// '(' declarator ')'
5045/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005046/// direct-declarator '(' parameter-type-list ')'
5047/// direct-declarator '(' identifier-list[opt] ')'
5048/// [GNU] direct-declarator '(' parameter-forward-declarations
5049/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005050///
5051void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005052 BalancedDelimiterTracker T(*this, tok::l_paren);
5053 T.consumeOpen();
5054
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005055 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00005056
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005057 // Eat any attributes before we look at whether this is a grouping or function
5058 // declarator paren. If this is a grouping paren, the attribute applies to
5059 // the type being built up, for example:
5060 // int (__attribute__(()) *x)(long y)
5061 // If this ends up not being a grouping paren, the attribute applies to the
5062 // first argument, for example:
5063 // int (__attribute__(()) int x)
5064 // In either case, we need to eat any attributes to be able to determine what
5065 // sort of paren this is.
5066 //
John McCall084e83d2011-03-24 11:26:52 +00005067 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005068 bool RequiresArg = false;
5069 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00005070 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005071
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005072 // We require that the argument list (if this is a non-grouping paren) be
5073 // present even if the attribute list was empty.
5074 RequiresArg = true;
5075 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00005076
Steve Naroff44ac7772008-12-25 14:16:32 +00005077 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00005078 ParseMicrosoftTypeAttributes(attrs);
5079
Dawn Perchik335e16b2010-09-03 01:29:35 +00005080 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00005081 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00005082 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005083
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005084 // If we haven't past the identifier yet (or where the identifier would be
5085 // stored, if this is an abstract declarator), then this is probably just
5086 // grouping parens. However, if this could be an abstract-declarator, then
5087 // this could also be the start of function arguments (consider 'void()').
5088 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00005089
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005090 if (!D.mayOmitIdentifier()) {
5091 // If this can't be an abstract-declarator, this *must* be a grouping
5092 // paren, because we haven't seen the identifier yet.
5093 isGrouping = true;
5094 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00005095 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
5096 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00005097 isDeclarationSpecifier() || // 'int(int)' is a function.
5098 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005099 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
5100 // considered to be a type, not a K&R identifier-list.
5101 isGrouping = false;
5102 } else {
5103 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
5104 isGrouping = true;
5105 }
Mike Stump11289f42009-09-09 15:08:12 +00005106
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005107 // If this is a grouping paren, handle:
5108 // direct-declarator: '(' declarator ')'
5109 // direct-declarator: '(' attributes declarator ')'
5110 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00005111 SourceLocation EllipsisLoc = D.getEllipsisLoc();
5112 D.setEllipsisLoc(SourceLocation());
5113
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005114 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00005115 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00005116 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005117 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005118 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00005119 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005120 T.getCloseLocation()),
5121 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00005122
5123 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00005124
5125 // An ellipsis cannot be placed outside parentheses.
5126 if (EllipsisLoc.isValid())
Nikola Smiljanic69fdc9f2014-06-06 02:58:59 +00005127 DiagnoseMisplacedEllipsisInDeclarator(EllipsisLoc, D);
Richard Smith0efa75c2012-03-29 01:16:42 +00005128
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005129 return;
5130 }
Mike Stump11289f42009-09-09 15:08:12 +00005131
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005132 // Okay, if this wasn't a grouping paren, it must be the start of a function
5133 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005134 // identifier (and remember where it would have been), then call into
5135 // ParseFunctionDeclarator to handle of argument list.
Craig Topper161e4db2014-05-21 06:02:52 +00005136 D.SetIdentifier(nullptr, Tok.getLocation());
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005137
David Blaikie15a430a2011-12-04 05:04:18 +00005138 // Enter function-declaration scope, limiting any declarators to the
5139 // function prototype scope, including parameter declarators.
5140 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00005141 Scope::FunctionPrototypeScope | Scope::DeclScope |
5142 (D.isFunctionDeclaratorAFunctionDeclaration()
5143 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00005144 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00005145 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005146}
5147
5148/// ParseFunctionDeclarator - We are after the identifier and have parsed the
5149/// declarator D up to a paren, which indicates that we are parsing function
5150/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00005151///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005152/// If FirstArgAttrs is non-null, then the caller parsed those arguments
5153/// immediately after the open paren - they should be considered to be the
5154/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005155///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005156/// If RequiresArg is true, then the first argument of the function is required
5157/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005158///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005159/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
5160/// (C++11) ref-qualifier[opt], exception-specification[opt],
5161/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
5162///
5163/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00005164/// dynamic-exception-specification
5165/// noexcept-specification
5166///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005167void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005168 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005169 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00005170 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005171 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00005172 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00005173 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00005174 // lparen is already consumed!
5175 assert(D.isPastIdentifier() && "Should not call before identifier!");
5176
5177 // This should be true when the function has typed arguments.
5178 // Otherwise, it is treated as a K&R-style function.
5179 bool HasProto = false;
5180 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005181 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005182 // Remember where we see an ellipsis, if any.
5183 SourceLocation EllipsisLoc;
5184
5185 DeclSpec DS(AttrFactory);
5186 bool RefQualifierIsLValueRef = true;
5187 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00005188 SourceLocation ConstQualifierLoc;
5189 SourceLocation VolatileQualifierLoc;
Hal Finkel23a07392014-10-20 17:32:04 +00005190 SourceLocation RestrictQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005191 ExceptionSpecificationType ESpecType = EST_None;
5192 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00005193 SmallVector<ParsedType, 2> DynamicExceptions;
5194 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005195 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005196 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00005197 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005198
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005199 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
5200 EndLoc is the end location for the function declarator.
5201 They differ for trailing return types. */
5202 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005203 SourceLocation LParenLoc, RParenLoc;
5204 LParenLoc = Tracker.getOpenLocation();
5205 StartLoc = LParenLoc;
5206
Douglas Gregor9e66af42011-07-05 16:44:18 +00005207 if (isFunctionDeclaratorIdentifierList()) {
5208 if (RequiresArg)
5209 Diag(Tok, diag::err_argument_required_after_attribute);
5210
5211 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
5212
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005213 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005214 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005215 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005216 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005217 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005218 if (Tok.isNot(tok::r_paren))
Faisal Vali2b391ab2013-09-26 19:54:12 +00005219 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo,
5220 EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005221 else if (RequiresArg)
5222 Diag(Tok, diag::err_argument_required_after_attribute);
5223
David Blaikiebbafb8a2012-03-11 07:00:24 +00005224 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005225
5226 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005227 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005228 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005229 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005230 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00005231
David Blaikiebbafb8a2012-03-11 07:00:24 +00005232 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005233 // FIXME: Accept these components in any order, and produce fixits to
5234 // correct the order if the user gets it wrong. Ideally we should deal
5235 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005236
5237 // Parse cv-qualifier-seq[opt].
Aaron Ballman08b06592014-07-22 12:44:22 +00005238 ParseTypeQualifierListOpt(DS, AR_NoAttributesParsed,
Richard Smith8e1ac332013-03-28 01:55:44 +00005239 /*AtomicAllowed*/ false);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005240 if (!DS.getSourceRange().getEnd().isInvalid()) {
5241 EndLoc = DS.getSourceRange().getEnd();
5242 ConstQualifierLoc = DS.getConstSpecLoc();
5243 VolatileQualifierLoc = DS.getVolatileSpecLoc();
Hal Finkel23a07392014-10-20 17:32:04 +00005244 RestrictQualifierLoc = DS.getRestrictSpecLoc();
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005245 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005246
5247 // Parse ref-qualifier[opt].
5248 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005249 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00005250 diag::warn_cxx98_compat_ref_qualifier :
5251 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005252
Douglas Gregor9e66af42011-07-05 16:44:18 +00005253 RefQualifierIsLValueRef = Tok.is(tok::amp);
5254 RefQualifierLoc = ConsumeToken();
5255 EndLoc = RefQualifierLoc;
5256 }
5257
Douglas Gregor3024f072012-04-16 07:05:22 +00005258 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00005259 // If a declaration declares a member function or member function
5260 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00005261 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00005262 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00005263 // declarator.
Richard Smithad1bbb92013-03-15 00:41:52 +00005264 // FIXME: currently, "static" case isn't handled correctly.
Chad Rosierc1183952012-06-26 22:30:43 +00005265 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005266 getLangOpts().CPlusPlus11 &&
Richard Smith990a6922014-01-17 21:01:18 +00005267 D.getDeclSpec().getStorageClassSpec() != DeclSpec::SCS_typedef &&
Richard Smithad1bbb92013-03-15 00:41:52 +00005268 (D.getContext() == Declarator::MemberContext
5269 ? !D.getDeclSpec().isFriendSpecified()
5270 : D.getContext() == Declarator::FileContext &&
5271 D.getCXXScopeSpec().isValid() &&
5272 Actions.CurContext->isRecord());
Douglas Gregor3024f072012-04-16 07:05:22 +00005273 Sema::CXXThisScopeRAII ThisScope(Actions,
5274 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00005275 DS.getTypeQualifiers() |
Richard Smith034185c2013-04-21 01:08:50 +00005276 (D.getDeclSpec().isConstexprSpecified() &&
Aaron Ballmandd69ef32014-08-19 15:55:55 +00005277 !getLangOpts().CPlusPlus14
Richard Smith01141a92013-01-14 01:55:13 +00005278 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00005279 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00005280
Douglas Gregor9e66af42011-07-05 16:44:18 +00005281 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00005282 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00005283 DynamicExceptions,
5284 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00005285 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005286 if (ESpecType != EST_None)
5287 EndLoc = ESpecRange.getEnd();
5288
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005289 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
5290 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00005291 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005292
Douglas Gregor9e66af42011-07-05 16:44:18 +00005293 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005294 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005295 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00005296 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005297 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
5298 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005299 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00005300 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00005301 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005302 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005303 }
5304 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00005305 }
5306
5307 // Remember that we parsed a function type, and remember the attributes.
5308 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005309 IsAmbiguous,
5310 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005311 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00005312 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005313 DS.getTypeQualifiers(),
5314 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00005315 RefQualifierLoc, ConstQualifierLoc,
5316 VolatileQualifierLoc,
Hal Finkel23a07392014-10-20 17:32:04 +00005317 RestrictQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00005318 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00005319 ESpecType, ESpecRange.getBegin(),
5320 DynamicExceptions.data(),
5321 DynamicExceptionRanges.data(),
5322 DynamicExceptions.size(),
5323 NoexceptExpr.isUsable() ?
Craig Topper161e4db2014-05-21 06:02:52 +00005324 NoexceptExpr.get() : nullptr,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00005325 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005326 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005327 FnAttrs, EndLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005328}
5329
5330/// isFunctionDeclaratorIdentifierList - This parameter list may have an
5331/// identifier list form for a K&R-style function: void foo(a,b,c)
5332///
5333/// Note that identifier-lists are only allowed for normal declarators, not for
5334/// abstract-declarators.
5335bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00005336 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00005337 && Tok.is(tok::identifier)
5338 && !TryAltiVecVectorToken()
5339 // K&R identifier lists can't have typedefs as identifiers, per C99
5340 // 6.7.5.3p11.
5341 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
5342 // Identifier lists follow a really simple grammar: the identifiers can
5343 // be followed *only* by a ", identifier" or ")". However, K&R
5344 // identifier lists are really rare in the brave new modern world, and
5345 // it is very common for someone to typo a type in a non-K&R style
5346 // list. If we are presented with something like: "void foo(intptr x,
5347 // float y)", we don't want to start parsing the function declarator as
5348 // though it is a K&R style declarator just because intptr is an
5349 // invalid type.
5350 //
5351 // To handle this, we check to see if the token after the first
5352 // identifier is a "," or ")". Only then do we parse it as an
5353 // identifier list.
5354 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
5355}
5356
5357/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
5358/// we found a K&R-style identifier list instead of a typed parameter list.
5359///
5360/// After returning, ParamInfo will hold the parsed parameters.
5361///
5362/// identifier-list: [C99 6.7.5]
5363/// identifier
5364/// identifier-list ',' identifier
5365///
5366void Parser::ParseFunctionDeclaratorIdentifierList(
5367 Declarator &D,
Craig Topper5603df42013-07-05 19:34:19 +00005368 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005369 // If there was no identifier specified for the declarator, either we are in
5370 // an abstract-declarator, or we are in a parameter declarator which was found
5371 // to be abstract. In abstract-declarators, identifier lists are not valid:
5372 // diagnose this.
5373 if (!D.getIdentifier())
5374 Diag(Tok, diag::ext_ident_list_in_param);
5375
5376 // Maintain an efficient lookup of params we have seen so far.
5377 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
5378
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005379 do {
Douglas Gregor9e66af42011-07-05 16:44:18 +00005380 // If this isn't an identifier, report the error and skip until ')'.
5381 if (Tok.isNot(tok::identifier)) {
Alp Tokerec543272013-12-24 09:48:30 +00005382 Diag(Tok, diag::err_expected) << tok::identifier;
Alexey Bataevee6507d2013-11-18 08:17:37 +00005383 SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor9e66af42011-07-05 16:44:18 +00005384 // Forget we parsed anything.
5385 ParamInfo.clear();
5386 return;
5387 }
5388
5389 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
5390
5391 // Reject 'typedef int y; int test(x, y)', but continue parsing.
5392 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
5393 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
5394
5395 // Verify that the argument identifier has not already been mentioned.
5396 if (!ParamsSoFar.insert(ParmII)) {
5397 Diag(Tok, diag::err_param_redefinition) << ParmII;
5398 } else {
5399 // Remember this identifier in ParamInfo.
5400 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5401 Tok.getLocation(),
Craig Topper161e4db2014-05-21 06:02:52 +00005402 nullptr));
Douglas Gregor9e66af42011-07-05 16:44:18 +00005403 }
5404
5405 // Eat the identifier.
5406 ConsumeToken();
Douglas Gregor9e66af42011-07-05 16:44:18 +00005407 // The list continues if we see a comma.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005408 } while (TryConsumeToken(tok::comma));
Douglas Gregor9e66af42011-07-05 16:44:18 +00005409}
5410
5411/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
5412/// after the opening parenthesis. This function will not parse a K&R-style
5413/// identifier list.
5414///
Richard Smith2620cd92012-04-11 04:01:28 +00005415/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
5416/// caller parsed those arguments immediately after the open paren - they should
5417/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005418///
5419/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
5420/// be the location of the ellipsis, if any was parsed.
5421///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005422/// parameter-type-list: [C99 6.7.5]
5423/// parameter-list
5424/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005425/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005426///
5427/// parameter-list: [C99 6.7.5]
5428/// parameter-declaration
5429/// parameter-list ',' parameter-declaration
5430///
5431/// parameter-declaration: [C99 6.7.5]
5432/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005433/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00005434/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00005435/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00005436/// declaration-specifiers abstract-declarator[opt]
5437/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00005438/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00005439/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00005440/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00005441///
Douglas Gregor9e66af42011-07-05 16:44:18 +00005442void Parser::ParseParameterDeclarationClause(
5443 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00005444 ParsedAttributes &FirstArgAttrs,
Craig Topper5603df42013-07-05 19:34:19 +00005445 SmallVectorImpl<DeclaratorChunk::ParamInfo> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00005446 SourceLocation &EllipsisLoc) {
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005447 do {
5448 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5449 // before deciding this was a parameter-declaration-clause.
5450 if (TryConsumeToken(tok::ellipsis, EllipsisLoc))
Chris Lattner371ed4e2008-04-06 06:57:35 +00005451 break;
Mike Stump11289f42009-09-09 15:08:12 +00005452
Chris Lattner371ed4e2008-04-06 06:57:35 +00005453 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00005454 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00005455 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005456
Richard Smith2620cd92012-04-11 04:01:28 +00005457 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00005458 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00005459
John McCall53fa7142010-12-24 02:08:15 +00005460 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00005461 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00005462
5463 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00005464
5465 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00005466 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00005467 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00005468 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5469 // too much hassle.
5470 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00005471
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005472 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005473
Faisal Vali2b391ab2013-09-26 19:54:12 +00005474
5475 // Parse the declarator. This is "PrototypeContext" or
5476 // "LambdaExprParameterContext", because we must accept either
5477 // 'declarator' or 'abstract-declarator' here.
5478 Declarator ParmDeclarator(DS,
5479 D.getContext() == Declarator::LambdaExprContext ?
5480 Declarator::LambdaExprParameterContext :
5481 Declarator::PrototypeContext);
5482 ParseDeclarator(ParmDeclarator);
Chris Lattner371ed4e2008-04-06 06:57:35 +00005483
5484 // Parse GNU attributes, if present.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005485 MaybeParseGNUAttributes(ParmDeclarator);
Mike Stump11289f42009-09-09 15:08:12 +00005486
Chris Lattner371ed4e2008-04-06 06:57:35 +00005487 // Remember this parsed parameter in ParamInfo.
Faisal Vali2b391ab2013-09-26 19:54:12 +00005488 IdentifierInfo *ParmII = ParmDeclarator.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005489
Douglas Gregor4d87df52008-12-16 21:30:33 +00005490 // DefArgToks is used when the parsing of default arguments needs
5491 // to be delayed.
Craig Topper161e4db2014-05-21 06:02:52 +00005492 CachedTokens *DefArgToks = nullptr;
Douglas Gregor4d87df52008-12-16 21:30:33 +00005493
Chris Lattner371ed4e2008-04-06 06:57:35 +00005494 // If no parameter was specified, verify that *something* was specified,
5495 // otherwise we have a missing type and identifier.
Craig Topper161e4db2014-05-21 06:02:52 +00005496 if (DS.isEmpty() && ParmDeclarator.getIdentifier() == nullptr &&
Faisal Vali2b391ab2013-09-26 19:54:12 +00005497 ParmDeclarator.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005498 // Completely missing, emit error.
5499 Diag(DSStart, diag::err_missing_param);
5500 } else {
5501 // Otherwise, we have something. Add it and let semantic analysis try
5502 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005503
Richard Smith36ee9fb2014-08-11 23:30:23 +00005504 // Last chance to recover from a misplaced ellipsis in an attempted
5505 // parameter pack declaration.
5506 if (Tok.is(tok::ellipsis) &&
5507 (NextToken().isNot(tok::r_paren) ||
5508 (!ParmDeclarator.getEllipsisLoc().isValid() &&
5509 !Actions.isUnexpandedParameterPackPermitted())) &&
5510 Actions.containsUnexpandedParameterPacks(ParmDeclarator))
5511 DiagnoseMisplacedEllipsisInDeclarator(ConsumeToken(), ParmDeclarator);
5512
Chris Lattner371ed4e2008-04-06 06:57:35 +00005513 // Inform the actions module about the parameter declarator, so it gets
5514 // added to the current scope.
Richard Smith95e1fb02014-08-27 03:23:12 +00005515 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDeclarator);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005516 // Parse the default argument, if any. We parse the default
5517 // arguments in all dialects; the semantic analysis in
5518 // ActOnParamDefaultArgument will reject the default argument in
5519 // C.
5520 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005521 SourceLocation EqualLoc = Tok.getLocation();
5522
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005523 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005524 if (D.getContext() == Declarator::MemberContext) {
5525 // If we're inside a class definition, cache the tokens
5526 // corresponding to the default argument. We'll actually parse
5527 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005528 // FIXME: Can we use a smart pointer for Toks?
5529 DefArgToks = new CachedTokens;
5530
Richard Smith1fff95c2013-09-12 23:28:08 +00005531 if (!ConsumeAndStoreInitializer(*DefArgToks, CIK_DefaultArgument)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005532 delete DefArgToks;
Craig Topper161e4db2014-05-21 06:02:52 +00005533 DefArgToks = nullptr;
Serge Pavlovb4b35782014-07-22 01:54:49 +00005534 Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005535 } else {
5536 // Mark the end of the default argument so that we know when to
5537 // stop when we parse it later on.
5538 Token DefArgEnd;
5539 DefArgEnd.startToken();
5540 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5541 DefArgEnd.setLocation(Tok.getLocation());
5542 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005543 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005544 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005545 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005546 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005547 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005548 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005549
Chad Rosierc1183952012-06-26 22:30:43 +00005550 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005551 // used.
5552 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005553 Sema::PotentiallyEvaluatedIfUsed,
5554 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005555
Sebastian Redldb63af22012-03-14 15:54:00 +00005556 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005557 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005558 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005559 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005560 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005561 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005562 if (DefArgResult.isInvalid()) {
Serge Pavlovb4b35782014-07-22 01:54:49 +00005563 Actions.ActOnParamDefaultArgumentError(Param, EqualLoc);
Alexey Bataevee6507d2013-11-18 08:17:37 +00005564 SkipUntil(tok::comma, tok::r_paren, StopAtSemi | StopBeforeMatch);
Douglas Gregor4d87df52008-12-16 21:30:33 +00005565 } else {
5566 // Inform the actions module about the default argument
5567 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005568 DefArgResult.get());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005569 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005570 }
5571 }
Mike Stump11289f42009-09-09 15:08:12 +00005572
5573 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
Faisal Vali2b391ab2013-09-26 19:54:12 +00005574 ParmDeclarator.getIdentifierLoc(),
5575 Param, DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005576 }
5577
Richard Smith36ee9fb2014-08-11 23:30:23 +00005578 if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) {
5579 if (!getLangOpts().CPlusPlus) {
5580 // We have ellipsis without a preceding ',', which is ill-formed
5581 // in C. Complain and provide the fix.
5582 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
5583 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
5584 } else if (ParmDeclarator.getEllipsisLoc().isValid() ||
5585 Actions.containsUnexpandedParameterPacks(ParmDeclarator)) {
5586 // It looks like this was supposed to be a parameter pack. Warn and
5587 // point out where the ellipsis should have gone.
5588 SourceLocation ParmEllipsis = ParmDeclarator.getEllipsisLoc();
5589 Diag(EllipsisLoc, diag::warn_misplaced_ellipsis_vararg)
5590 << ParmEllipsis.isValid() << ParmEllipsis;
5591 if (ParmEllipsis.isValid()) {
5592 Diag(ParmEllipsis,
5593 diag::note_misplaced_ellipsis_vararg_existing_ellipsis);
5594 } else {
5595 Diag(ParmDeclarator.getIdentifierLoc(),
5596 diag::note_misplaced_ellipsis_vararg_add_ellipsis)
5597 << FixItHint::CreateInsertion(ParmDeclarator.getIdentifierLoc(),
5598 "...")
5599 << !ParmDeclarator.hasName();
5600 }
5601 Diag(EllipsisLoc, diag::note_misplaced_ellipsis_vararg_add_comma)
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005602 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Richard Smith36ee9fb2014-08-11 23:30:23 +00005603 }
5604
5605 // We can't have any more parameters after an ellipsis.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005606 break;
5607 }
Mike Stump11289f42009-09-09 15:08:12 +00005608
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005609 // If the next token is a comma, consume it and keep reading arguments.
5610 } while (TryConsumeToken(tok::comma));
Chris Lattner6c940e62008-04-06 06:34:08 +00005611}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005612
Chris Lattnere8074e62006-08-06 18:30:15 +00005613/// [C90] direct-declarator '[' constant-expression[opt] ']'
5614/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5615/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5616/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5617/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005618/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5619/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005620void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005621 if (CheckProhibitedCXX11Attribute())
5622 return;
5623
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005624 BalancedDelimiterTracker T(*this, tok::l_square);
5625 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005626
Chris Lattner84a11622008-12-18 07:27:21 +00005627 // C array syntax has many features, but by-far the most common is [] and [4].
5628 // This code does a fast path to handle some of the most obvious cases.
5629 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005630 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005631 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005632 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005633
Chris Lattner84a11622008-12-18 07:27:21 +00005634 // Remember that we parsed the empty array type.
Craig Topper161e4db2014-05-21 06:02:52 +00005635 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, nullptr,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005636 T.getOpenLocation(),
5637 T.getCloseLocation()),
5638 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005639 return;
5640 } else if (Tok.getKind() == tok::numeric_constant &&
5641 GetLookAheadToken(1).is(tok::r_square)) {
5642 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005643 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005644 ConsumeToken();
5645
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005646 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005647 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005648 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005649
Chris Lattner84a11622008-12-18 07:27:21 +00005650 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005651 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005652 ExprRes.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005653 T.getOpenLocation(),
5654 T.getCloseLocation()),
5655 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005656 return;
5657 }
Mike Stump11289f42009-09-09 15:08:12 +00005658
Chris Lattnere8074e62006-08-06 18:30:15 +00005659 // If valid, this location is the position where we read the 'static' keyword.
5660 SourceLocation StaticLoc;
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005661 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005662
Chris Lattnere8074e62006-08-06 18:30:15 +00005663 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005664 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005665 DeclSpec DS(AttrFactory);
Aaron Ballman08b06592014-07-22 12:44:22 +00005666 ParseTypeQualifierListOpt(DS, AR_CXX11AttributesParsed);
Mike Stump11289f42009-09-09 15:08:12 +00005667
Chris Lattnere8074e62006-08-06 18:30:15 +00005668 // If we haven't already read 'static', check to see if there is one after the
5669 // type-qualifier-list.
Alp Tokera3ebe6e2013-12-17 14:12:37 +00005670 if (!StaticLoc.isValid())
5671 TryConsumeToken(tok::kw_static, StaticLoc);
Mike Stump11289f42009-09-09 15:08:12 +00005672
Chris Lattnere8074e62006-08-06 18:30:15 +00005673 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005674 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005675 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005676
Chris Lattner521ff2b2008-04-06 05:26:30 +00005677 // Handle the case where we have '[*]' as the array size. However, a leading
5678 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005679 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005680 // infrequent, use of lookahead is not costly here.
5681 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005682 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005683
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005684 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005685 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005686 StaticLoc = SourceLocation(); // Drop the static.
5687 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005688 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005689 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005690 // Note, in C89, this production uses the constant-expr production instead
5691 // of assignment-expr. The only difference is that assignment-expr allows
5692 // things like '=' and '*='. Sema rejects these in C89 mode because they
5693 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005694
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005695 // Parse the constant-expression or assignment-expression now (depending
5696 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005697 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005698 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005699 } else {
5700 EnterExpressionEvaluationContext Unevaluated(Actions,
5701 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005702 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005703 }
David Majnemerf9834d52014-08-08 07:21:18 +00005704 } else {
5705 if (StaticLoc.isValid()) {
5706 Diag(StaticLoc, diag::err_unspecified_size_with_static);
5707 StaticLoc = SourceLocation(); // Drop the static.
5708 }
Chris Lattner62591722006-08-12 18:40:58 +00005709 }
Mike Stump11289f42009-09-09 15:08:12 +00005710
Chris Lattner62591722006-08-12 18:40:58 +00005711 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005712 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005713 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005714 // If the expression was invalid, skip it.
Alexey Bataevee6507d2013-11-18 08:17:37 +00005715 SkipUntil(tok::r_square, StopAtSemi);
Chris Lattner62591722006-08-12 18:40:58 +00005716 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005717 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005718
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005719 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005720
John McCall084e83d2011-03-24 11:26:52 +00005721 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005722 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005723
Chris Lattner84a11622008-12-18 07:27:21 +00005724 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005725 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005726 StaticLoc.isValid(), isStar,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005727 NumElements.get(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005728 T.getOpenLocation(),
5729 T.getCloseLocation()),
5730 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005731}
5732
Richard Trieuf4b81d02014-06-24 23:14:24 +00005733/// Diagnose brackets before an identifier.
5734void Parser::ParseMisplacedBracketDeclarator(Declarator &D) {
5735 assert(Tok.is(tok::l_square) && "Missing opening bracket");
5736 assert(!D.mayOmitIdentifier() && "Declarator cannot omit identifier");
5737
5738 SourceLocation StartBracketLoc = Tok.getLocation();
5739 Declarator TempDeclarator(D.getDeclSpec(), D.getContext());
5740
5741 while (Tok.is(tok::l_square)) {
5742 ParseBracketDeclarator(TempDeclarator);
5743 }
5744
5745 // Stuff the location of the start of the brackets into the Declarator.
5746 // The diagnostics from ParseDirectDeclarator will make more sense if
5747 // they use this location instead.
5748 if (Tok.is(tok::semi))
5749 D.getName().EndLocation = StartBracketLoc;
5750
5751 SourceLocation SuggestParenLoc = Tok.getLocation();
5752
5753 // Now that the brackets are removed, try parsing the declarator again.
5754 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
5755
5756 // Something went wrong parsing the brackets, in which case,
5757 // ParseBracketDeclarator has emitted an error, and we don't need to emit
5758 // one here.
5759 if (TempDeclarator.getNumTypeObjects() == 0)
5760 return;
5761
5762 // Determine if parens will need to be suggested in the diagnostic.
5763 bool NeedParens = false;
5764 if (D.getNumTypeObjects() != 0) {
5765 switch (D.getTypeObject(D.getNumTypeObjects() - 1).Kind) {
5766 case DeclaratorChunk::Pointer:
5767 case DeclaratorChunk::Reference:
5768 case DeclaratorChunk::BlockPointer:
5769 case DeclaratorChunk::MemberPointer:
5770 NeedParens = true;
5771 break;
5772 case DeclaratorChunk::Array:
5773 case DeclaratorChunk::Function:
5774 case DeclaratorChunk::Paren:
5775 break;
5776 }
5777 }
5778
5779 if (NeedParens) {
5780 // Create a DeclaratorChunk for the inserted parens.
5781 ParsedAttributes attrs(AttrFactory);
5782 SourceLocation EndLoc = PP.getLocForEndOfToken(D.getLocEnd());
5783 D.AddTypeInfo(DeclaratorChunk::getParen(SuggestParenLoc, EndLoc), attrs,
5784 SourceLocation());
5785 }
5786
5787 // Adding back the bracket info to the end of the Declarator.
5788 for (unsigned i = 0, e = TempDeclarator.getNumTypeObjects(); i < e; ++i) {
5789 const DeclaratorChunk &Chunk = TempDeclarator.getTypeObject(i);
5790 ParsedAttributes attrs(AttrFactory);
5791 attrs.set(Chunk.Common.AttrList);
5792 D.AddTypeInfo(Chunk, attrs, SourceLocation());
5793 }
5794
5795 // The missing identifier would have been diagnosed in ParseDirectDeclarator.
5796 // If parentheses are required, always suggest them.
5797 if (!D.getIdentifier() && !NeedParens)
5798 return;
5799
5800 SourceLocation EndBracketLoc = TempDeclarator.getLocEnd();
5801
5802 // Generate the move bracket error message.
5803 SourceRange BracketRange(StartBracketLoc, EndBracketLoc);
5804 SourceLocation EndLoc = PP.getLocForEndOfToken(D.getLocEnd());
5805
5806 if (NeedParens) {
5807 Diag(EndLoc, diag::err_brackets_go_after_unqualified_id)
5808 << getLangOpts().CPlusPlus
5809 << FixItHint::CreateInsertion(SuggestParenLoc, "(")
5810 << FixItHint::CreateInsertion(EndLoc, ")")
5811 << FixItHint::CreateInsertionFromRange(
5812 EndLoc, CharSourceRange(BracketRange, true))
5813 << FixItHint::CreateRemoval(BracketRange);
5814 } else {
5815 Diag(EndLoc, diag::err_brackets_go_after_unqualified_id)
5816 << getLangOpts().CPlusPlus
5817 << FixItHint::CreateInsertionFromRange(
5818 EndLoc, CharSourceRange(BracketRange, true))
5819 << FixItHint::CreateRemoval(BracketRange);
5820 }
5821}
5822
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005823/// [GNU] typeof-specifier:
5824/// typeof ( expressions )
5825/// typeof ( type-name )
5826/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005827///
5828void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005829 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005830 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005831 SourceLocation StartLoc = ConsumeToken();
5832
John McCalle8595032010-01-13 20:03:27 +00005833 const bool hasParens = Tok.is(tok::l_paren);
5834
Eli Friedman15681d62012-09-26 04:34:21 +00005835 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5836 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005837
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005838 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005839 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005840 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005841 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5842 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005843 if (hasParens)
5844 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005845
5846 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005847 // FIXME: Not accurate, the range gets one token more than it should.
5848 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005849 else
5850 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005851
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005852 if (isCastExpr) {
5853 if (!CastTy) {
5854 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005855 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005856 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005857
Craig Topper161e4db2014-05-21 06:02:52 +00005858 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +00005859 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005860 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5861 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005862 DiagID, CastTy,
5863 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00005864 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005865 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005866 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005867
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005868 // If we get here, the operand to the typeof was an expresion.
5869 if (Operand.isInvalid()) {
5870 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005871 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005872 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005873
Eli Friedmane0afc982012-01-21 01:01:51 +00005874 // We might need to transform the operand if it is potentially evaluated.
5875 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5876 if (Operand.isInvalid()) {
5877 DS.SetTypeSpecError();
5878 return;
5879 }
5880
Craig Topper161e4db2014-05-21 06:02:52 +00005881 const char *PrevSpec = nullptr;
John McCall49bfce42009-08-03 20:12:06 +00005882 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005883 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5884 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005885 DiagID, Operand.get(),
5886 Actions.getASTContext().getPrintingPolicy()))
John McCall49bfce42009-08-03 20:12:06 +00005887 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005888}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005889
Benjamin Kramere56f3932011-12-23 17:00:35 +00005890/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005891/// _Atomic ( type-name )
5892///
5893void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
Richard Smith8e1ac332013-03-28 01:55:44 +00005894 assert(Tok.is(tok::kw__Atomic) && NextToken().is(tok::l_paren) &&
5895 "Not an atomic specifier");
Eli Friedman0dfb8892011-10-06 23:00:33 +00005896
5897 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005898 BalancedDelimiterTracker T(*this, tok::l_paren);
Richard Smith8e1ac332013-03-28 01:55:44 +00005899 if (T.consumeOpen())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005900 return;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005901
5902 TypeResult Result = ParseTypeName();
5903 if (Result.isInvalid()) {
Alexey Bataevee6507d2013-11-18 08:17:37 +00005904 SkipUntil(tok::r_paren, StopAtSemi);
Eli Friedman0dfb8892011-10-06 23:00:33 +00005905 return;
5906 }
5907
5908 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005909 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005910
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005911 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005912 return;
5913
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005914 DS.setTypeofParensRange(T.getRange());
5915 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005916
Craig Topper161e4db2014-05-21 06:02:52 +00005917 const char *PrevSpec = nullptr;
Eli Friedman0dfb8892011-10-06 23:00:33 +00005918 unsigned DiagID;
5919 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
Nikola Smiljanic01a75982014-05-29 10:55:11 +00005920 DiagID, Result.get(),
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005921 Actions.getASTContext().getPrintingPolicy()))
Eli Friedman0dfb8892011-10-06 23:00:33 +00005922 Diag(StartLoc, DiagID) << PrevSpec;
5923}
5924
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005925
5926/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5927/// from TryAltiVecVectorToken.
5928bool Parser::TryAltiVecVectorTokenOutOfLine() {
5929 Token Next = NextToken();
5930 switch (Next.getKind()) {
5931 default: return false;
5932 case tok::kw_short:
5933 case tok::kw_long:
5934 case tok::kw_signed:
5935 case tok::kw_unsigned:
5936 case tok::kw_void:
5937 case tok::kw_char:
5938 case tok::kw_int:
5939 case tok::kw_float:
5940 case tok::kw_double:
5941 case tok::kw_bool:
5942 case tok::kw___pixel:
5943 Tok.setKind(tok::kw___vector);
5944 return true;
5945 case tok::identifier:
5946 if (Next.getIdentifierInfo() == Ident_pixel) {
5947 Tok.setKind(tok::kw___vector);
5948 return true;
5949 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005950 if (Next.getIdentifierInfo() == Ident_bool) {
5951 Tok.setKind(tok::kw___vector);
5952 return true;
5953 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005954 return false;
5955 }
5956}
5957
5958bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5959 const char *&PrevSpec, unsigned &DiagID,
5960 bool &isInvalid) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005961 const PrintingPolicy &Policy = Actions.getASTContext().getPrintingPolicy();
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005962 if (Tok.getIdentifierInfo() == Ident_vector) {
5963 Token Next = NextToken();
5964 switch (Next.getKind()) {
5965 case tok::kw_short:
5966 case tok::kw_long:
5967 case tok::kw_signed:
5968 case tok::kw_unsigned:
5969 case tok::kw_void:
5970 case tok::kw_char:
5971 case tok::kw_int:
5972 case tok::kw_float:
5973 case tok::kw_double:
5974 case tok::kw_bool:
5975 case tok::kw___pixel:
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005976 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID, Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005977 return true;
5978 case tok::identifier:
5979 if (Next.getIdentifierInfo() == Ident_pixel) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005980 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005981 return true;
5982 }
Bill Schmidt99a084b2013-07-03 20:54:09 +00005983 if (Next.getIdentifierInfo() == Ident_bool) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005984 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID,Policy);
Bill Schmidt99a084b2013-07-03 20:54:09 +00005985 return true;
5986 }
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005987 break;
5988 default:
5989 break;
5990 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005991 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005992 DS.isTypeAltiVecVector()) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005993 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID, Policy);
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005994 return true;
Bill Schmidt99a084b2013-07-03 20:54:09 +00005995 } else if ((Tok.getIdentifierInfo() == Ident_bool) &&
5996 DS.isTypeAltiVecVector()) {
Erik Verbruggen888d52a2014-01-15 09:15:43 +00005997 isInvalid = DS.SetTypeAltiVecBool(true, Loc, PrevSpec, DiagID, Policy);
Bill Schmidt99a084b2013-07-03 20:54:09 +00005998 return true;
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005999 }
6000 return false;
6001}