blob: c5e41445d8be057f4069e63e4481b744f4a65a02 [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,
41 Decl **OwnedType) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000042 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smitha971d242012-05-09 20:55:26 +000043 if (DSC == DSC_normal)
44 DSC = DSC_type_specifier;
Richard Smith7796eb52012-03-12 08:56:40 +000045
Reid Spencer5f016e22007-07-11 17:01:13 +000046 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000047 DeclSpec DS(AttrFactory);
Richard Smith7796eb52012-03-12 08:56:40 +000048 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000049 if (OwnedType)
50 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000051
Reid Spencer5f016e22007-07-11 17:01:13 +000052 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000053 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000054 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000055 if (Range)
56 *Range = DeclaratorInfo.getSourceRange();
57
Chris Lattnereaaebc72009-04-25 08:06:05 +000058 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000059 return true;
60
Douglas Gregor23c94db2010-07-02 17:43:08 +000061 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000062}
63
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000064
65/// isAttributeLateParsed - Return true if the attribute has arguments that
66/// require late parsing.
67static bool isAttributeLateParsed(const IdentifierInfo &II) {
68 return llvm::StringSwitch<bool>(II.getName())
69#include "clang/Parse/AttrLateParsed.inc"
70 .Default(false);
71}
72
Sean Huntbbd37c62009-11-21 08:43:09 +000073/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000074///
75/// [GNU] attributes:
76/// attribute
77/// attributes attribute
78///
79/// [GNU] attribute:
80/// '__attribute__' '(' '(' attribute-list ')' ')'
81///
82/// [GNU] attribute-list:
83/// attrib
84/// attribute_list ',' attrib
85///
86/// [GNU] attrib:
87/// empty
88/// attrib-name
89/// attrib-name '(' identifier ')'
90/// attrib-name '(' identifier ',' nonempty-expr-list ')'
91/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
92///
93/// [GNU] attrib-name:
94/// identifier
95/// typespec
96/// typequal
97/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000098///
Reid Spencer5f016e22007-07-11 17:01:13 +000099/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +0000100/// token lookahead. Comment from gcc: "If they start with an identifier
101/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +0000102/// start with that identifier; otherwise they are an expression list."
103///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000104/// GCC does not require the ',' between attribs in an attribute-list.
105///
Reid Spencer5f016e22007-07-11 17:01:13 +0000106/// At the moment, I am not doing 2 token lookahead. I am also unaware of
107/// any attributes that don't work (based on my limited testing). Most
108/// attributes are very simple in practice. Until we find a bug, I don't see
109/// a pressing need to implement the 2 token lookahead.
110
John McCall7f040a92010-12-24 02:08:15 +0000111void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000112 SourceLocation *endLoc,
113 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000114 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Chris Lattner04d66662007-10-09 17:33:22 +0000116 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000117 ConsumeToken();
118 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
119 "attribute")) {
120 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000121 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000122 }
123 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
124 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000125 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000126 }
127 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000128 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
129 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000130 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
132 ConsumeToken();
133 continue;
134 }
135 // we have an identifier or declaration specifier (const, int, etc.)
136 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
137 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000139 if (Tok.is(tok::l_paren)) {
140 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000141 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000142 LateParsedAttribute *LA =
143 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
144 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000145
Bill Wendlingad017fa2012-12-20 19:22:21 +0000146 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000147 // with other late-parsed declarations.
DeLesley Hutchins161db022012-11-02 21:44:32 +0000148 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000149 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000151 // consume everything up to and including the matching right parens
152 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000154 Token Eof;
155 Eof.startToken();
156 Eof.setLocation(Tok.getLocation());
157 LA->Toks.push_back(Eof);
158 } else {
Michael Han6880f492012-10-03 01:56:22 +0000159 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000160 0, SourceLocation(), AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000161 }
162 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000163 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000164 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000165 }
166 }
167 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000169 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000170 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
171 SkipUntil(tok::r_paren, false);
172 }
John McCall7f040a92010-12-24 02:08:15 +0000173 if (endLoc)
174 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000176}
177
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000178
Michael Han6880f492012-10-03 01:56:22 +0000179/// Parse the arguments to a parameterized GNU attribute or
180/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000181void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
182 SourceLocation AttrNameLoc,
183 ParsedAttributes &Attrs,
Michael Han6880f492012-10-03 01:56:22 +0000184 SourceLocation *EndLoc,
185 IdentifierInfo *ScopeName,
186 SourceLocation ScopeLoc,
187 AttributeList::Syntax Syntax) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000188
189 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
190
191 // Availability attributes have their own grammar.
192 if (AttrName->isStr("availability")) {
193 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
194 return;
195 }
196 // Thread safety attributes fit into the FIXME case above, so we
197 // just parse the arguments as a list of expressions
198 if (IsThreadSafetyAttribute(AttrName->getName())) {
199 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
200 return;
201 }
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000202 // Type safety attributes have their own grammar.
203 if (AttrName->isStr("type_tag_for_datatype")) {
204 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
205 return;
206 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000207
208 ConsumeParen(); // ignore the left paren loc for now
209
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000210 IdentifierInfo *ParmName = 0;
211 SourceLocation ParmLoc;
212 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000213
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000214 switch (Tok.getKind()) {
215 case tok::kw_char:
216 case tok::kw_wchar_t:
217 case tok::kw_char16_t:
218 case tok::kw_char32_t:
219 case tok::kw_bool:
220 case tok::kw_short:
221 case tok::kw_int:
222 case tok::kw_long:
223 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000224 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000225 case tok::kw_signed:
226 case tok::kw_unsigned:
227 case tok::kw_float:
228 case tok::kw_double:
229 case tok::kw_void:
230 case tok::kw_typeof:
231 // __attribute__(( vec_type_hint(char) ))
232 // FIXME: Don't just discard the builtin type token.
233 ConsumeToken();
234 BuiltinType = true;
235 break;
236
237 case tok::identifier:
238 ParmName = Tok.getIdentifierInfo();
239 ParmLoc = ConsumeToken();
240 break;
241
242 default:
243 break;
244 }
245
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +0000246 ExprVector ArgExprs;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000247
248 if (!BuiltinType &&
249 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
250 // Eat the comma.
251 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000252 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000253
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000254 // Parse the non-empty comma-separated list of expressions.
255 while (1) {
256 ExprResult ArgExpr(ParseAssignmentExpression());
257 if (ArgExpr.isInvalid()) {
258 SkipUntil(tok::r_paren);
259 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000260 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000261 ArgExprs.push_back(ArgExpr.release());
262 if (Tok.isNot(tok::comma))
263 break;
264 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000265 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000266 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000267 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
268 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
269 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000270 while (Tok.is(tok::identifier)) {
271 ConsumeToken();
272 if (Tok.is(tok::greater))
273 break;
274 if (Tok.is(tok::comma)) {
275 ConsumeToken();
276 continue;
277 }
278 }
279 if (Tok.isNot(tok::greater))
280 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000281 SkipUntil(tok::r_paren, false, true); // skip until ')'
282 }
283 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000284
285 SourceLocation RParen = Tok.getLocation();
286 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
Michael Han45bed132012-10-04 16:42:52 +0000287 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000288 AttributeList *attr =
Michael Han45bed132012-10-04 16:42:52 +0000289 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen),
Michael Han6880f492012-10-03 01:56:22 +0000290 ScopeName, ScopeLoc, ParmName, ParmLoc,
291 ArgExprs.data(), ArgExprs.size(), Syntax);
Sean Hunt8e083e72012-06-19 23:57:03 +0000292 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000293 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000294 }
295}
296
Chad Rosier8decdee2012-06-26 22:30:43 +0000297/// \brief Parses a single argument for a declspec, including the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000298/// surrounding parens.
Chad Rosier8decdee2012-06-26 22:30:43 +0000299void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000300 SourceLocation AttrNameLoc,
301 ParsedAttributes &Attrs)
302{
303 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000304 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000305 AttrName->getNameStart(), tok::r_paren))
306 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000307
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000308 ExprResult ArgExpr(ParseConstantExpression());
309 if (ArgExpr.isInvalid()) {
310 T.skipToEnd();
311 return;
312 }
313 Expr *ExprList = ArgExpr.take();
Chad Rosier8decdee2012-06-26 22:30:43 +0000314 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000315 &ExprList, 1, AttributeList::AS_Declspec);
316
317 T.consumeClose();
318}
319
Chad Rosier8decdee2012-06-26 22:30:43 +0000320/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000321/// arguments.
322bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
323 return llvm::StringSwitch<bool>(Ident->getName())
324 .Case("dllimport", true)
325 .Case("dllexport", true)
326 .Case("noreturn", true)
327 .Case("nothrow", true)
328 .Case("noinline", true)
329 .Case("naked", true)
330 .Case("appdomain", true)
331 .Case("process", true)
332 .Case("jitintrinsic", true)
333 .Case("noalias", true)
334 .Case("restrict", true)
335 .Case("novtable", true)
336 .Case("selectany", true)
337 .Case("thread", true)
338 .Default(false);
339}
340
Chad Rosier8decdee2012-06-26 22:30:43 +0000341/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000342/// parameters). Will return false if we properly handled the declspec, or
343/// true if it is an unknown declspec.
Chad Rosier8decdee2012-06-26 22:30:43 +0000344void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000345 SourceLocation Loc,
346 ParsedAttributes &Attrs) {
347 // Try to handle the easy case first -- these declspecs all take a single
348 // parameter as their argument.
349 if (llvm::StringSwitch<bool>(Ident->getName())
350 .Case("uuid", true)
351 .Case("align", true)
352 .Case("allocate", true)
353 .Default(false)) {
354 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
355 } else if (Ident->getName() == "deprecated") {
Chad Rosier8decdee2012-06-26 22:30:43 +0000356 // The deprecated declspec has an optional single argument, so we will
357 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000358 // not.
359 if (Tok.getKind() == tok::l_paren)
360 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
361 else
Chad Rosier8decdee2012-06-26 22:30:43 +0000362 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000363 AttributeList::AS_Declspec);
364 } else if (Ident->getName() == "property") {
365 // The property declspec is more complex in that it can take one or two
Chad Rosier8decdee2012-06-26 22:30:43 +0000366 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000367 // must be named get or put.
368 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000369 // For right now, we will just skip to the closing right paren of the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000370 // property expression.
371 //
372 // FIXME: we should deal with __declspec(property) at some point because it
373 // is used in the platform SDK headers for the Parallel Patterns Library
374 // and ATL.
375 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000376 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000377 Ident->getNameStart(), tok::r_paren))
378 return;
379 T.skipToEnd();
380 } else {
381 // We don't recognize this as a valid declspec, but instead of creating the
382 // attribute and allowing sema to warn about it, we will warn here instead.
383 // This is because some attributes have multiple spellings, but we need to
384 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosier8decdee2012-06-26 22:30:43 +0000385 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000386 // both locations.
387 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
388
389 // If there's an open paren, we should eat the open and close parens under
390 // the assumption that this unknown declspec has parameters.
391 BalancedDelimiterTracker T(*this, tok::l_paren);
392 if (!T.consumeOpen())
393 T.skipToEnd();
394 }
395}
396
Eli Friedmana23b4852009-06-08 07:21:15 +0000397/// [MS] decl-specifier:
398/// __declspec ( extended-decl-modifier-seq )
399///
400/// [MS] extended-decl-modifier-seq:
401/// extended-decl-modifier[opt]
402/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000403void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000404 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000405
Steve Narofff59e17e2008-12-24 20:59:21 +0000406 ConsumeToken();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000407 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000408 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000409 tok::r_paren))
John McCall7f040a92010-12-24 02:08:15 +0000410 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000411
Chad Rosier8decdee2012-06-26 22:30:43 +0000412 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000413 // you can specify multiple attributes per declspec.
414 while (Tok.getKind() != tok::r_paren) {
415 // We expect either a well-known identifier or a generic string. Anything
416 // else is a malformed declspec.
417 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosier8decdee2012-06-26 22:30:43 +0000418 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000419 Tok.getKind() != tok::kw_restrict) {
420 Diag(Tok, diag::err_ms_declspec_type);
421 T.skipToEnd();
422 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000423 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000424
425 IdentifierInfo *AttrName;
426 SourceLocation AttrNameLoc;
427 if (IsString) {
428 SmallString<8> StrBuffer;
429 bool Invalid = false;
430 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
431 if (Invalid) {
432 T.skipToEnd();
433 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000434 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000435 AttrName = PP.getIdentifierInfo(Str);
436 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000437 } else {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000438 AttrName = Tok.getIdentifierInfo();
439 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000440 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000441
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000442 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosier8decdee2012-06-26 22:30:43 +0000443 // If we have a generic string, we will allow it because there is no
444 // documented list of allowable string declspecs, but we know they exist
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000445 // (for instance, SAL declspecs in older versions of MSVC).
446 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000447 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000448 // arguments and can be turned into an attribute directly.
Chad Rosier8decdee2012-06-26 22:30:43 +0000449 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000450 0, 0, AttributeList::AS_Declspec);
451 else
452 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000453 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000454 T.consumeClose();
Eli Friedman290eeb02009-06-08 23:27:34 +0000455}
456
John McCall7f040a92010-12-24 02:08:15 +0000457void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000458 // Treat these like attributes
Eli Friedman290eeb02009-06-08 23:27:34 +0000459 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000460 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000461 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Chad Rosierccbb4022012-12-21 21:27:13 +0000462 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000463 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
464 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000465 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith5cd532c2013-01-29 01:24:26 +0000466 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Eli Friedman290eeb02009-06-08 23:27:34 +0000467 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000468}
469
John McCall7f040a92010-12-24 02:08:15 +0000470void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000471 // Treat these like attributes
472 while (Tok.is(tok::kw___pascal)) {
473 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
474 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000475 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith5cd532c2013-01-29 01:24:26 +0000476 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000477 }
John McCall7f040a92010-12-24 02:08:15 +0000478}
479
Peter Collingbournef315fa82011-02-14 01:42:53 +0000480void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
481 // Treat these like attributes
482 while (Tok.is(tok::kw___kernel)) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000483 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbournef315fa82011-02-14 01:42:53 +0000484 SourceLocation AttrNameLoc = ConsumeToken();
Richard Smith5cd532c2013-01-29 01:24:26 +0000485 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
486 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000487 }
488}
489
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000490void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000491 // FIXME: The mapping from attribute spelling to semantics should be
492 // performed in Sema, not here.
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000493 SourceLocation Loc = Tok.getLocation();
494 switch(Tok.getKind()) {
495 // OpenCL qualifiers:
496 case tok::kw___private:
Chad Rosier8decdee2012-06-26 22:30:43 +0000497 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000498 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000499 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000500 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000501 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000502
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000503 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000504 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000505 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000506 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000507 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000508
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000509 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000510 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000511 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000512 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000513 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000514
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000515 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000516 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000517 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000518 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000519 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000520
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000521 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000522 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000523 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000524 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000525 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000526
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000527 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000528 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000529 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000530 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000531 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000532
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000533 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000534 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000535 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000536 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000537 break;
538 default: break;
539 }
540}
541
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000542/// \brief Parse a version number.
543///
544/// version:
545/// simple-integer
546/// simple-integer ',' simple-integer
547/// simple-integer ',' simple-integer ',' simple-integer
548VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
549 Range = Tok.getLocation();
550
551 if (!Tok.is(tok::numeric_constant)) {
552 Diag(Tok, diag::err_expected_version);
553 SkipUntil(tok::comma, tok::r_paren, true, true, true);
554 return VersionTuple();
555 }
556
557 // Parse the major (and possibly minor and subminor) versions, which
558 // are stored in the numeric constant. We utilize a quirk of the
559 // lexer, which is that it handles something like 1.2.3 as a single
560 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000561 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000562 Buffer.resize(Tok.getLength()+1);
563 const char *ThisTokBegin = &Buffer[0];
564
565 // Get the spelling of the token, which eliminates trigraphs, etc.
566 bool Invalid = false;
567 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
568 if (Invalid)
569 return VersionTuple();
570
571 // Parse the major version.
572 unsigned AfterMajor = 0;
573 unsigned Major = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000574 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000575 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
576 ++AfterMajor;
577 }
578
579 if (AfterMajor == 0) {
580 Diag(Tok, diag::err_expected_version);
581 SkipUntil(tok::comma, tok::r_paren, true, true, true);
582 return VersionTuple();
583 }
584
585 if (AfterMajor == ActualLength) {
586 ConsumeToken();
587
588 // We only had a single version component.
589 if (Major == 0) {
590 Diag(Tok, diag::err_zero_version);
591 return VersionTuple();
592 }
593
594 return VersionTuple(Major);
595 }
596
597 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
598 Diag(Tok, diag::err_expected_version);
599 SkipUntil(tok::comma, tok::r_paren, true, true, true);
600 return VersionTuple();
601 }
602
603 // Parse the minor version.
604 unsigned AfterMinor = AfterMajor + 1;
605 unsigned Minor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000606 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000607 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
608 ++AfterMinor;
609 }
610
611 if (AfterMinor == ActualLength) {
612 ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +0000613
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000614 // We had major.minor.
615 if (Major == 0 && Minor == 0) {
616 Diag(Tok, diag::err_zero_version);
617 return VersionTuple();
618 }
619
Chad Rosier8decdee2012-06-26 22:30:43 +0000620 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000621 }
622
623 // If what follows is not a '.', we have a problem.
624 if (ThisTokBegin[AfterMinor] != '.') {
625 Diag(Tok, diag::err_expected_version);
626 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosier8decdee2012-06-26 22:30:43 +0000627 return VersionTuple();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000628 }
629
630 // Parse the subminor version.
631 unsigned AfterSubminor = AfterMinor + 1;
632 unsigned Subminor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000633 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000634 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
635 ++AfterSubminor;
636 }
637
638 if (AfterSubminor != ActualLength) {
639 Diag(Tok, diag::err_expected_version);
640 SkipUntil(tok::comma, tok::r_paren, true, true, true);
641 return VersionTuple();
642 }
643 ConsumeToken();
644 return VersionTuple(Major, Minor, Subminor);
645}
646
647/// \brief Parse the contents of the "availability" attribute.
648///
649/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000650/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000651///
652/// platform:
653/// identifier
654///
655/// version-arg-list:
656/// version-arg
657/// version-arg ',' version-arg-list
658///
659/// version-arg:
660/// 'introduced' '=' version
661/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000662/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000663/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000664/// opt-message:
665/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000666void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
667 SourceLocation AvailabilityLoc,
668 ParsedAttributes &attrs,
669 SourceLocation *endLoc) {
670 SourceLocation PlatformLoc;
671 IdentifierInfo *Platform = 0;
672
673 enum { Introduced, Deprecated, Obsoleted, Unknown };
674 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000675 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000676
677 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000678 BalancedDelimiterTracker T(*this, tok::l_paren);
679 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000680 Diag(Tok, diag::err_expected_lparen);
681 return;
682 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000683
684 // Parse the platform name,
685 if (Tok.isNot(tok::identifier)) {
686 Diag(Tok, diag::err_availability_expected_platform);
687 SkipUntil(tok::r_paren);
688 return;
689 }
690 Platform = Tok.getIdentifierInfo();
691 PlatformLoc = ConsumeToken();
692
693 // Parse the ',' following the platform name.
694 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
695 return;
696
697 // If we haven't grabbed the pointers for the identifiers
698 // "introduced", "deprecated", and "obsoleted", do so now.
699 if (!Ident_introduced) {
700 Ident_introduced = PP.getIdentifierInfo("introduced");
701 Ident_deprecated = PP.getIdentifierInfo("deprecated");
702 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000703 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000704 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000705 }
706
707 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000708 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000709 do {
710 if (Tok.isNot(tok::identifier)) {
711 Diag(Tok, diag::err_availability_expected_change);
712 SkipUntil(tok::r_paren);
713 return;
714 }
715 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
716 SourceLocation KeywordLoc = ConsumeToken();
717
Douglas Gregorb53e4172011-03-26 03:35:55 +0000718 if (Keyword == Ident_unavailable) {
719 if (UnavailableLoc.isValid()) {
720 Diag(KeywordLoc, diag::err_availability_redundant)
721 << Keyword << SourceRange(UnavailableLoc);
Chad Rosier8decdee2012-06-26 22:30:43 +0000722 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000723 UnavailableLoc = KeywordLoc;
724
725 if (Tok.isNot(tok::comma))
726 break;
727
728 ConsumeToken();
729 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000730 }
731
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000732 if (Tok.isNot(tok::equal)) {
733 Diag(Tok, diag::err_expected_equal_after)
734 << Keyword;
735 SkipUntil(tok::r_paren);
736 return;
737 }
738 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000739 if (Keyword == Ident_message) {
740 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000741 Diag(Tok, diag::err_expected_string_literal)
742 << /*Source='availability attribute'*/2;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000743 SkipUntil(tok::r_paren);
744 return;
745 }
746 MessageExpr = ParseStringLiteralExpression();
747 break;
748 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000749
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000750 SourceRange VersionRange;
751 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000752
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000753 if (Version.empty()) {
754 SkipUntil(tok::r_paren);
755 return;
756 }
757
758 unsigned Index;
759 if (Keyword == Ident_introduced)
760 Index = Introduced;
761 else if (Keyword == Ident_deprecated)
762 Index = Deprecated;
763 else if (Keyword == Ident_obsoleted)
764 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000765 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000766 Index = Unknown;
767
768 if (Index < Unknown) {
769 if (!Changes[Index].KeywordLoc.isInvalid()) {
770 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000771 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000772 << SourceRange(Changes[Index].KeywordLoc,
773 Changes[Index].VersionRange.getEnd());
774 }
775
776 Changes[Index].KeywordLoc = KeywordLoc;
777 Changes[Index].Version = Version;
778 Changes[Index].VersionRange = VersionRange;
779 } else {
780 Diag(KeywordLoc, diag::err_availability_unknown_change)
781 << Keyword << VersionRange;
782 }
783
784 if (Tok.isNot(tok::comma))
785 break;
786
787 ConsumeToken();
788 } while (true);
789
790 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000791 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000792 return;
793
794 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000795 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000796
Douglas Gregorb53e4172011-03-26 03:35:55 +0000797 // The 'unavailable' availability cannot be combined with any other
798 // availability changes. Make sure that hasn't happened.
799 if (UnavailableLoc.isValid()) {
800 bool Complained = false;
801 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
802 if (Changes[Index].KeywordLoc.isValid()) {
803 if (!Complained) {
804 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
805 << SourceRange(Changes[Index].KeywordLoc,
806 Changes[Index].VersionRange.getEnd());
807 Complained = true;
808 }
809
810 // Clear out the availability.
811 Changes[Index] = AvailabilityChange();
812 }
813 }
814 }
815
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000816 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000817 attrs.addNew(&Availability,
818 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000819 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000820 Platform, PlatformLoc,
821 Changes[Introduced],
822 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000823 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000824 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000825 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000826}
827
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000828
Bill Wendlingad017fa2012-12-20 19:22:21 +0000829// Late Parsed Attributes:
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000830// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
831
832void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
833
834void Parser::LateParsedClass::ParseLexedAttributes() {
835 Self->ParseLexedAttributes(*Class);
836}
837
838void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000839 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000840}
841
842/// Wrapper class which calls ParseLexedAttribute, after setting up the
843/// scope appropriately.
844void Parser::ParseLexedAttributes(ParsingClass &Class) {
845 // Deal with templates
846 // FIXME: Test cases to make sure this does the right thing for templates.
847 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
848 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
849 HasTemplateScope);
850 if (HasTemplateScope)
851 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
852
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000853 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000854 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000855 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000856 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
857 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
858
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000859 // Enter the scope of nested classes
860 if (!AlreadyHasClassScope)
861 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
862 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000863 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000864 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
865 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
866 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000867 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000868
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000869 if (!AlreadyHasClassScope)
870 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
871 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000872}
873
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000874
875/// \brief Parse all attributes in LAs, and attach them to Decl D.
876void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
877 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins161db022012-11-02 21:44:32 +0000878 assert(LAs.parseSoon() &&
879 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000880 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +0000881 if (D)
882 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000883 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000884 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000885 }
886 LAs.clear();
887}
888
889
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000890/// \brief Finish parsing an attribute for which parsing was delayed.
891/// This will be called at the end of parsing a class declaration
892/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +0000893/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000894/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000895void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
896 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000897 // Save the current token position.
898 SourceLocation OrigLoc = Tok.getLocation();
899
900 // Append the current token at the end of the new token stream so that it
901 // doesn't get lost.
902 LA.Toks.push_back(Tok);
903 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
904 // Consume the previously pushed token.
905 ConsumeAnyToken();
906
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000907 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smithcd8ab512013-01-17 01:30:42 +0000908 // FIXME: Do not warn on C++11 attributes, once we start supporting
909 // them here.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000910 Diag(Tok, diag::warn_attribute_on_function_definition)
911 << LA.AttrName.getName();
912 }
913
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000914 ParsedAttributes Attrs(AttrFactory);
915 SourceLocation endLoc;
916
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000917 if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000918 Decl *D = LA.Decls[0];
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000919 NamedDecl *ND = dyn_cast<NamedDecl>(D);
920 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000921
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000922 // Allow 'this' within late-parsed attributes.
923 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
924 /*TypeQuals=*/0,
925 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000926
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000927 if (LA.Decls.size() == 1) {
928 // If the Decl is templatized, add template parameters to scope.
929 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
930 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
931 if (HasTemplateScope)
932 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000933
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000934 // If the Decl is on a function, add function parameters to the scope.
935 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
936 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
937 if (HasFunScope)
938 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000939
Michael Han6880f492012-10-03 01:56:22 +0000940 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000941 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000942
943 if (HasFunScope) {
944 Actions.ActOnExitFunctionContext();
945 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
946 }
947 if (HasTemplateScope) {
948 TempScope.Exit();
949 }
950 } else {
951 // If there are multiple decls, then the decl cannot be within the
952 // function scope.
Michael Han6880f492012-10-03 01:56:22 +0000953 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000954 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000955 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000956 } else {
957 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000958 }
959
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000960 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
961 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
962 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000963
964 if (Tok.getLocation() != OrigLoc) {
965 // Due to a parsing error, we either went over the cached tokens or
966 // there are still cached tokens left, so we skip the leftover tokens.
967 // Since this is an uncommon situation that should be avoided, use the
968 // expensive isBeforeInTranslationUnit call.
969 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
970 OrigLoc))
971 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000972 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000973 }
974}
975
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000976/// \brief Wrapper around a case statement checking if AttrName is
977/// one of the thread safety attributes
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000978bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000979 return llvm::StringSwitch<bool>(AttrName)
980 .Case("guarded_by", true)
981 .Case("guarded_var", true)
982 .Case("pt_guarded_by", true)
983 .Case("pt_guarded_var", true)
984 .Case("lockable", true)
985 .Case("scoped_lockable", true)
986 .Case("no_thread_safety_analysis", true)
987 .Case("acquired_after", true)
988 .Case("acquired_before", true)
989 .Case("exclusive_lock_function", true)
990 .Case("shared_lock_function", true)
991 .Case("exclusive_trylock_function", true)
992 .Case("shared_trylock_function", true)
993 .Case("unlock_function", true)
994 .Case("lock_returned", true)
995 .Case("locks_excluded", true)
996 .Case("exclusive_locks_required", true)
997 .Case("shared_locks_required", true)
998 .Default(false);
999}
1000
1001/// \brief Parse the contents of thread safety attributes. These
1002/// should always be parsed as an expression list.
1003///
1004/// We need to special case the parsing due to the fact that if the first token
1005/// of the first argument is an identifier, the main parse loop will store
1006/// that token as a "parameter" and the rest of
1007/// the arguments will be added to a list of "arguments". However,
1008/// subsequent tokens in the first argument are lost. We instead parse each
1009/// argument as an expression and add all arguments to the list of "arguments".
1010/// In future, we will take advantage of this special case to also
1011/// deal with some argument scoping issues here (for example, referring to a
1012/// function parameter in the attribute on that function).
1013void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1014 SourceLocation AttrNameLoc,
1015 ParsedAttributes &Attrs,
1016 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001017 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001018
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001019 BalancedDelimiterTracker T(*this, tok::l_paren);
1020 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +00001021
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001022 ExprVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001023 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00001024
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001025 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001026 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinsed4330b2013-02-07 19:01:07 +00001027 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001028 ExprResult ArgExpr(ParseAssignmentExpression());
1029 if (ArgExpr.isInvalid()) {
1030 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001031 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001032 break;
1033 } else {
1034 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001035 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001036 if (Tok.isNot(tok::comma))
1037 break;
1038 ConsumeToken(); // Eat the comma, move to the next argument
1039 }
1040 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001041 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001042 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001043 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001044 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001045 if (EndLoc)
1046 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001047}
1048
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001049void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1050 SourceLocation AttrNameLoc,
1051 ParsedAttributes &Attrs,
1052 SourceLocation *EndLoc) {
1053 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1054
1055 BalancedDelimiterTracker T(*this, tok::l_paren);
1056 T.consumeOpen();
1057
1058 if (Tok.isNot(tok::identifier)) {
1059 Diag(Tok, diag::err_expected_ident);
1060 T.skipToEnd();
1061 return;
1062 }
1063 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1064 SourceLocation ArgumentKindLoc = ConsumeToken();
1065
1066 if (Tok.isNot(tok::comma)) {
1067 Diag(Tok, diag::err_expected_comma);
1068 T.skipToEnd();
1069 return;
1070 }
1071 ConsumeToken();
1072
1073 SourceRange MatchingCTypeRange;
1074 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1075 if (MatchingCType.isInvalid()) {
1076 T.skipToEnd();
1077 return;
1078 }
1079
1080 bool LayoutCompatible = false;
1081 bool MustBeNull = false;
1082 while (Tok.is(tok::comma)) {
1083 ConsumeToken();
1084 if (Tok.isNot(tok::identifier)) {
1085 Diag(Tok, diag::err_expected_ident);
1086 T.skipToEnd();
1087 return;
1088 }
1089 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1090 if (Flag->isStr("layout_compatible"))
1091 LayoutCompatible = true;
1092 else if (Flag->isStr("must_be_null"))
1093 MustBeNull = true;
1094 else {
1095 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1096 T.skipToEnd();
1097 return;
1098 }
1099 ConsumeToken(); // consume flag
1100 }
1101
1102 if (!T.consumeClose()) {
1103 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1104 ArgumentKind, ArgumentKindLoc,
1105 MatchingCType.release(), LayoutCompatible,
1106 MustBeNull, AttributeList::AS_GNU);
1107 }
1108
1109 if (EndLoc)
1110 *EndLoc = T.getCloseLocation();
1111}
1112
Richard Smith6ee326a2012-04-10 01:32:12 +00001113/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1114/// of a C++11 attribute-specifier in a location where an attribute is not
1115/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1116/// situation.
1117///
1118/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1119/// this doesn't appear to actually be an attribute-specifier, and the caller
1120/// should try to parse it.
1121bool Parser::DiagnoseProhibitedCXX11Attribute() {
1122 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1123
1124 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1125 case CAK_NotAttributeSpecifier:
1126 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1127 return false;
1128
1129 case CAK_InvalidAttributeSpecifier:
1130 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1131 return false;
1132
1133 case CAK_AttributeSpecifier:
1134 // Parse and discard the attributes.
1135 SourceLocation BeginLoc = ConsumeBracket();
1136 ConsumeBracket();
1137 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1138 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1139 SourceLocation EndLoc = ConsumeBracket();
1140 Diag(BeginLoc, diag::err_attributes_not_allowed)
1141 << SourceRange(BeginLoc, EndLoc);
1142 return true;
1143 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001144 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001145}
1146
John McCall7f040a92010-12-24 02:08:15 +00001147void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1148 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1149 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001150}
1151
Michael Hanf64231e2012-11-06 19:34:54 +00001152void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1153 AttributeList *AttrList = attrs.getList();
1154 while (AttrList) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001155 if (AttrList->isCXX11Attribute()) {
Richard Smithd03de6a2013-01-29 10:02:16 +00001156 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Hanf64231e2012-11-06 19:34:54 +00001157 << AttrList->getName();
1158 AttrList->setInvalid();
1159 }
1160 AttrList = AttrList->getNext();
1161 }
1162}
1163
Reid Spencer5f016e22007-07-11 17:01:13 +00001164/// ParseDeclaration - Parse a full 'declaration', which consists of
1165/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001166/// 'Context' should be a Declarator::TheContext value. This returns the
1167/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001168///
1169/// declaration: [C99 6.7]
1170/// block-declaration ->
1171/// simple-declaration
1172/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001173/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001174/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001175/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001176/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001177/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001178/// others... [FIXME]
1179///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001180Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1181 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001182 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001183 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001184 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001185 // Must temporarily exit the objective-c container scope for
1186 // parsing c none objective-c decls.
1187 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001188
John McCalld226f652010-08-21 09:40:31 +00001189 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001190 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001191 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001192 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001193 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001194 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001195 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001196 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001197 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001198 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001199 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001200 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001201 SourceLocation InlineLoc = ConsumeToken();
1202 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1203 break;
1204 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001205 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001206 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001207 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001208 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001209 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001210 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001211 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001212 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001213 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001214 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001215 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001216 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001217 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001218 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001219 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001220 default:
John McCall7f040a92010-12-24 02:08:15 +00001221 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001222 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001223
Chris Lattner682bf922009-03-29 16:50:03 +00001224 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001225 // single decl, convert it now. Alias declarations can also declare a type;
1226 // include that too if it is present.
1227 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001228}
1229
1230/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1231/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001232/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1233/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001234///[C90/C++]init-declarator-list ';' [TODO]
1235/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001236///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001237/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001238/// attribute-specifier-seq[opt] type-specifier-seq declarator
1239///
Chris Lattnercd147752009-03-29 17:27:48 +00001240/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001241/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001242///
1243/// If FRI is non-null, we might be parsing a for-range-declaration instead
1244/// of a simple-declaration. If we find that we are, we also parse the
1245/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001246Parser::DeclGroupPtrTy
1247Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1248 SourceLocation &DeclEnd,
1249 ParsedAttributesWithRange &attrs,
1250 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001251 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001252 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001253 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001254
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001255 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001256 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001257
Reid Spencer5f016e22007-07-11 17:01:13 +00001258 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1259 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001260 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001261 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001262 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001263 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001264 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001265 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001266 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001267 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001268
1269 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001270}
Mike Stump1eb44332009-09-09 15:08:12 +00001271
Richard Smith0706df42011-10-19 21:33:05 +00001272/// Returns true if this might be the start of a declarator, or a common typo
1273/// for a declarator.
1274bool Parser::MightBeDeclarator(unsigned Context) {
1275 switch (Tok.getKind()) {
1276 case tok::annot_cxxscope:
1277 case tok::annot_template_id:
1278 case tok::caret:
1279 case tok::code_completion:
1280 case tok::coloncolon:
1281 case tok::ellipsis:
1282 case tok::kw___attribute:
1283 case tok::kw_operator:
1284 case tok::l_paren:
1285 case tok::star:
1286 return true;
1287
1288 case tok::amp:
1289 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001290 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001291
Richard Smith1c94c162012-01-09 22:31:44 +00001292 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith80ad52f2013-01-02 11:42:31 +00001293 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smith1c94c162012-01-09 22:31:44 +00001294 NextToken().is(tok::l_square);
1295
1296 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001297 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001298
Richard Smith0706df42011-10-19 21:33:05 +00001299 case tok::identifier:
1300 switch (NextToken().getKind()) {
1301 case tok::code_completion:
1302 case tok::coloncolon:
1303 case tok::comma:
1304 case tok::equal:
1305 case tok::equalequal: // Might be a typo for '='.
1306 case tok::kw_alignas:
1307 case tok::kw_asm:
1308 case tok::kw___attribute:
1309 case tok::l_brace:
1310 case tok::l_paren:
1311 case tok::l_square:
1312 case tok::less:
1313 case tok::r_brace:
1314 case tok::r_paren:
1315 case tok::r_square:
1316 case tok::semi:
1317 return true;
1318
1319 case tok::colon:
1320 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001321 // and in block scope it's probably a label. Inside a class definition,
1322 // this is a bit-field.
1323 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001324 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001325
1326 case tok::identifier: // Possible virt-specifier.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001327 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001328
1329 default:
1330 return false;
1331 }
1332
1333 default:
1334 return false;
1335 }
1336}
1337
Richard Smith994d73f2012-04-11 20:59:20 +00001338/// Skip until we reach something which seems like a sensible place to pick
1339/// up parsing after a malformed declaration. This will sometimes stop sooner
1340/// than SkipUntil(tok::r_brace) would, but will never stop later.
1341void Parser::SkipMalformedDecl() {
1342 while (true) {
1343 switch (Tok.getKind()) {
1344 case tok::l_brace:
1345 // Skip until matching }, then stop. We've probably skipped over
1346 // a malformed class or function definition or similar.
1347 ConsumeBrace();
1348 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1349 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1350 // This declaration isn't over yet. Keep skipping.
1351 continue;
1352 }
1353 if (Tok.is(tok::semi))
1354 ConsumeToken();
1355 return;
1356
1357 case tok::l_square:
1358 ConsumeBracket();
1359 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1360 continue;
1361
1362 case tok::l_paren:
1363 ConsumeParen();
1364 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1365 continue;
1366
1367 case tok::r_brace:
1368 return;
1369
1370 case tok::semi:
1371 ConsumeToken();
1372 return;
1373
1374 case tok::kw_inline:
1375 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001376 // a good place to pick back up parsing, except in an Objective-C
1377 // @interface context.
1378 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1379 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001380 return;
1381 break;
1382
1383 case tok::kw_namespace:
1384 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001385 // place to pick back up parsing, except in an Objective-C
1386 // @interface context.
1387 if (Tok.isAtStartOfLine() &&
1388 (!ParsingInObjCContainer || CurParsedObjCImpl))
1389 return;
1390 break;
1391
1392 case tok::at:
1393 // @end is very much like } in Objective-C contexts.
1394 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1395 ParsingInObjCContainer)
1396 return;
1397 break;
1398
1399 case tok::minus:
1400 case tok::plus:
1401 // - and + probably start new method declarations in Objective-C contexts.
1402 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001403 return;
1404 break;
1405
1406 case tok::eof:
1407 return;
1408
1409 default:
1410 break;
1411 }
1412
1413 ConsumeAnyToken();
1414 }
1415}
1416
John McCalld8ac0572009-11-03 19:26:08 +00001417/// ParseDeclGroup - Having concluded that this is either a function
1418/// definition or a group of object declarations, actually parse the
1419/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001420Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1421 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001422 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001423 SourceLocation *DeclEnd,
1424 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001425 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001426 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001427 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001428
John McCalld8ac0572009-11-03 19:26:08 +00001429 // Bail out if the first declarator didn't seem well-formed.
1430 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001431 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001432 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001433 }
Mike Stump1eb44332009-09-09 15:08:12 +00001434
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001435 // Save late-parsed attributes for now; they need to be parsed in the
1436 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001437 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1438 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001439 if (D.isFunctionDeclarator())
1440 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1441
Chris Lattnerc82daef2010-07-11 22:24:20 +00001442 // Check to see if we have a function *definition* which must have a body.
1443 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1444 // Look at the next token to make sure that this isn't a function
1445 // declaration. We have to check this because __attribute__ might be the
1446 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001447 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001448
Chris Lattner004659a2010-07-11 22:42:07 +00001449 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001450 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1451 Diag(Tok, diag::err_function_declared_typedef);
1452
1453 // Recover by treating the 'typedef' as spurious.
1454 DS.ClearStorageClassSpecs();
1455 }
1456
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001457 Decl *TheDecl =
1458 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001459 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001460 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001461
Chris Lattner004659a2010-07-11 22:42:07 +00001462 if (isDeclarationSpecifier()) {
1463 // If there is an invalid declaration specifier right after the function
1464 // prototype, then we must be in a missing semicolon case where this isn't
1465 // actually a body. Just fall through into the code that handles it as a
1466 // prototype, and let the top-level code handle the erroneous declspec
1467 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001468 } else {
1469 Diag(Tok, diag::err_expected_fn_body);
1470 SkipUntil(tok::semi);
1471 return DeclGroupPtrTy();
1472 }
1473 }
1474
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001475 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001476 return DeclGroupPtrTy();
1477
1478 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1479 // must parse and analyze the for-range-initializer before the declaration is
1480 // analyzed.
1481 if (FRI && Tok.is(tok::colon)) {
1482 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001483 if (Tok.is(tok::l_brace))
1484 FRI->RangeExpr = ParseBraceInitializer();
1485 else
1486 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001487 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1488 Actions.ActOnCXXForRangeDecl(ThisDecl);
1489 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001490 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001491 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1492 }
1493
Chris Lattner5f9e2722011-07-23 10:55:15 +00001494 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001495 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001496 if (LateParsedAttrs.size() > 0)
1497 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001498 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001499 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001500 DeclsInGroup.push_back(FirstDecl);
1501
Richard Smith0706df42011-10-19 21:33:05 +00001502 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001503
John McCalld8ac0572009-11-03 19:26:08 +00001504 // If we don't have a comma, it is either the end of the list (a ';') or an
1505 // error, bail out.
1506 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001507 SourceLocation CommaLoc = ConsumeToken();
1508
1509 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1510 // This comma was followed by a line-break and something which can't be
1511 // the start of a declarator. The comma was probably a typo for a
1512 // semicolon.
1513 Diag(CommaLoc, diag::err_expected_semi_declaration)
1514 << FixItHint::CreateReplacement(CommaLoc, ";");
1515 ExpectSemi = false;
1516 break;
1517 }
John McCalld8ac0572009-11-03 19:26:08 +00001518
1519 // Parse the next declarator.
1520 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001521 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001522
1523 // Accept attributes in an init-declarator. In the first declarator in a
1524 // declaration, these would be part of the declspec. In subsequent
1525 // declarators, they become part of the declarator itself, so that they
1526 // don't apply to declarators after *this* one. Examples:
1527 // short __attribute__((common)) var; -> declspec
1528 // short var __attribute__((common)); -> declarator
1529 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001530 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001531
1532 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001533 if (!D.isInvalidType()) {
1534 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1535 D.complete(ThisDecl);
1536 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001537 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001538 }
John McCalld8ac0572009-11-03 19:26:08 +00001539 }
1540
1541 if (DeclEnd)
1542 *DeclEnd = Tok.getLocation();
1543
Richard Smith0706df42011-10-19 21:33:05 +00001544 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001545 ExpectAndConsumeSemi(Context == Declarator::FileContext
1546 ? diag::err_invalid_token_after_toplevel_declarator
1547 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001548 // Okay, there was no semicolon and one was expected. If we see a
1549 // declaration specifier, just assume it was missing and continue parsing.
1550 // Otherwise things are very confused and we skip to recover.
1551 if (!isDeclarationSpecifier()) {
1552 SkipUntil(tok::r_brace, true, true);
1553 if (Tok.is(tok::semi))
1554 ConsumeToken();
1555 }
John McCalld8ac0572009-11-03 19:26:08 +00001556 }
1557
Douglas Gregor23c94db2010-07-02 17:43:08 +00001558 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001559 DeclsInGroup.data(),
1560 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001561}
1562
Richard Smithad762fc2011-04-14 22:09:26 +00001563/// Parse an optional simple-asm-expr and attributes, and attach them to a
1564/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001565bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001566 // If a simple-asm-expr is present, parse it.
1567 if (Tok.is(tok::kw_asm)) {
1568 SourceLocation Loc;
1569 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1570 if (AsmLabel.isInvalid()) {
1571 SkipUntil(tok::semi, true, true);
1572 return true;
1573 }
1574
1575 D.setAsmLabel(AsmLabel.release());
1576 D.SetRangeEnd(Loc);
1577 }
1578
1579 MaybeParseGNUAttributes(D);
1580 return false;
1581}
1582
Douglas Gregor1426e532009-05-12 21:31:51 +00001583/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1584/// declarator'. This method parses the remainder of the declaration
1585/// (including any attributes or initializer, among other things) and
1586/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001587///
Reid Spencer5f016e22007-07-11 17:01:13 +00001588/// init-declarator: [C99 6.7]
1589/// declarator
1590/// declarator '=' initializer
1591/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1592/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001593/// [C++] declarator initializer[opt]
1594///
1595/// [C++] initializer:
1596/// [C++] '=' initializer-clause
1597/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001598/// [C++0x] '=' 'default' [TODO]
1599/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001600/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001601///
1602/// According to the standard grammar, =default and =delete are function
1603/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001604///
John McCalld226f652010-08-21 09:40:31 +00001605Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001606 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001607 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001608 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001609
Richard Smithad762fc2011-04-14 22:09:26 +00001610 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1611}
Mike Stump1eb44332009-09-09 15:08:12 +00001612
Richard Smithad762fc2011-04-14 22:09:26 +00001613Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1614 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001615 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001616 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001617 switch (TemplateInfo.Kind) {
1618 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001619 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001620 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001621
Douglas Gregord5a423b2009-09-25 18:43:00 +00001622 case ParsedTemplateInfo::Template:
1623 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001624 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001625 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001626 D);
1627 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001628
Douglas Gregord5a423b2009-09-25 18:43:00 +00001629 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosier8decdee2012-06-26 22:30:43 +00001630 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001631 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001632 TemplateInfo.ExternLoc,
1633 TemplateInfo.TemplateLoc,
1634 D);
1635 if (ThisRes.isInvalid()) {
1636 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001637 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001638 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001639
Douglas Gregord5a423b2009-09-25 18:43:00 +00001640 ThisDecl = ThisRes.get();
1641 break;
1642 }
1643 }
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Richard Smith34b41d92011-02-20 03:19:35 +00001645 bool TypeContainsAuto =
1646 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1647
Douglas Gregor1426e532009-05-12 21:31:51 +00001648 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001649 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001650 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001651 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001652 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001653 if (D.isFunctionDeclarator())
1654 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1655 << 1 /* delete */;
1656 else
1657 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001658 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001659 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001660 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1661 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001662 else
1663 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001664 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001665 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001666 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001667 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001668 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001669
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001670 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001671 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001672 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001673 cutOffParsing();
1674 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001675 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001676
John McCall60d7b3a2010-08-24 06:29:42 +00001677 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001678
David Blaikie4e4d0842012-03-11 07:00:24 +00001679 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001680 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001681 ExitScope();
1682 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001683
Douglas Gregor1426e532009-05-12 21:31:51 +00001684 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001685 SkipUntil(tok::comma, true, true);
1686 Actions.ActOnInitializerError(ThisDecl);
1687 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001688 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1689 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001690 }
1691 } else if (Tok.is(tok::l_paren)) {
1692 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001693 BalancedDelimiterTracker T(*this, tok::l_paren);
1694 T.consumeOpen();
1695
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001696 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001697 CommaLocsTy CommaLocs;
1698
David Blaikie4e4d0842012-03-11 07:00:24 +00001699 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001700 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001701 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001702 }
1703
Douglas Gregor1426e532009-05-12 21:31:51 +00001704 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001705 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +00001706 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001707
David Blaikie4e4d0842012-03-11 07:00:24 +00001708 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001709 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001710 ExitScope();
1711 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001712 } else {
1713 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001714 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001715
1716 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1717 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001718
David Blaikie4e4d0842012-03-11 07:00:24 +00001719 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001720 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001721 ExitScope();
1722 }
1723
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001724 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1725 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001726 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001727 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1728 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001729 }
Richard Smith80ad52f2013-01-02 11:42:31 +00001730 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001731 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001732 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001733 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1734
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001735 if (D.getCXXScopeSpec().isSet()) {
1736 EnterScope(0);
1737 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1738 }
1739
1740 ExprResult Init(ParseBraceInitializer());
1741
1742 if (D.getCXXScopeSpec().isSet()) {
1743 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1744 ExitScope();
1745 }
1746
1747 if (Init.isInvalid()) {
1748 Actions.ActOnInitializerError(ThisDecl);
1749 } else
1750 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1751 /*DirectInit=*/true, TypeContainsAuto);
1752
Douglas Gregor1426e532009-05-12 21:31:51 +00001753 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001754 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001755 }
1756
Richard Smith483b9f32011-02-21 20:05:19 +00001757 Actions.FinalizeDeclaration(ThisDecl);
1758
Douglas Gregor1426e532009-05-12 21:31:51 +00001759 return ThisDecl;
1760}
1761
Reid Spencer5f016e22007-07-11 17:01:13 +00001762/// ParseSpecifierQualifierList
1763/// specifier-qualifier-list:
1764/// type-specifier specifier-qualifier-list[opt]
1765/// type-qualifier specifier-qualifier-list[opt]
1766/// [GNU] attributes specifier-qualifier-list[opt]
1767///
Richard Smith69730c12012-03-12 07:56:15 +00001768void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1769 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001770 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1771 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001772 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001773 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001774
Reid Spencer5f016e22007-07-11 17:01:13 +00001775 // Validate declspec for type-name.
1776 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001777 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1778 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001779 Diag(Tok, diag::err_expected_type);
1780 DS.SetTypeSpecError();
1781 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1782 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001783 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001784 if (!DS.hasTypeSpecifier())
1785 DS.SetTypeSpecError();
1786 }
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Reid Spencer5f016e22007-07-11 17:01:13 +00001788 // Issue diagnostic and remove storage class if present.
1789 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1790 if (DS.getStorageClassSpecLoc().isValid())
1791 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1792 else
1793 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1794 DS.ClearStorageClassSpecs();
1795 }
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Reid Spencer5f016e22007-07-11 17:01:13 +00001797 // Issue diagnostic and remove function specfier if present.
1798 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001799 if (DS.isInlineSpecified())
1800 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1801 if (DS.isVirtualSpecified())
1802 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1803 if (DS.isExplicitSpecified())
1804 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001805 DS.ClearFunctionSpecs();
1806 }
Richard Smith69730c12012-03-12 07:56:15 +00001807
1808 // Issue diagnostic and remove constexpr specfier if present.
1809 if (DS.isConstexprSpecified()) {
1810 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1811 DS.ClearConstexprSpec();
1812 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001813}
1814
Chris Lattnerc199ab32009-04-12 20:42:31 +00001815/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1816/// specified token is valid after the identifier in a declarator which
1817/// immediately follows the declspec. For example, these things are valid:
1818///
1819/// int x [ 4]; // direct-declarator
1820/// int x ( int y); // direct-declarator
1821/// int(int x ) // direct-declarator
1822/// int x ; // simple-declaration
1823/// int x = 17; // init-declarator-list
1824/// int x , y; // init-declarator-list
1825/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001826/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001827/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001828///
1829/// This is not, because 'x' does not immediately follow the declspec (though
1830/// ')' happens to be valid anyway).
1831/// int (x)
1832///
1833static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1834 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1835 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001836 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001837}
1838
Chris Lattnere40c2952009-04-14 21:34:55 +00001839
1840/// ParseImplicitInt - This method is called when we have an non-typename
1841/// identifier in a declspec (which normally terminates the decl spec) when
1842/// the declspec has no type specifier. In this case, the declspec is either
1843/// malformed or is "implicit int" (in K&R and C89).
1844///
1845/// This method handles diagnosing this prettily and returns false if the
1846/// declspec is done being processed. If it recovers and thinks there may be
1847/// other pieces of declspec after it, it returns true.
1848///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001849bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001850 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00001851 AccessSpecifier AS, DeclSpecContext DSC,
1852 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001853 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001854
Chris Lattnere40c2952009-04-14 21:34:55 +00001855 SourceLocation Loc = Tok.getLocation();
1856 // If we see an identifier that is not a type name, we normally would
1857 // parse it as the identifer being declared. However, when a typename
1858 // is typo'd or the definition is not included, this will incorrectly
1859 // parse the typename as the identifier name and fall over misparsing
1860 // later parts of the diagnostic.
1861 //
1862 // As such, we try to do some look-ahead in cases where this would
1863 // otherwise be an "implicit-int" case to see if this is invalid. For
1864 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1865 // an identifier with implicit int, we'd get a parse error because the
1866 // next token is obviously invalid for a type. Parse these as a case
1867 // with an invalid type specifier.
1868 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001869
Chris Lattnere40c2952009-04-14 21:34:55 +00001870 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001871 // error, do lookahead to try to do better recovery. This never applies
1872 // within a type specifier. Outside of C++, we allow this even if the
1873 // language doesn't "officially" support implicit int -- we support
1874 // implicit int as an extension in C99 and C11. Allegedly, MS also
1875 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001876 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001877 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001878 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001879 // If this token is valid for implicit int, e.g. "static x = 4", then
1880 // we just avoid eating the identifier, so it will be parsed as the
1881 // identifier in the declarator.
1882 return false;
1883 }
Mike Stump1eb44332009-09-09 15:08:12 +00001884
Richard Smith827adaf2012-05-15 21:01:51 +00001885 if (getLangOpts().CPlusPlus &&
1886 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1887 // Don't require a type specifier if we have the 'auto' storage class
1888 // specifier in C++98 -- we'll promote it to a type specifier.
1889 return false;
1890 }
1891
Chris Lattnere40c2952009-04-14 21:34:55 +00001892 // Otherwise, if we don't consume this token, we are going to emit an
1893 // error anyway. Try to recover from various common problems. Check
1894 // to see if this was a reference to a tag name without a tag specified.
1895 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001896 //
1897 // C++ doesn't need this, and isTagName doesn't take SS.
1898 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001899 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001900 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001901
Douglas Gregor23c94db2010-07-02 17:43:08 +00001902 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001903 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001904 case DeclSpec::TST_enum:
1905 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1906 case DeclSpec::TST_union:
1907 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1908 case DeclSpec::TST_struct:
1909 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00001910 case DeclSpec::TST_interface:
1911 TagName="__interface"; FixitTagName = "__interface ";
1912 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001913 case DeclSpec::TST_class:
1914 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001915 }
Mike Stump1eb44332009-09-09 15:08:12 +00001916
Chris Lattnerf4382f52009-04-14 22:17:06 +00001917 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001918 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1919 LookupResult R(Actions, TokenName, SourceLocation(),
1920 Sema::LookupOrdinaryName);
1921
Chris Lattnerf4382f52009-04-14 22:17:06 +00001922 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001923 << TokenName << TagName << getLangOpts().CPlusPlus
1924 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1925
1926 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1927 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1928 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001929 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001930 << TokenName << TagName;
1931 }
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Chris Lattnerf4382f52009-04-14 22:17:06 +00001933 // Parse this as a tag as if the missing tag were present.
1934 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001935 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001936 else
Richard Smith69730c12012-03-12 07:56:15 +00001937 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00001938 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001939 return true;
1940 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001941 }
Mike Stump1eb44332009-09-09 15:08:12 +00001942
Richard Smith8f0a7e72012-05-15 21:29:55 +00001943 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001944 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001945 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1946 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001947 // Look ahead to the next token to try to figure out what this declaration
1948 // was supposed to be.
1949 switch (NextToken().getKind()) {
1950 case tok::comma:
1951 case tok::equal:
1952 case tok::kw_asm:
1953 case tok::l_brace:
1954 case tok::l_square:
1955 case tok::semi:
1956 // This looks like a variable declaration. The type is probably missing.
1957 // We're done parsing decl-specifiers.
1958 return false;
1959
1960 case tok::l_paren: {
1961 // static x(4); // 'x' is not a type
1962 // x(int n); // 'x' is not a type
1963 // x (*p)[]; // 'x' is a type
1964 //
1965 // Since we're in an error case (or the rare 'implicit int in C++' MS
1966 // extension), we can afford to perform a tentative parse to determine
1967 // which case we're in.
1968 TentativeParsingAction PA(*this);
1969 ConsumeToken();
1970 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1971 PA.Revert();
1972 if (TPR == TPResult::False())
1973 return false;
1974 // The identifier is followed by a parenthesized declarator.
1975 // It's supposed to be a type.
1976 break;
1977 }
1978
1979 default:
1980 // This is probably supposed to be a type. This includes cases like:
1981 // int f(itn);
1982 // struct S { unsinged : 4; };
1983 break;
1984 }
1985 }
1986
Chad Rosier8decdee2012-06-26 22:30:43 +00001987 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00001988 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001989 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001990 IdentifierInfo *II = Tok.getIdentifierInfo();
1991 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001992 // The action emitted a diagnostic, so we don't have to.
1993 if (T) {
1994 // The action has suggested that the type T could be used. Set that as
1995 // the type in the declaration specifiers, consume the would-be type
1996 // name token, and we're done.
1997 const char *PrevSpec;
1998 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001999 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00002000 DS.SetRangeEnd(Tok.getLocation());
2001 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002002 // There may be other declaration specifiers after this.
2003 return true;
2004 } else if (II != Tok.getIdentifierInfo()) {
2005 // If no type was suggested, the correction is to a keyword
2006 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002007 // There may be other declaration specifiers after this.
2008 return true;
2009 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002010
Douglas Gregora786fdb2009-10-13 23:27:22 +00002011 // Fall through; the action had no suggestion for us.
2012 } else {
2013 // The action did not emit a diagnostic, so emit one now.
2014 SourceRange R;
2015 if (SS) R = SS->getRange();
2016 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2017 }
Mike Stump1eb44332009-09-09 15:08:12 +00002018
Douglas Gregora786fdb2009-10-13 23:27:22 +00002019 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002020 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002021 DS.SetRangeEnd(Tok.getLocation());
2022 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002023
Chris Lattnere40c2952009-04-14 21:34:55 +00002024 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2025 // avoid rippling error messages on subsequent uses of the same type,
2026 // could be useful if #include was forgotten.
2027 return false;
2028}
2029
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002030/// \brief Determine the declaration specifier context from the declarator
2031/// context.
2032///
2033/// \param Context the declarator context, which is one of the
2034/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002035Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002036Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2037 if (Context == Declarator::MemberContext)
2038 return DSC_class;
2039 if (Context == Declarator::FileContext)
2040 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002041 if (Context == Declarator::TrailingReturnContext)
2042 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002043 return DSC_normal;
2044}
2045
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002046/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2047///
2048/// FIXME: Simply returns an alignof() expression if the argument is a
2049/// type. Ideally, the type should be propagated directly into Sema.
2050///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002051/// [C11] type-id
2052/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002053/// [C++0x] type-id ...[opt]
2054/// [C++0x] assignment-expression ...[opt]
2055ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2056 SourceLocation &EllipsisLoc) {
2057 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002058 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002059 SourceLocation TypeLoc = Tok.getLocation();
2060 ParsedType Ty = ParseTypeName().get();
2061 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002062 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2063 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002064 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002065 ER = ParseConstantExpression();
2066
Richard Smith80ad52f2013-01-02 11:42:31 +00002067 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002068 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002069
2070 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002071}
2072
2073/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2074/// attribute to Attrs.
2075///
2076/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002077/// [C11] '_Alignas' '(' type-id ')'
2078/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smith33f04a22013-01-29 01:48:07 +00002079/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2080/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002081void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
2082 SourceLocation *endLoc) {
2083 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2084 "Not an alignment-specifier!");
2085
Richard Smith33f04a22013-01-29 01:48:07 +00002086 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2087 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002088
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002089 BalancedDelimiterTracker T(*this, tok::l_paren);
2090 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002091 return;
2092
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002093 SourceLocation EllipsisLoc;
2094 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002095 if (ArgExpr.isInvalid()) {
2096 SkipUntil(tok::r_paren);
2097 return;
2098 }
2099
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002100 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002101 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002102 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002103
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002104 // FIXME: Handle pack-expansions here.
2105 if (EllipsisLoc.isValid()) {
2106 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
2107 return;
2108 }
2109
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002110 ExprVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002111 ArgExprs.push_back(ArgExpr.release());
Richard Smith33f04a22013-01-29 01:48:07 +00002112 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
2113 ArgExprs.data(), 1, AttributeList::AS_Keyword);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002114}
2115
Reid Spencer5f016e22007-07-11 17:01:13 +00002116/// ParseDeclarationSpecifiers
2117/// declaration-specifiers: [C99 6.7]
2118/// storage-class-specifier declaration-specifiers[opt]
2119/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002120/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002121/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002122/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002123/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002124///
2125/// storage-class-specifier: [C99 6.7.1]
2126/// 'typedef'
2127/// 'extern'
2128/// 'static'
2129/// 'auto'
2130/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002131/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002132/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002133/// function-specifier: [C99 6.7.4]
2134/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002135/// [C++] 'virtual'
2136/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002137/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002138/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002139/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002140
Reid Spencer5f016e22007-07-11 17:01:13 +00002141///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002142void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002143 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002144 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002145 DeclSpecContext DSContext,
2146 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002147 if (DS.getSourceRange().isInvalid()) {
2148 DS.SetRangeStart(Tok.getLocation());
2149 DS.SetRangeEnd(Tok.getLocation());
2150 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002151
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002152 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002153 bool AttrsLastTime = false;
2154 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002155 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002156 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002157 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002158 unsigned DiagID = 0;
2159
Reid Spencer5f016e22007-07-11 17:01:13 +00002160 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002161
Reid Spencer5f016e22007-07-11 17:01:13 +00002162 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002163 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002164 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002165 if (!AttrsLastTime)
2166 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002167 else {
2168 // Reject C++11 attributes that appertain to decl specifiers as
2169 // we don't support any C++11 attributes that appertain to decl
2170 // specifiers. This also conforms to what g++ 4.8 is doing.
2171 ProhibitCXX11Attributes(attrs);
2172
Sean Hunt2edf0a22012-06-23 05:07:58 +00002173 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002174 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002175
Reid Spencer5f016e22007-07-11 17:01:13 +00002176 // If this is not a declaration specifier token, we're done reading decl
2177 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002178 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002179 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002180
Sean Hunt2edf0a22012-06-23 05:07:58 +00002181 case tok::l_square:
2182 case tok::kw_alignas:
2183 if (!isCXX11AttributeSpecifier())
2184 goto DoneWithDeclSpec;
2185
2186 ProhibitAttributes(attrs);
2187 // FIXME: It would be good to recover by accepting the attributes,
2188 // but attempting to do that now would cause serious
2189 // madness in terms of diagnostics.
2190 attrs.clear();
2191 attrs.Range = SourceRange();
2192
2193 ParseCXX11Attributes(attrs);
2194 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002195 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002196
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002197 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002198 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002199 if (DS.hasTypeSpecifier()) {
2200 bool AllowNonIdentifiers
2201 = (getCurScope()->getFlags() & (Scope::ControlScope |
2202 Scope::BlockScope |
2203 Scope::TemplateParamScope |
2204 Scope::FunctionPrototypeScope |
2205 Scope::AtCatchScope)) == 0;
2206 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002207 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002208 (DSContext == DSC_class && DS.isFriendSpecified());
2209
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002210 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002211 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002212 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002213 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002214 }
2215
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002216 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2217 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2218 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002219 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002220 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002221 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002222 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002223 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002224 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002225
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002226 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002227 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002228 }
2229
Chris Lattner5e02c472009-01-05 00:07:25 +00002230 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002231 // C++ scope specifier. Annotate and loop, or bail out on error.
2232 if (TryAnnotateCXXScopeToken(true)) {
2233 if (!DS.hasTypeSpecifier())
2234 DS.SetTypeSpecError();
2235 goto DoneWithDeclSpec;
2236 }
John McCall2e0a7152010-03-01 18:20:46 +00002237 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2238 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002239 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002240
2241 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002242 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002243 goto DoneWithDeclSpec;
2244
John McCallaa87d332009-12-12 11:40:51 +00002245 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002246 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2247 Tok.getAnnotationRange(),
2248 SS);
John McCallaa87d332009-12-12 11:40:51 +00002249
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002250 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002251 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002252 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002253 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002254 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002255 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002256
2257 // C++ [class.qual]p2:
2258 // In a lookup in which the constructor is an acceptable lookup
2259 // result and the nested-name-specifier nominates a class C:
2260 //
2261 // - if the name specified after the
2262 // nested-name-specifier, when looked up in C, is the
2263 // injected-class-name of C (Clause 9), or
2264 //
2265 // - if the name specified after the nested-name-specifier
2266 // is the same as the identifier or the
2267 // simple-template-id's template-name in the last
2268 // component of the nested-name-specifier,
2269 //
2270 // the name is instead considered to name the constructor of
2271 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002272 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002273 // Thus, if the template-name is actually the constructor
2274 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002275 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002276 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002277 if ((DSContext == DSC_top_level ||
2278 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2279 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002280 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002281 if (isConstructorDeclarator()) {
2282 // The user meant this to be an out-of-line constructor
2283 // definition, but template arguments are not allowed
2284 // there. Just allow this as a constructor; we'll
2285 // complain about it later.
2286 goto DoneWithDeclSpec;
2287 }
2288
2289 // The user meant this to name a type, but it actually names
2290 // a constructor with some extraneous template
2291 // arguments. Complain, then parse it as a type as the user
2292 // intended.
2293 Diag(TemplateId->TemplateNameLoc,
2294 diag::err_out_of_line_template_id_names_constructor)
2295 << TemplateId->Name;
2296 }
2297
John McCallaa87d332009-12-12 11:40:51 +00002298 DS.getTypeSpecScope() = SS;
2299 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002300 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002301 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002302 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002303 continue;
2304 }
2305
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002306 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002307 DS.getTypeSpecScope() = SS;
2308 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002309 if (Tok.getAnnotationValue()) {
2310 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002311 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002312 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002313 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002314 if (isInvalid)
2315 break;
John McCallb3d87482010-08-24 05:47:05 +00002316 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002317 else
2318 DS.SetTypeSpecError();
2319 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2320 ConsumeToken(); // The typename
2321 }
2322
Douglas Gregor9135c722009-03-25 15:40:00 +00002323 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002324 goto DoneWithDeclSpec;
2325
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002326 // If we're in a context where the identifier could be a class name,
2327 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002328 if ((DSContext == DSC_top_level ||
2329 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002330 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002331 &SS)) {
2332 if (isConstructorDeclarator())
2333 goto DoneWithDeclSpec;
2334
2335 // As noted in C++ [class.qual]p2 (cited above), when the name
2336 // of the class is qualified in a context where it could name
2337 // a constructor, its a constructor name. However, we've
2338 // looked at the declarator, and the user probably meant this
2339 // to be a type. Complain that it isn't supposed to be treated
2340 // as a type, then proceed to parse it as a type.
2341 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2342 << Next.getIdentifierInfo();
2343 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002344
John McCallb3d87482010-08-24 05:47:05 +00002345 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2346 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002347 getCurScope(), &SS,
2348 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002349 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002350 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002351
Chris Lattnerf4382f52009-04-14 22:17:06 +00002352 // If the referenced identifier is not a type, then this declspec is
2353 // erroneous: We already checked about that it has no type specifier, and
2354 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002355 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002356 if (TypeRep == 0) {
2357 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002358 ParsedAttributesWithRange Attrs(AttrFactory);
2359 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2360 if (!Attrs.empty()) {
2361 AttrsLastTime = true;
2362 attrs.takeAllFrom(Attrs);
2363 }
2364 continue;
2365 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002366 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002367 }
Mike Stump1eb44332009-09-09 15:08:12 +00002368
John McCallaa87d332009-12-12 11:40:51 +00002369 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002370 ConsumeToken(); // The C++ scope.
2371
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002372 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002373 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002374 if (isInvalid)
2375 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002376
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002377 DS.SetRangeEnd(Tok.getLocation());
2378 ConsumeToken(); // The typename.
2379
2380 continue;
2381 }
Mike Stump1eb44332009-09-09 15:08:12 +00002382
Chris Lattner80d0c892009-01-21 19:48:37 +00002383 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002384 if (Tok.getAnnotationValue()) {
2385 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002386 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002387 DiagID, T);
2388 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002389 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002390
Chris Lattner5c5db552010-04-05 18:18:31 +00002391 if (isInvalid)
2392 break;
2393
Chris Lattner80d0c892009-01-21 19:48:37 +00002394 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2395 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002396
Chris Lattner80d0c892009-01-21 19:48:37 +00002397 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2398 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002399 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002400 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002401 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002402
Chris Lattner80d0c892009-01-21 19:48:37 +00002403 continue;
2404 }
Mike Stump1eb44332009-09-09 15:08:12 +00002405
Douglas Gregorbfad9152011-04-28 15:48:45 +00002406 case tok::kw___is_signed:
2407 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2408 // typically treats it as a trait. If we see __is_signed as it appears
2409 // in libstdc++, e.g.,
2410 //
2411 // static const bool __is_signed;
2412 //
2413 // then treat __is_signed as an identifier rather than as a keyword.
2414 if (DS.getTypeSpecType() == TST_bool &&
2415 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2416 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2417 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2418 Tok.setKind(tok::identifier);
2419 }
2420
2421 // We're done with the declaration-specifiers.
2422 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002423
Chris Lattner3bd934a2008-07-26 01:18:38 +00002424 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002425 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002426 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002427 // In C++, check to see if this is a scope specifier like foo::bar::, if
2428 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002429 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002430 if (TryAnnotateCXXScopeToken(true)) {
2431 if (!DS.hasTypeSpecifier())
2432 DS.SetTypeSpecError();
2433 goto DoneWithDeclSpec;
2434 }
2435 if (!Tok.is(tok::identifier))
2436 continue;
2437 }
Mike Stump1eb44332009-09-09 15:08:12 +00002438
Chris Lattner3bd934a2008-07-26 01:18:38 +00002439 // This identifier can only be a typedef name if we haven't already seen
2440 // a type-specifier. Without this check we misparse:
2441 // typedef int X; struct Y { short X; }; as 'short int'.
2442 if (DS.hasTypeSpecifier())
2443 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002444
John Thompson82287d12010-02-05 00:12:22 +00002445 // Check for need to substitute AltiVec keyword tokens.
2446 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2447 break;
2448
Richard Smithf63eee72012-05-09 18:56:43 +00002449 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2450 // allow the use of a typedef name as a type specifier.
2451 if (DS.isTypeAltiVecVector())
2452 goto DoneWithDeclSpec;
2453
John McCallb3d87482010-08-24 05:47:05 +00002454 ParsedType TypeRep =
2455 Actions.getTypeName(*Tok.getIdentifierInfo(),
2456 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002457
Chris Lattnerc199ab32009-04-12 20:42:31 +00002458 // If this is not a typedef name, don't parse it as part of the declspec,
2459 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002460 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002461 ParsedAttributesWithRange Attrs(AttrFactory);
2462 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2463 if (!Attrs.empty()) {
2464 AttrsLastTime = true;
2465 attrs.takeAllFrom(Attrs);
2466 }
2467 continue;
2468 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002469 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002470 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002471
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002472 // If we're in a context where the identifier could be a class name,
2473 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002474 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002475 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002476 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002477 goto DoneWithDeclSpec;
2478
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002479 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002480 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002481 if (isInvalid)
2482 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002483
Chris Lattner3bd934a2008-07-26 01:18:38 +00002484 DS.SetRangeEnd(Tok.getLocation());
2485 ConsumeToken(); // The identifier
2486
2487 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2488 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002489 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002490 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002491 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002492
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002493 // Need to support trailing type qualifiers (e.g. "id<p> const").
2494 // If a type specifier follows, it will be diagnosed elsewhere.
2495 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002496 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002497
2498 // type-name
2499 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002500 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002501 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002502 // This template-id does not refer to a type name, so we're
2503 // done with the type-specifiers.
2504 goto DoneWithDeclSpec;
2505 }
2506
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002507 // If we're in a context where the template-id could be a
2508 // constructor name or specialization, check whether this is a
2509 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002510 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002511 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002512 isConstructorDeclarator())
2513 goto DoneWithDeclSpec;
2514
Douglas Gregor39a8de12009-02-25 19:37:18 +00002515 // Turn the template-id annotation token into a type annotation
2516 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002517 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002518 continue;
2519 }
2520
Reid Spencer5f016e22007-07-11 17:01:13 +00002521 // GNU attributes support.
2522 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002523 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002524 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002525
2526 // Microsoft declspec support.
2527 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002528 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002529 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002530
Steve Naroff239f0732008-12-25 14:16:32 +00002531 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002532 case tok::kw___forceinline: {
Chad Rosier22aa6902012-12-21 22:24:43 +00002533 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002534 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002535 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002536 // FIXME: This does not work correctly if it is set to be a declspec
2537 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002538 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002539 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002540 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002541 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002542
2543 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002544 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002545 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002546 case tok::kw___cdecl:
2547 case tok::kw___stdcall:
2548 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002549 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002550 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002551 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002552 continue;
2553
Dawn Perchik52fc3142010-09-03 01:29:35 +00002554 // Borland single token adornments.
2555 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002556 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002557 continue;
2558
Peter Collingbournef315fa82011-02-14 01:42:53 +00002559 // OpenCL single token adornments.
2560 case tok::kw___kernel:
2561 ParseOpenCLAttributes(DS.getAttributes());
2562 continue;
2563
Reid Spencer5f016e22007-07-11 17:01:13 +00002564 // storage-class-specifier
2565 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002566 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2567 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002568 break;
2569 case tok::kw_extern:
2570 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002571 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002572 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2573 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002574 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002575 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002576 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2577 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002578 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002579 case tok::kw_static:
2580 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002581 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002582 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2583 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002584 break;
2585 case tok::kw_auto:
Richard Smith80ad52f2013-01-02 11:42:31 +00002586 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002587 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002588 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2589 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002590 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002591 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002592 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002593 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002594 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2595 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002596 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002597 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2598 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002599 break;
2600 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002601 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2602 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002603 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002604 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002605 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2606 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002607 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002608 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002609 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002610 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002611
Reid Spencer5f016e22007-07-11 17:01:13 +00002612 // function-specifier
2613 case tok::kw_inline:
Chad Rosier22aa6902012-12-21 22:24:43 +00002614 isInvalid = DS.setFunctionSpecInline(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002615 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002616 case tok::kw_virtual:
Chad Rosier22aa6902012-12-21 22:24:43 +00002617 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002618 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002619 case tok::kw_explicit:
Chad Rosier22aa6902012-12-21 22:24:43 +00002620 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002621 break;
Richard Smithde03c152013-01-17 22:16:11 +00002622 case tok::kw__Noreturn:
2623 if (!getLangOpts().C11)
2624 Diag(Loc, diag::ext_c11_noreturn);
2625 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2626 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002627
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002628 // alignment-specifier
2629 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002630 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002631 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002632 ParseAlignmentSpecifier(DS.getAttributes());
2633 continue;
2634
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002635 // friend
2636 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002637 if (DSContext == DSC_class)
2638 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2639 else {
2640 PrevSpec = ""; // not actually used by the diagnostic
2641 DiagID = diag::err_friend_invalid_in_context;
2642 isInvalid = true;
2643 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002644 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002645
Douglas Gregor8d267c52011-09-09 02:06:17 +00002646 // Modules
2647 case tok::kw___module_private__:
2648 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2649 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002650
Sebastian Redl2ac67232009-11-05 15:47:02 +00002651 // constexpr
2652 case tok::kw_constexpr:
2653 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2654 break;
2655
Chris Lattner80d0c892009-01-21 19:48:37 +00002656 // type-specifier
2657 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002658 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2659 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002660 break;
2661 case tok::kw_long:
2662 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002663 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2664 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002665 else
John McCallfec54012009-08-03 20:12:06 +00002666 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2667 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002668 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002669 case tok::kw___int64:
2670 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2671 DiagID);
2672 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002673 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002674 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2675 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002676 break;
2677 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002678 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2679 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002680 break;
2681 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002682 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2683 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002684 break;
2685 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002686 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2687 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002688 break;
2689 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002690 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2691 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002692 break;
2693 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002694 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2695 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002696 break;
2697 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002698 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2699 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002700 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002701 case tok::kw___int128:
2702 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2703 DiagID);
2704 break;
2705 case tok::kw_half:
2706 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2707 DiagID);
2708 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002709 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002710 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2711 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002712 break;
2713 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002714 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2715 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002716 break;
2717 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002718 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2719 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002720 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002721 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002722 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2723 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002724 break;
2725 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002726 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2727 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002728 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002729 case tok::kw_bool:
2730 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002731 if (Tok.is(tok::kw_bool) &&
2732 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2733 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2734 PrevSpec = ""; // Not used by the diagnostic.
2735 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002736 // For better error recovery.
2737 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002738 isInvalid = true;
2739 } else {
2740 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2741 DiagID);
2742 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002743 break;
2744 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002745 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2746 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002747 break;
2748 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002749 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2750 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002751 break;
2752 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002753 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2754 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002755 break;
John Thompson82287d12010-02-05 00:12:22 +00002756 case tok::kw___vector:
2757 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2758 break;
2759 case tok::kw___pixel:
2760 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2761 break;
Guy Benyeib13621d2012-12-18 14:38:23 +00002762 case tok::kw_image1d_t:
2763 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2764 PrevSpec, DiagID);
2765 break;
2766 case tok::kw_image1d_array_t:
2767 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2768 PrevSpec, DiagID);
2769 break;
2770 case tok::kw_image1d_buffer_t:
2771 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2772 PrevSpec, DiagID);
2773 break;
2774 case tok::kw_image2d_t:
2775 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2776 PrevSpec, DiagID);
2777 break;
2778 case tok::kw_image2d_array_t:
2779 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2780 PrevSpec, DiagID);
2781 break;
2782 case tok::kw_image3d_t:
2783 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2784 PrevSpec, DiagID);
2785 break;
Guy Benyei21f18c42013-02-07 10:55:47 +00002786 case tok::kw_sampler_t:
2787 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2788 PrevSpec, DiagID);
2789 break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00002790 case tok::kw_event_t:
2791 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2792 PrevSpec, DiagID);
2793 break;
John McCalla5fc4722011-04-09 22:50:59 +00002794 case tok::kw___unknown_anytype:
2795 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2796 PrevSpec, DiagID);
2797 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002798
2799 // class-specifier:
2800 case tok::kw_class:
2801 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00002802 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00002803 case tok::kw_union: {
2804 tok::TokenKind Kind = Tok.getKind();
2805 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00002806
2807 // These are attributes following class specifiers.
2808 // To produce better diagnostic, we parse them when
2809 // parsing class specifier.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002810 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00002811 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002812 EnteringContext, DSContext, Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002813
2814 // If there are attributes following class specifier,
2815 // take them over and handle them here.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002816 if (!Attributes.empty()) {
Michael Han2e397132012-11-26 22:54:45 +00002817 AttrsLastTime = true;
Bill Wendlingad017fa2012-12-20 19:22:21 +00002818 attrs.takeAllFrom(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002819 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002820 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002821 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002822
2823 // enum-specifier:
2824 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002825 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002826 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002827 continue;
2828
2829 // cv-qualifier:
2830 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002831 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002832 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002833 break;
2834 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002835 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002836 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002837 break;
2838 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002839 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002840 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002841 break;
2842
Douglas Gregord57959a2009-03-27 23:10:48 +00002843 // C++ typename-specifier:
2844 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002845 if (TryAnnotateTypeOrScopeToken()) {
2846 DS.SetTypeSpecError();
2847 goto DoneWithDeclSpec;
2848 }
2849 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002850 continue;
2851 break;
2852
Chris Lattner80d0c892009-01-21 19:48:37 +00002853 // GNU typeof support.
2854 case tok::kw_typeof:
2855 ParseTypeofSpecifier(DS);
2856 continue;
2857
David Blaikie42d6d0c2011-12-04 05:04:18 +00002858 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002859 ParseDecltypeSpecifier(DS);
2860 continue;
2861
Sean Huntdb5d44b2011-05-19 05:37:45 +00002862 case tok::kw___underlying_type:
2863 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002864 continue;
2865
2866 case tok::kw__Atomic:
2867 ParseAtomicSpecifier(DS);
2868 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002869
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002870 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00002871 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002872 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002873 goto DoneWithDeclSpec;
2874 case tok::kw___private:
2875 case tok::kw___global:
2876 case tok::kw___local:
2877 case tok::kw___constant:
2878 case tok::kw___read_only:
2879 case tok::kw___write_only:
2880 case tok::kw___read_write:
2881 ParseOpenCLQualifiers(DS);
2882 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002883
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002884 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002885 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002886 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2887 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002888 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002889 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002890
Douglas Gregor46f936e2010-11-19 17:10:50 +00002891 if (!ParseObjCProtocolQualifiers(DS))
2892 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2893 << FixItHint::CreateInsertion(Loc, "id")
2894 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00002895
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002896 // Need to support trailing type qualifiers (e.g. "id<p> const").
2897 // If a type specifier follows, it will be diagnosed elsewhere.
2898 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002899 }
John McCallfec54012009-08-03 20:12:06 +00002900 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002901 if (isInvalid) {
2902 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002903 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00002904
Douglas Gregorae2fb142010-08-23 14:34:43 +00002905 if (DiagID == diag::ext_duplicate_declspec)
2906 Diag(Tok, DiagID)
2907 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2908 else
2909 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002910 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002911
Chris Lattner81c018d2008-03-13 06:29:04 +00002912 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002913 if (DiagID != diag::err_bool_redeclaration)
2914 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00002915
2916 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002917 }
2918}
Douglas Gregoradcac882008-12-01 23:54:00 +00002919
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002920/// ParseStructDeclaration - Parse a struct declaration without the terminating
2921/// semicolon.
2922///
Reid Spencer5f016e22007-07-11 17:01:13 +00002923/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002924/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002925/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002926/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002927/// struct-declarator-list:
2928/// struct-declarator
2929/// struct-declarator-list ',' struct-declarator
2930/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2931/// struct-declarator:
2932/// declarator
2933/// [GNU] declarator attributes[opt]
2934/// declarator[opt] ':' constant-expression
2935/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2936///
Chris Lattnere1359422008-04-10 06:46:29 +00002937void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002938ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00002939
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002940 if (Tok.is(tok::kw___extension__)) {
2941 // __extension__ silences extension warnings in the subexpression.
2942 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002943 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002944 return ParseStructDeclaration(DS, Fields);
2945 }
Mike Stump1eb44332009-09-09 15:08:12 +00002946
Steve Naroff28a7ca82007-08-20 22:28:22 +00002947 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002948 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002949
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002950 // If there are no declarators, this is a free-standing declaration
2951 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002952 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002953 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2954 DS);
2955 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002956 return;
2957 }
2958
2959 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002960 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002961 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002962 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002963 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00002964 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002965
Bill Wendlingad017fa2012-12-20 19:22:21 +00002966 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002967 if (!FirstDeclarator)
2968 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002969
Steve Naroff28a7ca82007-08-20 22:28:22 +00002970 /// struct-declarator: declarator
2971 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002972 if (Tok.isNot(tok::colon)) {
2973 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2974 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002975 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002976 }
Mike Stump1eb44332009-09-09 15:08:12 +00002977
Chris Lattner04d66662007-10-09 17:33:22 +00002978 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002979 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002980 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002981 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002982 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002983 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002984 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002985 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002986
Steve Naroff28a7ca82007-08-20 22:28:22 +00002987 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002988 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002989
John McCallbdd563e2009-11-03 02:38:08 +00002990 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00002991 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00002992
Steve Naroff28a7ca82007-08-20 22:28:22 +00002993 // If we don't have a comma, it is either the end of the list (a ';')
2994 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002995 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002996 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002997
Steve Naroff28a7ca82007-08-20 22:28:22 +00002998 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002999 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003000
John McCallbdd563e2009-11-03 02:38:08 +00003001 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003002 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00003003}
3004
3005/// ParseStructUnionBody
3006/// struct-contents:
3007/// struct-declaration-list
3008/// [EXT] empty
3009/// [GNU] "struct-declaration-list" without terminatoring ';'
3010/// struct-declaration-list:
3011/// struct-declaration
3012/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003013/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00003014///
Reid Spencer5f016e22007-07-11 17:01:13 +00003015void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00003016 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00003017 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3018 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00003019
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003020 BalancedDelimiterTracker T(*this, tok::l_brace);
3021 if (T.consumeOpen())
3022 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003023
Douglas Gregor3218c4b2009-01-09 22:42:13 +00003024 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003025 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00003026
Reid Spencer5f016e22007-07-11 17:01:13 +00003027 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
3028 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00003029 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00003030 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3031 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3032 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003033
Chris Lattner5f9e2722011-07-23 10:55:15 +00003034 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00003035
Reid Spencer5f016e22007-07-11 17:01:13 +00003036 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00003037 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003038 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003039
Reid Spencer5f016e22007-07-11 17:01:13 +00003040 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003041 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003042 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003043 continue;
3044 }
Chris Lattnere1359422008-04-10 06:46:29 +00003045
John McCallbdd563e2009-11-03 02:38:08 +00003046 if (!Tok.is(tok::at)) {
3047 struct CFieldCallback : FieldCallback {
3048 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003049 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003050 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003051
John McCalld226f652010-08-21 09:40:31 +00003052 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003053 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003054 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3055
Eli Friedmandcdff462012-08-08 23:53:27 +00003056 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00003057 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003058 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003059 FD.D.getDeclSpec().getSourceRange().getBegin(),
3060 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003061 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003062 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003063 }
John McCallbdd563e2009-11-03 02:38:08 +00003064 } Callback(*this, TagDecl, FieldDecls);
3065
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003066 // Parse all the comma separated declarators.
3067 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003068 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003069 } else { // Handle @defs
3070 ConsumeToken();
3071 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3072 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003073 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003074 continue;
3075 }
3076 ConsumeToken();
3077 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3078 if (!Tok.is(tok::identifier)) {
3079 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003080 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003081 continue;
3082 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003083 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003084 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003085 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003086 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3087 ConsumeToken();
3088 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003089 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003090
Chris Lattner04d66662007-10-09 17:33:22 +00003091 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003092 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003093 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003094 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003095 break;
3096 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003097 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3098 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003099 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003100 // If we stopped at a ';', eat it.
3101 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003102 }
3103 }
Mike Stump1eb44332009-09-09 15:08:12 +00003104
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003105 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003106
John McCall0b7e6782011-03-24 11:26:52 +00003107 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003108 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003109 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003110
Douglas Gregor23c94db2010-07-02 17:43:08 +00003111 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003112 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003113 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003114 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003115 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003116 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3117 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003118}
3119
Reid Spencer5f016e22007-07-11 17:01:13 +00003120/// ParseEnumSpecifier
3121/// enum-specifier: [C99 6.7.2.2]
3122/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003123///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003124/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3125/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003126/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3127/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003128/// 'enum' identifier
3129/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003130///
Richard Smith1af83c42012-03-23 03:33:32 +00003131/// [C++11] enum-head '{' enumerator-list[opt] '}'
3132/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003133///
Richard Smith1af83c42012-03-23 03:33:32 +00003134/// enum-head: [C++11]
3135/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3136/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3137/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003138///
Richard Smith1af83c42012-03-23 03:33:32 +00003139/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003140/// 'enum'
3141/// 'enum' 'class'
3142/// 'enum' 'struct'
3143///
Richard Smith1af83c42012-03-23 03:33:32 +00003144/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003145/// ':' type-specifier-seq
3146///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003147/// [C++] elaborated-type-specifier:
3148/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3149///
Chris Lattner4c97d762009-04-12 21:49:30 +00003150void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003151 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003152 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003153 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003154 if (Tok.is(tok::code_completion)) {
3155 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003156 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003157 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003158 }
John McCall57c13002011-07-06 05:58:41 +00003159
Sean Hunt2edf0a22012-06-23 05:07:58 +00003160 // If attributes exist after tag, parse them.
3161 ParsedAttributesWithRange attrs(AttrFactory);
3162 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003163 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003164
3165 // If declspecs exist after tag, parse them.
3166 while (Tok.is(tok::kw___declspec))
3167 ParseMicrosoftDeclSpec(attrs);
3168
Richard Smithbdad7a22012-01-10 01:33:14 +00003169 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003170 bool IsScopedUsingClassTag = false;
3171
John McCall1e12b3d2012-06-23 22:30:04 +00003172 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Smith80ad52f2013-01-02 11:42:31 +00003173 if (getLangOpts().CPlusPlus11 &&
John McCall57c13002011-07-06 05:58:41 +00003174 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00003175 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003176 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003177 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003178
Bill Wendlingad017fa2012-12-20 19:22:21 +00003179 // Attributes are not allowed between these keywords. Diagnose,
John McCall1e12b3d2012-06-23 22:30:04 +00003180 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003181 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003182
3183 // They are allowed afterwards, though.
3184 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003185 MaybeParseCXX11Attributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003186 while (Tok.is(tok::kw___declspec))
3187 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003188 }
Richard Smith1af83c42012-03-23 03:33:32 +00003189
John McCall13489672012-05-07 06:16:58 +00003190 // C++11 [temp.explicit]p12:
3191 // The usual access controls do not apply to names used to specify
3192 // explicit instantiations.
3193 // We extend this to also cover explicit specializations. Note that
3194 // we don't suppress if this turns out to be an elaborated type
3195 // specifier.
3196 bool shouldDelayDiagsInTag =
3197 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3198 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3199 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003200
Richard Smith7796eb52012-03-12 08:56:40 +00003201 // Enum definitions should not be parsed in a trailing-return-type.
3202 bool AllowDeclaration = DSC != DSC_trailing;
3203
3204 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith80ad52f2013-01-02 11:42:31 +00003205 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smith7796eb52012-03-12 08:56:40 +00003206 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003207
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003208 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003209 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003210 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3211 // if a fixed underlying type is allowed.
3212 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003213
3214 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003215 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00003216 return;
3217
3218 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003219 Diag(Tok, diag::err_expected_ident);
3220 if (Tok.isNot(tok::l_brace)) {
3221 // Has no name and is not a definition.
3222 // Skip the rest of this declarator, up until the comma or semicolon.
3223 SkipUntil(tok::comma, true);
3224 return;
3225 }
3226 }
3227 }
Mike Stump1eb44332009-09-09 15:08:12 +00003228
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003229 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003230 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003231 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003232 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003233
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003234 // Skip the rest of this declarator, up until the comma or semicolon.
3235 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003236 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003237 }
Mike Stump1eb44332009-09-09 15:08:12 +00003238
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003239 // If an identifier is present, consume and remember it.
3240 IdentifierInfo *Name = 0;
3241 SourceLocation NameLoc;
3242 if (Tok.is(tok::identifier)) {
3243 Name = Tok.getIdentifierInfo();
3244 NameLoc = ConsumeToken();
3245 }
Mike Stump1eb44332009-09-09 15:08:12 +00003246
Richard Smithbdad7a22012-01-10 01:33:14 +00003247 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003248 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3249 // declaration of a scoped enumeration.
3250 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003251 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003252 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003253 }
3254
John McCall13489672012-05-07 06:16:58 +00003255 // Okay, end the suppression area. We'll decide whether to emit the
3256 // diagnostics in a second.
3257 if (shouldDelayDiagsInTag)
3258 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003259
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003260 TypeResult BaseType;
3261
Douglas Gregora61b3e72010-12-01 17:42:47 +00003262 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003263 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003264 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003265 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003266 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003267 // If we're in class scope, this can either be an enum declaration with
3268 // an underlying type, or a declaration of a bitfield member. We try to
3269 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003270 // (integer literal, sizeof); if it's still ambiguous, we then consider
3271 // anything that's a simple-type-specifier followed by '(' as an
3272 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003273 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003274 EnterExpressionEvaluationContext Unevaluated(Actions,
3275 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003276 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003277 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003278 // bit-field. This is the common case.
3279 if (TPR == TPResult::True())
3280 PossibleBitfield = true;
3281 // If the next token starts a type-specifier-seq, it may be either a
3282 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003283 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003284 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003285 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003286 GetLookAheadToken(2).getKind() == tok::semi) {
3287 // Consume the ':'.
3288 ConsumeToken();
3289 } else {
3290 // We have the start of a type-specifier-seq, so we have to perform
3291 // tentative parsing to determine whether we have an expression or a
3292 // type.
3293 TentativeParsingAction TPA(*this);
3294
3295 // Consume the ':'.
3296 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003297
3298 // If we see a type specifier followed by an open-brace, we have an
3299 // ambiguity between an underlying type and a C++11 braced
3300 // function-style cast. Resolve this by always treating it as an
3301 // underlying type.
3302 // FIXME: The standard is not entirely clear on how to disambiguate in
3303 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003304 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003305 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003306 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003307 // We'll parse this as a bitfield later.
3308 PossibleBitfield = true;
3309 TPA.Revert();
3310 } else {
3311 // We have a type-specifier-seq.
3312 TPA.Commit();
3313 }
3314 }
3315 } else {
3316 // Consume the ':'.
3317 ConsumeToken();
3318 }
3319
3320 if (!PossibleBitfield) {
3321 SourceRange Range;
3322 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003323
Richard Smith80ad52f2013-01-02 11:42:31 +00003324 if (getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00003325 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003326 } else if (!getLangOpts().ObjC2) {
3327 if (getLangOpts().CPlusPlus)
3328 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3329 else
3330 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3331 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003332 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003333 }
3334
Richard Smithbdad7a22012-01-10 01:33:14 +00003335 // There are four options here. If we have 'friend enum foo;' then this is a
3336 // friend declaration, and cannot have an accompanying definition. If we have
3337 // 'enum foo;', then this is a forward declaration. If we have
3338 // 'enum foo {...' then this is a definition. Otherwise we have something
3339 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003340 //
3341 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3342 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3343 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3344 //
John McCallf312b1e2010-08-26 23:41:50 +00003345 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003346 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003347 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003348 } else if (Tok.is(tok::l_brace)) {
3349 if (DS.isFriendSpecified()) {
3350 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3351 << SourceRange(DS.getFriendSpecLoc());
3352 ConsumeBrace();
3353 SkipUntil(tok::r_brace);
3354 TUK = Sema::TUK_Friend;
3355 } else {
3356 TUK = Sema::TUK_Definition;
3357 }
Richard Smithc9f35172012-06-25 21:37:02 +00003358 } else if (DSC != DSC_type_specifier &&
3359 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003360 (Tok.isAtStartOfLine() &&
3361 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003362 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3363 if (Tok.isNot(tok::semi)) {
3364 // A semicolon was missing after this declaration. Diagnose and recover.
3365 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3366 "enum");
3367 PP.EnterToken(Tok);
3368 Tok.setKind(tok::semi);
3369 }
John McCall13489672012-05-07 06:16:58 +00003370 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003371 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003372 }
3373
3374 // If this is an elaborated type specifier, and we delayed
3375 // diagnostics before, just merge them into the current pool.
3376 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3377 diagsFromTag.redelay();
3378 }
Richard Smith1af83c42012-03-23 03:33:32 +00003379
3380 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003381 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003382 TUK != Sema::TUK_Reference) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003383 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith1af83c42012-03-23 03:33:32 +00003384 // Skip the rest of this declarator, up until the comma or semicolon.
3385 Diag(Tok, diag::err_enum_template);
3386 SkipUntil(tok::comma, true);
3387 return;
3388 }
3389
3390 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3391 // Enumerations can't be explicitly instantiated.
3392 DS.SetTypeSpecError();
3393 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3394 return;
3395 }
3396
3397 assert(TemplateInfo.TemplateParams && "no template parameters");
3398 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3399 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003400 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003401
Sean Hunt2edf0a22012-06-23 05:07:58 +00003402 if (TUK == Sema::TUK_Reference)
3403 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003404
Douglas Gregorb9075602011-02-22 02:55:24 +00003405 if (!Name && TUK != Sema::TUK_Definition) {
3406 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003407
Douglas Gregorb9075602011-02-22 02:55:24 +00003408 // Skip the rest of this declarator, up until the comma or semicolon.
3409 SkipUntil(tok::comma, true);
3410 return;
3411 }
Richard Smith1af83c42012-03-23 03:33:32 +00003412
Douglas Gregor402abb52009-05-28 23:31:59 +00003413 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003414 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003415 const char *PrevSpec = 0;
3416 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003417 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003418 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003419 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003420 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003421 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003422
Douglas Gregor48c89f42010-04-24 16:38:41 +00003423 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003424 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003425 // dependent tag.
3426 if (!Name) {
3427 DS.SetTypeSpecError();
3428 Diag(Tok, diag::err_expected_type_name_after_typename);
3429 return;
3430 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003431
Douglas Gregor23c94db2010-07-02 17:43:08 +00003432 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003433 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003434 NameLoc);
3435 if (Type.isInvalid()) {
3436 DS.SetTypeSpecError();
3437 return;
3438 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003439
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003440 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3441 NameLoc.isValid() ? NameLoc : StartLoc,
3442 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003443 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003444
Douglas Gregor48c89f42010-04-24 16:38:41 +00003445 return;
3446 }
Mike Stump1eb44332009-09-09 15:08:12 +00003447
John McCalld226f652010-08-21 09:40:31 +00003448 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003449 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003450 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003451 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003452 ConsumeBrace();
3453 SkipUntil(tok::r_brace);
3454 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003455
Douglas Gregor48c89f42010-04-24 16:38:41 +00003456 DS.SetTypeSpecError();
3457 return;
3458 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003459
Richard Smithc9f35172012-06-25 21:37:02 +00003460 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003461 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003462
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003463 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3464 NameLoc.isValid() ? NameLoc : StartLoc,
3465 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003466 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003467}
3468
3469/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3470/// enumerator-list:
3471/// enumerator
3472/// enumerator-list ',' enumerator
3473/// enumerator:
3474/// enumeration-constant
3475/// enumeration-constant '=' constant-expression
3476/// enumeration-constant:
3477/// identifier
3478///
John McCalld226f652010-08-21 09:40:31 +00003479void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003480 // Enter the scope of the enum body and start the definition.
3481 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003482 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003483
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003484 BalancedDelimiterTracker T(*this, tok::l_brace);
3485 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003486
Chris Lattner7946dd32007-08-27 17:24:30 +00003487 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003488 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003489 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003490
Chris Lattner5f9e2722011-07-23 10:55:15 +00003491 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003492
John McCalld226f652010-08-21 09:40:31 +00003493 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003494
Reid Spencer5f016e22007-07-11 17:01:13 +00003495 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003496 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003497 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3498 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003499
John McCall5b629aa2010-10-22 23:36:17 +00003500 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003501 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003502 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003503 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003504 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003505
Reid Spencer5f016e22007-07-11 17:01:13 +00003506 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003507 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003508 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003509
Chris Lattner04d66662007-10-09 17:33:22 +00003510 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003511 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003512 AssignedVal = ParseConstantExpression();
3513 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003514 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003515 }
Mike Stump1eb44332009-09-09 15:08:12 +00003516
Reid Spencer5f016e22007-07-11 17:01:13 +00003517 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003518 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3519 LastEnumConstDecl,
3520 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003521 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003522 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003523 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003524
Reid Spencer5f016e22007-07-11 17:01:13 +00003525 EnumConstantDecls.push_back(EnumConstDecl);
3526 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003527
Douglas Gregor751f6922010-09-07 14:51:08 +00003528 if (Tok.is(tok::identifier)) {
3529 // We're missing a comma between enumerators.
3530 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003531 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003532 << FixItHint::CreateInsertion(Loc, ", ");
3533 continue;
3534 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003535
Chris Lattner04d66662007-10-09 17:33:22 +00003536 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003537 break;
3538 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003539
Richard Smith7fe62082011-10-15 05:09:34 +00003540 if (Tok.isNot(tok::identifier)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003541 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003542 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3543 diag::ext_enumerator_list_comma_cxx :
3544 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003545 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith80ad52f2013-01-02 11:42:31 +00003546 else if (getLangOpts().CPlusPlus11)
Richard Smith7fe62082011-10-15 05:09:34 +00003547 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3548 << FixItHint::CreateRemoval(CommaLoc);
3549 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003550 }
Mike Stump1eb44332009-09-09 15:08:12 +00003551
Reid Spencer5f016e22007-07-11 17:01:13 +00003552 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003553 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003554
Reid Spencer5f016e22007-07-11 17:01:13 +00003555 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003556 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003557 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003558
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003559 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3560 EnumDecl, EnumConstantDecls.data(),
3561 EnumConstantDecls.size(), getCurScope(),
3562 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003563
Douglas Gregor72de6672009-01-08 20:45:30 +00003564 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003565 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3566 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003567
3568 // The next token must be valid after an enum definition. If not, a ';'
3569 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003570 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3571 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003572 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3573 // Push this token back into the preprocessor and change our current token
3574 // to ';' so that the rest of the code recovers as though there were an
3575 // ';' after the definition.
3576 PP.EnterToken(Tok);
3577 Tok.setKind(tok::semi);
3578 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003579}
3580
3581/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003582/// start of a type-qualifier-list.
3583bool Parser::isTypeQualifier() const {
3584 switch (Tok.getKind()) {
3585 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003586
3587 // type-qualifier only in OpenCL
3588 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003589 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003590
Steve Naroff5f8aa692008-02-11 23:15:56 +00003591 // type-qualifier
3592 case tok::kw_const:
3593 case tok::kw_volatile:
3594 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003595 case tok::kw___private:
3596 case tok::kw___local:
3597 case tok::kw___global:
3598 case tok::kw___constant:
3599 case tok::kw___read_only:
3600 case tok::kw___read_write:
3601 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003602 return true;
3603 }
3604}
3605
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003606/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3607/// is definitely a type-specifier. Return false if it isn't part of a type
3608/// specifier or if we're not sure.
3609bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3610 switch (Tok.getKind()) {
3611 default: return false;
3612 // type-specifiers
3613 case tok::kw_short:
3614 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003615 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003616 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003617 case tok::kw_signed:
3618 case tok::kw_unsigned:
3619 case tok::kw__Complex:
3620 case tok::kw__Imaginary:
3621 case tok::kw_void:
3622 case tok::kw_char:
3623 case tok::kw_wchar_t:
3624 case tok::kw_char16_t:
3625 case tok::kw_char32_t:
3626 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003627 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003628 case tok::kw_float:
3629 case tok::kw_double:
3630 case tok::kw_bool:
3631 case tok::kw__Bool:
3632 case tok::kw__Decimal32:
3633 case tok::kw__Decimal64:
3634 case tok::kw__Decimal128:
3635 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003636
Guy Benyeib13621d2012-12-18 14:38:23 +00003637 // OpenCL specific types:
3638 case tok::kw_image1d_t:
3639 case tok::kw_image1d_array_t:
3640 case tok::kw_image1d_buffer_t:
3641 case tok::kw_image2d_t:
3642 case tok::kw_image2d_array_t:
3643 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003644 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003645 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003646
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003647 // struct-or-union-specifier (C99) or class-specifier (C++)
3648 case tok::kw_class:
3649 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003650 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003651 case tok::kw_union:
3652 // enum-specifier
3653 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003654
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003655 // typedef-name
3656 case tok::annot_typename:
3657 return true;
3658 }
3659}
3660
Steve Naroff5f8aa692008-02-11 23:15:56 +00003661/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003662/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003663bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003664 switch (Tok.getKind()) {
3665 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003666
Chris Lattner166a8fc2009-01-04 23:41:41 +00003667 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003668 if (TryAltiVecVectorToken())
3669 return true;
3670 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003671 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003672 // Annotate typenames and C++ scope specifiers. If we get one, just
3673 // recurse to handle whatever we get.
3674 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003675 return true;
3676 if (Tok.is(tok::identifier))
3677 return false;
3678 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003679
Chris Lattner166a8fc2009-01-04 23:41:41 +00003680 case tok::coloncolon: // ::foo::bar
3681 if (NextToken().is(tok::kw_new) || // ::new
3682 NextToken().is(tok::kw_delete)) // ::delete
3683 return false;
3684
Chris Lattner166a8fc2009-01-04 23:41:41 +00003685 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003686 return true;
3687 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003688
Reid Spencer5f016e22007-07-11 17:01:13 +00003689 // GNU attributes support.
3690 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003691 // GNU typeof support.
3692 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003693
Reid Spencer5f016e22007-07-11 17:01:13 +00003694 // type-specifiers
3695 case tok::kw_short:
3696 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003697 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003698 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003699 case tok::kw_signed:
3700 case tok::kw_unsigned:
3701 case tok::kw__Complex:
3702 case tok::kw__Imaginary:
3703 case tok::kw_void:
3704 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003705 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003706 case tok::kw_char16_t:
3707 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003708 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003709 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003710 case tok::kw_float:
3711 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003712 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003713 case tok::kw__Bool:
3714 case tok::kw__Decimal32:
3715 case tok::kw__Decimal64:
3716 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003717 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003718
Guy Benyeib13621d2012-12-18 14:38:23 +00003719 // OpenCL specific types:
3720 case tok::kw_image1d_t:
3721 case tok::kw_image1d_array_t:
3722 case tok::kw_image1d_buffer_t:
3723 case tok::kw_image2d_t:
3724 case tok::kw_image2d_array_t:
3725 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003726 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003727 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003728
Chris Lattner99dc9142008-04-13 18:59:07 +00003729 // struct-or-union-specifier (C99) or class-specifier (C++)
3730 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003731 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003732 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003733 case tok::kw_union:
3734 // enum-specifier
3735 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003736
Reid Spencer5f016e22007-07-11 17:01:13 +00003737 // type-qualifier
3738 case tok::kw_const:
3739 case tok::kw_volatile:
3740 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003741
John McCallb8a8de32012-11-14 00:49:39 +00003742 // Debugger support.
3743 case tok::kw___unknown_anytype:
3744
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003745 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003746 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003747 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003748
Chris Lattner7c186be2008-10-20 00:25:30 +00003749 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3750 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003751 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003752
Steve Naroff239f0732008-12-25 14:16:32 +00003753 case tok::kw___cdecl:
3754 case tok::kw___stdcall:
3755 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003756 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003757 case tok::kw___w64:
3758 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003759 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003760 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003761 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003762
3763 case tok::kw___private:
3764 case tok::kw___local:
3765 case tok::kw___global:
3766 case tok::kw___constant:
3767 case tok::kw___read_only:
3768 case tok::kw___read_write:
3769 case tok::kw___write_only:
3770
Eli Friedman290eeb02009-06-08 23:27:34 +00003771 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003772
3773 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003774 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003775
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003776 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003777 case tok::kw__Atomic:
3778 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003779 }
3780}
3781
3782/// isDeclarationSpecifier() - Return true if the current token is part of a
3783/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003784///
3785/// \param DisambiguatingWithExpression True to indicate that the purpose of
3786/// this check is to disambiguate between an expression and a declaration.
3787bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003788 switch (Tok.getKind()) {
3789 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003790
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003791 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003792 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003793
Chris Lattner166a8fc2009-01-04 23:41:41 +00003794 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003795 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003796 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003797 return false;
John Thompson82287d12010-02-05 00:12:22 +00003798 if (TryAltiVecVectorToken())
3799 return true;
3800 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003801 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003802 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003803 // Annotate typenames and C++ scope specifiers. If we get one, just
3804 // recurse to handle whatever we get.
3805 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003806 return true;
3807 if (Tok.is(tok::identifier))
3808 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003809
Douglas Gregor9497a732010-09-16 01:51:54 +00003810 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00003811 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00003812 // expression is permitted, then this is probably a class message send
3813 // missing the initial '['. In this case, we won't consider this to be
3814 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00003815 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00003816 isStartOfObjCClassMessageMissingOpenBracket())
3817 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003818
John McCall9ba61662010-02-26 08:45:28 +00003819 return isDeclarationSpecifier();
3820
Chris Lattner166a8fc2009-01-04 23:41:41 +00003821 case tok::coloncolon: // ::foo::bar
3822 if (NextToken().is(tok::kw_new) || // ::new
3823 NextToken().is(tok::kw_delete)) // ::delete
3824 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003825
Chris Lattner166a8fc2009-01-04 23:41:41 +00003826 // Annotate typenames and C++ scope specifiers. If we get one, just
3827 // recurse to handle whatever we get.
3828 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003829 return true;
3830 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003831
Reid Spencer5f016e22007-07-11 17:01:13 +00003832 // storage-class-specifier
3833 case tok::kw_typedef:
3834 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003835 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003836 case tok::kw_static:
3837 case tok::kw_auto:
3838 case tok::kw_register:
3839 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003840
Douglas Gregor8d267c52011-09-09 02:06:17 +00003841 // Modules
3842 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00003843
John McCallb8a8de32012-11-14 00:49:39 +00003844 // Debugger support
3845 case tok::kw___unknown_anytype:
3846
Reid Spencer5f016e22007-07-11 17:01:13 +00003847 // type-specifiers
3848 case tok::kw_short:
3849 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003850 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003851 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003852 case tok::kw_signed:
3853 case tok::kw_unsigned:
3854 case tok::kw__Complex:
3855 case tok::kw__Imaginary:
3856 case tok::kw_void:
3857 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003858 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003859 case tok::kw_char16_t:
3860 case tok::kw_char32_t:
3861
Reid Spencer5f016e22007-07-11 17:01:13 +00003862 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003863 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003864 case tok::kw_float:
3865 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003866 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003867 case tok::kw__Bool:
3868 case tok::kw__Decimal32:
3869 case tok::kw__Decimal64:
3870 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003871 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003872
Guy Benyeib13621d2012-12-18 14:38:23 +00003873 // OpenCL specific types:
3874 case tok::kw_image1d_t:
3875 case tok::kw_image1d_array_t:
3876 case tok::kw_image1d_buffer_t:
3877 case tok::kw_image2d_t:
3878 case tok::kw_image2d_array_t:
3879 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003880 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003881 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003882
Chris Lattner99dc9142008-04-13 18:59:07 +00003883 // struct-or-union-specifier (C99) or class-specifier (C++)
3884 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003885 case tok::kw_struct:
3886 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00003887 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003888 // enum-specifier
3889 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003890
Reid Spencer5f016e22007-07-11 17:01:13 +00003891 // type-qualifier
3892 case tok::kw_const:
3893 case tok::kw_volatile:
3894 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003895
Reid Spencer5f016e22007-07-11 17:01:13 +00003896 // function-specifier
3897 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003898 case tok::kw_virtual:
3899 case tok::kw_explicit:
Richard Smithde03c152013-01-17 22:16:11 +00003900 case tok::kw__Noreturn:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003901
Richard Smith4cd81c52013-01-29 09:02:09 +00003902 // alignment-specifier
3903 case tok::kw__Alignas:
3904
Richard Smith53aec2a2012-10-25 00:00:53 +00003905 // friend keyword.
3906 case tok::kw_friend:
3907
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003908 // static_assert-declaration
3909 case tok::kw__Static_assert:
3910
Chris Lattner1ef08762007-08-09 17:01:07 +00003911 // GNU typeof support.
3912 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003913
Chris Lattner1ef08762007-08-09 17:01:07 +00003914 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003915 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00003916
Richard Smith53aec2a2012-10-25 00:00:53 +00003917 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003918 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00003919 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00003920
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003921 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003922 case tok::kw__Atomic:
3923 return true;
3924
Chris Lattnerf3948c42008-07-26 03:38:44 +00003925 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3926 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003927 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003928
Douglas Gregord9d75e52011-04-27 05:41:15 +00003929 // typedef-name
3930 case tok::annot_typename:
3931 return !DisambiguatingWithExpression ||
3932 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00003933
Steve Naroff47f52092009-01-06 19:34:12 +00003934 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003935 case tok::kw___cdecl:
3936 case tok::kw___stdcall:
3937 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003938 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003939 case tok::kw___w64:
3940 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003941 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003942 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003943 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003944 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003945
3946 case tok::kw___private:
3947 case tok::kw___local:
3948 case tok::kw___global:
3949 case tok::kw___constant:
3950 case tok::kw___read_only:
3951 case tok::kw___read_write:
3952 case tok::kw___write_only:
3953
Eli Friedman290eeb02009-06-08 23:27:34 +00003954 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003955 }
3956}
3957
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003958bool Parser::isConstructorDeclarator() {
3959 TentativeParsingAction TPA(*this);
3960
3961 // Parse the C++ scope specifier.
3962 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00003963 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003964 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003965 TPA.Revert();
3966 return false;
3967 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003968
3969 // Parse the constructor name.
3970 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3971 // We already know that we have a constructor name; just consume
3972 // the token.
3973 ConsumeToken();
3974 } else {
3975 TPA.Revert();
3976 return false;
3977 }
3978
Richard Smith22592862012-03-27 23:05:05 +00003979 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003980 if (Tok.isNot(tok::l_paren)) {
3981 TPA.Revert();
3982 return false;
3983 }
3984 ConsumeParen();
3985
Richard Smith22592862012-03-27 23:05:05 +00003986 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3987 // that we have a constructor.
3988 if (Tok.is(tok::r_paren) ||
3989 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003990 TPA.Revert();
3991 return true;
3992 }
3993
3994 // If we need to, enter the specified scope.
3995 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003996 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003997 DeclScopeObj.EnterDeclaratorScope();
3998
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003999 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00004000 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004001 MaybeParseMicrosoftAttributes(Attrs);
4002
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004003 // Check whether the next token(s) are part of a declaration
4004 // specifier, in which case we have the start of a parameter and,
4005 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00004006 bool IsConstructor = false;
4007 if (isDeclarationSpecifier())
4008 IsConstructor = true;
4009 else if (Tok.is(tok::identifier) ||
4010 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4011 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4012 // This might be a parenthesized member name, but is more likely to
4013 // be a constructor declaration with an invalid argument type. Keep
4014 // looking.
4015 if (Tok.is(tok::annot_cxxscope))
4016 ConsumeToken();
4017 ConsumeToken();
4018
4019 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00004020 // which must have one of the following syntactic forms (see the
4021 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00004022 switch (Tok.getKind()) {
4023 case tok::l_paren:
4024 // C(X ( int));
4025 case tok::l_square:
4026 // C(X [ 5]);
4027 // C(X [ [attribute]]);
4028 case tok::coloncolon:
4029 // C(X :: Y);
4030 // C(X :: *p);
4031 case tok::r_paren:
4032 // C(X )
4033 // Assume this isn't a constructor, rather than assuming it's a
4034 // constructor with an unnamed parameter of an ill-formed type.
4035 break;
4036
4037 default:
4038 IsConstructor = true;
4039 break;
4040 }
4041 }
4042
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004043 TPA.Revert();
4044 return IsConstructor;
4045}
Reid Spencer5f016e22007-07-11 17:01:13 +00004046
4047/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00004048/// type-qualifier-list: [C99 6.7.5]
4049/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004050/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004051/// [ only if VendorAttributesAllowed=true ]
4052/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004053/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004054/// [ only if VendorAttributesAllowed=true ]
4055/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith4e24f0f2013-01-02 12:01:23 +00004056/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik52fc3142010-09-03 01:29:35 +00004057/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00004058///
Dawn Perchik52fc3142010-09-03 01:29:35 +00004059void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4060 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00004061 bool CXX11AttributesAllowed) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004062 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00004063 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00004064 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00004065 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004066 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004067 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004068
4069 SourceLocation EndLoc;
4070
Reid Spencer5f016e22007-07-11 17:01:13 +00004071 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004072 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004073 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004074 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004075 SourceLocation Loc = Tok.getLocation();
4076
4077 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004078 case tok::code_completion:
4079 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004080 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004081
Reid Spencer5f016e22007-07-11 17:01:13 +00004082 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004083 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004084 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004085 break;
4086 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004087 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004088 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004089 break;
4090 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004091 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004092 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004093 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004094
4095 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00004096 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004097 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004098 goto DoneWithTypeQuals;
4099 case tok::kw___private:
4100 case tok::kw___global:
4101 case tok::kw___local:
4102 case tok::kw___constant:
4103 case tok::kw___read_only:
4104 case tok::kw___write_only:
4105 case tok::kw___read_write:
4106 ParseOpenCLQualifiers(DS);
4107 break;
4108
Eli Friedman290eeb02009-06-08 23:27:34 +00004109 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004110 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004111 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004112 case tok::kw___cdecl:
4113 case tok::kw___stdcall:
4114 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004115 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004116 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004117 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004118 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004119 continue;
4120 }
4121 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004122 case tok::kw___pascal:
4123 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004124 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004125 continue;
4126 }
4127 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004128 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004129 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004130 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004131 continue; // do *not* consume the next token!
4132 }
4133 // otherwise, FALL THROUGH!
4134 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004135 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004136 // If this is not a type-qualifier token, we're done reading type
4137 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004138 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004139 if (EndLoc.isValid())
4140 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004141 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004142 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004143
Reid Spencer5f016e22007-07-11 17:01:13 +00004144 // If the specifier combination wasn't legal, issue a diagnostic.
4145 if (isInvalid) {
4146 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004147 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004148 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004149 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004150 }
4151}
4152
4153
4154/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4155///
4156void Parser::ParseDeclarator(Declarator &D) {
4157 /// This implements the 'declarator' production in the C grammar, then checks
4158 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004159 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004160}
4161
Richard Smith9988f282012-03-29 01:16:42 +00004162static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4163 if (Kind == tok::star || Kind == tok::caret)
4164 return true;
4165
4166 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4167 if (!Lang.CPlusPlus)
4168 return false;
4169
4170 return Kind == tok::amp || Kind == tok::ampamp;
4171}
4172
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004173/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4174/// is parsed by the function passed to it. Pass null, and the direct-declarator
4175/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004176/// ptr-operator production.
4177///
Richard Smith0706df42011-10-19 21:33:05 +00004178/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004179/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4180/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004181///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004182/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4183/// [C] pointer[opt] direct-declarator
4184/// [C++] direct-declarator
4185/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004186///
4187/// pointer: [C99 6.7.5]
4188/// '*' type-qualifier-list[opt]
4189/// '*' type-qualifier-list[opt] pointer
4190///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004191/// ptr-operator:
4192/// '*' cv-qualifier-seq[opt]
4193/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004194/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004195/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004196/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004197/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004198void Parser::ParseDeclaratorInternal(Declarator &D,
4199 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004200 if (Diags.hasAllExtensionsSilenced())
4201 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004202
Sebastian Redlf30208a2009-01-24 21:16:55 +00004203 // C++ member pointers start with a '::' or a nested-name.
4204 // Member pointers get special handling, since there's no place for the
4205 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004206 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004207 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4208 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004209 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4210 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004211 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004212 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004213
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004214 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004215 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004216 // The scope spec really belongs to the direct-declarator.
Richard Smith6a502c42013-01-08 22:43:49 +00004217 if (D.mayHaveIdentifier())
4218 D.getCXXScopeSpec() = SS;
4219 else
4220 AnnotateScopeToken(SS, true);
4221
Sebastian Redlf30208a2009-01-24 21:16:55 +00004222 if (DirectDeclParser)
4223 (this->*DirectDeclParser)(D);
4224 return;
4225 }
4226
4227 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004228 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004229 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004230 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004231 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004232
4233 // Recurse to parse whatever is left.
4234 ParseDeclaratorInternal(D, DirectDeclParser);
4235
4236 // Sema will have to catch (syntactically invalid) pointers into global
4237 // scope. It has to catch pointers into namespace scope anyway.
4238 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004239 Loc),
4240 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004241 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004242 return;
4243 }
4244 }
4245
4246 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004247 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004248 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004249 if (DirectDeclParser)
4250 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004251 return;
4252 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004253
Sebastian Redl05532f22009-03-15 22:02:01 +00004254 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4255 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004256 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004257 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004258
Chris Lattner9af55002009-03-27 04:18:06 +00004259 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004260 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004261 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004262
Richard Smith6ee326a2012-04-10 01:32:12 +00004263 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004264 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004265 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004266
Reid Spencer5f016e22007-07-11 17:01:13 +00004267 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004268 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004269 if (Kind == tok::star)
4270 // Remember that we parsed a pointer type, and remember the type-quals.
4271 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004272 DS.getConstSpecLoc(),
4273 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004274 DS.getRestrictSpecLoc()),
4275 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004276 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004277 else
4278 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004279 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004280 Loc),
4281 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004282 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004283 } else {
4284 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004285 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004286
Sebastian Redl743de1f2009-03-23 00:00:23 +00004287 // Complain about rvalue references in C++03, but then go on and build
4288 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004289 if (Kind == tok::ampamp)
Richard Smith80ad52f2013-01-02 11:42:31 +00004290 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004291 diag::warn_cxx98_compat_rvalue_reference :
4292 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004293
Richard Smith6ee326a2012-04-10 01:32:12 +00004294 // GNU-style and C++11 attributes are allowed here, as is restrict.
4295 ParseTypeQualifierListOpt(DS);
4296 D.ExtendWithDeclSpec(DS);
4297
Reid Spencer5f016e22007-07-11 17:01:13 +00004298 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4299 // cv-qualifiers are introduced through the use of a typedef or of a
4300 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004301 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4302 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4303 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004304 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004305 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4306 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004307 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00004308 }
4309
4310 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004311 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004312
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004313 if (D.getNumTypeObjects() > 0) {
4314 // C++ [dcl.ref]p4: There shall be no references to references.
4315 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4316 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004317 if (const IdentifierInfo *II = D.getIdentifier())
4318 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4319 << II;
4320 else
4321 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4322 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004323
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004324 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004325 // can go ahead and build the (technically ill-formed)
4326 // declarator: reference collapsing will take care of it.
4327 }
4328 }
4329
Reid Spencer5f016e22007-07-11 17:01:13 +00004330 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00004331 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004332 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004333 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004334 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004335 }
4336}
4337
Richard Smith9988f282012-03-29 01:16:42 +00004338static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4339 SourceLocation EllipsisLoc) {
4340 if (EllipsisLoc.isValid()) {
4341 FixItHint Insertion;
4342 if (!D.getEllipsisLoc().isValid()) {
4343 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4344 D.setEllipsisLoc(EllipsisLoc);
4345 }
4346 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4347 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4348 }
4349}
4350
Reid Spencer5f016e22007-07-11 17:01:13 +00004351/// ParseDirectDeclarator
4352/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004353/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004354/// '(' declarator ')'
4355/// [GNU] '(' attributes declarator ')'
4356/// [C90] direct-declarator '[' constant-expression[opt] ']'
4357/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4358/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4359/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4360/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004361/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4362/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004363/// direct-declarator '(' parameter-type-list ')'
4364/// direct-declarator '(' identifier-list[opt] ')'
4365/// [GNU] direct-declarator '(' parameter-forward-declarations
4366/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004367/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4368/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004369/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4370/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4371/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004372/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004373/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004374///
4375/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004376/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004377/// '::'[opt] nested-name-specifier[opt] type-name
4378///
4379/// id-expression: [C++ 5.1]
4380/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004381/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004382///
4383/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004384/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004385/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004386/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004387/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004388/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004389///
Richard Smith5d8388c2012-03-27 01:42:32 +00004390/// Note, any additional constructs added here may need corresponding changes
4391/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004392void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004393 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004394
David Blaikie4e4d0842012-03-11 07:00:24 +00004395 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004396 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004397 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004398 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4399 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004400 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004401 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004402 }
4403
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004404 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004405 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004406 // Change the declaration context for name lookup, until this function
4407 // is exited (and the declarator has been parsed).
4408 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004409 }
4410
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004411 // C++0x [dcl.fct]p14:
4412 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004413 // of a parameter-declaration-clause without a preceding comma. In
4414 // this case, the ellipsis is parsed as part of the
4415 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004416 // parameter pack that has not been expanded; otherwise, it is parsed
4417 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004418 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004419 !((D.getContext() == Declarator::PrototypeContext ||
4420 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004421 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00004422 !Actions.containsUnexpandedParameterPacks(D))) {
4423 SourceLocation EllipsisLoc = ConsumeToken();
4424 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4425 // The ellipsis was put in the wrong place. Recover, and explain to
4426 // the user what they should have done.
4427 ParseDeclarator(D);
4428 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4429 return;
4430 } else
4431 D.setEllipsisLoc(EllipsisLoc);
4432
4433 // The ellipsis can't be followed by a parenthesized declarator. We
4434 // check for that in ParseParenDeclarator, after we have disambiguated
4435 // the l_paren token.
4436 }
4437
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004438 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4439 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4440 // We found something that indicates the start of an unqualified-id.
4441 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004442 bool AllowConstructorName;
4443 if (D.getDeclSpec().hasTypeSpecifier())
4444 AllowConstructorName = false;
4445 else if (D.getCXXScopeSpec().isSet())
4446 AllowConstructorName =
4447 (D.getContext() == Declarator::FileContext ||
4448 (D.getContext() == Declarator::MemberContext &&
4449 D.getDeclSpec().isFriendSpecified()));
4450 else
4451 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4452
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004453 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004454 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4455 /*EnteringContext=*/true,
4456 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004457 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004458 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004459 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004460 D.getName()) ||
4461 // Once we're past the identifier, if the scope was bad, mark the
4462 // whole declarator bad.
4463 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004464 D.SetIdentifier(0, Tok.getLocation());
4465 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004466 } else {
4467 // Parsed the unqualified-id; update range information and move along.
4468 if (D.getSourceRange().getBegin().isInvalid())
4469 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4470 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004471 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004472 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004473 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004474 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004475 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004476 "There's a C++-specific check for tok::identifier above");
4477 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4478 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4479 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004480 goto PastIdentifier;
4481 }
Richard Smith9988f282012-03-29 01:16:42 +00004482
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004483 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004484 // direct-declarator: '(' declarator ')'
4485 // direct-declarator: '(' attributes declarator ')'
4486 // Example: 'char (*X)' or 'int (*XX)(void)'
4487 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004488
4489 // If the declarator was parenthesized, we entered the declarator
4490 // scope when parsing the parenthesized declarator, then exited
4491 // the scope already. Re-enter the scope, if we need to.
4492 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004493 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004494 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004495 if (!D.isInvalidType() &&
4496 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004497 // Change the declaration context for name lookup, until this function
4498 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004499 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004500 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004501 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004502 // This could be something simple like "int" (in which case the declarator
4503 // portion is empty), if an abstract-declarator is allowed.
4504 D.SetIdentifier(0, Tok.getLocation());
4505 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004506 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004507 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004508 if (D.getContext() == Declarator::MemberContext)
4509 Diag(Tok, diag::err_expected_member_name_or_semi)
4510 << D.getDeclSpec().getSourceRange();
Richard Trieudb55c04c2013-01-26 02:31:38 +00004511 else if (getLangOpts().CPlusPlus) {
4512 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4513 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4514 else
4515 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4516 } else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004517 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004518 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004519 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004520 }
Mike Stump1eb44332009-09-09 15:08:12 +00004521
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004522 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004523 assert(D.isPastIdentifier() &&
4524 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004525
Richard Smith6ee326a2012-04-10 01:32:12 +00004526 // Don't parse attributes unless we have parsed an unparenthesized name.
4527 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith4e24f0f2013-01-02 12:01:23 +00004528 MaybeParseCXX11Attributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004529
Reid Spencer5f016e22007-07-11 17:01:13 +00004530 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004531 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004532 // Enter function-declaration scope, limiting any declarators to the
4533 // function prototype scope, including parameter declarators.
4534 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004535 Scope::FunctionPrototypeScope|Scope::DeclScope|
4536 (D.isFunctionDeclaratorAFunctionDeclaration()
4537 ? Scope::FunctionDeclarationScope : 0));
4538
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004539 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4540 // In such a case, check if we actually have a function declarator; if it
4541 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004542 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004543 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4544 // The name of the declarator, if any, is tentatively declared within
4545 // a possible direct initializer.
4546 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4547 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4548 TentativelyDeclaredIdentifiers.pop_back();
4549 if (!IsFunctionDecl)
4550 break;
4551 }
John McCall0b7e6782011-03-24 11:26:52 +00004552 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004553 BalancedDelimiterTracker T(*this, tok::l_paren);
4554 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004555 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004556 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004557 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004558 ParseBracketDeclarator(D);
4559 } else {
4560 break;
4561 }
4562 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004563}
Reid Spencer5f016e22007-07-11 17:01:13 +00004564
Chris Lattneref4715c2008-04-06 05:45:57 +00004565/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4566/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004567/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004568/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4569///
4570/// direct-declarator:
4571/// '(' declarator ')'
4572/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004573/// direct-declarator '(' parameter-type-list ')'
4574/// direct-declarator '(' identifier-list[opt] ')'
4575/// [GNU] direct-declarator '(' parameter-forward-declarations
4576/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004577///
4578void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004579 BalancedDelimiterTracker T(*this, tok::l_paren);
4580 T.consumeOpen();
4581
Chris Lattneref4715c2008-04-06 05:45:57 +00004582 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004583
Chris Lattner7399ee02008-10-20 02:05:46 +00004584 // Eat any attributes before we look at whether this is a grouping or function
4585 // declarator paren. If this is a grouping paren, the attribute applies to
4586 // the type being built up, for example:
4587 // int (__attribute__(()) *x)(long y)
4588 // If this ends up not being a grouping paren, the attribute applies to the
4589 // first argument, for example:
4590 // int (__attribute__(()) int x)
4591 // In either case, we need to eat any attributes to be able to determine what
4592 // sort of paren this is.
4593 //
John McCall0b7e6782011-03-24 11:26:52 +00004594 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004595 bool RequiresArg = false;
4596 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004597 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004598
Chris Lattner7399ee02008-10-20 02:05:46 +00004599 // We require that the argument list (if this is a non-grouping paren) be
4600 // present even if the attribute list was empty.
4601 RequiresArg = true;
4602 }
Chad Rosier9cab1c92012-12-21 21:22:20 +00004603
Steve Naroff239f0732008-12-25 14:16:32 +00004604 // Eat any Microsoft extensions.
Chad Rosier9cab1c92012-12-21 21:22:20 +00004605 ParseMicrosoftTypeAttributes(attrs);
4606
Dawn Perchik52fc3142010-09-03 01:29:35 +00004607 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004608 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004609 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004610
Chris Lattneref4715c2008-04-06 05:45:57 +00004611 // If we haven't past the identifier yet (or where the identifier would be
4612 // stored, if this is an abstract declarator), then this is probably just
4613 // grouping parens. However, if this could be an abstract-declarator, then
4614 // this could also be the start of function arguments (consider 'void()').
4615 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004616
Chris Lattneref4715c2008-04-06 05:45:57 +00004617 if (!D.mayOmitIdentifier()) {
4618 // If this can't be an abstract-declarator, this *must* be a grouping
4619 // paren, because we haven't seen the identifier yet.
4620 isGrouping = true;
4621 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004622 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4623 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004624 isDeclarationSpecifier() || // 'int(int)' is a function.
4625 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004626 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4627 // considered to be a type, not a K&R identifier-list.
4628 isGrouping = false;
4629 } else {
4630 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4631 isGrouping = true;
4632 }
Mike Stump1eb44332009-09-09 15:08:12 +00004633
Chris Lattneref4715c2008-04-06 05:45:57 +00004634 // If this is a grouping paren, handle:
4635 // direct-declarator: '(' declarator ')'
4636 // direct-declarator: '(' attributes declarator ')'
4637 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004638 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4639 D.setEllipsisLoc(SourceLocation());
4640
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004641 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004642 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004643 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004644 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004645 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004646 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004647 T.getCloseLocation()),
4648 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004649
4650 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004651
4652 // An ellipsis cannot be placed outside parentheses.
4653 if (EllipsisLoc.isValid())
4654 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4655
Chris Lattneref4715c2008-04-06 05:45:57 +00004656 return;
4657 }
Mike Stump1eb44332009-09-09 15:08:12 +00004658
Chris Lattneref4715c2008-04-06 05:45:57 +00004659 // Okay, if this wasn't a grouping paren, it must be the start of a function
4660 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004661 // identifier (and remember where it would have been), then call into
4662 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004663 D.SetIdentifier(0, Tok.getLocation());
4664
David Blaikie42d6d0c2011-12-04 05:04:18 +00004665 // Enter function-declaration scope, limiting any declarators to the
4666 // function prototype scope, including parameter declarators.
4667 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004668 Scope::FunctionPrototypeScope | Scope::DeclScope |
4669 (D.isFunctionDeclaratorAFunctionDeclaration()
4670 ? Scope::FunctionDeclarationScope : 0));
Richard Smithb9c62612012-07-30 21:30:52 +00004671 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004672 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004673}
4674
4675/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4676/// declarator D up to a paren, which indicates that we are parsing function
4677/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004678///
Richard Smith6ee326a2012-04-10 01:32:12 +00004679/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4680/// immediately after the open paren - they should be considered to be the
4681/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004682///
Richard Smith6ee326a2012-04-10 01:32:12 +00004683/// If RequiresArg is true, then the first argument of the function is required
4684/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004685///
Richard Smith6ee326a2012-04-10 01:32:12 +00004686/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4687/// (C++11) ref-qualifier[opt], exception-specification[opt],
4688/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4689///
4690/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004691/// dynamic-exception-specification
4692/// noexcept-specification
4693///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004694void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004695 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004696 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004697 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004698 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004699 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004700 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004701 // lparen is already consumed!
4702 assert(D.isPastIdentifier() && "Should not call before identifier!");
4703
4704 // This should be true when the function has typed arguments.
4705 // Otherwise, it is treated as a K&R-style function.
4706 bool HasProto = false;
4707 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004708 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004709 // Remember where we see an ellipsis, if any.
4710 SourceLocation EllipsisLoc;
4711
4712 DeclSpec DS(AttrFactory);
4713 bool RefQualifierIsLValueRef = true;
4714 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004715 SourceLocation ConstQualifierLoc;
4716 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004717 ExceptionSpecificationType ESpecType = EST_None;
4718 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004719 SmallVector<ParsedType, 2> DynamicExceptions;
4720 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004721 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004722 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004723 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004724
James Molloy16f1f712012-02-29 10:24:19 +00004725 Actions.ActOnStartFunctionDeclarator();
4726
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004727 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4728 EndLoc is the end location for the function declarator.
4729 They differ for trailing return types. */
4730 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004731 SourceLocation LParenLoc, RParenLoc;
4732 LParenLoc = Tracker.getOpenLocation();
4733 StartLoc = LParenLoc;
4734
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004735 if (isFunctionDeclaratorIdentifierList()) {
4736 if (RequiresArg)
4737 Diag(Tok, diag::err_argument_required_after_attribute);
4738
4739 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4740
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004741 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004742 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004743 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004744 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004745 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004746 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004747 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004748 else if (RequiresArg)
4749 Diag(Tok, diag::err_argument_required_after_attribute);
4750
David Blaikie4e4d0842012-03-11 07:00:24 +00004751 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004752
4753 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004754 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004755 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004756 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004757 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004758
David Blaikie4e4d0842012-03-11 07:00:24 +00004759 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004760 // FIXME: Accept these components in any order, and produce fixits to
4761 // correct the order if the user gets it wrong. Ideally we should deal
4762 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004763
4764 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004765 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4766 if (!DS.getSourceRange().getEnd().isInvalid()) {
4767 EndLoc = DS.getSourceRange().getEnd();
4768 ConstQualifierLoc = DS.getConstSpecLoc();
4769 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4770 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004771
4772 // Parse ref-qualifier[opt].
4773 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004774 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004775 diag::warn_cxx98_compat_ref_qualifier :
4776 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004777
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004778 RefQualifierIsLValueRef = Tok.is(tok::amp);
4779 RefQualifierLoc = ConsumeToken();
4780 EndLoc = RefQualifierLoc;
4781 }
4782
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004783 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00004784 // If a declaration declares a member function or member function
4785 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004786 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00004787 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004788 // declarator.
Chad Rosier8decdee2012-06-26 22:30:43 +00004789 bool IsCXX11MemberFunction =
Richard Smith80ad52f2013-01-02 11:42:31 +00004790 getLangOpts().CPlusPlus11 &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004791 (D.getContext() == Declarator::MemberContext ||
4792 (D.getContext() == Declarator::FileContext &&
Chad Rosier8decdee2012-06-26 22:30:43 +00004793 D.getCXXScopeSpec().isValid() &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004794 Actions.CurContext->isRecord()));
4795 Sema::CXXThisScopeRAII ThisScope(Actions,
4796 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith7b19cb12013-01-14 01:55:13 +00004797 DS.getTypeQualifiers() |
4798 (D.getDeclSpec().isConstexprSpecified()
4799 ? Qualifiers::Const : 0),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004800 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004801
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004802 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004803 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004804 DynamicExceptions,
4805 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004806 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004807 if (ESpecType != EST_None)
4808 EndLoc = ESpecRange.getEnd();
4809
Richard Smith6ee326a2012-04-10 01:32:12 +00004810 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4811 // after the exception-specification.
Richard Smith4e24f0f2013-01-02 12:01:23 +00004812 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004813
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004814 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004815 LocalEndLoc = EndLoc;
Richard Smith80ad52f2013-01-02 11:42:31 +00004816 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004817 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004818 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4819 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004820 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00004821 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004822 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004823 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004824 }
4825 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004826 }
4827
4828 // Remember that we parsed a function type, and remember the attributes.
4829 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004830 IsAmbiguous,
4831 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004832 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004833 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004834 DS.getTypeQualifiers(),
4835 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004836 RefQualifierLoc, ConstQualifierLoc,
4837 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004838 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004839 ESpecType, ESpecRange.getBegin(),
4840 DynamicExceptions.data(),
4841 DynamicExceptionRanges.data(),
4842 DynamicExceptions.size(),
4843 NoexceptExpr.isUsable() ?
4844 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004845 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004846 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004847 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004848
4849 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004850}
4851
4852/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4853/// identifier list form for a K&R-style function: void foo(a,b,c)
4854///
4855/// Note that identifier-lists are only allowed for normal declarators, not for
4856/// abstract-declarators.
4857bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004858 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004859 && Tok.is(tok::identifier)
4860 && !TryAltiVecVectorToken()
4861 // K&R identifier lists can't have typedefs as identifiers, per C99
4862 // 6.7.5.3p11.
4863 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4864 // Identifier lists follow a really simple grammar: the identifiers can
4865 // be followed *only* by a ", identifier" or ")". However, K&R
4866 // identifier lists are really rare in the brave new modern world, and
4867 // it is very common for someone to typo a type in a non-K&R style
4868 // list. If we are presented with something like: "void foo(intptr x,
4869 // float y)", we don't want to start parsing the function declarator as
4870 // though it is a K&R style declarator just because intptr is an
4871 // invalid type.
4872 //
4873 // To handle this, we check to see if the token after the first
4874 // identifier is a "," or ")". Only then do we parse it as an
4875 // identifier list.
4876 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4877}
4878
4879/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4880/// we found a K&R-style identifier list instead of a typed parameter list.
4881///
4882/// After returning, ParamInfo will hold the parsed parameters.
4883///
4884/// identifier-list: [C99 6.7.5]
4885/// identifier
4886/// identifier-list ',' identifier
4887///
4888void Parser::ParseFunctionDeclaratorIdentifierList(
4889 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004890 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004891 // If there was no identifier specified for the declarator, either we are in
4892 // an abstract-declarator, or we are in a parameter declarator which was found
4893 // to be abstract. In abstract-declarators, identifier lists are not valid:
4894 // diagnose this.
4895 if (!D.getIdentifier())
4896 Diag(Tok, diag::ext_ident_list_in_param);
4897
4898 // Maintain an efficient lookup of params we have seen so far.
4899 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4900
4901 while (1) {
4902 // If this isn't an identifier, report the error and skip until ')'.
4903 if (Tok.isNot(tok::identifier)) {
4904 Diag(Tok, diag::err_expected_ident);
4905 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4906 // Forget we parsed anything.
4907 ParamInfo.clear();
4908 return;
4909 }
4910
4911 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4912
4913 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4914 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4915 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4916
4917 // Verify that the argument identifier has not already been mentioned.
4918 if (!ParamsSoFar.insert(ParmII)) {
4919 Diag(Tok, diag::err_param_redefinition) << ParmII;
4920 } else {
4921 // Remember this identifier in ParamInfo.
4922 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4923 Tok.getLocation(),
4924 0));
4925 }
4926
4927 // Eat the identifier.
4928 ConsumeToken();
4929
4930 // The list continues if we see a comma.
4931 if (Tok.isNot(tok::comma))
4932 break;
4933 ConsumeToken();
4934 }
4935}
4936
4937/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4938/// after the opening parenthesis. This function will not parse a K&R-style
4939/// identifier list.
4940///
Richard Smith6ce48a72012-04-11 04:01:28 +00004941/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4942/// caller parsed those arguments immediately after the open paren - they should
4943/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004944///
4945/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4946/// be the location of the ellipsis, if any was parsed.
4947///
Reid Spencer5f016e22007-07-11 17:01:13 +00004948/// parameter-type-list: [C99 6.7.5]
4949/// parameter-list
4950/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004951/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004952///
4953/// parameter-list: [C99 6.7.5]
4954/// parameter-declaration
4955/// parameter-list ',' parameter-declaration
4956///
4957/// parameter-declaration: [C99 6.7.5]
4958/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004959/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004960/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004961/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004962/// declaration-specifiers abstract-declarator[opt]
4963/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004964/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004965/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004966/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004967///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004968void Parser::ParseParameterDeclarationClause(
4969 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004970 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004971 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004972 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004973
Chris Lattnerf97409f2008-04-06 06:57:35 +00004974 while (1) {
4975 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004976 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4977 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004978 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004979 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004980 }
Mike Stump1eb44332009-09-09 15:08:12 +00004981
Chris Lattnerf97409f2008-04-06 06:57:35 +00004982 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004983 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004984 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004985
Richard Smith6ce48a72012-04-11 04:01:28 +00004986 // Parse any C++11 attributes.
Richard Smith4e24f0f2013-01-02 12:01:23 +00004987 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith6ce48a72012-04-11 04:01:28 +00004988
John McCall7f040a92010-12-24 02:08:15 +00004989 // Skip any Microsoft attributes before a param.
Chad Rosier16f90bf2012-12-20 20:37:53 +00004990 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall7f040a92010-12-24 02:08:15 +00004991
4992 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004993
4994 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004995 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004996 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004997 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4998 // too much hassle.
4999 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00005000
Chris Lattnere64c5492009-02-27 18:38:20 +00005001 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00005002
Chris Lattnerf97409f2008-04-06 06:57:35 +00005003 // Parse the declarator. This is "PrototypeContext", because we must
5004 // accept either 'declarator' or 'abstract-declarator' here.
5005 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5006 ParseDeclarator(ParmDecl);
5007
5008 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00005009 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00005010
Chris Lattnerf97409f2008-04-06 06:57:35 +00005011 // Remember this parsed parameter in ParamInfo.
5012 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00005013
Douglas Gregor72b505b2008-12-16 21:30:33 +00005014 // DefArgToks is used when the parsing of default arguments needs
5015 // to be delayed.
5016 CachedTokens *DefArgToks = 0;
5017
Chris Lattnerf97409f2008-04-06 06:57:35 +00005018 // If no parameter was specified, verify that *something* was specified,
5019 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00005020 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5021 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005022 // Completely missing, emit error.
5023 Diag(DSStart, diag::err_missing_param);
5024 } else {
5025 // Otherwise, we have something. Add it and let semantic analysis try
5026 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00005027
Chris Lattnerf97409f2008-04-06 06:57:35 +00005028 // Inform the actions module about the parameter declarator, so it gets
5029 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00005030 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00005031
5032 // Parse the default argument, if any. We parse the default
5033 // arguments in all dialects; the semantic analysis in
5034 // ActOnParamDefaultArgument will reject the default argument in
5035 // C.
5036 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00005037 SourceLocation EqualLoc = Tok.getLocation();
5038
Chris Lattner04421082008-04-08 04:40:51 +00005039 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00005040 if (D.getContext() == Declarator::MemberContext) {
5041 // If we're inside a class definition, cache the tokens
5042 // corresponding to the default argument. We'll actually parse
5043 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00005044 // FIXME: Can we use a smart pointer for Toks?
5045 DefArgToks = new CachedTokens;
5046
Mike Stump1eb44332009-09-09 15:08:12 +00005047 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00005048 /*StopAtSemi=*/true,
5049 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005050 delete DefArgToks;
5051 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00005052 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005053 } else {
5054 // Mark the end of the default argument so that we know when to
5055 // stop when we parse it later on.
5056 Token DefArgEnd;
5057 DefArgEnd.startToken();
5058 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5059 DefArgEnd.setLocation(Tok.getLocation());
5060 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00005061 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00005062 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005063 }
Chris Lattner04421082008-04-08 04:40:51 +00005064 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005065 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00005066 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005067
Chad Rosier8decdee2012-06-26 22:30:43 +00005068 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005069 // used.
5070 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005071 Sema::PotentiallyEvaluatedIfUsed,
5072 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005073
Sebastian Redl84407ba2012-03-14 15:54:00 +00005074 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +00005075 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl3e280b52012-03-18 22:25:45 +00005076 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00005077 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00005078 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00005079 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00005080 if (DefArgResult.isInvalid()) {
5081 Actions.ActOnParamDefaultArgumentError(Param);
5082 SkipUntil(tok::comma, tok::r_paren, true, true);
5083 } else {
5084 // Inform the actions module about the default argument
5085 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005086 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005087 }
Chris Lattner04421082008-04-08 04:40:51 +00005088 }
5089 }
Mike Stump1eb44332009-09-09 15:08:12 +00005090
5091 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5092 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00005093 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005094 }
5095
5096 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00005097 if (Tok.isNot(tok::comma)) {
5098 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005099 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00005100
David Blaikie4e4d0842012-03-11 07:00:24 +00005101 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005102 // We have ellipsis without a preceding ',', which is ill-formed
5103 // in C. Complain and provide the fix.
5104 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00005105 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005106 }
5107 }
Chad Rosier8decdee2012-06-26 22:30:43 +00005108
Douglas Gregored5d6512009-09-22 21:41:40 +00005109 break;
5110 }
Mike Stump1eb44332009-09-09 15:08:12 +00005111
Chris Lattnerf97409f2008-04-06 06:57:35 +00005112 // Consume the comma.
5113 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005114 }
Mike Stump1eb44332009-09-09 15:08:12 +00005115
Chris Lattner66d28652008-04-06 06:34:08 +00005116}
Chris Lattneref4715c2008-04-06 05:45:57 +00005117
Reid Spencer5f016e22007-07-11 17:01:13 +00005118/// [C90] direct-declarator '[' constant-expression[opt] ']'
5119/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5120/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5121/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5122/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005123/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5124/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005125void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005126 if (CheckProhibitedCXX11Attribute())
5127 return;
5128
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005129 BalancedDelimiterTracker T(*this, tok::l_square);
5130 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005131
Chris Lattner378c7e42008-12-18 07:27:21 +00005132 // C array syntax has many features, but by-far the most common is [] and [4].
5133 // This code does a fast path to handle some of the most obvious cases.
5134 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005135 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005136 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005137 MaybeParseCXX11Attributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005138
Chris Lattner378c7e42008-12-18 07:27:21 +00005139 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005140 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005141 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005142 T.getOpenLocation(),
5143 T.getCloseLocation()),
5144 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005145 return;
5146 } else if (Tok.getKind() == tok::numeric_constant &&
5147 GetLookAheadToken(1).is(tok::r_square)) {
5148 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005149 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005150 ConsumeToken();
5151
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005152 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005153 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005154 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005155
Chris Lattner378c7e42008-12-18 07:27:21 +00005156 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicebf0fa82013-01-11 08:33:05 +00005157 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall7f040a92010-12-24 02:08:15 +00005158 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005159 T.getOpenLocation(),
5160 T.getCloseLocation()),
5161 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005162 return;
5163 }
Mike Stump1eb44332009-09-09 15:08:12 +00005164
Reid Spencer5f016e22007-07-11 17:01:13 +00005165 // If valid, this location is the position where we read the 'static' keyword.
5166 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005167 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005168 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005169
Reid Spencer5f016e22007-07-11 17:01:13 +00005170 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005171 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005172 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005173 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005174
Reid Spencer5f016e22007-07-11 17:01:13 +00005175 // If we haven't already read 'static', check to see if there is one after the
5176 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005177 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005178 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005179
Reid Spencer5f016e22007-07-11 17:01:13 +00005180 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5181 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005182 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005183
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005184 // Handle the case where we have '[*]' as the array size. However, a leading
5185 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005186 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005187 // infrequent, use of lookahead is not costly here.
5188 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005189 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005190
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005191 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005192 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005193 StaticLoc = SourceLocation(); // Drop the static.
5194 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005195 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005196 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005197 // Note, in C89, this production uses the constant-expr production instead
5198 // of assignment-expr. The only difference is that assignment-expr allows
5199 // things like '=' and '*='. Sema rejects these in C89 mode because they
5200 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005201
Douglas Gregore0762c92009-06-19 23:52:42 +00005202 // Parse the constant-expression or assignment-expression now (depending
5203 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005204 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005205 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005206 } else {
5207 EnterExpressionEvaluationContext Unevaluated(Actions,
5208 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005209 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005210 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005211 }
Mike Stump1eb44332009-09-09 15:08:12 +00005212
Reid Spencer5f016e22007-07-11 17:01:13 +00005213 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005214 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005215 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005216 // If the expression was invalid, skip it.
5217 SkipUntil(tok::r_square);
5218 return;
5219 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005220
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005221 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005222
John McCall0b7e6782011-03-24 11:26:52 +00005223 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005224 MaybeParseCXX11Attributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005225
Chris Lattner378c7e42008-12-18 07:27:21 +00005226 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005227 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005228 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005229 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005230 T.getOpenLocation(),
5231 T.getCloseLocation()),
5232 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005233}
5234
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005235/// [GNU] typeof-specifier:
5236/// typeof ( expressions )
5237/// typeof ( type-name )
5238/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005239///
5240void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005241 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005242 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005243 SourceLocation StartLoc = ConsumeToken();
5244
John McCallcfb708c2010-01-13 20:03:27 +00005245 const bool hasParens = Tok.is(tok::l_paren);
5246
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005247 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5248 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005249
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005250 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005251 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005252 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005253 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5254 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005255 if (hasParens)
5256 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005257
5258 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005259 // FIXME: Not accurate, the range gets one token more than it should.
5260 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005261 else
5262 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005263
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005264 if (isCastExpr) {
5265 if (!CastTy) {
5266 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005267 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005268 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005269
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005270 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005271 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005272 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5273 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005274 DiagID, CastTy))
5275 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005276 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005277 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005278
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005279 // If we get here, the operand to the typeof was an expresion.
5280 if (Operand.isInvalid()) {
5281 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005282 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005283 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005284
Eli Friedman71b8fb52012-01-21 01:01:51 +00005285 // We might need to transform the operand if it is potentially evaluated.
5286 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5287 if (Operand.isInvalid()) {
5288 DS.SetTypeSpecError();
5289 return;
5290 }
5291
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005292 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005293 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005294 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5295 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005296 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005297 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005298}
Chris Lattner1b492422010-02-28 18:33:55 +00005299
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005300/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005301/// _Atomic ( type-name )
5302///
5303void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5304 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5305
5306 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005307 BalancedDelimiterTracker T(*this, tok::l_paren);
5308 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00005309 SkipUntil(tok::r_paren);
5310 return;
5311 }
5312
5313 TypeResult Result = ParseTypeName();
5314 if (Result.isInvalid()) {
5315 SkipUntil(tok::r_paren);
5316 return;
5317 }
5318
5319 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005320 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005321
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005322 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005323 return;
5324
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005325 DS.setTypeofParensRange(T.getRange());
5326 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005327
5328 const char *PrevSpec = 0;
5329 unsigned DiagID;
5330 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5331 DiagID, Result.release()))
5332 Diag(StartLoc, DiagID) << PrevSpec;
5333}
5334
Chris Lattner1b492422010-02-28 18:33:55 +00005335
5336/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5337/// from TryAltiVecVectorToken.
5338bool Parser::TryAltiVecVectorTokenOutOfLine() {
5339 Token Next = NextToken();
5340 switch (Next.getKind()) {
5341 default: return false;
5342 case tok::kw_short:
5343 case tok::kw_long:
5344 case tok::kw_signed:
5345 case tok::kw_unsigned:
5346 case tok::kw_void:
5347 case tok::kw_char:
5348 case tok::kw_int:
5349 case tok::kw_float:
5350 case tok::kw_double:
5351 case tok::kw_bool:
5352 case tok::kw___pixel:
5353 Tok.setKind(tok::kw___vector);
5354 return true;
5355 case tok::identifier:
5356 if (Next.getIdentifierInfo() == Ident_pixel) {
5357 Tok.setKind(tok::kw___vector);
5358 return true;
5359 }
5360 return false;
5361 }
5362}
5363
5364bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5365 const char *&PrevSpec, unsigned &DiagID,
5366 bool &isInvalid) {
5367 if (Tok.getIdentifierInfo() == Ident_vector) {
5368 Token Next = NextToken();
5369 switch (Next.getKind()) {
5370 case tok::kw_short:
5371 case tok::kw_long:
5372 case tok::kw_signed:
5373 case tok::kw_unsigned:
5374 case tok::kw_void:
5375 case tok::kw_char:
5376 case tok::kw_int:
5377 case tok::kw_float:
5378 case tok::kw_double:
5379 case tok::kw_bool:
5380 case tok::kw___pixel:
5381 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5382 return true;
5383 case tok::identifier:
5384 if (Next.getIdentifierInfo() == Ident_pixel) {
5385 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5386 return true;
5387 }
5388 break;
5389 default:
5390 break;
5391 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005392 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005393 DS.isTypeAltiVecVector()) {
5394 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5395 return true;
5396 }
5397 return false;
5398}