blob: 554a60e4254a21df3a3d666338c2f6d8d490bb82 [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);
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +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());
1984 Attrs.addNew(PP.getIdentifierInfo("aligned"), KWLoc, 0, KWLoc,
Sean Hunt93f95f22012-06-18 16:13:52 +00001985 0, T.getOpenLocation(), ArgExprs.take(), 1,
Jakob Stoklund Olesen35329362012-06-19 21:48:43 +00001986 AttributeList::AS_CXX11);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00001987}
1988
Reid Spencer5f016e22007-07-11 17:01:13 +00001989/// ParseDeclarationSpecifiers
1990/// declaration-specifiers: [C99 6.7]
1991/// storage-class-specifier declaration-specifiers[opt]
1992/// type-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001993/// [C99] function-specifier declaration-specifiers[opt]
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00001994/// [C11] alignment-specifier declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001995/// [GNU] attributes declaration-specifiers[opt]
Douglas Gregor8d267c52011-09-09 02:06:17 +00001996/// [Clang] '__module_private__' declaration-specifiers[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00001997///
1998/// storage-class-specifier: [C99 6.7.1]
1999/// 'typedef'
2000/// 'extern'
2001/// 'static'
2002/// 'auto'
2003/// 'register'
Sebastian Redl669d5d72008-11-14 23:42:31 +00002004/// [C++] 'mutable'
Reid Spencer5f016e22007-07-11 17:01:13 +00002005/// [GNU] '__thread'
Reid Spencer5f016e22007-07-11 17:01:13 +00002006/// function-specifier: [C99 6.7.4]
2007/// [C99] 'inline'
Douglas Gregorb48fe382008-10-31 09:07:45 +00002008/// [C++] 'virtual'
2009/// [C++] 'explicit'
Peter Collingbournef315fa82011-02-14 01:42:53 +00002010/// [OpenCL] '__kernel'
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002011/// 'friend': [C++ dcl.friend]
Sebastian Redl2ac67232009-11-05 15:47:02 +00002012/// 'constexpr': [C++0x dcl.constexpr]
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002013
Reid Spencer5f016e22007-07-11 17:01:13 +00002014///
Douglas Gregorc4b4e7b2008-12-24 02:52:09 +00002015void Parser::ParseDeclarationSpecifiers(DeclSpec &DS,
Douglas Gregor4d9a16f2009-05-12 23:25:50 +00002016 const ParsedTemplateInfo &TemplateInfo,
John McCall67d1a672009-08-06 02:15:43 +00002017 AccessSpecifier AS,
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002018 DeclSpecContext DSContext,
2019 LateParsedAttrList *LateAttrs) {
Douglas Gregor312eadb2011-04-24 05:37:28 +00002020 if (DS.getSourceRange().isInvalid()) {
2021 DS.SetRangeStart(Tok.getLocation());
2022 DS.SetRangeEnd(Tok.getLocation());
2023 }
2024
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002025 bool EnteringContext = (DSContext == DSC_class || DSContext == DSC_top_level);
Reid Spencer5f016e22007-07-11 17:01:13 +00002026 while (1) {
John McCallfec54012009-08-03 20:12:06 +00002027 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00002028 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00002029 unsigned DiagID = 0;
2030
Reid Spencer5f016e22007-07-11 17:01:13 +00002031 SourceLocation Loc = Tok.getLocation();
Douglas Gregor12e083c2008-11-07 15:42:26 +00002032
Reid Spencer5f016e22007-07-11 17:01:13 +00002033 switch (Tok.getKind()) {
Mike Stump1eb44332009-09-09 15:08:12 +00002034 default:
Chris Lattnerbce61352008-07-26 00:20:22 +00002035 DoneWithDeclSpec:
Peter Collingbournef1907682011-09-29 18:03:57 +00002036 // [C++0x] decl-specifier-seq: decl-specifier attribute-specifier-seq[opt]
2037 MaybeParseCXX0XAttributes(DS.getAttributes());
2038
Reid Spencer5f016e22007-07-11 17:01:13 +00002039 // If this is not a declaration specifier token, we're done reading decl
2040 // specifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00002041 DS.Finish(Diags, PP);
Reid Spencer5f016e22007-07-11 17:01:13 +00002042 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002043
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002044 case tok::code_completion: {
John McCallf312b1e2010-08-26 23:41:50 +00002045 Sema::ParserCompletionContext CCC = Sema::PCC_Namespace;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002046 if (DS.hasTypeSpecifier()) {
2047 bool AllowNonIdentifiers
2048 = (getCurScope()->getFlags() & (Scope::ControlScope |
2049 Scope::BlockScope |
2050 Scope::TemplateParamScope |
2051 Scope::FunctionPrototypeScope |
2052 Scope::AtCatchScope)) == 0;
2053 bool AllowNestedNameSpecifiers
2054 = DSContext == DSC_top_level ||
2055 (DSContext == DSC_class && DS.isFriendSpecified());
2056
Douglas Gregorc7b6d882010-09-16 15:14:18 +00002057 Actions.CodeCompleteDeclSpec(getCurScope(), DS,
2058 AllowNonIdentifiers,
2059 AllowNestedNameSpecifiers);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002060 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002061 }
2062
Douglas Gregor68e3c2e2011-02-15 20:33:25 +00002063 if (getCurScope()->getFnParent() || getCurScope()->getBlockParent())
2064 CCC = Sema::PCC_LocalDeclarationSpecifiers;
2065 else if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate)
John McCallf312b1e2010-08-26 23:41:50 +00002066 CCC = DSContext == DSC_class? Sema::PCC_MemberTemplate
2067 : Sema::PCC_Template;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002068 else if (DSContext == DSC_class)
John McCallf312b1e2010-08-26 23:41:50 +00002069 CCC = Sema::PCC_Class;
Argyrios Kyrtzidis849639d2012-02-07 16:50:53 +00002070 else if (CurParsedObjCImpl)
John McCallf312b1e2010-08-26 23:41:50 +00002071 CCC = Sema::PCC_ObjCImplementation;
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002072
2073 Actions.CodeCompleteOrdinaryName(getCurScope(), CCC);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002074 return cutOffParsing();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00002075 }
2076
Chris Lattner5e02c472009-01-05 00:07:25 +00002077 case tok::coloncolon: // ::foo::bar
John McCall9ba61662010-02-26 08:45:28 +00002078 // C++ scope specifier. Annotate and loop, or bail out on error.
2079 if (TryAnnotateCXXScopeToken(true)) {
2080 if (!DS.hasTypeSpecifier())
2081 DS.SetTypeSpecError();
2082 goto DoneWithDeclSpec;
2083 }
John McCall2e0a7152010-03-01 18:20:46 +00002084 if (Tok.is(tok::coloncolon)) // ::new or ::delete
2085 goto DoneWithDeclSpec;
John McCall9ba61662010-02-26 08:45:28 +00002086 continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002087
2088 case tok::annot_cxxscope: {
Richard Smithf63eee72012-05-09 18:56:43 +00002089 if (DS.hasTypeSpecifier() || DS.isTypeAltiVecVector())
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002090 goto DoneWithDeclSpec;
2091
John McCallaa87d332009-12-12 11:40:51 +00002092 CXXScopeSpec SS;
Douglas Gregorc34348a2011-02-24 17:54:50 +00002093 Actions.RestoreNestedNameSpecifierAnnotation(Tok.getAnnotationValue(),
2094 Tok.getAnnotationRange(),
2095 SS);
John McCallaa87d332009-12-12 11:40:51 +00002096
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002097 // We are looking for a qualified typename.
Douglas Gregor9135c722009-03-25 15:40:00 +00002098 Token Next = NextToken();
Mike Stump1eb44332009-09-09 15:08:12 +00002099 if (Next.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002100 static_cast<TemplateIdAnnotation *>(Next.getAnnotationValue())
Douglas Gregorc45c2322009-03-31 00:43:58 +00002101 ->Kind == TNK_Type_template) {
Douglas Gregor9135c722009-03-25 15:40:00 +00002102 // We have a qualified template-id, e.g., N::A<int>
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002103
2104 // C++ [class.qual]p2:
2105 // In a lookup in which the constructor is an acceptable lookup
2106 // result and the nested-name-specifier nominates a class C:
2107 //
2108 // - if the name specified after the
2109 // nested-name-specifier, when looked up in C, is the
2110 // injected-class-name of C (Clause 9), or
2111 //
2112 // - if the name specified after the nested-name-specifier
2113 // is the same as the identifier or the
2114 // simple-template-id's template-name in the last
2115 // component of the nested-name-specifier,
2116 //
2117 // the name is instead considered to name the constructor of
2118 // class C.
2119 //
2120 // Thus, if the template-name is actually the constructor
2121 // name, then the code is ill-formed; this interpretation is
2122 // reinforced by the NAD status of core issue 635.
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002123 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Next);
John McCallba9d8532010-04-13 06:39:49 +00002124 if ((DSContext == DSC_top_level ||
2125 (DSContext == DSC_class && DS.isFriendSpecified())) &&
2126 TemplateId->Name &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002127 Actions.isCurrentClassName(*TemplateId->Name, getCurScope(), &SS)) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002128 if (isConstructorDeclarator()) {
2129 // The user meant this to be an out-of-line constructor
2130 // definition, but template arguments are not allowed
2131 // there. Just allow this as a constructor; we'll
2132 // complain about it later.
2133 goto DoneWithDeclSpec;
2134 }
2135
2136 // The user meant this to name a type, but it actually names
2137 // a constructor with some extraneous template
2138 // arguments. Complain, then parse it as a type as the user
2139 // intended.
2140 Diag(TemplateId->TemplateNameLoc,
2141 diag::err_out_of_line_template_id_names_constructor)
2142 << TemplateId->Name;
2143 }
2144
John McCallaa87d332009-12-12 11:40:51 +00002145 DS.getTypeSpecScope() = SS;
2146 ConsumeToken(); // The C++ scope.
Mike Stump1eb44332009-09-09 15:08:12 +00002147 assert(Tok.is(tok::annot_template_id) &&
Douglas Gregor9135c722009-03-25 15:40:00 +00002148 "ParseOptionalCXXScopeSpecifier not working");
Douglas Gregor059101f2011-03-02 00:47:37 +00002149 AnnotateTemplateIdTokenAsType();
Douglas Gregor9135c722009-03-25 15:40:00 +00002150 continue;
2151 }
2152
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002153 if (Next.is(tok::annot_typename)) {
John McCallaa87d332009-12-12 11:40:51 +00002154 DS.getTypeSpecScope() = SS;
2155 ConsumeToken(); // The C++ scope.
John McCallb3d87482010-08-24 05:47:05 +00002156 if (Tok.getAnnotationValue()) {
2157 ParsedType T = getTypeAnnotation(Tok);
Nico Weber253e80b2010-11-22 10:30:56 +00002158 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename,
2159 Tok.getAnnotationEndLoc(),
John McCallb3d87482010-08-24 05:47:05 +00002160 PrevSpec, DiagID, T);
2161 }
Douglas Gregor9d7b3532009-09-28 07:26:33 +00002162 else
2163 DS.SetTypeSpecError();
2164 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2165 ConsumeToken(); // The typename
2166 }
2167
Douglas Gregor9135c722009-03-25 15:40:00 +00002168 if (Next.isNot(tok::identifier))
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002169 goto DoneWithDeclSpec;
2170
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002171 // If we're in a context where the identifier could be a class name,
2172 // check whether this is a constructor declaration.
John McCallba9d8532010-04-13 06:39:49 +00002173 if ((DSContext == DSC_top_level ||
2174 (DSContext == DSC_class && DS.isFriendSpecified())) &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002175 Actions.isCurrentClassName(*Next.getIdentifierInfo(), getCurScope(),
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002176 &SS)) {
2177 if (isConstructorDeclarator())
2178 goto DoneWithDeclSpec;
2179
2180 // As noted in C++ [class.qual]p2 (cited above), when the name
2181 // of the class is qualified in a context where it could name
2182 // a constructor, its a constructor name. However, we've
2183 // looked at the declarator, and the user probably meant this
2184 // to be a type. Complain that it isn't supposed to be treated
2185 // as a type, then proceed to parse it as a type.
2186 Diag(Next.getLocation(), diag::err_out_of_line_type_names_constructor)
2187 << Next.getIdentifierInfo();
2188 }
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002189
John McCallb3d87482010-08-24 05:47:05 +00002190 ParsedType TypeRep = Actions.getTypeName(*Next.getIdentifierInfo(),
2191 Next.getLocation(),
Douglas Gregor9e876872011-03-01 18:12:44 +00002192 getCurScope(), &SS,
2193 false, false, ParsedType(),
Abramo Bagnarafad03b72012-01-27 08:46:19 +00002194 /*IsCtorOrDtorName=*/false,
Douglas Gregor9e876872011-03-01 18:12:44 +00002195 /*NonTrivialSourceInfo=*/true);
Douglas Gregor55f6b142009-02-09 18:46:07 +00002196
Chris Lattnerf4382f52009-04-14 22:17:06 +00002197 // If the referenced identifier is not a type, then this declspec is
2198 // erroneous: We already checked about that it has no type specifier, and
2199 // C++ doesn't have implicit int. Diagnose it as a typo w.r.t. to the
Mike Stump1eb44332009-09-09 15:08:12 +00002200 // typename.
Chris Lattnerf4382f52009-04-14 22:17:06 +00002201 if (TypeRep == 0) {
2202 ConsumeToken(); // Eat the scope spec so the identifier is current.
Richard Smith69730c12012-03-12 07:56:15 +00002203 if (ParseImplicitInt(DS, &SS, TemplateInfo, AS, DSContext)) continue;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002204 goto DoneWithDeclSpec;
Chris Lattnerf4382f52009-04-14 22:17:06 +00002205 }
Mike Stump1eb44332009-09-09 15:08:12 +00002206
John McCallaa87d332009-12-12 11:40:51 +00002207 DS.getTypeSpecScope() = SS;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002208 ConsumeToken(); // The C++ scope.
2209
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002210 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002211 DiagID, TypeRep);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002212 if (isInvalid)
2213 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002214
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002215 DS.SetRangeEnd(Tok.getLocation());
2216 ConsumeToken(); // The typename.
2217
2218 continue;
2219 }
Mike Stump1eb44332009-09-09 15:08:12 +00002220
Chris Lattner80d0c892009-01-21 19:48:37 +00002221 case tok::annot_typename: {
John McCallb3d87482010-08-24 05:47:05 +00002222 if (Tok.getAnnotationValue()) {
2223 ParsedType T = getTypeAnnotation(Tok);
Nico Weberc43271e2010-11-22 12:50:03 +00002224 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00002225 DiagID, T);
2226 } else
Douglas Gregor31a19b62009-04-01 21:51:26 +00002227 DS.SetTypeSpecError();
Chris Lattner5c5db552010-04-05 18:18:31 +00002228
2229 if (isInvalid)
2230 break;
2231
Chris Lattner80d0c892009-01-21 19:48:37 +00002232 DS.SetRangeEnd(Tok.getAnnotationEndLoc());
2233 ConsumeToken(); // The typename
Mike Stump1eb44332009-09-09 15:08:12 +00002234
Chris Lattner80d0c892009-01-21 19:48:37 +00002235 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2236 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002237 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002238 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002239 ParseObjCProtocolQualifiers(DS);
2240
Chris Lattner80d0c892009-01-21 19:48:37 +00002241 continue;
2242 }
Mike Stump1eb44332009-09-09 15:08:12 +00002243
Douglas Gregorbfad9152011-04-28 15:48:45 +00002244 case tok::kw___is_signed:
2245 // GNU libstdc++ 4.4 uses __is_signed as an identifier, but Clang
2246 // typically treats it as a trait. If we see __is_signed as it appears
2247 // in libstdc++, e.g.,
2248 //
2249 // static const bool __is_signed;
2250 //
2251 // then treat __is_signed as an identifier rather than as a keyword.
2252 if (DS.getTypeSpecType() == TST_bool &&
2253 DS.getTypeQualifiers() == DeclSpec::TQ_const &&
2254 DS.getStorageClassSpec() == DeclSpec::SCS_static) {
2255 Tok.getIdentifierInfo()->RevertTokenIDToIdentifier();
2256 Tok.setKind(tok::identifier);
2257 }
2258
2259 // We're done with the declaration-specifiers.
2260 goto DoneWithDeclSpec;
2261
Chris Lattner3bd934a2008-07-26 01:18:38 +00002262 // typedef-name
David Blaikie42d6d0c2011-12-04 05:04:18 +00002263 case tok::kw_decltype:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002264 case tok::identifier: {
Chris Lattner5e02c472009-01-05 00:07:25 +00002265 // In C++, check to see if this is a scope specifier like foo::bar::, if
2266 // so handle it as such. This is important for ctor parsing.
David Blaikie4e4d0842012-03-11 07:00:24 +00002267 if (getLangOpts().CPlusPlus) {
John McCall9ba61662010-02-26 08:45:28 +00002268 if (TryAnnotateCXXScopeToken(true)) {
2269 if (!DS.hasTypeSpecifier())
2270 DS.SetTypeSpecError();
2271 goto DoneWithDeclSpec;
2272 }
2273 if (!Tok.is(tok::identifier))
2274 continue;
2275 }
Mike Stump1eb44332009-09-09 15:08:12 +00002276
Chris Lattner3bd934a2008-07-26 01:18:38 +00002277 // This identifier can only be a typedef name if we haven't already seen
2278 // a type-specifier. Without this check we misparse:
2279 // typedef int X; struct Y { short X; }; as 'short int'.
2280 if (DS.hasTypeSpecifier())
2281 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002282
John Thompson82287d12010-02-05 00:12:22 +00002283 // Check for need to substitute AltiVec keyword tokens.
2284 if (TryAltiVecToken(DS, Loc, PrevSpec, DiagID, isInvalid))
2285 break;
2286
Richard Smithf63eee72012-05-09 18:56:43 +00002287 // [AltiVec] 2.2: [If the 'vector' specifier is used] The syntax does not
2288 // allow the use of a typedef name as a type specifier.
2289 if (DS.isTypeAltiVecVector())
2290 goto DoneWithDeclSpec;
2291
John McCallb3d87482010-08-24 05:47:05 +00002292 ParsedType TypeRep =
2293 Actions.getTypeName(*Tok.getIdentifierInfo(),
2294 Tok.getLocation(), getCurScope());
Douglas Gregor55f6b142009-02-09 18:46:07 +00002295
Chris Lattnerc199ab32009-04-12 20:42:31 +00002296 // If this is not a typedef name, don't parse it as part of the declspec,
2297 // it must be an implicit int or an error.
John McCallb3d87482010-08-24 05:47:05 +00002298 if (!TypeRep) {
Richard Smith69730c12012-03-12 07:56:15 +00002299 if (ParseImplicitInt(DS, 0, TemplateInfo, AS, DSContext)) continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002300 goto DoneWithDeclSpec;
Chris Lattnerc199ab32009-04-12 20:42:31 +00002301 }
Douglas Gregor55f6b142009-02-09 18:46:07 +00002302
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002303 // If we're in a context where the identifier could be a class name,
2304 // check whether this is a constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002305 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002306 Actions.isCurrentClassName(*Tok.getIdentifierInfo(), getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002307 isConstructorDeclarator())
Douglas Gregorb48fe382008-10-31 09:07:45 +00002308 goto DoneWithDeclSpec;
2309
Douglas Gregor1a51b4a2009-02-09 15:09:02 +00002310 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_typename, Loc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00002311 DiagID, TypeRep);
Chris Lattner3bd934a2008-07-26 01:18:38 +00002312 if (isInvalid)
2313 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002314
Chris Lattner3bd934a2008-07-26 01:18:38 +00002315 DS.SetRangeEnd(Tok.getLocation());
2316 ConsumeToken(); // The identifier
2317
2318 // Objective-C supports syntax of the form 'id<proto1,proto2>' where 'id'
2319 // is a specific typedef and 'itf<proto1,proto2>' where 'itf' is an
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002320 // Objective-C interface.
David Blaikie4e4d0842012-03-11 07:00:24 +00002321 if (Tok.is(tok::less) && getLangOpts().ObjC1)
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002322 ParseObjCProtocolQualifiers(DS);
2323
Steve Naroff4f9b9f12008-09-22 10:28:57 +00002324 // Need to support trailing type qualifiers (e.g. "id<p> const").
2325 // If a type specifier follows, it will be diagnosed elsewhere.
2326 continue;
Chris Lattner3bd934a2008-07-26 01:18:38 +00002327 }
Douglas Gregor39a8de12009-02-25 19:37:18 +00002328
2329 // type-name
2330 case tok::annot_template_id: {
Argyrios Kyrtzidis25a76762011-06-22 06:09:49 +00002331 TemplateIdAnnotation *TemplateId = takeTemplateIdAnnotation(Tok);
Douglas Gregorc45c2322009-03-31 00:43:58 +00002332 if (TemplateId->Kind != TNK_Type_template) {
Douglas Gregor39a8de12009-02-25 19:37:18 +00002333 // This template-id does not refer to a type name, so we're
2334 // done with the type-specifiers.
2335 goto DoneWithDeclSpec;
2336 }
2337
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002338 // If we're in a context where the template-id could be a
2339 // constructor name or specialization, check whether this is a
2340 // constructor declaration.
David Blaikie4e4d0842012-03-11 07:00:24 +00002341 if (getLangOpts().CPlusPlus && DSContext == DSC_class &&
Douglas Gregor23c94db2010-07-02 17:43:08 +00002342 Actions.isCurrentClassName(*TemplateId->Name, getCurScope()) &&
Douglas Gregor0efc2c12010-01-13 17:31:36 +00002343 isConstructorDeclarator())
2344 goto DoneWithDeclSpec;
2345
Douglas Gregor39a8de12009-02-25 19:37:18 +00002346 // Turn the template-id annotation token into a type annotation
2347 // token, then try again to parse it as a type-specifier.
Douglas Gregor31a19b62009-04-01 21:51:26 +00002348 AnnotateTemplateIdTokenAsType();
Douglas Gregor39a8de12009-02-25 19:37:18 +00002349 continue;
2350 }
2351
Reid Spencer5f016e22007-07-11 17:01:13 +00002352 // GNU attributes support.
2353 case tok::kw___attribute:
DeLesley Hutchins2287c5e2012-03-02 22:12:59 +00002354 ParseGNUAttributes(DS.getAttributes(), 0, LateAttrs);
Reid Spencer5f016e22007-07-11 17:01:13 +00002355 continue;
Steve Narofff59e17e2008-12-24 20:59:21 +00002356
2357 // Microsoft declspec support.
2358 case tok::kw___declspec:
John McCall7f040a92010-12-24 02:08:15 +00002359 ParseMicrosoftDeclSpec(DS.getAttributes());
Steve Narofff59e17e2008-12-24 20:59:21 +00002360 continue;
Mike Stump1eb44332009-09-09 15:08:12 +00002361
Steve Naroff239f0732008-12-25 14:16:32 +00002362 // Microsoft single token adornments.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002363 case tok::kw___forceinline: {
2364 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
2365 IdentifierInfo *AttrName = Tok.getIdentifierInfo();
2366 SourceLocation AttrNameLoc = ConsumeToken();
Sean Hunt93f95f22012-06-18 16:13:52 +00002367 // FIXME: This does not work correctly if it is set to be a declspec
2368 // attribute, and a GNU attribute is simply incorrect.
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002369 DS.getAttributes().addNew(AttrName, AttrNameLoc, 0, AttrNameLoc, 0,
Sean Hunt93f95f22012-06-18 16:13:52 +00002370 SourceLocation(), 0, 0, AttributeList::AS_GNU);
Michael J. Spenceradc6cbf2012-06-18 07:00:48 +00002371 continue;
2372 }
Eli Friedman290eeb02009-06-08 23:27:34 +00002373
2374 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00002375 case tok::kw___ptr32:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00002376 case tok::kw___w64:
Steve Naroff239f0732008-12-25 14:16:32 +00002377 case tok::kw___cdecl:
2378 case tok::kw___stdcall:
2379 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00002380 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00002381 case tok::kw___unaligned:
John McCall7f040a92010-12-24 02:08:15 +00002382 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00002383 continue;
2384
Dawn Perchik52fc3142010-09-03 01:29:35 +00002385 // Borland single token adornments.
2386 case tok::kw___pascal:
John McCall7f040a92010-12-24 02:08:15 +00002387 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00002388 continue;
2389
Peter Collingbournef315fa82011-02-14 01:42:53 +00002390 // OpenCL single token adornments.
2391 case tok::kw___kernel:
2392 ParseOpenCLAttributes(DS.getAttributes());
2393 continue;
2394
Reid Spencer5f016e22007-07-11 17:01:13 +00002395 // storage-class-specifier
2396 case tok::kw_typedef:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002397 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_typedef, Loc,
2398 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002399 break;
2400 case tok::kw_extern:
2401 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002402 Diag(Tok, diag::ext_thread_before) << "extern";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002403 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_extern, Loc,
2404 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002405 break;
Steve Naroff8d54bf22007-12-18 00:16:02 +00002406 case tok::kw___private_extern__:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002407 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_private_extern,
2408 Loc, PrevSpec, DiagID);
Steve Naroff8d54bf22007-12-18 00:16:02 +00002409 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002410 case tok::kw_static:
2411 if (DS.isThreadSpecified())
Chris Lattner1ab3b962008-11-18 07:48:38 +00002412 Diag(Tok, diag::ext_thread_before) << "static";
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002413 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_static, Loc,
2414 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002415 break;
2416 case tok::kw_auto:
David Blaikie4e4d0842012-03-11 07:00:24 +00002417 if (getLangOpts().CPlusPlus0x) {
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002418 if (isKnownToBeTypeSpecifier(GetLookAheadToken(1))) {
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002419 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2420 PrevSpec, DiagID);
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002421 if (!isInvalid)
Richard Smith8f4fb192011-09-04 19:54:14 +00002422 Diag(Tok, diag::ext_auto_storage_class)
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002423 << FixItHint::CreateRemoval(DS.getStorageClassSpecLoc());
Richard Smith8f4fb192011-09-04 19:54:14 +00002424 } else
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002425 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_auto, Loc, PrevSpec,
2426 DiagID);
Richard Smith8f4fb192011-09-04 19:54:14 +00002427 } else
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002428 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_auto, Loc,
2429 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002430 break;
2431 case tok::kw_register:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002432 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_register, Loc,
2433 PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002434 break;
Sebastian Redl669d5d72008-11-14 23:42:31 +00002435 case tok::kw_mutable:
Peter Collingbourneb8b0e752011-10-06 03:01:00 +00002436 isInvalid = DS.SetStorageClassSpec(Actions, DeclSpec::SCS_mutable, Loc,
2437 PrevSpec, DiagID);
Sebastian Redl669d5d72008-11-14 23:42:31 +00002438 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00002439 case tok::kw___thread:
John McCallfec54012009-08-03 20:12:06 +00002440 isInvalid = DS.SetStorageClassSpecThread(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002441 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002442
Reid Spencer5f016e22007-07-11 17:01:13 +00002443 // function-specifier
2444 case tok::kw_inline:
John McCallfec54012009-08-03 20:12:06 +00002445 isInvalid = DS.SetFunctionSpecInline(Loc, PrevSpec, DiagID);
Reid Spencer5f016e22007-07-11 17:01:13 +00002446 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002447 case tok::kw_virtual:
John McCallfec54012009-08-03 20:12:06 +00002448 isInvalid = DS.SetFunctionSpecVirtual(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002449 break;
Douglas Gregorb48fe382008-10-31 09:07:45 +00002450 case tok::kw_explicit:
John McCallfec54012009-08-03 20:12:06 +00002451 isInvalid = DS.SetFunctionSpecExplicit(Loc, PrevSpec, DiagID);
Douglas Gregorb48fe382008-10-31 09:07:45 +00002452 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002453
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002454 // alignment-specifier
2455 case tok::kw__Alignas:
David Blaikie4e4d0842012-03-11 07:00:24 +00002456 if (!getLangOpts().C11)
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00002457 Diag(Tok, diag::ext_c11_alignas);
Peter Collingbourne82d0b0a2011-09-29 18:04:28 +00002458 ParseAlignmentSpecifier(DS.getAttributes());
2459 continue;
2460
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002461 // friend
2462 case tok::kw_friend:
John McCall67d1a672009-08-06 02:15:43 +00002463 if (DSContext == DSC_class)
2464 isInvalid = DS.SetFriendSpec(Loc, PrevSpec, DiagID);
2465 else {
2466 PrevSpec = ""; // not actually used by the diagnostic
2467 DiagID = diag::err_friend_invalid_in_context;
2468 isInvalid = true;
2469 }
Anders Carlssonf47f7a12009-05-06 04:46:28 +00002470 break;
Mike Stump1eb44332009-09-09 15:08:12 +00002471
Douglas Gregor8d267c52011-09-09 02:06:17 +00002472 // Modules
2473 case tok::kw___module_private__:
2474 isInvalid = DS.setModulePrivateSpec(Loc, PrevSpec, DiagID);
2475 break;
2476
Sebastian Redl2ac67232009-11-05 15:47:02 +00002477 // constexpr
2478 case tok::kw_constexpr:
2479 isInvalid = DS.SetConstexprSpec(Loc, PrevSpec, DiagID);
2480 break;
2481
Chris Lattner80d0c892009-01-21 19:48:37 +00002482 // type-specifier
2483 case tok::kw_short:
John McCallfec54012009-08-03 20:12:06 +00002484 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_short, Loc, PrevSpec,
2485 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002486 break;
2487 case tok::kw_long:
2488 if (DS.getTypeSpecWidth() != DeclSpec::TSW_long)
John McCallfec54012009-08-03 20:12:06 +00002489 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_long, Loc, PrevSpec,
2490 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002491 else
John McCallfec54012009-08-03 20:12:06 +00002492 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2493 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002494 break;
Francois Pichet338d7f72011-04-28 01:59:37 +00002495 case tok::kw___int64:
2496 isInvalid = DS.SetTypeSpecWidth(DeclSpec::TSW_longlong, Loc, PrevSpec,
2497 DiagID);
2498 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002499 case tok::kw_signed:
John McCallfec54012009-08-03 20:12:06 +00002500 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_signed, Loc, PrevSpec,
2501 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002502 break;
2503 case tok::kw_unsigned:
John McCallfec54012009-08-03 20:12:06 +00002504 isInvalid = DS.SetTypeSpecSign(DeclSpec::TSS_unsigned, Loc, PrevSpec,
2505 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002506 break;
2507 case tok::kw__Complex:
John McCallfec54012009-08-03 20:12:06 +00002508 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_complex, Loc, PrevSpec,
2509 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002510 break;
2511 case tok::kw__Imaginary:
John McCallfec54012009-08-03 20:12:06 +00002512 isInvalid = DS.SetTypeSpecComplex(DeclSpec::TSC_imaginary, Loc, PrevSpec,
2513 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002514 break;
2515 case tok::kw_void:
John McCallfec54012009-08-03 20:12:06 +00002516 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_void, Loc, PrevSpec,
2517 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002518 break;
2519 case tok::kw_char:
John McCallfec54012009-08-03 20:12:06 +00002520 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char, Loc, PrevSpec,
2521 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002522 break;
2523 case tok::kw_int:
John McCallfec54012009-08-03 20:12:06 +00002524 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int, Loc, PrevSpec,
2525 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002526 break;
Richard Smith5a5a9712012-04-04 06:24:32 +00002527 case tok::kw___int128:
2528 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_int128, Loc, PrevSpec,
2529 DiagID);
2530 break;
2531 case tok::kw_half:
2532 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_half, Loc, PrevSpec,
2533 DiagID);
2534 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002535 case tok::kw_float:
John McCallfec54012009-08-03 20:12:06 +00002536 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_float, Loc, PrevSpec,
2537 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002538 break;
2539 case tok::kw_double:
John McCallfec54012009-08-03 20:12:06 +00002540 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_double, Loc, PrevSpec,
2541 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002542 break;
2543 case tok::kw_wchar_t:
John McCallfec54012009-08-03 20:12:06 +00002544 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_wchar, Loc, PrevSpec,
2545 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002546 break;
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002547 case tok::kw_char16_t:
John McCallfec54012009-08-03 20:12:06 +00002548 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char16, Loc, PrevSpec,
2549 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002550 break;
2551 case tok::kw_char32_t:
John McCallfec54012009-08-03 20:12:06 +00002552 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_char32, Loc, PrevSpec,
2553 DiagID);
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00002554 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002555 case tok::kw_bool:
2556 case tok::kw__Bool:
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002557 if (Tok.is(tok::kw_bool) &&
2558 DS.getTypeSpecType() != DeclSpec::TST_unspecified &&
2559 DS.getStorageClassSpec() == DeclSpec::SCS_typedef) {
2560 PrevSpec = ""; // Not used by the diagnostic.
2561 DiagID = diag::err_bool_redeclaration;
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002562 // For better error recovery.
2563 Tok.setKind(tok::identifier);
Argyrios Kyrtzidis4383e182010-11-16 18:18:13 +00002564 isInvalid = true;
2565 } else {
2566 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_bool, Loc, PrevSpec,
2567 DiagID);
2568 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002569 break;
2570 case tok::kw__Decimal32:
John McCallfec54012009-08-03 20:12:06 +00002571 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal32, Loc, PrevSpec,
2572 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002573 break;
2574 case tok::kw__Decimal64:
John McCallfec54012009-08-03 20:12:06 +00002575 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal64, Loc, PrevSpec,
2576 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002577 break;
2578 case tok::kw__Decimal128:
John McCallfec54012009-08-03 20:12:06 +00002579 isInvalid = DS.SetTypeSpecType(DeclSpec::TST_decimal128, Loc, PrevSpec,
2580 DiagID);
Chris Lattner80d0c892009-01-21 19:48:37 +00002581 break;
John Thompson82287d12010-02-05 00:12:22 +00002582 case tok::kw___vector:
2583 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
2584 break;
2585 case tok::kw___pixel:
2586 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
2587 break;
John McCalla5fc4722011-04-09 22:50:59 +00002588 case tok::kw___unknown_anytype:
2589 isInvalid = DS.SetTypeSpecType(TST_unknown_anytype, Loc,
2590 PrevSpec, DiagID);
2591 break;
Chris Lattner80d0c892009-01-21 19:48:37 +00002592
2593 // class-specifier:
2594 case tok::kw_class:
2595 case tok::kw_struct:
Chris Lattner4c97d762009-04-12 21:49:30 +00002596 case tok::kw_union: {
2597 tok::TokenKind Kind = Tok.getKind();
2598 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002599 ParseClassSpecifier(Kind, Loc, DS, TemplateInfo, AS,
2600 EnteringContext, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002601 continue;
Chris Lattner4c97d762009-04-12 21:49:30 +00002602 }
Chris Lattner80d0c892009-01-21 19:48:37 +00002603
2604 // enum-specifier:
2605 case tok::kw_enum:
Chris Lattner4c97d762009-04-12 21:49:30 +00002606 ConsumeToken();
Richard Smith69730c12012-03-12 07:56:15 +00002607 ParseEnumSpecifier(Loc, DS, TemplateInfo, AS, DSContext);
Chris Lattner80d0c892009-01-21 19:48:37 +00002608 continue;
2609
2610 // cv-qualifier:
2611 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00002612 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002613 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002614 break;
2615 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00002616 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002617 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002618 break;
2619 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00002620 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00002621 getLangOpts());
Chris Lattner80d0c892009-01-21 19:48:37 +00002622 break;
2623
Douglas Gregord57959a2009-03-27 23:10:48 +00002624 // C++ typename-specifier:
2625 case tok::kw_typename:
John McCall9ba61662010-02-26 08:45:28 +00002626 if (TryAnnotateTypeOrScopeToken()) {
2627 DS.SetTypeSpecError();
2628 goto DoneWithDeclSpec;
2629 }
2630 if (!Tok.is(tok::kw_typename))
Douglas Gregord57959a2009-03-27 23:10:48 +00002631 continue;
2632 break;
2633
Chris Lattner80d0c892009-01-21 19:48:37 +00002634 // GNU typeof support.
2635 case tok::kw_typeof:
2636 ParseTypeofSpecifier(DS);
2637 continue;
2638
David Blaikie42d6d0c2011-12-04 05:04:18 +00002639 case tok::annot_decltype:
Anders Carlsson6fd634f2009-06-24 17:47:40 +00002640 ParseDecltypeSpecifier(DS);
2641 continue;
2642
Sean Huntdb5d44b2011-05-19 05:37:45 +00002643 case tok::kw___underlying_type:
2644 ParseUnderlyingTypeSpecifier(DS);
Eli Friedmanb001de72011-10-06 23:00:33 +00002645 continue;
2646
2647 case tok::kw__Atomic:
2648 ParseAtomicSpecifier(DS);
2649 continue;
Sean Huntdb5d44b2011-05-19 05:37:45 +00002650
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002651 // OpenCL qualifiers:
2652 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00002653 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00002654 goto DoneWithDeclSpec;
2655 case tok::kw___private:
2656 case tok::kw___global:
2657 case tok::kw___local:
2658 case tok::kw___constant:
2659 case tok::kw___read_only:
2660 case tok::kw___write_only:
2661 case tok::kw___read_write:
2662 ParseOpenCLQualifiers(DS);
2663 break;
2664
Steve Naroffd3ded1f2008-06-05 00:02:44 +00002665 case tok::less:
Chris Lattner3bd934a2008-07-26 01:18:38 +00002666 // GCC ObjC supports types like "<SomeProtocol>" as a synonym for
Chris Lattnerbce61352008-07-26 00:20:22 +00002667 // "id<SomeProtocol>". This is hopelessly old fashioned and dangerous,
2668 // but we support it.
David Blaikie4e4d0842012-03-11 07:00:24 +00002669 if (DS.hasTypeSpecifier() || !getLangOpts().ObjC1)
Chris Lattnerbce61352008-07-26 00:20:22 +00002670 goto DoneWithDeclSpec;
Mike Stump1eb44332009-09-09 15:08:12 +00002671
Douglas Gregor46f936e2010-11-19 17:10:50 +00002672 if (!ParseObjCProtocolQualifiers(DS))
2673 Diag(Loc, diag::warn_objc_protocol_qualifier_missing_id)
2674 << FixItHint::CreateInsertion(Loc, "id")
2675 << SourceRange(Loc, DS.getSourceRange().getEnd());
Douglas Gregor9bd1d8d2010-10-21 23:17:00 +00002676
2677 // Need to support trailing type qualifiers (e.g. "id<p> const").
2678 // If a type specifier follows, it will be diagnosed elsewhere.
2679 continue;
Reid Spencer5f016e22007-07-11 17:01:13 +00002680 }
John McCallfec54012009-08-03 20:12:06 +00002681 // If the specifier wasn't legal, issue a diagnostic.
Reid Spencer5f016e22007-07-11 17:01:13 +00002682 if (isInvalid) {
2683 assert(PrevSpec && "Method did not return previous specifier!");
John McCallfec54012009-08-03 20:12:06 +00002684 assert(DiagID);
Douglas Gregorae2fb142010-08-23 14:34:43 +00002685
2686 if (DiagID == diag::ext_duplicate_declspec)
2687 Diag(Tok, DiagID)
2688 << PrevSpec << FixItHint::CreateRemoval(Tok.getLocation());
2689 else
2690 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00002691 }
Fariborz Jahanian12e3ece2011-02-22 23:17:49 +00002692
Chris Lattner81c018d2008-03-13 06:29:04 +00002693 DS.SetRangeEnd(Tok.getLocation());
Fariborz Jahaniane106a0b2011-04-19 21:42:37 +00002694 if (DiagID != diag::err_bool_redeclaration)
2695 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002696 }
2697}
Douglas Gregoradcac882008-12-01 23:54:00 +00002698
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002699/// ParseStructDeclaration - Parse a struct declaration without the terminating
2700/// semicolon.
2701///
Reid Spencer5f016e22007-07-11 17:01:13 +00002702/// struct-declaration:
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002703/// specifier-qualifier-list struct-declarator-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002704/// [GNU] __extension__ struct-declaration
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002705/// [GNU] specifier-qualifier-list
Reid Spencer5f016e22007-07-11 17:01:13 +00002706/// struct-declarator-list:
2707/// struct-declarator
2708/// struct-declarator-list ',' struct-declarator
2709/// [GNU] struct-declarator-list ',' attributes[opt] struct-declarator
2710/// struct-declarator:
2711/// declarator
2712/// [GNU] declarator attributes[opt]
2713/// declarator[opt] ':' constant-expression
2714/// [GNU] declarator[opt] ':' constant-expression attributes[opt]
2715///
Chris Lattnere1359422008-04-10 06:46:29 +00002716void Parser::
John McCallbdd563e2009-11-03 02:38:08 +00002717ParseStructDeclaration(DeclSpec &DS, FieldCallback &Fields) {
Fariborz Jahaniana28948f2011-08-22 15:54:49 +00002718
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002719 if (Tok.is(tok::kw___extension__)) {
2720 // __extension__ silences extension warnings in the subexpression.
2721 ExtensionRAIIObject O(Diags); // Use RAII to do this.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002722 ConsumeToken();
Chris Lattnerc46d1a12008-10-20 06:45:43 +00002723 return ParseStructDeclaration(DS, Fields);
2724 }
Mike Stump1eb44332009-09-09 15:08:12 +00002725
Steve Naroff28a7ca82007-08-20 22:28:22 +00002726 // Parse the common specifier-qualifiers-list piece.
Steve Naroff28a7ca82007-08-20 22:28:22 +00002727 ParseSpecifierQualifierList(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00002728
Douglas Gregor4920f1f2009-01-12 22:49:06 +00002729 // If there are no declarators, this is a free-standing declaration
2730 // specifier. Let the actions module cope with it.
Chris Lattner04d66662007-10-09 17:33:22 +00002731 if (Tok.is(tok::semi)) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00002732 Actions.ParsedFreeStandingDeclSpec(getCurScope(), AS_none, DS);
Steve Naroff28a7ca82007-08-20 22:28:22 +00002733 return;
2734 }
2735
2736 // Read struct-declarators until we find the semicolon.
John McCallbdd563e2009-11-03 02:38:08 +00002737 bool FirstDeclarator = true;
Richard Smith7984de32012-01-12 23:53:29 +00002738 SourceLocation CommaLoc;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002739 while (1) {
John McCall92576642012-05-07 06:16:41 +00002740 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
John McCallbdd563e2009-11-03 02:38:08 +00002741 FieldDeclarator DeclaratorInfo(DS);
Richard Smith7984de32012-01-12 23:53:29 +00002742 DeclaratorInfo.D.setCommaLoc(CommaLoc);
John McCallbdd563e2009-11-03 02:38:08 +00002743
2744 // Attributes are only allowed here on successive declarators.
John McCall7f040a92010-12-24 02:08:15 +00002745 if (!FirstDeclarator)
2746 MaybeParseGNUAttributes(DeclaratorInfo.D);
Mike Stump1eb44332009-09-09 15:08:12 +00002747
Steve Naroff28a7ca82007-08-20 22:28:22 +00002748 /// struct-declarator: declarator
2749 /// struct-declarator: declarator[opt] ':' constant-expression
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002750 if (Tok.isNot(tok::colon)) {
2751 // Don't parse FOO:BAR as if it were a typo for FOO::BAR.
2752 ColonProtectionRAIIObject X(*this);
Chris Lattnere1359422008-04-10 06:46:29 +00002753 ParseDeclarator(DeclaratorInfo.D);
Chris Lattnera1efc8c2009-12-10 01:59:24 +00002754 }
Mike Stump1eb44332009-09-09 15:08:12 +00002755
Chris Lattner04d66662007-10-09 17:33:22 +00002756 if (Tok.is(tok::colon)) {
Steve Naroff28a7ca82007-08-20 22:28:22 +00002757 ConsumeToken();
John McCall60d7b3a2010-08-24 06:29:42 +00002758 ExprResult Res(ParseConstantExpression());
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00002759 if (Res.isInvalid())
Steve Naroff28a7ca82007-08-20 22:28:22 +00002760 SkipUntil(tok::semi, true, true);
Chris Lattner60b1e3e2008-04-10 06:15:14 +00002761 else
Sebastian Redleffa8d12008-12-10 00:02:53 +00002762 DeclaratorInfo.BitfieldSize = Res.release();
Steve Naroff28a7ca82007-08-20 22:28:22 +00002763 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00002764
Steve Naroff28a7ca82007-08-20 22:28:22 +00002765 // If attributes exist after the declarator, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002766 MaybeParseGNUAttributes(DeclaratorInfo.D);
Sebastian Redlab197ba2009-02-09 18:23:29 +00002767
John McCallbdd563e2009-11-03 02:38:08 +00002768 // We're done with this declarator; invoke the callback.
John McCalld226f652010-08-21 09:40:31 +00002769 Decl *D = Fields.invoke(DeclaratorInfo);
John McCall54abf7d2009-11-04 02:18:39 +00002770 PD.complete(D);
John McCallbdd563e2009-11-03 02:38:08 +00002771
Steve Naroff28a7ca82007-08-20 22:28:22 +00002772 // If we don't have a comma, it is either the end of the list (a ';')
2773 // or an error, bail out.
Chris Lattner04d66662007-10-09 17:33:22 +00002774 if (Tok.isNot(tok::comma))
Chris Lattnercd4b83c2007-10-29 04:42:53 +00002775 return;
Sebastian Redlab197ba2009-02-09 18:23:29 +00002776
Steve Naroff28a7ca82007-08-20 22:28:22 +00002777 // Consume the comma.
Richard Smith7984de32012-01-12 23:53:29 +00002778 CommaLoc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00002779
John McCallbdd563e2009-11-03 02:38:08 +00002780 FirstDeclarator = false;
Steve Naroff28a7ca82007-08-20 22:28:22 +00002781 }
Steve Naroff28a7ca82007-08-20 22:28:22 +00002782}
2783
2784/// ParseStructUnionBody
2785/// struct-contents:
2786/// struct-declaration-list
2787/// [EXT] empty
2788/// [GNU] "struct-declaration-list" without terminatoring ';'
2789/// struct-declaration-list:
2790/// struct-declaration
2791/// struct-declaration-list struct-declaration
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002792/// [OBC] '@' 'defs' '(' class-name ')'
Steve Naroff28a7ca82007-08-20 22:28:22 +00002793///
Reid Spencer5f016e22007-07-11 17:01:13 +00002794void Parser::ParseStructUnionBody(SourceLocation RecordLoc,
John McCalld226f652010-08-21 09:40:31 +00002795 unsigned TagType, Decl *TagDecl) {
John McCallf312b1e2010-08-26 23:41:50 +00002796 PrettyDeclStackTraceEntry CrashInfo(Actions, TagDecl, RecordLoc,
2797 "parsing struct/union body");
Mike Stump1eb44332009-09-09 15:08:12 +00002798
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002799 BalancedDelimiterTracker T(*this, tok::l_brace);
2800 if (T.consumeOpen())
2801 return;
Mike Stump1eb44332009-09-09 15:08:12 +00002802
Douglas Gregor3218c4b2009-01-09 22:42:13 +00002803 ParseScope StructScope(this, Scope::ClassScope|Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00002804 Actions.ActOnTagStartDefinition(getCurScope(), TagDecl);
Douglas Gregor72de6672009-01-08 20:45:30 +00002805
Reid Spencer5f016e22007-07-11 17:01:13 +00002806 // Empty structs are an extension in C (C99 6.7.2.1p7), but are allowed in
2807 // C++.
David Blaikie4e4d0842012-03-11 07:00:24 +00002808 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus) {
Richard Smithd7c56e12011-12-29 21:57:33 +00002809 Diag(Tok, diag::ext_empty_struct_union) << (TagType == TST_union);
2810 Diag(Tok, diag::warn_empty_struct_union_compat) << (TagType == TST_union);
2811 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002812
Chris Lattner5f9e2722011-07-23 10:55:15 +00002813 SmallVector<Decl *, 32> FieldDecls;
Chris Lattnere1359422008-04-10 06:46:29 +00002814
Reid Spencer5f016e22007-07-11 17:01:13 +00002815 // While we still have something to read, read the declarations in the struct.
Chris Lattner04d66662007-10-09 17:33:22 +00002816 while (Tok.isNot(tok::r_brace) && Tok.isNot(tok::eof)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002817 // Each iteration of this loop reads one struct-declaration.
Mike Stump1eb44332009-09-09 15:08:12 +00002818
Reid Spencer5f016e22007-07-11 17:01:13 +00002819 // Check for extraneous top-level semicolon.
Chris Lattner04d66662007-10-09 17:33:22 +00002820 if (Tok.is(tok::semi)) {
Richard Trieu4b0e6f12012-05-16 19:04:59 +00002821 ConsumeExtraSemi(InsideStruct,
2822 DeclSpec::getSpecifierName((DeclSpec::TST)TagType));
Reid Spencer5f016e22007-07-11 17:01:13 +00002823 continue;
2824 }
Chris Lattnere1359422008-04-10 06:46:29 +00002825
2826 // Parse all the comma separated declarators.
John McCall0b7e6782011-03-24 11:26:52 +00002827 DeclSpec DS(AttrFactory);
Mike Stump1eb44332009-09-09 15:08:12 +00002828
John McCallbdd563e2009-11-03 02:38:08 +00002829 if (!Tok.is(tok::at)) {
2830 struct CFieldCallback : FieldCallback {
2831 Parser &P;
John McCalld226f652010-08-21 09:40:31 +00002832 Decl *TagDecl;
Chris Lattner5f9e2722011-07-23 10:55:15 +00002833 SmallVectorImpl<Decl *> &FieldDecls;
John McCallbdd563e2009-11-03 02:38:08 +00002834
John McCalld226f652010-08-21 09:40:31 +00002835 CFieldCallback(Parser &P, Decl *TagDecl,
Chris Lattner5f9e2722011-07-23 10:55:15 +00002836 SmallVectorImpl<Decl *> &FieldDecls) :
John McCallbdd563e2009-11-03 02:38:08 +00002837 P(P), TagDecl(TagDecl), FieldDecls(FieldDecls) {}
2838
John McCalld226f652010-08-21 09:40:31 +00002839 virtual Decl *invoke(FieldDeclarator &FD) {
John McCallbdd563e2009-11-03 02:38:08 +00002840 // Install the declarator into the current TagDecl.
John McCalld226f652010-08-21 09:40:31 +00002841 Decl *Field = P.Actions.ActOnField(P.getCurScope(), TagDecl,
John McCall4ba39712009-11-03 21:13:47 +00002842 FD.D.getDeclSpec().getSourceRange().getBegin(),
2843 FD.D, FD.BitfieldSize);
John McCallbdd563e2009-11-03 02:38:08 +00002844 FieldDecls.push_back(Field);
2845 return Field;
Douglas Gregor91a28862009-08-26 14:27:30 +00002846 }
John McCallbdd563e2009-11-03 02:38:08 +00002847 } Callback(*this, TagDecl, FieldDecls);
2848
2849 ParseStructDeclaration(DS, Callback);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002850 } else { // Handle @defs
2851 ConsumeToken();
2852 if (!Tok.isObjCAtKeyword(tok::objc_defs)) {
2853 Diag(Tok, diag::err_unexpected_at);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002854 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002855 continue;
2856 }
2857 ConsumeToken();
2858 ExpectAndConsume(tok::l_paren, diag::err_expected_lparen);
2859 if (!Tok.is(tok::identifier)) {
2860 Diag(Tok, diag::err_expected_ident);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002861 SkipUntil(tok::semi, true);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002862 continue;
2863 }
Chris Lattner5f9e2722011-07-23 10:55:15 +00002864 SmallVector<Decl *, 16> Fields;
Douglas Gregor23c94db2010-07-02 17:43:08 +00002865 Actions.ActOnDefs(getCurScope(), TagDecl, Tok.getLocation(),
Douglas Gregor44b43212008-12-11 16:49:14 +00002866 Tok.getIdentifierInfo(), Fields);
Chris Lattner5a6ddbf2008-06-21 19:39:06 +00002867 FieldDecls.insert(FieldDecls.end(), Fields.begin(), Fields.end());
2868 ConsumeToken();
2869 ExpectAndConsume(tok::r_paren, diag::err_expected_rparen);
Mike Stump1eb44332009-09-09 15:08:12 +00002870 }
Reid Spencer5f016e22007-07-11 17:01:13 +00002871
Chris Lattner04d66662007-10-09 17:33:22 +00002872 if (Tok.is(tok::semi)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002873 ConsumeToken();
Chris Lattner04d66662007-10-09 17:33:22 +00002874 } else if (Tok.is(tok::r_brace)) {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002875 ExpectAndConsume(tok::semi, diag::ext_expected_semi_decl_list);
Reid Spencer5f016e22007-07-11 17:01:13 +00002876 break;
2877 } else {
Chris Lattner3e156ad2010-02-02 00:37:27 +00002878 ExpectAndConsume(tok::semi, diag::err_expected_semi_decl_list);
2879 // Skip to end of block or statement to avoid ext-warning on extra ';'.
Reid Spencer5f016e22007-07-11 17:01:13 +00002880 SkipUntil(tok::r_brace, true, true);
Chris Lattner3e156ad2010-02-02 00:37:27 +00002881 // If we stopped at a ';', eat it.
2882 if (Tok.is(tok::semi)) ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00002883 }
2884 }
Mike Stump1eb44332009-09-09 15:08:12 +00002885
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002886 T.consumeClose();
Mike Stump1eb44332009-09-09 15:08:12 +00002887
John McCall0b7e6782011-03-24 11:26:52 +00002888 ParsedAttributes attrs(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00002889 // If attributes exist after struct contents, parse them.
John McCall7f040a92010-12-24 02:08:15 +00002890 MaybeParseGNUAttributes(attrs);
Daniel Dunbar1bfe1c22008-10-03 02:03:53 +00002891
Douglas Gregor23c94db2010-07-02 17:43:08 +00002892 Actions.ActOnFields(getCurScope(),
David Blaikie77b6de02011-09-22 02:58:26 +00002893 RecordLoc, TagDecl, FieldDecls,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002894 T.getOpenLocation(), T.getCloseLocation(),
John McCall7f040a92010-12-24 02:08:15 +00002895 attrs.getList());
Douglas Gregor72de6672009-01-08 20:45:30 +00002896 StructScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00002897 Actions.ActOnTagFinishDefinition(getCurScope(), TagDecl,
2898 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00002899}
2900
Reid Spencer5f016e22007-07-11 17:01:13 +00002901/// ParseEnumSpecifier
2902/// enum-specifier: [C99 6.7.2.2]
2903/// 'enum' identifier[opt] '{' enumerator-list '}'
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002904///[C99/C++]'enum' identifier[opt] '{' enumerator-list ',' '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002905/// [GNU] 'enum' attributes[opt] identifier[opt] '{' enumerator-list ',' [opt]
2906/// '}' attributes[opt]
Aaron Ballman6454a022012-03-01 04:09:28 +00002907/// [MS] 'enum' __declspec[opt] identifier[opt] '{' enumerator-list ',' [opt]
2908/// '}'
Reid Spencer5f016e22007-07-11 17:01:13 +00002909/// 'enum' identifier
2910/// [GNU] 'enum' attributes[opt] identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002911///
Richard Smith1af83c42012-03-23 03:33:32 +00002912/// [C++11] enum-head '{' enumerator-list[opt] '}'
2913/// [C++11] enum-head '{' enumerator-list ',' '}'
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002914///
Richard Smith1af83c42012-03-23 03:33:32 +00002915/// enum-head: [C++11]
2916/// enum-key attribute-specifier-seq[opt] identifier[opt] enum-base[opt]
2917/// enum-key attribute-specifier-seq[opt] nested-name-specifier
2918/// identifier enum-base[opt]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002919///
Richard Smith1af83c42012-03-23 03:33:32 +00002920/// enum-key: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002921/// 'enum'
2922/// 'enum' 'class'
2923/// 'enum' 'struct'
2924///
Richard Smith1af83c42012-03-23 03:33:32 +00002925/// enum-base: [C++11]
Douglas Gregor1274ccd2010-10-08 23:50:27 +00002926/// ':' type-specifier-seq
2927///
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002928/// [C++] elaborated-type-specifier:
2929/// [C++] 'enum' '::'[opt] nested-name-specifier[opt] identifier
2930///
Chris Lattner4c97d762009-04-12 21:49:30 +00002931void Parser::ParseEnumSpecifier(SourceLocation StartLoc, DeclSpec &DS,
Douglas Gregor9b9edd62010-03-02 17:53:14 +00002932 const ParsedTemplateInfo &TemplateInfo,
Richard Smith69730c12012-03-12 07:56:15 +00002933 AccessSpecifier AS, DeclSpecContext DSC) {
Reid Spencer5f016e22007-07-11 17:01:13 +00002934 // Parse the tag portion of this.
Douglas Gregor374929f2009-09-18 15:37:17 +00002935 if (Tok.is(tok::code_completion)) {
2936 // Code completion for an enum name.
Douglas Gregor23c94db2010-07-02 17:43:08 +00002937 Actions.CodeCompleteTag(getCurScope(), DeclSpec::TST_enum);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00002938 return cutOffParsing();
Douglas Gregor374929f2009-09-18 15:37:17 +00002939 }
John McCall57c13002011-07-06 05:58:41 +00002940
Richard Smithbdad7a22012-01-10 01:33:14 +00002941 SourceLocation ScopedEnumKWLoc;
John McCall57c13002011-07-06 05:58:41 +00002942 bool IsScopedUsingClassTag = false;
2943
David Blaikie4e4d0842012-03-11 07:00:24 +00002944 if (getLangOpts().CPlusPlus0x &&
John McCall57c13002011-07-06 05:58:41 +00002945 (Tok.is(tok::kw_class) || Tok.is(tok::kw_struct))) {
Richard Smith7fe62082011-10-15 05:09:34 +00002946 Diag(Tok, diag::warn_cxx98_compat_scoped_enum);
John McCall57c13002011-07-06 05:58:41 +00002947 IsScopedUsingClassTag = Tok.is(tok::kw_class);
Richard Smithbdad7a22012-01-10 01:33:14 +00002948 ScopedEnumKWLoc = ConsumeToken();
John McCall57c13002011-07-06 05:58:41 +00002949 }
Richard Smith1af83c42012-03-23 03:33:32 +00002950
John McCall13489672012-05-07 06:16:58 +00002951 // C++11 [temp.explicit]p12:
2952 // The usual access controls do not apply to names used to specify
2953 // explicit instantiations.
2954 // We extend this to also cover explicit specializations. Note that
2955 // we don't suppress if this turns out to be an elaborated type
2956 // specifier.
2957 bool shouldDelayDiagsInTag =
2958 (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation ||
2959 TemplateInfo.Kind == ParsedTemplateInfo::ExplicitSpecialization);
2960 SuppressAccessChecks diagsFromTag(*this, shouldDelayDiagsInTag);
Richard Smith1af83c42012-03-23 03:33:32 +00002961
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002962 // If attributes exist after tag, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00002963 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00002964 MaybeParseGNUAttributes(attrs);
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002965
Aaron Ballman6454a022012-03-01 04:09:28 +00002966 // If declspecs exist after tag, parse them.
2967 while (Tok.is(tok::kw___declspec))
2968 ParseMicrosoftDeclSpec(attrs);
2969
Richard Smith7796eb52012-03-12 08:56:40 +00002970 // Enum definitions should not be parsed in a trailing-return-type.
2971 bool AllowDeclaration = DSC != DSC_trailing;
2972
2973 bool AllowFixedUnderlyingType = AllowDeclaration &&
2974 (getLangOpts().CPlusPlus0x || getLangOpts().MicrosoftExt ||
2975 getLangOpts().ObjC2);
John McCall57c13002011-07-06 05:58:41 +00002976
Abramo Bagnarae4da7a02010-05-19 21:37:53 +00002977 CXXScopeSpec &SS = DS.getTypeSpecScope();
David Blaikie4e4d0842012-03-11 07:00:24 +00002978 if (getLangOpts().CPlusPlus) {
John McCall57c13002011-07-06 05:58:41 +00002979 // "enum foo : bar;" is not a potential typo for "enum foo::bar;"
2980 // if a fixed underlying type is allowed.
2981 ColonProtectionRAIIObject X(*this, AllowFixedUnderlyingType);
2982
Douglas Gregorefaa93a2011-11-07 17:33:42 +00002983 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
2984 /*EnteringContext=*/false))
John McCall9ba61662010-02-26 08:45:28 +00002985 return;
2986
2987 if (SS.isSet() && Tok.isNot(tok::identifier)) {
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00002988 Diag(Tok, diag::err_expected_ident);
2989 if (Tok.isNot(tok::l_brace)) {
2990 // Has no name and is not a definition.
2991 // Skip the rest of this declarator, up until the comma or semicolon.
2992 SkipUntil(tok::comma, true);
2993 return;
2994 }
2995 }
2996 }
Mike Stump1eb44332009-09-09 15:08:12 +00002997
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00002998 // Must have either 'enum name' or 'enum {...}'.
Douglas Gregorb9075602011-02-22 02:55:24 +00002999 if (Tok.isNot(tok::identifier) && Tok.isNot(tok::l_brace) &&
Richard Smith7796eb52012-03-12 08:56:40 +00003000 !(AllowFixedUnderlyingType && Tok.is(tok::colon))) {
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003001 Diag(Tok, diag::err_expected_ident_lbrace);
Mike Stump1eb44332009-09-09 15:08:12 +00003002
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003003 // Skip the rest of this declarator, up until the comma or semicolon.
3004 SkipUntil(tok::comma, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003005 return;
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003006 }
Mike Stump1eb44332009-09-09 15:08:12 +00003007
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003008 // If an identifier is present, consume and remember it.
3009 IdentifierInfo *Name = 0;
3010 SourceLocation NameLoc;
3011 if (Tok.is(tok::identifier)) {
3012 Name = Tok.getIdentifierInfo();
3013 NameLoc = ConsumeToken();
3014 }
Mike Stump1eb44332009-09-09 15:08:12 +00003015
Richard Smithbdad7a22012-01-10 01:33:14 +00003016 if (!Name && ScopedEnumKWLoc.isValid()) {
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003017 // C++0x 7.2p2: The optional identifier shall not be omitted in the
3018 // declaration of a scoped enumeration.
3019 Diag(Tok, diag::err_scoped_enum_missing_identifier);
Richard Smithbdad7a22012-01-10 01:33:14 +00003020 ScopedEnumKWLoc = SourceLocation();
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003021 IsScopedUsingClassTag = false;
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003022 }
3023
John McCall13489672012-05-07 06:16:58 +00003024 // Okay, end the suppression area. We'll decide whether to emit the
3025 // diagnostics in a second.
3026 if (shouldDelayDiagsInTag)
3027 diagsFromTag.done();
Richard Smith1af83c42012-03-23 03:33:32 +00003028
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003029 TypeResult BaseType;
3030
Douglas Gregora61b3e72010-12-01 17:42:47 +00003031 // Parse the fixed underlying type.
Douglas Gregorb9075602011-02-22 02:55:24 +00003032 if (AllowFixedUnderlyingType && Tok.is(tok::colon)) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003033 bool PossibleBitfield = false;
3034 if (getCurScope()->getFlags() & Scope::ClassScope) {
3035 // If we're in class scope, this can either be an enum declaration with
3036 // an underlying type, or a declaration of a bitfield member. We try to
3037 // use a simple disambiguation scheme first to catch the common cases
3038 // (integer literal, sizeof); if it's still ambiguous, we then consider
3039 // anything that's a simple-type-specifier followed by '(' as an
3040 // expression. This suffices because function types are not valid
3041 // underlying types anyway.
3042 TPResult TPR = isExpressionOrTypeSpecifierSimple(NextToken().getKind());
3043 // If the next token starts an expression, we know we're parsing a
3044 // bit-field. This is the common case.
3045 if (TPR == TPResult::True())
3046 PossibleBitfield = true;
3047 // If the next token starts a type-specifier-seq, it may be either a
3048 // a fixed underlying type or the start of a function-style cast in C++;
3049 // lookahead one more token to see if it's obvious that we have a
3050 // fixed underlying type.
3051 else if (TPR == TPResult::False() &&
3052 GetLookAheadToken(2).getKind() == tok::semi) {
3053 // Consume the ':'.
3054 ConsumeToken();
3055 } else {
3056 // We have the start of a type-specifier-seq, so we have to perform
3057 // tentative parsing to determine whether we have an expression or a
3058 // type.
3059 TentativeParsingAction TPA(*this);
3060
3061 // Consume the ':'.
3062 ConsumeToken();
Richard Smithd81e9612012-02-23 01:36:12 +00003063
3064 // If we see a type specifier followed by an open-brace, we have an
3065 // ambiguity between an underlying type and a C++11 braced
3066 // function-style cast. Resolve this by always treating it as an
3067 // underlying type.
3068 // FIXME: The standard is not entirely clear on how to disambiguate in
3069 // this case.
David Blaikie4e4d0842012-03-11 07:00:24 +00003070 if ((getLangOpts().CPlusPlus &&
Richard Smithd81e9612012-02-23 01:36:12 +00003071 isCXXDeclarationSpecifier(TPResult::True()) != TPResult::True()) ||
David Blaikie4e4d0842012-03-11 07:00:24 +00003072 (!getLangOpts().CPlusPlus && !isDeclarationSpecifier(true))) {
Douglas Gregora61b3e72010-12-01 17:42:47 +00003073 // We'll parse this as a bitfield later.
3074 PossibleBitfield = true;
3075 TPA.Revert();
3076 } else {
3077 // We have a type-specifier-seq.
3078 TPA.Commit();
3079 }
3080 }
3081 } else {
3082 // Consume the ':'.
3083 ConsumeToken();
3084 }
3085
3086 if (!PossibleBitfield) {
3087 SourceRange Range;
3088 BaseType = ParseTypeName(&Range);
Douglas Gregor86f208c2011-02-22 20:32:04 +00003089
David Blaikie4e4d0842012-03-11 07:00:24 +00003090 if (!getLangOpts().CPlusPlus0x && !getLangOpts().ObjC2)
Douglas Gregor86f208c2011-02-22 20:32:04 +00003091 Diag(StartLoc, diag::ext_ms_enum_fixed_underlying_type)
3092 << Range;
David Blaikie4e4d0842012-03-11 07:00:24 +00003093 if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003094 Diag(StartLoc, diag::warn_cxx98_compat_enum_fixed_underlying_type);
Douglas Gregora61b3e72010-12-01 17:42:47 +00003095 }
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003096 }
3097
Richard Smithbdad7a22012-01-10 01:33:14 +00003098 // There are four options here. If we have 'friend enum foo;' then this is a
3099 // friend declaration, and cannot have an accompanying definition. If we have
3100 // 'enum foo;', then this is a forward declaration. If we have
3101 // 'enum foo {...' then this is a definition. Otherwise we have something
3102 // like 'enum foo xyz', a reference.
Argyrios Kyrtzidise281b4c2008-09-11 00:21:41 +00003103 //
3104 // This is needed to handle stuff like this right (C99 6.7.2.3p11):
3105 // enum foo {..}; void bar() { enum foo; } <- new foo in bar.
3106 // enum foo {..}; void bar() { enum foo x; } <- use of old foo.
3107 //
John McCallf312b1e2010-08-26 23:41:50 +00003108 Sema::TagUseKind TUK;
John McCall13489672012-05-07 06:16:58 +00003109 if (!AllowDeclaration) {
Richard Smith7796eb52012-03-12 08:56:40 +00003110 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003111 } else if (Tok.is(tok::l_brace)) {
3112 if (DS.isFriendSpecified()) {
3113 Diag(Tok.getLocation(), diag::err_friend_decl_defines_type)
3114 << SourceRange(DS.getFriendSpecLoc());
3115 ConsumeBrace();
3116 SkipUntil(tok::r_brace);
3117 TUK = Sema::TUK_Friend;
3118 } else {
3119 TUK = Sema::TUK_Definition;
3120 }
3121 } else if (Tok.is(tok::semi) && DSC != DSC_type_specifier) {
3122 TUK = (DS.isFriendSpecified() ? Sema::TUK_Friend : Sema::TUK_Declaration);
3123 } else {
John McCallf312b1e2010-08-26 23:41:50 +00003124 TUK = Sema::TUK_Reference;
John McCall13489672012-05-07 06:16:58 +00003125 }
3126
3127 // If this is an elaborated type specifier, and we delayed
3128 // diagnostics before, just merge them into the current pool.
3129 if (TUK == Sema::TUK_Reference && shouldDelayDiagsInTag) {
3130 diagsFromTag.redelay();
3131 }
Richard Smith1af83c42012-03-23 03:33:32 +00003132
3133 MultiTemplateParamsArg TParams;
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003134 if (TemplateInfo.Kind != ParsedTemplateInfo::NonTemplate &&
John McCallf312b1e2010-08-26 23:41:50 +00003135 TUK != Sema::TUK_Reference) {
Richard Smith1af83c42012-03-23 03:33:32 +00003136 if (!getLangOpts().CPlusPlus0x || !SS.isSet()) {
3137 // Skip the rest of this declarator, up until the comma or semicolon.
3138 Diag(Tok, diag::err_enum_template);
3139 SkipUntil(tok::comma, true);
3140 return;
3141 }
3142
3143 if (TemplateInfo.Kind == ParsedTemplateInfo::ExplicitInstantiation) {
3144 // Enumerations can't be explicitly instantiated.
3145 DS.SetTypeSpecError();
3146 Diag(StartLoc, diag::err_explicit_instantiation_enum);
3147 return;
3148 }
3149
3150 assert(TemplateInfo.TemplateParams && "no template parameters");
3151 TParams = MultiTemplateParamsArg(TemplateInfo.TemplateParams->data(),
3152 TemplateInfo.TemplateParams->size());
Douglas Gregor8fc6d232010-05-03 17:48:54 +00003153 }
Richard Smith1af83c42012-03-23 03:33:32 +00003154
Douglas Gregorb9075602011-02-22 02:55:24 +00003155 if (!Name && TUK != Sema::TUK_Definition) {
3156 Diag(Tok, diag::err_enumerator_unnamed_no_def);
Richard Smith1af83c42012-03-23 03:33:32 +00003157
Douglas Gregorb9075602011-02-22 02:55:24 +00003158 // Skip the rest of this declarator, up until the comma or semicolon.
3159 SkipUntil(tok::comma, true);
3160 return;
3161 }
Richard Smith1af83c42012-03-23 03:33:32 +00003162
Douglas Gregor402abb52009-05-28 23:31:59 +00003163 bool Owned = false;
John McCallc4e70192009-09-11 04:59:25 +00003164 bool IsDependent = false;
Douglas Gregor48c89f42010-04-24 16:38:41 +00003165 const char *PrevSpec = 0;
3166 unsigned DiagID;
John McCalld226f652010-08-21 09:40:31 +00003167 Decl *TagDecl = Actions.ActOnTag(getCurScope(), DeclSpec::TST_enum, TUK,
John McCall7f040a92010-12-24 02:08:15 +00003168 StartLoc, SS, Name, NameLoc, attrs.getList(),
Richard Smith1af83c42012-03-23 03:33:32 +00003169 AS, DS.getModulePrivateSpecLoc(), TParams,
Richard Smithbdad7a22012-01-10 01:33:14 +00003170 Owned, IsDependent, ScopedEnumKWLoc,
Abramo Bagnaraa88cefd2010-12-03 18:54:17 +00003171 IsScopedUsingClassTag, BaseType);
Douglas Gregor1274ccd2010-10-08 23:50:27 +00003172
Douglas Gregor48c89f42010-04-24 16:38:41 +00003173 if (IsDependent) {
3174 // This enum has a dependent nested-name-specifier. Handle it as a
3175 // dependent tag.
3176 if (!Name) {
3177 DS.SetTypeSpecError();
3178 Diag(Tok, diag::err_expected_type_name_after_typename);
3179 return;
3180 }
3181
Douglas Gregor23c94db2010-07-02 17:43:08 +00003182 TypeResult Type = Actions.ActOnDependentTag(getCurScope(), DeclSpec::TST_enum,
Douglas Gregor48c89f42010-04-24 16:38:41 +00003183 TUK, SS, Name, StartLoc,
3184 NameLoc);
3185 if (Type.isInvalid()) {
3186 DS.SetTypeSpecError();
3187 return;
3188 }
3189
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003190 if (DS.SetTypeSpecType(DeclSpec::TST_typename, StartLoc,
3191 NameLoc.isValid() ? NameLoc : StartLoc,
3192 PrevSpec, DiagID, Type.get()))
Douglas Gregor48c89f42010-04-24 16:38:41 +00003193 Diag(StartLoc, DiagID) << PrevSpec;
3194
3195 return;
3196 }
Mike Stump1eb44332009-09-09 15:08:12 +00003197
John McCalld226f652010-08-21 09:40:31 +00003198 if (!TagDecl) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003199 // The action failed to produce an enumeration tag. If this is a
3200 // definition, consume the entire definition.
Richard Smith7796eb52012-03-12 08:56:40 +00003201 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
Douglas Gregor48c89f42010-04-24 16:38:41 +00003202 ConsumeBrace();
3203 SkipUntil(tok::r_brace);
3204 }
3205
3206 DS.SetTypeSpecError();
3207 return;
3208 }
Richard Smithbdad7a22012-01-10 01:33:14 +00003209
Richard Smith7796eb52012-03-12 08:56:40 +00003210 if (Tok.is(tok::l_brace) && TUK != Sema::TUK_Reference) {
John McCall13489672012-05-07 06:16:58 +00003211 ParseEnumBody(StartLoc, TagDecl);
Richard Smithbdad7a22012-01-10 01:33:14 +00003212 }
Mike Stump1eb44332009-09-09 15:08:12 +00003213
Abramo Bagnara0daaf322011-03-16 20:16:18 +00003214 if (DS.SetTypeSpecType(DeclSpec::TST_enum, StartLoc,
3215 NameLoc.isValid() ? NameLoc : StartLoc,
3216 PrevSpec, DiagID, TagDecl, Owned))
John McCallfec54012009-08-03 20:12:06 +00003217 Diag(StartLoc, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003218}
3219
3220/// ParseEnumBody - Parse a {} enclosed enumerator-list.
3221/// enumerator-list:
3222/// enumerator
3223/// enumerator-list ',' enumerator
3224/// enumerator:
3225/// enumeration-constant
3226/// enumeration-constant '=' constant-expression
3227/// enumeration-constant:
3228/// identifier
3229///
John McCalld226f652010-08-21 09:40:31 +00003230void Parser::ParseEnumBody(SourceLocation StartLoc, Decl *EnumDecl) {
Douglas Gregor074149e2009-01-05 19:45:36 +00003231 // Enter the scope of the enum body and start the definition.
3232 ParseScope EnumScope(this, Scope::DeclScope);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003233 Actions.ActOnTagStartDefinition(getCurScope(), EnumDecl);
Douglas Gregor074149e2009-01-05 19:45:36 +00003234
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003235 BalancedDelimiterTracker T(*this, tok::l_brace);
3236 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00003237
Chris Lattner7946dd32007-08-27 17:24:30 +00003238 // C does not allow an empty enumerator-list, C++ does [dcl.enum].
David Blaikie4e4d0842012-03-11 07:00:24 +00003239 if (Tok.is(tok::r_brace) && !getLangOpts().CPlusPlus)
Fariborz Jahanian05115522010-05-28 22:23:22 +00003240 Diag(Tok, diag::error_empty_enum);
Mike Stump1eb44332009-09-09 15:08:12 +00003241
Chris Lattner5f9e2722011-07-23 10:55:15 +00003242 SmallVector<Decl *, 32> EnumConstantDecls;
Reid Spencer5f016e22007-07-11 17:01:13 +00003243
John McCalld226f652010-08-21 09:40:31 +00003244 Decl *LastEnumConstDecl = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00003245
Reid Spencer5f016e22007-07-11 17:01:13 +00003246 // Parse the enumerator-list.
Chris Lattner04d66662007-10-09 17:33:22 +00003247 while (Tok.is(tok::identifier)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003248 IdentifierInfo *Ident = Tok.getIdentifierInfo();
3249 SourceLocation IdentLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003250
John McCall5b629aa2010-10-22 23:36:17 +00003251 // If attributes exist after the enumerator, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003252 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003253 MaybeParseGNUAttributes(attrs);
John McCall5b629aa2010-10-22 23:36:17 +00003254
Reid Spencer5f016e22007-07-11 17:01:13 +00003255 SourceLocation EqualLoc;
John McCall60d7b3a2010-08-24 06:29:42 +00003256 ExprResult AssignedVal;
John McCall92576642012-05-07 06:16:41 +00003257 ParsingDeclRAIIObject PD(*this, ParsingDeclRAIIObject::NoParent);
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003258
Chris Lattner04d66662007-10-09 17:33:22 +00003259 if (Tok.is(tok::equal)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003260 EqualLoc = ConsumeToken();
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00003261 AssignedVal = ParseConstantExpression();
3262 if (AssignedVal.isInvalid())
Reid Spencer5f016e22007-07-11 17:01:13 +00003263 SkipUntil(tok::comma, tok::r_brace, true, true);
Reid Spencer5f016e22007-07-11 17:01:13 +00003264 }
Mike Stump1eb44332009-09-09 15:08:12 +00003265
Reid Spencer5f016e22007-07-11 17:01:13 +00003266 // Install the enumerator constant into EnumDecl.
John McCalld226f652010-08-21 09:40:31 +00003267 Decl *EnumConstDecl = Actions.ActOnEnumConstant(getCurScope(), EnumDecl,
3268 LastEnumConstDecl,
3269 IdentLoc, Ident,
John McCall7f040a92010-12-24 02:08:15 +00003270 attrs.getList(), EqualLoc,
John McCalld226f652010-08-21 09:40:31 +00003271 AssignedVal.release());
Fariborz Jahanian5a477db2011-12-09 01:15:54 +00003272 PD.complete(EnumConstDecl);
3273
Reid Spencer5f016e22007-07-11 17:01:13 +00003274 EnumConstantDecls.push_back(EnumConstDecl);
3275 LastEnumConstDecl = EnumConstDecl;
Mike Stump1eb44332009-09-09 15:08:12 +00003276
Douglas Gregor751f6922010-09-07 14:51:08 +00003277 if (Tok.is(tok::identifier)) {
3278 // We're missing a comma between enumerators.
3279 SourceLocation Loc = PP.getLocForEndOfToken(PrevTokLocation);
3280 Diag(Loc, diag::err_enumerator_list_missing_comma)
3281 << FixItHint::CreateInsertion(Loc, ", ");
3282 continue;
3283 }
3284
Chris Lattner04d66662007-10-09 17:33:22 +00003285 if (Tok.isNot(tok::comma))
Reid Spencer5f016e22007-07-11 17:01:13 +00003286 break;
3287 SourceLocation CommaLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00003288
Richard Smith7fe62082011-10-15 05:09:34 +00003289 if (Tok.isNot(tok::identifier)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00003290 if (!getLangOpts().C99 && !getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003291 Diag(CommaLoc, diag::ext_enumerator_list_comma)
David Blaikie4e4d0842012-03-11 07:00:24 +00003292 << getLangOpts().CPlusPlus
Richard Smith7fe62082011-10-15 05:09:34 +00003293 << FixItHint::CreateRemoval(CommaLoc);
David Blaikie4e4d0842012-03-11 07:00:24 +00003294 else if (getLangOpts().CPlusPlus0x)
Richard Smith7fe62082011-10-15 05:09:34 +00003295 Diag(CommaLoc, diag::warn_cxx98_compat_enumerator_list_comma)
3296 << FixItHint::CreateRemoval(CommaLoc);
3297 }
Reid Spencer5f016e22007-07-11 17:01:13 +00003298 }
Mike Stump1eb44332009-09-09 15:08:12 +00003299
Reid Spencer5f016e22007-07-11 17:01:13 +00003300 // Eat the }.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003301 T.consumeClose();
Reid Spencer5f016e22007-07-11 17:01:13 +00003302
Reid Spencer5f016e22007-07-11 17:01:13 +00003303 // If attributes exist after the identifier list, parse them.
John McCall0b7e6782011-03-24 11:26:52 +00003304 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00003305 MaybeParseGNUAttributes(attrs);
Douglas Gregor72de6672009-01-08 20:45:30 +00003306
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003307 Actions.ActOnEnumBody(StartLoc, T.getOpenLocation(), T.getCloseLocation(),
3308 EnumDecl, EnumConstantDecls.data(),
3309 EnumConstantDecls.size(), getCurScope(),
3310 attrs.getList());
Mike Stump1eb44332009-09-09 15:08:12 +00003311
Douglas Gregor72de6672009-01-08 20:45:30 +00003312 EnumScope.Exit();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00003313 Actions.ActOnTagFinishDefinition(getCurScope(), EnumDecl,
3314 T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003315}
3316
3317/// isTypeSpecifierQualifier - Return true if the current token could be the
Steve Naroff5f8aa692008-02-11 23:15:56 +00003318/// start of a type-qualifier-list.
3319bool Parser::isTypeQualifier() const {
3320 switch (Tok.getKind()) {
3321 default: return false;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003322
3323 // type-qualifier only in OpenCL
3324 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003325 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003326
Steve Naroff5f8aa692008-02-11 23:15:56 +00003327 // type-qualifier
3328 case tok::kw_const:
3329 case tok::kw_volatile:
3330 case tok::kw_restrict:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003331 case tok::kw___private:
3332 case tok::kw___local:
3333 case tok::kw___global:
3334 case tok::kw___constant:
3335 case tok::kw___read_only:
3336 case tok::kw___read_write:
3337 case tok::kw___write_only:
Steve Naroff5f8aa692008-02-11 23:15:56 +00003338 return true;
3339 }
3340}
3341
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003342/// isKnownToBeTypeSpecifier - Return true if we know that the specified token
3343/// is definitely a type-specifier. Return false if it isn't part of a type
3344/// specifier or if we're not sure.
3345bool Parser::isKnownToBeTypeSpecifier(const Token &Tok) const {
3346 switch (Tok.getKind()) {
3347 default: return false;
3348 // type-specifiers
3349 case tok::kw_short:
3350 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003351 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003352 case tok::kw___int128:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003353 case tok::kw_signed:
3354 case tok::kw_unsigned:
3355 case tok::kw__Complex:
3356 case tok::kw__Imaginary:
3357 case tok::kw_void:
3358 case tok::kw_char:
3359 case tok::kw_wchar_t:
3360 case tok::kw_char16_t:
3361 case tok::kw_char32_t:
3362 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003363 case tok::kw_half:
Chris Lattnerb3a4e432010-02-28 18:18:36 +00003364 case tok::kw_float:
3365 case tok::kw_double:
3366 case tok::kw_bool:
3367 case tok::kw__Bool:
3368 case tok::kw__Decimal32:
3369 case tok::kw__Decimal64:
3370 case tok::kw__Decimal128:
3371 case tok::kw___vector:
3372
3373 // struct-or-union-specifier (C99) or class-specifier (C++)
3374 case tok::kw_class:
3375 case tok::kw_struct:
3376 case tok::kw_union:
3377 // enum-specifier
3378 case tok::kw_enum:
3379
3380 // typedef-name
3381 case tok::annot_typename:
3382 return true;
3383 }
3384}
3385
Steve Naroff5f8aa692008-02-11 23:15:56 +00003386/// isTypeSpecifierQualifier - Return true if the current token could be the
Reid Spencer5f016e22007-07-11 17:01:13 +00003387/// start of a specifier-qualifier-list.
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003388bool Parser::isTypeSpecifierQualifier() {
Reid Spencer5f016e22007-07-11 17:01:13 +00003389 switch (Tok.getKind()) {
3390 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003391
Chris Lattner166a8fc2009-01-04 23:41:41 +00003392 case tok::identifier: // foo::bar
John Thompson82287d12010-02-05 00:12:22 +00003393 if (TryAltiVecVectorToken())
3394 return true;
3395 // Fall through.
Douglas Gregord57959a2009-03-27 23:10:48 +00003396 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003397 // Annotate typenames and C++ scope specifiers. If we get one, just
3398 // recurse to handle whatever we get.
3399 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003400 return true;
3401 if (Tok.is(tok::identifier))
3402 return false;
3403 return isTypeSpecifierQualifier();
Douglas Gregord57959a2009-03-27 23:10:48 +00003404
Chris Lattner166a8fc2009-01-04 23:41:41 +00003405 case tok::coloncolon: // ::foo::bar
3406 if (NextToken().is(tok::kw_new) || // ::new
3407 NextToken().is(tok::kw_delete)) // ::delete
3408 return false;
3409
Chris Lattner166a8fc2009-01-04 23:41:41 +00003410 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003411 return true;
3412 return isTypeSpecifierQualifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003413
Reid Spencer5f016e22007-07-11 17:01:13 +00003414 // GNU attributes support.
3415 case tok::kw___attribute:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003416 // GNU typeof support.
3417 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003418
Reid Spencer5f016e22007-07-11 17:01:13 +00003419 // type-specifiers
3420 case tok::kw_short:
3421 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003422 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003423 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003424 case tok::kw_signed:
3425 case tok::kw_unsigned:
3426 case tok::kw__Complex:
3427 case tok::kw__Imaginary:
3428 case tok::kw_void:
3429 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003430 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003431 case tok::kw_char16_t:
3432 case tok::kw_char32_t:
Reid Spencer5f016e22007-07-11 17:01:13 +00003433 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003434 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003435 case tok::kw_float:
3436 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003437 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003438 case tok::kw__Bool:
3439 case tok::kw__Decimal32:
3440 case tok::kw__Decimal64:
3441 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003442 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003443
Chris Lattner99dc9142008-04-13 18:59:07 +00003444 // struct-or-union-specifier (C99) or class-specifier (C++)
3445 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003446 case tok::kw_struct:
3447 case tok::kw_union:
3448 // enum-specifier
3449 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003450
Reid Spencer5f016e22007-07-11 17:01:13 +00003451 // type-qualifier
3452 case tok::kw_const:
3453 case tok::kw_volatile:
3454 case tok::kw_restrict:
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00003455
3456 // typedef-name
Chris Lattnerb31757b2009-01-06 05:06:21 +00003457 case tok::annot_typename:
Reid Spencer5f016e22007-07-11 17:01:13 +00003458 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003459
Chris Lattner7c186be2008-10-20 00:25:30 +00003460 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3461 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003462 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003463
Steve Naroff239f0732008-12-25 14:16:32 +00003464 case tok::kw___cdecl:
3465 case tok::kw___stdcall:
3466 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003467 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003468 case tok::kw___w64:
3469 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003470 case tok::kw___ptr32:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003471 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003472 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003473
3474 case tok::kw___private:
3475 case tok::kw___local:
3476 case tok::kw___global:
3477 case tok::kw___constant:
3478 case tok::kw___read_only:
3479 case tok::kw___read_write:
3480 case tok::kw___write_only:
3481
Eli Friedman290eeb02009-06-08 23:27:34 +00003482 return true;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003483
3484 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003485 return getLangOpts().OpenCL;
Eli Friedmanb001de72011-10-06 23:00:33 +00003486
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003487 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003488 case tok::kw__Atomic:
3489 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003490 }
3491}
3492
3493/// isDeclarationSpecifier() - Return true if the current token is part of a
3494/// declaration specifier.
Douglas Gregor9497a732010-09-16 01:51:54 +00003495///
3496/// \param DisambiguatingWithExpression True to indicate that the purpose of
3497/// this check is to disambiguate between an expression and a declaration.
3498bool Parser::isDeclarationSpecifier(bool DisambiguatingWithExpression) {
Reid Spencer5f016e22007-07-11 17:01:13 +00003499 switch (Tok.getKind()) {
3500 default: return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003501
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003502 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003503 return getLangOpts().OpenCL;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003504
Chris Lattner166a8fc2009-01-04 23:41:41 +00003505 case tok::identifier: // foo::bar
Steve Naroff61f72cb2009-03-09 21:12:44 +00003506 // Unfortunate hack to support "Class.factoryMethod" notation.
David Blaikie4e4d0842012-03-11 07:00:24 +00003507 if (getLangOpts().ObjC1 && NextToken().is(tok::period))
Steve Naroff61f72cb2009-03-09 21:12:44 +00003508 return false;
John Thompson82287d12010-02-05 00:12:22 +00003509 if (TryAltiVecVectorToken())
3510 return true;
3511 // Fall through.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003512 case tok::kw_decltype: // decltype(T())::type
Douglas Gregord57959a2009-03-27 23:10:48 +00003513 case tok::kw_typename: // typename T::type
Chris Lattner166a8fc2009-01-04 23:41:41 +00003514 // Annotate typenames and C++ scope specifiers. If we get one, just
3515 // recurse to handle whatever we get.
3516 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003517 return true;
3518 if (Tok.is(tok::identifier))
3519 return false;
Douglas Gregor9497a732010-09-16 01:51:54 +00003520
3521 // If we're in Objective-C and we have an Objective-C class type followed
3522 // by an identifier and then either ':' or ']', in a place where an
3523 // expression is permitted, then this is probably a class message send
3524 // missing the initial '['. In this case, we won't consider this to be
3525 // the start of a declaration.
3526 if (DisambiguatingWithExpression &&
3527 isStartOfObjCClassMessageMissingOpenBracket())
3528 return false;
3529
John McCall9ba61662010-02-26 08:45:28 +00003530 return isDeclarationSpecifier();
3531
Chris Lattner166a8fc2009-01-04 23:41:41 +00003532 case tok::coloncolon: // ::foo::bar
3533 if (NextToken().is(tok::kw_new) || // ::new
3534 NextToken().is(tok::kw_delete)) // ::delete
3535 return false;
Mike Stump1eb44332009-09-09 15:08:12 +00003536
Chris Lattner166a8fc2009-01-04 23:41:41 +00003537 // Annotate typenames and C++ scope specifiers. If we get one, just
3538 // recurse to handle whatever we get.
3539 if (TryAnnotateTypeOrScopeToken())
John McCall9ba61662010-02-26 08:45:28 +00003540 return true;
3541 return isDeclarationSpecifier();
Mike Stump1eb44332009-09-09 15:08:12 +00003542
Reid Spencer5f016e22007-07-11 17:01:13 +00003543 // storage-class-specifier
3544 case tok::kw_typedef:
3545 case tok::kw_extern:
Steve Naroff8d54bf22007-12-18 00:16:02 +00003546 case tok::kw___private_extern__:
Reid Spencer5f016e22007-07-11 17:01:13 +00003547 case tok::kw_static:
3548 case tok::kw_auto:
3549 case tok::kw_register:
3550 case tok::kw___thread:
Mike Stump1eb44332009-09-09 15:08:12 +00003551
Douglas Gregor8d267c52011-09-09 02:06:17 +00003552 // Modules
3553 case tok::kw___module_private__:
3554
Reid Spencer5f016e22007-07-11 17:01:13 +00003555 // type-specifiers
3556 case tok::kw_short:
3557 case tok::kw_long:
Francois Pichet338d7f72011-04-28 01:59:37 +00003558 case tok::kw___int64:
Richard Smith5a5a9712012-04-04 06:24:32 +00003559 case tok::kw___int128:
Reid Spencer5f016e22007-07-11 17:01:13 +00003560 case tok::kw_signed:
3561 case tok::kw_unsigned:
3562 case tok::kw__Complex:
3563 case tok::kw__Imaginary:
3564 case tok::kw_void:
3565 case tok::kw_char:
Argyrios Kyrtzidis64c438a2008-08-09 16:51:54 +00003566 case tok::kw_wchar_t:
Alisdair Meredithf5c209d2009-07-14 06:30:34 +00003567 case tok::kw_char16_t:
3568 case tok::kw_char32_t:
3569
Reid Spencer5f016e22007-07-11 17:01:13 +00003570 case tok::kw_int:
Anton Korobeynikovaa4a99b2011-10-14 23:23:15 +00003571 case tok::kw_half:
Reid Spencer5f016e22007-07-11 17:01:13 +00003572 case tok::kw_float:
3573 case tok::kw_double:
Chris Lattner9298d962007-11-15 05:25:19 +00003574 case tok::kw_bool:
Reid Spencer5f016e22007-07-11 17:01:13 +00003575 case tok::kw__Bool:
3576 case tok::kw__Decimal32:
3577 case tok::kw__Decimal64:
3578 case tok::kw__Decimal128:
John Thompson82287d12010-02-05 00:12:22 +00003579 case tok::kw___vector:
Mike Stump1eb44332009-09-09 15:08:12 +00003580
Chris Lattner99dc9142008-04-13 18:59:07 +00003581 // struct-or-union-specifier (C99) or class-specifier (C++)
3582 case tok::kw_class:
Reid Spencer5f016e22007-07-11 17:01:13 +00003583 case tok::kw_struct:
3584 case tok::kw_union:
3585 // enum-specifier
3586 case tok::kw_enum:
Mike Stump1eb44332009-09-09 15:08:12 +00003587
Reid Spencer5f016e22007-07-11 17:01:13 +00003588 // type-qualifier
3589 case tok::kw_const:
3590 case tok::kw_volatile:
3591 case tok::kw_restrict:
Steve Naroffd1861fd2007-07-31 12:34:36 +00003592
Reid Spencer5f016e22007-07-11 17:01:13 +00003593 // function-specifier
3594 case tok::kw_inline:
Douglas Gregorb48fe382008-10-31 09:07:45 +00003595 case tok::kw_virtual:
3596 case tok::kw_explicit:
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003597
Peter Collingbournec6eb44b2011-04-15 00:35:57 +00003598 // static_assert-declaration
3599 case tok::kw__Static_assert:
3600
Chris Lattner1ef08762007-08-09 17:01:07 +00003601 // GNU typeof support.
3602 case tok::kw_typeof:
Mike Stump1eb44332009-09-09 15:08:12 +00003603
Chris Lattner1ef08762007-08-09 17:01:07 +00003604 // GNU attributes.
Chris Lattnerd6c7c182007-08-09 16:40:21 +00003605 case tok::kw___attribute:
Reid Spencer5f016e22007-07-11 17:01:13 +00003606 return true;
Mike Stump1eb44332009-09-09 15:08:12 +00003607
Francois Pichete3d49b42011-06-19 08:02:06 +00003608 // C++0x decltype.
David Blaikie42d6d0c2011-12-04 05:04:18 +00003609 case tok::annot_decltype:
Francois Pichete3d49b42011-06-19 08:02:06 +00003610 return true;
3611
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00003612 // C11 _Atomic()
Eli Friedmanb001de72011-10-06 23:00:33 +00003613 case tok::kw__Atomic:
3614 return true;
3615
Chris Lattnerf3948c42008-07-26 03:38:44 +00003616 // GNU ObjC bizarre protocol extension: <proto1,proto2> with implicit 'id'.
3617 case tok::less:
David Blaikie4e4d0842012-03-11 07:00:24 +00003618 return getLangOpts().ObjC1;
Mike Stump1eb44332009-09-09 15:08:12 +00003619
Douglas Gregord9d75e52011-04-27 05:41:15 +00003620 // typedef-name
3621 case tok::annot_typename:
3622 return !DisambiguatingWithExpression ||
3623 !isStartOfObjCClassMessageMissingOpenBracket();
3624
Steve Naroff47f52092009-01-06 19:34:12 +00003625 case tok::kw___declspec:
Steve Naroff239f0732008-12-25 14:16:32 +00003626 case tok::kw___cdecl:
3627 case tok::kw___stdcall:
3628 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003629 case tok::kw___thiscall:
Eli Friedman290eeb02009-06-08 23:27:34 +00003630 case tok::kw___w64:
3631 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003632 case tok::kw___ptr32:
Eli Friedman290eeb02009-06-08 23:27:34 +00003633 case tok::kw___forceinline:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003634 case tok::kw___pascal:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003635 case tok::kw___unaligned:
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003636
3637 case tok::kw___private:
3638 case tok::kw___local:
3639 case tok::kw___global:
3640 case tok::kw___constant:
3641 case tok::kw___read_only:
3642 case tok::kw___read_write:
3643 case tok::kw___write_only:
3644
Eli Friedman290eeb02009-06-08 23:27:34 +00003645 return true;
Reid Spencer5f016e22007-07-11 17:01:13 +00003646 }
3647}
3648
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003649bool Parser::isConstructorDeclarator() {
3650 TentativeParsingAction TPA(*this);
3651
3652 // Parse the C++ scope specifier.
3653 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003654 if (ParseOptionalCXXScopeSpecifier(SS, ParsedType(),
3655 /*EnteringContext=*/true)) {
John McCall9ba61662010-02-26 08:45:28 +00003656 TPA.Revert();
3657 return false;
3658 }
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003659
3660 // Parse the constructor name.
3661 if (Tok.is(tok::identifier) || Tok.is(tok::annot_template_id)) {
3662 // We already know that we have a constructor name; just consume
3663 // the token.
3664 ConsumeToken();
3665 } else {
3666 TPA.Revert();
3667 return false;
3668 }
3669
Richard Smith22592862012-03-27 23:05:05 +00003670 // Current class name must be followed by a left parenthesis.
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003671 if (Tok.isNot(tok::l_paren)) {
3672 TPA.Revert();
3673 return false;
3674 }
3675 ConsumeParen();
3676
Richard Smith22592862012-03-27 23:05:05 +00003677 // A right parenthesis, or ellipsis followed by a right parenthesis signals
3678 // that we have a constructor.
3679 if (Tok.is(tok::r_paren) ||
3680 (Tok.is(tok::ellipsis) && NextToken().is(tok::r_paren))) {
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003681 TPA.Revert();
3682 return true;
3683 }
3684
3685 // If we need to, enter the specified scope.
3686 DeclaratorScopeObj DeclScopeObj(*this, SS);
Douglas Gregor23c94db2010-07-02 17:43:08 +00003687 if (SS.isSet() && Actions.ShouldEnterDeclaratorScope(getCurScope(), SS))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003688 DeclScopeObj.EnterDeclaratorScope();
3689
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003690 // Optionally skip Microsoft attributes.
John McCall0b7e6782011-03-24 11:26:52 +00003691 ParsedAttributes Attrs(AttrFactory);
Francois Pichetdfaa5fb2011-01-31 04:54:32 +00003692 MaybeParseMicrosoftAttributes(Attrs);
3693
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003694 // Check whether the next token(s) are part of a declaration
3695 // specifier, in which case we have the start of a parameter and,
3696 // therefore, we know that this is a constructor.
Richard Smith412e0cc2012-03-27 00:56:56 +00003697 bool IsConstructor = false;
3698 if (isDeclarationSpecifier())
3699 IsConstructor = true;
3700 else if (Tok.is(tok::identifier) ||
3701 (Tok.is(tok::annot_cxxscope) && NextToken().is(tok::identifier))) {
3702 // We've seen "C ( X" or "C ( X::Y", but "X" / "X::Y" is not a type.
3703 // This might be a parenthesized member name, but is more likely to
3704 // be a constructor declaration with an invalid argument type. Keep
3705 // looking.
3706 if (Tok.is(tok::annot_cxxscope))
3707 ConsumeToken();
3708 ConsumeToken();
3709
3710 // If this is not a constructor, we must be parsing a declarator,
Richard Smith5d8388c2012-03-27 01:42:32 +00003711 // which must have one of the following syntactic forms (see the
3712 // grammar extract at the start of ParseDirectDeclarator):
Richard Smith412e0cc2012-03-27 00:56:56 +00003713 switch (Tok.getKind()) {
3714 case tok::l_paren:
3715 // C(X ( int));
3716 case tok::l_square:
3717 // C(X [ 5]);
3718 // C(X [ [attribute]]);
3719 case tok::coloncolon:
3720 // C(X :: Y);
3721 // C(X :: *p);
3722 case tok::r_paren:
3723 // C(X )
3724 // Assume this isn't a constructor, rather than assuming it's a
3725 // constructor with an unnamed parameter of an ill-formed type.
3726 break;
3727
3728 default:
3729 IsConstructor = true;
3730 break;
3731 }
3732 }
3733
Douglas Gregor0efc2c12010-01-13 17:31:36 +00003734 TPA.Revert();
3735 return IsConstructor;
3736}
Reid Spencer5f016e22007-07-11 17:01:13 +00003737
3738/// ParseTypeQualifierListOpt
Dawn Perchik52fc3142010-09-03 01:29:35 +00003739/// type-qualifier-list: [C99 6.7.5]
3740/// type-qualifier
3741/// [vendor] attributes
3742/// [ only if VendorAttributesAllowed=true ]
3743/// type-qualifier-list type-qualifier
3744/// [vendor] type-qualifier-list attributes
3745/// [ only if VendorAttributesAllowed=true ]
3746/// [C++0x] attribute-specifier[opt] is allowed before cv-qualifier-seq
3747/// [ only if CXX0XAttributesAllowed=true ]
3748/// Note: vendor can be GNU, MS, etc.
Reid Spencer5f016e22007-07-11 17:01:13 +00003749///
Dawn Perchik52fc3142010-09-03 01:29:35 +00003750void Parser::ParseTypeQualifierListOpt(DeclSpec &DS,
3751 bool VendorAttributesAllowed,
Richard Smithc56298d2012-04-10 03:25:07 +00003752 bool CXX11AttributesAllowed) {
3753 if (getLangOpts().CPlusPlus0x && CXX11AttributesAllowed &&
Richard Smith6ee326a2012-04-10 01:32:12 +00003754 isCXX11AttributeSpecifier()) {
John McCall0b7e6782011-03-24 11:26:52 +00003755 ParsedAttributesWithRange attrs(AttrFactory);
Richard Smithc56298d2012-04-10 03:25:07 +00003756 ParseCXX11Attributes(attrs);
Richard Smith6ee326a2012-04-10 01:32:12 +00003757 DS.takeAttributesFrom(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00003758 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003759
3760 SourceLocation EndLoc;
3761
Reid Spencer5f016e22007-07-11 17:01:13 +00003762 while (1) {
John McCallfec54012009-08-03 20:12:06 +00003763 bool isInvalid = false;
Reid Spencer5f016e22007-07-11 17:01:13 +00003764 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00003765 unsigned DiagID = 0;
Reid Spencer5f016e22007-07-11 17:01:13 +00003766 SourceLocation Loc = Tok.getLocation();
3767
3768 switch (Tok.getKind()) {
Douglas Gregor1a480c42010-08-27 17:35:51 +00003769 case tok::code_completion:
3770 Actions.CodeCompleteTypeQualifiers(DS);
Argyrios Kyrtzidis7d100872011-09-04 03:32:15 +00003771 return cutOffParsing();
Douglas Gregor1a480c42010-08-27 17:35:51 +00003772
Reid Spencer5f016e22007-07-11 17:01:13 +00003773 case tok::kw_const:
John McCallfec54012009-08-03 20:12:06 +00003774 isInvalid = DS.SetTypeQual(DeclSpec::TQ_const , Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003775 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003776 break;
3777 case tok::kw_volatile:
John McCallfec54012009-08-03 20:12:06 +00003778 isInvalid = DS.SetTypeQual(DeclSpec::TQ_volatile, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003779 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003780 break;
3781 case tok::kw_restrict:
John McCallfec54012009-08-03 20:12:06 +00003782 isInvalid = DS.SetTypeQual(DeclSpec::TQ_restrict, Loc, PrevSpec, DiagID,
David Blaikie4e4d0842012-03-11 07:00:24 +00003783 getLangOpts());
Reid Spencer5f016e22007-07-11 17:01:13 +00003784 break;
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003785
3786 // OpenCL qualifiers:
3787 case tok::kw_private:
David Blaikie4e4d0842012-03-11 07:00:24 +00003788 if (!getLangOpts().OpenCL)
Peter Collingbourne207f4d82011-03-18 22:38:29 +00003789 goto DoneWithTypeQuals;
3790 case tok::kw___private:
3791 case tok::kw___global:
3792 case tok::kw___local:
3793 case tok::kw___constant:
3794 case tok::kw___read_only:
3795 case tok::kw___write_only:
3796 case tok::kw___read_write:
3797 ParseOpenCLQualifiers(DS);
3798 break;
3799
Eli Friedman290eeb02009-06-08 23:27:34 +00003800 case tok::kw___w64:
Steve Naroff86bc6cf2008-12-25 14:41:26 +00003801 case tok::kw___ptr64:
Francois Pichet58fd97a2011-08-25 00:36:46 +00003802 case tok::kw___ptr32:
Steve Naroff239f0732008-12-25 14:16:32 +00003803 case tok::kw___cdecl:
3804 case tok::kw___stdcall:
3805 case tok::kw___fastcall:
Douglas Gregorf813a2c2010-05-18 16:57:00 +00003806 case tok::kw___thiscall:
Francois Pichet3bd9aa42011-08-18 09:59:55 +00003807 case tok::kw___unaligned:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003808 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003809 ParseMicrosoftTypeAttributes(DS.getAttributes());
Eli Friedman290eeb02009-06-08 23:27:34 +00003810 continue;
3811 }
3812 goto DoneWithTypeQuals;
Dawn Perchik52fc3142010-09-03 01:29:35 +00003813 case tok::kw___pascal:
3814 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003815 ParseBorlandTypeAttributes(DS.getAttributes());
Dawn Perchik52fc3142010-09-03 01:29:35 +00003816 continue;
3817 }
3818 goto DoneWithTypeQuals;
Reid Spencer5f016e22007-07-11 17:01:13 +00003819 case tok::kw___attribute:
Dawn Perchik52fc3142010-09-03 01:29:35 +00003820 if (VendorAttributesAllowed) {
John McCall7f040a92010-12-24 02:08:15 +00003821 ParseGNUAttributes(DS.getAttributes());
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003822 continue; // do *not* consume the next token!
3823 }
3824 // otherwise, FALL THROUGH!
3825 default:
Steve Naroff239f0732008-12-25 14:16:32 +00003826 DoneWithTypeQuals:
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003827 // If this is not a type-qualifier token, we're done reading type
3828 // qualifiers. First verify that DeclSpec's are consistent.
Douglas Gregor9b3064b2009-04-01 22:41:11 +00003829 DS.Finish(Diags, PP);
Abramo Bagnara796aa442011-03-12 11:17:06 +00003830 if (EndLoc.isValid())
3831 DS.SetRangeEnd(EndLoc);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00003832 return;
Reid Spencer5f016e22007-07-11 17:01:13 +00003833 }
Chris Lattnera1fcbad2008-12-18 06:50:14 +00003834
Reid Spencer5f016e22007-07-11 17:01:13 +00003835 // If the specifier combination wasn't legal, issue a diagnostic.
3836 if (isInvalid) {
3837 assert(PrevSpec && "Method did not return previous specifier!");
Chris Lattner1ab3b962008-11-18 07:48:38 +00003838 Diag(Tok, DiagID) << PrevSpec;
Reid Spencer5f016e22007-07-11 17:01:13 +00003839 }
Abramo Bagnara796aa442011-03-12 11:17:06 +00003840 EndLoc = ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00003841 }
3842}
3843
3844
3845/// ParseDeclarator - Parse and verify a newly-initialized declarator.
3846///
3847void Parser::ParseDeclarator(Declarator &D) {
3848 /// This implements the 'declarator' production in the C grammar, then checks
3849 /// for well-formedness and issues diagnostics.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003850 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Reid Spencer5f016e22007-07-11 17:01:13 +00003851}
3852
Richard Smith9988f282012-03-29 01:16:42 +00003853static bool isPtrOperatorToken(tok::TokenKind Kind, const LangOptions &Lang) {
3854 if (Kind == tok::star || Kind == tok::caret)
3855 return true;
3856
3857 // We parse rvalue refs in C++03, because otherwise the errors are scary.
3858 if (!Lang.CPlusPlus)
3859 return false;
3860
3861 return Kind == tok::amp || Kind == tok::ampamp;
3862}
3863
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003864/// ParseDeclaratorInternal - Parse a C or C++ declarator. The direct-declarator
3865/// is parsed by the function passed to it. Pass null, and the direct-declarator
3866/// isn't parsed at all, making this function effectively parse the C++
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003867/// ptr-operator production.
3868///
Richard Smith0706df42011-10-19 21:33:05 +00003869/// If the grammar of this construct is extended, matching changes must also be
Richard Smith5d8388c2012-03-27 01:42:32 +00003870/// made to TryParseDeclarator and MightBeDeclarator, and possibly to
3871/// isConstructorDeclarator.
Richard Smith0706df42011-10-19 21:33:05 +00003872///
Sebastian Redlf30208a2009-01-24 21:16:55 +00003873/// declarator: [C99 6.7.5] [C++ 8p4, dcl.decl]
3874/// [C] pointer[opt] direct-declarator
3875/// [C++] direct-declarator
3876/// [C++] ptr-operator declarator
Reid Spencer5f016e22007-07-11 17:01:13 +00003877///
3878/// pointer: [C99 6.7.5]
3879/// '*' type-qualifier-list[opt]
3880/// '*' type-qualifier-list[opt] pointer
3881///
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003882/// ptr-operator:
3883/// '*' cv-qualifier-seq[opt]
3884/// '&'
Sebastian Redl05532f22009-03-15 22:02:01 +00003885/// [C++0x] '&&'
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003886/// [GNU] '&' restrict[opt] attributes[opt]
Sebastian Redl05532f22009-03-15 22:02:01 +00003887/// [GNU?] '&&' restrict[opt] attributes[opt]
Sebastian Redlf30208a2009-01-24 21:16:55 +00003888/// '::'[opt] nested-name-specifier '*' cv-qualifier-seq[opt]
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003889void Parser::ParseDeclaratorInternal(Declarator &D,
3890 DirectDeclParseFunction DirectDeclParser) {
Douglas Gregor91a28862009-08-26 14:27:30 +00003891 if (Diags.hasAllExtensionsSilenced())
3892 D.setExtension();
Douglas Gregor2ccccb32010-08-23 18:23:48 +00003893
Sebastian Redlf30208a2009-01-24 21:16:55 +00003894 // C++ member pointers start with a '::' or a nested-name.
3895 // Member pointers get special handling, since there's no place for the
3896 // scope spec in the generic path below.
David Blaikie4e4d0842012-03-11 07:00:24 +00003897 if (getLangOpts().CPlusPlus &&
Chris Lattnerf919bfe2009-03-24 17:04:48 +00003898 (Tok.is(tok::coloncolon) || Tok.is(tok::identifier) ||
3899 Tok.is(tok::annot_cxxscope))) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003900 bool EnteringContext = D.getContext() == Declarator::FileContext ||
3901 D.getContext() == Declarator::MemberContext;
Sebastian Redlf30208a2009-01-24 21:16:55 +00003902 CXXScopeSpec SS;
Douglas Gregorefaa93a2011-11-07 17:33:42 +00003903 ParseOptionalCXXScopeSpecifier(SS, ParsedType(), EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00003904
Jeffrey Yasskinedc28772010-04-07 23:29:58 +00003905 if (SS.isNotEmpty()) {
Mike Stump1eb44332009-09-09 15:08:12 +00003906 if (Tok.isNot(tok::star)) {
Sebastian Redlf30208a2009-01-24 21:16:55 +00003907 // The scope spec really belongs to the direct-declarator.
3908 D.getCXXScopeSpec() = SS;
3909 if (DirectDeclParser)
3910 (this->*DirectDeclParser)(D);
3911 return;
3912 }
3913
3914 SourceLocation Loc = ConsumeToken();
Sebastian Redlab197ba2009-02-09 18:23:29 +00003915 D.SetRangeEnd(Loc);
John McCall0b7e6782011-03-24 11:26:52 +00003916 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003917 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003918 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003919
3920 // Recurse to parse whatever is left.
3921 ParseDeclaratorInternal(D, DirectDeclParser);
3922
3923 // Sema will have to catch (syntactically invalid) pointers into global
3924 // scope. It has to catch pointers into namespace scope anyway.
3925 D.AddTypeInfo(DeclaratorChunk::getMemberPointer(SS,DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003926 Loc),
3927 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003928 /* Don't replace range end. */SourceLocation());
Sebastian Redlf30208a2009-01-24 21:16:55 +00003929 return;
3930 }
3931 }
3932
3933 tok::TokenKind Kind = Tok.getKind();
Steve Naroff5618bd42008-08-27 16:04:49 +00003934 // Not a pointer, C++ reference, or block.
Richard Smith9988f282012-03-29 01:16:42 +00003935 if (!isPtrOperatorToken(Kind, getLangOpts())) {
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003936 if (DirectDeclParser)
3937 (this->*DirectDeclParser)(D);
Douglas Gregor2f1bc522008-11-07 20:08:42 +00003938 return;
3939 }
Sebastian Redlf30208a2009-01-24 21:16:55 +00003940
Sebastian Redl05532f22009-03-15 22:02:01 +00003941 // Otherwise, '*' -> pointer, '^' -> block, '&' -> lvalue reference,
3942 // '&&' -> rvalue reference
Sebastian Redl743de1f2009-03-23 00:00:23 +00003943 SourceLocation Loc = ConsumeToken(); // Eat the *, ^, & or &&.
Sebastian Redlab197ba2009-02-09 18:23:29 +00003944 D.SetRangeEnd(Loc);
Reid Spencer5f016e22007-07-11 17:01:13 +00003945
Chris Lattner9af55002009-03-27 04:18:06 +00003946 if (Kind == tok::star || Kind == tok::caret) {
Chris Lattner76549142008-02-21 01:32:26 +00003947 // Is a pointer.
John McCall0b7e6782011-03-24 11:26:52 +00003948 DeclSpec DS(AttrFactory);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003949
Richard Smith6ee326a2012-04-10 01:32:12 +00003950 // FIXME: GNU attributes are not allowed here in a new-type-id.
Reid Spencer5f016e22007-07-11 17:01:13 +00003951 ParseTypeQualifierListOpt(DS);
Sebastian Redlab197ba2009-02-09 18:23:29 +00003952 D.ExtendWithDeclSpec(DS);
Sebastian Redlf30208a2009-01-24 21:16:55 +00003953
Reid Spencer5f016e22007-07-11 17:01:13 +00003954 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003955 ParseDeclaratorInternal(D, DirectDeclParser);
Steve Naroff5618bd42008-08-27 16:04:49 +00003956 if (Kind == tok::star)
3957 // Remember that we parsed a pointer type, and remember the type-quals.
3958 D.AddTypeInfo(DeclaratorChunk::getPointer(DS.getTypeQualifiers(), Loc,
Chandler Carruthd067c072011-02-23 18:51:59 +00003959 DS.getConstSpecLoc(),
3960 DS.getVolatileSpecLoc(),
John McCall0b7e6782011-03-24 11:26:52 +00003961 DS.getRestrictSpecLoc()),
3962 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003963 SourceLocation());
Steve Naroff5618bd42008-08-27 16:04:49 +00003964 else
3965 // Remember that we parsed a Block type, and remember the type-quals.
Mike Stump1eb44332009-09-09 15:08:12 +00003966 D.AddTypeInfo(DeclaratorChunk::getBlockPointer(DS.getTypeQualifiers(),
John McCall0b7e6782011-03-24 11:26:52 +00003967 Loc),
3968 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00003969 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00003970 } else {
3971 // Is a reference
John McCall0b7e6782011-03-24 11:26:52 +00003972 DeclSpec DS(AttrFactory);
Reid Spencer5f016e22007-07-11 17:01:13 +00003973
Sebastian Redl743de1f2009-03-23 00:00:23 +00003974 // Complain about rvalue references in C++03, but then go on and build
3975 // the declarator.
Richard Smith7fe62082011-10-15 05:09:34 +00003976 if (Kind == tok::ampamp)
David Blaikie4e4d0842012-03-11 07:00:24 +00003977 Diag(Loc, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00003978 diag::warn_cxx98_compat_rvalue_reference :
3979 diag::ext_rvalue_reference);
Sebastian Redl743de1f2009-03-23 00:00:23 +00003980
Richard Smith6ee326a2012-04-10 01:32:12 +00003981 // GNU-style and C++11 attributes are allowed here, as is restrict.
3982 ParseTypeQualifierListOpt(DS);
3983 D.ExtendWithDeclSpec(DS);
3984
Reid Spencer5f016e22007-07-11 17:01:13 +00003985 // C++ 8.3.2p1: cv-qualified references are ill-formed except when the
3986 // cv-qualifiers are introduced through the use of a typedef or of a
3987 // template type argument, in which case the cv-qualifiers are ignored.
Reid Spencer5f016e22007-07-11 17:01:13 +00003988 if (DS.getTypeQualifiers() != DeclSpec::TQ_unspecified) {
3989 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
3990 Diag(DS.getConstSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003991 diag::err_invalid_reference_qualifier_application) << "const";
Reid Spencer5f016e22007-07-11 17:01:13 +00003992 if (DS.getTypeQualifiers() & DeclSpec::TQ_volatile)
3993 Diag(DS.getVolatileSpecLoc(),
Chris Lattner1ab3b962008-11-18 07:48:38 +00003994 diag::err_invalid_reference_qualifier_application) << "volatile";
Reid Spencer5f016e22007-07-11 17:01:13 +00003995 }
3996
3997 // Recursively parse the declarator.
Sebastian Redl4c5d3202008-11-21 19:14:01 +00003998 ParseDeclaratorInternal(D, DirectDeclParser);
Reid Spencer5f016e22007-07-11 17:01:13 +00003999
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004000 if (D.getNumTypeObjects() > 0) {
4001 // C++ [dcl.ref]p4: There shall be no references to references.
4002 DeclaratorChunk& InnerChunk = D.getTypeObject(D.getNumTypeObjects() - 1);
4003 if (InnerChunk.Kind == DeclaratorChunk::Reference) {
Chris Lattnerda83bac2008-11-19 07:37:42 +00004004 if (const IdentifierInfo *II = D.getIdentifier())
4005 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4006 << II;
4007 else
4008 Diag(InnerChunk.Loc, diag::err_illegal_decl_reference_to_reference)
4009 << "type name";
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004010
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004011 // Once we've complained about the reference-to-reference, we
Douglas Gregorf1f9b4e2008-11-03 15:51:28 +00004012 // can go ahead and build the (technically ill-formed)
4013 // declarator: reference collapsing will take care of it.
4014 }
4015 }
4016
Reid Spencer5f016e22007-07-11 17:01:13 +00004017 // Remember that we parsed a reference type. It doesn't have type-quals.
Chris Lattner76549142008-02-21 01:32:26 +00004018 D.AddTypeInfo(DeclaratorChunk::getReference(DS.getTypeQualifiers(), Loc,
Sebastian Redl05532f22009-03-15 22:02:01 +00004019 Kind == tok::amp),
John McCall0b7e6782011-03-24 11:26:52 +00004020 DS.getAttributes(),
Sebastian Redlab197ba2009-02-09 18:23:29 +00004021 SourceLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004022 }
4023}
4024
Richard Smith9988f282012-03-29 01:16:42 +00004025static void diagnoseMisplacedEllipsis(Parser &P, Declarator &D,
4026 SourceLocation EllipsisLoc) {
4027 if (EllipsisLoc.isValid()) {
4028 FixItHint Insertion;
4029 if (!D.getEllipsisLoc().isValid()) {
4030 Insertion = FixItHint::CreateInsertion(D.getIdentifierLoc(), "...");
4031 D.setEllipsisLoc(EllipsisLoc);
4032 }
4033 P.Diag(EllipsisLoc, diag::err_misplaced_ellipsis_in_declaration)
4034 << FixItHint::CreateRemoval(EllipsisLoc) << Insertion << !D.hasName();
4035 }
4036}
4037
Reid Spencer5f016e22007-07-11 17:01:13 +00004038/// ParseDirectDeclarator
4039/// direct-declarator: [C99 6.7.5]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004040/// [C99] identifier
Reid Spencer5f016e22007-07-11 17:01:13 +00004041/// '(' declarator ')'
4042/// [GNU] '(' attributes declarator ')'
4043/// [C90] direct-declarator '[' constant-expression[opt] ']'
4044/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4045/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4046/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4047/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004048/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4049/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004050/// direct-declarator '(' parameter-type-list ')'
4051/// direct-declarator '(' identifier-list[opt] ')'
4052/// [GNU] direct-declarator '(' parameter-forward-declarations
4053/// parameter-type-list[opt] ')'
Argyrios Kyrtzidis971c4fa2008-10-24 21:46:40 +00004054/// [C++] direct-declarator '(' parameter-declaration-clause ')'
4055/// cv-qualifier-seq[opt] exception-specification[opt]
Richard Smith6ee326a2012-04-10 01:32:12 +00004056/// [C++11] direct-declarator '(' parameter-declaration-clause ')'
4057/// attribute-specifier-seq[opt] cv-qualifier-seq[opt]
4058/// ref-qualifier[opt] exception-specification[opt]
Douglas Gregorb48fe382008-10-31 09:07:45 +00004059/// [C++] declarator-id
Richard Smith6ee326a2012-04-10 01:32:12 +00004060/// [C++11] declarator-id attribute-specifier-seq[opt]
Douglas Gregor42a552f2008-11-05 20:51:48 +00004061///
4062/// declarator-id: [C++ 8]
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004063/// '...'[opt] id-expression
Douglas Gregor42a552f2008-11-05 20:51:48 +00004064/// '::'[opt] nested-name-specifier[opt] type-name
4065///
4066/// id-expression: [C++ 5.1]
4067/// unqualified-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004068/// qualified-id
Douglas Gregor42a552f2008-11-05 20:51:48 +00004069///
4070/// unqualified-id: [C++ 5.1]
Mike Stump1eb44332009-09-09 15:08:12 +00004071/// identifier
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004072/// operator-function-id
Douglas Gregordb422df2009-09-25 21:45:23 +00004073/// conversion-function-id
Mike Stump1eb44332009-09-09 15:08:12 +00004074/// '~' class-name
Douglas Gregor39a8de12009-02-25 19:37:18 +00004075/// template-id
Argyrios Kyrtzidisc7ed9c62008-11-07 22:02:30 +00004076///
Richard Smith5d8388c2012-03-27 01:42:32 +00004077/// Note, any additional constructs added here may need corresponding changes
4078/// in isConstructorDeclarator.
Reid Spencer5f016e22007-07-11 17:01:13 +00004079void Parser::ParseDirectDeclarator(Declarator &D) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004080 DeclaratorScopeObj DeclScopeObj(*this, D.getCXXScopeSpec());
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004081
David Blaikie4e4d0842012-03-11 07:00:24 +00004082 if (getLangOpts().CPlusPlus && D.mayHaveIdentifier()) {
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004083 // ParseDeclaratorInternal might already have parsed the scope.
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004084 if (D.getCXXScopeSpec().isEmpty()) {
Douglas Gregorefaa93a2011-11-07 17:33:42 +00004085 bool EnteringContext = D.getContext() == Declarator::FileContext ||
4086 D.getContext() == Declarator::MemberContext;
4087 ParseOptionalCXXScopeSpecifier(D.getCXXScopeSpec(), ParsedType(),
4088 EnteringContext);
John McCall9ba61662010-02-26 08:45:28 +00004089 }
4090
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004091 if (D.getCXXScopeSpec().isValid()) {
Douglas Gregor23c94db2010-07-02 17:43:08 +00004092 if (Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
John McCalle7e278b2009-12-11 20:04:54 +00004093 // Change the declaration context for name lookup, until this function
4094 // is exited (and the declarator has been parsed).
4095 DeclScopeObj.EnterDeclaratorScope();
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004096 }
4097
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004098 // C++0x [dcl.fct]p14:
4099 // There is a syntactic ambiguity when an ellipsis occurs at the end
4100 // of a parameter-declaration-clause without a preceding comma. In
4101 // this case, the ellipsis is parsed as part of the
4102 // abstract-declarator if the type of the parameter names a template
4103 // parameter pack that has not been expanded; otherwise, it is parsed
4104 // as part of the parameter-declaration-clause.
Richard Smith9988f282012-03-29 01:16:42 +00004105 if (Tok.is(tok::ellipsis) && D.getCXXScopeSpec().isEmpty() &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004106 !((D.getContext() == Declarator::PrototypeContext ||
4107 D.getContext() == Declarator::BlockLiteralContext) &&
Douglas Gregora8bc8c92010-12-23 22:44:42 +00004108 NextToken().is(tok::r_paren) &&
Richard Smith9988f282012-03-29 01:16:42 +00004109 !Actions.containsUnexpandedParameterPacks(D))) {
4110 SourceLocation EllipsisLoc = ConsumeToken();
4111 if (isPtrOperatorToken(Tok.getKind(), getLangOpts())) {
4112 // The ellipsis was put in the wrong place. Recover, and explain to
4113 // the user what they should have done.
4114 ParseDeclarator(D);
4115 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4116 return;
4117 } else
4118 D.setEllipsisLoc(EllipsisLoc);
4119
4120 // The ellipsis can't be followed by a parenthesized declarator. We
4121 // check for that in ParseParenDeclarator, after we have disambiguated
4122 // the l_paren token.
4123 }
4124
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004125 if (Tok.is(tok::identifier) || Tok.is(tok::kw_operator) ||
4126 Tok.is(tok::annot_template_id) || Tok.is(tok::tilde)) {
4127 // We found something that indicates the start of an unqualified-id.
4128 // Parse that unqualified-id.
John McCallba9d8532010-04-13 06:39:49 +00004129 bool AllowConstructorName;
4130 if (D.getDeclSpec().hasTypeSpecifier())
4131 AllowConstructorName = false;
4132 else if (D.getCXXScopeSpec().isSet())
4133 AllowConstructorName =
4134 (D.getContext() == Declarator::FileContext ||
4135 (D.getContext() == Declarator::MemberContext &&
4136 D.getDeclSpec().isFriendSpecified()));
4137 else
4138 AllowConstructorName = (D.getContext() == Declarator::MemberContext);
4139
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004140 SourceLocation TemplateKWLoc;
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004141 if (ParseUnqualifiedId(D.getCXXScopeSpec(),
4142 /*EnteringContext=*/true,
4143 /*AllowDestructorName=*/true,
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004144 AllowConstructorName,
John McCallb3d87482010-08-24 05:47:05 +00004145 ParsedType(),
Abramo Bagnarae4b92762012-01-27 09:46:47 +00004146 TemplateKWLoc,
Jeffrey Yasskin9ab14542010-04-08 16:38:48 +00004147 D.getName()) ||
4148 // Once we're past the identifier, if the scope was bad, mark the
4149 // whole declarator bad.
4150 D.getCXXScopeSpec().isInvalid()) {
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004151 D.SetIdentifier(0, Tok.getLocation());
4152 D.setInvalidType(true);
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004153 } else {
4154 // Parsed the unqualified-id; update range information and move along.
4155 if (D.getSourceRange().getBegin().isInvalid())
4156 D.SetRangeBegin(D.getName().getSourceRange().getBegin());
4157 D.SetRangeEnd(D.getName().getSourceRange().getEnd());
Douglas Gregor2f1bc522008-11-07 20:08:42 +00004158 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004159 goto PastIdentifier;
Douglas Gregor1cd1b1e2008-11-06 22:13:31 +00004160 }
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004161 } else if (Tok.is(tok::identifier) && D.mayHaveIdentifier()) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004162 assert(!getLangOpts().CPlusPlus &&
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004163 "There's a C++-specific check for tok::identifier above");
4164 assert(Tok.getIdentifierInfo() && "Not an identifier?");
4165 D.SetIdentifier(Tok.getIdentifierInfo(), Tok.getLocation());
4166 ConsumeToken();
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004167 goto PastIdentifier;
4168 }
Richard Smith9988f282012-03-29 01:16:42 +00004169
Douglas Gregor3f9a0562009-11-03 01:35:08 +00004170 if (Tok.is(tok::l_paren)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004171 // direct-declarator: '(' declarator ')'
4172 // direct-declarator: '(' attributes declarator ')'
4173 // Example: 'char (*X)' or 'int (*XX)(void)'
4174 ParseParenDeclarator(D);
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004175
4176 // If the declarator was parenthesized, we entered the declarator
4177 // scope when parsing the parenthesized declarator, then exited
4178 // the scope already. Re-enter the scope, if we need to.
4179 if (D.getCXXScopeSpec().isSet()) {
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004180 // If there was an error parsing parenthesized declarator, declarator
Richard Smith9988f282012-03-29 01:16:42 +00004181 // scope may have been entered before. Don't do it again.
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004182 if (!D.isInvalidType() &&
4183 Actions.ShouldEnterDeclaratorScope(getCurScope(), D.getCXXScopeSpec()))
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004184 // Change the declaration context for name lookup, until this function
4185 // is exited (and the declarator has been parsed).
Fariborz Jahanian46877cd2010-08-17 23:50:37 +00004186 DeclScopeObj.EnterDeclaratorScope();
Douglas Gregor0efc2c12010-01-13 17:31:36 +00004187 }
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004188 } else if (D.mayOmitIdentifier()) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004189 // This could be something simple like "int" (in which case the declarator
4190 // portion is empty), if an abstract-declarator is allowed.
4191 D.SetIdentifier(0, Tok.getLocation());
4192 } else {
Douglas Gregore950d4b2009-03-06 23:28:18 +00004193 if (D.getContext() == Declarator::MemberContext)
4194 Diag(Tok, diag::err_expected_member_name_or_semi)
4195 << D.getDeclSpec().getSourceRange();
David Blaikie4e4d0842012-03-11 07:00:24 +00004196 else if (getLangOpts().CPlusPlus)
4197 Diag(Tok, diag::err_expected_unqualified_id) << getLangOpts().CPlusPlus;
Argyrios Kyrtzidiseb83ecd2008-11-08 16:45:02 +00004198 else
Chris Lattner1ab3b962008-11-18 07:48:38 +00004199 Diag(Tok, diag::err_expected_ident_lparen);
Reid Spencer5f016e22007-07-11 17:01:13 +00004200 D.SetIdentifier(0, Tok.getLocation());
Chris Lattner1f6f54b2008-11-11 06:13:16 +00004201 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004202 }
Mike Stump1eb44332009-09-09 15:08:12 +00004203
Argyrios Kyrtzidis314fe782008-11-26 22:40:03 +00004204 PastIdentifier:
Reid Spencer5f016e22007-07-11 17:01:13 +00004205 assert(D.isPastIdentifier() &&
4206 "Haven't past the location of the identifier yet?");
Mike Stump1eb44332009-09-09 15:08:12 +00004207
Richard Smith6ee326a2012-04-10 01:32:12 +00004208 // Don't parse attributes unless we have parsed an unparenthesized name.
4209 if (D.hasName() && !D.getNumTypeObjects())
John McCall7f040a92010-12-24 02:08:15 +00004210 MaybeParseCXX0XAttributes(D);
Sean Huntbbd37c62009-11-21 08:43:09 +00004211
Reid Spencer5f016e22007-07-11 17:01:13 +00004212 while (1) {
Chris Lattner04d66662007-10-09 17:33:22 +00004213 if (Tok.is(tok::l_paren)) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004214 // Enter function-declaration scope, limiting any declarators to the
4215 // function prototype scope, including parameter declarators.
4216 ParseScope PrototypeScope(this,
4217 Scope::FunctionPrototypeScope|Scope::DeclScope);
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004218 // The paren may be part of a C++ direct initializer, eg. "int x(1);".
4219 // In such a case, check if we actually have a function declarator; if it
4220 // is not, the declarator has been fully parsed.
David Blaikie4e4d0842012-03-11 07:00:24 +00004221 if (getLangOpts().CPlusPlus && D.mayBeFollowedByCXXDirectInit()) {
Chris Lattner7399ee02008-10-20 02:05:46 +00004222 // When not in file scope, warn for ambiguous function declarators, just
4223 // in case the author intended it as a variable definition.
4224 bool warnIfAmbiguous = D.getContext() != Declarator::FileContext;
4225 if (!isCXXFunctionDeclarator(warnIfAmbiguous))
4226 break;
4227 }
John McCall0b7e6782011-03-24 11:26:52 +00004228 ParsedAttributes attrs(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004229 BalancedDelimiterTracker T(*this, tok::l_paren);
4230 T.consumeOpen();
4231 ParseFunctionDeclarator(D, attrs, T);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004232 PrototypeScope.Exit();
Chris Lattner04d66662007-10-09 17:33:22 +00004233 } else if (Tok.is(tok::l_square)) {
Reid Spencer5f016e22007-07-11 17:01:13 +00004234 ParseBracketDeclarator(D);
4235 } else {
4236 break;
4237 }
4238 }
David Blaikie42d6d0c2011-12-04 05:04:18 +00004239}
Reid Spencer5f016e22007-07-11 17:01:13 +00004240
Chris Lattneref4715c2008-04-06 05:45:57 +00004241/// ParseParenDeclarator - We parsed the declarator D up to a paren. This is
4242/// only called before the identifier, so these are most likely just grouping
Mike Stump1eb44332009-09-09 15:08:12 +00004243/// parens for precedence. If we find that these are actually function
Chris Lattneref4715c2008-04-06 05:45:57 +00004244/// parameter parens in an abstract-declarator, we call ParseFunctionDeclarator.
4245///
4246/// direct-declarator:
4247/// '(' declarator ')'
4248/// [GNU] '(' attributes declarator ')'
Chris Lattner7399ee02008-10-20 02:05:46 +00004249/// direct-declarator '(' parameter-type-list ')'
4250/// direct-declarator '(' identifier-list[opt] ')'
4251/// [GNU] direct-declarator '(' parameter-forward-declarations
4252/// parameter-type-list[opt] ')'
Chris Lattneref4715c2008-04-06 05:45:57 +00004253///
4254void Parser::ParseParenDeclarator(Declarator &D) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004255 BalancedDelimiterTracker T(*this, tok::l_paren);
4256 T.consumeOpen();
4257
Chris Lattneref4715c2008-04-06 05:45:57 +00004258 assert(!D.isPastIdentifier() && "Should be called before passing identifier");
Mike Stump1eb44332009-09-09 15:08:12 +00004259
Chris Lattner7399ee02008-10-20 02:05:46 +00004260 // Eat any attributes before we look at whether this is a grouping or function
4261 // declarator paren. If this is a grouping paren, the attribute applies to
4262 // the type being built up, for example:
4263 // int (__attribute__(()) *x)(long y)
4264 // If this ends up not being a grouping paren, the attribute applies to the
4265 // first argument, for example:
4266 // int (__attribute__(()) int x)
4267 // In either case, we need to eat any attributes to be able to determine what
4268 // sort of paren this is.
4269 //
John McCall0b7e6782011-03-24 11:26:52 +00004270 ParsedAttributes attrs(AttrFactory);
Chris Lattner7399ee02008-10-20 02:05:46 +00004271 bool RequiresArg = false;
4272 if (Tok.is(tok::kw___attribute)) {
John McCall7f040a92010-12-24 02:08:15 +00004273 ParseGNUAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004274
Chris Lattner7399ee02008-10-20 02:05:46 +00004275 // We require that the argument list (if this is a non-grouping paren) be
4276 // present even if the attribute list was empty.
4277 RequiresArg = true;
4278 }
Steve Naroff239f0732008-12-25 14:16:32 +00004279 // Eat any Microsoft extensions.
Eli Friedman290eeb02009-06-08 23:27:34 +00004280 if (Tok.is(tok::kw___cdecl) || Tok.is(tok::kw___stdcall) ||
Douglas Gregorf813a2c2010-05-18 16:57:00 +00004281 Tok.is(tok::kw___thiscall) || Tok.is(tok::kw___fastcall) ||
Francois Pichet3bd9aa42011-08-18 09:59:55 +00004282 Tok.is(tok::kw___w64) || Tok.is(tok::kw___ptr64) ||
Francois Pichet58fd97a2011-08-25 00:36:46 +00004283 Tok.is(tok::kw___ptr32) || Tok.is(tok::kw___unaligned)) {
John McCall7f040a92010-12-24 02:08:15 +00004284 ParseMicrosoftTypeAttributes(attrs);
Eli Friedman290eeb02009-06-08 23:27:34 +00004285 }
Dawn Perchik52fc3142010-09-03 01:29:35 +00004286 // Eat any Borland extensions.
Ted Kremenek8113ecf2010-11-10 05:59:39 +00004287 if (Tok.is(tok::kw___pascal))
John McCall7f040a92010-12-24 02:08:15 +00004288 ParseBorlandTypeAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004289
Chris Lattneref4715c2008-04-06 05:45:57 +00004290 // If we haven't past the identifier yet (or where the identifier would be
4291 // stored, if this is an abstract declarator), then this is probably just
4292 // grouping parens. However, if this could be an abstract-declarator, then
4293 // this could also be the start of function arguments (consider 'void()').
4294 bool isGrouping;
Mike Stump1eb44332009-09-09 15:08:12 +00004295
Chris Lattneref4715c2008-04-06 05:45:57 +00004296 if (!D.mayOmitIdentifier()) {
4297 // If this can't be an abstract-declarator, this *must* be a grouping
4298 // paren, because we haven't seen the identifier yet.
4299 isGrouping = true;
4300 } else if (Tok.is(tok::r_paren) || // 'int()' is a function.
Richard Smith22592862012-03-27 23:05:05 +00004301 (getLangOpts().CPlusPlus && Tok.is(tok::ellipsis) &&
4302 NextToken().is(tok::r_paren)) || // C++ int(...)
Richard Smith6ce48a72012-04-11 04:01:28 +00004303 isDeclarationSpecifier() || // 'int(int)' is a function.
4304 isCXX11AttributeSpecifier()) { // 'int([[]]int)' is a function.
Chris Lattneref4715c2008-04-06 05:45:57 +00004305 // This handles C99 6.7.5.3p11: in "typedef int X; void foo(X)", X is
4306 // considered to be a type, not a K&R identifier-list.
4307 isGrouping = false;
4308 } else {
4309 // Otherwise, this is a grouping paren, e.g. 'int (*X)' or 'int(X)'.
4310 isGrouping = true;
4311 }
Mike Stump1eb44332009-09-09 15:08:12 +00004312
Chris Lattneref4715c2008-04-06 05:45:57 +00004313 // If this is a grouping paren, handle:
4314 // direct-declarator: '(' declarator ')'
4315 // direct-declarator: '(' attributes declarator ')'
4316 if (isGrouping) {
Richard Smith9988f282012-03-29 01:16:42 +00004317 SourceLocation EllipsisLoc = D.getEllipsisLoc();
4318 D.setEllipsisLoc(SourceLocation());
4319
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004320 bool hadGroupingParens = D.hasGroupingParens();
Argyrios Kyrtzidis73a0d882008-10-06 17:10:33 +00004321 D.setGroupingParens(true);
Sebastian Redl4c5d3202008-11-21 19:14:01 +00004322 ParseDeclaratorInternal(D, &Parser::ParseDirectDeclarator);
Chris Lattneref4715c2008-04-06 05:45:57 +00004323 // Match the ')'.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004324 T.consumeClose();
4325 D.AddTypeInfo(DeclaratorChunk::getParen(T.getOpenLocation(),
4326 T.getCloseLocation()),
4327 attrs, T.getCloseLocation());
Argyrios Kyrtzidis3f2a8a02008-10-07 10:21:57 +00004328
4329 D.setGroupingParens(hadGroupingParens);
Richard Smith9988f282012-03-29 01:16:42 +00004330
4331 // An ellipsis cannot be placed outside parentheses.
4332 if (EllipsisLoc.isValid())
4333 diagnoseMisplacedEllipsis(*this, D, EllipsisLoc);
4334
Chris Lattneref4715c2008-04-06 05:45:57 +00004335 return;
4336 }
Mike Stump1eb44332009-09-09 15:08:12 +00004337
Chris Lattneref4715c2008-04-06 05:45:57 +00004338 // Okay, if this wasn't a grouping paren, it must be the start of a function
4339 // argument list. Recognize that this declarator will never have an
Chris Lattner7399ee02008-10-20 02:05:46 +00004340 // identifier (and remember where it would have been), then call into
4341 // ParseFunctionDeclarator to handle of argument list.
Chris Lattneref4715c2008-04-06 05:45:57 +00004342 D.SetIdentifier(0, Tok.getLocation());
4343
David Blaikie42d6d0c2011-12-04 05:04:18 +00004344 // Enter function-declaration scope, limiting any declarators to the
4345 // function prototype scope, including parameter declarators.
4346 ParseScope PrototypeScope(this,
4347 Scope::FunctionPrototypeScope|Scope::DeclScope);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004348 ParseFunctionDeclarator(D, attrs, T, RequiresArg);
David Blaikie42d6d0c2011-12-04 05:04:18 +00004349 PrototypeScope.Exit();
Chris Lattneref4715c2008-04-06 05:45:57 +00004350}
4351
4352/// ParseFunctionDeclarator - We are after the identifier and have parsed the
4353/// declarator D up to a paren, which indicates that we are parsing function
4354/// arguments.
Reid Spencer5f016e22007-07-11 17:01:13 +00004355///
Richard Smith6ee326a2012-04-10 01:32:12 +00004356/// If FirstArgAttrs is non-null, then the caller parsed those arguments
4357/// immediately after the open paren - they should be considered to be the
4358/// first argument of a parameter.
Chris Lattner7399ee02008-10-20 02:05:46 +00004359///
Richard Smith6ee326a2012-04-10 01:32:12 +00004360/// If RequiresArg is true, then the first argument of the function is required
4361/// to be present and required to not be an identifier list.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004362///
Richard Smith6ee326a2012-04-10 01:32:12 +00004363/// For C++, after the parameter-list, it also parses the cv-qualifier-seq[opt],
4364/// (C++11) ref-qualifier[opt], exception-specification[opt],
4365/// (C++11) attribute-specifier-seq[opt], and (C++11) trailing-return-type[opt].
4366///
4367/// [C++11] exception-specification:
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004368/// dynamic-exception-specification
4369/// noexcept-specification
4370///
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004371void Parser::ParseFunctionDeclarator(Declarator &D,
Richard Smith6ee326a2012-04-10 01:32:12 +00004372 ParsedAttributes &FirstArgAttrs,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004373 BalancedDelimiterTracker &Tracker,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004374 bool RequiresArg) {
David Blaikie42d6d0c2011-12-04 05:04:18 +00004375 assert(getCurScope()->isFunctionPrototypeScope() &&
4376 "Should call from a Function scope");
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004377 // lparen is already consumed!
4378 assert(D.isPastIdentifier() && "Should not call before identifier!");
4379
4380 // This should be true when the function has typed arguments.
4381 // Otherwise, it is treated as a K&R-style function.
4382 bool HasProto = false;
4383 // Build up an array of information about the parsed arguments.
Chris Lattner5f9e2722011-07-23 10:55:15 +00004384 SmallVector<DeclaratorChunk::ParamInfo, 16> ParamInfo;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004385 // Remember where we see an ellipsis, if any.
4386 SourceLocation EllipsisLoc;
4387
4388 DeclSpec DS(AttrFactory);
4389 bool RefQualifierIsLValueRef = true;
4390 SourceLocation RefQualifierLoc;
Douglas Gregor43f51032011-10-19 06:04:55 +00004391 SourceLocation ConstQualifierLoc;
4392 SourceLocation VolatileQualifierLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004393 ExceptionSpecificationType ESpecType = EST_None;
4394 SourceRange ESpecRange;
Chris Lattner5f9e2722011-07-23 10:55:15 +00004395 SmallVector<ParsedType, 2> DynamicExceptions;
4396 SmallVector<SourceRange, 2> DynamicExceptionRanges;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004397 ExprResult NoexceptExpr;
Richard Smith6ee326a2012-04-10 01:32:12 +00004398 ParsedAttributes FnAttrs(AttrFactory);
Richard Smith54655be2012-06-12 01:51:59 +00004399 TypeResult TrailingReturnType;
Richard Smith6ee326a2012-04-10 01:32:12 +00004400
James Molloy16f1f712012-02-29 10:24:19 +00004401 Actions.ActOnStartFunctionDeclarator();
4402
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004403 SourceLocation EndLoc;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004404 if (isFunctionDeclaratorIdentifierList()) {
4405 if (RequiresArg)
4406 Diag(Tok, diag::err_argument_required_after_attribute);
4407
4408 ParseFunctionDeclaratorIdentifierList(D, ParamInfo);
4409
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004410 Tracker.consumeClose();
4411 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004412 } else {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004413 if (Tok.isNot(tok::r_paren))
Richard Smith6ee326a2012-04-10 01:32:12 +00004414 ParseParameterDeclarationClause(D, FirstArgAttrs, ParamInfo, EllipsisLoc);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004415 else if (RequiresArg)
4416 Diag(Tok, diag::err_argument_required_after_attribute);
4417
David Blaikie4e4d0842012-03-11 07:00:24 +00004418 HasProto = ParamInfo.size() || getLangOpts().CPlusPlus;
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004419
4420 // If we have the closing ')', eat it.
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004421 Tracker.consumeClose();
4422 EndLoc = Tracker.getCloseLocation();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004423
David Blaikie4e4d0842012-03-11 07:00:24 +00004424 if (getLangOpts().CPlusPlus) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004425 // FIXME: Accept these components in any order, and produce fixits to
4426 // correct the order if the user gets it wrong. Ideally we should deal
4427 // with the virt-specifier-seq and pure-specifier in the same way.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004428
4429 // Parse cv-qualifier-seq[opt].
Richard Smith6ee326a2012-04-10 01:32:12 +00004430 ParseTypeQualifierListOpt(DS, false /*no attributes*/, false);
4431 if (!DS.getSourceRange().getEnd().isInvalid()) {
4432 EndLoc = DS.getSourceRange().getEnd();
4433 ConstQualifierLoc = DS.getConstSpecLoc();
4434 VolatileQualifierLoc = DS.getVolatileSpecLoc();
4435 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004436
4437 // Parse ref-qualifier[opt].
4438 if (Tok.is(tok::amp) || Tok.is(tok::ampamp)) {
David Blaikie4e4d0842012-03-11 07:00:24 +00004439 Diag(Tok, getLangOpts().CPlusPlus0x ?
Richard Smith7fe62082011-10-15 05:09:34 +00004440 diag::warn_cxx98_compat_ref_qualifier :
4441 diag::ext_ref_qualifier);
Richard Smith6ee326a2012-04-10 01:32:12 +00004442
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004443 RefQualifierIsLValueRef = Tok.is(tok::amp);
4444 RefQualifierLoc = ConsumeToken();
4445 EndLoc = RefQualifierLoc;
4446 }
4447
Douglas Gregorcefc3af2012-04-16 07:05:22 +00004448 // C++11 [expr.prim.general]p3:
4449 // If a declaration declares a member function or member function
4450 // template of a class X, the expression this is a prvalue of type
4451 // "pointer to cv-qualifier-seq X" between the optional cv-qualifer-seq
4452 // and the end of the function-definition, member-declarator, or
4453 // declarator.
4454 bool IsCXX11MemberFunction =
4455 getLangOpts().CPlusPlus0x &&
4456 (D.getContext() == Declarator::MemberContext ||
4457 (D.getContext() == Declarator::FileContext &&
4458 D.getCXXScopeSpec().isValid() &&
4459 Actions.CurContext->isRecord()));
4460 Sema::CXXThisScopeRAII ThisScope(Actions,
4461 dyn_cast<CXXRecordDecl>(Actions.CurContext),
4462 DS.getTypeQualifiers(),
4463 IsCXX11MemberFunction);
Richard Smitha058fd42012-05-02 22:22:32 +00004464
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004465 // Parse exception-specification[opt].
Richard Smitha058fd42012-05-02 22:22:32 +00004466 ESpecType = tryParseExceptionSpecification(ESpecRange,
Douglas Gregor74e2fc32012-04-16 18:27:27 +00004467 DynamicExceptions,
4468 DynamicExceptionRanges,
Richard Smitha058fd42012-05-02 22:22:32 +00004469 NoexceptExpr);
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004470 if (ESpecType != EST_None)
4471 EndLoc = ESpecRange.getEnd();
4472
Richard Smith6ee326a2012-04-10 01:32:12 +00004473 // Parse attribute-specifier-seq[opt]. Per DR 979 and DR 1297, this goes
4474 // after the exception-specification.
4475 MaybeParseCXX0XAttributes(FnAttrs);
4476
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004477 // Parse trailing-return-type[opt].
David Blaikie4e4d0842012-03-11 07:00:24 +00004478 if (getLangOpts().CPlusPlus0x && Tok.is(tok::arrow)) {
Richard Smith7fe62082011-10-15 05:09:34 +00004479 Diag(Tok, diag::warn_cxx98_compat_trailing_return_type);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004480 SourceRange Range;
Richard Smith54655be2012-06-12 01:51:59 +00004481 TrailingReturnType = ParseTrailingReturnType(Range);
Douglas Gregorae7902c2011-08-04 15:30:47 +00004482 if (Range.getEnd().isValid())
4483 EndLoc = Range.getEnd();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004484 }
4485 }
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004486 }
4487
4488 // Remember that we parsed a function type, and remember the attributes.
4489 D.AddTypeInfo(DeclaratorChunk::getFunction(HasProto,
4490 /*isVariadic=*/EllipsisLoc.isValid(),
4491 EllipsisLoc,
4492 ParamInfo.data(), ParamInfo.size(),
4493 DS.getTypeQualifiers(),
4494 RefQualifierIsLValueRef,
Douglas Gregor43f51032011-10-19 06:04:55 +00004495 RefQualifierLoc, ConstQualifierLoc,
4496 VolatileQualifierLoc,
Douglas Gregor90ebed02011-07-13 21:47:47 +00004497 /*MutableLoc=*/SourceLocation(),
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004498 ESpecType, ESpecRange.getBegin(),
4499 DynamicExceptions.data(),
4500 DynamicExceptionRanges.data(),
4501 DynamicExceptions.size(),
4502 NoexceptExpr.isUsable() ?
4503 NoexceptExpr.get() : 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004504 Tracker.getOpenLocation(),
4505 EndLoc, D,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004506 TrailingReturnType),
Richard Smith6ee326a2012-04-10 01:32:12 +00004507 FnAttrs, EndLoc);
James Molloy16f1f712012-02-29 10:24:19 +00004508
4509 Actions.ActOnEndFunctionDeclarator();
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004510}
4511
4512/// isFunctionDeclaratorIdentifierList - This parameter list may have an
4513/// identifier list form for a K&R-style function: void foo(a,b,c)
4514///
4515/// Note that identifier-lists are only allowed for normal declarators, not for
4516/// abstract-declarators.
4517bool Parser::isFunctionDeclaratorIdentifierList() {
David Blaikie4e4d0842012-03-11 07:00:24 +00004518 return !getLangOpts().CPlusPlus
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004519 && Tok.is(tok::identifier)
4520 && !TryAltiVecVectorToken()
4521 // K&R identifier lists can't have typedefs as identifiers, per C99
4522 // 6.7.5.3p11.
4523 && (TryAnnotateTypeOrScopeToken() || !Tok.is(tok::annot_typename))
4524 // Identifier lists follow a really simple grammar: the identifiers can
4525 // be followed *only* by a ", identifier" or ")". However, K&R
4526 // identifier lists are really rare in the brave new modern world, and
4527 // it is very common for someone to typo a type in a non-K&R style
4528 // list. If we are presented with something like: "void foo(intptr x,
4529 // float y)", we don't want to start parsing the function declarator as
4530 // though it is a K&R style declarator just because intptr is an
4531 // invalid type.
4532 //
4533 // To handle this, we check to see if the token after the first
4534 // identifier is a "," or ")". Only then do we parse it as an
4535 // identifier list.
4536 && (NextToken().is(tok::comma) || NextToken().is(tok::r_paren));
4537}
4538
4539/// ParseFunctionDeclaratorIdentifierList - While parsing a function declarator
4540/// we found a K&R-style identifier list instead of a typed parameter list.
4541///
4542/// After returning, ParamInfo will hold the parsed parameters.
4543///
4544/// identifier-list: [C99 6.7.5]
4545/// identifier
4546/// identifier-list ',' identifier
4547///
4548void Parser::ParseFunctionDeclaratorIdentifierList(
4549 Declarator &D,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004550 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo) {
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004551 // If there was no identifier specified for the declarator, either we are in
4552 // an abstract-declarator, or we are in a parameter declarator which was found
4553 // to be abstract. In abstract-declarators, identifier lists are not valid:
4554 // diagnose this.
4555 if (!D.getIdentifier())
4556 Diag(Tok, diag::ext_ident_list_in_param);
4557
4558 // Maintain an efficient lookup of params we have seen so far.
4559 llvm::SmallSet<const IdentifierInfo*, 16> ParamsSoFar;
4560
4561 while (1) {
4562 // If this isn't an identifier, report the error and skip until ')'.
4563 if (Tok.isNot(tok::identifier)) {
4564 Diag(Tok, diag::err_expected_ident);
4565 SkipUntil(tok::r_paren, /*StopAtSemi=*/true, /*DontConsume=*/true);
4566 // Forget we parsed anything.
4567 ParamInfo.clear();
4568 return;
4569 }
4570
4571 IdentifierInfo *ParmII = Tok.getIdentifierInfo();
4572
4573 // Reject 'typedef int y; int test(x, y)', but continue parsing.
4574 if (Actions.getTypeName(*ParmII, Tok.getLocation(), getCurScope()))
4575 Diag(Tok, diag::err_unexpected_typedef_ident) << ParmII;
4576
4577 // Verify that the argument identifier has not already been mentioned.
4578 if (!ParamsSoFar.insert(ParmII)) {
4579 Diag(Tok, diag::err_param_redefinition) << ParmII;
4580 } else {
4581 // Remember this identifier in ParamInfo.
4582 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4583 Tok.getLocation(),
4584 0));
4585 }
4586
4587 // Eat the identifier.
4588 ConsumeToken();
4589
4590 // The list continues if we see a comma.
4591 if (Tok.isNot(tok::comma))
4592 break;
4593 ConsumeToken();
4594 }
4595}
4596
4597/// ParseParameterDeclarationClause - Parse a (possibly empty) parameter-list
4598/// after the opening parenthesis. This function will not parse a K&R-style
4599/// identifier list.
4600///
Richard Smith6ce48a72012-04-11 04:01:28 +00004601/// D is the declarator being parsed. If FirstArgAttrs is non-null, then the
4602/// caller parsed those arguments immediately after the open paren - they should
4603/// be considered to be part of the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004604///
4605/// After returning, ParamInfo will hold the parsed parameters. EllipsisLoc will
4606/// be the location of the ellipsis, if any was parsed.
4607///
Reid Spencer5f016e22007-07-11 17:01:13 +00004608/// parameter-type-list: [C99 6.7.5]
4609/// parameter-list
4610/// parameter-list ',' '...'
Douglas Gregored5d6512009-09-22 21:41:40 +00004611/// [C++] parameter-list '...'
Reid Spencer5f016e22007-07-11 17:01:13 +00004612///
4613/// parameter-list: [C99 6.7.5]
4614/// parameter-declaration
4615/// parameter-list ',' parameter-declaration
4616///
4617/// parameter-declaration: [C99 6.7.5]
4618/// declaration-specifiers declarator
Chris Lattner04421082008-04-08 04:40:51 +00004619/// [C++] declaration-specifiers declarator '=' assignment-expression
Sebastian Redl84407ba2012-03-14 15:54:00 +00004620/// [C++11] initializer-clause
Reid Spencer5f016e22007-07-11 17:01:13 +00004621/// [GNU] declaration-specifiers declarator attributes
Sebastian Redl50de12f2009-03-24 22:27:57 +00004622/// declaration-specifiers abstract-declarator[opt]
4623/// [C++] declaration-specifiers abstract-declarator[opt]
Chris Lattner8123a952008-04-10 02:22:51 +00004624/// '=' assignment-expression
Reid Spencer5f016e22007-07-11 17:01:13 +00004625/// [GNU] declaration-specifiers abstract-declarator[opt] attributes
Richard Smith6ce48a72012-04-11 04:01:28 +00004626/// [C++11] attribute-specifier-seq parameter-declaration
Reid Spencer5f016e22007-07-11 17:01:13 +00004627///
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004628void Parser::ParseParameterDeclarationClause(
4629 Declarator &D,
Richard Smith6ce48a72012-04-11 04:01:28 +00004630 ParsedAttributes &FirstArgAttrs,
Chris Lattner5f9e2722011-07-23 10:55:15 +00004631 SmallVector<DeclaratorChunk::ParamInfo, 16> &ParamInfo,
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004632 SourceLocation &EllipsisLoc) {
Mike Stump1eb44332009-09-09 15:08:12 +00004633
Chris Lattnerf97409f2008-04-06 06:57:35 +00004634 while (1) {
4635 if (Tok.is(tok::ellipsis)) {
Richard Smith6ce48a72012-04-11 04:01:28 +00004636 // FIXME: Issue a diagnostic if we parsed an attribute-specifier-seq
4637 // before deciding this was a parameter-declaration-clause.
Douglas Gregor965acbb2009-02-18 07:07:28 +00004638 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
Chris Lattnerf97409f2008-04-06 06:57:35 +00004639 break;
Reid Spencer5f016e22007-07-11 17:01:13 +00004640 }
Mike Stump1eb44332009-09-09 15:08:12 +00004641
Chris Lattnerf97409f2008-04-06 06:57:35 +00004642 // Parse the declaration-specifiers.
John McCall54abf7d2009-11-04 02:18:39 +00004643 // Just use the ParsingDeclaration "scope" of the declarator.
John McCall0b7e6782011-03-24 11:26:52 +00004644 DeclSpec DS(AttrFactory);
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004645
Richard Smith6ce48a72012-04-11 04:01:28 +00004646 // Parse any C++11 attributes.
4647 MaybeParseCXX0XAttributes(DS.getAttributes());
4648
John McCall7f040a92010-12-24 02:08:15 +00004649 // Skip any Microsoft attributes before a param.
David Blaikie4e4d0842012-03-11 07:00:24 +00004650 if (getLangOpts().MicrosoftExt && Tok.is(tok::l_square))
John McCall7f040a92010-12-24 02:08:15 +00004651 ParseMicrosoftAttributes(DS.getAttributes());
4652
4653 SourceLocation DSStart = Tok.getLocation();
Chris Lattner7399ee02008-10-20 02:05:46 +00004654
4655 // If the caller parsed attributes for the first argument, add them now.
John McCall7f040a92010-12-24 02:08:15 +00004656 // Take them so that we only apply the attributes to the first parameter.
Douglas Gregor3fd1ba02011-07-05 16:44:18 +00004657 // FIXME: If we can leave the attributes in the token stream somehow, we can
Richard Smith6ce48a72012-04-11 04:01:28 +00004658 // get rid of a parameter (FirstArgAttrs) and this statement. It might be
4659 // too much hassle.
4660 DS.takeAttributesFrom(FirstArgAttrs);
John McCall7f040a92010-12-24 02:08:15 +00004661
Chris Lattnere64c5492009-02-27 18:38:20 +00004662 ParseDeclarationSpecifiers(DS);
Mike Stump1eb44332009-09-09 15:08:12 +00004663
Chris Lattnerf97409f2008-04-06 06:57:35 +00004664 // Parse the declarator. This is "PrototypeContext", because we must
4665 // accept either 'declarator' or 'abstract-declarator' here.
4666 Declarator ParmDecl(DS, Declarator::PrototypeContext);
4667 ParseDeclarator(ParmDecl);
4668
4669 // Parse GNU attributes, if present.
John McCall7f040a92010-12-24 02:08:15 +00004670 MaybeParseGNUAttributes(ParmDecl);
Mike Stump1eb44332009-09-09 15:08:12 +00004671
Chris Lattnerf97409f2008-04-06 06:57:35 +00004672 // Remember this parsed parameter in ParamInfo.
4673 IdentifierInfo *ParmII = ParmDecl.getIdentifier();
Mike Stump1eb44332009-09-09 15:08:12 +00004674
Douglas Gregor72b505b2008-12-16 21:30:33 +00004675 // DefArgToks is used when the parsing of default arguments needs
4676 // to be delayed.
4677 CachedTokens *DefArgToks = 0;
4678
Chris Lattnerf97409f2008-04-06 06:57:35 +00004679 // If no parameter was specified, verify that *something* was specified,
4680 // otherwise we have a missing type and identifier.
Chris Lattnere64c5492009-02-27 18:38:20 +00004681 if (DS.isEmpty() && ParmDecl.getIdentifier() == 0 &&
4682 ParmDecl.getNumTypeObjects() == 0) {
Chris Lattnerf97409f2008-04-06 06:57:35 +00004683 // Completely missing, emit error.
4684 Diag(DSStart, diag::err_missing_param);
4685 } else {
4686 // Otherwise, we have something. Add it and let semantic analysis try
4687 // to grok it and add the result to the ParamInfo we are building.
Mike Stump1eb44332009-09-09 15:08:12 +00004688
Chris Lattnerf97409f2008-04-06 06:57:35 +00004689 // Inform the actions module about the parameter declarator, so it gets
4690 // added to the current scope.
John McCalld226f652010-08-21 09:40:31 +00004691 Decl *Param = Actions.ActOnParamDeclarator(getCurScope(), ParmDecl);
Chris Lattner04421082008-04-08 04:40:51 +00004692
4693 // Parse the default argument, if any. We parse the default
4694 // arguments in all dialects; the semantic analysis in
4695 // ActOnParamDefaultArgument will reject the default argument in
4696 // C.
4697 if (Tok.is(tok::equal)) {
Douglas Gregor61366e92008-12-24 00:01:03 +00004698 SourceLocation EqualLoc = Tok.getLocation();
4699
Chris Lattner04421082008-04-08 04:40:51 +00004700 // Parse the default argument
Douglas Gregor72b505b2008-12-16 21:30:33 +00004701 if (D.getContext() == Declarator::MemberContext) {
4702 // If we're inside a class definition, cache the tokens
4703 // corresponding to the default argument. We'll actually parse
4704 // them when we see the end of the class definition.
Douglas Gregor72b505b2008-12-16 21:30:33 +00004705 // FIXME: Can we use a smart pointer for Toks?
4706 DefArgToks = new CachedTokens;
4707
Mike Stump1eb44332009-09-09 15:08:12 +00004708 if (!ConsumeAndStoreUntil(tok::comma, tok::r_paren, *DefArgToks,
Argyrios Kyrtzidis14b91622010-04-23 21:20:12 +00004709 /*StopAtSemi=*/true,
4710 /*ConsumeFinalToken=*/false)) {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004711 delete DefArgToks;
4712 DefArgToks = 0;
Douglas Gregor61366e92008-12-24 00:01:03 +00004713 Actions.ActOnParamDefaultArgumentError(Param);
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004714 } else {
4715 // Mark the end of the default argument so that we know when to
4716 // stop when we parse it later on.
4717 Token DefArgEnd;
4718 DefArgEnd.startToken();
4719 DefArgEnd.setKind(tok::cxx_defaultarg_end);
4720 DefArgEnd.setLocation(Tok.getLocation());
4721 DefArgToks->push_back(DefArgEnd);
Mike Stump1eb44332009-09-09 15:08:12 +00004722 Actions.ActOnParamUnparsedDefaultArgument(Param, EqualLoc,
Anders Carlsson5e300d12009-06-12 16:51:40 +00004723 (*DefArgToks)[1].getLocation());
Argyrios Kyrtzidis2b602ad2010-08-06 09:47:24 +00004724 }
Chris Lattner04421082008-04-08 04:40:51 +00004725 } else {
Douglas Gregor72b505b2008-12-16 21:30:33 +00004726 // Consume the '='.
Douglas Gregor61366e92008-12-24 00:01:03 +00004727 ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004728
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004729 // The argument isn't actually potentially evaluated unless it is
4730 // used.
4731 EnterExpressionEvaluationContext Eval(Actions,
Douglas Gregorccc1b5e2012-02-21 00:37:24 +00004732 Sema::PotentiallyEvaluatedIfUsed,
4733 Param);
Douglas Gregorbe0f7bd2010-09-11 20:24:53 +00004734
Sebastian Redl84407ba2012-03-14 15:54:00 +00004735 ExprResult DefArgResult;
Sebastian Redl3e280b52012-03-18 22:25:45 +00004736 if (getLangOpts().CPlusPlus0x && Tok.is(tok::l_brace)) {
4737 Diag(Tok, diag::warn_cxx98_compat_generalized_initializer_lists);
Sebastian Redl84407ba2012-03-14 15:54:00 +00004738 DefArgResult = ParseBraceInitializer();
Sebastian Redl3e280b52012-03-18 22:25:45 +00004739 } else
Sebastian Redl84407ba2012-03-14 15:54:00 +00004740 DefArgResult = ParseAssignmentExpression();
Douglas Gregor72b505b2008-12-16 21:30:33 +00004741 if (DefArgResult.isInvalid()) {
4742 Actions.ActOnParamDefaultArgumentError(Param);
4743 SkipUntil(tok::comma, tok::r_paren, true, true);
4744 } else {
4745 // Inform the actions module about the default argument
4746 Actions.ActOnParamDefaultArgument(Param, EqualLoc,
John McCall9ae2f072010-08-23 23:25:46 +00004747 DefArgResult.take());
Douglas Gregor72b505b2008-12-16 21:30:33 +00004748 }
Chris Lattner04421082008-04-08 04:40:51 +00004749 }
4750 }
Mike Stump1eb44332009-09-09 15:08:12 +00004751
4752 ParamInfo.push_back(DeclaratorChunk::ParamInfo(ParmII,
4753 ParmDecl.getIdentifierLoc(), Param,
Douglas Gregor72b505b2008-12-16 21:30:33 +00004754 DefArgToks));
Chris Lattnerf97409f2008-04-06 06:57:35 +00004755 }
4756
4757 // If the next token is a comma, consume it and keep reading arguments.
Douglas Gregored5d6512009-09-22 21:41:40 +00004758 if (Tok.isNot(tok::comma)) {
4759 if (Tok.is(tok::ellipsis)) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004760 EllipsisLoc = ConsumeToken(); // Consume the ellipsis.
4761
David Blaikie4e4d0842012-03-11 07:00:24 +00004762 if (!getLangOpts().CPlusPlus) {
Douglas Gregored5d6512009-09-22 21:41:40 +00004763 // We have ellipsis without a preceding ',', which is ill-formed
4764 // in C. Complain and provide the fix.
4765 Diag(EllipsisLoc, diag::err_missing_comma_before_ellipsis)
Douglas Gregor849b2432010-03-31 17:46:05 +00004766 << FixItHint::CreateInsertion(EllipsisLoc, ", ");
Douglas Gregored5d6512009-09-22 21:41:40 +00004767 }
4768 }
4769
4770 break;
4771 }
Mike Stump1eb44332009-09-09 15:08:12 +00004772
Chris Lattnerf97409f2008-04-06 06:57:35 +00004773 // Consume the comma.
4774 ConsumeToken();
Reid Spencer5f016e22007-07-11 17:01:13 +00004775 }
Mike Stump1eb44332009-09-09 15:08:12 +00004776
Chris Lattner66d28652008-04-06 06:34:08 +00004777}
Chris Lattneref4715c2008-04-06 05:45:57 +00004778
Reid Spencer5f016e22007-07-11 17:01:13 +00004779/// [C90] direct-declarator '[' constant-expression[opt] ']'
4780/// [C99] direct-declarator '[' type-qual-list[opt] assignment-expr[opt] ']'
4781/// [C99] direct-declarator '[' 'static' type-qual-list[opt] assign-expr ']'
4782/// [C99] direct-declarator '[' type-qual-list 'static' assignment-expr ']'
4783/// [C99] direct-declarator '[' type-qual-list[opt] '*' ']'
Richard Smith6ee326a2012-04-10 01:32:12 +00004784/// [C++11] direct-declarator '[' constant-expression[opt] ']'
4785/// attribute-specifier-seq[opt]
Reid Spencer5f016e22007-07-11 17:01:13 +00004786void Parser::ParseBracketDeclarator(Declarator &D) {
Richard Smith6ee326a2012-04-10 01:32:12 +00004787 if (CheckProhibitedCXX11Attribute())
4788 return;
4789
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004790 BalancedDelimiterTracker T(*this, tok::l_square);
4791 T.consumeOpen();
Mike Stump1eb44332009-09-09 15:08:12 +00004792
Chris Lattner378c7e42008-12-18 07:27:21 +00004793 // C array syntax has many features, but by-far the most common is [] and [4].
4794 // This code does a fast path to handle some of the most obvious cases.
4795 if (Tok.getKind() == tok::r_square) {
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004796 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004797 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004798 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004799
Chris Lattner378c7e42008-12-18 07:27:21 +00004800 // Remember that we parsed the empty array type.
John McCall60d7b3a2010-08-24 06:29:42 +00004801 ExprResult NumElements;
John McCall0b7e6782011-03-24 11:26:52 +00004802 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, false, 0,
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004803 T.getOpenLocation(),
4804 T.getCloseLocation()),
4805 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004806 return;
4807 } else if (Tok.getKind() == tok::numeric_constant &&
4808 GetLookAheadToken(1).is(tok::r_square)) {
4809 // [4] is very common. Parse the numeric constant expression.
Richard Smith36f5cfe2012-03-09 08:00:36 +00004810 ExprResult ExprRes(Actions.ActOnNumericConstant(Tok, getCurScope()));
Chris Lattner378c7e42008-12-18 07:27:21 +00004811 ConsumeToken();
4812
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004813 T.consumeClose();
John McCall0b7e6782011-03-24 11:26:52 +00004814 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004815 MaybeParseCXX0XAttributes(attrs);
Mike Stump1eb44332009-09-09 15:08:12 +00004816
Chris Lattner378c7e42008-12-18 07:27:21 +00004817 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004818 D.AddTypeInfo(DeclaratorChunk::getArray(0, false, 0,
John McCall7f040a92010-12-24 02:08:15 +00004819 ExprRes.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004820 T.getOpenLocation(),
4821 T.getCloseLocation()),
4822 attrs, T.getCloseLocation());
Chris Lattner378c7e42008-12-18 07:27:21 +00004823 return;
4824 }
Mike Stump1eb44332009-09-09 15:08:12 +00004825
Reid Spencer5f016e22007-07-11 17:01:13 +00004826 // If valid, this location is the position where we read the 'static' keyword.
4827 SourceLocation StaticLoc;
Chris Lattner04d66662007-10-09 17:33:22 +00004828 if (Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004829 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004830
Reid Spencer5f016e22007-07-11 17:01:13 +00004831 // If there is a type-qualifier-list, read it now.
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004832 // Type qualifiers in an array subscript are a C99 feature.
John McCall0b7e6782011-03-24 11:26:52 +00004833 DeclSpec DS(AttrFactory);
Chris Lattner5a69d1c2008-12-18 07:02:59 +00004834 ParseTypeQualifierListOpt(DS, false /*no attributes*/);
Mike Stump1eb44332009-09-09 15:08:12 +00004835
Reid Spencer5f016e22007-07-11 17:01:13 +00004836 // If we haven't already read 'static', check to see if there is one after the
4837 // type-qualifier-list.
Chris Lattner04d66662007-10-09 17:33:22 +00004838 if (!StaticLoc.isValid() && Tok.is(tok::kw_static))
Reid Spencer5f016e22007-07-11 17:01:13 +00004839 StaticLoc = ConsumeToken();
Mike Stump1eb44332009-09-09 15:08:12 +00004840
Reid Spencer5f016e22007-07-11 17:01:13 +00004841 // Handle "direct-declarator [ type-qual-list[opt] * ]".
4842 bool isStar = false;
John McCall60d7b3a2010-08-24 06:29:42 +00004843 ExprResult NumElements;
Mike Stump1eb44332009-09-09 15:08:12 +00004844
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004845 // Handle the case where we have '[*]' as the array size. However, a leading
4846 // star could be the start of an expression, for example 'X[*p + 4]'. Verify
4847 // the the token after the star is a ']'. Since stars in arrays are
4848 // infrequent, use of lookahead is not costly here.
4849 if (Tok.is(tok::star) && GetLookAheadToken(1).is(tok::r_square)) {
Chris Lattnera711dd02008-04-06 05:27:21 +00004850 ConsumeToken(); // Eat the '*'.
Reid Spencer5f016e22007-07-11 17:01:13 +00004851
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004852 if (StaticLoc.isValid()) {
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004853 Diag(StaticLoc, diag::err_unspecified_vla_size_with_static);
Chris Lattnera1fcbad2008-12-18 06:50:14 +00004854 StaticLoc = SourceLocation(); // Drop the static.
4855 }
Chris Lattner5dcc6ce2008-04-06 05:26:30 +00004856 isStar = true;
Chris Lattner04d66662007-10-09 17:33:22 +00004857 } else if (Tok.isNot(tok::r_square)) {
Chris Lattner378c7e42008-12-18 07:27:21 +00004858 // Note, in C89, this production uses the constant-expr production instead
4859 // of assignment-expr. The only difference is that assignment-expr allows
4860 // things like '=' and '*='. Sema rejects these in C89 mode because they
4861 // are not i-c-e's, so we don't need to distinguish between the two here.
Mike Stump1eb44332009-09-09 15:08:12 +00004862
Douglas Gregore0762c92009-06-19 23:52:42 +00004863 // Parse the constant-expression or assignment-expression now (depending
4864 // on dialect).
David Blaikie4e4d0842012-03-11 07:00:24 +00004865 if (getLangOpts().CPlusPlus) {
Douglas Gregore0762c92009-06-19 23:52:42 +00004866 NumElements = ParseConstantExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004867 } else {
4868 EnterExpressionEvaluationContext Unevaluated(Actions,
4869 Sema::ConstantEvaluated);
Douglas Gregore0762c92009-06-19 23:52:42 +00004870 NumElements = ParseAssignmentExpression();
Eli Friedman71b8fb52012-01-21 01:01:51 +00004871 }
Reid Spencer5f016e22007-07-11 17:01:13 +00004872 }
Mike Stump1eb44332009-09-09 15:08:12 +00004873
Reid Spencer5f016e22007-07-11 17:01:13 +00004874 // If there was an error parsing the assignment-expression, recover.
Sebastian Redl0e9eabc2008-12-09 13:15:23 +00004875 if (NumElements.isInvalid()) {
Chris Lattner5cb10d32009-04-24 22:30:50 +00004876 D.setInvalidType(true);
Reid Spencer5f016e22007-07-11 17:01:13 +00004877 // If the expression was invalid, skip it.
4878 SkipUntil(tok::r_square);
4879 return;
4880 }
Sebastian Redlab197ba2009-02-09 18:23:29 +00004881
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004882 T.consumeClose();
Sebastian Redlab197ba2009-02-09 18:23:29 +00004883
John McCall0b7e6782011-03-24 11:26:52 +00004884 ParsedAttributes attrs(AttrFactory);
John McCall7f040a92010-12-24 02:08:15 +00004885 MaybeParseCXX0XAttributes(attrs);
Sean Huntbbd37c62009-11-21 08:43:09 +00004886
Chris Lattner378c7e42008-12-18 07:27:21 +00004887 // Remember that we parsed a array type, and remember its features.
John McCall0b7e6782011-03-24 11:26:52 +00004888 D.AddTypeInfo(DeclaratorChunk::getArray(DS.getTypeQualifiers(),
Reid Spencer5f016e22007-07-11 17:01:13 +00004889 StaticLoc.isValid(), isStar,
Douglas Gregor7e7eb3d2009-07-06 15:59:29 +00004890 NumElements.release(),
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004891 T.getOpenLocation(),
4892 T.getCloseLocation()),
4893 attrs, T.getCloseLocation());
Reid Spencer5f016e22007-07-11 17:01:13 +00004894}
4895
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004896/// [GNU] typeof-specifier:
4897/// typeof ( expressions )
4898/// typeof ( type-name )
4899/// [GNU/C++] typeof unary-expression
Steve Naroffd1861fd2007-07-31 12:34:36 +00004900///
4901void Parser::ParseTypeofSpecifier(DeclSpec &DS) {
Chris Lattner04d66662007-10-09 17:33:22 +00004902 assert(Tok.is(tok::kw_typeof) && "Not a typeof specifier");
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004903 Token OpTok = Tok;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004904 SourceLocation StartLoc = ConsumeToken();
4905
John McCallcfb708c2010-01-13 20:03:27 +00004906 const bool hasParens = Tok.is(tok::l_paren);
4907
Eli Friedman71b8fb52012-01-21 01:01:51 +00004908 EnterExpressionEvaluationContext Unevaluated(Actions, Sema::Unevaluated);
4909
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004910 bool isCastExpr;
John McCallb3d87482010-08-24 05:47:05 +00004911 ParsedType CastTy;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004912 SourceRange CastRange;
Peter Collingbournef4e3cfb2011-03-11 19:24:49 +00004913 ExprResult Operand = ParseExprAfterUnaryExprOrTypeTrait(OpTok, isCastExpr,
4914 CastTy, CastRange);
John McCallcfb708c2010-01-13 20:03:27 +00004915 if (hasParens)
4916 DS.setTypeofParensRange(CastRange);
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004917
4918 if (CastRange.getEnd().isInvalid())
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004919 // FIXME: Not accurate, the range gets one token more than it should.
4920 DS.SetRangeEnd(Tok.getLocation());
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004921 else
4922 DS.SetRangeEnd(CastRange.getEnd());
Mike Stump1eb44332009-09-09 15:08:12 +00004923
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004924 if (isCastExpr) {
4925 if (!CastTy) {
4926 DS.SetTypeSpecError();
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004927 return;
Douglas Gregor809070a2009-02-18 17:45:20 +00004928 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004929
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004930 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004931 unsigned DiagID;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004932 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4933 if (DS.SetTypeSpecType(DeclSpec::TST_typeofType, StartLoc, PrevSpec,
John McCallfec54012009-08-03 20:12:06 +00004934 DiagID, CastTy))
4935 Diag(StartLoc, DiagID) << PrevSpec;
Argyrios Kyrtzidis5ab06402009-05-22 10:22:50 +00004936 return;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004937 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004938
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004939 // If we get here, the operand to the typeof was an expresion.
4940 if (Operand.isInvalid()) {
4941 DS.SetTypeSpecError();
Steve Naroff9dfa7b42007-08-02 02:53:48 +00004942 return;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004943 }
Argyrios Kyrtzidis0f072032008-09-05 11:26:19 +00004944
Eli Friedman71b8fb52012-01-21 01:01:51 +00004945 // We might need to transform the operand if it is potentially evaluated.
4946 Operand = Actions.HandleExprEvaluationContextForTypeof(Operand.get());
4947 if (Operand.isInvalid()) {
4948 DS.SetTypeSpecError();
4949 return;
4950 }
4951
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004952 const char *PrevSpec = 0;
John McCallfec54012009-08-03 20:12:06 +00004953 unsigned DiagID;
Argyrios Kyrtzidis64096252009-05-22 10:22:18 +00004954 // Check for duplicate type specifiers (e.g. "int typeof(int)").
4955 if (DS.SetTypeSpecType(DeclSpec::TST_typeofExpr, StartLoc, PrevSpec,
John McCallb3d87482010-08-24 05:47:05 +00004956 DiagID, Operand.get()))
John McCallfec54012009-08-03 20:12:06 +00004957 Diag(StartLoc, DiagID) << PrevSpec;
Steve Naroffd1861fd2007-07-31 12:34:36 +00004958}
Chris Lattner1b492422010-02-28 18:33:55 +00004959
Benjamin Kramerffbe9b92011-12-23 17:00:35 +00004960/// [C11] atomic-specifier:
Eli Friedmanb001de72011-10-06 23:00:33 +00004961/// _Atomic ( type-name )
4962///
4963void Parser::ParseAtomicSpecifier(DeclSpec &DS) {
4964 assert(Tok.is(tok::kw__Atomic) && "Not an atomic specifier");
4965
4966 SourceLocation StartLoc = ConsumeToken();
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004967 BalancedDelimiterTracker T(*this, tok::l_paren);
4968 if (T.expectAndConsume(diag::err_expected_lparen_after, "_Atomic")) {
Eli Friedmanb001de72011-10-06 23:00:33 +00004969 SkipUntil(tok::r_paren);
4970 return;
4971 }
4972
4973 TypeResult Result = ParseTypeName();
4974 if (Result.isInvalid()) {
4975 SkipUntil(tok::r_paren);
4976 return;
4977 }
4978
4979 // Match the ')'
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004980 T.consumeClose();
Eli Friedmanb001de72011-10-06 23:00:33 +00004981
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004982 if (T.getCloseLocation().isInvalid())
Eli Friedmanb001de72011-10-06 23:00:33 +00004983 return;
4984
Douglas Gregor4a8dfb52011-10-12 16:37:45 +00004985 DS.setTypeofParensRange(T.getRange());
4986 DS.SetRangeEnd(T.getCloseLocation());
Eli Friedmanb001de72011-10-06 23:00:33 +00004987
4988 const char *PrevSpec = 0;
4989 unsigned DiagID;
4990 if (DS.SetTypeSpecType(DeclSpec::TST_atomic, StartLoc, PrevSpec,
4991 DiagID, Result.release()))
4992 Diag(StartLoc, DiagID) << PrevSpec;
4993}
4994
Chris Lattner1b492422010-02-28 18:33:55 +00004995
4996/// TryAltiVecVectorTokenOutOfLine - Out of line body that should only be called
4997/// from TryAltiVecVectorToken.
4998bool Parser::TryAltiVecVectorTokenOutOfLine() {
4999 Token Next = NextToken();
5000 switch (Next.getKind()) {
5001 default: return false;
5002 case tok::kw_short:
5003 case tok::kw_long:
5004 case tok::kw_signed:
5005 case tok::kw_unsigned:
5006 case tok::kw_void:
5007 case tok::kw_char:
5008 case tok::kw_int:
5009 case tok::kw_float:
5010 case tok::kw_double:
5011 case tok::kw_bool:
5012 case tok::kw___pixel:
5013 Tok.setKind(tok::kw___vector);
5014 return true;
5015 case tok::identifier:
5016 if (Next.getIdentifierInfo() == Ident_pixel) {
5017 Tok.setKind(tok::kw___vector);
5018 return true;
5019 }
5020 return false;
5021 }
5022}
5023
5024bool Parser::TryAltiVecTokenOutOfLine(DeclSpec &DS, SourceLocation Loc,
5025 const char *&PrevSpec, unsigned &DiagID,
5026 bool &isInvalid) {
5027 if (Tok.getIdentifierInfo() == Ident_vector) {
5028 Token Next = NextToken();
5029 switch (Next.getKind()) {
5030 case tok::kw_short:
5031 case tok::kw_long:
5032 case tok::kw_signed:
5033 case tok::kw_unsigned:
5034 case tok::kw_void:
5035 case tok::kw_char:
5036 case tok::kw_int:
5037 case tok::kw_float:
5038 case tok::kw_double:
5039 case tok::kw_bool:
5040 case tok::kw___pixel:
5041 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5042 return true;
5043 case tok::identifier:
5044 if (Next.getIdentifierInfo() == Ident_pixel) {
5045 isInvalid = DS.SetTypeAltiVecVector(true, Loc, PrevSpec, DiagID);
5046 return true;
5047 }
5048 break;
5049 default:
5050 break;
5051 }
Douglas Gregora8f031f2010-06-16 15:28:57 +00005052 } else if ((Tok.getIdentifierInfo() == Ident_pixel) &&
Chris Lattner1b492422010-02-28 18:33:55 +00005053 DS.isTypeAltiVecVector()) {
5054 isInvalid = DS.SetTypeAltiVecPixel(true, Loc, PrevSpec, DiagID);
5055 return true;
5056 }
5057 return false;
5058}