blob: 8c24f9761f4a0b061f038def7124ba3ea004adaa [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"
Chris Lattner500d3292009-01-29 05:15:15 +000015#include "clang/Parse/ParseDiagnostic.h"
Peter Collingbourne207f4d82011-03-18 22:38:29 +000016#include "clang/Basic/OpenCL.h"
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +000017#include "clang/Sema/Lookup.h"
John McCall19510852010-08-20 18:27:03 +000018#include "clang/Sema/Scope.h"
19#include "clang/Sema/ParsedTemplate.h"
John McCallf312b1e2010-08-26 23:41:50 +000020#include "clang/Sema/PrettyDeclStackTrace.h"
Chris Lattnerd167ca02009-12-10 00:21:05 +000021#include "RAIIObjectsForParser.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include "llvm/ADT/SmallSet.h"
Benjamin Kramer8fe83e12012-02-04 13:45:25 +000023#include "llvm/ADT/SmallString.h"
Caitlin Sadowskib51e0312011-08-09 17:59:31 +000024#include "llvm/ADT/StringSwitch.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000025using namespace clang;
26
27//===----------------------------------------------------------------------===//
28// C99 6.7: Declarations.
29//===----------------------------------------------------------------------===//
30
31/// ParseTypeName
32/// type-name: [C99 6.7.6]
33/// specifier-qualifier-list abstract-declarator[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +000034///
35/// Called type-id in C++.
Douglas Gregor683a81f2011-01-31 16:09:46 +000036TypeResult Parser::ParseTypeName(SourceRange *Range,
John McCallf85e1932011-06-15 23:02:42 +000037 Declarator::TheContext Context,
Richard Smithc89edf52011-07-01 19:46:12 +000038 AccessSpecifier AS,
39 Decl **OwnedType) {
Richard Smith6d96d3a2012-03-15 01:02:11 +000040 DeclSpecContext DSC = getDeclSpecContextFromDeclaratorContext(Context);
Richard Smitha971d242012-05-09 20:55:26 +000041 if (DSC == DSC_normal)
42 DSC = DSC_type_specifier;
Richard Smith7796eb52012-03-12 08:56:40 +000043
Reid Spencer5f016e22007-07-11 17:01:13 +000044 // Parse the common declaration-specifiers piece.
John McCall0b7e6782011-03-24 11:26:52 +000045 DeclSpec DS(AttrFactory);
Richard Smith7796eb52012-03-12 08:56:40 +000046 ParseSpecifierQualifierList(DS, AS, DSC);
Richard Smithc89edf52011-07-01 19:46:12 +000047 if (OwnedType)
48 *OwnedType = DS.isTypeSpecOwned() ? DS.getRepAsDecl() : 0;
Sebastian Redlef65f062009-05-29 18:02:33 +000049
Reid Spencer5f016e22007-07-11 17:01:13 +000050 // Parse the abstract-declarator, if present.
Douglas Gregor683a81f2011-01-31 16:09:46 +000051 Declarator DeclaratorInfo(DS, Context);
Reid Spencer5f016e22007-07-11 17:01:13 +000052 ParseDeclarator(DeclaratorInfo);
Sebastian Redlef65f062009-05-29 18:02:33 +000053 if (Range)
54 *Range = DeclaratorInfo.getSourceRange();
55
Chris Lattnereaaebc72009-04-25 08:06:05 +000056 if (DeclaratorInfo.isInvalidType())
Douglas Gregor809070a2009-02-18 17:45:20 +000057 return true;
58
Douglas Gregor23c94db2010-07-02 17:43:08 +000059 return Actions.ActOnTypeName(getCurScope(), DeclaratorInfo);
Reid Spencer5f016e22007-07-11 17:01:13 +000060}
61
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +000062
63/// isAttributeLateParsed - Return true if the attribute has arguments that
64/// require late parsing.
65static bool isAttributeLateParsed(const IdentifierInfo &II) {
66 return llvm::StringSwitch<bool>(II.getName())
67#include "clang/Parse/AttrLateParsed.inc"
68 .Default(false);
69}
70
71
Sean Huntbbd37c62009-11-21 08:43:09 +000072/// ParseGNUAttributes - Parse a non-empty attributes list.
Reid Spencer5f016e22007-07-11 17:01:13 +000073///
74/// [GNU] attributes:
75/// attribute
76/// attributes attribute
77///
78/// [GNU] attribute:
79/// '__attribute__' '(' '(' attribute-list ')' ')'
80///
81/// [GNU] attribute-list:
82/// attrib
83/// attribute_list ',' attrib
84///
85/// [GNU] attrib:
86/// empty
87/// attrib-name
88/// attrib-name '(' identifier ')'
89/// attrib-name '(' identifier ',' nonempty-expr-list ')'
90/// attrib-name '(' argument-expression-list [C99 6.5.2] ')'
91///
92/// [GNU] attrib-name:
93/// identifier
94/// typespec
95/// typequal
96/// storageclass
Mike Stump1eb44332009-09-09 15:08:12 +000097///
Reid Spencer5f016e22007-07-11 17:01:13 +000098/// FIXME: The GCC grammar/code for this construct implies we need two
Mike Stump1eb44332009-09-09 15:08:12 +000099/// token lookahead. Comment from gcc: "If they start with an identifier
100/// which is followed by a comma or close parenthesis, then the arguments
Reid Spencer5f016e22007-07-11 17:01:13 +0000101/// start with that identifier; otherwise they are an expression list."
102///
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000103/// GCC does not require the ',' between attribs in an attribute-list.
104///
Reid Spencer5f016e22007-07-11 17:01:13 +0000105/// At the moment, I am not doing 2 token lookahead. I am also unaware of
106/// any attributes that don't work (based on my limited testing). Most
107/// attributes are very simple in practice. Until we find a bug, I don't see
108/// a pressing need to implement the 2 token lookahead.
109
John McCall7f040a92010-12-24 02:08:15 +0000110void Parser::ParseGNUAttributes(ParsedAttributes &attrs,
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000111 SourceLocation *endLoc,
112 LateParsedAttrList *LateAttrs) {
Sean Huntbbd37c62009-11-21 08:43:09 +0000113 assert(Tok.is(tok::kw___attribute) && "Not a GNU attribute list!");
Mike Stump1eb44332009-09-09 15:08:12 +0000114
Chris Lattner04d66662007-10-09 17:33:22 +0000115 while (Tok.is(tok::kw___attribute)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000116 ConsumeToken();
117 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after,
118 "attribute")) {
119 SkipUntil(tok::r_paren, true); // skip until ) or ;
John McCall7f040a92010-12-24 02:08:15 +0000120 return;
Reid Spencer5f016e22007-07-11 17:01:13 +0000121 }
122 if (ExpectAndConsume(tok::l_paren, diag::err_expected_lparen_after, "(")) {
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 // Parse the attribute-list. e.g. __attribute__(( weak, alias("__f") ))
Chris Lattner04d66662007-10-09 17:33:22 +0000127 while (Tok.is(tok::identifier) || isDeclarationSpecifier() ||
128 Tok.is(tok::comma)) {
Mike Stump1eb44332009-09-09 15:08:12 +0000129 if (Tok.is(tok::comma)) {
Reid Spencer5f016e22007-07-11 17:01:13 +0000130 // allows for empty/non-empty attributes. ((__vector_size__(16),,,,))
131 ConsumeToken();
132 continue;
133 }
134 // we have an identifier or declaration specifier (const, int, etc.)
135 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
136 SourceLocation AttrNameLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +0000137
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000138 if (Tok.is(tok::l_paren)) {
139 // handle "parameterized" attributes
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000140 if (LateAttrs && isAttributeLateParsed(*AttrName)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000141 LateParsedAttribute *LA =
142 new LateParsedAttribute(this, *AttrName, AttrNameLoc);
143 LateAttrs->push_back(LA);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000144
145 // Attributes in a class are parsed at the end of the class, along
146 // with other late-parsed declarations.
147 if (!ClassStack.empty())
148 getCurrentClass().LateParsedDeclarations.push_back(LA);
Mike Stump1eb44332009-09-09 15:08:12 +0000149
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000150 // consume everything up to and including the matching right parens
151 ConsumeAndStoreUntil(tok::r_paren, LA->Toks, true, false);
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000153 Token Eof;
154 Eof.startToken();
155 Eof.setLocation(Tok.getLocation());
156 LA->Toks.push_back(Eof);
157 } else {
158 ParseGNUAttributeArgs(AttrName, AttrNameLoc, attrs, endLoc);
Reid Spencer5f016e22007-07-11 17:01:13 +0000159 }
160 } else {
John McCall0b7e6782011-03-24 11:26:52 +0000161 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000162 0, SourceLocation(), 0, 0, AttributeList::AS_GNU);
Reid Spencer5f016e22007-07-11 17:01:13 +0000163 }
164 }
165 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen))
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 SkipUntil(tok::r_paren, false);
Sean Huntbbd37c62009-11-21 08:43:09 +0000167 SourceLocation Loc = Tok.getLocation();
Sebastian Redlab197ba2009-02-09 18:23:29 +0000168 if (ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
169 SkipUntil(tok::r_paren, false);
170 }
John McCall7f040a92010-12-24 02:08:15 +0000171 if (endLoc)
172 *endLoc = Loc;
Reid Spencer5f016e22007-07-11 17:01:13 +0000173 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000174}
175
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000176
177/// Parse the arguments to a parameterized GNU attribute
178void Parser::ParseGNUAttributeArgs(IdentifierInfo *AttrName,
179 SourceLocation AttrNameLoc,
180 ParsedAttributes &Attrs,
181 SourceLocation *EndLoc) {
182
183 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
184
185 // Availability attributes have their own grammar.
186 if (AttrName->isStr("availability")) {
187 ParseAvailabilityAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
188 return;
189 }
190 // Thread safety attributes fit into the FIXME case above, so we
191 // just parse the arguments as a list of expressions
192 if (IsThreadSafetyAttribute(AttrName->getName())) {
193 ParseThreadSafetyAttribute(*AttrName, AttrNameLoc, Attrs, EndLoc);
194 return;
195 }
196
197 ConsumeParen(); // ignore the left paren loc for now
198
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000199 IdentifierInfo *ParmName = 0;
200 SourceLocation ParmLoc;
201 bool BuiltinType = false;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000202
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000203 switch (Tok.getKind()) {
204 case tok::kw_char:
205 case tok::kw_wchar_t:
206 case tok::kw_char16_t:
207 case tok::kw_char32_t:
208 case tok::kw_bool:
209 case tok::kw_short:
210 case tok::kw_int:
211 case tok::kw_long:
212 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +0000213 case tok::kw___int128:
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000214 case tok::kw_signed:
215 case tok::kw_unsigned:
216 case tok::kw_float:
217 case tok::kw_double:
218 case tok::kw_void:
219 case tok::kw_typeof:
220 // __attribute__(( vec_type_hint(char) ))
221 // FIXME: Don't just discard the builtin type token.
222 ConsumeToken();
223 BuiltinType = true;
224 break;
225
226 case tok::identifier:
227 ParmName = Tok.getIdentifierInfo();
228 ParmLoc = ConsumeToken();
229 break;
230
231 default:
232 break;
233 }
234
235 ExprVector ArgExprs(Actions);
236
237 if (!BuiltinType &&
238 (ParmLoc.isValid() ? Tok.is(tok::comma) : Tok.isNot(tok::r_paren))) {
239 // Eat the comma.
240 if (ParmLoc.isValid())
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000241 ConsumeToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000242
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000243 // Parse the non-empty comma-separated list of expressions.
244 while (1) {
245 ExprResult ArgExpr(ParseAssignmentExpression());
246 if (ArgExpr.isInvalid()) {
247 SkipUntil(tok::r_paren);
248 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000249 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000250 ArgExprs.push_back(ArgExpr.release());
251 if (Tok.isNot(tok::comma))
252 break;
253 ConsumeToken(); // Eat the comma, move to the next argument
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000254 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000255 }
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000256 else if (Tok.is(tok::less) && AttrName->isStr("iboutletcollection")) {
257 if (!ExpectAndConsume(tok::less, diag::err_expected_less_after, "<",
258 tok::greater)) {
Fariborz Jahanianb2243432011-10-18 23:13:50 +0000259 while (Tok.is(tok::identifier)) {
260 ConsumeToken();
261 if (Tok.is(tok::greater))
262 break;
263 if (Tok.is(tok::comma)) {
264 ConsumeToken();
265 continue;
266 }
267 }
268 if (Tok.isNot(tok::greater))
269 Diag(Tok, diag::err_iboutletcollection_with_protocol);
Fariborz Jahanian7a81e412011-10-18 17:11:10 +0000270 SkipUntil(tok::r_paren, false, true); // skip until ')'
271 }
272 }
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000273
274 SourceLocation RParen = Tok.getLocation();
275 if (!ExpectAndConsume(tok::r_paren, diag::err_expected_rparen)) {
276 AttributeList *attr =
Argyrios Kyrtzidisffcc3102011-09-13 16:05:53 +0000277 Attrs.addNew(AttrName, SourceRange(AttrNameLoc, RParen), 0, AttrNameLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +0000278 ParmName, ParmLoc, ArgExprs.take(), ArgExprs.size(),
279 AttributeList::AS_GNU);
Sean Hunt8e083e72012-06-19 23:57:03 +0000280 if (BuiltinType && attr->getKind() == AttributeList::AT_IBOutletCollection)
Richard Smithfe0a0fb2011-10-17 21:20:17 +0000281 Diag(Tok, diag::err_iboutletcollection_builtintype);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000282 }
283}
284
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000285/// \brief Parses a single argument for a declspec, including the
286/// surrounding parens.
287void Parser::ParseMicrosoftDeclSpecWithSingleArg(IdentifierInfo *AttrName,
288 SourceLocation AttrNameLoc,
289 ParsedAttributes &Attrs)
290{
291 BalancedDelimiterTracker T(*this, tok::l_paren);
292 if (T.expectAndConsume(diag::err_expected_lparen_after,
293 AttrName->getNameStart(), tok::r_paren))
294 return;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000295
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000296 ExprResult ArgExpr(ParseConstantExpression());
297 if (ArgExpr.isInvalid()) {
298 T.skipToEnd();
299 return;
300 }
301 Expr *ExprList = ArgExpr.take();
302 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
303 &ExprList, 1, AttributeList::AS_Declspec);
304
305 T.consumeClose();
306}
307
308/// \brief Determines whether a declspec is a "simple" one requiring no
309/// arguments.
310bool Parser::IsSimpleMicrosoftDeclSpec(IdentifierInfo *Ident) {
311 return llvm::StringSwitch<bool>(Ident->getName())
312 .Case("dllimport", true)
313 .Case("dllexport", true)
314 .Case("noreturn", true)
315 .Case("nothrow", true)
316 .Case("noinline", true)
317 .Case("naked", true)
318 .Case("appdomain", true)
319 .Case("process", true)
320 .Case("jitintrinsic", true)
321 .Case("noalias", true)
322 .Case("restrict", true)
323 .Case("novtable", true)
324 .Case("selectany", true)
325 .Case("thread", true)
326 .Default(false);
327}
328
329/// \brief Attempts to parse a declspec which is not simple (one that takes
330/// parameters). Will return false if we properly handled the declspec, or
331/// true if it is an unknown declspec.
332void Parser::ParseComplexMicrosoftDeclSpec(IdentifierInfo *Ident,
333 SourceLocation Loc,
334 ParsedAttributes &Attrs) {
335 // Try to handle the easy case first -- these declspecs all take a single
336 // parameter as their argument.
337 if (llvm::StringSwitch<bool>(Ident->getName())
338 .Case("uuid", true)
339 .Case("align", true)
340 .Case("allocate", true)
341 .Default(false)) {
342 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
343 } else if (Ident->getName() == "deprecated") {
344 // The deprecated declspec has an optional single argument, so we will
345 // check for a l-paren to decide whether we should parse an argument or
346 // not.
347 if (Tok.getKind() == tok::l_paren)
348 ParseMicrosoftDeclSpecWithSingleArg(Ident, Loc, Attrs);
349 else
350 Attrs.addNew(Ident, Loc, 0, Loc, 0, SourceLocation(), 0, 0,
351 AttributeList::AS_Declspec);
352 } else if (Ident->getName() == "property") {
353 // The property declspec is more complex in that it can take one or two
354 // assignment expressions as a parameter, but the lhs of the assignment
355 // must be named get or put.
356 //
357 // For right now, we will just skip to the closing right paren of the
358 // property expression.
359 //
360 // FIXME: we should deal with __declspec(property) at some point because it
361 // is used in the platform SDK headers for the Parallel Patterns Library
362 // and ATL.
363 BalancedDelimiterTracker T(*this, tok::l_paren);
364 if (T.expectAndConsume(diag::err_expected_lparen_after,
365 Ident->getNameStart(), tok::r_paren))
366 return;
367 T.skipToEnd();
368 } else {
369 // We don't recognize this as a valid declspec, but instead of creating the
370 // attribute and allowing sema to warn about it, we will warn here instead.
371 // This is because some attributes have multiple spellings, but we need to
372 // disallow that for declspecs (such as align vs aligned). If we made the
373 // attribute, we'd have to split the valid declspec spelling logic into
374 // both locations.
375 Diag(Loc, diag::warn_ms_declspec_unknown) << Ident;
376
377 // If there's an open paren, we should eat the open and close parens under
378 // the assumption that this unknown declspec has parameters.
379 BalancedDelimiterTracker T(*this, tok::l_paren);
380 if (!T.consumeOpen())
381 T.skipToEnd();
382 }
383}
384
Eli Friedmana23b4852009-06-08 07:21:15 +0000385/// [MS] decl-specifier:
386/// __declspec ( extended-decl-modifier-seq )
387///
388/// [MS] extended-decl-modifier-seq:
389/// extended-decl-modifier[opt]
390/// extended-decl-modifier extended-decl-modifier-seq
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000391void Parser::ParseMicrosoftDeclSpec(ParsedAttributes &Attrs) {
Steve Narofff59e17e2008-12-24 20:59:21 +0000392 assert(Tok.is(tok::kw___declspec) && "Not a declspec!");
Eli Friedmana23b4852009-06-08 07:21:15 +0000393
Steve Narofff59e17e2008-12-24 20:59:21 +0000394 ConsumeToken();
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000395 BalancedDelimiterTracker T(*this, tok::l_paren);
396 if (T.expectAndConsume(diag::err_expected_lparen_after, "__declspec",
397 tok::r_paren))
John McCall7f040a92010-12-24 02:08:15 +0000398 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000399
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000400 // An empty declspec is perfectly legal and should not warn. Additionally,
401 // you can specify multiple attributes per declspec.
402 while (Tok.getKind() != tok::r_paren) {
403 // We expect either a well-known identifier or a generic string. Anything
404 // else is a malformed declspec.
405 bool IsString = Tok.getKind() == tok::string_literal ? true : false;
406 if (!IsString && Tok.getKind() != tok::identifier &&
407 Tok.getKind() != tok::kw_restrict) {
408 Diag(Tok, diag::err_ms_declspec_type);
409 T.skipToEnd();
410 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000411 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000412
413 IdentifierInfo *AttrName;
414 SourceLocation AttrNameLoc;
415 if (IsString) {
416 SmallString<8> StrBuffer;
417 bool Invalid = false;
418 StringRef Str = PP.getSpelling(Tok, StrBuffer, &Invalid);
419 if (Invalid) {
420 T.skipToEnd();
421 return;
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000422 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000423 AttrName = PP.getIdentifierInfo(Str);
424 AttrNameLoc = ConsumeStringToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000425 } else {
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000426 AttrName = Tok.getIdentifierInfo();
427 AttrNameLoc = ConsumeToken();
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000428 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000429
430 if (IsString || IsSimpleMicrosoftDeclSpec(AttrName))
431 // If we have a generic string, we will allow it because there is no
432 // documented list of allowable string declspecs, but we know they exist
433 // (for instance, SAL declspecs in older versions of MSVC).
434 //
435 // Alternatively, if the identifier is a simple one, then it requires no
436 // arguments and can be turned into an attribute directly.
437 Attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
438 0, 0, AttributeList::AS_Declspec);
439 else
440 ParseComplexMicrosoftDeclSpec(AttrName, AttrNameLoc, Attrs);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +0000441 }
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000442 T.consumeClose();
Eli Friedman290eeb02009-06-08 23:27:34 +0000443}
444
John McCall7f040a92010-12-24 02:08:15 +0000445void Parser::ParseMicrosoftTypeAttributes(ParsedAttributes &attrs) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000446 // Treat these like attributes
Eli Friedman290eeb02009-06-08 23:27:34 +0000447 while (Tok.is(tok::kw___fastcall) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +0000448 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___cdecl) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000449 Tok.is(tok::kw___ptr64) || Tok.is(tok::kw___w64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +0000450 Tok.is(tok::kw___ptr32) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +0000451 Tok.is(tok::kw___unaligned)) {
Eli Friedman290eeb02009-06-08 23:27:34 +0000452 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
453 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000454 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000455 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Eli Friedman290eeb02009-06-08 23:27:34 +0000456 }
Steve Narofff59e17e2008-12-24 20:59:21 +0000457}
458
John McCall7f040a92010-12-24 02:08:15 +0000459void Parser::ParseBorlandTypeAttributes(ParsedAttributes &attrs) {
Dawn Perchik52fc3142010-09-03 01:29:35 +0000460 // Treat these like attributes
461 while (Tok.is(tok::kw___pascal)) {
462 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
463 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000464 attrs.addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Aaron Ballmanfc685ac2012-06-19 22:09:27 +0000465 SourceLocation(), 0, 0, AttributeList::AS_MSTypespec);
Dawn Perchik52fc3142010-09-03 01:29:35 +0000466 }
John McCall7f040a92010-12-24 02:08:15 +0000467}
468
Peter Collingbournef315fa82011-02-14 01:42:53 +0000469void Parser::ParseOpenCLAttributes(ParsedAttributes &attrs) {
470 // Treat these like attributes
471 while (Tok.is(tok::kw___kernel)) {
472 SourceLocation AttrNameLoc = ConsumeToken();
John McCall0b7e6782011-03-24 11:26:52 +0000473 attrs.addNew(PP.getIdentifierInfo("opencl_kernel_function"),
474 AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +0000475 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Peter Collingbournef315fa82011-02-14 01:42:53 +0000476 }
477}
478
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000479void Parser::ParseOpenCLQualifiers(DeclSpec &DS) {
480 SourceLocation Loc = Tok.getLocation();
481 switch(Tok.getKind()) {
482 // OpenCL qualifiers:
483 case tok::kw___private:
484 case tok::kw_private:
John McCall0b7e6782011-03-24 11:26:52 +0000485 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000486 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000487 PP.getIdentifierInfo("address_space"), Loc, 0);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000488 break;
489
490 case tok::kw___global:
John McCall0b7e6782011-03-24 11:26:52 +0000491 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000492 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000493 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_global);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000494 break;
495
496 case tok::kw___local:
John McCall0b7e6782011-03-24 11:26:52 +0000497 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000498 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000499 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_local);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000500 break;
501
502 case tok::kw___constant:
John McCall0b7e6782011-03-24 11:26:52 +0000503 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000504 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000505 PP.getIdentifierInfo("address_space"), Loc, LangAS::opencl_constant);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000506 break;
507
508 case tok::kw___read_only:
John McCall0b7e6782011-03-24 11:26:52 +0000509 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000510 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000511 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000512 break;
513
514 case tok::kw___write_only:
John McCall0b7e6782011-03-24 11:26:52 +0000515 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000516 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000517 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_write_only);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000518 break;
519
520 case tok::kw___read_write:
John McCall0b7e6782011-03-24 11:26:52 +0000521 DS.getAttributes().addNewInteger(
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000522 Actions.getASTContext(),
John McCall0b7e6782011-03-24 11:26:52 +0000523 PP.getIdentifierInfo("opencl_image_access"), Loc, CLIA_read_write);
Peter Collingbourne207f4d82011-03-18 22:38:29 +0000524 break;
525 default: break;
526 }
527}
528
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000529/// \brief Parse a version number.
530///
531/// version:
532/// simple-integer
533/// simple-integer ',' simple-integer
534/// simple-integer ',' simple-integer ',' simple-integer
535VersionTuple Parser::ParseVersionTuple(SourceRange &Range) {
536 Range = Tok.getLocation();
537
538 if (!Tok.is(tok::numeric_constant)) {
539 Diag(Tok, diag::err_expected_version);
540 SkipUntil(tok::comma, tok::r_paren, true, true, true);
541 return VersionTuple();
542 }
543
544 // Parse the major (and possibly minor and subminor) versions, which
545 // are stored in the numeric constant. We utilize a quirk of the
546 // lexer, which is that it handles something like 1.2.3 as a single
547 // numeric constant, rather than two separate tokens.
Dylan Noblesmithf7ccbad2012-02-05 02:13:05 +0000548 SmallString<512> Buffer;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000549 Buffer.resize(Tok.getLength()+1);
550 const char *ThisTokBegin = &Buffer[0];
551
552 // Get the spelling of the token, which eliminates trigraphs, etc.
553 bool Invalid = false;
554 unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid);
555 if (Invalid)
556 return VersionTuple();
557
558 // Parse the major version.
559 unsigned AfterMajor = 0;
560 unsigned Major = 0;
561 while (AfterMajor < ActualLength && isdigit(ThisTokBegin[AfterMajor])) {
562 Major = Major * 10 + ThisTokBegin[AfterMajor] - '0';
563 ++AfterMajor;
564 }
565
566 if (AfterMajor == 0) {
567 Diag(Tok, diag::err_expected_version);
568 SkipUntil(tok::comma, tok::r_paren, true, true, true);
569 return VersionTuple();
570 }
571
572 if (AfterMajor == ActualLength) {
573 ConsumeToken();
574
575 // We only had a single version component.
576 if (Major == 0) {
577 Diag(Tok, diag::err_zero_version);
578 return VersionTuple();
579 }
580
581 return VersionTuple(Major);
582 }
583
584 if (ThisTokBegin[AfterMajor] != '.' || (AfterMajor + 1 == ActualLength)) {
585 Diag(Tok, diag::err_expected_version);
586 SkipUntil(tok::comma, tok::r_paren, true, true, true);
587 return VersionTuple();
588 }
589
590 // Parse the minor version.
591 unsigned AfterMinor = AfterMajor + 1;
592 unsigned Minor = 0;
593 while (AfterMinor < ActualLength && isdigit(ThisTokBegin[AfterMinor])) {
594 Minor = Minor * 10 + ThisTokBegin[AfterMinor] - '0';
595 ++AfterMinor;
596 }
597
598 if (AfterMinor == ActualLength) {
599 ConsumeToken();
600
601 // We had major.minor.
602 if (Major == 0 && Minor == 0) {
603 Diag(Tok, diag::err_zero_version);
604 return VersionTuple();
605 }
606
607 return VersionTuple(Major, Minor);
608 }
609
610 // If what follows is not a '.', we have a problem.
611 if (ThisTokBegin[AfterMinor] != '.') {
612 Diag(Tok, diag::err_expected_version);
613 SkipUntil(tok::comma, tok::r_paren, true, true, true);
614 return VersionTuple();
615 }
616
617 // Parse the subminor version.
618 unsigned AfterSubminor = AfterMinor + 1;
619 unsigned Subminor = 0;
620 while (AfterSubminor < ActualLength && isdigit(ThisTokBegin[AfterSubminor])) {
621 Subminor = Subminor * 10 + ThisTokBegin[AfterSubminor] - '0';
622 ++AfterSubminor;
623 }
624
625 if (AfterSubminor != ActualLength) {
626 Diag(Tok, diag::err_expected_version);
627 SkipUntil(tok::comma, tok::r_paren, true, true, true);
628 return VersionTuple();
629 }
630 ConsumeToken();
631 return VersionTuple(Major, Minor, Subminor);
632}
633
634/// \brief Parse the contents of the "availability" attribute.
635///
636/// availability-attribute:
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000637/// 'availability' '(' platform ',' version-arg-list, opt-message')'
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000638///
639/// platform:
640/// identifier
641///
642/// version-arg-list:
643/// version-arg
644/// version-arg ',' version-arg-list
645///
646/// version-arg:
647/// 'introduced' '=' version
648/// 'deprecated' '=' version
Douglas Gregor93a70672012-03-11 04:53:21 +0000649/// 'obsoleted' = version
Douglas Gregorb53e4172011-03-26 03:35:55 +0000650/// 'unavailable'
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000651/// opt-message:
652/// 'message' '=' <string>
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000653void Parser::ParseAvailabilityAttribute(IdentifierInfo &Availability,
654 SourceLocation AvailabilityLoc,
655 ParsedAttributes &attrs,
656 SourceLocation *endLoc) {
657 SourceLocation PlatformLoc;
658 IdentifierInfo *Platform = 0;
659
660 enum { Introduced, Deprecated, Obsoleted, Unknown };
661 AvailabilityChange Changes[Unknown];
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000662 ExprResult MessageExpr;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000663
664 // Opening '('.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000665 BalancedDelimiterTracker T(*this, tok::l_paren);
666 if (T.consumeOpen()) {
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000667 Diag(Tok, diag::err_expected_lparen);
668 return;
669 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000670
671 // Parse the platform name,
672 if (Tok.isNot(tok::identifier)) {
673 Diag(Tok, diag::err_availability_expected_platform);
674 SkipUntil(tok::r_paren);
675 return;
676 }
677 Platform = Tok.getIdentifierInfo();
678 PlatformLoc = ConsumeToken();
679
680 // Parse the ',' following the platform name.
681 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "", tok::r_paren))
682 return;
683
684 // If we haven't grabbed the pointers for the identifiers
685 // "introduced", "deprecated", and "obsoleted", do so now.
686 if (!Ident_introduced) {
687 Ident_introduced = PP.getIdentifierInfo("introduced");
688 Ident_deprecated = PP.getIdentifierInfo("deprecated");
689 Ident_obsoleted = PP.getIdentifierInfo("obsoleted");
Douglas Gregorb53e4172011-03-26 03:35:55 +0000690 Ident_unavailable = PP.getIdentifierInfo("unavailable");
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000691 Ident_message = PP.getIdentifierInfo("message");
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000692 }
693
694 // Parse the set of introductions/deprecations/removals.
Douglas Gregorb53e4172011-03-26 03:35:55 +0000695 SourceLocation UnavailableLoc;
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000696 do {
697 if (Tok.isNot(tok::identifier)) {
698 Diag(Tok, diag::err_availability_expected_change);
699 SkipUntil(tok::r_paren);
700 return;
701 }
702 IdentifierInfo *Keyword = Tok.getIdentifierInfo();
703 SourceLocation KeywordLoc = ConsumeToken();
704
Douglas Gregorb53e4172011-03-26 03:35:55 +0000705 if (Keyword == Ident_unavailable) {
706 if (UnavailableLoc.isValid()) {
707 Diag(KeywordLoc, diag::err_availability_redundant)
708 << Keyword << SourceRange(UnavailableLoc);
709 }
710 UnavailableLoc = KeywordLoc;
711
712 if (Tok.isNot(tok::comma))
713 break;
714
715 ConsumeToken();
716 continue;
717 }
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000718
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000719 if (Tok.isNot(tok::equal)) {
720 Diag(Tok, diag::err_expected_equal_after)
721 << Keyword;
722 SkipUntil(tok::r_paren);
723 return;
724 }
725 ConsumeToken();
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000726 if (Keyword == Ident_message) {
727 if (!isTokenStringLiteral()) {
728 Diag(Tok, diag::err_expected_string_literal);
729 SkipUntil(tok::r_paren);
730 return;
731 }
732 MessageExpr = ParseStringLiteralExpression();
733 break;
734 }
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000735
736 SourceRange VersionRange;
737 VersionTuple Version = ParseVersionTuple(VersionRange);
738
739 if (Version.empty()) {
740 SkipUntil(tok::r_paren);
741 return;
742 }
743
744 unsigned Index;
745 if (Keyword == Ident_introduced)
746 Index = Introduced;
747 else if (Keyword == Ident_deprecated)
748 Index = Deprecated;
749 else if (Keyword == Ident_obsoleted)
750 Index = Obsoleted;
751 else
752 Index = Unknown;
753
754 if (Index < Unknown) {
755 if (!Changes[Index].KeywordLoc.isInvalid()) {
756 Diag(KeywordLoc, diag::err_availability_redundant)
757 << Keyword
758 << SourceRange(Changes[Index].KeywordLoc,
759 Changes[Index].VersionRange.getEnd());
760 }
761
762 Changes[Index].KeywordLoc = KeywordLoc;
763 Changes[Index].Version = Version;
764 Changes[Index].VersionRange = VersionRange;
765 } else {
766 Diag(KeywordLoc, diag::err_availability_unknown_change)
767 << Keyword << VersionRange;
768 }
769
770 if (Tok.isNot(tok::comma))
771 break;
772
773 ConsumeToken();
774 } while (true);
775
776 // Closing ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000777 if (T.consumeClose())
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000778 return;
779
780 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000781 *endLoc = T.getCloseLocation();
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000782
Douglas Gregorb53e4172011-03-26 03:35:55 +0000783 // The 'unavailable' availability cannot be combined with any other
784 // availability changes. Make sure that hasn't happened.
785 if (UnavailableLoc.isValid()) {
786 bool Complained = false;
787 for (unsigned Index = Introduced; Index != Unknown; ++Index) {
788 if (Changes[Index].KeywordLoc.isValid()) {
789 if (!Complained) {
790 Diag(UnavailableLoc, diag::warn_availability_and_unavailable)
791 << SourceRange(Changes[Index].KeywordLoc,
792 Changes[Index].VersionRange.getEnd());
793 Complained = true;
794 }
795
796 // Clear out the availability.
797 Changes[Index] = AvailabilityChange();
798 }
799 }
800 }
801
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000802 // Record this attribute
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000803 attrs.addNew(&Availability,
804 SourceRange(AvailabilityLoc, T.getCloseLocation()),
Fariborz Jahanianf96708d2012-01-23 23:38:32 +0000805 0, AvailabilityLoc,
John McCall0b7e6782011-03-24 11:26:52 +0000806 Platform, PlatformLoc,
807 Changes[Introduced],
808 Changes[Deprecated],
Douglas Gregorb53e4172011-03-26 03:35:55 +0000809 Changes[Obsoleted],
Fariborz Jahanian006e42f2011-12-10 00:28:41 +0000810 UnavailableLoc, MessageExpr.take(),
Sean Hunt93f95f22012-06-18 16:13:52 +0000811 AttributeList::AS_GNU);
Douglas Gregor0a0d2b12011-03-23 00:50:03 +0000812}
813
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000814
815// Late Parsed Attributes:
816// See other examples of late parsing in lib/Parse/ParseCXXInlineMethods
817
818void Parser::LateParsedDeclaration::ParseLexedAttributes() {}
819
820void Parser::LateParsedClass::ParseLexedAttributes() {
821 Self->ParseLexedAttributes(*Class);
822}
823
824void Parser::LateParsedAttribute::ParseLexedAttributes() {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000825 Self->ParseLexedAttribute(*this, true, false);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000826}
827
828/// Wrapper class which calls ParseLexedAttribute, after setting up the
829/// scope appropriately.
830void Parser::ParseLexedAttributes(ParsingClass &Class) {
831 // Deal with templates
832 // FIXME: Test cases to make sure this does the right thing for templates.
833 bool HasTemplateScope = !Class.TopLevelClass && Class.TemplateScope;
834 ParseScope ClassTemplateScope(this, Scope::TemplateParamScope,
835 HasTemplateScope);
836 if (HasTemplateScope)
837 Actions.ActOnReenterTemplateScope(getCurScope(), Class.TagOrTemplate);
838
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000839 // Set or update the scope flags.
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000840 bool AlreadyHasClassScope = Class.TopLevelClass;
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000841 unsigned ScopeFlags = Scope::ClassScope|Scope::DeclScope;
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000842 ParseScope ClassScope(this, ScopeFlags, !AlreadyHasClassScope);
843 ParseScopeFlags ClassScopeFlags(this, ScopeFlags, AlreadyHasClassScope);
844
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000845 // Enter the scope of nested classes
846 if (!AlreadyHasClassScope)
847 Actions.ActOnStartDelayedMemberDeclarations(getCurScope(),
848 Class.TagOrTemplate);
Benjamin Kramer268efba2012-05-17 12:01:52 +0000849 if (!Class.LateParsedDeclarations.empty()) {
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000850 // Allow 'this' within late-parsed attributes.
851 Sema::CXXThisScopeRAII ThisScope(Actions, Class.TagOrTemplate,
852 /*TypeQuals=*/0);
853
854 for (unsigned i = 0, ni = Class.LateParsedDeclarations.size(); i < ni; ++i){
855 Class.LateParsedDeclarations[i]->ParseLexedAttributes();
856 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000857 }
Douglas Gregorcefc3af2012-04-16 07:05:22 +0000858
DeLesley Hutchinscf2fa2f2012-04-06 15:10:17 +0000859 if (!AlreadyHasClassScope)
860 Actions.ActOnFinishDelayedMemberDeclarations(getCurScope(),
861 Class.TagOrTemplate);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000862}
863
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000864
865/// \brief Parse all attributes in LAs, and attach them to Decl D.
866void Parser::ParseLexedAttributeList(LateParsedAttrList &LAs, Decl *D,
867 bool EnterScope, bool OnDefinition) {
868 for (unsigned i = 0, ni = LAs.size(); i < ni; ++i) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000869 LAs[i]->addDecl(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000870 ParseLexedAttribute(*LAs[i], EnterScope, OnDefinition);
Benjamin Kramerd306cf72012-04-14 12:44:47 +0000871 delete LAs[i];
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000872 }
873 LAs.clear();
874}
875
876
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000877/// \brief Finish parsing an attribute for which parsing was delayed.
878/// This will be called at the end of parsing a class declaration
879/// for each LateParsedAttribute. We consume the saved tokens and
880/// create an attribute with the arguments filled in. We add this
881/// to the Attribute list for the decl.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000882void Parser::ParseLexedAttribute(LateParsedAttribute &LA,
883 bool EnterScope, bool OnDefinition) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000884 // Save the current token position.
885 SourceLocation OrigLoc = Tok.getLocation();
886
887 // Append the current token at the end of the new token stream so that it
888 // doesn't get lost.
889 LA.Toks.push_back(Tok);
890 PP.EnterTokenStream(LA.Toks.data(), LA.Toks.size(), true, false);
891 // Consume the previously pushed token.
892 ConsumeAnyToken();
893
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +0000894 if (OnDefinition && !IsThreadSafetyAttribute(LA.AttrName.getName())) {
895 Diag(Tok, diag::warn_attribute_on_function_definition)
896 << LA.AttrName.getName();
897 }
898
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000899 ParsedAttributes Attrs(AttrFactory);
900 SourceLocation endLoc;
901
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000902 if (LA.Decls.size() == 1) {
903 Decl *D = LA.Decls[0];
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000904
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000905 // If the Decl is templatized, add template parameters to scope.
906 bool HasTemplateScope = EnterScope && D->isTemplateDecl();
907 ParseScope TempScope(this, Scope::TemplateParamScope, HasTemplateScope);
908 if (HasTemplateScope)
909 Actions.ActOnReenterTemplateScope(Actions.CurScope, D);
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000910
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000911 // If the Decl is on a function, add function parameters to the scope.
912 bool HasFunctionScope = EnterScope && D->isFunctionOrFunctionTemplate();
913 ParseScope FnScope(this, Scope::FnScope|Scope::DeclScope, HasFunctionScope);
914 if (HasFunctionScope)
915 Actions.ActOnReenterFunctionContext(Actions.CurScope, D);
916
917 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
918
919 if (HasFunctionScope) {
920 Actions.ActOnExitFunctionContext();
921 FnScope.Exit(); // Pop scope, and remove Decls from IdResolver
922 }
923 if (HasTemplateScope) {
924 TempScope.Exit();
925 }
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000926 } else if (LA.Decls.size() > 0) {
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000927 // If there are multiple decls, then the decl cannot be within the
928 // function scope.
929 ParseGNUAttributeArgs(&LA.AttrName, LA.AttrNameLoc, Attrs, &endLoc);
DeLesley Hutchins7ec419a2012-03-02 22:29:50 +0000930 } else {
931 Diag(Tok, diag::warn_attribute_no_decl) << LA.AttrName.getName();
Caitlin Sadowskied9d84a2011-09-08 17:42:31 +0000932 }
933
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +0000934 for (unsigned i = 0, ni = LA.Decls.size(); i < ni; ++i) {
935 Actions.ActOnFinishDelayedAttribute(getCurScope(), LA.Decls[i], Attrs);
936 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000937
938 if (Tok.getLocation() != OrigLoc) {
939 // Due to a parsing error, we either went over the cached tokens or
940 // there are still cached tokens left, so we skip the leftover tokens.
941 // Since this is an uncommon situation that should be avoided, use the
942 // expensive isBeforeInTranslationUnit call.
943 if (PP.getSourceManager().isBeforeInTranslationUnit(Tok.getLocation(),
944 OrigLoc))
945 while (Tok.getLocation() != OrigLoc && Tok.isNot(tok::eof))
Douglas Gregord78ef5b2012-03-08 01:00:17 +0000946 ConsumeAnyToken();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000947 }
948}
949
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000950/// \brief Wrapper around a case statement checking if AttrName is
951/// one of the thread safety attributes
952bool Parser::IsThreadSafetyAttribute(llvm::StringRef AttrName){
953 return llvm::StringSwitch<bool>(AttrName)
954 .Case("guarded_by", true)
955 .Case("guarded_var", true)
956 .Case("pt_guarded_by", true)
957 .Case("pt_guarded_var", true)
958 .Case("lockable", true)
959 .Case("scoped_lockable", true)
960 .Case("no_thread_safety_analysis", true)
961 .Case("acquired_after", true)
962 .Case("acquired_before", true)
963 .Case("exclusive_lock_function", true)
964 .Case("shared_lock_function", true)
965 .Case("exclusive_trylock_function", true)
966 .Case("shared_trylock_function", true)
967 .Case("unlock_function", true)
968 .Case("lock_returned", true)
969 .Case("locks_excluded", true)
970 .Case("exclusive_locks_required", true)
971 .Case("shared_locks_required", true)
972 .Default(false);
973}
974
975/// \brief Parse the contents of thread safety attributes. These
976/// should always be parsed as an expression list.
977///
978/// We need to special case the parsing due to the fact that if the first token
979/// of the first argument is an identifier, the main parse loop will store
980/// that token as a "parameter" and the rest of
981/// the arguments will be added to a list of "arguments". However,
982/// subsequent tokens in the first argument are lost. We instead parse each
983/// argument as an expression and add all arguments to the list of "arguments".
984/// In future, we will take advantage of this special case to also
985/// deal with some argument scoping issues here (for example, referring to a
986/// function parameter in the attribute on that function).
987void Parser::ParseThreadSafetyAttribute(IdentifierInfo &AttrName,
988 SourceLocation AttrNameLoc,
989 ParsedAttributes &Attrs,
990 SourceLocation *EndLoc) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000991 assert(Tok.is(tok::l_paren) && "Attribute arg list not starting with '('");
Caitlin Sadowskib51e0312011-08-09 17:59:31 +0000992
Douglas Gregor4a8dfb52011-10-12 16:37:45 +0000993 BalancedDelimiterTracker T(*this, tok::l_paren);
994 T.consumeOpen();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +0000995
996 ExprVector ArgExprs(Actions);
997 bool ArgExprsOk = true;
998
999 // now parse the list of expressions
DeLesley Hutchins4805f152011-12-14 19:36:06 +00001000 while (Tok.isNot(tok::r_paren)) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001001 ExprResult ArgExpr(ParseAssignmentExpression());
1002 if (ArgExpr.isInvalid()) {
1003 ArgExprsOk = false;
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001004 T.consumeClose();
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001005 break;
1006 } else {
1007 ArgExprs.push_back(ArgExpr.release());
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001008 }
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001009 if (Tok.isNot(tok::comma))
1010 break;
1011 ConsumeToken(); // Eat the comma, move to the next argument
1012 }
1013 // Match the ')'.
DeLesley Hutchins23323e02012-01-20 22:50:54 +00001014 if (ArgExprsOk && !T.consumeClose()) {
Caitlin Sadowskieff98fc2011-09-08 17:42:22 +00001015 Attrs.addNew(&AttrName, AttrNameLoc, 0, AttrNameLoc, 0, SourceLocation(),
Sean Hunt93f95f22012-06-18 16:13:52 +00001016 ArgExprs.take(), ArgExprs.size(), AttributeList::AS_GNU);
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001017 }
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001018 if (EndLoc)
1019 *EndLoc = T.getCloseLocation();
Caitlin Sadowskib51e0312011-08-09 17:59:31 +00001020}
1021
Richard Smith6ee326a2012-04-10 01:32:12 +00001022/// DiagnoseProhibitedCXX11Attribute - We have found the opening square brackets
1023/// of a C++11 attribute-specifier in a location where an attribute is not
1024/// permitted. By C++11 [dcl.attr.grammar]p6, this is ill-formed. Diagnose this
1025/// situation.
1026///
1027/// \return \c true if we skipped an attribute-like chunk of tokens, \c false if
1028/// this doesn't appear to actually be an attribute-specifier, and the caller
1029/// should try to parse it.
1030bool Parser::DiagnoseProhibitedCXX11Attribute() {
1031 assert(Tok.is(tok::l_square) && NextToken().is(tok::l_square));
1032
1033 switch (isCXX11AttributeSpecifier(/*Disambiguate*/true)) {
1034 case CAK_NotAttributeSpecifier:
1035 // No diagnostic: we're in Obj-C++11 and this is not actually an attribute.
1036 return false;
1037
1038 case CAK_InvalidAttributeSpecifier:
1039 Diag(Tok.getLocation(), diag::err_l_square_l_square_not_attribute);
1040 return false;
1041
1042 case CAK_AttributeSpecifier:
1043 // Parse and discard the attributes.
1044 SourceLocation BeginLoc = ConsumeBracket();
1045 ConsumeBracket();
1046 SkipUntil(tok::r_square, /*StopAtSemi*/ false);
1047 assert(Tok.is(tok::r_square) && "isCXX11AttributeSpecifier lied");
1048 SourceLocation EndLoc = ConsumeBracket();
1049 Diag(BeginLoc, diag::err_attributes_not_allowed)
1050 << SourceRange(BeginLoc, EndLoc);
1051 return true;
1052 }
Chandler Carruth2c6dbd72012-04-10 16:03:08 +00001053 llvm_unreachable("All cases handled above.");
Richard Smith6ee326a2012-04-10 01:32:12 +00001054}
1055
John McCall7f040a92010-12-24 02:08:15 +00001056void Parser::DiagnoseProhibitedAttributes(ParsedAttributesWithRange &attrs) {
1057 Diag(attrs.Range.getBegin(), diag::err_attributes_not_allowed)
1058 << attrs.Range;
Dawn Perchik52fc3142010-09-03 01:29:35 +00001059}
1060
Reid Spencer5f016e22007-07-11 17:01:13 +00001061/// ParseDeclaration - Parse a full 'declaration', which consists of
1062/// declaration-specifiers, some number of declarators, and a semicolon.
Chris Lattner97144fc2009-04-02 04:16:50 +00001063/// 'Context' should be a Declarator::TheContext value. This returns the
1064/// location of the semicolon in DeclEnd.
Chris Lattner8f08cb72007-08-25 06:57:03 +00001065///
1066/// declaration: [C99 6.7]
1067/// block-declaration ->
1068/// simple-declaration
1069/// others [FIXME]
Douglas Gregoradcac882008-12-01 23:54:00 +00001070/// [C++] template-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001071/// [C++] namespace-definition
Douglas Gregorf780abc2008-12-30 03:27:21 +00001072/// [C++] using-directive
Douglas Gregord7f37bf2009-06-22 23:06:13 +00001073/// [C++] using-declaration
Richard Smith534986f2012-04-14 00:33:13 +00001074/// [C++11/C11] static_assert-declaration
Chris Lattner8f08cb72007-08-25 06:57:03 +00001075/// others... [FIXME]
1076///
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001077Parser::DeclGroupPtrTy Parser::ParseDeclaration(StmtVector &Stmts,
1078 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001079 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001080 ParsedAttributesWithRange &attrs) {
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001081 ParenBraceBracketBalancer BalancerRAIIObj(*this);
Fariborz Jahaniane8cff362011-08-30 17:10:52 +00001082 // Must temporarily exit the objective-c container scope for
1083 // parsing c none objective-c decls.
1084 ObjCDeclContextSwitch ObjCDC(*this);
Argyrios Kyrtzidis36d36802010-06-17 10:52:18 +00001085
John McCalld226f652010-08-21 09:40:31 +00001086 Decl *SingleDecl = 0;
Richard Smithc89edf52011-07-01 19:46:12 +00001087 Decl *OwnedType = 0;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001088 switch (Tok.getKind()) {
Douglas Gregoradcac882008-12-01 23:54:00 +00001089 case tok::kw_template:
Douglas Gregor1426e532009-05-12 21:31:51 +00001090 case tok::kw_export:
John McCall7f040a92010-12-24 02:08:15 +00001091 ProhibitAttributes(attrs);
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001092 SingleDecl = ParseDeclarationStartingWithTemplate(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001093 break;
Sebastian Redld078e642010-08-27 23:12:46 +00001094 case tok::kw_inline:
Sebastian Redl88e64ca2010-08-31 00:36:45 +00001095 // Could be the start of an inline namespace. Allowed as an ext in C++03.
David Blaikie4e4d0842012-03-11 07:00:24 +00001096 if (getLangOpts().CPlusPlus && NextToken().is(tok::kw_namespace)) {
John McCall7f040a92010-12-24 02:08:15 +00001097 ProhibitAttributes(attrs);
Sebastian Redld078e642010-08-27 23:12:46 +00001098 SourceLocation InlineLoc = ConsumeToken();
1099 SingleDecl = ParseNamespace(Context, DeclEnd, InlineLoc);
1100 break;
1101 }
John McCall7f040a92010-12-24 02:08:15 +00001102 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs,
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001103 true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001104 case tok::kw_namespace:
John McCall7f040a92010-12-24 02:08:15 +00001105 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001106 SingleDecl = ParseNamespace(Context, DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001107 break;
Douglas Gregorf780abc2008-12-30 03:27:21 +00001108 case tok::kw_using:
John McCall78b81052010-11-10 02:40:36 +00001109 SingleDecl = ParseUsingDirectiveOrDeclaration(Context, ParsedTemplateInfo(),
Richard Smithc89edf52011-07-01 19:46:12 +00001110 DeclEnd, attrs, &OwnedType);
Chris Lattner682bf922009-03-29 16:50:03 +00001111 break;
Anders Carlsson511d7ab2009-03-11 16:27:10 +00001112 case tok::kw_static_assert:
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00001113 case tok::kw__Static_assert:
John McCall7f040a92010-12-24 02:08:15 +00001114 ProhibitAttributes(attrs);
Chris Lattner97144fc2009-04-02 04:16:50 +00001115 SingleDecl = ParseStaticAssertDeclaration(DeclEnd);
Chris Lattner682bf922009-03-29 16:50:03 +00001116 break;
Chris Lattner8f08cb72007-08-25 06:57:03 +00001117 default:
John McCall7f040a92010-12-24 02:08:15 +00001118 return ParseSimpleDeclaration(Stmts, Context, DeclEnd, attrs, true);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001119 }
Sean Huntbbd37c62009-11-21 08:43:09 +00001120
Chris Lattner682bf922009-03-29 16:50:03 +00001121 // This routine returns a DeclGroup, if the thing we parsed only contains a
Richard Smithc89edf52011-07-01 19:46:12 +00001122 // single decl, convert it now. Alias declarations can also declare a type;
1123 // include that too if it is present.
1124 return Actions.ConvertDeclToDeclGroup(SingleDecl, OwnedType);
Chris Lattner8f08cb72007-08-25 06:57:03 +00001125}
1126
1127/// simple-declaration: [C99 6.7: declaration] [C++ 7p1: dcl.dcl]
1128/// declaration-specifiers init-declarator-list[opt] ';'
Sean Hunt2edf0a22012-06-23 05:07:58 +00001129/// [C++11] attribute-specifier-seq decl-specifier-seq[opt]
1130/// init-declarator-list ';'
Chris Lattner8f08cb72007-08-25 06:57:03 +00001131///[C90/C++]init-declarator-list ';' [TODO]
1132/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001133///
Sean Hunt2edf0a22012-06-23 05:07:58 +00001134/// for-range-declaration: [C++11 6.5p1: stmt.ranged]
Richard Smithad762fc2011-04-14 22:09:26 +00001135/// attribute-specifier-seq[opt] type-specifier-seq declarator
1136///
Chris Lattnercd147752009-03-29 17:27:48 +00001137/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001138/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001139///
1140/// If FRI is non-null, we might be parsing a for-range-declaration instead
1141/// of a simple-declaration. If we find that we are, we also parse the
1142/// for-range-initializer, and place it here.
Sean Hunt2edf0a22012-06-23 05:07:58 +00001143Parser::DeclGroupPtrTy
1144Parser::ParseSimpleDeclaration(StmtVector &Stmts, unsigned Context,
1145 SourceLocation &DeclEnd,
1146 ParsedAttributesWithRange &attrs,
1147 bool RequireSemi, ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001149 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001150 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001151
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001152 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001153 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001154
Reid Spencer5f016e22007-07-11 17:01:13 +00001155 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1156 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001157 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001158 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001159 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001160 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001161 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001162 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001163 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001164 }
Douglas Gregor312eadb2011-04-24 05:37:28 +00001165
1166 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001167}
Mike Stump1eb44332009-09-09 15:08:12 +00001168
Richard Smith0706df42011-10-19 21:33:05 +00001169/// Returns true if this might be the start of a declarator, or a common typo
1170/// for a declarator.
1171bool Parser::MightBeDeclarator(unsigned Context) {
1172 switch (Tok.getKind()) {
1173 case tok::annot_cxxscope:
1174 case tok::annot_template_id:
1175 case tok::caret:
1176 case tok::code_completion:
1177 case tok::coloncolon:
1178 case tok::ellipsis:
1179 case tok::kw___attribute:
1180 case tok::kw_operator:
1181 case tok::l_paren:
1182 case tok::star:
1183 return true;
1184
1185 case tok::amp:
1186 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001187 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001188
Richard Smith1c94c162012-01-09 22:31:44 +00001189 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001190 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001191 NextToken().is(tok::l_square);
1192
1193 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001194 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001195
Richard Smith0706df42011-10-19 21:33:05 +00001196 case tok::identifier:
1197 switch (NextToken().getKind()) {
1198 case tok::code_completion:
1199 case tok::coloncolon:
1200 case tok::comma:
1201 case tok::equal:
1202 case tok::equalequal: // Might be a typo for '='.
1203 case tok::kw_alignas:
1204 case tok::kw_asm:
1205 case tok::kw___attribute:
1206 case tok::l_brace:
1207 case tok::l_paren:
1208 case tok::l_square:
1209 case tok::less:
1210 case tok::r_brace:
1211 case tok::r_paren:
1212 case tok::r_square:
1213 case tok::semi:
1214 return true;
1215
1216 case tok::colon:
1217 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001218 // and in block scope it's probably a label. Inside a class definition,
1219 // this is a bit-field.
1220 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001221 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001222
1223 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001224 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001225
1226 default:
1227 return false;
1228 }
1229
1230 default:
1231 return false;
1232 }
1233}
1234
Richard Smith994d73f2012-04-11 20:59:20 +00001235/// Skip until we reach something which seems like a sensible place to pick
1236/// up parsing after a malformed declaration. This will sometimes stop sooner
1237/// than SkipUntil(tok::r_brace) would, but will never stop later.
1238void Parser::SkipMalformedDecl() {
1239 while (true) {
1240 switch (Tok.getKind()) {
1241 case tok::l_brace:
1242 // Skip until matching }, then stop. We've probably skipped over
1243 // a malformed class or function definition or similar.
1244 ConsumeBrace();
1245 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1246 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1247 // This declaration isn't over yet. Keep skipping.
1248 continue;
1249 }
1250 if (Tok.is(tok::semi))
1251 ConsumeToken();
1252 return;
1253
1254 case tok::l_square:
1255 ConsumeBracket();
1256 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1257 continue;
1258
1259 case tok::l_paren:
1260 ConsumeParen();
1261 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1262 continue;
1263
1264 case tok::r_brace:
1265 return;
1266
1267 case tok::semi:
1268 ConsumeToken();
1269 return;
1270
1271 case tok::kw_inline:
1272 // 'inline namespace' at the start of a line is almost certainly
1273 // a good place to pick back up parsing.
1274 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace))
1275 return;
1276 break;
1277
1278 case tok::kw_namespace:
1279 // 'namespace' at the start of a line is almost certainly a good
1280 // place to pick back up parsing.
1281 if (Tok.isAtStartOfLine())
1282 return;
1283 break;
1284
1285 case tok::eof:
1286 return;
1287
1288 default:
1289 break;
1290 }
1291
1292 ConsumeAnyToken();
1293 }
1294}
1295
John McCalld8ac0572009-11-03 19:26:08 +00001296/// ParseDeclGroup - Having concluded that this is either a function
1297/// definition or a group of object declarations, actually parse the
1298/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001299Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1300 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001301 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001302 SourceLocation *DeclEnd,
1303 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001304 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001305 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001306 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001307
John McCalld8ac0572009-11-03 19:26:08 +00001308 // Bail out if the first declarator didn't seem well-formed.
1309 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001310 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001311 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001312 }
Mike Stump1eb44332009-09-09 15:08:12 +00001313
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001314 // Save late-parsed attributes for now; they need to be parsed in the
1315 // appropriate function scope after the function Decl has been constructed.
1316 LateParsedAttrList LateParsedAttrs;
1317 if (D.isFunctionDeclarator())
1318 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1319
Chris Lattnerc82daef2010-07-11 22:24:20 +00001320 // Check to see if we have a function *definition* which must have a body.
1321 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1322 // Look at the next token to make sure that this isn't a function
1323 // declaration. We have to check this because __attribute__ might be the
1324 // start of a function definition in GCC-extended K&R C.
1325 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001326
Chris Lattner004659a2010-07-11 22:42:07 +00001327 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001328 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1329 Diag(Tok, diag::err_function_declared_typedef);
1330
1331 // Recover by treating the 'typedef' as spurious.
1332 DS.ClearStorageClassSpecs();
1333 }
1334
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001335 Decl *TheDecl =
1336 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001337 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001338 }
1339
1340 if (isDeclarationSpecifier()) {
1341 // If there is an invalid declaration specifier right after the function
1342 // prototype, then we must be in a missing semicolon case where this isn't
1343 // actually a body. Just fall through into the code that handles it as a
1344 // prototype, and let the top-level code handle the erroneous declspec
1345 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001346 } else {
1347 Diag(Tok, diag::err_expected_fn_body);
1348 SkipUntil(tok::semi);
1349 return DeclGroupPtrTy();
1350 }
1351 }
1352
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001353 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001354 return DeclGroupPtrTy();
1355
1356 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1357 // must parse and analyze the for-range-initializer before the declaration is
1358 // analyzed.
1359 if (FRI && Tok.is(tok::colon)) {
1360 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001361 if (Tok.is(tok::l_brace))
1362 FRI->RangeExpr = ParseBraceInitializer();
1363 else
1364 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001365 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1366 Actions.ActOnCXXForRangeDecl(ThisDecl);
1367 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001368 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001369 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1370 }
1371
Chris Lattner5f9e2722011-07-23 10:55:15 +00001372 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001373 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001374 if (LateParsedAttrs.size() > 0)
1375 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001376 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001377 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001378 DeclsInGroup.push_back(FirstDecl);
1379
Richard Smith0706df42011-10-19 21:33:05 +00001380 bool ExpectSemi = Context != Declarator::ForContext;
1381
John McCalld8ac0572009-11-03 19:26:08 +00001382 // If we don't have a comma, it is either the end of the list (a ';') or an
1383 // error, bail out.
1384 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001385 SourceLocation CommaLoc = ConsumeToken();
1386
1387 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1388 // This comma was followed by a line-break and something which can't be
1389 // the start of a declarator. The comma was probably a typo for a
1390 // semicolon.
1391 Diag(CommaLoc, diag::err_expected_semi_declaration)
1392 << FixItHint::CreateReplacement(CommaLoc, ";");
1393 ExpectSemi = false;
1394 break;
1395 }
John McCalld8ac0572009-11-03 19:26:08 +00001396
1397 // Parse the next declarator.
1398 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001399 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001400
1401 // Accept attributes in an init-declarator. In the first declarator in a
1402 // declaration, these would be part of the declspec. In subsequent
1403 // declarators, they become part of the declarator itself, so that they
1404 // don't apply to declarators after *this* one. Examples:
1405 // short __attribute__((common)) var; -> declspec
1406 // short var __attribute__((common)); -> declarator
1407 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001408 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001409
1410 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001411 if (!D.isInvalidType()) {
1412 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1413 D.complete(ThisDecl);
1414 if (ThisDecl)
1415 DeclsInGroup.push_back(ThisDecl);
1416 }
John McCalld8ac0572009-11-03 19:26:08 +00001417 }
1418
1419 if (DeclEnd)
1420 *DeclEnd = Tok.getLocation();
1421
Richard Smith0706df42011-10-19 21:33:05 +00001422 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001423 ExpectAndConsumeSemi(Context == Declarator::FileContext
1424 ? diag::err_invalid_token_after_toplevel_declarator
1425 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001426 // Okay, there was no semicolon and one was expected. If we see a
1427 // declaration specifier, just assume it was missing and continue parsing.
1428 // Otherwise things are very confused and we skip to recover.
1429 if (!isDeclarationSpecifier()) {
1430 SkipUntil(tok::r_brace, true, true);
1431 if (Tok.is(tok::semi))
1432 ConsumeToken();
1433 }
John McCalld8ac0572009-11-03 19:26:08 +00001434 }
1435
Douglas Gregor23c94db2010-07-02 17:43:08 +00001436 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001437 DeclsInGroup.data(),
1438 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001439}
1440
Richard Smithad762fc2011-04-14 22:09:26 +00001441/// Parse an optional simple-asm-expr and attributes, and attach them to a
1442/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001443bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001444 // If a simple-asm-expr is present, parse it.
1445 if (Tok.is(tok::kw_asm)) {
1446 SourceLocation Loc;
1447 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1448 if (AsmLabel.isInvalid()) {
1449 SkipUntil(tok::semi, true, true);
1450 return true;
1451 }
1452
1453 D.setAsmLabel(AsmLabel.release());
1454 D.SetRangeEnd(Loc);
1455 }
1456
1457 MaybeParseGNUAttributes(D);
1458 return false;
1459}
1460
Douglas Gregor1426e532009-05-12 21:31:51 +00001461/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1462/// declarator'. This method parses the remainder of the declaration
1463/// (including any attributes or initializer, among other things) and
1464/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001465///
Reid Spencer5f016e22007-07-11 17:01:13 +00001466/// init-declarator: [C99 6.7]
1467/// declarator
1468/// declarator '=' initializer
1469/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1470/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001471/// [C++] declarator initializer[opt]
1472///
1473/// [C++] initializer:
1474/// [C++] '=' initializer-clause
1475/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001476/// [C++0x] '=' 'default' [TODO]
1477/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001478/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001479///
1480/// According to the standard grammar, =default and =delete are function
1481/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001482///
John McCalld226f652010-08-21 09:40:31 +00001483Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001484 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001485 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001486 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Richard Smithad762fc2011-04-14 22:09:26 +00001488 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1489}
Mike Stump1eb44332009-09-09 15:08:12 +00001490
Richard Smithad762fc2011-04-14 22:09:26 +00001491Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1492 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001493 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001494 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001495 switch (TemplateInfo.Kind) {
1496 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001497 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001498 break;
1499
1500 case ParsedTemplateInfo::Template:
1501 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001502 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001503 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001504 TemplateInfo.TemplateParams->data(),
1505 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001506 D);
1507 break;
1508
1509 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001510 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001511 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001512 TemplateInfo.ExternLoc,
1513 TemplateInfo.TemplateLoc,
1514 D);
1515 if (ThisRes.isInvalid()) {
1516 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001517 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001518 }
1519
1520 ThisDecl = ThisRes.get();
1521 break;
1522 }
1523 }
Mike Stump1eb44332009-09-09 15:08:12 +00001524
Richard Smith34b41d92011-02-20 03:19:35 +00001525 bool TypeContainsAuto =
1526 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1527
Douglas Gregor1426e532009-05-12 21:31:51 +00001528 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001529 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001530 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001531 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001532 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001533 if (D.isFunctionDeclarator())
1534 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1535 << 1 /* delete */;
1536 else
1537 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001538 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001539 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001540 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1541 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001542 else
1543 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001544 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001545 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001546 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001547 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001548 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001549
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001550 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001551 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001552 cutOffParsing();
1553 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001554 }
1555
John McCall60d7b3a2010-08-24 06:29:42 +00001556 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001557
David Blaikie4e4d0842012-03-11 07:00:24 +00001558 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001559 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001560 ExitScope();
1561 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001562
Douglas Gregor1426e532009-05-12 21:31:51 +00001563 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001564 SkipUntil(tok::comma, true, true);
1565 Actions.ActOnInitializerError(ThisDecl);
1566 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001567 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1568 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001569 }
1570 } else if (Tok.is(tok::l_paren)) {
1571 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001572 BalancedDelimiterTracker T(*this, tok::l_paren);
1573 T.consumeOpen();
1574
Douglas Gregor1426e532009-05-12 21:31:51 +00001575 ExprVector Exprs(Actions);
1576 CommaLocsTy CommaLocs;
1577
David Blaikie4e4d0842012-03-11 07:00:24 +00001578 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001579 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001580 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001581 }
1582
Douglas Gregor1426e532009-05-12 21:31:51 +00001583 if (ParseExpressionList(Exprs, CommaLocs)) {
1584 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001585
David Blaikie4e4d0842012-03-11 07:00:24 +00001586 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001587 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001588 ExitScope();
1589 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001590 } else {
1591 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001592 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001593
1594 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1595 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001596
David Blaikie4e4d0842012-03-11 07:00:24 +00001597 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001598 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001599 ExitScope();
1600 }
1601
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001602 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1603 T.getCloseLocation(),
1604 move_arg(Exprs));
1605 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1606 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001607 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001608 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001609 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001610 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1611
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001612 if (D.getCXXScopeSpec().isSet()) {
1613 EnterScope(0);
1614 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1615 }
1616
1617 ExprResult Init(ParseBraceInitializer());
1618
1619 if (D.getCXXScopeSpec().isSet()) {
1620 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1621 ExitScope();
1622 }
1623
1624 if (Init.isInvalid()) {
1625 Actions.ActOnInitializerError(ThisDecl);
1626 } else
1627 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1628 /*DirectInit=*/true, TypeContainsAuto);
1629
Douglas Gregor1426e532009-05-12 21:31:51 +00001630 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001631 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001632 }
1633
Richard Smith483b9f32011-02-21 20:05:19 +00001634 Actions.FinalizeDeclaration(ThisDecl);
1635
Douglas Gregor1426e532009-05-12 21:31:51 +00001636 return ThisDecl;
1637}
1638
Reid Spencer5f016e22007-07-11 17:01:13 +00001639/// ParseSpecifierQualifierList
1640/// specifier-qualifier-list:
1641/// type-specifier specifier-qualifier-list[opt]
1642/// type-qualifier specifier-qualifier-list[opt]
1643/// [GNU] attributes specifier-qualifier-list[opt]
1644///
Richard Smith69730c12012-03-12 07:56:15 +00001645void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1646 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001647 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1648 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001649 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001650 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001651
Reid Spencer5f016e22007-07-11 17:01:13 +00001652 // Validate declspec for type-name.
1653 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001654 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1655 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001656 Diag(Tok, diag::err_expected_type);
1657 DS.SetTypeSpecError();
1658 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1659 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001660 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001661 if (!DS.hasTypeSpecifier())
1662 DS.SetTypeSpecError();
1663 }
Mike Stump1eb44332009-09-09 15:08:12 +00001664
Reid Spencer5f016e22007-07-11 17:01:13 +00001665 // Issue diagnostic and remove storage class if present.
1666 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1667 if (DS.getStorageClassSpecLoc().isValid())
1668 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1669 else
1670 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1671 DS.ClearStorageClassSpecs();
1672 }
Mike Stump1eb44332009-09-09 15:08:12 +00001673
Reid Spencer5f016e22007-07-11 17:01:13 +00001674 // Issue diagnostic and remove function specfier if present.
1675 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001676 if (DS.isInlineSpecified())
1677 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1678 if (DS.isVirtualSpecified())
1679 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1680 if (DS.isExplicitSpecified())
1681 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001682 DS.ClearFunctionSpecs();
1683 }
Richard Smith69730c12012-03-12 07:56:15 +00001684
1685 // Issue diagnostic and remove constexpr specfier if present.
1686 if (DS.isConstexprSpecified()) {
1687 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1688 DS.ClearConstexprSpec();
1689 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001690}
1691
Chris Lattnerc199ab32009-04-12 20:42:31 +00001692/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1693/// specified token is valid after the identifier in a declarator which
1694/// immediately follows the declspec. For example, these things are valid:
1695///
1696/// int x [ 4]; // direct-declarator
1697/// int x ( int y); // direct-declarator
1698/// int(int x ) // direct-declarator
1699/// int x ; // simple-declaration
1700/// int x = 17; // init-declarator-list
1701/// int x , y; // init-declarator-list
1702/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001703/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001704/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001705///
1706/// This is not, because 'x' does not immediately follow the declspec (though
1707/// ')' happens to be valid anyway).
1708/// int (x)
1709///
1710static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1711 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1712 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001713 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001714}
1715
Chris Lattnere40c2952009-04-14 21:34:55 +00001716
1717/// ParseImplicitInt - This method is called when we have an non-typename
1718/// identifier in a declspec (which normally terminates the decl spec) when
1719/// the declspec has no type specifier. In this case, the declspec is either
1720/// malformed or is "implicit int" (in K&R and C89).
1721///
1722/// This method handles diagnosing this prettily and returns false if the
1723/// declspec is done being processed. If it recovers and thinks there may be
1724/// other pieces of declspec after it, it returns true.
1725///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001726bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001727 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001728 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001729 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001730
Chris Lattnere40c2952009-04-14 21:34:55 +00001731 SourceLocation Loc = Tok.getLocation();
1732 // If we see an identifier that is not a type name, we normally would
1733 // parse it as the identifer being declared. However, when a typename
1734 // is typo'd or the definition is not included, this will incorrectly
1735 // parse the typename as the identifier name and fall over misparsing
1736 // later parts of the diagnostic.
1737 //
1738 // As such, we try to do some look-ahead in cases where this would
1739 // otherwise be an "implicit-int" case to see if this is invalid. For
1740 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1741 // an identifier with implicit int, we'd get a parse error because the
1742 // next token is obviously invalid for a type. Parse these as a case
1743 // with an invalid type specifier.
1744 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001745
Chris Lattnere40c2952009-04-14 21:34:55 +00001746 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001747 // error, do lookahead to try to do better recovery. This never applies
1748 // within a type specifier. Outside of C++, we allow this even if the
1749 // language doesn't "officially" support implicit int -- we support
1750 // implicit int as an extension in C99 and C11. Allegedly, MS also
1751 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001752 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001753 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001754 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001755 // If this token is valid for implicit int, e.g. "static x = 4", then
1756 // we just avoid eating the identifier, so it will be parsed as the
1757 // identifier in the declarator.
1758 return false;
1759 }
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Richard Smith827adaf2012-05-15 21:01:51 +00001761 if (getLangOpts().CPlusPlus &&
1762 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1763 // Don't require a type specifier if we have the 'auto' storage class
1764 // specifier in C++98 -- we'll promote it to a type specifier.
1765 return false;
1766 }
1767
Chris Lattnere40c2952009-04-14 21:34:55 +00001768 // Otherwise, if we don't consume this token, we are going to emit an
1769 // error anyway. Try to recover from various common problems. Check
1770 // to see if this was a reference to a tag name without a tag specified.
1771 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001772 //
1773 // C++ doesn't need this, and isTagName doesn't take SS.
1774 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001775 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001776 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Douglas Gregor23c94db2010-07-02 17:43:08 +00001778 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001779 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001780 case DeclSpec::TST_enum:
1781 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1782 case DeclSpec::TST_union:
1783 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1784 case DeclSpec::TST_struct:
1785 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1786 case DeclSpec::TST_class:
1787 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001788 }
Mike Stump1eb44332009-09-09 15:08:12 +00001789
Chris Lattnerf4382f52009-04-14 22:17:06 +00001790 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001791 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1792 LookupResult R(Actions, TokenName, SourceLocation(),
1793 Sema::LookupOrdinaryName);
1794
Chris Lattnerf4382f52009-04-14 22:17:06 +00001795 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001796 << TokenName << TagName << getLangOpts().CPlusPlus
1797 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1798
1799 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1800 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1801 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001802 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001803 << TokenName << TagName;
1804 }
Mike Stump1eb44332009-09-09 15:08:12 +00001805
Chris Lattnerf4382f52009-04-14 22:17:06 +00001806 // Parse this as a tag as if the missing tag were present.
1807 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001808 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001809 else
Richard Smith69730c12012-03-12 07:56:15 +00001810 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1811 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001812 return true;
1813 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001814 }
Mike Stump1eb44332009-09-09 15:08:12 +00001815
Richard Smith8f0a7e72012-05-15 21:29:55 +00001816 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001817 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001818 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1819 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001820 // Look ahead to the next token to try to figure out what this declaration
1821 // was supposed to be.
1822 switch (NextToken().getKind()) {
1823 case tok::comma:
1824 case tok::equal:
1825 case tok::kw_asm:
1826 case tok::l_brace:
1827 case tok::l_square:
1828 case tok::semi:
1829 // This looks like a variable declaration. The type is probably missing.
1830 // We're done parsing decl-specifiers.
1831 return false;
1832
1833 case tok::l_paren: {
1834 // static x(4); // 'x' is not a type
1835 // x(int n); // 'x' is not a type
1836 // x (*p)[]; // 'x' is a type
1837 //
1838 // Since we're in an error case (or the rare 'implicit int in C++' MS
1839 // extension), we can afford to perform a tentative parse to determine
1840 // which case we're in.
1841 TentativeParsingAction PA(*this);
1842 ConsumeToken();
1843 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1844 PA.Revert();
1845 if (TPR == TPResult::False())
1846 return false;
1847 // The identifier is followed by a parenthesized declarator.
1848 // It's supposed to be a type.
1849 break;
1850 }
1851
1852 default:
1853 // This is probably supposed to be a type. This includes cases like:
1854 // int f(itn);
1855 // struct S { unsinged : 4; };
1856 break;
1857 }
1858 }
1859
Douglas Gregora786fdb2009-10-13 23:27:22 +00001860 // This is almost certainly an invalid type name. Let the action emit a
1861 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001862 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001863 IdentifierInfo *II = Tok.getIdentifierInfo();
1864 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001865 // The action emitted a diagnostic, so we don't have to.
1866 if (T) {
1867 // The action has suggested that the type T could be used. Set that as
1868 // the type in the declaration specifiers, consume the would-be type
1869 // name token, and we're done.
1870 const char *PrevSpec;
1871 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001872 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001873 DS.SetRangeEnd(Tok.getLocation());
1874 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001875 // There may be other declaration specifiers after this.
1876 return true;
1877 } else if (II != Tok.getIdentifierInfo()) {
1878 // If no type was suggested, the correction is to a keyword
1879 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00001880 // There may be other declaration specifiers after this.
1881 return true;
1882 }
1883
1884 // Fall through; the action had no suggestion for us.
1885 } else {
1886 // The action did not emit a diagnostic, so emit one now.
1887 SourceRange R;
1888 if (SS) R = SS->getRange();
1889 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1890 }
Mike Stump1eb44332009-09-09 15:08:12 +00001891
Douglas Gregora786fdb2009-10-13 23:27:22 +00001892 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001893 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001894 DS.SetRangeEnd(Tok.getLocation());
1895 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001896
Chris Lattnere40c2952009-04-14 21:34:55 +00001897 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1898 // avoid rippling error messages on subsequent uses of the same type,
1899 // could be useful if #include was forgotten.
1900 return false;
1901}
1902
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001903/// \brief Determine the declaration specifier context from the declarator
1904/// context.
1905///
1906/// \param Context the declarator context, which is one of the
1907/// Declarator::TheContext enumerator values.
1908Parser::DeclSpecContext
1909Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1910 if (Context == Declarator::MemberContext)
1911 return DSC_class;
1912 if (Context == Declarator::FileContext)
1913 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001914 if (Context == Declarator::TrailingReturnContext)
1915 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001916 return DSC_normal;
1917}
1918
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001919/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1920///
1921/// FIXME: Simply returns an alignof() expression if the argument is a
1922/// type. Ideally, the type should be propagated directly into Sema.
1923///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001924/// [C11] type-id
1925/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001926/// [C++0x] type-id ...[opt]
1927/// [C++0x] assignment-expression ...[opt]
1928ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1929 SourceLocation &EllipsisLoc) {
1930 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001931 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001932 SourceLocation TypeLoc = Tok.getLocation();
1933 ParsedType Ty = ParseTypeName().get();
1934 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001935 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1936 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001937 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001938 ER = ParseConstantExpression();
1939
David Blaikie4e4d0842012-03-11 07:00:24 +00001940 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001941 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001942
1943 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001944}
1945
1946/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1947/// attribute to Attrs.
1948///
1949/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001950/// [C11] '_Alignas' '(' type-id ')'
1951/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001952/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1953/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001954void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1955 SourceLocation *endLoc) {
1956 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1957 "Not an alignment-specifier!");
1958
1959 SourceLocation KWLoc = Tok.getLocation();
1960 ConsumeToken();
1961
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001962 BalancedDelimiterTracker T(*this, tok::l_paren);
1963 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001964 return;
1965
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001966 SourceLocation EllipsisLoc;
1967 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001968 if (ArgExpr.isInvalid()) {
1969 SkipUntil(tok::r_paren);
1970 return;
1971 }
1972
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001973 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001974 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001975 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001976
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001977 // FIXME: Handle pack-expansions here.
1978 if (EllipsisLoc.isValid()) {
1979 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1980 return;
1981 }
1982
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001983 ExprVector ArgExprs(Actions);
1984 ArgExprs.push_back(ArgExpr.release());
Sean Hunt8e083e72012-06-19 23:57:03 +00001985 // FIXME: This should not be GNU, but we since the attribute used is
1986 // based on the spelling, and there is no true spelling for
1987 // C++11 attributes, this isn't accepted.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001988 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +00001989 0, T.getOpenLocation(), ArgExprs.take(), 1,
Sean Hunt8e083e72012-06-19 23:57:03 +00001990 AttributeList::AS_GNU);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001991}
1992
Reid Spencer5f016e22007-07-11 17:01:13 +00001993/// ParseDeclarationSpecifiers
1994/// declaration-specifiers: [C99 6.7]
1995/// storage-class-specifier declaration-specifiers[opt]
1996/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001997/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001998/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001999/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00002000/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002001///
2002/// storage-class-specifier: [C99 6.7.1]
2003/// 'typedef'
2004/// 'extern'
2005/// 'static'
2006/// 'auto'
2007/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002008/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002009/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002010/// function-specifier: [C99 6.7.4]
2011/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002012/// [C++] 'virtual'
2013/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002014/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002015/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002016/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002017
Reid Spencer5f016e22007-07-11 17:01:13 +00002018///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002019void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002020 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002021 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002022 DeclSpecContext DSContext,
2023 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002024 if (DS.getSourceRange().isInvalid()) {
2025 DS.SetRangeStart(Tok.getLocation());
2026 DS.SetRangeEnd(Tok.getLocation());
2027 }
2028
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002029 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Sean Hunt2edf0a22012-06-23 05:07:58 +00002030 bool AttrsLastTime = false;
2031 ParsedAttributesWithRange attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002032 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002033 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002034 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002035 unsigned DiagID = 0;
2036
Reid Spencer5f016e22007-07-11 17:01:13 +00002037 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002038
Reid Spencer5f016e22007-07-11 17:01:13 +00002039 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002040 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002041 DoneWithDeclSpec:
Sean Hunt2edf0a22012-06-23 05:07:58 +00002042 if (!AttrsLastTime)
2043 ProhibitAttributes(attrs);
2044 else
2045 DS.takeAttributesFrom(attrs);
Peter Collingbournef1907682011-09-29 18:03:57 +00002046
Reid Spencer5f016e22007-07-11 17:01:13 +00002047 // If this is not a declaration specifier token, we're done reading decl
2048 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002049 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002050 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002051
Sean Hunt2edf0a22012-06-23 05:07:58 +00002052 case tok::l_square:
2053 case tok::kw_alignas:
2054 if (!isCXX11AttributeSpecifier())
2055 goto DoneWithDeclSpec;
2056
2057 ProhibitAttributes(attrs);
2058 // FIXME: It would be good to recover by accepting the attributes,
2059 // but attempting to do that now would cause serious
2060 // madness in terms of diagnostics.
2061 attrs.clear();
2062 attrs.Range = SourceRange();
2063
2064 ParseCXX11Attributes(attrs);
2065 AttrsLastTime = true;
2066 continue;
2067
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002068 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002069 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002070 if (DS.hasTypeSpecifier()) {
2071 bool AllowNonIdentifiers
2072 = (getCurScope()->getFlags() & (Scope::ControlScope |
2073 Scope::BlockScope |
2074 Scope::TemplateParamScope |
2075 Scope::FunctionPrototypeScope |
2076 Scope::AtCatchScope)) == 0;
2077 bool AllowNestedNameSpecifiers
2078 = DSContext == DSC_top_level ||
2079 (DSContext == DSC_class && DS.isFriendSpecified());
2080
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002081 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
2082 AllowNonIdentifiers,
2083 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002084 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002085 }
2086
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002087 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2088 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2089 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00002090 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
2091 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002092 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002093 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002094 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002095 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002096
2097 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002098 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002099 }
2100
Chris Lattner5e02c472009-01-05 00:07:25 +00002101 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002102 // C++ scope specifier. Annotate and loop, or bail out on error.
2103 if (TryAnnotateCXXScopeToken(true)) {
2104 if (!DS.hasTypeSpecifier())
2105 DS.SetTypeSpecError();
2106 goto DoneWithDeclSpec;
2107 }
John McCall2e0a7152010-03-01 18:20:46 +00002108 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2109 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002110 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002111
2112 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002113 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002114 goto DoneWithDeclSpec;
2115
John McCallaa87d332009-12-12 11:40:51 +00002116 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002117 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2118 Tok.getAnnotationRange(),
2119 SS);
John McCallaa87d332009-12-12 11:40:51 +00002120
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002121 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002122 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002123 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002124 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002125 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002126 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002127
2128 // C++ [class.qual]p2:
2129 // In a lookup in which the constructor is an acceptable lookup
2130 // result and the nested-name-specifier nominates a class C:
2131 //
2132 // - if the name specified after the
2133 // nested-name-specifier, when looked up in C, is the
2134 // injected-class-name of C (Clause 9), or
2135 //
2136 // - if the name specified after the nested-name-specifier
2137 // is the same as the identifier or the
2138 // simple-template-id's template-name in the last
2139 // component of the nested-name-specifier,
2140 //
2141 // the name is instead considered to name the constructor of
2142 // class C.
2143 //
2144 // Thus, if the template-name is actually the constructor
2145 // name, then the code is ill-formed; this interpretation is
2146 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002147 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002148 if ((DSContext == DSC_top_level ||
2149 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2150 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002151 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002152 if (isConstructorDeclarator()) {
2153 // The user meant this to be an out-of-line constructor
2154 // definition, but template arguments are not allowed
2155 // there. Just allow this as a constructor; we'll
2156 // complain about it later.
2157 goto DoneWithDeclSpec;
2158 }
2159
2160 // The user meant this to name a type, but it actually names
2161 // a constructor with some extraneous template
2162 // arguments. Complain, then parse it as a type as the user
2163 // intended.
2164 Diag(TemplateId->TemplateNameLoc,
2165 diag::err_out_of_line_template_id_names_constructor)
2166 << TemplateId->Name;
2167 }
2168
John McCallaa87d332009-12-12 11:40:51 +00002169 DS.getTypeSpecScope() = SS;
2170 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002171 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002172 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002173 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002174 continue;
2175 }
2176
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002177 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002178 DS.getTypeSpecScope() = SS;
2179 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002180 if (Tok.getAnnotationValue()) {
2181 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002182 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2183 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002184 PrevSpec, DiagID, T);
2185 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002186 else
2187 DS.SetTypeSpecError();
2188 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2189 ConsumeToken(); // The typename
2190 }
2191
Douglas Gregor9135c722009-03-25 15:40:00 +00002192 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002193 goto DoneWithDeclSpec;
2194
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002195 // If we're in a context where the identifier could be a class name,
2196 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002197 if ((DSContext == DSC_top_level ||
2198 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002199 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002200 &SS)) {
2201 if (isConstructorDeclarator())
2202 goto DoneWithDeclSpec;
2203
2204 // As noted in C++ [class.qual]p2 (cited above), when the name
2205 // of the class is qualified in a context where it could name
2206 // a constructor, its a constructor name. However, we've
2207 // looked at the declarator, and the user probably meant this
2208 // to be a type. Complain that it isn't supposed to be treated
2209 // as a type, then proceed to parse it as a type.
2210 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2211 << Next.getIdentifierInfo();
2212 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002213
John McCallb3d87482010-08-24 05:47:05 +00002214 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2215 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002216 getCurScope(), &SS,
2217 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002218 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002219 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002220
Chris Lattnerf4382f52009-04-14 22:17:06 +00002221 // If the referenced identifier is not a type, then this declspec is
2222 // erroneous: We already checked about that it has no type specifier, and
2223 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002224 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002225 if (TypeRep == 0) {
2226 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00002227 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002228 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002229 }
Mike Stump1eb44332009-09-09 15:08:12 +00002230
John McCallaa87d332009-12-12 11:40:51 +00002231 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002232 ConsumeToken(); // The C++ scope.
2233
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002234 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002235 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002236 if (isInvalid)
2237 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002238
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002239 DS.SetRangeEnd(Tok.getLocation());
2240 ConsumeToken(); // The typename.
2241
2242 continue;
2243 }
Mike Stump1eb44332009-09-09 15:08:12 +00002244
Chris Lattner80d0c892009-01-21 19:48:37 +00002245 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002246 if (Tok.getAnnotationValue()) {
2247 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002248 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002249 DiagID, T);
2250 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002251 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00002252
2253 if (isInvalid)
2254 break;
2255
Chris Lattner80d0c892009-01-21 19:48:37 +00002256 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2257 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002258
Chris Lattner80d0c892009-01-21 19:48:37 +00002259 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2260 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002261 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002262 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002263 ParseObjCProtocolQualifiers(DS);
2264
Chris Lattner80d0c892009-01-21 19:48:37 +00002265 continue;
2266 }
Mike Stump1eb44332009-09-09 15:08:12 +00002267
Douglas Gregorbfad9152011-04-28 15:48:45 +00002268 case tok::kw___is_signed:
2269 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2270 // typically treats it as a trait. If we see __is_signed as it appears
2271 // in libstdc++, e.g.,
2272 //
2273 // static const bool __is_signed;
2274 //
2275 // then treat __is_signed as an identifier rather than as a keyword.
2276 if (DS.getTypeSpecType() == TST_bool &&
2277 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2278 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2279 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2280 Tok.setKind(tok::identifier);
2281 }
2282
2283 // We're done with the declaration-specifiers.
2284 goto DoneWithDeclSpec;
2285
Chris Lattner3bd934a2008-07-26 01:18:38 +00002286 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002287 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002288 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002289 // In C++, check to see if this is a scope specifier like foo::bar::, if
2290 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002291 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002292 if (TryAnnotateCXXScopeToken(true)) {
2293 if (!DS.hasTypeSpecifier())
2294 DS.SetTypeSpecError();
2295 goto DoneWithDeclSpec;
2296 }
2297 if (!Tok.is(tok::identifier))
2298 continue;
2299 }
Mike Stump1eb44332009-09-09 15:08:12 +00002300
Chris Lattner3bd934a2008-07-26 01:18:38 +00002301 // This identifier can only be a typedef name if we haven't already seen
2302 // a type-specifier. Without this check we misparse:
2303 // typedef int X; struct Y { short X; }; as 'short int'.
2304 if (DS.hasTypeSpecifier())
2305 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002306
John Thompson82287d12010-02-05 00:12:22 +00002307 // Check for need to substitute AltiVec keyword tokens.
2308 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2309 break;
2310
Richard Smithf63eee72012-05-09 18:56:43 +00002311 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2312 // allow the use of a typedef name as a type specifier.
2313 if (DS.isTypeAltiVecVector())
2314 goto DoneWithDeclSpec;
2315
John McCallb3d87482010-08-24 05:47:05 +00002316 ParsedType TypeRep =
2317 Actions.getTypeName(*Tok.getIdentifierInfo(),
2318 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002319
Chris Lattnerc199ab32009-04-12 20:42:31 +00002320 // If this is not a typedef name, don't parse it as part of the declspec,
2321 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002322 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002323 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002324 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002325 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002326
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002327 // If we're in a context where the identifier could be a class name,
2328 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002329 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002330 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002331 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002332 goto DoneWithDeclSpec;
2333
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002334 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002335 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002336 if (isInvalid)
2337 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002338
Chris Lattner3bd934a2008-07-26 01:18:38 +00002339 DS.SetRangeEnd(Tok.getLocation());
2340 ConsumeToken(); // The identifier
2341
2342 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2343 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002344 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002345 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002346 ParseObjCProtocolQualifiers(DS);
2347
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002348 // Need to support trailing type qualifiers (e.g. "id<p> const").
2349 // If a type specifier follows, it will be diagnosed elsewhere.
2350 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002351 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002352
2353 // type-name
2354 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002355 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002356 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002357 // This template-id does not refer to a type name, so we're
2358 // done with the type-specifiers.
2359 goto DoneWithDeclSpec;
2360 }
2361
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002362 // If we're in a context where the template-id could be a
2363 // constructor name or specialization, check whether this is a
2364 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002365 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002366 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002367 isConstructorDeclarator())
2368 goto DoneWithDeclSpec;
2369
Douglas Gregor39a8de12009-02-25 19:37:18 +00002370 // Turn the template-id annotation token into a type annotation
2371 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002372 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002373 continue;
2374 }
2375
Reid Spencer5f016e22007-07-11 17:01:13 +00002376 // GNU attributes support.
2377 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002378 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002379 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002380
2381 // Microsoft declspec support.
2382 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002383 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002384 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002385
Steve Naroff239f0732008-12-25 14:16:32 +00002386 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002387 case tok::kw___forceinline: {
2388 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2389 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
2390 SourceLocation AttrNameLoc = ConsumeToken();
Sean Hunt93f95f22012-06-18 16:13:52 +00002391 // FIXME: This does not work correctly if it is set to be a declspec
2392 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002393 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002394 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002395 continue;
2396 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002397
2398 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002399 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002400 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002401 case tok::kw___cdecl:
2402 case tok::kw___stdcall:
2403 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002404 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002405 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002406 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002407 continue;
2408
Dawn Perchik52fc3142010-09-03 01:29:35 +00002409 // Borland single token adornments.
2410 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002411 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002412 continue;
2413
Peter Collingbournef315fa82011-02-14 01:42:53 +00002414 // OpenCL single token adornments.
2415 case tok::kw___kernel:
2416 ParseOpenCLAttributes(DS.getAttributes());
2417 continue;
2418
Reid Spencer5f016e22007-07-11 17:01:13 +00002419 // storage-class-specifier
2420 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002421 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2422 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002423 break;
2424 case tok::kw_extern:
2425 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002426 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002427 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2428 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002429 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002430 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002431 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2432 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002433 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002434 case tok::kw_static:
2435 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002436 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002437 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2438 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002439 break;
2440 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002441 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002442 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002443 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2444 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002445 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002446 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002447 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002448 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002449 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2450 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002451 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002452 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2453 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002454 break;
2455 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002456 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2457 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002458 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002459 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002460 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2461 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002462 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002463 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002464 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002465 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002466
Reid Spencer5f016e22007-07-11 17:01:13 +00002467 // function-specifier
2468 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002469 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002470 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002471 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002472 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002473 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002474 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002475 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002476 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002477
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002478 // alignment-specifier
2479 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002480 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002481 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002482 ParseAlignmentSpecifier(DS.getAttributes());
2483 continue;
2484
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002485 // friend
2486 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002487 if (DSContext == DSC_class)
2488 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2489 else {
2490 PrevSpec = ""; // not actually used by the diagnostic
2491 DiagID = diag::err_friend_invalid_in_context;
2492 isInvalid = true;
2493 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002494 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002495
Douglas Gregor8d267c52011-09-09 02:06:17 +00002496 // Modules
2497 case tok::kw___module_private__:
2498 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2499 break;
2500
Sebastian Redl2ac67232009-11-05 15:47:02 +00002501 // constexpr
2502 case tok::kw_constexpr:
2503 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2504 break;
2505
Chris Lattner80d0c892009-01-21 19:48:37 +00002506 // type-specifier
2507 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002508 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2509 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002510 break;
2511 case tok::kw_long:
2512 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002513 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2514 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002515 else
John McCallfec54012009-08-03 20:12:06 +00002516 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2517 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002518 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002519 case tok::kw___int64:
2520 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2521 DiagID);
2522 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002523 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002524 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2525 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002526 break;
2527 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002528 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2529 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002530 break;
2531 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002532 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2533 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002534 break;
2535 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002536 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2537 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002538 break;
2539 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002540 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2541 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002542 break;
2543 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002544 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2545 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002546 break;
2547 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002548 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2549 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002550 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002551 case tok::kw___int128:
2552 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2553 DiagID);
2554 break;
2555 case tok::kw_half:
2556 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2557 DiagID);
2558 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002559 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002560 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2561 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002562 break;
2563 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002564 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2565 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002566 break;
2567 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002568 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2569 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002570 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002571 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002572 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2573 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002574 break;
2575 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002576 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2577 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002578 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002579 case tok::kw_bool:
2580 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002581 if (Tok.is(tok::kw_bool) &&
2582 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2583 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2584 PrevSpec = ""; // Not used by the diagnostic.
2585 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002586 // For better error recovery.
2587 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002588 isInvalid = true;
2589 } else {
2590 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2591 DiagID);
2592 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002593 break;
2594 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002595 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2596 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002597 break;
2598 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002599 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2600 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002601 break;
2602 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002603 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2604 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002605 break;
John Thompson82287d12010-02-05 00:12:22 +00002606 case tok::kw___vector:
2607 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2608 break;
2609 case tok::kw___pixel:
2610 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2611 break;
John McCalla5fc4722011-04-09 22:50:59 +00002612 case tok::kw___unknown_anytype:
2613 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2614 PrevSpec, DiagID);
2615 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002616
2617 // class-specifier:
2618 case tok::kw_class:
2619 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002620 case tok::kw_union: {
2621 tok::TokenKind Kind = Tok.getKind();
2622 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002623 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2624 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002625 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002626 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002627
2628 // enum-specifier:
2629 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002630 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002631 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002632 continue;
2633
2634 // cv-qualifier:
2635 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002636 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002637 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002638 break;
2639 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002640 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002641 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002642 break;
2643 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002644 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002645 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002646 break;
2647
Douglas Gregord57959a2009-03-27 23:10:48 +00002648 // C++ typename-specifier:
2649 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002650 if (TryAnnotateTypeOrScopeToken()) {
2651 DS.SetTypeSpecError();
2652 goto DoneWithDeclSpec;
2653 }
2654 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002655 continue;
2656 break;
2657
Chris Lattner80d0c892009-01-21 19:48:37 +00002658 // GNU typeof support.
2659 case tok::kw_typeof:
2660 ParseTypeofSpecifier(DS);
2661 continue;
2662
David Blaikie42d6d0c2011-12-04 05:04:18 +00002663 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002664 ParseDecltypeSpecifier(DS);
2665 continue;
2666
Sean Huntdb5d44b2011-05-19 05:37:45 +00002667 case tok::kw___underlying_type:
2668 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002669 continue;
2670
2671 case tok::kw__Atomic:
2672 ParseAtomicSpecifier(DS);
2673 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002674
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002675 // OpenCL qualifiers:
2676 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002677 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002678 goto DoneWithDeclSpec;
2679 case tok::kw___private:
2680 case tok::kw___global:
2681 case tok::kw___local:
2682 case tok::kw___constant:
2683 case tok::kw___read_only:
2684 case tok::kw___write_only:
2685 case tok::kw___read_write:
2686 ParseOpenCLQualifiers(DS);
2687 break;
2688
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002689 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002690 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002691 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2692 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002693 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002694 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002695
Douglas Gregor46f936e2010-11-19 17:10:50 +00002696 if (!ParseObjCProtocolQualifiers(DS))
2697 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2698 << FixItHint::CreateInsertion(Loc, "id")
2699 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002700
2701 // Need to support trailing type qualifiers (e.g. "id<p> const").
2702 // If a type specifier follows, it will be diagnosed elsewhere.
2703 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002704 }
John McCallfec54012009-08-03 20:12:06 +00002705 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002706 if (isInvalid) {
2707 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002708 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002709
2710 if (DiagID == diag::ext_duplicate_declspec)
2711 Diag(Tok, DiagID)
2712 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2713 else
2714 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002715 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002716
Chris Lattner81c018d2008-03-13 06:29:04 +00002717 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002718 if (DiagID != diag::err_bool_redeclaration)
2719 ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00002720
2721 AttrsLastTime = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002722 }
2723}
Douglas Gregoradcac882008-12-01 23:54:00 +00002724
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002725/// ParseStructDeclaration - Parse a struct declaration without the terminating
2726/// semicolon.
2727///
Reid Spencer5f016e22007-07-11 17:01:13 +00002728/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002729/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002730/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002731/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002732/// struct-declarator-list:
2733/// struct-declarator
2734/// struct-declarator-list ',' struct-declarator
2735/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2736/// struct-declarator:
2737/// declarator
2738/// [GNU] declarator attributes[opt]
2739/// declarator[opt] ':' constant-expression
2740/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2741///
Chris Lattnere1359422008-04-10 06:46:29 +00002742void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002743ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002744
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002745 if (Tok.is(tok::kw___extension__)) {
2746 // __extension__ silences extension warnings in the subexpression.
2747 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002748 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002749 return ParseStructDeclaration(DS, Fields);
2750 }
Mike Stump1eb44332009-09-09 15:08:12 +00002751
Steve Naroff28a7ca82007-08-20 22:28:22 +00002752 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002753 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002754
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002755 // If there are no declarators, this is a free-standing declaration
2756 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002757 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002758 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002759 return;
2760 }
2761
2762 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002763 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002764 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002765 while (1) {
John McCall92576642012-05-07 06:16:41 +00002766 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCallbdd563e2009-11-03 02:38:08 +00002767 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002768 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002769
2770 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002771 if (!FirstDeclarator)
2772 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002773
Steve Naroff28a7ca82007-08-20 22:28:22 +00002774 /// struct-declarator: declarator
2775 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002776 if (Tok.isNot(tok::colon)) {
2777 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2778 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002779 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002780 }
Mike Stump1eb44332009-09-09 15:08:12 +00002781
Chris Lattner04d66662007-10-09 17:33:22 +00002782 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002783 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002784 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002785 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002786 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002787 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002788 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002789 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002790
Steve Naroff28a7ca82007-08-20 22:28:22 +00002791 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002792 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002793
John McCallbdd563e2009-11-03 02:38:08 +00002794 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002795 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002796 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002797
Steve Naroff28a7ca82007-08-20 22:28:22 +00002798 // If we don't have a comma, it is either the end of the list (a ';')
2799 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002800 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002801 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002802
Steve Naroff28a7ca82007-08-20 22:28:22 +00002803 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002804 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002805
John McCallbdd563e2009-11-03 02:38:08 +00002806 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002807 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002808}
2809
2810/// ParseStructUnionBody
2811/// struct-contents:
2812/// struct-declaration-list
2813/// [EXT] empty
2814/// [GNU] "struct-declaration-list" without terminatoring ';'
2815/// struct-declaration-list:
2816/// struct-declaration
2817/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002818/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002819///
Reid Spencer5f016e22007-07-11 17:01:13 +00002820void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002821 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002822 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2823 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002824
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002825 BalancedDelimiterTracker T(*this, tok::l_brace);
2826 if (T.consumeOpen())
2827 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002828
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002829 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002830 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002831
Reid Spencer5f016e22007-07-11 17:01:13 +00002832 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2833 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002834 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002835 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2836 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2837 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002838
Chris Lattner5f9e2722011-07-23 10:55:15 +00002839 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002840
Reid Spencer5f016e22007-07-11 17:01:13 +00002841 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002842 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002843 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002844
Reid Spencer5f016e22007-07-11 17:01:13 +00002845 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002846 if (Tok.is(tok::semi)) {
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002847 ConsumeExtraSemi(InsideStruct,
2848 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Reid Spencer5f016e22007-07-11 17:01:13 +00002849 continue;
2850 }
Chris Lattnere1359422008-04-10 06:46:29 +00002851
2852 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002853 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002854
John McCallbdd563e2009-11-03 02:38:08 +00002855 if (!Tok.is(tok::at)) {
2856 struct CFieldCallback : FieldCallback {
2857 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002858 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002859 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002860
John McCalld226f652010-08-21 09:40:31 +00002861 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002862 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002863 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2864
John McCalld226f652010-08-21 09:40:31 +00002865 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002866 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002867 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002868 FD.D.getDeclSpec().getSourceRange().getBegin(),
2869 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002870 FieldDecls.push_back(Field);
2871 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002872 }
John McCallbdd563e2009-11-03 02:38:08 +00002873 } Callback(*this, TagDecl, FieldDecls);
2874
2875 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002876 } else { // Handle @defs
2877 ConsumeToken();
2878 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2879 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002880 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002881 continue;
2882 }
2883 ConsumeToken();
2884 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2885 if (!Tok.is(tok::identifier)) {
2886 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002887 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002888 continue;
2889 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002890 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002891 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002892 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002893 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2894 ConsumeToken();
2895 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002896 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002897
Chris Lattner04d66662007-10-09 17:33:22 +00002898 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002899 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002900 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002901 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002902 break;
2903 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002904 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2905 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002906 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002907 // If we stopped at a ';', eat it.
2908 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002909 }
2910 }
Mike Stump1eb44332009-09-09 15:08:12 +00002911
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002912 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002913
John McCall0b7e6782011-03-24 11:26:52 +00002914 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002915 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002916 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002917
Douglas Gregor23c94db2010-07-02 17:43:08 +00002918 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002919 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002920 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002921 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002922 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002923 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2924 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002925}
2926
Reid Spencer5f016e22007-07-11 17:01:13 +00002927/// ParseEnumSpecifier
2928/// enum-specifier: [C99 6.7.2.2]
2929/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002930///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002931/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2932/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002933/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2934/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002935/// 'enum' identifier
2936/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002937///
Richard Smith1af83c42012-03-23 03:33:32 +00002938/// [C++11] enum-head '{' enumerator-list[opt] '}'
2939/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002940///
Richard Smith1af83c42012-03-23 03:33:32 +00002941/// enum-head: [C++11]
2942/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2943/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2944/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002945///
Richard Smith1af83c42012-03-23 03:33:32 +00002946/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002947/// 'enum'
2948/// 'enum' 'class'
2949/// 'enum' 'struct'
2950///
Richard Smith1af83c42012-03-23 03:33:32 +00002951/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002952/// ':' type-specifier-seq
2953///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002954/// [C++] elaborated-type-specifier:
2955/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2956///
Chris Lattner4c97d762009-04-12 21:49:30 +00002957void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002958 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002959 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002960 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002961 if (Tok.is(tok::code_completion)) {
2962 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002963 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002964 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002965 }
John McCall57c13002011-07-06 05:58:41 +00002966
Sean Hunt2edf0a22012-06-23 05:07:58 +00002967 // If attributes exist after tag, parse them.
2968 ParsedAttributesWithRange attrs(AttrFactory);
2969 MaybeParseGNUAttributes(attrs);
2970 MaybeParseCXX0XAttributes(attrs);
2971
2972 // If declspecs exist after tag, parse them.
2973 while (Tok.is(tok::kw___declspec))
2974 ParseMicrosoftDeclSpec(attrs);
2975
Richard Smithbdad7a22012-01-10 01:33:14 +00002976 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002977 bool IsScopedUsingClassTag = false;
2978
David Blaikie4e4d0842012-03-11 07:00:24 +00002979 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002980 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002981 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002982 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002983 ScopedEnumKWLoc = ConsumeToken();
Sean Hunt2edf0a22012-06-23 05:07:58 +00002984
2985 ProhibitAttributes(attrs);
2986 // Recovery: assume that the attributes came after the tag.
2987 MaybeParseCXX0XAttributes(attrs);
John McCall57c13002011-07-06 05:58:41 +00002988 }
Richard Smith1af83c42012-03-23 03:33:32 +00002989
John McCall13489672012-05-07 06:16:58 +00002990 // C++11 [temp.explicit]p12:
2991 // The usual access controls do not apply to names used to specify
2992 // explicit instantiations.
2993 // We extend this to also cover explicit specializations. Note that
2994 // we don't suppress if this turns out to be an elaborated type
2995 // specifier.
2996 bool shouldDelayDiagsInTag =
2997 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2998 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2999 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00003000
Richard Smith7796eb52012-03-12 08:56:40 +00003001 // Enum definitions should not be parsed in a trailing-return-type.
3002 bool AllowDeclaration = DSC != DSC_trailing;
3003
3004 bool AllowFixedUnderlyingType = AllowDeclaration &&
3005 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
3006 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00003007
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00003008 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00003009 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00003010 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
3011 // if a fixed underlying type is allowed.
3012 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
3013
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003014 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3015 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00003016 return;
3017
3018 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003019 Diag(Tok, diag::err_expected_ident);
3020 if (Tok.isNot(tok::l_brace)) {
3021 // Has no name and is not a definition.
3022 // Skip the rest of this declarator, up until the comma or semicolon.
3023 SkipUntil(tok::comma, true);
3024 return;
3025 }
3026 }
3027 }
Mike Stump1eb44332009-09-09 15:08:12 +00003028
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003029 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003030 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003031 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003032 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003033
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003034 // Skip the rest of this declarator, up until the comma or semicolon.
3035 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003036 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003037 }
Mike Stump1eb44332009-09-09 15:08:12 +00003038
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003039 // If an identifier is present, consume and remember it.
3040 IdentifierInfo *Name = 0;
3041 SourceLocation NameLoc;
3042 if (Tok.is(tok::identifier)) {
3043 Name = Tok.getIdentifierInfo();
3044 NameLoc = ConsumeToken();
3045 }
Mike Stump1eb44332009-09-09 15:08:12 +00003046
Richard Smithbdad7a22012-01-10 01:33:14 +00003047 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003048 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3049 // declaration of a scoped enumeration.
3050 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003051 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003052 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003053 }
3054
John McCall13489672012-05-07 06:16:58 +00003055 // Okay, end the suppression area. We'll decide whether to emit the
3056 // diagnostics in a second.
3057 if (shouldDelayDiagsInTag)
3058 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003059
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003060 TypeResult BaseType;
3061
Douglas Gregora61b3e72010-12-01 17:42:47 +00003062 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00003063 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003064 bool PossibleBitfield = false;
3065 if (getCurScope()->getFlags() & Scope::ClassScope) {
3066 // If we're in class scope, this can either be an enum declaration with
3067 // an underlying type, or a declaration of a bitfield member. We try to
3068 // use a simple disambiguation scheme first to catch the common cases
3069 // (integer literal, sizeof); if it's still ambiguous, we then consider
3070 // anything that's a simple-type-specifier followed by '(' as an
3071 // expression. This suffices because function types are not valid
3072 // underlying types anyway.
3073 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
3074 // If the next token starts an expression, we know we're parsing a
3075 // bit-field. This is the common case.
3076 if (TPR == TPResult::True())
3077 PossibleBitfield = true;
3078 // If the next token starts a type-specifier-seq, it may be either a
3079 // a fixed underlying type or the start of a function-style cast in C++;
3080 // lookahead one more token to see if it's obvious that we have a
3081 // fixed underlying type.
3082 else if (TPR == TPResult::False() &&
3083 GetLookAheadToken(2).getKind() == tok::semi) {
3084 // Consume the ':'.
3085 ConsumeToken();
3086 } else {
3087 // We have the start of a type-specifier-seq, so we have to perform
3088 // tentative parsing to determine whether we have an expression or a
3089 // type.
3090 TentativeParsingAction TPA(*this);
3091
3092 // Consume the ':'.
3093 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003094
3095 // If we see a type specifier followed by an open-brace, we have an
3096 // ambiguity between an underlying type and a C++11 braced
3097 // function-style cast. Resolve this by always treating it as an
3098 // underlying type.
3099 // FIXME: The standard is not entirely clear on how to disambiguate in
3100 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003101 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003102 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003103 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003104 // We'll parse this as a bitfield later.
3105 PossibleBitfield = true;
3106 TPA.Revert();
3107 } else {
3108 // We have a type-specifier-seq.
3109 TPA.Commit();
3110 }
3111 }
3112 } else {
3113 // Consume the ':'.
3114 ConsumeToken();
3115 }
3116
3117 if (!PossibleBitfield) {
3118 SourceRange Range;
3119 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00003120
David Blaikie4e4d0842012-03-11 07:00:24 +00003121 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00003122 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
3123 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00003124 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003125 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003126 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003127 }
3128
Richard Smithbdad7a22012-01-10 01:33:14 +00003129 // There are four options here. If we have 'friend enum foo;' then this is a
3130 // friend declaration, and cannot have an accompanying definition. If we have
3131 // 'enum foo;', then this is a forward declaration. If we have
3132 // 'enum foo {...' then this is a definition. Otherwise we have something
3133 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003134 //
3135 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3136 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3137 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3138 //
John McCallf312b1e2010-08-26 23:41:50 +00003139 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003140 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003141 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003142 } else if (Tok.is(tok::l_brace)) {
3143 if (DS.isFriendSpecified()) {
3144 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3145 << SourceRange(DS.getFriendSpecLoc());
3146 ConsumeBrace();
3147 SkipUntil(tok::r_brace);
3148 TUK = Sema::TUK_Friend;
3149 } else {
3150 TUK = Sema::TUK_Definition;
3151 }
3152 } else if (Tok.is(tok::semi) && DSC != DSC_type_specifier) {
3153 TUK = (DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration);
3154 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003155 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003156 }
3157
3158 // If this is an elaborated type specifier, and we delayed
3159 // diagnostics before, just merge them into the current pool.
3160 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3161 diagsFromTag.redelay();
3162 }
Richard Smith1af83c42012-03-23 03:33:32 +00003163
3164 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003165 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003166 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003167 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3168 // Skip the rest of this declarator, up until the comma or semicolon.
3169 Diag(Tok, diag::err_enum_template);
3170 SkipUntil(tok::comma, true);
3171 return;
3172 }
3173
3174 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3175 // Enumerations can't be explicitly instantiated.
3176 DS.SetTypeSpecError();
3177 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3178 return;
3179 }
3180
3181 assert(TemplateInfo.TemplateParams && "no template parameters");
3182 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3183 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003184 }
Sean Hunt2edf0a22012-06-23 05:07:58 +00003185
3186 if (TUK == Sema::TUK_Reference)
3187 ProhibitAttributes(attrs);
Richard Smith1af83c42012-03-23 03:33:32 +00003188
Douglas Gregorb9075602011-02-22 02:55:24 +00003189 if (!Name && TUK != Sema::TUK_Definition) {
3190 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003191
Douglas Gregorb9075602011-02-22 02:55:24 +00003192 // Skip the rest of this declarator, up until the comma or semicolon.
3193 SkipUntil(tok::comma, true);
3194 return;
3195 }
Richard Smith1af83c42012-03-23 03:33:32 +00003196
Douglas Gregor402abb52009-05-28 23:31:59 +00003197 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003198 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003199 const char *PrevSpec = 0;
3200 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003201 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003202 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003203 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003204 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003205 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003206
Douglas Gregor48c89f42010-04-24 16:38:41 +00003207 if (IsDependent) {
3208 // This enum has a dependent nested-name-specifier. Handle it as a
3209 // dependent tag.
3210 if (!Name) {
3211 DS.SetTypeSpecError();
3212 Diag(Tok, diag::err_expected_type_name_after_typename);
3213 return;
3214 }
3215
Douglas Gregor23c94db2010-07-02 17:43:08 +00003216 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003217 TUK, SS, Name, StartLoc,
3218 NameLoc);
3219 if (Type.isInvalid()) {
3220 DS.SetTypeSpecError();
3221 return;
3222 }
3223
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003224 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3225 NameLoc.isValid() ? NameLoc : StartLoc,
3226 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003227 Diag(StartLoc, DiagID) << PrevSpec;
3228
3229 return;
3230 }
Mike Stump1eb44332009-09-09 15:08:12 +00003231
John McCalld226f652010-08-21 09:40:31 +00003232 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003233 // The action failed to produce an enumeration tag. If this is a
3234 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003235 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003236 ConsumeBrace();
3237 SkipUntil(tok::r_brace);
3238 }
3239
3240 DS.SetTypeSpecError();
3241 return;
3242 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003243
Richard Smith7796eb52012-03-12 08:56:40 +00003244 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
John McCall13489672012-05-07 06:16:58 +00003245 ParseEnumBody(StartLoc, TagDecl);
Richard Smithbdad7a22012-01-10 01:33:14 +00003246 }
Mike Stump1eb44332009-09-09 15:08:12 +00003247
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003248 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3249 NameLoc.isValid() ? NameLoc : StartLoc,
3250 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003251 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003252}
3253
3254/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3255/// enumerator-list:
3256/// enumerator
3257/// enumerator-list ',' enumerator
3258/// enumerator:
3259/// enumeration-constant
3260/// enumeration-constant '=' constant-expression
3261/// enumeration-constant:
3262/// identifier
3263///
John McCalld226f652010-08-21 09:40:31 +00003264void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003265 // Enter the scope of the enum body and start the definition.
3266 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003267 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003268
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003269 BalancedDelimiterTracker T(*this, tok::l_brace);
3270 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003271
Chris Lattner7946dd32007-08-27 17:24:30 +00003272 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003273 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003274 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003275
Chris Lattner5f9e2722011-07-23 10:55:15 +00003276 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003277
John McCalld226f652010-08-21 09:40:31 +00003278 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003279
Reid Spencer5f016e22007-07-11 17:01:13 +00003280 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003281 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003282 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3283 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003284
John McCall5b629aa2010-10-22 23:36:17 +00003285 // If attributes exist after the enumerator, parse them.
Sean Hunt2edf0a22012-06-23 05:07:58 +00003286 ParsedAttributesWithRange attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003287 MaybeParseGNUAttributes(attrs);
Sean Hunt2edf0a22012-06-23 05:07:58 +00003288 MaybeParseCXX0XAttributes(attrs);
3289 ProhibitAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003290
Reid Spencer5f016e22007-07-11 17:01:13 +00003291 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003292 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003293 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003294
Chris Lattner04d66662007-10-09 17:33:22 +00003295 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003296 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003297 AssignedVal = ParseConstantExpression();
3298 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003299 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003300 }
Mike Stump1eb44332009-09-09 15:08:12 +00003301
Reid Spencer5f016e22007-07-11 17:01:13 +00003302 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003303 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3304 LastEnumConstDecl,
3305 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003306 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003307 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003308 PD.complete(EnumConstDecl);
3309
Reid Spencer5f016e22007-07-11 17:01:13 +00003310 EnumConstantDecls.push_back(EnumConstDecl);
3311 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003312
Douglas Gregor751f6922010-09-07 14:51:08 +00003313 if (Tok.is(tok::identifier)) {
3314 // We're missing a comma between enumerators.
3315 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3316 Diag(Loc, diag::err_enumerator_list_missing_comma)
3317 << FixItHint::CreateInsertion(Loc, ", ");
3318 continue;
3319 }
3320
Chris Lattner04d66662007-10-09 17:33:22 +00003321 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003322 break;
3323 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003324
Richard Smith7fe62082011-10-15 05:09:34 +00003325 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003326 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003327 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00003328 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00003329 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003330 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003331 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3332 << FixItHint::CreateRemoval(CommaLoc);
3333 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003334 }
Mike Stump1eb44332009-09-09 15:08:12 +00003335
Reid Spencer5f016e22007-07-11 17:01:13 +00003336 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003337 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003338
Reid Spencer5f016e22007-07-11 17:01:13 +00003339 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003340 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003341 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003342
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003343 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3344 EnumDecl, EnumConstantDecls.data(),
3345 EnumConstantDecls.size(), getCurScope(),
3346 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003347
Douglas Gregor72de6672009-01-08 20:45:30 +00003348 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003349 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3350 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003351}
3352
3353/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003354/// start of a type-qualifier-list.
3355bool Parser::isTypeQualifier() const {
3356 switch (Tok.getKind()) {
3357 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003358
3359 // type-qualifier only in OpenCL
3360 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003361 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003362
Steve Naroff5f8aa692008-02-11 23:15:56 +00003363 // type-qualifier
3364 case tok::kw_const:
3365 case tok::kw_volatile:
3366 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003367 case tok::kw___private:
3368 case tok::kw___local:
3369 case tok::kw___global:
3370 case tok::kw___constant:
3371 case tok::kw___read_only:
3372 case tok::kw___read_write:
3373 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003374 return true;
3375 }
3376}
3377
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003378/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3379/// is definitely a type-specifier. Return false if it isn't part of a type
3380/// specifier or if we're not sure.
3381bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3382 switch (Tok.getKind()) {
3383 default: return false;
3384 // type-specifiers
3385 case tok::kw_short:
3386 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003387 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003388 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003389 case tok::kw_signed:
3390 case tok::kw_unsigned:
3391 case tok::kw__Complex:
3392 case tok::kw__Imaginary:
3393 case tok::kw_void:
3394 case tok::kw_char:
3395 case tok::kw_wchar_t:
3396 case tok::kw_char16_t:
3397 case tok::kw_char32_t:
3398 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003399 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003400 case tok::kw_float:
3401 case tok::kw_double:
3402 case tok::kw_bool:
3403 case tok::kw__Bool:
3404 case tok::kw__Decimal32:
3405 case tok::kw__Decimal64:
3406 case tok::kw__Decimal128:
3407 case tok::kw___vector:
3408
3409 // struct-or-union-specifier (C99) or class-specifier (C++)
3410 case tok::kw_class:
3411 case tok::kw_struct:
3412 case tok::kw_union:
3413 // enum-specifier
3414 case tok::kw_enum:
3415
3416 // typedef-name
3417 case tok::annot_typename:
3418 return true;
3419 }
3420}
3421
Steve Naroff5f8aa692008-02-11 23:15:56 +00003422/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003423/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003424bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003425 switch (Tok.getKind()) {
3426 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003427
Chris Lattner166a8fc2009-01-04 23:41:41 +00003428 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003429 if (TryAltiVecVectorToken())
3430 return true;
3431 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003432 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003433 // Annotate typenames and C++ scope specifiers. If we get one, just
3434 // recurse to handle whatever we get.
3435 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003436 return true;
3437 if (Tok.is(tok::identifier))
3438 return false;
3439 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003440
Chris Lattner166a8fc2009-01-04 23:41:41 +00003441 case tok::coloncolon: // ::foo::bar
3442 if (NextToken().is(tok::kw_new) || // ::new
3443 NextToken().is(tok::kw_delete)) // ::delete
3444 return false;
3445
Chris Lattner166a8fc2009-01-04 23:41:41 +00003446 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003447 return true;
3448 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003449
Reid Spencer5f016e22007-07-11 17:01:13 +00003450 // GNU attributes support.
3451 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003452 // GNU typeof support.
3453 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003454
Reid Spencer5f016e22007-07-11 17:01:13 +00003455 // type-specifiers
3456 case tok::kw_short:
3457 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003458 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003459 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003460 case tok::kw_signed:
3461 case tok::kw_unsigned:
3462 case tok::kw__Complex:
3463 case tok::kw__Imaginary:
3464 case tok::kw_void:
3465 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003466 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003467 case tok::kw_char16_t:
3468 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003469 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003470 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003471 case tok::kw_float:
3472 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003473 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003474 case tok::kw__Bool:
3475 case tok::kw__Decimal32:
3476 case tok::kw__Decimal64:
3477 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003478 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003479
Chris Lattner99dc9142008-04-13 18:59:07 +00003480 // struct-or-union-specifier (C99) or class-specifier (C++)
3481 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003482 case tok::kw_struct:
3483 case tok::kw_union:
3484 // enum-specifier
3485 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003486
Reid Spencer5f016e22007-07-11 17:01:13 +00003487 // type-qualifier
3488 case tok::kw_const:
3489 case tok::kw_volatile:
3490 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003491
3492 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003493 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003494 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003495
Chris Lattner7c186be2008-10-20 00:25:30 +00003496 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3497 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003498 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003499
Steve Naroff239f0732008-12-25 14:16:32 +00003500 case tok::kw___cdecl:
3501 case tok::kw___stdcall:
3502 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003503 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003504 case tok::kw___w64:
3505 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003506 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003507 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003508 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003509
3510 case tok::kw___private:
3511 case tok::kw___local:
3512 case tok::kw___global:
3513 case tok::kw___constant:
3514 case tok::kw___read_only:
3515 case tok::kw___read_write:
3516 case tok::kw___write_only:
3517
Eli Friedman290eeb02009-06-08 23:27:34 +00003518 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003519
3520 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003521 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003522
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003523 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003524 case tok::kw__Atomic:
3525 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003526 }
3527}
3528
3529/// isDeclarationSpecifier() - Return true if the current token is part of a
3530/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003531///
3532/// \param DisambiguatingWithExpression True to indicate that the purpose of
3533/// this check is to disambiguate between an expression and a declaration.
3534bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003535 switch (Tok.getKind()) {
3536 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003537
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003538 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003539 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003540
Chris Lattner166a8fc2009-01-04 23:41:41 +00003541 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003542 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003543 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003544 return false;
John Thompson82287d12010-02-05 00:12:22 +00003545 if (TryAltiVecVectorToken())
3546 return true;
3547 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003548 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003549 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003550 // Annotate typenames and C++ scope specifiers. If we get one, just
3551 // recurse to handle whatever we get.
3552 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003553 return true;
3554 if (Tok.is(tok::identifier))
3555 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003556
3557 // If we're in Objective-C and we have an Objective-C class type followed
3558 // by an identifier and then either ':' or ']', in a place where an
3559 // expression is permitted, then this is probably a class message send
3560 // missing the initial '['. In this case, we won't consider this to be
3561 // the start of a declaration.
3562 if (DisambiguatingWithExpression &&
3563 isStartOfObjCClassMessageMissingOpenBracket())
3564 return false;
3565
John McCall9ba61662010-02-26 08:45:28 +00003566 return isDeclarationSpecifier();
3567
Chris Lattner166a8fc2009-01-04 23:41:41 +00003568 case tok::coloncolon: // ::foo::bar
3569 if (NextToken().is(tok::kw_new) || // ::new
3570 NextToken().is(tok::kw_delete)) // ::delete
3571 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003572
Chris Lattner166a8fc2009-01-04 23:41:41 +00003573 // Annotate typenames and C++ scope specifiers. If we get one, just
3574 // recurse to handle whatever we get.
3575 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003576 return true;
3577 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003578
Reid Spencer5f016e22007-07-11 17:01:13 +00003579 // storage-class-specifier
3580 case tok::kw_typedef:
3581 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003582 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003583 case tok::kw_static:
3584 case tok::kw_auto:
3585 case tok::kw_register:
3586 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003587
Douglas Gregor8d267c52011-09-09 02:06:17 +00003588 // Modules
3589 case tok::kw___module_private__:
3590
Reid Spencer5f016e22007-07-11 17:01:13 +00003591 // type-specifiers
3592 case tok::kw_short:
3593 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003594 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003595 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003596 case tok::kw_signed:
3597 case tok::kw_unsigned:
3598 case tok::kw__Complex:
3599 case tok::kw__Imaginary:
3600 case tok::kw_void:
3601 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003602 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003603 case tok::kw_char16_t:
3604 case tok::kw_char32_t:
3605
Reid Spencer5f016e22007-07-11 17:01:13 +00003606 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003607 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003608 case tok::kw_float:
3609 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003610 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003611 case tok::kw__Bool:
3612 case tok::kw__Decimal32:
3613 case tok::kw__Decimal64:
3614 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003615 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003616
Chris Lattner99dc9142008-04-13 18:59:07 +00003617 // struct-or-union-specifier (C99) or class-specifier (C++)
3618 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003619 case tok::kw_struct:
3620 case tok::kw_union:
3621 // enum-specifier
3622 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003623
Reid Spencer5f016e22007-07-11 17:01:13 +00003624 // type-qualifier
3625 case tok::kw_const:
3626 case tok::kw_volatile:
3627 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003628
Reid Spencer5f016e22007-07-11 17:01:13 +00003629 // function-specifier
3630 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003631 case tok::kw_virtual:
3632 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003633
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003634 // static_assert-declaration
3635 case tok::kw__Static_assert:
3636
Chris Lattner1ef08762007-08-09 17:01:07 +00003637 // GNU typeof support.
3638 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003639
Chris Lattner1ef08762007-08-09 17:01:07 +00003640 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003641 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003642 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003643
Francois Pichete3d49b42011-06-19 08:02:06 +00003644 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003645 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003646 return true;
3647
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003648 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003649 case tok::kw__Atomic:
3650 return true;
3651
Chris Lattnerf3948c42008-07-26 03:38:44 +00003652 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3653 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003654 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003655
Douglas Gregord9d75e52011-04-27 05:41:15 +00003656 // typedef-name
3657 case tok::annot_typename:
3658 return !DisambiguatingWithExpression ||
3659 !isStartOfObjCClassMessageMissingOpenBracket();
3660
Steve Naroff47f52092009-01-06 19:34:12 +00003661 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003662 case tok::kw___cdecl:
3663 case tok::kw___stdcall:
3664 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003665 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003666 case tok::kw___w64:
3667 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003668 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003669 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003670 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003671 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003672
3673 case tok::kw___private:
3674 case tok::kw___local:
3675 case tok::kw___global:
3676 case tok::kw___constant:
3677 case tok::kw___read_only:
3678 case tok::kw___read_write:
3679 case tok::kw___write_only:
3680
Eli Friedman290eeb02009-06-08 23:27:34 +00003681 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003682 }
3683}
3684
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003685bool Parser::isConstructorDeclarator() {
3686 TentativeParsingAction TPA(*this);
3687
3688 // Parse the C++ scope specifier.
3689 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003690 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3691 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003692 TPA.Revert();
3693 return false;
3694 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003695
3696 // Parse the constructor name.
3697 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3698 // We already know that we have a constructor name; just consume
3699 // the token.
3700 ConsumeToken();
3701 } else {
3702 TPA.Revert();
3703 return false;
3704 }
3705
Richard Smith22592862012-03-27 23:05:05 +00003706 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003707 if (Tok.isNot(tok::l_paren)) {
3708 TPA.Revert();
3709 return false;
3710 }
3711 ConsumeParen();
3712
Richard Smith22592862012-03-27 23:05:05 +00003713 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3714 // that we have a constructor.
3715 if (Tok.is(tok::r_paren) ||
3716 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003717 TPA.Revert();
3718 return true;
3719 }
3720
3721 // If we need to, enter the specified scope.
3722 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003723 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003724 DeclScopeObj.EnterDeclaratorScope();
3725
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003726 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003727 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003728 MaybeParseMicrosoftAttributes(Attrs);
3729
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003730 // Check whether the next token(s) are part of a declaration
3731 // specifier, in which case we have the start of a parameter and,
3732 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003733 bool IsConstructor = false;
3734 if (isDeclarationSpecifier())
3735 IsConstructor = true;
3736 else if (Tok.is(tok::identifier) ||
3737 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3738 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3739 // This might be a parenthesized member name, but is more likely to
3740 // be a constructor declaration with an invalid argument type. Keep
3741 // looking.
3742 if (Tok.is(tok::annot_cxxscope))
3743 ConsumeToken();
3744 ConsumeToken();
3745
3746 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003747 // which must have one of the following syntactic forms (see the
3748 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003749 switch (Tok.getKind()) {
3750 case tok::l_paren:
3751 // C(X ( int));
3752 case tok::l_square:
3753 // C(X [ 5]);
3754 // C(X [ [attribute]]);
3755 case tok::coloncolon:
3756 // C(X :: Y);
3757 // C(X :: *p);
3758 case tok::r_paren:
3759 // C(X )
3760 // Assume this isn't a constructor, rather than assuming it's a
3761 // constructor with an unnamed parameter of an ill-formed type.
3762 break;
3763
3764 default:
3765 IsConstructor = true;
3766 break;
3767 }
3768 }
3769
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003770 TPA.Revert();
3771 return IsConstructor;
3772}
Reid Spencer5f016e22007-07-11 17:01:13 +00003773
3774/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003775/// type-qualifier-list: [C99 6.7.5]
3776/// type-qualifier
3777/// [vendor] attributes
3778/// [ only if VendorAttributesAllowed=true ]
3779/// type-qualifier-list type-qualifier
3780/// [vendor] type-qualifier-list attributes
3781/// [ only if VendorAttributesAllowed=true ]
3782/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3783/// [ only if CXX0XAttributesAllowed=true ]
3784/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003785///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003786void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3787 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003788 bool CXX11AttributesAllowed) {
3789 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003790 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003791 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003792 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003793 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003794 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003795
3796 SourceLocation EndLoc;
3797
Reid Spencer5f016e22007-07-11 17:01:13 +00003798 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003799 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003800 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003801 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003802 SourceLocation Loc = Tok.getLocation();
3803
3804 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003805 case tok::code_completion:
3806 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003807 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003808
Reid Spencer5f016e22007-07-11 17:01:13 +00003809 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003810 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003811 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003812 break;
3813 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003814 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003815 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003816 break;
3817 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003818 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003819 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003820 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003821
3822 // OpenCL qualifiers:
3823 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003824 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003825 goto DoneWithTypeQuals;
3826 case tok::kw___private:
3827 case tok::kw___global:
3828 case tok::kw___local:
3829 case tok::kw___constant:
3830 case tok::kw___read_only:
3831 case tok::kw___write_only:
3832 case tok::kw___read_write:
3833 ParseOpenCLQualifiers(DS);
3834 break;
3835
Eli Friedman290eeb02009-06-08 23:27:34 +00003836 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003837 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003838 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003839 case tok::kw___cdecl:
3840 case tok::kw___stdcall:
3841 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003842 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003843 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003844 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003845 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003846 continue;
3847 }
3848 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003849 case tok::kw___pascal:
3850 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003851 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003852 continue;
3853 }
3854 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003855 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003856 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003857 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003858 continue; // do *not* consume the next token!
3859 }
3860 // otherwise, FALL THROUGH!
3861 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003862 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003863 // If this is not a type-qualifier token, we're done reading type
3864 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003865 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003866 if (EndLoc.isValid())
3867 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003868 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003869 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003870
Reid Spencer5f016e22007-07-11 17:01:13 +00003871 // If the specifier combination wasn't legal, issue a diagnostic.
3872 if (isInvalid) {
3873 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003874 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003875 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003876 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003877 }
3878}
3879
3880
3881/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3882///
3883void Parser::ParseDeclarator(Declarator &D) {
3884 /// This implements the 'declarator' production in the C grammar, then checks
3885 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003886 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003887}
3888
Richard Smith9988f282012-03-29 01:16:42 +00003889static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3890 if (Kind == tok::star || Kind == tok::caret)
3891 return true;
3892
3893 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3894 if (!Lang.CPlusPlus)
3895 return false;
3896
3897 return Kind == tok::amp || Kind == tok::ampamp;
3898}
3899
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003900/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3901/// is parsed by the function passed to it. Pass null, and the direct-declarator
3902/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003903/// ptr-operator production.
3904///
Richard Smith0706df42011-10-19 21:33:05 +00003905/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003906/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3907/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003908///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003909/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3910/// [C] pointer[opt] direct-declarator
3911/// [C++] direct-declarator
3912/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003913///
3914/// pointer: [C99 6.7.5]
3915/// '*' type-qualifier-list[opt]
3916/// '*' type-qualifier-list[opt] pointer
3917///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003918/// ptr-operator:
3919/// '*' cv-qualifier-seq[opt]
3920/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003921/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003922/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003923/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003924/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003925void Parser::ParseDeclaratorInternal(Declarator &D,
3926 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003927 if (Diags.hasAllExtensionsSilenced())
3928 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003929
Sebastian Redlf30208a2009-01-24 21:16:55 +00003930 // C++ member pointers start with a '::' or a nested-name.
3931 // Member pointers get special handling, since there's no place for the
3932 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003933 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003934 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3935 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003936 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3937 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003938 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003939 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003940
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003941 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003942 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003943 // The scope spec really belongs to the direct-declarator.
3944 D.getCXXScopeSpec() = SS;
3945 if (DirectDeclParser)
3946 (this->*DirectDeclParser)(D);
3947 return;
3948 }
3949
3950 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003951 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003952 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003953 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003954 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003955
3956 // Recurse to parse whatever is left.
3957 ParseDeclaratorInternal(D, DirectDeclParser);
3958
3959 // Sema will have to catch (syntactically invalid) pointers into global
3960 // scope. It has to catch pointers into namespace scope anyway.
3961 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003962 Loc),
3963 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003964 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003965 return;
3966 }
3967 }
3968
3969 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003970 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00003971 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003972 if (DirectDeclParser)
3973 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003974 return;
3975 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003976
Sebastian Redl05532f22009-03-15 22:02:01 +00003977 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3978 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003979 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003980 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003981
Chris Lattner9af55002009-03-27 04:18:06 +00003982 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003983 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003984 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003985
Richard Smith6ee326a2012-04-10 01:32:12 +00003986 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00003987 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003988 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003989
Reid Spencer5f016e22007-07-11 17:01:13 +00003990 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003991 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003992 if (Kind == tok::star)
3993 // Remember that we parsed a pointer type, and remember the type-quals.
3994 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003995 DS.getConstSpecLoc(),
3996 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003997 DS.getRestrictSpecLoc()),
3998 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003999 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00004000 else
4001 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00004002 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00004003 Loc),
4004 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004005 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004006 } else {
4007 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00004008 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00004009
Sebastian Redl743de1f2009-03-23 00:00:23 +00004010 // Complain about rvalue references in C++03, but then go on and build
4011 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00004012 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00004013 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004014 diag::warn_cxx98_compat_rvalue_reference :
4015 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00004016
Richard Smith6ee326a2012-04-10 01:32:12 +00004017 // GNU-style and C++11 attributes are allowed here, as is restrict.
4018 ParseTypeQualifierListOpt(DS);
4019 D.ExtendWithDeclSpec(DS);
4020
Reid Spencer5f016e22007-07-11 17:01:13 +00004021 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
4022 // cv-qualifiers are introduced through the use of a typedef or of a
4023 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00004024 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
4025 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
4026 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004027 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00004028 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
4029 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00004030 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00004031 }
4032
4033 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004034 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004035
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004036 if (D.getNumTypeObjects() > 0) {
4037 // C++ [dcl.ref]p4: There shall be no references to references.
4038 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4039 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004040 if (const IdentifierInfo *II = D.getIdentifier())
4041 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4042 << II;
4043 else
4044 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4045 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004046
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004047 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004048 // can go ahead and build the (technically ill-formed)
4049 // declarator: reference collapsing will take care of it.
4050 }
4051 }
4052
Reid Spencer5f016e22007-07-11 17:01:13 +00004053 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00004054 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004055 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004056 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004057 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004058 }
4059}
4060
Richard Smith9988f282012-03-29 01:16:42 +00004061static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4062 SourceLocation EllipsisLoc) {
4063 if (EllipsisLoc.isValid()) {
4064 FixItHint Insertion;
4065 if (!D.getEllipsisLoc().isValid()) {
4066 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4067 D.setEllipsisLoc(EllipsisLoc);
4068 }
4069 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4070 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4071 }
4072}
4073
Reid Spencer5f016e22007-07-11 17:01:13 +00004074/// ParseDirectDeclarator
4075/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004076/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004077/// '(' declarator ')'
4078/// [GNU] '(' attributes declarator ')'
4079/// [C90] direct-declarator '[' constant-expression[opt] ']'
4080/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4081/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4082/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4083/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004084/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4085/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004086/// direct-declarator '(' parameter-type-list ')'
4087/// direct-declarator '(' identifier-list[opt] ')'
4088/// [GNU] direct-declarator '(' parameter-forward-declarations
4089/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004090/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4091/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004092/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4093/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4094/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004095/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004096/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004097///
4098/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004099/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004100/// '::'[opt] nested-name-specifier[opt] type-name
4101///
4102/// id-expression: [C++ 5.1]
4103/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004104/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004105///
4106/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004107/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004108/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004109/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004110/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004111/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004112///
Richard Smith5d8388c2012-03-27 01:42:32 +00004113/// Note, any additional constructs added here may need corresponding changes
4114/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004115void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004116 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004117
David Blaikie4e4d0842012-03-11 07:00:24 +00004118 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004119 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004120 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004121 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4122 D.getContext() == Declarator::MemberContext;
4123 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
4124 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004125 }
4126
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004127 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004128 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004129 // Change the declaration context for name lookup, until this function
4130 // is exited (and the declarator has been parsed).
4131 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004132 }
4133
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004134 // C++0x [dcl.fct]p14:
4135 // There is a syntactic ambiguity when an ellipsis occurs at the end
4136 // of a parameter-declaration-clause without a preceding comma. In
4137 // this case, the ellipsis is parsed as part of the
4138 // abstract-declarator if the type of the parameter names a template
4139 // parameter pack that has not been expanded; otherwise, it is parsed
4140 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004141 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004142 !((D.getContext() == Declarator::PrototypeContext ||
4143 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004144 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00004145 !Actions.containsUnexpandedParameterPacks(D))) {
4146 SourceLocation EllipsisLoc = ConsumeToken();
4147 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4148 // The ellipsis was put in the wrong place. Recover, and explain to
4149 // the user what they should have done.
4150 ParseDeclarator(D);
4151 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4152 return;
4153 } else
4154 D.setEllipsisLoc(EllipsisLoc);
4155
4156 // The ellipsis can't be followed by a parenthesized declarator. We
4157 // check for that in ParseParenDeclarator, after we have disambiguated
4158 // the l_paren token.
4159 }
4160
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004161 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4162 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4163 // We found something that indicates the start of an unqualified-id.
4164 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004165 bool AllowConstructorName;
4166 if (D.getDeclSpec().hasTypeSpecifier())
4167 AllowConstructorName = false;
4168 else if (D.getCXXScopeSpec().isSet())
4169 AllowConstructorName =
4170 (D.getContext() == Declarator::FileContext ||
4171 (D.getContext() == Declarator::MemberContext &&
4172 D.getDeclSpec().isFriendSpecified()));
4173 else
4174 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4175
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004176 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004177 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4178 /*EnteringContext=*/true,
4179 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004180 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004181 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004182 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004183 D.getName()) ||
4184 // Once we're past the identifier, if the scope was bad, mark the
4185 // whole declarator bad.
4186 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004187 D.SetIdentifier(0, Tok.getLocation());
4188 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004189 } else {
4190 // Parsed the unqualified-id; update range information and move along.
4191 if (D.getSourceRange().getBegin().isInvalid())
4192 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4193 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004194 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004195 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004196 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004197 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004198 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004199 "There's a C++-specific check for tok::identifier above");
4200 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4201 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4202 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004203 goto PastIdentifier;
4204 }
Richard Smith9988f282012-03-29 01:16:42 +00004205
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004206 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004207 // direct-declarator: '(' declarator ')'
4208 // direct-declarator: '(' attributes declarator ')'
4209 // Example: 'char (*X)' or 'int (*XX)(void)'
4210 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004211
4212 // If the declarator was parenthesized, we entered the declarator
4213 // scope when parsing the parenthesized declarator, then exited
4214 // the scope already. Re-enter the scope, if we need to.
4215 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004216 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004217 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004218 if (!D.isInvalidType() &&
4219 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004220 // Change the declaration context for name lookup, until this function
4221 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004222 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004223 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004224 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004225 // This could be something simple like "int" (in which case the declarator
4226 // portion is empty), if an abstract-declarator is allowed.
4227 D.SetIdentifier(0, Tok.getLocation());
4228 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00004229 if (D.getContext() == Declarator::MemberContext)
4230 Diag(Tok, diag::err_expected_member_name_or_semi)
4231 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00004232 else if (getLangOpts().CPlusPlus)
4233 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004234 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004235 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004236 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004237 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004238 }
Mike Stump1eb44332009-09-09 15:08:12 +00004239
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004240 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004241 assert(D.isPastIdentifier() &&
4242 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004243
Richard Smith6ee326a2012-04-10 01:32:12 +00004244 // Don't parse attributes unless we have parsed an unparenthesized name.
4245 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004246 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004247
Reid Spencer5f016e22007-07-11 17:01:13 +00004248 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004249 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004250 // Enter function-declaration scope, limiting any declarators to the
4251 // function prototype scope, including parameter declarators.
4252 ParseScope PrototypeScope(this,
4253 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004254 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4255 // In such a case, check if we actually have a function declarator; if it
4256 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00004257 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00004258 // When not in file scope, warn for ambiguous function declarators, just
4259 // in case the author intended it as a variable definition.
4260 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4261 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4262 break;
4263 }
John McCall0b7e6782011-03-24 11:26:52 +00004264 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004265 BalancedDelimiterTracker T(*this, tok::l_paren);
4266 T.consumeOpen();
4267 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004268 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004269 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004270 ParseBracketDeclarator(D);
4271 } else {
4272 break;
4273 }
4274 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004275}
Reid Spencer5f016e22007-07-11 17:01:13 +00004276
Chris Lattneref4715c2008-04-06 05:45:57 +00004277/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4278/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004279/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004280/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4281///
4282/// direct-declarator:
4283/// '(' declarator ')'
4284/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004285/// direct-declarator '(' parameter-type-list ')'
4286/// direct-declarator '(' identifier-list[opt] ')'
4287/// [GNU] direct-declarator '(' parameter-forward-declarations
4288/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004289///
4290void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004291 BalancedDelimiterTracker T(*this, tok::l_paren);
4292 T.consumeOpen();
4293
Chris Lattneref4715c2008-04-06 05:45:57 +00004294 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004295
Chris Lattner7399ee02008-10-20 02:05:46 +00004296 // Eat any attributes before we look at whether this is a grouping or function
4297 // declarator paren. If this is a grouping paren, the attribute applies to
4298 // the type being built up, for example:
4299 // int (__attribute__(()) *x)(long y)
4300 // If this ends up not being a grouping paren, the attribute applies to the
4301 // first argument, for example:
4302 // int (__attribute__(()) int x)
4303 // In either case, we need to eat any attributes to be able to determine what
4304 // sort of paren this is.
4305 //
John McCall0b7e6782011-03-24 11:26:52 +00004306 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004307 bool RequiresArg = false;
4308 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004309 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004310
Chris Lattner7399ee02008-10-20 02:05:46 +00004311 // We require that the argument list (if this is a non-grouping paren) be
4312 // present even if the attribute list was empty.
4313 RequiresArg = true;
4314 }
Steve Naroff239f0732008-12-25 14:16:32 +00004315 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004316 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004317 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004318 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004319 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004320 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004321 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004322 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004323 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004324 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004325
Chris Lattneref4715c2008-04-06 05:45:57 +00004326 // If we haven't past the identifier yet (or where the identifier would be
4327 // stored, if this is an abstract declarator), then this is probably just
4328 // grouping parens. However, if this could be an abstract-declarator, then
4329 // this could also be the start of function arguments (consider 'void()').
4330 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004331
Chris Lattneref4715c2008-04-06 05:45:57 +00004332 if (!D.mayOmitIdentifier()) {
4333 // If this can't be an abstract-declarator, this *must* be a grouping
4334 // paren, because we haven't seen the identifier yet.
4335 isGrouping = true;
4336 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004337 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4338 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004339 isDeclarationSpecifier() || // 'int(int)' is a function.
4340 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004341 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4342 // considered to be a type, not a K&R identifier-list.
4343 isGrouping = false;
4344 } else {
4345 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4346 isGrouping = true;
4347 }
Mike Stump1eb44332009-09-09 15:08:12 +00004348
Chris Lattneref4715c2008-04-06 05:45:57 +00004349 // If this is a grouping paren, handle:
4350 // direct-declarator: '(' declarator ')'
4351 // direct-declarator: '(' attributes declarator ')'
4352 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004353 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4354 D.setEllipsisLoc(SourceLocation());
4355
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004356 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004357 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004358 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004359 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004360 T.consumeClose();
4361 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4362 T.getCloseLocation()),
4363 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004364
4365 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004366
4367 // An ellipsis cannot be placed outside parentheses.
4368 if (EllipsisLoc.isValid())
4369 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4370
Chris Lattneref4715c2008-04-06 05:45:57 +00004371 return;
4372 }
Mike Stump1eb44332009-09-09 15:08:12 +00004373
Chris Lattneref4715c2008-04-06 05:45:57 +00004374 // Okay, if this wasn't a grouping paren, it must be the start of a function
4375 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004376 // identifier (and remember where it would have been), then call into
4377 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004378 D.SetIdentifier(0, Tok.getLocation());
4379
David Blaikie42d6d0c2011-12-04 05:04:18 +00004380 // Enter function-declaration scope, limiting any declarators to the
4381 // function prototype scope, including parameter declarators.
4382 ParseScope PrototypeScope(this,
4383 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004384 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004385 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004386}
4387
4388/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4389/// declarator D up to a paren, which indicates that we are parsing function
4390/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004391///
Richard Smith6ee326a2012-04-10 01:32:12 +00004392/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4393/// immediately after the open paren - they should be considered to be the
4394/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004395///
Richard Smith6ee326a2012-04-10 01:32:12 +00004396/// If RequiresArg is true, then the first argument of the function is required
4397/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004398///
Richard Smith6ee326a2012-04-10 01:32:12 +00004399/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4400/// (C++11) ref-qualifier[opt], exception-specification[opt],
4401/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4402///
4403/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004404/// dynamic-exception-specification
4405/// noexcept-specification
4406///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004407void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004408 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004409 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004410 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004411 assert(getCurScope()->isFunctionPrototypeScope() &&
4412 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004413 // lparen is already consumed!
4414 assert(D.isPastIdentifier() && "Should not call before identifier!");
4415
4416 // This should be true when the function has typed arguments.
4417 // Otherwise, it is treated as a K&R-style function.
4418 bool HasProto = false;
4419 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004420 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004421 // Remember where we see an ellipsis, if any.
4422 SourceLocation EllipsisLoc;
4423
4424 DeclSpec DS(AttrFactory);
4425 bool RefQualifierIsLValueRef = true;
4426 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004427 SourceLocation ConstQualifierLoc;
4428 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004429 ExceptionSpecificationType ESpecType = EST_None;
4430 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004431 SmallVector<ParsedType, 2> DynamicExceptions;
4432 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004433 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004434 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004435 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004436
James Molloy16f1f712012-02-29 10:24:19 +00004437 Actions.ActOnStartFunctionDeclarator();
4438
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004439 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004440 if (isFunctionDeclaratorIdentifierList()) {
4441 if (RequiresArg)
4442 Diag(Tok, diag::err_argument_required_after_attribute);
4443
4444 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4445
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004446 Tracker.consumeClose();
4447 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004448 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004449 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004450 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004451 else if (RequiresArg)
4452 Diag(Tok, diag::err_argument_required_after_attribute);
4453
David Blaikie4e4d0842012-03-11 07:00:24 +00004454 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004455
4456 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004457 Tracker.consumeClose();
4458 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004459
David Blaikie4e4d0842012-03-11 07:00:24 +00004460 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004461 // FIXME: Accept these components in any order, and produce fixits to
4462 // correct the order if the user gets it wrong. Ideally we should deal
4463 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004464
4465 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004466 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4467 if (!DS.getSourceRange().getEnd().isInvalid()) {
4468 EndLoc = DS.getSourceRange().getEnd();
4469 ConstQualifierLoc = DS.getConstSpecLoc();
4470 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4471 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004472
4473 // Parse ref-qualifier[opt].
4474 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004475 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004476 diag::warn_cxx98_compat_ref_qualifier :
4477 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004478
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004479 RefQualifierIsLValueRef = Tok.is(tok::amp);
4480 RefQualifierLoc = ConsumeToken();
4481 EndLoc = RefQualifierLoc;
4482 }
4483
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004484 // C++11 [expr.prim.general]p3:
4485 // If a declaration declares a member function or member function
4486 // template of a class X, the expression this is a prvalue of type
4487 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
4488 // and the end of the function-definition, member-declarator, or
4489 // declarator.
4490 bool IsCXX11MemberFunction =
4491 getLangOpts().CPlusPlus0x &&
4492 (D.getContext() == Declarator::MemberContext ||
4493 (D.getContext() == Declarator::FileContext &&
4494 D.getCXXScopeSpec().isValid() &&
4495 Actions.CurContext->isRecord()));
4496 Sema::CXXThisScopeRAII ThisScope(Actions,
4497 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4498 DS.getTypeQualifiers(),
4499 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004500
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004501 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004502 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004503 DynamicExceptions,
4504 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004505 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004506 if (ESpecType != EST_None)
4507 EndLoc = ESpecRange.getEnd();
4508
Richard Smith6ee326a2012-04-10 01:32:12 +00004509 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4510 // after the exception-specification.
4511 MaybeParseCXX0XAttributes(FnAttrs);
4512
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004513 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004514 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004515 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004516 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004517 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004518 if (Range.getEnd().isValid())
4519 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004520 }
4521 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004522 }
4523
4524 // Remember that we parsed a function type, and remember the attributes.
4525 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4526 /*isVariadic=*/EllipsisLoc.isValid(),
4527 EllipsisLoc,
4528 ParamInfo.data(), ParamInfo.size(),
4529 DS.getTypeQualifiers(),
4530 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004531 RefQualifierLoc, ConstQualifierLoc,
4532 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004533 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004534 ESpecType, ESpecRange.getBegin(),
4535 DynamicExceptions.data(),
4536 DynamicExceptionRanges.data(),
4537 DynamicExceptions.size(),
4538 NoexceptExpr.isUsable() ?
4539 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004540 Tracker.getOpenLocation(),
4541 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004542 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004543 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004544
4545 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004546}
4547
4548/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4549/// identifier list form for a K&R-style function: void foo(a,b,c)
4550///
4551/// Note that identifier-lists are only allowed for normal declarators, not for
4552/// abstract-declarators.
4553bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004554 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004555 && Tok.is(tok::identifier)
4556 && !TryAltiVecVectorToken()
4557 // K&R identifier lists can't have typedefs as identifiers, per C99
4558 // 6.7.5.3p11.
4559 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4560 // Identifier lists follow a really simple grammar: the identifiers can
4561 // be followed *only* by a ", identifier" or ")". However, K&R
4562 // identifier lists are really rare in the brave new modern world, and
4563 // it is very common for someone to typo a type in a non-K&R style
4564 // list. If we are presented with something like: "void foo(intptr x,
4565 // float y)", we don't want to start parsing the function declarator as
4566 // though it is a K&R style declarator just because intptr is an
4567 // invalid type.
4568 //
4569 // To handle this, we check to see if the token after the first
4570 // identifier is a "," or ")". Only then do we parse it as an
4571 // identifier list.
4572 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4573}
4574
4575/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4576/// we found a K&R-style identifier list instead of a typed parameter list.
4577///
4578/// After returning, ParamInfo will hold the parsed parameters.
4579///
4580/// identifier-list: [C99 6.7.5]
4581/// identifier
4582/// identifier-list ',' identifier
4583///
4584void Parser::ParseFunctionDeclaratorIdentifierList(
4585 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004586 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004587 // If there was no identifier specified for the declarator, either we are in
4588 // an abstract-declarator, or we are in a parameter declarator which was found
4589 // to be abstract. In abstract-declarators, identifier lists are not valid:
4590 // diagnose this.
4591 if (!D.getIdentifier())
4592 Diag(Tok, diag::ext_ident_list_in_param);
4593
4594 // Maintain an efficient lookup of params we have seen so far.
4595 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4596
4597 while (1) {
4598 // If this isn't an identifier, report the error and skip until ')'.
4599 if (Tok.isNot(tok::identifier)) {
4600 Diag(Tok, diag::err_expected_ident);
4601 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4602 // Forget we parsed anything.
4603 ParamInfo.clear();
4604 return;
4605 }
4606
4607 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4608
4609 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4610 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4611 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4612
4613 // Verify that the argument identifier has not already been mentioned.
4614 if (!ParamsSoFar.insert(ParmII)) {
4615 Diag(Tok, diag::err_param_redefinition) << ParmII;
4616 } else {
4617 // Remember this identifier in ParamInfo.
4618 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4619 Tok.getLocation(),
4620 0));
4621 }
4622
4623 // Eat the identifier.
4624 ConsumeToken();
4625
4626 // The list continues if we see a comma.
4627 if (Tok.isNot(tok::comma))
4628 break;
4629 ConsumeToken();
4630 }
4631}
4632
4633/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4634/// after the opening parenthesis. This function will not parse a K&R-style
4635/// identifier list.
4636///
Richard Smith6ce48a72012-04-11 04:01:28 +00004637/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4638/// caller parsed those arguments immediately after the open paren - they should
4639/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004640///
4641/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4642/// be the location of the ellipsis, if any was parsed.
4643///
Reid Spencer5f016e22007-07-11 17:01:13 +00004644/// parameter-type-list: [C99 6.7.5]
4645/// parameter-list
4646/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004647/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004648///
4649/// parameter-list: [C99 6.7.5]
4650/// parameter-declaration
4651/// parameter-list ',' parameter-declaration
4652///
4653/// parameter-declaration: [C99 6.7.5]
4654/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004655/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004656/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004657/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004658/// declaration-specifiers abstract-declarator[opt]
4659/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004660/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004661/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004662/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004663///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004664void Parser::ParseParameterDeclarationClause(
4665 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004666 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004667 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004668 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004669
Chris Lattnerf97409f2008-04-06 06:57:35 +00004670 while (1) {
4671 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004672 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4673 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004674 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004675 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004676 }
Mike Stump1eb44332009-09-09 15:08:12 +00004677
Chris Lattnerf97409f2008-04-06 06:57:35 +00004678 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004679 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004680 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004681
Richard Smith6ce48a72012-04-11 04:01:28 +00004682 // Parse any C++11 attributes.
4683 MaybeParseCXX0XAttributes(DS.getAttributes());
4684
John McCall7f040a92010-12-24 02:08:15 +00004685 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004686 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004687 ParseMicrosoftAttributes(DS.getAttributes());
4688
4689 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004690
4691 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004692 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004693 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004694 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4695 // too much hassle.
4696 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004697
Chris Lattnere64c5492009-02-27 18:38:20 +00004698 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004699
Chris Lattnerf97409f2008-04-06 06:57:35 +00004700 // Parse the declarator. This is "PrototypeContext", because we must
4701 // accept either 'declarator' or 'abstract-declarator' here.
4702 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4703 ParseDeclarator(ParmDecl);
4704
4705 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004706 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004707
Chris Lattnerf97409f2008-04-06 06:57:35 +00004708 // Remember this parsed parameter in ParamInfo.
4709 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004710
Douglas Gregor72b505b2008-12-16 21:30:33 +00004711 // DefArgToks is used when the parsing of default arguments needs
4712 // to be delayed.
4713 CachedTokens *DefArgToks = 0;
4714
Chris Lattnerf97409f2008-04-06 06:57:35 +00004715 // If no parameter was specified, verify that *something* was specified,
4716 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004717 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4718 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004719 // Completely missing, emit error.
4720 Diag(DSStart, diag::err_missing_param);
4721 } else {
4722 // Otherwise, we have something. Add it and let semantic analysis try
4723 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004724
Chris Lattnerf97409f2008-04-06 06:57:35 +00004725 // Inform the actions module about the parameter declarator, so it gets
4726 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004727 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004728
4729 // Parse the default argument, if any. We parse the default
4730 // arguments in all dialects; the semantic analysis in
4731 // ActOnParamDefaultArgument will reject the default argument in
4732 // C.
4733 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004734 SourceLocation EqualLoc = Tok.getLocation();
4735
Chris Lattner04421082008-04-08 04:40:51 +00004736 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004737 if (D.getContext() == Declarator::MemberContext) {
4738 // If we're inside a class definition, cache the tokens
4739 // corresponding to the default argument. We'll actually parse
4740 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00004741 // FIXME: Can we use a smart pointer for Toks?
4742 DefArgToks = new CachedTokens;
4743
Mike Stump1eb44332009-09-09 15:08:12 +00004744 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004745 /*StopAtSemi=*/true,
4746 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004747 delete DefArgToks;
4748 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004749 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004750 } else {
4751 // Mark the end of the default argument so that we know when to
4752 // stop when we parse it later on.
4753 Token DefArgEnd;
4754 DefArgEnd.startToken();
4755 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4756 DefArgEnd.setLocation(Tok.getLocation());
4757 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004758 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004759 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004760 }
Chris Lattner04421082008-04-08 04:40:51 +00004761 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004762 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004763 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004764
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004765 // The argument isn't actually potentially evaluated unless it is
4766 // used.
4767 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004768 Sema::PotentiallyEvaluatedIfUsed,
4769 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004770
Sebastian Redl84407ba2012-03-14 15:54:00 +00004771 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004772 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4773 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004774 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004775 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004776 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004777 if (DefArgResult.isInvalid()) {
4778 Actions.ActOnParamDefaultArgumentError(Param);
4779 SkipUntil(tok::comma, tok::r_paren, true, true);
4780 } else {
4781 // Inform the actions module about the default argument
4782 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004783 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004784 }
Chris Lattner04421082008-04-08 04:40:51 +00004785 }
4786 }
Mike Stump1eb44332009-09-09 15:08:12 +00004787
4788 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4789 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004790 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004791 }
4792
4793 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004794 if (Tok.isNot(tok::comma)) {
4795 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004796 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4797
David Blaikie4e4d0842012-03-11 07:00:24 +00004798 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004799 // We have ellipsis without a preceding ',', which is ill-formed
4800 // in C. Complain and provide the fix.
4801 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004802 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004803 }
4804 }
4805
4806 break;
4807 }
Mike Stump1eb44332009-09-09 15:08:12 +00004808
Chris Lattnerf97409f2008-04-06 06:57:35 +00004809 // Consume the comma.
4810 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004811 }
Mike Stump1eb44332009-09-09 15:08:12 +00004812
Chris Lattner66d28652008-04-06 06:34:08 +00004813}
Chris Lattneref4715c2008-04-06 05:45:57 +00004814
Reid Spencer5f016e22007-07-11 17:01:13 +00004815/// [C90] direct-declarator '[' constant-expression[opt] ']'
4816/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4817/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4818/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4819/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004820/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4821/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004822void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004823 if (CheckProhibitedCXX11Attribute())
4824 return;
4825
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004826 BalancedDelimiterTracker T(*this, tok::l_square);
4827 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004828
Chris Lattner378c7e42008-12-18 07:27:21 +00004829 // C array syntax has many features, but by-far the most common is [] and [4].
4830 // This code does a fast path to handle some of the most obvious cases.
4831 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004832 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004833 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004834 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004835
Chris Lattner378c7e42008-12-18 07:27:21 +00004836 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004837 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004838 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004839 T.getOpenLocation(),
4840 T.getCloseLocation()),
4841 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004842 return;
4843 } else if (Tok.getKind() == tok::numeric_constant &&
4844 GetLookAheadToken(1).is(tok::r_square)) {
4845 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004846 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004847 ConsumeToken();
4848
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004849 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004850 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004851 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004852
Chris Lattner378c7e42008-12-18 07:27:21 +00004853 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004854 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004855 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004856 T.getOpenLocation(),
4857 T.getCloseLocation()),
4858 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004859 return;
4860 }
Mike Stump1eb44332009-09-09 15:08:12 +00004861
Reid Spencer5f016e22007-07-11 17:01:13 +00004862 // If valid, this location is the position where we read the 'static' keyword.
4863 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004864 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004865 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004866
Reid Spencer5f016e22007-07-11 17:01:13 +00004867 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004868 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004869 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004870 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004871
Reid Spencer5f016e22007-07-11 17:01:13 +00004872 // If we haven't already read 'static', check to see if there is one after the
4873 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004874 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004875 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004876
Reid Spencer5f016e22007-07-11 17:01:13 +00004877 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4878 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004879 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004880
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004881 // Handle the case where we have '[*]' as the array size. However, a leading
4882 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4883 // the the token after the star is a ']'. Since stars in arrays are
4884 // infrequent, use of lookahead is not costly here.
4885 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004886 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004887
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004888 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004889 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004890 StaticLoc = SourceLocation(); // Drop the static.
4891 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004892 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004893 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004894 // Note, in C89, this production uses the constant-expr production instead
4895 // of assignment-expr. The only difference is that assignment-expr allows
4896 // things like '=' and '*='. Sema rejects these in C89 mode because they
4897 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004898
Douglas Gregore0762c92009-06-19 23:52:42 +00004899 // Parse the constant-expression or assignment-expression now (depending
4900 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004901 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004902 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004903 } else {
4904 EnterExpressionEvaluationContext Unevaluated(Actions,
4905 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004906 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004907 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004908 }
Mike Stump1eb44332009-09-09 15:08:12 +00004909
Reid Spencer5f016e22007-07-11 17:01:13 +00004910 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004911 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004912 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004913 // If the expression was invalid, skip it.
4914 SkipUntil(tok::r_square);
4915 return;
4916 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004917
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004918 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004919
John McCall0b7e6782011-03-24 11:26:52 +00004920 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004921 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004922
Chris Lattner378c7e42008-12-18 07:27:21 +00004923 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004924 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004925 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004926 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004927 T.getOpenLocation(),
4928 T.getCloseLocation()),
4929 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004930}
4931
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004932/// [GNU] typeof-specifier:
4933/// typeof ( expressions )
4934/// typeof ( type-name )
4935/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004936///
4937void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004938 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004939 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004940 SourceLocation StartLoc = ConsumeToken();
4941
John McCallcfb708c2010-01-13 20:03:27 +00004942 const bool hasParens = Tok.is(tok::l_paren);
4943
Eli Friedman71b8fb52012-01-21 01:01:51 +00004944 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4945
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004946 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004947 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004948 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004949 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4950 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004951 if (hasParens)
4952 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004953
4954 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004955 // FIXME: Not accurate, the range gets one token more than it should.
4956 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004957 else
4958 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004959
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004960 if (isCastExpr) {
4961 if (!CastTy) {
4962 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004963 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004964 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004965
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004966 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004967 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004968 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4969 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004970 DiagID, CastTy))
4971 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004972 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004973 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004974
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004975 // If we get here, the operand to the typeof was an expresion.
4976 if (Operand.isInvalid()) {
4977 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004978 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004979 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004980
Eli Friedman71b8fb52012-01-21 01:01:51 +00004981 // We might need to transform the operand if it is potentially evaluated.
4982 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4983 if (Operand.isInvalid()) {
4984 DS.SetTypeSpecError();
4985 return;
4986 }
4987
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004988 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004989 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004990 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4991 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004992 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004993 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004994}
Chris Lattner1b492422010-02-28 18:33:55 +00004995
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004996/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004997/// _Atomic ( type-name )
4998///
4999void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
5000 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
5001
5002 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005003 BalancedDelimiterTracker T(*this, tok::l_paren);
5004 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00005005 SkipUntil(tok::r_paren);
5006 return;
5007 }
5008
5009 TypeResult Result = ParseTypeName();
5010 if (Result.isInvalid()) {
5011 SkipUntil(tok::r_paren);
5012 return;
5013 }
5014
5015 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005016 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00005017
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005018 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00005019 return;
5020
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00005021 DS.setTypeofParensRange(T.getRange());
5022 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00005023
5024 const char *PrevSpec = 0;
5025 unsigned DiagID;
5026 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
5027 DiagID, Result.release()))
5028 Diag(StartLoc, DiagID) << PrevSpec;
5029}
5030
Chris Lattner1b492422010-02-28 18:33:55 +00005031
5032/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5033/// from TryAltiVecVectorToken.
5034bool Parser::TryAltiVecVectorTokenOutOfLine() {
5035 Token Next = NextToken();
5036 switch (Next.getKind()) {
5037 default: return false;
5038 case tok::kw_short:
5039 case tok::kw_long:
5040 case tok::kw_signed:
5041 case tok::kw_unsigned:
5042 case tok::kw_void:
5043 case tok::kw_char:
5044 case tok::kw_int:
5045 case tok::kw_float:
5046 case tok::kw_double:
5047 case tok::kw_bool:
5048 case tok::kw___pixel:
5049 Tok.setKind(tok::kw___vector);
5050 return true;
5051 case tok::identifier:
5052 if (Next.getIdentifierInfo() == Ident_pixel) {
5053 Tok.setKind(tok::kw___vector);
5054 return true;
5055 }
5056 return false;
5057 }
5058}
5059
5060bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5061 const char *&PrevSpec, unsigned &DiagID,
5062 bool &isInvalid) {
5063 if (Tok.getIdentifierInfo() == Ident_vector) {
5064 Token Next = NextToken();
5065 switch (Next.getKind()) {
5066 case tok::kw_short:
5067 case tok::kw_long:
5068 case tok::kw_signed:
5069 case tok::kw_unsigned:
5070 case tok::kw_void:
5071 case tok::kw_char:
5072 case tok::kw_int:
5073 case tok::kw_float:
5074 case tok::kw_double:
5075 case tok::kw_bool:
5076 case tok::kw___pixel:
5077 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5078 return true;
5079 case tok::identifier:
5080 if (Next.getIdentifierInfo() == Ident_pixel) {
5081 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5082 return true;
5083 }
5084 break;
5085 default:
5086 break;
5087 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005088 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005089 DS.isTypeAltiVecVector()) {
5090 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5091 return true;
5092 }
5093 return false;
5094}