blob: a2045327890ad028d50686dadaa2a04530f13283 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Benjamin Kramer9852f582012-12-01 16:35:25 +000016#include "clang/Basic/AddressSpaces.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000017#include "clang/Basic/CharInfo.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000018#include "clang/Basic/OpenCL.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +000020#include "clang/Sema/Lookup.h"
John McCall19510852010-08-20 18:27:03 +000021#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000022#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000026#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// C99 6.7: Declarations.
31//===----------------------------------------------------------------------===//
32
33/// ParseTypeName
34/// type-name: [C99 6.7.6]
35/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000036///
37/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000038TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000039 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000040 AccessSpecifier AS,
Richard Smith6b3d3e52013-02-20 19:22:51 +000041 Decl **OwnedType,
42 ParsedAttributes *Attrs) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000043 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smitha971d242012-05-09 20:55:26 +000044 if (DSC == DSC_normal)
45 DSC = DSC_type_specifier;
Richard Smith7796eb52012-03-12 08:56:40 +000046
Reid Spencer5f016e22007-07-11 17:01:13 +000047 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000048 DeclSpec DS(AttrFactory);
Richard Smith6b3d3e52013-02-20 19:22:51 +000049 if (Attrs)
50 DS.addAttributes(Attrs->getList());
Richard Smith7796eb52012-03-12 08:56:40 +000051 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000052 if (OwnedType)
53 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000054
Reid Spencer5f016e22007-07-11 17:01:13 +000055 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000056 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000057 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000058 if (Range)
59 *Range = DeclaratorInfo.getSourceRange();
60
Chris Lattnereaaebc72009-04-25 08:06:05 +000061 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000062 return true;
63
Douglas Gregor23c94db2010-07-02 17:43:08 +000064 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000065}
66
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000067
68/// isAttributeLateParsed - Return true if the attribute has arguments that
69/// require late parsing.
70static bool isAttributeLateParsed(const IdentifierInfo &II) {
71 return llvm::StringSwitch<bool>(II.getName())
72#include "clang/Parse/AttrLateParsed.inc"
73 .Default(false);
74}
75
Sean Huntbbd37c62009-11-21 08:43:09 +000076/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000077///
78/// [GNU] attributes:
79/// attribute
80/// attributes attribute
81///
82/// [GNU] attribute:
83/// '__attribute__' '(' '(' attribute-list ')' ')'
84///
85/// [GNU] attribute-list:
86/// attrib
87/// attribute_list ',' attrib
88///
89/// [GNU] attrib:
90/// empty
91/// attrib-name
92/// attrib-name '(' identifier ')'
93/// attrib-name '(' identifier ',' nonempty-expr-list ')'
94/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
95///
96/// [GNU] attrib-name:
97/// identifier
98/// typespec
99/// typequal
100/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +0000101///
Reid Spencer5f016e22007-07-11 17:01:13 +0000102/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +0000103/// token lookahead. Comment from gcc: "If they start with an identifier
104/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +0000105/// start with that identifier; otherwise they are an expression list."
106///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000107/// GCC does not require the ',' between attribs in an attribute-list.
108///
Reid Spencer5f016e22007-07-11 17:01:13 +0000109/// At the moment, I am not doing 2 token lookahead. I am also unaware of
110/// any attributes that don't work (based on my limited testing). Most
111/// attributes are very simple in practice. Until we find a bug, I don't see
112/// a pressing need to implement the 2 token lookahead.
113
John McCall7f040a92010-12-24 02:08:15 +0000114void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000115 SourceLocation *endLoc,
116 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000117 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Chris Lattner04d66662007-10-09 17:33:22 +0000119 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 ConsumeToken();
121 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
122 "attribute")) {
123 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000124 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 }
126 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
127 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000128 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 }
130 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000131 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
132 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000133 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
135 ConsumeToken();
136 continue;
137 }
138 // we have an identifier or declaration specifier (const, int, etc.)
139 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
140 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000142 if (Tok.is(tok::l_paren)) {
143 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000144 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000145 LateParsedAttribute *LA =
146 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
147 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000148
Bill Wendlingad017fa2012-12-20 19:22:21 +0000149 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000150 // with other late-parsed declarations.
DeLesley Hutchins161db022012-11-02 21:44:32 +0000151 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000152 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000154 // consume everything up to and including the matching right parens
155 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000157 Token Eof;
158 Eof.startToken();
159 Eof.setLocation(Tok.getLocation());
160 LA->Toks.push_back(Eof);
161 } else {
Michael Han6880f492012-10-03 01:56:22 +0000162 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000163 0, SourceLocation(), AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 }
165 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000166 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000167 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 }
169 }
170 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000172 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000173 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
174 SkipUntil(tok::r_paren, false);
175 }
John McCall7f040a92010-12-24 02:08:15 +0000176 if (endLoc)
177 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000179}
180
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000181
Michael Han6880f492012-10-03 01:56:22 +0000182/// Parse the arguments to a parameterized GNU attribute or
183/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000184void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
185 SourceLocation AttrNameLoc,
186 ParsedAttributes &Attrs,
Michael Han6880f492012-10-03 01:56:22 +0000187 SourceLocation *EndLoc,
188 IdentifierInfo *ScopeName,
189 SourceLocation ScopeLoc,
190 AttributeList::Syntax Syntax) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000191
192 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
193
194 // Availability attributes have their own grammar.
195 if (AttrName->isStr("availability")) {
196 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
197 return;
198 }
199 // Thread safety attributes fit into the FIXME case above, so we
200 // just parse the arguments as a list of expressions
201 if (IsThreadSafetyAttribute(AttrName->getName())) {
202 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
203 return;
204 }
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000205 // Type safety attributes have their own grammar.
206 if (AttrName->isStr("type_tag_for_datatype")) {
207 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
208 return;
209 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000210
211 ConsumeParen(); // ignore the left paren loc for now
212
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000213 IdentifierInfo *ParmName = 0;
214 SourceLocation ParmLoc;
215 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000216
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000217 switch (Tok.getKind()) {
218 case tok::kw_char:
219 case tok::kw_wchar_t:
220 case tok::kw_char16_t:
221 case tok::kw_char32_t:
222 case tok::kw_bool:
223 case tok::kw_short:
224 case tok::kw_int:
225 case tok::kw_long:
226 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000227 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000228 case tok::kw_signed:
229 case tok::kw_unsigned:
230 case tok::kw_float:
231 case tok::kw_double:
232 case tok::kw_void:
233 case tok::kw_typeof:
234 // __attribute__(( vec_type_hint(char) ))
235 // FIXME: Don't just discard the builtin type token.
236 ConsumeToken();
237 BuiltinType = true;
238 break;
239
240 case tok::identifier:
241 ParmName = Tok.getIdentifierInfo();
242 ParmLoc = ConsumeToken();
243 break;
244
245 default:
246 break;
247 }
248
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +0000249 ExprVector ArgExprs;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000250
251 if (!BuiltinType &&
252 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
253 // Eat the comma.
254 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000255 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000256
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000257 // Parse the non-empty comma-separated list of expressions.
258 while (1) {
259 ExprResult ArgExpr(ParseAssignmentExpression());
260 if (ArgExpr.isInvalid()) {
261 SkipUntil(tok::r_paren);
262 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000263 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000264 ArgExprs.push_back(ArgExpr.release());
265 if (Tok.isNot(tok::comma))
266 break;
267 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000268 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000269 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000270 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
271 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
272 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000273 while (Tok.is(tok::identifier)) {
274 ConsumeToken();
275 if (Tok.is(tok::greater))
276 break;
277 if (Tok.is(tok::comma)) {
278 ConsumeToken();
279 continue;
280 }
281 }
282 if (Tok.isNot(tok::greater))
283 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000284 SkipUntil(tok::r_paren, false, true); // skip until ')'
285 }
286 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000287
288 SourceLocation RParen = Tok.getLocation();
289 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
Michael Han45bed132012-10-04 16:42:52 +0000290 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000291 AttributeList *attr =
Michael Han45bed132012-10-04 16:42:52 +0000292 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen),
Michael Han6880f492012-10-03 01:56:22 +0000293 ScopeName, ScopeLoc, ParmName, ParmLoc,
294 ArgExprs.data(), ArgExprs.size(), Syntax);
Sean Hunt8e083e72012-06-19 23:57:03 +0000295 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000296 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000297 }
298}
299
Chad Rosier8decdee2012-06-26 22:30:43 +0000300/// \brief Parses a single argument for a declspec, including the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000301/// surrounding parens.
Chad Rosier8decdee2012-06-26 22:30:43 +0000302void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000303 SourceLocation AttrNameLoc,
304 ParsedAttributes &Attrs)
305{
306 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000307 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000308 AttrName->getNameStart(), tok::r_paren))
309 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000310
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000311 ExprResult ArgExpr(ParseConstantExpression());
312 if (ArgExpr.isInvalid()) {
313 T.skipToEnd();
314 return;
315 }
316 Expr *ExprList = ArgExpr.take();
Chad Rosier8decdee2012-06-26 22:30:43 +0000317 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000318 &ExprList, 1, AttributeList::AS_Declspec);
319
320 T.consumeClose();
321}
322
Chad Rosier8decdee2012-06-26 22:30:43 +0000323/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000324/// arguments.
325bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
326 return llvm::StringSwitch<bool>(Ident->getName())
327 .Case("dllimport", true)
328 .Case("dllexport", true)
329 .Case("noreturn", true)
330 .Case("nothrow", true)
331 .Case("noinline", true)
332 .Case("naked", true)
333 .Case("appdomain", true)
334 .Case("process", true)
335 .Case("jitintrinsic", true)
336 .Case("noalias", true)
337 .Case("restrict", true)
338 .Case("novtable", true)
339 .Case("selectany", true)
340 .Case("thread", true)
341 .Default(false);
342}
343
Chad Rosier8decdee2012-06-26 22:30:43 +0000344/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000345/// parameters). Will return false if we properly handled the declspec, or
346/// true if it is an unknown declspec.
Chad Rosier8decdee2012-06-26 22:30:43 +0000347void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000348 SourceLocation Loc,
349 ParsedAttributes &Attrs) {
350 // Try to handle the easy case first -- these declspecs all take a single
351 // parameter as their argument.
352 if (llvm::StringSwitch<bool>(Ident->getName())
353 .Case("uuid", true)
354 .Case("align", true)
355 .Case("allocate", true)
356 .Default(false)) {
357 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
358 } else if (Ident->getName() == "deprecated") {
Chad Rosier8decdee2012-06-26 22:30:43 +0000359 // The deprecated declspec has an optional single argument, so we will
360 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000361 // not.
362 if (Tok.getKind() == tok::l_paren)
363 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
364 else
Chad Rosier8decdee2012-06-26 22:30:43 +0000365 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000366 AttributeList::AS_Declspec);
367 } else if (Ident->getName() == "property") {
368 // The property declspec is more complex in that it can take one or two
Chad Rosier8decdee2012-06-26 22:30:43 +0000369 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000370 // must be named get or put.
371 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000372 // For right now, we will just skip to the closing right paren of the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000373 // property expression.
374 //
375 // FIXME: we should deal with __declspec(property) at some point because it
376 // is used in the platform SDK headers for the Parallel Patterns Library
377 // and ATL.
378 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000379 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000380 Ident->getNameStart(), tok::r_paren))
381 return;
382 T.skipToEnd();
383 } else {
384 // We don't recognize this as a valid declspec, but instead of creating the
385 // attribute and allowing sema to warn about it, we will warn here instead.
386 // This is because some attributes have multiple spellings, but we need to
387 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosier8decdee2012-06-26 22:30:43 +0000388 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000389 // both locations.
390 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
391
392 // If there's an open paren, we should eat the open and close parens under
393 // the assumption that this unknown declspec has parameters.
394 BalancedDelimiterTracker T(*this, tok::l_paren);
395 if (!T.consumeOpen())
396 T.skipToEnd();
397 }
398}
399
Eli Friedmana23b4852009-06-08 07:21:15 +0000400/// [MS] decl-specifier:
401/// __declspec ( extended-decl-modifier-seq )
402///
403/// [MS] extended-decl-modifier-seq:
404/// extended-decl-modifier[opt]
405/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000406void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000407 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000408
Steve Narofff59e17e2008-12-24 20:59:21 +0000409 ConsumeToken();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000410 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000411 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000412 tok::r_paren))
John McCall7f040a92010-12-24 02:08:15 +0000413 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000414
Chad Rosier8decdee2012-06-26 22:30:43 +0000415 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000416 // you can specify multiple attributes per declspec.
417 while (Tok.getKind() != tok::r_paren) {
418 // We expect either a well-known identifier or a generic string. Anything
419 // else is a malformed declspec.
420 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosier8decdee2012-06-26 22:30:43 +0000421 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000422 Tok.getKind() != tok::kw_restrict) {
423 Diag(Tok, diag::err_ms_declspec_type);
424 T.skipToEnd();
425 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000426 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000427
428 IdentifierInfo *AttrName;
429 SourceLocation AttrNameLoc;
430 if (IsString) {
431 SmallString<8> StrBuffer;
432 bool Invalid = false;
433 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
434 if (Invalid) {
435 T.skipToEnd();
436 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000437 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000438 AttrName = PP.getIdentifierInfo(Str);
439 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000440 } else {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000441 AttrName = Tok.getIdentifierInfo();
442 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000443 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000444
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000445 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosier8decdee2012-06-26 22:30:43 +0000446 // If we have a generic string, we will allow it because there is no
447 // documented list of allowable string declspecs, but we know they exist
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000448 // (for instance, SAL declspecs in older versions of MSVC).
449 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000450 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000451 // arguments and can be turned into an attribute directly.
Chad Rosier8decdee2012-06-26 22:30:43 +0000452 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000453 0, 0, AttributeList::AS_Declspec);
454 else
455 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000456 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000457 T.consumeClose();
Eli Friedman290eeb02009-06-08 23:27:34 +0000458}
459
John McCall7f040a92010-12-24 02:08:15 +0000460void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000461 // Treat these like attributes
Eli Friedman290eeb02009-06-08 23:27:34 +0000462 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000463 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000464 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Chad Rosierccbb4022012-12-21 21:27:13 +0000465 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000466 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
467 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000468 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith5cd532c2013-01-29 01:24:26 +0000469 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Eli Friedman290eeb02009-06-08 23:27:34 +0000470 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000471}
472
John McCall7f040a92010-12-24 02:08:15 +0000473void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000474 // Treat these like attributes
475 while (Tok.is(tok::kw___pascal)) {
476 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
477 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000478 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith5cd532c2013-01-29 01:24:26 +0000479 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000480 }
John McCall7f040a92010-12-24 02:08:15 +0000481}
482
Peter Collingbournef315fa82011-02-14 01:42:53 +0000483void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
484 // Treat these like attributes
485 while (Tok.is(tok::kw___kernel)) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000486 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbournef315fa82011-02-14 01:42:53 +0000487 SourceLocation AttrNameLoc = ConsumeToken();
Richard Smith5cd532c2013-01-29 01:24:26 +0000488 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
489 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000490 }
491}
492
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000493void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000494 // FIXME: The mapping from attribute spelling to semantics should be
495 // performed in Sema, not here.
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000496 SourceLocation Loc = Tok.getLocation();
497 switch(Tok.getKind()) {
498 // OpenCL qualifiers:
499 case tok::kw___private:
Chad Rosier8decdee2012-06-26 22:30:43 +0000500 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000501 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000502 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000503 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000504 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000505
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000506 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000507 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000508 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000509 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000510 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000511
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000512 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000513 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000514 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000515 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000516 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000517
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000518 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000519 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000520 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000521 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000522 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000523
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000524 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000525 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000526 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000527 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000528 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000529
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000530 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000531 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000532 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000533 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000534 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000535
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000536 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000537 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000538 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000539 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000540 break;
541 default: break;
542 }
543}
544
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000545/// \brief Parse a version number.
546///
547/// version:
548/// simple-integer
549/// simple-integer ',' simple-integer
550/// simple-integer ',' simple-integer ',' simple-integer
551VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
552 Range = Tok.getLocation();
553
554 if (!Tok.is(tok::numeric_constant)) {
555 Diag(Tok, diag::err_expected_version);
556 SkipUntil(tok::comma, tok::r_paren, true, true, true);
557 return VersionTuple();
558 }
559
560 // Parse the major (and possibly minor and subminor) versions, which
561 // are stored in the numeric constant. We utilize a quirk of the
562 // lexer, which is that it handles something like 1.2.3 as a single
563 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000564 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000565 Buffer.resize(Tok.getLength()+1);
566 const char *ThisTokBegin = &Buffer[0];
567
568 // Get the spelling of the token, which eliminates trigraphs, etc.
569 bool Invalid = false;
570 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
571 if (Invalid)
572 return VersionTuple();
573
574 // Parse the major version.
575 unsigned AfterMajor = 0;
576 unsigned Major = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000577 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000578 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
579 ++AfterMajor;
580 }
581
582 if (AfterMajor == 0) {
583 Diag(Tok, diag::err_expected_version);
584 SkipUntil(tok::comma, tok::r_paren, true, true, true);
585 return VersionTuple();
586 }
587
588 if (AfterMajor == ActualLength) {
589 ConsumeToken();
590
591 // We only had a single version component.
592 if (Major == 0) {
593 Diag(Tok, diag::err_zero_version);
594 return VersionTuple();
595 }
596
597 return VersionTuple(Major);
598 }
599
600 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
601 Diag(Tok, diag::err_expected_version);
602 SkipUntil(tok::comma, tok::r_paren, true, true, true);
603 return VersionTuple();
604 }
605
606 // Parse the minor version.
607 unsigned AfterMinor = AfterMajor + 1;
608 unsigned Minor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000609 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000610 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
611 ++AfterMinor;
612 }
613
614 if (AfterMinor == ActualLength) {
615 ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +0000616
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000617 // We had major.minor.
618 if (Major == 0 && Minor == 0) {
619 Diag(Tok, diag::err_zero_version);
620 return VersionTuple();
621 }
622
Chad Rosier8decdee2012-06-26 22:30:43 +0000623 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000624 }
625
626 // If what follows is not a '.', we have a problem.
627 if (ThisTokBegin[AfterMinor] != '.') {
628 Diag(Tok, diag::err_expected_version);
629 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosier8decdee2012-06-26 22:30:43 +0000630 return VersionTuple();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000631 }
632
633 // Parse the subminor version.
634 unsigned AfterSubminor = AfterMinor + 1;
635 unsigned Subminor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000636 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000637 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
638 ++AfterSubminor;
639 }
640
641 if (AfterSubminor != ActualLength) {
642 Diag(Tok, diag::err_expected_version);
643 SkipUntil(tok::comma, tok::r_paren, true, true, true);
644 return VersionTuple();
645 }
646 ConsumeToken();
647 return VersionTuple(Major, Minor, Subminor);
648}
649
650/// \brief Parse the contents of the "availability" attribute.
651///
652/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000653/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000654///
655/// platform:
656/// identifier
657///
658/// version-arg-list:
659/// version-arg
660/// version-arg ',' version-arg-list
661///
662/// version-arg:
663/// 'introduced' '=' version
664/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000665/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000666/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000667/// opt-message:
668/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000669void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
670 SourceLocation AvailabilityLoc,
671 ParsedAttributes &attrs,
672 SourceLocation *endLoc) {
673 SourceLocation PlatformLoc;
674 IdentifierInfo *Platform = 0;
675
676 enum { Introduced, Deprecated, Obsoleted, Unknown };
677 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000678 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000679
680 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000681 BalancedDelimiterTracker T(*this, tok::l_paren);
682 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000683 Diag(Tok, diag::err_expected_lparen);
684 return;
685 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000686
687 // Parse the platform name,
688 if (Tok.isNot(tok::identifier)) {
689 Diag(Tok, diag::err_availability_expected_platform);
690 SkipUntil(tok::r_paren);
691 return;
692 }
693 Platform = Tok.getIdentifierInfo();
694 PlatformLoc = ConsumeToken();
695
696 // Parse the ',' following the platform name.
697 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
698 return;
699
700 // If we haven't grabbed the pointers for the identifiers
701 // "introduced", "deprecated", and "obsoleted", do so now.
702 if (!Ident_introduced) {
703 Ident_introduced = PP.getIdentifierInfo("introduced");
704 Ident_deprecated = PP.getIdentifierInfo("deprecated");
705 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000706 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000707 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000708 }
709
710 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000711 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000712 do {
713 if (Tok.isNot(tok::identifier)) {
714 Diag(Tok, diag::err_availability_expected_change);
715 SkipUntil(tok::r_paren);
716 return;
717 }
718 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
719 SourceLocation KeywordLoc = ConsumeToken();
720
Douglas Gregorb53e4172011-03-26 03:35:55 +0000721 if (Keyword == Ident_unavailable) {
722 if (UnavailableLoc.isValid()) {
723 Diag(KeywordLoc, diag::err_availability_redundant)
724 << Keyword << SourceRange(UnavailableLoc);
Chad Rosier8decdee2012-06-26 22:30:43 +0000725 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000726 UnavailableLoc = KeywordLoc;
727
728 if (Tok.isNot(tok::comma))
729 break;
730
731 ConsumeToken();
732 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000733 }
734
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000735 if (Tok.isNot(tok::equal)) {
736 Diag(Tok, diag::err_expected_equal_after)
737 << Keyword;
738 SkipUntil(tok::r_paren);
739 return;
740 }
741 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000742 if (Keyword == Ident_message) {
743 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000744 Diag(Tok, diag::err_expected_string_literal)
745 << /*Source='availability attribute'*/2;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000746 SkipUntil(tok::r_paren);
747 return;
748 }
749 MessageExpr = ParseStringLiteralExpression();
750 break;
751 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000752
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000753 SourceRange VersionRange;
754 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000755
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000756 if (Version.empty()) {
757 SkipUntil(tok::r_paren);
758 return;
759 }
760
761 unsigned Index;
762 if (Keyword == Ident_introduced)
763 Index = Introduced;
764 else if (Keyword == Ident_deprecated)
765 Index = Deprecated;
766 else if (Keyword == Ident_obsoleted)
767 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000768 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000769 Index = Unknown;
770
771 if (Index < Unknown) {
772 if (!Changes[Index].KeywordLoc.isInvalid()) {
773 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000774 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000775 << SourceRange(Changes[Index].KeywordLoc,
776 Changes[Index].VersionRange.getEnd());
777 }
778
779 Changes[Index].KeywordLoc = KeywordLoc;
780 Changes[Index].Version = Version;
781 Changes[Index].VersionRange = VersionRange;
782 } else {
783 Diag(KeywordLoc, diag::err_availability_unknown_change)
784 << Keyword << VersionRange;
785 }
786
787 if (Tok.isNot(tok::comma))
788 break;
789
790 ConsumeToken();
791 } while (true);
792
793 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000794 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000795 return;
796
797 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000798 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000799
Douglas Gregorb53e4172011-03-26 03:35:55 +0000800 // The 'unavailable' availability cannot be combined with any other
801 // availability changes. Make sure that hasn't happened.
802 if (UnavailableLoc.isValid()) {
803 bool Complained = false;
804 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
805 if (Changes[Index].KeywordLoc.isValid()) {
806 if (!Complained) {
807 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
808 << SourceRange(Changes[Index].KeywordLoc,
809 Changes[Index].VersionRange.getEnd());
810 Complained = true;
811 }
812
813 // Clear out the availability.
814 Changes[Index] = AvailabilityChange();
815 }
816 }
817 }
818
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000819 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000820 attrs.addNew(&Availability,
821 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000822 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000823 Platform, PlatformLoc,
824 Changes[Introduced],
825 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000826 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000827 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000828 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000829}
830
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000831
Bill Wendlingad017fa2012-12-20 19:22:21 +0000832// Late Parsed Attributes:
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000833// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
834
835void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
836
837void Parser::LateParsedClass::ParseLexedAttributes() {
838 Self->ParseLexedAttributes(*Class);
839}
840
841void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000842 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000843}
844
845/// Wrapper class which calls ParseLexedAttribute, after setting up the
846/// scope appropriately.
847void Parser::ParseLexedAttributes(ParsingClass &Class) {
848 // Deal with templates
849 // FIXME: Test cases to make sure this does the right thing for templates.
850 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
851 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
852 HasTemplateScope);
853 if (HasTemplateScope)
854 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
855
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000856 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000857 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000858 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000859 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
860 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
861
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000862 // Enter the scope of nested classes
863 if (!AlreadyHasClassScope)
864 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
865 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000866 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000867 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
868 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
869 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000870 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000871
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000872 if (!AlreadyHasClassScope)
873 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
874 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000875}
876
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000877
878/// \brief Parse all attributes in LAs, and attach them to Decl D.
879void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
880 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins161db022012-11-02 21:44:32 +0000881 assert(LAs.parseSoon() &&
882 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000883 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +0000884 if (D)
885 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000886 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000887 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000888 }
889 LAs.clear();
890}
891
892
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000893/// \brief Finish parsing an attribute for which parsing was delayed.
894/// This will be called at the end of parsing a class declaration
895/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +0000896/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000897/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000898void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
899 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000900 // Save the current token position.
901 SourceLocation OrigLoc = Tok.getLocation();
902
903 // Append the current token at the end of the new token stream so that it
904 // doesn't get lost.
905 LA.Toks.push_back(Tok);
906 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
907 // Consume the previously pushed token.
908 ConsumeAnyToken();
909
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000910 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smithcd8ab512013-01-17 01:30:42 +0000911 // FIXME: Do not warn on C++11 attributes, once we start supporting
912 // them here.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000913 Diag(Tok, diag::warn_attribute_on_function_definition)
914 << LA.AttrName.getName();
915 }
916
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000917 ParsedAttributes Attrs(AttrFactory);
918 SourceLocation endLoc;
919
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000920 if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000921 Decl *D = LA.Decls[0];
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000922 NamedDecl *ND = dyn_cast<NamedDecl>(D);
923 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000924
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000925 // Allow 'this' within late-parsed attributes.
926 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
927 /*TypeQuals=*/0,
928 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000929
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000930 if (LA.Decls.size() == 1) {
931 // If the Decl is templatized, add template parameters to scope.
932 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
933 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
934 if (HasTemplateScope)
935 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000936
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000937 // If the Decl is on a function, add function parameters to the scope.
938 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
939 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
940 if (HasFunScope)
941 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000942
Michael Han6880f492012-10-03 01:56:22 +0000943 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000944 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000945
946 if (HasFunScope) {
947 Actions.ActOnExitFunctionContext();
948 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
949 }
950 if (HasTemplateScope) {
951 TempScope.Exit();
952 }
953 } else {
954 // If there are multiple decls, then the decl cannot be within the
955 // function scope.
Michael Han6880f492012-10-03 01:56:22 +0000956 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000957 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000958 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000959 } else {
960 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000961 }
962
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000963 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
964 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
965 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000966
967 if (Tok.getLocation() != OrigLoc) {
968 // Due to a parsing error, we either went over the cached tokens or
969 // there are still cached tokens left, so we skip the leftover tokens.
970 // Since this is an uncommon situation that should be avoided, use the
971 // expensive isBeforeInTranslationUnit call.
972 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
973 OrigLoc))
974 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000975 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000976 }
977}
978
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000979/// \brief Wrapper around a case statement checking if AttrName is
980/// one of the thread safety attributes
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000981bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000982 return llvm::StringSwitch<bool>(AttrName)
983 .Case("guarded_by", true)
984 .Case("guarded_var", true)
985 .Case("pt_guarded_by", true)
986 .Case("pt_guarded_var", true)
987 .Case("lockable", true)
988 .Case("scoped_lockable", true)
989 .Case("no_thread_safety_analysis", true)
990 .Case("acquired_after", true)
991 .Case("acquired_before", true)
992 .Case("exclusive_lock_function", true)
993 .Case("shared_lock_function", true)
994 .Case("exclusive_trylock_function", true)
995 .Case("shared_trylock_function", true)
996 .Case("unlock_function", true)
997 .Case("lock_returned", true)
998 .Case("locks_excluded", true)
999 .Case("exclusive_locks_required", true)
1000 .Case("shared_locks_required", true)
1001 .Default(false);
1002}
1003
1004/// \brief Parse the contents of thread safety attributes. These
1005/// should always be parsed as an expression list.
1006///
1007/// We need to special case the parsing due to the fact that if the first token
1008/// of the first argument is an identifier, the main parse loop will store
1009/// that token as a "parameter" and the rest of
1010/// the arguments will be added to a list of "arguments". However,
1011/// subsequent tokens in the first argument are lost. We instead parse each
1012/// argument as an expression and add all arguments to the list of "arguments".
1013/// In future, we will take advantage of this special case to also
1014/// deal with some argument scoping issues here (for example, referring to a
1015/// function parameter in the attribute on that function).
1016void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1017 SourceLocation AttrNameLoc,
1018 ParsedAttributes &Attrs,
1019 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001020 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001021
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001022 BalancedDelimiterTracker T(*this, tok::l_paren);
1023 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +00001024
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001025 ExprVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001026 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00001027
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001028 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001029 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinsed4330b2013-02-07 19:01:07 +00001030 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001031 ExprResult ArgExpr(ParseAssignmentExpression());
1032 if (ArgExpr.isInvalid()) {
1033 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001034 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001035 break;
1036 } else {
1037 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001038 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001039 if (Tok.isNot(tok::comma))
1040 break;
1041 ConsumeToken(); // Eat the comma, move to the next argument
1042 }
1043 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001044 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001045 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001046 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001047 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001048 if (EndLoc)
1049 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001050}
1051
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001052void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1053 SourceLocation AttrNameLoc,
1054 ParsedAttributes &Attrs,
1055 SourceLocation *EndLoc) {
1056 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1057
1058 BalancedDelimiterTracker T(*this, tok::l_paren);
1059 T.consumeOpen();
1060
1061 if (Tok.isNot(tok::identifier)) {
1062 Diag(Tok, diag::err_expected_ident);
1063 T.skipToEnd();
1064 return;
1065 }
1066 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1067 SourceLocation ArgumentKindLoc = ConsumeToken();
1068
1069 if (Tok.isNot(tok::comma)) {
1070 Diag(Tok, diag::err_expected_comma);
1071 T.skipToEnd();
1072 return;
1073 }
1074 ConsumeToken();
1075
1076 SourceRange MatchingCTypeRange;
1077 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1078 if (MatchingCType.isInvalid()) {
1079 T.skipToEnd();
1080 return;
1081 }
1082
1083 bool LayoutCompatible = false;
1084 bool MustBeNull = false;
1085 while (Tok.is(tok::comma)) {
1086 ConsumeToken();
1087 if (Tok.isNot(tok::identifier)) {
1088 Diag(Tok, diag::err_expected_ident);
1089 T.skipToEnd();
1090 return;
1091 }
1092 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1093 if (Flag->isStr("layout_compatible"))
1094 LayoutCompatible = true;
1095 else if (Flag->isStr("must_be_null"))
1096 MustBeNull = true;
1097 else {
1098 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1099 T.skipToEnd();
1100 return;
1101 }
1102 ConsumeToken(); // consume flag
1103 }
1104
1105 if (!T.consumeClose()) {
1106 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1107 ArgumentKind, ArgumentKindLoc,
1108 MatchingCType.release(), LayoutCompatible,
1109 MustBeNull, AttributeList::AS_GNU);
1110 }
1111
1112 if (EndLoc)
1113 *EndLoc = T.getCloseLocation();
1114}
1115
Richard Smith6ee326a2012-04-10 01:32:12 +00001116/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1117/// of a C++11 attribute-specifier in a location where an attribute is not
1118/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1119/// situation.
1120///
1121/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1122/// this doesn't appear to actually be an attribute-specifier, and the caller
1123/// should try to parse it.
1124bool Parser::DiagnoseProhibitedCXX11Attribute() {
1125 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1126
1127 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1128 case CAK_NotAttributeSpecifier:
1129 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1130 return false;
1131
1132 case CAK_InvalidAttributeSpecifier:
1133 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1134 return false;
1135
1136 case CAK_AttributeSpecifier:
1137 // Parse and discard the attributes.
1138 SourceLocation BeginLoc = ConsumeBracket();
1139 ConsumeBracket();
1140 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1141 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1142 SourceLocation EndLoc = ConsumeBracket();
1143 Diag(BeginLoc, diag::err_attributes_not_allowed)
1144 << SourceRange(BeginLoc, EndLoc);
1145 return true;
1146 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001147 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001148}
1149
Richard Smith975d52c2013-02-20 01:17:14 +00001150/// \brief We have found the opening square brackets of a C++11
1151/// attribute-specifier in a location where an attribute is not permitted, but
1152/// we know where the attributes ought to be written. Parse them anyway, and
1153/// provide a fixit moving them to the right place.
Richard Smith05321402013-02-19 23:47:15 +00001154void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1155 SourceLocation CorrectLocation) {
1156 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1157 Tok.is(tok::kw_alignas));
1158
1159 // Consume the attributes.
1160 SourceLocation Loc = Tok.getLocation();
1161 ParseCXX11Attributes(Attrs);
1162 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1163
1164 Diag(Loc, diag::err_attributes_not_allowed)
1165 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1166 << FixItHint::CreateRemoval(AttrRange);
1167}
1168
John McCall7f040a92010-12-24 02:08:15 +00001169void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1170 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1171 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001172}
1173
Michael Hanf64231e2012-11-06 19:34:54 +00001174void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1175 AttributeList *AttrList = attrs.getList();
1176 while (AttrList) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001177 if (AttrList->isCXX11Attribute()) {
Richard Smithd03de6a2013-01-29 10:02:16 +00001178 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Hanf64231e2012-11-06 19:34:54 +00001179 << AttrList->getName();
1180 AttrList->setInvalid();
1181 }
1182 AttrList = AttrList->getNext();
1183 }
1184}
1185
Reid Spencer5f016e22007-07-11 17:01:13 +00001186/// ParseDeclaration - Parse a full 'declaration', which consists of
1187/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001188/// 'Context' should be a Declarator::TheContext value. This returns the
1189/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001190///
1191/// declaration: [C99 6.7]
1192/// block-declaration ->
1193/// simple-declaration
1194/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001195/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001196/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001197/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001198/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001199/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001200/// others... [FIXME]
1201///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001202Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1203 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001204 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001205 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001206 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001207 // Must temporarily exit the objective-c container scope for
1208 // parsing c none objective-c decls.
1209 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001210
John McCalld226f652010-08-21 09:40:31 +00001211 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001212 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001213 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001214 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001215 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001216 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001217 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001218 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001219 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001220 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001221 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001222 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001223 SourceLocation InlineLoc = ConsumeToken();
1224 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1225 break;
1226 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001227 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001228 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001229 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001230 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001231 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001232 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001233 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001234 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001235 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001236 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001237 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001238 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001239 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001240 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001241 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001242 default:
John McCall7f040a92010-12-24 02:08:15 +00001243 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001244 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001245
Chris Lattner682bf922009-03-29 16:50:03 +00001246 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001247 // single decl, convert it now. Alias declarations can also declare a type;
1248 // include that too if it is present.
1249 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001250}
1251
1252/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1253/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001254/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1255/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001256///[C90/C++]init-declarator-list ';' [TODO]
1257/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001258///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001259/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001260/// attribute-specifier-seq[opt] type-specifier-seq declarator
1261///
Chris Lattnercd147752009-03-29 17:27:48 +00001262/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001263/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001264///
1265/// If FRI is non-null, we might be parsing a for-range-declaration instead
1266/// of a simple-declaration. If we find that we are, we also parse the
1267/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001268Parser::DeclGroupPtrTy
1269Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1270 SourceLocation &DeclEnd,
1271 ParsedAttributesWithRange &attrs,
1272 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001274 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001275 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001276
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001277 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001278 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001279
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1281 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001282 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001283 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001284 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001285 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001286 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001287 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001288 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001289 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001290
1291 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001292}
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Richard Smith0706df42011-10-19 21:33:05 +00001294/// Returns true if this might be the start of a declarator, or a common typo
1295/// for a declarator.
1296bool Parser::MightBeDeclarator(unsigned Context) {
1297 switch (Tok.getKind()) {
1298 case tok::annot_cxxscope:
1299 case tok::annot_template_id:
1300 case tok::caret:
1301 case tok::code_completion:
1302 case tok::coloncolon:
1303 case tok::ellipsis:
1304 case tok::kw___attribute:
1305 case tok::kw_operator:
1306 case tok::l_paren:
1307 case tok::star:
1308 return true;
1309
1310 case tok::amp:
1311 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001312 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001313
Richard Smith1c94c162012-01-09 22:31:44 +00001314 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith80ad52f2013-01-02 11:42:31 +00001315 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smith1c94c162012-01-09 22:31:44 +00001316 NextToken().is(tok::l_square);
1317
1318 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001319 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001320
Richard Smith0706df42011-10-19 21:33:05 +00001321 case tok::identifier:
1322 switch (NextToken().getKind()) {
1323 case tok::code_completion:
1324 case tok::coloncolon:
1325 case tok::comma:
1326 case tok::equal:
1327 case tok::equalequal: // Might be a typo for '='.
1328 case tok::kw_alignas:
1329 case tok::kw_asm:
1330 case tok::kw___attribute:
1331 case tok::l_brace:
1332 case tok::l_paren:
1333 case tok::l_square:
1334 case tok::less:
1335 case tok::r_brace:
1336 case tok::r_paren:
1337 case tok::r_square:
1338 case tok::semi:
1339 return true;
1340
1341 case tok::colon:
1342 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001343 // and in block scope it's probably a label. Inside a class definition,
1344 // this is a bit-field.
1345 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001346 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001347
1348 case tok::identifier: // Possible virt-specifier.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001349 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001350
1351 default:
1352 return false;
1353 }
1354
1355 default:
1356 return false;
1357 }
1358}
1359
Richard Smith994d73f2012-04-11 20:59:20 +00001360/// Skip until we reach something which seems like a sensible place to pick
1361/// up parsing after a malformed declaration. This will sometimes stop sooner
1362/// than SkipUntil(tok::r_brace) would, but will never stop later.
1363void Parser::SkipMalformedDecl() {
1364 while (true) {
1365 switch (Tok.getKind()) {
1366 case tok::l_brace:
1367 // Skip until matching }, then stop. We've probably skipped over
1368 // a malformed class or function definition or similar.
1369 ConsumeBrace();
1370 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1371 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1372 // This declaration isn't over yet. Keep skipping.
1373 continue;
1374 }
1375 if (Tok.is(tok::semi))
1376 ConsumeToken();
1377 return;
1378
1379 case tok::l_square:
1380 ConsumeBracket();
1381 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1382 continue;
1383
1384 case tok::l_paren:
1385 ConsumeParen();
1386 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1387 continue;
1388
1389 case tok::r_brace:
1390 return;
1391
1392 case tok::semi:
1393 ConsumeToken();
1394 return;
1395
1396 case tok::kw_inline:
1397 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001398 // a good place to pick back up parsing, except in an Objective-C
1399 // @interface context.
1400 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1401 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001402 return;
1403 break;
1404
1405 case tok::kw_namespace:
1406 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001407 // place to pick back up parsing, except in an Objective-C
1408 // @interface context.
1409 if (Tok.isAtStartOfLine() &&
1410 (!ParsingInObjCContainer || CurParsedObjCImpl))
1411 return;
1412 break;
1413
1414 case tok::at:
1415 // @end is very much like } in Objective-C contexts.
1416 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1417 ParsingInObjCContainer)
1418 return;
1419 break;
1420
1421 case tok::minus:
1422 case tok::plus:
1423 // - and + probably start new method declarations in Objective-C contexts.
1424 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001425 return;
1426 break;
1427
1428 case tok::eof:
1429 return;
1430
1431 default:
1432 break;
1433 }
1434
1435 ConsumeAnyToken();
1436 }
1437}
1438
John McCalld8ac0572009-11-03 19:26:08 +00001439/// ParseDeclGroup - Having concluded that this is either a function
1440/// definition or a group of object declarations, actually parse the
1441/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001442Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1443 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001444 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001445 SourceLocation *DeclEnd,
1446 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001447 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001448 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001449 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001450
John McCalld8ac0572009-11-03 19:26:08 +00001451 // Bail out if the first declarator didn't seem well-formed.
1452 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001453 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001454 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001455 }
Mike Stump1eb44332009-09-09 15:08:12 +00001456
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001457 // Save late-parsed attributes for now; they need to be parsed in the
1458 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001459 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1460 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001461 if (D.isFunctionDeclarator())
1462 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1463
Chris Lattnerc82daef2010-07-11 22:24:20 +00001464 // Check to see if we have a function *definition* which must have a body.
1465 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1466 // Look at the next token to make sure that this isn't a function
1467 // declaration. We have to check this because __attribute__ might be the
1468 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001469 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001470
Chris Lattner004659a2010-07-11 22:42:07 +00001471 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001472 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1473 Diag(Tok, diag::err_function_declared_typedef);
1474
1475 // Recover by treating the 'typedef' as spurious.
1476 DS.ClearStorageClassSpecs();
1477 }
1478
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001479 Decl *TheDecl =
1480 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001481 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001482 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001483
Chris Lattner004659a2010-07-11 22:42:07 +00001484 if (isDeclarationSpecifier()) {
1485 // If there is an invalid declaration specifier right after the function
1486 // prototype, then we must be in a missing semicolon case where this isn't
1487 // actually a body. Just fall through into the code that handles it as a
1488 // prototype, and let the top-level code handle the erroneous declspec
1489 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001490 } else {
1491 Diag(Tok, diag::err_expected_fn_body);
1492 SkipUntil(tok::semi);
1493 return DeclGroupPtrTy();
1494 }
1495 }
1496
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001497 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001498 return DeclGroupPtrTy();
1499
1500 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1501 // must parse and analyze the for-range-initializer before the declaration is
1502 // analyzed.
1503 if (FRI && Tok.is(tok::colon)) {
1504 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001505 if (Tok.is(tok::l_brace))
1506 FRI->RangeExpr = ParseBraceInitializer();
1507 else
1508 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001509 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1510 Actions.ActOnCXXForRangeDecl(ThisDecl);
1511 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001512 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001513 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1514 }
1515
Chris Lattner5f9e2722011-07-23 10:55:15 +00001516 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001517 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001518 if (LateParsedAttrs.size() > 0)
1519 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001520 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001521 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001522 DeclsInGroup.push_back(FirstDecl);
1523
Richard Smith0706df42011-10-19 21:33:05 +00001524 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001525
John McCalld8ac0572009-11-03 19:26:08 +00001526 // If we don't have a comma, it is either the end of the list (a ';') or an
1527 // error, bail out.
1528 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001529 SourceLocation CommaLoc = ConsumeToken();
1530
1531 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1532 // This comma was followed by a line-break and something which can't be
1533 // the start of a declarator. The comma was probably a typo for a
1534 // semicolon.
1535 Diag(CommaLoc, diag::err_expected_semi_declaration)
1536 << FixItHint::CreateReplacement(CommaLoc, ";");
1537 ExpectSemi = false;
1538 break;
1539 }
John McCalld8ac0572009-11-03 19:26:08 +00001540
1541 // Parse the next declarator.
1542 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001543 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001544
1545 // Accept attributes in an init-declarator. In the first declarator in a
1546 // declaration, these would be part of the declspec. In subsequent
1547 // declarators, they become part of the declarator itself, so that they
1548 // don't apply to declarators after *this* one. Examples:
1549 // short __attribute__((common)) var; -> declspec
1550 // short var __attribute__((common)); -> declarator
1551 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001552 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001553
1554 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001555 if (!D.isInvalidType()) {
1556 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1557 D.complete(ThisDecl);
1558 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001559 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001560 }
John McCalld8ac0572009-11-03 19:26:08 +00001561 }
1562
1563 if (DeclEnd)
1564 *DeclEnd = Tok.getLocation();
1565
Richard Smith0706df42011-10-19 21:33:05 +00001566 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001567 ExpectAndConsumeSemi(Context == Declarator::FileContext
1568 ? diag::err_invalid_token_after_toplevel_declarator
1569 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001570 // Okay, there was no semicolon and one was expected. If we see a
1571 // declaration specifier, just assume it was missing and continue parsing.
1572 // Otherwise things are very confused and we skip to recover.
1573 if (!isDeclarationSpecifier()) {
1574 SkipUntil(tok::r_brace, true, true);
1575 if (Tok.is(tok::semi))
1576 ConsumeToken();
1577 }
John McCalld8ac0572009-11-03 19:26:08 +00001578 }
1579
Douglas Gregor23c94db2010-07-02 17:43:08 +00001580 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001581 DeclsInGroup.data(),
1582 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001583}
1584
Richard Smithad762fc2011-04-14 22:09:26 +00001585/// Parse an optional simple-asm-expr and attributes, and attach them to a
1586/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001587bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001588 // If a simple-asm-expr is present, parse it.
1589 if (Tok.is(tok::kw_asm)) {
1590 SourceLocation Loc;
1591 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1592 if (AsmLabel.isInvalid()) {
1593 SkipUntil(tok::semi, true, true);
1594 return true;
1595 }
1596
1597 D.setAsmLabel(AsmLabel.release());
1598 D.SetRangeEnd(Loc);
1599 }
1600
1601 MaybeParseGNUAttributes(D);
1602 return false;
1603}
1604
Douglas Gregor1426e532009-05-12 21:31:51 +00001605/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1606/// declarator'. This method parses the remainder of the declaration
1607/// (including any attributes or initializer, among other things) and
1608/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001609///
Reid Spencer5f016e22007-07-11 17:01:13 +00001610/// init-declarator: [C99 6.7]
1611/// declarator
1612/// declarator '=' initializer
1613/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1614/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001615/// [C++] declarator initializer[opt]
1616///
1617/// [C++] initializer:
1618/// [C++] '=' initializer-clause
1619/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001620/// [C++0x] '=' 'default' [TODO]
1621/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001622/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001623///
1624/// According to the standard grammar, =default and =delete are function
1625/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001626///
John McCalld226f652010-08-21 09:40:31 +00001627Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001628 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001629 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001630 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Richard Smithad762fc2011-04-14 22:09:26 +00001632 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1633}
Mike Stump1eb44332009-09-09 15:08:12 +00001634
Richard Smithad762fc2011-04-14 22:09:26 +00001635Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1636 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001637 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001638 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001639 switch (TemplateInfo.Kind) {
1640 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001641 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001642 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001643
Douglas Gregord5a423b2009-09-25 18:43:00 +00001644 case ParsedTemplateInfo::Template:
1645 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001646 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001647 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001648 D);
1649 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001650
Douglas Gregord5a423b2009-09-25 18:43:00 +00001651 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosier8decdee2012-06-26 22:30:43 +00001652 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001653 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001654 TemplateInfo.ExternLoc,
1655 TemplateInfo.TemplateLoc,
1656 D);
1657 if (ThisRes.isInvalid()) {
1658 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001659 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001660 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001661
Douglas Gregord5a423b2009-09-25 18:43:00 +00001662 ThisDecl = ThisRes.get();
1663 break;
1664 }
1665 }
Mike Stump1eb44332009-09-09 15:08:12 +00001666
Richard Smith34b41d92011-02-20 03:19:35 +00001667 bool TypeContainsAuto =
1668 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1669
Douglas Gregor1426e532009-05-12 21:31:51 +00001670 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001671 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001672 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001673 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001674 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001675 if (D.isFunctionDeclarator())
1676 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1677 << 1 /* delete */;
1678 else
1679 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001680 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001681 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001682 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1683 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001684 else
1685 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001686 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001687 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001688 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001689 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001690 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001691
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001692 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001693 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001694 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001695 cutOffParsing();
1696 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001697 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001698
John McCall60d7b3a2010-08-24 06:29:42 +00001699 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001700
David Blaikie4e4d0842012-03-11 07:00:24 +00001701 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001702 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001703 ExitScope();
1704 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001705
Douglas Gregor1426e532009-05-12 21:31:51 +00001706 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001707 SkipUntil(tok::comma, true, true);
1708 Actions.ActOnInitializerError(ThisDecl);
1709 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001710 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1711 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001712 }
1713 } else if (Tok.is(tok::l_paren)) {
1714 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001715 BalancedDelimiterTracker T(*this, tok::l_paren);
1716 T.consumeOpen();
1717
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001718 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001719 CommaLocsTy CommaLocs;
1720
David Blaikie4e4d0842012-03-11 07:00:24 +00001721 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001722 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001723 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001724 }
1725
Douglas Gregor1426e532009-05-12 21:31:51 +00001726 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001727 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +00001728 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001729
David Blaikie4e4d0842012-03-11 07:00:24 +00001730 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001731 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001732 ExitScope();
1733 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001734 } else {
1735 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001736 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001737
1738 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1739 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001740
David Blaikie4e4d0842012-03-11 07:00:24 +00001741 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001742 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001743 ExitScope();
1744 }
1745
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001746 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1747 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001748 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001749 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1750 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001751 }
Richard Smith80ad52f2013-01-02 11:42:31 +00001752 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001753 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001754 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001755 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1756
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001757 if (D.getCXXScopeSpec().isSet()) {
1758 EnterScope(0);
1759 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1760 }
1761
1762 ExprResult Init(ParseBraceInitializer());
1763
1764 if (D.getCXXScopeSpec().isSet()) {
1765 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1766 ExitScope();
1767 }
1768
1769 if (Init.isInvalid()) {
1770 Actions.ActOnInitializerError(ThisDecl);
1771 } else
1772 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1773 /*DirectInit=*/true, TypeContainsAuto);
1774
Douglas Gregor1426e532009-05-12 21:31:51 +00001775 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001776 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001777 }
1778
Richard Smith483b9f32011-02-21 20:05:19 +00001779 Actions.FinalizeDeclaration(ThisDecl);
1780
Douglas Gregor1426e532009-05-12 21:31:51 +00001781 return ThisDecl;
1782}
1783
Reid Spencer5f016e22007-07-11 17:01:13 +00001784/// ParseSpecifierQualifierList
1785/// specifier-qualifier-list:
1786/// type-specifier specifier-qualifier-list[opt]
1787/// type-qualifier specifier-qualifier-list[opt]
1788/// [GNU] attributes specifier-qualifier-list[opt]
1789///
Richard Smith69730c12012-03-12 07:56:15 +00001790void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1791 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001792 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1793 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001794 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001795 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Reid Spencer5f016e22007-07-11 17:01:13 +00001797 // Validate declspec for type-name.
1798 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001799 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1800 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001801 Diag(Tok, diag::err_expected_type);
1802 DS.SetTypeSpecError();
1803 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1804 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001805 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001806 if (!DS.hasTypeSpecifier())
1807 DS.SetTypeSpecError();
1808 }
Mike Stump1eb44332009-09-09 15:08:12 +00001809
Reid Spencer5f016e22007-07-11 17:01:13 +00001810 // Issue diagnostic and remove storage class if present.
1811 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1812 if (DS.getStorageClassSpecLoc().isValid())
1813 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1814 else
1815 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1816 DS.ClearStorageClassSpecs();
1817 }
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Reid Spencer5f016e22007-07-11 17:01:13 +00001819 // Issue diagnostic and remove function specfier if present.
1820 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001821 if (DS.isInlineSpecified())
1822 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1823 if (DS.isVirtualSpecified())
1824 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1825 if (DS.isExplicitSpecified())
1826 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001827 DS.ClearFunctionSpecs();
1828 }
Richard Smith69730c12012-03-12 07:56:15 +00001829
1830 // Issue diagnostic and remove constexpr specfier if present.
1831 if (DS.isConstexprSpecified()) {
1832 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1833 DS.ClearConstexprSpec();
1834 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001835}
1836
Chris Lattnerc199ab32009-04-12 20:42:31 +00001837/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1838/// specified token is valid after the identifier in a declarator which
1839/// immediately follows the declspec. For example, these things are valid:
1840///
1841/// int x [ 4]; // direct-declarator
1842/// int x ( int y); // direct-declarator
1843/// int(int x ) // direct-declarator
1844/// int x ; // simple-declaration
1845/// int x = 17; // init-declarator-list
1846/// int x , y; // init-declarator-list
1847/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001848/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001849/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001850///
1851/// This is not, because 'x' does not immediately follow the declspec (though
1852/// ')' happens to be valid anyway).
1853/// int (x)
1854///
1855static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1856 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1857 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001858 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001859}
1860
Chris Lattnere40c2952009-04-14 21:34:55 +00001861
1862/// ParseImplicitInt - This method is called when we have an non-typename
1863/// identifier in a declspec (which normally terminates the decl spec) when
1864/// the declspec has no type specifier. In this case, the declspec is either
1865/// malformed or is "implicit int" (in K&R and C89).
1866///
1867/// This method handles diagnosing this prettily and returns false if the
1868/// declspec is done being processed. If it recovers and thinks there may be
1869/// other pieces of declspec after it, it returns true.
1870///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001871bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001872 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00001873 AccessSpecifier AS, DeclSpecContext DSC,
1874 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001875 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001876
Chris Lattnere40c2952009-04-14 21:34:55 +00001877 SourceLocation Loc = Tok.getLocation();
1878 // If we see an identifier that is not a type name, we normally would
1879 // parse it as the identifer being declared. However, when a typename
1880 // is typo'd or the definition is not included, this will incorrectly
1881 // parse the typename as the identifier name and fall over misparsing
1882 // later parts of the diagnostic.
1883 //
1884 // As such, we try to do some look-ahead in cases where this would
1885 // otherwise be an "implicit-int" case to see if this is invalid. For
1886 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1887 // an identifier with implicit int, we'd get a parse error because the
1888 // next token is obviously invalid for a type. Parse these as a case
1889 // with an invalid type specifier.
1890 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001891
Chris Lattnere40c2952009-04-14 21:34:55 +00001892 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001893 // error, do lookahead to try to do better recovery. This never applies
1894 // within a type specifier. Outside of C++, we allow this even if the
1895 // language doesn't "officially" support implicit int -- we support
1896 // implicit int as an extension in C99 and C11. Allegedly, MS also
1897 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001898 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001899 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001900 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001901 // If this token is valid for implicit int, e.g. "static x = 4", then
1902 // we just avoid eating the identifier, so it will be parsed as the
1903 // identifier in the declarator.
1904 return false;
1905 }
Mike Stump1eb44332009-09-09 15:08:12 +00001906
Richard Smith827adaf2012-05-15 21:01:51 +00001907 if (getLangOpts().CPlusPlus &&
1908 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1909 // Don't require a type specifier if we have the 'auto' storage class
1910 // specifier in C++98 -- we'll promote it to a type specifier.
1911 return false;
1912 }
1913
Chris Lattnere40c2952009-04-14 21:34:55 +00001914 // Otherwise, if we don't consume this token, we are going to emit an
1915 // error anyway. Try to recover from various common problems. Check
1916 // to see if this was a reference to a tag name without a tag specified.
1917 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001918 //
1919 // C++ doesn't need this, and isTagName doesn't take SS.
1920 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001921 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001922 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001923
Douglas Gregor23c94db2010-07-02 17:43:08 +00001924 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001925 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001926 case DeclSpec::TST_enum:
1927 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1928 case DeclSpec::TST_union:
1929 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1930 case DeclSpec::TST_struct:
1931 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00001932 case DeclSpec::TST_interface:
1933 TagName="__interface"; FixitTagName = "__interface ";
1934 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001935 case DeclSpec::TST_class:
1936 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001937 }
Mike Stump1eb44332009-09-09 15:08:12 +00001938
Chris Lattnerf4382f52009-04-14 22:17:06 +00001939 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001940 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1941 LookupResult R(Actions, TokenName, SourceLocation(),
1942 Sema::LookupOrdinaryName);
1943
Chris Lattnerf4382f52009-04-14 22:17:06 +00001944 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001945 << TokenName << TagName << getLangOpts().CPlusPlus
1946 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1947
1948 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1949 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1950 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001951 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001952 << TokenName << TagName;
1953 }
Mike Stump1eb44332009-09-09 15:08:12 +00001954
Chris Lattnerf4382f52009-04-14 22:17:06 +00001955 // Parse this as a tag as if the missing tag were present.
1956 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001957 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001958 else
Richard Smith69730c12012-03-12 07:56:15 +00001959 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00001960 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001961 return true;
1962 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001963 }
Mike Stump1eb44332009-09-09 15:08:12 +00001964
Richard Smith8f0a7e72012-05-15 21:29:55 +00001965 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001966 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001967 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1968 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001969 // Look ahead to the next token to try to figure out what this declaration
1970 // was supposed to be.
1971 switch (NextToken().getKind()) {
1972 case tok::comma:
1973 case tok::equal:
1974 case tok::kw_asm:
1975 case tok::l_brace:
1976 case tok::l_square:
1977 case tok::semi:
1978 // This looks like a variable declaration. The type is probably missing.
1979 // We're done parsing decl-specifiers.
1980 return false;
1981
1982 case tok::l_paren: {
1983 // static x(4); // 'x' is not a type
1984 // x(int n); // 'x' is not a type
1985 // x (*p)[]; // 'x' is a type
1986 //
1987 // Since we're in an error case (or the rare 'implicit int in C++' MS
1988 // extension), we can afford to perform a tentative parse to determine
1989 // which case we're in.
1990 TentativeParsingAction PA(*this);
1991 ConsumeToken();
1992 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1993 PA.Revert();
1994 if (TPR == TPResult::False())
1995 return false;
1996 // The identifier is followed by a parenthesized declarator.
1997 // It's supposed to be a type.
1998 break;
1999 }
2000
2001 default:
2002 // This is probably supposed to be a type. This includes cases like:
2003 // int f(itn);
2004 // struct S { unsinged : 4; };
2005 break;
2006 }
2007 }
2008
Chad Rosier8decdee2012-06-26 22:30:43 +00002009 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00002010 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00002011 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002012 IdentifierInfo *II = Tok.getIdentifierInfo();
2013 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00002014 // The action emitted a diagnostic, so we don't have to.
2015 if (T) {
2016 // The action has suggested that the type T could be used. Set that as
2017 // the type in the declaration specifiers, consume the would-be type
2018 // name token, and we're done.
2019 const char *PrevSpec;
2020 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00002021 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00002022 DS.SetRangeEnd(Tok.getLocation());
2023 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002024 // There may be other declaration specifiers after this.
2025 return true;
2026 } else if (II != Tok.getIdentifierInfo()) {
2027 // If no type was suggested, the correction is to a keyword
2028 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002029 // There may be other declaration specifiers after this.
2030 return true;
2031 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002032
Douglas Gregora786fdb2009-10-13 23:27:22 +00002033 // Fall through; the action had no suggestion for us.
2034 } else {
2035 // The action did not emit a diagnostic, so emit one now.
2036 SourceRange R;
2037 if (SS) R = SS->getRange();
2038 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2039 }
Mike Stump1eb44332009-09-09 15:08:12 +00002040
Douglas Gregora786fdb2009-10-13 23:27:22 +00002041 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002042 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002043 DS.SetRangeEnd(Tok.getLocation());
2044 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002045
Chris Lattnere40c2952009-04-14 21:34:55 +00002046 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2047 // avoid rippling error messages on subsequent uses of the same type,
2048 // could be useful if #include was forgotten.
2049 return false;
2050}
2051
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002052/// \brief Determine the declaration specifier context from the declarator
2053/// context.
2054///
2055/// \param Context the declarator context, which is one of the
2056/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002057Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002058Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2059 if (Context == Declarator::MemberContext)
2060 return DSC_class;
2061 if (Context == Declarator::FileContext)
2062 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002063 if (Context == Declarator::TrailingReturnContext)
2064 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002065 return DSC_normal;
2066}
2067
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002068/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2069///
2070/// FIXME: Simply returns an alignof() expression if the argument is a
2071/// type. Ideally, the type should be propagated directly into Sema.
2072///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002073/// [C11] type-id
2074/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002075/// [C++0x] type-id ...[opt]
2076/// [C++0x] assignment-expression ...[opt]
2077ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2078 SourceLocation &EllipsisLoc) {
2079 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002080 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002081 SourceLocation TypeLoc = Tok.getLocation();
2082 ParsedType Ty = ParseTypeName().get();
2083 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002084 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2085 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002086 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002087 ER = ParseConstantExpression();
2088
Richard Smith80ad52f2013-01-02 11:42:31 +00002089 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002090 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002091
2092 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002093}
2094
2095/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2096/// attribute to Attrs.
2097///
2098/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002099/// [C11] '_Alignas' '(' type-id ')'
2100/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smith33f04a22013-01-29 01:48:07 +00002101/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2102/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002103void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2104 SourceLocation *endLoc) {
2105 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2106 "Not an alignment-specifier!");
2107
Richard Smith33f04a22013-01-29 01:48:07 +00002108 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2109 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002110
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002111 BalancedDelimiterTracker T(*this, tok::l_paren);
2112 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002113 return;
2114
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002115 SourceLocation EllipsisLoc;
2116 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002117 if (ArgExpr.isInvalid()) {
2118 SkipUntil(tok::r_paren);
2119 return;
2120 }
2121
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002122 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002123 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002124 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002125
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002126 // FIXME: Handle pack-expansions here.
2127 if (EllipsisLoc.isValid()) {
2128 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2129 return;
2130 }
2131
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002132 ExprVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002133 ArgExprs.push_back(ArgExpr.release());
Richard Smith33f04a22013-01-29 01:48:07 +00002134 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
2135 ArgExprs.data(), 1, AttributeList::AS_Keyword);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002136}
2137
Reid Spencer5f016e22007-07-11 17:01:13 +00002138/// ParseDeclarationSpecifiers
2139/// declaration-specifiers: [C99 6.7]
2140/// storage-class-specifier declaration-specifiers[opt]
2141/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002142/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002143/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002144/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002145/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002146///
2147/// storage-class-specifier: [C99 6.7.1]
2148/// 'typedef'
2149/// 'extern'
2150/// 'static'
2151/// 'auto'
2152/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002153/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002154/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002155/// function-specifier: [C99 6.7.4]
2156/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002157/// [C++] 'virtual'
2158/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002159/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002160/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002161/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002162
Reid Spencer5f016e22007-07-11 17:01:13 +00002163///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002164void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002165 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002166 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002167 DeclSpecContext DSContext,
2168 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002169 if (DS.getSourceRange().isInvalid()) {
2170 DS.SetRangeStart(Tok.getLocation());
2171 DS.SetRangeEnd(Tok.getLocation());
2172 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002173
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002174 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002175 bool AttrsLastTime = false;
2176 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002177 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002178 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002179 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002180 unsigned DiagID = 0;
2181
Reid Spencer5f016e22007-07-11 17:01:13 +00002182 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002183
Reid Spencer5f016e22007-07-11 17:01:13 +00002184 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002185 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002186 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002187 if (!AttrsLastTime)
2188 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002189 else {
2190 // Reject C++11 attributes that appertain to decl specifiers as
2191 // we don't support any C++11 attributes that appertain to decl
2192 // specifiers. This also conforms to what g++ 4.8 is doing.
2193 ProhibitCXX11Attributes(attrs);
2194
Sean Hunt2edf0a22012-06-23 05:07:58 +00002195 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002196 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002197
Reid Spencer5f016e22007-07-11 17:01:13 +00002198 // If this is not a declaration specifier token, we're done reading decl
2199 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002200 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002201 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002202
Sean Hunt2edf0a22012-06-23 05:07:58 +00002203 case tok::l_square:
2204 case tok::kw_alignas:
2205 if (!isCXX11AttributeSpecifier())
2206 goto DoneWithDeclSpec;
2207
2208 ProhibitAttributes(attrs);
2209 // FIXME: It would be good to recover by accepting the attributes,
2210 // but attempting to do that now would cause serious
2211 // madness in terms of diagnostics.
2212 attrs.clear();
2213 attrs.Range = SourceRange();
2214
2215 ParseCXX11Attributes(attrs);
2216 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002217 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002218
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002219 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002220 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002221 if (DS.hasTypeSpecifier()) {
2222 bool AllowNonIdentifiers
2223 = (getCurScope()->getFlags() & (Scope::ControlScope |
2224 Scope::BlockScope |
2225 Scope::TemplateParamScope |
2226 Scope::FunctionPrototypeScope |
2227 Scope::AtCatchScope)) == 0;
2228 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002229 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002230 (DSContext == DSC_class && DS.isFriendSpecified());
2231
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002232 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002233 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002234 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002235 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002236 }
2237
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002238 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2239 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2240 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002241 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002242 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002243 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002244 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002245 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002246 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002247
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002248 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002249 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002250 }
2251
Chris Lattner5e02c472009-01-05 00:07:25 +00002252 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002253 // C++ scope specifier. Annotate and loop, or bail out on error.
2254 if (TryAnnotateCXXScopeToken(true)) {
2255 if (!DS.hasTypeSpecifier())
2256 DS.SetTypeSpecError();
2257 goto DoneWithDeclSpec;
2258 }
John McCall2e0a7152010-03-01 18:20:46 +00002259 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2260 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002261 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002262
2263 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002264 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002265 goto DoneWithDeclSpec;
2266
John McCallaa87d332009-12-12 11:40:51 +00002267 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002268 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2269 Tok.getAnnotationRange(),
2270 SS);
John McCallaa87d332009-12-12 11:40:51 +00002271
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002272 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002273 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002274 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002275 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002276 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002277 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002278
2279 // C++ [class.qual]p2:
2280 // In a lookup in which the constructor is an acceptable lookup
2281 // result and the nested-name-specifier nominates a class C:
2282 //
2283 // - if the name specified after the
2284 // nested-name-specifier, when looked up in C, is the
2285 // injected-class-name of C (Clause 9), or
2286 //
2287 // - if the name specified after the nested-name-specifier
2288 // is the same as the identifier or the
2289 // simple-template-id's template-name in the last
2290 // component of the nested-name-specifier,
2291 //
2292 // the name is instead considered to name the constructor of
2293 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002294 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002295 // Thus, if the template-name is actually the constructor
2296 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002297 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002298 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002299 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCallba9d8532010-04-13 06:39:49 +00002300 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002301 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002302 if (isConstructorDeclarator()) {
2303 // The user meant this to be an out-of-line constructor
2304 // definition, but template arguments are not allowed
2305 // there. Just allow this as a constructor; we'll
2306 // complain about it later.
2307 goto DoneWithDeclSpec;
2308 }
2309
2310 // The user meant this to name a type, but it actually names
2311 // a constructor with some extraneous template
2312 // arguments. Complain, then parse it as a type as the user
2313 // intended.
2314 Diag(TemplateId->TemplateNameLoc,
2315 diag::err_out_of_line_template_id_names_constructor)
2316 << TemplateId->Name;
2317 }
2318
John McCallaa87d332009-12-12 11:40:51 +00002319 DS.getTypeSpecScope() = SS;
2320 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002321 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002322 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002323 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002324 continue;
2325 }
2326
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002327 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002328 DS.getTypeSpecScope() = SS;
2329 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002330 if (Tok.getAnnotationValue()) {
2331 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002332 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002333 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002334 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002335 if (isInvalid)
2336 break;
John McCallb3d87482010-08-24 05:47:05 +00002337 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002338 else
2339 DS.SetTypeSpecError();
2340 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2341 ConsumeToken(); // The typename
2342 }
2343
Douglas Gregor9135c722009-03-25 15:40:00 +00002344 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002345 goto DoneWithDeclSpec;
2346
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002347 // If we're in a context where the identifier could be a class name,
2348 // check whether this is a constructor declaration.
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002349 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002350 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002351 &SS)) {
2352 if (isConstructorDeclarator())
2353 goto DoneWithDeclSpec;
2354
2355 // As noted in C++ [class.qual]p2 (cited above), when the name
2356 // of the class is qualified in a context where it could name
2357 // a constructor, its a constructor name. However, we've
2358 // looked at the declarator, and the user probably meant this
2359 // to be a type. Complain that it isn't supposed to be treated
2360 // as a type, then proceed to parse it as a type.
2361 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2362 << Next.getIdentifierInfo();
2363 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002364
John McCallb3d87482010-08-24 05:47:05 +00002365 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2366 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002367 getCurScope(), &SS,
2368 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002369 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002370 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002371
Chris Lattnerf4382f52009-04-14 22:17:06 +00002372 // If the referenced identifier is not a type, then this declspec is
2373 // erroneous: We already checked about that it has no type specifier, and
2374 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002375 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002376 if (TypeRep == 0) {
2377 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002378 ParsedAttributesWithRange Attrs(AttrFactory);
2379 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2380 if (!Attrs.empty()) {
2381 AttrsLastTime = true;
2382 attrs.takeAllFrom(Attrs);
2383 }
2384 continue;
2385 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002386 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002387 }
Mike Stump1eb44332009-09-09 15:08:12 +00002388
John McCallaa87d332009-12-12 11:40:51 +00002389 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002390 ConsumeToken(); // The C++ scope.
2391
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002392 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002393 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002394 if (isInvalid)
2395 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002396
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002397 DS.SetRangeEnd(Tok.getLocation());
2398 ConsumeToken(); // The typename.
2399
2400 continue;
2401 }
Mike Stump1eb44332009-09-09 15:08:12 +00002402
Chris Lattner80d0c892009-01-21 19:48:37 +00002403 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002404 if (Tok.getAnnotationValue()) {
2405 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002406 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002407 DiagID, T);
2408 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002409 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002410
Chris Lattner5c5db552010-04-05 18:18:31 +00002411 if (isInvalid)
2412 break;
2413
Chris Lattner80d0c892009-01-21 19:48:37 +00002414 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2415 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002416
Chris Lattner80d0c892009-01-21 19:48:37 +00002417 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2418 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002419 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002420 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002421 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002422
Chris Lattner80d0c892009-01-21 19:48:37 +00002423 continue;
2424 }
Mike Stump1eb44332009-09-09 15:08:12 +00002425
Douglas Gregorbfad9152011-04-28 15:48:45 +00002426 case tok::kw___is_signed:
2427 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2428 // typically treats it as a trait. If we see __is_signed as it appears
2429 // in libstdc++, e.g.,
2430 //
2431 // static const bool __is_signed;
2432 //
2433 // then treat __is_signed as an identifier rather than as a keyword.
2434 if (DS.getTypeSpecType() == TST_bool &&
2435 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2436 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2437 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2438 Tok.setKind(tok::identifier);
2439 }
2440
2441 // We're done with the declaration-specifiers.
2442 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002443
Chris Lattner3bd934a2008-07-26 01:18:38 +00002444 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002445 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002446 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002447 // In C++, check to see if this is a scope specifier like foo::bar::, if
2448 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002449 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002450 if (TryAnnotateCXXScopeToken(true)) {
2451 if (!DS.hasTypeSpecifier())
2452 DS.SetTypeSpecError();
2453 goto DoneWithDeclSpec;
2454 }
2455 if (!Tok.is(tok::identifier))
2456 continue;
2457 }
Mike Stump1eb44332009-09-09 15:08:12 +00002458
Chris Lattner3bd934a2008-07-26 01:18:38 +00002459 // This identifier can only be a typedef name if we haven't already seen
2460 // a type-specifier. Without this check we misparse:
2461 // typedef int X; struct Y { short X; }; as 'short int'.
2462 if (DS.hasTypeSpecifier())
2463 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002464
John Thompson82287d12010-02-05 00:12:22 +00002465 // Check for need to substitute AltiVec keyword tokens.
2466 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2467 break;
2468
Richard Smithf63eee72012-05-09 18:56:43 +00002469 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2470 // allow the use of a typedef name as a type specifier.
2471 if (DS.isTypeAltiVecVector())
2472 goto DoneWithDeclSpec;
2473
John McCallb3d87482010-08-24 05:47:05 +00002474 ParsedType TypeRep =
2475 Actions.getTypeName(*Tok.getIdentifierInfo(),
2476 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002477
Chris Lattnerc199ab32009-04-12 20:42:31 +00002478 // If this is not a typedef name, don't parse it as part of the declspec,
2479 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002480 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002481 ParsedAttributesWithRange Attrs(AttrFactory);
2482 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2483 if (!Attrs.empty()) {
2484 AttrsLastTime = true;
2485 attrs.takeAllFrom(Attrs);
2486 }
2487 continue;
2488 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002489 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002490 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002491
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002492 // If we're in a context where the identifier could be a class name,
2493 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002494 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002495 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002496 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002497 goto DoneWithDeclSpec;
2498
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002499 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002500 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002501 if (isInvalid)
2502 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002503
Chris Lattner3bd934a2008-07-26 01:18:38 +00002504 DS.SetRangeEnd(Tok.getLocation());
2505 ConsumeToken(); // The identifier
2506
2507 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2508 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002509 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002510 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002511 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002512
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002513 // Need to support trailing type qualifiers (e.g. "id<p> const").
2514 // If a type specifier follows, it will be diagnosed elsewhere.
2515 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002516 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002517
2518 // type-name
2519 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002520 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002521 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002522 // This template-id does not refer to a type name, so we're
2523 // done with the type-specifiers.
2524 goto DoneWithDeclSpec;
2525 }
2526
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002527 // If we're in a context where the template-id could be a
2528 // constructor name or specialization, check whether this is a
2529 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002530 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002531 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002532 isConstructorDeclarator())
2533 goto DoneWithDeclSpec;
2534
Douglas Gregor39a8de12009-02-25 19:37:18 +00002535 // Turn the template-id annotation token into a type annotation
2536 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002537 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002538 continue;
2539 }
2540
Reid Spencer5f016e22007-07-11 17:01:13 +00002541 // GNU attributes support.
2542 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002543 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002544 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002545
2546 // Microsoft declspec support.
2547 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002548 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002549 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002550
Steve Naroff239f0732008-12-25 14:16:32 +00002551 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002552 case tok::kw___forceinline: {
Chad Rosier22aa6902012-12-21 22:24:43 +00002553 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002554 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002555 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002556 // FIXME: This does not work correctly if it is set to be a declspec
2557 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002558 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002559 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002560 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002561 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002562
2563 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002564 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002565 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002566 case tok::kw___cdecl:
2567 case tok::kw___stdcall:
2568 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002569 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002570 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002571 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002572 continue;
2573
Dawn Perchik52fc3142010-09-03 01:29:35 +00002574 // Borland single token adornments.
2575 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002576 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002577 continue;
2578
Peter Collingbournef315fa82011-02-14 01:42:53 +00002579 // OpenCL single token adornments.
2580 case tok::kw___kernel:
2581 ParseOpenCLAttributes(DS.getAttributes());
2582 continue;
2583
Reid Spencer5f016e22007-07-11 17:01:13 +00002584 // storage-class-specifier
2585 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002586 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2587 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002588 break;
2589 case tok::kw_extern:
2590 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002591 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002592 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2593 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002594 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002595 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002596 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2597 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002598 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002599 case tok::kw_static:
2600 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002601 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002602 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2603 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002604 break;
2605 case tok::kw_auto:
Richard Smith80ad52f2013-01-02 11:42:31 +00002606 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002607 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002608 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2609 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002610 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002611 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002612 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002613 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002614 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2615 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002616 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002617 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2618 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002619 break;
2620 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002621 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2622 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002623 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002624 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002625 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2626 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002627 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002628 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002629 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002630 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002631
Reid Spencer5f016e22007-07-11 17:01:13 +00002632 // function-specifier
2633 case tok::kw_inline:
Chad Rosier22aa6902012-12-21 22:24:43 +00002634 isInvalid = DS.setFunctionSpecInline(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002635 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002636 case tok::kw_virtual:
Chad Rosier22aa6902012-12-21 22:24:43 +00002637 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002638 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002639 case tok::kw_explicit:
Chad Rosier22aa6902012-12-21 22:24:43 +00002640 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002641 break;
Richard Smithde03c152013-01-17 22:16:11 +00002642 case tok::kw__Noreturn:
2643 if (!getLangOpts().C11)
2644 Diag(Loc, diag::ext_c11_noreturn);
2645 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2646 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002647
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002648 // alignment-specifier
2649 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002650 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002651 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002652 ParseAlignmentSpecifier(DS.getAttributes());
2653 continue;
2654
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002655 // friend
2656 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002657 if (DSContext == DSC_class)
2658 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2659 else {
2660 PrevSpec = ""; // not actually used by the diagnostic
2661 DiagID = diag::err_friend_invalid_in_context;
2662 isInvalid = true;
2663 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002664 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002665
Douglas Gregor8d267c52011-09-09 02:06:17 +00002666 // Modules
2667 case tok::kw___module_private__:
2668 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2669 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002670
Sebastian Redl2ac67232009-11-05 15:47:02 +00002671 // constexpr
2672 case tok::kw_constexpr:
2673 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2674 break;
2675
Chris Lattner80d0c892009-01-21 19:48:37 +00002676 // type-specifier
2677 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002678 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2679 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002680 break;
2681 case tok::kw_long:
2682 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002683 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2684 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002685 else
John McCallfec54012009-08-03 20:12:06 +00002686 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2687 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002688 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002689 case tok::kw___int64:
2690 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2691 DiagID);
2692 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002693 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002694 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2695 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002696 break;
2697 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002698 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2699 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002700 break;
2701 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002702 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2703 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002704 break;
2705 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002706 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2707 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002708 break;
2709 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002710 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2711 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002712 break;
2713 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002714 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2715 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002716 break;
2717 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002718 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2719 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002720 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002721 case tok::kw___int128:
2722 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2723 DiagID);
2724 break;
2725 case tok::kw_half:
2726 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2727 DiagID);
2728 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002729 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002730 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2731 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002732 break;
2733 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002734 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2735 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002736 break;
2737 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002738 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2739 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002740 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002741 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002742 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2743 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002744 break;
2745 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002746 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2747 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002748 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002749 case tok::kw_bool:
2750 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002751 if (Tok.is(tok::kw_bool) &&
2752 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2753 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2754 PrevSpec = ""; // Not used by the diagnostic.
2755 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002756 // For better error recovery.
2757 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002758 isInvalid = true;
2759 } else {
2760 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2761 DiagID);
2762 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002763 break;
2764 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002765 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2766 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002767 break;
2768 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002769 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2770 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002771 break;
2772 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002773 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2774 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002775 break;
John Thompson82287d12010-02-05 00:12:22 +00002776 case tok::kw___vector:
2777 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2778 break;
2779 case tok::kw___pixel:
2780 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2781 break;
Guy Benyeib13621d2012-12-18 14:38:23 +00002782 case tok::kw_image1d_t:
2783 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2784 PrevSpec, DiagID);
2785 break;
2786 case tok::kw_image1d_array_t:
2787 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2788 PrevSpec, DiagID);
2789 break;
2790 case tok::kw_image1d_buffer_t:
2791 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2792 PrevSpec, DiagID);
2793 break;
2794 case tok::kw_image2d_t:
2795 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2796 PrevSpec, DiagID);
2797 break;
2798 case tok::kw_image2d_array_t:
2799 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2800 PrevSpec, DiagID);
2801 break;
2802 case tok::kw_image3d_t:
2803 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2804 PrevSpec, DiagID);
2805 break;
Guy Benyei21f18c42013-02-07 10:55:47 +00002806 case tok::kw_sampler_t:
2807 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2808 PrevSpec, DiagID);
2809 break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00002810 case tok::kw_event_t:
2811 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2812 PrevSpec, DiagID);
2813 break;
John McCalla5fc4722011-04-09 22:50:59 +00002814 case tok::kw___unknown_anytype:
2815 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2816 PrevSpec, DiagID);
2817 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002818
2819 // class-specifier:
2820 case tok::kw_class:
2821 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00002822 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00002823 case tok::kw_union: {
2824 tok::TokenKind Kind = Tok.getKind();
2825 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00002826
2827 // These are attributes following class specifiers.
2828 // To produce better diagnostic, we parse them when
2829 // parsing class specifier.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002830 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00002831 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002832 EnteringContext, DSContext, Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002833
2834 // If there are attributes following class specifier,
2835 // take them over and handle them here.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002836 if (!Attributes.empty()) {
Michael Han2e397132012-11-26 22:54:45 +00002837 AttrsLastTime = true;
Bill Wendlingad017fa2012-12-20 19:22:21 +00002838 attrs.takeAllFrom(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002839 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002840 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002841 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002842
2843 // enum-specifier:
2844 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002845 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002846 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002847 continue;
2848
2849 // cv-qualifier:
2850 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002851 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002852 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002853 break;
2854 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002855 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002856 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002857 break;
2858 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002859 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002860 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002861 break;
2862
Douglas Gregord57959a2009-03-27 23:10:48 +00002863 // C++ typename-specifier:
2864 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002865 if (TryAnnotateTypeOrScopeToken()) {
2866 DS.SetTypeSpecError();
2867 goto DoneWithDeclSpec;
2868 }
2869 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002870 continue;
2871 break;
2872
Chris Lattner80d0c892009-01-21 19:48:37 +00002873 // GNU typeof support.
2874 case tok::kw_typeof:
2875 ParseTypeofSpecifier(DS);
2876 continue;
2877
David Blaikie42d6d0c2011-12-04 05:04:18 +00002878 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002879 ParseDecltypeSpecifier(DS);
2880 continue;
2881
Sean Huntdb5d44b2011-05-19 05:37:45 +00002882 case tok::kw___underlying_type:
2883 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002884 continue;
2885
2886 case tok::kw__Atomic:
2887 ParseAtomicSpecifier(DS);
2888 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002889
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002890 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00002891 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002892 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002893 goto DoneWithDeclSpec;
2894 case tok::kw___private:
2895 case tok::kw___global:
2896 case tok::kw___local:
2897 case tok::kw___constant:
2898 case tok::kw___read_only:
2899 case tok::kw___write_only:
2900 case tok::kw___read_write:
2901 ParseOpenCLQualifiers(DS);
2902 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002903
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002904 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002905 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002906 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2907 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002908 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002909 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002910
Douglas Gregor46f936e2010-11-19 17:10:50 +00002911 if (!ParseObjCProtocolQualifiers(DS))
2912 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2913 << FixItHint::CreateInsertion(Loc, "id")
2914 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00002915
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002916 // Need to support trailing type qualifiers (e.g. "id<p> const").
2917 // If a type specifier follows, it will be diagnosed elsewhere.
2918 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002919 }
John McCallfec54012009-08-03 20:12:06 +00002920 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002921 if (isInvalid) {
2922 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002923 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00002924
Douglas Gregorae2fb142010-08-23 14:34:43 +00002925 if (DiagID == diag::ext_duplicate_declspec)
2926 Diag(Tok, DiagID)
2927 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2928 else
2929 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002930 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002931
Chris Lattner81c018d2008-03-13 06:29:04 +00002932 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002933 if (DiagID != diag::err_bool_redeclaration)
2934 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00002935
2936 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002937 }
2938}
Douglas Gregoradcac882008-12-01 23:54:00 +00002939
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002940/// ParseStructDeclaration - Parse a struct declaration without the terminating
2941/// semicolon.
2942///
Reid Spencer5f016e22007-07-11 17:01:13 +00002943/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002944/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002945/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002946/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002947/// struct-declarator-list:
2948/// struct-declarator
2949/// struct-declarator-list ',' struct-declarator
2950/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2951/// struct-declarator:
2952/// declarator
2953/// [GNU] declarator attributes[opt]
2954/// declarator[opt] ':' constant-expression
2955/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2956///
Chris Lattnere1359422008-04-10 06:46:29 +00002957void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002958ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00002959
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002960 if (Tok.is(tok::kw___extension__)) {
2961 // __extension__ silences extension warnings in the subexpression.
2962 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002963 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002964 return ParseStructDeclaration(DS, Fields);
2965 }
Mike Stump1eb44332009-09-09 15:08:12 +00002966
Steve Naroff28a7ca82007-08-20 22:28:22 +00002967 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002968 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002969
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002970 // If there are no declarators, this is a free-standing declaration
2971 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002972 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002973 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2974 DS);
2975 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002976 return;
2977 }
2978
2979 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002980 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002981 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002982 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002983 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00002984 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002985
Bill Wendlingad017fa2012-12-20 19:22:21 +00002986 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002987 if (!FirstDeclarator)
2988 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002989
Steve Naroff28a7ca82007-08-20 22:28:22 +00002990 /// struct-declarator: declarator
2991 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002992 if (Tok.isNot(tok::colon)) {
2993 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2994 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002995 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002996 }
Mike Stump1eb44332009-09-09 15:08:12 +00002997
Chris Lattner04d66662007-10-09 17:33:22 +00002998 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002999 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00003000 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003001 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00003002 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00003003 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00003004 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00003005 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003006
Steve Naroff28a7ca82007-08-20 22:28:22 +00003007 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003008 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003009
John McCallbdd563e2009-11-03 02:38:08 +00003010 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00003011 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00003012
Steve Naroff28a7ca82007-08-20 22:28:22 +00003013 // If we don't have a comma, it is either the end of the list (a ';')
3014 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00003015 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003016 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00003017
Steve Naroff28a7ca82007-08-20 22:28:22 +00003018 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00003019 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003020
John McCallbdd563e2009-11-03 02:38:08 +00003021 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003022 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00003023}
3024
3025/// ParseStructUnionBody
3026/// struct-contents:
3027/// struct-declaration-list
3028/// [EXT] empty
3029/// [GNU] "struct-declaration-list" without terminatoring ';'
3030/// struct-declaration-list:
3031/// struct-declaration
3032/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003033/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00003034///
Reid Spencer5f016e22007-07-11 17:01:13 +00003035void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00003036 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00003037 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3038 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00003039
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003040 BalancedDelimiterTracker T(*this, tok::l_brace);
3041 if (T.consumeOpen())
3042 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003043
Douglas Gregor3218c4b2009-01-09 22:42:13 +00003044 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003045 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00003046
Reid Spencer5f016e22007-07-11 17:01:13 +00003047 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
3048 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00003049 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00003050 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3051 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3052 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003053
Chris Lattner5f9e2722011-07-23 10:55:15 +00003054 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00003055
Reid Spencer5f016e22007-07-11 17:01:13 +00003056 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00003057 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003058 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003059
Reid Spencer5f016e22007-07-11 17:01:13 +00003060 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003061 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003062 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003063 continue;
3064 }
Chris Lattnere1359422008-04-10 06:46:29 +00003065
John McCallbdd563e2009-11-03 02:38:08 +00003066 if (!Tok.is(tok::at)) {
3067 struct CFieldCallback : FieldCallback {
3068 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003069 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003070 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003071
John McCalld226f652010-08-21 09:40:31 +00003072 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003073 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003074 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3075
Eli Friedmandcdff462012-08-08 23:53:27 +00003076 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00003077 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003078 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003079 FD.D.getDeclSpec().getSourceRange().getBegin(),
3080 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003081 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003082 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003083 }
John McCallbdd563e2009-11-03 02:38:08 +00003084 } Callback(*this, TagDecl, FieldDecls);
3085
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003086 // Parse all the comma separated declarators.
3087 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003088 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003089 } else { // Handle @defs
3090 ConsumeToken();
3091 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3092 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003093 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003094 continue;
3095 }
3096 ConsumeToken();
3097 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3098 if (!Tok.is(tok::identifier)) {
3099 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003100 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003101 continue;
3102 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003103 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003104 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003105 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003106 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3107 ConsumeToken();
3108 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003109 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003110
Chris Lattner04d66662007-10-09 17:33:22 +00003111 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003112 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003113 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003114 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003115 break;
3116 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003117 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3118 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003119 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003120 // If we stopped at a ';', eat it.
3121 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003122 }
3123 }
Mike Stump1eb44332009-09-09 15:08:12 +00003124
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003125 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003126
John McCall0b7e6782011-03-24 11:26:52 +00003127 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003128 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003129 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003130
Douglas Gregor23c94db2010-07-02 17:43:08 +00003131 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003132 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003133 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003134 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003135 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003136 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3137 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003138}
3139
Reid Spencer5f016e22007-07-11 17:01:13 +00003140/// ParseEnumSpecifier
3141/// enum-specifier: [C99 6.7.2.2]
3142/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003143///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003144/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3145/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003146/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3147/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003148/// 'enum' identifier
3149/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003150///
Richard Smith1af83c42012-03-23 03:33:32 +00003151/// [C++11] enum-head '{' enumerator-list[opt] '}'
3152/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003153///
Richard Smith1af83c42012-03-23 03:33:32 +00003154/// enum-head: [C++11]
3155/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3156/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3157/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003158///
Richard Smith1af83c42012-03-23 03:33:32 +00003159/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003160/// 'enum'
3161/// 'enum' 'class'
3162/// 'enum' 'struct'
3163///
Richard Smith1af83c42012-03-23 03:33:32 +00003164/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003165/// ':' type-specifier-seq
3166///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003167/// [C++] elaborated-type-specifier:
3168/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3169///
Chris Lattner4c97d762009-04-12 21:49:30 +00003170void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003171 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003172 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003173 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003174 if (Tok.is(tok::code_completion)) {
3175 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003176 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003177 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003178 }
John McCall57c13002011-07-06 05:58:41 +00003179
Sean Hunt2edf0a22012-06-23 05:07:58 +00003180 // If attributes exist after tag, parse them.
3181 ParsedAttributesWithRange attrs(AttrFactory);
3182 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003183 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003184
3185 // If declspecs exist after tag, parse them.
3186 while (Tok.is(tok::kw___declspec))
3187 ParseMicrosoftDeclSpec(attrs);
3188
Richard Smithbdad7a22012-01-10 01:33:14 +00003189 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003190 bool IsScopedUsingClassTag = false;
3191
John McCall1e12b3d2012-06-23 22:30:04 +00003192 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Smith80ad52f2013-01-02 11:42:31 +00003193 if (getLangOpts().CPlusPlus11 &&
John McCall57c13002011-07-06 05:58:41 +00003194 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00003195 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003196 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003197 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003198
Bill Wendlingad017fa2012-12-20 19:22:21 +00003199 // Attributes are not allowed between these keywords. Diagnose,
John McCall1e12b3d2012-06-23 22:30:04 +00003200 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003201 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003202
3203 // They are allowed afterwards, though.
3204 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003205 MaybeParseCXX11Attributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003206 while (Tok.is(tok::kw___declspec))
3207 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003208 }
Richard Smith1af83c42012-03-23 03:33:32 +00003209
John McCall13489672012-05-07 06:16:58 +00003210 // C++11 [temp.explicit]p12:
3211 // The usual access controls do not apply to names used to specify
3212 // explicit instantiations.
3213 // We extend this to also cover explicit specializations. Note that
3214 // we don't suppress if this turns out to be an elaborated type
3215 // specifier.
3216 bool shouldDelayDiagsInTag =
3217 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3218 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3219 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003220
Richard Smith7796eb52012-03-12 08:56:40 +00003221 // Enum definitions should not be parsed in a trailing-return-type.
3222 bool AllowDeclaration = DSC != DSC_trailing;
3223
3224 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith80ad52f2013-01-02 11:42:31 +00003225 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smith7796eb52012-03-12 08:56:40 +00003226 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003227
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003228 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003229 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003230 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3231 // if a fixed underlying type is allowed.
3232 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003233
3234 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003235 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00003236 return;
3237
3238 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003239 Diag(Tok, diag::err_expected_ident);
3240 if (Tok.isNot(tok::l_brace)) {
3241 // Has no name and is not a definition.
3242 // Skip the rest of this declarator, up until the comma or semicolon.
3243 SkipUntil(tok::comma, true);
3244 return;
3245 }
3246 }
3247 }
Mike Stump1eb44332009-09-09 15:08:12 +00003248
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003249 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003250 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003251 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003252 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003253
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003254 // Skip the rest of this declarator, up until the comma or semicolon.
3255 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003256 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003257 }
Mike Stump1eb44332009-09-09 15:08:12 +00003258
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003259 // If an identifier is present, consume and remember it.
3260 IdentifierInfo *Name = 0;
3261 SourceLocation NameLoc;
3262 if (Tok.is(tok::identifier)) {
3263 Name = Tok.getIdentifierInfo();
3264 NameLoc = ConsumeToken();
3265 }
Mike Stump1eb44332009-09-09 15:08:12 +00003266
Richard Smithbdad7a22012-01-10 01:33:14 +00003267 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003268 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3269 // declaration of a scoped enumeration.
3270 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003271 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003272 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003273 }
3274
John McCall13489672012-05-07 06:16:58 +00003275 // Okay, end the suppression area. We'll decide whether to emit the
3276 // diagnostics in a second.
3277 if (shouldDelayDiagsInTag)
3278 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003279
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003280 TypeResult BaseType;
3281
Douglas Gregora61b3e72010-12-01 17:42:47 +00003282 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003283 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003284 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003285 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003286 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003287 // If we're in class scope, this can either be an enum declaration with
3288 // an underlying type, or a declaration of a bitfield member. We try to
3289 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003290 // (integer literal, sizeof); if it's still ambiguous, we then consider
3291 // anything that's a simple-type-specifier followed by '(' as an
3292 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003293 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003294 EnterExpressionEvaluationContext Unevaluated(Actions,
3295 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003296 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003297 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003298 // bit-field. This is the common case.
3299 if (TPR == TPResult::True())
3300 PossibleBitfield = true;
3301 // If the next token starts a type-specifier-seq, it may be either a
3302 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003303 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003304 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003305 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003306 GetLookAheadToken(2).getKind() == tok::semi) {
3307 // Consume the ':'.
3308 ConsumeToken();
3309 } else {
3310 // We have the start of a type-specifier-seq, so we have to perform
3311 // tentative parsing to determine whether we have an expression or a
3312 // type.
3313 TentativeParsingAction TPA(*this);
3314
3315 // Consume the ':'.
3316 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003317
3318 // If we see a type specifier followed by an open-brace, we have an
3319 // ambiguity between an underlying type and a C++11 braced
3320 // function-style cast. Resolve this by always treating it as an
3321 // underlying type.
3322 // FIXME: The standard is not entirely clear on how to disambiguate in
3323 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003324 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003325 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003326 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003327 // We'll parse this as a bitfield later.
3328 PossibleBitfield = true;
3329 TPA.Revert();
3330 } else {
3331 // We have a type-specifier-seq.
3332 TPA.Commit();
3333 }
3334 }
3335 } else {
3336 // Consume the ':'.
3337 ConsumeToken();
3338 }
3339
3340 if (!PossibleBitfield) {
3341 SourceRange Range;
3342 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003343
Richard Smith80ad52f2013-01-02 11:42:31 +00003344 if (getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00003345 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003346 } else if (!getLangOpts().ObjC2) {
3347 if (getLangOpts().CPlusPlus)
3348 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3349 else
3350 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3351 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003352 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003353 }
3354
Richard Smithbdad7a22012-01-10 01:33:14 +00003355 // There are four options here. If we have 'friend enum foo;' then this is a
3356 // friend declaration, and cannot have an accompanying definition. If we have
3357 // 'enum foo;', then this is a forward declaration. If we have
3358 // 'enum foo {...' then this is a definition. Otherwise we have something
3359 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003360 //
3361 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3362 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3363 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3364 //
John McCallf312b1e2010-08-26 23:41:50 +00003365 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003366 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003367 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003368 } else if (Tok.is(tok::l_brace)) {
3369 if (DS.isFriendSpecified()) {
3370 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3371 << SourceRange(DS.getFriendSpecLoc());
3372 ConsumeBrace();
3373 SkipUntil(tok::r_brace);
3374 TUK = Sema::TUK_Friend;
3375 } else {
3376 TUK = Sema::TUK_Definition;
3377 }
Richard Smithc9f35172012-06-25 21:37:02 +00003378 } else if (DSC != DSC_type_specifier &&
3379 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003380 (Tok.isAtStartOfLine() &&
3381 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003382 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3383 if (Tok.isNot(tok::semi)) {
3384 // A semicolon was missing after this declaration. Diagnose and recover.
3385 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3386 "enum");
3387 PP.EnterToken(Tok);
3388 Tok.setKind(tok::semi);
3389 }
John McCall13489672012-05-07 06:16:58 +00003390 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003391 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003392 }
3393
3394 // If this is an elaborated type specifier, and we delayed
3395 // diagnostics before, just merge them into the current pool.
3396 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3397 diagsFromTag.redelay();
3398 }
Richard Smith1af83c42012-03-23 03:33:32 +00003399
3400 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003401 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003402 TUK != Sema::TUK_Reference) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003403 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith1af83c42012-03-23 03:33:32 +00003404 // Skip the rest of this declarator, up until the comma or semicolon.
3405 Diag(Tok, diag::err_enum_template);
3406 SkipUntil(tok::comma, true);
3407 return;
3408 }
3409
3410 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3411 // Enumerations can't be explicitly instantiated.
3412 DS.SetTypeSpecError();
3413 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3414 return;
3415 }
3416
3417 assert(TemplateInfo.TemplateParams && "no template parameters");
3418 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3419 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003420 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003421
Sean Hunt2edf0a22012-06-23 05:07:58 +00003422 if (TUK == Sema::TUK_Reference)
3423 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003424
Douglas Gregorb9075602011-02-22 02:55:24 +00003425 if (!Name && TUK != Sema::TUK_Definition) {
3426 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003427
Douglas Gregorb9075602011-02-22 02:55:24 +00003428 // Skip the rest of this declarator, up until the comma or semicolon.
3429 SkipUntil(tok::comma, true);
3430 return;
3431 }
Richard Smith1af83c42012-03-23 03:33:32 +00003432
Douglas Gregor402abb52009-05-28 23:31:59 +00003433 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003434 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003435 const char *PrevSpec = 0;
3436 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003437 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003438 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003439 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003440 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003441 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003442
Douglas Gregor48c89f42010-04-24 16:38:41 +00003443 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003444 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003445 // dependent tag.
3446 if (!Name) {
3447 DS.SetTypeSpecError();
3448 Diag(Tok, diag::err_expected_type_name_after_typename);
3449 return;
3450 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003451
Douglas Gregor23c94db2010-07-02 17:43:08 +00003452 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003453 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003454 NameLoc);
3455 if (Type.isInvalid()) {
3456 DS.SetTypeSpecError();
3457 return;
3458 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003459
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003460 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3461 NameLoc.isValid() ? NameLoc : StartLoc,
3462 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003463 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003464
Douglas Gregor48c89f42010-04-24 16:38:41 +00003465 return;
3466 }
Mike Stump1eb44332009-09-09 15:08:12 +00003467
John McCalld226f652010-08-21 09:40:31 +00003468 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003469 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003470 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003471 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003472 ConsumeBrace();
3473 SkipUntil(tok::r_brace);
3474 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003475
Douglas Gregor48c89f42010-04-24 16:38:41 +00003476 DS.SetTypeSpecError();
3477 return;
3478 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003479
Richard Smithc9f35172012-06-25 21:37:02 +00003480 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003481 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003482
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003483 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3484 NameLoc.isValid() ? NameLoc : StartLoc,
3485 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003486 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003487}
3488
3489/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3490/// enumerator-list:
3491/// enumerator
3492/// enumerator-list ',' enumerator
3493/// enumerator:
3494/// enumeration-constant
3495/// enumeration-constant '=' constant-expression
3496/// enumeration-constant:
3497/// identifier
3498///
John McCalld226f652010-08-21 09:40:31 +00003499void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003500 // Enter the scope of the enum body and start the definition.
3501 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003502 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003503
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003504 BalancedDelimiterTracker T(*this, tok::l_brace);
3505 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003506
Chris Lattner7946dd32007-08-27 17:24:30 +00003507 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003508 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003509 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003510
Chris Lattner5f9e2722011-07-23 10:55:15 +00003511 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003512
John McCalld226f652010-08-21 09:40:31 +00003513 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003514
Reid Spencer5f016e22007-07-11 17:01:13 +00003515 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003516 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003517 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3518 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003519
John McCall5b629aa2010-10-22 23:36:17 +00003520 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003521 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003522 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003523 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003524 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003525
Reid Spencer5f016e22007-07-11 17:01:13 +00003526 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003527 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003528 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003529
Chris Lattner04d66662007-10-09 17:33:22 +00003530 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003531 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003532 AssignedVal = ParseConstantExpression();
3533 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003534 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003535 }
Mike Stump1eb44332009-09-09 15:08:12 +00003536
Reid Spencer5f016e22007-07-11 17:01:13 +00003537 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003538 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3539 LastEnumConstDecl,
3540 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003541 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003542 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003543 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003544
Reid Spencer5f016e22007-07-11 17:01:13 +00003545 EnumConstantDecls.push_back(EnumConstDecl);
3546 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003547
Douglas Gregor751f6922010-09-07 14:51:08 +00003548 if (Tok.is(tok::identifier)) {
3549 // We're missing a comma between enumerators.
3550 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003551 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003552 << FixItHint::CreateInsertion(Loc, ", ");
3553 continue;
3554 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003555
Chris Lattner04d66662007-10-09 17:33:22 +00003556 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003557 break;
3558 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003559
Richard Smith7fe62082011-10-15 05:09:34 +00003560 if (Tok.isNot(tok::identifier)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003561 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003562 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3563 diag::ext_enumerator_list_comma_cxx :
3564 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003565 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith80ad52f2013-01-02 11:42:31 +00003566 else if (getLangOpts().CPlusPlus11)
Richard Smith7fe62082011-10-15 05:09:34 +00003567 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3568 << FixItHint::CreateRemoval(CommaLoc);
3569 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003570 }
Mike Stump1eb44332009-09-09 15:08:12 +00003571
Reid Spencer5f016e22007-07-11 17:01:13 +00003572 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003573 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003574
Reid Spencer5f016e22007-07-11 17:01:13 +00003575 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003576 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003577 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003578
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003579 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3580 EnumDecl, EnumConstantDecls.data(),
3581 EnumConstantDecls.size(), getCurScope(),
3582 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003583
Douglas Gregor72de6672009-01-08 20:45:30 +00003584 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003585 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3586 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003587
3588 // The next token must be valid after an enum definition. If not, a ';'
3589 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003590 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3591 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003592 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3593 // Push this token back into the preprocessor and change our current token
3594 // to ';' so that the rest of the code recovers as though there were an
3595 // ';' after the definition.
3596 PP.EnterToken(Tok);
3597 Tok.setKind(tok::semi);
3598 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003599}
3600
3601/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003602/// start of a type-qualifier-list.
3603bool Parser::isTypeQualifier() const {
3604 switch (Tok.getKind()) {
3605 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003606
3607 // type-qualifier only in OpenCL
3608 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003609 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003610
Steve Naroff5f8aa692008-02-11 23:15:56 +00003611 // type-qualifier
3612 case tok::kw_const:
3613 case tok::kw_volatile:
3614 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003615 case tok::kw___private:
3616 case tok::kw___local:
3617 case tok::kw___global:
3618 case tok::kw___constant:
3619 case tok::kw___read_only:
3620 case tok::kw___read_write:
3621 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003622 return true;
3623 }
3624}
3625
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003626/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3627/// is definitely a type-specifier. Return false if it isn't part of a type
3628/// specifier or if we're not sure.
3629bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3630 switch (Tok.getKind()) {
3631 default: return false;
3632 // type-specifiers
3633 case tok::kw_short:
3634 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003635 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003636 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003637 case tok::kw_signed:
3638 case tok::kw_unsigned:
3639 case tok::kw__Complex:
3640 case tok::kw__Imaginary:
3641 case tok::kw_void:
3642 case tok::kw_char:
3643 case tok::kw_wchar_t:
3644 case tok::kw_char16_t:
3645 case tok::kw_char32_t:
3646 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003647 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003648 case tok::kw_float:
3649 case tok::kw_double:
3650 case tok::kw_bool:
3651 case tok::kw__Bool:
3652 case tok::kw__Decimal32:
3653 case tok::kw__Decimal64:
3654 case tok::kw__Decimal128:
3655 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003656
Guy Benyeib13621d2012-12-18 14:38:23 +00003657 // OpenCL specific types:
3658 case tok::kw_image1d_t:
3659 case tok::kw_image1d_array_t:
3660 case tok::kw_image1d_buffer_t:
3661 case tok::kw_image2d_t:
3662 case tok::kw_image2d_array_t:
3663 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003664 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003665 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003666
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003667 // struct-or-union-specifier (C99) or class-specifier (C++)
3668 case tok::kw_class:
3669 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003670 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003671 case tok::kw_union:
3672 // enum-specifier
3673 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003674
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003675 // typedef-name
3676 case tok::annot_typename:
3677 return true;
3678 }
3679}
3680
Steve Naroff5f8aa692008-02-11 23:15:56 +00003681/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003682/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003683bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003684 switch (Tok.getKind()) {
3685 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003686
Chris Lattner166a8fc2009-01-04 23:41:41 +00003687 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003688 if (TryAltiVecVectorToken())
3689 return true;
3690 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003691 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003692 // Annotate typenames and C++ scope specifiers. If we get one, just
3693 // recurse to handle whatever we get.
3694 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003695 return true;
3696 if (Tok.is(tok::identifier))
3697 return false;
3698 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003699
Chris Lattner166a8fc2009-01-04 23:41:41 +00003700 case tok::coloncolon: // ::foo::bar
3701 if (NextToken().is(tok::kw_new) || // ::new
3702 NextToken().is(tok::kw_delete)) // ::delete
3703 return false;
3704
Chris Lattner166a8fc2009-01-04 23:41:41 +00003705 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003706 return true;
3707 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003708
Reid Spencer5f016e22007-07-11 17:01:13 +00003709 // GNU attributes support.
3710 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003711 // GNU typeof support.
3712 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003713
Reid Spencer5f016e22007-07-11 17:01:13 +00003714 // type-specifiers
3715 case tok::kw_short:
3716 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003717 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003718 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003719 case tok::kw_signed:
3720 case tok::kw_unsigned:
3721 case tok::kw__Complex:
3722 case tok::kw__Imaginary:
3723 case tok::kw_void:
3724 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003725 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003726 case tok::kw_char16_t:
3727 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003728 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003729 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003730 case tok::kw_float:
3731 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003732 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003733 case tok::kw__Bool:
3734 case tok::kw__Decimal32:
3735 case tok::kw__Decimal64:
3736 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003737 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003738
Guy Benyeib13621d2012-12-18 14:38:23 +00003739 // OpenCL specific types:
3740 case tok::kw_image1d_t:
3741 case tok::kw_image1d_array_t:
3742 case tok::kw_image1d_buffer_t:
3743 case tok::kw_image2d_t:
3744 case tok::kw_image2d_array_t:
3745 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003746 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003747 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003748
Chris Lattner99dc9142008-04-13 18:59:07 +00003749 // struct-or-union-specifier (C99) or class-specifier (C++)
3750 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003751 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003752 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003753 case tok::kw_union:
3754 // enum-specifier
3755 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003756
Reid Spencer5f016e22007-07-11 17:01:13 +00003757 // type-qualifier
3758 case tok::kw_const:
3759 case tok::kw_volatile:
3760 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003761
John McCallb8a8de32012-11-14 00:49:39 +00003762 // Debugger support.
3763 case tok::kw___unknown_anytype:
3764
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003765 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003766 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003767 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003768
Chris Lattner7c186be2008-10-20 00:25:30 +00003769 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3770 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003771 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003772
Steve Naroff239f0732008-12-25 14:16:32 +00003773 case tok::kw___cdecl:
3774 case tok::kw___stdcall:
3775 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003776 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003777 case tok::kw___w64:
3778 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003779 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003780 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003781 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003782
3783 case tok::kw___private:
3784 case tok::kw___local:
3785 case tok::kw___global:
3786 case tok::kw___constant:
3787 case tok::kw___read_only:
3788 case tok::kw___read_write:
3789 case tok::kw___write_only:
3790
Eli Friedman290eeb02009-06-08 23:27:34 +00003791 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003792
3793 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003794 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003795
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003796 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003797 case tok::kw__Atomic:
3798 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003799 }
3800}
3801
3802/// isDeclarationSpecifier() - Return true if the current token is part of a
3803/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003804///
3805/// \param DisambiguatingWithExpression True to indicate that the purpose of
3806/// this check is to disambiguate between an expression and a declaration.
3807bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003808 switch (Tok.getKind()) {
3809 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003810
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003811 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003812 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003813
Chris Lattner166a8fc2009-01-04 23:41:41 +00003814 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003815 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003816 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003817 return false;
John Thompson82287d12010-02-05 00:12:22 +00003818 if (TryAltiVecVectorToken())
3819 return true;
3820 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003821 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003822 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003823 // Annotate typenames and C++ scope specifiers. If we get one, just
3824 // recurse to handle whatever we get.
3825 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003826 return true;
3827 if (Tok.is(tok::identifier))
3828 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003829
Douglas Gregor9497a732010-09-16 01:51:54 +00003830 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00003831 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00003832 // expression is permitted, then this is probably a class message send
3833 // missing the initial '['. In this case, we won't consider this to be
3834 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00003835 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00003836 isStartOfObjCClassMessageMissingOpenBracket())
3837 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003838
John McCall9ba61662010-02-26 08:45:28 +00003839 return isDeclarationSpecifier();
3840
Chris Lattner166a8fc2009-01-04 23:41:41 +00003841 case tok::coloncolon: // ::foo::bar
3842 if (NextToken().is(tok::kw_new) || // ::new
3843 NextToken().is(tok::kw_delete)) // ::delete
3844 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003845
Chris Lattner166a8fc2009-01-04 23:41:41 +00003846 // Annotate typenames and C++ scope specifiers. If we get one, just
3847 // recurse to handle whatever we get.
3848 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003849 return true;
3850 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003851
Reid Spencer5f016e22007-07-11 17:01:13 +00003852 // storage-class-specifier
3853 case tok::kw_typedef:
3854 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003855 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003856 case tok::kw_static:
3857 case tok::kw_auto:
3858 case tok::kw_register:
3859 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003860
Douglas Gregor8d267c52011-09-09 02:06:17 +00003861 // Modules
3862 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00003863
John McCallb8a8de32012-11-14 00:49:39 +00003864 // Debugger support
3865 case tok::kw___unknown_anytype:
3866
Reid Spencer5f016e22007-07-11 17:01:13 +00003867 // type-specifiers
3868 case tok::kw_short:
3869 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003870 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003871 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003872 case tok::kw_signed:
3873 case tok::kw_unsigned:
3874 case tok::kw__Complex:
3875 case tok::kw__Imaginary:
3876 case tok::kw_void:
3877 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003878 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003879 case tok::kw_char16_t:
3880 case tok::kw_char32_t:
3881
Reid Spencer5f016e22007-07-11 17:01:13 +00003882 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003883 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003884 case tok::kw_float:
3885 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003886 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003887 case tok::kw__Bool:
3888 case tok::kw__Decimal32:
3889 case tok::kw__Decimal64:
3890 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003891 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003892
Guy Benyeib13621d2012-12-18 14:38:23 +00003893 // OpenCL specific types:
3894 case tok::kw_image1d_t:
3895 case tok::kw_image1d_array_t:
3896 case tok::kw_image1d_buffer_t:
3897 case tok::kw_image2d_t:
3898 case tok::kw_image2d_array_t:
3899 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003900 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003901 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003902
Chris Lattner99dc9142008-04-13 18:59:07 +00003903 // struct-or-union-specifier (C99) or class-specifier (C++)
3904 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003905 case tok::kw_struct:
3906 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00003907 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003908 // enum-specifier
3909 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003910
Reid Spencer5f016e22007-07-11 17:01:13 +00003911 // type-qualifier
3912 case tok::kw_const:
3913 case tok::kw_volatile:
3914 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003915
Reid Spencer5f016e22007-07-11 17:01:13 +00003916 // function-specifier
3917 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003918 case tok::kw_virtual:
3919 case tok::kw_explicit:
Richard Smithde03c152013-01-17 22:16:11 +00003920 case tok::kw__Noreturn:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003921
Richard Smith4cd81c52013-01-29 09:02:09 +00003922 // alignment-specifier
3923 case tok::kw__Alignas:
3924
Richard Smith53aec2a2012-10-25 00:00:53 +00003925 // friend keyword.
3926 case tok::kw_friend:
3927
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003928 // static_assert-declaration
3929 case tok::kw__Static_assert:
3930
Chris Lattner1ef08762007-08-09 17:01:07 +00003931 // GNU typeof support.
3932 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003933
Chris Lattner1ef08762007-08-09 17:01:07 +00003934 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003935 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00003936
Richard Smith53aec2a2012-10-25 00:00:53 +00003937 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003938 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00003939 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00003940
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003941 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003942 case tok::kw__Atomic:
3943 return true;
3944
Chris Lattnerf3948c42008-07-26 03:38:44 +00003945 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3946 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003947 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003948
Douglas Gregord9d75e52011-04-27 05:41:15 +00003949 // typedef-name
3950 case tok::annot_typename:
3951 return !DisambiguatingWithExpression ||
3952 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00003953
Steve Naroff47f52092009-01-06 19:34:12 +00003954 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003955 case tok::kw___cdecl:
3956 case tok::kw___stdcall:
3957 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003958 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003959 case tok::kw___w64:
3960 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003961 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003962 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003963 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003964 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003965
3966 case tok::kw___private:
3967 case tok::kw___local:
3968 case tok::kw___global:
3969 case tok::kw___constant:
3970 case tok::kw___read_only:
3971 case tok::kw___read_write:
3972 case tok::kw___write_only:
3973
Eli Friedman290eeb02009-06-08 23:27:34 +00003974 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003975 }
3976}
3977
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003978bool Parser::isConstructorDeclarator() {
3979 TentativeParsingAction TPA(*this);
3980
3981 // Parse the C++ scope specifier.
3982 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00003983 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003984 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003985 TPA.Revert();
3986 return false;
3987 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003988
3989 // Parse the constructor name.
3990 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3991 // We already know that we have a constructor name; just consume
3992 // the token.
3993 ConsumeToken();
3994 } else {
3995 TPA.Revert();
3996 return false;
3997 }
3998
Richard Smith22592862012-03-27 23:05:05 +00003999 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004000 if (Tok.isNot(tok::l_paren)) {
4001 TPA.Revert();
4002 return false;
4003 }
4004 ConsumeParen();
4005
Richard Smith22592862012-03-27 23:05:05 +00004006 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4007 // that we have a constructor.
4008 if (Tok.is(tok::r_paren) ||
4009 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004010 TPA.Revert();
4011 return true;
4012 }
4013
4014 // If we need to, enter the specified scope.
4015 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00004016 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004017 DeclScopeObj.EnterDeclaratorScope();
4018
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004019 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00004020 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004021 MaybeParseMicrosoftAttributes(Attrs);
4022
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004023 // Check whether the next token(s) are part of a declaration
4024 // specifier, in which case we have the start of a parameter and,
4025 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00004026 bool IsConstructor = false;
4027 if (isDeclarationSpecifier())
4028 IsConstructor = true;
4029 else if (Tok.is(tok::identifier) ||
4030 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4031 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4032 // This might be a parenthesized member name, but is more likely to
4033 // be a constructor declaration with an invalid argument type. Keep
4034 // looking.
4035 if (Tok.is(tok::annot_cxxscope))
4036 ConsumeToken();
4037 ConsumeToken();
4038
4039 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00004040 // which must have one of the following syntactic forms (see the
4041 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00004042 switch (Tok.getKind()) {
4043 case tok::l_paren:
4044 // C(X ( int));
4045 case tok::l_square:
4046 // C(X [ 5]);
4047 // C(X [ [attribute]]);
4048 case tok::coloncolon:
4049 // C(X :: Y);
4050 // C(X :: *p);
4051 case tok::r_paren:
4052 // C(X )
4053 // Assume this isn't a constructor, rather than assuming it's a
4054 // constructor with an unnamed parameter of an ill-formed type.
4055 break;
4056
4057 default:
4058 IsConstructor = true;
4059 break;
4060 }
4061 }
4062
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004063 TPA.Revert();
4064 return IsConstructor;
4065}
Reid Spencer5f016e22007-07-11 17:01:13 +00004066
4067/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00004068/// type-qualifier-list: [C99 6.7.5]
4069/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004070/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004071/// [ only if VendorAttributesAllowed=true ]
4072/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004073/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004074/// [ only if VendorAttributesAllowed=true ]
4075/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith4e24f0f2013-01-02 12:01:23 +00004076/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik52fc3142010-09-03 01:29:35 +00004077/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00004078///
Dawn Perchik52fc3142010-09-03 01:29:35 +00004079void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4080 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00004081 bool CXX11AttributesAllowed) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004082 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00004083 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00004084 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00004085 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004086 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004087 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004088
4089 SourceLocation EndLoc;
4090
Reid Spencer5f016e22007-07-11 17:01:13 +00004091 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004092 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004093 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004094 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004095 SourceLocation Loc = Tok.getLocation();
4096
4097 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004098 case tok::code_completion:
4099 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004100 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004101
Reid Spencer5f016e22007-07-11 17:01:13 +00004102 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004103 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004104 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004105 break;
4106 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004107 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004108 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004109 break;
4110 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004111 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004112 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004113 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004114
4115 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00004116 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004117 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004118 goto DoneWithTypeQuals;
4119 case tok::kw___private:
4120 case tok::kw___global:
4121 case tok::kw___local:
4122 case tok::kw___constant:
4123 case tok::kw___read_only:
4124 case tok::kw___write_only:
4125 case tok::kw___read_write:
4126 ParseOpenCLQualifiers(DS);
4127 break;
4128
Eli Friedman290eeb02009-06-08 23:27:34 +00004129 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004130 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004131 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004132 case tok::kw___cdecl:
4133 case tok::kw___stdcall:
4134 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004135 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004136 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004137 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004138 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004139 continue;
4140 }
4141 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004142 case tok::kw___pascal:
4143 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004144 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004145 continue;
4146 }
4147 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004148 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004149 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004150 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004151 continue; // do *not* consume the next token!
4152 }
4153 // otherwise, FALL THROUGH!
4154 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004155 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004156 // If this is not a type-qualifier token, we're done reading type
4157 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004158 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004159 if (EndLoc.isValid())
4160 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004161 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004162 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004163
Reid Spencer5f016e22007-07-11 17:01:13 +00004164 // If the specifier combination wasn't legal, issue a diagnostic.
4165 if (isInvalid) {
4166 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004167 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004168 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004169 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004170 }
4171}
4172
4173
4174/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4175///
4176void Parser::ParseDeclarator(Declarator &D) {
4177 /// This implements the 'declarator' production in the C grammar, then checks
4178 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004179 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004180}
4181
Richard Smith9988f282012-03-29 01:16:42 +00004182static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4183 if (Kind == tok::star || Kind == tok::caret)
4184 return true;
4185
4186 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4187 if (!Lang.CPlusPlus)
4188 return false;
4189
4190 return Kind == tok::amp || Kind == tok::ampamp;
4191}
4192
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004193/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4194/// is parsed by the function passed to it. Pass null, and the direct-declarator
4195/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004196/// ptr-operator production.
4197///
Richard Smith0706df42011-10-19 21:33:05 +00004198/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004199/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4200/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004201///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004202/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4203/// [C] pointer[opt] direct-declarator
4204/// [C++] direct-declarator
4205/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004206///
4207/// pointer: [C99 6.7.5]
4208/// '*' type-qualifier-list[opt]
4209/// '*' type-qualifier-list[opt] pointer
4210///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004211/// ptr-operator:
4212/// '*' cv-qualifier-seq[opt]
4213/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004214/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004215/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004216/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004217/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004218void Parser::ParseDeclaratorInternal(Declarator &D,
4219 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004220 if (Diags.hasAllExtensionsSilenced())
4221 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004222
Sebastian Redlf30208a2009-01-24 21:16:55 +00004223 // C++ member pointers start with a '::' or a nested-name.
4224 // Member pointers get special handling, since there's no place for the
4225 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004226 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004227 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4228 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004229 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4230 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004231 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004232 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004233
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004234 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004235 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004236 // The scope spec really belongs to the direct-declarator.
Richard Smith6a502c42013-01-08 22:43:49 +00004237 if (D.mayHaveIdentifier())
4238 D.getCXXScopeSpec() = SS;
4239 else
4240 AnnotateScopeToken(SS, true);
4241
Sebastian Redlf30208a2009-01-24 21:16:55 +00004242 if (DirectDeclParser)
4243 (this->*DirectDeclParser)(D);
4244 return;
4245 }
4246
4247 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004248 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004249 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004250 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004251 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004252
4253 // Recurse to parse whatever is left.
4254 ParseDeclaratorInternal(D, DirectDeclParser);
4255
4256 // Sema will have to catch (syntactically invalid) pointers into global
4257 // scope. It has to catch pointers into namespace scope anyway.
4258 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004259 Loc),
4260 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004261 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004262 return;
4263 }
4264 }
4265
4266 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004267 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004268 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004269 if (DirectDeclParser)
4270 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004271 return;
4272 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004273
Sebastian Redl05532f22009-03-15 22:02:01 +00004274 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4275 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004276 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004277 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004278
Chris Lattner9af55002009-03-27 04:18:06 +00004279 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004280 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004281 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004282
Richard Smith6ee326a2012-04-10 01:32:12 +00004283 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004284 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004285 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004286
Reid Spencer5f016e22007-07-11 17:01:13 +00004287 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004288 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004289 if (Kind == tok::star)
4290 // Remember that we parsed a pointer type, and remember the type-quals.
4291 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004292 DS.getConstSpecLoc(),
4293 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004294 DS.getRestrictSpecLoc()),
4295 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004296 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004297 else
4298 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004299 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004300 Loc),
4301 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004302 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004303 } else {
4304 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004305 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004306
Sebastian Redl743de1f2009-03-23 00:00:23 +00004307 // Complain about rvalue references in C++03, but then go on and build
4308 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004309 if (Kind == tok::ampamp)
Richard Smith80ad52f2013-01-02 11:42:31 +00004310 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004311 diag::warn_cxx98_compat_rvalue_reference :
4312 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004313
Richard Smith6ee326a2012-04-10 01:32:12 +00004314 // GNU-style and C++11 attributes are allowed here, as is restrict.
4315 ParseTypeQualifierListOpt(DS);
4316 D.ExtendWithDeclSpec(DS);
4317
Reid Spencer5f016e22007-07-11 17:01:13 +00004318 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4319 // cv-qualifiers are introduced through the use of a typedef or of a
4320 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004321 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4322 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4323 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004324 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004325 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4326 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004327 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00004328 }
4329
4330 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004331 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004332
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004333 if (D.getNumTypeObjects() > 0) {
4334 // C++ [dcl.ref]p4: There shall be no references to references.
4335 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4336 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004337 if (const IdentifierInfo *II = D.getIdentifier())
4338 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4339 << II;
4340 else
4341 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4342 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004343
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004344 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004345 // can go ahead and build the (technically ill-formed)
4346 // declarator: reference collapsing will take care of it.
4347 }
4348 }
4349
Reid Spencer5f016e22007-07-11 17:01:13 +00004350 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00004351 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004352 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004353 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004354 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004355 }
4356}
4357
Richard Smith9988f282012-03-29 01:16:42 +00004358static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4359 SourceLocation EllipsisLoc) {
4360 if (EllipsisLoc.isValid()) {
4361 FixItHint Insertion;
4362 if (!D.getEllipsisLoc().isValid()) {
4363 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4364 D.setEllipsisLoc(EllipsisLoc);
4365 }
4366 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4367 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4368 }
4369}
4370
Reid Spencer5f016e22007-07-11 17:01:13 +00004371/// ParseDirectDeclarator
4372/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004373/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004374/// '(' declarator ')'
4375/// [GNU] '(' attributes declarator ')'
4376/// [C90] direct-declarator '[' constant-expression[opt] ']'
4377/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4378/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4379/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4380/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004381/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4382/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004383/// direct-declarator '(' parameter-type-list ')'
4384/// direct-declarator '(' identifier-list[opt] ')'
4385/// [GNU] direct-declarator '(' parameter-forward-declarations
4386/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004387/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4388/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004389/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4390/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4391/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004392/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004393/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004394///
4395/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004396/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004397/// '::'[opt] nested-name-specifier[opt] type-name
4398///
4399/// id-expression: [C++ 5.1]
4400/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004401/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004402///
4403/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004404/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004405/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004406/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004407/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004408/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004409///
Richard Smith5d8388c2012-03-27 01:42:32 +00004410/// Note, any additional constructs added here may need corresponding changes
4411/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004412void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004413 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004414
David Blaikie4e4d0842012-03-11 07:00:24 +00004415 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004416 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004417 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004418 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4419 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004420 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004421 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004422 }
4423
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004424 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004425 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004426 // Change the declaration context for name lookup, until this function
4427 // is exited (and the declarator has been parsed).
4428 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004429 }
4430
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004431 // C++0x [dcl.fct]p14:
4432 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004433 // of a parameter-declaration-clause without a preceding comma. In
4434 // this case, the ellipsis is parsed as part of the
4435 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004436 // parameter pack that has not been expanded; otherwise, it is parsed
4437 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004438 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004439 !((D.getContext() == Declarator::PrototypeContext ||
4440 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004441 NextToken().is(tok::r_paren) &&
Richard Smith30f2a742013-02-20 20:19:27 +00004442 !D.hasGroupingParens() &&
Richard Smith9988f282012-03-29 01:16:42 +00004443 !Actions.containsUnexpandedParameterPacks(D))) {
4444 SourceLocation EllipsisLoc = ConsumeToken();
4445 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4446 // The ellipsis was put in the wrong place. Recover, and explain to
4447 // the user what they should have done.
4448 ParseDeclarator(D);
4449 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4450 return;
4451 } else
4452 D.setEllipsisLoc(EllipsisLoc);
4453
4454 // The ellipsis can't be followed by a parenthesized declarator. We
4455 // check for that in ParseParenDeclarator, after we have disambiguated
4456 // the l_paren token.
4457 }
4458
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004459 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4460 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4461 // We found something that indicates the start of an unqualified-id.
4462 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004463 bool AllowConstructorName;
4464 if (D.getDeclSpec().hasTypeSpecifier())
4465 AllowConstructorName = false;
4466 else if (D.getCXXScopeSpec().isSet())
4467 AllowConstructorName =
4468 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00004469 D.getContext() == Declarator::MemberContext);
John McCallba9d8532010-04-13 06:39:49 +00004470 else
4471 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4472
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004473 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004474 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4475 /*EnteringContext=*/true,
4476 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004477 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004478 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004479 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004480 D.getName()) ||
4481 // Once we're past the identifier, if the scope was bad, mark the
4482 // whole declarator bad.
4483 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004484 D.SetIdentifier(0, Tok.getLocation());
4485 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004486 } else {
4487 // Parsed the unqualified-id; update range information and move along.
4488 if (D.getSourceRange().getBegin().isInvalid())
4489 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4490 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004491 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004492 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004493 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004494 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004495 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004496 "There's a C++-specific check for tok::identifier above");
4497 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4498 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4499 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004500 goto PastIdentifier;
4501 }
Richard Smith9988f282012-03-29 01:16:42 +00004502
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004503 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004504 // direct-declarator: '(' declarator ')'
4505 // direct-declarator: '(' attributes declarator ')'
4506 // Example: 'char (*X)' or 'int (*XX)(void)'
4507 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004508
4509 // If the declarator was parenthesized, we entered the declarator
4510 // scope when parsing the parenthesized declarator, then exited
4511 // the scope already. Re-enter the scope, if we need to.
4512 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004513 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004514 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004515 if (!D.isInvalidType() &&
4516 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004517 // Change the declaration context for name lookup, until this function
4518 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004519 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004520 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004521 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004522 // This could be something simple like "int" (in which case the declarator
4523 // portion is empty), if an abstract-declarator is allowed.
4524 D.SetIdentifier(0, Tok.getLocation());
Richard Smith30f2a742013-02-20 20:19:27 +00004525
4526 // The grammar for abstract-pack-declarator does not allow grouping parens.
4527 // FIXME: Revisit this once core issue 1488 is resolved.
4528 if (D.hasEllipsis() && D.hasGroupingParens())
4529 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4530 diag::ext_abstract_pack_declarator_parens);
Reid Spencer5f016e22007-07-11 17:01:13 +00004531 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004532 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004533 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004534 if (D.getContext() == Declarator::MemberContext)
4535 Diag(Tok, diag::err_expected_member_name_or_semi)
4536 << D.getDeclSpec().getSourceRange();
Richard Trieudb55c04c2013-01-26 02:31:38 +00004537 else if (getLangOpts().CPlusPlus) {
4538 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4539 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4540 else
4541 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4542 } else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004543 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004544 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004545 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004546 }
Mike Stump1eb44332009-09-09 15:08:12 +00004547
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004548 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004549 assert(D.isPastIdentifier() &&
4550 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004551
Richard Smith6ee326a2012-04-10 01:32:12 +00004552 // Don't parse attributes unless we have parsed an unparenthesized name.
4553 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith4e24f0f2013-01-02 12:01:23 +00004554 MaybeParseCXX11Attributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004555
Reid Spencer5f016e22007-07-11 17:01:13 +00004556 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004557 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004558 // Enter function-declaration scope, limiting any declarators to the
4559 // function prototype scope, including parameter declarators.
4560 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004561 Scope::FunctionPrototypeScope|Scope::DeclScope|
4562 (D.isFunctionDeclaratorAFunctionDeclaration()
4563 ? Scope::FunctionDeclarationScope : 0));
4564
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004565 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4566 // In such a case, check if we actually have a function declarator; if it
4567 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004568 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004569 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4570 // The name of the declarator, if any, is tentatively declared within
4571 // a possible direct initializer.
4572 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4573 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4574 TentativelyDeclaredIdentifiers.pop_back();
4575 if (!IsFunctionDecl)
4576 break;
4577 }
John McCall0b7e6782011-03-24 11:26:52 +00004578 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004579 BalancedDelimiterTracker T(*this, tok::l_paren);
4580 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004581 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004582 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004583 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004584 ParseBracketDeclarator(D);
4585 } else {
4586 break;
4587 }
4588 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004589}
Reid Spencer5f016e22007-07-11 17:01:13 +00004590
Chris Lattneref4715c2008-04-06 05:45:57 +00004591/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4592/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004593/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004594/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4595///
4596/// direct-declarator:
4597/// '(' declarator ')'
4598/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004599/// direct-declarator '(' parameter-type-list ')'
4600/// direct-declarator '(' identifier-list[opt] ')'
4601/// [GNU] direct-declarator '(' parameter-forward-declarations
4602/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004603///
4604void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004605 BalancedDelimiterTracker T(*this, tok::l_paren);
4606 T.consumeOpen();
4607
Chris Lattneref4715c2008-04-06 05:45:57 +00004608 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004609
Chris Lattner7399ee02008-10-20 02:05:46 +00004610 // Eat any attributes before we look at whether this is a grouping or function
4611 // declarator paren. If this is a grouping paren, the attribute applies to
4612 // the type being built up, for example:
4613 // int (__attribute__(()) *x)(long y)
4614 // If this ends up not being a grouping paren, the attribute applies to the
4615 // first argument, for example:
4616 // int (__attribute__(()) int x)
4617 // In either case, we need to eat any attributes to be able to determine what
4618 // sort of paren this is.
4619 //
John McCall0b7e6782011-03-24 11:26:52 +00004620 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004621 bool RequiresArg = false;
4622 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004623 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004624
Chris Lattner7399ee02008-10-20 02:05:46 +00004625 // We require that the argument list (if this is a non-grouping paren) be
4626 // present even if the attribute list was empty.
4627 RequiresArg = true;
4628 }
Chad Rosier9cab1c92012-12-21 21:22:20 +00004629
Steve Naroff239f0732008-12-25 14:16:32 +00004630 // Eat any Microsoft extensions.
Chad Rosier9cab1c92012-12-21 21:22:20 +00004631 ParseMicrosoftTypeAttributes(attrs);
4632
Dawn Perchik52fc3142010-09-03 01:29:35 +00004633 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004634 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004635 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004636
Chris Lattneref4715c2008-04-06 05:45:57 +00004637 // If we haven't past the identifier yet (or where the identifier would be
4638 // stored, if this is an abstract declarator), then this is probably just
4639 // grouping parens. However, if this could be an abstract-declarator, then
4640 // this could also be the start of function arguments (consider 'void()').
4641 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004642
Chris Lattneref4715c2008-04-06 05:45:57 +00004643 if (!D.mayOmitIdentifier()) {
4644 // If this can't be an abstract-declarator, this *must* be a grouping
4645 // paren, because we haven't seen the identifier yet.
4646 isGrouping = true;
4647 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004648 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4649 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004650 isDeclarationSpecifier() || // 'int(int)' is a function.
4651 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004652 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4653 // considered to be a type, not a K&R identifier-list.
4654 isGrouping = false;
4655 } else {
4656 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4657 isGrouping = true;
4658 }
Mike Stump1eb44332009-09-09 15:08:12 +00004659
Chris Lattneref4715c2008-04-06 05:45:57 +00004660 // If this is a grouping paren, handle:
4661 // direct-declarator: '(' declarator ')'
4662 // direct-declarator: '(' attributes declarator ')'
4663 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004664 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4665 D.setEllipsisLoc(SourceLocation());
4666
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004667 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004668 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004669 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004670 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004671 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004672 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004673 T.getCloseLocation()),
4674 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004675
4676 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004677
4678 // An ellipsis cannot be placed outside parentheses.
4679 if (EllipsisLoc.isValid())
4680 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4681
Chris Lattneref4715c2008-04-06 05:45:57 +00004682 return;
4683 }
Mike Stump1eb44332009-09-09 15:08:12 +00004684
Chris Lattneref4715c2008-04-06 05:45:57 +00004685 // Okay, if this wasn't a grouping paren, it must be the start of a function
4686 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004687 // identifier (and remember where it would have been), then call into
4688 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004689 D.SetIdentifier(0, Tok.getLocation());
4690
David Blaikie42d6d0c2011-12-04 05:04:18 +00004691 // Enter function-declaration scope, limiting any declarators to the
4692 // function prototype scope, including parameter declarators.
4693 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004694 Scope::FunctionPrototypeScope | Scope::DeclScope |
4695 (D.isFunctionDeclaratorAFunctionDeclaration()
4696 ? Scope::FunctionDeclarationScope : 0));
Richard Smithb9c62612012-07-30 21:30:52 +00004697 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004698 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004699}
4700
4701/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4702/// declarator D up to a paren, which indicates that we are parsing function
4703/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004704///
Richard Smith6ee326a2012-04-10 01:32:12 +00004705/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4706/// immediately after the open paren - they should be considered to be the
4707/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004708///
Richard Smith6ee326a2012-04-10 01:32:12 +00004709/// If RequiresArg is true, then the first argument of the function is required
4710/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004711///
Richard Smith6ee326a2012-04-10 01:32:12 +00004712/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4713/// (C++11) ref-qualifier[opt], exception-specification[opt],
4714/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4715///
4716/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004717/// dynamic-exception-specification
4718/// noexcept-specification
4719///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004720void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004721 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004722 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004723 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004724 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004725 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004726 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004727 // lparen is already consumed!
4728 assert(D.isPastIdentifier() && "Should not call before identifier!");
4729
4730 // This should be true when the function has typed arguments.
4731 // Otherwise, it is treated as a K&R-style function.
4732 bool HasProto = false;
4733 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004734 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004735 // Remember where we see an ellipsis, if any.
4736 SourceLocation EllipsisLoc;
4737
4738 DeclSpec DS(AttrFactory);
4739 bool RefQualifierIsLValueRef = true;
4740 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004741 SourceLocation ConstQualifierLoc;
4742 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004743 ExceptionSpecificationType ESpecType = EST_None;
4744 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004745 SmallVector<ParsedType, 2> DynamicExceptions;
4746 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004747 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004748 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004749 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004750
James Molloy16f1f712012-02-29 10:24:19 +00004751 Actions.ActOnStartFunctionDeclarator();
4752
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004753 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4754 EndLoc is the end location for the function declarator.
4755 They differ for trailing return types. */
4756 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004757 SourceLocation LParenLoc, RParenLoc;
4758 LParenLoc = Tracker.getOpenLocation();
4759 StartLoc = LParenLoc;
4760
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004761 if (isFunctionDeclaratorIdentifierList()) {
4762 if (RequiresArg)
4763 Diag(Tok, diag::err_argument_required_after_attribute);
4764
4765 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4766
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004767 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004768 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004769 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004770 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004771 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004772 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004773 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004774 else if (RequiresArg)
4775 Diag(Tok, diag::err_argument_required_after_attribute);
4776
David Blaikie4e4d0842012-03-11 07:00:24 +00004777 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004778
4779 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004780 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004781 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004782 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004783 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004784
David Blaikie4e4d0842012-03-11 07:00:24 +00004785 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004786 // FIXME: Accept these components in any order, and produce fixits to
4787 // correct the order if the user gets it wrong. Ideally we should deal
4788 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004789
4790 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004791 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4792 if (!DS.getSourceRange().getEnd().isInvalid()) {
4793 EndLoc = DS.getSourceRange().getEnd();
4794 ConstQualifierLoc = DS.getConstSpecLoc();
4795 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4796 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004797
4798 // Parse ref-qualifier[opt].
4799 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004800 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004801 diag::warn_cxx98_compat_ref_qualifier :
4802 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004803
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004804 RefQualifierIsLValueRef = Tok.is(tok::amp);
4805 RefQualifierLoc = ConsumeToken();
4806 EndLoc = RefQualifierLoc;
4807 }
4808
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004809 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00004810 // If a declaration declares a member function or member function
4811 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004812 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00004813 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004814 // declarator.
Chad Rosier8decdee2012-06-26 22:30:43 +00004815 bool IsCXX11MemberFunction =
Richard Smith80ad52f2013-01-02 11:42:31 +00004816 getLangOpts().CPlusPlus11 &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004817 (D.getContext() == Declarator::MemberContext ||
4818 (D.getContext() == Declarator::FileContext &&
Chad Rosier8decdee2012-06-26 22:30:43 +00004819 D.getCXXScopeSpec().isValid() &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004820 Actions.CurContext->isRecord()));
4821 Sema::CXXThisScopeRAII ThisScope(Actions,
4822 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith7b19cb12013-01-14 01:55:13 +00004823 DS.getTypeQualifiers() |
4824 (D.getDeclSpec().isConstexprSpecified()
4825 ? Qualifiers::Const : 0),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004826 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004827
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004828 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004829 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004830 DynamicExceptions,
4831 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004832 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004833 if (ESpecType != EST_None)
4834 EndLoc = ESpecRange.getEnd();
4835
Richard Smith6ee326a2012-04-10 01:32:12 +00004836 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4837 // after the exception-specification.
Richard Smith4e24f0f2013-01-02 12:01:23 +00004838 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004839
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004840 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004841 LocalEndLoc = EndLoc;
Richard Smith80ad52f2013-01-02 11:42:31 +00004842 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004843 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004844 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4845 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004846 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00004847 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004848 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004849 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004850 }
4851 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004852 }
4853
4854 // Remember that we parsed a function type, and remember the attributes.
4855 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004856 IsAmbiguous,
4857 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004858 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004859 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004860 DS.getTypeQualifiers(),
4861 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004862 RefQualifierLoc, ConstQualifierLoc,
4863 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004864 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004865 ESpecType, ESpecRange.getBegin(),
4866 DynamicExceptions.data(),
4867 DynamicExceptionRanges.data(),
4868 DynamicExceptions.size(),
4869 NoexceptExpr.isUsable() ?
4870 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004871 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004872 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004873 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004874
4875 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004876}
4877
4878/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4879/// identifier list form for a K&R-style function: void foo(a,b,c)
4880///
4881/// Note that identifier-lists are only allowed for normal declarators, not for
4882/// abstract-declarators.
4883bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004884 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004885 && Tok.is(tok::identifier)
4886 && !TryAltiVecVectorToken()
4887 // K&R identifier lists can't have typedefs as identifiers, per C99
4888 // 6.7.5.3p11.
4889 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4890 // Identifier lists follow a really simple grammar: the identifiers can
4891 // be followed *only* by a ", identifier" or ")". However, K&R
4892 // identifier lists are really rare in the brave new modern world, and
4893 // it is very common for someone to typo a type in a non-K&R style
4894 // list. If we are presented with something like: "void foo(intptr x,
4895 // float y)", we don't want to start parsing the function declarator as
4896 // though it is a K&R style declarator just because intptr is an
4897 // invalid type.
4898 //
4899 // To handle this, we check to see if the token after the first
4900 // identifier is a "," or ")". Only then do we parse it as an
4901 // identifier list.
4902 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4903}
4904
4905/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4906/// we found a K&R-style identifier list instead of a typed parameter list.
4907///
4908/// After returning, ParamInfo will hold the parsed parameters.
4909///
4910/// identifier-list: [C99 6.7.5]
4911/// identifier
4912/// identifier-list ',' identifier
4913///
4914void Parser::ParseFunctionDeclaratorIdentifierList(
4915 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004916 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004917 // If there was no identifier specified for the declarator, either we are in
4918 // an abstract-declarator, or we are in a parameter declarator which was found
4919 // to be abstract. In abstract-declarators, identifier lists are not valid:
4920 // diagnose this.
4921 if (!D.getIdentifier())
4922 Diag(Tok, diag::ext_ident_list_in_param);
4923
4924 // Maintain an efficient lookup of params we have seen so far.
4925 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4926
4927 while (1) {
4928 // If this isn't an identifier, report the error and skip until ')'.
4929 if (Tok.isNot(tok::identifier)) {
4930 Diag(Tok, diag::err_expected_ident);
4931 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4932 // Forget we parsed anything.
4933 ParamInfo.clear();
4934 return;
4935 }
4936
4937 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4938
4939 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4940 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4941 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4942
4943 // Verify that the argument identifier has not already been mentioned.
4944 if (!ParamsSoFar.insert(ParmII)) {
4945 Diag(Tok, diag::err_param_redefinition) << ParmII;
4946 } else {
4947 // Remember this identifier in ParamInfo.
4948 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4949 Tok.getLocation(),
4950 0));
4951 }
4952
4953 // Eat the identifier.
4954 ConsumeToken();
4955
4956 // The list continues if we see a comma.
4957 if (Tok.isNot(tok::comma))
4958 break;
4959 ConsumeToken();
4960 }
4961}
4962
4963/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4964/// after the opening parenthesis. This function will not parse a K&R-style
4965/// identifier list.
4966///
Richard Smith6ce48a72012-04-11 04:01:28 +00004967/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4968/// caller parsed those arguments immediately after the open paren - they should
4969/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004970///
4971/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4972/// be the location of the ellipsis, if any was parsed.
4973///
Reid Spencer5f016e22007-07-11 17:01:13 +00004974/// parameter-type-list: [C99 6.7.5]
4975/// parameter-list
4976/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004977/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004978///
4979/// parameter-list: [C99 6.7.5]
4980/// parameter-declaration
4981/// parameter-list ',' parameter-declaration
4982///
4983/// parameter-declaration: [C99 6.7.5]
4984/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004985/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004986/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004987/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004988/// declaration-specifiers abstract-declarator[opt]
4989/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004990/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004991/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004992/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004993///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004994void Parser::ParseParameterDeclarationClause(
4995 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004996 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004997 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004998 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004999
Chris Lattnerf97409f2008-04-06 06:57:35 +00005000 while (1) {
5001 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00005002 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
5003 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00005004 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00005005 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00005006 }
Mike Stump1eb44332009-09-09 15:08:12 +00005007
Chris Lattnerf97409f2008-04-06 06:57:35 +00005008 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00005009 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00005010 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005011
Richard Smith6ce48a72012-04-11 04:01:28 +00005012 // Parse any C++11 attributes.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005013 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith6ce48a72012-04-11 04:01:28 +00005014
John McCall7f040a92010-12-24 02:08:15 +00005015 // Skip any Microsoft attributes before a param.
Chad Rosier16f90bf2012-12-20 20:37:53 +00005016 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall7f040a92010-12-24 02:08:15 +00005017
5018 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00005019
5020 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00005021 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005022 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00005023 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5024 // too much hassle.
5025 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00005026
Chris Lattnere64c5492009-02-27 18:38:20 +00005027 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00005028
Chris Lattnerf97409f2008-04-06 06:57:35 +00005029 // Parse the declarator. This is "PrototypeContext", because we must
5030 // accept either 'declarator' or 'abstract-declarator' here.
5031 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5032 ParseDeclarator(ParmDecl);
5033
5034 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00005035 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00005036
Chris Lattnerf97409f2008-04-06 06:57:35 +00005037 // Remember this parsed parameter in ParamInfo.
5038 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00005039
Douglas Gregor72b505b2008-12-16 21:30:33 +00005040 // DefArgToks is used when the parsing of default arguments needs
5041 // to be delayed.
5042 CachedTokens *DefArgToks = 0;
5043
Chris Lattnerf97409f2008-04-06 06:57:35 +00005044 // If no parameter was specified, verify that *something* was specified,
5045 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00005046 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5047 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005048 // Completely missing, emit error.
5049 Diag(DSStart, diag::err_missing_param);
5050 } else {
5051 // Otherwise, we have something. Add it and let semantic analysis try
5052 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00005053
Chris Lattnerf97409f2008-04-06 06:57:35 +00005054 // Inform the actions module about the parameter declarator, so it gets
5055 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00005056 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00005057
5058 // Parse the default argument, if any. We parse the default
5059 // arguments in all dialects; the semantic analysis in
5060 // ActOnParamDefaultArgument will reject the default argument in
5061 // C.
5062 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00005063 SourceLocation EqualLoc = Tok.getLocation();
5064
Chris Lattner04421082008-04-08 04:40:51 +00005065 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00005066 if (D.getContext() == Declarator::MemberContext) {
5067 // If we're inside a class definition, cache the tokens
5068 // corresponding to the default argument. We'll actually parse
5069 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00005070 // FIXME: Can we use a smart pointer for Toks?
5071 DefArgToks = new CachedTokens;
5072
Mike Stump1eb44332009-09-09 15:08:12 +00005073 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00005074 /*StopAtSemi=*/true,
5075 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005076 delete DefArgToks;
5077 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00005078 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005079 } else {
5080 // Mark the end of the default argument so that we know when to
5081 // stop when we parse it later on.
5082 Token DefArgEnd;
5083 DefArgEnd.startToken();
5084 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5085 DefArgEnd.setLocation(Tok.getLocation());
5086 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00005087 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00005088 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005089 }
Chris Lattner04421082008-04-08 04:40:51 +00005090 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005091 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00005092 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005093
Chad Rosier8decdee2012-06-26 22:30:43 +00005094 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005095 // used.
5096 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005097 Sema::PotentiallyEvaluatedIfUsed,
5098 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005099
Sebastian Redl84407ba2012-03-14 15:54:00 +00005100 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +00005101 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl3e280b52012-03-18 22:25:45 +00005102 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00005103 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00005104 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00005105 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00005106 if (DefArgResult.isInvalid()) {
5107 Actions.ActOnParamDefaultArgumentError(Param);
5108 SkipUntil(tok::comma, tok::r_paren, true, true);
5109 } else {
5110 // Inform the actions module about the default argument
5111 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005112 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005113 }
Chris Lattner04421082008-04-08 04:40:51 +00005114 }
5115 }
Mike Stump1eb44332009-09-09 15:08:12 +00005116
5117 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5118 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00005119 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005120 }
5121
5122 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00005123 if (Tok.isNot(tok::comma)) {
5124 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005125 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00005126
David Blaikie4e4d0842012-03-11 07:00:24 +00005127 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005128 // We have ellipsis without a preceding ',', which is ill-formed
5129 // in C. Complain and provide the fix.
5130 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00005131 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005132 }
5133 }
Chad Rosier8decdee2012-06-26 22:30:43 +00005134
Douglas Gregored5d6512009-09-22 21:41:40 +00005135 break;
5136 }
Mike Stump1eb44332009-09-09 15:08:12 +00005137
Chris Lattnerf97409f2008-04-06 06:57:35 +00005138 // Consume the comma.
5139 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005140 }
Mike Stump1eb44332009-09-09 15:08:12 +00005141
Chris Lattner66d28652008-04-06 06:34:08 +00005142}
Chris Lattneref4715c2008-04-06 05:45:57 +00005143
Reid Spencer5f016e22007-07-11 17:01:13 +00005144/// [C90] direct-declarator '[' constant-expression[opt] ']'
5145/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5146/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5147/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5148/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005149/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5150/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005151void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005152 if (CheckProhibitedCXX11Attribute())
5153 return;
5154
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005155 BalancedDelimiterTracker T(*this, tok::l_square);
5156 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005157
Chris Lattner378c7e42008-12-18 07:27:21 +00005158 // C array syntax has many features, but by-far the most common is [] and [4].
5159 // This code does a fast path to handle some of the most obvious cases.
5160 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005161 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005162 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005163 MaybeParseCXX11Attributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005164
Chris Lattner378c7e42008-12-18 07:27:21 +00005165 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005166 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005167 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005168 T.getOpenLocation(),
5169 T.getCloseLocation()),
5170 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005171 return;
5172 } else if (Tok.getKind() == tok::numeric_constant &&
5173 GetLookAheadToken(1).is(tok::r_square)) {
5174 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005175 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005176 ConsumeToken();
5177
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005178 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005179 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005180 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005181
Chris Lattner378c7e42008-12-18 07:27:21 +00005182 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicebf0fa82013-01-11 08:33:05 +00005183 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall7f040a92010-12-24 02:08:15 +00005184 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005185 T.getOpenLocation(),
5186 T.getCloseLocation()),
5187 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005188 return;
5189 }
Mike Stump1eb44332009-09-09 15:08:12 +00005190
Reid Spencer5f016e22007-07-11 17:01:13 +00005191 // If valid, this location is the position where we read the 'static' keyword.
5192 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005193 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005194 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005195
Reid Spencer5f016e22007-07-11 17:01:13 +00005196 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005197 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005198 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005199 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005200
Reid Spencer5f016e22007-07-11 17:01:13 +00005201 // If we haven't already read 'static', check to see if there is one after the
5202 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005203 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005204 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005205
Reid Spencer5f016e22007-07-11 17:01:13 +00005206 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5207 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005208 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005209
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005210 // Handle the case where we have '[*]' as the array size. However, a leading
5211 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005212 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005213 // infrequent, use of lookahead is not costly here.
5214 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005215 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005216
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005217 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005218 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005219 StaticLoc = SourceLocation(); // Drop the static.
5220 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005221 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005222 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005223 // Note, in C89, this production uses the constant-expr production instead
5224 // of assignment-expr. The only difference is that assignment-expr allows
5225 // things like '=' and '*='. Sema rejects these in C89 mode because they
5226 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005227
Douglas Gregore0762c92009-06-19 23:52:42 +00005228 // Parse the constant-expression or assignment-expression now (depending
5229 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005230 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005231 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005232 } else {
5233 EnterExpressionEvaluationContext Unevaluated(Actions,
5234 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005235 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005236 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005237 }
Mike Stump1eb44332009-09-09 15:08:12 +00005238
Reid Spencer5f016e22007-07-11 17:01:13 +00005239 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005240 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005241 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005242 // If the expression was invalid, skip it.
5243 SkipUntil(tok::r_square);
5244 return;
5245 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005246
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005247 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005248
John McCall0b7e6782011-03-24 11:26:52 +00005249 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005250 MaybeParseCXX11Attributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005251
Chris Lattner378c7e42008-12-18 07:27:21 +00005252 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005253 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005254 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005255 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005256 T.getOpenLocation(),
5257 T.getCloseLocation()),
5258 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005259}
5260
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005261/// [GNU] typeof-specifier:
5262/// typeof ( expressions )
5263/// typeof ( type-name )
5264/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005265///
5266void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005267 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005268 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005269 SourceLocation StartLoc = ConsumeToken();
5270
John McCallcfb708c2010-01-13 20:03:27 +00005271 const bool hasParens = Tok.is(tok::l_paren);
5272
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005273 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5274 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005275
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005276 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005277 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005278 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005279 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5280 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005281 if (hasParens)
5282 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005283
5284 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005285 // FIXME: Not accurate, the range gets one token more than it should.
5286 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005287 else
5288 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005289
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005290 if (isCastExpr) {
5291 if (!CastTy) {
5292 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005293 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005294 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005295
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005296 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005297 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005298 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5299 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005300 DiagID, CastTy))
5301 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005302 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005303 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005304
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005305 // If we get here, the operand to the typeof was an expresion.
5306 if (Operand.isInvalid()) {
5307 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005308 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005309 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005310
Eli Friedman71b8fb52012-01-21 01:01:51 +00005311 // We might need to transform the operand if it is potentially evaluated.
5312 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5313 if (Operand.isInvalid()) {
5314 DS.SetTypeSpecError();
5315 return;
5316 }
5317
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005318 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005319 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005320 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5321 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005322 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005323 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005324}
Chris Lattner1b492422010-02-28 18:33:55 +00005325
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005326/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005327/// _Atomic ( type-name )
5328///
5329void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5330 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5331
5332 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005333 BalancedDelimiterTracker T(*this, tok::l_paren);
5334 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00005335 SkipUntil(tok::r_paren);
5336 return;
5337 }
5338
5339 TypeResult Result = ParseTypeName();
5340 if (Result.isInvalid()) {
5341 SkipUntil(tok::r_paren);
5342 return;
5343 }
5344
5345 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005346 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005347
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005348 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005349 return;
5350
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005351 DS.setTypeofParensRange(T.getRange());
5352 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005353
5354 const char *PrevSpec = 0;
5355 unsigned DiagID;
5356 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5357 DiagID, Result.release()))
5358 Diag(StartLoc, DiagID) << PrevSpec;
5359}
5360
Chris Lattner1b492422010-02-28 18:33:55 +00005361
5362/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5363/// from TryAltiVecVectorToken.
5364bool Parser::TryAltiVecVectorTokenOutOfLine() {
5365 Token Next = NextToken();
5366 switch (Next.getKind()) {
5367 default: return false;
5368 case tok::kw_short:
5369 case tok::kw_long:
5370 case tok::kw_signed:
5371 case tok::kw_unsigned:
5372 case tok::kw_void:
5373 case tok::kw_char:
5374 case tok::kw_int:
5375 case tok::kw_float:
5376 case tok::kw_double:
5377 case tok::kw_bool:
5378 case tok::kw___pixel:
5379 Tok.setKind(tok::kw___vector);
5380 return true;
5381 case tok::identifier:
5382 if (Next.getIdentifierInfo() == Ident_pixel) {
5383 Tok.setKind(tok::kw___vector);
5384 return true;
5385 }
5386 return false;
5387 }
5388}
5389
5390bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5391 const char *&PrevSpec, unsigned &DiagID,
5392 bool &isInvalid) {
5393 if (Tok.getIdentifierInfo() == Ident_vector) {
5394 Token Next = NextToken();
5395 switch (Next.getKind()) {
5396 case tok::kw_short:
5397 case tok::kw_long:
5398 case tok::kw_signed:
5399 case tok::kw_unsigned:
5400 case tok::kw_void:
5401 case tok::kw_char:
5402 case tok::kw_int:
5403 case tok::kw_float:
5404 case tok::kw_double:
5405 case tok::kw_bool:
5406 case tok::kw___pixel:
5407 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5408 return true;
5409 case tok::identifier:
5410 if (Next.getIdentifierInfo() == Ident_pixel) {
5411 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5412 return true;
5413 }
5414 break;
5415 default:
5416 break;
5417 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005418 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005419 DS.isTypeAltiVecVector()) {
5420 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5421 return true;
5422 }
5423 return false;
5424}