blob: 14fd581ddcb11f561604df0832f0305767867a4b [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"
Benjamin Kramerd7d2b1f2012-12-01 16:35:25 +000016#include "clang/Basic/AddressSpaces.h"
Peter Collingbourne599cb8e2011-03-18 22:38:29 +000017#include "clang/Basic/OpenCL.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000018#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrain031643e2012-04-26 23:36:17 +000019#include "clang/Sema/Lookup.h"
John McCall8b0666c2010-08-20 18:27:03 +000020#include "clang/Sema/ParsedTemplate.h"
John McCallfaf5fb42010-08-26 23:41:50 +000021#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth3a022472012-12-04 09:13:33 +000022#include "clang/Sema/Scope.h"
Chris Lattnerad9ac942007-01-23 01:14:52 +000023#include "llvm/ADT/SmallSet.h"
Benjamin Kramer49038022012-02-04 13:45:25 +000024#include "llvm/ADT/SmallString.h"
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +000025#include "llvm/ADT/StringSwitch.h"
Chris Lattnerc0acd3d2006-07-31 05:13:43 +000026using namespace clang;
27
28//===----------------------------------------------------------------------===//
29// C99 6.7: Declarations.
30//===----------------------------------------------------------------------===//
31
Chris Lattnerf5fbd792006-08-10 23:56:11 +000032/// ParseTypeName
33/// type-name: [C99 6.7.6]
34/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +000035///
36/// Called type-id in C++.
Douglas Gregor205d5e32011-01-31 16:09:46 +000037TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCall31168b02011-06-15 23:02:42 +000038 Declarator::TheContext Context,
Richard Smithcd1c0552011-07-01 19:46:12 +000039 AccessSpecifier AS,
40 Decl **OwnedType) {
Richard Smith62dad822012-03-15 01:02:11 +000041 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smith2f07ad52012-05-09 20:55:26 +000042 if (DSC == DSC_normal)
43 DSC = DSC_type_specifier;
Richard Smithbfdb1082012-03-12 08:56:40 +000044
Chris Lattnerf5fbd792006-08-10 23:56:11 +000045 // Parse the common declaration-specifiers piece.
John McCall084e83d2011-03-24 11:26:52 +000046 DeclSpec DS(AttrFactory);
Richard Smithbfdb1082012-03-12 08:56:40 +000047 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithcd1c0552011-07-01 19:46:12 +000048 if (OwnedType)
49 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redld6434562009-05-29 18:02:33 +000050
Chris Lattnerf5fbd792006-08-10 23:56:11 +000051 // Parse the abstract-declarator, if present.
Douglas Gregor205d5e32011-01-31 16:09:46 +000052 Declarator DeclaratorInfo(DS, Context);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000053 ParseDeclarator(DeclaratorInfo);
Sebastian Redld6434562009-05-29 18:02:33 +000054 if (Range)
55 *Range = DeclaratorInfo.getSourceRange();
56
Chris Lattnerf6d1c9c2009-04-25 08:06:05 +000057 if (DeclaratorInfo.isInvalidType())
Douglas Gregor220cac52009-02-18 17:45:20 +000058 return true;
59
Douglas Gregor0be31a22010-07-02 17:43:08 +000060 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Chris Lattnerf5fbd792006-08-10 23:56:11 +000061}
62
Caitlin Sadowski9385dd72011-09-08 17:42:22 +000063
64/// isAttributeLateParsed - Return true if the attribute has arguments that
65/// require late parsing.
66static bool isAttributeLateParsed(const IdentifierInfo &II) {
67 return llvm::StringSwitch<bool>(II.getName())
68#include "clang/Parse/AttrLateParsed.inc"
69 .Default(false);
70}
71
Alexis Hunt96d5c762009-11-21 08:43:09 +000072/// ParseGNUAttributes - Parse a non-empty attributes list.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000073///
74/// [GNU] attributes:
75/// attribute
76/// attributes attribute
77///
78/// [GNU] attribute:
79/// '__attribute__' '(' '(' attribute-list ')' ')'
80///
81/// [GNU] attribute-list:
82/// attrib
83/// attribute_list ',' attrib
84///
85/// [GNU] attrib:
86/// empty
Steve Naroff0f2fe172007-06-01 17:11:19 +000087/// attrib-name
88/// attrib-name '(' identifier ')'
89/// attrib-name '(' identifier ',' nonempty-expr-list ')'
90/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
Chris Lattnerb8cd5c22006-08-15 04:10:46 +000091///
Steve Naroff0f2fe172007-06-01 17:11:19 +000092/// [GNU] attrib-name:
93/// identifier
94/// typespec
95/// typequal
96/// storageclass
Mike Stump11289f42009-09-09 15:08:12 +000097///
Steve Naroff0f2fe172007-06-01 17:11:19 +000098/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump11289f42009-09-09 15:08:12 +000099/// token lookahead. Comment from gcc: "If they start with an identifier
100/// which is followed by a comma or close parenthesis, then the arguments
Steve Naroff0f2fe172007-06-01 17:11:19 +0000101/// start with that identifier; otherwise they are an expression list."
102///
Richard Smithb12bf692011-10-17 21:20:17 +0000103/// GCC does not require the ',' between attribs in an attribute-list.
104///
Steve Naroff0f2fe172007-06-01 17:11:19 +0000105/// At the moment, I am not doing 2 token lookahead. I am also unaware of
106/// any attributes that don't work (based on my limited testing). Most
107/// attributes are very simple in practice. Until we find a bug, I don't see
108/// a pressing need to implement the 2 token lookahead.
Chris Lattnerb8cd5c22006-08-15 04:10:46 +0000109
John McCall53fa7142010-12-24 02:08:15 +0000110void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000111 SourceLocation *endLoc,
112 LateParsedAttrList *LateAttrs) {
Alexis Hunt96d5c762009-11-21 08:43:09 +0000113 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump11289f42009-09-09 15:08:12 +0000114
Chris Lattner76c72282007-10-09 17:33:22 +0000115 while (Tok.is(tok::kw___attribute)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000116 ConsumeToken();
117 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
118 "attribute")) {
119 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000120 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000121 }
122 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
123 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall53fa7142010-12-24 02:08:15 +0000124 return;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000125 }
126 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner76c72282007-10-09 17:33:22 +0000127 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
128 Tok.is(tok::comma)) {
Mike Stump11289f42009-09-09 15:08:12 +0000129 if (Tok.is(tok::comma)) {
Steve Naroff0f2fe172007-06-01 17:11:19 +0000130 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
131 ConsumeToken();
132 continue;
133 }
134 // we have an identifier or declaration specifier (const, int, etc.)
135 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
136 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +0000137
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000138 if (Tok.is(tok::l_paren)) {
139 // handle "parameterized" attributes
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000140 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000141 LateParsedAttribute *LA =
142 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
143 LateAttrs->push_back(LA);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000144
Bill Wendling44426052012-12-20 19:22:21 +0000145 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000146 // with other late-parsed declarations.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000147 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000148 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump11289f42009-09-09 15:08:12 +0000149
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000150 // consume everything up to and including the matching right parens
151 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump11289f42009-09-09 15:08:12 +0000152
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000153 Token Eof;
154 Eof.startToken();
155 Eof.setLocation(Tok.getLocation());
156 LA->Toks.push_back(Eof);
157 } else {
Michael Han23214e52012-10-03 01:56:22 +0000158 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000159 0, SourceLocation(), AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000160 }
161 } else {
John McCall084e83d2011-03-24 11:26:52 +0000162 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Alexis Hunta0e54d42012-06-18 16:13:52 +0000163 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Steve Naroff0f2fe172007-06-01 17:11:19 +0000164 }
165 }
Steve Naroff98d153c2007-06-06 23:19:11 +0000166 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Steve Naroff98d153c2007-06-06 23:19:11 +0000167 SkipUntil(tok::r_paren, false);
Alexis Hunt96d5c762009-11-21 08:43:09 +0000168 SourceLocation Loc = Tok.getLocation();
Sebastian Redlf6591ca2009-02-09 18:23:29 +0000169 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
170 SkipUntil(tok::r_paren, false);
171 }
John McCall53fa7142010-12-24 02:08:15 +0000172 if (endLoc)
173 *endLoc = Loc;
Steve Naroff0f2fe172007-06-01 17:11:19 +0000174 }
Steve Naroff0f2fe172007-06-01 17:11:19 +0000175}
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000176
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000177
Michael Han23214e52012-10-03 01:56:22 +0000178/// Parse the arguments to a parameterized GNU attribute or
179/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000180void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
181 SourceLocation AttrNameLoc,
182 ParsedAttributes &Attrs,
Michael Han23214e52012-10-03 01:56:22 +0000183 SourceLocation *EndLoc,
184 IdentifierInfo *ScopeName,
185 SourceLocation ScopeLoc,
186 AttributeList::Syntax Syntax) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000187
188 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
189
190 // Availability attributes have their own grammar.
191 if (AttrName->isStr("availability")) {
192 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
193 return;
194 }
195 // Thread safety attributes fit into the FIXME case above, so we
196 // just parse the arguments as a list of expressions
197 if (IsThreadSafetyAttribute(AttrName->getName())) {
198 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
199 return;
200 }
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +0000201 // Type safety attributes have their own grammar.
202 if (AttrName->isStr("type_tag_for_datatype")) {
203 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
204 return;
205 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000206
207 ConsumeParen(); // ignore the left paren loc for now
208
Richard Smithb12bf692011-10-17 21:20:17 +0000209 IdentifierInfo *ParmName = 0;
210 SourceLocation ParmLoc;
211 bool BuiltinType = false;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000212
Richard Smithb12bf692011-10-17 21:20:17 +0000213 switch (Tok.getKind()) {
214 case tok::kw_char:
215 case tok::kw_wchar_t:
216 case tok::kw_char16_t:
217 case tok::kw_char32_t:
218 case tok::kw_bool:
219 case tok::kw_short:
220 case tok::kw_int:
221 case tok::kw_long:
222 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +0000223 case tok::kw___int128:
Richard Smithb12bf692011-10-17 21:20:17 +0000224 case tok::kw_signed:
225 case tok::kw_unsigned:
226 case tok::kw_float:
227 case tok::kw_double:
228 case tok::kw_void:
229 case tok::kw_typeof:
230 // __attribute__(( vec_type_hint(char) ))
231 // FIXME: Don't just discard the builtin type token.
232 ConsumeToken();
233 BuiltinType = true;
234 break;
235
236 case tok::identifier:
237 ParmName = Tok.getIdentifierInfo();
238 ParmLoc = ConsumeToken();
239 break;
240
241 default:
242 break;
243 }
244
Benjamin Kramerf0623432012-08-23 22:51:59 +0000245 ExprVector ArgExprs;
Richard Smithb12bf692011-10-17 21:20:17 +0000246
247 if (!BuiltinType &&
248 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
249 // Eat the comma.
250 if (ParmLoc.isValid())
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000251 ConsumeToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000252
Richard Smithb12bf692011-10-17 21:20:17 +0000253 // Parse the non-empty comma-separated list of expressions.
254 while (1) {
255 ExprResult ArgExpr(ParseAssignmentExpression());
256 if (ArgExpr.isInvalid()) {
257 SkipUntil(tok::r_paren);
258 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000259 }
Richard Smithb12bf692011-10-17 21:20:17 +0000260 ArgExprs.push_back(ArgExpr.release());
261 if (Tok.isNot(tok::comma))
262 break;
263 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000264 }
Richard Smithb12bf692011-10-17 21:20:17 +0000265 }
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000266 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
267 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
268 tok::greater)) {
Fariborz Jahanian7f733022011-10-18 23:13:50 +0000269 while (Tok.is(tok::identifier)) {
270 ConsumeToken();
271 if (Tok.is(tok::greater))
272 break;
273 if (Tok.is(tok::comma)) {
274 ConsumeToken();
275 continue;
276 }
277 }
278 if (Tok.isNot(tok::greater))
279 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian6b708652011-10-18 17:11:10 +0000280 SkipUntil(tok::r_paren, false, true); // skip until ')'
281 }
282 }
Richard Smithb12bf692011-10-17 21:20:17 +0000283
284 SourceLocation RParen = Tok.getLocation();
285 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
Michael Han360d2252012-10-04 16:42:52 +0000286 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithb12bf692011-10-17 21:20:17 +0000287 AttributeList *attr =
Michael Han360d2252012-10-04 16:42:52 +0000288 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen),
Michael Han23214e52012-10-03 01:56:22 +0000289 ScopeName, ScopeLoc, ParmName, ParmLoc,
290 ArgExprs.data(), ArgExprs.size(), Syntax);
Alexis Hunt3bc72c12012-06-19 23:57:03 +0000291 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithb12bf692011-10-17 21:20:17 +0000292 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000293 }
294}
295
Chad Rosierc1183952012-06-26 22:30:43 +0000296/// \brief Parses a single argument for a declspec, including the
Aaron Ballman478faed2012-06-19 22:09:27 +0000297/// surrounding parens.
Chad Rosierc1183952012-06-26 22:30:43 +0000298void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballman478faed2012-06-19 22:09:27 +0000299 SourceLocation AttrNameLoc,
300 ParsedAttributes &Attrs)
301{
302 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000303 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000304 AttrName->getNameStart(), tok::r_paren))
305 return;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000306
Aaron Ballman478faed2012-06-19 22:09:27 +0000307 ExprResult ArgExpr(ParseConstantExpression());
308 if (ArgExpr.isInvalid()) {
309 T.skipToEnd();
310 return;
311 }
312 Expr *ExprList = ArgExpr.take();
Chad Rosierc1183952012-06-26 22:30:43 +0000313 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000314 &ExprList, 1, AttributeList::AS_Declspec);
315
316 T.consumeClose();
317}
318
Chad Rosierc1183952012-06-26 22:30:43 +0000319/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballman478faed2012-06-19 22:09:27 +0000320/// arguments.
321bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
322 return llvm::StringSwitch<bool>(Ident->getName())
323 .Case("dllimport", true)
324 .Case("dllexport", true)
325 .Case("noreturn", true)
326 .Case("nothrow", true)
327 .Case("noinline", true)
328 .Case("naked", true)
329 .Case("appdomain", true)
330 .Case("process", true)
331 .Case("jitintrinsic", true)
332 .Case("noalias", true)
333 .Case("restrict", true)
334 .Case("novtable", true)
335 .Case("selectany", true)
336 .Case("thread", true)
337 .Default(false);
338}
339
Chad Rosierc1183952012-06-26 22:30:43 +0000340/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballman478faed2012-06-19 22:09:27 +0000341/// parameters). Will return false if we properly handled the declspec, or
342/// true if it is an unknown declspec.
Chad Rosierc1183952012-06-26 22:30:43 +0000343void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballman478faed2012-06-19 22:09:27 +0000344 SourceLocation Loc,
345 ParsedAttributes &Attrs) {
346 // Try to handle the easy case first -- these declspecs all take a single
347 // parameter as their argument.
348 if (llvm::StringSwitch<bool>(Ident->getName())
349 .Case("uuid", true)
350 .Case("align", true)
351 .Case("allocate", true)
352 .Default(false)) {
353 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
354 } else if (Ident->getName() == "deprecated") {
Chad Rosierc1183952012-06-26 22:30:43 +0000355 // The deprecated declspec has an optional single argument, so we will
356 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballman478faed2012-06-19 22:09:27 +0000357 // not.
358 if (Tok.getKind() == tok::l_paren)
359 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
360 else
Chad Rosierc1183952012-06-26 22:30:43 +0000361 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballman478faed2012-06-19 22:09:27 +0000362 AttributeList::AS_Declspec);
363 } else if (Ident->getName() == "property") {
364 // The property declspec is more complex in that it can take one or two
Chad Rosierc1183952012-06-26 22:30:43 +0000365 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballman478faed2012-06-19 22:09:27 +0000366 // must be named get or put.
367 //
Chad Rosierc1183952012-06-26 22:30:43 +0000368 // For right now, we will just skip to the closing right paren of the
Aaron Ballman478faed2012-06-19 22:09:27 +0000369 // property expression.
370 //
371 // FIXME: we should deal with __declspec(property) at some point because it
372 // is used in the platform SDK headers for the Parallel Patterns Library
373 // and ATL.
374 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000375 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballman478faed2012-06-19 22:09:27 +0000376 Ident->getNameStart(), tok::r_paren))
377 return;
378 T.skipToEnd();
379 } else {
380 // We don't recognize this as a valid declspec, but instead of creating the
381 // attribute and allowing sema to warn about it, we will warn here instead.
382 // This is because some attributes have multiple spellings, but we need to
383 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosierc1183952012-06-26 22:30:43 +0000384 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballman478faed2012-06-19 22:09:27 +0000385 // both locations.
386 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
387
388 // If there's an open paren, we should eat the open and close parens under
389 // the assumption that this unknown declspec has parameters.
390 BalancedDelimiterTracker T(*this, tok::l_paren);
391 if (!T.consumeOpen())
392 T.skipToEnd();
393 }
394}
395
Eli Friedman06de2b52009-06-08 07:21:15 +0000396/// [MS] decl-specifier:
397/// __declspec ( extended-decl-modifier-seq )
398///
399/// [MS] extended-decl-modifier-seq:
400/// extended-decl-modifier[opt]
401/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballman478faed2012-06-19 22:09:27 +0000402void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000403 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedman06de2b52009-06-08 07:21:15 +0000404
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000405 ConsumeToken();
Aaron Ballman478faed2012-06-19 22:09:27 +0000406 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosierc1183952012-06-26 22:30:43 +0000407 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballman478faed2012-06-19 22:09:27 +0000408 tok::r_paren))
John McCall53fa7142010-12-24 02:08:15 +0000409 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000410
Chad Rosierc1183952012-06-26 22:30:43 +0000411 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballman478faed2012-06-19 22:09:27 +0000412 // you can specify multiple attributes per declspec.
413 while (Tok.getKind() != tok::r_paren) {
414 // We expect either a well-known identifier or a generic string. Anything
415 // else is a malformed declspec.
416 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosierc1183952012-06-26 22:30:43 +0000417 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballman478faed2012-06-19 22:09:27 +0000418 Tok.getKind() != tok::kw_restrict) {
419 Diag(Tok, diag::err_ms_declspec_type);
420 T.skipToEnd();
421 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000422 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000423
424 IdentifierInfo *AttrName;
425 SourceLocation AttrNameLoc;
426 if (IsString) {
427 SmallString<8> StrBuffer;
428 bool Invalid = false;
429 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
430 if (Invalid) {
431 T.skipToEnd();
432 return;
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000433 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000434 AttrName = PP.getIdentifierInfo(Str);
435 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000436 } else {
Aaron Ballman478faed2012-06-19 22:09:27 +0000437 AttrName = Tok.getIdentifierInfo();
438 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000439 }
Chad Rosierc1183952012-06-26 22:30:43 +0000440
Aaron Ballman478faed2012-06-19 22:09:27 +0000441 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosierc1183952012-06-26 22:30:43 +0000442 // If we have a generic string, we will allow it because there is no
443 // documented list of allowable string declspecs, but we know they exist
Aaron Ballman478faed2012-06-19 22:09:27 +0000444 // (for instance, SAL declspecs in older versions of MSVC).
445 //
Chad Rosierc1183952012-06-26 22:30:43 +0000446 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballman478faed2012-06-19 22:09:27 +0000447 // arguments and can be turned into an attribute directly.
Chad Rosierc1183952012-06-26 22:30:43 +0000448 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballman478faed2012-06-19 22:09:27 +0000449 0, 0, AttributeList::AS_Declspec);
450 else
451 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesene1c0ae62012-06-19 21:48:43 +0000452 }
Aaron Ballman478faed2012-06-19 22:09:27 +0000453 T.consumeClose();
Eli Friedman53339e02009-06-08 23:27:34 +0000454}
455
John McCall53fa7142010-12-24 02:08:15 +0000456void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman53339e02009-06-08 23:27:34 +0000457 // Treat these like attributes
Eli Friedman53339e02009-06-08 23:27:34 +0000458 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregora941dca2010-05-18 16:57:00 +0000459 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet17ed0202011-08-18 09:59:55 +0000460 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Chad Rosier6c0da112012-12-21 21:27:13 +0000461 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
Eli Friedman53339e02009-06-08 23:27:34 +0000462 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
463 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000464 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith0cdcc982013-01-29 01:24:26 +0000465 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Eli Friedman53339e02009-06-08 23:27:34 +0000466 }
Steve Naroff3a9b7e02008-12-24 20:59:21 +0000467}
468
John McCall53fa7142010-12-24 02:08:15 +0000469void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik335e16b2010-09-03 01:29:35 +0000470 // Treat these like attributes
471 while (Tok.is(tok::kw___pascal)) {
472 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
473 SourceLocation AttrNameLoc = ConsumeToken();
John McCall084e83d2011-03-24 11:26:52 +0000474 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith0cdcc982013-01-29 01:24:26 +0000475 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Dawn Perchik335e16b2010-09-03 01:29:35 +0000476 }
John McCall53fa7142010-12-24 02:08:15 +0000477}
478
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000479void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
480 // Treat these like attributes
481 while (Tok.is(tok::kw___kernel)) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000482 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000483 SourceLocation AttrNameLoc = ConsumeToken();
Richard Smith0cdcc982013-01-29 01:24:26 +0000484 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
485 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +0000486 }
487}
488
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000489void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith0cdcc982013-01-29 01:24:26 +0000490 // FIXME: The mapping from attribute spelling to semantics should be
491 // performed in Sema, not here.
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000492 SourceLocation Loc = Tok.getLocation();
493 switch(Tok.getKind()) {
494 // OpenCL qualifiers:
495 case tok::kw___private:
Chad Rosierc1183952012-06-26 22:30:43 +0000496 case tok::kw_private:
John McCall084e83d2011-03-24 11:26:52 +0000497 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000498 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000499 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000500 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000501
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000502 case tok::kw___global:
John McCall084e83d2011-03-24 11:26:52 +0000503 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000504 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000505 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000506 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000507
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000508 case tok::kw___local:
John McCall084e83d2011-03-24 11:26:52 +0000509 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000510 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000511 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000512 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000513
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000514 case tok::kw___constant:
John McCall084e83d2011-03-24 11:26:52 +0000515 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000516 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000517 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000518 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000519
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000520 case tok::kw___read_only:
John McCall084e83d2011-03-24 11:26:52 +0000521 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000522 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000523 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000524 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000525
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000526 case tok::kw___write_only:
John McCall084e83d2011-03-24 11:26:52 +0000527 DS.getAttributes().addNewInteger(
Chad Rosierc1183952012-06-26 22:30:43 +0000528 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000529 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000530 break;
Chad Rosierc1183952012-06-26 22:30:43 +0000531
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000532 case tok::kw___read_write:
John McCall084e83d2011-03-24 11:26:52 +0000533 DS.getAttributes().addNewInteger(
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000534 Actions.getASTContext(),
John McCall084e83d2011-03-24 11:26:52 +0000535 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne599cb8e2011-03-18 22:38:29 +0000536 break;
537 default: break;
538 }
539}
540
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000541/// \brief Parse a version number.
542///
543/// version:
544/// simple-integer
545/// simple-integer ',' simple-integer
546/// simple-integer ',' simple-integer ',' simple-integer
547VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
548 Range = Tok.getLocation();
549
550 if (!Tok.is(tok::numeric_constant)) {
551 Diag(Tok, diag::err_expected_version);
552 SkipUntil(tok::comma, tok::r_paren, true, true, true);
553 return VersionTuple();
554 }
555
556 // Parse the major (and possibly minor and subminor) versions, which
557 // are stored in the numeric constant. We utilize a quirk of the
558 // lexer, which is that it handles something like 1.2.3 as a single
559 // numeric constant, rather than two separate tokens.
Dylan Noblesmith2c1dd272012-02-05 02:13:05 +0000560 SmallString<512> Buffer;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000561 Buffer.resize(Tok.getLength()+1);
562 const char *ThisTokBegin = &Buffer[0];
563
564 // Get the spelling of the token, which eliminates trigraphs, etc.
565 bool Invalid = false;
566 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
567 if (Invalid)
568 return VersionTuple();
569
570 // Parse the major version.
571 unsigned AfterMajor = 0;
572 unsigned Major = 0;
573 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
574 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
575 ++AfterMajor;
576 }
577
578 if (AfterMajor == 0) {
579 Diag(Tok, diag::err_expected_version);
580 SkipUntil(tok::comma, tok::r_paren, true, true, true);
581 return VersionTuple();
582 }
583
584 if (AfterMajor == ActualLength) {
585 ConsumeToken();
586
587 // We only had a single version component.
588 if (Major == 0) {
589 Diag(Tok, diag::err_zero_version);
590 return VersionTuple();
591 }
592
593 return VersionTuple(Major);
594 }
595
596 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
597 Diag(Tok, diag::err_expected_version);
598 SkipUntil(tok::comma, tok::r_paren, true, true, true);
599 return VersionTuple();
600 }
601
602 // Parse the minor version.
603 unsigned AfterMinor = AfterMajor + 1;
604 unsigned Minor = 0;
605 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
606 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
607 ++AfterMinor;
608 }
609
610 if (AfterMinor == ActualLength) {
611 ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +0000612
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000613 // We had major.minor.
614 if (Major == 0 && Minor == 0) {
615 Diag(Tok, diag::err_zero_version);
616 return VersionTuple();
617 }
618
Chad Rosierc1183952012-06-26 22:30:43 +0000619 return VersionTuple(Major, Minor);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000620 }
621
622 // If what follows is not a '.', we have a problem.
623 if (ThisTokBegin[AfterMinor] != '.') {
624 Diag(Tok, diag::err_expected_version);
625 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosierc1183952012-06-26 22:30:43 +0000626 return VersionTuple();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000627 }
628
629 // Parse the subminor version.
630 unsigned AfterSubminor = AfterMinor + 1;
631 unsigned Subminor = 0;
632 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
633 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
634 ++AfterSubminor;
635 }
636
637 if (AfterSubminor != ActualLength) {
638 Diag(Tok, diag::err_expected_version);
639 SkipUntil(tok::comma, tok::r_paren, true, true, true);
640 return VersionTuple();
641 }
642 ConsumeToken();
643 return VersionTuple(Major, Minor, Subminor);
644}
645
646/// \brief Parse the contents of the "availability" attribute.
647///
648/// availability-attribute:
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000649/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000650///
651/// platform:
652/// identifier
653///
654/// version-arg-list:
655/// version-arg
656/// version-arg ',' version-arg-list
657///
658/// version-arg:
659/// 'introduced' '=' version
660/// 'deprecated' '=' version
Douglas Gregorfdd417f2012-03-11 04:53:21 +0000661/// 'obsoleted' = version
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000662/// 'unavailable'
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000663/// opt-message:
664/// 'message' '=' <string>
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000665void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
666 SourceLocation AvailabilityLoc,
667 ParsedAttributes &attrs,
668 SourceLocation *endLoc) {
669 SourceLocation PlatformLoc;
670 IdentifierInfo *Platform = 0;
671
672 enum { Introduced, Deprecated, Obsoleted, Unknown };
673 AvailabilityChange Changes[Unknown];
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000674 ExprResult MessageExpr;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000675
676 // Opening '('.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000677 BalancedDelimiterTracker T(*this, tok::l_paren);
678 if (T.consumeOpen()) {
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000679 Diag(Tok, diag::err_expected_lparen);
680 return;
681 }
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000682
683 // Parse the platform name,
684 if (Tok.isNot(tok::identifier)) {
685 Diag(Tok, diag::err_availability_expected_platform);
686 SkipUntil(tok::r_paren);
687 return;
688 }
689 Platform = Tok.getIdentifierInfo();
690 PlatformLoc = ConsumeToken();
691
692 // Parse the ',' following the platform name.
693 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
694 return;
695
696 // If we haven't grabbed the pointers for the identifiers
697 // "introduced", "deprecated", and "obsoleted", do so now.
698 if (!Ident_introduced) {
699 Ident_introduced = PP.getIdentifierInfo("introduced");
700 Ident_deprecated = PP.getIdentifierInfo("deprecated");
701 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000702 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000703 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000704 }
705
706 // Parse the set of introductions/deprecations/removals.
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000707 SourceLocation UnavailableLoc;
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000708 do {
709 if (Tok.isNot(tok::identifier)) {
710 Diag(Tok, diag::err_availability_expected_change);
711 SkipUntil(tok::r_paren);
712 return;
713 }
714 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
715 SourceLocation KeywordLoc = ConsumeToken();
716
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000717 if (Keyword == Ident_unavailable) {
718 if (UnavailableLoc.isValid()) {
719 Diag(KeywordLoc, diag::err_availability_redundant)
720 << Keyword << SourceRange(UnavailableLoc);
Chad Rosierc1183952012-06-26 22:30:43 +0000721 }
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000722 UnavailableLoc = KeywordLoc;
723
724 if (Tok.isNot(tok::comma))
725 break;
726
727 ConsumeToken();
728 continue;
Chad Rosierc1183952012-06-26 22:30:43 +0000729 }
730
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000731 if (Tok.isNot(tok::equal)) {
732 Diag(Tok, diag::err_expected_equal_after)
733 << Keyword;
734 SkipUntil(tok::r_paren);
735 return;
736 }
737 ConsumeToken();
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000738 if (Keyword == Ident_message) {
739 if (!isTokenStringLiteral()) {
Andy Gibbsa8df57a2012-11-17 19:16:52 +0000740 Diag(Tok, diag::err_expected_string_literal)
741 << /*Source='availability attribute'*/2;
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000742 SkipUntil(tok::r_paren);
743 return;
744 }
745 MessageExpr = ParseStringLiteralExpression();
746 break;
747 }
Chad Rosierc1183952012-06-26 22:30:43 +0000748
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000749 SourceRange VersionRange;
750 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosierc1183952012-06-26 22:30:43 +0000751
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000752 if (Version.empty()) {
753 SkipUntil(tok::r_paren);
754 return;
755 }
756
757 unsigned Index;
758 if (Keyword == Ident_introduced)
759 Index = Introduced;
760 else if (Keyword == Ident_deprecated)
761 Index = Deprecated;
762 else if (Keyword == Ident_obsoleted)
763 Index = Obsoleted;
Chad Rosierc1183952012-06-26 22:30:43 +0000764 else
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000765 Index = Unknown;
766
767 if (Index < Unknown) {
768 if (!Changes[Index].KeywordLoc.isInvalid()) {
769 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosierc1183952012-06-26 22:30:43 +0000770 << Keyword
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000771 << SourceRange(Changes[Index].KeywordLoc,
772 Changes[Index].VersionRange.getEnd());
773 }
774
775 Changes[Index].KeywordLoc = KeywordLoc;
776 Changes[Index].Version = Version;
777 Changes[Index].VersionRange = VersionRange;
778 } else {
779 Diag(KeywordLoc, diag::err_availability_unknown_change)
780 << Keyword << VersionRange;
781 }
782
783 if (Tok.isNot(tok::comma))
784 break;
785
786 ConsumeToken();
787 } while (true);
788
789 // Closing ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000790 if (T.consumeClose())
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000791 return;
792
793 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +0000794 *endLoc = T.getCloseLocation();
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000795
Douglas Gregor7ab142b2011-03-26 03:35:55 +0000796 // The 'unavailable' availability cannot be combined with any other
797 // availability changes. Make sure that hasn't happened.
798 if (UnavailableLoc.isValid()) {
799 bool Complained = false;
800 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
801 if (Changes[Index].KeywordLoc.isValid()) {
802 if (!Complained) {
803 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
804 << SourceRange(Changes[Index].KeywordLoc,
805 Changes[Index].VersionRange.getEnd());
806 Complained = true;
807 }
808
809 // Clear out the availability.
810 Changes[Index] = AvailabilityChange();
811 }
812 }
813 }
814
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000815 // Record this attribute
Chad Rosierc1183952012-06-26 22:30:43 +0000816 attrs.addNew(&Availability,
817 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanian586be882012-01-23 23:38:32 +0000818 0, AvailabilityLoc,
John McCall084e83d2011-03-24 11:26:52 +0000819 Platform, PlatformLoc,
820 Changes[Introduced],
821 Changes[Deprecated],
Chad Rosierc1183952012-06-26 22:30:43 +0000822 Changes[Obsoleted],
Fariborz Jahanian88d510d2011-12-10 00:28:41 +0000823 UnavailableLoc, MessageExpr.take(),
Alexis Hunta0e54d42012-06-18 16:13:52 +0000824 AttributeList::AS_GNU);
Douglas Gregor20b2ebd2011-03-23 00:50:03 +0000825}
826
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000827
Bill Wendling44426052012-12-20 19:22:21 +0000828// Late Parsed Attributes:
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000829// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
830
831void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
832
833void Parser::LateParsedClass::ParseLexedAttributes() {
834 Self->ParseLexedAttributes(*Class);
835}
836
837void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000838 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000839}
840
841/// Wrapper class which calls ParseLexedAttribute, after setting up the
842/// scope appropriately.
843void Parser::ParseLexedAttributes(ParsingClass &Class) {
844 // Deal with templates
845 // FIXME: Test cases to make sure this does the right thing for templates.
846 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
847 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
848 HasTemplateScope);
849 if (HasTemplateScope)
850 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
851
Douglas Gregor3024f072012-04-16 07:05:22 +0000852 // Set or update the scope flags.
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000853 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregor3024f072012-04-16 07:05:22 +0000854 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000855 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
856 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
857
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000858 // Enter the scope of nested classes
859 if (!AlreadyHasClassScope)
860 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
861 Class.TagOrTemplate);
Benjamin Kramer1d373c62012-05-17 12:01:52 +0000862 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregor3024f072012-04-16 07:05:22 +0000863 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
864 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
865 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000866 }
Chad Rosierc1183952012-06-26 22:30:43 +0000867
DeLesley Hutchins6f860042012-04-06 15:10:17 +0000868 if (!AlreadyHasClassScope)
869 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
870 Class.TagOrTemplate);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000871}
872
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000873
874/// \brief Parse all attributes in LAs, and attach them to Decl D.
875void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
876 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins66e300e2012-11-02 21:44:32 +0000877 assert(LAs.parseSoon() &&
878 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000879 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins19c722d2012-08-15 22:41:04 +0000880 if (D)
881 LAs[i]->addDecl(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000882 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerbafc49a2012-04-14 12:44:47 +0000883 delete LAs[i];
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000884 }
885 LAs.clear();
886}
887
888
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000889/// \brief Finish parsing an attribute for which parsing was delayed.
890/// This will be called at the end of parsing a class declaration
891/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosierc1183952012-06-26 22:30:43 +0000892/// create an attribute with the arguments filled in. We add this
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000893/// to the Attribute list for the decl.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000894void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
895 bool EnterScope, bool OnDefinition) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000896 // Save the current token position.
897 SourceLocation OrigLoc = Tok.getLocation();
898
899 // Append the current token at the end of the new token stream so that it
900 // doesn't get lost.
901 LA.Toks.push_back(Tok);
902 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
903 // Consume the previously pushed token.
904 ConsumeAnyToken();
905
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000906 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smith10876ef2013-01-17 01:30:42 +0000907 // FIXME: Do not warn on C++11 attributes, once we start supporting
908 // them here.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +0000909 Diag(Tok, diag::warn_attribute_on_function_definition)
910 << LA.AttrName.getName();
911 }
912
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000913 ParsedAttributes Attrs(AttrFactory);
914 SourceLocation endLoc;
915
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000916 if (LA.Decls.size() > 0) {
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000917 Decl *D = LA.Decls[0];
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000918 NamedDecl *ND = dyn_cast<NamedDecl>(D);
919 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000920
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000921 // Allow 'this' within late-parsed attributes.
922 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
923 /*TypeQuals=*/0,
924 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000925
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000926 if (LA.Decls.size() == 1) {
927 // If the Decl is templatized, add template parameters to scope.
928 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
929 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
930 if (HasTemplateScope)
931 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000932
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000933 // If the Decl is on a function, add function parameters to the scope.
934 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
935 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
936 if (HasFunScope)
937 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000938
Michael Han23214e52012-10-03 01:56:22 +0000939 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000940 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsf1150d32012-08-20 21:32:18 +0000941
942 if (HasFunScope) {
943 Actions.ActOnExitFunctionContext();
944 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
945 }
946 if (HasTemplateScope) {
947 TempScope.Exit();
948 }
949 } else {
950 // If there are multiple decls, then the decl cannot be within the
951 // function scope.
Michael Han23214e52012-10-03 01:56:22 +0000952 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han360d2252012-10-04 16:42:52 +0000953 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000954 }
DeLesley Hutchins71d61032012-03-02 22:29:50 +0000955 } else {
956 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowski990d5712011-09-08 17:42:31 +0000957 }
958
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +0000959 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
960 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
961 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000962
963 if (Tok.getLocation() != OrigLoc) {
964 // Due to a parsing error, we either went over the cached tokens or
965 // there are still cached tokens left, so we skip the leftover tokens.
966 // Since this is an uncommon situation that should be avoided, use the
967 // expensive isBeforeInTranslationUnit call.
968 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
969 OrigLoc))
970 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregor0cf55e92012-03-08 01:00:17 +0000971 ConsumeAnyToken();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +0000972 }
973}
974
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000975/// \brief Wrapper around a case statement checking if AttrName is
976/// one of the thread safety attributes
Dmitri Gribenkof8579502013-01-12 19:30:44 +0000977bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +0000978 return llvm::StringSwitch<bool>(AttrName)
979 .Case("guarded_by", true)
980 .Case("guarded_var", true)
981 .Case("pt_guarded_by", true)
982 .Case("pt_guarded_var", true)
983 .Case("lockable", true)
984 .Case("scoped_lockable", true)
985 .Case("no_thread_safety_analysis", true)
986 .Case("acquired_after", true)
987 .Case("acquired_before", true)
988 .Case("exclusive_lock_function", true)
989 .Case("shared_lock_function", true)
990 .Case("exclusive_trylock_function", true)
991 .Case("shared_trylock_function", true)
992 .Case("unlock_function", true)
993 .Case("lock_returned", true)
994 .Case("locks_excluded", true)
995 .Case("exclusive_locks_required", true)
996 .Case("shared_locks_required", true)
997 .Default(false);
998}
999
1000/// \brief Parse the contents of thread safety attributes. These
1001/// should always be parsed as an expression list.
1002///
1003/// We need to special case the parsing due to the fact that if the first token
1004/// of the first argument is an identifier, the main parse loop will store
1005/// that token as a "parameter" and the rest of
1006/// the arguments will be added to a list of "arguments". However,
1007/// subsequent tokens in the first argument are lost. We instead parse each
1008/// argument as an expression and add all arguments to the list of "arguments".
1009/// In future, we will take advantage of this special case to also
1010/// deal with some argument scoping issues here (for example, referring to a
1011/// function parameter in the attribute on that function).
1012void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1013 SourceLocation AttrNameLoc,
1014 ParsedAttributes &Attrs,
1015 SourceLocation *EndLoc) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001016 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001017
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001018 BalancedDelimiterTracker T(*this, tok::l_paren);
1019 T.consumeOpen();
Chad Rosierc1183952012-06-26 22:30:43 +00001020
Benjamin Kramerf0623432012-08-23 22:51:59 +00001021 ExprVector ArgExprs;
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001022 bool ArgExprsOk = true;
Chad Rosierc1183952012-06-26 22:30:43 +00001023
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001024 // now parse the list of expressions
DeLesley Hutchins36f5d852011-12-14 19:36:06 +00001025 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinseb849c62013-02-07 19:01:07 +00001026 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001027 ExprResult ArgExpr(ParseAssignmentExpression());
1028 if (ArgExpr.isInvalid()) {
1029 ArgExprsOk = false;
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001030 T.consumeClose();
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001031 break;
1032 } else {
1033 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001034 }
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001035 if (Tok.isNot(tok::comma))
1036 break;
1037 ConsumeToken(); // Eat the comma, move to the next argument
1038 }
1039 // Match the ')'.
DeLesley Hutchins30398dd2012-01-20 22:50:54 +00001040 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowski9385dd72011-09-08 17:42:22 +00001041 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramerf0623432012-08-23 22:51:59 +00001042 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001043 }
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001044 if (EndLoc)
1045 *EndLoc = T.getCloseLocation();
Caitlin Sadowski4b1e8392011-08-09 17:59:31 +00001046}
1047
Dmitri Gribenkoe4a5a902012-08-17 00:08:38 +00001048void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1049 SourceLocation AttrNameLoc,
1050 ParsedAttributes &Attrs,
1051 SourceLocation *EndLoc) {
1052 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1053
1054 BalancedDelimiterTracker T(*this, tok::l_paren);
1055 T.consumeOpen();
1056
1057 if (Tok.isNot(tok::identifier)) {
1058 Diag(Tok, diag::err_expected_ident);
1059 T.skipToEnd();
1060 return;
1061 }
1062 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1063 SourceLocation ArgumentKindLoc = ConsumeToken();
1064
1065 if (Tok.isNot(tok::comma)) {
1066 Diag(Tok, diag::err_expected_comma);
1067 T.skipToEnd();
1068 return;
1069 }
1070 ConsumeToken();
1071
1072 SourceRange MatchingCTypeRange;
1073 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1074 if (MatchingCType.isInvalid()) {
1075 T.skipToEnd();
1076 return;
1077 }
1078
1079 bool LayoutCompatible = false;
1080 bool MustBeNull = false;
1081 while (Tok.is(tok::comma)) {
1082 ConsumeToken();
1083 if (Tok.isNot(tok::identifier)) {
1084 Diag(Tok, diag::err_expected_ident);
1085 T.skipToEnd();
1086 return;
1087 }
1088 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1089 if (Flag->isStr("layout_compatible"))
1090 LayoutCompatible = true;
1091 else if (Flag->isStr("must_be_null"))
1092 MustBeNull = true;
1093 else {
1094 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1095 T.skipToEnd();
1096 return;
1097 }
1098 ConsumeToken(); // consume flag
1099 }
1100
1101 if (!T.consumeClose()) {
1102 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1103 ArgumentKind, ArgumentKindLoc,
1104 MatchingCType.release(), LayoutCompatible,
1105 MustBeNull, AttributeList::AS_GNU);
1106 }
1107
1108 if (EndLoc)
1109 *EndLoc = T.getCloseLocation();
1110}
1111
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001112/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1113/// of a C++11 attribute-specifier in a location where an attribute is not
1114/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1115/// situation.
1116///
1117/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1118/// this doesn't appear to actually be an attribute-specifier, and the caller
1119/// should try to parse it.
1120bool Parser::DiagnoseProhibitedCXX11Attribute() {
1121 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1122
1123 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1124 case CAK_NotAttributeSpecifier:
1125 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1126 return false;
1127
1128 case CAK_InvalidAttributeSpecifier:
1129 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1130 return false;
1131
1132 case CAK_AttributeSpecifier:
1133 // Parse and discard the attributes.
1134 SourceLocation BeginLoc = ConsumeBracket();
1135 ConsumeBracket();
1136 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1137 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1138 SourceLocation EndLoc = ConsumeBracket();
1139 Diag(BeginLoc, diag::err_attributes_not_allowed)
1140 << SourceRange(BeginLoc, EndLoc);
1141 return true;
1142 }
Chandler Carruthd8f7d382012-04-10 16:03:08 +00001143 llvm_unreachable("All cases handled above.");
Richard Smith7bdcc4a2012-04-10 01:32:12 +00001144}
1145
John McCall53fa7142010-12-24 02:08:15 +00001146void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1147 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1148 << attrs.Range;
Dawn Perchik335e16b2010-09-03 01:29:35 +00001149}
1150
Michael Han64536a62012-11-06 19:34:54 +00001151void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1152 AttributeList *AttrList = attrs.getList();
1153 while (AttrList) {
Richard Smith89645bc2013-01-02 12:01:23 +00001154 if (AttrList->isCXX11Attribute()) {
Richard Smith810ad3e2013-01-29 10:02:16 +00001155 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Han64536a62012-11-06 19:34:54 +00001156 << AttrList->getName();
1157 AttrList->setInvalid();
1158 }
1159 AttrList = AttrList->getNext();
1160 }
1161}
1162
Chris Lattner53361ac2006-08-10 05:19:57 +00001163/// ParseDeclaration - Parse a full 'declaration', which consists of
1164/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner49836b42009-04-02 04:16:50 +00001165/// 'Context' should be a Declarator::TheContext value. This returns the
1166/// location of the semicolon in DeclEnd.
Chris Lattnera5235172007-08-25 06:57:03 +00001167///
1168/// declaration: [C99 6.7]
1169/// block-declaration ->
1170/// simple-declaration
1171/// others [FIXME]
Douglas Gregoreb31f392008-12-01 23:54:00 +00001172/// [C++] template-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001173/// [C++] namespace-definition
Douglas Gregord7c4d982008-12-30 03:27:21 +00001174/// [C++] using-directive
Douglas Gregor77b50e12009-06-22 23:06:13 +00001175/// [C++] using-declaration
Richard Smithc202b282012-04-14 00:33:13 +00001176/// [C++11/C11] static_assert-declaration
Chris Lattnera5235172007-08-25 06:57:03 +00001177/// others... [FIXME]
1178///
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001179Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1180 unsigned Context,
Alexis Hunt96d5c762009-11-21 08:43:09 +00001181 SourceLocation &DeclEnd,
John McCall53fa7142010-12-24 02:08:15 +00001182 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis355094e2010-06-17 10:52:18 +00001183 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahanian59b75282011-08-30 17:10:52 +00001184 // Must temporarily exit the objective-c container scope for
1185 // parsing c none objective-c decls.
1186 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosierc1183952012-06-26 22:30:43 +00001187
John McCall48871652010-08-21 09:40:31 +00001188 Decl *SingleDecl = 0;
Richard Smithcd1c0552011-07-01 19:46:12 +00001189 Decl *OwnedType = 0;
Chris Lattnera5235172007-08-25 06:57:03 +00001190 switch (Tok.getKind()) {
Douglas Gregoreb31f392008-12-01 23:54:00 +00001191 case tok::kw_template:
Douglas Gregor23996282009-05-12 21:31:51 +00001192 case tok::kw_export:
John McCall53fa7142010-12-24 02:08:15 +00001193 ProhibitAttributes(attrs);
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001194 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001195 break;
Sebastian Redl67667942010-08-27 23:12:46 +00001196 case tok::kw_inline:
Sebastian Redl5a5f2c72010-08-31 00:36:45 +00001197 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001198 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall53fa7142010-12-24 02:08:15 +00001199 ProhibitAttributes(attrs);
Sebastian Redl67667942010-08-27 23:12:46 +00001200 SourceLocation InlineLoc = ConsumeToken();
1201 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1202 break;
1203 }
Chad Rosierc1183952012-06-26 22:30:43 +00001204 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanian1db5c942010-09-28 20:42:35 +00001205 true);
Chris Lattnera5235172007-08-25 06:57:03 +00001206 case tok::kw_namespace:
John McCall53fa7142010-12-24 02:08:15 +00001207 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001208 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001209 break;
Douglas Gregord7c4d982008-12-30 03:27:21 +00001210 case tok::kw_using:
John McCall9b72f892010-11-10 02:40:36 +00001211 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithcd1c0552011-07-01 19:46:12 +00001212 DeclEnd, attrs, &OwnedType);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001213 break;
Anders Carlssonf24fcff62009-03-11 16:27:10 +00001214 case tok::kw_static_assert:
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00001215 case tok::kw__Static_assert:
John McCall53fa7142010-12-24 02:08:15 +00001216 ProhibitAttributes(attrs);
Chris Lattner49836b42009-04-02 04:16:50 +00001217 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001218 break;
Chris Lattnera5235172007-08-25 06:57:03 +00001219 default:
John McCall53fa7142010-12-24 02:08:15 +00001220 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattnera5235172007-08-25 06:57:03 +00001221 }
Chad Rosierc1183952012-06-26 22:30:43 +00001222
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001223 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithcd1c0552011-07-01 19:46:12 +00001224 // single decl, convert it now. Alias declarations can also declare a type;
1225 // include that too if it is present.
1226 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattnera5235172007-08-25 06:57:03 +00001227}
1228
1229/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1230/// declaration-specifiers init-declarator-list[opt] ';'
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001231/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1232/// init-declarator-list ';'
Chris Lattnera5235172007-08-25 06:57:03 +00001233///[C90/C++]init-declarator-list ';' [TODO]
1234/// [OMP] threadprivate-directive [TODO]
Chris Lattner32dc41c2009-03-29 17:27:48 +00001235///
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001236/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smith02e85f32011-04-14 22:09:26 +00001237/// attribute-specifier-seq[opt] type-specifier-seq declarator
1238///
Chris Lattner32dc41c2009-03-29 17:27:48 +00001239/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner005fc1b2010-04-05 18:18:31 +00001240/// declaration. If it is true, it checks for and eats it.
Richard Smith02e85f32011-04-14 22:09:26 +00001241///
1242/// If FRI is non-null, we might be parsing a for-range-declaration instead
1243/// of a simple-declaration. If we find that we are, we also parse the
1244/// for-range-initializer, and place it here.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00001245Parser::DeclGroupPtrTy
1246Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1247 SourceLocation &DeclEnd,
1248 ParsedAttributesWithRange &attrs,
1249 bool RequireSemi, ForRangeInit *FRI) {
Chris Lattner53361ac2006-08-10 05:19:57 +00001250 // Parse the common declaration-specifiers piece.
John McCall28a6aea2009-11-04 02:18:39 +00001251 ParsingDeclSpec DS(*this);
John McCall53fa7142010-12-24 02:08:15 +00001252 DS.takeAttributesFrom(attrs);
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001253
Douglas Gregor9de54ea2010-01-13 17:31:36 +00001254 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith30482bc2011-02-20 03:19:35 +00001255 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara1cd83682012-01-07 10:52:36 +00001256
Chris Lattner0e894622006-08-13 19:58:17 +00001257 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1258 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner76c72282007-10-09 17:33:22 +00001259 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidisfbb2bb52012-05-16 23:49:15 +00001260 DeclEnd = Tok.getLocation();
Chris Lattner005fc1b2010-04-05 18:18:31 +00001261 if (RequireSemi) ConsumeToken();
John McCall48871652010-08-21 09:40:31 +00001262 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor0e7dde52011-04-24 05:37:28 +00001263 DS);
John McCall28a6aea2009-11-04 02:18:39 +00001264 DS.complete(TheDecl);
Chris Lattner5bbb3c82009-03-29 16:50:03 +00001265 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner0e894622006-08-13 19:58:17 +00001266 }
Chad Rosierc1183952012-06-26 22:30:43 +00001267
1268 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld5a36322009-11-03 19:26:08 +00001269}
Mike Stump11289f42009-09-09 15:08:12 +00001270
Richard Smith09f76ee2011-10-19 21:33:05 +00001271/// Returns true if this might be the start of a declarator, or a common typo
1272/// for a declarator.
1273bool Parser::MightBeDeclarator(unsigned Context) {
1274 switch (Tok.getKind()) {
1275 case tok::annot_cxxscope:
1276 case tok::annot_template_id:
1277 case tok::caret:
1278 case tok::code_completion:
1279 case tok::coloncolon:
1280 case tok::ellipsis:
1281 case tok::kw___attribute:
1282 case tok::kw_operator:
1283 case tok::l_paren:
1284 case tok::star:
1285 return true;
1286
1287 case tok::amp:
1288 case tok::ampamp:
David Blaikiebbafb8a2012-03-11 07:00:24 +00001289 return getLangOpts().CPlusPlus;
Richard Smith09f76ee2011-10-19 21:33:05 +00001290
Richard Smithc8a79032012-01-09 22:31:44 +00001291 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001292 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smithc8a79032012-01-09 22:31:44 +00001293 NextToken().is(tok::l_square);
1294
1295 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikiebbafb8a2012-03-11 07:00:24 +00001296 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smithc8a79032012-01-09 22:31:44 +00001297
Richard Smith09f76ee2011-10-19 21:33:05 +00001298 case tok::identifier:
1299 switch (NextToken().getKind()) {
1300 case tok::code_completion:
1301 case tok::coloncolon:
1302 case tok::comma:
1303 case tok::equal:
1304 case tok::equalequal: // Might be a typo for '='.
1305 case tok::kw_alignas:
1306 case tok::kw_asm:
1307 case tok::kw___attribute:
1308 case tok::l_brace:
1309 case tok::l_paren:
1310 case tok::l_square:
1311 case tok::less:
1312 case tok::r_brace:
1313 case tok::r_paren:
1314 case tok::r_square:
1315 case tok::semi:
1316 return true;
1317
1318 case tok::colon:
1319 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smithc8a79032012-01-09 22:31:44 +00001320 // and in block scope it's probably a label. Inside a class definition,
1321 // this is a bit-field.
1322 return Context == Declarator::MemberContext ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00001323 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smithc8a79032012-01-09 22:31:44 +00001324
1325 case tok::identifier: // Possible virt-specifier.
Richard Smith89645bc2013-01-02 12:01:23 +00001326 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith09f76ee2011-10-19 21:33:05 +00001327
1328 default:
1329 return false;
1330 }
1331
1332 default:
1333 return false;
1334 }
1335}
1336
Richard Smithb8caac82012-04-11 20:59:20 +00001337/// Skip until we reach something which seems like a sensible place to pick
1338/// up parsing after a malformed declaration. This will sometimes stop sooner
1339/// than SkipUntil(tok::r_brace) would, but will never stop later.
1340void Parser::SkipMalformedDecl() {
1341 while (true) {
1342 switch (Tok.getKind()) {
1343 case tok::l_brace:
1344 // Skip until matching }, then stop. We've probably skipped over
1345 // a malformed class or function definition or similar.
1346 ConsumeBrace();
1347 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1348 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1349 // This declaration isn't over yet. Keep skipping.
1350 continue;
1351 }
1352 if (Tok.is(tok::semi))
1353 ConsumeToken();
1354 return;
1355
1356 case tok::l_square:
1357 ConsumeBracket();
1358 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1359 continue;
1360
1361 case tok::l_paren:
1362 ConsumeParen();
1363 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1364 continue;
1365
1366 case tok::r_brace:
1367 return;
1368
1369 case tok::semi:
1370 ConsumeToken();
1371 return;
1372
1373 case tok::kw_inline:
1374 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose12e730c2012-07-09 16:54:53 +00001375 // a good place to pick back up parsing, except in an Objective-C
1376 // @interface context.
1377 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1378 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smithb8caac82012-04-11 20:59:20 +00001379 return;
1380 break;
1381
1382 case tok::kw_namespace:
1383 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose12e730c2012-07-09 16:54:53 +00001384 // place to pick back up parsing, except in an Objective-C
1385 // @interface context.
1386 if (Tok.isAtStartOfLine() &&
1387 (!ParsingInObjCContainer || CurParsedObjCImpl))
1388 return;
1389 break;
1390
1391 case tok::at:
1392 // @end is very much like } in Objective-C contexts.
1393 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1394 ParsingInObjCContainer)
1395 return;
1396 break;
1397
1398 case tok::minus:
1399 case tok::plus:
1400 // - and + probably start new method declarations in Objective-C contexts.
1401 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smithb8caac82012-04-11 20:59:20 +00001402 return;
1403 break;
1404
1405 case tok::eof:
1406 return;
1407
1408 default:
1409 break;
1410 }
1411
1412 ConsumeAnyToken();
1413 }
1414}
1415
John McCalld5a36322009-11-03 19:26:08 +00001416/// ParseDeclGroup - Having concluded that this is either a function
1417/// definition or a group of object declarations, actually parse the
1418/// result.
John McCall28a6aea2009-11-04 02:18:39 +00001419Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1420 unsigned Context,
John McCalld5a36322009-11-03 19:26:08 +00001421 bool AllowFunctionDefinitions,
Richard Smith02e85f32011-04-14 22:09:26 +00001422 SourceLocation *DeclEnd,
1423 ForRangeInit *FRI) {
John McCalld5a36322009-11-03 19:26:08 +00001424 // Parse the first declarator.
John McCall28a6aea2009-11-04 02:18:39 +00001425 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld5a36322009-11-03 19:26:08 +00001426 ParseDeclarator(D);
Chris Lattner32dc41c2009-03-29 17:27:48 +00001427
John McCalld5a36322009-11-03 19:26:08 +00001428 // Bail out if the first declarator didn't seem well-formed.
1429 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smithb8caac82012-04-11 20:59:20 +00001430 SkipMalformedDecl();
John McCalld5a36322009-11-03 19:26:08 +00001431 return DeclGroupPtrTy();
Chris Lattnerefb0f112009-03-29 17:18:04 +00001432 }
Mike Stump11289f42009-09-09 15:08:12 +00001433
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001434 // Save late-parsed attributes for now; they need to be parsed in the
1435 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins66e300e2012-11-02 21:44:32 +00001436 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1437 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001438 if (D.isFunctionDeclarator())
1439 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1440
Chris Lattnerdbb1e932010-07-11 22:24:20 +00001441 // Check to see if we have a function *definition* which must have a body.
1442 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1443 // Look at the next token to make sure that this isn't a function
1444 // declaration. We have to check this because __attribute__ might be the
1445 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanian712bb812012-08-10 15:54:40 +00001446 !isDeclarationAfterDeclarator()) {
Chad Rosierc1183952012-06-26 22:30:43 +00001447
Chris Lattner13901342010-07-11 22:42:07 +00001448 if (isStartOfFunctionDefinition(D)) {
John McCalld5a36322009-11-03 19:26:08 +00001449 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1450 Diag(Tok, diag::err_function_declared_typedef);
1451
1452 // Recover by treating the 'typedef' as spurious.
1453 DS.ClearStorageClassSpecs();
1454 }
1455
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001456 Decl *TheDecl =
1457 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld5a36322009-11-03 19:26:08 +00001458 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner13901342010-07-11 22:42:07 +00001459 }
Chad Rosierc1183952012-06-26 22:30:43 +00001460
Chris Lattner13901342010-07-11 22:42:07 +00001461 if (isDeclarationSpecifier()) {
1462 // If there is an invalid declaration specifier right after the function
1463 // prototype, then we must be in a missing semicolon case where this isn't
1464 // actually a body. Just fall through into the code that handles it as a
1465 // prototype, and let the top-level code handle the erroneous declspec
1466 // where it would otherwise expect a comma or semicolon.
John McCalld5a36322009-11-03 19:26:08 +00001467 } else {
1468 Diag(Tok, diag::err_expected_fn_body);
1469 SkipUntil(tok::semi);
1470 return DeclGroupPtrTy();
1471 }
1472 }
1473
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001474 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001475 return DeclGroupPtrTy();
1476
1477 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1478 // must parse and analyze the for-range-initializer before the declaration is
1479 // analyzed.
1480 if (FRI && Tok.is(tok::colon)) {
1481 FRI->ColonLoc = ConsumeToken();
Sebastian Redl3da34892011-06-05 12:23:16 +00001482 if (Tok.is(tok::l_brace))
1483 FRI->RangeExpr = ParseBraceInitializer();
1484 else
1485 FRI->RangeExpr = ParseExpression();
Richard Smith02e85f32011-04-14 22:09:26 +00001486 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1487 Actions.ActOnCXXForRangeDecl(ThisDecl);
1488 Actions.FinalizeDeclaration(ThisDecl);
John McCallcf6e0c82012-01-27 01:29:43 +00001489 D.complete(ThisDecl);
Richard Smith02e85f32011-04-14 22:09:26 +00001490 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1491 }
1492
Chris Lattner0e62c1c2011-07-23 10:55:15 +00001493 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smith02e85f32011-04-14 22:09:26 +00001494 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001495 if (LateParsedAttrs.size() > 0)
1496 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall28a6aea2009-11-04 02:18:39 +00001497 D.complete(FirstDecl);
John McCall48871652010-08-21 09:40:31 +00001498 if (FirstDecl)
John McCalld5a36322009-11-03 19:26:08 +00001499 DeclsInGroup.push_back(FirstDecl);
1500
Richard Smith09f76ee2011-10-19 21:33:05 +00001501 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian577574a2012-07-02 23:37:09 +00001502
John McCalld5a36322009-11-03 19:26:08 +00001503 // If we don't have a comma, it is either the end of the list (a ';') or an
1504 // error, bail out.
1505 while (Tok.is(tok::comma)) {
Richard Smith09f76ee2011-10-19 21:33:05 +00001506 SourceLocation CommaLoc = ConsumeToken();
1507
1508 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1509 // This comma was followed by a line-break and something which can't be
1510 // the start of a declarator. The comma was probably a typo for a
1511 // semicolon.
1512 Diag(CommaLoc, diag::err_expected_semi_declaration)
1513 << FixItHint::CreateReplacement(CommaLoc, ";");
1514 ExpectSemi = false;
1515 break;
1516 }
John McCalld5a36322009-11-03 19:26:08 +00001517
1518 // Parse the next declarator.
1519 D.clear();
Richard Smith8d06f422012-01-12 23:53:29 +00001520 D.setCommaLoc(CommaLoc);
John McCalld5a36322009-11-03 19:26:08 +00001521
1522 // Accept attributes in an init-declarator. In the first declarator in a
1523 // declaration, these would be part of the declspec. In subsequent
1524 // declarators, they become part of the declarator itself, so that they
1525 // don't apply to declarators after *this* one. Examples:
1526 // short __attribute__((common)) var; -> declspec
1527 // short var __attribute__((common)); -> declarator
1528 // short x, __attribute__((common)) var; -> declarator
John McCall53fa7142010-12-24 02:08:15 +00001529 MaybeParseGNUAttributes(D);
John McCalld5a36322009-11-03 19:26:08 +00001530
1531 ParseDeclarator(D);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001532 if (!D.isInvalidType()) {
1533 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1534 D.complete(ThisDecl);
1535 if (ThisDecl)
Chad Rosierc1183952012-06-26 22:30:43 +00001536 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian372030b2012-01-13 00:14:12 +00001537 }
John McCalld5a36322009-11-03 19:26:08 +00001538 }
1539
1540 if (DeclEnd)
1541 *DeclEnd = Tok.getLocation();
1542
Richard Smith09f76ee2011-10-19 21:33:05 +00001543 if (ExpectSemi &&
Chris Lattner02f1b612012-04-28 16:12:17 +00001544 ExpectAndConsumeSemi(Context == Declarator::FileContext
1545 ? diag::err_invalid_token_after_toplevel_declarator
1546 : diag::err_expected_semi_declaration)) {
Chris Lattner13901342010-07-11 22:42:07 +00001547 // Okay, there was no semicolon and one was expected. If we see a
1548 // declaration specifier, just assume it was missing and continue parsing.
1549 // Otherwise things are very confused and we skip to recover.
1550 if (!isDeclarationSpecifier()) {
1551 SkipUntil(tok::r_brace, true, true);
1552 if (Tok.is(tok::semi))
1553 ConsumeToken();
1554 }
John McCalld5a36322009-11-03 19:26:08 +00001555 }
1556
Douglas Gregor0be31a22010-07-02 17:43:08 +00001557 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld5a36322009-11-03 19:26:08 +00001558 DeclsInGroup.data(),
1559 DeclsInGroup.size());
Chris Lattner53361ac2006-08-10 05:19:57 +00001560}
1561
Richard Smith02e85f32011-04-14 22:09:26 +00001562/// Parse an optional simple-asm-expr and attributes, and attach them to a
1563/// declarator. Returns true on an error.
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001564bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smith02e85f32011-04-14 22:09:26 +00001565 // If a simple-asm-expr is present, parse it.
1566 if (Tok.is(tok::kw_asm)) {
1567 SourceLocation Loc;
1568 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1569 if (AsmLabel.isInvalid()) {
1570 SkipUntil(tok::semi, true, true);
1571 return true;
1572 }
1573
1574 D.setAsmLabel(AsmLabel.release());
1575 D.SetRangeEnd(Loc);
1576 }
1577
1578 MaybeParseGNUAttributes(D);
1579 return false;
1580}
1581
Douglas Gregor23996282009-05-12 21:31:51 +00001582/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1583/// declarator'. This method parses the remainder of the declaration
1584/// (including any attributes or initializer, among other things) and
1585/// finalizes the declaration.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001586///
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001587/// init-declarator: [C99 6.7]
1588/// declarator
1589/// declarator '=' initializer
Chris Lattner6d7e6342006-08-15 03:41:14 +00001590/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1591/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00001592/// [C++] declarator initializer[opt]
1593///
1594/// [C++] initializer:
1595/// [C++] '=' initializer-clause
1596/// [C++] '(' expression-list ')'
Sebastian Redlf769df52009-03-24 22:27:57 +00001597/// [C++0x] '=' 'default' [TODO]
1598/// [C++0x] '=' 'delete'
Sebastian Redl3da34892011-06-05 12:23:16 +00001599/// [C++0x] braced-init-list
Sebastian Redlf769df52009-03-24 22:27:57 +00001600///
1601/// According to the standard grammar, =default and =delete are function
1602/// definitions, but that definitely doesn't fit with the parser here.
Chris Lattnerf0f3baa2006-08-14 00:15:20 +00001603///
John McCall48871652010-08-21 09:40:31 +00001604Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregorb52fabb2009-06-23 23:11:28 +00001605 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchins3fc6e4a2012-02-16 16:50:43 +00001606 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smith02e85f32011-04-14 22:09:26 +00001607 return 0;
Mike Stump11289f42009-09-09 15:08:12 +00001608
Richard Smith02e85f32011-04-14 22:09:26 +00001609 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1610}
Mike Stump11289f42009-09-09 15:08:12 +00001611
Richard Smith02e85f32011-04-14 22:09:26 +00001612Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1613 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor23996282009-05-12 21:31:51 +00001614 // Inform the current actions module that we just parsed this declarator.
John McCall48871652010-08-21 09:40:31 +00001615 Decl *ThisDecl = 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001616 switch (TemplateInfo.Kind) {
1617 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001618 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregor450f00842009-09-25 18:43:00 +00001619 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001620
Douglas Gregor450f00842009-09-25 18:43:00 +00001621 case ParsedTemplateInfo::Template:
1622 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor0be31a22010-07-02 17:43:08 +00001623 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramercc4c49d2012-08-23 23:38:35 +00001624 *TemplateInfo.TemplateParams,
Douglas Gregor450f00842009-09-25 18:43:00 +00001625 D);
1626 break;
Chad Rosierc1183952012-06-26 22:30:43 +00001627
Douglas Gregor450f00842009-09-25 18:43:00 +00001628 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosierc1183952012-06-26 22:30:43 +00001629 DeclResult ThisRes
Douglas Gregor0be31a22010-07-02 17:43:08 +00001630 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregor450f00842009-09-25 18:43:00 +00001631 TemplateInfo.ExternLoc,
1632 TemplateInfo.TemplateLoc,
1633 D);
1634 if (ThisRes.isInvalid()) {
1635 SkipUntil(tok::semi, true, true);
John McCall48871652010-08-21 09:40:31 +00001636 return 0;
Douglas Gregor450f00842009-09-25 18:43:00 +00001637 }
Chad Rosierc1183952012-06-26 22:30:43 +00001638
Douglas Gregor450f00842009-09-25 18:43:00 +00001639 ThisDecl = ThisRes.get();
1640 break;
1641 }
1642 }
Mike Stump11289f42009-09-09 15:08:12 +00001643
Richard Smith30482bc2011-02-20 03:19:35 +00001644 bool TypeContainsAuto =
1645 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1646
Douglas Gregor23996282009-05-12 21:31:51 +00001647 // Parse declarator '=' initializer.
Richard Trieuc64d3232012-01-18 22:54:52 +00001648 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieu4972a6d2012-01-19 22:01:51 +00001649 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor23996282009-05-12 21:31:51 +00001650 ConsumeToken();
Anders Carlsson991285e2010-09-24 21:25:25 +00001651 if (Tok.is(tok::kw_delete)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001652 if (D.isFunctionDeclarator())
1653 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1654 << 1 /* delete */;
1655 else
1656 Diag(ConsumeToken(), diag::err_deleted_non_function);
Alexis Hunt5dafebc2011-05-06 01:42:00 +00001657 } else if (Tok.is(tok::kw_default)) {
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001658 if (D.isFunctionDeclarator())
Sebastian Redl46afb552012-02-11 23:51:21 +00001659 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1660 << 0 /* default */;
Alexis Hunt5a7fa252011-05-12 06:15:49 +00001661 else
1662 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor23996282009-05-12 21:31:51 +00001663 } else {
David Blaikiebbafb8a2012-03-11 07:00:24 +00001664 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall1f4ee7b2009-12-19 09:28:58 +00001665 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001666 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001667 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001668
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001669 if (Tok.is(tok::code_completion)) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001670 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourne6b4fdc22012-07-27 12:56:09 +00001671 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00001672 cutOffParsing();
1673 return 0;
Douglas Gregor7aa6b222010-05-30 01:49:25 +00001674 }
Chad Rosierc1183952012-06-26 22:30:43 +00001675
John McCalldadc5752010-08-24 06:29:42 +00001676 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001677
David Blaikiebbafb8a2012-03-11 07:00:24 +00001678 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001679 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall1f4ee7b2009-12-19 09:28:58 +00001680 ExitScope();
1681 }
Argyrios Kyrtzidis3df19782009-06-17 22:50:06 +00001682
Douglas Gregor23996282009-05-12 21:31:51 +00001683 if (Init.isInvalid()) {
Douglas Gregor604c3022010-03-01 18:27:54 +00001684 SkipUntil(tok::comma, true, true);
1685 Actions.ActOnInitializerError(ThisDecl);
1686 } else
Richard Smith30482bc2011-02-20 03:19:35 +00001687 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1688 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001689 }
1690 } else if (Tok.is(tok::l_paren)) {
1691 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001692 BalancedDelimiterTracker T(*this, tok::l_paren);
1693 T.consumeOpen();
1694
Benjamin Kramerf0623432012-08-23 22:51:59 +00001695 ExprVector Exprs;
Douglas Gregor23996282009-05-12 21:31:51 +00001696 CommaLocsTy CommaLocs;
1697
David Blaikiebbafb8a2012-03-11 07:00:24 +00001698 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor613bf102009-12-22 17:47:17 +00001699 EnterScope(0);
Douglas Gregor0be31a22010-07-02 17:43:08 +00001700 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001701 }
1702
Douglas Gregor23996282009-05-12 21:31:51 +00001703 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikieeae04112012-10-10 23:15:05 +00001704 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor23996282009-05-12 21:31:51 +00001705 SkipUntil(tok::r_paren);
Douglas Gregor613bf102009-12-22 17:47:17 +00001706
David Blaikiebbafb8a2012-03-11 07:00:24 +00001707 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001708 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001709 ExitScope();
1710 }
Douglas Gregor23996282009-05-12 21:31:51 +00001711 } else {
1712 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00001713 T.consumeClose();
Douglas Gregor23996282009-05-12 21:31:51 +00001714
1715 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1716 "Unexpected number of commas!");
Douglas Gregor613bf102009-12-22 17:47:17 +00001717
David Blaikiebbafb8a2012-03-11 07:00:24 +00001718 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00001719 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregor613bf102009-12-22 17:47:17 +00001720 ExitScope();
1721 }
1722
Sebastian Redla9351792012-02-11 23:51:47 +00001723 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1724 T.getCloseLocation(),
Benjamin Kramer62b95d82012-08-23 21:35:17 +00001725 Exprs);
Sebastian Redla9351792012-02-11 23:51:47 +00001726 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1727 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001728 }
Richard Smith2bf7fdb2013-01-02 11:42:31 +00001729 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanian8be1ecd2012-07-03 23:22:13 +00001730 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redl3da34892011-06-05 12:23:16 +00001731 // Parse C++0x braced-init-list.
Richard Smith5d164bc2011-10-15 05:09:34 +00001732 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1733
Sebastian Redl3da34892011-06-05 12:23:16 +00001734 if (D.getCXXScopeSpec().isSet()) {
1735 EnterScope(0);
1736 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1737 }
1738
1739 ExprResult Init(ParseBraceInitializer());
1740
1741 if (D.getCXXScopeSpec().isSet()) {
1742 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1743 ExitScope();
1744 }
1745
1746 if (Init.isInvalid()) {
1747 Actions.ActOnInitializerError(ThisDecl);
1748 } else
1749 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1750 /*DirectInit=*/true, TypeContainsAuto);
1751
Douglas Gregor23996282009-05-12 21:31:51 +00001752 } else {
Richard Smith30482bc2011-02-20 03:19:35 +00001753 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor23996282009-05-12 21:31:51 +00001754 }
1755
Richard Smithb2bc2e62011-02-21 20:05:19 +00001756 Actions.FinalizeDeclaration(ThisDecl);
1757
Douglas Gregor23996282009-05-12 21:31:51 +00001758 return ThisDecl;
1759}
1760
Chris Lattner1890ac82006-08-13 01:16:23 +00001761/// ParseSpecifierQualifierList
1762/// specifier-qualifier-list:
1763/// type-specifier specifier-qualifier-list[opt]
1764/// type-qualifier specifier-qualifier-list[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00001765/// [GNU] attributes specifier-qualifier-list[opt]
Chris Lattner1890ac82006-08-13 01:16:23 +00001766///
Richard Smithc5b05522012-03-12 07:56:15 +00001767void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1768 DeclSpecContext DSC) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001769 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1770 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00001771 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smithc5b05522012-03-12 07:56:15 +00001772 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump11289f42009-09-09 15:08:12 +00001773
Chris Lattner1890ac82006-08-13 01:16:23 +00001774 // Validate declspec for type-name.
1775 unsigned Specs = DS.getParsedSpecifiers();
Richard Smith2f07ad52012-05-09 20:55:26 +00001776 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1777 !DS.hasTypeSpecifier()) {
Richard Smithc5b05522012-03-12 07:56:15 +00001778 Diag(Tok, diag::err_expected_type);
1779 DS.SetTypeSpecError();
1780 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1781 !DS.hasAttributes()) {
Chris Lattner1890ac82006-08-13 01:16:23 +00001782 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smithc5b05522012-03-12 07:56:15 +00001783 if (!DS.hasTypeSpecifier())
1784 DS.SetTypeSpecError();
1785 }
Mike Stump11289f42009-09-09 15:08:12 +00001786
Chris Lattner1b22eed2006-11-28 05:12:07 +00001787 // Issue diagnostic and remove storage class if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001788 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
Chris Lattner1b22eed2006-11-28 05:12:07 +00001789 if (DS.getStorageClassSpecLoc().isValid())
1790 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1791 else
1792 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
Chris Lattnera925dc62006-11-28 04:33:46 +00001793 DS.ClearStorageClassSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001794 }
Mike Stump11289f42009-09-09 15:08:12 +00001795
Chris Lattner1b22eed2006-11-28 05:12:07 +00001796 // Issue diagnostic and remove function specfier if present.
Chris Lattner1890ac82006-08-13 01:16:23 +00001797 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregor61956c42008-10-31 09:07:45 +00001798 if (DS.isInlineSpecified())
1799 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1800 if (DS.isVirtualSpecified())
1801 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1802 if (DS.isExplicitSpecified())
1803 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Chris Lattnera925dc62006-11-28 04:33:46 +00001804 DS.ClearFunctionSpecs();
Chris Lattner1890ac82006-08-13 01:16:23 +00001805 }
Richard Smithc5b05522012-03-12 07:56:15 +00001806
1807 // Issue diagnostic and remove constexpr specfier if present.
1808 if (DS.isConstexprSpecified()) {
1809 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1810 DS.ClearConstexprSpec();
1811 }
Chris Lattner1890ac82006-08-13 01:16:23 +00001812}
Chris Lattner53361ac2006-08-10 05:19:57 +00001813
Chris Lattner6cc055a2009-04-12 20:42:31 +00001814/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1815/// specified token is valid after the identifier in a declarator which
1816/// immediately follows the declspec. For example, these things are valid:
1817///
1818/// int x [ 4]; // direct-declarator
1819/// int x ( int y); // direct-declarator
1820/// int(int x ) // direct-declarator
1821/// int x ; // simple-declaration
1822/// int x = 17; // init-declarator-list
1823/// int x , y; // init-declarator-list
1824/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnera723ba92009-04-14 21:16:09 +00001825/// int x : 4; // struct-declarator
Chris Lattner2b988c12009-04-12 22:29:43 +00001826/// int x { 5}; // C++'0x unified initializers
Chris Lattner6cc055a2009-04-12 20:42:31 +00001827///
1828/// This is not, because 'x' does not immediately follow the declspec (though
1829/// ')' happens to be valid anyway).
1830/// int (x)
1831///
1832static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1833 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1834 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnera723ba92009-04-14 21:16:09 +00001835 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattner6cc055a2009-04-12 20:42:31 +00001836}
1837
Chris Lattner20a0c612009-04-14 21:34:55 +00001838
1839/// ParseImplicitInt - This method is called when we have an non-typename
1840/// identifier in a declspec (which normally terminates the decl spec) when
1841/// the declspec has no type specifier. In this case, the declspec is either
1842/// malformed or is "implicit int" (in K&R and C89).
1843///
1844/// This method handles diagnosing this prettily and returns false if the
1845/// declspec is done being processed. If it recovers and thinks there may be
1846/// other pieces of declspec after it, it returns true.
1847///
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001848bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00001849 const ParsedTemplateInfo &TemplateInfo,
Michael Han9407e502012-11-26 22:54:45 +00001850 AccessSpecifier AS, DeclSpecContext DSC,
1851 ParsedAttributesWithRange &Attrs) {
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001852 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump11289f42009-09-09 15:08:12 +00001853
Chris Lattner20a0c612009-04-14 21:34:55 +00001854 SourceLocation Loc = Tok.getLocation();
1855 // If we see an identifier that is not a type name, we normally would
1856 // parse it as the identifer being declared. However, when a typename
1857 // is typo'd or the definition is not included, this will incorrectly
1858 // parse the typename as the identifier name and fall over misparsing
1859 // later parts of the diagnostic.
1860 //
1861 // As such, we try to do some look-ahead in cases where this would
1862 // otherwise be an "implicit-int" case to see if this is invalid. For
1863 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1864 // an identifier with implicit int, we'd get a parse error because the
1865 // next token is obviously invalid for a type. Parse these as a case
1866 // with an invalid type specifier.
1867 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump11289f42009-09-09 15:08:12 +00001868
Chris Lattner20a0c612009-04-14 21:34:55 +00001869 // Since we know that this either implicit int (which is rare) or an
Richard Smitha952ebb2012-05-15 21:01:51 +00001870 // error, do lookahead to try to do better recovery. This never applies
1871 // within a type specifier. Outside of C++, we allow this even if the
1872 // language doesn't "officially" support implicit int -- we support
1873 // implicit int as an extension in C99 and C11. Allegedly, MS also
1874 // supports implicit int in C++ mode.
Richard Smith2f07ad52012-05-09 20:55:26 +00001875 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smitha952ebb2012-05-15 21:01:51 +00001876 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smithc5b05522012-03-12 07:56:15 +00001877 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001878 // If this token is valid for implicit int, e.g. "static x = 4", then
1879 // we just avoid eating the identifier, so it will be parsed as the
1880 // identifier in the declarator.
1881 return false;
1882 }
Mike Stump11289f42009-09-09 15:08:12 +00001883
Richard Smitha952ebb2012-05-15 21:01:51 +00001884 if (getLangOpts().CPlusPlus &&
1885 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1886 // Don't require a type specifier if we have the 'auto' storage class
1887 // specifier in C++98 -- we'll promote it to a type specifier.
1888 return false;
1889 }
1890
Chris Lattner20a0c612009-04-14 21:34:55 +00001891 // Otherwise, if we don't consume this token, we are going to emit an
1892 // error anyway. Try to recover from various common problems. Check
1893 // to see if this was a reference to a tag name without a tag specified.
1894 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001895 //
1896 // C++ doesn't need this, and isTagName doesn't take SS.
1897 if (SS == 0) {
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001898 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001899 tok::TokenKind TagKind = tok::unknown;
Mike Stump11289f42009-09-09 15:08:12 +00001900
Douglas Gregor0be31a22010-07-02 17:43:08 +00001901 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattner20a0c612009-04-14 21:34:55 +00001902 default: break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001903 case DeclSpec::TST_enum:
1904 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1905 case DeclSpec::TST_union:
1906 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1907 case DeclSpec::TST_struct:
1908 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matosdc86f942012-08-31 18:45:21 +00001909 case DeclSpec::TST_interface:
1910 TagName="__interface"; FixitTagName = "__interface ";
1911 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidis1f329402011-04-21 17:29:47 +00001912 case DeclSpec::TST_class:
1913 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattner20a0c612009-04-14 21:34:55 +00001914 }
Mike Stump11289f42009-09-09 15:08:12 +00001915
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001916 if (TagName) {
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001917 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1918 LookupResult R(Actions, TokenName, SourceLocation(),
1919 Sema::LookupOrdinaryName);
1920
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001921 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001922 << TokenName << TagName << getLangOpts().CPlusPlus
1923 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1924
1925 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1926 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1927 I != IEnd; ++I)
Kaelyn Uhrain3fe3f852012-04-27 18:26:49 +00001928 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrain031643e2012-04-26 23:36:17 +00001929 << TokenName << TagName;
1930 }
Mike Stump11289f42009-09-09 15:08:12 +00001931
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001932 // Parse this as a tag as if the missing tag were present.
1933 if (TagKind == tok::kw_enum)
Richard Smithc5b05522012-03-12 07:56:15 +00001934 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001935 else
Richard Smithc5b05522012-03-12 07:56:15 +00001936 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han9407e502012-11-26 22:54:45 +00001937 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00001938 return true;
1939 }
Chris Lattner20a0c612009-04-14 21:34:55 +00001940 }
Mike Stump11289f42009-09-09 15:08:12 +00001941
Richard Smithfe904f02012-05-15 21:29:55 +00001942 // Determine whether this identifier could plausibly be the name of something
Richard Smithedd124e2012-05-15 21:42:17 +00001943 // being declared (with a missing type).
Richard Smithfe904f02012-05-15 21:29:55 +00001944 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1945 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smitha952ebb2012-05-15 21:01:51 +00001946 // Look ahead to the next token to try to figure out what this declaration
1947 // was supposed to be.
1948 switch (NextToken().getKind()) {
1949 case tok::comma:
1950 case tok::equal:
1951 case tok::kw_asm:
1952 case tok::l_brace:
1953 case tok::l_square:
1954 case tok::semi:
1955 // This looks like a variable declaration. The type is probably missing.
1956 // We're done parsing decl-specifiers.
1957 return false;
1958
1959 case tok::l_paren: {
1960 // static x(4); // 'x' is not a type
1961 // x(int n); // 'x' is not a type
1962 // x (*p)[]; // 'x' is a type
1963 //
1964 // Since we're in an error case (or the rare 'implicit int in C++' MS
1965 // extension), we can afford to perform a tentative parse to determine
1966 // which case we're in.
1967 TentativeParsingAction PA(*this);
1968 ConsumeToken();
1969 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1970 PA.Revert();
1971 if (TPR == TPResult::False())
1972 return false;
1973 // The identifier is followed by a parenthesized declarator.
1974 // It's supposed to be a type.
1975 break;
1976 }
1977
1978 default:
1979 // This is probably supposed to be a type. This includes cases like:
1980 // int f(itn);
1981 // struct S { unsinged : 4; };
1982 break;
1983 }
1984 }
1985
Chad Rosierc1183952012-06-26 22:30:43 +00001986 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregor15e56022009-10-13 23:27:22 +00001987 // diagnostic and attempt to recover.
John McCallba7bf592010-08-24 05:47:05 +00001988 ParsedType T;
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00001989 IdentifierInfo *II = Tok.getIdentifierInfo();
1990 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregor15e56022009-10-13 23:27:22 +00001991 // The action emitted a diagnostic, so we don't have to.
1992 if (T) {
1993 // The action has suggested that the type T could be used. Set that as
1994 // the type in the declaration specifiers, consume the would-be type
1995 // name token, and we're done.
1996 const char *PrevSpec;
1997 unsigned DiagID;
John McCallba7bf592010-08-24 05:47:05 +00001998 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregor15e56022009-10-13 23:27:22 +00001999 DS.SetRangeEnd(Tok.getLocation());
2000 ConsumeToken();
Kaelyn Uhrainb5b17fe2012-06-15 23:45:58 +00002001 // There may be other declaration specifiers after this.
2002 return true;
2003 } else if (II != Tok.getIdentifierInfo()) {
2004 // If no type was suggested, the correction is to a keyword
2005 Tok.setKind(II->getTokenID());
Douglas Gregor15e56022009-10-13 23:27:22 +00002006 // There may be other declaration specifiers after this.
2007 return true;
2008 }
Chad Rosierc1183952012-06-26 22:30:43 +00002009
Douglas Gregor15e56022009-10-13 23:27:22 +00002010 // Fall through; the action had no suggestion for us.
2011 } else {
2012 // The action did not emit a diagnostic, so emit one now.
2013 SourceRange R;
2014 if (SS) R = SS->getRange();
2015 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2016 }
Mike Stump11289f42009-09-09 15:08:12 +00002017
Douglas Gregor15e56022009-10-13 23:27:22 +00002018 // Mark this as an error.
Richard Smithc5b05522012-03-12 07:56:15 +00002019 DS.SetTypeSpecError();
Chris Lattner20a0c612009-04-14 21:34:55 +00002020 DS.SetRangeEnd(Tok.getLocation());
2021 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00002022
Chris Lattner20a0c612009-04-14 21:34:55 +00002023 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2024 // avoid rippling error messages on subsequent uses of the same type,
2025 // could be useful if #include was forgotten.
2026 return false;
2027}
2028
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002029/// \brief Determine the declaration specifier context from the declarator
2030/// context.
2031///
2032/// \param Context the declarator context, which is one of the
2033/// Declarator::TheContext enumerator values.
Chad Rosierc1183952012-06-26 22:30:43 +00002034Parser::DeclSpecContext
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002035Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2036 if (Context == Declarator::MemberContext)
2037 return DSC_class;
2038 if (Context == Declarator::FileContext)
2039 return DSC_top_level;
Richard Smith62dad822012-03-15 01:02:11 +00002040 if (Context == Declarator::TrailingReturnContext)
2041 return DSC_trailing;
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002042 return DSC_normal;
2043}
2044
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002045/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2046///
2047/// FIXME: Simply returns an alignof() expression if the argument is a
2048/// type. Ideally, the type should be propagated directly into Sema.
2049///
Benjamin Kramere56f3932011-12-23 17:00:35 +00002050/// [C11] type-id
2051/// [C11] constant-expression
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002052/// [C++0x] type-id ...[opt]
2053/// [C++0x] assignment-expression ...[opt]
2054ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2055 SourceLocation &EllipsisLoc) {
2056 ExprResult ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002057 if (isTypeIdInParens()) {
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002058 SourceLocation TypeLoc = Tok.getLocation();
2059 ParsedType Ty = ParseTypeName().get();
2060 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002061 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2062 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002063 } else
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002064 ER = ParseConstantExpression();
2065
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002066 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbourneccbcce02011-10-24 17:56:00 +00002067 EllipsisLoc = ConsumeToken();
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002068
2069 return ER;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002070}
2071
2072/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2073/// attribute to Attrs.
2074///
2075/// alignment-specifier:
Benjamin Kramere56f3932011-12-23 17:00:35 +00002076/// [C11] '_Alignas' '(' type-id ')'
2077/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smithd11c7a12013-01-29 01:48:07 +00002078/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2079/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002080void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2081 SourceLocation *endLoc) {
2082 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2083 "Not an alignment-specifier!");
2084
Richard Smithd11c7a12013-01-29 01:48:07 +00002085 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2086 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002087
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002088 BalancedDelimiterTracker T(*this, tok::l_paren);
2089 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002090 return;
2091
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002092 SourceLocation EllipsisLoc;
2093 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002094 if (ArgExpr.isInvalid()) {
2095 SkipUntil(tok::r_paren);
2096 return;
2097 }
2098
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002099 T.consumeClose();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002100 if (endLoc)
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00002101 *endLoc = T.getCloseLocation();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002102
Peter Collingbourne7d33cd32011-10-23 20:07:52 +00002103 // FIXME: Handle pack-expansions here.
2104 if (EllipsisLoc.isValid()) {
2105 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2106 return;
2107 }
2108
Benjamin Kramerf0623432012-08-23 22:51:59 +00002109 ExprVector ArgExprs;
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002110 ArgExprs.push_back(ArgExpr.release());
Richard Smithd11c7a12013-01-29 01:48:07 +00002111 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
2112 ArgExprs.data(), 1, AttributeList::AS_Keyword);
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002113}
2114
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002115/// ParseDeclarationSpecifiers
2116/// declaration-specifiers: [C99 6.7]
Chris Lattner3b561a32006-08-13 00:12:11 +00002117/// storage-class-specifier declaration-specifiers[opt]
2118/// type-specifier declaration-specifiers[opt]
Chris Lattner3b561a32006-08-13 00:12:11 +00002119/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramere56f3932011-12-23 17:00:35 +00002120/// [C11] alignment-specifier declaration-specifiers[opt]
Chris Lattnere37e2332006-08-15 04:50:22 +00002121/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor26701a42011-09-09 02:06:17 +00002122/// [Clang] '__module_private__' declaration-specifiers[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002123///
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002124/// storage-class-specifier: [C99 6.7.1]
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002125/// 'typedef'
2126/// 'extern'
2127/// 'static'
2128/// 'auto'
2129/// 'register'
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002130/// [C++] 'mutable'
Chris Lattnerda48a8e2006-08-04 05:25:55 +00002131/// [GNU] '__thread'
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002132/// function-specifier: [C99 6.7.4]
Chris Lattner3b561a32006-08-13 00:12:11 +00002133/// [C99] 'inline'
Douglas Gregor61956c42008-10-31 09:07:45 +00002134/// [C++] 'virtual'
2135/// [C++] 'explicit'
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002136/// [OpenCL] '__kernel'
Anders Carlssoncd8db412009-05-06 04:46:28 +00002137/// 'friend': [C++ dcl.friend]
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002138/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssoncd8db412009-05-06 04:46:28 +00002139
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002140///
Douglas Gregorb9bd8a92008-12-24 02:52:09 +00002141void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor1b57ff32009-05-12 23:25:50 +00002142 const ParsedTemplateInfo &TemplateInfo,
John McCall07e91c02009-08-06 02:15:43 +00002143 AccessSpecifier AS,
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002144 DeclSpecContext DSContext,
2145 LateParsedAttrList *LateAttrs) {
Douglas Gregor0e7dde52011-04-24 05:37:28 +00002146 if (DS.getSourceRange().isInvalid()) {
2147 DS.SetRangeStart(Tok.getLocation());
2148 DS.SetRangeEnd(Tok.getLocation());
2149 }
Chad Rosierc1183952012-06-26 22:30:43 +00002150
Douglas Gregordf593fb2011-11-07 17:33:42 +00002151 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002152 bool AttrsLastTime = false;
2153 ParsedAttributesWithRange attrs(AttrFactory);
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002154 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00002155 bool isInvalid = false;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002156 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00002157 unsigned DiagID = 0;
2158
Chris Lattner4d8f8732006-11-28 05:05:08 +00002159 SourceLocation Loc = Tok.getLocation();
Douglas Gregor450c75a2008-11-07 15:42:26 +00002160
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002161 switch (Tok.getKind()) {
Mike Stump11289f42009-09-09 15:08:12 +00002162 default:
Chris Lattner0974b232008-07-26 00:20:22 +00002163 DoneWithDeclSpec:
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002164 if (!AttrsLastTime)
2165 ProhibitAttributes(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002166 else {
2167 // Reject C++11 attributes that appertain to decl specifiers as
2168 // we don't support any C++11 attributes that appertain to decl
2169 // specifiers. This also conforms to what g++ 4.8 is doing.
2170 ProhibitCXX11Attributes(attrs);
2171
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002172 DS.takeAttributesFrom(attrs);
Michael Han64536a62012-11-06 19:34:54 +00002173 }
Peter Collingbourne70188b32011-09-29 18:03:57 +00002174
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002175 // If this is not a declaration specifier token, we're done reading decl
2176 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00002177 DS.Finish(Diags, PP);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002178 return;
Mike Stump11289f42009-09-09 15:08:12 +00002179
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002180 case tok::l_square:
2181 case tok::kw_alignas:
2182 if (!isCXX11AttributeSpecifier())
2183 goto DoneWithDeclSpec;
2184
2185 ProhibitAttributes(attrs);
2186 // FIXME: It would be good to recover by accepting the attributes,
2187 // but attempting to do that now would cause serious
2188 // madness in terms of diagnostics.
2189 attrs.clear();
2190 attrs.Range = SourceRange();
2191
2192 ParseCXX11Attributes(attrs);
2193 AttrsLastTime = true;
Chad Rosierc1183952012-06-26 22:30:43 +00002194 continue;
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002195
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002196 case tok::code_completion: {
John McCallfaf5fb42010-08-26 23:41:50 +00002197 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002198 if (DS.hasTypeSpecifier()) {
2199 bool AllowNonIdentifiers
2200 = (getCurScope()->getFlags() & (Scope::ControlScope |
2201 Scope::BlockScope |
2202 Scope::TemplateParamScope |
2203 Scope::FunctionPrototypeScope |
2204 Scope::AtCatchScope)) == 0;
2205 bool AllowNestedNameSpecifiers
Chad Rosierc1183952012-06-26 22:30:43 +00002206 = DSContext == DSC_top_level ||
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002207 (DSContext == DSC_class && DS.isFriendSpecified());
2208
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002209 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosierc1183952012-06-26 22:30:43 +00002210 AllowNonIdentifiers,
Douglas Gregorbfcea8b2010-09-16 15:14:18 +00002211 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002212 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00002213 }
2214
Douglas Gregor80039242011-02-15 20:33:25 +00002215 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2216 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2217 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosierc1183952012-06-26 22:30:43 +00002218 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallfaf5fb42010-08-26 23:41:50 +00002219 : Sema::PCC_Template;
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002220 else if (DSContext == DSC_class)
John McCallfaf5fb42010-08-26 23:41:50 +00002221 CCC = Sema::PCC_Class;
Argyrios Kyrtzidisb6c6a582012-02-07 16:50:53 +00002222 else if (CurParsedObjCImpl)
John McCallfaf5fb42010-08-26 23:41:50 +00002223 CCC = Sema::PCC_ObjCImplementation;
Chad Rosierc1183952012-06-26 22:30:43 +00002224
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002225 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00002226 return cutOffParsing();
Douglas Gregorc49f5b22010-08-23 18:23:48 +00002227 }
2228
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002229 case tok::coloncolon: // ::foo::bar
John McCall1f476a12010-02-26 08:45:28 +00002230 // C++ scope specifier. Annotate and loop, or bail out on error.
2231 if (TryAnnotateCXXScopeToken(true)) {
2232 if (!DS.hasTypeSpecifier())
2233 DS.SetTypeSpecError();
2234 goto DoneWithDeclSpec;
2235 }
John McCall8bc2a702010-03-01 18:20:46 +00002236 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2237 goto DoneWithDeclSpec;
John McCall1f476a12010-02-26 08:45:28 +00002238 continue;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002239
2240 case tok::annot_cxxscope: {
Richard Smith3092a3b2012-05-09 18:56:43 +00002241 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002242 goto DoneWithDeclSpec;
2243
John McCall9dab4e62009-12-12 11:40:51 +00002244 CXXScopeSpec SS;
Douglas Gregor869ad452011-02-24 17:54:50 +00002245 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2246 Tok.getAnnotationRange(),
2247 SS);
John McCall9dab4e62009-12-12 11:40:51 +00002248
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002249 // We are looking for a qualified typename.
Douglas Gregor167fa622009-03-25 15:40:00 +00002250 Token Next = NextToken();
Mike Stump11289f42009-09-09 15:08:12 +00002251 if (Next.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002252 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorb67535d2009-03-31 00:43:58 +00002253 ->Kind == TNK_Type_template) {
Douglas Gregor167fa622009-03-25 15:40:00 +00002254 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002255
2256 // C++ [class.qual]p2:
2257 // In a lookup in which the constructor is an acceptable lookup
2258 // result and the nested-name-specifier nominates a class C:
2259 //
2260 // - if the name specified after the
2261 // nested-name-specifier, when looked up in C, is the
2262 // injected-class-name of C (Clause 9), or
2263 //
2264 // - if the name specified after the nested-name-specifier
2265 // is the same as the identifier or the
2266 // simple-template-id's template-name in the last
2267 // component of the nested-name-specifier,
2268 //
2269 // the name is instead considered to name the constructor of
2270 // class C.
Chad Rosierc1183952012-06-26 22:30:43 +00002271 //
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002272 // Thus, if the template-name is actually the constructor
2273 // name, then the code is ill-formed; this interpretation is
Chad Rosierc1183952012-06-26 22:30:43 +00002274 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002275 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCall84821e72010-04-13 06:39:49 +00002276 if ((DSContext == DSC_top_level ||
2277 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2278 TemplateId->Name &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002279 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002280 if (isConstructorDeclarator()) {
2281 // The user meant this to be an out-of-line constructor
2282 // definition, but template arguments are not allowed
2283 // there. Just allow this as a constructor; we'll
2284 // complain about it later.
2285 goto DoneWithDeclSpec;
2286 }
2287
2288 // The user meant this to name a type, but it actually names
2289 // a constructor with some extraneous template
2290 // arguments. Complain, then parse it as a type as the user
2291 // intended.
2292 Diag(TemplateId->TemplateNameLoc,
2293 diag::err_out_of_line_template_id_names_constructor)
2294 << TemplateId->Name;
2295 }
2296
John McCall9dab4e62009-12-12 11:40:51 +00002297 DS.getTypeSpecScope() = SS;
2298 ConsumeToken(); // The C++ scope.
Mike Stump11289f42009-09-09 15:08:12 +00002299 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor167fa622009-03-25 15:40:00 +00002300 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregore7c20652011-03-02 00:47:37 +00002301 AnnotateTemplateIdTokenAsType();
Douglas Gregor167fa622009-03-25 15:40:00 +00002302 continue;
2303 }
2304
Douglas Gregorc5790df2009-09-28 07:26:33 +00002305 if (Next.is(tok::annot_typename)) {
John McCall9dab4e62009-12-12 11:40:51 +00002306 DS.getTypeSpecScope() = SS;
2307 ConsumeToken(); // The C++ scope.
John McCallba7bf592010-08-24 05:47:05 +00002308 if (Tok.getAnnotationValue()) {
2309 ParsedType T = getTypeAnnotation(Tok);
Nico Weber77430342010-11-22 10:30:56 +00002310 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosierc1183952012-06-26 22:30:43 +00002311 Tok.getAnnotationEndLoc(),
John McCallba7bf592010-08-24 05:47:05 +00002312 PrevSpec, DiagID, T);
Richard Smithda837032012-09-14 18:27:01 +00002313 if (isInvalid)
2314 break;
John McCallba7bf592010-08-24 05:47:05 +00002315 }
Douglas Gregorc5790df2009-09-28 07:26:33 +00002316 else
2317 DS.SetTypeSpecError();
2318 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2319 ConsumeToken(); // The typename
2320 }
2321
Douglas Gregor167fa622009-03-25 15:40:00 +00002322 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002323 goto DoneWithDeclSpec;
2324
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002325 // If we're in a context where the identifier could be a class name,
2326 // check whether this is a constructor declaration.
John McCall84821e72010-04-13 06:39:49 +00002327 if ((DSContext == DSC_top_level ||
2328 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosierc1183952012-06-26 22:30:43 +00002329 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002330 &SS)) {
2331 if (isConstructorDeclarator())
2332 goto DoneWithDeclSpec;
2333
2334 // As noted in C++ [class.qual]p2 (cited above), when the name
2335 // of the class is qualified in a context where it could name
2336 // a constructor, its a constructor name. However, we've
2337 // looked at the declarator, and the user probably meant this
2338 // to be a type. Complain that it isn't supposed to be treated
2339 // as a type, then proceed to parse it as a type.
2340 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2341 << Next.getIdentifierInfo();
2342 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002343
John McCallba7bf592010-08-24 05:47:05 +00002344 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2345 Next.getLocation(),
Douglas Gregor844cb502011-03-01 18:12:44 +00002346 getCurScope(), &SS,
2347 false, false, ParsedType(),
Abramo Bagnara4244b432012-01-27 08:46:19 +00002348 /*IsCtorOrDtorName=*/false,
Douglas Gregor844cb502011-03-01 18:12:44 +00002349 /*NonTrivialSourceInfo=*/true);
Douglas Gregor8bf42052009-02-09 18:46:07 +00002350
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002351 // If the referenced identifier is not a type, then this declspec is
2352 // erroneous: We already checked about that it has no type specifier, and
2353 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump11289f42009-09-09 15:08:12 +00002354 // typename.
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002355 if (TypeRep == 0) {
2356 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han9407e502012-11-26 22:54:45 +00002357 ParsedAttributesWithRange Attrs(AttrFactory);
2358 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2359 if (!Attrs.empty()) {
2360 AttrsLastTime = true;
2361 attrs.takeAllFrom(Attrs);
2362 }
2363 continue;
2364 }
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002365 goto DoneWithDeclSpec;
Chris Lattnerb4a8fe82009-04-14 22:17:06 +00002366 }
Mike Stump11289f42009-09-09 15:08:12 +00002367
John McCall9dab4e62009-12-12 11:40:51 +00002368 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002369 ConsumeToken(); // The C++ scope.
2370
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002371 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002372 DiagID, TypeRep);
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002373 if (isInvalid)
2374 break;
Mike Stump11289f42009-09-09 15:08:12 +00002375
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00002376 DS.SetRangeEnd(Tok.getLocation());
2377 ConsumeToken(); // The typename.
2378
2379 continue;
2380 }
Mike Stump11289f42009-09-09 15:08:12 +00002381
Chris Lattnere387d9e2009-01-21 19:48:37 +00002382 case tok::annot_typename: {
John McCallba7bf592010-08-24 05:47:05 +00002383 if (Tok.getAnnotationValue()) {
2384 ParsedType T = getTypeAnnotation(Tok);
Nico Weber7f8bb362010-11-22 12:50:03 +00002385 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00002386 DiagID, T);
2387 } else
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002388 DS.SetTypeSpecError();
Chad Rosierc1183952012-06-26 22:30:43 +00002389
Chris Lattner005fc1b2010-04-05 18:18:31 +00002390 if (isInvalid)
2391 break;
2392
Chris Lattnere387d9e2009-01-21 19:48:37 +00002393 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2394 ConsumeToken(); // The typename
Mike Stump11289f42009-09-09 15:08:12 +00002395
Chris Lattnere387d9e2009-01-21 19:48:37 +00002396 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2397 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002398 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002399 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002400 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002401
Chris Lattnere387d9e2009-01-21 19:48:37 +00002402 continue;
2403 }
Mike Stump11289f42009-09-09 15:08:12 +00002404
Douglas Gregor06873092011-04-28 15:48:45 +00002405 case tok::kw___is_signed:
2406 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2407 // typically treats it as a trait. If we see __is_signed as it appears
2408 // in libstdc++, e.g.,
2409 //
2410 // static const bool __is_signed;
2411 //
2412 // then treat __is_signed as an identifier rather than as a keyword.
2413 if (DS.getTypeSpecType() == TST_bool &&
2414 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2415 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2416 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2417 Tok.setKind(tok::identifier);
2418 }
2419
2420 // We're done with the declaration-specifiers.
2421 goto DoneWithDeclSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00002422
Chris Lattner16fac4f2008-07-26 01:18:38 +00002423 // typedef-name
David Blaikie15a430a2011-12-04 05:04:18 +00002424 case tok::kw_decltype:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002425 case tok::identifier: {
Chris Lattnerbd31aa32009-01-05 00:07:25 +00002426 // In C++, check to see if this is a scope specifier like foo::bar::, if
2427 // so handle it as such. This is important for ctor parsing.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002428 if (getLangOpts().CPlusPlus) {
John McCall1f476a12010-02-26 08:45:28 +00002429 if (TryAnnotateCXXScopeToken(true)) {
2430 if (!DS.hasTypeSpecifier())
2431 DS.SetTypeSpecError();
2432 goto DoneWithDeclSpec;
2433 }
2434 if (!Tok.is(tok::identifier))
2435 continue;
2436 }
Mike Stump11289f42009-09-09 15:08:12 +00002437
Chris Lattner16fac4f2008-07-26 01:18:38 +00002438 // This identifier can only be a typedef name if we haven't already seen
2439 // a type-specifier. Without this check we misparse:
2440 // typedef int X; struct Y { short X; }; as 'short int'.
2441 if (DS.hasTypeSpecifier())
2442 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002443
John Thompson22334602010-02-05 00:12:22 +00002444 // Check for need to substitute AltiVec keyword tokens.
2445 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2446 break;
2447
Richard Smith3092a3b2012-05-09 18:56:43 +00002448 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2449 // allow the use of a typedef name as a type specifier.
2450 if (DS.isTypeAltiVecVector())
2451 goto DoneWithDeclSpec;
2452
John McCallba7bf592010-08-24 05:47:05 +00002453 ParsedType TypeRep =
2454 Actions.getTypeName(*Tok.getIdentifierInfo(),
2455 Tok.getLocation(), getCurScope());
Douglas Gregor8bf42052009-02-09 18:46:07 +00002456
Chris Lattner6cc055a2009-04-12 20:42:31 +00002457 // If this is not a typedef name, don't parse it as part of the declspec,
2458 // it must be an implicit int or an error.
John McCallba7bf592010-08-24 05:47:05 +00002459 if (!TypeRep) {
Michael Han9407e502012-11-26 22:54:45 +00002460 ParsedAttributesWithRange Attrs(AttrFactory);
2461 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2462 if (!Attrs.empty()) {
2463 AttrsLastTime = true;
2464 attrs.takeAllFrom(Attrs);
2465 }
2466 continue;
2467 }
Chris Lattner16fac4f2008-07-26 01:18:38 +00002468 goto DoneWithDeclSpec;
Chris Lattner6cc055a2009-04-12 20:42:31 +00002469 }
Douglas Gregor8bf42052009-02-09 18:46:07 +00002470
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002471 // If we're in a context where the identifier could be a class name,
2472 // check whether this is a constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002473 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002474 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002475 isConstructorDeclarator())
Douglas Gregor61956c42008-10-31 09:07:45 +00002476 goto DoneWithDeclSpec;
2477
Douglas Gregor9817f4a2009-02-09 15:09:02 +00002478 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00002479 DiagID, TypeRep);
Chris Lattner16fac4f2008-07-26 01:18:38 +00002480 if (isInvalid)
2481 break;
Mike Stump11289f42009-09-09 15:08:12 +00002482
Chris Lattner16fac4f2008-07-26 01:18:38 +00002483 DS.SetRangeEnd(Tok.getLocation());
2484 ConsumeToken(); // The identifier
2485
2486 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2487 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosierc1183952012-06-26 22:30:43 +00002488 // Objective-C interface.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002489 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002490 ParseObjCProtocolQualifiers(DS);
Chad Rosierc1183952012-06-26 22:30:43 +00002491
Steve Naroffcd5e7822008-09-22 10:28:57 +00002492 // Need to support trailing type qualifiers (e.g. "id<p> const").
2493 // If a type specifier follows, it will be diagnosed elsewhere.
2494 continue;
Chris Lattner16fac4f2008-07-26 01:18:38 +00002495 }
Douglas Gregor7f741122009-02-25 19:37:18 +00002496
2497 // type-name
2498 case tok::annot_template_id: {
Argyrios Kyrtzidisc0c5dd22011-06-22 06:09:49 +00002499 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorb67535d2009-03-31 00:43:58 +00002500 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor7f741122009-02-25 19:37:18 +00002501 // This template-id does not refer to a type name, so we're
2502 // done with the type-specifiers.
2503 goto DoneWithDeclSpec;
2504 }
2505
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002506 // If we're in a context where the template-id could be a
2507 // constructor name or specialization, check whether this is a
2508 // constructor declaration.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002509 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor0be31a22010-07-02 17:43:08 +00002510 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor9de54ea2010-01-13 17:31:36 +00002511 isConstructorDeclarator())
2512 goto DoneWithDeclSpec;
2513
Douglas Gregor7f741122009-02-25 19:37:18 +00002514 // Turn the template-id annotation token into a type annotation
2515 // token, then try again to parse it as a type-specifier.
Douglas Gregorfe3d7d02009-04-01 21:51:26 +00002516 AnnotateTemplateIdTokenAsType();
Douglas Gregor7f741122009-02-25 19:37:18 +00002517 continue;
2518 }
2519
Chris Lattnere37e2332006-08-15 04:50:22 +00002520 // GNU attributes support.
2521 case tok::kw___attribute:
DeLesley Hutchinsbd2ee132012-03-02 22:12:59 +00002522 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Chris Lattnerb95cca02006-10-17 03:01:08 +00002523 continue;
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002524
2525 // Microsoft declspec support.
2526 case tok::kw___declspec:
John McCall53fa7142010-12-24 02:08:15 +00002527 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Naroff3a9b7e02008-12-24 20:59:21 +00002528 continue;
Mike Stump11289f42009-09-09 15:08:12 +00002529
Steve Naroff44ac7772008-12-25 14:16:32 +00002530 // Microsoft single token adornments.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002531 case tok::kw___forceinline: {
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002532 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002533 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithda837032012-09-14 18:27:01 +00002534 SourceLocation AttrNameLoc = Tok.getLocation();
Alexis Hunta0e54d42012-06-18 16:13:52 +00002535 // FIXME: This does not work correctly if it is set to be a declspec
2536 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002537 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Alexis Hunta0e54d42012-06-18 16:13:52 +00002538 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithda837032012-09-14 18:27:01 +00002539 break;
Michael J. Spencerf97bd8c2012-06-18 07:00:48 +00002540 }
Eli Friedman53339e02009-06-08 23:27:34 +00002541
2542 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00002543 case tok::kw___ptr32:
Steve Narofff9c29d42008-12-25 14:41:26 +00002544 case tok::kw___w64:
Steve Naroff44ac7772008-12-25 14:16:32 +00002545 case tok::kw___cdecl:
2546 case tok::kw___stdcall:
2547 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00002548 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00002549 case tok::kw___unaligned:
John McCall53fa7142010-12-24 02:08:15 +00002550 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00002551 continue;
2552
Dawn Perchik335e16b2010-09-03 01:29:35 +00002553 // Borland single token adornments.
2554 case tok::kw___pascal:
John McCall53fa7142010-12-24 02:08:15 +00002555 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00002556 continue;
2557
Peter Collingbourne7ce13fc2011-02-14 01:42:53 +00002558 // OpenCL single token adornments.
2559 case tok::kw___kernel:
2560 ParseOpenCLAttributes(DS.getAttributes());
2561 continue;
2562
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002563 // storage-class-specifier
2564 case tok::kw_typedef:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002565 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2566 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002567 break;
2568 case tok::kw_extern:
Chris Lattner353f5742006-11-28 04:50:12 +00002569 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002570 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002571 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2572 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002573 break;
Steve Naroff2050b0d2007-12-18 00:16:02 +00002574 case tok::kw___private_extern__:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002575 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2576 Loc, PrevSpec, DiagID);
Steve Naroff2050b0d2007-12-18 00:16:02 +00002577 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002578 case tok::kw_static:
Chris Lattner353f5742006-11-28 04:50:12 +00002579 if (DS.isThreadSpecified())
Chris Lattner6d29c102008-11-18 07:48:38 +00002580 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002581 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2582 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002583 break;
2584 case tok::kw_auto:
Richard Smith2bf7fdb2013-01-02 11:42:31 +00002585 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002586 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002587 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2588 PrevSpec, DiagID);
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002589 if (!isInvalid)
Richard Smith58c74332011-09-04 19:54:14 +00002590 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002591 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith58c74332011-09-04 19:54:14 +00002592 } else
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002593 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2594 DiagID);
Richard Smith58c74332011-09-04 19:54:14 +00002595 } else
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002596 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2597 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002598 break;
2599 case tok::kw_register:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002600 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2601 PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002602 break;
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002603 case tok::kw_mutable:
Peter Collingbourne485b80f2011-10-06 03:01:00 +00002604 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2605 PrevSpec, DiagID);
Sebastian Redlccdfaba2008-11-14 23:42:31 +00002606 break;
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002607 case tok::kw___thread:
John McCall49bfce42009-08-03 20:12:06 +00002608 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Chris Lattnerf63f89a2006-08-05 03:28:50 +00002609 break;
Mike Stump11289f42009-09-09 15:08:12 +00002610
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002611 // function-specifier
2612 case tok::kw_inline:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002613 isInvalid = DS.setFunctionSpecInline(Loc);
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002614 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002615 case tok::kw_virtual:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002616 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002617 break;
Douglas Gregor61956c42008-10-31 09:07:45 +00002618 case tok::kw_explicit:
Chad Rosier92f0dcc2012-12-21 22:24:43 +00002619 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregor61956c42008-10-31 09:07:45 +00002620 break;
Richard Smith0015f092013-01-17 22:16:11 +00002621 case tok::kw__Noreturn:
2622 if (!getLangOpts().C11)
2623 Diag(Loc, diag::ext_c11_noreturn);
2624 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2625 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002626
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002627 // alignment-specifier
2628 case tok::kw__Alignas:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002629 if (!getLangOpts().C11)
Jordan Rose58d54722012-06-30 21:33:57 +00002630 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne2f3cf4b2011-09-29 18:04:28 +00002631 ParseAlignmentSpecifier(DS.getAttributes());
2632 continue;
2633
Anders Carlssoncd8db412009-05-06 04:46:28 +00002634 // friend
2635 case tok::kw_friend:
John McCall07e91c02009-08-06 02:15:43 +00002636 if (DSContext == DSC_class)
2637 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2638 else {
2639 PrevSpec = ""; // not actually used by the diagnostic
2640 DiagID = diag::err_friend_invalid_in_context;
2641 isInvalid = true;
2642 }
Anders Carlssoncd8db412009-05-06 04:46:28 +00002643 break;
Mike Stump11289f42009-09-09 15:08:12 +00002644
Douglas Gregor26701a42011-09-09 02:06:17 +00002645 // Modules
2646 case tok::kw___module_private__:
2647 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2648 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002649
Sebastian Redl39c2a8b2009-11-05 15:47:02 +00002650 // constexpr
2651 case tok::kw_constexpr:
2652 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2653 break;
2654
Chris Lattnere387d9e2009-01-21 19:48:37 +00002655 // type-specifier
2656 case tok::kw_short:
John McCall49bfce42009-08-03 20:12:06 +00002657 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2658 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002659 break;
2660 case tok::kw_long:
2661 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCall49bfce42009-08-03 20:12:06 +00002662 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2663 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002664 else
John McCall49bfce42009-08-03 20:12:06 +00002665 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2666 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002667 break;
Francois Pichet84133e42011-04-28 01:59:37 +00002668 case tok::kw___int64:
2669 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2670 DiagID);
2671 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002672 case tok::kw_signed:
John McCall49bfce42009-08-03 20:12:06 +00002673 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2674 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002675 break;
2676 case tok::kw_unsigned:
John McCall49bfce42009-08-03 20:12:06 +00002677 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2678 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002679 break;
2680 case tok::kw__Complex:
John McCall49bfce42009-08-03 20:12:06 +00002681 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2682 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002683 break;
2684 case tok::kw__Imaginary:
John McCall49bfce42009-08-03 20:12:06 +00002685 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2686 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002687 break;
2688 case tok::kw_void:
John McCall49bfce42009-08-03 20:12:06 +00002689 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2690 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002691 break;
2692 case tok::kw_char:
John McCall49bfce42009-08-03 20:12:06 +00002693 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2694 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002695 break;
2696 case tok::kw_int:
John McCall49bfce42009-08-03 20:12:06 +00002697 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2698 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002699 break;
Richard Smithf016bbc2012-04-04 06:24:32 +00002700 case tok::kw___int128:
2701 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2702 DiagID);
2703 break;
2704 case tok::kw_half:
2705 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2706 DiagID);
2707 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002708 case tok::kw_float:
John McCall49bfce42009-08-03 20:12:06 +00002709 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2710 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002711 break;
2712 case tok::kw_double:
John McCall49bfce42009-08-03 20:12:06 +00002713 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2714 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002715 break;
2716 case tok::kw_wchar_t:
John McCall49bfce42009-08-03 20:12:06 +00002717 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2718 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002719 break;
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002720 case tok::kw_char16_t:
John McCall49bfce42009-08-03 20:12:06 +00002721 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2722 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002723 break;
2724 case tok::kw_char32_t:
John McCall49bfce42009-08-03 20:12:06 +00002725 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2726 DiagID);
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00002727 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002728 case tok::kw_bool:
2729 case tok::kw__Bool:
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002730 if (Tok.is(tok::kw_bool) &&
2731 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2732 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2733 PrevSpec = ""; // Not used by the diagnostic.
2734 DiagID = diag::err_bool_redeclaration;
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002735 // For better error recovery.
2736 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis20ee5ae2010-11-16 18:18:13 +00002737 isInvalid = true;
2738 } else {
2739 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2740 DiagID);
2741 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002742 break;
2743 case tok::kw__Decimal32:
John McCall49bfce42009-08-03 20:12:06 +00002744 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2745 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002746 break;
2747 case tok::kw__Decimal64:
John McCall49bfce42009-08-03 20:12:06 +00002748 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2749 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002750 break;
2751 case tok::kw__Decimal128:
John McCall49bfce42009-08-03 20:12:06 +00002752 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2753 DiagID);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002754 break;
John Thompson22334602010-02-05 00:12:22 +00002755 case tok::kw___vector:
2756 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2757 break;
2758 case tok::kw___pixel:
2759 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2760 break;
Guy Benyeid8a08ea2012-12-18 14:38:23 +00002761 case tok::kw_image1d_t:
2762 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2763 PrevSpec, DiagID);
2764 break;
2765 case tok::kw_image1d_array_t:
2766 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2767 PrevSpec, DiagID);
2768 break;
2769 case tok::kw_image1d_buffer_t:
2770 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2771 PrevSpec, DiagID);
2772 break;
2773 case tok::kw_image2d_t:
2774 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2775 PrevSpec, DiagID);
2776 break;
2777 case tok::kw_image2d_array_t:
2778 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2779 PrevSpec, DiagID);
2780 break;
2781 case tok::kw_image3d_t:
2782 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2783 PrevSpec, DiagID);
2784 break;
Guy Benyei61054192013-02-07 10:55:47 +00002785 case tok::kw_sampler_t:
2786 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2787 PrevSpec, DiagID);
2788 break;
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00002789 case tok::kw_event_t:
2790 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2791 PrevSpec, DiagID);
2792 break;
John McCall39439732011-04-09 22:50:59 +00002793 case tok::kw___unknown_anytype:
2794 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2795 PrevSpec, DiagID);
2796 break;
Chris Lattnere387d9e2009-01-21 19:48:37 +00002797
2798 // class-specifier:
2799 case tok::kw_class:
2800 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00002801 case tok::kw___interface:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002802 case tok::kw_union: {
2803 tok::TokenKind Kind = Tok.getKind();
2804 ConsumeToken();
Michael Han9407e502012-11-26 22:54:45 +00002805
2806 // These are attributes following class specifiers.
2807 // To produce better diagnostic, we parse them when
2808 // parsing class specifier.
Bill Wendling44426052012-12-20 19:22:21 +00002809 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smithc5b05522012-03-12 07:56:15 +00002810 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendling44426052012-12-20 19:22:21 +00002811 EnteringContext, DSContext, Attributes);
Michael Han9407e502012-11-26 22:54:45 +00002812
2813 // If there are attributes following class specifier,
2814 // take them over and handle them here.
Bill Wendling44426052012-12-20 19:22:21 +00002815 if (!Attributes.empty()) {
Michael Han9407e502012-11-26 22:54:45 +00002816 AttrsLastTime = true;
Bill Wendling44426052012-12-20 19:22:21 +00002817 attrs.takeAllFrom(Attributes);
Michael Han9407e502012-11-26 22:54:45 +00002818 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002819 continue;
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002820 }
Chris Lattnere387d9e2009-01-21 19:48:37 +00002821
2822 // enum-specifier:
2823 case tok::kw_enum:
Chris Lattnerffaa0e62009-04-12 21:49:30 +00002824 ConsumeToken();
Richard Smithc5b05522012-03-12 07:56:15 +00002825 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattnere387d9e2009-01-21 19:48:37 +00002826 continue;
2827
2828 // cv-qualifier:
2829 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00002830 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002831 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002832 break;
2833 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00002834 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002835 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002836 break;
2837 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00002838 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00002839 getLangOpts());
Chris Lattnere387d9e2009-01-21 19:48:37 +00002840 break;
2841
Douglas Gregor333489b2009-03-27 23:10:48 +00002842 // C++ typename-specifier:
2843 case tok::kw_typename:
John McCall1f476a12010-02-26 08:45:28 +00002844 if (TryAnnotateTypeOrScopeToken()) {
2845 DS.SetTypeSpecError();
2846 goto DoneWithDeclSpec;
2847 }
2848 if (!Tok.is(tok::kw_typename))
Douglas Gregor333489b2009-03-27 23:10:48 +00002849 continue;
2850 break;
2851
Chris Lattnere387d9e2009-01-21 19:48:37 +00002852 // GNU typeof support.
2853 case tok::kw_typeof:
2854 ParseTypeofSpecifier(DS);
2855 continue;
2856
David Blaikie15a430a2011-12-04 05:04:18 +00002857 case tok::annot_decltype:
Anders Carlsson74948d02009-06-24 17:47:40 +00002858 ParseDecltypeSpecifier(DS);
2859 continue;
2860
Alexis Hunt4a257072011-05-19 05:37:45 +00002861 case tok::kw___underlying_type:
2862 ParseUnderlyingTypeSpecifier(DS);
Eli Friedman0dfb8892011-10-06 23:00:33 +00002863 continue;
2864
2865 case tok::kw__Atomic:
2866 ParseAtomicSpecifier(DS);
2867 continue;
Alexis Hunt4a257072011-05-19 05:37:45 +00002868
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002869 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00002870 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00002871 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00002872 goto DoneWithDeclSpec;
2873 case tok::kw___private:
2874 case tok::kw___global:
2875 case tok::kw___local:
2876 case tok::kw___constant:
2877 case tok::kw___read_only:
2878 case tok::kw___write_only:
2879 case tok::kw___read_write:
2880 ParseOpenCLQualifiers(DS);
2881 break;
Chad Rosierc1183952012-06-26 22:30:43 +00002882
Steve Naroffcfdf6162008-06-05 00:02:44 +00002883 case tok::less:
Chris Lattner16fac4f2008-07-26 01:18:38 +00002884 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattner0974b232008-07-26 00:20:22 +00002885 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2886 // but we support it.
David Blaikiebbafb8a2012-03-11 07:00:24 +00002887 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattner0974b232008-07-26 00:20:22 +00002888 goto DoneWithDeclSpec;
Mike Stump11289f42009-09-09 15:08:12 +00002889
Douglas Gregor3a001f42010-11-19 17:10:50 +00002890 if (!ParseObjCProtocolQualifiers(DS))
2891 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2892 << FixItHint::CreateInsertion(Loc, "id")
2893 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosierc1183952012-06-26 22:30:43 +00002894
Douglas Gregor06e41ae2010-10-21 23:17:00 +00002895 // Need to support trailing type qualifiers (e.g. "id<p> const").
2896 // If a type specifier follows, it will be diagnosed elsewhere.
2897 continue;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002898 }
John McCall49bfce42009-08-03 20:12:06 +00002899 // If the specifier wasn't legal, issue a diagnostic.
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002900 if (isInvalid) {
2901 assert(PrevSpec && "Method did not return previous specifier!");
John McCall49bfce42009-08-03 20:12:06 +00002902 assert(DiagID);
Chad Rosierc1183952012-06-26 22:30:43 +00002903
Douglas Gregora05f5ab2010-08-23 14:34:43 +00002904 if (DiagID == diag::ext_duplicate_declspec)
2905 Diag(Tok, DiagID)
2906 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2907 else
2908 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerb9093cd2006-08-04 04:39:53 +00002909 }
Fariborz Jahanianbb6db562011-02-22 23:17:49 +00002910
Chris Lattner2e232092008-03-13 06:29:04 +00002911 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahanian2b059992011-04-19 21:42:37 +00002912 if (DiagID != diag::err_bool_redeclaration)
2913 ConsumeToken();
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00002914
2915 AttrsLastTime = false;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00002916 }
2917}
Douglas Gregoreb31f392008-12-01 23:54:00 +00002918
Chris Lattner70ae4912007-10-29 04:42:53 +00002919/// ParseStructDeclaration - Parse a struct declaration without the terminating
2920/// semicolon.
2921///
Chris Lattner90a26b02007-01-23 04:38:16 +00002922/// struct-declaration:
Chris Lattner70ae4912007-10-29 04:42:53 +00002923/// specifier-qualifier-list struct-declarator-list
Chris Lattner736ed5d2007-06-09 05:59:07 +00002924/// [GNU] __extension__ struct-declaration
Chris Lattner70ae4912007-10-29 04:42:53 +00002925/// [GNU] specifier-qualifier-list
Chris Lattner90a26b02007-01-23 04:38:16 +00002926/// struct-declarator-list:
2927/// struct-declarator
2928/// struct-declarator-list ',' struct-declarator
2929/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2930/// struct-declarator:
2931/// declarator
2932/// [GNU] declarator attributes[opt]
2933/// declarator[opt] ':' constant-expression
2934/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2935///
Chris Lattnera12405b2008-04-10 06:46:29 +00002936void Parser::
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002937ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosierc1183952012-06-26 22:30:43 +00002938
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002939 if (Tok.is(tok::kw___extension__)) {
2940 // __extension__ silences extension warnings in the subexpression.
2941 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff97170802007-08-20 22:28:22 +00002942 ConsumeToken();
Chris Lattnerf02ef3e2008-10-20 06:45:43 +00002943 return ParseStructDeclaration(DS, Fields);
2944 }
Mike Stump11289f42009-09-09 15:08:12 +00002945
Steve Naroff97170802007-08-20 22:28:22 +00002946 // Parse the common specifier-qualifiers-list piece.
Steve Naroff97170802007-08-20 22:28:22 +00002947 ParseSpecifierQualifierList(DS);
Mike Stump11289f42009-09-09 15:08:12 +00002948
Douglas Gregorc6f58fe2009-01-12 22:49:06 +00002949 // If there are no declarators, this is a free-standing declaration
2950 // specifier. Let the actions module cope with it.
Chris Lattner76c72282007-10-09 17:33:22 +00002951 if (Tok.is(tok::semi)) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002952 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2953 DS);
2954 DS.complete(TheDecl);
Steve Naroff97170802007-08-20 22:28:22 +00002955 return;
2956 }
2957
2958 // Read struct-declarators until we find the semicolon.
John McCallcfefb6d2009-11-03 02:38:08 +00002959 bool FirstDeclarator = true;
Richard Smith8d06f422012-01-12 23:53:29 +00002960 SourceLocation CommaLoc;
Steve Naroff97170802007-08-20 22:28:22 +00002961 while (1) {
Eli Friedman89b1f2c2012-08-08 23:04:35 +00002962 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith8d06f422012-01-12 23:53:29 +00002963 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallcfefb6d2009-11-03 02:38:08 +00002964
Bill Wendling44426052012-12-20 19:22:21 +00002965 // Attributes are only allowed here on successive declarators.
John McCall53fa7142010-12-24 02:08:15 +00002966 if (!FirstDeclarator)
2967 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump11289f42009-09-09 15:08:12 +00002968
Steve Naroff97170802007-08-20 22:28:22 +00002969 /// struct-declarator: declarator
2970 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002971 if (Tok.isNot(tok::colon)) {
2972 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2973 ColonProtectionRAIIObject X(*this);
Chris Lattnera12405b2008-04-10 06:46:29 +00002974 ParseDeclarator(DeclaratorInfo.D);
Chris Lattner17c3b1f2009-12-10 01:59:24 +00002975 }
Mike Stump11289f42009-09-09 15:08:12 +00002976
Chris Lattner76c72282007-10-09 17:33:22 +00002977 if (Tok.is(tok::colon)) {
Steve Naroff97170802007-08-20 22:28:22 +00002978 ConsumeToken();
John McCalldadc5752010-08-24 06:29:42 +00002979 ExprResult Res(ParseConstantExpression());
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00002980 if (Res.isInvalid())
Steve Naroff97170802007-08-20 22:28:22 +00002981 SkipUntil(tok::semi, true, true);
Chris Lattner32295d32008-04-10 06:15:14 +00002982 else
Sebastian Redld9f7b1c2008-12-10 00:02:53 +00002983 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff97170802007-08-20 22:28:22 +00002984 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002985
Steve Naroff97170802007-08-20 22:28:22 +00002986 // If attributes exist after the declarator, parse them.
John McCall53fa7142010-12-24 02:08:15 +00002987 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002988
John McCallcfefb6d2009-11-03 02:38:08 +00002989 // We're done with this declarator; invoke the callback.
Eli Friedmanba01f2b2012-08-08 23:35:12 +00002990 Fields.invoke(DeclaratorInfo);
John McCallcfefb6d2009-11-03 02:38:08 +00002991
Steve Naroff97170802007-08-20 22:28:22 +00002992 // If we don't have a comma, it is either the end of the list (a ';')
2993 // or an error, bail out.
Chris Lattner76c72282007-10-09 17:33:22 +00002994 if (Tok.isNot(tok::comma))
Chris Lattner70ae4912007-10-29 04:42:53 +00002995 return;
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002996
Steve Naroff97170802007-08-20 22:28:22 +00002997 // Consume the comma.
Richard Smith8d06f422012-01-12 23:53:29 +00002998 CommaLoc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00002999
John McCallcfefb6d2009-11-03 02:38:08 +00003000 FirstDeclarator = false;
Steve Naroff97170802007-08-20 22:28:22 +00003001 }
Steve Naroff97170802007-08-20 22:28:22 +00003002}
3003
3004/// ParseStructUnionBody
3005/// struct-contents:
3006/// struct-declaration-list
3007/// [EXT] empty
3008/// [GNU] "struct-declaration-list" without terminatoring ';'
3009/// struct-declaration-list:
3010/// struct-declaration
3011/// struct-declaration-list struct-declaration
Chris Lattner535b8302008-06-21 19:39:06 +00003012/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff97170802007-08-20 22:28:22 +00003013///
Chris Lattner1300fb92007-01-23 23:42:53 +00003014void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCall48871652010-08-21 09:40:31 +00003015 unsigned TagType, Decl *TagDecl) {
John McCallfaf5fb42010-08-26 23:41:50 +00003016 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3017 "parsing struct/union body");
Mike Stump11289f42009-09-09 15:08:12 +00003018
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003019 BalancedDelimiterTracker T(*this, tok::l_brace);
3020 if (T.consumeOpen())
3021 return;
Mike Stump11289f42009-09-09 15:08:12 +00003022
Douglas Gregor658b9552009-01-09 22:42:13 +00003023 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003024 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003025
Chris Lattner7b9ace62007-01-23 20:11:08 +00003026 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
3027 // C++.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003028 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithe4345902011-12-29 21:57:33 +00003029 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3030 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3031 }
Chris Lattner7b9ace62007-01-23 20:11:08 +00003032
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003033 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnera12405b2008-04-10 06:46:29 +00003034
Chris Lattner7b9ace62007-01-23 20:11:08 +00003035 // While we still have something to read, read the declarations in the struct.
Chris Lattner76c72282007-10-09 17:33:22 +00003036 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003037 // Each iteration of this loop reads one struct-declaration.
Mike Stump11289f42009-09-09 15:08:12 +00003038
Chris Lattner736ed5d2007-06-09 05:59:07 +00003039 // Check for extraneous top-level semicolon.
Chris Lattner76c72282007-10-09 17:33:22 +00003040 if (Tok.is(tok::semi)) {
Richard Smith87f5dc52012-07-23 05:45:25 +00003041 ConsumeExtraSemi(InsideStruct, TagType);
Chris Lattner36e46a22007-06-09 05:49:55 +00003042 continue;
3043 }
Chris Lattnera12405b2008-04-10 06:46:29 +00003044
John McCallcfefb6d2009-11-03 02:38:08 +00003045 if (!Tok.is(tok::at)) {
3046 struct CFieldCallback : FieldCallback {
3047 Parser &P;
John McCall48871652010-08-21 09:40:31 +00003048 Decl *TagDecl;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003049 SmallVectorImpl<Decl *> &FieldDecls;
John McCallcfefb6d2009-11-03 02:38:08 +00003050
John McCall48871652010-08-21 09:40:31 +00003051 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003052 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallcfefb6d2009-11-03 02:38:08 +00003053 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3054
Eli Friedman934dbbf2012-08-08 23:53:27 +00003055 void invoke(ParsingFieldDeclarator &FD) {
John McCallcfefb6d2009-11-03 02:38:08 +00003056 // Install the declarator into the current TagDecl.
John McCall48871652010-08-21 09:40:31 +00003057 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall5e6253b2009-11-03 21:13:47 +00003058 FD.D.getDeclSpec().getSourceRange().getBegin(),
3059 FD.D, FD.BitfieldSize);
John McCallcfefb6d2009-11-03 02:38:08 +00003060 FieldDecls.push_back(Field);
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003061 FD.complete(Field);
Douglas Gregor66a985d2009-08-26 14:27:30 +00003062 }
John McCallcfefb6d2009-11-03 02:38:08 +00003063 } Callback(*this, TagDecl, FieldDecls);
3064
Eli Friedman89b1f2c2012-08-08 23:04:35 +00003065 // Parse all the comma separated declarators.
3066 ParsingDeclSpec DS(*this);
John McCallcfefb6d2009-11-03 02:38:08 +00003067 ParseStructDeclaration(DS, Callback);
Chris Lattner535b8302008-06-21 19:39:06 +00003068 } else { // Handle @defs
3069 ConsumeToken();
3070 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3071 Diag(Tok, diag::err_unexpected_at);
Chris Lattner245c5332010-02-02 00:37:27 +00003072 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003073 continue;
3074 }
3075 ConsumeToken();
3076 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3077 if (!Tok.is(tok::identifier)) {
3078 Diag(Tok, diag::err_expected_ident);
Chris Lattner245c5332010-02-02 00:37:27 +00003079 SkipUntil(tok::semi, true);
Chris Lattner535b8302008-06-21 19:39:06 +00003080 continue;
3081 }
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003082 SmallVector<Decl *, 16> Fields;
Douglas Gregor0be31a22010-07-02 17:43:08 +00003083 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor91f84212008-12-11 16:49:14 +00003084 Tok.getIdentifierInfo(), Fields);
Chris Lattner535b8302008-06-21 19:39:06 +00003085 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3086 ConsumeToken();
3087 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump11289f42009-09-09 15:08:12 +00003088 }
Chris Lattner736ed5d2007-06-09 05:59:07 +00003089
Chris Lattner76c72282007-10-09 17:33:22 +00003090 if (Tok.is(tok::semi)) {
Chris Lattner90a26b02007-01-23 04:38:16 +00003091 ConsumeToken();
Chris Lattner76c72282007-10-09 17:33:22 +00003092 } else if (Tok.is(tok::r_brace)) {
Chris Lattner245c5332010-02-02 00:37:27 +00003093 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Chris Lattner0c7e82d2007-06-09 05:54:40 +00003094 break;
Chris Lattner90a26b02007-01-23 04:38:16 +00003095 } else {
Chris Lattner245c5332010-02-02 00:37:27 +00003096 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3097 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Chris Lattner90a26b02007-01-23 04:38:16 +00003098 SkipUntil(tok::r_brace, true, true);
Chris Lattner245c5332010-02-02 00:37:27 +00003099 // If we stopped at a ';', eat it.
3100 if (Tok.is(tok::semi)) ConsumeToken();
Chris Lattner90a26b02007-01-23 04:38:16 +00003101 }
3102 }
Mike Stump11289f42009-09-09 15:08:12 +00003103
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003104 T.consumeClose();
Mike Stump11289f42009-09-09 15:08:12 +00003105
John McCall084e83d2011-03-24 11:26:52 +00003106 ParsedAttributes attrs(AttrFactory);
Chris Lattner90a26b02007-01-23 04:38:16 +00003107 // If attributes exist after struct contents, parse them.
John McCall53fa7142010-12-24 02:08:15 +00003108 MaybeParseGNUAttributes(attrs);
Daniel Dunbar15619c72008-10-03 02:03:53 +00003109
Douglas Gregor0be31a22010-07-02 17:43:08 +00003110 Actions.ActOnFields(getCurScope(),
David Blaikie751c5582011-09-22 02:58:26 +00003111 RecordLoc, TagDecl, FieldDecls,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003112 T.getOpenLocation(), T.getCloseLocation(),
John McCall53fa7142010-12-24 02:08:15 +00003113 attrs.getList());
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003114 StructScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003115 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3116 T.getCloseLocation());
Chris Lattner90a26b02007-01-23 04:38:16 +00003117}
3118
Chris Lattner3b561a32006-08-13 00:12:11 +00003119/// ParseEnumSpecifier
Chris Lattner1890ac82006-08-13 01:16:23 +00003120/// enum-specifier: [C99 6.7.2.2]
Chris Lattner3b561a32006-08-13 00:12:11 +00003121/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003122///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Chris Lattnere37e2332006-08-15 04:50:22 +00003123/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3124/// '}' attributes[opt]
Aaron Ballman9ecff022012-03-01 04:09:28 +00003125/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3126/// '}'
Chris Lattner3b561a32006-08-13 00:12:11 +00003127/// 'enum' identifier
Chris Lattnere37e2332006-08-15 04:50:22 +00003128/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003129///
Richard Smith7d137e32012-03-23 03:33:32 +00003130/// [C++11] enum-head '{' enumerator-list[opt] '}'
3131/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor0bf31402010-10-08 23:50:27 +00003132///
Richard Smith7d137e32012-03-23 03:33:32 +00003133/// enum-head: [C++11]
3134/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3135/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3136/// identifier enum-base[opt]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003137///
Richard Smith7d137e32012-03-23 03:33:32 +00003138/// enum-key: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003139/// 'enum'
3140/// 'enum' 'class'
3141/// 'enum' 'struct'
3142///
Richard Smith7d137e32012-03-23 03:33:32 +00003143/// enum-base: [C++11]
Douglas Gregor0bf31402010-10-08 23:50:27 +00003144/// ':' type-specifier-seq
3145///
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003146/// [C++] elaborated-type-specifier:
3147/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3148///
Chris Lattnerffaa0e62009-04-12 21:49:30 +00003149void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregordc70c3a2010-03-02 17:53:14 +00003150 const ParsedTemplateInfo &TemplateInfo,
Richard Smithc5b05522012-03-12 07:56:15 +00003151 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerffbc2712007-01-25 06:05:38 +00003152 // Parse the tag portion of this.
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003153 if (Tok.is(tok::code_completion)) {
3154 // Code completion for an enum name.
Douglas Gregor0be31a22010-07-02 17:43:08 +00003155 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00003156 return cutOffParsing();
Douglas Gregorf45b0cf2009-09-18 15:37:17 +00003157 }
John McCallcb432fa2011-07-06 05:58:41 +00003158
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003159 // If attributes exist after tag, parse them.
3160 ParsedAttributesWithRange attrs(AttrFactory);
3161 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003162 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003163
3164 // If declspecs exist after tag, parse them.
3165 while (Tok.is(tok::kw___declspec))
3166 ParseMicrosoftDeclSpec(attrs);
3167
Richard Smith0f8ee222012-01-10 01:33:14 +00003168 SourceLocation ScopedEnumKWLoc;
John McCallcb432fa2011-07-06 05:58:41 +00003169 bool IsScopedUsingClassTag = false;
3170
John McCallbeae29a2012-06-23 22:30:04 +00003171 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003172 if (getLangOpts().CPlusPlus11 &&
John McCallcb432fa2011-07-06 05:58:41 +00003173 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003174 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCallcb432fa2011-07-06 05:58:41 +00003175 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smith0f8ee222012-01-10 01:33:14 +00003176 ScopedEnumKWLoc = ConsumeToken();
Chad Rosierc1183952012-06-26 22:30:43 +00003177
Bill Wendling44426052012-12-20 19:22:21 +00003178 // Attributes are not allowed between these keywords. Diagnose,
John McCallbeae29a2012-06-23 22:30:04 +00003179 // but then just treat them like they appeared in the right place.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003180 ProhibitAttributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003181
3182 // They are allowed afterwards, though.
3183 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003184 MaybeParseCXX11Attributes(attrs);
John McCallbeae29a2012-06-23 22:30:04 +00003185 while (Tok.is(tok::kw___declspec))
3186 ParseMicrosoftDeclSpec(attrs);
John McCallcb432fa2011-07-06 05:58:41 +00003187 }
Richard Smith7d137e32012-03-23 03:33:32 +00003188
John McCall6347b682012-05-07 06:16:58 +00003189 // C++11 [temp.explicit]p12:
3190 // The usual access controls do not apply to names used to specify
3191 // explicit instantiations.
3192 // We extend this to also cover explicit specializations. Note that
3193 // we don't suppress if this turns out to be an elaborated type
3194 // specifier.
3195 bool shouldDelayDiagsInTag =
3196 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3197 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3198 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith7d137e32012-03-23 03:33:32 +00003199
Richard Smithbfdb1082012-03-12 08:56:40 +00003200 // Enum definitions should not be parsed in a trailing-return-type.
3201 bool AllowDeclaration = DSC != DSC_trailing;
3202
3203 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003204 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smithbfdb1082012-03-12 08:56:40 +00003205 getLangOpts().ObjC2);
John McCallcb432fa2011-07-06 05:58:41 +00003206
Abramo Bagnarad7548482010-05-19 21:37:53 +00003207 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikiebbafb8a2012-03-11 07:00:24 +00003208 if (getLangOpts().CPlusPlus) {
John McCallcb432fa2011-07-06 05:58:41 +00003209 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3210 // if a fixed underlying type is allowed.
3211 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosierc1183952012-06-26 22:30:43 +00003212
3213 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003214 /*EnteringContext=*/false))
John McCall1f476a12010-02-26 08:45:28 +00003215 return;
3216
3217 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003218 Diag(Tok, diag::err_expected_ident);
3219 if (Tok.isNot(tok::l_brace)) {
3220 // Has no name and is not a definition.
3221 // Skip the rest of this declarator, up until the comma or semicolon.
3222 SkipUntil(tok::comma, true);
3223 return;
3224 }
3225 }
3226 }
Mike Stump11289f42009-09-09 15:08:12 +00003227
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003228 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003229 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smithbfdb1082012-03-12 08:56:40 +00003230 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003231 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump11289f42009-09-09 15:08:12 +00003232
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003233 // Skip the rest of this declarator, up until the comma or semicolon.
3234 SkipUntil(tok::comma, true);
Chris Lattner3b561a32006-08-13 00:12:11 +00003235 return;
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003236 }
Mike Stump11289f42009-09-09 15:08:12 +00003237
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003238 // If an identifier is present, consume and remember it.
3239 IdentifierInfo *Name = 0;
3240 SourceLocation NameLoc;
3241 if (Tok.is(tok::identifier)) {
3242 Name = Tok.getIdentifierInfo();
3243 NameLoc = ConsumeToken();
3244 }
Mike Stump11289f42009-09-09 15:08:12 +00003245
Richard Smith0f8ee222012-01-10 01:33:14 +00003246 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor0bf31402010-10-08 23:50:27 +00003247 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3248 // declaration of a scoped enumeration.
3249 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smith0f8ee222012-01-10 01:33:14 +00003250 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003251 IsScopedUsingClassTag = false;
Douglas Gregor0bf31402010-10-08 23:50:27 +00003252 }
3253
John McCall6347b682012-05-07 06:16:58 +00003254 // Okay, end the suppression area. We'll decide whether to emit the
3255 // diagnostics in a second.
3256 if (shouldDelayDiagsInTag)
3257 diagsFromTag.done();
Richard Smith7d137e32012-03-23 03:33:32 +00003258
Douglas Gregor0bf31402010-10-08 23:50:27 +00003259 TypeResult BaseType;
3260
Douglas Gregord1f69f62010-12-01 17:42:47 +00003261 // Parse the fixed underlying type.
Richard Smith200f47c2012-07-02 19:14:01 +00003262 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003263 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003264 bool PossibleBitfield = false;
Richard Smith200f47c2012-07-02 19:14:01 +00003265 if (CanBeBitfield) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003266 // If we're in class scope, this can either be an enum declaration with
3267 // an underlying type, or a declaration of a bitfield member. We try to
3268 // use a simple disambiguation scheme first to catch the common cases
Chad Rosierc1183952012-06-26 22:30:43 +00003269 // (integer literal, sizeof); if it's still ambiguous, we then consider
3270 // anything that's a simple-type-specifier followed by '(' as an
3271 // expression. This suffices because function types are not valid
Douglas Gregord1f69f62010-12-01 17:42:47 +00003272 // underlying types anyway.
Richard Smith4f605af2012-08-18 00:55:03 +00003273 EnterExpressionEvaluationContext Unevaluated(Actions,
3274 Sema::ConstantEvaluated);
Douglas Gregord1f69f62010-12-01 17:42:47 +00003275 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosierc1183952012-06-26 22:30:43 +00003276 // If the next token starts an expression, we know we're parsing a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003277 // bit-field. This is the common case.
3278 if (TPR == TPResult::True())
3279 PossibleBitfield = true;
3280 // If the next token starts a type-specifier-seq, it may be either a
3281 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosierc1183952012-06-26 22:30:43 +00003282 // lookahead one more token to see if it's obvious that we have a
Douglas Gregord1f69f62010-12-01 17:42:47 +00003283 // fixed underlying type.
Chad Rosierc1183952012-06-26 22:30:43 +00003284 else if (TPR == TPResult::False() &&
Douglas Gregord1f69f62010-12-01 17:42:47 +00003285 GetLookAheadToken(2).getKind() == tok::semi) {
3286 // Consume the ':'.
3287 ConsumeToken();
3288 } else {
3289 // We have the start of a type-specifier-seq, so we have to perform
3290 // tentative parsing to determine whether we have an expression or a
3291 // type.
3292 TentativeParsingAction TPA(*this);
3293
3294 // Consume the ':'.
3295 ConsumeToken();
Richard Smith1e3b0f02012-02-23 01:36:12 +00003296
3297 // If we see a type specifier followed by an open-brace, we have an
3298 // ambiguity between an underlying type and a C++11 braced
3299 // function-style cast. Resolve this by always treating it as an
3300 // underlying type.
3301 // FIXME: The standard is not entirely clear on how to disambiguate in
3302 // this case.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003303 if ((getLangOpts().CPlusPlus &&
Richard Smith1e3b0f02012-02-23 01:36:12 +00003304 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikiebbafb8a2012-03-11 07:00:24 +00003305 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregord1f69f62010-12-01 17:42:47 +00003306 // We'll parse this as a bitfield later.
3307 PossibleBitfield = true;
3308 TPA.Revert();
3309 } else {
3310 // We have a type-specifier-seq.
3311 TPA.Commit();
3312 }
3313 }
3314 } else {
3315 // Consume the ':'.
3316 ConsumeToken();
3317 }
3318
3319 if (!PossibleBitfield) {
3320 SourceRange Range;
3321 BaseType = ParseTypeName(&Range);
Chad Rosierc1183952012-06-26 22:30:43 +00003322
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003323 if (getLangOpts().CPlusPlus11) {
Richard Smith5d164bc2011-10-15 05:09:34 +00003324 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedman0d0355ab2012-11-02 01:34:28 +00003325 } else if (!getLangOpts().ObjC2) {
3326 if (getLangOpts().CPlusPlus)
3327 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3328 else
3329 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3330 }
Douglas Gregord1f69f62010-12-01 17:42:47 +00003331 }
Douglas Gregor0bf31402010-10-08 23:50:27 +00003332 }
3333
Richard Smith0f8ee222012-01-10 01:33:14 +00003334 // There are four options here. If we have 'friend enum foo;' then this is a
3335 // friend declaration, and cannot have an accompanying definition. If we have
3336 // 'enum foo;', then this is a forward declaration. If we have
3337 // 'enum foo {...' then this is a definition. Otherwise we have something
3338 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidisf01fa822008-09-11 00:21:41 +00003339 //
3340 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3341 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3342 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3343 //
John McCallfaf5fb42010-08-26 23:41:50 +00003344 Sema::TagUseKind TUK;
John McCall6347b682012-05-07 06:16:58 +00003345 if (!AllowDeclaration) {
Richard Smithbfdb1082012-03-12 08:56:40 +00003346 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003347 } else if (Tok.is(tok::l_brace)) {
3348 if (DS.isFriendSpecified()) {
3349 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3350 << SourceRange(DS.getFriendSpecLoc());
3351 ConsumeBrace();
3352 SkipUntil(tok::r_brace);
3353 TUK = Sema::TUK_Friend;
3354 } else {
3355 TUK = Sema::TUK_Definition;
3356 }
Richard Smith369b9f92012-06-25 21:37:02 +00003357 } else if (DSC != DSC_type_specifier &&
3358 (Tok.is(tok::semi) ||
Richard Smith200f47c2012-07-02 19:14:01 +00003359 (Tok.isAtStartOfLine() &&
3360 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smith369b9f92012-06-25 21:37:02 +00003361 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3362 if (Tok.isNot(tok::semi)) {
3363 // A semicolon was missing after this declaration. Diagnose and recover.
3364 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3365 "enum");
3366 PP.EnterToken(Tok);
3367 Tok.setKind(tok::semi);
3368 }
John McCall6347b682012-05-07 06:16:58 +00003369 } else {
John McCallfaf5fb42010-08-26 23:41:50 +00003370 TUK = Sema::TUK_Reference;
John McCall6347b682012-05-07 06:16:58 +00003371 }
3372
3373 // If this is an elaborated type specifier, and we delayed
3374 // diagnostics before, just merge them into the current pool.
3375 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3376 diagsFromTag.redelay();
3377 }
Richard Smith7d137e32012-03-23 03:33:32 +00003378
3379 MultiTemplateParamsArg TParams;
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003380 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallfaf5fb42010-08-26 23:41:50 +00003381 TUK != Sema::TUK_Reference) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003382 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith7d137e32012-03-23 03:33:32 +00003383 // Skip the rest of this declarator, up until the comma or semicolon.
3384 Diag(Tok, diag::err_enum_template);
3385 SkipUntil(tok::comma, true);
3386 return;
3387 }
3388
3389 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3390 // Enumerations can't be explicitly instantiated.
3391 DS.SetTypeSpecError();
3392 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3393 return;
3394 }
3395
3396 assert(TemplateInfo.TemplateParams && "no template parameters");
3397 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3398 TemplateInfo.TemplateParams->size());
Douglas Gregorcbbf3e32010-05-03 17:48:54 +00003399 }
Chad Rosierc1183952012-06-26 22:30:43 +00003400
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003401 if (TUK == Sema::TUK_Reference)
3402 ProhibitAttributes(attrs);
Richard Smith7d137e32012-03-23 03:33:32 +00003403
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003404 if (!Name && TUK != Sema::TUK_Definition) {
3405 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith7d137e32012-03-23 03:33:32 +00003406
Douglas Gregor6cd5ae42011-02-22 02:55:24 +00003407 // Skip the rest of this declarator, up until the comma or semicolon.
3408 SkipUntil(tok::comma, true);
3409 return;
3410 }
Richard Smith7d137e32012-03-23 03:33:32 +00003411
Douglas Gregord6ab8742009-05-28 23:31:59 +00003412 bool Owned = false;
John McCall7f41d982009-09-11 04:59:25 +00003413 bool IsDependent = false;
Douglas Gregorba41d012010-04-24 16:38:41 +00003414 const char *PrevSpec = 0;
3415 unsigned DiagID;
John McCall48871652010-08-21 09:40:31 +00003416 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall53fa7142010-12-24 02:08:15 +00003417 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith7d137e32012-03-23 03:33:32 +00003418 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smith0f8ee222012-01-10 01:33:14 +00003419 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnara0e05e242010-12-03 18:54:17 +00003420 IsScopedUsingClassTag, BaseType);
Douglas Gregor0bf31402010-10-08 23:50:27 +00003421
Douglas Gregorba41d012010-04-24 16:38:41 +00003422 if (IsDependent) {
Chad Rosierc1183952012-06-26 22:30:43 +00003423 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregorba41d012010-04-24 16:38:41 +00003424 // dependent tag.
3425 if (!Name) {
3426 DS.SetTypeSpecError();
3427 Diag(Tok, diag::err_expected_type_name_after_typename);
3428 return;
3429 }
Chad Rosierc1183952012-06-26 22:30:43 +00003430
Douglas Gregor0be31a22010-07-02 17:43:08 +00003431 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosierc1183952012-06-26 22:30:43 +00003432 TUK, SS, Name, StartLoc,
Douglas Gregorba41d012010-04-24 16:38:41 +00003433 NameLoc);
3434 if (Type.isInvalid()) {
3435 DS.SetTypeSpecError();
3436 return;
3437 }
Chad Rosierc1183952012-06-26 22:30:43 +00003438
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003439 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3440 NameLoc.isValid() ? NameLoc : StartLoc,
3441 PrevSpec, DiagID, Type.get()))
Douglas Gregorba41d012010-04-24 16:38:41 +00003442 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosierc1183952012-06-26 22:30:43 +00003443
Douglas Gregorba41d012010-04-24 16:38:41 +00003444 return;
3445 }
Mike Stump11289f42009-09-09 15:08:12 +00003446
John McCall48871652010-08-21 09:40:31 +00003447 if (!TagDecl) {
Chad Rosierc1183952012-06-26 22:30:43 +00003448 // The action failed to produce an enumeration tag. If this is a
Douglas Gregorba41d012010-04-24 16:38:41 +00003449 // definition, consume the entire definition.
Richard Smithbfdb1082012-03-12 08:56:40 +00003450 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregorba41d012010-04-24 16:38:41 +00003451 ConsumeBrace();
3452 SkipUntil(tok::r_brace);
3453 }
Chad Rosierc1183952012-06-26 22:30:43 +00003454
Douglas Gregorba41d012010-04-24 16:38:41 +00003455 DS.SetTypeSpecError();
3456 return;
3457 }
Richard Smith0f8ee222012-01-10 01:33:14 +00003458
Richard Smith369b9f92012-06-25 21:37:02 +00003459 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall6347b682012-05-07 06:16:58 +00003460 ParseEnumBody(StartLoc, TagDecl);
Mike Stump11289f42009-09-09 15:08:12 +00003461
Abramo Bagnara9875a3c2011-03-16 20:16:18 +00003462 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3463 NameLoc.isValid() ? NameLoc : StartLoc,
3464 PrevSpec, DiagID, TagDecl, Owned))
John McCall49bfce42009-08-03 20:12:06 +00003465 Diag(StartLoc, DiagID) << PrevSpec;
Chris Lattner3b561a32006-08-13 00:12:11 +00003466}
3467
Chris Lattnerc1915e22007-01-25 07:29:02 +00003468/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3469/// enumerator-list:
3470/// enumerator
3471/// enumerator-list ',' enumerator
3472/// enumerator:
3473/// enumeration-constant
3474/// enumeration-constant '=' constant-expression
3475/// enumeration-constant:
3476/// identifier
3477///
John McCall48871652010-08-21 09:40:31 +00003478void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor07665a62009-01-05 19:45:36 +00003479 // Enter the scope of the enum body and start the definition.
3480 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003481 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor07665a62009-01-05 19:45:36 +00003482
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003483 BalancedDelimiterTracker T(*this, tok::l_brace);
3484 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00003485
Chris Lattner37256fb2007-08-27 17:24:30 +00003486 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikiebbafb8a2012-03-11 07:00:24 +00003487 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian6e814922010-05-28 22:23:22 +00003488 Diag(Tok, diag::error_empty_enum);
Mike Stump11289f42009-09-09 15:08:12 +00003489
Chris Lattner0e62c1c2011-07-23 10:55:15 +00003490 SmallVector<Decl *, 32> EnumConstantDecls;
Chris Lattnerc1915e22007-01-25 07:29:02 +00003491
John McCall48871652010-08-21 09:40:31 +00003492 Decl *LastEnumConstDecl = 0;
Mike Stump11289f42009-09-09 15:08:12 +00003493
Chris Lattnerc1915e22007-01-25 07:29:02 +00003494 // Parse the enumerator-list.
Chris Lattner76c72282007-10-09 17:33:22 +00003495 while (Tok.is(tok::identifier)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003496 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3497 SourceLocation IdentLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003498
John McCall811a0f52010-10-22 23:36:17 +00003499 // If attributes exist after the enumerator, parse them.
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003500 ParsedAttributesWithRange attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003501 MaybeParseGNUAttributes(attrs);
Richard Smith89645bc2013-01-02 12:01:23 +00003502 MaybeParseCXX11Attributes(attrs);
Alexis Hunt6aa9bee2012-06-23 05:07:58 +00003503 ProhibitAttributes(attrs);
John McCall811a0f52010-10-22 23:36:17 +00003504
Chris Lattnerc1915e22007-01-25 07:29:02 +00003505 SourceLocation EqualLoc;
John McCalldadc5752010-08-24 06:29:42 +00003506 ExprResult AssignedVal;
John McCall2ec85372012-05-07 06:16:41 +00003507 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosierc1183952012-06-26 22:30:43 +00003508
Chris Lattner76c72282007-10-09 17:33:22 +00003509 if (Tok.is(tok::equal)) {
Chris Lattnerc1915e22007-01-25 07:29:02 +00003510 EqualLoc = ConsumeToken();
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00003511 AssignedVal = ParseConstantExpression();
3512 if (AssignedVal.isInvalid())
Chris Lattnerda6c2ce2007-04-27 19:13:15 +00003513 SkipUntil(tok::comma, tok::r_brace, true, true);
Chris Lattnerc1915e22007-01-25 07:29:02 +00003514 }
Mike Stump11289f42009-09-09 15:08:12 +00003515
Chris Lattnerc1915e22007-01-25 07:29:02 +00003516 // Install the enumerator constant into EnumDecl.
John McCall48871652010-08-21 09:40:31 +00003517 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3518 LastEnumConstDecl,
3519 IdentLoc, Ident,
John McCall53fa7142010-12-24 02:08:15 +00003520 attrs.getList(), EqualLoc,
John McCall48871652010-08-21 09:40:31 +00003521 AssignedVal.release());
Fariborz Jahanian329b3512011-12-09 01:15:54 +00003522 PD.complete(EnumConstDecl);
Chad Rosierc1183952012-06-26 22:30:43 +00003523
Chris Lattner4ef40012007-06-11 01:28:17 +00003524 EnumConstantDecls.push_back(EnumConstDecl);
3525 LastEnumConstDecl = EnumConstDecl;
Mike Stump11289f42009-09-09 15:08:12 +00003526
Douglas Gregorce66d022010-09-07 14:51:08 +00003527 if (Tok.is(tok::identifier)) {
3528 // We're missing a comma between enumerators.
3529 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosierc1183952012-06-26 22:30:43 +00003530 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregorce66d022010-09-07 14:51:08 +00003531 << FixItHint::CreateInsertion(Loc, ", ");
3532 continue;
3533 }
Chad Rosierc1183952012-06-26 22:30:43 +00003534
Chris Lattner76c72282007-10-09 17:33:22 +00003535 if (Tok.isNot(tok::comma))
Chris Lattnerc1915e22007-01-25 07:29:02 +00003536 break;
3537 SourceLocation CommaLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00003538
Richard Smith5d164bc2011-10-15 05:09:34 +00003539 if (Tok.isNot(tok::identifier)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003540 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smith87f5dc52012-07-23 05:45:25 +00003541 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3542 diag::ext_enumerator_list_comma_cxx :
3543 diag::ext_enumerator_list_comma_c)
Richard Smith5d164bc2011-10-15 05:09:34 +00003544 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith2bf7fdb2013-01-02 11:42:31 +00003545 else if (getLangOpts().CPlusPlus11)
Richard Smith5d164bc2011-10-15 05:09:34 +00003546 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3547 << FixItHint::CreateRemoval(CommaLoc);
3548 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003549 }
Mike Stump11289f42009-09-09 15:08:12 +00003550
Chris Lattnerc1915e22007-01-25 07:29:02 +00003551 // Eat the }.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003552 T.consumeClose();
Chris Lattnerc1915e22007-01-25 07:29:02 +00003553
Chris Lattnerc1915e22007-01-25 07:29:02 +00003554 // If attributes exist after the identifier list, parse them.
John McCall084e83d2011-03-24 11:26:52 +00003555 ParsedAttributes attrs(AttrFactory);
John McCall53fa7142010-12-24 02:08:15 +00003556 MaybeParseGNUAttributes(attrs);
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003557
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003558 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3559 EnumDecl, EnumConstantDecls.data(),
3560 EnumConstantDecls.size(), getCurScope(),
3561 attrs.getList());
Mike Stump11289f42009-09-09 15:08:12 +00003562
Douglas Gregor82ac25e2009-01-08 20:45:30 +00003563 EnumScope.Exit();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00003564 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3565 T.getCloseLocation());
Richard Smith369b9f92012-06-25 21:37:02 +00003566
3567 // The next token must be valid after an enum definition. If not, a ';'
3568 // was probably forgotten.
Richard Smith200f47c2012-07-02 19:14:01 +00003569 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3570 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smith369b9f92012-06-25 21:37:02 +00003571 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3572 // Push this token back into the preprocessor and change our current token
3573 // to ';' so that the rest of the code recovers as though there were an
3574 // ';' after the definition.
3575 PP.EnterToken(Tok);
3576 Tok.setKind(tok::semi);
3577 }
Chris Lattnerc1915e22007-01-25 07:29:02 +00003578}
Chris Lattner3b561a32006-08-13 00:12:11 +00003579
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003580/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003581/// start of a type-qualifier-list.
3582bool Parser::isTypeQualifier() const {
3583 switch (Tok.getKind()) {
3584 default: return false;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003585
3586 // type-qualifier only in OpenCL
3587 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003588 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003589
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003590 // type-qualifier
3591 case tok::kw_const:
3592 case tok::kw_volatile:
3593 case tok::kw_restrict:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003594 case tok::kw___private:
3595 case tok::kw___local:
3596 case tok::kw___global:
3597 case tok::kw___constant:
3598 case tok::kw___read_only:
3599 case tok::kw___read_write:
3600 case tok::kw___write_only:
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003601 return true;
3602 }
3603}
3604
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003605/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3606/// is definitely a type-specifier. Return false if it isn't part of a type
3607/// specifier or if we're not sure.
3608bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3609 switch (Tok.getKind()) {
3610 default: return false;
3611 // type-specifiers
3612 case tok::kw_short:
3613 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003614 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003615 case tok::kw___int128:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003616 case tok::kw_signed:
3617 case tok::kw_unsigned:
3618 case tok::kw__Complex:
3619 case tok::kw__Imaginary:
3620 case tok::kw_void:
3621 case tok::kw_char:
3622 case tok::kw_wchar_t:
3623 case tok::kw_char16_t:
3624 case tok::kw_char32_t:
3625 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003626 case tok::kw_half:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003627 case tok::kw_float:
3628 case tok::kw_double:
3629 case tok::kw_bool:
3630 case tok::kw__Bool:
3631 case tok::kw__Decimal32:
3632 case tok::kw__Decimal64:
3633 case tok::kw__Decimal128:
3634 case tok::kw___vector:
Chad Rosierc1183952012-06-26 22:30:43 +00003635
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003636 // OpenCL specific types:
3637 case tok::kw_image1d_t:
3638 case tok::kw_image1d_array_t:
3639 case tok::kw_image1d_buffer_t:
3640 case tok::kw_image2d_t:
3641 case tok::kw_image2d_array_t:
3642 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003643 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003644 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003645
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003646 // struct-or-union-specifier (C99) or class-specifier (C++)
3647 case tok::kw_class:
3648 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003649 case tok::kw___interface:
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003650 case tok::kw_union:
3651 // enum-specifier
3652 case tok::kw_enum:
Chad Rosierc1183952012-06-26 22:30:43 +00003653
Chris Lattnerfd48afe2010-02-28 18:18:36 +00003654 // typedef-name
3655 case tok::annot_typename:
3656 return true;
3657 }
3658}
3659
Steve Naroff69e8f9e2008-02-11 23:15:56 +00003660/// isTypeSpecifierQualifier - Return true if the current token could be the
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003661/// start of a specifier-qualifier-list.
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003662bool Parser::isTypeSpecifierQualifier() {
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003663 switch (Tok.getKind()) {
3664 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003665
Chris Lattner020bab92009-01-04 23:41:41 +00003666 case tok::identifier: // foo::bar
John Thompson22334602010-02-05 00:12:22 +00003667 if (TryAltiVecVectorToken())
3668 return true;
3669 // Fall through.
Douglas Gregor333489b2009-03-27 23:10:48 +00003670 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003671 // Annotate typenames and C++ scope specifiers. If we get one, just
3672 // recurse to handle whatever we get.
3673 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003674 return true;
3675 if (Tok.is(tok::identifier))
3676 return false;
3677 return isTypeSpecifierQualifier();
Douglas Gregor333489b2009-03-27 23:10:48 +00003678
Chris Lattner020bab92009-01-04 23:41:41 +00003679 case tok::coloncolon: // ::foo::bar
3680 if (NextToken().is(tok::kw_new) || // ::new
3681 NextToken().is(tok::kw_delete)) // ::delete
3682 return false;
3683
Chris Lattner020bab92009-01-04 23:41:41 +00003684 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003685 return true;
3686 return isTypeSpecifierQualifier();
Mike Stump11289f42009-09-09 15:08:12 +00003687
Chris Lattnere37e2332006-08-15 04:50:22 +00003688 // GNU attributes support.
3689 case tok::kw___attribute:
Steve Naroffad373bd2007-07-31 12:34:36 +00003690 // GNU typeof support.
3691 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003692
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003693 // type-specifiers
3694 case tok::kw_short:
3695 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003696 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003697 case tok::kw___int128:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003698 case tok::kw_signed:
3699 case tok::kw_unsigned:
3700 case tok::kw__Complex:
3701 case tok::kw__Imaginary:
3702 case tok::kw_void:
3703 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003704 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003705 case tok::kw_char16_t:
3706 case tok::kw_char32_t:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003707 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003708 case tok::kw_half:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003709 case tok::kw_float:
3710 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003711 case tok::kw_bool:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003712 case tok::kw__Bool:
3713 case tok::kw__Decimal32:
3714 case tok::kw__Decimal64:
3715 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003716 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003717
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003718 // OpenCL specific types:
3719 case tok::kw_image1d_t:
3720 case tok::kw_image1d_array_t:
3721 case tok::kw_image1d_buffer_t:
3722 case tok::kw_image2d_t:
3723 case tok::kw_image2d_array_t:
3724 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003725 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003726 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003727
Chris Lattner861a2262008-04-13 18:59:07 +00003728 // struct-or-union-specifier (C99) or class-specifier (C++)
3729 case tok::kw_class:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003730 case tok::kw_struct:
Joao Matosdc86f942012-08-31 18:45:21 +00003731 case tok::kw___interface:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003732 case tok::kw_union:
3733 // enum-specifier
3734 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003735
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003736 // type-qualifier
3737 case tok::kw_const:
3738 case tok::kw_volatile:
3739 case tok::kw_restrict:
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003740
John McCallea0a39e2012-11-14 00:49:39 +00003741 // Debugger support.
3742 case tok::kw___unknown_anytype:
3743
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00003744 // typedef-name
Chris Lattnera8a3f732009-01-06 05:06:21 +00003745 case tok::annot_typename:
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003746 return true;
Mike Stump11289f42009-09-09 15:08:12 +00003747
Chris Lattner409bf7d2008-10-20 00:25:30 +00003748 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3749 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003750 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003751
Steve Naroff44ac7772008-12-25 14:16:32 +00003752 case tok::kw___cdecl:
3753 case tok::kw___stdcall:
3754 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003755 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003756 case tok::kw___w64:
3757 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003758 case tok::kw___ptr32:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003759 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003760 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003761
3762 case tok::kw___private:
3763 case tok::kw___local:
3764 case tok::kw___global:
3765 case tok::kw___constant:
3766 case tok::kw___read_only:
3767 case tok::kw___read_write:
3768 case tok::kw___write_only:
3769
Eli Friedman53339e02009-06-08 23:27:34 +00003770 return true;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003771
3772 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003773 return getLangOpts().OpenCL;
Eli Friedman0dfb8892011-10-06 23:00:33 +00003774
Benjamin Kramere56f3932011-12-23 17:00:35 +00003775 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003776 case tok::kw__Atomic:
3777 return true;
Chris Lattnerf5fbd792006-08-10 23:56:11 +00003778 }
3779}
3780
Chris Lattneracd58a32006-08-06 17:24:14 +00003781/// isDeclarationSpecifier() - Return true if the current token is part of a
3782/// declaration specifier.
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003783///
3784/// \param DisambiguatingWithExpression True to indicate that the purpose of
3785/// this check is to disambiguate between an expression and a declaration.
3786bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Chris Lattneracd58a32006-08-06 17:24:14 +00003787 switch (Tok.getKind()) {
3788 default: return false;
Mike Stump11289f42009-09-09 15:08:12 +00003789
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003790 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003791 return getLangOpts().OpenCL;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003792
Chris Lattner020bab92009-01-04 23:41:41 +00003793 case tok::identifier: // foo::bar
Steve Naroff9527bbf2009-03-09 21:12:44 +00003794 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikiebbafb8a2012-03-11 07:00:24 +00003795 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff9527bbf2009-03-09 21:12:44 +00003796 return false;
John Thompson22334602010-02-05 00:12:22 +00003797 if (TryAltiVecVectorToken())
3798 return true;
3799 // Fall through.
David Blaikie15a430a2011-12-04 05:04:18 +00003800 case tok::kw_decltype: // decltype(T())::type
Douglas Gregor333489b2009-03-27 23:10:48 +00003801 case tok::kw_typename: // typename T::type
Chris Lattner020bab92009-01-04 23:41:41 +00003802 // Annotate typenames and C++ scope specifiers. If we get one, just
3803 // recurse to handle whatever we get.
3804 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003805 return true;
3806 if (Tok.is(tok::identifier))
3807 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003808
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003809 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosierc1183952012-06-26 22:30:43 +00003810 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003811 // expression is permitted, then this is probably a class message send
3812 // missing the initial '['. In this case, we won't consider this to be
3813 // the start of a declaration.
Chad Rosierc1183952012-06-26 22:30:43 +00003814 if (DisambiguatingWithExpression &&
Douglas Gregorabf4a3e2010-09-16 01:51:54 +00003815 isStartOfObjCClassMessageMissingOpenBracket())
3816 return false;
Chad Rosierc1183952012-06-26 22:30:43 +00003817
John McCall1f476a12010-02-26 08:45:28 +00003818 return isDeclarationSpecifier();
3819
Chris Lattner020bab92009-01-04 23:41:41 +00003820 case tok::coloncolon: // ::foo::bar
3821 if (NextToken().is(tok::kw_new) || // ::new
3822 NextToken().is(tok::kw_delete)) // ::delete
3823 return false;
Mike Stump11289f42009-09-09 15:08:12 +00003824
Chris Lattner020bab92009-01-04 23:41:41 +00003825 // Annotate typenames and C++ scope specifiers. If we get one, just
3826 // recurse to handle whatever we get.
3827 if (TryAnnotateTypeOrScopeToken())
John McCall1f476a12010-02-26 08:45:28 +00003828 return true;
3829 return isDeclarationSpecifier();
Mike Stump11289f42009-09-09 15:08:12 +00003830
Chris Lattneracd58a32006-08-06 17:24:14 +00003831 // storage-class-specifier
3832 case tok::kw_typedef:
3833 case tok::kw_extern:
Steve Naroff2050b0d2007-12-18 00:16:02 +00003834 case tok::kw___private_extern__:
Chris Lattneracd58a32006-08-06 17:24:14 +00003835 case tok::kw_static:
3836 case tok::kw_auto:
3837 case tok::kw_register:
3838 case tok::kw___thread:
Mike Stump11289f42009-09-09 15:08:12 +00003839
Douglas Gregor26701a42011-09-09 02:06:17 +00003840 // Modules
3841 case tok::kw___module_private__:
Chad Rosierc1183952012-06-26 22:30:43 +00003842
John McCallea0a39e2012-11-14 00:49:39 +00003843 // Debugger support
3844 case tok::kw___unknown_anytype:
3845
Chris Lattneracd58a32006-08-06 17:24:14 +00003846 // type-specifiers
3847 case tok::kw_short:
3848 case tok::kw_long:
Francois Pichet84133e42011-04-28 01:59:37 +00003849 case tok::kw___int64:
Richard Smithf016bbc2012-04-04 06:24:32 +00003850 case tok::kw___int128:
Chris Lattneracd58a32006-08-06 17:24:14 +00003851 case tok::kw_signed:
3852 case tok::kw_unsigned:
3853 case tok::kw__Complex:
3854 case tok::kw__Imaginary:
3855 case tok::kw_void:
3856 case tok::kw_char:
Argyrios Kyrtzidis40e9e482008-08-09 16:51:54 +00003857 case tok::kw_wchar_t:
Alisdair Mereditha9ad47d2009-07-14 06:30:34 +00003858 case tok::kw_char16_t:
3859 case tok::kw_char32_t:
3860
Chris Lattneracd58a32006-08-06 17:24:14 +00003861 case tok::kw_int:
Anton Korobeynikovf0c267e2011-10-14 23:23:15 +00003862 case tok::kw_half:
Chris Lattneracd58a32006-08-06 17:24:14 +00003863 case tok::kw_float:
3864 case tok::kw_double:
Chris Lattnerbb31a422007-11-15 05:25:19 +00003865 case tok::kw_bool:
Chris Lattneracd58a32006-08-06 17:24:14 +00003866 case tok::kw__Bool:
3867 case tok::kw__Decimal32:
3868 case tok::kw__Decimal64:
3869 case tok::kw__Decimal128:
John Thompson22334602010-02-05 00:12:22 +00003870 case tok::kw___vector:
Mike Stump11289f42009-09-09 15:08:12 +00003871
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003872 // OpenCL specific types:
3873 case tok::kw_image1d_t:
3874 case tok::kw_image1d_array_t:
3875 case tok::kw_image1d_buffer_t:
3876 case tok::kw_image2d_t:
3877 case tok::kw_image2d_array_t:
3878 case tok::kw_image3d_t:
Guy Benyei61054192013-02-07 10:55:47 +00003879 case tok::kw_sampler_t:
Guy Benyei1b4fb3e2013-01-20 12:31:11 +00003880 case tok::kw_event_t:
Guy Benyeid8a08ea2012-12-18 14:38:23 +00003881
Chris Lattner861a2262008-04-13 18:59:07 +00003882 // struct-or-union-specifier (C99) or class-specifier (C++)
3883 case tok::kw_class:
Chris Lattneracd58a32006-08-06 17:24:14 +00003884 case tok::kw_struct:
3885 case tok::kw_union:
Joao Matosdc86f942012-08-31 18:45:21 +00003886 case tok::kw___interface:
Chris Lattneracd58a32006-08-06 17:24:14 +00003887 // enum-specifier
3888 case tok::kw_enum:
Mike Stump11289f42009-09-09 15:08:12 +00003889
Chris Lattneracd58a32006-08-06 17:24:14 +00003890 // type-qualifier
3891 case tok::kw_const:
3892 case tok::kw_volatile:
3893 case tok::kw_restrict:
Steve Naroffad373bd2007-07-31 12:34:36 +00003894
Chris Lattneracd58a32006-08-06 17:24:14 +00003895 // function-specifier
3896 case tok::kw_inline:
Douglas Gregor61956c42008-10-31 09:07:45 +00003897 case tok::kw_virtual:
3898 case tok::kw_explicit:
Richard Smith0015f092013-01-17 22:16:11 +00003899 case tok::kw__Noreturn:
Chris Lattner7b20dc72007-08-09 16:40:21 +00003900
Richard Smith1dba27c2013-01-29 09:02:09 +00003901 // alignment-specifier
3902 case tok::kw__Alignas:
3903
Richard Smithd16fe122012-10-25 00:00:53 +00003904 // friend keyword.
3905 case tok::kw_friend:
3906
Peter Collingbourne3d9cbdc2011-04-15 00:35:57 +00003907 // static_assert-declaration
3908 case tok::kw__Static_assert:
3909
Chris Lattner599e47e2007-08-09 17:01:07 +00003910 // GNU typeof support.
3911 case tok::kw_typeof:
Mike Stump11289f42009-09-09 15:08:12 +00003912
Chris Lattner599e47e2007-08-09 17:01:07 +00003913 // GNU attributes.
Chris Lattner7b20dc72007-08-09 16:40:21 +00003914 case tok::kw___attribute:
Mike Stump11289f42009-09-09 15:08:12 +00003915
Richard Smithd16fe122012-10-25 00:00:53 +00003916 // C++11 decltype and constexpr.
David Blaikie15a430a2011-12-04 05:04:18 +00003917 case tok::annot_decltype:
Richard Smithd16fe122012-10-25 00:00:53 +00003918 case tok::kw_constexpr:
Francois Pichete878cb62011-06-19 08:02:06 +00003919
Benjamin Kramere56f3932011-12-23 17:00:35 +00003920 // C11 _Atomic()
Eli Friedman0dfb8892011-10-06 23:00:33 +00003921 case tok::kw__Atomic:
3922 return true;
3923
Chris Lattner8b2ec162008-07-26 03:38:44 +00003924 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3925 case tok::less:
David Blaikiebbafb8a2012-03-11 07:00:24 +00003926 return getLangOpts().ObjC1;
Mike Stump11289f42009-09-09 15:08:12 +00003927
Douglas Gregor19b7acf2011-04-27 05:41:15 +00003928 // typedef-name
3929 case tok::annot_typename:
3930 return !DisambiguatingWithExpression ||
3931 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosierc1183952012-06-26 22:30:43 +00003932
Steve Narofff192fab2009-01-06 19:34:12 +00003933 case tok::kw___declspec:
Steve Naroff44ac7772008-12-25 14:16:32 +00003934 case tok::kw___cdecl:
3935 case tok::kw___stdcall:
3936 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00003937 case tok::kw___thiscall:
Eli Friedman53339e02009-06-08 23:27:34 +00003938 case tok::kw___w64:
3939 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00003940 case tok::kw___ptr32:
Eli Friedman53339e02009-06-08 23:27:34 +00003941 case tok::kw___forceinline:
Dawn Perchik335e16b2010-09-03 01:29:35 +00003942 case tok::kw___pascal:
Francois Pichet17ed0202011-08-18 09:59:55 +00003943 case tok::kw___unaligned:
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00003944
3945 case tok::kw___private:
3946 case tok::kw___local:
3947 case tok::kw___global:
3948 case tok::kw___constant:
3949 case tok::kw___read_only:
3950 case tok::kw___read_write:
3951 case tok::kw___write_only:
3952
Eli Friedman53339e02009-06-08 23:27:34 +00003953 return true;
Chris Lattneracd58a32006-08-06 17:24:14 +00003954 }
3955}
3956
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003957bool Parser::isConstructorDeclarator() {
3958 TentativeParsingAction TPA(*this);
3959
3960 // Parse the C++ scope specifier.
3961 CXXScopeSpec SS;
Chad Rosierc1183952012-06-26 22:30:43 +00003962 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00003963 /*EnteringContext=*/true)) {
John McCall1f476a12010-02-26 08:45:28 +00003964 TPA.Revert();
3965 return false;
3966 }
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003967
3968 // Parse the constructor name.
3969 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3970 // We already know that we have a constructor name; just consume
3971 // the token.
3972 ConsumeToken();
3973 } else {
3974 TPA.Revert();
3975 return false;
3976 }
3977
Richard Smith43f340f2012-03-27 23:05:05 +00003978 // Current class name must be followed by a left parenthesis.
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003979 if (Tok.isNot(tok::l_paren)) {
3980 TPA.Revert();
3981 return false;
3982 }
3983 ConsumeParen();
3984
Richard Smith43f340f2012-03-27 23:05:05 +00003985 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3986 // that we have a constructor.
3987 if (Tok.is(tok::r_paren) ||
3988 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003989 TPA.Revert();
3990 return true;
3991 }
3992
3993 // If we need to, enter the specified scope.
3994 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor0be31a22010-07-02 17:43:08 +00003995 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00003996 DeclScopeObj.EnterDeclaratorScope();
3997
Francois Pichet79f3a872011-01-31 04:54:32 +00003998 // Optionally skip Microsoft attributes.
John McCall084e83d2011-03-24 11:26:52 +00003999 ParsedAttributes Attrs(AttrFactory);
Francois Pichet79f3a872011-01-31 04:54:32 +00004000 MaybeParseMicrosoftAttributes(Attrs);
4001
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004002 // Check whether the next token(s) are part of a declaration
4003 // specifier, in which case we have the start of a parameter and,
4004 // therefore, we know that this is a constructor.
Richard Smithefd009d2012-03-27 00:56:56 +00004005 bool IsConstructor = false;
4006 if (isDeclarationSpecifier())
4007 IsConstructor = true;
4008 else if (Tok.is(tok::identifier) ||
4009 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4010 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4011 // This might be a parenthesized member name, but is more likely to
4012 // be a constructor declaration with an invalid argument type. Keep
4013 // looking.
4014 if (Tok.is(tok::annot_cxxscope))
4015 ConsumeToken();
4016 ConsumeToken();
4017
4018 // If this is not a constructor, we must be parsing a declarator,
Richard Smith1453e312012-03-27 01:42:32 +00004019 // which must have one of the following syntactic forms (see the
4020 // grammar extract at the start of ParseDirectDeclarator):
Richard Smithefd009d2012-03-27 00:56:56 +00004021 switch (Tok.getKind()) {
4022 case tok::l_paren:
4023 // C(X ( int));
4024 case tok::l_square:
4025 // C(X [ 5]);
4026 // C(X [ [attribute]]);
4027 case tok::coloncolon:
4028 // C(X :: Y);
4029 // C(X :: *p);
4030 case tok::r_paren:
4031 // C(X )
4032 // Assume this isn't a constructor, rather than assuming it's a
4033 // constructor with an unnamed parameter of an ill-formed type.
4034 break;
4035
4036 default:
4037 IsConstructor = true;
4038 break;
4039 }
4040 }
4041
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004042 TPA.Revert();
4043 return IsConstructor;
4044}
Chris Lattnerb9093cd2006-08-04 04:39:53 +00004045
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004046/// ParseTypeQualifierListOpt
Dawn Perchik335e16b2010-09-03 01:29:35 +00004047/// type-qualifier-list: [C99 6.7.5]
4048/// type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004049/// [vendor] attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004050/// [ only if VendorAttributesAllowed=true ]
4051/// type-qualifier-list type-qualifier
Chad Rosierc1183952012-06-26 22:30:43 +00004052/// [vendor] type-qualifier-list attributes
Dawn Perchik335e16b2010-09-03 01:29:35 +00004053/// [ only if VendorAttributesAllowed=true ]
4054/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith89645bc2013-01-02 12:01:23 +00004055/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik335e16b2010-09-03 01:29:35 +00004056/// Note: vendor can be GNU, MS, etc.
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004057///
Dawn Perchik335e16b2010-09-03 01:29:35 +00004058void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4059 bool VendorAttributesAllowed,
Richard Smith3dff2512012-04-10 03:25:07 +00004060 bool CXX11AttributesAllowed) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004061 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004062 isCXX11AttributeSpecifier()) {
John McCall084e83d2011-03-24 11:26:52 +00004063 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smith3dff2512012-04-10 03:25:07 +00004064 ParseCXX11Attributes(attrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004065 DS.takeAttributesFrom(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004066 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004067
4068 SourceLocation EndLoc;
4069
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004070 while (1) {
John McCall49bfce42009-08-03 20:12:06 +00004071 bool isInvalid = false;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004072 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00004073 unsigned DiagID = 0;
Chris Lattner60809f52006-11-28 05:18:46 +00004074 SourceLocation Loc = Tok.getLocation();
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004075
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004076 switch (Tok.getKind()) {
Douglas Gregor28c78432010-08-27 17:35:51 +00004077 case tok::code_completion:
4078 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis5cec2ae2011-09-04 03:32:15 +00004079 return cutOffParsing();
Chad Rosierc1183952012-06-26 22:30:43 +00004080
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004081 case tok::kw_const:
John McCall49bfce42009-08-03 20:12:06 +00004082 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004083 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004084 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004085 case tok::kw_volatile:
John McCall49bfce42009-08-03 20:12:06 +00004086 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004087 getLangOpts());
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004088 break;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004089 case tok::kw_restrict:
John McCall49bfce42009-08-03 20:12:06 +00004090 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smith87e79512012-10-17 23:31:46 +00004091 getLangOpts());
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004092 break;
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004093
4094 // OpenCL qualifiers:
Chad Rosierc1183952012-06-26 22:30:43 +00004095 case tok::kw_private:
David Blaikiebbafb8a2012-03-11 07:00:24 +00004096 if (!getLangOpts().OpenCL)
Peter Collingbourne599cb8e2011-03-18 22:38:29 +00004097 goto DoneWithTypeQuals;
4098 case tok::kw___private:
4099 case tok::kw___global:
4100 case tok::kw___local:
4101 case tok::kw___constant:
4102 case tok::kw___read_only:
4103 case tok::kw___write_only:
4104 case tok::kw___read_write:
4105 ParseOpenCLQualifiers(DS);
4106 break;
4107
Eli Friedman53339e02009-06-08 23:27:34 +00004108 case tok::kw___w64:
Steve Narofff9c29d42008-12-25 14:41:26 +00004109 case tok::kw___ptr64:
Francois Pichetf2fb4112011-08-25 00:36:46 +00004110 case tok::kw___ptr32:
Steve Naroff44ac7772008-12-25 14:16:32 +00004111 case tok::kw___cdecl:
4112 case tok::kw___stdcall:
4113 case tok::kw___fastcall:
Douglas Gregora941dca2010-05-18 16:57:00 +00004114 case tok::kw___thiscall:
Francois Pichet17ed0202011-08-18 09:59:55 +00004115 case tok::kw___unaligned:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004116 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004117 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman53339e02009-06-08 23:27:34 +00004118 continue;
4119 }
4120 goto DoneWithTypeQuals;
Dawn Perchik335e16b2010-09-03 01:29:35 +00004121 case tok::kw___pascal:
4122 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004123 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik335e16b2010-09-03 01:29:35 +00004124 continue;
4125 }
4126 goto DoneWithTypeQuals;
Chris Lattnere37e2332006-08-15 04:50:22 +00004127 case tok::kw___attribute:
Dawn Perchik335e16b2010-09-03 01:29:35 +00004128 if (VendorAttributesAllowed) {
John McCall53fa7142010-12-24 02:08:15 +00004129 ParseGNUAttributes(DS.getAttributes());
Chris Lattnercf0bab22008-12-18 07:02:59 +00004130 continue; // do *not* consume the next token!
4131 }
4132 // otherwise, FALL THROUGH!
4133 default:
Steve Naroff44ac7772008-12-25 14:16:32 +00004134 DoneWithTypeQuals:
Chris Lattnercf0bab22008-12-18 07:02:59 +00004135 // If this is not a type-qualifier token, we're done reading type
4136 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregore3e01a22009-04-01 22:41:11 +00004137 DS.Finish(Diags, PP);
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004138 if (EndLoc.isValid())
4139 DS.SetRangeEnd(EndLoc);
Chris Lattnercf0bab22008-12-18 07:02:59 +00004140 return;
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004141 }
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00004142
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004143 // If the specifier combination wasn't legal, issue a diagnostic.
4144 if (isInvalid) {
4145 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner6d29c102008-11-18 07:48:38 +00004146 Diag(Tok, DiagID) << PrevSpec;
Chris Lattnerd9c3c592006-08-05 06:26:47 +00004147 }
Abramo Bagnaraf2a79d92011-03-12 11:17:06 +00004148 EndLoc = ConsumeToken();
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004149 }
4150}
4151
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004152
4153/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4154///
4155void Parser::ParseDeclarator(Declarator &D) {
4156 /// This implements the 'declarator' production in the C grammar, then checks
4157 /// for well-formedness and issues diagnostics.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004158 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerd5d0a6c2006-08-07 00:58:14 +00004159}
4160
Richard Smith0efa75c2012-03-29 01:16:42 +00004161static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4162 if (Kind == tok::star || Kind == tok::caret)
4163 return true;
4164
4165 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4166 if (!Lang.CPlusPlus)
4167 return false;
4168
4169 return Kind == tok::amp || Kind == tok::ampamp;
4170}
4171
Sebastian Redlbd150f42008-11-21 19:14:01 +00004172/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4173/// is parsed by the function passed to it. Pass null, and the direct-declarator
4174/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004175/// ptr-operator production.
4176///
Richard Smith09f76ee2011-10-19 21:33:05 +00004177/// If the grammar of this construct is extended, matching changes must also be
Richard Smith1453e312012-03-27 01:42:32 +00004178/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4179/// isConstructorDeclarator.
Richard Smith09f76ee2011-10-19 21:33:05 +00004180///
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004181/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4182/// [C] pointer[opt] direct-declarator
4183/// [C++] direct-declarator
4184/// [C++] ptr-operator declarator
Chris Lattner6c7416c2006-08-07 00:19:33 +00004185///
4186/// pointer: [C99 6.7.5]
4187/// '*' type-qualifier-list[opt]
4188/// '*' type-qualifier-list[opt] pointer
4189///
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004190/// ptr-operator:
4191/// '*' cv-qualifier-seq[opt]
4192/// '&'
Sebastian Redled0f3b02009-03-15 22:02:01 +00004193/// [C++0x] '&&'
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004194/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redled0f3b02009-03-15 22:02:01 +00004195/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004196/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redlbd150f42008-11-21 19:14:01 +00004197void Parser::ParseDeclaratorInternal(Declarator &D,
4198 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor66a985d2009-08-26 14:27:30 +00004199 if (Diags.hasAllExtensionsSilenced())
4200 D.setExtension();
Chad Rosierc1183952012-06-26 22:30:43 +00004201
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004202 // C++ member pointers start with a '::' or a nested-name.
4203 // Member pointers get special handling, since there's no place for the
4204 // scope spec in the generic path below.
David Blaikiebbafb8a2012-03-11 07:00:24 +00004205 if (getLangOpts().CPlusPlus &&
Chris Lattner803802d2009-03-24 17:04:48 +00004206 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4207 Tok.is(tok::annot_cxxscope))) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004208 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4209 D.getContext() == Declarator::MemberContext;
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004210 CXXScopeSpec SS;
Douglas Gregordf593fb2011-11-07 17:33:42 +00004211 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004212
Jeffrey Yasskin4e150f82010-04-07 23:29:58 +00004213 if (SS.isNotEmpty()) {
Mike Stump11289f42009-09-09 15:08:12 +00004214 if (Tok.isNot(tok::star)) {
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004215 // The scope spec really belongs to the direct-declarator.
Richard Smith5f044ad2013-01-08 22:43:49 +00004216 if (D.mayHaveIdentifier())
4217 D.getCXXScopeSpec() = SS;
4218 else
4219 AnnotateScopeToken(SS, true);
4220
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004221 if (DirectDeclParser)
4222 (this->*DirectDeclParser)(D);
4223 return;
4224 }
4225
4226 SourceLocation Loc = ConsumeToken();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004227 D.SetRangeEnd(Loc);
John McCall084e83d2011-03-24 11:26:52 +00004228 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004229 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004230 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004231
4232 // Recurse to parse whatever is left.
4233 ParseDeclaratorInternal(D, DirectDeclParser);
4234
4235 // Sema will have to catch (syntactically invalid) pointers into global
4236 // scope. It has to catch pointers into namespace scope anyway.
4237 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004238 Loc),
4239 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004240 /* Don't replace range end. */SourceLocation());
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004241 return;
4242 }
4243 }
4244
4245 tok::TokenKind Kind = Tok.getKind();
Steve Naroffec33ed92008-08-27 16:04:49 +00004246 // Not a pointer, C++ reference, or block.
Richard Smith0efa75c2012-03-29 01:16:42 +00004247 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redlbd150f42008-11-21 19:14:01 +00004248 if (DirectDeclParser)
4249 (this->*DirectDeclParser)(D);
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004250 return;
4251 }
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004252
Sebastian Redled0f3b02009-03-15 22:02:01 +00004253 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4254 // '&&' -> rvalue reference
Sebastian Redl3b27be62009-03-23 00:00:23 +00004255 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004256 D.SetRangeEnd(Loc);
Bill Wendling3708c182007-05-27 10:15:43 +00004257
Chris Lattner9eac9312009-03-27 04:18:06 +00004258 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner788404f2008-02-21 01:32:26 +00004259 // Is a pointer.
John McCall084e83d2011-03-24 11:26:52 +00004260 DeclSpec DS(AttrFactory);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004261
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004262 // FIXME: GNU attributes are not allowed here in a new-type-id.
Bill Wendling3708c182007-05-27 10:15:43 +00004263 ParseTypeQualifierListOpt(DS);
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004264 D.ExtendWithDeclSpec(DS);
Sebastian Redl9ed6efd2009-01-24 21:16:55 +00004265
Bill Wendling3708c182007-05-27 10:15:43 +00004266 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004267 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroffec33ed92008-08-27 16:04:49 +00004268 if (Kind == tok::star)
4269 // Remember that we parsed a pointer type, and remember the type-quals.
4270 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthe71b378d2011-02-23 18:51:59 +00004271 DS.getConstSpecLoc(),
4272 DS.getVolatileSpecLoc(),
John McCall084e83d2011-03-24 11:26:52 +00004273 DS.getRestrictSpecLoc()),
4274 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004275 SourceLocation());
Steve Naroffec33ed92008-08-27 16:04:49 +00004276 else
4277 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump11289f42009-09-09 15:08:12 +00004278 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall084e83d2011-03-24 11:26:52 +00004279 Loc),
4280 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004281 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004282 } else {
4283 // Is a reference
John McCall084e83d2011-03-24 11:26:52 +00004284 DeclSpec DS(AttrFactory);
Bill Wendling93efb222007-06-02 23:28:54 +00004285
Sebastian Redl3b27be62009-03-23 00:00:23 +00004286 // Complain about rvalue references in C++03, but then go on and build
4287 // the declarator.
Richard Smith5d164bc2011-10-15 05:09:34 +00004288 if (Kind == tok::ampamp)
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004289 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004290 diag::warn_cxx98_compat_rvalue_reference :
4291 diag::ext_rvalue_reference);
Sebastian Redl3b27be62009-03-23 00:00:23 +00004292
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004293 // GNU-style and C++11 attributes are allowed here, as is restrict.
4294 ParseTypeQualifierListOpt(DS);
4295 D.ExtendWithDeclSpec(DS);
4296
Bill Wendling93efb222007-06-02 23:28:54 +00004297 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4298 // cv-qualifiers are introduced through the use of a typedef or of a
4299 // template type argument, in which case the cv-qualifiers are ignored.
Bill Wendling93efb222007-06-02 23:28:54 +00004300 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4301 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4302 Diag(DS.getConstSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004303 diag::err_invalid_reference_qualifier_application) << "const";
Bill Wendling93efb222007-06-02 23:28:54 +00004304 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4305 Diag(DS.getVolatileSpecLoc(),
Chris Lattner6d29c102008-11-18 07:48:38 +00004306 diag::err_invalid_reference_qualifier_application) << "volatile";
Bill Wendling93efb222007-06-02 23:28:54 +00004307 }
Bill Wendling3708c182007-05-27 10:15:43 +00004308
4309 // Recursively parse the declarator.
Sebastian Redlbd150f42008-11-21 19:14:01 +00004310 ParseDeclaratorInternal(D, DirectDeclParser);
Bill Wendling3708c182007-05-27 10:15:43 +00004311
Douglas Gregor66583c52008-11-03 15:51:28 +00004312 if (D.getNumTypeObjects() > 0) {
4313 // C++ [dcl.ref]p4: There shall be no references to references.
4314 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4315 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerebad6a22008-11-19 07:37:42 +00004316 if (const IdentifierInfo *II = D.getIdentifier())
4317 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4318 << II;
4319 else
4320 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4321 << "type name";
Douglas Gregor66583c52008-11-03 15:51:28 +00004322
Sebastian Redlbd150f42008-11-21 19:14:01 +00004323 // Once we've complained about the reference-to-reference, we
Douglas Gregor66583c52008-11-03 15:51:28 +00004324 // can go ahead and build the (technically ill-formed)
4325 // declarator: reference collapsing will take care of it.
4326 }
4327 }
4328
Bill Wendling3708c182007-05-27 10:15:43 +00004329 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner788404f2008-02-21 01:32:26 +00004330 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redled0f3b02009-03-15 22:02:01 +00004331 Kind == tok::amp),
John McCall084e83d2011-03-24 11:26:52 +00004332 DS.getAttributes(),
Sebastian Redlf6591ca2009-02-09 18:23:29 +00004333 SourceLocation());
Bill Wendling3708c182007-05-27 10:15:43 +00004334 }
Chris Lattner6c7416c2006-08-07 00:19:33 +00004335}
4336
Richard Smith0efa75c2012-03-29 01:16:42 +00004337static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4338 SourceLocation EllipsisLoc) {
4339 if (EllipsisLoc.isValid()) {
4340 FixItHint Insertion;
4341 if (!D.getEllipsisLoc().isValid()) {
4342 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4343 D.setEllipsisLoc(EllipsisLoc);
4344 }
4345 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4346 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4347 }
4348}
4349
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004350/// ParseDirectDeclarator
4351/// direct-declarator: [C99 6.7.5]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004352/// [C99] identifier
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004353/// '(' declarator ')'
4354/// [GNU] '(' attributes declarator ')'
Chris Lattnere8074e62006-08-06 18:30:15 +00004355/// [C90] direct-declarator '[' constant-expression[opt] ']'
4356/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4357/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4358/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4359/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004360/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4361/// attribute-specifier-seq[opt]
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004362/// direct-declarator '(' parameter-type-list ')'
4363/// direct-declarator '(' identifier-list[opt] ')'
4364/// [GNU] direct-declarator '(' parameter-forward-declarations
4365/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis22c40fa2008-10-24 21:46:40 +00004366/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4367/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004368/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4369/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4370/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregor61956c42008-10-31 09:07:45 +00004371/// [C++] declarator-id
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004372/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor831c93f2008-11-05 20:51:48 +00004373///
4374/// declarator-id: [C++ 8]
Douglas Gregor27b4c162010-12-23 22:44:42 +00004375/// '...'[opt] id-expression
Douglas Gregor831c93f2008-11-05 20:51:48 +00004376/// '::'[opt] nested-name-specifier[opt] type-name
4377///
4378/// id-expression: [C++ 5.1]
4379/// unqualified-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004380/// qualified-id
Douglas Gregor831c93f2008-11-05 20:51:48 +00004381///
4382/// unqualified-id: [C++ 5.1]
Mike Stump11289f42009-09-09 15:08:12 +00004383/// identifier
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004384/// operator-function-id
Douglas Gregord90fd522009-09-25 21:45:23 +00004385/// conversion-function-id
Mike Stump11289f42009-09-09 15:08:12 +00004386/// '~' class-name
Douglas Gregor7f741122009-02-25 19:37:18 +00004387/// template-id
Argyrios Kyrtzidise4426352008-11-07 22:02:30 +00004388///
Richard Smith1453e312012-03-27 01:42:32 +00004389/// Note, any additional constructs added here may need corresponding changes
4390/// in isConstructorDeclarator.
Chris Lattneracd58a32006-08-06 17:24:14 +00004391void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004392 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidis32a03792008-11-08 16:45:02 +00004393
David Blaikiebbafb8a2012-03-11 07:00:24 +00004394 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor7861a802009-11-03 01:35:08 +00004395 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004396 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregordf593fb2011-11-07 17:33:42 +00004397 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4398 D.getContext() == Declarator::MemberContext;
Chad Rosierc1183952012-06-26 22:30:43 +00004399 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregordf593fb2011-11-07 17:33:42 +00004400 EnteringContext);
John McCall1f476a12010-02-26 08:45:28 +00004401 }
4402
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004403 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor0be31a22010-07-02 17:43:08 +00004404 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCall2b058ef2009-12-11 20:04:54 +00004405 // Change the declaration context for name lookup, until this function
4406 // is exited (and the declarator has been parsed).
4407 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004408 }
4409
Douglas Gregor27b4c162010-12-23 22:44:42 +00004410 // C++0x [dcl.fct]p14:
4411 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosierc1183952012-06-26 22:30:43 +00004412 // of a parameter-declaration-clause without a preceding comma. In
4413 // this case, the ellipsis is parsed as part of the
4414 // abstract-declarator if the type of the parameter names a template
Douglas Gregor27b4c162010-12-23 22:44:42 +00004415 // parameter pack that has not been expanded; otherwise, it is parsed
4416 // as part of the parameter-declaration-clause.
Richard Smith0efa75c2012-03-29 01:16:42 +00004417 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004418 !((D.getContext() == Declarator::PrototypeContext ||
4419 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregor27b4c162010-12-23 22:44:42 +00004420 NextToken().is(tok::r_paren) &&
Richard Smith0efa75c2012-03-29 01:16:42 +00004421 !Actions.containsUnexpandedParameterPacks(D))) {
4422 SourceLocation EllipsisLoc = ConsumeToken();
4423 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4424 // The ellipsis was put in the wrong place. Recover, and explain to
4425 // the user what they should have done.
4426 ParseDeclarator(D);
4427 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4428 return;
4429 } else
4430 D.setEllipsisLoc(EllipsisLoc);
4431
4432 // The ellipsis can't be followed by a parenthesized declarator. We
4433 // check for that in ParseParenDeclarator, after we have disambiguated
4434 // the l_paren token.
4435 }
4436
Douglas Gregor7861a802009-11-03 01:35:08 +00004437 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4438 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4439 // We found something that indicates the start of an unqualified-id.
4440 // Parse that unqualified-id.
John McCall84821e72010-04-13 06:39:49 +00004441 bool AllowConstructorName;
4442 if (D.getDeclSpec().hasTypeSpecifier())
4443 AllowConstructorName = false;
4444 else if (D.getCXXScopeSpec().isSet())
4445 AllowConstructorName =
4446 (D.getContext() == Declarator::FileContext ||
4447 (D.getContext() == Declarator::MemberContext &&
4448 D.getDeclSpec().isFriendSpecified()));
4449 else
4450 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4451
Abramo Bagnara7945c982012-01-27 09:46:47 +00004452 SourceLocation TemplateKWLoc;
Chad Rosierc1183952012-06-26 22:30:43 +00004453 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4454 /*EnteringContext=*/true,
4455 /*AllowDestructorName=*/true,
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004456 AllowConstructorName,
John McCallba7bf592010-08-24 05:47:05 +00004457 ParsedType(),
Abramo Bagnara7945c982012-01-27 09:46:47 +00004458 TemplateKWLoc,
Jeffrey Yasskinc76498d2010-04-08 16:38:48 +00004459 D.getName()) ||
4460 // Once we're past the identifier, if the scope was bad, mark the
4461 // whole declarator bad.
4462 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004463 D.SetIdentifier(0, Tok.getLocation());
4464 D.setInvalidType(true);
Douglas Gregor7861a802009-11-03 01:35:08 +00004465 } else {
4466 // Parsed the unqualified-id; update range information and move along.
4467 if (D.getSourceRange().getBegin().isInvalid())
4468 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4469 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregordbc5daf2008-11-07 20:08:42 +00004470 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004471 goto PastIdentifier;
Douglas Gregor11d0c4c2008-11-06 22:13:31 +00004472 }
Douglas Gregor7861a802009-11-03 01:35:08 +00004473 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004474 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004475 "There's a C++-specific check for tok::identifier above");
4476 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4477 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4478 ConsumeToken();
Douglas Gregor7861a802009-11-03 01:35:08 +00004479 goto PastIdentifier;
4480 }
Richard Smith0efa75c2012-03-29 01:16:42 +00004481
Douglas Gregor7861a802009-11-03 01:35:08 +00004482 if (Tok.is(tok::l_paren)) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004483 // direct-declarator: '(' declarator ')'
Chris Lattnere37e2332006-08-15 04:50:22 +00004484 // direct-declarator: '(' attributes declarator ')'
Chris Lattneracd58a32006-08-06 17:24:14 +00004485 // Example: 'char (*X)' or 'int (*XX)(void)'
4486 ParseParenDeclarator(D);
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004487
4488 // If the declarator was parenthesized, we entered the declarator
4489 // scope when parsing the parenthesized declarator, then exited
4490 // the scope already. Re-enter the scope, if we need to.
4491 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004492 // If there was an error parsing parenthesized declarator, declarator
Richard Smith0efa75c2012-03-29 01:16:42 +00004493 // scope may have been entered before. Don't do it again.
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004494 if (!D.isInvalidType() &&
4495 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004496 // Change the declaration context for name lookup, until this function
4497 // is exited (and the declarator has been parsed).
Fariborz Jahanian358acd52010-08-17 23:50:37 +00004498 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor9de54ea2010-01-13 17:31:36 +00004499 }
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004500 } else if (D.mayOmitIdentifier()) {
Chris Lattneracd58a32006-08-06 17:24:14 +00004501 // This could be something simple like "int" (in which case the declarator
4502 // portion is empty), if an abstract-declarator is allowed.
4503 D.SetIdentifier(0, Tok.getLocation());
4504 } else {
David Blaikie5d577a22012-06-29 22:03:56 +00004505 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie5bd4c2a2012-08-21 18:56:49 +00004506 LLVM_BUILTIN_TRAP;
Douglas Gregord9f92e22009-03-06 23:28:18 +00004507 if (D.getContext() == Declarator::MemberContext)
4508 Diag(Tok, diag::err_expected_member_name_or_semi)
4509 << D.getDeclSpec().getSourceRange();
Richard Trieu9c672672013-01-26 02:31:38 +00004510 else if (getLangOpts().CPlusPlus) {
4511 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4512 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4513 else
4514 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4515 } else
Chris Lattner6d29c102008-11-18 07:48:38 +00004516 Diag(Tok, diag::err_expected_ident_lparen);
Chris Lattnereec40f92006-08-06 21:55:29 +00004517 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner8c5dd732008-11-11 06:13:16 +00004518 D.setInvalidType(true);
Chris Lattneracd58a32006-08-06 17:24:14 +00004519 }
Mike Stump11289f42009-09-09 15:08:12 +00004520
Argyrios Kyrtzidis9323b042008-11-26 22:40:03 +00004521 PastIdentifier:
Chris Lattneracd58a32006-08-06 17:24:14 +00004522 assert(D.isPastIdentifier() &&
4523 "Haven't past the location of the identifier yet?");
Mike Stump11289f42009-09-09 15:08:12 +00004524
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004525 // Don't parse attributes unless we have parsed an unparenthesized name.
4526 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith89645bc2013-01-02 12:01:23 +00004527 MaybeParseCXX11Attributes(D);
Alexis Hunt96d5c762009-11-21 08:43:09 +00004528
Chris Lattneracd58a32006-08-06 17:24:14 +00004529 while (1) {
Chris Lattner76c72282007-10-09 17:33:22 +00004530 if (Tok.is(tok::l_paren)) {
David Blaikie15a430a2011-12-04 05:04:18 +00004531 // Enter function-declaration scope, limiting any declarators to the
4532 // function prototype scope, including parameter declarators.
4533 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004534 Scope::FunctionPrototypeScope|Scope::DeclScope|
4535 (D.isFunctionDeclaratorAFunctionDeclaration()
4536 ? Scope::FunctionDeclarationScope : 0));
4537
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004538 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4539 // In such a case, check if we actually have a function declarator; if it
4540 // is not, the declarator has been fully parsed.
Richard Smith943c4402012-07-30 21:30:52 +00004541 bool IsAmbiguous = false;
Richard Smith4f605af2012-08-18 00:55:03 +00004542 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4543 // The name of the declarator, if any, is tentatively declared within
4544 // a possible direct initializer.
4545 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4546 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4547 TentativelyDeclaredIdentifiers.pop_back();
4548 if (!IsFunctionDecl)
4549 break;
4550 }
John McCall084e83d2011-03-24 11:26:52 +00004551 ParsedAttributes attrs(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004552 BalancedDelimiterTracker T(*this, tok::l_paren);
4553 T.consumeOpen();
Richard Smith943c4402012-07-30 21:30:52 +00004554 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie15a430a2011-12-04 05:04:18 +00004555 PrototypeScope.Exit();
Chris Lattner76c72282007-10-09 17:33:22 +00004556 } else if (Tok.is(tok::l_square)) {
Chris Lattnere8074e62006-08-06 18:30:15 +00004557 ParseBracketDeclarator(D);
Chris Lattneracd58a32006-08-06 17:24:14 +00004558 } else {
4559 break;
4560 }
4561 }
Chad Rosierc1183952012-06-26 22:30:43 +00004562}
Chris Lattneracd58a32006-08-06 17:24:14 +00004563
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004564/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4565/// only called before the identifier, so these are most likely just grouping
Mike Stump11289f42009-09-09 15:08:12 +00004566/// parens for precedence. If we find that these are actually function
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004567/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4568///
4569/// direct-declarator:
4570/// '(' declarator ')'
4571/// [GNU] '(' attributes declarator ')'
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004572/// direct-declarator '(' parameter-type-list ')'
4573/// direct-declarator '(' identifier-list[opt] ')'
4574/// [GNU] direct-declarator '(' parameter-forward-declarations
4575/// parameter-type-list[opt] ')'
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004576///
4577void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004578 BalancedDelimiterTracker T(*this, tok::l_paren);
4579 T.consumeOpen();
4580
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004581 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump11289f42009-09-09 15:08:12 +00004582
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004583 // Eat any attributes before we look at whether this is a grouping or function
4584 // declarator paren. If this is a grouping paren, the attribute applies to
4585 // the type being built up, for example:
4586 // int (__attribute__(()) *x)(long y)
4587 // If this ends up not being a grouping paren, the attribute applies to the
4588 // first argument, for example:
4589 // int (__attribute__(()) int x)
4590 // In either case, we need to eat any attributes to be able to determine what
4591 // sort of paren this is.
4592 //
John McCall084e83d2011-03-24 11:26:52 +00004593 ParsedAttributes attrs(AttrFactory);
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004594 bool RequiresArg = false;
4595 if (Tok.is(tok::kw___attribute)) {
John McCall53fa7142010-12-24 02:08:15 +00004596 ParseGNUAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004597
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004598 // We require that the argument list (if this is a non-grouping paren) be
4599 // present even if the attribute list was empty.
4600 RequiresArg = true;
4601 }
Chad Rosiereea9ca72012-12-21 21:22:20 +00004602
Steve Naroff44ac7772008-12-25 14:16:32 +00004603 // Eat any Microsoft extensions.
Chad Rosiereea9ca72012-12-21 21:22:20 +00004604 ParseMicrosoftTypeAttributes(attrs);
4605
Dawn Perchik335e16b2010-09-03 01:29:35 +00004606 // Eat any Borland extensions.
Ted Kremenek5eec2b02010-11-10 05:59:39 +00004607 if (Tok.is(tok::kw___pascal))
John McCall53fa7142010-12-24 02:08:15 +00004608 ParseBorlandTypeAttributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00004609
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004610 // If we haven't past the identifier yet (or where the identifier would be
4611 // stored, if this is an abstract declarator), then this is probably just
4612 // grouping parens. However, if this could be an abstract-declarator, then
4613 // this could also be the start of function arguments (consider 'void()').
4614 bool isGrouping;
Mike Stump11289f42009-09-09 15:08:12 +00004615
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004616 if (!D.mayOmitIdentifier()) {
4617 // If this can't be an abstract-declarator, this *must* be a grouping
4618 // paren, because we haven't seen the identifier yet.
4619 isGrouping = true;
4620 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith43f340f2012-03-27 23:05:05 +00004621 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4622 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith2620cd92012-04-11 04:01:28 +00004623 isDeclarationSpecifier() || // 'int(int)' is a function.
4624 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004625 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4626 // considered to be a type, not a K&R identifier-list.
4627 isGrouping = false;
4628 } else {
4629 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4630 isGrouping = true;
4631 }
Mike Stump11289f42009-09-09 15:08:12 +00004632
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004633 // If this is a grouping paren, handle:
4634 // direct-declarator: '(' declarator ')'
4635 // direct-declarator: '(' attributes declarator ')'
4636 if (isGrouping) {
Richard Smith0efa75c2012-03-29 01:16:42 +00004637 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4638 D.setEllipsisLoc(SourceLocation());
4639
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004640 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis9a1191c2008-10-06 17:10:33 +00004641 D.setGroupingParens(true);
Sebastian Redlbd150f42008-11-21 19:14:01 +00004642 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004643 // Match the ')'.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004644 T.consumeClose();
Chad Rosierc1183952012-06-26 22:30:43 +00004645 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004646 T.getCloseLocation()),
4647 attrs, T.getCloseLocation());
Argyrios Kyrtzidis8ae36842008-10-07 10:21:57 +00004648
4649 D.setGroupingParens(hadGroupingParens);
Richard Smith0efa75c2012-03-29 01:16:42 +00004650
4651 // An ellipsis cannot be placed outside parentheses.
4652 if (EllipsisLoc.isValid())
4653 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4654
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004655 return;
4656 }
Mike Stump11289f42009-09-09 15:08:12 +00004657
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004658 // Okay, if this wasn't a grouping paren, it must be the start of a function
4659 // argument list. Recognize that this declarator will never have an
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004660 // identifier (and remember where it would have been), then call into
4661 // ParseFunctionDeclarator to handle of argument list.
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004662 D.SetIdentifier(0, Tok.getLocation());
4663
David Blaikie15a430a2011-12-04 05:04:18 +00004664 // Enter function-declaration scope, limiting any declarators to the
4665 // function prototype scope, including parameter declarators.
4666 ParseScope PrototypeScope(this,
Richard Smithe233fbf2013-01-28 22:42:45 +00004667 Scope::FunctionPrototypeScope | Scope::DeclScope |
4668 (D.isFunctionDeclaratorAFunctionDeclaration()
4669 ? Scope::FunctionDeclarationScope : 0));
Richard Smith943c4402012-07-30 21:30:52 +00004670 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie15a430a2011-12-04 05:04:18 +00004671 PrototypeScope.Exit();
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00004672}
4673
4674/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4675/// declarator D up to a paren, which indicates that we are parsing function
4676/// arguments.
Chris Lattneracd58a32006-08-06 17:24:14 +00004677///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004678/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4679/// immediately after the open paren - they should be considered to be the
4680/// first argument of a parameter.
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004681///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004682/// If RequiresArg is true, then the first argument of the function is required
4683/// to be present and required to not be an identifier list.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004684///
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004685/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4686/// (C++11) ref-qualifier[opt], exception-specification[opt],
4687/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4688///
4689/// [C++11] exception-specification:
Douglas Gregor9e66af42011-07-05 16:44:18 +00004690/// dynamic-exception-specification
4691/// noexcept-specification
4692///
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004693void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004694 ParsedAttributes &FirstArgAttrs,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004695 BalancedDelimiterTracker &Tracker,
Richard Smith943c4402012-07-30 21:30:52 +00004696 bool IsAmbiguous,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004697 bool RequiresArg) {
Chad Rosierc1183952012-06-26 22:30:43 +00004698 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie15a430a2011-12-04 05:04:18 +00004699 "Should call from a Function scope");
Douglas Gregor9e66af42011-07-05 16:44:18 +00004700 // lparen is already consumed!
4701 assert(D.isPastIdentifier() && "Should not call before identifier!");
4702
4703 // This should be true when the function has typed arguments.
4704 // Otherwise, it is treated as a K&R-style function.
4705 bool HasProto = false;
4706 // Build up an array of information about the parsed arguments.
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004707 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004708 // Remember where we see an ellipsis, if any.
4709 SourceLocation EllipsisLoc;
4710
4711 DeclSpec DS(AttrFactory);
4712 bool RefQualifierIsLValueRef = true;
4713 SourceLocation RefQualifierLoc;
Douglas Gregore248eea2011-10-19 06:04:55 +00004714 SourceLocation ConstQualifierLoc;
4715 SourceLocation VolatileQualifierLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004716 ExceptionSpecificationType ESpecType = EST_None;
4717 SourceRange ESpecRange;
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004718 SmallVector<ParsedType, 2> DynamicExceptions;
4719 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004720 ExprResult NoexceptExpr;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004721 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith700537c2012-06-12 01:51:59 +00004722 TypeResult TrailingReturnType;
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004723
James Molloy6f8780b2012-02-29 10:24:19 +00004724 Actions.ActOnStartFunctionDeclarator();
4725
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004726 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4727 EndLoc is the end location for the function declarator.
4728 They differ for trailing return types. */
4729 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004730 SourceLocation LParenLoc, RParenLoc;
4731 LParenLoc = Tracker.getOpenLocation();
4732 StartLoc = LParenLoc;
4733
Douglas Gregor9e66af42011-07-05 16:44:18 +00004734 if (isFunctionDeclaratorIdentifierList()) {
4735 if (RequiresArg)
4736 Diag(Tok, diag::err_argument_required_after_attribute);
4737
4738 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4739
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004740 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004741 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004742 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004743 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004744 } else {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004745 if (Tok.isNot(tok::r_paren))
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004746 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004747 else if (RequiresArg)
4748 Diag(Tok, diag::err_argument_required_after_attribute);
4749
David Blaikiebbafb8a2012-03-11 07:00:24 +00004750 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004751
4752 // If we have the closing ')', eat it.
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004753 Tracker.consumeClose();
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004754 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004755 LocalEndLoc = RParenLoc;
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004756 EndLoc = RParenLoc;
Douglas Gregor9e66af42011-07-05 16:44:18 +00004757
David Blaikiebbafb8a2012-03-11 07:00:24 +00004758 if (getLangOpts().CPlusPlus) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004759 // FIXME: Accept these components in any order, and produce fixits to
4760 // correct the order if the user gets it wrong. Ideally we should deal
4761 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004762
4763 // Parse cv-qualifier-seq[opt].
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004764 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4765 if (!DS.getSourceRange().getEnd().isInvalid()) {
4766 EndLoc = DS.getSourceRange().getEnd();
4767 ConstQualifierLoc = DS.getConstSpecLoc();
4768 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4769 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004770
4771 // Parse ref-qualifier[opt].
4772 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004773 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith5d164bc2011-10-15 05:09:34 +00004774 diag::warn_cxx98_compat_ref_qualifier :
4775 diag::ext_ref_qualifier);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004776
Douglas Gregor9e66af42011-07-05 16:44:18 +00004777 RefQualifierIsLValueRef = Tok.is(tok::amp);
4778 RefQualifierLoc = ConsumeToken();
4779 EndLoc = RefQualifierLoc;
4780 }
4781
Douglas Gregor3024f072012-04-16 07:05:22 +00004782 // C++11 [expr.prim.general]p3:
Chad Rosierc1183952012-06-26 22:30:43 +00004783 // If a declaration declares a member function or member function
4784 // template of a class X, the expression this is a prvalue of type
Douglas Gregor3024f072012-04-16 07:05:22 +00004785 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosierc1183952012-06-26 22:30:43 +00004786 // and the end of the function-definition, member-declarator, or
Douglas Gregor3024f072012-04-16 07:05:22 +00004787 // declarator.
Chad Rosierc1183952012-06-26 22:30:43 +00004788 bool IsCXX11MemberFunction =
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004789 getLangOpts().CPlusPlus11 &&
Douglas Gregor3024f072012-04-16 07:05:22 +00004790 (D.getContext() == Declarator::MemberContext ||
4791 (D.getContext() == Declarator::FileContext &&
Chad Rosierc1183952012-06-26 22:30:43 +00004792 D.getCXXScopeSpec().isValid() &&
Douglas Gregor3024f072012-04-16 07:05:22 +00004793 Actions.CurContext->isRecord()));
4794 Sema::CXXThisScopeRAII ThisScope(Actions,
4795 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith01141a92013-01-14 01:55:13 +00004796 DS.getTypeQualifiers() |
4797 (D.getDeclSpec().isConstexprSpecified()
4798 ? Qualifiers::Const : 0),
Douglas Gregor3024f072012-04-16 07:05:22 +00004799 IsCXX11MemberFunction);
Richard Smith2331bbf2012-05-02 22:22:32 +00004800
Douglas Gregor9e66af42011-07-05 16:44:18 +00004801 // Parse exception-specification[opt].
Richard Smith2331bbf2012-05-02 22:22:32 +00004802 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor433e0532012-04-16 18:27:27 +00004803 DynamicExceptions,
4804 DynamicExceptionRanges,
Richard Smith2331bbf2012-05-02 22:22:32 +00004805 NoexceptExpr);
Douglas Gregor9e66af42011-07-05 16:44:18 +00004806 if (ESpecType != EST_None)
4807 EndLoc = ESpecRange.getEnd();
4808
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004809 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4810 // after the exception-specification.
Richard Smith89645bc2013-01-02 12:01:23 +00004811 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004812
Douglas Gregor9e66af42011-07-05 16:44:18 +00004813 // Parse trailing-return-type[opt].
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004814 LocalEndLoc = EndLoc;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00004815 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith5d164bc2011-10-15 05:09:34 +00004816 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004817 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4818 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004819 LocalEndLoc = Tok.getLocation();
Douglas Gregordb0b9f12011-08-04 15:30:47 +00004820 SourceRange Range;
Richard Smith700537c2012-06-12 01:51:59 +00004821 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004822 EndLoc = Range.getEnd();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004823 }
4824 }
Douglas Gregor9e66af42011-07-05 16:44:18 +00004825 }
4826
4827 // Remember that we parsed a function type, and remember the attributes.
4828 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004829 IsAmbiguous,
4830 LParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004831 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnaraaeeb9892012-10-04 21:42:10 +00004832 EllipsisLoc, RParenLoc,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004833 DS.getTypeQualifiers(),
4834 RefQualifierIsLValueRef,
Douglas Gregore248eea2011-10-19 06:04:55 +00004835 RefQualifierLoc, ConstQualifierLoc,
4836 VolatileQualifierLoc,
Douglas Gregorad69e652011-07-13 21:47:47 +00004837 /*MutableLoc=*/SourceLocation(),
Douglas Gregor9e66af42011-07-05 16:44:18 +00004838 ESpecType, ESpecRange.getBegin(),
4839 DynamicExceptions.data(),
4840 DynamicExceptionRanges.data(),
4841 DynamicExceptions.size(),
4842 NoexceptExpr.isUsable() ?
4843 NoexceptExpr.get() : 0,
Abramo Bagnara2fc03ca2012-10-15 21:05:46 +00004844 StartLoc, LocalEndLoc, D,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004845 TrailingReturnType),
Richard Smith7bdcc4a2012-04-10 01:32:12 +00004846 FnAttrs, EndLoc);
James Molloy6f8780b2012-02-29 10:24:19 +00004847
4848 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor9e66af42011-07-05 16:44:18 +00004849}
4850
4851/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4852/// identifier list form for a K&R-style function: void foo(a,b,c)
4853///
4854/// Note that identifier-lists are only allowed for normal declarators, not for
4855/// abstract-declarators.
4856bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikiebbafb8a2012-03-11 07:00:24 +00004857 return !getLangOpts().CPlusPlus
Douglas Gregor9e66af42011-07-05 16:44:18 +00004858 && Tok.is(tok::identifier)
4859 && !TryAltiVecVectorToken()
4860 // K&R identifier lists can't have typedefs as identifiers, per C99
4861 // 6.7.5.3p11.
4862 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4863 // Identifier lists follow a really simple grammar: the identifiers can
4864 // be followed *only* by a ", identifier" or ")". However, K&R
4865 // identifier lists are really rare in the brave new modern world, and
4866 // it is very common for someone to typo a type in a non-K&R style
4867 // list. If we are presented with something like: "void foo(intptr x,
4868 // float y)", we don't want to start parsing the function declarator as
4869 // though it is a K&R style declarator just because intptr is an
4870 // invalid type.
4871 //
4872 // To handle this, we check to see if the token after the first
4873 // identifier is a "," or ")". Only then do we parse it as an
4874 // identifier list.
4875 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4876}
4877
4878/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4879/// we found a K&R-style identifier list instead of a typed parameter list.
4880///
4881/// After returning, ParamInfo will hold the parsed parameters.
4882///
4883/// identifier-list: [C99 6.7.5]
4884/// identifier
4885/// identifier-list ',' identifier
4886///
4887void Parser::ParseFunctionDeclaratorIdentifierList(
4888 Declarator &D,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004889 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor9e66af42011-07-05 16:44:18 +00004890 // If there was no identifier specified for the declarator, either we are in
4891 // an abstract-declarator, or we are in a parameter declarator which was found
4892 // to be abstract. In abstract-declarators, identifier lists are not valid:
4893 // diagnose this.
4894 if (!D.getIdentifier())
4895 Diag(Tok, diag::ext_ident_list_in_param);
4896
4897 // Maintain an efficient lookup of params we have seen so far.
4898 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4899
4900 while (1) {
4901 // If this isn't an identifier, report the error and skip until ')'.
4902 if (Tok.isNot(tok::identifier)) {
4903 Diag(Tok, diag::err_expected_ident);
4904 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4905 // Forget we parsed anything.
4906 ParamInfo.clear();
4907 return;
4908 }
4909
4910 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4911
4912 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4913 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4914 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4915
4916 // Verify that the argument identifier has not already been mentioned.
4917 if (!ParamsSoFar.insert(ParmII)) {
4918 Diag(Tok, diag::err_param_redefinition) << ParmII;
4919 } else {
4920 // Remember this identifier in ParamInfo.
4921 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4922 Tok.getLocation(),
4923 0));
4924 }
4925
4926 // Eat the identifier.
4927 ConsumeToken();
4928
4929 // The list continues if we see a comma.
4930 if (Tok.isNot(tok::comma))
4931 break;
4932 ConsumeToken();
4933 }
4934}
4935
4936/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4937/// after the opening parenthesis. This function will not parse a K&R-style
4938/// identifier list.
4939///
Richard Smith2620cd92012-04-11 04:01:28 +00004940/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4941/// caller parsed those arguments immediately after the open paren - they should
4942/// be considered to be part of the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004943///
4944/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4945/// be the location of the ellipsis, if any was parsed.
4946///
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004947/// parameter-type-list: [C99 6.7.5]
4948/// parameter-list
4949/// parameter-list ',' '...'
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00004950/// [C++] parameter-list '...'
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004951///
4952/// parameter-list: [C99 6.7.5]
4953/// parameter-declaration
4954/// parameter-list ',' parameter-declaration
4955///
4956/// parameter-declaration: [C99 6.7.5]
4957/// declaration-specifiers declarator
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00004958/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redldb63af22012-03-14 15:54:00 +00004959/// [C++11] initializer-clause
Chris Lattnere37e2332006-08-15 04:50:22 +00004960/// [GNU] declaration-specifiers declarator attributes
Sebastian Redlf769df52009-03-24 22:27:57 +00004961/// declaration-specifiers abstract-declarator[opt]
4962/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner58258242008-04-10 02:22:51 +00004963/// '=' assignment-expression
Chris Lattnere37e2332006-08-15 04:50:22 +00004964/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith2620cd92012-04-11 04:01:28 +00004965/// [C++11] attribute-specifier-seq parameter-declaration
Chris Lattnerc0acd3d2006-07-31 05:13:43 +00004966///
Douglas Gregor9e66af42011-07-05 16:44:18 +00004967void Parser::ParseParameterDeclarationClause(
4968 Declarator &D,
Richard Smith2620cd92012-04-11 04:01:28 +00004969 ParsedAttributes &FirstArgAttrs,
Chris Lattner0e62c1c2011-07-23 10:55:15 +00004970 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor9e66af42011-07-05 16:44:18 +00004971 SourceLocation &EllipsisLoc) {
Mike Stump11289f42009-09-09 15:08:12 +00004972
Chris Lattner371ed4e2008-04-06 06:57:35 +00004973 while (1) {
4974 if (Tok.is(tok::ellipsis)) {
Richard Smith2620cd92012-04-11 04:01:28 +00004975 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4976 // before deciding this was a parameter-declaration-clause.
Douglas Gregor94349fd2009-02-18 07:07:28 +00004977 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattner371ed4e2008-04-06 06:57:35 +00004978 break;
Chris Lattneracd58a32006-08-06 17:24:14 +00004979 }
Mike Stump11289f42009-09-09 15:08:12 +00004980
Chris Lattner371ed4e2008-04-06 06:57:35 +00004981 // Parse the declaration-specifiers.
John McCall28a6aea2009-11-04 02:18:39 +00004982 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall084e83d2011-03-24 11:26:52 +00004983 DeclSpec DS(AttrFactory);
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00004984
Richard Smith2620cd92012-04-11 04:01:28 +00004985 // Parse any C++11 attributes.
Richard Smith89645bc2013-01-02 12:01:23 +00004986 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith2620cd92012-04-11 04:01:28 +00004987
John McCall53fa7142010-12-24 02:08:15 +00004988 // Skip any Microsoft attributes before a param.
Chad Rosierf8a2e702012-12-20 20:37:53 +00004989 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall53fa7142010-12-24 02:08:15 +00004990
4991 SourceLocation DSStart = Tok.getLocation();
Chris Lattner8ff2c6c2008-10-20 02:05:46 +00004992
4993 // If the caller parsed attributes for the first argument, add them now.
John McCall53fa7142010-12-24 02:08:15 +00004994 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor9e66af42011-07-05 16:44:18 +00004995 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith2620cd92012-04-11 04:01:28 +00004996 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4997 // too much hassle.
4998 DS.takeAttributesFrom(FirstArgAttrs);
John McCall53fa7142010-12-24 02:08:15 +00004999
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005000 ParseDeclarationSpecifiers(DS);
Mike Stump11289f42009-09-09 15:08:12 +00005001
Chris Lattner371ed4e2008-04-06 06:57:35 +00005002 // Parse the declarator. This is "PrototypeContext", because we must
5003 // accept either 'declarator' or 'abstract-declarator' here.
5004 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5005 ParseDeclarator(ParmDecl);
5006
5007 // Parse GNU attributes, if present.
John McCall53fa7142010-12-24 02:08:15 +00005008 MaybeParseGNUAttributes(ParmDecl);
Mike Stump11289f42009-09-09 15:08:12 +00005009
Chris Lattner371ed4e2008-04-06 06:57:35 +00005010 // Remember this parsed parameter in ParamInfo.
5011 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump11289f42009-09-09 15:08:12 +00005012
Douglas Gregor4d87df52008-12-16 21:30:33 +00005013 // DefArgToks is used when the parsing of default arguments needs
5014 // to be delayed.
5015 CachedTokens *DefArgToks = 0;
5016
Chris Lattner371ed4e2008-04-06 06:57:35 +00005017 // If no parameter was specified, verify that *something* was specified,
5018 // otherwise we have a missing type and identifier.
Chris Lattnerde39c3e2009-02-27 18:38:20 +00005019 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5020 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattner371ed4e2008-04-06 06:57:35 +00005021 // Completely missing, emit error.
5022 Diag(DSStart, diag::err_missing_param);
5023 } else {
5024 // Otherwise, we have something. Add it and let semantic analysis try
5025 // to grok it and add the result to the ParamInfo we are building.
Mike Stump11289f42009-09-09 15:08:12 +00005026
Chris Lattner371ed4e2008-04-06 06:57:35 +00005027 // Inform the actions module about the parameter declarator, so it gets
5028 // added to the current scope.
John McCall48871652010-08-21 09:40:31 +00005029 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005030
5031 // Parse the default argument, if any. We parse the default
5032 // arguments in all dialects; the semantic analysis in
5033 // ActOnParamDefaultArgument will reject the default argument in
5034 // C.
5035 if (Tok.is(tok::equal)) {
Douglas Gregor58354032008-12-24 00:01:03 +00005036 SourceLocation EqualLoc = Tok.getLocation();
5037
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005038 // Parse the default argument
Douglas Gregor4d87df52008-12-16 21:30:33 +00005039 if (D.getContext() == Declarator::MemberContext) {
5040 // If we're inside a class definition, cache the tokens
5041 // corresponding to the default argument. We'll actually parse
5042 // them when we see the end of the class definition.
Douglas Gregor4d87df52008-12-16 21:30:33 +00005043 // FIXME: Can we use a smart pointer for Toks?
5044 DefArgToks = new CachedTokens;
5045
Mike Stump11289f42009-09-09 15:08:12 +00005046 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis8d7bdba2010-04-23 21:20:12 +00005047 /*StopAtSemi=*/true,
5048 /*ConsumeFinalToken=*/false)) {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005049 delete DefArgToks;
5050 DefArgToks = 0;
Douglas Gregor58354032008-12-24 00:01:03 +00005051 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005052 } else {
5053 // Mark the end of the default argument so that we know when to
5054 // stop when we parse it later on.
5055 Token DefArgEnd;
5056 DefArgEnd.startToken();
5057 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5058 DefArgEnd.setLocation(Tok.getLocation());
5059 DefArgToks->push_back(DefArgEnd);
Mike Stump11289f42009-09-09 15:08:12 +00005060 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson84613c42009-06-12 16:51:40 +00005061 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis249179c2010-08-06 09:47:24 +00005062 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005063 } else {
Douglas Gregor4d87df52008-12-16 21:30:33 +00005064 // Consume the '='.
Douglas Gregor58354032008-12-24 00:01:03 +00005065 ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005066
Chad Rosierc1183952012-06-26 22:30:43 +00005067 // The argument isn't actually potentially evaluated unless it is
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005068 // used.
5069 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregor7fcbd902012-02-21 00:37:24 +00005070 Sema::PotentiallyEvaluatedIfUsed,
5071 Param);
Douglas Gregor8a01b2a2010-09-11 20:24:53 +00005072
Sebastian Redldb63af22012-03-14 15:54:00 +00005073 ExprResult DefArgResult;
Richard Smith2bf7fdb2013-01-02 11:42:31 +00005074 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005075 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redldb63af22012-03-14 15:54:00 +00005076 DefArgResult = ParseBraceInitializer();
Sebastian Redl1678d5f2012-03-18 22:25:45 +00005077 } else
Sebastian Redldb63af22012-03-14 15:54:00 +00005078 DefArgResult = ParseAssignmentExpression();
Douglas Gregor4d87df52008-12-16 21:30:33 +00005079 if (DefArgResult.isInvalid()) {
5080 Actions.ActOnParamDefaultArgumentError(Param);
5081 SkipUntil(tok::comma, tok::r_paren, true, true);
5082 } else {
5083 // Inform the actions module about the default argument
5084 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCallb268a282010-08-23 23:25:46 +00005085 DefArgResult.take());
Douglas Gregor4d87df52008-12-16 21:30:33 +00005086 }
Chris Lattneraa9c7ae2008-04-08 04:40:51 +00005087 }
5088 }
Mike Stump11289f42009-09-09 15:08:12 +00005089
5090 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5091 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor4d87df52008-12-16 21:30:33 +00005092 DefArgToks));
Chris Lattner371ed4e2008-04-06 06:57:35 +00005093 }
5094
5095 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005096 if (Tok.isNot(tok::comma)) {
5097 if (Tok.is(tok::ellipsis)) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005098 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosierc1183952012-06-26 22:30:43 +00005099
David Blaikiebbafb8a2012-03-11 07:00:24 +00005100 if (!getLangOpts().CPlusPlus) {
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005101 // We have ellipsis without a preceding ',', which is ill-formed
5102 // in C. Complain and provide the fix.
5103 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregora771f462010-03-31 17:46:05 +00005104 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005105 }
5106 }
Chad Rosierc1183952012-06-26 22:30:43 +00005107
Douglas Gregor9bfc2e52009-09-22 21:41:40 +00005108 break;
5109 }
Mike Stump11289f42009-09-09 15:08:12 +00005110
Chris Lattner371ed4e2008-04-06 06:57:35 +00005111 // Consume the comma.
5112 ConsumeToken();
Chris Lattneracd58a32006-08-06 17:24:14 +00005113 }
Mike Stump11289f42009-09-09 15:08:12 +00005114
Chris Lattner6c940e62008-04-06 06:34:08 +00005115}
Chris Lattnerc0a1c7d2008-04-06 05:45:57 +00005116
Chris Lattnere8074e62006-08-06 18:30:15 +00005117/// [C90] direct-declarator '[' constant-expression[opt] ']'
5118/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5119/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5120/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5121/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005122/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5123/// attribute-specifier-seq[opt]
Chris Lattnere8074e62006-08-06 18:30:15 +00005124void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith7bdcc4a2012-04-10 01:32:12 +00005125 if (CheckProhibitedCXX11Attribute())
5126 return;
5127
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005128 BalancedDelimiterTracker T(*this, tok::l_square);
5129 T.consumeOpen();
Mike Stump11289f42009-09-09 15:08:12 +00005130
Chris Lattner84a11622008-12-18 07:27:21 +00005131 // C array syntax has many features, but by-far the most common is [] and [4].
5132 // This code does a fast path to handle some of the most obvious cases.
5133 if (Tok.getKind() == tok::r_square) {
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005134 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005135 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005136 MaybeParseCXX11Attributes(attrs);
Chad Rosierc1183952012-06-26 22:30:43 +00005137
Chris Lattner84a11622008-12-18 07:27:21 +00005138 // Remember that we parsed the empty array type.
John McCalldadc5752010-08-24 06:29:42 +00005139 ExprResult NumElements;
John McCall084e83d2011-03-24 11:26:52 +00005140 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005141 T.getOpenLocation(),
5142 T.getCloseLocation()),
5143 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005144 return;
5145 } else if (Tok.getKind() == tok::numeric_constant &&
5146 GetLookAheadToken(1).is(tok::r_square)) {
5147 // [4] is very common. Parse the numeric constant expression.
Richard Smithbcc22fc2012-03-09 08:00:36 +00005148 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner84a11622008-12-18 07:27:21 +00005149 ConsumeToken();
5150
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005151 T.consumeClose();
John McCall084e83d2011-03-24 11:26:52 +00005152 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005153 MaybeParseCXX11Attributes(attrs);
Mike Stump11289f42009-09-09 15:08:12 +00005154
Chris Lattner84a11622008-12-18 07:27:21 +00005155 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicc531dcc2013-01-11 08:33:05 +00005156 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall53fa7142010-12-24 02:08:15 +00005157 ExprRes.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005158 T.getOpenLocation(),
5159 T.getCloseLocation()),
5160 attrs, T.getCloseLocation());
Chris Lattner84a11622008-12-18 07:27:21 +00005161 return;
5162 }
Mike Stump11289f42009-09-09 15:08:12 +00005163
Chris Lattnere8074e62006-08-06 18:30:15 +00005164 // If valid, this location is the position where we read the 'static' keyword.
5165 SourceLocation StaticLoc;
Chris Lattner76c72282007-10-09 17:33:22 +00005166 if (Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005167 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005168
Chris Lattnere8074e62006-08-06 18:30:15 +00005169 // If there is a type-qualifier-list, read it now.
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005170 // Type qualifiers in an array subscript are a C99 feature.
John McCall084e83d2011-03-24 11:26:52 +00005171 DeclSpec DS(AttrFactory);
Chris Lattnercf0bab22008-12-18 07:02:59 +00005172 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump11289f42009-09-09 15:08:12 +00005173
Chris Lattnere8074e62006-08-06 18:30:15 +00005174 // If we haven't already read 'static', check to see if there is one after the
5175 // type-qualifier-list.
Chris Lattner76c72282007-10-09 17:33:22 +00005176 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Chris Lattneraf635312006-10-16 06:06:51 +00005177 StaticLoc = ConsumeToken();
Mike Stump11289f42009-09-09 15:08:12 +00005178
Chris Lattnere8074e62006-08-06 18:30:15 +00005179 // Handle "direct-declarator [ type-qual-list[opt] * ]".
Chris Lattnere8074e62006-08-06 18:30:15 +00005180 bool isStar = false;
John McCalldadc5752010-08-24 06:29:42 +00005181 ExprResult NumElements;
Mike Stump11289f42009-09-09 15:08:12 +00005182
Chris Lattner521ff2b2008-04-06 05:26:30 +00005183 // Handle the case where we have '[*]' as the array size. However, a leading
5184 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledru830885c2012-07-23 08:59:39 +00005185 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner521ff2b2008-04-06 05:26:30 +00005186 // infrequent, use of lookahead is not costly here.
5187 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnerc439f0d2008-04-06 05:27:21 +00005188 ConsumeToken(); // Eat the '*'.
Chris Lattner1906f802006-08-06 19:14:46 +00005189
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005190 if (StaticLoc.isValid()) {
Chris Lattner521ff2b2008-04-06 05:26:30 +00005191 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnerb6ec4e72008-12-18 06:50:14 +00005192 StaticLoc = SourceLocation(); // Drop the static.
5193 }
Chris Lattner521ff2b2008-04-06 05:26:30 +00005194 isStar = true;
Chris Lattner76c72282007-10-09 17:33:22 +00005195 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner84a11622008-12-18 07:27:21 +00005196 // Note, in C89, this production uses the constant-expr production instead
5197 // of assignment-expr. The only difference is that assignment-expr allows
5198 // things like '=' and '*='. Sema rejects these in C89 mode because they
5199 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump11289f42009-09-09 15:08:12 +00005200
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005201 // Parse the constant-expression or assignment-expression now (depending
5202 // on dialect).
David Blaikiebbafb8a2012-03-11 07:00:24 +00005203 if (getLangOpts().CPlusPlus) {
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005204 NumElements = ParseConstantExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005205 } else {
5206 EnterExpressionEvaluationContext Unevaluated(Actions,
5207 Sema::ConstantEvaluated);
Douglas Gregorc9c02ed2009-06-19 23:52:42 +00005208 NumElements = ParseAssignmentExpression();
Eli Friedmane0afc982012-01-21 01:01:51 +00005209 }
Chris Lattner62591722006-08-12 18:40:58 +00005210 }
Mike Stump11289f42009-09-09 15:08:12 +00005211
Chris Lattner62591722006-08-12 18:40:58 +00005212 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl17f2c7d2008-12-09 13:15:23 +00005213 if (NumElements.isInvalid()) {
Chris Lattnercd2a8c52009-04-24 22:30:50 +00005214 D.setInvalidType(true);
Chris Lattner62591722006-08-12 18:40:58 +00005215 // If the expression was invalid, skip it.
5216 SkipUntil(tok::r_square);
5217 return;
Chris Lattnere8074e62006-08-06 18:30:15 +00005218 }
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005219
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005220 T.consumeClose();
Sebastian Redlf6591ca2009-02-09 18:23:29 +00005221
John McCall084e83d2011-03-24 11:26:52 +00005222 ParsedAttributes attrs(AttrFactory);
Richard Smith89645bc2013-01-02 12:01:23 +00005223 MaybeParseCXX11Attributes(attrs);
Alexis Hunt96d5c762009-11-21 08:43:09 +00005224
Chris Lattner84a11622008-12-18 07:27:21 +00005225 // Remember that we parsed a array type, and remember its features.
John McCall084e83d2011-03-24 11:26:52 +00005226 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Chris Lattnercbc426d2006-12-02 06:43:02 +00005227 StaticLoc.isValid(), isStar,
Douglas Gregor04318252009-07-06 15:59:29 +00005228 NumElements.release(),
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005229 T.getOpenLocation(),
5230 T.getCloseLocation()),
5231 attrs, T.getCloseLocation());
Chris Lattnere8074e62006-08-06 18:30:15 +00005232}
5233
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005234/// [GNU] typeof-specifier:
5235/// typeof ( expressions )
5236/// typeof ( type-name )
5237/// [GNU/C++] typeof unary-expression
Steve Naroffad373bd2007-07-31 12:34:36 +00005238///
5239void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner76c72282007-10-09 17:33:22 +00005240 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005241 Token OpTok = Tok;
Steve Naroffad373bd2007-07-31 12:34:36 +00005242 SourceLocation StartLoc = ConsumeToken();
5243
John McCalle8595032010-01-13 20:03:27 +00005244 const bool hasParens = Tok.is(tok::l_paren);
5245
Eli Friedman15681d62012-09-26 04:34:21 +00005246 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5247 Sema::ReuseLambdaContextDecl);
Eli Friedmane0afc982012-01-21 01:01:51 +00005248
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005249 bool isCastExpr;
John McCallba7bf592010-08-24 05:47:05 +00005250 ParsedType CastTy;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005251 SourceRange CastRange;
Peter Collingbournee190dee2011-03-11 19:24:49 +00005252 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5253 CastTy, CastRange);
John McCalle8595032010-01-13 20:03:27 +00005254 if (hasParens)
5255 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005256
5257 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005258 // FIXME: Not accurate, the range gets one token more than it should.
5259 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005260 else
5261 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump11289f42009-09-09 15:08:12 +00005262
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005263 if (isCastExpr) {
5264 if (!CastTy) {
5265 DS.SetTypeSpecError();
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005266 return;
Douglas Gregor220cac52009-02-18 17:45:20 +00005267 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005268
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005269 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005270 unsigned DiagID;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005271 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5272 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCall49bfce42009-08-03 20:12:06 +00005273 DiagID, CastTy))
5274 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis7bd98442009-05-22 10:22:50 +00005275 return;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005276 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005277
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005278 // If we get here, the operand to the typeof was an expresion.
5279 if (Operand.isInvalid()) {
5280 DS.SetTypeSpecError();
Steve Naroff4bd2f712007-08-02 02:53:48 +00005281 return;
Steve Naroffad373bd2007-07-31 12:34:36 +00005282 }
Argyrios Kyrtzidis2545aeb2008-09-05 11:26:19 +00005283
Eli Friedmane0afc982012-01-21 01:01:51 +00005284 // We might need to transform the operand if it is potentially evaluated.
5285 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5286 if (Operand.isInvalid()) {
5287 DS.SetTypeSpecError();
5288 return;
5289 }
5290
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005291 const char *PrevSpec = 0;
John McCall49bfce42009-08-03 20:12:06 +00005292 unsigned DiagID;
Argyrios Kyrtzidisf5cc7ac2009-05-22 10:22:18 +00005293 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5294 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallba7bf592010-08-24 05:47:05 +00005295 DiagID, Operand.get()))
John McCall49bfce42009-08-03 20:12:06 +00005296 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffad373bd2007-07-31 12:34:36 +00005297}
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005298
Benjamin Kramere56f3932011-12-23 17:00:35 +00005299/// [C11] atomic-specifier:
Eli Friedman0dfb8892011-10-06 23:00:33 +00005300/// _Atomic ( type-name )
5301///
5302void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5303 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5304
5305 SourceLocation StartLoc = ConsumeToken();
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005306 BalancedDelimiterTracker T(*this, tok::l_paren);
5307 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedman0dfb8892011-10-06 23:00:33 +00005308 SkipUntil(tok::r_paren);
5309 return;
5310 }
5311
5312 TypeResult Result = ParseTypeName();
5313 if (Result.isInvalid()) {
5314 SkipUntil(tok::r_paren);
5315 return;
5316 }
5317
5318 // Match the ')'
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005319 T.consumeClose();
Eli Friedman0dfb8892011-10-06 23:00:33 +00005320
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005321 if (T.getCloseLocation().isInvalid())
Eli Friedman0dfb8892011-10-06 23:00:33 +00005322 return;
5323
Douglas Gregore7a8e3b2011-10-12 16:37:45 +00005324 DS.setTypeofParensRange(T.getRange());
5325 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedman0dfb8892011-10-06 23:00:33 +00005326
5327 const char *PrevSpec = 0;
5328 unsigned DiagID;
5329 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5330 DiagID, Result.release()))
5331 Diag(StartLoc, DiagID) << PrevSpec;
5332}
5333
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005334
5335/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5336/// from TryAltiVecVectorToken.
5337bool Parser::TryAltiVecVectorTokenOutOfLine() {
5338 Token Next = NextToken();
5339 switch (Next.getKind()) {
5340 default: return false;
5341 case tok::kw_short:
5342 case tok::kw_long:
5343 case tok::kw_signed:
5344 case tok::kw_unsigned:
5345 case tok::kw_void:
5346 case tok::kw_char:
5347 case tok::kw_int:
5348 case tok::kw_float:
5349 case tok::kw_double:
5350 case tok::kw_bool:
5351 case tok::kw___pixel:
5352 Tok.setKind(tok::kw___vector);
5353 return true;
5354 case tok::identifier:
5355 if (Next.getIdentifierInfo() == Ident_pixel) {
5356 Tok.setKind(tok::kw___vector);
5357 return true;
5358 }
5359 return false;
5360 }
5361}
5362
5363bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5364 const char *&PrevSpec, unsigned &DiagID,
5365 bool &isInvalid) {
5366 if (Tok.getIdentifierInfo() == Ident_vector) {
5367 Token Next = NextToken();
5368 switch (Next.getKind()) {
5369 case tok::kw_short:
5370 case tok::kw_long:
5371 case tok::kw_signed:
5372 case tok::kw_unsigned:
5373 case tok::kw_void:
5374 case tok::kw_char:
5375 case tok::kw_int:
5376 case tok::kw_float:
5377 case tok::kw_double:
5378 case tok::kw_bool:
5379 case tok::kw___pixel:
5380 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5381 return true;
5382 case tok::identifier:
5383 if (Next.getIdentifierInfo() == Ident_pixel) {
5384 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5385 return true;
5386 }
5387 break;
5388 default:
5389 break;
5390 }
Douglas Gregor9938e3b2010-06-16 15:28:57 +00005391 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner73a9c7d2010-02-28 18:33:55 +00005392 DS.isTypeAltiVecVector()) {
5393 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5394 return true;
5395 }
5396 return false;
5397}