blob: 8ac3c504e2f0eb1bb0e25d3561f5354c16337a84 [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,
Richard Smith68ea3ae2013-02-22 09:06:26 +00001271 ParsedAttributesWithRange &Attrs,
Sean Hunt2edf0a22012-06-23 05:07:58 +00001272 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);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001275
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001276 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001277 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001278
Reid Spencer5f016e22007-07-11 17:01:13 +00001279 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1280 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001281 if (Tok.is(tok::semi)) {
Richard Smith68ea3ae2013-02-22 09:06:26 +00001282 ProhibitAttributes(Attrs);
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
Richard Smith68ea3ae2013-02-22 09:06:26 +00001291 DS.takeAttributesFrom(Attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00001292 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001293}
Mike Stump1eb44332009-09-09 15:08:12 +00001294
Richard Smith0706df42011-10-19 21:33:05 +00001295/// Returns true if this might be the start of a declarator, or a common typo
1296/// for a declarator.
1297bool Parser::MightBeDeclarator(unsigned Context) {
1298 switch (Tok.getKind()) {
1299 case tok::annot_cxxscope:
1300 case tok::annot_template_id:
1301 case tok::caret:
1302 case tok::code_completion:
1303 case tok::coloncolon:
1304 case tok::ellipsis:
1305 case tok::kw___attribute:
1306 case tok::kw_operator:
1307 case tok::l_paren:
1308 case tok::star:
1309 return true;
1310
1311 case tok::amp:
1312 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001313 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001314
Richard Smith1c94c162012-01-09 22:31:44 +00001315 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith80ad52f2013-01-02 11:42:31 +00001316 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smith1c94c162012-01-09 22:31:44 +00001317 NextToken().is(tok::l_square);
1318
1319 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001320 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001321
Richard Smith0706df42011-10-19 21:33:05 +00001322 case tok::identifier:
1323 switch (NextToken().getKind()) {
1324 case tok::code_completion:
1325 case tok::coloncolon:
1326 case tok::comma:
1327 case tok::equal:
1328 case tok::equalequal: // Might be a typo for '='.
1329 case tok::kw_alignas:
1330 case tok::kw_asm:
1331 case tok::kw___attribute:
1332 case tok::l_brace:
1333 case tok::l_paren:
1334 case tok::l_square:
1335 case tok::less:
1336 case tok::r_brace:
1337 case tok::r_paren:
1338 case tok::r_square:
1339 case tok::semi:
1340 return true;
1341
1342 case tok::colon:
1343 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001344 // and in block scope it's probably a label. Inside a class definition,
1345 // this is a bit-field.
1346 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001347 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001348
1349 case tok::identifier: // Possible virt-specifier.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001350 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001351
1352 default:
1353 return false;
1354 }
1355
1356 default:
1357 return false;
1358 }
1359}
1360
Richard Smith994d73f2012-04-11 20:59:20 +00001361/// Skip until we reach something which seems like a sensible place to pick
1362/// up parsing after a malformed declaration. This will sometimes stop sooner
1363/// than SkipUntil(tok::r_brace) would, but will never stop later.
1364void Parser::SkipMalformedDecl() {
1365 while (true) {
1366 switch (Tok.getKind()) {
1367 case tok::l_brace:
1368 // Skip until matching }, then stop. We've probably skipped over
1369 // a malformed class or function definition or similar.
1370 ConsumeBrace();
1371 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1372 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1373 // This declaration isn't over yet. Keep skipping.
1374 continue;
1375 }
1376 if (Tok.is(tok::semi))
1377 ConsumeToken();
1378 return;
1379
1380 case tok::l_square:
1381 ConsumeBracket();
1382 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1383 continue;
1384
1385 case tok::l_paren:
1386 ConsumeParen();
1387 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1388 continue;
1389
1390 case tok::r_brace:
1391 return;
1392
1393 case tok::semi:
1394 ConsumeToken();
1395 return;
1396
1397 case tok::kw_inline:
1398 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001399 // a good place to pick back up parsing, except in an Objective-C
1400 // @interface context.
1401 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1402 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001403 return;
1404 break;
1405
1406 case tok::kw_namespace:
1407 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001408 // place to pick back up parsing, except in an Objective-C
1409 // @interface context.
1410 if (Tok.isAtStartOfLine() &&
1411 (!ParsingInObjCContainer || CurParsedObjCImpl))
1412 return;
1413 break;
1414
1415 case tok::at:
1416 // @end is very much like } in Objective-C contexts.
1417 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1418 ParsingInObjCContainer)
1419 return;
1420 break;
1421
1422 case tok::minus:
1423 case tok::plus:
1424 // - and + probably start new method declarations in Objective-C contexts.
1425 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001426 return;
1427 break;
1428
1429 case tok::eof:
1430 return;
1431
1432 default:
1433 break;
1434 }
1435
1436 ConsumeAnyToken();
1437 }
1438}
1439
John McCalld8ac0572009-11-03 19:26:08 +00001440/// ParseDeclGroup - Having concluded that this is either a function
1441/// definition or a group of object declarations, actually parse the
1442/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001443Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1444 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001445 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001446 SourceLocation *DeclEnd,
1447 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001448 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001449 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001450 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001451
John McCalld8ac0572009-11-03 19:26:08 +00001452 // Bail out if the first declarator didn't seem well-formed.
1453 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001454 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001455 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001456 }
Mike Stump1eb44332009-09-09 15:08:12 +00001457
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001458 // Save late-parsed attributes for now; they need to be parsed in the
1459 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001460 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1461 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001462 if (D.isFunctionDeclarator())
1463 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1464
Chris Lattnerc82daef2010-07-11 22:24:20 +00001465 // Check to see if we have a function *definition* which must have a body.
1466 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1467 // Look at the next token to make sure that this isn't a function
1468 // declaration. We have to check this because __attribute__ might be the
1469 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001470 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001471
Chris Lattner004659a2010-07-11 22:42:07 +00001472 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001473 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1474 Diag(Tok, diag::err_function_declared_typedef);
1475
1476 // Recover by treating the 'typedef' as spurious.
1477 DS.ClearStorageClassSpecs();
1478 }
1479
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001480 Decl *TheDecl =
1481 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001482 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001483 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001484
Chris Lattner004659a2010-07-11 22:42:07 +00001485 if (isDeclarationSpecifier()) {
1486 // If there is an invalid declaration specifier right after the function
1487 // prototype, then we must be in a missing semicolon case where this isn't
1488 // actually a body. Just fall through into the code that handles it as a
1489 // prototype, and let the top-level code handle the erroneous declspec
1490 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001491 } else {
1492 Diag(Tok, diag::err_expected_fn_body);
1493 SkipUntil(tok::semi);
1494 return DeclGroupPtrTy();
1495 }
1496 }
1497
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001498 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001499 return DeclGroupPtrTy();
1500
1501 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1502 // must parse and analyze the for-range-initializer before the declaration is
1503 // analyzed.
1504 if (FRI && Tok.is(tok::colon)) {
1505 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001506 if (Tok.is(tok::l_brace))
1507 FRI->RangeExpr = ParseBraceInitializer();
1508 else
1509 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001510 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1511 Actions.ActOnCXXForRangeDecl(ThisDecl);
1512 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001513 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001514 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1515 }
1516
Chris Lattner5f9e2722011-07-23 10:55:15 +00001517 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001518 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001519 if (LateParsedAttrs.size() > 0)
1520 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001521 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001522 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001523 DeclsInGroup.push_back(FirstDecl);
1524
Richard Smith0706df42011-10-19 21:33:05 +00001525 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001526
John McCalld8ac0572009-11-03 19:26:08 +00001527 // If we don't have a comma, it is either the end of the list (a ';') or an
1528 // error, bail out.
1529 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001530 SourceLocation CommaLoc = ConsumeToken();
1531
1532 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1533 // This comma was followed by a line-break and something which can't be
1534 // the start of a declarator. The comma was probably a typo for a
1535 // semicolon.
1536 Diag(CommaLoc, diag::err_expected_semi_declaration)
1537 << FixItHint::CreateReplacement(CommaLoc, ";");
1538 ExpectSemi = false;
1539 break;
1540 }
John McCalld8ac0572009-11-03 19:26:08 +00001541
1542 // Parse the next declarator.
1543 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001544 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001545
1546 // Accept attributes in an init-declarator. In the first declarator in a
1547 // declaration, these would be part of the declspec. In subsequent
1548 // declarators, they become part of the declarator itself, so that they
1549 // don't apply to declarators after *this* one. Examples:
1550 // short __attribute__((common)) var; -> declspec
1551 // short var __attribute__((common)); -> declarator
1552 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001553 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001554
1555 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001556 if (!D.isInvalidType()) {
1557 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1558 D.complete(ThisDecl);
1559 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001560 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001561 }
John McCalld8ac0572009-11-03 19:26:08 +00001562 }
1563
1564 if (DeclEnd)
1565 *DeclEnd = Tok.getLocation();
1566
Richard Smith0706df42011-10-19 21:33:05 +00001567 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001568 ExpectAndConsumeSemi(Context == Declarator::FileContext
1569 ? diag::err_invalid_token_after_toplevel_declarator
1570 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001571 // Okay, there was no semicolon and one was expected. If we see a
1572 // declaration specifier, just assume it was missing and continue parsing.
1573 // Otherwise things are very confused and we skip to recover.
1574 if (!isDeclarationSpecifier()) {
1575 SkipUntil(tok::r_brace, true, true);
1576 if (Tok.is(tok::semi))
1577 ConsumeToken();
1578 }
John McCalld8ac0572009-11-03 19:26:08 +00001579 }
1580
Douglas Gregor23c94db2010-07-02 17:43:08 +00001581 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001582 DeclsInGroup.data(),
1583 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001584}
1585
Richard Smithad762fc2011-04-14 22:09:26 +00001586/// Parse an optional simple-asm-expr and attributes, and attach them to a
1587/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001588bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001589 // If a simple-asm-expr is present, parse it.
1590 if (Tok.is(tok::kw_asm)) {
1591 SourceLocation Loc;
1592 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1593 if (AsmLabel.isInvalid()) {
1594 SkipUntil(tok::semi, true, true);
1595 return true;
1596 }
1597
1598 D.setAsmLabel(AsmLabel.release());
1599 D.SetRangeEnd(Loc);
1600 }
1601
1602 MaybeParseGNUAttributes(D);
1603 return false;
1604}
1605
Douglas Gregor1426e532009-05-12 21:31:51 +00001606/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1607/// declarator'. This method parses the remainder of the declaration
1608/// (including any attributes or initializer, among other things) and
1609/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001610///
Reid Spencer5f016e22007-07-11 17:01:13 +00001611/// init-declarator: [C99 6.7]
1612/// declarator
1613/// declarator '=' initializer
1614/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1615/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001616/// [C++] declarator initializer[opt]
1617///
1618/// [C++] initializer:
1619/// [C++] '=' initializer-clause
1620/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001621/// [C++0x] '=' 'default' [TODO]
1622/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001623/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001624///
1625/// According to the standard grammar, =default and =delete are function
1626/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001627///
John McCalld226f652010-08-21 09:40:31 +00001628Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001629 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001630 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001631 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001632
Richard Smithad762fc2011-04-14 22:09:26 +00001633 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1634}
Mike Stump1eb44332009-09-09 15:08:12 +00001635
Richard Smithad762fc2011-04-14 22:09:26 +00001636Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1637 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001638 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001639 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001640 switch (TemplateInfo.Kind) {
1641 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001642 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001643 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001644
Douglas Gregord5a423b2009-09-25 18:43:00 +00001645 case ParsedTemplateInfo::Template:
1646 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001647 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001648 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001649 D);
1650 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001651
Douglas Gregord5a423b2009-09-25 18:43:00 +00001652 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosier8decdee2012-06-26 22:30:43 +00001653 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001654 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001655 TemplateInfo.ExternLoc,
1656 TemplateInfo.TemplateLoc,
1657 D);
1658 if (ThisRes.isInvalid()) {
1659 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001660 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001661 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001662
Douglas Gregord5a423b2009-09-25 18:43:00 +00001663 ThisDecl = ThisRes.get();
1664 break;
1665 }
1666 }
Mike Stump1eb44332009-09-09 15:08:12 +00001667
Richard Smith34b41d92011-02-20 03:19:35 +00001668 bool TypeContainsAuto =
1669 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1670
Douglas Gregor1426e532009-05-12 21:31:51 +00001671 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001672 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001673 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001674 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001675 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001676 if (D.isFunctionDeclarator())
1677 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1678 << 1 /* delete */;
1679 else
1680 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001681 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001682 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001683 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1684 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001685 else
1686 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001687 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001688 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001689 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001690 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001691 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001692
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001693 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001694 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001695 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001696 cutOffParsing();
1697 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001698 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001699
John McCall60d7b3a2010-08-24 06:29:42 +00001700 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001701
David Blaikie4e4d0842012-03-11 07:00:24 +00001702 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001703 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001704 ExitScope();
1705 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001706
Douglas Gregor1426e532009-05-12 21:31:51 +00001707 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001708 SkipUntil(tok::comma, true, true);
1709 Actions.ActOnInitializerError(ThisDecl);
1710 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001711 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1712 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001713 }
1714 } else if (Tok.is(tok::l_paren)) {
1715 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001716 BalancedDelimiterTracker T(*this, tok::l_paren);
1717 T.consumeOpen();
1718
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001719 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001720 CommaLocsTy CommaLocs;
1721
David Blaikie4e4d0842012-03-11 07:00:24 +00001722 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001723 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001724 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001725 }
1726
Douglas Gregor1426e532009-05-12 21:31:51 +00001727 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001728 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +00001729 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001730
David Blaikie4e4d0842012-03-11 07:00:24 +00001731 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001732 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001733 ExitScope();
1734 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001735 } else {
1736 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001737 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001738
1739 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1740 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001741
David Blaikie4e4d0842012-03-11 07:00:24 +00001742 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001743 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001744 ExitScope();
1745 }
1746
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001747 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1748 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001749 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001750 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1751 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001752 }
Richard Smith80ad52f2013-01-02 11:42:31 +00001753 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001754 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001755 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001756 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1757
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001758 if (D.getCXXScopeSpec().isSet()) {
1759 EnterScope(0);
1760 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1761 }
1762
1763 ExprResult Init(ParseBraceInitializer());
1764
1765 if (D.getCXXScopeSpec().isSet()) {
1766 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1767 ExitScope();
1768 }
1769
1770 if (Init.isInvalid()) {
1771 Actions.ActOnInitializerError(ThisDecl);
1772 } else
1773 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1774 /*DirectInit=*/true, TypeContainsAuto);
1775
Douglas Gregor1426e532009-05-12 21:31:51 +00001776 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001777 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001778 }
1779
Richard Smith483b9f32011-02-21 20:05:19 +00001780 Actions.FinalizeDeclaration(ThisDecl);
1781
Douglas Gregor1426e532009-05-12 21:31:51 +00001782 return ThisDecl;
1783}
1784
Reid Spencer5f016e22007-07-11 17:01:13 +00001785/// ParseSpecifierQualifierList
1786/// specifier-qualifier-list:
1787/// type-specifier specifier-qualifier-list[opt]
1788/// type-qualifier specifier-qualifier-list[opt]
1789/// [GNU] attributes specifier-qualifier-list[opt]
1790///
Richard Smith69730c12012-03-12 07:56:15 +00001791void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1792 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001793 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1794 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001795 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001796 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001797
Reid Spencer5f016e22007-07-11 17:01:13 +00001798 // Validate declspec for type-name.
1799 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001800 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1801 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001802 Diag(Tok, diag::err_expected_type);
1803 DS.SetTypeSpecError();
1804 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1805 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001806 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001807 if (!DS.hasTypeSpecifier())
1808 DS.SetTypeSpecError();
1809 }
Mike Stump1eb44332009-09-09 15:08:12 +00001810
Reid Spencer5f016e22007-07-11 17:01:13 +00001811 // Issue diagnostic and remove storage class if present.
1812 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1813 if (DS.getStorageClassSpecLoc().isValid())
1814 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1815 else
1816 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1817 DS.ClearStorageClassSpecs();
1818 }
Mike Stump1eb44332009-09-09 15:08:12 +00001819
Reid Spencer5f016e22007-07-11 17:01:13 +00001820 // Issue diagnostic and remove function specfier if present.
1821 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001822 if (DS.isInlineSpecified())
1823 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1824 if (DS.isVirtualSpecified())
1825 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1826 if (DS.isExplicitSpecified())
1827 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001828 DS.ClearFunctionSpecs();
1829 }
Richard Smith69730c12012-03-12 07:56:15 +00001830
1831 // Issue diagnostic and remove constexpr specfier if present.
1832 if (DS.isConstexprSpecified()) {
1833 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1834 DS.ClearConstexprSpec();
1835 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001836}
1837
Chris Lattnerc199ab32009-04-12 20:42:31 +00001838/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1839/// specified token is valid after the identifier in a declarator which
1840/// immediately follows the declspec. For example, these things are valid:
1841///
1842/// int x [ 4]; // direct-declarator
1843/// int x ( int y); // direct-declarator
1844/// int(int x ) // direct-declarator
1845/// int x ; // simple-declaration
1846/// int x = 17; // init-declarator-list
1847/// int x , y; // init-declarator-list
1848/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001849/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001850/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001851///
1852/// This is not, because 'x' does not immediately follow the declspec (though
1853/// ')' happens to be valid anyway).
1854/// int (x)
1855///
1856static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1857 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1858 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001859 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001860}
1861
Chris Lattnere40c2952009-04-14 21:34:55 +00001862
1863/// ParseImplicitInt - This method is called when we have an non-typename
1864/// identifier in a declspec (which normally terminates the decl spec) when
1865/// the declspec has no type specifier. In this case, the declspec is either
1866/// malformed or is "implicit int" (in K&R and C89).
1867///
1868/// This method handles diagnosing this prettily and returns false if the
1869/// declspec is done being processed. If it recovers and thinks there may be
1870/// other pieces of declspec after it, it returns true.
1871///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001872bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001873 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00001874 AccessSpecifier AS, DeclSpecContext DSC,
1875 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001876 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001877
Chris Lattnere40c2952009-04-14 21:34:55 +00001878 SourceLocation Loc = Tok.getLocation();
1879 // If we see an identifier that is not a type name, we normally would
1880 // parse it as the identifer being declared. However, when a typename
1881 // is typo'd or the definition is not included, this will incorrectly
1882 // parse the typename as the identifier name and fall over misparsing
1883 // later parts of the diagnostic.
1884 //
1885 // As such, we try to do some look-ahead in cases where this would
1886 // otherwise be an "implicit-int" case to see if this is invalid. For
1887 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1888 // an identifier with implicit int, we'd get a parse error because the
1889 // next token is obviously invalid for a type. Parse these as a case
1890 // with an invalid type specifier.
1891 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001892
Chris Lattnere40c2952009-04-14 21:34:55 +00001893 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001894 // error, do lookahead to try to do better recovery. This never applies
1895 // within a type specifier. Outside of C++, we allow this even if the
1896 // language doesn't "officially" support implicit int -- we support
1897 // implicit int as an extension in C99 and C11. Allegedly, MS also
1898 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001899 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001900 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001901 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001902 // If this token is valid for implicit int, e.g. "static x = 4", then
1903 // we just avoid eating the identifier, so it will be parsed as the
1904 // identifier in the declarator.
1905 return false;
1906 }
Mike Stump1eb44332009-09-09 15:08:12 +00001907
Richard Smith827adaf2012-05-15 21:01:51 +00001908 if (getLangOpts().CPlusPlus &&
1909 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1910 // Don't require a type specifier if we have the 'auto' storage class
1911 // specifier in C++98 -- we'll promote it to a type specifier.
1912 return false;
1913 }
1914
Chris Lattnere40c2952009-04-14 21:34:55 +00001915 // Otherwise, if we don't consume this token, we are going to emit an
1916 // error anyway. Try to recover from various common problems. Check
1917 // to see if this was a reference to a tag name without a tag specified.
1918 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001919 //
1920 // C++ doesn't need this, and isTagName doesn't take SS.
1921 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001922 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001923 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001924
Douglas Gregor23c94db2010-07-02 17:43:08 +00001925 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001926 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001927 case DeclSpec::TST_enum:
1928 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1929 case DeclSpec::TST_union:
1930 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1931 case DeclSpec::TST_struct:
1932 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00001933 case DeclSpec::TST_interface:
1934 TagName="__interface"; FixitTagName = "__interface ";
1935 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001936 case DeclSpec::TST_class:
1937 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001938 }
Mike Stump1eb44332009-09-09 15:08:12 +00001939
Chris Lattnerf4382f52009-04-14 22:17:06 +00001940 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001941 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1942 LookupResult R(Actions, TokenName, SourceLocation(),
1943 Sema::LookupOrdinaryName);
1944
Chris Lattnerf4382f52009-04-14 22:17:06 +00001945 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001946 << TokenName << TagName << getLangOpts().CPlusPlus
1947 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1948
1949 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1950 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1951 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001952 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001953 << TokenName << TagName;
1954 }
Mike Stump1eb44332009-09-09 15:08:12 +00001955
Chris Lattnerf4382f52009-04-14 22:17:06 +00001956 // Parse this as a tag as if the missing tag were present.
1957 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001958 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001959 else
Richard Smith69730c12012-03-12 07:56:15 +00001960 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00001961 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001962 return true;
1963 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001964 }
Mike Stump1eb44332009-09-09 15:08:12 +00001965
Richard Smith8f0a7e72012-05-15 21:29:55 +00001966 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001967 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001968 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1969 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001970 // Look ahead to the next token to try to figure out what this declaration
1971 // was supposed to be.
1972 switch (NextToken().getKind()) {
1973 case tok::comma:
1974 case tok::equal:
1975 case tok::kw_asm:
1976 case tok::l_brace:
1977 case tok::l_square:
1978 case tok::semi:
1979 // This looks like a variable declaration. The type is probably missing.
1980 // We're done parsing decl-specifiers.
1981 return false;
1982
1983 case tok::l_paren: {
1984 // static x(4); // 'x' is not a type
1985 // x(int n); // 'x' is not a type
1986 // x (*p)[]; // 'x' is a type
1987 //
1988 // Since we're in an error case (or the rare 'implicit int in C++' MS
1989 // extension), we can afford to perform a tentative parse to determine
1990 // which case we're in.
1991 TentativeParsingAction PA(*this);
1992 ConsumeToken();
1993 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1994 PA.Revert();
1995 if (TPR == TPResult::False())
1996 return false;
1997 // The identifier is followed by a parenthesized declarator.
1998 // It's supposed to be a type.
1999 break;
2000 }
2001
2002 default:
2003 // This is probably supposed to be a type. This includes cases like:
2004 // int f(itn);
2005 // struct S { unsinged : 4; };
2006 break;
2007 }
2008 }
2009
Chad Rosier8decdee2012-06-26 22:30:43 +00002010 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00002011 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00002012 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002013 IdentifierInfo *II = Tok.getIdentifierInfo();
2014 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00002015 // The action emitted a diagnostic, so we don't have to.
2016 if (T) {
2017 // The action has suggested that the type T could be used. Set that as
2018 // the type in the declaration specifiers, consume the would-be type
2019 // name token, and we're done.
2020 const char *PrevSpec;
2021 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00002022 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00002023 DS.SetRangeEnd(Tok.getLocation());
2024 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002025 // There may be other declaration specifiers after this.
2026 return true;
2027 } else if (II != Tok.getIdentifierInfo()) {
2028 // If no type was suggested, the correction is to a keyword
2029 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002030 // There may be other declaration specifiers after this.
2031 return true;
2032 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002033
Douglas Gregora786fdb2009-10-13 23:27:22 +00002034 // Fall through; the action had no suggestion for us.
2035 } else {
2036 // The action did not emit a diagnostic, so emit one now.
2037 SourceRange R;
2038 if (SS) R = SS->getRange();
2039 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2040 }
Mike Stump1eb44332009-09-09 15:08:12 +00002041
Douglas Gregora786fdb2009-10-13 23:27:22 +00002042 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002043 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002044 DS.SetRangeEnd(Tok.getLocation());
2045 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002046
Chris Lattnere40c2952009-04-14 21:34:55 +00002047 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2048 // avoid rippling error messages on subsequent uses of the same type,
2049 // could be useful if #include was forgotten.
2050 return false;
2051}
2052
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002053/// \brief Determine the declaration specifier context from the declarator
2054/// context.
2055///
2056/// \param Context the declarator context, which is one of the
2057/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002058Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002059Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2060 if (Context == Declarator::MemberContext)
2061 return DSC_class;
2062 if (Context == Declarator::FileContext)
2063 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002064 if (Context == Declarator::TrailingReturnContext)
2065 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002066 return DSC_normal;
2067}
2068
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002069/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2070///
2071/// FIXME: Simply returns an alignof() expression if the argument is a
2072/// type. Ideally, the type should be propagated directly into Sema.
2073///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002074/// [C11] type-id
2075/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002076/// [C++0x] type-id ...[opt]
2077/// [C++0x] assignment-expression ...[opt]
2078ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2079 SourceLocation &EllipsisLoc) {
2080 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002081 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002082 SourceLocation TypeLoc = Tok.getLocation();
2083 ParsedType Ty = ParseTypeName().get();
2084 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002085 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2086 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002087 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002088 ER = ParseConstantExpression();
2089
Richard Smith80ad52f2013-01-02 11:42:31 +00002090 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002091 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002092
2093 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002094}
2095
2096/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2097/// attribute to Attrs.
2098///
2099/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002100/// [C11] '_Alignas' '(' type-id ')'
2101/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smith33f04a22013-01-29 01:48:07 +00002102/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2103/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002104void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smithf6565a92013-02-22 08:32:16 +00002105 SourceLocation *EndLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002106 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2107 "Not an alignment-specifier!");
2108
Richard Smith33f04a22013-01-29 01:48:07 +00002109 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2110 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002111
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002112 BalancedDelimiterTracker T(*this, tok::l_paren);
2113 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002114 return;
2115
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002116 SourceLocation EllipsisLoc;
2117 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002118 if (ArgExpr.isInvalid()) {
2119 SkipUntil(tok::r_paren);
2120 return;
2121 }
2122
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002123 T.consumeClose();
Richard Smithf6565a92013-02-22 08:32:16 +00002124 if (EndLoc)
2125 *EndLoc = T.getCloseLocation();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002126
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002127 ExprVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002128 ArgExprs.push_back(ArgExpr.release());
Richard Smith33f04a22013-01-29 01:48:07 +00002129 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
Richard Smithf6565a92013-02-22 08:32:16 +00002130 ArgExprs.data(), 1, AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002131}
2132
Reid Spencer5f016e22007-07-11 17:01:13 +00002133/// ParseDeclarationSpecifiers
2134/// declaration-specifiers: [C99 6.7]
2135/// storage-class-specifier declaration-specifiers[opt]
2136/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002137/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002138/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002139/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002140/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002141///
2142/// storage-class-specifier: [C99 6.7.1]
2143/// 'typedef'
2144/// 'extern'
2145/// 'static'
2146/// 'auto'
2147/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002148/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002149/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002150/// function-specifier: [C99 6.7.4]
2151/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002152/// [C++] 'virtual'
2153/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002154/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002155/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002156/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002157
Reid Spencer5f016e22007-07-11 17:01:13 +00002158///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002159void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002160 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002161 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002162 DeclSpecContext DSContext,
2163 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002164 if (DS.getSourceRange().isInvalid()) {
2165 DS.SetRangeStart(Tok.getLocation());
2166 DS.SetRangeEnd(Tok.getLocation());
2167 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002168
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002169 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002170 bool AttrsLastTime = false;
2171 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002172 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002173 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002174 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002175 unsigned DiagID = 0;
2176
Reid Spencer5f016e22007-07-11 17:01:13 +00002177 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002178
Reid Spencer5f016e22007-07-11 17:01:13 +00002179 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002180 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002181 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002182 if (!AttrsLastTime)
2183 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002184 else {
2185 // Reject C++11 attributes that appertain to decl specifiers as
2186 // we don't support any C++11 attributes that appertain to decl
2187 // specifiers. This also conforms to what g++ 4.8 is doing.
2188 ProhibitCXX11Attributes(attrs);
2189
Sean Hunt2edf0a22012-06-23 05:07:58 +00002190 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002191 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002192
Reid Spencer5f016e22007-07-11 17:01:13 +00002193 // If this is not a declaration specifier token, we're done reading decl
2194 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002195 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002196 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002197
Sean Hunt2edf0a22012-06-23 05:07:58 +00002198 case tok::l_square:
2199 case tok::kw_alignas:
2200 if (!isCXX11AttributeSpecifier())
2201 goto DoneWithDeclSpec;
2202
2203 ProhibitAttributes(attrs);
2204 // FIXME: It would be good to recover by accepting the attributes,
2205 // but attempting to do that now would cause serious
2206 // madness in terms of diagnostics.
2207 attrs.clear();
2208 attrs.Range = SourceRange();
2209
2210 ParseCXX11Attributes(attrs);
2211 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002212 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002213
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002214 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002215 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002216 if (DS.hasTypeSpecifier()) {
2217 bool AllowNonIdentifiers
2218 = (getCurScope()->getFlags() & (Scope::ControlScope |
2219 Scope::BlockScope |
2220 Scope::TemplateParamScope |
2221 Scope::FunctionPrototypeScope |
2222 Scope::AtCatchScope)) == 0;
2223 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002224 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002225 (DSContext == DSC_class && DS.isFriendSpecified());
2226
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002227 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002228 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002229 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002230 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002231 }
2232
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002233 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2234 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2235 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002236 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002237 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002238 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002239 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002240 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002241 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002242
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002243 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002244 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002245 }
2246
Chris Lattner5e02c472009-01-05 00:07:25 +00002247 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002248 // C++ scope specifier. Annotate and loop, or bail out on error.
2249 if (TryAnnotateCXXScopeToken(true)) {
2250 if (!DS.hasTypeSpecifier())
2251 DS.SetTypeSpecError();
2252 goto DoneWithDeclSpec;
2253 }
John McCall2e0a7152010-03-01 18:20:46 +00002254 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2255 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002256 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002257
2258 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002259 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002260 goto DoneWithDeclSpec;
2261
John McCallaa87d332009-12-12 11:40:51 +00002262 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002263 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2264 Tok.getAnnotationRange(),
2265 SS);
John McCallaa87d332009-12-12 11:40:51 +00002266
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002267 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002268 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002269 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002270 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002271 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002272 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002273
2274 // C++ [class.qual]p2:
2275 // In a lookup in which the constructor is an acceptable lookup
2276 // result and the nested-name-specifier nominates a class C:
2277 //
2278 // - if the name specified after the
2279 // nested-name-specifier, when looked up in C, is the
2280 // injected-class-name of C (Clause 9), or
2281 //
2282 // - if the name specified after the nested-name-specifier
2283 // is the same as the identifier or the
2284 // simple-template-id's template-name in the last
2285 // component of the nested-name-specifier,
2286 //
2287 // the name is instead considered to name the constructor of
2288 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002289 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002290 // Thus, if the template-name is actually the constructor
2291 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002292 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002293 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002294 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCallba9d8532010-04-13 06:39:49 +00002295 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002296 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002297 if (isConstructorDeclarator()) {
2298 // The user meant this to be an out-of-line constructor
2299 // definition, but template arguments are not allowed
2300 // there. Just allow this as a constructor; we'll
2301 // complain about it later.
2302 goto DoneWithDeclSpec;
2303 }
2304
2305 // The user meant this to name a type, but it actually names
2306 // a constructor with some extraneous template
2307 // arguments. Complain, then parse it as a type as the user
2308 // intended.
2309 Diag(TemplateId->TemplateNameLoc,
2310 diag::err_out_of_line_template_id_names_constructor)
2311 << TemplateId->Name;
2312 }
2313
John McCallaa87d332009-12-12 11:40:51 +00002314 DS.getTypeSpecScope() = SS;
2315 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002316 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002317 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002318 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002319 continue;
2320 }
2321
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002322 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002323 DS.getTypeSpecScope() = SS;
2324 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002325 if (Tok.getAnnotationValue()) {
2326 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002327 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002328 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002329 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002330 if (isInvalid)
2331 break;
John McCallb3d87482010-08-24 05:47:05 +00002332 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002333 else
2334 DS.SetTypeSpecError();
2335 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2336 ConsumeToken(); // The typename
2337 }
2338
Douglas Gregor9135c722009-03-25 15:40:00 +00002339 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002340 goto DoneWithDeclSpec;
2341
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002342 // If we're in a context where the identifier could be a class name,
2343 // check whether this is a constructor declaration.
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002344 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002345 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002346 &SS)) {
2347 if (isConstructorDeclarator())
2348 goto DoneWithDeclSpec;
2349
2350 // As noted in C++ [class.qual]p2 (cited above), when the name
2351 // of the class is qualified in a context where it could name
2352 // a constructor, its a constructor name. However, we've
2353 // looked at the declarator, and the user probably meant this
2354 // to be a type. Complain that it isn't supposed to be treated
2355 // as a type, then proceed to parse it as a type.
2356 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2357 << Next.getIdentifierInfo();
2358 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002359
John McCallb3d87482010-08-24 05:47:05 +00002360 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2361 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002362 getCurScope(), &SS,
2363 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002364 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002365 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002366
Chris Lattnerf4382f52009-04-14 22:17:06 +00002367 // If the referenced identifier is not a type, then this declspec is
2368 // erroneous: We already checked about that it has no type specifier, and
2369 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002370 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002371 if (TypeRep == 0) {
2372 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002373 ParsedAttributesWithRange Attrs(AttrFactory);
2374 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2375 if (!Attrs.empty()) {
2376 AttrsLastTime = true;
2377 attrs.takeAllFrom(Attrs);
2378 }
2379 continue;
2380 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002381 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002382 }
Mike Stump1eb44332009-09-09 15:08:12 +00002383
John McCallaa87d332009-12-12 11:40:51 +00002384 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002385 ConsumeToken(); // The C++ scope.
2386
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002387 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002388 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002389 if (isInvalid)
2390 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002391
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002392 DS.SetRangeEnd(Tok.getLocation());
2393 ConsumeToken(); // The typename.
2394
2395 continue;
2396 }
Mike Stump1eb44332009-09-09 15:08:12 +00002397
Chris Lattner80d0c892009-01-21 19:48:37 +00002398 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002399 if (Tok.getAnnotationValue()) {
2400 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002401 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002402 DiagID, T);
2403 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002404 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002405
Chris Lattner5c5db552010-04-05 18:18:31 +00002406 if (isInvalid)
2407 break;
2408
Chris Lattner80d0c892009-01-21 19:48:37 +00002409 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2410 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002411
Chris Lattner80d0c892009-01-21 19:48:37 +00002412 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2413 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002414 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002415 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002416 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002417
Chris Lattner80d0c892009-01-21 19:48:37 +00002418 continue;
2419 }
Mike Stump1eb44332009-09-09 15:08:12 +00002420
Douglas Gregorbfad9152011-04-28 15:48:45 +00002421 case tok::kw___is_signed:
2422 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2423 // typically treats it as a trait. If we see __is_signed as it appears
2424 // in libstdc++, e.g.,
2425 //
2426 // static const bool __is_signed;
2427 //
2428 // then treat __is_signed as an identifier rather than as a keyword.
2429 if (DS.getTypeSpecType() == TST_bool &&
2430 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2431 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2432 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2433 Tok.setKind(tok::identifier);
2434 }
2435
2436 // We're done with the declaration-specifiers.
2437 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002438
Chris Lattner3bd934a2008-07-26 01:18:38 +00002439 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002440 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002441 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002442 // In C++, check to see if this is a scope specifier like foo::bar::, if
2443 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002444 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002445 if (TryAnnotateCXXScopeToken(true)) {
2446 if (!DS.hasTypeSpecifier())
2447 DS.SetTypeSpecError();
2448 goto DoneWithDeclSpec;
2449 }
2450 if (!Tok.is(tok::identifier))
2451 continue;
2452 }
Mike Stump1eb44332009-09-09 15:08:12 +00002453
Chris Lattner3bd934a2008-07-26 01:18:38 +00002454 // This identifier can only be a typedef name if we haven't already seen
2455 // a type-specifier. Without this check we misparse:
2456 // typedef int X; struct Y { short X; }; as 'short int'.
2457 if (DS.hasTypeSpecifier())
2458 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002459
John Thompson82287d12010-02-05 00:12:22 +00002460 // Check for need to substitute AltiVec keyword tokens.
2461 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2462 break;
2463
Richard Smithf63eee72012-05-09 18:56:43 +00002464 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2465 // allow the use of a typedef name as a type specifier.
2466 if (DS.isTypeAltiVecVector())
2467 goto DoneWithDeclSpec;
2468
John McCallb3d87482010-08-24 05:47:05 +00002469 ParsedType TypeRep =
2470 Actions.getTypeName(*Tok.getIdentifierInfo(),
2471 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002472
Chris Lattnerc199ab32009-04-12 20:42:31 +00002473 // If this is not a typedef name, don't parse it as part of the declspec,
2474 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002475 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002476 ParsedAttributesWithRange Attrs(AttrFactory);
2477 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2478 if (!Attrs.empty()) {
2479 AttrsLastTime = true;
2480 attrs.takeAllFrom(Attrs);
2481 }
2482 continue;
2483 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002484 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002485 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002486
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002487 // If we're in a context where the identifier could be a class name,
2488 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002489 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002490 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002491 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002492 goto DoneWithDeclSpec;
2493
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002494 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002495 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002496 if (isInvalid)
2497 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002498
Chris Lattner3bd934a2008-07-26 01:18:38 +00002499 DS.SetRangeEnd(Tok.getLocation());
2500 ConsumeToken(); // The identifier
2501
2502 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2503 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002504 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002505 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002506 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002507
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002508 // Need to support trailing type qualifiers (e.g. "id<p> const").
2509 // If a type specifier follows, it will be diagnosed elsewhere.
2510 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002511 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002512
2513 // type-name
2514 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002515 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002516 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002517 // This template-id does not refer to a type name, so we're
2518 // done with the type-specifiers.
2519 goto DoneWithDeclSpec;
2520 }
2521
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002522 // If we're in a context where the template-id could be a
2523 // constructor name or specialization, check whether this is a
2524 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002525 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002526 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002527 isConstructorDeclarator())
2528 goto DoneWithDeclSpec;
2529
Douglas Gregor39a8de12009-02-25 19:37:18 +00002530 // Turn the template-id annotation token into a type annotation
2531 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002532 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002533 continue;
2534 }
2535
Reid Spencer5f016e22007-07-11 17:01:13 +00002536 // GNU attributes support.
2537 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002538 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002539 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002540
2541 // Microsoft declspec support.
2542 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002543 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002544 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002545
Steve Naroff239f0732008-12-25 14:16:32 +00002546 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002547 case tok::kw___forceinline: {
Chad Rosier22aa6902012-12-21 22:24:43 +00002548 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002549 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002550 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002551 // FIXME: This does not work correctly if it is set to be a declspec
2552 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002553 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002554 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002555 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002556 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002557
2558 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002559 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002560 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002561 case tok::kw___cdecl:
2562 case tok::kw___stdcall:
2563 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002564 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002565 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002566 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002567 continue;
2568
Dawn Perchik52fc3142010-09-03 01:29:35 +00002569 // Borland single token adornments.
2570 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002571 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002572 continue;
2573
Peter Collingbournef315fa82011-02-14 01:42:53 +00002574 // OpenCL single token adornments.
2575 case tok::kw___kernel:
2576 ParseOpenCLAttributes(DS.getAttributes());
2577 continue;
2578
Reid Spencer5f016e22007-07-11 17:01:13 +00002579 // storage-class-specifier
2580 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002581 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2582 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002583 break;
2584 case tok::kw_extern:
2585 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002586 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002587 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2588 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002589 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002590 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002591 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2592 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002593 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002594 case tok::kw_static:
2595 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002596 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002597 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2598 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002599 break;
2600 case tok::kw_auto:
Richard Smith80ad52f2013-01-02 11:42:31 +00002601 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002602 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002603 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2604 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002605 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002606 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002607 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002608 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002609 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2610 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002611 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002612 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2613 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002614 break;
2615 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002616 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2617 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002618 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002619 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002620 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2621 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002622 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002623 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002624 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002625 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002626
Reid Spencer5f016e22007-07-11 17:01:13 +00002627 // function-specifier
2628 case tok::kw_inline:
Chad Rosier22aa6902012-12-21 22:24:43 +00002629 isInvalid = DS.setFunctionSpecInline(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002630 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002631 case tok::kw_virtual:
Chad Rosier22aa6902012-12-21 22:24:43 +00002632 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002633 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002634 case tok::kw_explicit:
Chad Rosier22aa6902012-12-21 22:24:43 +00002635 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002636 break;
Richard Smithde03c152013-01-17 22:16:11 +00002637 case tok::kw__Noreturn:
2638 if (!getLangOpts().C11)
2639 Diag(Loc, diag::ext_c11_noreturn);
2640 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2641 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002642
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002643 // alignment-specifier
2644 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002645 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002646 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002647 ParseAlignmentSpecifier(DS.getAttributes());
2648 continue;
2649
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002650 // friend
2651 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002652 if (DSContext == DSC_class)
2653 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2654 else {
2655 PrevSpec = ""; // not actually used by the diagnostic
2656 DiagID = diag::err_friend_invalid_in_context;
2657 isInvalid = true;
2658 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002659 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002660
Douglas Gregor8d267c52011-09-09 02:06:17 +00002661 // Modules
2662 case tok::kw___module_private__:
2663 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2664 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002665
Sebastian Redl2ac67232009-11-05 15:47:02 +00002666 // constexpr
2667 case tok::kw_constexpr:
2668 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2669 break;
2670
Chris Lattner80d0c892009-01-21 19:48:37 +00002671 // type-specifier
2672 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002673 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2674 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002675 break;
2676 case tok::kw_long:
2677 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002678 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2679 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002680 else
John McCallfec54012009-08-03 20:12:06 +00002681 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2682 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002683 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002684 case tok::kw___int64:
2685 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2686 DiagID);
2687 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002688 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002689 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2690 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002691 break;
2692 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002693 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2694 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002695 break;
2696 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002697 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2698 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002699 break;
2700 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002701 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2702 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002703 break;
2704 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002705 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2706 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002707 break;
2708 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002709 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2710 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002711 break;
2712 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002713 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2714 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002715 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002716 case tok::kw___int128:
2717 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2718 DiagID);
2719 break;
2720 case tok::kw_half:
2721 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2722 DiagID);
2723 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002724 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002725 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2726 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002727 break;
2728 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002729 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2730 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002731 break;
2732 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002733 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2734 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002735 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002736 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002737 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2738 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002739 break;
2740 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002741 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2742 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002743 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002744 case tok::kw_bool:
2745 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002746 if (Tok.is(tok::kw_bool) &&
2747 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2748 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2749 PrevSpec = ""; // Not used by the diagnostic.
2750 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002751 // For better error recovery.
2752 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002753 isInvalid = true;
2754 } else {
2755 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2756 DiagID);
2757 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002758 break;
2759 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002760 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2761 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002762 break;
2763 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002764 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2765 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002766 break;
2767 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002768 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2769 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002770 break;
John Thompson82287d12010-02-05 00:12:22 +00002771 case tok::kw___vector:
2772 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2773 break;
2774 case tok::kw___pixel:
2775 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2776 break;
Guy Benyeib13621d2012-12-18 14:38:23 +00002777 case tok::kw_image1d_t:
2778 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2779 PrevSpec, DiagID);
2780 break;
2781 case tok::kw_image1d_array_t:
2782 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2783 PrevSpec, DiagID);
2784 break;
2785 case tok::kw_image1d_buffer_t:
2786 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2787 PrevSpec, DiagID);
2788 break;
2789 case tok::kw_image2d_t:
2790 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2791 PrevSpec, DiagID);
2792 break;
2793 case tok::kw_image2d_array_t:
2794 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2795 PrevSpec, DiagID);
2796 break;
2797 case tok::kw_image3d_t:
2798 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2799 PrevSpec, DiagID);
2800 break;
Guy Benyei21f18c42013-02-07 10:55:47 +00002801 case tok::kw_sampler_t:
2802 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2803 PrevSpec, DiagID);
2804 break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00002805 case tok::kw_event_t:
2806 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2807 PrevSpec, DiagID);
2808 break;
John McCalla5fc4722011-04-09 22:50:59 +00002809 case tok::kw___unknown_anytype:
2810 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2811 PrevSpec, DiagID);
2812 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002813
2814 // class-specifier:
2815 case tok::kw_class:
2816 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00002817 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00002818 case tok::kw_union: {
2819 tok::TokenKind Kind = Tok.getKind();
2820 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00002821
2822 // These are attributes following class specifiers.
2823 // To produce better diagnostic, we parse them when
2824 // parsing class specifier.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002825 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00002826 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002827 EnteringContext, DSContext, Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002828
2829 // If there are attributes following class specifier,
2830 // take them over and handle them here.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002831 if (!Attributes.empty()) {
Michael Han2e397132012-11-26 22:54:45 +00002832 AttrsLastTime = true;
Bill Wendlingad017fa2012-12-20 19:22:21 +00002833 attrs.takeAllFrom(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002834 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002835 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002836 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002837
2838 // enum-specifier:
2839 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002840 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002841 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002842 continue;
2843
2844 // cv-qualifier:
2845 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002846 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002847 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002848 break;
2849 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002850 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002851 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002852 break;
2853 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002854 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002855 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002856 break;
2857
Douglas Gregord57959a2009-03-27 23:10:48 +00002858 // C++ typename-specifier:
2859 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002860 if (TryAnnotateTypeOrScopeToken()) {
2861 DS.SetTypeSpecError();
2862 goto DoneWithDeclSpec;
2863 }
2864 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002865 continue;
2866 break;
2867
Chris Lattner80d0c892009-01-21 19:48:37 +00002868 // GNU typeof support.
2869 case tok::kw_typeof:
2870 ParseTypeofSpecifier(DS);
2871 continue;
2872
David Blaikie42d6d0c2011-12-04 05:04:18 +00002873 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002874 ParseDecltypeSpecifier(DS);
2875 continue;
2876
Sean Huntdb5d44b2011-05-19 05:37:45 +00002877 case tok::kw___underlying_type:
2878 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002879 continue;
2880
2881 case tok::kw__Atomic:
2882 ParseAtomicSpecifier(DS);
2883 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002884
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002885 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00002886 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002887 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002888 goto DoneWithDeclSpec;
2889 case tok::kw___private:
2890 case tok::kw___global:
2891 case tok::kw___local:
2892 case tok::kw___constant:
2893 case tok::kw___read_only:
2894 case tok::kw___write_only:
2895 case tok::kw___read_write:
2896 ParseOpenCLQualifiers(DS);
2897 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002898
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002899 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002900 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002901 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2902 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002903 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002904 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002905
Douglas Gregor46f936e2010-11-19 17:10:50 +00002906 if (!ParseObjCProtocolQualifiers(DS))
2907 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2908 << FixItHint::CreateInsertion(Loc, "id")
2909 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00002910
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002911 // Need to support trailing type qualifiers (e.g. "id<p> const").
2912 // If a type specifier follows, it will be diagnosed elsewhere.
2913 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002914 }
John McCallfec54012009-08-03 20:12:06 +00002915 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002916 if (isInvalid) {
2917 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002918 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00002919
Douglas Gregorae2fb142010-08-23 14:34:43 +00002920 if (DiagID == diag::ext_duplicate_declspec)
2921 Diag(Tok, DiagID)
2922 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2923 else
2924 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002925 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002926
Chris Lattner81c018d2008-03-13 06:29:04 +00002927 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002928 if (DiagID != diag::err_bool_redeclaration)
2929 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00002930
2931 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002932 }
2933}
Douglas Gregoradcac882008-12-01 23:54:00 +00002934
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002935/// ParseStructDeclaration - Parse a struct declaration without the terminating
2936/// semicolon.
2937///
Reid Spencer5f016e22007-07-11 17:01:13 +00002938/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002939/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002940/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002941/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002942/// struct-declarator-list:
2943/// struct-declarator
2944/// struct-declarator-list ',' struct-declarator
2945/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2946/// struct-declarator:
2947/// declarator
2948/// [GNU] declarator attributes[opt]
2949/// declarator[opt] ':' constant-expression
2950/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2951///
Chris Lattnere1359422008-04-10 06:46:29 +00002952void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002953ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00002954
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002955 if (Tok.is(tok::kw___extension__)) {
2956 // __extension__ silences extension warnings in the subexpression.
2957 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002958 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002959 return ParseStructDeclaration(DS, Fields);
2960 }
Mike Stump1eb44332009-09-09 15:08:12 +00002961
Steve Naroff28a7ca82007-08-20 22:28:22 +00002962 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002963 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002964
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002965 // If there are no declarators, this is a free-standing declaration
2966 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002967 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002968 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2969 DS);
2970 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002971 return;
2972 }
2973
2974 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002975 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002976 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002977 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002978 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00002979 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002980
Bill Wendlingad017fa2012-12-20 19:22:21 +00002981 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002982 if (!FirstDeclarator)
2983 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002984
Steve Naroff28a7ca82007-08-20 22:28:22 +00002985 /// struct-declarator: declarator
2986 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002987 if (Tok.isNot(tok::colon)) {
2988 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2989 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002990 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002991 }
Mike Stump1eb44332009-09-09 15:08:12 +00002992
Chris Lattner04d66662007-10-09 17:33:22 +00002993 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002994 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002995 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002996 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002997 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002998 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002999 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00003000 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003001
Steve Naroff28a7ca82007-08-20 22:28:22 +00003002 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003003 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003004
John McCallbdd563e2009-11-03 02:38:08 +00003005 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00003006 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00003007
Steve Naroff28a7ca82007-08-20 22:28:22 +00003008 // If we don't have a comma, it is either the end of the list (a ';')
3009 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00003010 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003011 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00003012
Steve Naroff28a7ca82007-08-20 22:28:22 +00003013 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00003014 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003015
John McCallbdd563e2009-11-03 02:38:08 +00003016 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003017 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00003018}
3019
3020/// ParseStructUnionBody
3021/// struct-contents:
3022/// struct-declaration-list
3023/// [EXT] empty
3024/// [GNU] "struct-declaration-list" without terminatoring ';'
3025/// struct-declaration-list:
3026/// struct-declaration
3027/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003028/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00003029///
Reid Spencer5f016e22007-07-11 17:01:13 +00003030void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00003031 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00003032 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3033 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00003034
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003035 BalancedDelimiterTracker T(*this, tok::l_brace);
3036 if (T.consumeOpen())
3037 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003038
Douglas Gregor3218c4b2009-01-09 22:42:13 +00003039 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003040 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00003041
Reid Spencer5f016e22007-07-11 17:01:13 +00003042 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
3043 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00003044 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00003045 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3046 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3047 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003048
Chris Lattner5f9e2722011-07-23 10:55:15 +00003049 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00003050
Reid Spencer5f016e22007-07-11 17:01:13 +00003051 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00003052 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003053 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003054
Reid Spencer5f016e22007-07-11 17:01:13 +00003055 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003056 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003057 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003058 continue;
3059 }
Chris Lattnere1359422008-04-10 06:46:29 +00003060
John McCallbdd563e2009-11-03 02:38:08 +00003061 if (!Tok.is(tok::at)) {
3062 struct CFieldCallback : FieldCallback {
3063 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003064 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003065 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003066
John McCalld226f652010-08-21 09:40:31 +00003067 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003068 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003069 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3070
Eli Friedmandcdff462012-08-08 23:53:27 +00003071 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00003072 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003073 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003074 FD.D.getDeclSpec().getSourceRange().getBegin(),
3075 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003076 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003077 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003078 }
John McCallbdd563e2009-11-03 02:38:08 +00003079 } Callback(*this, TagDecl, FieldDecls);
3080
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003081 // Parse all the comma separated declarators.
3082 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003083 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003084 } else { // Handle @defs
3085 ConsumeToken();
3086 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3087 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003088 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003089 continue;
3090 }
3091 ConsumeToken();
3092 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3093 if (!Tok.is(tok::identifier)) {
3094 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003095 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003096 continue;
3097 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003098 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003099 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003100 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003101 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3102 ConsumeToken();
3103 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003104 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003105
Chris Lattner04d66662007-10-09 17:33:22 +00003106 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003107 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003108 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003109 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003110 break;
3111 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003112 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3113 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003114 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003115 // If we stopped at a ';', eat it.
3116 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003117 }
3118 }
Mike Stump1eb44332009-09-09 15:08:12 +00003119
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003120 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003121
John McCall0b7e6782011-03-24 11:26:52 +00003122 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003123 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003124 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003125
Douglas Gregor23c94db2010-07-02 17:43:08 +00003126 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003127 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003128 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003129 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003130 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003131 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3132 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003133}
3134
Reid Spencer5f016e22007-07-11 17:01:13 +00003135/// ParseEnumSpecifier
3136/// enum-specifier: [C99 6.7.2.2]
3137/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003138///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003139/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3140/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003141/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3142/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003143/// 'enum' identifier
3144/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003145///
Richard Smith1af83c42012-03-23 03:33:32 +00003146/// [C++11] enum-head '{' enumerator-list[opt] '}'
3147/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003148///
Richard Smith1af83c42012-03-23 03:33:32 +00003149/// enum-head: [C++11]
3150/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3151/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3152/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003153///
Richard Smith1af83c42012-03-23 03:33:32 +00003154/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003155/// 'enum'
3156/// 'enum' 'class'
3157/// 'enum' 'struct'
3158///
Richard Smith1af83c42012-03-23 03:33:32 +00003159/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003160/// ':' type-specifier-seq
3161///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003162/// [C++] elaborated-type-specifier:
3163/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3164///
Chris Lattner4c97d762009-04-12 21:49:30 +00003165void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003166 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003167 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003168 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003169 if (Tok.is(tok::code_completion)) {
3170 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003171 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003172 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003173 }
John McCall57c13002011-07-06 05:58:41 +00003174
Sean Hunt2edf0a22012-06-23 05:07:58 +00003175 // If attributes exist after tag, parse them.
3176 ParsedAttributesWithRange attrs(AttrFactory);
3177 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003178 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003179
3180 // If declspecs exist after tag, parse them.
3181 while (Tok.is(tok::kw___declspec))
3182 ParseMicrosoftDeclSpec(attrs);
3183
Richard Smithbdad7a22012-01-10 01:33:14 +00003184 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003185 bool IsScopedUsingClassTag = false;
3186
John McCall1e12b3d2012-06-23 22:30:04 +00003187 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Smith80ad52f2013-01-02 11:42:31 +00003188 if (getLangOpts().CPlusPlus11 &&
John McCall57c13002011-07-06 05:58:41 +00003189 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00003190 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003191 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003192 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003193
Bill Wendlingad017fa2012-12-20 19:22:21 +00003194 // Attributes are not allowed between these keywords. Diagnose,
John McCall1e12b3d2012-06-23 22:30:04 +00003195 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003196 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003197
3198 // They are allowed afterwards, though.
3199 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003200 MaybeParseCXX11Attributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003201 while (Tok.is(tok::kw___declspec))
3202 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003203 }
Richard Smith1af83c42012-03-23 03:33:32 +00003204
John McCall13489672012-05-07 06:16:58 +00003205 // C++11 [temp.explicit]p12:
3206 // The usual access controls do not apply to names used to specify
3207 // explicit instantiations.
3208 // We extend this to also cover explicit specializations. Note that
3209 // we don't suppress if this turns out to be an elaborated type
3210 // specifier.
3211 bool shouldDelayDiagsInTag =
3212 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3213 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3214 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003215
Richard Smith7796eb52012-03-12 08:56:40 +00003216 // Enum definitions should not be parsed in a trailing-return-type.
3217 bool AllowDeclaration = DSC != DSC_trailing;
3218
3219 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith80ad52f2013-01-02 11:42:31 +00003220 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smith7796eb52012-03-12 08:56:40 +00003221 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003222
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003223 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003224 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003225 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3226 // if a fixed underlying type is allowed.
3227 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003228
3229 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003230 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00003231 return;
3232
3233 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003234 Diag(Tok, diag::err_expected_ident);
3235 if (Tok.isNot(tok::l_brace)) {
3236 // Has no name and is not a definition.
3237 // Skip the rest of this declarator, up until the comma or semicolon.
3238 SkipUntil(tok::comma, true);
3239 return;
3240 }
3241 }
3242 }
Mike Stump1eb44332009-09-09 15:08:12 +00003243
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003244 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003245 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003246 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003247 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003248
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003249 // Skip the rest of this declarator, up until the comma or semicolon.
3250 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003251 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003252 }
Mike Stump1eb44332009-09-09 15:08:12 +00003253
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003254 // If an identifier is present, consume and remember it.
3255 IdentifierInfo *Name = 0;
3256 SourceLocation NameLoc;
3257 if (Tok.is(tok::identifier)) {
3258 Name = Tok.getIdentifierInfo();
3259 NameLoc = ConsumeToken();
3260 }
Mike Stump1eb44332009-09-09 15:08:12 +00003261
Richard Smithbdad7a22012-01-10 01:33:14 +00003262 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003263 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3264 // declaration of a scoped enumeration.
3265 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003266 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003267 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003268 }
3269
John McCall13489672012-05-07 06:16:58 +00003270 // Okay, end the suppression area. We'll decide whether to emit the
3271 // diagnostics in a second.
3272 if (shouldDelayDiagsInTag)
3273 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003274
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003275 TypeResult BaseType;
3276
Douglas Gregora61b3e72010-12-01 17:42:47 +00003277 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003278 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003279 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003280 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003281 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003282 // If we're in class scope, this can either be an enum declaration with
3283 // an underlying type, or a declaration of a bitfield member. We try to
3284 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003285 // (integer literal, sizeof); if it's still ambiguous, we then consider
3286 // anything that's a simple-type-specifier followed by '(' as an
3287 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003288 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003289 EnterExpressionEvaluationContext Unevaluated(Actions,
3290 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003291 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003292 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003293 // bit-field. This is the common case.
3294 if (TPR == TPResult::True())
3295 PossibleBitfield = true;
3296 // If the next token starts a type-specifier-seq, it may be either a
3297 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003298 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003299 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003300 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003301 GetLookAheadToken(2).getKind() == tok::semi) {
3302 // Consume the ':'.
3303 ConsumeToken();
3304 } else {
3305 // We have the start of a type-specifier-seq, so we have to perform
3306 // tentative parsing to determine whether we have an expression or a
3307 // type.
3308 TentativeParsingAction TPA(*this);
3309
3310 // Consume the ':'.
3311 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003312
3313 // If we see a type specifier followed by an open-brace, we have an
3314 // ambiguity between an underlying type and a C++11 braced
3315 // function-style cast. Resolve this by always treating it as an
3316 // underlying type.
3317 // FIXME: The standard is not entirely clear on how to disambiguate in
3318 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003319 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003320 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003321 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003322 // We'll parse this as a bitfield later.
3323 PossibleBitfield = true;
3324 TPA.Revert();
3325 } else {
3326 // We have a type-specifier-seq.
3327 TPA.Commit();
3328 }
3329 }
3330 } else {
3331 // Consume the ':'.
3332 ConsumeToken();
3333 }
3334
3335 if (!PossibleBitfield) {
3336 SourceRange Range;
3337 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003338
Richard Smith80ad52f2013-01-02 11:42:31 +00003339 if (getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00003340 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003341 } else if (!getLangOpts().ObjC2) {
3342 if (getLangOpts().CPlusPlus)
3343 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3344 else
3345 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3346 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003347 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003348 }
3349
Richard Smithbdad7a22012-01-10 01:33:14 +00003350 // There are four options here. If we have 'friend enum foo;' then this is a
3351 // friend declaration, and cannot have an accompanying definition. If we have
3352 // 'enum foo;', then this is a forward declaration. If we have
3353 // 'enum foo {...' then this is a definition. Otherwise we have something
3354 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003355 //
3356 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3357 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3358 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3359 //
John McCallf312b1e2010-08-26 23:41:50 +00003360 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003361 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003362 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003363 } else if (Tok.is(tok::l_brace)) {
3364 if (DS.isFriendSpecified()) {
3365 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3366 << SourceRange(DS.getFriendSpecLoc());
3367 ConsumeBrace();
3368 SkipUntil(tok::r_brace);
3369 TUK = Sema::TUK_Friend;
3370 } else {
3371 TUK = Sema::TUK_Definition;
3372 }
Richard Smithc9f35172012-06-25 21:37:02 +00003373 } else if (DSC != DSC_type_specifier &&
3374 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003375 (Tok.isAtStartOfLine() &&
3376 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003377 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3378 if (Tok.isNot(tok::semi)) {
3379 // A semicolon was missing after this declaration. Diagnose and recover.
3380 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3381 "enum");
3382 PP.EnterToken(Tok);
3383 Tok.setKind(tok::semi);
3384 }
John McCall13489672012-05-07 06:16:58 +00003385 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003386 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003387 }
3388
3389 // If this is an elaborated type specifier, and we delayed
3390 // diagnostics before, just merge them into the current pool.
3391 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3392 diagsFromTag.redelay();
3393 }
Richard Smith1af83c42012-03-23 03:33:32 +00003394
3395 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003396 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003397 TUK != Sema::TUK_Reference) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003398 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith1af83c42012-03-23 03:33:32 +00003399 // Skip the rest of this declarator, up until the comma or semicolon.
3400 Diag(Tok, diag::err_enum_template);
3401 SkipUntil(tok::comma, true);
3402 return;
3403 }
3404
3405 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3406 // Enumerations can't be explicitly instantiated.
3407 DS.SetTypeSpecError();
3408 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3409 return;
3410 }
3411
3412 assert(TemplateInfo.TemplateParams && "no template parameters");
3413 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3414 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003415 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003416
Sean Hunt2edf0a22012-06-23 05:07:58 +00003417 if (TUK == Sema::TUK_Reference)
3418 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003419
Douglas Gregorb9075602011-02-22 02:55:24 +00003420 if (!Name && TUK != Sema::TUK_Definition) {
3421 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003422
Douglas Gregorb9075602011-02-22 02:55:24 +00003423 // Skip the rest of this declarator, up until the comma or semicolon.
3424 SkipUntil(tok::comma, true);
3425 return;
3426 }
Richard Smith1af83c42012-03-23 03:33:32 +00003427
Douglas Gregor402abb52009-05-28 23:31:59 +00003428 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003429 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003430 const char *PrevSpec = 0;
3431 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003432 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003433 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003434 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003435 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003436 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003437
Douglas Gregor48c89f42010-04-24 16:38:41 +00003438 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003439 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003440 // dependent tag.
3441 if (!Name) {
3442 DS.SetTypeSpecError();
3443 Diag(Tok, diag::err_expected_type_name_after_typename);
3444 return;
3445 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003446
Douglas Gregor23c94db2010-07-02 17:43:08 +00003447 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003448 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003449 NameLoc);
3450 if (Type.isInvalid()) {
3451 DS.SetTypeSpecError();
3452 return;
3453 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003454
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003455 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3456 NameLoc.isValid() ? NameLoc : StartLoc,
3457 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003458 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003459
Douglas Gregor48c89f42010-04-24 16:38:41 +00003460 return;
3461 }
Mike Stump1eb44332009-09-09 15:08:12 +00003462
John McCalld226f652010-08-21 09:40:31 +00003463 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003464 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003465 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003466 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003467 ConsumeBrace();
3468 SkipUntil(tok::r_brace);
3469 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003470
Douglas Gregor48c89f42010-04-24 16:38:41 +00003471 DS.SetTypeSpecError();
3472 return;
3473 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003474
Richard Smithc9f35172012-06-25 21:37:02 +00003475 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003476 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003477
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003478 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3479 NameLoc.isValid() ? NameLoc : StartLoc,
3480 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003481 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003482}
3483
3484/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3485/// enumerator-list:
3486/// enumerator
3487/// enumerator-list ',' enumerator
3488/// enumerator:
3489/// enumeration-constant
3490/// enumeration-constant '=' constant-expression
3491/// enumeration-constant:
3492/// identifier
3493///
John McCalld226f652010-08-21 09:40:31 +00003494void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003495 // Enter the scope of the enum body and start the definition.
3496 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003497 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003498
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003499 BalancedDelimiterTracker T(*this, tok::l_brace);
3500 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003501
Chris Lattner7946dd32007-08-27 17:24:30 +00003502 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003503 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003504 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003505
Chris Lattner5f9e2722011-07-23 10:55:15 +00003506 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003507
John McCalld226f652010-08-21 09:40:31 +00003508 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003509
Reid Spencer5f016e22007-07-11 17:01:13 +00003510 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003511 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003512 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3513 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003514
John McCall5b629aa2010-10-22 23:36:17 +00003515 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003516 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003517 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003518 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003519 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003520
Reid Spencer5f016e22007-07-11 17:01:13 +00003521 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003522 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003523 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003524
Chris Lattner04d66662007-10-09 17:33:22 +00003525 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003526 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003527 AssignedVal = ParseConstantExpression();
3528 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003529 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003530 }
Mike Stump1eb44332009-09-09 15:08:12 +00003531
Reid Spencer5f016e22007-07-11 17:01:13 +00003532 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003533 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3534 LastEnumConstDecl,
3535 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003536 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003537 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003538 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003539
Reid Spencer5f016e22007-07-11 17:01:13 +00003540 EnumConstantDecls.push_back(EnumConstDecl);
3541 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003542
Douglas Gregor751f6922010-09-07 14:51:08 +00003543 if (Tok.is(tok::identifier)) {
3544 // We're missing a comma between enumerators.
3545 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003546 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003547 << FixItHint::CreateInsertion(Loc, ", ");
3548 continue;
3549 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003550
Chris Lattner04d66662007-10-09 17:33:22 +00003551 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003552 break;
3553 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003554
Richard Smith7fe62082011-10-15 05:09:34 +00003555 if (Tok.isNot(tok::identifier)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003556 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003557 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3558 diag::ext_enumerator_list_comma_cxx :
3559 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003560 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith80ad52f2013-01-02 11:42:31 +00003561 else if (getLangOpts().CPlusPlus11)
Richard Smith7fe62082011-10-15 05:09:34 +00003562 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3563 << FixItHint::CreateRemoval(CommaLoc);
3564 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003565 }
Mike Stump1eb44332009-09-09 15:08:12 +00003566
Reid Spencer5f016e22007-07-11 17:01:13 +00003567 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003568 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003569
Reid Spencer5f016e22007-07-11 17:01:13 +00003570 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003571 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003572 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003573
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003574 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3575 EnumDecl, EnumConstantDecls.data(),
3576 EnumConstantDecls.size(), getCurScope(),
3577 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003578
Douglas Gregor72de6672009-01-08 20:45:30 +00003579 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003580 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3581 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003582
3583 // The next token must be valid after an enum definition. If not, a ';'
3584 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003585 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3586 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003587 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3588 // Push this token back into the preprocessor and change our current token
3589 // to ';' so that the rest of the code recovers as though there were an
3590 // ';' after the definition.
3591 PP.EnterToken(Tok);
3592 Tok.setKind(tok::semi);
3593 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003594}
3595
3596/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003597/// start of a type-qualifier-list.
3598bool Parser::isTypeQualifier() const {
3599 switch (Tok.getKind()) {
3600 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003601
3602 // type-qualifier only in OpenCL
3603 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003604 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003605
Steve Naroff5f8aa692008-02-11 23:15:56 +00003606 // type-qualifier
3607 case tok::kw_const:
3608 case tok::kw_volatile:
3609 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003610 case tok::kw___private:
3611 case tok::kw___local:
3612 case tok::kw___global:
3613 case tok::kw___constant:
3614 case tok::kw___read_only:
3615 case tok::kw___read_write:
3616 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003617 return true;
3618 }
3619}
3620
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003621/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3622/// is definitely a type-specifier. Return false if it isn't part of a type
3623/// specifier or if we're not sure.
3624bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3625 switch (Tok.getKind()) {
3626 default: return false;
3627 // type-specifiers
3628 case tok::kw_short:
3629 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003630 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003631 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003632 case tok::kw_signed:
3633 case tok::kw_unsigned:
3634 case tok::kw__Complex:
3635 case tok::kw__Imaginary:
3636 case tok::kw_void:
3637 case tok::kw_char:
3638 case tok::kw_wchar_t:
3639 case tok::kw_char16_t:
3640 case tok::kw_char32_t:
3641 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003642 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003643 case tok::kw_float:
3644 case tok::kw_double:
3645 case tok::kw_bool:
3646 case tok::kw__Bool:
3647 case tok::kw__Decimal32:
3648 case tok::kw__Decimal64:
3649 case tok::kw__Decimal128:
3650 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003651
Guy Benyeib13621d2012-12-18 14:38:23 +00003652 // OpenCL specific types:
3653 case tok::kw_image1d_t:
3654 case tok::kw_image1d_array_t:
3655 case tok::kw_image1d_buffer_t:
3656 case tok::kw_image2d_t:
3657 case tok::kw_image2d_array_t:
3658 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003659 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003660 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003661
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003662 // struct-or-union-specifier (C99) or class-specifier (C++)
3663 case tok::kw_class:
3664 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003665 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003666 case tok::kw_union:
3667 // enum-specifier
3668 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003669
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003670 // typedef-name
3671 case tok::annot_typename:
3672 return true;
3673 }
3674}
3675
Steve Naroff5f8aa692008-02-11 23:15:56 +00003676/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003677/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003678bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003679 switch (Tok.getKind()) {
3680 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003681
Chris Lattner166a8fc2009-01-04 23:41:41 +00003682 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003683 if (TryAltiVecVectorToken())
3684 return true;
3685 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003686 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003687 // Annotate typenames and C++ scope specifiers. If we get one, just
3688 // recurse to handle whatever we get.
3689 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003690 return true;
3691 if (Tok.is(tok::identifier))
3692 return false;
3693 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003694
Chris Lattner166a8fc2009-01-04 23:41:41 +00003695 case tok::coloncolon: // ::foo::bar
3696 if (NextToken().is(tok::kw_new) || // ::new
3697 NextToken().is(tok::kw_delete)) // ::delete
3698 return false;
3699
Chris Lattner166a8fc2009-01-04 23:41:41 +00003700 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003701 return true;
3702 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003703
Reid Spencer5f016e22007-07-11 17:01:13 +00003704 // GNU attributes support.
3705 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003706 // GNU typeof support.
3707 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003708
Reid Spencer5f016e22007-07-11 17:01:13 +00003709 // type-specifiers
3710 case tok::kw_short:
3711 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003712 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003713 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003714 case tok::kw_signed:
3715 case tok::kw_unsigned:
3716 case tok::kw__Complex:
3717 case tok::kw__Imaginary:
3718 case tok::kw_void:
3719 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003720 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003721 case tok::kw_char16_t:
3722 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003723 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003724 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003725 case tok::kw_float:
3726 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003727 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003728 case tok::kw__Bool:
3729 case tok::kw__Decimal32:
3730 case tok::kw__Decimal64:
3731 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003732 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003733
Guy Benyeib13621d2012-12-18 14:38:23 +00003734 // OpenCL specific types:
3735 case tok::kw_image1d_t:
3736 case tok::kw_image1d_array_t:
3737 case tok::kw_image1d_buffer_t:
3738 case tok::kw_image2d_t:
3739 case tok::kw_image2d_array_t:
3740 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003741 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003742 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003743
Chris Lattner99dc9142008-04-13 18:59:07 +00003744 // struct-or-union-specifier (C99) or class-specifier (C++)
3745 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003746 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003747 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003748 case tok::kw_union:
3749 // enum-specifier
3750 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003751
Reid Spencer5f016e22007-07-11 17:01:13 +00003752 // type-qualifier
3753 case tok::kw_const:
3754 case tok::kw_volatile:
3755 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003756
John McCallb8a8de32012-11-14 00:49:39 +00003757 // Debugger support.
3758 case tok::kw___unknown_anytype:
3759
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003760 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003761 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003762 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003763
Chris Lattner7c186be2008-10-20 00:25:30 +00003764 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3765 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003766 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003767
Steve Naroff239f0732008-12-25 14:16:32 +00003768 case tok::kw___cdecl:
3769 case tok::kw___stdcall:
3770 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003771 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003772 case tok::kw___w64:
3773 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003774 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003775 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003776 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003777
3778 case tok::kw___private:
3779 case tok::kw___local:
3780 case tok::kw___global:
3781 case tok::kw___constant:
3782 case tok::kw___read_only:
3783 case tok::kw___read_write:
3784 case tok::kw___write_only:
3785
Eli Friedman290eeb02009-06-08 23:27:34 +00003786 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003787
3788 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003789 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003790
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003791 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003792 case tok::kw__Atomic:
3793 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003794 }
3795}
3796
3797/// isDeclarationSpecifier() - Return true if the current token is part of a
3798/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003799///
3800/// \param DisambiguatingWithExpression True to indicate that the purpose of
3801/// this check is to disambiguate between an expression and a declaration.
3802bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003803 switch (Tok.getKind()) {
3804 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003805
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003806 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003807 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003808
Chris Lattner166a8fc2009-01-04 23:41:41 +00003809 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003810 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003811 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003812 return false;
John Thompson82287d12010-02-05 00:12:22 +00003813 if (TryAltiVecVectorToken())
3814 return true;
3815 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003816 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003817 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003818 // Annotate typenames and C++ scope specifiers. If we get one, just
3819 // recurse to handle whatever we get.
3820 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003821 return true;
3822 if (Tok.is(tok::identifier))
3823 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003824
Douglas Gregor9497a732010-09-16 01:51:54 +00003825 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00003826 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00003827 // expression is permitted, then this is probably a class message send
3828 // missing the initial '['. In this case, we won't consider this to be
3829 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00003830 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00003831 isStartOfObjCClassMessageMissingOpenBracket())
3832 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003833
John McCall9ba61662010-02-26 08:45:28 +00003834 return isDeclarationSpecifier();
3835
Chris Lattner166a8fc2009-01-04 23:41:41 +00003836 case tok::coloncolon: // ::foo::bar
3837 if (NextToken().is(tok::kw_new) || // ::new
3838 NextToken().is(tok::kw_delete)) // ::delete
3839 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003840
Chris Lattner166a8fc2009-01-04 23:41:41 +00003841 // Annotate typenames and C++ scope specifiers. If we get one, just
3842 // recurse to handle whatever we get.
3843 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003844 return true;
3845 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003846
Reid Spencer5f016e22007-07-11 17:01:13 +00003847 // storage-class-specifier
3848 case tok::kw_typedef:
3849 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003850 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003851 case tok::kw_static:
3852 case tok::kw_auto:
3853 case tok::kw_register:
3854 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003855
Douglas Gregor8d267c52011-09-09 02:06:17 +00003856 // Modules
3857 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00003858
John McCallb8a8de32012-11-14 00:49:39 +00003859 // Debugger support
3860 case tok::kw___unknown_anytype:
3861
Reid Spencer5f016e22007-07-11 17:01:13 +00003862 // type-specifiers
3863 case tok::kw_short:
3864 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003865 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003866 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003867 case tok::kw_signed:
3868 case tok::kw_unsigned:
3869 case tok::kw__Complex:
3870 case tok::kw__Imaginary:
3871 case tok::kw_void:
3872 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003873 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003874 case tok::kw_char16_t:
3875 case tok::kw_char32_t:
3876
Reid Spencer5f016e22007-07-11 17:01:13 +00003877 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003878 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003879 case tok::kw_float:
3880 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003881 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003882 case tok::kw__Bool:
3883 case tok::kw__Decimal32:
3884 case tok::kw__Decimal64:
3885 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003886 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003887
Guy Benyeib13621d2012-12-18 14:38:23 +00003888 // OpenCL specific types:
3889 case tok::kw_image1d_t:
3890 case tok::kw_image1d_array_t:
3891 case tok::kw_image1d_buffer_t:
3892 case tok::kw_image2d_t:
3893 case tok::kw_image2d_array_t:
3894 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003895 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003896 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003897
Chris Lattner99dc9142008-04-13 18:59:07 +00003898 // struct-or-union-specifier (C99) or class-specifier (C++)
3899 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003900 case tok::kw_struct:
3901 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00003902 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003903 // enum-specifier
3904 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003905
Reid Spencer5f016e22007-07-11 17:01:13 +00003906 // type-qualifier
3907 case tok::kw_const:
3908 case tok::kw_volatile:
3909 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003910
Reid Spencer5f016e22007-07-11 17:01:13 +00003911 // function-specifier
3912 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003913 case tok::kw_virtual:
3914 case tok::kw_explicit:
Richard Smithde03c152013-01-17 22:16:11 +00003915 case tok::kw__Noreturn:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003916
Richard Smith4cd81c52013-01-29 09:02:09 +00003917 // alignment-specifier
3918 case tok::kw__Alignas:
3919
Richard Smith53aec2a2012-10-25 00:00:53 +00003920 // friend keyword.
3921 case tok::kw_friend:
3922
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003923 // static_assert-declaration
3924 case tok::kw__Static_assert:
3925
Chris Lattner1ef08762007-08-09 17:01:07 +00003926 // GNU typeof support.
3927 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003928
Chris Lattner1ef08762007-08-09 17:01:07 +00003929 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003930 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00003931
Richard Smith53aec2a2012-10-25 00:00:53 +00003932 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003933 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00003934 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00003935
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003936 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003937 case tok::kw__Atomic:
3938 return true;
3939
Chris Lattnerf3948c42008-07-26 03:38:44 +00003940 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3941 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003942 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003943
Douglas Gregord9d75e52011-04-27 05:41:15 +00003944 // typedef-name
3945 case tok::annot_typename:
3946 return !DisambiguatingWithExpression ||
3947 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00003948
Steve Naroff47f52092009-01-06 19:34:12 +00003949 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003950 case tok::kw___cdecl:
3951 case tok::kw___stdcall:
3952 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003953 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003954 case tok::kw___w64:
3955 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003956 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003957 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003958 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003959 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003960
3961 case tok::kw___private:
3962 case tok::kw___local:
3963 case tok::kw___global:
3964 case tok::kw___constant:
3965 case tok::kw___read_only:
3966 case tok::kw___read_write:
3967 case tok::kw___write_only:
3968
Eli Friedman290eeb02009-06-08 23:27:34 +00003969 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003970 }
3971}
3972
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003973bool Parser::isConstructorDeclarator() {
3974 TentativeParsingAction TPA(*this);
3975
3976 // Parse the C++ scope specifier.
3977 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00003978 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003979 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003980 TPA.Revert();
3981 return false;
3982 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003983
3984 // Parse the constructor name.
3985 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3986 // We already know that we have a constructor name; just consume
3987 // the token.
3988 ConsumeToken();
3989 } else {
3990 TPA.Revert();
3991 return false;
3992 }
3993
Richard Smith22592862012-03-27 23:05:05 +00003994 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003995 if (Tok.isNot(tok::l_paren)) {
3996 TPA.Revert();
3997 return false;
3998 }
3999 ConsumeParen();
4000
Richard Smith22592862012-03-27 23:05:05 +00004001 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4002 // that we have a constructor.
4003 if (Tok.is(tok::r_paren) ||
4004 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004005 TPA.Revert();
4006 return true;
4007 }
4008
4009 // If we need to, enter the specified scope.
4010 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00004011 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004012 DeclScopeObj.EnterDeclaratorScope();
4013
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004014 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00004015 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004016 MaybeParseMicrosoftAttributes(Attrs);
4017
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004018 // Check whether the next token(s) are part of a declaration
4019 // specifier, in which case we have the start of a parameter and,
4020 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00004021 bool IsConstructor = false;
4022 if (isDeclarationSpecifier())
4023 IsConstructor = true;
4024 else if (Tok.is(tok::identifier) ||
4025 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4026 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4027 // This might be a parenthesized member name, but is more likely to
4028 // be a constructor declaration with an invalid argument type. Keep
4029 // looking.
4030 if (Tok.is(tok::annot_cxxscope))
4031 ConsumeToken();
4032 ConsumeToken();
4033
4034 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00004035 // which must have one of the following syntactic forms (see the
4036 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00004037 switch (Tok.getKind()) {
4038 case tok::l_paren:
4039 // C(X ( int));
4040 case tok::l_square:
4041 // C(X [ 5]);
4042 // C(X [ [attribute]]);
4043 case tok::coloncolon:
4044 // C(X :: Y);
4045 // C(X :: *p);
4046 case tok::r_paren:
4047 // C(X )
4048 // Assume this isn't a constructor, rather than assuming it's a
4049 // constructor with an unnamed parameter of an ill-formed type.
4050 break;
4051
4052 default:
4053 IsConstructor = true;
4054 break;
4055 }
4056 }
4057
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004058 TPA.Revert();
4059 return IsConstructor;
4060}
Reid Spencer5f016e22007-07-11 17:01:13 +00004061
4062/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00004063/// type-qualifier-list: [C99 6.7.5]
4064/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004065/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004066/// [ only if VendorAttributesAllowed=true ]
4067/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004068/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004069/// [ only if VendorAttributesAllowed=true ]
4070/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith4e24f0f2013-01-02 12:01:23 +00004071/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik52fc3142010-09-03 01:29:35 +00004072/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00004073///
Dawn Perchik52fc3142010-09-03 01:29:35 +00004074void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4075 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00004076 bool CXX11AttributesAllowed) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004077 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00004078 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00004079 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00004080 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004081 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004082 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004083
4084 SourceLocation EndLoc;
4085
Reid Spencer5f016e22007-07-11 17:01:13 +00004086 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004087 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004088 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004089 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004090 SourceLocation Loc = Tok.getLocation();
4091
4092 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004093 case tok::code_completion:
4094 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004095 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004096
Reid Spencer5f016e22007-07-11 17:01:13 +00004097 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004098 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004099 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004100 break;
4101 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004102 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004103 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004104 break;
4105 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004106 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004107 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004108 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004109
4110 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00004111 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004112 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004113 goto DoneWithTypeQuals;
4114 case tok::kw___private:
4115 case tok::kw___global:
4116 case tok::kw___local:
4117 case tok::kw___constant:
4118 case tok::kw___read_only:
4119 case tok::kw___write_only:
4120 case tok::kw___read_write:
4121 ParseOpenCLQualifiers(DS);
4122 break;
4123
Eli Friedman290eeb02009-06-08 23:27:34 +00004124 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004125 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004126 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004127 case tok::kw___cdecl:
4128 case tok::kw___stdcall:
4129 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004130 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004131 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004132 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004133 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004134 continue;
4135 }
4136 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004137 case tok::kw___pascal:
4138 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004139 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004140 continue;
4141 }
4142 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004143 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004144 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004145 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004146 continue; // do *not* consume the next token!
4147 }
4148 // otherwise, FALL THROUGH!
4149 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004150 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004151 // If this is not a type-qualifier token, we're done reading type
4152 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004153 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004154 if (EndLoc.isValid())
4155 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004156 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004157 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004158
Reid Spencer5f016e22007-07-11 17:01:13 +00004159 // If the specifier combination wasn't legal, issue a diagnostic.
4160 if (isInvalid) {
4161 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004162 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004163 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004164 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004165 }
4166}
4167
4168
4169/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4170///
4171void Parser::ParseDeclarator(Declarator &D) {
4172 /// This implements the 'declarator' production in the C grammar, then checks
4173 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004174 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004175}
4176
Richard Smith9988f282012-03-29 01:16:42 +00004177static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4178 if (Kind == tok::star || Kind == tok::caret)
4179 return true;
4180
4181 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4182 if (!Lang.CPlusPlus)
4183 return false;
4184
4185 return Kind == tok::amp || Kind == tok::ampamp;
4186}
4187
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004188/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4189/// is parsed by the function passed to it. Pass null, and the direct-declarator
4190/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004191/// ptr-operator production.
4192///
Richard Smith0706df42011-10-19 21:33:05 +00004193/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004194/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4195/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004196///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004197/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4198/// [C] pointer[opt] direct-declarator
4199/// [C++] direct-declarator
4200/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004201///
4202/// pointer: [C99 6.7.5]
4203/// '*' type-qualifier-list[opt]
4204/// '*' type-qualifier-list[opt] pointer
4205///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004206/// ptr-operator:
4207/// '*' cv-qualifier-seq[opt]
4208/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004209/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004210/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004211/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004212/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004213void Parser::ParseDeclaratorInternal(Declarator &D,
4214 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004215 if (Diags.hasAllExtensionsSilenced())
4216 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004217
Sebastian Redlf30208a2009-01-24 21:16:55 +00004218 // C++ member pointers start with a '::' or a nested-name.
4219 // Member pointers get special handling, since there's no place for the
4220 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004221 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004222 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4223 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004224 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4225 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004226 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004227 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004228
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004229 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004230 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004231 // The scope spec really belongs to the direct-declarator.
Richard Smith6a502c42013-01-08 22:43:49 +00004232 if (D.mayHaveIdentifier())
4233 D.getCXXScopeSpec() = SS;
4234 else
4235 AnnotateScopeToken(SS, true);
4236
Sebastian Redlf30208a2009-01-24 21:16:55 +00004237 if (DirectDeclParser)
4238 (this->*DirectDeclParser)(D);
4239 return;
4240 }
4241
4242 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004243 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004244 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004245 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004246 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004247
4248 // Recurse to parse whatever is left.
4249 ParseDeclaratorInternal(D, DirectDeclParser);
4250
4251 // Sema will have to catch (syntactically invalid) pointers into global
4252 // scope. It has to catch pointers into namespace scope anyway.
4253 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004254 Loc),
4255 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004256 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004257 return;
4258 }
4259 }
4260
4261 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004262 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004263 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004264 if (DirectDeclParser)
4265 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004266 return;
4267 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004268
Sebastian Redl05532f22009-03-15 22:02:01 +00004269 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4270 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004271 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004272 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004273
Chris Lattner9af55002009-03-27 04:18:06 +00004274 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004275 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004276 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004277
Richard Smith6ee326a2012-04-10 01:32:12 +00004278 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004279 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004280 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004281
Reid Spencer5f016e22007-07-11 17:01:13 +00004282 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004283 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004284 if (Kind == tok::star)
4285 // Remember that we parsed a pointer type, and remember the type-quals.
4286 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004287 DS.getConstSpecLoc(),
4288 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004289 DS.getRestrictSpecLoc()),
4290 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004291 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004292 else
4293 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004294 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004295 Loc),
4296 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004297 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004298 } else {
4299 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004300 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004301
Sebastian Redl743de1f2009-03-23 00:00:23 +00004302 // Complain about rvalue references in C++03, but then go on and build
4303 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004304 if (Kind == tok::ampamp)
Richard Smith80ad52f2013-01-02 11:42:31 +00004305 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004306 diag::warn_cxx98_compat_rvalue_reference :
4307 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004308
Richard Smith6ee326a2012-04-10 01:32:12 +00004309 // GNU-style and C++11 attributes are allowed here, as is restrict.
4310 ParseTypeQualifierListOpt(DS);
4311 D.ExtendWithDeclSpec(DS);
4312
Reid Spencer5f016e22007-07-11 17:01:13 +00004313 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4314 // cv-qualifiers are introduced through the use of a typedef or of a
4315 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004316 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4317 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4318 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004319 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004320 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4321 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004322 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00004323 }
4324
4325 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004326 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004327
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004328 if (D.getNumTypeObjects() > 0) {
4329 // C++ [dcl.ref]p4: There shall be no references to references.
4330 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4331 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004332 if (const IdentifierInfo *II = D.getIdentifier())
4333 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4334 << II;
4335 else
4336 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4337 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004338
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004339 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004340 // can go ahead and build the (technically ill-formed)
4341 // declarator: reference collapsing will take care of it.
4342 }
4343 }
4344
Reid Spencer5f016e22007-07-11 17:01:13 +00004345 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00004346 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004347 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004348 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004349 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004350 }
4351}
4352
Richard Smith9988f282012-03-29 01:16:42 +00004353static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4354 SourceLocation EllipsisLoc) {
4355 if (EllipsisLoc.isValid()) {
4356 FixItHint Insertion;
4357 if (!D.getEllipsisLoc().isValid()) {
4358 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4359 D.setEllipsisLoc(EllipsisLoc);
4360 }
4361 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4362 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4363 }
4364}
4365
Reid Spencer5f016e22007-07-11 17:01:13 +00004366/// ParseDirectDeclarator
4367/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004368/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004369/// '(' declarator ')'
4370/// [GNU] '(' attributes declarator ')'
4371/// [C90] direct-declarator '[' constant-expression[opt] ']'
4372/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4373/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4374/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4375/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004376/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4377/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004378/// direct-declarator '(' parameter-type-list ')'
4379/// direct-declarator '(' identifier-list[opt] ')'
4380/// [GNU] direct-declarator '(' parameter-forward-declarations
4381/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004382/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4383/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004384/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4385/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4386/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004387/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004388/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004389///
4390/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004391/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004392/// '::'[opt] nested-name-specifier[opt] type-name
4393///
4394/// id-expression: [C++ 5.1]
4395/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004396/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004397///
4398/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004399/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004400/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004401/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004402/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004403/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004404///
Richard Smith5d8388c2012-03-27 01:42:32 +00004405/// Note, any additional constructs added here may need corresponding changes
4406/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004407void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004408 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004409
David Blaikie4e4d0842012-03-11 07:00:24 +00004410 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004411 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004412 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004413 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4414 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004415 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004416 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004417 }
4418
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004419 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004420 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004421 // Change the declaration context for name lookup, until this function
4422 // is exited (and the declarator has been parsed).
4423 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004424 }
4425
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004426 // C++0x [dcl.fct]p14:
4427 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004428 // of a parameter-declaration-clause without a preceding comma. In
4429 // this case, the ellipsis is parsed as part of the
4430 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004431 // parameter pack that has not been expanded; otherwise, it is parsed
4432 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004433 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004434 !((D.getContext() == Declarator::PrototypeContext ||
4435 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004436 NextToken().is(tok::r_paren) &&
Richard Smith30f2a742013-02-20 20:19:27 +00004437 !D.hasGroupingParens() &&
Richard Smith9988f282012-03-29 01:16:42 +00004438 !Actions.containsUnexpandedParameterPacks(D))) {
4439 SourceLocation EllipsisLoc = ConsumeToken();
4440 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4441 // The ellipsis was put in the wrong place. Recover, and explain to
4442 // the user what they should have done.
4443 ParseDeclarator(D);
4444 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4445 return;
4446 } else
4447 D.setEllipsisLoc(EllipsisLoc);
4448
4449 // The ellipsis can't be followed by a parenthesized declarator. We
4450 // check for that in ParseParenDeclarator, after we have disambiguated
4451 // the l_paren token.
4452 }
4453
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004454 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4455 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4456 // We found something that indicates the start of an unqualified-id.
4457 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004458 bool AllowConstructorName;
4459 if (D.getDeclSpec().hasTypeSpecifier())
4460 AllowConstructorName = false;
4461 else if (D.getCXXScopeSpec().isSet())
4462 AllowConstructorName =
4463 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00004464 D.getContext() == Declarator::MemberContext);
John McCallba9d8532010-04-13 06:39:49 +00004465 else
4466 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4467
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004468 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004469 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4470 /*EnteringContext=*/true,
4471 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004472 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004473 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004474 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004475 D.getName()) ||
4476 // Once we're past the identifier, if the scope was bad, mark the
4477 // whole declarator bad.
4478 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004479 D.SetIdentifier(0, Tok.getLocation());
4480 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004481 } else {
4482 // Parsed the unqualified-id; update range information and move along.
4483 if (D.getSourceRange().getBegin().isInvalid())
4484 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4485 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004486 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004487 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004488 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004489 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004490 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004491 "There's a C++-specific check for tok::identifier above");
4492 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4493 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4494 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004495 goto PastIdentifier;
4496 }
Richard Smith9988f282012-03-29 01:16:42 +00004497
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004498 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004499 // direct-declarator: '(' declarator ')'
4500 // direct-declarator: '(' attributes declarator ')'
4501 // Example: 'char (*X)' or 'int (*XX)(void)'
4502 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004503
4504 // If the declarator was parenthesized, we entered the declarator
4505 // scope when parsing the parenthesized declarator, then exited
4506 // the scope already. Re-enter the scope, if we need to.
4507 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004508 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004509 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004510 if (!D.isInvalidType() &&
4511 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004512 // Change the declaration context for name lookup, until this function
4513 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004514 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004515 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004516 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004517 // This could be something simple like "int" (in which case the declarator
4518 // portion is empty), if an abstract-declarator is allowed.
4519 D.SetIdentifier(0, Tok.getLocation());
Richard Smith30f2a742013-02-20 20:19:27 +00004520
4521 // The grammar for abstract-pack-declarator does not allow grouping parens.
4522 // FIXME: Revisit this once core issue 1488 is resolved.
4523 if (D.hasEllipsis() && D.hasGroupingParens())
4524 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4525 diag::ext_abstract_pack_declarator_parens);
Reid Spencer5f016e22007-07-11 17:01:13 +00004526 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004527 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004528 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004529 if (D.getContext() == Declarator::MemberContext)
4530 Diag(Tok, diag::err_expected_member_name_or_semi)
4531 << D.getDeclSpec().getSourceRange();
Richard Trieudb55c04c2013-01-26 02:31:38 +00004532 else if (getLangOpts().CPlusPlus) {
4533 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4534 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4535 else
4536 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4537 } else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004538 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004539 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004540 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004541 }
Mike Stump1eb44332009-09-09 15:08:12 +00004542
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004543 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004544 assert(D.isPastIdentifier() &&
4545 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004546
Richard Smith6ee326a2012-04-10 01:32:12 +00004547 // Don't parse attributes unless we have parsed an unparenthesized name.
4548 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith4e24f0f2013-01-02 12:01:23 +00004549 MaybeParseCXX11Attributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004550
Reid Spencer5f016e22007-07-11 17:01:13 +00004551 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004552 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004553 // Enter function-declaration scope, limiting any declarators to the
4554 // function prototype scope, including parameter declarators.
4555 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004556 Scope::FunctionPrototypeScope|Scope::DeclScope|
4557 (D.isFunctionDeclaratorAFunctionDeclaration()
4558 ? Scope::FunctionDeclarationScope : 0));
4559
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004560 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4561 // In such a case, check if we actually have a function declarator; if it
4562 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004563 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004564 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4565 // The name of the declarator, if any, is tentatively declared within
4566 // a possible direct initializer.
4567 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4568 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4569 TentativelyDeclaredIdentifiers.pop_back();
4570 if (!IsFunctionDecl)
4571 break;
4572 }
John McCall0b7e6782011-03-24 11:26:52 +00004573 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004574 BalancedDelimiterTracker T(*this, tok::l_paren);
4575 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004576 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004577 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004578 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004579 ParseBracketDeclarator(D);
4580 } else {
4581 break;
4582 }
4583 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004584}
Reid Spencer5f016e22007-07-11 17:01:13 +00004585
Chris Lattneref4715c2008-04-06 05:45:57 +00004586/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4587/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004588/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004589/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4590///
4591/// direct-declarator:
4592/// '(' declarator ')'
4593/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004594/// direct-declarator '(' parameter-type-list ')'
4595/// direct-declarator '(' identifier-list[opt] ')'
4596/// [GNU] direct-declarator '(' parameter-forward-declarations
4597/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004598///
4599void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004600 BalancedDelimiterTracker T(*this, tok::l_paren);
4601 T.consumeOpen();
4602
Chris Lattneref4715c2008-04-06 05:45:57 +00004603 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004604
Chris Lattner7399ee02008-10-20 02:05:46 +00004605 // Eat any attributes before we look at whether this is a grouping or function
4606 // declarator paren. If this is a grouping paren, the attribute applies to
4607 // the type being built up, for example:
4608 // int (__attribute__(()) *x)(long y)
4609 // If this ends up not being a grouping paren, the attribute applies to the
4610 // first argument, for example:
4611 // int (__attribute__(()) int x)
4612 // In either case, we need to eat any attributes to be able to determine what
4613 // sort of paren this is.
4614 //
John McCall0b7e6782011-03-24 11:26:52 +00004615 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004616 bool RequiresArg = false;
4617 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004618 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004619
Chris Lattner7399ee02008-10-20 02:05:46 +00004620 // We require that the argument list (if this is a non-grouping paren) be
4621 // present even if the attribute list was empty.
4622 RequiresArg = true;
4623 }
Chad Rosier9cab1c92012-12-21 21:22:20 +00004624
Steve Naroff239f0732008-12-25 14:16:32 +00004625 // Eat any Microsoft extensions.
Chad Rosier9cab1c92012-12-21 21:22:20 +00004626 ParseMicrosoftTypeAttributes(attrs);
4627
Dawn Perchik52fc3142010-09-03 01:29:35 +00004628 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004629 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004630 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004631
Chris Lattneref4715c2008-04-06 05:45:57 +00004632 // If we haven't past the identifier yet (or where the identifier would be
4633 // stored, if this is an abstract declarator), then this is probably just
4634 // grouping parens. However, if this could be an abstract-declarator, then
4635 // this could also be the start of function arguments (consider 'void()').
4636 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004637
Chris Lattneref4715c2008-04-06 05:45:57 +00004638 if (!D.mayOmitIdentifier()) {
4639 // If this can't be an abstract-declarator, this *must* be a grouping
4640 // paren, because we haven't seen the identifier yet.
4641 isGrouping = true;
4642 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004643 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4644 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004645 isDeclarationSpecifier() || // 'int(int)' is a function.
4646 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004647 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4648 // considered to be a type, not a K&R identifier-list.
4649 isGrouping = false;
4650 } else {
4651 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4652 isGrouping = true;
4653 }
Mike Stump1eb44332009-09-09 15:08:12 +00004654
Chris Lattneref4715c2008-04-06 05:45:57 +00004655 // If this is a grouping paren, handle:
4656 // direct-declarator: '(' declarator ')'
4657 // direct-declarator: '(' attributes declarator ')'
4658 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004659 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4660 D.setEllipsisLoc(SourceLocation());
4661
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004662 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004663 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004664 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004665 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004666 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004667 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004668 T.getCloseLocation()),
4669 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004670
4671 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004672
4673 // An ellipsis cannot be placed outside parentheses.
4674 if (EllipsisLoc.isValid())
4675 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4676
Chris Lattneref4715c2008-04-06 05:45:57 +00004677 return;
4678 }
Mike Stump1eb44332009-09-09 15:08:12 +00004679
Chris Lattneref4715c2008-04-06 05:45:57 +00004680 // Okay, if this wasn't a grouping paren, it must be the start of a function
4681 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004682 // identifier (and remember where it would have been), then call into
4683 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004684 D.SetIdentifier(0, Tok.getLocation());
4685
David Blaikie42d6d0c2011-12-04 05:04:18 +00004686 // Enter function-declaration scope, limiting any declarators to the
4687 // function prototype scope, including parameter declarators.
4688 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004689 Scope::FunctionPrototypeScope | Scope::DeclScope |
4690 (D.isFunctionDeclaratorAFunctionDeclaration()
4691 ? Scope::FunctionDeclarationScope : 0));
Richard Smithb9c62612012-07-30 21:30:52 +00004692 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004693 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004694}
4695
4696/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4697/// declarator D up to a paren, which indicates that we are parsing function
4698/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004699///
Richard Smith6ee326a2012-04-10 01:32:12 +00004700/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4701/// immediately after the open paren - they should be considered to be the
4702/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004703///
Richard Smith6ee326a2012-04-10 01:32:12 +00004704/// If RequiresArg is true, then the first argument of the function is required
4705/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004706///
Richard Smith6ee326a2012-04-10 01:32:12 +00004707/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4708/// (C++11) ref-qualifier[opt], exception-specification[opt],
4709/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4710///
4711/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004712/// dynamic-exception-specification
4713/// noexcept-specification
4714///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004715void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004716 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004717 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004718 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004719 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004720 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004721 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004722 // lparen is already consumed!
4723 assert(D.isPastIdentifier() && "Should not call before identifier!");
4724
4725 // This should be true when the function has typed arguments.
4726 // Otherwise, it is treated as a K&R-style function.
4727 bool HasProto = false;
4728 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004729 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004730 // Remember where we see an ellipsis, if any.
4731 SourceLocation EllipsisLoc;
4732
4733 DeclSpec DS(AttrFactory);
4734 bool RefQualifierIsLValueRef = true;
4735 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004736 SourceLocation ConstQualifierLoc;
4737 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004738 ExceptionSpecificationType ESpecType = EST_None;
4739 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004740 SmallVector<ParsedType, 2> DynamicExceptions;
4741 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004742 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004743 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004744 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004745
James Molloy16f1f712012-02-29 10:24:19 +00004746 Actions.ActOnStartFunctionDeclarator();
4747
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004748 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4749 EndLoc is the end location for the function declarator.
4750 They differ for trailing return types. */
4751 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004752 SourceLocation LParenLoc, RParenLoc;
4753 LParenLoc = Tracker.getOpenLocation();
4754 StartLoc = LParenLoc;
4755
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004756 if (isFunctionDeclaratorIdentifierList()) {
4757 if (RequiresArg)
4758 Diag(Tok, diag::err_argument_required_after_attribute);
4759
4760 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4761
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004762 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004763 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004764 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004765 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004766 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004767 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004768 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004769 else if (RequiresArg)
4770 Diag(Tok, diag::err_argument_required_after_attribute);
4771
David Blaikie4e4d0842012-03-11 07:00:24 +00004772 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004773
4774 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004775 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004776 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004777 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004778 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004779
David Blaikie4e4d0842012-03-11 07:00:24 +00004780 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004781 // FIXME: Accept these components in any order, and produce fixits to
4782 // correct the order if the user gets it wrong. Ideally we should deal
4783 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004784
4785 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004786 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4787 if (!DS.getSourceRange().getEnd().isInvalid()) {
4788 EndLoc = DS.getSourceRange().getEnd();
4789 ConstQualifierLoc = DS.getConstSpecLoc();
4790 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4791 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004792
4793 // Parse ref-qualifier[opt].
4794 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004795 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004796 diag::warn_cxx98_compat_ref_qualifier :
4797 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004798
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004799 RefQualifierIsLValueRef = Tok.is(tok::amp);
4800 RefQualifierLoc = ConsumeToken();
4801 EndLoc = RefQualifierLoc;
4802 }
4803
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004804 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00004805 // If a declaration declares a member function or member function
4806 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004807 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00004808 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004809 // declarator.
Chad Rosier8decdee2012-06-26 22:30:43 +00004810 bool IsCXX11MemberFunction =
Richard Smith80ad52f2013-01-02 11:42:31 +00004811 getLangOpts().CPlusPlus11 &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004812 (D.getContext() == Declarator::MemberContext ||
4813 (D.getContext() == Declarator::FileContext &&
Chad Rosier8decdee2012-06-26 22:30:43 +00004814 D.getCXXScopeSpec().isValid() &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004815 Actions.CurContext->isRecord()));
4816 Sema::CXXThisScopeRAII ThisScope(Actions,
4817 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith7b19cb12013-01-14 01:55:13 +00004818 DS.getTypeQualifiers() |
4819 (D.getDeclSpec().isConstexprSpecified()
4820 ? Qualifiers::Const : 0),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004821 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004822
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004823 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004824 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004825 DynamicExceptions,
4826 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004827 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004828 if (ESpecType != EST_None)
4829 EndLoc = ESpecRange.getEnd();
4830
Richard Smith6ee326a2012-04-10 01:32:12 +00004831 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4832 // after the exception-specification.
Richard Smith4e24f0f2013-01-02 12:01:23 +00004833 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004834
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004835 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004836 LocalEndLoc = EndLoc;
Richard Smith80ad52f2013-01-02 11:42:31 +00004837 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004838 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004839 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4840 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004841 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00004842 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004843 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004844 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004845 }
4846 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004847 }
4848
4849 // Remember that we parsed a function type, and remember the attributes.
4850 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004851 IsAmbiguous,
4852 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004853 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004854 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004855 DS.getTypeQualifiers(),
4856 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004857 RefQualifierLoc, ConstQualifierLoc,
4858 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004859 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004860 ESpecType, ESpecRange.getBegin(),
4861 DynamicExceptions.data(),
4862 DynamicExceptionRanges.data(),
4863 DynamicExceptions.size(),
4864 NoexceptExpr.isUsable() ?
4865 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004866 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004867 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004868 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004869
4870 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004871}
4872
4873/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4874/// identifier list form for a K&R-style function: void foo(a,b,c)
4875///
4876/// Note that identifier-lists are only allowed for normal declarators, not for
4877/// abstract-declarators.
4878bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004879 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004880 && Tok.is(tok::identifier)
4881 && !TryAltiVecVectorToken()
4882 // K&R identifier lists can't have typedefs as identifiers, per C99
4883 // 6.7.5.3p11.
4884 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4885 // Identifier lists follow a really simple grammar: the identifiers can
4886 // be followed *only* by a ", identifier" or ")". However, K&R
4887 // identifier lists are really rare in the brave new modern world, and
4888 // it is very common for someone to typo a type in a non-K&R style
4889 // list. If we are presented with something like: "void foo(intptr x,
4890 // float y)", we don't want to start parsing the function declarator as
4891 // though it is a K&R style declarator just because intptr is an
4892 // invalid type.
4893 //
4894 // To handle this, we check to see if the token after the first
4895 // identifier is a "," or ")". Only then do we parse it as an
4896 // identifier list.
4897 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4898}
4899
4900/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4901/// we found a K&R-style identifier list instead of a typed parameter list.
4902///
4903/// After returning, ParamInfo will hold the parsed parameters.
4904///
4905/// identifier-list: [C99 6.7.5]
4906/// identifier
4907/// identifier-list ',' identifier
4908///
4909void Parser::ParseFunctionDeclaratorIdentifierList(
4910 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004911 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004912 // If there was no identifier specified for the declarator, either we are in
4913 // an abstract-declarator, or we are in a parameter declarator which was found
4914 // to be abstract. In abstract-declarators, identifier lists are not valid:
4915 // diagnose this.
4916 if (!D.getIdentifier())
4917 Diag(Tok, diag::ext_ident_list_in_param);
4918
4919 // Maintain an efficient lookup of params we have seen so far.
4920 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4921
4922 while (1) {
4923 // If this isn't an identifier, report the error and skip until ')'.
4924 if (Tok.isNot(tok::identifier)) {
4925 Diag(Tok, diag::err_expected_ident);
4926 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4927 // Forget we parsed anything.
4928 ParamInfo.clear();
4929 return;
4930 }
4931
4932 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4933
4934 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4935 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4936 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4937
4938 // Verify that the argument identifier has not already been mentioned.
4939 if (!ParamsSoFar.insert(ParmII)) {
4940 Diag(Tok, diag::err_param_redefinition) << ParmII;
4941 } else {
4942 // Remember this identifier in ParamInfo.
4943 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4944 Tok.getLocation(),
4945 0));
4946 }
4947
4948 // Eat the identifier.
4949 ConsumeToken();
4950
4951 // The list continues if we see a comma.
4952 if (Tok.isNot(tok::comma))
4953 break;
4954 ConsumeToken();
4955 }
4956}
4957
4958/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4959/// after the opening parenthesis. This function will not parse a K&R-style
4960/// identifier list.
4961///
Richard Smith6ce48a72012-04-11 04:01:28 +00004962/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4963/// caller parsed those arguments immediately after the open paren - they should
4964/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004965///
4966/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4967/// be the location of the ellipsis, if any was parsed.
4968///
Reid Spencer5f016e22007-07-11 17:01:13 +00004969/// parameter-type-list: [C99 6.7.5]
4970/// parameter-list
4971/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004972/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004973///
4974/// parameter-list: [C99 6.7.5]
4975/// parameter-declaration
4976/// parameter-list ',' parameter-declaration
4977///
4978/// parameter-declaration: [C99 6.7.5]
4979/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004980/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004981/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004982/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004983/// declaration-specifiers abstract-declarator[opt]
4984/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004985/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004986/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004987/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004988///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004989void Parser::ParseParameterDeclarationClause(
4990 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004991 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004992 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004993 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004994
Chris Lattnerf97409f2008-04-06 06:57:35 +00004995 while (1) {
4996 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004997 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4998 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004999 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00005000 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00005001 }
Mike Stump1eb44332009-09-09 15:08:12 +00005002
Chris Lattnerf97409f2008-04-06 06:57:35 +00005003 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00005004 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00005005 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005006
Richard Smith6ce48a72012-04-11 04:01:28 +00005007 // Parse any C++11 attributes.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005008 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith6ce48a72012-04-11 04:01:28 +00005009
John McCall7f040a92010-12-24 02:08:15 +00005010 // Skip any Microsoft attributes before a param.
Chad Rosier16f90bf2012-12-20 20:37:53 +00005011 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall7f040a92010-12-24 02:08:15 +00005012
5013 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00005014
5015 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00005016 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005017 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00005018 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5019 // too much hassle.
5020 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00005021
Chris Lattnere64c5492009-02-27 18:38:20 +00005022 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00005023
Chris Lattnerf97409f2008-04-06 06:57:35 +00005024 // Parse the declarator. This is "PrototypeContext", because we must
5025 // accept either 'declarator' or 'abstract-declarator' here.
5026 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5027 ParseDeclarator(ParmDecl);
5028
5029 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00005030 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00005031
Chris Lattnerf97409f2008-04-06 06:57:35 +00005032 // Remember this parsed parameter in ParamInfo.
5033 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00005034
Douglas Gregor72b505b2008-12-16 21:30:33 +00005035 // DefArgToks is used when the parsing of default arguments needs
5036 // to be delayed.
5037 CachedTokens *DefArgToks = 0;
5038
Chris Lattnerf97409f2008-04-06 06:57:35 +00005039 // If no parameter was specified, verify that *something* was specified,
5040 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00005041 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5042 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005043 // Completely missing, emit error.
5044 Diag(DSStart, diag::err_missing_param);
5045 } else {
5046 // Otherwise, we have something. Add it and let semantic analysis try
5047 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00005048
Chris Lattnerf97409f2008-04-06 06:57:35 +00005049 // Inform the actions module about the parameter declarator, so it gets
5050 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00005051 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00005052
5053 // Parse the default argument, if any. We parse the default
5054 // arguments in all dialects; the semantic analysis in
5055 // ActOnParamDefaultArgument will reject the default argument in
5056 // C.
5057 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00005058 SourceLocation EqualLoc = Tok.getLocation();
5059
Chris Lattner04421082008-04-08 04:40:51 +00005060 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00005061 if (D.getContext() == Declarator::MemberContext) {
5062 // If we're inside a class definition, cache the tokens
5063 // corresponding to the default argument. We'll actually parse
5064 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00005065 // FIXME: Can we use a smart pointer for Toks?
5066 DefArgToks = new CachedTokens;
5067
Mike Stump1eb44332009-09-09 15:08:12 +00005068 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00005069 /*StopAtSemi=*/true,
5070 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005071 delete DefArgToks;
5072 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00005073 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005074 } else {
5075 // Mark the end of the default argument so that we know when to
5076 // stop when we parse it later on.
5077 Token DefArgEnd;
5078 DefArgEnd.startToken();
5079 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5080 DefArgEnd.setLocation(Tok.getLocation());
5081 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00005082 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00005083 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005084 }
Chris Lattner04421082008-04-08 04:40:51 +00005085 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005086 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00005087 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005088
Chad Rosier8decdee2012-06-26 22:30:43 +00005089 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005090 // used.
5091 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005092 Sema::PotentiallyEvaluatedIfUsed,
5093 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005094
Sebastian Redl84407ba2012-03-14 15:54:00 +00005095 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +00005096 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl3e280b52012-03-18 22:25:45 +00005097 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00005098 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00005099 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00005100 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00005101 if (DefArgResult.isInvalid()) {
5102 Actions.ActOnParamDefaultArgumentError(Param);
5103 SkipUntil(tok::comma, tok::r_paren, true, true);
5104 } else {
5105 // Inform the actions module about the default argument
5106 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005107 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005108 }
Chris Lattner04421082008-04-08 04:40:51 +00005109 }
5110 }
Mike Stump1eb44332009-09-09 15:08:12 +00005111
5112 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5113 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00005114 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005115 }
5116
5117 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00005118 if (Tok.isNot(tok::comma)) {
5119 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005120 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00005121
David Blaikie4e4d0842012-03-11 07:00:24 +00005122 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005123 // We have ellipsis without a preceding ',', which is ill-formed
5124 // in C. Complain and provide the fix.
5125 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00005126 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005127 }
5128 }
Chad Rosier8decdee2012-06-26 22:30:43 +00005129
Douglas Gregored5d6512009-09-22 21:41:40 +00005130 break;
5131 }
Mike Stump1eb44332009-09-09 15:08:12 +00005132
Chris Lattnerf97409f2008-04-06 06:57:35 +00005133 // Consume the comma.
5134 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005135 }
Mike Stump1eb44332009-09-09 15:08:12 +00005136
Chris Lattner66d28652008-04-06 06:34:08 +00005137}
Chris Lattneref4715c2008-04-06 05:45:57 +00005138
Reid Spencer5f016e22007-07-11 17:01:13 +00005139/// [C90] direct-declarator '[' constant-expression[opt] ']'
5140/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5141/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5142/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5143/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005144/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5145/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005146void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005147 if (CheckProhibitedCXX11Attribute())
5148 return;
5149
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005150 BalancedDelimiterTracker T(*this, tok::l_square);
5151 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005152
Chris Lattner378c7e42008-12-18 07:27:21 +00005153 // C array syntax has many features, but by-far the most common is [] and [4].
5154 // This code does a fast path to handle some of the most obvious cases.
5155 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005156 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005157 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005158 MaybeParseCXX11Attributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005159
Chris Lattner378c7e42008-12-18 07:27:21 +00005160 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005161 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005162 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005163 T.getOpenLocation(),
5164 T.getCloseLocation()),
5165 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005166 return;
5167 } else if (Tok.getKind() == tok::numeric_constant &&
5168 GetLookAheadToken(1).is(tok::r_square)) {
5169 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005170 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005171 ConsumeToken();
5172
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005173 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005174 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005175 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005176
Chris Lattner378c7e42008-12-18 07:27:21 +00005177 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicebf0fa82013-01-11 08:33:05 +00005178 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall7f040a92010-12-24 02:08:15 +00005179 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005180 T.getOpenLocation(),
5181 T.getCloseLocation()),
5182 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005183 return;
5184 }
Mike Stump1eb44332009-09-09 15:08:12 +00005185
Reid Spencer5f016e22007-07-11 17:01:13 +00005186 // If valid, this location is the position where we read the 'static' keyword.
5187 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005188 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005189 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005190
Reid Spencer5f016e22007-07-11 17:01:13 +00005191 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005192 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005193 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005194 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005195
Reid Spencer5f016e22007-07-11 17:01:13 +00005196 // If we haven't already read 'static', check to see if there is one after the
5197 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005198 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005199 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005200
Reid Spencer5f016e22007-07-11 17:01:13 +00005201 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5202 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005203 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005204
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005205 // Handle the case where we have '[*]' as the array size. However, a leading
5206 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005207 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005208 // infrequent, use of lookahead is not costly here.
5209 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005210 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005211
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005212 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005213 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005214 StaticLoc = SourceLocation(); // Drop the static.
5215 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005216 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005217 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005218 // Note, in C89, this production uses the constant-expr production instead
5219 // of assignment-expr. The only difference is that assignment-expr allows
5220 // things like '=' and '*='. Sema rejects these in C89 mode because they
5221 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005222
Douglas Gregore0762c92009-06-19 23:52:42 +00005223 // Parse the constant-expression or assignment-expression now (depending
5224 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005225 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005226 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005227 } else {
5228 EnterExpressionEvaluationContext Unevaluated(Actions,
5229 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005230 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005231 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005232 }
Mike Stump1eb44332009-09-09 15:08:12 +00005233
Reid Spencer5f016e22007-07-11 17:01:13 +00005234 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005235 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005236 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005237 // If the expression was invalid, skip it.
5238 SkipUntil(tok::r_square);
5239 return;
5240 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005241
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005242 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005243
John McCall0b7e6782011-03-24 11:26:52 +00005244 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005245 MaybeParseCXX11Attributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005246
Chris Lattner378c7e42008-12-18 07:27:21 +00005247 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005248 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005249 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005250 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005251 T.getOpenLocation(),
5252 T.getCloseLocation()),
5253 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005254}
5255
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005256/// [GNU] typeof-specifier:
5257/// typeof ( expressions )
5258/// typeof ( type-name )
5259/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005260///
5261void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005262 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005263 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005264 SourceLocation StartLoc = ConsumeToken();
5265
John McCallcfb708c2010-01-13 20:03:27 +00005266 const bool hasParens = Tok.is(tok::l_paren);
5267
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005268 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5269 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005270
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005271 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005272 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005273 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005274 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5275 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005276 if (hasParens)
5277 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005278
5279 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005280 // FIXME: Not accurate, the range gets one token more than it should.
5281 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005282 else
5283 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005284
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005285 if (isCastExpr) {
5286 if (!CastTy) {
5287 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005288 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005289 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005290
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005291 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005292 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005293 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5294 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005295 DiagID, CastTy))
5296 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005297 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005298 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005299
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005300 // If we get here, the operand to the typeof was an expresion.
5301 if (Operand.isInvalid()) {
5302 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005303 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005304 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005305
Eli Friedman71b8fb52012-01-21 01:01:51 +00005306 // We might need to transform the operand if it is potentially evaluated.
5307 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5308 if (Operand.isInvalid()) {
5309 DS.SetTypeSpecError();
5310 return;
5311 }
5312
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005313 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005314 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005315 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5316 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005317 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005318 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005319}
Chris Lattner1b492422010-02-28 18:33:55 +00005320
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005321/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005322/// _Atomic ( type-name )
5323///
5324void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5325 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5326
5327 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005328 BalancedDelimiterTracker T(*this, tok::l_paren);
5329 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00005330 SkipUntil(tok::r_paren);
5331 return;
5332 }
5333
5334 TypeResult Result = ParseTypeName();
5335 if (Result.isInvalid()) {
5336 SkipUntil(tok::r_paren);
5337 return;
5338 }
5339
5340 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005341 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005342
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005343 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005344 return;
5345
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005346 DS.setTypeofParensRange(T.getRange());
5347 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005348
5349 const char *PrevSpec = 0;
5350 unsigned DiagID;
5351 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5352 DiagID, Result.release()))
5353 Diag(StartLoc, DiagID) << PrevSpec;
5354}
5355
Chris Lattner1b492422010-02-28 18:33:55 +00005356
5357/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5358/// from TryAltiVecVectorToken.
5359bool Parser::TryAltiVecVectorTokenOutOfLine() {
5360 Token Next = NextToken();
5361 switch (Next.getKind()) {
5362 default: return false;
5363 case tok::kw_short:
5364 case tok::kw_long:
5365 case tok::kw_signed:
5366 case tok::kw_unsigned:
5367 case tok::kw_void:
5368 case tok::kw_char:
5369 case tok::kw_int:
5370 case tok::kw_float:
5371 case tok::kw_double:
5372 case tok::kw_bool:
5373 case tok::kw___pixel:
5374 Tok.setKind(tok::kw___vector);
5375 return true;
5376 case tok::identifier:
5377 if (Next.getIdentifierInfo() == Ident_pixel) {
5378 Tok.setKind(tok::kw___vector);
5379 return true;
5380 }
5381 return false;
5382 }
5383}
5384
5385bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5386 const char *&PrevSpec, unsigned &DiagID,
5387 bool &isInvalid) {
5388 if (Tok.getIdentifierInfo() == Ident_vector) {
5389 Token Next = NextToken();
5390 switch (Next.getKind()) {
5391 case tok::kw_short:
5392 case tok::kw_long:
5393 case tok::kw_signed:
5394 case tok::kw_unsigned:
5395 case tok::kw_void:
5396 case tok::kw_char:
5397 case tok::kw_int:
5398 case tok::kw_float:
5399 case tok::kw_double:
5400 case tok::kw_bool:
5401 case tok::kw___pixel:
5402 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5403 return true;
5404 case tok::identifier:
5405 if (Next.getIdentifierInfo() == Ident_pixel) {
5406 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5407 return true;
5408 }
5409 break;
5410 default:
5411 break;
5412 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005413 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005414 DS.isTypeAltiVecVector()) {
5415 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5416 return true;
5417 }
5418 return false;
5419}