blob: 3c1c7e213fff1c2e46dbce1b7623ea5263d1843d [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] ';'
1129///[C90/C++]init-declarator-list ';' [TODO]
1130/// [OMP] threadprivate-directive [TODO]
Chris Lattnercd147752009-03-29 17:27:48 +00001131///
Richard Smithad762fc2011-04-14 22:09:26 +00001132/// for-range-declaration: [C++0x 6.5p1: stmt.ranged]
1133/// attribute-specifier-seq[opt] type-specifier-seq declarator
1134///
Chris Lattnercd147752009-03-29 17:27:48 +00001135/// If RequireSemi is false, this does not check for a ';' at the end of the
Chris Lattner5c5db552010-04-05 18:18:31 +00001136/// declaration. If it is true, it checks for and eats it.
Richard Smithad762fc2011-04-14 22:09:26 +00001137///
1138/// If FRI is non-null, we might be parsing a for-range-declaration instead
1139/// of a simple-declaration. If we find that we are, we also parse the
1140/// for-range-initializer, and place it here.
Fariborz Jahanianc5be7b02010-09-28 20:42:35 +00001141Parser::DeclGroupPtrTy Parser::ParseSimpleDeclaration(StmtVector &Stmts,
1142 unsigned Context,
Sean Huntbbd37c62009-11-21 08:43:09 +00001143 SourceLocation &DeclEnd,
John McCall7f040a92010-12-24 02:08:15 +00001144 ParsedAttributes &attrs,
Richard Smithad762fc2011-04-14 22:09:26 +00001145 bool RequireSemi,
1146 ForRangeInit *FRI) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001147 // Parse the common declaration-specifiers piece.
John McCall54abf7d2009-11-04 02:18:39 +00001148 ParsingDeclSpec DS(*this);
John McCall7f040a92010-12-24 02:08:15 +00001149 DS.takeAttributesFrom(attrs);
Douglas Gregor312eadb2011-04-24 05:37:28 +00001150
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001151 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS_none,
Richard Smith34b41d92011-02-20 03:19:35 +00001152 getDeclSpecContextFromDeclaratorContext(Context));
Abramo Bagnara06284c12012-01-07 10:52:36 +00001153
Reid Spencer5f016e22007-07-11 17:01:13 +00001154 // C99 6.7.2.3p6: Handle "struct-or-union identifier;", "enum { X };"
1155 // declaration-specifiers init-declarator-list[opt] ';'
Chris Lattner04d66662007-10-09 17:33:22 +00001156 if (Tok.is(tok::semi)) {
Argyrios Kyrtzidis5641b0d2012-05-16 23:49:15 +00001157 DeclEnd = Tok.getLocation();
Chris Lattner5c5db552010-04-05 18:18:31 +00001158 if (RequireSemi) ConsumeToken();
John McCalld226f652010-08-21 09:40:31 +00001159 Decl *TheDecl = Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none,
Douglas Gregor312eadb2011-04-24 05:37:28 +00001160 DS);
John McCall54abf7d2009-11-04 02:18:39 +00001161 DS.complete(TheDecl);
Chris Lattner682bf922009-03-29 16:50:03 +00001162 return Actions.ConvertDeclToDeclGroup(TheDecl);
Reid Spencer5f016e22007-07-11 17:01:13 +00001163 }
Douglas Gregor312eadb2011-04-24 05:37:28 +00001164
1165 return ParseDeclGroup(DS, Context, /*FunctionDefs=*/ false, &DeclEnd, FRI);
John McCalld8ac0572009-11-03 19:26:08 +00001166}
Mike Stump1eb44332009-09-09 15:08:12 +00001167
Richard Smith0706df42011-10-19 21:33:05 +00001168/// Returns true if this might be the start of a declarator, or a common typo
1169/// for a declarator.
1170bool Parser::MightBeDeclarator(unsigned Context) {
1171 switch (Tok.getKind()) {
1172 case tok::annot_cxxscope:
1173 case tok::annot_template_id:
1174 case tok::caret:
1175 case tok::code_completion:
1176 case tok::coloncolon:
1177 case tok::ellipsis:
1178 case tok::kw___attribute:
1179 case tok::kw_operator:
1180 case tok::l_paren:
1181 case tok::star:
1182 return true;
1183
1184 case tok::amp:
1185 case tok::ampamp:
David Blaikie4e4d0842012-03-11 07:00:24 +00001186 return getLangOpts().CPlusPlus;
Richard Smith0706df42011-10-19 21:33:05 +00001187
Richard Smith1c94c162012-01-09 22:31:44 +00001188 case tok::l_square: // Might be an attribute on an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001189 return Context == Declarator::MemberContext && getLangOpts().CPlusPlus0x &&
Richard Smith1c94c162012-01-09 22:31:44 +00001190 NextToken().is(tok::l_square);
1191
1192 case tok::colon: // Might be a typo for '::' or an unnamed bit-field.
David Blaikie4e4d0842012-03-11 07:00:24 +00001193 return Context == Declarator::MemberContext || getLangOpts().CPlusPlus;
Richard Smith1c94c162012-01-09 22:31:44 +00001194
Richard Smith0706df42011-10-19 21:33:05 +00001195 case tok::identifier:
1196 switch (NextToken().getKind()) {
1197 case tok::code_completion:
1198 case tok::coloncolon:
1199 case tok::comma:
1200 case tok::equal:
1201 case tok::equalequal: // Might be a typo for '='.
1202 case tok::kw_alignas:
1203 case tok::kw_asm:
1204 case tok::kw___attribute:
1205 case tok::l_brace:
1206 case tok::l_paren:
1207 case tok::l_square:
1208 case tok::less:
1209 case tok::r_brace:
1210 case tok::r_paren:
1211 case tok::r_square:
1212 case tok::semi:
1213 return true;
1214
1215 case tok::colon:
1216 // At namespace scope, 'identifier:' is probably a typo for 'identifier::'
Richard Smith1c94c162012-01-09 22:31:44 +00001217 // and in block scope it's probably a label. Inside a class definition,
1218 // this is a bit-field.
1219 return Context == Declarator::MemberContext ||
David Blaikie4e4d0842012-03-11 07:00:24 +00001220 (getLangOpts().CPlusPlus && Context == Declarator::FileContext);
Richard Smith1c94c162012-01-09 22:31:44 +00001221
1222 case tok::identifier: // Possible virt-specifier.
David Blaikie4e4d0842012-03-11 07:00:24 +00001223 return getLangOpts().CPlusPlus0x && isCXX0XVirtSpecifier(NextToken());
Richard Smith0706df42011-10-19 21:33:05 +00001224
1225 default:
1226 return false;
1227 }
1228
1229 default:
1230 return false;
1231 }
1232}
1233
Richard Smith994d73f2012-04-11 20:59:20 +00001234/// Skip until we reach something which seems like a sensible place to pick
1235/// up parsing after a malformed declaration. This will sometimes stop sooner
1236/// than SkipUntil(tok::r_brace) would, but will never stop later.
1237void Parser::SkipMalformedDecl() {
1238 while (true) {
1239 switch (Tok.getKind()) {
1240 case tok::l_brace:
1241 // Skip until matching }, then stop. We've probably skipped over
1242 // a malformed class or function definition or similar.
1243 ConsumeBrace();
1244 SkipUntil(tok::r_brace, /*StopAtSemi*/false);
1245 if (Tok.is(tok::comma) || Tok.is(tok::l_brace) || Tok.is(tok::kw_try)) {
1246 // This declaration isn't over yet. Keep skipping.
1247 continue;
1248 }
1249 if (Tok.is(tok::semi))
1250 ConsumeToken();
1251 return;
1252
1253 case tok::l_square:
1254 ConsumeBracket();
1255 SkipUntil(tok::r_square, /*StopAtSemi*/false);
1256 continue;
1257
1258 case tok::l_paren:
1259 ConsumeParen();
1260 SkipUntil(tok::r_paren, /*StopAtSemi*/false);
1261 continue;
1262
1263 case tok::r_brace:
1264 return;
1265
1266 case tok::semi:
1267 ConsumeToken();
1268 return;
1269
1270 case tok::kw_inline:
1271 // 'inline namespace' at the start of a line is almost certainly
1272 // a good place to pick back up parsing.
1273 if (Tok.isAtStartOfLine() && NextToken().is(tok::kw_namespace))
1274 return;
1275 break;
1276
1277 case tok::kw_namespace:
1278 // 'namespace' at the start of a line is almost certainly a good
1279 // place to pick back up parsing.
1280 if (Tok.isAtStartOfLine())
1281 return;
1282 break;
1283
1284 case tok::eof:
1285 return;
1286
1287 default:
1288 break;
1289 }
1290
1291 ConsumeAnyToken();
1292 }
1293}
1294
John McCalld8ac0572009-11-03 19:26:08 +00001295/// ParseDeclGroup - Having concluded that this is either a function
1296/// definition or a group of object declarations, actually parse the
1297/// result.
John McCall54abf7d2009-11-04 02:18:39 +00001298Parser::DeclGroupPtrTy Parser::ParseDeclGroup(ParsingDeclSpec &DS,
1299 unsigned Context,
John McCalld8ac0572009-11-03 19:26:08 +00001300 bool AllowFunctionDefinitions,
Richard Smithad762fc2011-04-14 22:09:26 +00001301 SourceLocation *DeclEnd,
1302 ForRangeInit *FRI) {
John McCalld8ac0572009-11-03 19:26:08 +00001303 // Parse the first declarator.
John McCall54abf7d2009-11-04 02:18:39 +00001304 ParsingDeclarator D(*this, DS, static_cast<Declarator::TheContext>(Context));
John McCalld8ac0572009-11-03 19:26:08 +00001305 ParseDeclarator(D);
Chris Lattnercd147752009-03-29 17:27:48 +00001306
John McCalld8ac0572009-11-03 19:26:08 +00001307 // Bail out if the first declarator didn't seem well-formed.
1308 if (!D.hasName() && !D.mayOmitIdentifier()) {
Richard Smith994d73f2012-04-11 20:59:20 +00001309 SkipMalformedDecl();
John McCalld8ac0572009-11-03 19:26:08 +00001310 return DeclGroupPtrTy();
Chris Lattner23c4b182009-03-29 17:18:04 +00001311 }
Mike Stump1eb44332009-09-09 15:08:12 +00001312
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001313 // Save late-parsed attributes for now; they need to be parsed in the
1314 // appropriate function scope after the function Decl has been constructed.
1315 LateParsedAttrList LateParsedAttrs;
1316 if (D.isFunctionDeclarator())
1317 MaybeParseGNUAttributes(D, &LateParsedAttrs);
1318
Chris Lattnerc82daef2010-07-11 22:24:20 +00001319 // Check to see if we have a function *definition* which must have a body.
1320 if (AllowFunctionDefinitions && D.isFunctionDeclarator() &&
1321 // Look at the next token to make sure that this isn't a function
1322 // declaration. We have to check this because __attribute__ might be the
1323 // start of a function definition in GCC-extended K&R C.
1324 !isDeclarationAfterDeclarator()) {
Richard Smith58196dc2011-11-30 23:45:35 +00001325
Chris Lattner004659a2010-07-11 22:42:07 +00001326 if (isStartOfFunctionDefinition(D)) {
John McCalld8ac0572009-11-03 19:26:08 +00001327 if (DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
1328 Diag(Tok, diag::err_function_declared_typedef);
1329
1330 // Recover by treating the 'typedef' as spurious.
1331 DS.ClearStorageClassSpecs();
1332 }
1333
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001334 Decl *TheDecl =
1335 ParseFunctionDefinition(D, ParsedTemplateInfo(), &LateParsedAttrs);
John McCalld8ac0572009-11-03 19:26:08 +00001336 return Actions.ConvertDeclToDeclGroup(TheDecl);
Chris Lattner004659a2010-07-11 22:42:07 +00001337 }
1338
1339 if (isDeclarationSpecifier()) {
1340 // If there is an invalid declaration specifier right after the function
1341 // prototype, then we must be in a missing semicolon case where this isn't
1342 // actually a body. Just fall through into the code that handles it as a
1343 // prototype, and let the top-level code handle the erroneous declspec
1344 // where it would otherwise expect a comma or semicolon.
John McCalld8ac0572009-11-03 19:26:08 +00001345 } else {
1346 Diag(Tok, diag::err_expected_fn_body);
1347 SkipUntil(tok::semi);
1348 return DeclGroupPtrTy();
1349 }
1350 }
1351
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001352 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001353 return DeclGroupPtrTy();
1354
1355 // C++0x [stmt.iter]p1: Check if we have a for-range-declarator. If so, we
1356 // must parse and analyze the for-range-initializer before the declaration is
1357 // analyzed.
1358 if (FRI && Tok.is(tok::colon)) {
1359 FRI->ColonLoc = ConsumeToken();
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001360 if (Tok.is(tok::l_brace))
1361 FRI->RangeExpr = ParseBraceInitializer();
1362 else
1363 FRI->RangeExpr = ParseExpression();
Richard Smithad762fc2011-04-14 22:09:26 +00001364 Decl *ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
1365 Actions.ActOnCXXForRangeDecl(ThisDecl);
1366 Actions.FinalizeDeclaration(ThisDecl);
John McCall6895a642012-01-27 01:29:43 +00001367 D.complete(ThisDecl);
Richard Smithad762fc2011-04-14 22:09:26 +00001368 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS, &ThisDecl, 1);
1369 }
1370
Chris Lattner5f9e2722011-07-23 10:55:15 +00001371 SmallVector<Decl *, 8> DeclsInGroup;
Richard Smithad762fc2011-04-14 22:09:26 +00001372 Decl *FirstDecl = ParseDeclarationAfterDeclaratorAndAttributes(D);
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001373 if (LateParsedAttrs.size() > 0)
1374 ParseLexedAttributeList(LateParsedAttrs, FirstDecl, true, false);
John McCall54abf7d2009-11-04 02:18:39 +00001375 D.complete(FirstDecl);
John McCalld226f652010-08-21 09:40:31 +00001376 if (FirstDecl)
John McCalld8ac0572009-11-03 19:26:08 +00001377 DeclsInGroup.push_back(FirstDecl);
1378
Richard Smith0706df42011-10-19 21:33:05 +00001379 bool ExpectSemi = Context != Declarator::ForContext;
1380
John McCalld8ac0572009-11-03 19:26:08 +00001381 // If we don't have a comma, it is either the end of the list (a ';') or an
1382 // error, bail out.
1383 while (Tok.is(tok::comma)) {
Richard Smith0706df42011-10-19 21:33:05 +00001384 SourceLocation CommaLoc = ConsumeToken();
1385
1386 if (Tok.isAtStartOfLine() && ExpectSemi && !MightBeDeclarator(Context)) {
1387 // This comma was followed by a line-break and something which can't be
1388 // the start of a declarator. The comma was probably a typo for a
1389 // semicolon.
1390 Diag(CommaLoc, diag::err_expected_semi_declaration)
1391 << FixItHint::CreateReplacement(CommaLoc, ";");
1392 ExpectSemi = false;
1393 break;
1394 }
John McCalld8ac0572009-11-03 19:26:08 +00001395
1396 // Parse the next declarator.
1397 D.clear();
Richard Smith7984de32012-01-12 23:53:29 +00001398 D.setCommaLoc(CommaLoc);
John McCalld8ac0572009-11-03 19:26:08 +00001399
1400 // Accept attributes in an init-declarator. In the first declarator in a
1401 // declaration, these would be part of the declspec. In subsequent
1402 // declarators, they become part of the declarator itself, so that they
1403 // don't apply to declarators after *this* one. Examples:
1404 // short __attribute__((common)) var; -> declspec
1405 // short var __attribute__((common)); -> declarator
1406 // short x, __attribute__((common)) var; -> declarator
John McCall7f040a92010-12-24 02:08:15 +00001407 MaybeParseGNUAttributes(D);
John McCalld8ac0572009-11-03 19:26:08 +00001408
1409 ParseDeclarator(D);
Fariborz Jahanian9baf39d2012-01-13 00:14:12 +00001410 if (!D.isInvalidType()) {
1411 Decl *ThisDecl = ParseDeclarationAfterDeclarator(D);
1412 D.complete(ThisDecl);
1413 if (ThisDecl)
1414 DeclsInGroup.push_back(ThisDecl);
1415 }
John McCalld8ac0572009-11-03 19:26:08 +00001416 }
1417
1418 if (DeclEnd)
1419 *DeclEnd = Tok.getLocation();
1420
Richard Smith0706df42011-10-19 21:33:05 +00001421 if (ExpectSemi &&
Chris Lattner8bb21d32012-04-28 16:12:17 +00001422 ExpectAndConsumeSemi(Context == Declarator::FileContext
1423 ? diag::err_invalid_token_after_toplevel_declarator
1424 : diag::err_expected_semi_declaration)) {
Chris Lattner004659a2010-07-11 22:42:07 +00001425 // Okay, there was no semicolon and one was expected. If we see a
1426 // declaration specifier, just assume it was missing and continue parsing.
1427 // Otherwise things are very confused and we skip to recover.
1428 if (!isDeclarationSpecifier()) {
1429 SkipUntil(tok::r_brace, true, true);
1430 if (Tok.is(tok::semi))
1431 ConsumeToken();
1432 }
John McCalld8ac0572009-11-03 19:26:08 +00001433 }
1434
Douglas Gregor23c94db2010-07-02 17:43:08 +00001435 return Actions.FinalizeDeclaratorGroup(getCurScope(), DS,
John McCalld8ac0572009-11-03 19:26:08 +00001436 DeclsInGroup.data(),
1437 DeclsInGroup.size());
Reid Spencer5f016e22007-07-11 17:01:13 +00001438}
1439
Richard Smithad762fc2011-04-14 22:09:26 +00001440/// Parse an optional simple-asm-expr and attributes, and attach them to a
1441/// declarator. Returns true on an error.
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001442bool Parser::ParseAsmAttributesAfterDeclarator(Declarator &D) {
Richard Smithad762fc2011-04-14 22:09:26 +00001443 // If a simple-asm-expr is present, parse it.
1444 if (Tok.is(tok::kw_asm)) {
1445 SourceLocation Loc;
1446 ExprResult AsmLabel(ParseSimpleAsm(&Loc));
1447 if (AsmLabel.isInvalid()) {
1448 SkipUntil(tok::semi, true, true);
1449 return true;
1450 }
1451
1452 D.setAsmLabel(AsmLabel.release());
1453 D.SetRangeEnd(Loc);
1454 }
1455
1456 MaybeParseGNUAttributes(D);
1457 return false;
1458}
1459
Douglas Gregor1426e532009-05-12 21:31:51 +00001460/// \brief Parse 'declaration' after parsing 'declaration-specifiers
1461/// declarator'. This method parses the remainder of the declaration
1462/// (including any attributes or initializer, among other things) and
1463/// finalizes the declaration.
Reid Spencer5f016e22007-07-11 17:01:13 +00001464///
Reid Spencer5f016e22007-07-11 17:01:13 +00001465/// init-declarator: [C99 6.7]
1466/// declarator
1467/// declarator '=' initializer
1468/// [GNU] declarator simple-asm-expr[opt] attributes[opt]
1469/// [GNU] declarator simple-asm-expr[opt] attributes[opt] '=' initializer
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00001470/// [C++] declarator initializer[opt]
1471///
1472/// [C++] initializer:
1473/// [C++] '=' initializer-clause
1474/// [C++] '(' expression-list ')'
Sebastian Redl50de12f2009-03-24 22:27:57 +00001475/// [C++0x] '=' 'default' [TODO]
1476/// [C++0x] '=' 'delete'
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001477/// [C++0x] braced-init-list
Sebastian Redl50de12f2009-03-24 22:27:57 +00001478///
1479/// According to the standard grammar, =default and =delete are function
1480/// definitions, but that definitely doesn't fit with the parser here.
Reid Spencer5f016e22007-07-11 17:01:13 +00001481///
John McCalld226f652010-08-21 09:40:31 +00001482Decl *Parser::ParseDeclarationAfterDeclarator(Declarator &D,
Douglas Gregore542c862009-06-23 23:11:28 +00001483 const ParsedTemplateInfo &TemplateInfo) {
DeLesley Hutchinsc24a2332012-02-16 16:50:43 +00001484 if (ParseAsmAttributesAfterDeclarator(D))
Richard Smithad762fc2011-04-14 22:09:26 +00001485 return 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001486
Richard Smithad762fc2011-04-14 22:09:26 +00001487 return ParseDeclarationAfterDeclaratorAndAttributes(D, TemplateInfo);
1488}
Mike Stump1eb44332009-09-09 15:08:12 +00001489
Richard Smithad762fc2011-04-14 22:09:26 +00001490Decl *Parser::ParseDeclarationAfterDeclaratorAndAttributes(Declarator &D,
1491 const ParsedTemplateInfo &TemplateInfo) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001492 // Inform the current actions module that we just parsed this declarator.
John McCalld226f652010-08-21 09:40:31 +00001493 Decl *ThisDecl = 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001494 switch (TemplateInfo.Kind) {
1495 case ParsedTemplateInfo::NonTemplate:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001496 ThisDecl = Actions.ActOnDeclarator(getCurScope(), D);
Douglas Gregord5a423b2009-09-25 18:43:00 +00001497 break;
1498
1499 case ParsedTemplateInfo::Template:
1500 case ParsedTemplateInfo::ExplicitSpecialization:
Douglas Gregor23c94db2010-07-02 17:43:08 +00001501 ThisDecl = Actions.ActOnTemplateDeclarator(getCurScope(),
John McCallf312b1e2010-08-26 23:41:50 +00001502 MultiTemplateParamsArg(Actions,
Douglas Gregore542c862009-06-23 23:11:28 +00001503 TemplateInfo.TemplateParams->data(),
1504 TemplateInfo.TemplateParams->size()),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001505 D);
1506 break;
1507
1508 case ParsedTemplateInfo::ExplicitInstantiation: {
John McCalld226f652010-08-21 09:40:31 +00001509 DeclResult ThisRes
Douglas Gregor23c94db2010-07-02 17:43:08 +00001510 = Actions.ActOnExplicitInstantiation(getCurScope(),
Douglas Gregord5a423b2009-09-25 18:43:00 +00001511 TemplateInfo.ExternLoc,
1512 TemplateInfo.TemplateLoc,
1513 D);
1514 if (ThisRes.isInvalid()) {
1515 SkipUntil(tok::semi, true, true);
John McCalld226f652010-08-21 09:40:31 +00001516 return 0;
Douglas Gregord5a423b2009-09-25 18:43:00 +00001517 }
1518
1519 ThisDecl = ThisRes.get();
1520 break;
1521 }
1522 }
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Richard Smith34b41d92011-02-20 03:19:35 +00001524 bool TypeContainsAuto =
1525 D.getDeclSpec().getTypeSpecType() == DeclSpec::TST_auto;
1526
Douglas Gregor1426e532009-05-12 21:31:51 +00001527 // Parse declarator '=' initializer.
Richard Trieud6c7c672012-01-18 22:54:52 +00001528 // If a '==' or '+=' is found, suggest a fixit to '='.
Richard Trieufcaf27e2012-01-19 22:01:51 +00001529 if (isTokenEqualOrEqualTypo()) {
Douglas Gregor1426e532009-05-12 21:31:51 +00001530 ConsumeToken();
Anders Carlsson37bf9d22010-09-24 21:25:25 +00001531 if (Tok.is(tok::kw_delete)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001532 if (D.isFunctionDeclarator())
1533 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1534 << 1 /* delete */;
1535 else
1536 Diag(ConsumeToken(), diag::err_deleted_non_function);
Sean Huntfe2695e2011-05-06 01:42:00 +00001537 } else if (Tok.is(tok::kw_default)) {
Sean Hunte4246a62011-05-12 06:15:49 +00001538 if (D.isFunctionDeclarator())
Sebastian Redlecfcd562012-02-11 23:51:21 +00001539 Diag(ConsumeToken(), diag::err_default_delete_in_multiple_declaration)
1540 << 0 /* default */;
Sean Hunte4246a62011-05-12 06:15:49 +00001541 else
1542 Diag(ConsumeToken(), diag::err_default_special_members);
Douglas Gregor1426e532009-05-12 21:31:51 +00001543 } else {
David Blaikie4e4d0842012-03-11 07:00:24 +00001544 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
John McCall731ad842009-12-19 09:28:58 +00001545 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001546 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001547 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001548
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001549 if (Tok.is(tok::code_completion)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001550 Actions.CodeCompleteInitializer(getCurScope(), ThisDecl);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00001551 cutOffParsing();
1552 return 0;
Douglas Gregor5ac3bdb2010-05-30 01:49:25 +00001553 }
1554
John McCall60d7b3a2010-08-24 06:29:42 +00001555 ExprResult Init(ParseInitializer());
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001556
David Blaikie4e4d0842012-03-11 07:00:24 +00001557 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001558 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
John McCall731ad842009-12-19 09:28:58 +00001559 ExitScope();
1560 }
Argyrios Kyrtzidis0ffd9ff2009-06-17 22:50:06 +00001561
Douglas Gregor1426e532009-05-12 21:31:51 +00001562 if (Init.isInvalid()) {
Douglas Gregor00225542010-03-01 18:27:54 +00001563 SkipUntil(tok::comma, true, true);
1564 Actions.ActOnInitializerError(ThisDecl);
1565 } else
Richard Smith34b41d92011-02-20 03:19:35 +00001566 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1567 /*DirectInit=*/false, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001568 }
1569 } else if (Tok.is(tok::l_paren)) {
1570 // Parse C++ direct initializer: '(' expression-list ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001571 BalancedDelimiterTracker T(*this, tok::l_paren);
1572 T.consumeOpen();
1573
Douglas Gregor1426e532009-05-12 21:31:51 +00001574 ExprVector Exprs(Actions);
1575 CommaLocsTy CommaLocs;
1576
David Blaikie4e4d0842012-03-11 07:00:24 +00001577 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregorb4debae2009-12-22 17:47:17 +00001578 EnterScope(0);
Douglas Gregor23c94db2010-07-02 17:43:08 +00001579 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001580 }
1581
Douglas Gregor1426e532009-05-12 21:31:51 +00001582 if (ParseExpressionList(Exprs, CommaLocs)) {
1583 SkipUntil(tok::r_paren);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001584
David Blaikie4e4d0842012-03-11 07:00:24 +00001585 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001586 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001587 ExitScope();
1588 }
Douglas Gregor1426e532009-05-12 21:31:51 +00001589 } else {
1590 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001591 T.consumeClose();
Douglas Gregor1426e532009-05-12 21:31:51 +00001592
1593 assert(!Exprs.empty() && Exprs.size()-1 == CommaLocs.size() &&
1594 "Unexpected number of commas!");
Douglas Gregorb4debae2009-12-22 17:47:17 +00001595
David Blaikie4e4d0842012-03-11 07:00:24 +00001596 if (getLangOpts().CPlusPlus && D.getCXXScopeSpec().isSet()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00001597 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
Douglas Gregorb4debae2009-12-22 17:47:17 +00001598 ExitScope();
1599 }
1600
Sebastian Redl5b9cc5d2012-02-11 23:51:47 +00001601 ExprResult Initializer = Actions.ActOnParenListExpr(T.getOpenLocation(),
1602 T.getCloseLocation(),
1603 move_arg(Exprs));
1604 Actions.AddInitializerToDecl(ThisDecl, Initializer.take(),
1605 /*DirectInit=*/true, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001606 }
David Blaikie4e4d0842012-03-11 07:00:24 +00001607 } else if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001608 // Parse C++0x braced-init-list.
Richard Smith7fe62082011-10-15 05:09:34 +00001609 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
1610
Sebastian Redldbef1bb2011-06-05 12:23:16 +00001611 if (D.getCXXScopeSpec().isSet()) {
1612 EnterScope(0);
1613 Actions.ActOnCXXEnterDeclInitializer(getCurScope(), ThisDecl);
1614 }
1615
1616 ExprResult Init(ParseBraceInitializer());
1617
1618 if (D.getCXXScopeSpec().isSet()) {
1619 Actions.ActOnCXXExitDeclInitializer(getCurScope(), ThisDecl);
1620 ExitScope();
1621 }
1622
1623 if (Init.isInvalid()) {
1624 Actions.ActOnInitializerError(ThisDecl);
1625 } else
1626 Actions.AddInitializerToDecl(ThisDecl, Init.take(),
1627 /*DirectInit=*/true, TypeContainsAuto);
1628
Douglas Gregor1426e532009-05-12 21:31:51 +00001629 } else {
Richard Smith34b41d92011-02-20 03:19:35 +00001630 Actions.ActOnUninitializedDecl(ThisDecl, TypeContainsAuto);
Douglas Gregor1426e532009-05-12 21:31:51 +00001631 }
1632
Richard Smith483b9f32011-02-21 20:05:19 +00001633 Actions.FinalizeDeclaration(ThisDecl);
1634
Douglas Gregor1426e532009-05-12 21:31:51 +00001635 return ThisDecl;
1636}
1637
Reid Spencer5f016e22007-07-11 17:01:13 +00001638/// ParseSpecifierQualifierList
1639/// specifier-qualifier-list:
1640/// type-specifier specifier-qualifier-list[opt]
1641/// type-qualifier specifier-qualifier-list[opt]
1642/// [GNU] attributes specifier-qualifier-list[opt]
1643///
Richard Smith69730c12012-03-12 07:56:15 +00001644void Parser::ParseSpecifierQualifierList(DeclSpec &DS, AccessSpecifier AS,
1645 DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001646 /// specifier-qualifier-list is a subset of declaration-specifiers. Just
1647 /// parse declaration-specifiers and complain about extra stuff.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001648 /// TODO: diagnose attribute-specifiers and alignment-specifiers.
Richard Smith69730c12012-03-12 07:56:15 +00001649 ParseDeclarationSpecifiers(DS, ParsedTemplateInfo(), AS, DSC);
Mike Stump1eb44332009-09-09 15:08:12 +00001650
Reid Spencer5f016e22007-07-11 17:01:13 +00001651 // Validate declspec for type-name.
1652 unsigned Specs = DS.getParsedSpecifiers();
Richard Smitha971d242012-05-09 20:55:26 +00001653 if ((DSC == DSC_type_specifier || DSC == DSC_trailing) &&
1654 !DS.hasTypeSpecifier()) {
Richard Smith69730c12012-03-12 07:56:15 +00001655 Diag(Tok, diag::err_expected_type);
1656 DS.SetTypeSpecError();
1657 } else if (Specs == DeclSpec::PQ_None && !DS.getNumProtocolQualifiers() &&
1658 !DS.hasAttributes()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00001659 Diag(Tok, diag::err_typename_requires_specqual);
Richard Smith69730c12012-03-12 07:56:15 +00001660 if (!DS.hasTypeSpecifier())
1661 DS.SetTypeSpecError();
1662 }
Mike Stump1eb44332009-09-09 15:08:12 +00001663
Reid Spencer5f016e22007-07-11 17:01:13 +00001664 // Issue diagnostic and remove storage class if present.
1665 if (Specs & DeclSpec::PQ_StorageClassSpecifier) {
1666 if (DS.getStorageClassSpecLoc().isValid())
1667 Diag(DS.getStorageClassSpecLoc(),diag::err_typename_invalid_storageclass);
1668 else
1669 Diag(DS.getThreadSpecLoc(), diag::err_typename_invalid_storageclass);
1670 DS.ClearStorageClassSpecs();
1671 }
Mike Stump1eb44332009-09-09 15:08:12 +00001672
Reid Spencer5f016e22007-07-11 17:01:13 +00001673 // Issue diagnostic and remove function specfier if present.
1674 if (Specs & DeclSpec::PQ_FunctionSpecifier) {
Douglas Gregorb48fe382008-10-31 09:07:45 +00001675 if (DS.isInlineSpecified())
1676 Diag(DS.getInlineSpecLoc(), diag::err_typename_invalid_functionspec);
1677 if (DS.isVirtualSpecified())
1678 Diag(DS.getVirtualSpecLoc(), diag::err_typename_invalid_functionspec);
1679 if (DS.isExplicitSpecified())
1680 Diag(DS.getExplicitSpecLoc(), diag::err_typename_invalid_functionspec);
Reid Spencer5f016e22007-07-11 17:01:13 +00001681 DS.ClearFunctionSpecs();
1682 }
Richard Smith69730c12012-03-12 07:56:15 +00001683
1684 // Issue diagnostic and remove constexpr specfier if present.
1685 if (DS.isConstexprSpecified()) {
1686 Diag(DS.getConstexprSpecLoc(), diag::err_typename_invalid_constexpr);
1687 DS.ClearConstexprSpec();
1688 }
Reid Spencer5f016e22007-07-11 17:01:13 +00001689}
1690
Chris Lattnerc199ab32009-04-12 20:42:31 +00001691/// isValidAfterIdentifierInDeclaratorAfterDeclSpec - Return true if the
1692/// specified token is valid after the identifier in a declarator which
1693/// immediately follows the declspec. For example, these things are valid:
1694///
1695/// int x [ 4]; // direct-declarator
1696/// int x ( int y); // direct-declarator
1697/// int(int x ) // direct-declarator
1698/// int x ; // simple-declaration
1699/// int x = 17; // init-declarator-list
1700/// int x , y; // init-declarator-list
1701/// int x __asm__ ("foo"); // init-declarator-list
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001702/// int x : 4; // struct-declarator
Chris Lattnerc83c27a2009-04-12 22:29:43 +00001703/// int x { 5}; // C++'0x unified initializers
Chris Lattnerc199ab32009-04-12 20:42:31 +00001704///
1705/// This is not, because 'x' does not immediately follow the declspec (though
1706/// ')' happens to be valid anyway).
1707/// int (x)
1708///
1709static bool isValidAfterIdentifierInDeclarator(const Token &T) {
1710 return T.is(tok::l_square) || T.is(tok::l_paren) || T.is(tok::r_paren) ||
1711 T.is(tok::semi) || T.is(tok::comma) || T.is(tok::equal) ||
Chris Lattnerb6645dd2009-04-14 21:16:09 +00001712 T.is(tok::kw_asm) || T.is(tok::l_brace) || T.is(tok::colon);
Chris Lattnerc199ab32009-04-12 20:42:31 +00001713}
1714
Chris Lattnere40c2952009-04-14 21:34:55 +00001715
1716/// ParseImplicitInt - This method is called when we have an non-typename
1717/// identifier in a declspec (which normally terminates the decl spec) when
1718/// the declspec has no type specifier. In this case, the declspec is either
1719/// malformed or is "implicit int" (in K&R and C89).
1720///
1721/// This method handles diagnosing this prettily and returns false if the
1722/// declspec is done being processed. If it recovers and thinks there may be
1723/// other pieces of declspec after it, it returns true.
1724///
Chris Lattnerf4382f52009-04-14 22:17:06 +00001725bool Parser::ParseImplicitInt(DeclSpec &DS, CXXScopeSpec *SS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00001726 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00001727 AccessSpecifier AS, DeclSpecContext DSC) {
Chris Lattnerf4382f52009-04-14 22:17:06 +00001728 assert(Tok.is(tok::identifier) && "should have identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00001729
Chris Lattnere40c2952009-04-14 21:34:55 +00001730 SourceLocation Loc = Tok.getLocation();
1731 // If we see an identifier that is not a type name, we normally would
1732 // parse it as the identifer being declared. However, when a typename
1733 // is typo'd or the definition is not included, this will incorrectly
1734 // parse the typename as the identifier name and fall over misparsing
1735 // later parts of the diagnostic.
1736 //
1737 // As such, we try to do some look-ahead in cases where this would
1738 // otherwise be an "implicit-int" case to see if this is invalid. For
1739 // example: "static foo_t x = 4;" In this case, if we parsed foo_t as
1740 // an identifier with implicit int, we'd get a parse error because the
1741 // next token is obviously invalid for a type. Parse these as a case
1742 // with an invalid type specifier.
1743 assert(!DS.hasTypeSpecifier() && "Type specifier checked above");
Mike Stump1eb44332009-09-09 15:08:12 +00001744
Chris Lattnere40c2952009-04-14 21:34:55 +00001745 // Since we know that this either implicit int (which is rare) or an
Richard Smith827adaf2012-05-15 21:01:51 +00001746 // error, do lookahead to try to do better recovery. This never applies
1747 // within a type specifier. Outside of C++, we allow this even if the
1748 // language doesn't "officially" support implicit int -- we support
1749 // implicit int as an extension in C99 and C11. Allegedly, MS also
1750 // supports implicit int in C++ mode.
Richard Smitha971d242012-05-09 20:55:26 +00001751 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
Richard Smith827adaf2012-05-15 21:01:51 +00001752 (!getLangOpts().CPlusPlus || getLangOpts().MicrosoftExt) &&
Richard Smith69730c12012-03-12 07:56:15 +00001753 isValidAfterIdentifierInDeclarator(NextToken())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001754 // If this token is valid for implicit int, e.g. "static x = 4", then
1755 // we just avoid eating the identifier, so it will be parsed as the
1756 // identifier in the declarator.
1757 return false;
1758 }
Mike Stump1eb44332009-09-09 15:08:12 +00001759
Richard Smith827adaf2012-05-15 21:01:51 +00001760 if (getLangOpts().CPlusPlus &&
1761 DS.getStorageClassSpec() == DeclSpec::SCS_auto) {
1762 // Don't require a type specifier if we have the 'auto' storage class
1763 // specifier in C++98 -- we'll promote it to a type specifier.
1764 return false;
1765 }
1766
Chris Lattnere40c2952009-04-14 21:34:55 +00001767 // Otherwise, if we don't consume this token, we are going to emit an
1768 // error anyway. Try to recover from various common problems. Check
1769 // to see if this was a reference to a tag name without a tag specified.
1770 // This is a common problem in C (saying 'foo' instead of 'struct foo').
Chris Lattnerf4382f52009-04-14 22:17:06 +00001771 //
1772 // C++ doesn't need this, and isTagName doesn't take SS.
1773 if (SS == 0) {
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001774 const char *TagName = 0, *FixitTagName = 0;
Chris Lattnerf4382f52009-04-14 22:17:06 +00001775 tok::TokenKind TagKind = tok::unknown;
Mike Stump1eb44332009-09-09 15:08:12 +00001776
Douglas Gregor23c94db2010-07-02 17:43:08 +00001777 switch (Actions.isTagName(*Tok.getIdentifierInfo(), getCurScope())) {
Chris Lattnere40c2952009-04-14 21:34:55 +00001778 default: break;
Argyrios Kyrtzidisb8a9d3b2011-04-21 17:29:47 +00001779 case DeclSpec::TST_enum:
1780 TagName="enum" ; FixitTagName = "enum " ; TagKind=tok::kw_enum ;break;
1781 case DeclSpec::TST_union:
1782 TagName="union" ; FixitTagName = "union " ;TagKind=tok::kw_union ;break;
1783 case DeclSpec::TST_struct:
1784 TagName="struct"; FixitTagName = "struct ";TagKind=tok::kw_struct;break;
1785 case DeclSpec::TST_class:
1786 TagName="class" ; FixitTagName = "class " ;TagKind=tok::kw_class ;break;
Chris Lattnere40c2952009-04-14 21:34:55 +00001787 }
Mike Stump1eb44332009-09-09 15:08:12 +00001788
Chris Lattnerf4382f52009-04-14 22:17:06 +00001789 if (TagName) {
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001790 IdentifierInfo *TokenName = Tok.getIdentifierInfo();
1791 LookupResult R(Actions, TokenName, SourceLocation(),
1792 Sema::LookupOrdinaryName);
1793
Chris Lattnerf4382f52009-04-14 22:17:06 +00001794 Diag(Loc, diag::err_use_of_tag_name_without_tag)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001795 << TokenName << TagName << getLangOpts().CPlusPlus
1796 << FixItHint::CreateInsertion(Tok.getLocation(), FixitTagName);
1797
1798 if (Actions.LookupParsedName(R, getCurScope(), SS)) {
1799 for (LookupResult::iterator I = R.begin(), IEnd = R.end();
1800 I != IEnd; ++I)
Kaelyn Uhrain392b3f52012-04-27 18:26:49 +00001801 Diag((*I)->getLocation(), diag::note_decl_hiding_tag_type)
Kaelyn Uhrainaec2ac62012-04-26 23:36:17 +00001802 << TokenName << TagName;
1803 }
Mike Stump1eb44332009-09-09 15:08:12 +00001804
Chris Lattnerf4382f52009-04-14 22:17:06 +00001805 // Parse this as a tag as if the missing tag were present.
1806 if (TagKind == tok::kw_enum)
Richard Smith69730c12012-03-12 07:56:15 +00001807 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001808 else
Richard Smith69730c12012-03-12 07:56:15 +00001809 ParseClassSpecifier(TagKind, Loc, DS, TemplateInfo, AS,
1810 /*EnteringContext*/ false, DSC_normal);
Chris Lattnerf4382f52009-04-14 22:17:06 +00001811 return true;
1812 }
Chris Lattnere40c2952009-04-14 21:34:55 +00001813 }
Mike Stump1eb44332009-09-09 15:08:12 +00001814
Richard Smith8f0a7e72012-05-15 21:29:55 +00001815 // Determine whether this identifier could plausibly be the name of something
Richard Smith7514db22012-05-15 21:42:17 +00001816 // being declared (with a missing type).
Richard Smith8f0a7e72012-05-15 21:29:55 +00001817 if (DSC != DSC_type_specifier && DSC != DSC_trailing &&
1818 (!SS || DSC == DSC_top_level || DSC == DSC_class)) {
Richard Smith827adaf2012-05-15 21:01:51 +00001819 // Look ahead to the next token to try to figure out what this declaration
1820 // was supposed to be.
1821 switch (NextToken().getKind()) {
1822 case tok::comma:
1823 case tok::equal:
1824 case tok::kw_asm:
1825 case tok::l_brace:
1826 case tok::l_square:
1827 case tok::semi:
1828 // This looks like a variable declaration. The type is probably missing.
1829 // We're done parsing decl-specifiers.
1830 return false;
1831
1832 case tok::l_paren: {
1833 // static x(4); // 'x' is not a type
1834 // x(int n); // 'x' is not a type
1835 // x (*p)[]; // 'x' is a type
1836 //
1837 // Since we're in an error case (or the rare 'implicit int in C++' MS
1838 // extension), we can afford to perform a tentative parse to determine
1839 // which case we're in.
1840 TentativeParsingAction PA(*this);
1841 ConsumeToken();
1842 TPResult TPR = TryParseDeclarator(/*mayBeAbstract*/false);
1843 PA.Revert();
1844 if (TPR == TPResult::False())
1845 return false;
1846 // The identifier is followed by a parenthesized declarator.
1847 // It's supposed to be a type.
1848 break;
1849 }
1850
1851 default:
1852 // This is probably supposed to be a type. This includes cases like:
1853 // int f(itn);
1854 // struct S { unsinged : 4; };
1855 break;
1856 }
1857 }
1858
Douglas Gregora786fdb2009-10-13 23:27:22 +00001859 // This is almost certainly an invalid type name. Let the action emit a
1860 // diagnostic and attempt to recover.
John McCallb3d87482010-08-24 05:47:05 +00001861 ParsedType T;
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001862 IdentifierInfo *II = Tok.getIdentifierInfo();
1863 if (Actions.DiagnoseUnknownTypeName(II, Loc, getCurScope(), SS, T)) {
Douglas Gregora786fdb2009-10-13 23:27:22 +00001864 // The action emitted a diagnostic, so we don't have to.
1865 if (T) {
1866 // The action has suggested that the type T could be used. Set that as
1867 // the type in the declaration specifiers, consume the would-be type
1868 // name token, and we're done.
1869 const char *PrevSpec;
1870 unsigned DiagID;
John McCallb3d87482010-08-24 05:47:05 +00001871 DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec, DiagID, T);
Douglas Gregora786fdb2009-10-13 23:27:22 +00001872 DS.SetRangeEnd(Tok.getLocation());
1873 ConsumeToken();
Kaelyn Uhrain50dc12a2012-06-15 23:45:58 +00001874 // There may be other declaration specifiers after this.
1875 return true;
1876 } else if (II != Tok.getIdentifierInfo()) {
1877 // If no type was suggested, the correction is to a keyword
1878 Tok.setKind(II->getTokenID());
Douglas Gregora786fdb2009-10-13 23:27:22 +00001879 // There may be other declaration specifiers after this.
1880 return true;
1881 }
1882
1883 // Fall through; the action had no suggestion for us.
1884 } else {
1885 // The action did not emit a diagnostic, so emit one now.
1886 SourceRange R;
1887 if (SS) R = SS->getRange();
1888 Diag(Loc, diag::err_unknown_typename) << Tok.getIdentifierInfo() << R;
1889 }
Mike Stump1eb44332009-09-09 15:08:12 +00001890
Douglas Gregora786fdb2009-10-13 23:27:22 +00001891 // Mark this as an error.
Richard Smith69730c12012-03-12 07:56:15 +00001892 DS.SetTypeSpecError();
Chris Lattnere40c2952009-04-14 21:34:55 +00001893 DS.SetRangeEnd(Tok.getLocation());
1894 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00001895
Chris Lattnere40c2952009-04-14 21:34:55 +00001896 // TODO: Could inject an invalid typedef decl in an enclosing scope to
1897 // avoid rippling error messages on subsequent uses of the same type,
1898 // could be useful if #include was forgotten.
1899 return false;
1900}
1901
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001902/// \brief Determine the declaration specifier context from the declarator
1903/// context.
1904///
1905/// \param Context the declarator context, which is one of the
1906/// Declarator::TheContext enumerator values.
1907Parser::DeclSpecContext
1908Parser::getDeclSpecContextFromDeclaratorContext(unsigned Context) {
1909 if (Context == Declarator::MemberContext)
1910 return DSC_class;
1911 if (Context == Declarator::FileContext)
1912 return DSC_top_level;
Richard Smith6d96d3a2012-03-15 01:02:11 +00001913 if (Context == Declarator::TrailingReturnContext)
1914 return DSC_trailing;
Douglas Gregor0efc2c12010-01-13 17:31:36 +00001915 return DSC_normal;
1916}
1917
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001918/// ParseAlignArgument - Parse the argument to an alignment-specifier.
1919///
1920/// FIXME: Simply returns an alignof() expression if the argument is a
1921/// type. Ideally, the type should be propagated directly into Sema.
1922///
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001923/// [C11] type-id
1924/// [C11] constant-expression
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001925/// [C++0x] type-id ...[opt]
1926/// [C++0x] assignment-expression ...[opt]
1927ExprResult Parser::ParseAlignArgument(SourceLocation Start,
1928 SourceLocation &EllipsisLoc) {
1929 ExprResult ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001930 if (isTypeIdInParens()) {
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001931 SourceLocation TypeLoc = Tok.getLocation();
1932 ParsedType Ty = ParseTypeName().get();
1933 SourceRange TypeRange(Start, Tok.getLocation());
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001934 ER = Actions.ActOnUnaryExprOrTypeTraitExpr(TypeLoc, UETT_AlignOf, true,
1935 Ty.getAsOpaquePtr(), TypeRange);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001936 } else
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001937 ER = ParseConstantExpression();
1938
David Blaikie4e4d0842012-03-11 07:00:24 +00001939 if (getLangOpts().CPlusPlus0x && Tok.is(tok::ellipsis))
Peter Collingbournefe9b2a82011-10-24 17:56:00 +00001940 EllipsisLoc = ConsumeToken();
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001941
1942 return ER;
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001943}
1944
1945/// ParseAlignmentSpecifier - Parse an alignment-specifier, and add the
1946/// attribute to Attrs.
1947///
1948/// alignment-specifier:
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001949/// [C11] '_Alignas' '(' type-id ')'
1950/// [C11] '_Alignas' '(' constant-expression ')'
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001951/// [C++0x] 'alignas' '(' type-id ...[opt] ')'
1952/// [C++0x] 'alignas' '(' assignment-expression ...[opt] ')'
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001953void Parser::ParseAlignmentSpecifier(ParsedAttributes &Attrs,
1954 SourceLocation *endLoc) {
1955 assert((Tok.is(tok::kw_alignas) || Tok.is(tok::kw__Alignas)) &&
1956 "Not an alignment-specifier!");
1957
1958 SourceLocation KWLoc = Tok.getLocation();
1959 ConsumeToken();
1960
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001961 BalancedDelimiterTracker T(*this, tok::l_paren);
1962 if (T.expectAndConsume(diag::err_expected_lparen))
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001963 return;
1964
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001965 SourceLocation EllipsisLoc;
1966 ExprResult ArgExpr = ParseAlignArgument(T.getOpenLocation(), EllipsisLoc);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001967 if (ArgExpr.isInvalid()) {
1968 SkipUntil(tok::r_paren);
1969 return;
1970 }
1971
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001972 T.consumeClose();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001973 if (endLoc)
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00001974 *endLoc = T.getCloseLocation();
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001975
Peter Collingbourne0b64ba92011-10-23 20:07:52 +00001976 // FIXME: Handle pack-expansions here.
1977 if (EllipsisLoc.isValid()) {
1978 Diag(EllipsisLoc, diag::err_alignas_pack_exp_unsupported);
1979 return;
1980 }
1981
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001982 ExprVector ArgExprs(Actions);
1983 ArgExprs.push_back(ArgExpr.release());
Sean Hunt8e083e72012-06-19 23:57:03 +00001984 // FIXME: This should not be GNU, but we since the attribute used is
1985 // based on the spelling, and there is no true spelling for
1986 // C++11 attributes, this isn't accepted.
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001987 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +00001988 0, T.getOpenLocation(), ArgExprs.take(), 1,
Sean Hunt8e083e72012-06-19 23:57:03 +00001989 AttributeList::AS_GNU);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001990}
1991
Reid Spencer5f016e22007-07-11 17:01:13 +00001992/// ParseDeclarationSpecifiers
1993/// declaration-specifiers: [C99 6.7]
1994/// storage-class-specifier declaration-specifiers[opt]
1995/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001996/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001997/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001998/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001999/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00002000///
2001/// storage-class-specifier: [C99 6.7.1]
2002/// 'typedef'
2003/// 'extern'
2004/// 'static'
2005/// 'auto'
2006/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002007/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002008/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002009/// function-specifier: [C99 6.7.4]
2010/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002011/// [C++] 'virtual'
2012/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002013/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002014/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002015/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002016
Reid Spencer5f016e22007-07-11 17:01:13 +00002017///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002018void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002019 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002020 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002021 DeclSpecContext DSContext,
2022 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002023 if (DS.getSourceRange().isInvalid()) {
2024 DS.SetRangeStart(Tok.getLocation());
2025 DS.SetRangeEnd(Tok.getLocation());
2026 }
2027
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002028 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00002029 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002030 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002031 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002032 unsigned DiagID = 0;
2033
Reid Spencer5f016e22007-07-11 17:01:13 +00002034 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002035
Reid Spencer5f016e22007-07-11 17:01:13 +00002036 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002037 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002038 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00002039 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
2040 MaybeParseCXX0XAttributes(DS.getAttributes());
2041
Reid Spencer5f016e22007-07-11 17:01:13 +00002042 // If this is not a declaration specifier token, we're done reading decl
2043 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002044 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002045 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002046
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002047 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002048 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002049 if (DS.hasTypeSpecifier()) {
2050 bool AllowNonIdentifiers
2051 = (getCurScope()->getFlags() & (Scope::ControlScope |
2052 Scope::BlockScope |
2053 Scope::TemplateParamScope |
2054 Scope::FunctionPrototypeScope |
2055 Scope::AtCatchScope)) == 0;
2056 bool AllowNestedNameSpecifiers
2057 = DSContext == DSC_top_level ||
2058 (DSContext == DSC_class && DS.isFriendSpecified());
2059
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002060 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
2061 AllowNonIdentifiers,
2062 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002063 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002064 }
2065
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002066 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2067 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2068 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00002069 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
2070 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002071 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002072 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002073 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002074 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002075
2076 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002077 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002078 }
2079
Chris Lattner5e02c472009-01-05 00:07:25 +00002080 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002081 // C++ scope specifier. Annotate and loop, or bail out on error.
2082 if (TryAnnotateCXXScopeToken(true)) {
2083 if (!DS.hasTypeSpecifier())
2084 DS.SetTypeSpecError();
2085 goto DoneWithDeclSpec;
2086 }
John McCall2e0a7152010-03-01 18:20:46 +00002087 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2088 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002089 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002090
2091 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002092 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002093 goto DoneWithDeclSpec;
2094
John McCallaa87d332009-12-12 11:40:51 +00002095 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002096 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2097 Tok.getAnnotationRange(),
2098 SS);
John McCallaa87d332009-12-12 11:40:51 +00002099
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002100 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002101 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002102 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002103 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002104 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002105 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002106
2107 // C++ [class.qual]p2:
2108 // In a lookup in which the constructor is an acceptable lookup
2109 // result and the nested-name-specifier nominates a class C:
2110 //
2111 // - if the name specified after the
2112 // nested-name-specifier, when looked up in C, is the
2113 // injected-class-name of C (Clause 9), or
2114 //
2115 // - if the name specified after the nested-name-specifier
2116 // is the same as the identifier or the
2117 // simple-template-id's template-name in the last
2118 // component of the nested-name-specifier,
2119 //
2120 // the name is instead considered to name the constructor of
2121 // class C.
2122 //
2123 // Thus, if the template-name is actually the constructor
2124 // name, then the code is ill-formed; this interpretation is
2125 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002126 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002127 if ((DSContext == DSC_top_level ||
2128 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2129 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002130 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002131 if (isConstructorDeclarator()) {
2132 // The user meant this to be an out-of-line constructor
2133 // definition, but template arguments are not allowed
2134 // there. Just allow this as a constructor; we'll
2135 // complain about it later.
2136 goto DoneWithDeclSpec;
2137 }
2138
2139 // The user meant this to name a type, but it actually names
2140 // a constructor with some extraneous template
2141 // arguments. Complain, then parse it as a type as the user
2142 // intended.
2143 Diag(TemplateId->TemplateNameLoc,
2144 diag::err_out_of_line_template_id_names_constructor)
2145 << TemplateId->Name;
2146 }
2147
John McCallaa87d332009-12-12 11:40:51 +00002148 DS.getTypeSpecScope() = SS;
2149 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002150 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002151 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002152 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002153 continue;
2154 }
2155
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002156 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002157 DS.getTypeSpecScope() = SS;
2158 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002159 if (Tok.getAnnotationValue()) {
2160 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002161 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2162 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002163 PrevSpec, DiagID, T);
2164 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002165 else
2166 DS.SetTypeSpecError();
2167 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2168 ConsumeToken(); // The typename
2169 }
2170
Douglas Gregor9135c722009-03-25 15:40:00 +00002171 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002172 goto DoneWithDeclSpec;
2173
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002174 // If we're in a context where the identifier could be a class name,
2175 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002176 if ((DSContext == DSC_top_level ||
2177 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002178 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002179 &SS)) {
2180 if (isConstructorDeclarator())
2181 goto DoneWithDeclSpec;
2182
2183 // As noted in C++ [class.qual]p2 (cited above), when the name
2184 // of the class is qualified in a context where it could name
2185 // a constructor, its a constructor name. However, we've
2186 // looked at the declarator, and the user probably meant this
2187 // to be a type. Complain that it isn't supposed to be treated
2188 // as a type, then proceed to parse it as a type.
2189 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2190 << Next.getIdentifierInfo();
2191 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002192
John McCallb3d87482010-08-24 05:47:05 +00002193 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2194 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002195 getCurScope(), &SS,
2196 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002197 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002198 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002199
Chris Lattnerf4382f52009-04-14 22:17:06 +00002200 // If the referenced identifier is not a type, then this declspec is
2201 // erroneous: We already checked about that it has no type specifier, and
2202 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002203 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002204 if (TypeRep == 0) {
2205 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00002206 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002207 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002208 }
Mike Stump1eb44332009-09-09 15:08:12 +00002209
John McCallaa87d332009-12-12 11:40:51 +00002210 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002211 ConsumeToken(); // The C++ scope.
2212
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002213 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002214 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002215 if (isInvalid)
2216 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002217
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002218 DS.SetRangeEnd(Tok.getLocation());
2219 ConsumeToken(); // The typename.
2220
2221 continue;
2222 }
Mike Stump1eb44332009-09-09 15:08:12 +00002223
Chris Lattner80d0c892009-01-21 19:48:37 +00002224 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002225 if (Tok.getAnnotationValue()) {
2226 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002227 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002228 DiagID, T);
2229 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002230 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00002231
2232 if (isInvalid)
2233 break;
2234
Chris Lattner80d0c892009-01-21 19:48:37 +00002235 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2236 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002237
Chris Lattner80d0c892009-01-21 19:48:37 +00002238 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2239 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002240 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002241 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002242 ParseObjCProtocolQualifiers(DS);
2243
Chris Lattner80d0c892009-01-21 19:48:37 +00002244 continue;
2245 }
Mike Stump1eb44332009-09-09 15:08:12 +00002246
Douglas Gregorbfad9152011-04-28 15:48:45 +00002247 case tok::kw___is_signed:
2248 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2249 // typically treats it as a trait. If we see __is_signed as it appears
2250 // in libstdc++, e.g.,
2251 //
2252 // static const bool __is_signed;
2253 //
2254 // then treat __is_signed as an identifier rather than as a keyword.
2255 if (DS.getTypeSpecType() == TST_bool &&
2256 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2257 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2258 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2259 Tok.setKind(tok::identifier);
2260 }
2261
2262 // We're done with the declaration-specifiers.
2263 goto DoneWithDeclSpec;
2264
Chris Lattner3bd934a2008-07-26 01:18:38 +00002265 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002266 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002267 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002268 // In C++, check to see if this is a scope specifier like foo::bar::, if
2269 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002270 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002271 if (TryAnnotateCXXScopeToken(true)) {
2272 if (!DS.hasTypeSpecifier())
2273 DS.SetTypeSpecError();
2274 goto DoneWithDeclSpec;
2275 }
2276 if (!Tok.is(tok::identifier))
2277 continue;
2278 }
Mike Stump1eb44332009-09-09 15:08:12 +00002279
Chris Lattner3bd934a2008-07-26 01:18:38 +00002280 // This identifier can only be a typedef name if we haven't already seen
2281 // a type-specifier. Without this check we misparse:
2282 // typedef int X; struct Y { short X; }; as 'short int'.
2283 if (DS.hasTypeSpecifier())
2284 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002285
John Thompson82287d12010-02-05 00:12:22 +00002286 // Check for need to substitute AltiVec keyword tokens.
2287 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2288 break;
2289
Richard Smithf63eee72012-05-09 18:56:43 +00002290 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2291 // allow the use of a typedef name as a type specifier.
2292 if (DS.isTypeAltiVecVector())
2293 goto DoneWithDeclSpec;
2294
John McCallb3d87482010-08-24 05:47:05 +00002295 ParsedType TypeRep =
2296 Actions.getTypeName(*Tok.getIdentifierInfo(),
2297 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002298
Chris Lattnerc199ab32009-04-12 20:42:31 +00002299 // If this is not a typedef name, don't parse it as part of the declspec,
2300 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002301 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002302 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002303 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002304 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002305
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002306 // If we're in a context where the identifier could be a class name,
2307 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002308 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002309 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002310 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002311 goto DoneWithDeclSpec;
2312
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002313 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002314 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002315 if (isInvalid)
2316 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002317
Chris Lattner3bd934a2008-07-26 01:18:38 +00002318 DS.SetRangeEnd(Tok.getLocation());
2319 ConsumeToken(); // The identifier
2320
2321 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2322 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002323 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002324 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002325 ParseObjCProtocolQualifiers(DS);
2326
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002327 // Need to support trailing type qualifiers (e.g. "id<p> const").
2328 // If a type specifier follows, it will be diagnosed elsewhere.
2329 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002330 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002331
2332 // type-name
2333 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002334 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002335 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002336 // This template-id does not refer to a type name, so we're
2337 // done with the type-specifiers.
2338 goto DoneWithDeclSpec;
2339 }
2340
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002341 // If we're in a context where the template-id could be a
2342 // constructor name or specialization, check whether this is a
2343 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002344 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002345 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002346 isConstructorDeclarator())
2347 goto DoneWithDeclSpec;
2348
Douglas Gregor39a8de12009-02-25 19:37:18 +00002349 // Turn the template-id annotation token into a type annotation
2350 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002351 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002352 continue;
2353 }
2354
Reid Spencer5f016e22007-07-11 17:01:13 +00002355 // GNU attributes support.
2356 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002357 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002358 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002359
2360 // Microsoft declspec support.
2361 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002362 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002363 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002364
Steve Naroff239f0732008-12-25 14:16:32 +00002365 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002366 case tok::kw___forceinline: {
2367 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2368 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
2369 SourceLocation AttrNameLoc = ConsumeToken();
Sean Hunt93f95f22012-06-18 16:13:52 +00002370 // FIXME: This does not work correctly if it is set to be a declspec
2371 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002372 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002373 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002374 continue;
2375 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002376
2377 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002378 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002379 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002380 case tok::kw___cdecl:
2381 case tok::kw___stdcall:
2382 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002383 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002384 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002385 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002386 continue;
2387
Dawn Perchik52fc3142010-09-03 01:29:35 +00002388 // Borland single token adornments.
2389 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002390 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002391 continue;
2392
Peter Collingbournef315fa82011-02-14 01:42:53 +00002393 // OpenCL single token adornments.
2394 case tok::kw___kernel:
2395 ParseOpenCLAttributes(DS.getAttributes());
2396 continue;
2397
Reid Spencer5f016e22007-07-11 17:01:13 +00002398 // storage-class-specifier
2399 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002400 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2401 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002402 break;
2403 case tok::kw_extern:
2404 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002405 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002406 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2407 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002408 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002409 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002410 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2411 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002412 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002413 case tok::kw_static:
2414 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002415 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002416 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2417 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002418 break;
2419 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002420 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002421 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002422 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2423 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002424 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002425 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002426 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002427 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002428 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2429 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002430 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002431 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2432 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002433 break;
2434 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002435 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2436 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002437 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002438 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002439 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2440 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002441 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002442 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002443 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002444 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002445
Reid Spencer5f016e22007-07-11 17:01:13 +00002446 // function-specifier
2447 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002448 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002449 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002450 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002451 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002452 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002453 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002454 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002455 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002456
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002457 // alignment-specifier
2458 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002459 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002460 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002461 ParseAlignmentSpecifier(DS.getAttributes());
2462 continue;
2463
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002464 // friend
2465 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002466 if (DSContext == DSC_class)
2467 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2468 else {
2469 PrevSpec = ""; // not actually used by the diagnostic
2470 DiagID = diag::err_friend_invalid_in_context;
2471 isInvalid = true;
2472 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002473 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002474
Douglas Gregor8d267c52011-09-09 02:06:17 +00002475 // Modules
2476 case tok::kw___module_private__:
2477 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2478 break;
2479
Sebastian Redl2ac67232009-11-05 15:47:02 +00002480 // constexpr
2481 case tok::kw_constexpr:
2482 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2483 break;
2484
Chris Lattner80d0c892009-01-21 19:48:37 +00002485 // type-specifier
2486 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002487 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2488 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002489 break;
2490 case tok::kw_long:
2491 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002492 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2493 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002494 else
John McCallfec54012009-08-03 20:12:06 +00002495 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2496 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002497 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002498 case tok::kw___int64:
2499 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2500 DiagID);
2501 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002502 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002503 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2504 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002505 break;
2506 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002507 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2508 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002509 break;
2510 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002511 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2512 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002513 break;
2514 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002515 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2516 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002517 break;
2518 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002519 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2520 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002521 break;
2522 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002523 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2524 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002525 break;
2526 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002527 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2528 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002529 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002530 case tok::kw___int128:
2531 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2532 DiagID);
2533 break;
2534 case tok::kw_half:
2535 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2536 DiagID);
2537 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002538 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002539 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2540 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002541 break;
2542 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002543 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2544 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002545 break;
2546 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002547 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2548 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002549 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002550 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002551 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2552 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002553 break;
2554 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002555 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2556 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002557 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002558 case tok::kw_bool:
2559 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002560 if (Tok.is(tok::kw_bool) &&
2561 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2562 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2563 PrevSpec = ""; // Not used by the diagnostic.
2564 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002565 // For better error recovery.
2566 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002567 isInvalid = true;
2568 } else {
2569 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2570 DiagID);
2571 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002572 break;
2573 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002574 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2575 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002576 break;
2577 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002578 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2579 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002580 break;
2581 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002582 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2583 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002584 break;
John Thompson82287d12010-02-05 00:12:22 +00002585 case tok::kw___vector:
2586 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2587 break;
2588 case tok::kw___pixel:
2589 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2590 break;
John McCalla5fc4722011-04-09 22:50:59 +00002591 case tok::kw___unknown_anytype:
2592 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2593 PrevSpec, DiagID);
2594 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002595
2596 // class-specifier:
2597 case tok::kw_class:
2598 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002599 case tok::kw_union: {
2600 tok::TokenKind Kind = Tok.getKind();
2601 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002602 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2603 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002604 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002605 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002606
2607 // enum-specifier:
2608 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002609 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002610 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002611 continue;
2612
2613 // cv-qualifier:
2614 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002615 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002616 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002617 break;
2618 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002619 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002620 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002621 break;
2622 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002623 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002624 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002625 break;
2626
Douglas Gregord57959a2009-03-27 23:10:48 +00002627 // C++ typename-specifier:
2628 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002629 if (TryAnnotateTypeOrScopeToken()) {
2630 DS.SetTypeSpecError();
2631 goto DoneWithDeclSpec;
2632 }
2633 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002634 continue;
2635 break;
2636
Chris Lattner80d0c892009-01-21 19:48:37 +00002637 // GNU typeof support.
2638 case tok::kw_typeof:
2639 ParseTypeofSpecifier(DS);
2640 continue;
2641
David Blaikie42d6d0c2011-12-04 05:04:18 +00002642 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002643 ParseDecltypeSpecifier(DS);
2644 continue;
2645
Sean Huntdb5d44b2011-05-19 05:37:45 +00002646 case tok::kw___underlying_type:
2647 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002648 continue;
2649
2650 case tok::kw__Atomic:
2651 ParseAtomicSpecifier(DS);
2652 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002653
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002654 // OpenCL qualifiers:
2655 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002656 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002657 goto DoneWithDeclSpec;
2658 case tok::kw___private:
2659 case tok::kw___global:
2660 case tok::kw___local:
2661 case tok::kw___constant:
2662 case tok::kw___read_only:
2663 case tok::kw___write_only:
2664 case tok::kw___read_write:
2665 ParseOpenCLQualifiers(DS);
2666 break;
2667
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002668 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002669 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002670 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2671 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002672 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002673 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002674
Douglas Gregor46f936e2010-11-19 17:10:50 +00002675 if (!ParseObjCProtocolQualifiers(DS))
2676 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2677 << FixItHint::CreateInsertion(Loc, "id")
2678 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002679
2680 // Need to support trailing type qualifiers (e.g. "id<p> const").
2681 // If a type specifier follows, it will be diagnosed elsewhere.
2682 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002683 }
John McCallfec54012009-08-03 20:12:06 +00002684 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002685 if (isInvalid) {
2686 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002687 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002688
2689 if (DiagID == diag::ext_duplicate_declspec)
2690 Diag(Tok, DiagID)
2691 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2692 else
2693 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002694 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002695
Chris Lattner81c018d2008-03-13 06:29:04 +00002696 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002697 if (DiagID != diag::err_bool_redeclaration)
2698 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002699 }
2700}
Douglas Gregoradcac882008-12-01 23:54:00 +00002701
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002702/// ParseStructDeclaration - Parse a struct declaration without the terminating
2703/// semicolon.
2704///
Reid Spencer5f016e22007-07-11 17:01:13 +00002705/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002706/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002707/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002708/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002709/// struct-declarator-list:
2710/// struct-declarator
2711/// struct-declarator-list ',' struct-declarator
2712/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2713/// struct-declarator:
2714/// declarator
2715/// [GNU] declarator attributes[opt]
2716/// declarator[opt] ':' constant-expression
2717/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2718///
Chris Lattnere1359422008-04-10 06:46:29 +00002719void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002720ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002721
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002722 if (Tok.is(tok::kw___extension__)) {
2723 // __extension__ silences extension warnings in the subexpression.
2724 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002725 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002726 return ParseStructDeclaration(DS, Fields);
2727 }
Mike Stump1eb44332009-09-09 15:08:12 +00002728
Steve Naroff28a7ca82007-08-20 22:28:22 +00002729 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002730 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002731
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002732 // If there are no declarators, this is a free-standing declaration
2733 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002734 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002735 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002736 return;
2737 }
2738
2739 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002740 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002741 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002742 while (1) {
John McCall92576642012-05-07 06:16:41 +00002743 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCallbdd563e2009-11-03 02:38:08 +00002744 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002745 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002746
2747 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002748 if (!FirstDeclarator)
2749 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002750
Steve Naroff28a7ca82007-08-20 22:28:22 +00002751 /// struct-declarator: declarator
2752 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002753 if (Tok.isNot(tok::colon)) {
2754 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2755 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002756 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002757 }
Mike Stump1eb44332009-09-09 15:08:12 +00002758
Chris Lattner04d66662007-10-09 17:33:22 +00002759 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002760 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002761 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002762 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002763 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002764 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002765 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002766 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002767
Steve Naroff28a7ca82007-08-20 22:28:22 +00002768 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002769 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002770
John McCallbdd563e2009-11-03 02:38:08 +00002771 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002772 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002773 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002774
Steve Naroff28a7ca82007-08-20 22:28:22 +00002775 // If we don't have a comma, it is either the end of the list (a ';')
2776 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002777 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002778 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002779
Steve Naroff28a7ca82007-08-20 22:28:22 +00002780 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002781 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002782
John McCallbdd563e2009-11-03 02:38:08 +00002783 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002784 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002785}
2786
2787/// ParseStructUnionBody
2788/// struct-contents:
2789/// struct-declaration-list
2790/// [EXT] empty
2791/// [GNU] "struct-declaration-list" without terminatoring ';'
2792/// struct-declaration-list:
2793/// struct-declaration
2794/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002795/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002796///
Reid Spencer5f016e22007-07-11 17:01:13 +00002797void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002798 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002799 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2800 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002801
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002802 BalancedDelimiterTracker T(*this, tok::l_brace);
2803 if (T.consumeOpen())
2804 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002805
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002806 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002807 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002808
Reid Spencer5f016e22007-07-11 17:01:13 +00002809 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2810 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002811 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002812 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2813 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2814 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002815
Chris Lattner5f9e2722011-07-23 10:55:15 +00002816 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002817
Reid Spencer5f016e22007-07-11 17:01:13 +00002818 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002819 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002820 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002821
Reid Spencer5f016e22007-07-11 17:01:13 +00002822 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002823 if (Tok.is(tok::semi)) {
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002824 ConsumeExtraSemi(InsideStruct,
2825 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Reid Spencer5f016e22007-07-11 17:01:13 +00002826 continue;
2827 }
Chris Lattnere1359422008-04-10 06:46:29 +00002828
2829 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002830 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002831
John McCallbdd563e2009-11-03 02:38:08 +00002832 if (!Tok.is(tok::at)) {
2833 struct CFieldCallback : FieldCallback {
2834 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002835 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002836 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002837
John McCalld226f652010-08-21 09:40:31 +00002838 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002839 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002840 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2841
John McCalld226f652010-08-21 09:40:31 +00002842 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002843 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002844 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002845 FD.D.getDeclSpec().getSourceRange().getBegin(),
2846 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002847 FieldDecls.push_back(Field);
2848 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002849 }
John McCallbdd563e2009-11-03 02:38:08 +00002850 } Callback(*this, TagDecl, FieldDecls);
2851
2852 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002853 } else { // Handle @defs
2854 ConsumeToken();
2855 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2856 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002857 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002858 continue;
2859 }
2860 ConsumeToken();
2861 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2862 if (!Tok.is(tok::identifier)) {
2863 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002864 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002865 continue;
2866 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002867 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002868 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002869 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002870 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2871 ConsumeToken();
2872 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002873 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002874
Chris Lattner04d66662007-10-09 17:33:22 +00002875 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002876 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002877 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002878 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002879 break;
2880 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002881 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2882 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002883 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002884 // If we stopped at a ';', eat it.
2885 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002886 }
2887 }
Mike Stump1eb44332009-09-09 15:08:12 +00002888
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002889 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002890
John McCall0b7e6782011-03-24 11:26:52 +00002891 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002892 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002893 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002894
Douglas Gregor23c94db2010-07-02 17:43:08 +00002895 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002896 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002897 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002898 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002899 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002900 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2901 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002902}
2903
Reid Spencer5f016e22007-07-11 17:01:13 +00002904/// ParseEnumSpecifier
2905/// enum-specifier: [C99 6.7.2.2]
2906/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002907///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002908/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2909/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002910/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2911/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002912/// 'enum' identifier
2913/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002914///
Richard Smith1af83c42012-03-23 03:33:32 +00002915/// [C++11] enum-head '{' enumerator-list[opt] '}'
2916/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002917///
Richard Smith1af83c42012-03-23 03:33:32 +00002918/// enum-head: [C++11]
2919/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2920/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2921/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002922///
Richard Smith1af83c42012-03-23 03:33:32 +00002923/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002924/// 'enum'
2925/// 'enum' 'class'
2926/// 'enum' 'struct'
2927///
Richard Smith1af83c42012-03-23 03:33:32 +00002928/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002929/// ':' type-specifier-seq
2930///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002931/// [C++] elaborated-type-specifier:
2932/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2933///
Chris Lattner4c97d762009-04-12 21:49:30 +00002934void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002935 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002936 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002937 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002938 if (Tok.is(tok::code_completion)) {
2939 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002940 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002941 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002942 }
John McCall57c13002011-07-06 05:58:41 +00002943
Richard Smithbdad7a22012-01-10 01:33:14 +00002944 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002945 bool IsScopedUsingClassTag = false;
2946
David Blaikie4e4d0842012-03-11 07:00:24 +00002947 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002948 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002949 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002950 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002951 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002952 }
Richard Smith1af83c42012-03-23 03:33:32 +00002953
John McCall13489672012-05-07 06:16:58 +00002954 // C++11 [temp.explicit]p12:
2955 // The usual access controls do not apply to names used to specify
2956 // explicit instantiations.
2957 // We extend this to also cover explicit specializations. Note that
2958 // we don't suppress if this turns out to be an elaborated type
2959 // specifier.
2960 bool shouldDelayDiagsInTag =
2961 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2962 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2963 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00002964
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002965 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002966 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002967 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002968
Aaron Ballman6454a022012-03-01 04:09:28 +00002969 // If declspecs exist after tag, parse them.
2970 while (Tok.is(tok::kw___declspec))
2971 ParseMicrosoftDeclSpec(attrs);
2972
Richard Smith7796eb52012-03-12 08:56:40 +00002973 // Enum definitions should not be parsed in a trailing-return-type.
2974 bool AllowDeclaration = DSC != DSC_trailing;
2975
2976 bool AllowFixedUnderlyingType = AllowDeclaration &&
2977 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2978 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002979
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002980 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002981 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002982 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2983 // if a fixed underlying type is allowed.
2984 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2985
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002986 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2987 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002988 return;
2989
2990 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002991 Diag(Tok, diag::err_expected_ident);
2992 if (Tok.isNot(tok::l_brace)) {
2993 // Has no name and is not a definition.
2994 // Skip the rest of this declarator, up until the comma or semicolon.
2995 SkipUntil(tok::comma, true);
2996 return;
2997 }
2998 }
2999 }
Mike Stump1eb44332009-09-09 15:08:12 +00003000
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003001 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00003002 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003003 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003004 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003005
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003006 // Skip the rest of this declarator, up until the comma or semicolon.
3007 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003008 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003009 }
Mike Stump1eb44332009-09-09 15:08:12 +00003010
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003011 // If an identifier is present, consume and remember it.
3012 IdentifierInfo *Name = 0;
3013 SourceLocation NameLoc;
3014 if (Tok.is(tok::identifier)) {
3015 Name = Tok.getIdentifierInfo();
3016 NameLoc = ConsumeToken();
3017 }
Mike Stump1eb44332009-09-09 15:08:12 +00003018
Richard Smithbdad7a22012-01-10 01:33:14 +00003019 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003020 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3021 // declaration of a scoped enumeration.
3022 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003023 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003024 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003025 }
3026
John McCall13489672012-05-07 06:16:58 +00003027 // Okay, end the suppression area. We'll decide whether to emit the
3028 // diagnostics in a second.
3029 if (shouldDelayDiagsInTag)
3030 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003031
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003032 TypeResult BaseType;
3033
Douglas Gregora61b3e72010-12-01 17:42:47 +00003034 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00003035 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003036 bool PossibleBitfield = false;
3037 if (getCurScope()->getFlags() & Scope::ClassScope) {
3038 // If we're in class scope, this can either be an enum declaration with
3039 // an underlying type, or a declaration of a bitfield member. We try to
3040 // use a simple disambiguation scheme first to catch the common cases
3041 // (integer literal, sizeof); if it's still ambiguous, we then consider
3042 // anything that's a simple-type-specifier followed by '(' as an
3043 // expression. This suffices because function types are not valid
3044 // underlying types anyway.
3045 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
3046 // If the next token starts an expression, we know we're parsing a
3047 // bit-field. This is the common case.
3048 if (TPR == TPResult::True())
3049 PossibleBitfield = true;
3050 // If the next token starts a type-specifier-seq, it may be either a
3051 // a fixed underlying type or the start of a function-style cast in C++;
3052 // lookahead one more token to see if it's obvious that we have a
3053 // fixed underlying type.
3054 else if (TPR == TPResult::False() &&
3055 GetLookAheadToken(2).getKind() == tok::semi) {
3056 // Consume the ':'.
3057 ConsumeToken();
3058 } else {
3059 // We have the start of a type-specifier-seq, so we have to perform
3060 // tentative parsing to determine whether we have an expression or a
3061 // type.
3062 TentativeParsingAction TPA(*this);
3063
3064 // Consume the ':'.
3065 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003066
3067 // If we see a type specifier followed by an open-brace, we have an
3068 // ambiguity between an underlying type and a C++11 braced
3069 // function-style cast. Resolve this by always treating it as an
3070 // underlying type.
3071 // FIXME: The standard is not entirely clear on how to disambiguate in
3072 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003073 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003074 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003075 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003076 // We'll parse this as a bitfield later.
3077 PossibleBitfield = true;
3078 TPA.Revert();
3079 } else {
3080 // We have a type-specifier-seq.
3081 TPA.Commit();
3082 }
3083 }
3084 } else {
3085 // Consume the ':'.
3086 ConsumeToken();
3087 }
3088
3089 if (!PossibleBitfield) {
3090 SourceRange Range;
3091 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00003092
David Blaikie4e4d0842012-03-11 07:00:24 +00003093 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00003094 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
3095 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00003096 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003097 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003098 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003099 }
3100
Richard Smithbdad7a22012-01-10 01:33:14 +00003101 // There are four options here. If we have 'friend enum foo;' then this is a
3102 // friend declaration, and cannot have an accompanying definition. If we have
3103 // 'enum foo;', then this is a forward declaration. If we have
3104 // 'enum foo {...' then this is a definition. Otherwise we have something
3105 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003106 //
3107 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3108 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3109 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3110 //
John McCallf312b1e2010-08-26 23:41:50 +00003111 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003112 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003113 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003114 } else if (Tok.is(tok::l_brace)) {
3115 if (DS.isFriendSpecified()) {
3116 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3117 << SourceRange(DS.getFriendSpecLoc());
3118 ConsumeBrace();
3119 SkipUntil(tok::r_brace);
3120 TUK = Sema::TUK_Friend;
3121 } else {
3122 TUK = Sema::TUK_Definition;
3123 }
3124 } else if (Tok.is(tok::semi) && DSC != DSC_type_specifier) {
3125 TUK = (DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration);
3126 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003127 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003128 }
3129
3130 // If this is an elaborated type specifier, and we delayed
3131 // diagnostics before, just merge them into the current pool.
3132 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3133 diagsFromTag.redelay();
3134 }
Richard Smith1af83c42012-03-23 03:33:32 +00003135
3136 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003137 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003138 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003139 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3140 // Skip the rest of this declarator, up until the comma or semicolon.
3141 Diag(Tok, diag::err_enum_template);
3142 SkipUntil(tok::comma, true);
3143 return;
3144 }
3145
3146 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3147 // Enumerations can't be explicitly instantiated.
3148 DS.SetTypeSpecError();
3149 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3150 return;
3151 }
3152
3153 assert(TemplateInfo.TemplateParams && "no template parameters");
3154 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3155 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003156 }
Richard Smith1af83c42012-03-23 03:33:32 +00003157
Douglas Gregorb9075602011-02-22 02:55:24 +00003158 if (!Name && TUK != Sema::TUK_Definition) {
3159 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003160
Douglas Gregorb9075602011-02-22 02:55:24 +00003161 // Skip the rest of this declarator, up until the comma or semicolon.
3162 SkipUntil(tok::comma, true);
3163 return;
3164 }
Richard Smith1af83c42012-03-23 03:33:32 +00003165
Douglas Gregor402abb52009-05-28 23:31:59 +00003166 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003167 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003168 const char *PrevSpec = 0;
3169 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003170 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003171 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003172 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003173 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003174 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003175
Douglas Gregor48c89f42010-04-24 16:38:41 +00003176 if (IsDependent) {
3177 // This enum has a dependent nested-name-specifier. Handle it as a
3178 // dependent tag.
3179 if (!Name) {
3180 DS.SetTypeSpecError();
3181 Diag(Tok, diag::err_expected_type_name_after_typename);
3182 return;
3183 }
3184
Douglas Gregor23c94db2010-07-02 17:43:08 +00003185 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003186 TUK, SS, Name, StartLoc,
3187 NameLoc);
3188 if (Type.isInvalid()) {
3189 DS.SetTypeSpecError();
3190 return;
3191 }
3192
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003193 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3194 NameLoc.isValid() ? NameLoc : StartLoc,
3195 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003196 Diag(StartLoc, DiagID) << PrevSpec;
3197
3198 return;
3199 }
Mike Stump1eb44332009-09-09 15:08:12 +00003200
John McCalld226f652010-08-21 09:40:31 +00003201 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003202 // The action failed to produce an enumeration tag. If this is a
3203 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003204 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003205 ConsumeBrace();
3206 SkipUntil(tok::r_brace);
3207 }
3208
3209 DS.SetTypeSpecError();
3210 return;
3211 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003212
Richard Smith7796eb52012-03-12 08:56:40 +00003213 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
John McCall13489672012-05-07 06:16:58 +00003214 ParseEnumBody(StartLoc, TagDecl);
Richard Smithbdad7a22012-01-10 01:33:14 +00003215 }
Mike Stump1eb44332009-09-09 15:08:12 +00003216
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003217 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3218 NameLoc.isValid() ? NameLoc : StartLoc,
3219 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003220 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003221}
3222
3223/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3224/// enumerator-list:
3225/// enumerator
3226/// enumerator-list ',' enumerator
3227/// enumerator:
3228/// enumeration-constant
3229/// enumeration-constant '=' constant-expression
3230/// enumeration-constant:
3231/// identifier
3232///
John McCalld226f652010-08-21 09:40:31 +00003233void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003234 // Enter the scope of the enum body and start the definition.
3235 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003236 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003237
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003238 BalancedDelimiterTracker T(*this, tok::l_brace);
3239 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003240
Chris Lattner7946dd32007-08-27 17:24:30 +00003241 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003242 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003243 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003244
Chris Lattner5f9e2722011-07-23 10:55:15 +00003245 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003246
John McCalld226f652010-08-21 09:40:31 +00003247 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003248
Reid Spencer5f016e22007-07-11 17:01:13 +00003249 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003250 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003251 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3252 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003253
John McCall5b629aa2010-10-22 23:36:17 +00003254 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003255 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003256 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003257
Reid Spencer5f016e22007-07-11 17:01:13 +00003258 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003259 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003260 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003261
Chris Lattner04d66662007-10-09 17:33:22 +00003262 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003263 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003264 AssignedVal = ParseConstantExpression();
3265 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003266 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003267 }
Mike Stump1eb44332009-09-09 15:08:12 +00003268
Reid Spencer5f016e22007-07-11 17:01:13 +00003269 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003270 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3271 LastEnumConstDecl,
3272 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003273 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003274 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003275 PD.complete(EnumConstDecl);
3276
Reid Spencer5f016e22007-07-11 17:01:13 +00003277 EnumConstantDecls.push_back(EnumConstDecl);
3278 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003279
Douglas Gregor751f6922010-09-07 14:51:08 +00003280 if (Tok.is(tok::identifier)) {
3281 // We're missing a comma between enumerators.
3282 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3283 Diag(Loc, diag::err_enumerator_list_missing_comma)
3284 << FixItHint::CreateInsertion(Loc, ", ");
3285 continue;
3286 }
3287
Chris Lattner04d66662007-10-09 17:33:22 +00003288 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003289 break;
3290 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003291
Richard Smith7fe62082011-10-15 05:09:34 +00003292 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003293 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003294 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00003295 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00003296 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003297 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003298 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3299 << FixItHint::CreateRemoval(CommaLoc);
3300 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003301 }
Mike Stump1eb44332009-09-09 15:08:12 +00003302
Reid Spencer5f016e22007-07-11 17:01:13 +00003303 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003304 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003305
Reid Spencer5f016e22007-07-11 17:01:13 +00003306 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003307 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003308 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003309
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003310 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3311 EnumDecl, EnumConstantDecls.data(),
3312 EnumConstantDecls.size(), getCurScope(),
3313 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003314
Douglas Gregor72de6672009-01-08 20:45:30 +00003315 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003316 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3317 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003318}
3319
3320/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003321/// start of a type-qualifier-list.
3322bool Parser::isTypeQualifier() const {
3323 switch (Tok.getKind()) {
3324 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003325
3326 // type-qualifier only in OpenCL
3327 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003328 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003329
Steve Naroff5f8aa692008-02-11 23:15:56 +00003330 // type-qualifier
3331 case tok::kw_const:
3332 case tok::kw_volatile:
3333 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003334 case tok::kw___private:
3335 case tok::kw___local:
3336 case tok::kw___global:
3337 case tok::kw___constant:
3338 case tok::kw___read_only:
3339 case tok::kw___read_write:
3340 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003341 return true;
3342 }
3343}
3344
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003345/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3346/// is definitely a type-specifier. Return false if it isn't part of a type
3347/// specifier or if we're not sure.
3348bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3349 switch (Tok.getKind()) {
3350 default: return false;
3351 // type-specifiers
3352 case tok::kw_short:
3353 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003354 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003355 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003356 case tok::kw_signed:
3357 case tok::kw_unsigned:
3358 case tok::kw__Complex:
3359 case tok::kw__Imaginary:
3360 case tok::kw_void:
3361 case tok::kw_char:
3362 case tok::kw_wchar_t:
3363 case tok::kw_char16_t:
3364 case tok::kw_char32_t:
3365 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003366 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003367 case tok::kw_float:
3368 case tok::kw_double:
3369 case tok::kw_bool:
3370 case tok::kw__Bool:
3371 case tok::kw__Decimal32:
3372 case tok::kw__Decimal64:
3373 case tok::kw__Decimal128:
3374 case tok::kw___vector:
3375
3376 // struct-or-union-specifier (C99) or class-specifier (C++)
3377 case tok::kw_class:
3378 case tok::kw_struct:
3379 case tok::kw_union:
3380 // enum-specifier
3381 case tok::kw_enum:
3382
3383 // typedef-name
3384 case tok::annot_typename:
3385 return true;
3386 }
3387}
3388
Steve Naroff5f8aa692008-02-11 23:15:56 +00003389/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003390/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003391bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003392 switch (Tok.getKind()) {
3393 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003394
Chris Lattner166a8fc2009-01-04 23:41:41 +00003395 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003396 if (TryAltiVecVectorToken())
3397 return true;
3398 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003399 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003400 // Annotate typenames and C++ scope specifiers. If we get one, just
3401 // recurse to handle whatever we get.
3402 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003403 return true;
3404 if (Tok.is(tok::identifier))
3405 return false;
3406 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003407
Chris Lattner166a8fc2009-01-04 23:41:41 +00003408 case tok::coloncolon: // ::foo::bar
3409 if (NextToken().is(tok::kw_new) || // ::new
3410 NextToken().is(tok::kw_delete)) // ::delete
3411 return false;
3412
Chris Lattner166a8fc2009-01-04 23:41:41 +00003413 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003414 return true;
3415 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003416
Reid Spencer5f016e22007-07-11 17:01:13 +00003417 // GNU attributes support.
3418 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003419 // GNU typeof support.
3420 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003421
Reid Spencer5f016e22007-07-11 17:01:13 +00003422 // type-specifiers
3423 case tok::kw_short:
3424 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003425 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003426 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003427 case tok::kw_signed:
3428 case tok::kw_unsigned:
3429 case tok::kw__Complex:
3430 case tok::kw__Imaginary:
3431 case tok::kw_void:
3432 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003433 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003434 case tok::kw_char16_t:
3435 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003436 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003437 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003438 case tok::kw_float:
3439 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003440 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003441 case tok::kw__Bool:
3442 case tok::kw__Decimal32:
3443 case tok::kw__Decimal64:
3444 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003445 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003446
Chris Lattner99dc9142008-04-13 18:59:07 +00003447 // struct-or-union-specifier (C99) or class-specifier (C++)
3448 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003449 case tok::kw_struct:
3450 case tok::kw_union:
3451 // enum-specifier
3452 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003453
Reid Spencer5f016e22007-07-11 17:01:13 +00003454 // type-qualifier
3455 case tok::kw_const:
3456 case tok::kw_volatile:
3457 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003458
3459 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003460 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003461 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003462
Chris Lattner7c186be2008-10-20 00:25:30 +00003463 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3464 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003465 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003466
Steve Naroff239f0732008-12-25 14:16:32 +00003467 case tok::kw___cdecl:
3468 case tok::kw___stdcall:
3469 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003470 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003471 case tok::kw___w64:
3472 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003473 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003474 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003475 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003476
3477 case tok::kw___private:
3478 case tok::kw___local:
3479 case tok::kw___global:
3480 case tok::kw___constant:
3481 case tok::kw___read_only:
3482 case tok::kw___read_write:
3483 case tok::kw___write_only:
3484
Eli Friedman290eeb02009-06-08 23:27:34 +00003485 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003486
3487 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003488 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003489
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003490 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003491 case tok::kw__Atomic:
3492 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003493 }
3494}
3495
3496/// isDeclarationSpecifier() - Return true if the current token is part of a
3497/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003498///
3499/// \param DisambiguatingWithExpression True to indicate that the purpose of
3500/// this check is to disambiguate between an expression and a declaration.
3501bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003502 switch (Tok.getKind()) {
3503 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003504
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003505 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003506 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003507
Chris Lattner166a8fc2009-01-04 23:41:41 +00003508 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003509 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003510 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003511 return false;
John Thompson82287d12010-02-05 00:12:22 +00003512 if (TryAltiVecVectorToken())
3513 return true;
3514 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003515 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003516 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003517 // Annotate typenames and C++ scope specifiers. If we get one, just
3518 // recurse to handle whatever we get.
3519 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003520 return true;
3521 if (Tok.is(tok::identifier))
3522 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003523
3524 // If we're in Objective-C and we have an Objective-C class type followed
3525 // by an identifier and then either ':' or ']', in a place where an
3526 // expression is permitted, then this is probably a class message send
3527 // missing the initial '['. In this case, we won't consider this to be
3528 // the start of a declaration.
3529 if (DisambiguatingWithExpression &&
3530 isStartOfObjCClassMessageMissingOpenBracket())
3531 return false;
3532
John McCall9ba61662010-02-26 08:45:28 +00003533 return isDeclarationSpecifier();
3534
Chris Lattner166a8fc2009-01-04 23:41:41 +00003535 case tok::coloncolon: // ::foo::bar
3536 if (NextToken().is(tok::kw_new) || // ::new
3537 NextToken().is(tok::kw_delete)) // ::delete
3538 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003539
Chris Lattner166a8fc2009-01-04 23:41:41 +00003540 // Annotate typenames and C++ scope specifiers. If we get one, just
3541 // recurse to handle whatever we get.
3542 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003543 return true;
3544 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003545
Reid Spencer5f016e22007-07-11 17:01:13 +00003546 // storage-class-specifier
3547 case tok::kw_typedef:
3548 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003549 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003550 case tok::kw_static:
3551 case tok::kw_auto:
3552 case tok::kw_register:
3553 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003554
Douglas Gregor8d267c52011-09-09 02:06:17 +00003555 // Modules
3556 case tok::kw___module_private__:
3557
Reid Spencer5f016e22007-07-11 17:01:13 +00003558 // type-specifiers
3559 case tok::kw_short:
3560 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003561 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003562 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003563 case tok::kw_signed:
3564 case tok::kw_unsigned:
3565 case tok::kw__Complex:
3566 case tok::kw__Imaginary:
3567 case tok::kw_void:
3568 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003569 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003570 case tok::kw_char16_t:
3571 case tok::kw_char32_t:
3572
Reid Spencer5f016e22007-07-11 17:01:13 +00003573 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003574 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003575 case tok::kw_float:
3576 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003577 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003578 case tok::kw__Bool:
3579 case tok::kw__Decimal32:
3580 case tok::kw__Decimal64:
3581 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003582 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003583
Chris Lattner99dc9142008-04-13 18:59:07 +00003584 // struct-or-union-specifier (C99) or class-specifier (C++)
3585 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003586 case tok::kw_struct:
3587 case tok::kw_union:
3588 // enum-specifier
3589 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003590
Reid Spencer5f016e22007-07-11 17:01:13 +00003591 // type-qualifier
3592 case tok::kw_const:
3593 case tok::kw_volatile:
3594 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003595
Reid Spencer5f016e22007-07-11 17:01:13 +00003596 // function-specifier
3597 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003598 case tok::kw_virtual:
3599 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003600
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003601 // static_assert-declaration
3602 case tok::kw__Static_assert:
3603
Chris Lattner1ef08762007-08-09 17:01:07 +00003604 // GNU typeof support.
3605 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003606
Chris Lattner1ef08762007-08-09 17:01:07 +00003607 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003608 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003609 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003610
Francois Pichete3d49b42011-06-19 08:02:06 +00003611 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003612 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003613 return true;
3614
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003615 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003616 case tok::kw__Atomic:
3617 return true;
3618
Chris Lattnerf3948c42008-07-26 03:38:44 +00003619 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3620 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003621 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003622
Douglas Gregord9d75e52011-04-27 05:41:15 +00003623 // typedef-name
3624 case tok::annot_typename:
3625 return !DisambiguatingWithExpression ||
3626 !isStartOfObjCClassMessageMissingOpenBracket();
3627
Steve Naroff47f52092009-01-06 19:34:12 +00003628 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003629 case tok::kw___cdecl:
3630 case tok::kw___stdcall:
3631 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003632 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003633 case tok::kw___w64:
3634 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003635 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003636 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003637 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003638 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003639
3640 case tok::kw___private:
3641 case tok::kw___local:
3642 case tok::kw___global:
3643 case tok::kw___constant:
3644 case tok::kw___read_only:
3645 case tok::kw___read_write:
3646 case tok::kw___write_only:
3647
Eli Friedman290eeb02009-06-08 23:27:34 +00003648 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003649 }
3650}
3651
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003652bool Parser::isConstructorDeclarator() {
3653 TentativeParsingAction TPA(*this);
3654
3655 // Parse the C++ scope specifier.
3656 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003657 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3658 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003659 TPA.Revert();
3660 return false;
3661 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003662
3663 // Parse the constructor name.
3664 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3665 // We already know that we have a constructor name; just consume
3666 // the token.
3667 ConsumeToken();
3668 } else {
3669 TPA.Revert();
3670 return false;
3671 }
3672
Richard Smith22592862012-03-27 23:05:05 +00003673 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003674 if (Tok.isNot(tok::l_paren)) {
3675 TPA.Revert();
3676 return false;
3677 }
3678 ConsumeParen();
3679
Richard Smith22592862012-03-27 23:05:05 +00003680 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3681 // that we have a constructor.
3682 if (Tok.is(tok::r_paren) ||
3683 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003684 TPA.Revert();
3685 return true;
3686 }
3687
3688 // If we need to, enter the specified scope.
3689 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003690 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003691 DeclScopeObj.EnterDeclaratorScope();
3692
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003693 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003694 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003695 MaybeParseMicrosoftAttributes(Attrs);
3696
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003697 // Check whether the next token(s) are part of a declaration
3698 // specifier, in which case we have the start of a parameter and,
3699 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003700 bool IsConstructor = false;
3701 if (isDeclarationSpecifier())
3702 IsConstructor = true;
3703 else if (Tok.is(tok::identifier) ||
3704 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3705 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3706 // This might be a parenthesized member name, but is more likely to
3707 // be a constructor declaration with an invalid argument type. Keep
3708 // looking.
3709 if (Tok.is(tok::annot_cxxscope))
3710 ConsumeToken();
3711 ConsumeToken();
3712
3713 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003714 // which must have one of the following syntactic forms (see the
3715 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003716 switch (Tok.getKind()) {
3717 case tok::l_paren:
3718 // C(X ( int));
3719 case tok::l_square:
3720 // C(X [ 5]);
3721 // C(X [ [attribute]]);
3722 case tok::coloncolon:
3723 // C(X :: Y);
3724 // C(X :: *p);
3725 case tok::r_paren:
3726 // C(X )
3727 // Assume this isn't a constructor, rather than assuming it's a
3728 // constructor with an unnamed parameter of an ill-formed type.
3729 break;
3730
3731 default:
3732 IsConstructor = true;
3733 break;
3734 }
3735 }
3736
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003737 TPA.Revert();
3738 return IsConstructor;
3739}
Reid Spencer5f016e22007-07-11 17:01:13 +00003740
3741/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003742/// type-qualifier-list: [C99 6.7.5]
3743/// type-qualifier
3744/// [vendor] attributes
3745/// [ only if VendorAttributesAllowed=true ]
3746/// type-qualifier-list type-qualifier
3747/// [vendor] type-qualifier-list attributes
3748/// [ only if VendorAttributesAllowed=true ]
3749/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3750/// [ only if CXX0XAttributesAllowed=true ]
3751/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003752///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003753void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3754 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003755 bool CXX11AttributesAllowed) {
3756 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003757 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003758 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003759 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003760 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003761 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003762
3763 SourceLocation EndLoc;
3764
Reid Spencer5f016e22007-07-11 17:01:13 +00003765 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003766 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003767 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003768 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003769 SourceLocation Loc = Tok.getLocation();
3770
3771 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003772 case tok::code_completion:
3773 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003774 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003775
Reid Spencer5f016e22007-07-11 17:01:13 +00003776 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003777 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003778 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003779 break;
3780 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003781 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003782 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003783 break;
3784 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003785 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003786 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003787 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003788
3789 // OpenCL qualifiers:
3790 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003791 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003792 goto DoneWithTypeQuals;
3793 case tok::kw___private:
3794 case tok::kw___global:
3795 case tok::kw___local:
3796 case tok::kw___constant:
3797 case tok::kw___read_only:
3798 case tok::kw___write_only:
3799 case tok::kw___read_write:
3800 ParseOpenCLQualifiers(DS);
3801 break;
3802
Eli Friedman290eeb02009-06-08 23:27:34 +00003803 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003804 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003805 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003806 case tok::kw___cdecl:
3807 case tok::kw___stdcall:
3808 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003809 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003810 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003811 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003812 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003813 continue;
3814 }
3815 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003816 case tok::kw___pascal:
3817 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003818 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003819 continue;
3820 }
3821 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003822 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003823 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003824 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003825 continue; // do *not* consume the next token!
3826 }
3827 // otherwise, FALL THROUGH!
3828 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003829 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003830 // If this is not a type-qualifier token, we're done reading type
3831 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003832 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003833 if (EndLoc.isValid())
3834 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003835 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003836 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003837
Reid Spencer5f016e22007-07-11 17:01:13 +00003838 // If the specifier combination wasn't legal, issue a diagnostic.
3839 if (isInvalid) {
3840 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003841 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003842 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003843 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003844 }
3845}
3846
3847
3848/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3849///
3850void Parser::ParseDeclarator(Declarator &D) {
3851 /// This implements the 'declarator' production in the C grammar, then checks
3852 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003853 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003854}
3855
Richard Smith9988f282012-03-29 01:16:42 +00003856static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3857 if (Kind == tok::star || Kind == tok::caret)
3858 return true;
3859
3860 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3861 if (!Lang.CPlusPlus)
3862 return false;
3863
3864 return Kind == tok::amp || Kind == tok::ampamp;
3865}
3866
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003867/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3868/// is parsed by the function passed to it. Pass null, and the direct-declarator
3869/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003870/// ptr-operator production.
3871///
Richard Smith0706df42011-10-19 21:33:05 +00003872/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003873/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3874/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003875///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003876/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3877/// [C] pointer[opt] direct-declarator
3878/// [C++] direct-declarator
3879/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003880///
3881/// pointer: [C99 6.7.5]
3882/// '*' type-qualifier-list[opt]
3883/// '*' type-qualifier-list[opt] pointer
3884///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003885/// ptr-operator:
3886/// '*' cv-qualifier-seq[opt]
3887/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003888/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003889/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003890/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003891/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003892void Parser::ParseDeclaratorInternal(Declarator &D,
3893 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003894 if (Diags.hasAllExtensionsSilenced())
3895 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003896
Sebastian Redlf30208a2009-01-24 21:16:55 +00003897 // C++ member pointers start with a '::' or a nested-name.
3898 // Member pointers get special handling, since there's no place for the
3899 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003900 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003901 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3902 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003903 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3904 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003905 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003906 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003907
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003908 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003909 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003910 // The scope spec really belongs to the direct-declarator.
3911 D.getCXXScopeSpec() = SS;
3912 if (DirectDeclParser)
3913 (this->*DirectDeclParser)(D);
3914 return;
3915 }
3916
3917 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003918 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003919 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003920 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003921 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003922
3923 // Recurse to parse whatever is left.
3924 ParseDeclaratorInternal(D, DirectDeclParser);
3925
3926 // Sema will have to catch (syntactically invalid) pointers into global
3927 // scope. It has to catch pointers into namespace scope anyway.
3928 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003929 Loc),
3930 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003931 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003932 return;
3933 }
3934 }
3935
3936 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003937 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00003938 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003939 if (DirectDeclParser)
3940 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003941 return;
3942 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003943
Sebastian Redl05532f22009-03-15 22:02:01 +00003944 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3945 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003946 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003947 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003948
Chris Lattner9af55002009-03-27 04:18:06 +00003949 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003950 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003951 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003952
Richard Smith6ee326a2012-04-10 01:32:12 +00003953 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00003954 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003955 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003956
Reid Spencer5f016e22007-07-11 17:01:13 +00003957 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003958 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003959 if (Kind == tok::star)
3960 // Remember that we parsed a pointer type, and remember the type-quals.
3961 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003962 DS.getConstSpecLoc(),
3963 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003964 DS.getRestrictSpecLoc()),
3965 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003966 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003967 else
3968 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003969 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003970 Loc),
3971 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003972 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003973 } else {
3974 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003975 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003976
Sebastian Redl743de1f2009-03-23 00:00:23 +00003977 // Complain about rvalue references in C++03, but then go on and build
3978 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003979 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003980 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003981 diag::warn_cxx98_compat_rvalue_reference :
3982 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003983
Richard Smith6ee326a2012-04-10 01:32:12 +00003984 // GNU-style and C++11 attributes are allowed here, as is restrict.
3985 ParseTypeQualifierListOpt(DS);
3986 D.ExtendWithDeclSpec(DS);
3987
Reid Spencer5f016e22007-07-11 17:01:13 +00003988 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3989 // cv-qualifiers are introduced through the use of a typedef or of a
3990 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00003991 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3992 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3993 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003994 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003995 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3996 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003997 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003998 }
3999
4000 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004001 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00004002
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004003 if (D.getNumTypeObjects() > 0) {
4004 // C++ [dcl.ref]p4: There shall be no references to references.
4005 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4006 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004007 if (const IdentifierInfo *II = D.getIdentifier())
4008 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4009 << II;
4010 else
4011 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4012 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004013
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004014 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004015 // can go ahead and build the (technically ill-formed)
4016 // declarator: reference collapsing will take care of it.
4017 }
4018 }
4019
Reid Spencer5f016e22007-07-11 17:01:13 +00004020 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00004021 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004022 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004023 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004024 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004025 }
4026}
4027
Richard Smith9988f282012-03-29 01:16:42 +00004028static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4029 SourceLocation EllipsisLoc) {
4030 if (EllipsisLoc.isValid()) {
4031 FixItHint Insertion;
4032 if (!D.getEllipsisLoc().isValid()) {
4033 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4034 D.setEllipsisLoc(EllipsisLoc);
4035 }
4036 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4037 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4038 }
4039}
4040
Reid Spencer5f016e22007-07-11 17:01:13 +00004041/// ParseDirectDeclarator
4042/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004043/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004044/// '(' declarator ')'
4045/// [GNU] '(' attributes declarator ')'
4046/// [C90] direct-declarator '[' constant-expression[opt] ']'
4047/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4048/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4049/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4050/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004051/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4052/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004053/// direct-declarator '(' parameter-type-list ')'
4054/// direct-declarator '(' identifier-list[opt] ')'
4055/// [GNU] direct-declarator '(' parameter-forward-declarations
4056/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004057/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4058/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004059/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4060/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4061/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004062/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004063/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004064///
4065/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004066/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004067/// '::'[opt] nested-name-specifier[opt] type-name
4068///
4069/// id-expression: [C++ 5.1]
4070/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004071/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004072///
4073/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004074/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004075/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004076/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004077/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004078/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004079///
Richard Smith5d8388c2012-03-27 01:42:32 +00004080/// Note, any additional constructs added here may need corresponding changes
4081/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004082void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004083 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004084
David Blaikie4e4d0842012-03-11 07:00:24 +00004085 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004086 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004087 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004088 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4089 D.getContext() == Declarator::MemberContext;
4090 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
4091 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004092 }
4093
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004094 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004095 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004096 // Change the declaration context for name lookup, until this function
4097 // is exited (and the declarator has been parsed).
4098 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004099 }
4100
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004101 // C++0x [dcl.fct]p14:
4102 // There is a syntactic ambiguity when an ellipsis occurs at the end
4103 // of a parameter-declaration-clause without a preceding comma. In
4104 // this case, the ellipsis is parsed as part of the
4105 // abstract-declarator if the type of the parameter names a template
4106 // parameter pack that has not been expanded; otherwise, it is parsed
4107 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004108 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004109 !((D.getContext() == Declarator::PrototypeContext ||
4110 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004111 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00004112 !Actions.containsUnexpandedParameterPacks(D))) {
4113 SourceLocation EllipsisLoc = ConsumeToken();
4114 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4115 // The ellipsis was put in the wrong place. Recover, and explain to
4116 // the user what they should have done.
4117 ParseDeclarator(D);
4118 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4119 return;
4120 } else
4121 D.setEllipsisLoc(EllipsisLoc);
4122
4123 // The ellipsis can't be followed by a parenthesized declarator. We
4124 // check for that in ParseParenDeclarator, after we have disambiguated
4125 // the l_paren token.
4126 }
4127
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004128 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4129 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4130 // We found something that indicates the start of an unqualified-id.
4131 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004132 bool AllowConstructorName;
4133 if (D.getDeclSpec().hasTypeSpecifier())
4134 AllowConstructorName = false;
4135 else if (D.getCXXScopeSpec().isSet())
4136 AllowConstructorName =
4137 (D.getContext() == Declarator::FileContext ||
4138 (D.getContext() == Declarator::MemberContext &&
4139 D.getDeclSpec().isFriendSpecified()));
4140 else
4141 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4142
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004143 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004144 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4145 /*EnteringContext=*/true,
4146 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004147 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004148 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004149 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004150 D.getName()) ||
4151 // Once we're past the identifier, if the scope was bad, mark the
4152 // whole declarator bad.
4153 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004154 D.SetIdentifier(0, Tok.getLocation());
4155 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004156 } else {
4157 // Parsed the unqualified-id; update range information and move along.
4158 if (D.getSourceRange().getBegin().isInvalid())
4159 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4160 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004161 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004162 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004163 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004164 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004165 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004166 "There's a C++-specific check for tok::identifier above");
4167 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4168 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4169 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004170 goto PastIdentifier;
4171 }
Richard Smith9988f282012-03-29 01:16:42 +00004172
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004173 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004174 // direct-declarator: '(' declarator ')'
4175 // direct-declarator: '(' attributes declarator ')'
4176 // Example: 'char (*X)' or 'int (*XX)(void)'
4177 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004178
4179 // If the declarator was parenthesized, we entered the declarator
4180 // scope when parsing the parenthesized declarator, then exited
4181 // the scope already. Re-enter the scope, if we need to.
4182 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004183 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004184 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004185 if (!D.isInvalidType() &&
4186 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004187 // Change the declaration context for name lookup, until this function
4188 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004189 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004190 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004191 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004192 // This could be something simple like "int" (in which case the declarator
4193 // portion is empty), if an abstract-declarator is allowed.
4194 D.SetIdentifier(0, Tok.getLocation());
4195 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00004196 if (D.getContext() == Declarator::MemberContext)
4197 Diag(Tok, diag::err_expected_member_name_or_semi)
4198 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00004199 else if (getLangOpts().CPlusPlus)
4200 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004201 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004202 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004203 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004204 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004205 }
Mike Stump1eb44332009-09-09 15:08:12 +00004206
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004207 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004208 assert(D.isPastIdentifier() &&
4209 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004210
Richard Smith6ee326a2012-04-10 01:32:12 +00004211 // Don't parse attributes unless we have parsed an unparenthesized name.
4212 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004213 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004214
Reid Spencer5f016e22007-07-11 17:01:13 +00004215 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004216 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004217 // Enter function-declaration scope, limiting any declarators to the
4218 // function prototype scope, including parameter declarators.
4219 ParseScope PrototypeScope(this,
4220 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004221 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4222 // In such a case, check if we actually have a function declarator; if it
4223 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00004224 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00004225 // When not in file scope, warn for ambiguous function declarators, just
4226 // in case the author intended it as a variable definition.
4227 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4228 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4229 break;
4230 }
John McCall0b7e6782011-03-24 11:26:52 +00004231 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004232 BalancedDelimiterTracker T(*this, tok::l_paren);
4233 T.consumeOpen();
4234 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004235 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004236 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004237 ParseBracketDeclarator(D);
4238 } else {
4239 break;
4240 }
4241 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004242}
Reid Spencer5f016e22007-07-11 17:01:13 +00004243
Chris Lattneref4715c2008-04-06 05:45:57 +00004244/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4245/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004246/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004247/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4248///
4249/// direct-declarator:
4250/// '(' declarator ')'
4251/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004252/// direct-declarator '(' parameter-type-list ')'
4253/// direct-declarator '(' identifier-list[opt] ')'
4254/// [GNU] direct-declarator '(' parameter-forward-declarations
4255/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004256///
4257void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004258 BalancedDelimiterTracker T(*this, tok::l_paren);
4259 T.consumeOpen();
4260
Chris Lattneref4715c2008-04-06 05:45:57 +00004261 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004262
Chris Lattner7399ee02008-10-20 02:05:46 +00004263 // Eat any attributes before we look at whether this is a grouping or function
4264 // declarator paren. If this is a grouping paren, the attribute applies to
4265 // the type being built up, for example:
4266 // int (__attribute__(()) *x)(long y)
4267 // If this ends up not being a grouping paren, the attribute applies to the
4268 // first argument, for example:
4269 // int (__attribute__(()) int x)
4270 // In either case, we need to eat any attributes to be able to determine what
4271 // sort of paren this is.
4272 //
John McCall0b7e6782011-03-24 11:26:52 +00004273 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004274 bool RequiresArg = false;
4275 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004276 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004277
Chris Lattner7399ee02008-10-20 02:05:46 +00004278 // We require that the argument list (if this is a non-grouping paren) be
4279 // present even if the attribute list was empty.
4280 RequiresArg = true;
4281 }
Steve Naroff239f0732008-12-25 14:16:32 +00004282 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004283 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004284 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004285 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004286 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004287 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004288 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004289 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004290 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004291 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004292
Chris Lattneref4715c2008-04-06 05:45:57 +00004293 // If we haven't past the identifier yet (or where the identifier would be
4294 // stored, if this is an abstract declarator), then this is probably just
4295 // grouping parens. However, if this could be an abstract-declarator, then
4296 // this could also be the start of function arguments (consider 'void()').
4297 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004298
Chris Lattneref4715c2008-04-06 05:45:57 +00004299 if (!D.mayOmitIdentifier()) {
4300 // If this can't be an abstract-declarator, this *must* be a grouping
4301 // paren, because we haven't seen the identifier yet.
4302 isGrouping = true;
4303 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004304 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4305 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004306 isDeclarationSpecifier() || // 'int(int)' is a function.
4307 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004308 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4309 // considered to be a type, not a K&R identifier-list.
4310 isGrouping = false;
4311 } else {
4312 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4313 isGrouping = true;
4314 }
Mike Stump1eb44332009-09-09 15:08:12 +00004315
Chris Lattneref4715c2008-04-06 05:45:57 +00004316 // If this is a grouping paren, handle:
4317 // direct-declarator: '(' declarator ')'
4318 // direct-declarator: '(' attributes declarator ')'
4319 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004320 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4321 D.setEllipsisLoc(SourceLocation());
4322
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004323 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004324 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004325 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004326 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004327 T.consumeClose();
4328 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4329 T.getCloseLocation()),
4330 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004331
4332 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004333
4334 // An ellipsis cannot be placed outside parentheses.
4335 if (EllipsisLoc.isValid())
4336 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4337
Chris Lattneref4715c2008-04-06 05:45:57 +00004338 return;
4339 }
Mike Stump1eb44332009-09-09 15:08:12 +00004340
Chris Lattneref4715c2008-04-06 05:45:57 +00004341 // Okay, if this wasn't a grouping paren, it must be the start of a function
4342 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004343 // identifier (and remember where it would have been), then call into
4344 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004345 D.SetIdentifier(0, Tok.getLocation());
4346
David Blaikie42d6d0c2011-12-04 05:04:18 +00004347 // Enter function-declaration scope, limiting any declarators to the
4348 // function prototype scope, including parameter declarators.
4349 ParseScope PrototypeScope(this,
4350 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004351 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004352 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004353}
4354
4355/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4356/// declarator D up to a paren, which indicates that we are parsing function
4357/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004358///
Richard Smith6ee326a2012-04-10 01:32:12 +00004359/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4360/// immediately after the open paren - they should be considered to be the
4361/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004362///
Richard Smith6ee326a2012-04-10 01:32:12 +00004363/// If RequiresArg is true, then the first argument of the function is required
4364/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004365///
Richard Smith6ee326a2012-04-10 01:32:12 +00004366/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4367/// (C++11) ref-qualifier[opt], exception-specification[opt],
4368/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4369///
4370/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004371/// dynamic-exception-specification
4372/// noexcept-specification
4373///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004374void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004375 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004376 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004377 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004378 assert(getCurScope()->isFunctionPrototypeScope() &&
4379 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004380 // lparen is already consumed!
4381 assert(D.isPastIdentifier() && "Should not call before identifier!");
4382
4383 // This should be true when the function has typed arguments.
4384 // Otherwise, it is treated as a K&R-style function.
4385 bool HasProto = false;
4386 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004387 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004388 // Remember where we see an ellipsis, if any.
4389 SourceLocation EllipsisLoc;
4390
4391 DeclSpec DS(AttrFactory);
4392 bool RefQualifierIsLValueRef = true;
4393 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004394 SourceLocation ConstQualifierLoc;
4395 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004396 ExceptionSpecificationType ESpecType = EST_None;
4397 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004398 SmallVector<ParsedType, 2> DynamicExceptions;
4399 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004400 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004401 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004402 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004403
James Molloy16f1f712012-02-29 10:24:19 +00004404 Actions.ActOnStartFunctionDeclarator();
4405
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004406 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004407 if (isFunctionDeclaratorIdentifierList()) {
4408 if (RequiresArg)
4409 Diag(Tok, diag::err_argument_required_after_attribute);
4410
4411 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4412
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004413 Tracker.consumeClose();
4414 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004415 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004416 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004417 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004418 else if (RequiresArg)
4419 Diag(Tok, diag::err_argument_required_after_attribute);
4420
David Blaikie4e4d0842012-03-11 07:00:24 +00004421 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004422
4423 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004424 Tracker.consumeClose();
4425 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004426
David Blaikie4e4d0842012-03-11 07:00:24 +00004427 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004428 // FIXME: Accept these components in any order, and produce fixits to
4429 // correct the order if the user gets it wrong. Ideally we should deal
4430 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004431
4432 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004433 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4434 if (!DS.getSourceRange().getEnd().isInvalid()) {
4435 EndLoc = DS.getSourceRange().getEnd();
4436 ConstQualifierLoc = DS.getConstSpecLoc();
4437 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4438 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004439
4440 // Parse ref-qualifier[opt].
4441 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004442 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004443 diag::warn_cxx98_compat_ref_qualifier :
4444 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004445
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004446 RefQualifierIsLValueRef = Tok.is(tok::amp);
4447 RefQualifierLoc = ConsumeToken();
4448 EndLoc = RefQualifierLoc;
4449 }
4450
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004451 // C++11 [expr.prim.general]p3:
4452 // If a declaration declares a member function or member function
4453 // template of a class X, the expression this is a prvalue of type
4454 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
4455 // and the end of the function-definition, member-declarator, or
4456 // declarator.
4457 bool IsCXX11MemberFunction =
4458 getLangOpts().CPlusPlus0x &&
4459 (D.getContext() == Declarator::MemberContext ||
4460 (D.getContext() == Declarator::FileContext &&
4461 D.getCXXScopeSpec().isValid() &&
4462 Actions.CurContext->isRecord()));
4463 Sema::CXXThisScopeRAII ThisScope(Actions,
4464 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4465 DS.getTypeQualifiers(),
4466 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004467
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004468 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004469 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004470 DynamicExceptions,
4471 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004472 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004473 if (ESpecType != EST_None)
4474 EndLoc = ESpecRange.getEnd();
4475
Richard Smith6ee326a2012-04-10 01:32:12 +00004476 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4477 // after the exception-specification.
4478 MaybeParseCXX0XAttributes(FnAttrs);
4479
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004480 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004481 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004482 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004483 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004484 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004485 if (Range.getEnd().isValid())
4486 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004487 }
4488 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004489 }
4490
4491 // Remember that we parsed a function type, and remember the attributes.
4492 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4493 /*isVariadic=*/EllipsisLoc.isValid(),
4494 EllipsisLoc,
4495 ParamInfo.data(), ParamInfo.size(),
4496 DS.getTypeQualifiers(),
4497 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004498 RefQualifierLoc, ConstQualifierLoc,
4499 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004500 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004501 ESpecType, ESpecRange.getBegin(),
4502 DynamicExceptions.data(),
4503 DynamicExceptionRanges.data(),
4504 DynamicExceptions.size(),
4505 NoexceptExpr.isUsable() ?
4506 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004507 Tracker.getOpenLocation(),
4508 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004509 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004510 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004511
4512 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004513}
4514
4515/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4516/// identifier list form for a K&R-style function: void foo(a,b,c)
4517///
4518/// Note that identifier-lists are only allowed for normal declarators, not for
4519/// abstract-declarators.
4520bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004521 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004522 && Tok.is(tok::identifier)
4523 && !TryAltiVecVectorToken()
4524 // K&R identifier lists can't have typedefs as identifiers, per C99
4525 // 6.7.5.3p11.
4526 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4527 // Identifier lists follow a really simple grammar: the identifiers can
4528 // be followed *only* by a ", identifier" or ")". However, K&R
4529 // identifier lists are really rare in the brave new modern world, and
4530 // it is very common for someone to typo a type in a non-K&R style
4531 // list. If we are presented with something like: "void foo(intptr x,
4532 // float y)", we don't want to start parsing the function declarator as
4533 // though it is a K&R style declarator just because intptr is an
4534 // invalid type.
4535 //
4536 // To handle this, we check to see if the token after the first
4537 // identifier is a "," or ")". Only then do we parse it as an
4538 // identifier list.
4539 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4540}
4541
4542/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4543/// we found a K&R-style identifier list instead of a typed parameter list.
4544///
4545/// After returning, ParamInfo will hold the parsed parameters.
4546///
4547/// identifier-list: [C99 6.7.5]
4548/// identifier
4549/// identifier-list ',' identifier
4550///
4551void Parser::ParseFunctionDeclaratorIdentifierList(
4552 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004553 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004554 // If there was no identifier specified for the declarator, either we are in
4555 // an abstract-declarator, or we are in a parameter declarator which was found
4556 // to be abstract. In abstract-declarators, identifier lists are not valid:
4557 // diagnose this.
4558 if (!D.getIdentifier())
4559 Diag(Tok, diag::ext_ident_list_in_param);
4560
4561 // Maintain an efficient lookup of params we have seen so far.
4562 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4563
4564 while (1) {
4565 // If this isn't an identifier, report the error and skip until ')'.
4566 if (Tok.isNot(tok::identifier)) {
4567 Diag(Tok, diag::err_expected_ident);
4568 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4569 // Forget we parsed anything.
4570 ParamInfo.clear();
4571 return;
4572 }
4573
4574 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4575
4576 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4577 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4578 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4579
4580 // Verify that the argument identifier has not already been mentioned.
4581 if (!ParamsSoFar.insert(ParmII)) {
4582 Diag(Tok, diag::err_param_redefinition) << ParmII;
4583 } else {
4584 // Remember this identifier in ParamInfo.
4585 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4586 Tok.getLocation(),
4587 0));
4588 }
4589
4590 // Eat the identifier.
4591 ConsumeToken();
4592
4593 // The list continues if we see a comma.
4594 if (Tok.isNot(tok::comma))
4595 break;
4596 ConsumeToken();
4597 }
4598}
4599
4600/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4601/// after the opening parenthesis. This function will not parse a K&R-style
4602/// identifier list.
4603///
Richard Smith6ce48a72012-04-11 04:01:28 +00004604/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4605/// caller parsed those arguments immediately after the open paren - they should
4606/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004607///
4608/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4609/// be the location of the ellipsis, if any was parsed.
4610///
Reid Spencer5f016e22007-07-11 17:01:13 +00004611/// parameter-type-list: [C99 6.7.5]
4612/// parameter-list
4613/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004614/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004615///
4616/// parameter-list: [C99 6.7.5]
4617/// parameter-declaration
4618/// parameter-list ',' parameter-declaration
4619///
4620/// parameter-declaration: [C99 6.7.5]
4621/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004622/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004623/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004624/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004625/// declaration-specifiers abstract-declarator[opt]
4626/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004627/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004628/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004629/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004630///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004631void Parser::ParseParameterDeclarationClause(
4632 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004633 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004634 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004635 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004636
Chris Lattnerf97409f2008-04-06 06:57:35 +00004637 while (1) {
4638 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004639 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4640 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004641 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004642 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004643 }
Mike Stump1eb44332009-09-09 15:08:12 +00004644
Chris Lattnerf97409f2008-04-06 06:57:35 +00004645 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004646 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004647 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004648
Richard Smith6ce48a72012-04-11 04:01:28 +00004649 // Parse any C++11 attributes.
4650 MaybeParseCXX0XAttributes(DS.getAttributes());
4651
John McCall7f040a92010-12-24 02:08:15 +00004652 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004653 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004654 ParseMicrosoftAttributes(DS.getAttributes());
4655
4656 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004657
4658 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004659 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004660 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004661 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4662 // too much hassle.
4663 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004664
Chris Lattnere64c5492009-02-27 18:38:20 +00004665 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004666
Chris Lattnerf97409f2008-04-06 06:57:35 +00004667 // Parse the declarator. This is "PrototypeContext", because we must
4668 // accept either 'declarator' or 'abstract-declarator' here.
4669 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4670 ParseDeclarator(ParmDecl);
4671
4672 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004673 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004674
Chris Lattnerf97409f2008-04-06 06:57:35 +00004675 // Remember this parsed parameter in ParamInfo.
4676 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004677
Douglas Gregor72b505b2008-12-16 21:30:33 +00004678 // DefArgToks is used when the parsing of default arguments needs
4679 // to be delayed.
4680 CachedTokens *DefArgToks = 0;
4681
Chris Lattnerf97409f2008-04-06 06:57:35 +00004682 // If no parameter was specified, verify that *something* was specified,
4683 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004684 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4685 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004686 // Completely missing, emit error.
4687 Diag(DSStart, diag::err_missing_param);
4688 } else {
4689 // Otherwise, we have something. Add it and let semantic analysis try
4690 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004691
Chris Lattnerf97409f2008-04-06 06:57:35 +00004692 // Inform the actions module about the parameter declarator, so it gets
4693 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004694 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004695
4696 // Parse the default argument, if any. We parse the default
4697 // arguments in all dialects; the semantic analysis in
4698 // ActOnParamDefaultArgument will reject the default argument in
4699 // C.
4700 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004701 SourceLocation EqualLoc = Tok.getLocation();
4702
Chris Lattner04421082008-04-08 04:40:51 +00004703 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004704 if (D.getContext() == Declarator::MemberContext) {
4705 // If we're inside a class definition, cache the tokens
4706 // corresponding to the default argument. We'll actually parse
4707 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00004708 // FIXME: Can we use a smart pointer for Toks?
4709 DefArgToks = new CachedTokens;
4710
Mike Stump1eb44332009-09-09 15:08:12 +00004711 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004712 /*StopAtSemi=*/true,
4713 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004714 delete DefArgToks;
4715 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004716 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004717 } else {
4718 // Mark the end of the default argument so that we know when to
4719 // stop when we parse it later on.
4720 Token DefArgEnd;
4721 DefArgEnd.startToken();
4722 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4723 DefArgEnd.setLocation(Tok.getLocation());
4724 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004725 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004726 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004727 }
Chris Lattner04421082008-04-08 04:40:51 +00004728 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004729 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004730 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004731
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004732 // The argument isn't actually potentially evaluated unless it is
4733 // used.
4734 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004735 Sema::PotentiallyEvaluatedIfUsed,
4736 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004737
Sebastian Redl84407ba2012-03-14 15:54:00 +00004738 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004739 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4740 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004741 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004742 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004743 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004744 if (DefArgResult.isInvalid()) {
4745 Actions.ActOnParamDefaultArgumentError(Param);
4746 SkipUntil(tok::comma, tok::r_paren, true, true);
4747 } else {
4748 // Inform the actions module about the default argument
4749 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004750 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004751 }
Chris Lattner04421082008-04-08 04:40:51 +00004752 }
4753 }
Mike Stump1eb44332009-09-09 15:08:12 +00004754
4755 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4756 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004757 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004758 }
4759
4760 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004761 if (Tok.isNot(tok::comma)) {
4762 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004763 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4764
David Blaikie4e4d0842012-03-11 07:00:24 +00004765 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004766 // We have ellipsis without a preceding ',', which is ill-formed
4767 // in C. Complain and provide the fix.
4768 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004769 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004770 }
4771 }
4772
4773 break;
4774 }
Mike Stump1eb44332009-09-09 15:08:12 +00004775
Chris Lattnerf97409f2008-04-06 06:57:35 +00004776 // Consume the comma.
4777 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004778 }
Mike Stump1eb44332009-09-09 15:08:12 +00004779
Chris Lattner66d28652008-04-06 06:34:08 +00004780}
Chris Lattneref4715c2008-04-06 05:45:57 +00004781
Reid Spencer5f016e22007-07-11 17:01:13 +00004782/// [C90] direct-declarator '[' constant-expression[opt] ']'
4783/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4784/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4785/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4786/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004787/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4788/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004789void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004790 if (CheckProhibitedCXX11Attribute())
4791 return;
4792
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004793 BalancedDelimiterTracker T(*this, tok::l_square);
4794 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004795
Chris Lattner378c7e42008-12-18 07:27:21 +00004796 // C array syntax has many features, but by-far the most common is [] and [4].
4797 // This code does a fast path to handle some of the most obvious cases.
4798 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004799 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004800 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004801 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004802
Chris Lattner378c7e42008-12-18 07:27:21 +00004803 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004804 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004805 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004806 T.getOpenLocation(),
4807 T.getCloseLocation()),
4808 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004809 return;
4810 } else if (Tok.getKind() == tok::numeric_constant &&
4811 GetLookAheadToken(1).is(tok::r_square)) {
4812 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004813 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004814 ConsumeToken();
4815
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004816 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004817 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004818 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004819
Chris Lattner378c7e42008-12-18 07:27:21 +00004820 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004821 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004822 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004823 T.getOpenLocation(),
4824 T.getCloseLocation()),
4825 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004826 return;
4827 }
Mike Stump1eb44332009-09-09 15:08:12 +00004828
Reid Spencer5f016e22007-07-11 17:01:13 +00004829 // If valid, this location is the position where we read the 'static' keyword.
4830 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004831 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004832 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004833
Reid Spencer5f016e22007-07-11 17:01:13 +00004834 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004835 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004836 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004837 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004838
Reid Spencer5f016e22007-07-11 17:01:13 +00004839 // If we haven't already read 'static', check to see if there is one after the
4840 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004841 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004842 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004843
Reid Spencer5f016e22007-07-11 17:01:13 +00004844 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4845 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004846 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004847
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004848 // Handle the case where we have '[*]' as the array size. However, a leading
4849 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4850 // the the token after the star is a ']'. Since stars in arrays are
4851 // infrequent, use of lookahead is not costly here.
4852 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004853 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004854
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004855 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004856 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004857 StaticLoc = SourceLocation(); // Drop the static.
4858 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004859 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004860 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004861 // Note, in C89, this production uses the constant-expr production instead
4862 // of assignment-expr. The only difference is that assignment-expr allows
4863 // things like '=' and '*='. Sema rejects these in C89 mode because they
4864 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004865
Douglas Gregore0762c92009-06-19 23:52:42 +00004866 // Parse the constant-expression or assignment-expression now (depending
4867 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004868 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004869 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004870 } else {
4871 EnterExpressionEvaluationContext Unevaluated(Actions,
4872 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004873 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004874 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004875 }
Mike Stump1eb44332009-09-09 15:08:12 +00004876
Reid Spencer5f016e22007-07-11 17:01:13 +00004877 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004878 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004879 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004880 // If the expression was invalid, skip it.
4881 SkipUntil(tok::r_square);
4882 return;
4883 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004884
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004885 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004886
John McCall0b7e6782011-03-24 11:26:52 +00004887 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004888 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004889
Chris Lattner378c7e42008-12-18 07:27:21 +00004890 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004891 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004892 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004893 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004894 T.getOpenLocation(),
4895 T.getCloseLocation()),
4896 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004897}
4898
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004899/// [GNU] typeof-specifier:
4900/// typeof ( expressions )
4901/// typeof ( type-name )
4902/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004903///
4904void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004905 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004906 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004907 SourceLocation StartLoc = ConsumeToken();
4908
John McCallcfb708c2010-01-13 20:03:27 +00004909 const bool hasParens = Tok.is(tok::l_paren);
4910
Eli Friedman71b8fb52012-01-21 01:01:51 +00004911 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4912
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004913 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004914 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004915 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004916 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4917 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004918 if (hasParens)
4919 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004920
4921 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004922 // FIXME: Not accurate, the range gets one token more than it should.
4923 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004924 else
4925 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004926
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004927 if (isCastExpr) {
4928 if (!CastTy) {
4929 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004930 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004931 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004932
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004933 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004934 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004935 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4936 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004937 DiagID, CastTy))
4938 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004939 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004940 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004941
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004942 // If we get here, the operand to the typeof was an expresion.
4943 if (Operand.isInvalid()) {
4944 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004945 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004946 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004947
Eli Friedman71b8fb52012-01-21 01:01:51 +00004948 // We might need to transform the operand if it is potentially evaluated.
4949 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4950 if (Operand.isInvalid()) {
4951 DS.SetTypeSpecError();
4952 return;
4953 }
4954
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004955 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004956 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004957 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4958 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004959 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004960 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004961}
Chris Lattner1b492422010-02-28 18:33:55 +00004962
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004963/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004964/// _Atomic ( type-name )
4965///
4966void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4967 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4968
4969 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004970 BalancedDelimiterTracker T(*this, tok::l_paren);
4971 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004972 SkipUntil(tok::r_paren);
4973 return;
4974 }
4975
4976 TypeResult Result = ParseTypeName();
4977 if (Result.isInvalid()) {
4978 SkipUntil(tok::r_paren);
4979 return;
4980 }
4981
4982 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004983 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004984
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004985 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004986 return;
4987
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004988 DS.setTypeofParensRange(T.getRange());
4989 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004990
4991 const char *PrevSpec = 0;
4992 unsigned DiagID;
4993 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4994 DiagID, Result.release()))
4995 Diag(StartLoc, DiagID) << PrevSpec;
4996}
4997
Chris Lattner1b492422010-02-28 18:33:55 +00004998
4999/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
5000/// from TryAltiVecVectorToken.
5001bool Parser::TryAltiVecVectorTokenOutOfLine() {
5002 Token Next = NextToken();
5003 switch (Next.getKind()) {
5004 default: return false;
5005 case tok::kw_short:
5006 case tok::kw_long:
5007 case tok::kw_signed:
5008 case tok::kw_unsigned:
5009 case tok::kw_void:
5010 case tok::kw_char:
5011 case tok::kw_int:
5012 case tok::kw_float:
5013 case tok::kw_double:
5014 case tok::kw_bool:
5015 case tok::kw___pixel:
5016 Tok.setKind(tok::kw___vector);
5017 return true;
5018 case tok::identifier:
5019 if (Next.getIdentifierInfo() == Ident_pixel) {
5020 Tok.setKind(tok::kw___vector);
5021 return true;
5022 }
5023 return false;
5024 }
5025}
5026
5027bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5028 const char *&PrevSpec, unsigned &DiagID,
5029 bool &isInvalid) {
5030 if (Tok.getIdentifierInfo() == Ident_vector) {
5031 Token Next = NextToken();
5032 switch (Next.getKind()) {
5033 case tok::kw_short:
5034 case tok::kw_long:
5035 case tok::kw_signed:
5036 case tok::kw_unsigned:
5037 case tok::kw_void:
5038 case tok::kw_char:
5039 case tok::kw_int:
5040 case tok::kw_float:
5041 case tok::kw_double:
5042 case tok::kw_bool:
5043 case tok::kw___pixel:
5044 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5045 return true;
5046 case tok::identifier:
5047 if (Next.getIdentifierInfo() == Ident_pixel) {
5048 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5049 return true;
5050 }
5051 break;
5052 default:
5053 break;
5054 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005055 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005056 DS.isTypeAltiVecVector()) {
5057 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5058 return true;
5059 }
5060 return false;
5061}