blob: 4628f3dc17c19e26499e4b8076c9db7a4890eab2 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- ParseDecl.cpp - Declaration Parsing ------------------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the Declaration portions of the Parser interfaces.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Parse/Parser.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000015#include "RAIIObjectsForParser.h"
Benjamin Kramer9852f582012-12-01 16:35:25 +000016#include "clang/Basic/AddressSpaces.h"
Jordan Rose3f6f51e2013-02-08 22:30:41 +000017#include "clang/Basic/CharInfo.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000018#include "clang/Basic/OpenCL.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000019#include "clang/Parse/ParseDiagnostic.h"
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +000020#include "clang/Sema/Lookup.h"
John McCall19510852010-08-20 18:27:03 +000021#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000022#include "clang/Sema/PrettyDeclStackTrace.h"
Chandler Carruth55fc8732012-12-04 09:13:33 +000023#include "clang/Sema/Scope.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000024#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000025#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000026#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000027using namespace clang;
28
29//===----------------------------------------------------------------------===//
30// C99 6.7: Declarations.
31//===----------------------------------------------------------------------===//
32
33/// ParseTypeName
34/// type-name: [C99 6.7.6]
35/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000036///
37/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000038TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000039 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000040 AccessSpecifier AS,
Richard Smith6b3d3e52013-02-20 19:22:51 +000041 Decl **OwnedType,
42 ParsedAttributes *Attrs) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000043 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smitha971d242012-05-09 20:55:26 +000044 if (DSC == DSC_normal)
45 DSC = DSC_type_specifier;
Richard Smith7796eb52012-03-12 08:56:40 +000046
Reid Spencer5f016e22007-07-11 17:01:13 +000047 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000048 DeclSpec DS(AttrFactory);
Richard Smith6b3d3e52013-02-20 19:22:51 +000049 if (Attrs)
50 DS.addAttributes(Attrs->getList());
Richard Smith7796eb52012-03-12 08:56:40 +000051 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000052 if (OwnedType)
53 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000054
Reid Spencer5f016e22007-07-11 17:01:13 +000055 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000056 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000057 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000058 if (Range)
59 *Range = DeclaratorInfo.getSourceRange();
60
Chris Lattnereaaebc72009-04-25 08:06:05 +000061 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000062 return true;
63
Douglas Gregor23c94db2010-07-02 17:43:08 +000064 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000065}
66
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000067
68/// isAttributeLateParsed - Return true if the attribute has arguments that
69/// require late parsing.
70static bool isAttributeLateParsed(const IdentifierInfo &II) {
71 return llvm::StringSwitch<bool>(II.getName())
72#include "clang/Parse/AttrLateParsed.inc"
73 .Default(false);
74}
75
Sean Huntbbd37c62009-11-21 08:43:09 +000076/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000077///
78/// [GNU] attributes:
79/// attribute
80/// attributes attribute
81///
82/// [GNU] attribute:
83/// '__attribute__' '(' '(' attribute-list ')' ')'
84///
85/// [GNU] attribute-list:
86/// attrib
87/// attribute_list ',' attrib
88///
89/// [GNU] attrib:
90/// empty
91/// attrib-name
92/// attrib-name '(' identifier ')'
93/// attrib-name '(' identifier ',' nonempty-expr-list ')'
94/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
95///
96/// [GNU] attrib-name:
97/// identifier
98/// typespec
99/// typequal
100/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +0000101///
Reid Spencer5f016e22007-07-11 17:01:13 +0000102/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +0000103/// token lookahead. Comment from gcc: "If they start with an identifier
104/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +0000105/// start with that identifier; otherwise they are an expression list."
106///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000107/// GCC does not require the ',' between attribs in an attribute-list.
108///
Reid Spencer5f016e22007-07-11 17:01:13 +0000109/// At the moment, I am not doing 2 token lookahead. I am also unaware of
110/// any attributes that don't work (based on my limited testing). Most
111/// attributes are very simple in practice. Until we find a bug, I don't see
112/// a pressing need to implement the 2 token lookahead.
113
John McCall7f040a92010-12-24 02:08:15 +0000114void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000115 SourceLocation *endLoc,
116 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000117 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000118
Chris Lattner04d66662007-10-09 17:33:22 +0000119 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000120 ConsumeToken();
121 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
122 "attribute")) {
123 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000124 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000125 }
126 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
127 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000128 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000129 }
130 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000131 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
132 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000133 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000134 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
135 ConsumeToken();
136 continue;
137 }
138 // we have an identifier or declaration specifier (const, int, etc.)
139 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
140 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000141
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000142 if (Tok.is(tok::l_paren)) {
143 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000144 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000145 LateParsedAttribute *LA =
146 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
147 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000148
Bill Wendlingad017fa2012-12-20 19:22:21 +0000149 // Attributes in a class are parsed at the end of the class, along
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000150 // with other late-parsed declarations.
DeLesley Hutchins161db022012-11-02 21:44:32 +0000151 if (!ClassStack.empty() && !LateAttrs->parseSoon())
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000152 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000153
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000154 // consume everything up to and including the matching right parens
155 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000156
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000157 Token Eof;
158 Eof.startToken();
159 Eof.setLocation(Tok.getLocation());
160 LA->Toks.push_back(Eof);
161 } else {
Michael Han6880f492012-10-03 01:56:22 +0000162 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000163 0, SourceLocation(), AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 }
165 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000166 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000167 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 }
169 }
170 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000171 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000172 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000173 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
174 SkipUntil(tok::r_paren, false);
175 }
John McCall7f040a92010-12-24 02:08:15 +0000176 if (endLoc)
177 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000178 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000179}
180
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000181
Michael Han6880f492012-10-03 01:56:22 +0000182/// Parse the arguments to a parameterized GNU attribute or
183/// a C++11 attribute in "gnu" namespace.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000184void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
185 SourceLocation AttrNameLoc,
186 ParsedAttributes &Attrs,
Michael Han6880f492012-10-03 01:56:22 +0000187 SourceLocation *EndLoc,
188 IdentifierInfo *ScopeName,
189 SourceLocation ScopeLoc,
190 AttributeList::Syntax Syntax) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000191
192 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
193
194 // Availability attributes have their own grammar.
195 if (AttrName->isStr("availability")) {
196 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
197 return;
198 }
199 // Thread safety attributes fit into the FIXME case above, so we
200 // just parse the arguments as a list of expressions
201 if (IsThreadSafetyAttribute(AttrName->getName())) {
202 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
203 return;
204 }
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +0000205 // Type safety attributes have their own grammar.
206 if (AttrName->isStr("type_tag_for_datatype")) {
207 ParseTypeTagForDatatypeAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
208 return;
209 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000210
211 ConsumeParen(); // ignore the left paren loc for now
212
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000213 IdentifierInfo *ParmName = 0;
214 SourceLocation ParmLoc;
215 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000216
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000217 switch (Tok.getKind()) {
218 case tok::kw_char:
219 case tok::kw_wchar_t:
220 case tok::kw_char16_t:
221 case tok::kw_char32_t:
222 case tok::kw_bool:
223 case tok::kw_short:
224 case tok::kw_int:
225 case tok::kw_long:
226 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000227 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000228 case tok::kw_signed:
229 case tok::kw_unsigned:
230 case tok::kw_float:
231 case tok::kw_double:
232 case tok::kw_void:
233 case tok::kw_typeof:
234 // __attribute__(( vec_type_hint(char) ))
235 // FIXME: Don't just discard the builtin type token.
236 ConsumeToken();
237 BuiltinType = true;
238 break;
239
240 case tok::identifier:
241 ParmName = Tok.getIdentifierInfo();
242 ParmLoc = ConsumeToken();
243 break;
244
245 default:
246 break;
247 }
248
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +0000249 ExprVector ArgExprs;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000250
251 if (!BuiltinType &&
252 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
253 // Eat the comma.
254 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000255 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000256
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000257 // Parse the non-empty comma-separated list of expressions.
258 while (1) {
259 ExprResult ArgExpr(ParseAssignmentExpression());
260 if (ArgExpr.isInvalid()) {
261 SkipUntil(tok::r_paren);
262 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000263 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000264 ArgExprs.push_back(ArgExpr.release());
265 if (Tok.isNot(tok::comma))
266 break;
267 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000268 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000269 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000270 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
271 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
272 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000273 while (Tok.is(tok::identifier)) {
274 ConsumeToken();
275 if (Tok.is(tok::greater))
276 break;
277 if (Tok.is(tok::comma)) {
278 ConsumeToken();
279 continue;
280 }
281 }
282 if (Tok.isNot(tok::greater))
283 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000284 SkipUntil(tok::r_paren, false, true); // skip until ')'
285 }
286 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000287
288 SourceLocation RParen = Tok.getLocation();
289 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
Michael Han45bed132012-10-04 16:42:52 +0000290 SourceLocation AttrLoc = ScopeLoc.isValid() ? ScopeLoc : AttrNameLoc;
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000291 AttributeList *attr =
Michael Han45bed132012-10-04 16:42:52 +0000292 Attrs.addNew(AttrName, SourceRange(AttrLoc, RParen),
Michael Han6880f492012-10-03 01:56:22 +0000293 ScopeName, ScopeLoc, ParmName, ParmLoc,
294 ArgExprs.data(), ArgExprs.size(), Syntax);
Sean Hunt8e083e72012-06-19 23:57:03 +0000295 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000296 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000297 }
298}
299
Chad Rosier8decdee2012-06-26 22:30:43 +0000300/// \brief Parses a single argument for a declspec, including the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000301/// surrounding parens.
Chad Rosier8decdee2012-06-26 22:30:43 +0000302void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000303 SourceLocation AttrNameLoc,
304 ParsedAttributes &Attrs)
305{
306 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000307 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000308 AttrName->getNameStart(), tok::r_paren))
309 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000310
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000311 ExprResult ArgExpr(ParseConstantExpression());
312 if (ArgExpr.isInvalid()) {
313 T.skipToEnd();
314 return;
315 }
316 Expr *ExprList = ArgExpr.take();
Chad Rosier8decdee2012-06-26 22:30:43 +0000317 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000318 &ExprList, 1, AttributeList::AS_Declspec);
319
320 T.consumeClose();
321}
322
Chad Rosier8decdee2012-06-26 22:30:43 +0000323/// \brief Determines whether a declspec is a "simple" one requiring no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000324/// arguments.
325bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
326 return llvm::StringSwitch<bool>(Ident->getName())
327 .Case("dllimport", true)
328 .Case("dllexport", true)
329 .Case("noreturn", true)
330 .Case("nothrow", true)
331 .Case("noinline", true)
332 .Case("naked", true)
333 .Case("appdomain", true)
334 .Case("process", true)
335 .Case("jitintrinsic", true)
336 .Case("noalias", true)
337 .Case("restrict", true)
338 .Case("novtable", true)
339 .Case("selectany", true)
340 .Case("thread", true)
341 .Default(false);
342}
343
Chad Rosier8decdee2012-06-26 22:30:43 +0000344/// \brief Attempts to parse a declspec which is not simple (one that takes
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000345/// parameters). Will return false if we properly handled the declspec, or
346/// true if it is an unknown declspec.
Chad Rosier8decdee2012-06-26 22:30:43 +0000347void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000348 SourceLocation Loc,
349 ParsedAttributes &Attrs) {
350 // Try to handle the easy case first -- these declspecs all take a single
351 // parameter as their argument.
352 if (llvm::StringSwitch<bool>(Ident->getName())
353 .Case("uuid", true)
354 .Case("align", true)
355 .Case("allocate", true)
356 .Default(false)) {
357 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
358 } else if (Ident->getName() == "deprecated") {
Chad Rosier8decdee2012-06-26 22:30:43 +0000359 // The deprecated declspec has an optional single argument, so we will
360 // check for a l-paren to decide whether we should parse an argument or
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000361 // not.
362 if (Tok.getKind() == tok::l_paren)
363 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
364 else
Chad Rosier8decdee2012-06-26 22:30:43 +0000365 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000366 AttributeList::AS_Declspec);
367 } else if (Ident->getName() == "property") {
368 // The property declspec is more complex in that it can take one or two
Chad Rosier8decdee2012-06-26 22:30:43 +0000369 // assignment expressions as a parameter, but the lhs of the assignment
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000370 // must be named get or put.
371 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000372 // For right now, we will just skip to the closing right paren of the
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000373 // property expression.
374 //
375 // FIXME: we should deal with __declspec(property) at some point because it
376 // is used in the platform SDK headers for the Parallel Patterns Library
377 // and ATL.
378 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000379 if (T.expectAndConsume(diag::err_expected_lparen_after,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000380 Ident->getNameStart(), tok::r_paren))
381 return;
382 T.skipToEnd();
383 } else {
384 // We don't recognize this as a valid declspec, but instead of creating the
385 // attribute and allowing sema to warn about it, we will warn here instead.
386 // This is because some attributes have multiple spellings, but we need to
387 // disallow that for declspecs (such as align vs aligned). If we made the
Chad Rosier8decdee2012-06-26 22:30:43 +0000388 // attribute, we'd have to split the valid declspec spelling logic into
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000389 // both locations.
390 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
391
392 // If there's an open paren, we should eat the open and close parens under
393 // the assumption that this unknown declspec has parameters.
394 BalancedDelimiterTracker T(*this, tok::l_paren);
395 if (!T.consumeOpen())
396 T.skipToEnd();
397 }
398}
399
Eli Friedmana23b4852009-06-08 07:21:15 +0000400/// [MS] decl-specifier:
401/// __declspec ( extended-decl-modifier-seq )
402///
403/// [MS] extended-decl-modifier-seq:
404/// extended-decl-modifier[opt]
405/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000406void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000407 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000408
Steve Narofff59e17e2008-12-24 20:59:21 +0000409 ConsumeToken();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000410 BalancedDelimiterTracker T(*this, tok::l_paren);
Chad Rosier8decdee2012-06-26 22:30:43 +0000411 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000412 tok::r_paren))
John McCall7f040a92010-12-24 02:08:15 +0000413 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000414
Chad Rosier8decdee2012-06-26 22:30:43 +0000415 // An empty declspec is perfectly legal and should not warn. Additionally,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000416 // you can specify multiple attributes per declspec.
417 while (Tok.getKind() != tok::r_paren) {
418 // We expect either a well-known identifier or a generic string. Anything
419 // else is a malformed declspec.
420 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
Chad Rosier8decdee2012-06-26 22:30:43 +0000421 if (!IsString && Tok.getKind() != tok::identifier &&
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000422 Tok.getKind() != tok::kw_restrict) {
423 Diag(Tok, diag::err_ms_declspec_type);
424 T.skipToEnd();
425 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000426 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000427
428 IdentifierInfo *AttrName;
429 SourceLocation AttrNameLoc;
430 if (IsString) {
431 SmallString<8> StrBuffer;
432 bool Invalid = false;
433 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
434 if (Invalid) {
435 T.skipToEnd();
436 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000437 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000438 AttrName = PP.getIdentifierInfo(Str);
439 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000440 } else {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000441 AttrName = Tok.getIdentifierInfo();
442 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000443 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000444
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000445 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
Chad Rosier8decdee2012-06-26 22:30:43 +0000446 // If we have a generic string, we will allow it because there is no
447 // documented list of allowable string declspecs, but we know they exist
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000448 // (for instance, SAL declspecs in older versions of MSVC).
449 //
Chad Rosier8decdee2012-06-26 22:30:43 +0000450 // Alternatively, if the identifier is a simple one, then it requires no
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000451 // arguments and can be turned into an attribute directly.
Chad Rosier8decdee2012-06-26 22:30:43 +0000452 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000453 0, 0, AttributeList::AS_Declspec);
454 else
455 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000456 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000457 T.consumeClose();
Eli Friedman290eeb02009-06-08 23:27:34 +0000458}
459
John McCall7f040a92010-12-24 02:08:15 +0000460void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000461 // Treat these like attributes
Eli Friedman290eeb02009-06-08 23:27:34 +0000462 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000463 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000464 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Chad Rosierccbb4022012-12-21 21:27:13 +0000465 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000466 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
467 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000468 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith5cd532c2013-01-29 01:24:26 +0000469 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Eli Friedman290eeb02009-06-08 23:27:34 +0000470 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000471}
472
John McCall7f040a92010-12-24 02:08:15 +0000473void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000474 // Treat these like attributes
475 while (Tok.is(tok::kw___pascal)) {
476 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
477 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000478 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Richard Smith5cd532c2013-01-29 01:24:26 +0000479 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000480 }
John McCall7f040a92010-12-24 02:08:15 +0000481}
482
Peter Collingbournef315fa82011-02-14 01:42:53 +0000483void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
484 // Treat these like attributes
485 while (Tok.is(tok::kw___kernel)) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000486 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Peter Collingbournef315fa82011-02-14 01:42:53 +0000487 SourceLocation AttrNameLoc = ConsumeToken();
Richard Smith5cd532c2013-01-29 01:24:26 +0000488 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
489 SourceLocation(), 0, 0, AttributeList::AS_Keyword);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000490 }
491}
492
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000493void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
Richard Smith5cd532c2013-01-29 01:24:26 +0000494 // FIXME: The mapping from attribute spelling to semantics should be
495 // performed in Sema, not here.
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000496 SourceLocation Loc = Tok.getLocation();
497 switch(Tok.getKind()) {
498 // OpenCL qualifiers:
499 case tok::kw___private:
Chad Rosier8decdee2012-06-26 22:30:43 +0000500 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000501 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000502 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000503 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000504 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000505
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000506 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000507 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000508 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000509 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000510 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000511
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000512 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000513 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000514 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000515 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000516 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000517
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000518 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000519 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000520 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000521 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000522 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000523
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000524 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000525 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000526 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000527 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000528 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000529
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000530 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000531 DS.getAttributes().addNewInteger(
Chad Rosier8decdee2012-06-26 22:30:43 +0000532 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000533 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000534 break;
Chad Rosier8decdee2012-06-26 22:30:43 +0000535
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000536 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000537 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000538 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000539 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000540 break;
541 default: break;
542 }
543}
544
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000545/// \brief Parse a version number.
546///
547/// version:
548/// simple-integer
549/// simple-integer ',' simple-integer
550/// simple-integer ',' simple-integer ',' simple-integer
551VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
552 Range = Tok.getLocation();
553
554 if (!Tok.is(tok::numeric_constant)) {
555 Diag(Tok, diag::err_expected_version);
556 SkipUntil(tok::comma, tok::r_paren, true, true, true);
557 return VersionTuple();
558 }
559
560 // Parse the major (and possibly minor and subminor) versions, which
561 // are stored in the numeric constant. We utilize a quirk of the
562 // lexer, which is that it handles something like 1.2.3 as a single
563 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000564 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000565 Buffer.resize(Tok.getLength()+1);
566 const char *ThisTokBegin = &Buffer[0];
567
568 // Get the spelling of the token, which eliminates trigraphs, etc.
569 bool Invalid = false;
570 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
571 if (Invalid)
572 return VersionTuple();
573
574 // Parse the major version.
575 unsigned AfterMajor = 0;
576 unsigned Major = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000577 while (AfterMajor < ActualLength && isDigit(ThisTokBegin[AfterMajor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000578 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
579 ++AfterMajor;
580 }
581
582 if (AfterMajor == 0) {
583 Diag(Tok, diag::err_expected_version);
584 SkipUntil(tok::comma, tok::r_paren, true, true, true);
585 return VersionTuple();
586 }
587
588 if (AfterMajor == ActualLength) {
589 ConsumeToken();
590
591 // We only had a single version component.
592 if (Major == 0) {
593 Diag(Tok, diag::err_zero_version);
594 return VersionTuple();
595 }
596
597 return VersionTuple(Major);
598 }
599
600 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
601 Diag(Tok, diag::err_expected_version);
602 SkipUntil(tok::comma, tok::r_paren, true, true, true);
603 return VersionTuple();
604 }
605
606 // Parse the minor version.
607 unsigned AfterMinor = AfterMajor + 1;
608 unsigned Minor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000609 while (AfterMinor < ActualLength && isDigit(ThisTokBegin[AfterMinor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000610 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
611 ++AfterMinor;
612 }
613
614 if (AfterMinor == ActualLength) {
615 ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +0000616
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000617 // We had major.minor.
618 if (Major == 0 && Minor == 0) {
619 Diag(Tok, diag::err_zero_version);
620 return VersionTuple();
621 }
622
Chad Rosier8decdee2012-06-26 22:30:43 +0000623 return VersionTuple(Major, Minor);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000624 }
625
626 // If what follows is not a '.', we have a problem.
627 if (ThisTokBegin[AfterMinor] != '.') {
628 Diag(Tok, diag::err_expected_version);
629 SkipUntil(tok::comma, tok::r_paren, true, true, true);
Chad Rosier8decdee2012-06-26 22:30:43 +0000630 return VersionTuple();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000631 }
632
633 // Parse the subminor version.
634 unsigned AfterSubminor = AfterMinor + 1;
635 unsigned Subminor = 0;
Jordan Rose3f6f51e2013-02-08 22:30:41 +0000636 while (AfterSubminor < ActualLength && isDigit(ThisTokBegin[AfterSubminor])) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000637 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
638 ++AfterSubminor;
639 }
640
641 if (AfterSubminor != ActualLength) {
642 Diag(Tok, diag::err_expected_version);
643 SkipUntil(tok::comma, tok::r_paren, true, true, true);
644 return VersionTuple();
645 }
646 ConsumeToken();
647 return VersionTuple(Major, Minor, Subminor);
648}
649
650/// \brief Parse the contents of the "availability" attribute.
651///
652/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000653/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000654///
655/// platform:
656/// identifier
657///
658/// version-arg-list:
659/// version-arg
660/// version-arg ',' version-arg-list
661///
662/// version-arg:
663/// 'introduced' '=' version
664/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000665/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000666/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000667/// opt-message:
668/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000669void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
670 SourceLocation AvailabilityLoc,
671 ParsedAttributes &attrs,
672 SourceLocation *endLoc) {
673 SourceLocation PlatformLoc;
674 IdentifierInfo *Platform = 0;
675
676 enum { Introduced, Deprecated, Obsoleted, Unknown };
677 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000678 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000679
680 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000681 BalancedDelimiterTracker T(*this, tok::l_paren);
682 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000683 Diag(Tok, diag::err_expected_lparen);
684 return;
685 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000686
687 // Parse the platform name,
688 if (Tok.isNot(tok::identifier)) {
689 Diag(Tok, diag::err_availability_expected_platform);
690 SkipUntil(tok::r_paren);
691 return;
692 }
693 Platform = Tok.getIdentifierInfo();
694 PlatformLoc = ConsumeToken();
695
696 // Parse the ',' following the platform name.
697 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
698 return;
699
700 // If we haven't grabbed the pointers for the identifiers
701 // "introduced", "deprecated", and "obsoleted", do so now.
702 if (!Ident_introduced) {
703 Ident_introduced = PP.getIdentifierInfo("introduced");
704 Ident_deprecated = PP.getIdentifierInfo("deprecated");
705 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000706 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000707 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000708 }
709
710 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000711 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000712 do {
713 if (Tok.isNot(tok::identifier)) {
714 Diag(Tok, diag::err_availability_expected_change);
715 SkipUntil(tok::r_paren);
716 return;
717 }
718 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
719 SourceLocation KeywordLoc = ConsumeToken();
720
Douglas Gregorb53e4172011-03-26 03:35:55 +0000721 if (Keyword == Ident_unavailable) {
722 if (UnavailableLoc.isValid()) {
723 Diag(KeywordLoc, diag::err_availability_redundant)
724 << Keyword << SourceRange(UnavailableLoc);
Chad Rosier8decdee2012-06-26 22:30:43 +0000725 }
Douglas Gregorb53e4172011-03-26 03:35:55 +0000726 UnavailableLoc = KeywordLoc;
727
728 if (Tok.isNot(tok::comma))
729 break;
730
731 ConsumeToken();
732 continue;
Chad Rosier8decdee2012-06-26 22:30:43 +0000733 }
734
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000735 if (Tok.isNot(tok::equal)) {
736 Diag(Tok, diag::err_expected_equal_after)
737 << Keyword;
738 SkipUntil(tok::r_paren);
739 return;
740 }
741 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000742 if (Keyword == Ident_message) {
743 if (!isTokenStringLiteral()) {
Andy Gibbs97f84612012-11-17 19:16:52 +0000744 Diag(Tok, diag::err_expected_string_literal)
745 << /*Source='availability attribute'*/2;
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000746 SkipUntil(tok::r_paren);
747 return;
748 }
749 MessageExpr = ParseStringLiteralExpression();
750 break;
751 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000752
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000753 SourceRange VersionRange;
754 VersionTuple Version = ParseVersionTuple(VersionRange);
Chad Rosier8decdee2012-06-26 22:30:43 +0000755
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000756 if (Version.empty()) {
757 SkipUntil(tok::r_paren);
758 return;
759 }
760
761 unsigned Index;
762 if (Keyword == Ident_introduced)
763 Index = Introduced;
764 else if (Keyword == Ident_deprecated)
765 Index = Deprecated;
766 else if (Keyword == Ident_obsoleted)
767 Index = Obsoleted;
Chad Rosier8decdee2012-06-26 22:30:43 +0000768 else
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000769 Index = Unknown;
770
771 if (Index < Unknown) {
772 if (!Changes[Index].KeywordLoc.isInvalid()) {
773 Diag(KeywordLoc, diag::err_availability_redundant)
Chad Rosier8decdee2012-06-26 22:30:43 +0000774 << Keyword
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000775 << SourceRange(Changes[Index].KeywordLoc,
776 Changes[Index].VersionRange.getEnd());
777 }
778
779 Changes[Index].KeywordLoc = KeywordLoc;
780 Changes[Index].Version = Version;
781 Changes[Index].VersionRange = VersionRange;
782 } else {
783 Diag(KeywordLoc, diag::err_availability_unknown_change)
784 << Keyword << VersionRange;
785 }
786
787 if (Tok.isNot(tok::comma))
788 break;
789
790 ConsumeToken();
791 } while (true);
792
793 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000794 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000795 return;
796
797 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000798 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000799
Douglas Gregorb53e4172011-03-26 03:35:55 +0000800 // The 'unavailable' availability cannot be combined with any other
801 // availability changes. Make sure that hasn't happened.
802 if (UnavailableLoc.isValid()) {
803 bool Complained = false;
804 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
805 if (Changes[Index].KeywordLoc.isValid()) {
806 if (!Complained) {
807 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
808 << SourceRange(Changes[Index].KeywordLoc,
809 Changes[Index].VersionRange.getEnd());
810 Complained = true;
811 }
812
813 // Clear out the availability.
814 Changes[Index] = AvailabilityChange();
815 }
816 }
817 }
818
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000819 // Record this attribute
Chad Rosier8decdee2012-06-26 22:30:43 +0000820 attrs.addNew(&Availability,
821 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000822 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000823 Platform, PlatformLoc,
824 Changes[Introduced],
825 Changes[Deprecated],
Chad Rosier8decdee2012-06-26 22:30:43 +0000826 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000827 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000828 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000829}
830
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000831
Bill Wendlingad017fa2012-12-20 19:22:21 +0000832// Late Parsed Attributes:
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000833// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
834
835void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
836
837void Parser::LateParsedClass::ParseLexedAttributes() {
838 Self->ParseLexedAttributes(*Class);
839}
840
841void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000842 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000843}
844
845/// Wrapper class which calls ParseLexedAttribute, after setting up the
846/// scope appropriately.
847void Parser::ParseLexedAttributes(ParsingClass &Class) {
848 // Deal with templates
849 // FIXME: Test cases to make sure this does the right thing for templates.
850 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
851 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
852 HasTemplateScope);
853 if (HasTemplateScope)
854 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
855
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000856 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000857 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000858 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000859 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
860 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
861
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000862 // Enter the scope of nested classes
863 if (!AlreadyHasClassScope)
864 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
865 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000866 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000867 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
868 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
869 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000870 }
Chad Rosier8decdee2012-06-26 22:30:43 +0000871
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000872 if (!AlreadyHasClassScope)
873 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
874 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000875}
876
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000877
878/// \brief Parse all attributes in LAs, and attach them to Decl D.
879void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
880 bool EnterScope, bool OnDefinition) {
DeLesley Hutchins161db022012-11-02 21:44:32 +0000881 assert(LAs.parseSoon() &&
882 "Attribute list should be marked for immediate parsing.");
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000883 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins95526a42012-08-15 22:41:04 +0000884 if (D)
885 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000886 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000887 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000888 }
889 LAs.clear();
890}
891
892
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000893/// \brief Finish parsing an attribute for which parsing was delayed.
894/// This will be called at the end of parsing a class declaration
895/// for each LateParsedAttribute. We consume the saved tokens and
Chad Rosier8decdee2012-06-26 22:30:43 +0000896/// create an attribute with the arguments filled in. We add this
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000897/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000898void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
899 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000900 // Save the current token position.
901 SourceLocation OrigLoc = Tok.getLocation();
902
903 // Append the current token at the end of the new token stream so that it
904 // doesn't get lost.
905 LA.Toks.push_back(Tok);
906 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
907 // Consume the previously pushed token.
908 ConsumeAnyToken();
909
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000910 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
Richard Smithcd8ab512013-01-17 01:30:42 +0000911 // FIXME: Do not warn on C++11 attributes, once we start supporting
912 // them here.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000913 Diag(Tok, diag::warn_attribute_on_function_definition)
914 << LA.AttrName.getName();
915 }
916
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000917 ParsedAttributes Attrs(AttrFactory);
918 SourceLocation endLoc;
919
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000920 if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000921 Decl *D = LA.Decls[0];
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000922 NamedDecl *ND = dyn_cast<NamedDecl>(D);
923 RecordDecl *RD = dyn_cast_or_null<RecordDecl>(D->getDeclContext());
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000924
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000925 // Allow 'this' within late-parsed attributes.
926 Sema::CXXThisScopeRAII ThisScope(Actions, RD,
927 /*TypeQuals=*/0,
928 ND && RD && ND->isCXXInstanceMember());
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000929
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000930 if (LA.Decls.size() == 1) {
931 // If the Decl is templatized, add template parameters to scope.
932 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
933 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
934 if (HasTemplateScope)
935 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000936
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000937 // If the Decl is on a function, add function parameters to the scope.
938 bool HasFunScope = EnterScope && D->isFunctionOrFunctionTemplate();
939 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunScope);
940 if (HasFunScope)
941 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000942
Michael Han6880f492012-10-03 01:56:22 +0000943 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000944 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchinsd30fb9e2012-08-20 21:32:18 +0000945
946 if (HasFunScope) {
947 Actions.ActOnExitFunctionContext();
948 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
949 }
950 if (HasTemplateScope) {
951 TempScope.Exit();
952 }
953 } else {
954 // If there are multiple decls, then the decl cannot be within the
955 // function scope.
Michael Han6880f492012-10-03 01:56:22 +0000956 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc,
Michael Han45bed132012-10-04 16:42:52 +0000957 0, SourceLocation(), AttributeList::AS_GNU);
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000958 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000959 } else {
960 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000961 }
962
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000963 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
964 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
965 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000966
967 if (Tok.getLocation() != OrigLoc) {
968 // Due to a parsing error, we either went over the cached tokens or
969 // there are still cached tokens left, so we skip the leftover tokens.
970 // Since this is an uncommon situation that should be avoided, use the
971 // expensive isBeforeInTranslationUnit call.
972 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
973 OrigLoc))
974 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000975 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000976 }
977}
978
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000979/// \brief Wrapper around a case statement checking if AttrName is
980/// one of the thread safety attributes
Dmitri Gribenkocfa88f82013-01-12 19:30:44 +0000981bool Parser::IsThreadSafetyAttribute(StringRef AttrName) {
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000982 return llvm::StringSwitch<bool>(AttrName)
983 .Case("guarded_by", true)
984 .Case("guarded_var", true)
985 .Case("pt_guarded_by", true)
986 .Case("pt_guarded_var", true)
987 .Case("lockable", true)
988 .Case("scoped_lockable", true)
989 .Case("no_thread_safety_analysis", true)
990 .Case("acquired_after", true)
991 .Case("acquired_before", true)
992 .Case("exclusive_lock_function", true)
993 .Case("shared_lock_function", true)
994 .Case("exclusive_trylock_function", true)
995 .Case("shared_trylock_function", true)
996 .Case("unlock_function", true)
997 .Case("lock_returned", true)
998 .Case("locks_excluded", true)
999 .Case("exclusive_locks_required", true)
1000 .Case("shared_locks_required", true)
1001 .Default(false);
1002}
1003
1004/// \brief Parse the contents of thread safety attributes. These
1005/// should always be parsed as an expression list.
1006///
1007/// We need to special case the parsing due to the fact that if the first token
1008/// of the first argument is an identifier, the main parse loop will store
1009/// that token as a "parameter" and the rest of
1010/// the arguments will be added to a list of "arguments". However,
1011/// subsequent tokens in the first argument are lost. We instead parse each
1012/// argument as an expression and add all arguments to the list of "arguments".
1013/// In future, we will take advantage of this special case to also
1014/// deal with some argument scoping issues here (for example, referring to a
1015/// function parameter in the attribute on that function).
1016void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
1017 SourceLocation AttrNameLoc,
1018 ParsedAttributes &Attrs,
1019 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001020 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001021
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001022 BalancedDelimiterTracker T(*this, tok::l_paren);
1023 T.consumeOpen();
Chad Rosier8decdee2012-06-26 22:30:43 +00001024
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001025 ExprVector ArgExprs;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001026 bool ArgExprsOk = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00001027
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001028 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001029 while (Tok.isNot(tok::r_paren)) {
DeLesley Hutchinsed4330b2013-02-07 19:01:07 +00001030 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001031 ExprResult ArgExpr(ParseAssignmentExpression());
1032 if (ArgExpr.isInvalid()) {
1033 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001034 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001035 break;
1036 } else {
1037 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001038 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001039 if (Tok.isNot(tok::comma))
1040 break;
1041 ConsumeToken(); // Eat the comma, move to the next argument
1042 }
1043 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001044 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001045 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001046 ArgExprs.data(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001047 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001048 if (EndLoc)
1049 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001050}
1051
Dmitri Gribenko0d5a0692012-08-17 00:08:38 +00001052void Parser::ParseTypeTagForDatatypeAttribute(IdentifierInfo &AttrName,
1053 SourceLocation AttrNameLoc,
1054 ParsedAttributes &Attrs,
1055 SourceLocation *EndLoc) {
1056 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
1057
1058 BalancedDelimiterTracker T(*this, tok::l_paren);
1059 T.consumeOpen();
1060
1061 if (Tok.isNot(tok::identifier)) {
1062 Diag(Tok, diag::err_expected_ident);
1063 T.skipToEnd();
1064 return;
1065 }
1066 IdentifierInfo *ArgumentKind = Tok.getIdentifierInfo();
1067 SourceLocation ArgumentKindLoc = ConsumeToken();
1068
1069 if (Tok.isNot(tok::comma)) {
1070 Diag(Tok, diag::err_expected_comma);
1071 T.skipToEnd();
1072 return;
1073 }
1074 ConsumeToken();
1075
1076 SourceRange MatchingCTypeRange;
1077 TypeResult MatchingCType = ParseTypeName(&MatchingCTypeRange);
1078 if (MatchingCType.isInvalid()) {
1079 T.skipToEnd();
1080 return;
1081 }
1082
1083 bool LayoutCompatible = false;
1084 bool MustBeNull = false;
1085 while (Tok.is(tok::comma)) {
1086 ConsumeToken();
1087 if (Tok.isNot(tok::identifier)) {
1088 Diag(Tok, diag::err_expected_ident);
1089 T.skipToEnd();
1090 return;
1091 }
1092 IdentifierInfo *Flag = Tok.getIdentifierInfo();
1093 if (Flag->isStr("layout_compatible"))
1094 LayoutCompatible = true;
1095 else if (Flag->isStr("must_be_null"))
1096 MustBeNull = true;
1097 else {
1098 Diag(Tok, diag::err_type_safety_unknown_flag) << Flag;
1099 T.skipToEnd();
1100 return;
1101 }
1102 ConsumeToken(); // consume flag
1103 }
1104
1105 if (!T.consumeClose()) {
1106 Attrs.addNewTypeTagForDatatype(&AttrName, AttrNameLoc, 0, AttrNameLoc,
1107 ArgumentKind, ArgumentKindLoc,
1108 MatchingCType.release(), LayoutCompatible,
1109 MustBeNull, AttributeList::AS_GNU);
1110 }
1111
1112 if (EndLoc)
1113 *EndLoc = T.getCloseLocation();
1114}
1115
Richard Smith6ee326a2012-04-10 01:32:12 +00001116/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1117/// of a C++11 attribute-specifier in a location where an attribute is not
1118/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1119/// situation.
1120///
1121/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1122/// this doesn't appear to actually be an attribute-specifier, and the caller
1123/// should try to parse it.
1124bool Parser::DiagnoseProhibitedCXX11Attribute() {
1125 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1126
1127 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1128 case CAK_NotAttributeSpecifier:
1129 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1130 return false;
1131
1132 case CAK_InvalidAttributeSpecifier:
1133 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1134 return false;
1135
1136 case CAK_AttributeSpecifier:
1137 // Parse and discard the attributes.
1138 SourceLocation BeginLoc = ConsumeBracket();
1139 ConsumeBracket();
1140 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1141 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1142 SourceLocation EndLoc = ConsumeBracket();
1143 Diag(BeginLoc, diag::err_attributes_not_allowed)
1144 << SourceRange(BeginLoc, EndLoc);
1145 return true;
1146 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001147 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001148}
1149
Richard Smith975d52c2013-02-20 01:17:14 +00001150/// \brief We have found the opening square brackets of a C++11
1151/// attribute-specifier in a location where an attribute is not permitted, but
1152/// we know where the attributes ought to be written. Parse them anyway, and
1153/// provide a fixit moving them to the right place.
Richard Smith05321402013-02-19 23:47:15 +00001154void Parser::DiagnoseMisplacedCXX11Attribute(ParsedAttributesWithRange &Attrs,
1155 SourceLocation CorrectLocation) {
1156 assert((Tok.is(tok::l_square) && NextToken().is(tok::l_square)) ||
1157 Tok.is(tok::kw_alignas));
1158
1159 // Consume the attributes.
1160 SourceLocation Loc = Tok.getLocation();
1161 ParseCXX11Attributes(Attrs);
1162 CharSourceRange AttrRange(SourceRange(Loc, Attrs.Range.getEnd()), true);
1163
1164 Diag(Loc, diag::err_attributes_not_allowed)
1165 << FixItHint::CreateInsertionFromRange(CorrectLocation, AttrRange)
1166 << FixItHint::CreateRemoval(AttrRange);
1167}
1168
John McCall7f040a92010-12-24 02:08:15 +00001169void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1170 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1171 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001172}
1173
Michael Hanf64231e2012-11-06 19:34:54 +00001174void Parser::ProhibitCXX11Attributes(ParsedAttributesWithRange &attrs) {
1175 AttributeList *AttrList = attrs.getList();
1176 while (AttrList) {
Richard Smith4e24f0f2013-01-02 12:01:23 +00001177 if (AttrList->isCXX11Attribute()) {
Richard Smithd03de6a2013-01-29 10:02:16 +00001178 Diag(AttrList->getLoc(), diag::err_attribute_not_type_attr)
Michael Hanf64231e2012-11-06 19:34:54 +00001179 << AttrList->getName();
1180 AttrList->setInvalid();
1181 }
1182 AttrList = AttrList->getNext();
1183 }
1184}
1185
Reid Spencer5f016e22007-07-11 17:01:13 +00001186/// ParseDeclaration - Parse a full 'declaration', which consists of
1187/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001188/// 'Context' should be a Declarator::TheContext value. This returns the
1189/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001190///
1191/// declaration: [C99 6.7]
1192/// block-declaration ->
1193/// simple-declaration
1194/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001195/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001196/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001197/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001198/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001199/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001200/// others... [FIXME]
1201///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001202Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1203 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001204 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001205 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001206 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001207 // Must temporarily exit the objective-c container scope for
1208 // parsing c none objective-c decls.
1209 ObjCDeclContextSwitch ObjCDC(*this);
Chad Rosier8decdee2012-06-26 22:30:43 +00001210
John McCalld226f652010-08-21 09:40:31 +00001211 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001212 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001213 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001214 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001215 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001216 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001217 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001218 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001219 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001220 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001221 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001222 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001223 SourceLocation InlineLoc = ConsumeToken();
1224 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1225 break;
1226 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001227 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001228 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001229 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001230 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001231 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001232 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001233 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001234 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001235 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001236 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001237 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001238 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001239 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001240 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001241 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001242 default:
John McCall7f040a92010-12-24 02:08:15 +00001243 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001244 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001245
Chris Lattner682bf922009-03-29 16:50:03 +00001246 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001247 // single decl, convert it now. Alias declarations can also declare a type;
1248 // include that too if it is present.
1249 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001250}
1251
1252/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1253/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001254/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1255/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001256///[C90/C++]init-declarator-list ';' [TODO]
1257/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001258///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001259/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001260/// attribute-specifier-seq[opt] type-specifier-seq declarator
1261///
Chris Lattnercd147752009-03-29 17:27:48 +00001262/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001263/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001264///
1265/// If FRI is non-null, we might be parsing a for-range-declaration instead
1266/// of a simple-declaration. If we find that we are, we also parse the
1267/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001268Parser::DeclGroupPtrTy
1269Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1270 SourceLocation &DeclEnd,
1271 ParsedAttributesWithRange &attrs,
1272 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001273 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001274 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001275 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001276
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001277 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001278 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001279
Reid Spencer5f016e22007-07-11 17:01:13 +00001280 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1281 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001282 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001283 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001284 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001285 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001286 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001287 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001288 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001289 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001290
1291 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001292}
Mike Stump1eb44332009-09-09 15:08:12 +00001293
Richard Smith0706df42011-10-19 21:33:05 +00001294/// Returns true if this might be the start of a declarator, or a common typo
1295/// for a declarator.
1296bool Parser::MightBeDeclarator(unsigned Context) {
1297 switch (Tok.getKind()) {
1298 case tok::annot_cxxscope:
1299 case tok::annot_template_id:
1300 case tok::caret:
1301 case tok::code_completion:
1302 case tok::coloncolon:
1303 case tok::ellipsis:
1304 case tok::kw___attribute:
1305 case tok::kw_operator:
1306 case tok::l_paren:
1307 case tok::star:
1308 return true;
1309
1310 case tok::amp:
1311 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001312 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001313
Richard Smith1c94c162012-01-09 22:31:44 +00001314 case tok::l_square: // Might be an attribute on an unnamed bit-field.
Richard Smith80ad52f2013-01-02 11:42:31 +00001315 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus11 &&
Richard Smith1c94c162012-01-09 22:31:44 +00001316 NextToken().is(tok::l_square);
1317
1318 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001319 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001320
Richard Smith0706df42011-10-19 21:33:05 +00001321 case tok::identifier:
1322 switch (NextToken().getKind()) {
1323 case tok::code_completion:
1324 case tok::coloncolon:
1325 case tok::comma:
1326 case tok::equal:
1327 case tok::equalequal: // Might be a typo for '='.
1328 case tok::kw_alignas:
1329 case tok::kw_asm:
1330 case tok::kw___attribute:
1331 case tok::l_brace:
1332 case tok::l_paren:
1333 case tok::l_square:
1334 case tok::less:
1335 case tok::r_brace:
1336 case tok::r_paren:
1337 case tok::r_square:
1338 case tok::semi:
1339 return true;
1340
1341 case tok::colon:
1342 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001343 // and in block scope it's probably a label. Inside a class definition,
1344 // this is a bit-field.
1345 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001346 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001347
1348 case tok::identifier: // Possible virt-specifier.
Richard Smith4e24f0f2013-01-02 12:01:23 +00001349 return getLangOpts().CPlusPlus11 && isCXX11VirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001350
1351 default:
1352 return false;
1353 }
1354
1355 default:
1356 return false;
1357 }
1358}
1359
Richard Smith994d73f2012-04-11 20:59:20 +00001360/// Skip until we reach something which seems like a sensible place to pick
1361/// up parsing after a malformed declaration. This will sometimes stop sooner
1362/// than SkipUntil(tok::r_brace) would, but will never stop later.
1363void Parser::SkipMalformedDecl() {
1364 while (true) {
1365 switch (Tok.getKind()) {
1366 case tok::l_brace:
1367 // Skip until matching }, then stop. We've probably skipped over
1368 // a malformed class or function definition or similar.
1369 ConsumeBrace();
1370 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1371 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1372 // This declaration isn't over yet. Keep skipping.
1373 continue;
1374 }
1375 if (Tok.is(tok::semi))
1376 ConsumeToken();
1377 return;
1378
1379 case tok::l_square:
1380 ConsumeBracket();
1381 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1382 continue;
1383
1384 case tok::l_paren:
1385 ConsumeParen();
1386 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1387 continue;
1388
1389 case tok::r_brace:
1390 return;
1391
1392 case tok::semi:
1393 ConsumeToken();
1394 return;
1395
1396 case tok::kw_inline:
1397 // 'inline namespace' at the start of a line is almost certainly
Jordan Rose94f29f42012-07-09 16:54:53 +00001398 // a good place to pick back up parsing, except in an Objective-C
1399 // @interface context.
1400 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace) &&
1401 (!ParsingInObjCContainer || CurParsedObjCImpl))
Richard Smith994d73f2012-04-11 20:59:20 +00001402 return;
1403 break;
1404
1405 case tok::kw_namespace:
1406 // 'namespace' at the start of a line is almost certainly a good
Jordan Rose94f29f42012-07-09 16:54:53 +00001407 // place to pick back up parsing, except in an Objective-C
1408 // @interface context.
1409 if (Tok.isAtStartOfLine() &&
1410 (!ParsingInObjCContainer || CurParsedObjCImpl))
1411 return;
1412 break;
1413
1414 case tok::at:
1415 // @end is very much like } in Objective-C contexts.
1416 if (NextToken().isObjCAtKeyword(tok::objc_end) &&
1417 ParsingInObjCContainer)
1418 return;
1419 break;
1420
1421 case tok::minus:
1422 case tok::plus:
1423 // - and + probably start new method declarations in Objective-C contexts.
1424 if (Tok.isAtStartOfLine() && ParsingInObjCContainer)
Richard Smith994d73f2012-04-11 20:59:20 +00001425 return;
1426 break;
1427
1428 case tok::eof:
1429 return;
1430
1431 default:
1432 break;
1433 }
1434
1435 ConsumeAnyToken();
1436 }
1437}
1438
John McCalld8ac0572009-11-03 19:26:08 +00001439/// ParseDeclGroup - Having concluded that this is either a function
1440/// definition or a group of object declarations, actually parse the
1441/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001442Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1443 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001444 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001445 SourceLocation *DeclEnd,
1446 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001447 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001448 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001449 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001450
John McCalld8ac0572009-11-03 19:26:08 +00001451 // Bail out if the first declarator didn't seem well-formed.
1452 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001453 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001454 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001455 }
Mike Stump1eb44332009-09-09 15:08:12 +00001456
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001457 // Save late-parsed attributes for now; they need to be parsed in the
1458 // appropriate function scope after the function Decl has been constructed.
DeLesley Hutchins161db022012-11-02 21:44:32 +00001459 // These will be parsed in ParseFunctionDefinition or ParseLexedAttrList.
1460 LateParsedAttrList LateParsedAttrs(true);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001461 if (D.isFunctionDeclarator())
1462 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1463
Chris Lattnerc82daef2010-07-11 22:24:20 +00001464 // Check to see if we have a function *definition* which must have a body.
1465 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1466 // Look at the next token to make sure that this isn't a function
1467 // declaration. We have to check this because __attribute__ might be the
1468 // start of a function definition in GCC-extended K&R C.
Fariborz Jahanianbe1d4ec2012-08-10 15:54:40 +00001469 !isDeclarationAfterDeclarator()) {
Chad Rosier8decdee2012-06-26 22:30:43 +00001470
Chris Lattner004659a2010-07-11 22:42:07 +00001471 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001472 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1473 Diag(Tok, diag::err_function_declared_typedef);
1474
1475 // Recover by treating the 'typedef' as spurious.
1476 DS.ClearStorageClassSpecs();
1477 }
1478
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001479 Decl *TheDecl =
1480 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001481 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001482 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001483
Chris Lattner004659a2010-07-11 22:42:07 +00001484 if (isDeclarationSpecifier()) {
1485 // If there is an invalid declaration specifier right after the function
1486 // prototype, then we must be in a missing semicolon case where this isn't
1487 // actually a body. Just fall through into the code that handles it as a
1488 // prototype, and let the top-level code handle the erroneous declspec
1489 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001490 } else {
1491 Diag(Tok, diag::err_expected_fn_body);
1492 SkipUntil(tok::semi);
1493 return DeclGroupPtrTy();
1494 }
1495 }
1496
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001497 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001498 return DeclGroupPtrTy();
1499
1500 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1501 // must parse and analyze the for-range-initializer before the declaration is
1502 // analyzed.
1503 if (FRI && Tok.is(tok::colon)) {
1504 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001505 if (Tok.is(tok::l_brace))
1506 FRI->RangeExpr = ParseBraceInitializer();
1507 else
1508 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001509 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1510 Actions.ActOnCXXForRangeDecl(ThisDecl);
1511 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001512 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001513 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1514 }
1515
Chris Lattner5f9e2722011-07-23 10:55:15 +00001516 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001517 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001518 if (LateParsedAttrs.size() > 0)
1519 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001520 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001521 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001522 DeclsInGroup.push_back(FirstDecl);
1523
Richard Smith0706df42011-10-19 21:33:05 +00001524 bool ExpectSemi = Context != Declarator::ForContext;
Fariborz Jahanian6c89eaf2012-07-02 23:37:09 +00001525
John McCalld8ac0572009-11-03 19:26:08 +00001526 // If we don't have a comma, it is either the end of the list (a ';') or an
1527 // error, bail out.
1528 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001529 SourceLocation CommaLoc = ConsumeToken();
1530
1531 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1532 // This comma was followed by a line-break and something which can't be
1533 // the start of a declarator. The comma was probably a typo for a
1534 // semicolon.
1535 Diag(CommaLoc, diag::err_expected_semi_declaration)
1536 << FixItHint::CreateReplacement(CommaLoc, ";");
1537 ExpectSemi = false;
1538 break;
1539 }
John McCalld8ac0572009-11-03 19:26:08 +00001540
1541 // Parse the next declarator.
1542 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001543 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001544
1545 // Accept attributes in an init-declarator. In the first declarator in a
1546 // declaration, these would be part of the declspec. In subsequent
1547 // declarators, they become part of the declarator itself, so that they
1548 // don't apply to declarators after *this* one. Examples:
1549 // short __attribute__((common)) var; -> declspec
1550 // short var __attribute__((common)); -> declarator
1551 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001552 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001553
1554 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001555 if (!D.isInvalidType()) {
1556 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1557 D.complete(ThisDecl);
1558 if (ThisDecl)
Chad Rosier8decdee2012-06-26 22:30:43 +00001559 DeclsInGroup.push_back(ThisDecl);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001560 }
John McCalld8ac0572009-11-03 19:26:08 +00001561 }
1562
1563 if (DeclEnd)
1564 *DeclEnd = Tok.getLocation();
1565
Richard Smith0706df42011-10-19 21:33:05 +00001566 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001567 ExpectAndConsumeSemi(Context == Declarator::FileContext
1568 ? diag::err_invalid_token_after_toplevel_declarator
1569 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001570 // Okay, there was no semicolon and one was expected. If we see a
1571 // declaration specifier, just assume it was missing and continue parsing.
1572 // Otherwise things are very confused and we skip to recover.
1573 if (!isDeclarationSpecifier()) {
1574 SkipUntil(tok::r_brace, true, true);
1575 if (Tok.is(tok::semi))
1576 ConsumeToken();
1577 }
John McCalld8ac0572009-11-03 19:26:08 +00001578 }
1579
Douglas Gregor23c94db2010-07-02 17:43:08 +00001580 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001581 DeclsInGroup.data(),
1582 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001583}
1584
Richard Smithad762fc2011-04-14 22:09:26 +00001585/// Parse an optional simple-asm-expr and attributes, and attach them to a
1586/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001587bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001588 // If a simple-asm-expr is present, parse it.
1589 if (Tok.is(tok::kw_asm)) {
1590 SourceLocation Loc;
1591 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1592 if (AsmLabel.isInvalid()) {
1593 SkipUntil(tok::semi, true, true);
1594 return true;
1595 }
1596
1597 D.setAsmLabel(AsmLabel.release());
1598 D.SetRangeEnd(Loc);
1599 }
1600
1601 MaybeParseGNUAttributes(D);
1602 return false;
1603}
1604
Douglas Gregor1426e532009-05-12 21:31:51 +00001605/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1606/// declarator'. This method parses the remainder of the declaration
1607/// (including any attributes or initializer, among other things) and
1608/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001609///
Reid Spencer5f016e22007-07-11 17:01:13 +00001610/// init-declarator: [C99 6.7]
1611/// declarator
1612/// declarator '=' initializer
1613/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1614/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001615/// [C++] declarator initializer[opt]
1616///
1617/// [C++] initializer:
1618/// [C++] '=' initializer-clause
1619/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001620/// [C++0x] '=' 'default' [TODO]
1621/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001622/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001623///
1624/// According to the standard grammar, =default and =delete are function
1625/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001626///
John McCalld226f652010-08-21 09:40:31 +00001627Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001628 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001629 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001630 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001631
Richard Smithad762fc2011-04-14 22:09:26 +00001632 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1633}
Mike Stump1eb44332009-09-09 15:08:12 +00001634
Richard Smithad762fc2011-04-14 22:09:26 +00001635Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1636 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001637 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001638 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001639 switch (TemplateInfo.Kind) {
1640 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001641 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001642 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001643
Douglas Gregord5a423b2009-09-25 18:43:00 +00001644 case ParsedTemplateInfo::Template:
1645 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001646 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
Benjamin Kramer5354e772012-08-23 23:38:35 +00001647 *TemplateInfo.TemplateParams,
Douglas Gregord5a423b2009-09-25 18:43:00 +00001648 D);
1649 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00001650
Douglas Gregord5a423b2009-09-25 18:43:00 +00001651 case ParsedTemplateInfo::ExplicitInstantiation: {
Chad Rosier8decdee2012-06-26 22:30:43 +00001652 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001653 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001654 TemplateInfo.ExternLoc,
1655 TemplateInfo.TemplateLoc,
1656 D);
1657 if (ThisRes.isInvalid()) {
1658 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001659 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001660 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001661
Douglas Gregord5a423b2009-09-25 18:43:00 +00001662 ThisDecl = ThisRes.get();
1663 break;
1664 }
1665 }
Mike Stump1eb44332009-09-09 15:08:12 +00001666
Richard Smith34b41d92011-02-20 03:19:35 +00001667 bool TypeContainsAuto =
1668 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1669
Douglas Gregor1426e532009-05-12 21:31:51 +00001670 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001671 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001672 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001673 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001674 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001675 if (D.isFunctionDeclarator())
1676 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1677 << 1 /* delete */;
1678 else
1679 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001680 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001681 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001682 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1683 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001684 else
1685 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001686 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001687 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001688 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001689 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001690 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001691
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001692 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001693 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Peter Collingbourneec98f2f2012-07-27 12:56:09 +00001694 Actions.FinalizeDeclaration(ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001695 cutOffParsing();
1696 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001697 }
Chad Rosier8decdee2012-06-26 22:30:43 +00001698
John McCall60d7b3a2010-08-24 06:29:42 +00001699 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001700
David Blaikie4e4d0842012-03-11 07:00:24 +00001701 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001702 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001703 ExitScope();
1704 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001705
Douglas Gregor1426e532009-05-12 21:31:51 +00001706 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001707 SkipUntil(tok::comma, true, true);
1708 Actions.ActOnInitializerError(ThisDecl);
1709 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001710 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1711 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001712 }
1713 } else if (Tok.is(tok::l_paren)) {
1714 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001715 BalancedDelimiterTracker T(*this, tok::l_paren);
1716 T.consumeOpen();
1717
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00001718 ExprVector Exprs;
Douglas Gregor1426e532009-05-12 21:31:51 +00001719 CommaLocsTy CommaLocs;
1720
David Blaikie4e4d0842012-03-11 07:00:24 +00001721 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001722 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001723 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001724 }
1725
Douglas Gregor1426e532009-05-12 21:31:51 +00001726 if (ParseExpressionList(Exprs, CommaLocs)) {
David Blaikie3ea19c82012-10-10 23:15:05 +00001727 Actions.ActOnInitializerError(ThisDecl);
Douglas Gregor1426e532009-05-12 21:31:51 +00001728 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001729
David Blaikie4e4d0842012-03-11 07:00:24 +00001730 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001731 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001732 ExitScope();
1733 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001734 } else {
1735 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001736 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001737
1738 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1739 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001740
David Blaikie4e4d0842012-03-11 07:00:24 +00001741 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001742 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001743 ExitScope();
1744 }
1745
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001746 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1747 T.getCloseLocation(),
Benjamin Kramer3fe198b2012-08-23 21:35:17 +00001748 Exprs);
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001749 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1750 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001751 }
Richard Smith80ad52f2013-01-02 11:42:31 +00001752 } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace) &&
Fariborz Jahanianb0ed95c2012-07-03 23:22:13 +00001753 (!CurParsedObjCImpl || !D.isFunctionDeclarator())) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001754 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001755 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1756
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001757 if (D.getCXXScopeSpec().isSet()) {
1758 EnterScope(0);
1759 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1760 }
1761
1762 ExprResult Init(ParseBraceInitializer());
1763
1764 if (D.getCXXScopeSpec().isSet()) {
1765 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1766 ExitScope();
1767 }
1768
1769 if (Init.isInvalid()) {
1770 Actions.ActOnInitializerError(ThisDecl);
1771 } else
1772 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1773 /*DirectInit=*/true, TypeContainsAuto);
1774
Douglas Gregor1426e532009-05-12 21:31:51 +00001775 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001776 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001777 }
1778
Richard Smith483b9f32011-02-21 20:05:19 +00001779 Actions.FinalizeDeclaration(ThisDecl);
1780
Douglas Gregor1426e532009-05-12 21:31:51 +00001781 return ThisDecl;
1782}
1783
Reid Spencer5f016e22007-07-11 17:01:13 +00001784/// ParseSpecifierQualifierList
1785/// specifier-qualifier-list:
1786/// type-specifier specifier-qualifier-list[opt]
1787/// type-qualifier specifier-qualifier-list[opt]
1788/// [GNU] attributes specifier-qualifier-list[opt]
1789///
Richard Smith69730c12012-03-12 07:56:15 +00001790void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1791 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001792 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1793 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001794 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001795 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001796
Reid Spencer5f016e22007-07-11 17:01:13 +00001797 // Validate declspec for type-name.
1798 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001799 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1800 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001801 Diag(Tok, diag::err_expected_type);
1802 DS.SetTypeSpecError();
1803 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1804 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001805 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001806 if (!DS.hasTypeSpecifier())
1807 DS.SetTypeSpecError();
1808 }
Mike Stump1eb44332009-09-09 15:08:12 +00001809
Reid Spencer5f016e22007-07-11 17:01:13 +00001810 // Issue diagnostic and remove storage class if present.
1811 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1812 if (DS.getStorageClassSpecLoc().isValid())
1813 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1814 else
1815 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1816 DS.ClearStorageClassSpecs();
1817 }
Mike Stump1eb44332009-09-09 15:08:12 +00001818
Reid Spencer5f016e22007-07-11 17:01:13 +00001819 // Issue diagnostic and remove function specfier if present.
1820 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001821 if (DS.isInlineSpecified())
1822 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1823 if (DS.isVirtualSpecified())
1824 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1825 if (DS.isExplicitSpecified())
1826 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001827 DS.ClearFunctionSpecs();
1828 }
Richard Smith69730c12012-03-12 07:56:15 +00001829
1830 // Issue diagnostic and remove constexpr specfier if present.
1831 if (DS.isConstexprSpecified()) {
1832 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1833 DS.ClearConstexprSpec();
1834 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001835}
1836
Chris Lattnerc199ab32009-04-12 20:42:31 +00001837/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1838/// specified token is valid after the identifier in a declarator which
1839/// immediately follows the declspec. For example, these things are valid:
1840///
1841/// int x [ 4]; // direct-declarator
1842/// int x ( int y); // direct-declarator
1843/// int(int x ) // direct-declarator
1844/// int x ; // simple-declaration
1845/// int x = 17; // init-declarator-list
1846/// int x , y; // init-declarator-list
1847/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001848/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001849/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001850///
1851/// This is not, because 'x' does not immediately follow the declspec (though
1852/// ')' happens to be valid anyway).
1853/// int (x)
1854///
1855static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1856 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1857 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001858 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001859}
1860
Chris Lattnere40c2952009-04-14 21:34:55 +00001861
1862/// ParseImplicitInt - This method is called when we have an non-typename
1863/// identifier in a declspec (which normally terminates the decl spec) when
1864/// the declspec has no type specifier. In this case, the declspec is either
1865/// malformed or is "implicit int" (in K&R and C89).
1866///
1867/// This method handles diagnosing this prettily and returns false if the
1868/// declspec is done being processed. If it recovers and thinks there may be
1869/// other pieces of declspec after it, it returns true.
1870///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001871bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001872 const ParsedTemplateInfo &TemplateInfo,
Michael Han2e397132012-11-26 22:54:45 +00001873 AccessSpecifier AS, DeclSpecContext DSC,
1874 ParsedAttributesWithRange &Attrs) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001875 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001876
Chris Lattnere40c2952009-04-14 21:34:55 +00001877 SourceLocation Loc = Tok.getLocation();
1878 // If we see an identifier that is not a type name, we normally would
1879 // parse it as the identifer being declared. However, when a typename
1880 // is typo'd or the definition is not included, this will incorrectly
1881 // parse the typename as the identifier name and fall over misparsing
1882 // later parts of the diagnostic.
1883 //
1884 // As such, we try to do some look-ahead in cases where this would
1885 // otherwise be an "implicit-int" case to see if this is invalid. For
1886 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1887 // an identifier with implicit int, we'd get a parse error because the
1888 // next token is obviously invalid for a type. Parse these as a case
1889 // with an invalid type specifier.
1890 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001891
Chris Lattnere40c2952009-04-14 21:34:55 +00001892 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001893 // error, do lookahead to try to do better recovery. This never applies
1894 // within a type specifier. Outside of C++, we allow this even if the
1895 // language doesn't "officially" support implicit int -- we support
1896 // implicit int as an extension in C99 and C11. Allegedly, MS also
1897 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001898 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001899 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001900 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001901 // If this token is valid for implicit int, e.g. "static x = 4", then
1902 // we just avoid eating the identifier, so it will be parsed as the
1903 // identifier in the declarator.
1904 return false;
1905 }
Mike Stump1eb44332009-09-09 15:08:12 +00001906
Richard Smith827adaf2012-05-15 21:01:51 +00001907 if (getLangOpts().CPlusPlus &&
1908 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1909 // Don't require a type specifier if we have the 'auto' storage class
1910 // specifier in C++98 -- we'll promote it to a type specifier.
1911 return false;
1912 }
1913
Chris Lattnere40c2952009-04-14 21:34:55 +00001914 // Otherwise, if we don't consume this token, we are going to emit an
1915 // error anyway. Try to recover from various common problems. Check
1916 // to see if this was a reference to a tag name without a tag specified.
1917 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001918 //
1919 // C++ doesn't need this, and isTagName doesn't take SS.
1920 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001921 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001922 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001923
Douglas Gregor23c94db2010-07-02 17:43:08 +00001924 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001925 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001926 case DeclSpec::TST_enum:
1927 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1928 case DeclSpec::TST_union:
1929 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1930 case DeclSpec::TST_struct:
1931 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
Joao Matos6666ed42012-08-31 18:45:21 +00001932 case DeclSpec::TST_interface:
1933 TagName="__interface"; FixitTagName = "__interface ";
1934 TagKind=tok::kw___interface;break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001935 case DeclSpec::TST_class:
1936 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001937 }
Mike Stump1eb44332009-09-09 15:08:12 +00001938
Chris Lattnerf4382f52009-04-14 22:17:06 +00001939 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001940 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1941 LookupResult R(Actions, TokenName, SourceLocation(),
1942 Sema::LookupOrdinaryName);
1943
Chris Lattnerf4382f52009-04-14 22:17:06 +00001944 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001945 << TokenName << TagName << getLangOpts().CPlusPlus
1946 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1947
1948 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1949 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1950 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001951 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001952 << TokenName << TagName;
1953 }
Mike Stump1eb44332009-09-09 15:08:12 +00001954
Chris Lattnerf4382f52009-04-14 22:17:06 +00001955 // Parse this as a tag as if the missing tag were present.
1956 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001957 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001958 else
Richard Smith69730c12012-03-12 07:56:15 +00001959 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
Michael Han2e397132012-11-26 22:54:45 +00001960 /*EnteringContext*/ false, DSC_normal, Attrs);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001961 return true;
1962 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001963 }
Mike Stump1eb44332009-09-09 15:08:12 +00001964
Richard Smith8f0a7e72012-05-15 21:29:55 +00001965 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001966 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001967 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1968 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001969 // Look ahead to the next token to try to figure out what this declaration
1970 // was supposed to be.
1971 switch (NextToken().getKind()) {
1972 case tok::comma:
1973 case tok::equal:
1974 case tok::kw_asm:
1975 case tok::l_brace:
1976 case tok::l_square:
1977 case tok::semi:
1978 // This looks like a variable declaration. The type is probably missing.
1979 // We're done parsing decl-specifiers.
1980 return false;
1981
1982 case tok::l_paren: {
1983 // static x(4); // 'x' is not a type
1984 // x(int n); // 'x' is not a type
1985 // x (*p)[]; // 'x' is a type
1986 //
1987 // Since we're in an error case (or the rare 'implicit int in C++' MS
1988 // extension), we can afford to perform a tentative parse to determine
1989 // which case we're in.
1990 TentativeParsingAction PA(*this);
1991 ConsumeToken();
1992 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1993 PA.Revert();
1994 if (TPR == TPResult::False())
1995 return false;
1996 // The identifier is followed by a parenthesized declarator.
1997 // It's supposed to be a type.
1998 break;
1999 }
2000
2001 default:
2002 // This is probably supposed to be a type. This includes cases like:
2003 // int f(itn);
2004 // struct S { unsinged : 4; };
2005 break;
2006 }
2007 }
2008
Chad Rosier8decdee2012-06-26 22:30:43 +00002009 // This is almost certainly an invalid type name. Let the action emit a
Douglas Gregora786fdb2009-10-13 23:27:22 +00002010 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00002011 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002012 IdentifierInfo *II = Tok.getIdentifierInfo();
2013 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00002014 // The action emitted a diagnostic, so we don't have to.
2015 if (T) {
2016 // The action has suggested that the type T could be used. Set that as
2017 // the type in the declaration specifiers, consume the would-be type
2018 // name token, and we're done.
2019 const char *PrevSpec;
2020 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00002021 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00002022 DS.SetRangeEnd(Tok.getLocation());
2023 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00002024 // There may be other declaration specifiers after this.
2025 return true;
2026 } else if (II != Tok.getIdentifierInfo()) {
2027 // If no type was suggested, the correction is to a keyword
2028 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00002029 // There may be other declaration specifiers after this.
2030 return true;
2031 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002032
Douglas Gregora786fdb2009-10-13 23:27:22 +00002033 // Fall through; the action had no suggestion for us.
2034 } else {
2035 // The action did not emit a diagnostic, so emit one now.
2036 SourceRange R;
2037 if (SS) R = SS->getRange();
2038 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
2039 }
Mike Stump1eb44332009-09-09 15:08:12 +00002040
Douglas Gregora786fdb2009-10-13 23:27:22 +00002041 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00002042 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00002043 DS.SetRangeEnd(Tok.getLocation());
2044 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002045
Chris Lattnere40c2952009-04-14 21:34:55 +00002046 // TODO: Could inject an invalid typedef decl in an enclosing scope to
2047 // avoid rippling error messages on subsequent uses of the same type,
2048 // could be useful if #include was forgotten.
2049 return false;
2050}
2051
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002052/// \brief Determine the declaration specifier context from the declarator
2053/// context.
2054///
2055/// \param Context the declarator context, which is one of the
2056/// Declarator::TheContext enumerator values.
Chad Rosier8decdee2012-06-26 22:30:43 +00002057Parser::DeclSpecContext
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002058Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
2059 if (Context == Declarator::MemberContext)
2060 return DSC_class;
2061 if (Context == Declarator::FileContext)
2062 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00002063 if (Context == Declarator::TrailingReturnContext)
2064 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002065 return DSC_normal;
2066}
2067
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002068/// ParseAlignArgument - Parse the argument to an alignment-specifier.
2069///
2070/// FIXME: Simply returns an alignof() expression if the argument is a
2071/// type. Ideally, the type should be propagated directly into Sema.
2072///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002073/// [C11] type-id
2074/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002075/// [C++0x] type-id ...[opt]
2076/// [C++0x] assignment-expression ...[opt]
2077ExprResult Parser::ParseAlignArgument(SourceLocation Start,
2078 SourceLocation &EllipsisLoc) {
2079 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002080 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002081 SourceLocation TypeLoc = Tok.getLocation();
2082 ParsedType Ty = ParseTypeName().get();
2083 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002084 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
2085 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002086 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002087 ER = ParseConstantExpression();
2088
Richard Smith80ad52f2013-01-02 11:42:31 +00002089 if (getLangOpts().CPlusPlus11 && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00002090 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002091
2092 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002093}
2094
2095/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
2096/// attribute to Attrs.
2097///
2098/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002099/// [C11] '_Alignas' '(' type-id ')'
2100/// [C11] '_Alignas' '(' constant-expression ')'
Richard Smith33f04a22013-01-29 01:48:07 +00002101/// [C++11] 'alignas' '(' type-id ...[opt] ')'
2102/// [C++11] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002103void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
Richard Smithf6565a92013-02-22 08:32:16 +00002104 SourceLocation *EndLoc) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002105 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
2106 "Not an alignment-specifier!");
2107
Richard Smith33f04a22013-01-29 01:48:07 +00002108 IdentifierInfo *KWName = Tok.getIdentifierInfo();
2109 SourceLocation KWLoc = ConsumeToken();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002110
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002111 BalancedDelimiterTracker T(*this, tok::l_paren);
2112 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002113 return;
2114
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002115 SourceLocation EllipsisLoc;
2116 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002117 if (ArgExpr.isInvalid()) {
2118 SkipUntil(tok::r_paren);
2119 return;
2120 }
2121
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002122 T.consumeClose();
Richard Smithf6565a92013-02-22 08:32:16 +00002123 if (EndLoc)
2124 *EndLoc = T.getCloseLocation();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00002125
Benjamin Kramer4e28d9e2012-08-23 22:51:59 +00002126 ExprVector ArgExprs;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002127 ArgExprs.push_back(ArgExpr.release());
Richard Smith33f04a22013-01-29 01:48:07 +00002128 Attrs.addNew(KWName, KWLoc, 0, KWLoc, 0, T.getOpenLocation(),
Richard Smithf6565a92013-02-22 08:32:16 +00002129 ArgExprs.data(), 1, AttributeList::AS_Keyword, EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002130}
2131
Reid Spencer5f016e22007-07-11 17:01:13 +00002132/// ParseDeclarationSpecifiers
2133/// declaration-specifiers: [C99 6.7]
2134/// storage-class-specifier declaration-specifiers[opt]
2135/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002136/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002137/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002138/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002139/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002140///
2141/// storage-class-specifier: [C99 6.7.1]
2142/// 'typedef'
2143/// 'extern'
2144/// 'static'
2145/// 'auto'
2146/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002147/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002148/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002149/// function-specifier: [C99 6.7.4]
2150/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002151/// [C++] 'virtual'
2152/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002153/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002154/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002155/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002156
Reid Spencer5f016e22007-07-11 17:01:13 +00002157///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002158void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002159 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002160 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002161 DeclSpecContext DSContext,
2162 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002163 if (DS.getSourceRange().isInvalid()) {
2164 DS.SetRangeStart(Tok.getLocation());
2165 DS.SetRangeEnd(Tok.getLocation());
2166 }
Chad Rosier8decdee2012-06-26 22:30:43 +00002167
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002168 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002169 bool AttrsLastTime = false;
2170 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002171 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002172 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002173 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002174 unsigned DiagID = 0;
2175
Reid Spencer5f016e22007-07-11 17:01:13 +00002176 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002177
Reid Spencer5f016e22007-07-11 17:01:13 +00002178 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002179 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002180 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002181 if (!AttrsLastTime)
2182 ProhibitAttributes(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002183 else {
2184 // Reject C++11 attributes that appertain to decl specifiers as
2185 // we don't support any C++11 attributes that appertain to decl
2186 // specifiers. This also conforms to what g++ 4.8 is doing.
2187 ProhibitCXX11Attributes(attrs);
2188
Sean Hunt2edf0a22012-06-23 05:07:58 +00002189 DS.takeAttributesFrom(attrs);
Michael Hanf64231e2012-11-06 19:34:54 +00002190 }
Peter Collingbournef1907682011-09-29 18:03:57 +00002191
Reid Spencer5f016e22007-07-11 17:01:13 +00002192 // If this is not a declaration specifier token, we're done reading decl
2193 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002194 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002195 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002196
Sean Hunt2edf0a22012-06-23 05:07:58 +00002197 case tok::l_square:
2198 case tok::kw_alignas:
2199 if (!isCXX11AttributeSpecifier())
2200 goto DoneWithDeclSpec;
2201
2202 ProhibitAttributes(attrs);
2203 // FIXME: It would be good to recover by accepting the attributes,
2204 // but attempting to do that now would cause serious
2205 // madness in terms of diagnostics.
2206 attrs.clear();
2207 attrs.Range = SourceRange();
2208
2209 ParseCXX11Attributes(attrs);
2210 AttrsLastTime = true;
Chad Rosier8decdee2012-06-26 22:30:43 +00002211 continue;
Sean Hunt2edf0a22012-06-23 05:07:58 +00002212
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002213 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002214 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002215 if (DS.hasTypeSpecifier()) {
2216 bool AllowNonIdentifiers
2217 = (getCurScope()->getFlags() & (Scope::ControlScope |
2218 Scope::BlockScope |
2219 Scope::TemplateParamScope |
2220 Scope::FunctionPrototypeScope |
2221 Scope::AtCatchScope)) == 0;
2222 bool AllowNestedNameSpecifiers
Chad Rosier8decdee2012-06-26 22:30:43 +00002223 = DSContext == DSC_top_level ||
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002224 (DSContext == DSC_class && DS.isFriendSpecified());
2225
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002226 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
Chad Rosier8decdee2012-06-26 22:30:43 +00002227 AllowNonIdentifiers,
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002228 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002229 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00002230 }
2231
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002232 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2233 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2234 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
Chad Rosier8decdee2012-06-26 22:30:43 +00002235 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
John McCallf312b1e2010-08-26 23:41:50 +00002236 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002237 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002238 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002239 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002240 CCC = Sema::PCC_ObjCImplementation;
Chad Rosier8decdee2012-06-26 22:30:43 +00002241
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002242 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002243 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002244 }
2245
Chris Lattner5e02c472009-01-05 00:07:25 +00002246 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002247 // C++ scope specifier. Annotate and loop, or bail out on error.
2248 if (TryAnnotateCXXScopeToken(true)) {
2249 if (!DS.hasTypeSpecifier())
2250 DS.SetTypeSpecError();
2251 goto DoneWithDeclSpec;
2252 }
John McCall2e0a7152010-03-01 18:20:46 +00002253 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2254 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002255 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002256
2257 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002258 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002259 goto DoneWithDeclSpec;
2260
John McCallaa87d332009-12-12 11:40:51 +00002261 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002262 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2263 Tok.getAnnotationRange(),
2264 SS);
John McCallaa87d332009-12-12 11:40:51 +00002265
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002266 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002267 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002268 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002269 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002270 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002271 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002272
2273 // C++ [class.qual]p2:
2274 // In a lookup in which the constructor is an acceptable lookup
2275 // result and the nested-name-specifier nominates a class C:
2276 //
2277 // - if the name specified after the
2278 // nested-name-specifier, when looked up in C, is the
2279 // injected-class-name of C (Clause 9), or
2280 //
2281 // - if the name specified after the nested-name-specifier
2282 // is the same as the identifier or the
2283 // simple-template-id's template-name in the last
2284 // component of the nested-name-specifier,
2285 //
2286 // the name is instead considered to name the constructor of
2287 // class C.
Chad Rosier8decdee2012-06-26 22:30:43 +00002288 //
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002289 // Thus, if the template-name is actually the constructor
2290 // name, then the code is ill-formed; this interpretation is
Chad Rosier8decdee2012-06-26 22:30:43 +00002291 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002292 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002293 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
John McCallba9d8532010-04-13 06:39:49 +00002294 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002295 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002296 if (isConstructorDeclarator()) {
2297 // The user meant this to be an out-of-line constructor
2298 // definition, but template arguments are not allowed
2299 // there. Just allow this as a constructor; we'll
2300 // complain about it later.
2301 goto DoneWithDeclSpec;
2302 }
2303
2304 // The user meant this to name a type, but it actually names
2305 // a constructor with some extraneous template
2306 // arguments. Complain, then parse it as a type as the user
2307 // intended.
2308 Diag(TemplateId->TemplateNameLoc,
2309 diag::err_out_of_line_template_id_names_constructor)
2310 << TemplateId->Name;
2311 }
2312
John McCallaa87d332009-12-12 11:40:51 +00002313 DS.getTypeSpecScope() = SS;
2314 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002315 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002316 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002317 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002318 continue;
2319 }
2320
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002321 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002322 DS.getTypeSpecScope() = SS;
2323 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002324 if (Tok.getAnnotationValue()) {
2325 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002326 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
Chad Rosier8decdee2012-06-26 22:30:43 +00002327 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002328 PrevSpec, DiagID, T);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002329 if (isInvalid)
2330 break;
John McCallb3d87482010-08-24 05:47:05 +00002331 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002332 else
2333 DS.SetTypeSpecError();
2334 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2335 ConsumeToken(); // The typename
2336 }
2337
Douglas Gregor9135c722009-03-25 15:40:00 +00002338 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002339 goto DoneWithDeclSpec;
2340
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002341 // If we're in a context where the identifier could be a class name,
2342 // check whether this is a constructor declaration.
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00002343 if ((DSContext == DSC_top_level || DSContext == DSC_class) &&
Chad Rosier8decdee2012-06-26 22:30:43 +00002344 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002345 &SS)) {
2346 if (isConstructorDeclarator())
2347 goto DoneWithDeclSpec;
2348
2349 // As noted in C++ [class.qual]p2 (cited above), when the name
2350 // of the class is qualified in a context where it could name
2351 // a constructor, its a constructor name. However, we've
2352 // looked at the declarator, and the user probably meant this
2353 // to be a type. Complain that it isn't supposed to be treated
2354 // as a type, then proceed to parse it as a type.
2355 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2356 << Next.getIdentifierInfo();
2357 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002358
John McCallb3d87482010-08-24 05:47:05 +00002359 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2360 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002361 getCurScope(), &SS,
2362 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002363 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002364 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002365
Chris Lattnerf4382f52009-04-14 22:17:06 +00002366 // If the referenced identifier is not a type, then this declspec is
2367 // erroneous: We already checked about that it has no type specifier, and
2368 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002369 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002370 if (TypeRep == 0) {
2371 ConsumeToken(); // Eat the scope spec so the identifier is current.
Michael Han2e397132012-11-26 22:54:45 +00002372 ParsedAttributesWithRange Attrs(AttrFactory);
2373 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext, Attrs)) {
2374 if (!Attrs.empty()) {
2375 AttrsLastTime = true;
2376 attrs.takeAllFrom(Attrs);
2377 }
2378 continue;
2379 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002380 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002381 }
Mike Stump1eb44332009-09-09 15:08:12 +00002382
John McCallaa87d332009-12-12 11:40:51 +00002383 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002384 ConsumeToken(); // The C++ scope.
2385
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002386 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002387 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002388 if (isInvalid)
2389 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002390
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002391 DS.SetRangeEnd(Tok.getLocation());
2392 ConsumeToken(); // The typename.
2393
2394 continue;
2395 }
Mike Stump1eb44332009-09-09 15:08:12 +00002396
Chris Lattner80d0c892009-01-21 19:48:37 +00002397 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002398 if (Tok.getAnnotationValue()) {
2399 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002400 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002401 DiagID, T);
2402 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002403 DS.SetTypeSpecError();
Chad Rosier8decdee2012-06-26 22:30:43 +00002404
Chris Lattner5c5db552010-04-05 18:18:31 +00002405 if (isInvalid)
2406 break;
2407
Chris Lattner80d0c892009-01-21 19:48:37 +00002408 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2409 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002410
Chris Lattner80d0c892009-01-21 19:48:37 +00002411 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2412 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002413 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002414 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002415 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002416
Chris Lattner80d0c892009-01-21 19:48:37 +00002417 continue;
2418 }
Mike Stump1eb44332009-09-09 15:08:12 +00002419
Douglas Gregorbfad9152011-04-28 15:48:45 +00002420 case tok::kw___is_signed:
2421 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2422 // typically treats it as a trait. If we see __is_signed as it appears
2423 // in libstdc++, e.g.,
2424 //
2425 // static const bool __is_signed;
2426 //
2427 // then treat __is_signed as an identifier rather than as a keyword.
2428 if (DS.getTypeSpecType() == TST_bool &&
2429 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2430 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2431 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2432 Tok.setKind(tok::identifier);
2433 }
2434
2435 // We're done with the declaration-specifiers.
2436 goto DoneWithDeclSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00002437
Chris Lattner3bd934a2008-07-26 01:18:38 +00002438 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002439 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002440 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002441 // In C++, check to see if this is a scope specifier like foo::bar::, if
2442 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002443 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002444 if (TryAnnotateCXXScopeToken(true)) {
2445 if (!DS.hasTypeSpecifier())
2446 DS.SetTypeSpecError();
2447 goto DoneWithDeclSpec;
2448 }
2449 if (!Tok.is(tok::identifier))
2450 continue;
2451 }
Mike Stump1eb44332009-09-09 15:08:12 +00002452
Chris Lattner3bd934a2008-07-26 01:18:38 +00002453 // This identifier can only be a typedef name if we haven't already seen
2454 // a type-specifier. Without this check we misparse:
2455 // typedef int X; struct Y { short X; }; as 'short int'.
2456 if (DS.hasTypeSpecifier())
2457 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002458
John Thompson82287d12010-02-05 00:12:22 +00002459 // Check for need to substitute AltiVec keyword tokens.
2460 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2461 break;
2462
Richard Smithf63eee72012-05-09 18:56:43 +00002463 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2464 // allow the use of a typedef name as a type specifier.
2465 if (DS.isTypeAltiVecVector())
2466 goto DoneWithDeclSpec;
2467
John McCallb3d87482010-08-24 05:47:05 +00002468 ParsedType TypeRep =
2469 Actions.getTypeName(*Tok.getIdentifierInfo(),
2470 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002471
Chris Lattnerc199ab32009-04-12 20:42:31 +00002472 // If this is not a typedef name, don't parse it as part of the declspec,
2473 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002474 if (!TypeRep) {
Michael Han2e397132012-11-26 22:54:45 +00002475 ParsedAttributesWithRange Attrs(AttrFactory);
2476 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext, Attrs)) {
2477 if (!Attrs.empty()) {
2478 AttrsLastTime = true;
2479 attrs.takeAllFrom(Attrs);
2480 }
2481 continue;
2482 }
Chris Lattner3bd934a2008-07-26 01:18:38 +00002483 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002484 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002485
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002486 // If we're in a context where the identifier could be a class name,
2487 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002488 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002489 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002490 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002491 goto DoneWithDeclSpec;
2492
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002493 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002494 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002495 if (isInvalid)
2496 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002497
Chris Lattner3bd934a2008-07-26 01:18:38 +00002498 DS.SetRangeEnd(Tok.getLocation());
2499 ConsumeToken(); // The identifier
2500
2501 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2502 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Chad Rosier8decdee2012-06-26 22:30:43 +00002503 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002504 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002505 ParseObjCProtocolQualifiers(DS);
Chad Rosier8decdee2012-06-26 22:30:43 +00002506
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002507 // Need to support trailing type qualifiers (e.g. "id<p> const").
2508 // If a type specifier follows, it will be diagnosed elsewhere.
2509 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002510 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002511
2512 // type-name
2513 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002514 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002515 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002516 // This template-id does not refer to a type name, so we're
2517 // done with the type-specifiers.
2518 goto DoneWithDeclSpec;
2519 }
2520
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002521 // If we're in a context where the template-id could be a
2522 // constructor name or specialization, check whether this is a
2523 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002524 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002525 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002526 isConstructorDeclarator())
2527 goto DoneWithDeclSpec;
2528
Douglas Gregor39a8de12009-02-25 19:37:18 +00002529 // Turn the template-id annotation token into a type annotation
2530 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002531 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002532 continue;
2533 }
2534
Reid Spencer5f016e22007-07-11 17:01:13 +00002535 // GNU attributes support.
2536 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002537 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002538 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002539
2540 // Microsoft declspec support.
2541 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002542 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002543 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002544
Steve Naroff239f0732008-12-25 14:16:32 +00002545 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002546 case tok::kw___forceinline: {
Chad Rosier22aa6902012-12-21 22:24:43 +00002547 isInvalid = DS.setFunctionSpecInline(Loc);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002548 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
Richard Smithb3cd3c02012-09-14 18:27:01 +00002549 SourceLocation AttrNameLoc = Tok.getLocation();
Sean Hunt93f95f22012-06-18 16:13:52 +00002550 // FIXME: This does not work correctly if it is set to be a declspec
2551 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002552 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002553 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Richard Smithb3cd3c02012-09-14 18:27:01 +00002554 break;
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002555 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002556
2557 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002558 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002559 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002560 case tok::kw___cdecl:
2561 case tok::kw___stdcall:
2562 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002563 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002564 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002565 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002566 continue;
2567
Dawn Perchik52fc3142010-09-03 01:29:35 +00002568 // Borland single token adornments.
2569 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002570 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002571 continue;
2572
Peter Collingbournef315fa82011-02-14 01:42:53 +00002573 // OpenCL single token adornments.
2574 case tok::kw___kernel:
2575 ParseOpenCLAttributes(DS.getAttributes());
2576 continue;
2577
Reid Spencer5f016e22007-07-11 17:01:13 +00002578 // storage-class-specifier
2579 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002580 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2581 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002582 break;
2583 case tok::kw_extern:
2584 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002585 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002586 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2587 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002588 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002589 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002590 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2591 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002592 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002593 case tok::kw_static:
2594 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002595 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002596 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2597 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002598 break;
2599 case tok::kw_auto:
Richard Smith80ad52f2013-01-02 11:42:31 +00002600 if (getLangOpts().CPlusPlus11) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002601 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002602 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2603 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002604 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002605 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002606 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002607 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002608 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2609 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002610 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002611 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2612 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002613 break;
2614 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002615 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2616 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002617 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002618 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002619 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2620 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002621 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002622 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002623 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002624 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002625
Reid Spencer5f016e22007-07-11 17:01:13 +00002626 // function-specifier
2627 case tok::kw_inline:
Chad Rosier22aa6902012-12-21 22:24:43 +00002628 isInvalid = DS.setFunctionSpecInline(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00002629 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002630 case tok::kw_virtual:
Chad Rosier22aa6902012-12-21 22:24:43 +00002631 isInvalid = DS.setFunctionSpecVirtual(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002632 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002633 case tok::kw_explicit:
Chad Rosier22aa6902012-12-21 22:24:43 +00002634 isInvalid = DS.setFunctionSpecExplicit(Loc);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002635 break;
Richard Smithde03c152013-01-17 22:16:11 +00002636 case tok::kw__Noreturn:
2637 if (!getLangOpts().C11)
2638 Diag(Loc, diag::ext_c11_noreturn);
2639 isInvalid = DS.setFunctionSpecNoreturn(Loc);
2640 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002641
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002642 // alignment-specifier
2643 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002644 if (!getLangOpts().C11)
Jordan Rosef70a8862012-06-30 21:33:57 +00002645 Diag(Tok, diag::ext_c11_alignment) << Tok.getName();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002646 ParseAlignmentSpecifier(DS.getAttributes());
2647 continue;
2648
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002649 // friend
2650 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002651 if (DSContext == DSC_class)
2652 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2653 else {
2654 PrevSpec = ""; // not actually used by the diagnostic
2655 DiagID = diag::err_friend_invalid_in_context;
2656 isInvalid = true;
2657 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002658 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002659
Douglas Gregor8d267c52011-09-09 02:06:17 +00002660 // Modules
2661 case tok::kw___module_private__:
2662 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2663 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002664
Sebastian Redl2ac67232009-11-05 15:47:02 +00002665 // constexpr
2666 case tok::kw_constexpr:
2667 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2668 break;
2669
Chris Lattner80d0c892009-01-21 19:48:37 +00002670 // type-specifier
2671 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002672 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2673 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002674 break;
2675 case tok::kw_long:
2676 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002677 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2678 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002679 else
John McCallfec54012009-08-03 20:12:06 +00002680 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2681 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002682 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002683 case tok::kw___int64:
2684 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2685 DiagID);
2686 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002687 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002688 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2689 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002690 break;
2691 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002692 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2693 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002694 break;
2695 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002696 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2697 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002698 break;
2699 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002700 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2701 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002702 break;
2703 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002704 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2705 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002706 break;
2707 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002708 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2709 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002710 break;
2711 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002712 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2713 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002714 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002715 case tok::kw___int128:
2716 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2717 DiagID);
2718 break;
2719 case tok::kw_half:
2720 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2721 DiagID);
2722 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002723 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002724 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2725 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002726 break;
2727 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002728 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2729 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002730 break;
2731 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002732 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2733 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002734 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002735 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002736 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2737 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002738 break;
2739 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002740 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2741 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002742 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002743 case tok::kw_bool:
2744 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002745 if (Tok.is(tok::kw_bool) &&
2746 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2747 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2748 PrevSpec = ""; // Not used by the diagnostic.
2749 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002750 // For better error recovery.
2751 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002752 isInvalid = true;
2753 } else {
2754 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2755 DiagID);
2756 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002757 break;
2758 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002759 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2760 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002761 break;
2762 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002763 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2764 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002765 break;
2766 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002767 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2768 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002769 break;
John Thompson82287d12010-02-05 00:12:22 +00002770 case tok::kw___vector:
2771 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2772 break;
2773 case tok::kw___pixel:
2774 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2775 break;
Guy Benyeib13621d2012-12-18 14:38:23 +00002776 case tok::kw_image1d_t:
2777 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_t, Loc,
2778 PrevSpec, DiagID);
2779 break;
2780 case tok::kw_image1d_array_t:
2781 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_array_t, Loc,
2782 PrevSpec, DiagID);
2783 break;
2784 case tok::kw_image1d_buffer_t:
2785 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image1d_buffer_t, Loc,
2786 PrevSpec, DiagID);
2787 break;
2788 case tok::kw_image2d_t:
2789 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_t, Loc,
2790 PrevSpec, DiagID);
2791 break;
2792 case tok::kw_image2d_array_t:
2793 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image2d_array_t, Loc,
2794 PrevSpec, DiagID);
2795 break;
2796 case tok::kw_image3d_t:
2797 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_image3d_t, Loc,
2798 PrevSpec, DiagID);
2799 break;
Guy Benyei21f18c42013-02-07 10:55:47 +00002800 case tok::kw_sampler_t:
2801 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_sampler_t, Loc,
2802 PrevSpec, DiagID);
2803 break;
Guy Benyeie6b9d802013-01-20 12:31:11 +00002804 case tok::kw_event_t:
2805 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_event_t, Loc,
2806 PrevSpec, DiagID);
2807 break;
John McCalla5fc4722011-04-09 22:50:59 +00002808 case tok::kw___unknown_anytype:
2809 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2810 PrevSpec, DiagID);
2811 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002812
2813 // class-specifier:
2814 case tok::kw_class:
2815 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00002816 case tok::kw___interface:
Chris Lattner4c97d762009-04-12 21:49:30 +00002817 case tok::kw_union: {
2818 tok::TokenKind Kind = Tok.getKind();
2819 ConsumeToken();
Michael Han2e397132012-11-26 22:54:45 +00002820
2821 // These are attributes following class specifiers.
2822 // To produce better diagnostic, we parse them when
2823 // parsing class specifier.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002824 ParsedAttributesWithRange Attributes(AttrFactory);
Richard Smith69730c12012-03-12 07:56:15 +00002825 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
Bill Wendlingad017fa2012-12-20 19:22:21 +00002826 EnteringContext, DSContext, Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002827
2828 // If there are attributes following class specifier,
2829 // take them over and handle them here.
Bill Wendlingad017fa2012-12-20 19:22:21 +00002830 if (!Attributes.empty()) {
Michael Han2e397132012-11-26 22:54:45 +00002831 AttrsLastTime = true;
Bill Wendlingad017fa2012-12-20 19:22:21 +00002832 attrs.takeAllFrom(Attributes);
Michael Han2e397132012-11-26 22:54:45 +00002833 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002834 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002835 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002836
2837 // enum-specifier:
2838 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002839 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002840 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002841 continue;
2842
2843 // cv-qualifier:
2844 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002845 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002846 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002847 break;
2848 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002849 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002850 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002851 break;
2852 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002853 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00002854 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002855 break;
2856
Douglas Gregord57959a2009-03-27 23:10:48 +00002857 // C++ typename-specifier:
2858 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002859 if (TryAnnotateTypeOrScopeToken()) {
2860 DS.SetTypeSpecError();
2861 goto DoneWithDeclSpec;
2862 }
2863 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002864 continue;
2865 break;
2866
Chris Lattner80d0c892009-01-21 19:48:37 +00002867 // GNU typeof support.
2868 case tok::kw_typeof:
2869 ParseTypeofSpecifier(DS);
2870 continue;
2871
David Blaikie42d6d0c2011-12-04 05:04:18 +00002872 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002873 ParseDecltypeSpecifier(DS);
2874 continue;
2875
Sean Huntdb5d44b2011-05-19 05:37:45 +00002876 case tok::kw___underlying_type:
2877 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002878 continue;
2879
2880 case tok::kw__Atomic:
2881 ParseAtomicSpecifier(DS);
2882 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002883
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002884 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00002885 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002886 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002887 goto DoneWithDeclSpec;
2888 case tok::kw___private:
2889 case tok::kw___global:
2890 case tok::kw___local:
2891 case tok::kw___constant:
2892 case tok::kw___read_only:
2893 case tok::kw___write_only:
2894 case tok::kw___read_write:
2895 ParseOpenCLQualifiers(DS);
2896 break;
Chad Rosier8decdee2012-06-26 22:30:43 +00002897
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002898 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002899 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002900 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2901 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002902 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002903 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002904
Douglas Gregor46f936e2010-11-19 17:10:50 +00002905 if (!ParseObjCProtocolQualifiers(DS))
2906 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2907 << FixItHint::CreateInsertion(Loc, "id")
2908 << SourceRange(Loc, DS.getSourceRange().getEnd());
Chad Rosier8decdee2012-06-26 22:30:43 +00002909
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002910 // Need to support trailing type qualifiers (e.g. "id<p> const").
2911 // If a type specifier follows, it will be diagnosed elsewhere.
2912 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002913 }
John McCallfec54012009-08-03 20:12:06 +00002914 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002915 if (isInvalid) {
2916 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002917 assert(DiagID);
Chad Rosier8decdee2012-06-26 22:30:43 +00002918
Douglas Gregorae2fb142010-08-23 14:34:43 +00002919 if (DiagID == diag::ext_duplicate_declspec)
2920 Diag(Tok, DiagID)
2921 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2922 else
2923 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002924 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002925
Chris Lattner81c018d2008-03-13 06:29:04 +00002926 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002927 if (DiagID != diag::err_bool_redeclaration)
2928 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00002929
2930 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002931 }
2932}
Douglas Gregoradcac882008-12-01 23:54:00 +00002933
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002934/// ParseStructDeclaration - Parse a struct declaration without the terminating
2935/// semicolon.
2936///
Reid Spencer5f016e22007-07-11 17:01:13 +00002937/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002938/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002939/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002940/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002941/// struct-declarator-list:
2942/// struct-declarator
2943/// struct-declarator-list ',' struct-declarator
2944/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2945/// struct-declarator:
2946/// declarator
2947/// [GNU] declarator attributes[opt]
2948/// declarator[opt] ':' constant-expression
2949/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2950///
Chris Lattnere1359422008-04-10 06:46:29 +00002951void Parser::
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002952ParseStructDeclaration(ParsingDeclSpec &DS, FieldCallback &Fields) {
Chad Rosier8decdee2012-06-26 22:30:43 +00002953
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002954 if (Tok.is(tok::kw___extension__)) {
2955 // __extension__ silences extension warnings in the subexpression.
2956 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002957 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002958 return ParseStructDeclaration(DS, Fields);
2959 }
Mike Stump1eb44332009-09-09 15:08:12 +00002960
Steve Naroff28a7ca82007-08-20 22:28:22 +00002961 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002962 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002963
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002964 // If there are no declarators, this is a free-standing declaration
2965 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002966 if (Tok.is(tok::semi)) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002967 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
2968 DS);
2969 DS.complete(TheDecl);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002970 return;
2971 }
2972
2973 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002974 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002975 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002976 while (1) {
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00002977 ParsingFieldDeclarator DeclaratorInfo(*this, DS);
Richard Smith7984de32012-01-12 23:53:29 +00002978 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002979
Bill Wendlingad017fa2012-12-20 19:22:21 +00002980 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002981 if (!FirstDeclarator)
2982 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002983
Steve Naroff28a7ca82007-08-20 22:28:22 +00002984 /// struct-declarator: declarator
2985 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002986 if (Tok.isNot(tok::colon)) {
2987 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2988 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002989 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002990 }
Mike Stump1eb44332009-09-09 15:08:12 +00002991
Chris Lattner04d66662007-10-09 17:33:22 +00002992 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002993 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002994 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002995 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002996 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002997 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002998 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002999 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00003000
Steve Naroff28a7ca82007-08-20 22:28:22 +00003001 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003002 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003003
John McCallbdd563e2009-11-03 02:38:08 +00003004 // We're done with this declarator; invoke the callback.
Eli Friedman817a8862012-08-08 23:35:12 +00003005 Fields.invoke(DeclaratorInfo);
John McCallbdd563e2009-11-03 02:38:08 +00003006
Steve Naroff28a7ca82007-08-20 22:28:22 +00003007 // If we don't have a comma, it is either the end of the list (a ';')
3008 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00003009 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00003010 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00003011
Steve Naroff28a7ca82007-08-20 22:28:22 +00003012 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00003013 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003014
John McCallbdd563e2009-11-03 02:38:08 +00003015 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00003016 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00003017}
3018
3019/// ParseStructUnionBody
3020/// struct-contents:
3021/// struct-declaration-list
3022/// [EXT] empty
3023/// [GNU] "struct-declaration-list" without terminatoring ';'
3024/// struct-declaration-list:
3025/// struct-declaration
3026/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003027/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00003028///
Reid Spencer5f016e22007-07-11 17:01:13 +00003029void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00003030 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00003031 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
3032 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00003033
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003034 BalancedDelimiterTracker T(*this, tok::l_brace);
3035 if (T.consumeOpen())
3036 return;
Mike Stump1eb44332009-09-09 15:08:12 +00003037
Douglas Gregor3218c4b2009-01-09 22:42:13 +00003038 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003039 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00003040
Reid Spencer5f016e22007-07-11 17:01:13 +00003041 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
3042 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00003043 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00003044 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
3045 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
3046 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003047
Chris Lattner5f9e2722011-07-23 10:55:15 +00003048 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00003049
Reid Spencer5f016e22007-07-11 17:01:13 +00003050 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00003051 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003052 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00003053
Reid Spencer5f016e22007-07-11 17:01:13 +00003054 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00003055 if (Tok.is(tok::semi)) {
Richard Smitheab9d6f2012-07-23 05:45:25 +00003056 ConsumeExtraSemi(InsideStruct, TagType);
Reid Spencer5f016e22007-07-11 17:01:13 +00003057 continue;
3058 }
Chris Lattnere1359422008-04-10 06:46:29 +00003059
John McCallbdd563e2009-11-03 02:38:08 +00003060 if (!Tok.is(tok::at)) {
3061 struct CFieldCallback : FieldCallback {
3062 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00003063 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00003064 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00003065
John McCalld226f652010-08-21 09:40:31 +00003066 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00003067 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00003068 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
3069
Eli Friedmandcdff462012-08-08 23:53:27 +00003070 void invoke(ParsingFieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00003071 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00003072 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00003073 FD.D.getDeclSpec().getSourceRange().getBegin(),
3074 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00003075 FieldDecls.push_back(Field);
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003076 FD.complete(Field);
Douglas Gregor91a28862009-08-26 14:27:30 +00003077 }
John McCallbdd563e2009-11-03 02:38:08 +00003078 } Callback(*this, TagDecl, FieldDecls);
3079
Eli Friedmanf66a0dd2012-08-08 23:04:35 +00003080 // Parse all the comma separated declarators.
3081 ParsingDeclSpec DS(*this);
John McCallbdd563e2009-11-03 02:38:08 +00003082 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003083 } else { // Handle @defs
3084 ConsumeToken();
3085 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
3086 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003087 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003088 continue;
3089 }
3090 ConsumeToken();
3091 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
3092 if (!Tok.is(tok::identifier)) {
3093 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003094 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003095 continue;
3096 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00003097 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00003098 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00003099 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00003100 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
3101 ConsumeToken();
3102 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00003103 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003104
Chris Lattner04d66662007-10-09 17:33:22 +00003105 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003106 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00003107 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003108 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00003109 break;
3110 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00003111 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
3112 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00003113 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00003114 // If we stopped at a ';', eat it.
3115 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003116 }
3117 }
Mike Stump1eb44332009-09-09 15:08:12 +00003118
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003119 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00003120
John McCall0b7e6782011-03-24 11:26:52 +00003121 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003122 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00003123 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00003124
Douglas Gregor23c94db2010-07-02 17:43:08 +00003125 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00003126 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003127 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00003128 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00003129 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003130 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
3131 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003132}
3133
Reid Spencer5f016e22007-07-11 17:01:13 +00003134/// ParseEnumSpecifier
3135/// enum-specifier: [C99 6.7.2.2]
3136/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003137///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003138/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
3139/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00003140/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
3141/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00003142/// 'enum' identifier
3143/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003144///
Richard Smith1af83c42012-03-23 03:33:32 +00003145/// [C++11] enum-head '{' enumerator-list[opt] '}'
3146/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003147///
Richard Smith1af83c42012-03-23 03:33:32 +00003148/// enum-head: [C++11]
3149/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
3150/// enum-key attribute-specifier-seq[opt] nested-name-specifier
3151/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003152///
Richard Smith1af83c42012-03-23 03:33:32 +00003153/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003154/// 'enum'
3155/// 'enum' 'class'
3156/// 'enum' 'struct'
3157///
Richard Smith1af83c42012-03-23 03:33:32 +00003158/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003159/// ':' type-specifier-seq
3160///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003161/// [C++] elaborated-type-specifier:
3162/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
3163///
Chris Lattner4c97d762009-04-12 21:49:30 +00003164void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00003165 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00003166 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003167 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00003168 if (Tok.is(tok::code_completion)) {
3169 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00003170 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003171 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00003172 }
John McCall57c13002011-07-06 05:58:41 +00003173
Sean Hunt2edf0a22012-06-23 05:07:58 +00003174 // If attributes exist after tag, parse them.
3175 ParsedAttributesWithRange attrs(AttrFactory);
3176 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003177 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003178
3179 // If declspecs exist after tag, parse them.
3180 while (Tok.is(tok::kw___declspec))
3181 ParseMicrosoftDeclSpec(attrs);
3182
Richard Smithbdad7a22012-01-10 01:33:14 +00003183 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00003184 bool IsScopedUsingClassTag = false;
3185
John McCall1e12b3d2012-06-23 22:30:04 +00003186 // In C++11, recognize 'enum class' and 'enum struct'.
Richard Smith80ad52f2013-01-02 11:42:31 +00003187 if (getLangOpts().CPlusPlus11 &&
John McCall57c13002011-07-06 05:58:41 +00003188 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00003189 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00003190 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00003191 ScopedEnumKWLoc = ConsumeToken();
Chad Rosier8decdee2012-06-26 22:30:43 +00003192
Bill Wendlingad017fa2012-12-20 19:22:21 +00003193 // Attributes are not allowed between these keywords. Diagnose,
John McCall1e12b3d2012-06-23 22:30:04 +00003194 // but then just treat them like they appeared in the right place.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003195 ProhibitAttributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003196
3197 // They are allowed afterwards, though.
3198 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003199 MaybeParseCXX11Attributes(attrs);
John McCall1e12b3d2012-06-23 22:30:04 +00003200 while (Tok.is(tok::kw___declspec))
3201 ParseMicrosoftDeclSpec(attrs);
John McCall57c13002011-07-06 05:58:41 +00003202 }
Richard Smith1af83c42012-03-23 03:33:32 +00003203
John McCall13489672012-05-07 06:16:58 +00003204 // C++11 [temp.explicit]p12:
3205 // The usual access controls do not apply to names used to specify
3206 // explicit instantiations.
3207 // We extend this to also cover explicit specializations. Note that
3208 // we don't suppress if this turns out to be an elaborated type
3209 // specifier.
3210 bool shouldDelayDiagsInTag =
3211 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
3212 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
3213 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003214
Richard Smith7796eb52012-03-12 08:56:40 +00003215 // Enum definitions should not be parsed in a trailing-return-type.
3216 bool AllowDeclaration = DSC != DSC_trailing;
3217
3218 bool AllowFixedUnderlyingType = AllowDeclaration &&
Richard Smith80ad52f2013-01-02 11:42:31 +00003219 (getLangOpts().CPlusPlus11 || getLangOpts().MicrosoftExt ||
Richard Smith7796eb52012-03-12 08:56:40 +00003220 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003221
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003222 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003223 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003224 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3225 // if a fixed underlying type is allowed.
3226 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
Chad Rosier8decdee2012-06-26 22:30:43 +00003227
3228 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003229 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00003230 return;
3231
3232 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003233 Diag(Tok, diag::err_expected_ident);
3234 if (Tok.isNot(tok::l_brace)) {
3235 // Has no name and is not a definition.
3236 // Skip the rest of this declarator, up until the comma or semicolon.
3237 SkipUntil(tok::comma, true);
3238 return;
3239 }
3240 }
3241 }
Mike Stump1eb44332009-09-09 15:08:12 +00003242
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003243 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003244 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003245 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003246 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003247
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003248 // Skip the rest of this declarator, up until the comma or semicolon.
3249 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003250 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003251 }
Mike Stump1eb44332009-09-09 15:08:12 +00003252
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003253 // If an identifier is present, consume and remember it.
3254 IdentifierInfo *Name = 0;
3255 SourceLocation NameLoc;
3256 if (Tok.is(tok::identifier)) {
3257 Name = Tok.getIdentifierInfo();
3258 NameLoc = ConsumeToken();
3259 }
Mike Stump1eb44332009-09-09 15:08:12 +00003260
Richard Smithbdad7a22012-01-10 01:33:14 +00003261 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003262 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3263 // declaration of a scoped enumeration.
3264 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003265 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003266 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003267 }
3268
John McCall13489672012-05-07 06:16:58 +00003269 // Okay, end the suppression area. We'll decide whether to emit the
3270 // diagnostics in a second.
3271 if (shouldDelayDiagsInTag)
3272 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003273
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003274 TypeResult BaseType;
3275
Douglas Gregora61b3e72010-12-01 17:42:47 +00003276 // Parse the fixed underlying type.
Richard Smith139be702012-07-02 19:14:01 +00003277 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
Douglas Gregorb9075602011-02-22 02:55:24 +00003278 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003279 bool PossibleBitfield = false;
Richard Smith139be702012-07-02 19:14:01 +00003280 if (CanBeBitfield) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003281 // If we're in class scope, this can either be an enum declaration with
3282 // an underlying type, or a declaration of a bitfield member. We try to
3283 // use a simple disambiguation scheme first to catch the common cases
Chad Rosier8decdee2012-06-26 22:30:43 +00003284 // (integer literal, sizeof); if it's still ambiguous, we then consider
3285 // anything that's a simple-type-specifier followed by '(' as an
3286 // expression. This suffices because function types are not valid
Douglas Gregora61b3e72010-12-01 17:42:47 +00003287 // underlying types anyway.
Richard Smith05766812012-08-18 00:55:03 +00003288 EnterExpressionEvaluationContext Unevaluated(Actions,
3289 Sema::ConstantEvaluated);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003290 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
Chad Rosier8decdee2012-06-26 22:30:43 +00003291 // If the next token starts an expression, we know we're parsing a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003292 // bit-field. This is the common case.
3293 if (TPR == TPResult::True())
3294 PossibleBitfield = true;
3295 // If the next token starts a type-specifier-seq, it may be either a
3296 // a fixed underlying type or the start of a function-style cast in C++;
Chad Rosier8decdee2012-06-26 22:30:43 +00003297 // lookahead one more token to see if it's obvious that we have a
Douglas Gregora61b3e72010-12-01 17:42:47 +00003298 // fixed underlying type.
Chad Rosier8decdee2012-06-26 22:30:43 +00003299 else if (TPR == TPResult::False() &&
Douglas Gregora61b3e72010-12-01 17:42:47 +00003300 GetLookAheadToken(2).getKind() == tok::semi) {
3301 // Consume the ':'.
3302 ConsumeToken();
3303 } else {
3304 // We have the start of a type-specifier-seq, so we have to perform
3305 // tentative parsing to determine whether we have an expression or a
3306 // type.
3307 TentativeParsingAction TPA(*this);
3308
3309 // Consume the ':'.
3310 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003311
3312 // If we see a type specifier followed by an open-brace, we have an
3313 // ambiguity between an underlying type and a C++11 braced
3314 // function-style cast. Resolve this by always treating it as an
3315 // underlying type.
3316 // FIXME: The standard is not entirely clear on how to disambiguate in
3317 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003318 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003319 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003320 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003321 // We'll parse this as a bitfield later.
3322 PossibleBitfield = true;
3323 TPA.Revert();
3324 } else {
3325 // We have a type-specifier-seq.
3326 TPA.Commit();
3327 }
3328 }
3329 } else {
3330 // Consume the ':'.
3331 ConsumeToken();
3332 }
3333
3334 if (!PossibleBitfield) {
3335 SourceRange Range;
3336 BaseType = ParseTypeName(&Range);
Chad Rosier8decdee2012-06-26 22:30:43 +00003337
Richard Smith80ad52f2013-01-02 11:42:31 +00003338 if (getLangOpts().CPlusPlus11) {
Richard Smith7fe62082011-10-15 05:09:34 +00003339 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Eli Friedmancef3a7b2012-11-02 01:34:28 +00003340 } else if (!getLangOpts().ObjC2) {
3341 if (getLangOpts().CPlusPlus)
3342 Diag(StartLoc, diag::ext_cxx11_enum_fixed_underlying_type) << Range;
3343 else
3344 Diag(StartLoc, diag::ext_c_enum_fixed_underlying_type) << Range;
3345 }
Douglas Gregora61b3e72010-12-01 17:42:47 +00003346 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003347 }
3348
Richard Smithbdad7a22012-01-10 01:33:14 +00003349 // There are four options here. If we have 'friend enum foo;' then this is a
3350 // friend declaration, and cannot have an accompanying definition. If we have
3351 // 'enum foo;', then this is a forward declaration. If we have
3352 // 'enum foo {...' then this is a definition. Otherwise we have something
3353 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003354 //
3355 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3356 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3357 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3358 //
John McCallf312b1e2010-08-26 23:41:50 +00003359 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003360 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003361 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003362 } else if (Tok.is(tok::l_brace)) {
3363 if (DS.isFriendSpecified()) {
3364 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3365 << SourceRange(DS.getFriendSpecLoc());
3366 ConsumeBrace();
3367 SkipUntil(tok::r_brace);
3368 TUK = Sema::TUK_Friend;
3369 } else {
3370 TUK = Sema::TUK_Definition;
3371 }
Richard Smithc9f35172012-06-25 21:37:02 +00003372 } else if (DSC != DSC_type_specifier &&
3373 (Tok.is(tok::semi) ||
Richard Smith139be702012-07-02 19:14:01 +00003374 (Tok.isAtStartOfLine() &&
3375 !isValidAfterTypeSpecifier(CanBeBitfield)))) {
Richard Smithc9f35172012-06-25 21:37:02 +00003376 TUK = DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration;
3377 if (Tok.isNot(tok::semi)) {
3378 // A semicolon was missing after this declaration. Diagnose and recover.
3379 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl,
3380 "enum");
3381 PP.EnterToken(Tok);
3382 Tok.setKind(tok::semi);
3383 }
John McCall13489672012-05-07 06:16:58 +00003384 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003385 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003386 }
3387
3388 // If this is an elaborated type specifier, and we delayed
3389 // diagnostics before, just merge them into the current pool.
3390 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3391 diagsFromTag.redelay();
3392 }
Richard Smith1af83c42012-03-23 03:33:32 +00003393
3394 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003395 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003396 TUK != Sema::TUK_Reference) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003397 if (!getLangOpts().CPlusPlus11 || !SS.isSet()) {
Richard Smith1af83c42012-03-23 03:33:32 +00003398 // Skip the rest of this declarator, up until the comma or semicolon.
3399 Diag(Tok, diag::err_enum_template);
3400 SkipUntil(tok::comma, true);
3401 return;
3402 }
3403
3404 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3405 // Enumerations can't be explicitly instantiated.
3406 DS.SetTypeSpecError();
3407 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3408 return;
3409 }
3410
3411 assert(TemplateInfo.TemplateParams && "no template parameters");
3412 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3413 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003414 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003415
Sean Hunt2edf0a22012-06-23 05:07:58 +00003416 if (TUK == Sema::TUK_Reference)
3417 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003418
Douglas Gregorb9075602011-02-22 02:55:24 +00003419 if (!Name && TUK != Sema::TUK_Definition) {
3420 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003421
Douglas Gregorb9075602011-02-22 02:55:24 +00003422 // Skip the rest of this declarator, up until the comma or semicolon.
3423 SkipUntil(tok::comma, true);
3424 return;
3425 }
Richard Smith1af83c42012-03-23 03:33:32 +00003426
Douglas Gregor402abb52009-05-28 23:31:59 +00003427 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003428 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003429 const char *PrevSpec = 0;
3430 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003431 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003432 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003433 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003434 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003435 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003436
Douglas Gregor48c89f42010-04-24 16:38:41 +00003437 if (IsDependent) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003438 // This enum has a dependent nested-name-specifier. Handle it as a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003439 // dependent tag.
3440 if (!Name) {
3441 DS.SetTypeSpecError();
3442 Diag(Tok, diag::err_expected_type_name_after_typename);
3443 return;
3444 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003445
Douglas Gregor23c94db2010-07-02 17:43:08 +00003446 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Chad Rosier8decdee2012-06-26 22:30:43 +00003447 TUK, SS, Name, StartLoc,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003448 NameLoc);
3449 if (Type.isInvalid()) {
3450 DS.SetTypeSpecError();
3451 return;
3452 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003453
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003454 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3455 NameLoc.isValid() ? NameLoc : StartLoc,
3456 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003457 Diag(StartLoc, DiagID) << PrevSpec;
Chad Rosier8decdee2012-06-26 22:30:43 +00003458
Douglas Gregor48c89f42010-04-24 16:38:41 +00003459 return;
3460 }
Mike Stump1eb44332009-09-09 15:08:12 +00003461
John McCalld226f652010-08-21 09:40:31 +00003462 if (!TagDecl) {
Chad Rosier8decdee2012-06-26 22:30:43 +00003463 // The action failed to produce an enumeration tag. If this is a
Douglas Gregor48c89f42010-04-24 16:38:41 +00003464 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003465 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003466 ConsumeBrace();
3467 SkipUntil(tok::r_brace);
3468 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003469
Douglas Gregor48c89f42010-04-24 16:38:41 +00003470 DS.SetTypeSpecError();
3471 return;
3472 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003473
Richard Smithc9f35172012-06-25 21:37:02 +00003474 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference)
John McCall13489672012-05-07 06:16:58 +00003475 ParseEnumBody(StartLoc, TagDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00003476
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003477 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3478 NameLoc.isValid() ? NameLoc : StartLoc,
3479 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003480 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003481}
3482
3483/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3484/// enumerator-list:
3485/// enumerator
3486/// enumerator-list ',' enumerator
3487/// enumerator:
3488/// enumeration-constant
3489/// enumeration-constant '=' constant-expression
3490/// enumeration-constant:
3491/// identifier
3492///
John McCalld226f652010-08-21 09:40:31 +00003493void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003494 // Enter the scope of the enum body and start the definition.
3495 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003496 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003497
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003498 BalancedDelimiterTracker T(*this, tok::l_brace);
3499 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003500
Chris Lattner7946dd32007-08-27 17:24:30 +00003501 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003502 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003503 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003504
Chris Lattner5f9e2722011-07-23 10:55:15 +00003505 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003506
John McCalld226f652010-08-21 09:40:31 +00003507 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003508
Reid Spencer5f016e22007-07-11 17:01:13 +00003509 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003510 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003511 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3512 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003513
John McCall5b629aa2010-10-22 23:36:17 +00003514 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003515 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003516 MaybeParseGNUAttributes(attrs);
Richard Smith4e24f0f2013-01-02 12:01:23 +00003517 MaybeParseCXX11Attributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003518 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003519
Reid Spencer5f016e22007-07-11 17:01:13 +00003520 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003521 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003522 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Chad Rosier8decdee2012-06-26 22:30:43 +00003523
Chris Lattner04d66662007-10-09 17:33:22 +00003524 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003525 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003526 AssignedVal = ParseConstantExpression();
3527 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003528 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003529 }
Mike Stump1eb44332009-09-09 15:08:12 +00003530
Reid Spencer5f016e22007-07-11 17:01:13 +00003531 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003532 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3533 LastEnumConstDecl,
3534 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003535 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003536 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003537 PD.complete(EnumConstDecl);
Chad Rosier8decdee2012-06-26 22:30:43 +00003538
Reid Spencer5f016e22007-07-11 17:01:13 +00003539 EnumConstantDecls.push_back(EnumConstDecl);
3540 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003541
Douglas Gregor751f6922010-09-07 14:51:08 +00003542 if (Tok.is(tok::identifier)) {
3543 // We're missing a comma between enumerators.
3544 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
Chad Rosier8decdee2012-06-26 22:30:43 +00003545 Diag(Loc, diag::err_enumerator_list_missing_comma)
Douglas Gregor751f6922010-09-07 14:51:08 +00003546 << FixItHint::CreateInsertion(Loc, ", ");
3547 continue;
3548 }
Chad Rosier8decdee2012-06-26 22:30:43 +00003549
Chris Lattner04d66662007-10-09 17:33:22 +00003550 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003551 break;
3552 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003553
Richard Smith7fe62082011-10-15 05:09:34 +00003554 if (Tok.isNot(tok::identifier)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00003555 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus11)
Richard Smitheab9d6f2012-07-23 05:45:25 +00003556 Diag(CommaLoc, getLangOpts().CPlusPlus ?
3557 diag::ext_enumerator_list_comma_cxx :
3558 diag::ext_enumerator_list_comma_c)
Richard Smith7fe62082011-10-15 05:09:34 +00003559 << FixItHint::CreateRemoval(CommaLoc);
Richard Smith80ad52f2013-01-02 11:42:31 +00003560 else if (getLangOpts().CPlusPlus11)
Richard Smith7fe62082011-10-15 05:09:34 +00003561 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3562 << FixItHint::CreateRemoval(CommaLoc);
3563 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003564 }
Mike Stump1eb44332009-09-09 15:08:12 +00003565
Reid Spencer5f016e22007-07-11 17:01:13 +00003566 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003567 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003568
Reid Spencer5f016e22007-07-11 17:01:13 +00003569 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003570 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003571 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003572
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003573 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3574 EnumDecl, EnumConstantDecls.data(),
3575 EnumConstantDecls.size(), getCurScope(),
3576 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003577
Douglas Gregor72de6672009-01-08 20:45:30 +00003578 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003579 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3580 T.getCloseLocation());
Richard Smithc9f35172012-06-25 21:37:02 +00003581
3582 // The next token must be valid after an enum definition. If not, a ';'
3583 // was probably forgotten.
Richard Smith139be702012-07-02 19:14:01 +00003584 bool CanBeBitfield = getCurScope()->getFlags() & Scope::ClassScope;
3585 if (!isValidAfterTypeSpecifier(CanBeBitfield)) {
Richard Smithc9f35172012-06-25 21:37:02 +00003586 ExpectAndConsume(tok::semi, diag::err_expected_semi_after_tagdecl, "enum");
3587 // Push this token back into the preprocessor and change our current token
3588 // to ';' so that the rest of the code recovers as though there were an
3589 // ';' after the definition.
3590 PP.EnterToken(Tok);
3591 Tok.setKind(tok::semi);
3592 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003593}
3594
3595/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003596/// start of a type-qualifier-list.
3597bool Parser::isTypeQualifier() const {
3598 switch (Tok.getKind()) {
3599 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003600
3601 // type-qualifier only in OpenCL
3602 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003603 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003604
Steve Naroff5f8aa692008-02-11 23:15:56 +00003605 // type-qualifier
3606 case tok::kw_const:
3607 case tok::kw_volatile:
3608 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003609 case tok::kw___private:
3610 case tok::kw___local:
3611 case tok::kw___global:
3612 case tok::kw___constant:
3613 case tok::kw___read_only:
3614 case tok::kw___read_write:
3615 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003616 return true;
3617 }
3618}
3619
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003620/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3621/// is definitely a type-specifier. Return false if it isn't part of a type
3622/// specifier or if we're not sure.
3623bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3624 switch (Tok.getKind()) {
3625 default: return false;
3626 // type-specifiers
3627 case tok::kw_short:
3628 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003629 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003630 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003631 case tok::kw_signed:
3632 case tok::kw_unsigned:
3633 case tok::kw__Complex:
3634 case tok::kw__Imaginary:
3635 case tok::kw_void:
3636 case tok::kw_char:
3637 case tok::kw_wchar_t:
3638 case tok::kw_char16_t:
3639 case tok::kw_char32_t:
3640 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003641 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003642 case tok::kw_float:
3643 case tok::kw_double:
3644 case tok::kw_bool:
3645 case tok::kw__Bool:
3646 case tok::kw__Decimal32:
3647 case tok::kw__Decimal64:
3648 case tok::kw__Decimal128:
3649 case tok::kw___vector:
Chad Rosier8decdee2012-06-26 22:30:43 +00003650
Guy Benyeib13621d2012-12-18 14:38:23 +00003651 // OpenCL specific types:
3652 case tok::kw_image1d_t:
3653 case tok::kw_image1d_array_t:
3654 case tok::kw_image1d_buffer_t:
3655 case tok::kw_image2d_t:
3656 case tok::kw_image2d_array_t:
3657 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003658 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003659 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003660
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003661 // struct-or-union-specifier (C99) or class-specifier (C++)
3662 case tok::kw_class:
3663 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003664 case tok::kw___interface:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003665 case tok::kw_union:
3666 // enum-specifier
3667 case tok::kw_enum:
Chad Rosier8decdee2012-06-26 22:30:43 +00003668
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003669 // typedef-name
3670 case tok::annot_typename:
3671 return true;
3672 }
3673}
3674
Steve Naroff5f8aa692008-02-11 23:15:56 +00003675/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003676/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003677bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003678 switch (Tok.getKind()) {
3679 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003680
Chris Lattner166a8fc2009-01-04 23:41:41 +00003681 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003682 if (TryAltiVecVectorToken())
3683 return true;
3684 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003685 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003686 // Annotate typenames and C++ scope specifiers. If we get one, just
3687 // recurse to handle whatever we get.
3688 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003689 return true;
3690 if (Tok.is(tok::identifier))
3691 return false;
3692 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003693
Chris Lattner166a8fc2009-01-04 23:41:41 +00003694 case tok::coloncolon: // ::foo::bar
3695 if (NextToken().is(tok::kw_new) || // ::new
3696 NextToken().is(tok::kw_delete)) // ::delete
3697 return false;
3698
Chris Lattner166a8fc2009-01-04 23:41:41 +00003699 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003700 return true;
3701 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003702
Reid Spencer5f016e22007-07-11 17:01:13 +00003703 // GNU attributes support.
3704 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003705 // GNU typeof support.
3706 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003707
Reid Spencer5f016e22007-07-11 17:01:13 +00003708 // type-specifiers
3709 case tok::kw_short:
3710 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003711 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003712 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003713 case tok::kw_signed:
3714 case tok::kw_unsigned:
3715 case tok::kw__Complex:
3716 case tok::kw__Imaginary:
3717 case tok::kw_void:
3718 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003719 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003720 case tok::kw_char16_t:
3721 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003722 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003723 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003724 case tok::kw_float:
3725 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003726 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003727 case tok::kw__Bool:
3728 case tok::kw__Decimal32:
3729 case tok::kw__Decimal64:
3730 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003731 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003732
Guy Benyeib13621d2012-12-18 14:38:23 +00003733 // OpenCL specific types:
3734 case tok::kw_image1d_t:
3735 case tok::kw_image1d_array_t:
3736 case tok::kw_image1d_buffer_t:
3737 case tok::kw_image2d_t:
3738 case tok::kw_image2d_array_t:
3739 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003740 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003741 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003742
Chris Lattner99dc9142008-04-13 18:59:07 +00003743 // struct-or-union-specifier (C99) or class-specifier (C++)
3744 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003745 case tok::kw_struct:
Joao Matos6666ed42012-08-31 18:45:21 +00003746 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003747 case tok::kw_union:
3748 // enum-specifier
3749 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003750
Reid Spencer5f016e22007-07-11 17:01:13 +00003751 // type-qualifier
3752 case tok::kw_const:
3753 case tok::kw_volatile:
3754 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003755
John McCallb8a8de32012-11-14 00:49:39 +00003756 // Debugger support.
3757 case tok::kw___unknown_anytype:
3758
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003759 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003760 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003761 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003762
Chris Lattner7c186be2008-10-20 00:25:30 +00003763 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3764 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003765 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003766
Steve Naroff239f0732008-12-25 14:16:32 +00003767 case tok::kw___cdecl:
3768 case tok::kw___stdcall:
3769 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003770 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003771 case tok::kw___w64:
3772 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003773 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003774 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003775 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003776
3777 case tok::kw___private:
3778 case tok::kw___local:
3779 case tok::kw___global:
3780 case tok::kw___constant:
3781 case tok::kw___read_only:
3782 case tok::kw___read_write:
3783 case tok::kw___write_only:
3784
Eli Friedman290eeb02009-06-08 23:27:34 +00003785 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003786
3787 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003788 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003789
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003790 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003791 case tok::kw__Atomic:
3792 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003793 }
3794}
3795
3796/// isDeclarationSpecifier() - Return true if the current token is part of a
3797/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003798///
3799/// \param DisambiguatingWithExpression True to indicate that the purpose of
3800/// this check is to disambiguate between an expression and a declaration.
3801bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003802 switch (Tok.getKind()) {
3803 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003804
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003805 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003806 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003807
Chris Lattner166a8fc2009-01-04 23:41:41 +00003808 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003809 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003810 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003811 return false;
John Thompson82287d12010-02-05 00:12:22 +00003812 if (TryAltiVecVectorToken())
3813 return true;
3814 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003815 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003816 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003817 // Annotate typenames and C++ scope specifiers. If we get one, just
3818 // recurse to handle whatever we get.
3819 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003820 return true;
3821 if (Tok.is(tok::identifier))
3822 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003823
Douglas Gregor9497a732010-09-16 01:51:54 +00003824 // If we're in Objective-C and we have an Objective-C class type followed
Chad Rosier8decdee2012-06-26 22:30:43 +00003825 // by an identifier and then either ':' or ']', in a place where an
Douglas Gregor9497a732010-09-16 01:51:54 +00003826 // expression is permitted, then this is probably a class message send
3827 // missing the initial '['. In this case, we won't consider this to be
3828 // the start of a declaration.
Chad Rosier8decdee2012-06-26 22:30:43 +00003829 if (DisambiguatingWithExpression &&
Douglas Gregor9497a732010-09-16 01:51:54 +00003830 isStartOfObjCClassMessageMissingOpenBracket())
3831 return false;
Chad Rosier8decdee2012-06-26 22:30:43 +00003832
John McCall9ba61662010-02-26 08:45:28 +00003833 return isDeclarationSpecifier();
3834
Chris Lattner166a8fc2009-01-04 23:41:41 +00003835 case tok::coloncolon: // ::foo::bar
3836 if (NextToken().is(tok::kw_new) || // ::new
3837 NextToken().is(tok::kw_delete)) // ::delete
3838 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003839
Chris Lattner166a8fc2009-01-04 23:41:41 +00003840 // Annotate typenames and C++ scope specifiers. If we get one, just
3841 // recurse to handle whatever we get.
3842 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003843 return true;
3844 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003845
Reid Spencer5f016e22007-07-11 17:01:13 +00003846 // storage-class-specifier
3847 case tok::kw_typedef:
3848 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003849 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003850 case tok::kw_static:
3851 case tok::kw_auto:
3852 case tok::kw_register:
3853 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003854
Douglas Gregor8d267c52011-09-09 02:06:17 +00003855 // Modules
3856 case tok::kw___module_private__:
Chad Rosier8decdee2012-06-26 22:30:43 +00003857
John McCallb8a8de32012-11-14 00:49:39 +00003858 // Debugger support
3859 case tok::kw___unknown_anytype:
3860
Reid Spencer5f016e22007-07-11 17:01:13 +00003861 // type-specifiers
3862 case tok::kw_short:
3863 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003864 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003865 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003866 case tok::kw_signed:
3867 case tok::kw_unsigned:
3868 case tok::kw__Complex:
3869 case tok::kw__Imaginary:
3870 case tok::kw_void:
3871 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003872 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003873 case tok::kw_char16_t:
3874 case tok::kw_char32_t:
3875
Reid Spencer5f016e22007-07-11 17:01:13 +00003876 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003877 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003878 case tok::kw_float:
3879 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003880 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003881 case tok::kw__Bool:
3882 case tok::kw__Decimal32:
3883 case tok::kw__Decimal64:
3884 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003885 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003886
Guy Benyeib13621d2012-12-18 14:38:23 +00003887 // OpenCL specific types:
3888 case tok::kw_image1d_t:
3889 case tok::kw_image1d_array_t:
3890 case tok::kw_image1d_buffer_t:
3891 case tok::kw_image2d_t:
3892 case tok::kw_image2d_array_t:
3893 case tok::kw_image3d_t:
Guy Benyei21f18c42013-02-07 10:55:47 +00003894 case tok::kw_sampler_t:
Guy Benyeie6b9d802013-01-20 12:31:11 +00003895 case tok::kw_event_t:
Guy Benyeib13621d2012-12-18 14:38:23 +00003896
Chris Lattner99dc9142008-04-13 18:59:07 +00003897 // struct-or-union-specifier (C99) or class-specifier (C++)
3898 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003899 case tok::kw_struct:
3900 case tok::kw_union:
Joao Matos6666ed42012-08-31 18:45:21 +00003901 case tok::kw___interface:
Reid Spencer5f016e22007-07-11 17:01:13 +00003902 // enum-specifier
3903 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003904
Reid Spencer5f016e22007-07-11 17:01:13 +00003905 // type-qualifier
3906 case tok::kw_const:
3907 case tok::kw_volatile:
3908 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003909
Reid Spencer5f016e22007-07-11 17:01:13 +00003910 // function-specifier
3911 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003912 case tok::kw_virtual:
3913 case tok::kw_explicit:
Richard Smithde03c152013-01-17 22:16:11 +00003914 case tok::kw__Noreturn:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003915
Richard Smith4cd81c52013-01-29 09:02:09 +00003916 // alignment-specifier
3917 case tok::kw__Alignas:
3918
Richard Smith53aec2a2012-10-25 00:00:53 +00003919 // friend keyword.
3920 case tok::kw_friend:
3921
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003922 // static_assert-declaration
3923 case tok::kw__Static_assert:
3924
Chris Lattner1ef08762007-08-09 17:01:07 +00003925 // GNU typeof support.
3926 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003927
Chris Lattner1ef08762007-08-09 17:01:07 +00003928 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003929 case tok::kw___attribute:
Mike Stump1eb44332009-09-09 15:08:12 +00003930
Richard Smith53aec2a2012-10-25 00:00:53 +00003931 // C++11 decltype and constexpr.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003932 case tok::annot_decltype:
Richard Smith53aec2a2012-10-25 00:00:53 +00003933 case tok::kw_constexpr:
Francois Pichete3d49b42011-06-19 08:02:06 +00003934
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003935 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003936 case tok::kw__Atomic:
3937 return true;
3938
Chris Lattnerf3948c42008-07-26 03:38:44 +00003939 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3940 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003941 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003942
Douglas Gregord9d75e52011-04-27 05:41:15 +00003943 // typedef-name
3944 case tok::annot_typename:
3945 return !DisambiguatingWithExpression ||
3946 !isStartOfObjCClassMessageMissingOpenBracket();
Chad Rosier8decdee2012-06-26 22:30:43 +00003947
Steve Naroff47f52092009-01-06 19:34:12 +00003948 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003949 case tok::kw___cdecl:
3950 case tok::kw___stdcall:
3951 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003952 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003953 case tok::kw___w64:
3954 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003955 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003956 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003957 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003958 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003959
3960 case tok::kw___private:
3961 case tok::kw___local:
3962 case tok::kw___global:
3963 case tok::kw___constant:
3964 case tok::kw___read_only:
3965 case tok::kw___read_write:
3966 case tok::kw___write_only:
3967
Eli Friedman290eeb02009-06-08 23:27:34 +00003968 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003969 }
3970}
3971
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003972bool Parser::isConstructorDeclarator() {
3973 TentativeParsingAction TPA(*this);
3974
3975 // Parse the C++ scope specifier.
3976 CXXScopeSpec SS;
Chad Rosier8decdee2012-06-26 22:30:43 +00003977 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003978 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003979 TPA.Revert();
3980 return false;
3981 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003982
3983 // Parse the constructor name.
3984 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3985 // We already know that we have a constructor name; just consume
3986 // the token.
3987 ConsumeToken();
3988 } else {
3989 TPA.Revert();
3990 return false;
3991 }
3992
Richard Smith22592862012-03-27 23:05:05 +00003993 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003994 if (Tok.isNot(tok::l_paren)) {
3995 TPA.Revert();
3996 return false;
3997 }
3998 ConsumeParen();
3999
Richard Smith22592862012-03-27 23:05:05 +00004000 // A right parenthesis, or ellipsis followed by a right parenthesis signals
4001 // that we have a constructor.
4002 if (Tok.is(tok::r_paren) ||
4003 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004004 TPA.Revert();
4005 return true;
4006 }
4007
4008 // If we need to, enter the specified scope.
4009 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00004010 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004011 DeclScopeObj.EnterDeclaratorScope();
4012
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004013 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00004014 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00004015 MaybeParseMicrosoftAttributes(Attrs);
4016
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004017 // Check whether the next token(s) are part of a declaration
4018 // specifier, in which case we have the start of a parameter and,
4019 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00004020 bool IsConstructor = false;
4021 if (isDeclarationSpecifier())
4022 IsConstructor = true;
4023 else if (Tok.is(tok::identifier) ||
4024 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
4025 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
4026 // This might be a parenthesized member name, but is more likely to
4027 // be a constructor declaration with an invalid argument type. Keep
4028 // looking.
4029 if (Tok.is(tok::annot_cxxscope))
4030 ConsumeToken();
4031 ConsumeToken();
4032
4033 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00004034 // which must have one of the following syntactic forms (see the
4035 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00004036 switch (Tok.getKind()) {
4037 case tok::l_paren:
4038 // C(X ( int));
4039 case tok::l_square:
4040 // C(X [ 5]);
4041 // C(X [ [attribute]]);
4042 case tok::coloncolon:
4043 // C(X :: Y);
4044 // C(X :: *p);
4045 case tok::r_paren:
4046 // C(X )
4047 // Assume this isn't a constructor, rather than assuming it's a
4048 // constructor with an unnamed parameter of an ill-formed type.
4049 break;
4050
4051 default:
4052 IsConstructor = true;
4053 break;
4054 }
4055 }
4056
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004057 TPA.Revert();
4058 return IsConstructor;
4059}
Reid Spencer5f016e22007-07-11 17:01:13 +00004060
4061/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00004062/// type-qualifier-list: [C99 6.7.5]
4063/// type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004064/// [vendor] attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004065/// [ only if VendorAttributesAllowed=true ]
4066/// type-qualifier-list type-qualifier
Chad Rosier8decdee2012-06-26 22:30:43 +00004067/// [vendor] type-qualifier-list attributes
Dawn Perchik52fc3142010-09-03 01:29:35 +00004068/// [ only if VendorAttributesAllowed=true ]
4069/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
Richard Smith4e24f0f2013-01-02 12:01:23 +00004070/// [ only if CXX11AttributesAllowed=true ]
Dawn Perchik52fc3142010-09-03 01:29:35 +00004071/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00004072///
Dawn Perchik52fc3142010-09-03 01:29:35 +00004073void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
4074 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00004075 bool CXX11AttributesAllowed) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004076 if (getLangOpts().CPlusPlus11 && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00004077 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00004078 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00004079 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004080 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004081 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004082
4083 SourceLocation EndLoc;
4084
Reid Spencer5f016e22007-07-11 17:01:13 +00004085 while (1) {
John McCallfec54012009-08-03 20:12:06 +00004086 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00004087 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004088 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00004089 SourceLocation Loc = Tok.getLocation();
4090
4091 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00004092 case tok::code_completion:
4093 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00004094 return cutOffParsing();
Chad Rosier8decdee2012-06-26 22:30:43 +00004095
Reid Spencer5f016e22007-07-11 17:01:13 +00004096 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00004097 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004098 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004099 break;
4100 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00004101 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004102 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004103 break;
4104 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00004105 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
Richard Smithd654f2d2012-10-17 23:31:46 +00004106 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00004107 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004108
4109 // OpenCL qualifiers:
Chad Rosier8decdee2012-06-26 22:30:43 +00004110 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00004111 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00004112 goto DoneWithTypeQuals;
4113 case tok::kw___private:
4114 case tok::kw___global:
4115 case tok::kw___local:
4116 case tok::kw___constant:
4117 case tok::kw___read_only:
4118 case tok::kw___write_only:
4119 case tok::kw___read_write:
4120 ParseOpenCLQualifiers(DS);
4121 break;
4122
Eli Friedman290eeb02009-06-08 23:27:34 +00004123 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00004124 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00004125 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00004126 case tok::kw___cdecl:
4127 case tok::kw___stdcall:
4128 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004129 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004130 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004131 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004132 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00004133 continue;
4134 }
4135 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00004136 case tok::kw___pascal:
4137 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004138 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00004139 continue;
4140 }
4141 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00004142 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00004143 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00004144 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004145 continue; // do *not* consume the next token!
4146 }
4147 // otherwise, FALL THROUGH!
4148 default:
Steve Naroff239f0732008-12-25 14:16:32 +00004149 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004150 // If this is not a type-qualifier token, we're done reading type
4151 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00004152 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00004153 if (EndLoc.isValid())
4154 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004155 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00004156 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004157
Reid Spencer5f016e22007-07-11 17:01:13 +00004158 // If the specifier combination wasn't legal, issue a diagnostic.
4159 if (isInvalid) {
4160 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00004161 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00004162 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00004163 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004164 }
4165}
4166
4167
4168/// ParseDeclarator - Parse and verify a newly-initialized declarator.
4169///
4170void Parser::ParseDeclarator(Declarator &D) {
4171 /// This implements the 'declarator' production in the C grammar, then checks
4172 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004173 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00004174}
4175
Richard Smith9988f282012-03-29 01:16:42 +00004176static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
4177 if (Kind == tok::star || Kind == tok::caret)
4178 return true;
4179
4180 // We parse rvalue refs in C++03, because otherwise the errors are scary.
4181 if (!Lang.CPlusPlus)
4182 return false;
4183
4184 return Kind == tok::amp || Kind == tok::ampamp;
4185}
4186
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004187/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
4188/// is parsed by the function passed to it. Pass null, and the direct-declarator
4189/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004190/// ptr-operator production.
4191///
Richard Smith0706df42011-10-19 21:33:05 +00004192/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00004193/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
4194/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00004195///
Sebastian Redlf30208a2009-01-24 21:16:55 +00004196/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
4197/// [C] pointer[opt] direct-declarator
4198/// [C++] direct-declarator
4199/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00004200///
4201/// pointer: [C99 6.7.5]
4202/// '*' type-qualifier-list[opt]
4203/// '*' type-qualifier-list[opt] pointer
4204///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004205/// ptr-operator:
4206/// '*' cv-qualifier-seq[opt]
4207/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00004208/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004209/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00004210/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00004211/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004212void Parser::ParseDeclaratorInternal(Declarator &D,
4213 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00004214 if (Diags.hasAllExtensionsSilenced())
4215 D.setExtension();
Chad Rosier8decdee2012-06-26 22:30:43 +00004216
Sebastian Redlf30208a2009-01-24 21:16:55 +00004217 // C++ member pointers start with a '::' or a nested-name.
4218 // Member pointers get special handling, since there's no place for the
4219 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00004220 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00004221 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
4222 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004223 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4224 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00004225 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004226 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004227
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00004228 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00004229 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00004230 // The scope spec really belongs to the direct-declarator.
Richard Smith6a502c42013-01-08 22:43:49 +00004231 if (D.mayHaveIdentifier())
4232 D.getCXXScopeSpec() = SS;
4233 else
4234 AnnotateScopeToken(SS, true);
4235
Sebastian Redlf30208a2009-01-24 21:16:55 +00004236 if (DirectDeclParser)
4237 (this->*DirectDeclParser)(D);
4238 return;
4239 }
4240
4241 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004242 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00004243 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004244 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004245 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004246
4247 // Recurse to parse whatever is left.
4248 ParseDeclaratorInternal(D, DirectDeclParser);
4249
4250 // Sema will have to catch (syntactically invalid) pointers into global
4251 // scope. It has to catch pointers into namespace scope anyway.
4252 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004253 Loc),
4254 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004255 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00004256 return;
4257 }
4258 }
4259
4260 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00004261 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00004262 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004263 if (DirectDeclParser)
4264 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004265 return;
4266 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00004267
Sebastian Redl05532f22009-03-15 22:02:01 +00004268 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
4269 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00004270 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00004271 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00004272
Chris Lattner9af55002009-03-27 04:18:06 +00004273 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00004274 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00004275 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004276
Richard Smith6ee326a2012-04-10 01:32:12 +00004277 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00004278 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00004279 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00004280
Reid Spencer5f016e22007-07-11 17:01:13 +00004281 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004282 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00004283 if (Kind == tok::star)
4284 // Remember that we parsed a pointer type, and remember the type-quals.
4285 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00004286 DS.getConstSpecLoc(),
4287 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00004288 DS.getRestrictSpecLoc()),
4289 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004290 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004291 else
4292 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004293 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004294 Loc),
4295 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004296 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004297 } else {
4298 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004299 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004300
Sebastian Redl743de1f2009-03-23 00:00:23 +00004301 // Complain about rvalue references in C++03, but then go on and build
4302 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004303 if (Kind == tok::ampamp)
Richard Smith80ad52f2013-01-02 11:42:31 +00004304 Diag(Loc, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004305 diag::warn_cxx98_compat_rvalue_reference :
4306 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004307
Richard Smith6ee326a2012-04-10 01:32:12 +00004308 // GNU-style and C++11 attributes are allowed here, as is restrict.
4309 ParseTypeQualifierListOpt(DS);
4310 D.ExtendWithDeclSpec(DS);
4311
Reid Spencer5f016e22007-07-11 17:01:13 +00004312 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4313 // cv-qualifiers are introduced through the use of a typedef or of a
4314 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004315 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4316 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4317 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004318 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004319 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4320 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004321 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00004322 }
4323
4324 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004325 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004326
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004327 if (D.getNumTypeObjects() > 0) {
4328 // C++ [dcl.ref]p4: There shall be no references to references.
4329 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4330 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004331 if (const IdentifierInfo *II = D.getIdentifier())
4332 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4333 << II;
4334 else
4335 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4336 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004337
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004338 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004339 // can go ahead and build the (technically ill-formed)
4340 // declarator: reference collapsing will take care of it.
4341 }
4342 }
4343
Reid Spencer5f016e22007-07-11 17:01:13 +00004344 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00004345 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004346 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004347 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004348 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004349 }
4350}
4351
Richard Smith9988f282012-03-29 01:16:42 +00004352static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4353 SourceLocation EllipsisLoc) {
4354 if (EllipsisLoc.isValid()) {
4355 FixItHint Insertion;
4356 if (!D.getEllipsisLoc().isValid()) {
4357 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4358 D.setEllipsisLoc(EllipsisLoc);
4359 }
4360 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4361 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4362 }
4363}
4364
Reid Spencer5f016e22007-07-11 17:01:13 +00004365/// ParseDirectDeclarator
4366/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004367/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004368/// '(' declarator ')'
4369/// [GNU] '(' attributes declarator ')'
4370/// [C90] direct-declarator '[' constant-expression[opt] ']'
4371/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4372/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4373/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4374/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004375/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4376/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004377/// direct-declarator '(' parameter-type-list ')'
4378/// direct-declarator '(' identifier-list[opt] ')'
4379/// [GNU] direct-declarator '(' parameter-forward-declarations
4380/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004381/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4382/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004383/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4384/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4385/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004386/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004387/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004388///
4389/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004390/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004391/// '::'[opt] nested-name-specifier[opt] type-name
4392///
4393/// id-expression: [C++ 5.1]
4394/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004395/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004396///
4397/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004398/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004399/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004400/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004401/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004402/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004403///
Richard Smith5d8388c2012-03-27 01:42:32 +00004404/// Note, any additional constructs added here may need corresponding changes
4405/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004406void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004407 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004408
David Blaikie4e4d0842012-03-11 07:00:24 +00004409 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004410 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004411 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004412 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4413 D.getContext() == Declarator::MemberContext;
Chad Rosier8decdee2012-06-26 22:30:43 +00004414 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004415 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004416 }
4417
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004418 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004419 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004420 // Change the declaration context for name lookup, until this function
4421 // is exited (and the declarator has been parsed).
4422 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004423 }
4424
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004425 // C++0x [dcl.fct]p14:
4426 // There is a syntactic ambiguity when an ellipsis occurs at the end
Chad Rosier8decdee2012-06-26 22:30:43 +00004427 // of a parameter-declaration-clause without a preceding comma. In
4428 // this case, the ellipsis is parsed as part of the
4429 // abstract-declarator if the type of the parameter names a template
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004430 // parameter pack that has not been expanded; otherwise, it is parsed
4431 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004432 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004433 !((D.getContext() == Declarator::PrototypeContext ||
4434 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004435 NextToken().is(tok::r_paren) &&
Richard Smith30f2a742013-02-20 20:19:27 +00004436 !D.hasGroupingParens() &&
Richard Smith9988f282012-03-29 01:16:42 +00004437 !Actions.containsUnexpandedParameterPacks(D))) {
4438 SourceLocation EllipsisLoc = ConsumeToken();
4439 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4440 // The ellipsis was put in the wrong place. Recover, and explain to
4441 // the user what they should have done.
4442 ParseDeclarator(D);
4443 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4444 return;
4445 } else
4446 D.setEllipsisLoc(EllipsisLoc);
4447
4448 // The ellipsis can't be followed by a parenthesized declarator. We
4449 // check for that in ParseParenDeclarator, after we have disambiguated
4450 // the l_paren token.
4451 }
4452
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004453 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4454 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4455 // We found something that indicates the start of an unqualified-id.
4456 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004457 bool AllowConstructorName;
4458 if (D.getDeclSpec().hasTypeSpecifier())
4459 AllowConstructorName = false;
4460 else if (D.getCXXScopeSpec().isSet())
4461 AllowConstructorName =
4462 (D.getContext() == Declarator::FileContext ||
Dmitri Gribenko1b9e8f72013-02-12 17:27:41 +00004463 D.getContext() == Declarator::MemberContext);
John McCallba9d8532010-04-13 06:39:49 +00004464 else
4465 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4466
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004467 SourceLocation TemplateKWLoc;
Chad Rosier8decdee2012-06-26 22:30:43 +00004468 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4469 /*EnteringContext=*/true,
4470 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004471 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004472 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004473 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004474 D.getName()) ||
4475 // Once we're past the identifier, if the scope was bad, mark the
4476 // whole declarator bad.
4477 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004478 D.SetIdentifier(0, Tok.getLocation());
4479 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004480 } else {
4481 // Parsed the unqualified-id; update range information and move along.
4482 if (D.getSourceRange().getBegin().isInvalid())
4483 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4484 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004485 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004486 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004487 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004488 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004489 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004490 "There's a C++-specific check for tok::identifier above");
4491 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4492 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4493 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004494 goto PastIdentifier;
4495 }
Richard Smith9988f282012-03-29 01:16:42 +00004496
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004497 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004498 // direct-declarator: '(' declarator ')'
4499 // direct-declarator: '(' attributes declarator ')'
4500 // Example: 'char (*X)' or 'int (*XX)(void)'
4501 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004502
4503 // If the declarator was parenthesized, we entered the declarator
4504 // scope when parsing the parenthesized declarator, then exited
4505 // the scope already. Re-enter the scope, if we need to.
4506 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004507 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004508 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004509 if (!D.isInvalidType() &&
4510 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004511 // Change the declaration context for name lookup, until this function
4512 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004513 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004514 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004515 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004516 // This could be something simple like "int" (in which case the declarator
4517 // portion is empty), if an abstract-declarator is allowed.
4518 D.SetIdentifier(0, Tok.getLocation());
Richard Smith30f2a742013-02-20 20:19:27 +00004519
4520 // The grammar for abstract-pack-declarator does not allow grouping parens.
4521 // FIXME: Revisit this once core issue 1488 is resolved.
4522 if (D.hasEllipsis() && D.hasGroupingParens())
4523 Diag(PP.getLocForEndOfToken(D.getEllipsisLoc()),
4524 diag::ext_abstract_pack_declarator_parens);
Reid Spencer5f016e22007-07-11 17:01:13 +00004525 } else {
David Blaikiee75d9cf2012-06-29 22:03:56 +00004526 if (Tok.getKind() == tok::annot_pragma_parser_crash)
David Blaikie377da4c2012-08-21 18:56:49 +00004527 LLVM_BUILTIN_TRAP;
Douglas Gregore950d4b2009-03-06 23:28:18 +00004528 if (D.getContext() == Declarator::MemberContext)
4529 Diag(Tok, diag::err_expected_member_name_or_semi)
4530 << D.getDeclSpec().getSourceRange();
Richard Trieudb55c04c2013-01-26 02:31:38 +00004531 else if (getLangOpts().CPlusPlus) {
4532 if (Tok.is(tok::period) || Tok.is(tok::arrow))
4533 Diag(Tok, diag::err_invalid_operator_on_type) << Tok.is(tok::arrow);
4534 else
4535 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
4536 } else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004537 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004538 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004539 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004540 }
Mike Stump1eb44332009-09-09 15:08:12 +00004541
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004542 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004543 assert(D.isPastIdentifier() &&
4544 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004545
Richard Smith6ee326a2012-04-10 01:32:12 +00004546 // Don't parse attributes unless we have parsed an unparenthesized name.
4547 if (D.hasName() && !D.getNumTypeObjects())
Richard Smith4e24f0f2013-01-02 12:01:23 +00004548 MaybeParseCXX11Attributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004549
Reid Spencer5f016e22007-07-11 17:01:13 +00004550 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004551 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004552 // Enter function-declaration scope, limiting any declarators to the
4553 // function prototype scope, including parameter declarators.
4554 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004555 Scope::FunctionPrototypeScope|Scope::DeclScope|
4556 (D.isFunctionDeclaratorAFunctionDeclaration()
4557 ? Scope::FunctionDeclarationScope : 0));
4558
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004559 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4560 // In such a case, check if we actually have a function declarator; if it
4561 // is not, the declarator has been fully parsed.
Richard Smithb9c62612012-07-30 21:30:52 +00004562 bool IsAmbiguous = false;
Richard Smith05766812012-08-18 00:55:03 +00004563 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
4564 // The name of the declarator, if any, is tentatively declared within
4565 // a possible direct initializer.
4566 TentativelyDeclaredIdentifiers.push_back(D.getIdentifier());
4567 bool IsFunctionDecl = isCXXFunctionDeclarator(&IsAmbiguous);
4568 TentativelyDeclaredIdentifiers.pop_back();
4569 if (!IsFunctionDecl)
4570 break;
4571 }
John McCall0b7e6782011-03-24 11:26:52 +00004572 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004573 BalancedDelimiterTracker T(*this, tok::l_paren);
4574 T.consumeOpen();
Richard Smithb9c62612012-07-30 21:30:52 +00004575 ParseFunctionDeclarator(D, attrs, T, IsAmbiguous);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004576 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004577 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004578 ParseBracketDeclarator(D);
4579 } else {
4580 break;
4581 }
4582 }
Chad Rosier8decdee2012-06-26 22:30:43 +00004583}
Reid Spencer5f016e22007-07-11 17:01:13 +00004584
Chris Lattneref4715c2008-04-06 05:45:57 +00004585/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4586/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004587/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004588/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4589///
4590/// direct-declarator:
4591/// '(' declarator ')'
4592/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004593/// direct-declarator '(' parameter-type-list ')'
4594/// direct-declarator '(' identifier-list[opt] ')'
4595/// [GNU] direct-declarator '(' parameter-forward-declarations
4596/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004597///
4598void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004599 BalancedDelimiterTracker T(*this, tok::l_paren);
4600 T.consumeOpen();
4601
Chris Lattneref4715c2008-04-06 05:45:57 +00004602 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004603
Chris Lattner7399ee02008-10-20 02:05:46 +00004604 // Eat any attributes before we look at whether this is a grouping or function
4605 // declarator paren. If this is a grouping paren, the attribute applies to
4606 // the type being built up, for example:
4607 // int (__attribute__(()) *x)(long y)
4608 // If this ends up not being a grouping paren, the attribute applies to the
4609 // first argument, for example:
4610 // int (__attribute__(()) int x)
4611 // In either case, we need to eat any attributes to be able to determine what
4612 // sort of paren this is.
4613 //
John McCall0b7e6782011-03-24 11:26:52 +00004614 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004615 bool RequiresArg = false;
4616 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004617 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004618
Chris Lattner7399ee02008-10-20 02:05:46 +00004619 // We require that the argument list (if this is a non-grouping paren) be
4620 // present even if the attribute list was empty.
4621 RequiresArg = true;
4622 }
Chad Rosier9cab1c92012-12-21 21:22:20 +00004623
Steve Naroff239f0732008-12-25 14:16:32 +00004624 // Eat any Microsoft extensions.
Chad Rosier9cab1c92012-12-21 21:22:20 +00004625 ParseMicrosoftTypeAttributes(attrs);
4626
Dawn Perchik52fc3142010-09-03 01:29:35 +00004627 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004628 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004629 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004630
Chris Lattneref4715c2008-04-06 05:45:57 +00004631 // If we haven't past the identifier yet (or where the identifier would be
4632 // stored, if this is an abstract declarator), then this is probably just
4633 // grouping parens. However, if this could be an abstract-declarator, then
4634 // this could also be the start of function arguments (consider 'void()').
4635 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004636
Chris Lattneref4715c2008-04-06 05:45:57 +00004637 if (!D.mayOmitIdentifier()) {
4638 // If this can't be an abstract-declarator, this *must* be a grouping
4639 // paren, because we haven't seen the identifier yet.
4640 isGrouping = true;
4641 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004642 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4643 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004644 isDeclarationSpecifier() || // 'int(int)' is a function.
4645 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004646 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4647 // considered to be a type, not a K&R identifier-list.
4648 isGrouping = false;
4649 } else {
4650 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4651 isGrouping = true;
4652 }
Mike Stump1eb44332009-09-09 15:08:12 +00004653
Chris Lattneref4715c2008-04-06 05:45:57 +00004654 // If this is a grouping paren, handle:
4655 // direct-declarator: '(' declarator ')'
4656 // direct-declarator: '(' attributes declarator ')'
4657 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004658 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4659 D.setEllipsisLoc(SourceLocation());
4660
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004661 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004662 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004663 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004664 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004665 T.consumeClose();
Chad Rosier8decdee2012-06-26 22:30:43 +00004666 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004667 T.getCloseLocation()),
4668 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004669
4670 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004671
4672 // An ellipsis cannot be placed outside parentheses.
4673 if (EllipsisLoc.isValid())
4674 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4675
Chris Lattneref4715c2008-04-06 05:45:57 +00004676 return;
4677 }
Mike Stump1eb44332009-09-09 15:08:12 +00004678
Chris Lattneref4715c2008-04-06 05:45:57 +00004679 // Okay, if this wasn't a grouping paren, it must be the start of a function
4680 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004681 // identifier (and remember where it would have been), then call into
4682 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004683 D.SetIdentifier(0, Tok.getLocation());
4684
David Blaikie42d6d0c2011-12-04 05:04:18 +00004685 // Enter function-declaration scope, limiting any declarators to the
4686 // function prototype scope, including parameter declarators.
4687 ParseScope PrototypeScope(this,
Richard Smith3a2b7a12013-01-28 22:42:45 +00004688 Scope::FunctionPrototypeScope | Scope::DeclScope |
4689 (D.isFunctionDeclaratorAFunctionDeclaration()
4690 ? Scope::FunctionDeclarationScope : 0));
Richard Smithb9c62612012-07-30 21:30:52 +00004691 ParseFunctionDeclarator(D, attrs, T, false, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004692 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004693}
4694
4695/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4696/// declarator D up to a paren, which indicates that we are parsing function
4697/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004698///
Richard Smith6ee326a2012-04-10 01:32:12 +00004699/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4700/// immediately after the open paren - they should be considered to be the
4701/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004702///
Richard Smith6ee326a2012-04-10 01:32:12 +00004703/// If RequiresArg is true, then the first argument of the function is required
4704/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004705///
Richard Smith6ee326a2012-04-10 01:32:12 +00004706/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4707/// (C++11) ref-qualifier[opt], exception-specification[opt],
4708/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4709///
4710/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004711/// dynamic-exception-specification
4712/// noexcept-specification
4713///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004714void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004715 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004716 BalancedDelimiterTracker &Tracker,
Richard Smithb9c62612012-07-30 21:30:52 +00004717 bool IsAmbiguous,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004718 bool RequiresArg) {
Chad Rosier8decdee2012-06-26 22:30:43 +00004719 assert(getCurScope()->isFunctionPrototypeScope() &&
David Blaikie42d6d0c2011-12-04 05:04:18 +00004720 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004721 // lparen is already consumed!
4722 assert(D.isPastIdentifier() && "Should not call before identifier!");
4723
4724 // This should be true when the function has typed arguments.
4725 // Otherwise, it is treated as a K&R-style function.
4726 bool HasProto = false;
4727 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004728 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004729 // Remember where we see an ellipsis, if any.
4730 SourceLocation EllipsisLoc;
4731
4732 DeclSpec DS(AttrFactory);
4733 bool RefQualifierIsLValueRef = true;
4734 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004735 SourceLocation ConstQualifierLoc;
4736 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004737 ExceptionSpecificationType ESpecType = EST_None;
4738 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004739 SmallVector<ParsedType, 2> DynamicExceptions;
4740 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004741 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004742 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004743 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004744
James Molloy16f1f712012-02-29 10:24:19 +00004745 Actions.ActOnStartFunctionDeclarator();
4746
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004747 /* LocalEndLoc is the end location for the local FunctionTypeLoc.
4748 EndLoc is the end location for the function declarator.
4749 They differ for trailing return types. */
4750 SourceLocation StartLoc, LocalEndLoc, EndLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004751 SourceLocation LParenLoc, RParenLoc;
4752 LParenLoc = Tracker.getOpenLocation();
4753 StartLoc = LParenLoc;
4754
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004755 if (isFunctionDeclaratorIdentifierList()) {
4756 if (RequiresArg)
4757 Diag(Tok, diag::err_argument_required_after_attribute);
4758
4759 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4760
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004761 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004762 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004763 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004764 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004765 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004766 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004767 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004768 else if (RequiresArg)
4769 Diag(Tok, diag::err_argument_required_after_attribute);
4770
David Blaikie4e4d0842012-03-11 07:00:24 +00004771 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004772
4773 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004774 Tracker.consumeClose();
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004775 RParenLoc = Tracker.getCloseLocation();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004776 LocalEndLoc = RParenLoc;
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004777 EndLoc = RParenLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004778
David Blaikie4e4d0842012-03-11 07:00:24 +00004779 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004780 // FIXME: Accept these components in any order, and produce fixits to
4781 // correct the order if the user gets it wrong. Ideally we should deal
4782 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004783
4784 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004785 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4786 if (!DS.getSourceRange().getEnd().isInvalid()) {
4787 EndLoc = DS.getSourceRange().getEnd();
4788 ConstQualifierLoc = DS.getConstSpecLoc();
4789 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4790 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004791
4792 // Parse ref-qualifier[opt].
4793 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
Richard Smith80ad52f2013-01-02 11:42:31 +00004794 Diag(Tok, getLangOpts().CPlusPlus11 ?
Richard Smith7fe62082011-10-15 05:09:34 +00004795 diag::warn_cxx98_compat_ref_qualifier :
4796 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004797
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004798 RefQualifierIsLValueRef = Tok.is(tok::amp);
4799 RefQualifierLoc = ConsumeToken();
4800 EndLoc = RefQualifierLoc;
4801 }
4802
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004803 // C++11 [expr.prim.general]p3:
Chad Rosier8decdee2012-06-26 22:30:43 +00004804 // If a declaration declares a member function or member function
4805 // template of a class X, the expression this is a prvalue of type
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004806 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
Chad Rosier8decdee2012-06-26 22:30:43 +00004807 // and the end of the function-definition, member-declarator, or
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004808 // declarator.
Chad Rosier8decdee2012-06-26 22:30:43 +00004809 bool IsCXX11MemberFunction =
Richard Smith80ad52f2013-01-02 11:42:31 +00004810 getLangOpts().CPlusPlus11 &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004811 (D.getContext() == Declarator::MemberContext ||
4812 (D.getContext() == Declarator::FileContext &&
Chad Rosier8decdee2012-06-26 22:30:43 +00004813 D.getCXXScopeSpec().isValid() &&
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004814 Actions.CurContext->isRecord()));
4815 Sema::CXXThisScopeRAII ThisScope(Actions,
4816 dyn_cast<CXXRecordDecl>(Actions.CurContext),
Richard Smith7b19cb12013-01-14 01:55:13 +00004817 DS.getTypeQualifiers() |
4818 (D.getDeclSpec().isConstexprSpecified()
4819 ? Qualifiers::Const : 0),
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004820 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004821
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004822 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004823 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004824 DynamicExceptions,
4825 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004826 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004827 if (ESpecType != EST_None)
4828 EndLoc = ESpecRange.getEnd();
4829
Richard Smith6ee326a2012-04-10 01:32:12 +00004830 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4831 // after the exception-specification.
Richard Smith4e24f0f2013-01-02 12:01:23 +00004832 MaybeParseCXX11Attributes(FnAttrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00004833
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004834 // Parse trailing-return-type[opt].
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004835 LocalEndLoc = EndLoc;
Richard Smith80ad52f2013-01-02 11:42:31 +00004836 if (getLangOpts().CPlusPlus11 && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004837 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004838 if (D.getDeclSpec().getTypeSpecType() == TST_auto)
4839 StartLoc = D.getDeclSpec().getTypeSpecTypeLoc();
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004840 LocalEndLoc = Tok.getLocation();
Douglas Gregorae7902c2011-08-04 15:30:47 +00004841 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004842 TrailingReturnType = ParseTrailingReturnType(Range);
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004843 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004844 }
4845 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004846 }
4847
4848 // Remember that we parsed a function type, and remember the attributes.
4849 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004850 IsAmbiguous,
4851 LParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004852 ParamInfo.data(), ParamInfo.size(),
Abramo Bagnara59c0a812012-10-04 21:42:10 +00004853 EllipsisLoc, RParenLoc,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004854 DS.getTypeQualifiers(),
4855 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004856 RefQualifierLoc, ConstQualifierLoc,
4857 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004858 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004859 ESpecType, ESpecRange.getBegin(),
4860 DynamicExceptions.data(),
4861 DynamicExceptionRanges.data(),
4862 DynamicExceptions.size(),
4863 NoexceptExpr.isUsable() ?
4864 NoexceptExpr.get() : 0,
Abramo Bagnaraac8ea052012-10-15 21:05:46 +00004865 StartLoc, LocalEndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004866 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004867 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004868
4869 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004870}
4871
4872/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4873/// identifier list form for a K&R-style function: void foo(a,b,c)
4874///
4875/// Note that identifier-lists are only allowed for normal declarators, not for
4876/// abstract-declarators.
4877bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004878 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004879 && Tok.is(tok::identifier)
4880 && !TryAltiVecVectorToken()
4881 // K&R identifier lists can't have typedefs as identifiers, per C99
4882 // 6.7.5.3p11.
4883 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4884 // Identifier lists follow a really simple grammar: the identifiers can
4885 // be followed *only* by a ", identifier" or ")". However, K&R
4886 // identifier lists are really rare in the brave new modern world, and
4887 // it is very common for someone to typo a type in a non-K&R style
4888 // list. If we are presented with something like: "void foo(intptr x,
4889 // float y)", we don't want to start parsing the function declarator as
4890 // though it is a K&R style declarator just because intptr is an
4891 // invalid type.
4892 //
4893 // To handle this, we check to see if the token after the first
4894 // identifier is a "," or ")". Only then do we parse it as an
4895 // identifier list.
4896 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4897}
4898
4899/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4900/// we found a K&R-style identifier list instead of a typed parameter list.
4901///
4902/// After returning, ParamInfo will hold the parsed parameters.
4903///
4904/// identifier-list: [C99 6.7.5]
4905/// identifier
4906/// identifier-list ',' identifier
4907///
4908void Parser::ParseFunctionDeclaratorIdentifierList(
4909 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004910 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004911 // If there was no identifier specified for the declarator, either we are in
4912 // an abstract-declarator, or we are in a parameter declarator which was found
4913 // to be abstract. In abstract-declarators, identifier lists are not valid:
4914 // diagnose this.
4915 if (!D.getIdentifier())
4916 Diag(Tok, diag::ext_ident_list_in_param);
4917
4918 // Maintain an efficient lookup of params we have seen so far.
4919 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4920
4921 while (1) {
4922 // If this isn't an identifier, report the error and skip until ')'.
4923 if (Tok.isNot(tok::identifier)) {
4924 Diag(Tok, diag::err_expected_ident);
4925 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4926 // Forget we parsed anything.
4927 ParamInfo.clear();
4928 return;
4929 }
4930
4931 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4932
4933 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4934 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4935 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4936
4937 // Verify that the argument identifier has not already been mentioned.
4938 if (!ParamsSoFar.insert(ParmII)) {
4939 Diag(Tok, diag::err_param_redefinition) << ParmII;
4940 } else {
4941 // Remember this identifier in ParamInfo.
4942 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4943 Tok.getLocation(),
4944 0));
4945 }
4946
4947 // Eat the identifier.
4948 ConsumeToken();
4949
4950 // The list continues if we see a comma.
4951 if (Tok.isNot(tok::comma))
4952 break;
4953 ConsumeToken();
4954 }
4955}
4956
4957/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4958/// after the opening parenthesis. This function will not parse a K&R-style
4959/// identifier list.
4960///
Richard Smith6ce48a72012-04-11 04:01:28 +00004961/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4962/// caller parsed those arguments immediately after the open paren - they should
4963/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004964///
4965/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4966/// be the location of the ellipsis, if any was parsed.
4967///
Reid Spencer5f016e22007-07-11 17:01:13 +00004968/// parameter-type-list: [C99 6.7.5]
4969/// parameter-list
4970/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004971/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004972///
4973/// parameter-list: [C99 6.7.5]
4974/// parameter-declaration
4975/// parameter-list ',' parameter-declaration
4976///
4977/// parameter-declaration: [C99 6.7.5]
4978/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004979/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004980/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004981/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004982/// declaration-specifiers abstract-declarator[opt]
4983/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004984/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004985/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004986/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004987///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004988void Parser::ParseParameterDeclarationClause(
4989 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004990 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004991 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004992 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004993
Chris Lattnerf97409f2008-04-06 06:57:35 +00004994 while (1) {
4995 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004996 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4997 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004998 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004999 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00005000 }
Mike Stump1eb44332009-09-09 15:08:12 +00005001
Chris Lattnerf97409f2008-04-06 06:57:35 +00005002 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00005003 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00005004 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005005
Richard Smith6ce48a72012-04-11 04:01:28 +00005006 // Parse any C++11 attributes.
Richard Smith4e24f0f2013-01-02 12:01:23 +00005007 MaybeParseCXX11Attributes(DS.getAttributes());
Richard Smith6ce48a72012-04-11 04:01:28 +00005008
John McCall7f040a92010-12-24 02:08:15 +00005009 // Skip any Microsoft attributes before a param.
Chad Rosier16f90bf2012-12-20 20:37:53 +00005010 MaybeParseMicrosoftAttributes(DS.getAttributes());
John McCall7f040a92010-12-24 02:08:15 +00005011
5012 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00005013
5014 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00005015 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00005016 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00005017 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
5018 // too much hassle.
5019 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00005020
Chris Lattnere64c5492009-02-27 18:38:20 +00005021 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00005022
Chris Lattnerf97409f2008-04-06 06:57:35 +00005023 // Parse the declarator. This is "PrototypeContext", because we must
5024 // accept either 'declarator' or 'abstract-declarator' here.
5025 Declarator ParmDecl(DS, Declarator::PrototypeContext);
5026 ParseDeclarator(ParmDecl);
5027
5028 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00005029 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00005030
Chris Lattnerf97409f2008-04-06 06:57:35 +00005031 // Remember this parsed parameter in ParamInfo.
5032 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00005033
Douglas Gregor72b505b2008-12-16 21:30:33 +00005034 // DefArgToks is used when the parsing of default arguments needs
5035 // to be delayed.
5036 CachedTokens *DefArgToks = 0;
5037
Chris Lattnerf97409f2008-04-06 06:57:35 +00005038 // If no parameter was specified, verify that *something* was specified,
5039 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00005040 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
5041 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00005042 // Completely missing, emit error.
5043 Diag(DSStart, diag::err_missing_param);
5044 } else {
5045 // Otherwise, we have something. Add it and let semantic analysis try
5046 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00005047
Chris Lattnerf97409f2008-04-06 06:57:35 +00005048 // Inform the actions module about the parameter declarator, so it gets
5049 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00005050 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00005051
5052 // Parse the default argument, if any. We parse the default
5053 // arguments in all dialects; the semantic analysis in
5054 // ActOnParamDefaultArgument will reject the default argument in
5055 // C.
5056 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00005057 SourceLocation EqualLoc = Tok.getLocation();
5058
Chris Lattner04421082008-04-08 04:40:51 +00005059 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00005060 if (D.getContext() == Declarator::MemberContext) {
5061 // If we're inside a class definition, cache the tokens
5062 // corresponding to the default argument. We'll actually parse
5063 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00005064 // FIXME: Can we use a smart pointer for Toks?
5065 DefArgToks = new CachedTokens;
5066
Mike Stump1eb44332009-09-09 15:08:12 +00005067 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00005068 /*StopAtSemi=*/true,
5069 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005070 delete DefArgToks;
5071 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00005072 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005073 } else {
5074 // Mark the end of the default argument so that we know when to
5075 // stop when we parse it later on.
5076 Token DefArgEnd;
5077 DefArgEnd.startToken();
5078 DefArgEnd.setKind(tok::cxx_defaultarg_end);
5079 DefArgEnd.setLocation(Tok.getLocation());
5080 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00005081 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00005082 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00005083 }
Chris Lattner04421082008-04-08 04:40:51 +00005084 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00005085 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00005086 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005087
Chad Rosier8decdee2012-06-26 22:30:43 +00005088 // The argument isn't actually potentially evaluated unless it is
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005089 // used.
5090 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00005091 Sema::PotentiallyEvaluatedIfUsed,
5092 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00005093
Sebastian Redl84407ba2012-03-14 15:54:00 +00005094 ExprResult DefArgResult;
Richard Smith80ad52f2013-01-02 11:42:31 +00005095 if (getLangOpts().CPlusPlus11 && Tok.is(tok::l_brace)) {
Sebastian Redl3e280b52012-03-18 22:25:45 +00005096 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00005097 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00005098 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00005099 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00005100 if (DefArgResult.isInvalid()) {
5101 Actions.ActOnParamDefaultArgumentError(Param);
5102 SkipUntil(tok::comma, tok::r_paren, true, true);
5103 } else {
5104 // Inform the actions module about the default argument
5105 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00005106 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00005107 }
Chris Lattner04421082008-04-08 04:40:51 +00005108 }
5109 }
Mike Stump1eb44332009-09-09 15:08:12 +00005110
5111 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
5112 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00005113 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00005114 }
5115
5116 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00005117 if (Tok.isNot(tok::comma)) {
5118 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005119 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chad Rosier8decdee2012-06-26 22:30:43 +00005120
David Blaikie4e4d0842012-03-11 07:00:24 +00005121 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00005122 // We have ellipsis without a preceding ',', which is ill-formed
5123 // in C. Complain and provide the fix.
5124 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00005125 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00005126 }
5127 }
Chad Rosier8decdee2012-06-26 22:30:43 +00005128
Douglas Gregored5d6512009-09-22 21:41:40 +00005129 break;
5130 }
Mike Stump1eb44332009-09-09 15:08:12 +00005131
Chris Lattnerf97409f2008-04-06 06:57:35 +00005132 // Consume the comma.
5133 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00005134 }
Mike Stump1eb44332009-09-09 15:08:12 +00005135
Chris Lattner66d28652008-04-06 06:34:08 +00005136}
Chris Lattneref4715c2008-04-06 05:45:57 +00005137
Reid Spencer5f016e22007-07-11 17:01:13 +00005138/// [C90] direct-declarator '[' constant-expression[opt] ']'
5139/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
5140/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
5141/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
5142/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00005143/// [C++11] direct-declarator '[' constant-expression[opt] ']'
5144/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00005145void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00005146 if (CheckProhibitedCXX11Attribute())
5147 return;
5148
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005149 BalancedDelimiterTracker T(*this, tok::l_square);
5150 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00005151
Chris Lattner378c7e42008-12-18 07:27:21 +00005152 // C array syntax has many features, but by-far the most common is [] and [4].
5153 // This code does a fast path to handle some of the most obvious cases.
5154 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005155 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005156 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005157 MaybeParseCXX11Attributes(attrs);
Chad Rosier8decdee2012-06-26 22:30:43 +00005158
Chris Lattner378c7e42008-12-18 07:27:21 +00005159 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00005160 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00005161 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005162 T.getOpenLocation(),
5163 T.getCloseLocation()),
5164 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005165 return;
5166 } else if (Tok.getKind() == tok::numeric_constant &&
5167 GetLookAheadToken(1).is(tok::r_square)) {
5168 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00005169 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00005170 ConsumeToken();
5171
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005172 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00005173 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005174 MaybeParseCXX11Attributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00005175
Chris Lattner378c7e42008-12-18 07:27:21 +00005176 // Remember that we parsed a array type, and remember its features.
Nikola Smiljanicebf0fa82013-01-11 08:33:05 +00005177 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false,
John McCall7f040a92010-12-24 02:08:15 +00005178 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005179 T.getOpenLocation(),
5180 T.getCloseLocation()),
5181 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00005182 return;
5183 }
Mike Stump1eb44332009-09-09 15:08:12 +00005184
Reid Spencer5f016e22007-07-11 17:01:13 +00005185 // If valid, this location is the position where we read the 'static' keyword.
5186 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00005187 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005188 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005189
Reid Spencer5f016e22007-07-11 17:01:13 +00005190 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005191 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00005192 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00005193 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00005194
Reid Spencer5f016e22007-07-11 17:01:13 +00005195 // If we haven't already read 'static', check to see if there is one after the
5196 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00005197 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00005198 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00005199
Reid Spencer5f016e22007-07-11 17:01:13 +00005200 // Handle "direct-declarator [ type-qual-list[opt] * ]".
5201 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00005202 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00005203
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005204 // Handle the case where we have '[*]' as the array size. However, a leading
5205 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
Sylvestre Ledrubed28ac2012-07-23 08:59:39 +00005206 // the token after the star is a ']'. Since stars in arrays are
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005207 // infrequent, use of lookahead is not costly here.
5208 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00005209 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00005210
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005211 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005212 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00005213 StaticLoc = SourceLocation(); // Drop the static.
5214 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00005215 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00005216 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00005217 // Note, in C89, this production uses the constant-expr production instead
5218 // of assignment-expr. The only difference is that assignment-expr allows
5219 // things like '=' and '*='. Sema rejects these in C89 mode because they
5220 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00005221
Douglas Gregore0762c92009-06-19 23:52:42 +00005222 // Parse the constant-expression or assignment-expression now (depending
5223 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00005224 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00005225 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005226 } else {
5227 EnterExpressionEvaluationContext Unevaluated(Actions,
5228 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00005229 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00005230 }
Reid Spencer5f016e22007-07-11 17:01:13 +00005231 }
Mike Stump1eb44332009-09-09 15:08:12 +00005232
Reid Spencer5f016e22007-07-11 17:01:13 +00005233 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00005234 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00005235 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00005236 // If the expression was invalid, skip it.
5237 SkipUntil(tok::r_square);
5238 return;
5239 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00005240
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005241 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00005242
John McCall0b7e6782011-03-24 11:26:52 +00005243 ParsedAttributes attrs(AttrFactory);
Richard Smith4e24f0f2013-01-02 12:01:23 +00005244 MaybeParseCXX11Attributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00005245
Chris Lattner378c7e42008-12-18 07:27:21 +00005246 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00005247 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00005248 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00005249 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005250 T.getOpenLocation(),
5251 T.getCloseLocation()),
5252 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00005253}
5254
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005255/// [GNU] typeof-specifier:
5256/// typeof ( expressions )
5257/// typeof ( type-name )
5258/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00005259///
5260void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00005261 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005262 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005263 SourceLocation StartLoc = ConsumeToken();
5264
John McCallcfb708c2010-01-13 20:03:27 +00005265 const bool hasParens = Tok.is(tok::l_paren);
5266
Eli Friedman80bfa3d2012-09-26 04:34:21 +00005267 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated,
5268 Sema::ReuseLambdaContextDecl);
Eli Friedman71b8fb52012-01-21 01:01:51 +00005269
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005270 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00005271 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005272 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00005273 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
5274 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00005275 if (hasParens)
5276 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005277
5278 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005279 // FIXME: Not accurate, the range gets one token more than it should.
5280 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005281 else
5282 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00005283
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005284 if (isCastExpr) {
5285 if (!CastTy) {
5286 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005287 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00005288 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005289
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005290 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005291 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005292 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5293 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00005294 DiagID, CastTy))
5295 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00005296 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005297 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005298
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005299 // If we get here, the operand to the typeof was an expresion.
5300 if (Operand.isInvalid()) {
5301 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00005302 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005303 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00005304
Eli Friedman71b8fb52012-01-21 01:01:51 +00005305 // We might need to transform the operand if it is potentially evaluated.
5306 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
5307 if (Operand.isInvalid()) {
5308 DS.SetTypeSpecError();
5309 return;
5310 }
5311
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005312 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00005313 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00005314 // Check for duplicate type specifiers (e.g. "int typeof(int)").
5315 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00005316 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00005317 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00005318}
Chris Lattner1b492422010-02-28 18:33:55 +00005319
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00005320/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00005321/// _Atomic ( type-name )
5322///
5323void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5324 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5325
5326 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005327 BalancedDelimiterTracker T(*this, tok::l_paren);
5328 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00005329 SkipUntil(tok::r_paren);
5330 return;
5331 }
5332
5333 TypeResult Result = ParseTypeName();
5334 if (Result.isInvalid()) {
5335 SkipUntil(tok::r_paren);
5336 return;
5337 }
5338
5339 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005340 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005341
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005342 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005343 return;
5344
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005345 DS.setTypeofParensRange(T.getRange());
5346 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005347
5348 const char *PrevSpec = 0;
5349 unsigned DiagID;
5350 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5351 DiagID, Result.release()))
5352 Diag(StartLoc, DiagID) << PrevSpec;
5353}
5354
Chris Lattner1b492422010-02-28 18:33:55 +00005355
5356/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5357/// from TryAltiVecVectorToken.
5358bool Parser::TryAltiVecVectorTokenOutOfLine() {
5359 Token Next = NextToken();
5360 switch (Next.getKind()) {
5361 default: return false;
5362 case tok::kw_short:
5363 case tok::kw_long:
5364 case tok::kw_signed:
5365 case tok::kw_unsigned:
5366 case tok::kw_void:
5367 case tok::kw_char:
5368 case tok::kw_int:
5369 case tok::kw_float:
5370 case tok::kw_double:
5371 case tok::kw_bool:
5372 case tok::kw___pixel:
5373 Tok.setKind(tok::kw___vector);
5374 return true;
5375 case tok::identifier:
5376 if (Next.getIdentifierInfo() == Ident_pixel) {
5377 Tok.setKind(tok::kw___vector);
5378 return true;
5379 }
5380 return false;
5381 }
5382}
5383
5384bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5385 const char *&PrevSpec, unsigned &DiagID,
5386 bool &isInvalid) {
5387 if (Tok.getIdentifierInfo() == Ident_vector) {
5388 Token Next = NextToken();
5389 switch (Next.getKind()) {
5390 case tok::kw_short:
5391 case tok::kw_long:
5392 case tok::kw_signed:
5393 case tok::kw_unsigned:
5394 case tok::kw_void:
5395 case tok::kw_char:
5396 case tok::kw_int:
5397 case tok::kw_float:
5398 case tok::kw_double:
5399 case tok::kw_bool:
5400 case tok::kw___pixel:
5401 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5402 return true;
5403 case tok::identifier:
5404 if (Next.getIdentifierInfo() == Ident_pixel) {
5405 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5406 return true;
5407 }
5408 break;
5409 default:
5410 break;
5411 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005412 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005413 DS.isTypeAltiVecVector()) {
5414 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5415 return true;
5416 }
5417 return false;
5418}